Linux 6.9-rc1
[linux-2.6-microblaze.git] / security / device_cgroup.c
index a9f8c63..dc4df74 100644 (file)
@@ -82,6 +82,17 @@ free_and_exit:
        return -ENOMEM;
 }
 
+static void dev_exceptions_move(struct list_head *dest, struct list_head *orig)
+{
+       struct dev_exception_item *ex, *tmp;
+
+       lockdep_assert_held(&devcgroup_mutex);
+
+       list_for_each_entry_safe(ex, tmp, orig, list) {
+               list_move_tail(&ex->list, dest);
+       }
+}
+
 /*
  * called under devcgroup_mutex
  */
@@ -205,7 +216,7 @@ static void devcgroup_offline(struct cgroup_subsys_state *css)
 }
 
 /*
- * called from kernel/cgroup.c with cgroup_lock() held.
+ * called from kernel/cgroup/cgroup.c with cgroup_lock() held.
  */
 static struct cgroup_subsys_state *
 devcgroup_css_alloc(struct cgroup_subsys_state *parent_css)
@@ -410,7 +421,7 @@ static bool verify_new_ex(struct dev_cgroup *dev_cgroup,
                } else {
                        /*
                         * new exception in the child will add more devices
-                        * that can be acessed, so it can't match any of
+                        * that can be accessed, so it can't match any of
                         * parent's exceptions, even slightly
                         */ 
                        match = match_exception_partial(&dev_cgroup->exceptions,
@@ -604,11 +615,13 @@ static int devcgroup_update_access(struct dev_cgroup *devcgroup,
        int count, rc = 0;
        struct dev_exception_item ex;
        struct dev_cgroup *parent = css_to_devcgroup(devcgroup->css.parent);
+       struct dev_cgroup tmp_devcgrp;
 
        if (!capable(CAP_SYS_ADMIN))
                return -EPERM;
 
        memset(&ex, 0, sizeof(ex));
+       memset(&tmp_devcgrp, 0, sizeof(tmp_devcgrp));
        b = buffer;
 
        switch (*b) {
@@ -620,15 +633,27 @@ static int devcgroup_update_access(struct dev_cgroup *devcgroup,
 
                        if (!may_allow_all(parent))
                                return -EPERM;
-                       dev_exception_clean(devcgroup);
-                       devcgroup->behavior = DEVCG_DEFAULT_ALLOW;
-                       if (!parent)
+                       if (!parent) {
+                               devcgroup->behavior = DEVCG_DEFAULT_ALLOW;
+                               dev_exception_clean(devcgroup);
                                break;
+                       }
 
+                       INIT_LIST_HEAD(&tmp_devcgrp.exceptions);
+                       rc = dev_exceptions_copy(&tmp_devcgrp.exceptions,
+                                                &devcgroup->exceptions);
+                       if (rc)
+                               return rc;
+                       dev_exception_clean(devcgroup);
                        rc = dev_exceptions_copy(&devcgroup->exceptions,
                                                 &parent->exceptions);
-                       if (rc)
+                       if (rc) {
+                               dev_exceptions_move(&devcgroup->exceptions,
+                                                   &tmp_devcgrp.exceptions);
                                return rc;
+                       }
+                       devcgroup->behavior = DEVCG_DEFAULT_ALLOW;
+                       dev_exception_clean(&tmp_devcgrp);
                        break;
                case DEVCG_DENY:
                        if (css_has_online_children(&devcgroup->css))
@@ -797,7 +822,6 @@ struct cgroup_subsys devices_cgrp_subsys = {
 
 /**
  * devcgroup_legacy_check_permission - checks if an inode operation is permitted
- * @dev_cgroup: the dev cgroup to be tested against
  * @type: device type
  * @major: device major number
  * @minor: device minor number