Skip to contentSkip to conversation

cgroup/cpuset: Prevent UAF in proc_cpuset_show()

PATCH In review Chen Ridong 3 participants 5 messages Jun 28, 2024 – Jun 30, 2024

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 cgroup/cpuset: Prevent UAF in proc_cpuset_show()

An UAF can happen when /proc/cpuset is read as reported in [1].

This can be reproduced by the following methods:
1.add an mdelay(1000) before acquiring the cgroup_lock In the
cgroup_path_ns function.
2.$cat /proc/<pid>/cpuset repeatly.
3.$mount -t cgroup -o cpuset cpuset /sys/fs/cgroup/cpuset/
$umount /sys/fs/cgroup/cpuset/ repeatly.

The race that cause this bug can be shown as below:

(umount) | (cat /proc/<pid>/cpuset)
css_release | proc_cpuset_show
css_release_work_fn | css = task_get_css(tsk, cpuset_cgrp_id);
css_free_rwork_fn | cgroup_path_ns(css->cgroup, ...);
cgroup_destroy_root | mutex_lock(&cgroup_mutex);
rebind_subsystems |
cgroup_free_root |

			|	// cgrp was freed, UAF
			|	cgroup_path_ns_locked(cgrp,..);

When the cpuset is initialized, the root node top_cpuset.css.cgrp will point to &cgrp_dfl_root.cgrp. In cgroup v1, the mount operation will allocate cgroup_root, and top_cpuset.css.cgrp will point to the allocated &cgroup_root.cgrp. When the umount operation is executed, top_cpuset.css.cgrp will be rebound to &cgrp_dfl_root.cgrp.

The problem is that when rebinding to cgrp_dfl_root, there are cases where the cgroup_root allocated by setting up the root for cgroup v1 is cached. This could lead to a Use-After-Free (UAF) if it is subsequently freed. The descendant cgroups of cgroup v1 can only be freed after the css is released. However, the css of the root will never be released, yet the cgroup_root should be freed when it is unmounted. This means that obtaining a reference to the css of the root does not guarantee that css.cgrp->root will not be freed.

Fix this problem by using rcu_read_lock in proc_cpuset_show(). As cgroup_root is kfree_rcu after commit d23b5c577715 ("cgroup: Make operations on the cgroup root_list RCU safe"), css->cgroup won't be freed during the critical section.
To call cgroup_path_ns_locked, css_set_lock is needed, so it is safe to replace task_get_css with task_css.

[1] https://syzkaller.appspot.com/bug?extid=9b1ff7be974a403aa4cd

Fixes a79a908fd2b0 ("cgroup: introduce cgroup namespaces")
---
 kernel/cgroup/cpuset.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
kernel/cgroup/cpuset.c +9 -4
index c12b9fdb22a4..bcb4da8f54c8 100644
@@ -21,6 +21,7 @@
  *  License.  See the file COPYING in the main directory of the Linux
  *  distribution for more details.
  */
+#include "cgroup-internal.h"
 
 #include <linux/cpu.h>
 #include <linux/cpumask.h>
@@ -5051,10 +5052,14 @@ int proc_cpuset_show(struct seq_file *m, struct pid_namespace *ns,
 	if (!buf)
 		goto out;
 
-	css = task_get_css(tsk, cpuset_cgrp_id);
-	retval = cgroup_path_ns(css->cgroup, buf, PATH_MAX,
-				current->nsproxy->cgroup_ns);
-	css_put(css);
+	rcu_read_lock();
+	spin_lock_irq(&css_set_lock);
+	css = task_css(tsk, cpuset_cgrp_id);
+	retval = cgroup_path_ns_locked(css->cgroup, buf, PATH_MAX,
+				       current->nsproxy->cgroup_ns);
+	spin_unlock_irq(&css_set_lock);
+	rcu_read_unlock();
+
 	if (retval == -E2BIG)
 		retval = -ENAMETOOLONG;
 	if (retval < 0)
Signature · 1 line
2.34.1
maintainer
#1 lore raw

Hello,

Replaced the v3 patch in cgroup/for-6.10-fixes with this version.

Thanks.

Signature · 1 line
tejun
#2 lore raw
In reply to Chen Ridong

…

Quoted 1 line

+++ b/kernel/cgroup/cpuset.c

…

Quoted 5 lines
kernel/cgroup/cpuset.c +0 -1
@@ -5051,10 +5052,14 @@ int proc_cpuset_show(struct seq_file *m, struct pid_namespace *ns,
 	if (!buf)
 		goto out;
 
-	css = task_get_css(tsk, cpuset_cgrp_id);

kernel/cgroup/cpuset.c int proc_cpuset_show(struct seq_file *m, struct pid_namespace *ns, · line 5061

Quoted 10 lines
kernel/cgroup/cpuset.c +7 -3
-	retval = cgroup_path_ns(css->cgroup, buf, PATH_MAX,
-				current->nsproxy->cgroup_ns);
-	css_put(css);
+	rcu_read_lock();
+	spin_lock_irq(&css_set_lock);
+	css = task_css(tsk, cpuset_cgrp_id);
+	retval = cgroup_path_ns_locked(css->cgroup, buf, PATH_MAX,
+				       current->nsproxy->cgroup_ns);
+	spin_unlock_irq(&css_set_lock);
+	rcu_read_unlock();

…

Under which circumstances would you become interested to apply statements like the following?

* guard(rcu)();
https://elixir.bootlin.com/linux/v6.10-rc5/source/include/linux/rcupdate.h#L1093

* guard(spinlock_irq)(&css_set_lock);
https://elixir.bootlin.com/linux/v6.10-rc5/source/include/linux/spinlock.h#L567

Regards,
Markus

maintainer
#3 lore raw

Hello,

Quoted 8 lines · Markus Elfring

Under which circumstances would you become interested to apply statements like the following?

* guard(rcu)();
https://elixir.bootlin.com/linux/v6.10-rc5/source/include/linux/rcupdate.h#L1093

* guard(spinlock_irq)(&css_set_lock);
https://elixir.bootlin.com/linux/v6.10-rc5/source/include/linux/spinlock.h#L567

I don't really care either way. Neither makes meaningful difference here.

Thanks.

Signature · 1 line
tejun
#4 lore raw
Quoted 10 lines
Quoted 8 lines

Under which circumstances would you become interested to apply statements like the following?

* guard(rcu)();
https://elixir.bootlin.com/linux/v6.10-rc5/source/include/linux/rcupdate.h#L1093

* guard(spinlock_irq)(&css_set_lock);
https://elixir.bootlin.com/linux/v6.10-rc5/source/include/linux/spinlock.h#L567

I don't really care either way.

I find such feedback interesting somehow.

Quoted 1 line

Neither makes meaningful difference here.

Would you like to support making the affected source code safer and a bit more succinct?
https://elixir.bootlin.com/linux/v6.10-rc5/source/kernel/cgroup/cpuset.c#L5034

Regards,
Markus