Skip to contentSkip to conversation

Kobject: bail early if no new_parent in kobject_move()

PATCH In review Phil Carmody 3 participants 8 messages Oct 1, 2009 – Oct 6, 2009

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

PATCH 1/1 Kobject: bail early if no new_parent in kobject_move()

From: Phil Carmody <[email protected]>

In the absense of kobj->kset, new_parent would remain NULL. NULL-dereference shenanighans then ensues in the subsequent sysfs_move_dir(..., new_parent) call. Bail early instead.

---
 lib/kobject.c |    6 +++++-
 1 files changed, 5 insertions(+), 1 deletions(-)
lib/kobject.c +5 -1
index b512b74..3574f94 100644
@@ -471,8 +471,12 @@ int kobject_move(struct kobject *kobj, struct kobject *new_parent)
 		return -EINVAL;
 	new_parent = kobject_get(new_parent);
 	if (!new_parent) {
-		if (kobj->kset)
+		if (kobj->kset) {
 			new_parent = kobject_get(&kobj->kset->kobj);
+		} else {
+			error = -EINVAL;
+			goto out;
+		}
 	}
 	/* old object path */
 	devpath = kobject_get_path(kobj, GFP_KERNEL);
Signature · 1 line
1.5.4.3
#1 lore raw
Quoted 5 lines · Phil Carmody

From: Phil Carmody <[email protected]>

In the absense of kobj->kset, new_parent would remain NULL. NULL-dereference shenanighans then ensues in the subsequent sysfs_move_dir(..., new_parent) call. Bail early instead.

What caller causes this to happen?

thanks,

greg k-h

#2 lore raw
Quoted 8 lines · ext Greg KH
Quoted 5 lines · Phil Carmody

From: Phil Carmody <[email protected]>

In the absense of kobj->kset, new_parent would remain NULL. NULL-dereference shenanighans then ensues in the subsequent sysfs_move_dir(..., new_parent) call. Bail early instead.

What caller causes this to happen?

It was spotted by static code analysis. If there's no such path, then the ``if (kobj->kset)'' condition itself would be unnecessary.

Phil

Signature · 2 lines
"They weren't designed to run on a computer, they were designed
to run on a Powerpoint slide projector." -- Peter Gutmann
#3 lore raw
Quoted 4 lines · Phil Carmody
Quoted 3 lines · ext Greg KH
Quoted 2 lines · Phil Carmody

From: Phil Carmody <[email protected]>

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

In the absense of kobj->kset, new_parent would remain NULL. NULL-dereference shenanighans then ensues in the subsequent sysfs_move_dir(..., new_parent) call. Bail early instead.

What caller causes this to happen?

It was spotted by static code analysis. If there's no such path, then the ``if (kobj->kset)'' condition itself would be unnecessary.

Hm, as there are only 2-3 callers of rename, I guess this isn't that big of a deal. I'll queue it up for .33.

thanks,

greg k-h

#4 lore raw
In reply to Phil Carmody
Quoted 5 lines · Phil Carmody

From: Phil Carmody <[email protected]>

In the absense of kobj->kset, new_parent would remain NULL. NULL-dereference shenanighans then ensues in the subsequent sysfs_move_dir(..., new_parent) call. Bail early instead.

But sysfs_move_dir(..., NULL) should work and fall back to the sysfs root...

<looks at code>

It seems that has been broken for some time. Should probably be something like this instead (uncompiled); I can send it with proper description and s-o-b once I gave it a test.

---
 fs/sysfs/dir.c |    3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
fs/sysfs/dir.c +2 -1
@@ -894,7 +894,8 @@ int sysfs_move_dir(struct kobject *kobj,
 
 	mutex_lock(&sysfs_rename_mutex);
 	BUG_ON(!sd->s_parent);
-	new_parent_sd = new_parent_kobj->sd ? new_parent_kobj->sd : &sysfs_root;
+	new_parent_sd = (new_parent_kobj && new_parent_kobj->sd) ?
+		new_parent_kobj->sd : &sysfs_root;
 
 	error = 0;
 	if (sd->s_parent == new_parent_sd)
#5 lore raw
Quoted 11 lines · ext Cornelia Huck
Quoted 5 lines · Phil Carmody

From: Phil Carmody <[email protected]>

In the absense of kobj->kset, new_parent would remain NULL. NULL-dereference shenanighans then ensues in the subsequent sysfs_move_dir(..., new_parent) call. Bail early instead.

But sysfs_move_dir(..., NULL) should work and fall back to the sysfs root...

I guess there are two schools of thought - the strict and the accommodating. I was contemplating resending an even stricter patch, but there's nothing wrong with adopting sane safe fallbacks instead.

Quoted 13 lines

<looks at code>

It seems that has been broken for some time. Should probably be something like this instead (uncompiled); I can send it with proper description and s-o-b once I gave it a test.

---
 fs/sysfs/dir.c |    3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

fs/sysfs/dir.c int sysfs_move_dir(struct kobject *kobj, · line 901

Quoted 9 lines
fs/sysfs/dir.c +2 -1
@@ -894,7 +894,8 @@ int sysfs_move_dir(struct kobject *kobj,
 
 	mutex_lock(&sysfs_rename_mutex);
 	BUG_ON(!sd->s_parent);
-	new_parent_sd = new_parent_kobj->sd ? new_parent_kobj->sd : &sysfs_root;
+	new_parent_sd = (new_parent_kobj && new_parent_kobj->sd) ?
+		new_parent_kobj->sd : &sysfs_root;
 
 	error = 0;
 	if (sd->s_parent == new_parent_sd)

Looks like it knocks the NULL dereference squarely on the head. Yup.

Phil

Signature · 2 lines
"They weren't designed to run on a computer, they were designed
to run on a Powerpoint slide projector." -- Peter Gutmann
#6 lore raw
Quoted 10 lines · Phil Carmody
Quoted 9 lines · ext Cornelia Huck
Quoted 5 lines · Phil Carmody

From: Phil Carmody <[email protected]>

In the absense of kobj->kset, new_parent would remain NULL. NULL-dereference shenanighans then ensues in the subsequent sysfs_move_dir(..., new_parent) call. Bail early instead.

Quoted 6 lines
Quoted 2 lines

But sysfs_move_dir(..., NULL) should work and fall back to the sysfs root...

I guess there are two schools of thought - the strict and the accommodating. I was contemplating resending an even stricter patch, but there's nothing wrong with adopting sane safe fallbacks instead.

I made all the *_move() functions legal for a NULL destination (see c744aeae9d173a953b771a7ad5c872f91fa99dec), so I'd like to have it back that way :)

I've put the patch on my s390 system and ran through sysfs_move(); proper patch posting follows.

#7 lore raw

PATCH 1/1 sysfs: Allow sysfs_move_dir(..., NULL) again.

In reply to Phil Carmody

As device_move() and kobject_move() both handle a NULL destination, sysfs_move_dir() should do this as well (again) and fall back to sysfs_root in that case.

---
 fs/sysfs/dir.c |    3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
fs/sysfs/dir.c +2 -1
@@ -894,7 +894,8 @@ int sysfs_move_dir(struct kobject *kobj,
 
 	mutex_lock(&sysfs_rename_mutex);
 	BUG_ON(!sd->s_parent);
-	new_parent_sd = new_parent_kobj->sd ? new_parent_kobj->sd : &sysfs_root;
+	new_parent_sd = (new_parent_kobj && new_parent_kobj->sd) ?
+		new_parent_kobj->sd : &sysfs_root;
 
 	error = 0;
 	if (sd->s_parent == new_parent_sd)