Skip to contentSkip to conversation

sysfs null dentry pointer checking

RFC New John Zielinski 1 participant 1 message May 23, 2004 Driver core

lore
Share & tools
lore
#0 lore raw

RFC 1/1 sysfs null dentry pointer checking

I've just discovered that some of the sysfs directory functions do not check if the dentry pointer is valid. Some of the subsystems in the kernel don't check the return value of subsystem_register since not being able to create a directory in sysfs is not fatal and shouldn't prevent booting the machine. Any kobject that tries to create a directory under that subsystem would cause a null pointer dereference.

Here's a patch where the dentry pointers should be checked. The sysfs directory structure wouldn't get updated if there were any directory creation errors in a parent but the internal kset/kobject hierarchy would still function properly.

John

fs/sysfs/dir.c +10 -1
diff -urNX dontdiff linux-2.6.6/fs/sysfs/dir.c linux/fs/sysfs/dir.c
@@ -26,6 +26,9 @@
 {
 	int error;
 
+	if (!p)
+		return -EFAULT;
+
 	down(&p->d_inode->i_sem);
 	*d = sysfs_get_dentry(p,n);
 	if (!IS_ERR(*d)) {
@@ -95,7 +98,8 @@
 
 void sysfs_remove_subdir(struct dentry * d)
 {
-	remove_dir(d);
+	if (d)
+		remove_dir(d);
 }
 
 
@@ -164,6 +168,11 @@
 	if (!kobj->parent)
 		return;
 
+	if (!kobj->dentry) {
+		kobject_set_name(kobj,new_name);
+		return;
+	}
+
 	parent = kobj->parent->dentry;
 
 	down(&parent->d_inode->i_sem);