Skip to contentSkip to conversation

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

PATCH Superseded Waiman Long 2 participants 3 messages Sep 30

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Waiman Long

author maintainer
#0 lore raw

PATCH 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 | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
kernel/cgroup/cpuset.c +8 -2
index 19661df6244f..266ce17d1af0 100644
@@ -147,6 +147,11 @@ static cpumask_var_t	subpartitions_cpus;	/* RWCS */
  */
 static cpumask_var_t	isolated_cpus;		/* CSCB */
 
+/*
+ * Set if "cpuset_v2_mode" mount option is used
+ */
+static bool		cpuset_v2_mode;		/* Unprotected */
+
 /*
  * Set if housekeeping cpumasks are to be updated.
  */
@@ -439,8 +444,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 +3668,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 v1 1/1
#1 lore raw
Quoted 23 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 | 10 ++++++++--
  1 file changed, 8 insertions(+), 2 deletions(-)
kernel/cgroup/cpuset.c +0 -0
index 19661df6244f..266ce17d1af0 100644

kernel/cgroup/cpuset.c static cpumask_var_t subpartitions_cpus; /* RWCS */ · line 154

Quoted 8 lines
kernel/cgroup/cpuset.c +5 -0
@@ -147,6 +147,11 @@ static cpumask_var_t	subpartitions_cpus;	/* RWCS */
   */
  static cpumask_var_t	isolated_cpus;		/* CSCB */
 
+/*
+ * Set if "cpuset_v2_mode" mount option is used
+ */
+static bool		cpuset_v2_mode;		/* Unprotected */
+

Nit.

Unprotected is wired here, will it be better with:

/* Cached at bind time; accessed via {READ,WRITE}_ONCE */

Quoted 14 lines
kernel/cgroup/cpuset.c +1 -2
  /*
   * Set if housekeeping cpumasks are to be updated.
   */
@@ -439,8 +444,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 3675

Quoted 8 lines
kernel/cgroup/cpuset.c +2 -0
@@ -3664,6 +3668,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
author maintainer
#2 lore raw
Quoted 37 lines · Ridong Chen
Quoted 29 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 | 10 ++++++++--

  1 file changed, 8 insertions(+), 2 deletions(-)
kernel/cgroup/cpuset.c +0 -0
index 19661df6244f..266ce17d1af0 100644
@@ -147,6 +147,11 @@ static cpumask_var_t subpartitions_cpus;    /* 

RWCS */
   */
  static cpumask_var_t    isolated_cpus;        /* CSCB */ +/*
+ * Set if "cpuset_v2_mode" mount option is used
+ */

kernel/cgroup/cpuset.c static cpumask_var_t subpartitions_cpus; /* RWCS */ · line 154

Quoted 2 lines
kernel/cgroup/cpuset.c +2 -0
+static bool        cpuset_v2_mode;        /* Unprotected */
+

Nit.

Quoted 3 lines

Unprotected is wired here, will it be better with:

/* Cached at bind time; accessed via {READ,WRITE}_ONCE */

Unprotected just means it is not protected by any lock. I should have said "not lock protected".

Quoted 26 lines
Quoted 21 lines

  /*
   * Set if housekeeping cpumasks are to be updated. */

kernel/cgroup/cpuset.c +2 -2
@@ -439,8 +444,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 +3668,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()) {
Quoted 2 lines

          cpumask_copy(top_cpuset.cpus_allowed, cpu_possible_mask); cpumask_copy(top_cpuset.effective_xcpus, cpu_possible_mask);

Quoted 2 lines

Thanks.

Thanks for the review.

Cheers,
Longman