Skip to contentSkip to conversation

PATCH -- kobject_set_name() doesn't allocate enough space

PATCH Linda Xie 2 participants 3 messages Dec 16, 2003

Subsystems (2)
lore
Share & tools
lore

Linda Xie

author
#0 lore raw

Hi All,

The sapce allocated in kobject_set_name() is 1 byte less than it should be. The Attached patch fixes this bug.

Comments are welcome.

Thanks,

Linda Xie
IBM Linux Technology Center

lib/kobject.c +2 -2
diff -Nru a/lib/kobject.c b/lib/kobject.c
@@ -344,12 +344,12 @@
 		/*
 		 * Need more space? Allocate it and try again
 		 */
-		name = kmalloc(need,GFP_KERNEL);
+		limit = need + 1;
+		name = kmalloc(limit,GFP_KERNEL);
 		if (!name) {
 			error = -ENOMEM;
 			goto Done;
 		}
-		limit = need;
 		need = vsnprintf(name,limit,fmt,args);
 
 		/* Still? Give up. */
#1 lore raw
Quoted 3 lines · Linda Xie

The sapce allocated in kobject_set_name() is 1 byte less than it should be. The Attached patch fixes this bug.

Good catch - it _should_ mean that long names always had the last byte cut off. Why didn't anybody notice? Are people just not using long names?

Quoted 1 line

Comments are welcome.

The patch looks correct, but you should change the last test to be appropriate too, ie the

	/* Still? Give up. */
	if (need > limit) {

test should, as far as I can tell, be

	if (need >= limit) {

instead.

Linus

author
#2 lore raw
Quoted 7 lines · Linus Torvalds

The patch looks correct, but you should change the last test to be appropriate too, ie the

	/* Still? Give up. */
	if (need > limit) {
Quoted 7 lines

test should, as far as I can tell, be

	if (need >= limit) {

instead.

Linus

Hi Linus,

Thank you for pointing that out. Here is the updated patch:

lib/kobject.c Tue Dec 16 17:10:16 2003 +3 -3
diff -Nru a/lib/kobject.c b/lib/kobject.c
@@ -344,16 +344,16 @@
                /*
                 * Need more space? Allocate it and try again
                 */
-               name = kmalloc(need,GFP_KERNEL);
+               limit = need + 1;
+               name = kmalloc(limit,GFP_KERNEL);
                if (!name) {
                        error = -ENOMEM;
                        goto Done;
                }
-               limit = need;
                need = vsnprintf(name,limit,fmt,args);
 
                /* Still? Give up. */
-               if (need > limit) {
+               if (need >= limit) {
                        kfree(name);
                        error = -EFAULT;
                        goto Done;