[libata] sata_svw: update code comments relating to data corruption
[safe/jmp/linux-2.6] / security / device_cgroup.c
index 4237b19..ddd92ce 100644 (file)
@@ -9,6 +9,7 @@
 #include <linux/ctype.h>
 #include <linux/list.h>
 #include <linux/uaccess.h>
+#include <linux/seq_file.h>
 
 #define ACC_MKNOD 1
 #define ACC_READ  2
@@ -48,10 +49,14 @@ struct dev_cgroup {
        spinlock_t lock;
 };
 
+static inline struct dev_cgroup *css_to_devcgroup(struct cgroup_subsys_state *s)
+{
+       return container_of(s, struct dev_cgroup, css);
+}
+
 static inline struct dev_cgroup *cgroup_to_devcgroup(struct cgroup *cgroup)
 {
-       return container_of(cgroup_subsys_state(cgroup, devices_subsys_id),
-                           struct dev_cgroup, css);
+       return css_to_devcgroup(cgroup_subsys_state(cgroup, devices_subsys_id));
 }
 
 struct cgroup_subsys devices_subsys;
@@ -101,7 +106,7 @@ free_and_exit:
 static int dev_whitelist_add(struct dev_cgroup *dev_cgroup,
                        struct dev_whitelist_item *wh)
 {
-       struct dev_whitelist_item *whcopy;
+       struct dev_whitelist_item *whcopy, *walk;
 
        whcopy = kmalloc(sizeof(*whcopy), GFP_KERNEL);
        if (!whcopy)
@@ -109,7 +114,21 @@ static int dev_whitelist_add(struct dev_cgroup *dev_cgroup,
 
        memcpy(whcopy, wh, sizeof(*whcopy));
        spin_lock(&dev_cgroup->lock);
-       list_add_tail(&whcopy->list, &dev_cgroup->whitelist);
+       list_for_each_entry(walk, &dev_cgroup->whitelist, list) {
+               if (walk->type != wh->type)
+                       continue;
+               if (walk->major != wh->major)
+                       continue;
+               if (walk->minor != wh->minor)
+                       continue;
+
+               walk->access |= wh->access;
+               kfree(whcopy);
+               whcopy = NULL;
+       }
+
+       if (whcopy != NULL)
+               list_add_tail(&whcopy->list, &dev_cgroup->whitelist);
        spin_unlock(&dev_cgroup->lock);
        return 0;
 }
@@ -201,11 +220,15 @@ static void devcgroup_destroy(struct cgroup_subsys *ss,
 
 #define DEVCG_ALLOW 1
 #define DEVCG_DENY 2
+#define DEVCG_LIST 3
+
+#define MAJMINLEN 13
+#define ACCLEN 4
 
 static void set_access(char *acc, short access)
 {
        int idx = 0;
-       memset(acc, 0, 4);
+       memset(acc, 0, ACCLEN);
        if (access & ACC_READ)
                acc[idx++] = 'r';
        if (access & ACC_WRITE)
@@ -225,70 +248,33 @@ static char type_to_char(short type)
        return 'X';
 }
 
-static void set_majmin(char *str, int len, unsigned m)
+static void set_majmin(char *str, unsigned m)
 {
-       memset(str, 0, len);
+       memset(str, 0, MAJMINLEN);
        if (m == ~0)
                sprintf(str, "*");
        else
-               snprintf(str, len, "%d", m);
+               snprintf(str, MAJMINLEN, "%u", m);
 }
 
-static char *print_whitelist(struct dev_cgroup *devcgroup, int *len)
+static int devcgroup_seq_read(struct cgroup *cgroup, struct cftype *cft,
+                               struct seq_file *m)
 {
-       char *buf, *s, acc[4];
+       struct dev_cgroup *devcgroup = cgroup_to_devcgroup(cgroup);
        struct dev_whitelist_item *wh;
-       int ret;
-       int count = 0;
-       char maj[10], min[10];
-
-       buf = kmalloc(4096, GFP_KERNEL);
-       if (!buf)
-               return ERR_PTR(-ENOMEM);
-       s = buf;
-       *s = '\0';
-       *len = 0;
+       char maj[MAJMINLEN], min[MAJMINLEN], acc[ACCLEN];
 
        spin_lock(&devcgroup->lock);
        list_for_each_entry(wh, &devcgroup->whitelist, list) {
                set_access(acc, wh->access);
-               set_majmin(maj, 10, wh->major);
-               set_majmin(min, 10, wh->minor);
-               ret = snprintf(s, 4095-(s-buf), "%c %s:%s %s\n",
-                       type_to_char(wh->type), maj, min, acc);
-               if (s+ret >= buf+4095) {
-                       kfree(buf);
-                       buf = ERR_PTR(-ENOMEM);
-                       break;
-               }
-               s += ret;
-               *len += ret;
-               count++;
+               set_majmin(maj, wh->major);
+               set_majmin(min, wh->minor);
+               seq_printf(m, "%c %s:%s %s\n", type_to_char(wh->type),
+                          maj, min, acc);
        }
        spin_unlock(&devcgroup->lock);
 
-       return buf;
-}
-
-static ssize_t devcgroup_access_read(struct cgroup *cgroup,
-                       struct cftype *cft, struct file *file,
-                       char __user *userbuf, size_t nbytes, loff_t *ppos)
-{
-       struct dev_cgroup *devcgroup = cgroup_to_devcgroup(cgroup);
-       int filetype = cft->private;
-       char *buffer;
-       int uninitialized_var(len);
-       int retval;
-
-       if (filetype != DEVCG_ALLOW)
-               return -EINVAL;
-       buffer = print_whitelist(devcgroup, &len);
-       if (IS_ERR(buffer))
-               return PTR_ERR(buffer);
-
-       retval = simple_read_from_buffer(userbuf, nbytes, ppos, buffer, len);
-       kfree(buffer);
-       return retval;
+       return 0;
 }
 
 /*
@@ -314,7 +300,7 @@ static int may_access_whitelist(struct dev_cgroup *c,
                        continue;
                if (whitem->minor != ~0 && whitem->minor != refwh->minor)
                        continue;
-               if (refwh->access & (~(whitem->access | ACC_MASK)))
+               if (refwh->access & (~whitem->access))
                        continue;
                return 1;
        }
@@ -396,6 +382,8 @@ static ssize_t devcgroup_access_write(struct cgroup *cgroup, struct cftype *cft,
        case 'a':
                wh.type = DEV_ALL;
                wh.access = ACC_MASK;
+               wh.major = ~0;
+               wh.minor = ~0;
                goto handle;
        case 'b':
                wh.type = DEV_BLOCK;
@@ -501,7 +489,6 @@ out1:
 static struct cftype dev_cgroup_files[] = {
        {
                .name = "allow",
-               .read = devcgroup_access_read,
                .write  = devcgroup_access_write,
                .private = DEVCG_ALLOW,
        },
@@ -510,6 +497,11 @@ static struct cftype dev_cgroup_files[] = {
                .write = devcgroup_access_write,
                .private = DEVCG_DENY,
        },
+       {
+               .name = "list",
+               .read_seq_string = devcgroup_seq_read,
+               .private = DEVCG_LIST,
+       },
 };
 
 static int devcgroup_populate(struct cgroup_subsys *ss,
@@ -530,7 +522,6 @@ struct cgroup_subsys devices_subsys = {
 
 int devcgroup_inode_permission(struct inode *inode, int mask)
 {
-       struct cgroup *cgroup;
        struct dev_cgroup *dev_cgroup;
        struct dev_whitelist_item *wh;
 
@@ -539,8 +530,8 @@ int devcgroup_inode_permission(struct inode *inode, int mask)
                return 0;
        if (!S_ISBLK(inode->i_mode) && !S_ISCHR(inode->i_mode))
                return 0;
-       cgroup = task_cgroup(current, devices_subsys.subsys_id);
-       dev_cgroup = cgroup_to_devcgroup(cgroup);
+       dev_cgroup = css_to_devcgroup(task_subsys_state(current,
+                               devices_subsys_id));
        if (!dev_cgroup)
                return 0;
 
@@ -571,12 +562,11 @@ acc_check:
 
 int devcgroup_inode_mknod(int mode, dev_t dev)
 {
-       struct cgroup *cgroup;
        struct dev_cgroup *dev_cgroup;
        struct dev_whitelist_item *wh;
 
-       cgroup = task_cgroup(current, devices_subsys.subsys_id);
-       dev_cgroup = cgroup_to_devcgroup(cgroup);
+       dev_cgroup = css_to_devcgroup(task_subsys_state(current,
+                               devices_subsys_id));
        if (!dev_cgroup)
                return 0;