Skip to contentSkip to conversation

protect against buggy drivers

PATCH Applied Stephen Hemminger 4 participants 11 messages Oct 8, 2004 – Oct 22, 2004 VFS

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 protect against buggy drivers

# This is a BitKeeper generated diff -Nru style patch. #
# ChangeSet

#   2004/10/08 09:52:03-07:00 [email protected] 
#   Protect against bad driver writers who pass invalid names when
#   setting up character devices.
# 
# fs/char_dev.c
#   2004/10/08 09:51:52-07:00 [email protected] +5 -0
#   Protect against bad driver writers who pass invalid names when
#   setting up character devices.
# 
diff -Nru a/fs/char_dev.c b/fs/char_dev.c
--- a/fs/char_dev.c	2004-10-08 09:52:15 -07:00
+++ b/fs/char_dev.c	2004-10-08 09:52:15 -07:00
@@ -196,6 +196,11 @@
 	char *s;
 	int err = -ENOMEM;
+	if (name == NULL || *name == '\0' || 
+	    strlen(name) >= KOBJ_NAME_LEN || 

+ !strcmp(name, ".") || !strcmp(name, ".."))
+ return -EINVAL;
+
cd = __register_chrdev_region(major, 0, 256, name); if (IS_ERR(cd))
return PTR_ERR(cd);

#1 lore raw

Review

Quoted 1 line · Stephen Hemminger
(quoted diff) +1 -0
+	    strlen(name) >= KOBJ_NAME_LEN || 

There's no need for this check, if we fix the other usage of cdev->kobj.name in this file to use the proper kobject_name() and kobject_set_name() functions.

thanks,

greg k-h

#2 lore raw
Quoted 10 lines · Greg KH

Review

Quoted 1 line · Stephen Hemminger
(quoted diff) +1 -0
+	    strlen(name) >= KOBJ_NAME_LEN ||

There's no need for this check, if we fix the other usage of cdev->kobj.name in this file to use the proper kobject_name() and kobject_set_name() functions.

thanks,

greg k-h

Well the module name is passed in register/unregister_chrdev(). It was not documented as the allowed length of the name so it was possible to install a device and then only "partially" uninstall the device so a subsequent open of the device-file would crash the kernel. A device name of :

"Octrangle Contrabulator" 23 characters

... in a test program was sufficiently-long to kill the kernel. I recommend truncating any name to an acceptable length. This would show up in /proc/iomem, etc., prompting the developer to shorten the name.

Also, the new length of 20 characters is probably too short. There was no such limitation on 2.4.x, where many modules are being ported from.

Cheers,
Dick Johnson
Penguin : Linux version 2.6.8 on an i686 machine (5537.79 BogoMips).
Note 96.31% of all statistics are fiction.

#3 lore raw
Quoted 17 lines · Richard B. Johnson
Quoted 6 lines · Greg KH

Review

Quoted 1 line · Stephen Hemminger
(quoted diff) +1 -0
+	    strlen(name) >= KOBJ_NAME_LEN ||

There's no need for this check, if we fix the other usage of cdev->kobj.name in this file to use the proper kobject_name() and kobject_set_name() functions.

Well the module name is passed in register/unregister_chrdev(). It was not documented as the allowed length of the name so it was possible to install a device and then only "partially" uninstall the device so a subsequent open of the device-file would crash the kernel. A device name of :

"Octrangle Contrabulator" 23 characters

Quoted 8 lines

... in a test program was sufficiently-long to kill the kernel. I recommend truncating any name to an acceptable length. This would show up in /proc/iomem, etc., prompting the developer to shorten the name.

Also, the new length of 20 characters is probably too short. There was no such limitation on 2.4.x, where many modules are being ported from.

That's why I said this check should not go in, and the cdev code fixed to use the proper functions. That would enable you to have as long as a name as you wanted to.

thanks,

greg k-h

#4 lore raw
Quoted 27 lines · Greg KH
Quoted 17 lines · Richard B. Johnson
Quoted 6 lines · Greg KH

Review

Quoted 1 line · Stephen Hemminger
(quoted diff) +1 -0
+	    strlen(name) >= KOBJ_NAME_LEN ||

There's no need for this check, if we fix the other usage of cdev->kobj.name in this file to use the proper kobject_name() and kobject_set_name() functions.

Well the module name is passed in register/unregister_chrdev(). It was not documented as the allowed length of the name so it was possible to install a device and then only "partially" uninstall the device so a subsequent open of the device-file would crash the kernel. A device name of :

"Octrangle Contrabulator" 23 characters

Quoted 8 lines

... in a test program was sufficiently-long to kill the kernel. I recommend truncating any name to an acceptable length. This would show up in /proc/iomem, etc., prompting the developer to shorten the name.

Also, the new length of 20 characters is probably too short. There was no such limitation on 2.4.x, where many modules are being ported from.

Quoted 8 lines

That's why I said this check should not go in, and the cdev code fixed to use the proper functions. That would enable you to have as long as a name as you wanted to.

thanks,

greg k-h

In the meantime, can we do something like:

fs/char_dev.c renamed from fs/char_dev.c.orig +1 -1
@@ -206,7 +206,7 @@
 
 	cdev->owner = fops->owner;
 	cdev->ops = fops;
-	strcpy(cdev->kobj.name, name);
+	strncpy(cdev->kobj.name, name, KOBJ_NAME_LEN-1);
 	for (s = strchr(cdev->kobj.name, '/'); s; s = strchr(s, '/'))
 		*s = '!';
 

Cheers,
Dick Johnson
Penguin : Linux version 2.6.8 on an i686 machine (5537.79 BogoMips).
Note 96.31% of all statistics are fiction.

#5 lore raw

fs/char_dev.c line 211

Quoted 13 lines · Richard B. Johnson

In the meantime, can we do something like:

fs/char_dev.c renamed from fs/char_dev.c.orig +1 -1
@@ -206,7 +206,7 @@
 
 	cdev->owner = fops->owner;
 	cdev->ops = fops;
-	strcpy(cdev->kobj.name, name);
+	strncpy(cdev->kobj.name, name, KOBJ_NAME_LEN-1);
 	for (s = strchr(cdev->kobj.name, '/'); s; s = strchr(s, '/'))
 		*s = '!';

No, that's still wrong. Use Stephen's other patch instead.

thanks,

greg k-h

#6 lore raw
In reply to Greg KH
Quoted 27 lines · Greg KH
Quoted 17 lines · Richard B. Johnson
Quoted 6 lines · Greg KH

Review

Quoted 1 line · Stephen Hemminger
(quoted diff) +1 -0
+	    strlen(name) >= KOBJ_NAME_LEN ||

There's no need for this check, if we fix the other usage of cdev->kobj.name in this file to use the proper kobject_name() and kobject_set_name() functions.

Well the module name is passed in register/unregister_chrdev(). It was not documented as the allowed length of the name so it was possible to install a device and then only "partially" uninstall the device so a subsequent open of the device-file would crash the kernel. A device name of :

"Octrangle Contrabulator" 23 characters

Quoted 8 lines

... in a test program was sufficiently-long to kill the kernel. I recommend truncating any name to an acceptable length. This would show up in /proc/iomem, etc., prompting the developer to shorten the name.

Also, the new length of 20 characters is probably too short. There was no such limitation on 2.4.x, where many modules are being ported from.

Quoted 7 lines

That's why I said this check should not go in, and the cdev code fixed to use the proper functions. That would enable you to have as long as a name as you wanted to.

thanks,

greg k-h

Okay. Thanks. Updating sources now.

Cheers,
Dick Johnson
Penguin : Linux version 2.6.8 on an i686 machine (5537.79 BogoMips).
Note 96.31% of all statistics are fiction.

#7 lore raw
In reply to Greg KH

Here is a better fix (thanks Greg) that allows long names for character device objects.

fs/char_dev.c +2 -2
diff -Nru a/fs/char_dev.c b/fs/char_dev.c
@@ -207,8 +207,8 @@
 
 	cdev->owner = fops->owner;
 	cdev->ops = fops;
-	strcpy(cdev->kobj.name, name);
-	for (s = strchr(cdev->kobj.name, '/'); s; s = strchr(s, '/'))
+	kobject_set_name(&cdev->kobj, "%s", name);
+	for (s = strchr(kobject_name(&cdev->kobj),'/'); s; s = strchr(s, '/'))
 		*s = '!';
 		
 	err = cdev_add(cdev, MKDEV(cd->major, 0), 256);
#8 lore raw
Quoted 4 lines · Stephen Hemminger

Here is a better fix (thanks Greg) that allows long names for character device objects.

Thanks, I'll add this to my trees, and send it to Linus after 2.6.9 is out (no in-kernel modules need this, so it isn't a real rush.)

thanks again,

greg k-h

#10 lore raw
In reply to Stephen Hemminger
Quoted 5 lines · Stephen Hemminger
# fs/char_dev.c
#   2004/10/08 09:51:52-07:00 [email protected] +5 -0
#   Protect against bad driver writers who pass invalid names when
#   setting up character devices.
# 

And how many badly mannered people check the return on this ? Shouldn't it just BUG() ?