Skip to contentSkip to conversation

Fix a rare memory leak in kobject_set_name_vargs

PATCH In review Philippe De Muyter 3 participants 4 messages Mar 13, 2010

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Fix a rare memory leak in kobject_set_name_vargs

Hello Greg,

This is a possible memory leak that I discovered only by accidental code reading.

--

If kvasprintf fails in kobject_set_name_vargs, the memory used by the original kobj->name is leaked. Fix that. I also avoid useless memory accesses to kobj->name by using the local variables old_name and new_name instead.

linux-2.6.x/lib/kobject.c +6 -4
diff -r 373fdd3df333 linux-2.6.x/lib/kobject.c
@@ -216,20 +216,22 @@ int kobject_set_name_vargs(struct kobjec
 				  va_list vargs)
 {
 	const char *old_name = kobj->name;
+	char *new_name;
 	char *s;
 
-	if (kobj->name && !fmt)
+	if (old_name && !fmt)
 		return 0;
 
-	kobj->name = kvasprintf(GFP_KERNEL, fmt, vargs);
-	if (!kobj->name)
+	new_name = kvasprintf(GFP_KERNEL, fmt, vargs);
+	if (!new_name)
 		return -ENOMEM;
 
 	/* ewww... some of these buggers have '/' in the name ... */
-	while ((s = strchr(kobj->name, '/')))
+	while ((s = strchr(new_name, '/')))
 		s[0] = '!';
 
 	kfree(old_name);
+	kobj->name = new_name;
 	return 0;
 }
 
#1 lore raw
Quoted 33 lines · Philippe De Muyter

Hello Greg,

This is a possible memory leak that I discovered only by accidental code reading.

--

If kvasprintf fails in kobject_set_name_vargs, the memory used by the original kobj->name is leaked. Fix that. I also avoid useless memory accesses to kobj->name by using the local variables old_name and new_name instead.

diff -r 373fdd3df333 linux-2.6.x/lib/kobject.c

linux-2.6.x/lib/kobject.c +4 -3
@@ -216,20 +216,22 @@ int kobject_set_name_vargs(struct kobjec
 				  va_list vargs)
 {
 	const char *old_name = kobj->name;
+	char *new_name;
 	char *s;
 
-	if (kobj->name && !fmt)
+	if (old_name && !fmt)
 		return 0;
 
-	kobj->name = kvasprintf(GFP_KERNEL, fmt, vargs);
-	if (!kobj->name)
+	new_name = kvasprintf(GFP_KERNEL, fmt, vargs);
+	if (!new_name)
 		return -ENOMEM;

linux-2.6.x/lib/kobject.c int kobject_set_name_vargs(struct kobjec · line 236

Quoted 10 lines
linux-2.6.x/lib/kobject.c +2 -1
 
 	/* ewww... some of these buggers have '/' in the name ... */
-	while ((s = strchr(kobj->name, '/')))
+	while ((s = strchr(new_name, '/')))
 		s[0] = '!';
 
 	kfree(old_name);
+	kobj->name = new_name;
 	return 0;
 }

the routine kobject_set_name_vargs() is described in
Documentation/kobject.txt as "legacy cruft" to be removed at some point, so it's not clear there's any value in "fixing" it.

rday
--

========================================================================
Robert P. J. Day Waterloo, Ontario, CANADA

Linux Consulting, Training and Kernel Pedantry.

Web page:                                          http://crashcourse.ca
Twitter:                                       http://twitter.com/rpjday

========================================================================

#2 lore raw
Quoted 41 lines · Robert P. J. Day
Quoted 39 lines · Philippe De Muyter

Hello Greg,

This is a possible memory leak that I discovered only by accidental code reading.

--

If kvasprintf fails in kobject_set_name_vargs, the memory used by the original kobj->name is leaked. Fix that. I also avoid useless memory accesses to kobj->name by using the local variables old_name and new_name instead.

diff -r 373fdd3df333 linux-2.6.x/lib/kobject.c

linux-2.6.x/lib/kobject.c +5 -4
@@ -216,20 +216,22 @@ int kobject_set_name_vargs(struct kobjec
 				  va_list vargs)
 {
 	const char *old_name = kobj->name;
+	char *new_name;
 	char *s;
 
-	if (kobj->name && !fmt)
+	if (old_name && !fmt)
 		return 0;
 
-	kobj->name = kvasprintf(GFP_KERNEL, fmt, vargs);
-	if (!kobj->name)
+	new_name = kvasprintf(GFP_KERNEL, fmt, vargs);
+	if (!new_name)
 		return -ENOMEM;
 
 	/* ewww... some of these buggers have '/' in the name ... */
-	while ((s = strchr(kobj->name, '/')))
+	while ((s = strchr(new_name, '/')))
 		s[0] = '!';
 
Quoted 9 lines

linux-2.6.x/lib/kobject.c int kobject_set_name_vargs(struct kobjec · line 236

Quoted 4 lines
linux-2.6.x/lib/kobject.c +1 -0
 	kfree(old_name);
+	kobj->name = new_name;
 	return 0;
 }

the routine kobject_set_name_vargs() is described in
Documentation/kobject.txt as "legacy cruft" to be removed at some point, so it's not clear there's any value in "fixing" it.

Given I submitted a similar patch two days before, I guess a fix would be welcome or else we might see one or two attempts per week from various people.

http://lkml.org/lkml/2010/3/11/438

Legacy or not, this code looks wrong. I caught it while looking for kmemleaks reports on my dev machine, that were triggered by CONFIG_NO_BOOTMEM use.

#3 lore raw
Quoted 56 lines · Eric Dumazet
Quoted 41 lines · Robert P. J. Day
Quoted 39 lines · Philippe De Muyter

Hello Greg,

This is a possible memory leak that I discovered only by accidental code reading.

--

If kvasprintf fails in kobject_set_name_vargs, the memory used by the original kobj->name is leaked. Fix that. I also avoid useless memory accesses to kobj->name by using the local variables old_name and new_name instead.

diff -r 373fdd3df333 linux-2.6.x/lib/kobject.c

linux-2.6.x/lib/kobject.c +5 -4
@@ -216,20 +216,22 @@ int kobject_set_name_vargs(struct kobjec
 				  va_list vargs)
 {
 	const char *old_name = kobj->name;
+	char *new_name;
 	char *s;
 
-	if (kobj->name && !fmt)
+	if (old_name && !fmt)
 		return 0;
 
-	kobj->name = kvasprintf(GFP_KERNEL, fmt, vargs);
-	if (!kobj->name)
+	new_name = kvasprintf(GFP_KERNEL, fmt, vargs);
+	if (!new_name)
 		return -ENOMEM;
 
 	/* ewww... some of these buggers have '/' in the name ... */
-	while ((s = strchr(kobj->name, '/')))
+	while ((s = strchr(new_name, '/')))
 		s[0] = '!';
 
Quoted 9 lines

linux-2.6.x/lib/kobject.c int kobject_set_name_vargs(struct kobjec · line 236

Quoted 4 lines
linux-2.6.x/lib/kobject.c +1 -0
 	kfree(old_name);
+	kobj->name = new_name;
 	return 0;
 }

the routine kobject_set_name_vargs() is described in
Documentation/kobject.txt as "legacy cruft" to be removed at some point, so it's not clear there's any value in "fixing" it.

Given I submitted a similar patch two days before, I guess a fix would be welcome or else we might see one or two attempts per week from various people.

Quoted 5 lines

http://lkml.org/lkml/2010/3/11/438

Legacy or not, this code looks wrong. I caught it while looking for kmemleaks reports on my dev machine, that were triggered by CONFIG_NO_BOOTMEM use.

fair enough, i was just going off of what i read in Doc/kobject.txt.

rday
--

========================================================================
Robert P. J. Day Waterloo, Ontario, CANADA

Linux Consulting, Training and Kernel Pedantry.

Web page:                                          http://crashcourse.ca
Twitter:                                       http://twitter.com/rpjday

========================================================================