]> nv-tegra.nvidia Code Review - linux-2.6.git/blobdiff - ipc/shm.c
[PATCH] Fix the kernel panic of audit_filter_task when key field is set
[linux-2.6.git] / ipc / shm.c
index 65a44bcc4ac2f780e180e1a095caa5f9618489b0..e77ec698cf408c2344d302ac693814d44d411180 100644 (file)
--- a/ipc/shm.c
+++ b/ipc/shm.c
@@ -111,36 +111,9 @@ void __init shm_init (void)
                                IPC_SHM_IDS, sysvipc_shm_proc_show);
 }
 
-/*
- * shm_lock_(check_)down routines are called in the paths where the rw_mutex
- * is held to protect access to the idr tree.
- */
-static inline struct shmid_kernel *shm_lock_down(struct ipc_namespace *ns,
-                                               int id)
-{
-       struct kern_ipc_perm *ipcp = ipc_lock_down(&shm_ids(ns), id);
-
-       if (IS_ERR(ipcp))
-               return (struct shmid_kernel *)ipcp;
-
-       return container_of(ipcp, struct shmid_kernel, shm_perm);
-}
-
-static inline struct shmid_kernel *shm_lock_check_down(
-                                               struct ipc_namespace *ns,
-                                               int id)
-{
-       struct kern_ipc_perm *ipcp = ipc_lock_check_down(&shm_ids(ns), id);
-
-       if (IS_ERR(ipcp))
-               return (struct shmid_kernel *)ipcp;
-
-       return container_of(ipcp, struct shmid_kernel, shm_perm);
-}
-
 /*
  * shm_lock_(check_) routines are called in the paths where the rw_mutex
- * is not held.
+ * is not necessarily held.
  */
 static inline struct shmid_kernel *shm_lock(struct ipc_namespace *ns, int id)
 {
@@ -223,7 +196,7 @@ static void shm_close(struct vm_area_struct *vma)
 
        down_write(&shm_ids(ns).rw_mutex);
        /* remove from the list of attaches of the shm segment */
-       shp = shm_lock_down(ns, sfd->id);
+       shp = shm_lock(ns, sfd->id);
        BUG_ON(IS_ERR(shp));
        shp->shm_lprid = task_tgid_vnr(current);
        shp->shm_dtim = get_seconds();
@@ -511,28 +484,14 @@ static inline unsigned long copy_shmid_to_user(void __user *buf, struct shmid64_
        }
 }
 
-struct shm_setbuf {
-       uid_t   uid;
-       gid_t   gid;
-       mode_t  mode;
-};     
-
-static inline unsigned long copy_shmid_from_user(struct shm_setbuf *out, void __user *buf, int version)
+static inline unsigned long
+copy_shmid_from_user(struct shmid64_ds *out, void __user *buf, int version)
 {
        switch(version) {
        case IPC_64:
-           {
-               struct shmid64_ds tbuf;
-
-               if (copy_from_user(&tbuf, buf, sizeof(tbuf)))
+               if (copy_from_user(out, buf, sizeof(*out)))
                        return -EFAULT;
-
-               out->uid        = tbuf.shm_perm.uid;
-               out->gid        = tbuf.shm_perm.gid;
-               out->mode       = tbuf.shm_perm.mode;
-
                return 0;
-           }
        case IPC_OLD:
            {
                struct shmid_ds tbuf_old;
@@ -540,9 +499,9 @@ static inline unsigned long copy_shmid_from_user(struct shm_setbuf *out, void __
                if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
                        return -EFAULT;
 
-               out->uid        = tbuf_old.shm_perm.uid;
-               out->gid        = tbuf_old.shm_perm.gid;
-               out->mode       = tbuf_old.shm_perm.mode;
+               out->shm_perm.uid       = tbuf_old.shm_perm.uid;
+               out->shm_perm.gid       = tbuf_old.shm_perm.gid;
+               out->shm_perm.mode      = tbuf_old.shm_perm.mode;
 
                return 0;
            }
@@ -603,7 +562,8 @@ static void shm_get_stat(struct ipc_namespace *ns, unsigned long *rss,
 
                if (is_file_hugepages(shp->shm_file)) {
                        struct address_space *mapping = inode->i_mapping;
-                       *rss += (HPAGE_SIZE/PAGE_SIZE)*mapping->nrpages;
+                       struct hstate *h = hstate_file(shp->shm_file);
+                       *rss += pages_per_huge_page(h) * mapping->nrpages;
                } else {
                        struct shmem_inode_info *info = SHMEM_I(inode);
                        spin_lock(&info->lock);
@@ -625,41 +585,20 @@ static int shmctl_down(struct ipc_namespace *ns, int shmid, int cmd,
                       struct shmid_ds __user *buf, int version)
 {
        struct kern_ipc_perm *ipcp;
-       struct shm_setbuf setbuf;
+       struct shmid64_ds shmid64;
        struct shmid_kernel *shp;
        int err;
 
        if (cmd == IPC_SET) {
-               if (copy_shmid_from_user(&setbuf, buf, version))
+               if (copy_shmid_from_user(&shmid64, buf, version))
                        return -EFAULT;
        }
 
-       down_write(&shm_ids(ns).rw_mutex);
-       shp = shm_lock_check_down(ns, shmid);
-       if (IS_ERR(shp)) {
-               err = PTR_ERR(shp);
-               goto out_up;
-       }
-
-       ipcp = &shp->shm_perm;
-
-       err = audit_ipc_obj(ipcp);
-       if (err)
-               goto out_unlock;
-
-       if (cmd == IPC_SET) {
-               err = audit_ipc_set_perm(0, setbuf.uid,
-                                        setbuf.gid, setbuf.mode);
-               if (err)
-                       goto out_unlock;
-       }
+       ipcp = ipcctl_pre_down(&shm_ids(ns), shmid, cmd, &shmid64.shm_perm, 0);
+       if (IS_ERR(ipcp))
+               return PTR_ERR(ipcp);
 
-       if (current->euid != ipcp->uid &&
-           current->euid != ipcp->cuid &&
-           !capable(CAP_SYS_ADMIN)) {
-               err = -EPERM;
-               goto out_unlock;
-       }
+       shp = container_of(ipcp, struct shmid_kernel, shm_perm);
 
        err = security_shm_shmctl(shp, cmd);
        if (err)
@@ -669,10 +608,7 @@ static int shmctl_down(struct ipc_namespace *ns, int shmid, int cmd,
                do_shm_rmid(ns, ipcp);
                goto out_up;
        case IPC_SET:
-               ipcp->uid = setbuf.uid;
-               ipcp->gid = setbuf.gid;
-               ipcp->mode = (ipcp->mode & ~S_IRWXUGO)
-                       | (setbuf.mode & S_IRWXUGO);
+               ipc_update_perm(&shmid64.shm_perm, ipcp);
                shp->shm_ctim = get_seconds();
                break;
        default:
@@ -944,8 +880,6 @@ long do_shmat(int shmid, char __user *shmaddr, int shmflg, ulong *raddr)
        if (!sfd)
                goto out_put_dentry;
 
-       err = -ENOMEM;
-
        file = alloc_file(path.mnt, path.dentry, f_mode, &shm_file_operations);
        if (!file)
                goto out_free;
@@ -983,7 +917,7 @@ invalid:
 
 out_nattch:
        down_write(&shm_ids(ns).rw_mutex);
-       shp = shm_lock_down(ns, shmid);
+       shp = shm_lock(ns, shmid);
        BUG_ON(IS_ERR(shp));
        shp->shm_nattch--;
        if(shp->shm_nattch == 0 &&
@@ -1110,16 +1044,16 @@ asmlinkage long sys_shmdt(char __user *shmaddr)
 static int sysvipc_shm_proc_show(struct seq_file *s, void *it)
 {
        struct shmid_kernel *shp = it;
-       char *format;
 
-#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"
+#if BITS_PER_LONG <= 32
+#define SIZE_SPEC "%10lu"
+#else
+#define SIZE_SPEC "%21lu"
+#endif
 
-       if (sizeof(size_t) <= sizeof(int))
-               format = SMALL_STRING;
-       else
-               format = BIG_STRING;
-       return seq_printf(s, format,
+       return seq_printf(s,
+                         "%10d %10d  %4o " SIZE_SPEC " %5u %5u  "
+                         "%5lu %5u %5u %5u %5u %10lu %10lu %10lu\n",
                          shp->shm_perm.key,
                          shp->shm_perm.id,
                          shp->shm_perm.mode,