Skip to contentSkip to conversation

cpuset: fix cpuset_print_task_mems_allowed() vs rename() race

PATCH Superseded Li Zefan 2 participants 3 messages Jan 25, 2013 cgroup

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Li Zefan

author
#0 lore raw

PATCH 1/1 cpuset: fix cpuset_print_task_mems_allowed() vs rename() race

rename() will change dentry->d_name. The result of this race can be worse than seeing partially rewritten name, but we might access a stale pointer because rename() will re-allocate memory to hold a longer name.

It's safe in the protection of dentry->d_lock.

---
 kernel/cpuset.c | 4 ++++
 1 file changed, 4 insertions(+)
kernel/cpuset.c +4 -0
index 16be7c9..b2476c2 100644
@@ -2606,8 +2606,12 @@ void cpuset_print_task_mems_allowed(struct task_struct *tsk)
 
 	dentry = task_cs(tsk)->css.cgroup->dentry;
 	spin_lock(&cpuset_buffer_lock);
+
+	spin_lock(&dentry->d_lock);
 	snprintf(cpuset_name, CPUSET_NAME_LEN,
 		 dentry ? (const char *)dentry->d_name.name : "/");
+	spin_unlock(&dentry->d_lock);
+
 	nodelist_scnprintf(cpuset_nodelist, CPUSET_NODELIST_LEN,
 			   tsk->mems_allowed);
 	printk(KERN_INFO "%s cpuset=%s mems_allowed=%s\n",
Signature · 1 line
1.8.0.2
#1 lore raw
Quoted 17 lines · Li Zefan

rename() will change dentry->d_name. The result of this race can be worse than seeing partially rewritten name, but we might access a stale pointer because rename() will re-allocate memory to hold a longer name.

It's safe in the protection of dentry->d_lock.

---
 kernel/cpuset.c | 4 ++++
 1 file changed, 4 insertions(+)
kernel/cpuset.c +0 -0
index 16be7c9..b2476c2 100644

kernel/cpuset.c void cpuset_print_task_mems_allowed(struct task_struct *tsk) · line 2612

Quoted 7 lines
kernel/cpuset.c +2 -0
@@ -2606,8 +2606,12 @@ void cpuset_print_task_mems_allowed(struct task_struct *tsk)
 
 	dentry = task_cs(tsk)->css.cgroup->dentry;
 	spin_lock(&cpuset_buffer_lock);
+
+	spin_lock(&dentry->d_lock);
 	snprintf(cpuset_name, CPUSET_NAME_LEN,
 		 dentry ? (const char *)dentry->d_name.name : "/");

Ahem... Can dentry actually be NULL here? If not, this conditional is bogus; otherwise, spin_lock(&dentry->d_lock) is going to blow up...

author
#2 lore raw

On 2013/1/25 15:26, Al Viro wrpte:

Quoted 19 lines · Li Zefan
Quoted 18 lines

rename() will change dentry->d_name. The result of this race can be worse than seeing partially rewritten name, but we might access a stale pointer because rename() will re-allocate memory to hold a longer name.

It's safe in the protection of dentry->d_lock.

---
 kernel/cpuset.c | 4 ++++
 1 file changed, 4 insertions(+)
kernel/cpuset.c +0 -0
index 16be7c9..b2476c2 100644
@@ -2606,8 +2606,12 @@ void cpuset_print_task_mems_allowed(struct task_struct *tsk)
 
Quoted 9 lines

kernel/cpuset.c void cpuset_print_task_mems_allowed(struct task_struct *tsk) · line 2612

Quoted 6 lines
kernel/cpuset.c +2 -0
 	dentry = task_cs(tsk)->css.cgroup->dentry;
 	spin_lock(&cpuset_buffer_lock);
+
+	spin_lock(&dentry->d_lock);
 	snprintf(cpuset_name, CPUSET_NAME_LEN,
 		 dentry ? (const char *)dentry->d_name.name : "/");

Ahem... Can dentry actually be NULL here? If not, this conditional is bogus; otherwise, spin_lock(&dentry->d_lock) is going to blow up...

oops, my fault...It's NULL if cpuset is not mounted.