Skip to contentSkip to conversation

cgroup/cpuset: Don't access cpuset_cgrp_subsys.root in is_in_v2_mode()

PATCH Applied Waiman Long 5 participants 6 messages Sep 30

Subsystems (2)
lore
Share & tools
lore

Waiman Long

author maintainer
#0 lore raw

PATCH v2 1/1 cgroup/cpuset: Don't access cpuset_cgrp_subsys.root in is_in_v2_mode()

After seeing the patch [1] to guard is_in_v2_mode() with RCU, it makes me realize that is_in_v2_mode() may be called in a context where a new cgroup filesystem is being rebound with stale cpuset_cgrp_subsys.root pointer. Avoid this potential UaF situation by adding a new cpuset_v2_mode flag which is set when the cpuset_v2_mode mount option is used. This flag is written into only when cpuset_bind() is being called with a stable cpuset_cgrp_subsys.root value. The is_in_v2_mode() helper is modified to read the new cpuset_v2_mode flag instead of accessing cpuset_cgrp_subsys.root directly.

[1] https://lore.kernel.org/lkml/[email protected]

Fixes b8d1b8ee93df ("cpuset: Allow v2 behavior in v1 cgroup")
---
 kernel/cgroup/cpuset.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

[v2] Update the comment as suggested by Ridong

kernel/cgroup/cpuset.c +9 -2
index 19661df6244f..4a2b6f154609 100644
@@ -152,6 +152,12 @@ static cpumask_var_t	isolated_cpus;		/* CSCB */
  */
 static bool		update_housekeeping;	/* RWCS */
 
+/*
+ * Set if "cpuset_v2_mode" mount option is used
+ * Cached at bind time and not lock protected; accessed via {READ,WRITE}_ONCE
+ */
+static bool		cpuset_v2_mode;
+
 /*
  * Copy of isolated_cpus to be passed to housekeeping_update()
  */
@@ -439,8 +445,7 @@ static inline bool cpuset_v2(void)
  */
 static inline bool is_in_v2_mode(void)
 {
-	return cpuset_v2() ||
-	      (cpuset_cgrp_subsys.root->flags & CGRP_ROOT_CPUSET_V2_MODE);
+	return cpuset_v2() || READ_ONCE(cpuset_v2_mode);
 }
 
 /**
@@ -3664,6 +3669,8 @@ static void cpuset_bind(struct cgroup_subsys_state *root_css)
 	mutex_lock(&cpuset_mutex);
 	spin_lock_irq(&callback_lock);
 
+	WRITE_ONCE(cpuset_v2_mode,
+		   !!(cpuset_cgrp_subsys.root->flags & CGRP_ROOT_CPUSET_V2_MODE));
 	if (is_in_v2_mode()) {
 		cpumask_copy(top_cpuset.cpus_allowed, cpu_possible_mask);
 		cpumask_copy(top_cpuset.effective_xcpus, cpu_possible_mask);
Signature · 1 line
2.55.0
reviewer Reviewed-by on v2 1/1
#1 lore raw
Quoted 48 lines · Waiman Long

After seeing the patch [1] to guard is_in_v2_mode() with RCU, it makes me realize that is_in_v2_mode() may be called in a context where a new cgroup filesystem is being rebound with stale cpuset_cgrp_subsys.root pointer. Avoid this potential UaF situation by adding a new cpuset_v2_mode flag which is set when the cpuset_v2_mode mount option is used. This flag is written into only when cpuset_bind() is being called with a stable cpuset_cgrp_subsys.root value. The is_in_v2_mode() helper is modified to read the new cpuset_v2_mode flag instead of accessing cpuset_cgrp_subsys.root directly.

[1] https://lore.kernel.org/lkml/[email protected]

Fixes b8d1b8ee93df ("cpuset: Allow v2 behavior in v1 cgroup")
---
  kernel/cgroup/cpuset.c | 11 +++++++++--
  1 file changed, 9 insertions(+), 2 deletions(-)

[v2] Update the comment as suggested by Ridong

kernel/cgroup/cpuset.c +7 -2
index 19661df6244f..4a2b6f154609 100644
@@ -152,6 +152,12 @@ static cpumask_var_t	isolated_cpus;		/* CSCB */
   */
  static bool		update_housekeeping;	/* RWCS */
 
+/*
+ * Set if "cpuset_v2_mode" mount option is used
+ * Cached at bind time and not lock protected; accessed via {READ,WRITE}_ONCE
+ */
+static bool		cpuset_v2_mode;
+
  /*
   * Copy of isolated_cpus to be passed to housekeeping_update()
   */
@@ -439,8 +445,7 @@ static inline bool cpuset_v2(void)
   */
  static inline bool is_in_v2_mode(void)
  {
-	return cpuset_v2() ||
-	      (cpuset_cgrp_subsys.root->flags & CGRP_ROOT_CPUSET_V2_MODE);
+	return cpuset_v2() || READ_ONCE(cpuset_v2_mode);
  }
 
  /**

kernel/cgroup/cpuset.c static void cpuset_bind(struct cgroup_subsys_state *root_css) · line 3676

Quoted 8 lines
kernel/cgroup/cpuset.c +2 -0
@@ -3664,6 +3669,8 @@ static void cpuset_bind(struct cgroup_subsys_state *root_css)
  	mutex_lock(&cpuset_mutex);
  	spin_lock_irq(&callback_lock);
 
+	WRITE_ONCE(cpuset_v2_mode,
+		   !!(cpuset_cgrp_subsys.root->flags & CGRP_ROOT_CPUSET_V2_MODE));
  	if (is_in_v2_mode()) {
  		cpumask_copy(top_cpuset.cpus_allowed, cpu_possible_mask);
  		cpumask_copy(top_cpuset.effective_xcpus, cpu_possible_mask);

Thanks.

Signature · 2 lines
Best regards
Ridong
Reviewed-by on v2 1/1
#2 lore raw
In reply to Waiman Long

Hi Waiman,

Quoted 10 lines · Waiman Long

After seeing the patch [1] to guard is_in_v2_mode() with RCU, it makes me realize that is_in_v2_mode() may be called in a context where a new cgroup filesystem is being rebound with stale cpuset_cgrp_subsys.root pointer. Avoid this potential UaF situation by adding a new cpuset_v2_mode flag which is set when the cpuset_v2_mode mount option is used. This flag is written into only when cpuset_bind() is being called with a stable cpuset_cgrp_subsys.root value. The is_in_v2_mode() helper is modified to read the new cpuset_v2_mode flag instead of accessing cpuset_cgrp_subsys.root directly.

Quoted 4 lines

[1] https://lore.kernel.org/lkml/[email protected]

Fixes b8d1b8ee93df ("cpuset: Allow v2 behavior in v1 cgroup")

This works for me.

Thanks,
-Andrea

Quoted 45 lines
---
 kernel/cgroup/cpuset.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

[v2] Update the comment as suggested by Ridong

kernel/cgroup/cpuset.c +9 -2
index 19661df6244f..4a2b6f154609 100644
@@ -152,6 +152,12 @@ static cpumask_var_t	isolated_cpus;		/* CSCB */
  */
 static bool		update_housekeeping;	/* RWCS */
 
+/*
+ * Set if "cpuset_v2_mode" mount option is used
+ * Cached at bind time and not lock protected; accessed via {READ,WRITE}_ONCE
+ */
+static bool		cpuset_v2_mode;
+
 /*
  * Copy of isolated_cpus to be passed to housekeeping_update()
  */
@@ -439,8 +445,7 @@ static inline bool cpuset_v2(void)
  */
 static inline bool is_in_v2_mode(void)
 {
-	return cpuset_v2() ||
-	      (cpuset_cgrp_subsys.root->flags & CGRP_ROOT_CPUSET_V2_MODE);
+	return cpuset_v2() || READ_ONCE(cpuset_v2_mode);
 }
 
 /**
@@ -3664,6 +3669,8 @@ static void cpuset_bind(struct cgroup_subsys_state *root_css)
 	mutex_lock(&cpuset_mutex);
 	spin_lock_irq(&callback_lock);
 
+	WRITE_ONCE(cpuset_v2_mode,
+		   !!(cpuset_cgrp_subsys.root->flags & CGRP_ROOT_CPUSET_V2_MODE));
 	if (is_in_v2_mode()) {
 		cpumask_copy(top_cpuset.cpus_allowed, cpu_possible_mask);
 		cpumask_copy(top_cpuset.effective_xcpus, cpu_possible_mask);
Signature · 1 line
2.55.0
maintainer
#4 lore raw
In reply to Waiman Long
Quoted 4 lines · Waiman Long

After seeing the patch [1] to guard is_in_v2_mode() with RCU, it makes me realize that is_in_v2_mode() may be called in a context where a new cgroup filesystem is being rebound with stale cpuset_cgrp_subsys.root pointer.

add: which is a privileged operation.

Quoted 6 lines

Avoid this potential UaF situation by adding a new cpuset_v2_mode flag which is set when the cpuset_v2_mode mount option is used. This flag is written into only when cpuset_bind() is being called with a stable cpuset_cgrp_subsys.root value. The is_in_v2_mode() helper is modified to read the new cpuset_v2_mode flag instead of accessing cpuset_cgrp_subsys.root directly.

You write about "potential" situation. But are there any such callers (after the cpuset_v2() conversion in cpuset_num_cpus())?

Quoted 1 line
Fixes b8d1b8ee93df ("cpuset: Allow v2 behavior in v1 cgroup")

I'd even consider (pruning to)

Fixes d23b5c5777158 ("cgroup: Make operations on the cgroup root_list RCU safe")

(Admittedly, is_in_v2_mode() wasn't always called under cgroup_mutex, but I'd argue (without comprehensive analysis) that predicate's callers were synchronized via cpuset_mutex in cpuset_bind() (and cgroup_mutex) anyways.)

The caching you proposed looks safe. (I'm mainly writing because of the commit message wrt UaF.)

Michal

author maintainer
#5 lore raw
Quoted 7 lines · Michal Koutný
Quoted 4 lines · Waiman Long

After seeing the patch [1] to guard is_in_v2_mode() with RCU, it makes me realize that is_in_v2_mode() may be called in a context where a new cgroup filesystem is being rebound with stale cpuset_cgrp_subsys.root pointer.

add: which is a privileged operation.

Quoted 8 lines
Quoted 6 lines

Avoid this potential UaF situation by adding a new cpuset_v2_mode flag which is set when the cpuset_v2_mode mount option is used. This flag is written into only when cpuset_bind() is being called with a stable cpuset_cgrp_subsys.root value. The is_in_v2_mode() helper is modified to read the new cpuset_v2_mode flag instead of accessing cpuset_cgrp_subsys.root directly.

You write about "potential" situation. But are there any such callers (after the cpuset_v2() conversion in cpuset_num_cpus())?

Other than the cpuset_num_cpus(), is_in_v2_mode() should only be used with either cpuset_mutex or callback_lock held. Rebinding a cgroup subsystem is protected by holding cgroup_mutex. I am not aware of any situation where rebinding is happening while cpuset code is being called into, but I can't rule out that possibility. Also it is possible that future cpuset code extension may make it possible that this race condition can happen. For safety, it is better to make the code safe. That is the reason why I said it is a potential situation.

Quoted 5 lines
Quoted 2 lines
Fixes b8d1b8ee93df ("cpuset: Allow v2 behavior in v1 cgroup")

I'd even consider (pruning to)

Fixes d23b5c5777158 ("cgroup: Make operations on the cgroup root_list RCU safe")
Quoted 7 lines

(Admittedly, is_in_v2_mode() wasn't always called under cgroup_mutex, but I'd argue (without comprehensive analysis) that predicate's callers were synchronized via cpuset_mutex in cpuset_bind() (and cgroup_mutex) anyways.)

The caching you proposed looks safe. (I'm mainly writing because of the commit message wrt UaF.)

I said UaF because it is what Andrea patch is implying. As I said before, I don't think that can happen, but I can't rule it out.

Cheers,
Longman

Quoted 2 lines

Michal