Fedora kernel-2.6.17-1.2142_FC4 patched with stable patch-2.6.17.4-vs2.0.2-rc26.diff
[linux-2.6.git] / ipc / shm.c
index 07fb859..0d14fb9 100644 (file)
--- a/ipc/shm.c
+++ b/ipc/shm.c
@@ -13,6 +13,8 @@
  * Shared /dev/zero support, Kanoj Sarcar <kanoj@sgi.com>
  * Move the mm functionality over to mm/shmem.c, Christoph Rohland <cr@sap.com>
  *
+ * support for audit of ipc object properties and permission changes
+ * Dustin Kirkland <dustin.kirkland@us.ibm.com>
  */
 
 #include <linux/config.h>
 #include <linux/init.h>
 #include <linux/file.h>
 #include <linux/mman.h>
-#include <linux/proc_fs.h>
 #include <linux/shmem_fs.h>
 #include <linux/security.h>
-#include <linux/vs_base.h>
+#include <linux/syscalls.h>
+#include <linux/audit.h>
+#include <linux/capability.h>
+#include <linux/ptrace.h>
+#include <linux/seq_file.h>
+#include <linux/mutex.h>
+#include <linux/vs_context.h>
+#include <linux/vs_limit.h>
 
 #include <asm/uaccess.h>
 
 #include "util.h"
 
-#define shm_flags      shm_perm.mode
-
 static struct file_operations shm_file_operations;
 static struct vm_operations_struct shm_vm_ops;
 
@@ -49,7 +55,7 @@ static int newseg (key_t key, int shmflg, size_t size);
 static void shm_open (struct vm_area_struct *shmd);
 static void shm_close (struct vm_area_struct *shmd);
 #ifdef CONFIG_PROC_FS
-static int sysvipc_shm_read_proc(char *buffer, char **start, off_t offset, int length, int *eof, void *data);
+static int sysvipc_shm_proc_show(struct seq_file *s, void *it);
 #endif
 
 size_t shm_ctlmax = SHMMAX;
@@ -61,9 +67,10 @@ static int shm_tot; /* total number of shared memory pages */
 void __init shm_init (void)
 {
        ipc_init_ids(&shm_ids, 1);
-#ifdef CONFIG_PROC_FS
-       create_proc_read_entry("sysvipc/shm", 0, 0, sysvipc_shm_read_proc, NULL);
-#endif
+       ipc_init_proc_interface("sysvipc/shm",
+                               "       key      shmid perms       size  cpid  lpid nattch   uid   gid  cuid  cgid      atime      dtime      ctime\n",
+                               &shm_ids,
+                               sysvipc_shm_proc_show);
 }
 
 static inline int shm_checkid(struct shmid_kernel *s, int id)
@@ -80,7 +87,7 @@ static inline struct shmid_kernel *shm_rmid(int id)
 
 static inline int shm_addid(struct shmid_kernel *shp)
 {
-       return ipc_addid(&shm_ids, &shp->shm_perm, shm_ctlmni+1);
+       return ipc_addid(&shm_ids, &shp->shm_perm, shm_ctlmni);
 }
 
 
@@ -88,8 +95,8 @@ static inline int shm_addid(struct shmid_kernel *shp)
 static inline void shm_inc (int id) {
        struct shmid_kernel *shp;
 
-       if(!(shp = shm_lock(id)))
-               BUG();
+       shp = shm_lock(id);
+       BUG_ON(!shp);
        shp->shm_atim = get_seconds();
        shp->shm_lprid = current->tgid;
        shp->shm_nattch++;
@@ -107,19 +114,28 @@ static void shm_open (struct vm_area_struct *shmd)
  *
  * @shp: struct to free
  *
- * It has to be called with shp and shm_ids.sem locked,
+ * It has to be called with shp and shm_ids.mutex locked,
  * but returns with shp unlocked and freed.
  */
 static void shm_destroy (struct shmid_kernel *shp)
 {
-       shm_tot -= (shp->shm_segsz + PAGE_SIZE - 1) >> PAGE_SHIFT;
+       struct vx_info *vxi = lookup_vx_info(shp->shm_perm.xid);
+       int numpages = (shp->shm_segsz + PAGE_SIZE - 1) >> PAGE_SHIFT;
+
+       vx_ipcshm_sub(vxi, shp, numpages);
+       shm_tot -= numpages;
+
        shm_rmid (shp->id);
        shm_unlock(shp);
        if (!is_file_hugepages(shp->shm_file))
-               shmem_lock(shp->shm_file, 0);
+               shmem_lock(shp->shm_file, 0, shp->mlock_user);
+       else
+               user_shm_unlock(shp->shm_file->f_dentry->d_inode->i_size,
+                                               shp->mlock_user);
        fput (shp->shm_file);
        security_shm_free(shp);
-       ipc_rcu_free(shp, sizeof(struct shmid_kernel));
+       put_vx_info(vxi);
+       ipc_rcu_putref(shp);
 }
 
 /*
@@ -134,38 +150,48 @@ static void shm_close (struct vm_area_struct *shmd)
        int id = file->f_dentry->d_inode->i_ino;
        struct shmid_kernel *shp;
 
-       down (&shm_ids.sem);
+       mutex_lock(&shm_ids.mutex);
        /* remove from the list of attaches of the shm segment */
-       if(!(shp = shm_lock(id)))
-               BUG();
+       shp = shm_lock(id);
+       BUG_ON(!shp);
        shp->shm_lprid = current->tgid;
        shp->shm_dtim = get_seconds();
        shp->shm_nattch--;
        if(shp->shm_nattch == 0 &&
-          shp->shm_flags & SHM_DEST)
+          shp->shm_perm.mode & SHM_DEST)
                shm_destroy (shp);
        else
                shm_unlock(shp);
-       up (&shm_ids.sem);
+       mutex_unlock(&shm_ids.mutex);
 }
 
 static int shm_mmap(struct file * file, struct vm_area_struct * vma)
 {
-       file_accessed(file);
-       vma->vm_ops = &shm_vm_ops;
-       shm_inc(file->f_dentry->d_inode->i_ino);
-       return 0;
+       int ret;
+
+       ret = shmem_mmap(file, vma);
+       if (ret == 0) {
+               vma->vm_ops = &shm_vm_ops;
+               if (!(vma->vm_flags & VM_WRITE))
+                       vma->vm_flags &= ~VM_MAYWRITE;
+               shm_inc(file->f_dentry->d_inode->i_ino);
+       }
+
+       return ret;
 }
 
 static struct file_operations shm_file_operations = {
-       .mmap   = shm_mmap
+       .mmap   = shm_mmap,
+#ifndef CONFIG_MMU
+       .get_unmapped_area = shmem_get_unmapped_area,
+#endif
 };
 
 static struct vm_operations_struct shm_vm_ops = {
        .open   = shm_open,     /* callback for a new vm-area open */
        .close  = shm_close,    /* callback for when the vm-area is released */
        .nopage = shmem_nopage,
-#ifdef CONFIG_NUMA
+#if defined(CONFIG_NUMA) && defined(CONFIG_SHMEM)
        .set_policy = shmem_set_policy,
        .get_policy = shmem_get_policy,
 #endif
@@ -185,27 +211,40 @@ static int newseg (key_t key, int shmflg, size_t size)
 
        if (shm_tot + numpages >= shm_ctlall)
                return -ENOSPC;
+       if (!vx_ipcshm_avail(current->vx_info, numpages))
+               return -ENOSPC;
 
        shp = ipc_rcu_alloc(sizeof(*shp));
        if (!shp)
                return -ENOMEM;
 
        shp->shm_perm.key = key;
-       shp->shm_perm.xid = current->xid;
-       shp->shm_flags = (shmflg & S_IRWXUGO);
+       shp->shm_perm.xid = vx_current_xid();
+       shp->shm_perm.mode = (shmflg & S_IRWXUGO);
+       shp->mlock_user = NULL;
 
        shp->shm_perm.security = NULL;
        error = security_shm_alloc(shp);
        if (error) {
-               ipc_rcu_free(shp, sizeof(*shp));
+               ipc_rcu_putref(shp);
                return error;
        }
 
-       if (shmflg & SHM_HUGETLB)
+       if (shmflg & SHM_HUGETLB) {
+               /* hugetlb_zero_setup takes care of mlock user accounting */
                file = hugetlb_zero_setup(size);
-       else {
+               shp->mlock_user = current->user;
+       } else {
+               int acctflag = VM_ACCOUNT;
+               /*
+                * Do not allow no accounting for OVERCOMMIT_NEVER, even
+                * if it's asked for.
+                */
+               if  ((shmflg & SHM_NORESERVE) &&
+                               sysctl_overcommit_memory != OVERCOMMIT_NEVER)
+                       acctflag = 0;
                sprintf (name, "SYSV%08x", key);
-               file = shmem_file_setup(name, size, VM_ACCOUNT);
+               file = shmem_file_setup(name, size, acctflag);
        }
        error = PTR_ERR(file);
        if (IS_ERR(file))
@@ -225,11 +264,13 @@ static int newseg (key_t key, int shmflg, size_t size)
        shp->id = shm_buildid(id,shp->shm_perm.seq);
        shp->shm_file = file;
        file->f_dentry->d_inode->i_ino = shp->id;
-       if (shmflg & SHM_HUGETLB)
-               set_file_hugepages(file);
-       else
+
+       /* Hugetlb ops would have already been assigned. */
+       if (!(shmflg & SHM_HUGETLB))
                file->f_op = &shm_file_operations;
+
        shm_tot += numpages;
+       vx_ipcshm_add(current->vx_info, key, numpages);
        shm_unlock(shp);
        return shp->id;
 
@@ -237,7 +278,7 @@ no_id:
        fput(file);
 no_file:
        security_shm_free(shp);
-       ipc_rcu_free(shp, sizeof(*shp));
+       ipc_rcu_putref(shp);
        return error;
 }
 
@@ -246,7 +287,7 @@ asmlinkage long sys_shmget (key_t key, size_t size, int shmflg)
        struct shmid_kernel *shp;
        int err, id = 0;
 
-       down(&shm_ids.sem);
+       mutex_lock(&shm_ids.mutex);
        if (key == IPC_PRIVATE) {
                err = newseg(key, shmflg, size);
        } else if ((id = ipc_findkey(&shm_ids, key)) == -1) {
@@ -258,8 +299,7 @@ asmlinkage long sys_shmget (key_t key, size_t size, int shmflg)
                err = -EEXIST;
        } else {
                shp = shm_lock(id);
-               if(shp==NULL)
-                       BUG();
+               BUG_ON(shp==NULL);
                if (shp->shm_segsz < size)
                        err = -EINVAL;
                else if (ipcperms(&shp->shm_perm, shmflg))
@@ -272,7 +312,7 @@ asmlinkage long sys_shmget (key_t key, size_t size, int shmflg)
                }
                shm_unlock(shp);
        }
-       up(&shm_ids.sem);
+       mutex_unlock(&shm_ids.mutex);
 
        return err;
 }
@@ -320,7 +360,7 @@ static inline unsigned long copy_shmid_from_user(struct shm_setbuf *out, void __
 
                out->uid        = tbuf.shm_perm.uid;
                out->gid        = tbuf.shm_perm.gid;
-               out->mode       = tbuf.shm_flags;
+               out->mode       = tbuf.shm_perm.mode;
 
                return 0;
            }
@@ -333,7 +373,7 @@ static inline unsigned long copy_shmid_from_user(struct shm_setbuf *out, void __
 
                out->uid        = tbuf_old.shm_perm.uid;
                out->gid        = tbuf_old.shm_perm.gid;
-               out->mode       = tbuf_old.shm_flags;
+               out->mode       = tbuf_old.shm_perm.mode;
 
                return 0;
            }
@@ -443,14 +483,14 @@ asmlinkage long sys_shmctl (int shmid, int cmd, struct shmid_ds __user *buf)
                        return err;
 
                memset(&shm_info,0,sizeof(shm_info));
-               down(&shm_ids.sem);
+               mutex_lock(&shm_ids.mutex);
                shm_info.used_ids = shm_ids.in_use;
                shm_get_stat (&shm_info.shm_rss, &shm_info.shm_swp);
                shm_info.shm_tot = shm_tot;
                shm_info.swap_attempts = 0;
                shm_info.swap_successes = 0;
                err = shm_ids.max_id;
-               up(&shm_ids.sem);
+               mutex_unlock(&shm_ids.mutex);
                if(copy_to_user (buf, &shm_info, sizeof(shm_info))) {
                        err = -EFAULT;
                        goto out;
@@ -507,14 +547,6 @@ asmlinkage long sys_shmctl (int shmid, int cmd, struct shmid_ds __user *buf)
        case SHM_LOCK:
        case SHM_UNLOCK:
        {
-/* Allow superuser to lock segment in memory */
-/* Should the pages be faulted in here or leave it to user? */
-/* need to determine interaction with current->swappable */
-               if (!capable(CAP_IPC_LOCK)) {
-                       err = -EPERM;
-                       goto out;
-               }
-
                shp = shm_lock(shmid);
                if(shp==NULL) {
                        err = -EINVAL;
@@ -524,18 +556,37 @@ asmlinkage long sys_shmctl (int shmid, int cmd, struct shmid_ds __user *buf)
                if(err)
                        goto out_unlock;
 
+               err = audit_ipc_obj(&(shp->shm_perm));
+               if (err)
+                       goto out_unlock;
+
+               if (!capable(CAP_IPC_LOCK)) {
+                       err = -EPERM;
+                       if (current->euid != shp->shm_perm.uid &&
+                           current->euid != shp->shm_perm.cuid)
+                               goto out_unlock;
+                       if (cmd == SHM_LOCK &&
+                           !current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur)
+                               goto out_unlock;
+               }
+
                err = security_shm_shmctl(shp, cmd);
                if (err)
                        goto out_unlock;
                
                if(cmd==SHM_LOCK) {
-                       if (!is_file_hugepages(shp->shm_file))
-                               shmem_lock(shp->shm_file, 1);
-                       shp->shm_flags |= SHM_LOCKED;
-               } else {
-                       if (!is_file_hugepages(shp->shm_file))
-                               shmem_lock(shp->shm_file, 0);
-                       shp->shm_flags &= ~SHM_LOCKED;
+                       struct user_struct * user = current->user;
+                       if (!is_file_hugepages(shp->shm_file)) {
+                               err = shmem_lock(shp->shm_file, 1, user);
+                               if (!err) {
+                                       shp->shm_perm.mode |= SHM_LOCKED;
+                                       shp->mlock_user = user;
+                               }
+                       }
+               } else if (!is_file_hugepages(shp->shm_file)) {
+                       shmem_lock(shp->shm_file, 0, shp->mlock_user);
+                       shp->shm_perm.mode &= ~SHM_LOCKED;
+                       shp->mlock_user = NULL;
                }
                shm_unlock(shp);
                goto out;
@@ -552,7 +603,7 @@ asmlinkage long sys_shmctl (int shmid, int cmd, struct shmid_ds __user *buf)
                 *      Instead we set a destroyed flag, and then blow
                 *      the name away when the usage hits zero.
                 */
-               down(&shm_ids.sem);
+               mutex_lock(&shm_ids.mutex);
                shp = shm_lock(shmid);
                err = -EINVAL;
                if (shp == NULL) 
@@ -561,6 +612,10 @@ asmlinkage long sys_shmctl (int shmid, int cmd, struct shmid_ds __user *buf)
                if(err)
                        goto out_unlock_up;
 
+               err = audit_ipc_obj(&(shp->shm_perm));
+               if (err)
+                       goto out_unlock_up;
+
                if (current->euid != shp->shm_perm.uid &&
                    current->euid != shp->shm_perm.cuid && 
                    !capable(CAP_SYS_ADMIN)) {
@@ -573,13 +628,13 @@ asmlinkage long sys_shmctl (int shmid, int cmd, struct shmid_ds __user *buf)
                        goto out_unlock_up;
 
                if (shp->shm_nattch){
-                       shp->shm_flags |= SHM_DEST;
+                       shp->shm_perm.mode |= SHM_DEST;
                        /* Do not find it any more */
                        shp->shm_perm.key = IPC_PRIVATE;
                        shm_unlock(shp);
                } else
                        shm_destroy (shp);
-               up(&shm_ids.sem);
+               mutex_unlock(&shm_ids.mutex);
                goto out;
        }
 
@@ -589,7 +644,7 @@ asmlinkage long sys_shmctl (int shmid, int cmd, struct shmid_ds __user *buf)
                        err = -EFAULT;
                        goto out;
                }
-               down(&shm_ids.sem);
+               mutex_lock(&shm_ids.mutex);
                shp = shm_lock(shmid);
                err=-EINVAL;
                if(shp==NULL)
@@ -597,6 +652,12 @@ asmlinkage long sys_shmctl (int shmid, int cmd, struct shmid_ds __user *buf)
                err = shm_checkid(shp,shmid);
                if(err)
                        goto out_unlock_up;
+               err = audit_ipc_obj(&(shp->shm_perm));
+               if (err)
+                       goto out_unlock_up;
+               err = audit_ipc_set_perm(0, setbuf.uid, setbuf.gid, setbuf.mode, &(shp->shm_perm));
+               if (err)
+                       goto out_unlock_up;
                err=-EPERM;
                if (current->euid != shp->shm_perm.uid &&
                    current->euid != shp->shm_perm.cuid && 
@@ -610,7 +671,7 @@ asmlinkage long sys_shmctl (int shmid, int cmd, struct shmid_ds __user *buf)
                
                shp->shm_perm.uid = setbuf.uid;
                shp->shm_perm.gid = setbuf.gid;
-               shp->shm_flags = (shp->shm_flags & ~S_IRWXUGO)
+               shp->shm_perm.mode = (shp->shm_perm.mode & ~S_IRWXUGO)
                        | (setbuf.mode & S_IRWXUGO);
                shp->shm_ctim = get_seconds();
                break;
@@ -625,7 +686,7 @@ asmlinkage long sys_shmctl (int shmid, int cmd, struct shmid_ds __user *buf)
 out_unlock_up:
        shm_unlock(shp);
 out_up:
-       up(&shm_ids.sem);
+       mutex_unlock(&shm_ids.mutex);
        goto out;
 out_unlock:
        shm_unlock(shp);
@@ -683,6 +744,10 @@ long do_shmat(int shmid, char __user *shmaddr, int shmflg, ulong *raddr)
                o_flags = O_RDWR;
                acc_mode = S_IRUGO | S_IWUGO;
        }
+       if (shmflg & SHM_EXEC) {
+               prot |= PROT_EXEC;
+               acc_mode |= S_IXUGO;
+       }
 
        /*
         * We cannot rely on the fs check since SYSV IPC does have an
@@ -734,16 +799,16 @@ long do_shmat(int shmid, char __user *shmaddr, int shmflg, ulong *raddr)
 invalid:
        up_write(&current->mm->mmap_sem);
 
-       down (&shm_ids.sem);
-       if(!(shp = shm_lock(shmid)))
-               BUG();
+       mutex_lock(&shm_ids.mutex);
+       shp = shm_lock(shmid);
+       BUG_ON(!shp);
        shp->shm_nattch--;
        if(shp->shm_nattch == 0 &&
-          shp->shm_flags & SHM_DEST)
+          shp->shm_perm.mode & SHM_DEST)
                shm_destroy (shp);
        else
                shm_unlock(shp);
-       up (&shm_ids.sem);
+       mutex_unlock(&shm_ids.mutex);
 
        *raddr = (unsigned long) user_addr;
        err = 0;
@@ -753,6 +818,18 @@ out:
        return err;
 }
 
+asmlinkage long sys_shmat(int shmid, char __user *shmaddr, int shmflg)
+{
+       unsigned long ret;
+       long err;
+
+       err = do_shmat(shmid, shmaddr, shmflg, &ret);
+       if (err)
+               return err;
+       force_successful_syscall_return();
+       return (long)ret;
+}
+
 /*
  * detach and kill segment if marked destroyed.
  * The work is done in shm_close.
@@ -765,6 +842,9 @@ asmlinkage long sys_shmdt(char __user *shmaddr)
        loff_t size = 0;
        int retval = -EINVAL;
 
+       if (addr & ~PAGE_MASK)
+               return retval;
+
        down_write(&mm->mmap_sem);
 
        /*
@@ -821,6 +901,7 @@ asmlinkage long sys_shmdt(char __user *shmaddr)
         * could possibly have landed at. Also cast things to loff_t to
         * prevent overflows and make comparisions vs. equal-width types.
         */
+       size = PAGE_ALIGN(size);
        while (vma && (loff_t)(vma->vm_end - addr) <= size) {
                next = vma->vm_next;
 
@@ -837,67 +918,35 @@ asmlinkage long sys_shmdt(char __user *shmaddr)
 }
 
 #ifdef CONFIG_PROC_FS
-static int sysvipc_shm_read_proc(char *buffer, char **start, off_t offset, int length, int *eof, void *data)
+static int sysvipc_shm_proc_show(struct seq_file *s, void *it)
 {
-       off_t pos = 0;
-       off_t begin = 0;
-       int i, len = 0;
+       struct shmid_kernel *shp = it;
+       char *format;
 
-       down(&shm_ids.sem);
-       len += sprintf(buffer, "       key      shmid perms       size  cpid  lpid nattch   uid   gid  cuid  cgid      atime      dtime      ctime\n");
-
-       for(i = 0; i <= shm_ids.max_id; i++) {
-               struct shmid_kernel* shp;
-
-               shp = shm_lock(i);
-               if (shp) {
 #define SMALL_STRING "%10d %10d  %4o %10u %5u %5u  %5d %5u %5u %5u %5u %10lu %10lu %10lu\n"
 #define BIG_STRING   "%10d %10d  %4o %21u %5u %5u  %5d %5u %5u %5u %5u %10lu %10lu %10lu\n"
-                       char *format;
 
-                       if (!vx_check(shp->shm_perm.xid, VX_IDENT)) {
-                               shm_unlock(shp);
-                               continue;       
-                       }
-                       if (sizeof(size_t) <= sizeof(int))
-                               format = SMALL_STRING;
-                       else
-                               format = BIG_STRING;
-                       len += sprintf(buffer + len, format,
-                               shp->shm_perm.key,
-                               shm_buildid(i, shp->shm_perm.seq),
-                               shp->shm_flags,
-                               shp->shm_segsz,
-                               shp->shm_cprid,
-                               shp->shm_lprid,
-                               is_file_hugepages(shp->shm_file) ? (file_count(shp->shm_file) - 1) : shp->shm_nattch,
-                               shp->shm_perm.uid,
-                               shp->shm_perm.gid,
-                               shp->shm_perm.cuid,
-                               shp->shm_perm.cgid,
-                               shp->shm_atim,
-                               shp->shm_dtim,
-                               shp->shm_ctim);
-                       shm_unlock(shp);
+       if (!vx_check(shp->shm_perm.xid, VX_IDENT))
+               return 0;
 
-                       pos += len;
-                       if(pos < offset) {
-                               len = 0;
-                               begin = pos;
-                       }
-                       if(pos > offset + length)
-                               goto done;
-               }
-       }
-       *eof = 1;
-done:
-       up(&shm_ids.sem);
-       *start = buffer + (offset - begin);
-       len -= (offset - begin);
-       if(len > length)
-               len = length;
-       if(len < 0)
-               len = 0;
-       return len;
+       if (sizeof(size_t) <= sizeof(int))
+               format = SMALL_STRING;
+       else
+               format = BIG_STRING;
+       return seq_printf(s, format,
+                         shp->shm_perm.key,
+                         shp->id,
+                         shp->shm_perm.mode,
+                         shp->shm_segsz,
+                         shp->shm_cprid,
+                         shp->shm_lprid,
+                         is_file_hugepages(shp->shm_file) ? (file_count(shp->shm_file) - 1) : shp->shm_nattch,
+                         shp->shm_perm.uid,
+                         shp->shm_perm.gid,
+                         shp->shm_perm.cuid,
+                         shp->shm_perm.cgid,
+                         shp->shm_atim,
+                         shp->shm_dtim,
+                         shp->shm_ctim);
 }
 #endif