Skip to contentSkip to conversation

[2.4] init/do_mounts.c::rd_load_image() memleak

PATCH Oleg Drokin 8 participants 11 messages Mar 13, 2003 – Mar 15, 2003 VFS

lore
Share & tools
lore
#0 lore raw

Hello!

rd_load_image() leaks some memory if it cannot determine source device size, if it cannot close or open source for ramdisk device.

Probably this is not all that critical, since we most likely panic after failure to load initrd, but still there is chance that we have valid root device too, from which we can try to continue to boot.

Found with help of smatch + enhanced unfree script.

Bye,
Oleg

===== init/do_mounts.c 1.35 vs edited =====

init/do_mounts.c +3 -2
@@ -551,7 +551,7 @@
 	int in_fd, out_fd;
 	unsigned long rd_blocks, devblocks;
 	int nblocks, i;
-	char *buf;
+	char *buf = 0;
 	unsigned short rotate = 0;
 #if !defined(CONFIG_ARCH_S390) && !defined(CONFIG_PPC_ISERIES)
 	char rotator[4] = { '|' , '/' , '-' , '\\' };
@@ -648,7 +648,6 @@
 #endif
 	}
 	printk("done.\n");
-	kfree(buf);
 
 successful_load:
 	res = 1;
@@ -656,6 +655,8 @@
 	close(in_fd);
 noclose_input:
 	close(out_fd);
+	if (buf)
+		kfree(buf);
 out:
 	sys_unlink("/dev/ram");
 #endif
#1 lore raw

init/do_mounts.c line 659

Quoted 2 lines · Oleg Drokin
init/do_mounts.c +2 -0
+	if (buf)
+		kfree(buf);

kfree(NULL); is valid - you don't need this check.

Signature · 2 lines
Russell King ([email protected])                The developer of ARM Linux
             http://www.arm.linux.org.uk/personal/aboutme.html
#2 lore raw

Hello!

Quoted 3 lines · Russell King

init/do_mounts.c line 659

Quoted 2 lines
init/do_mounts.c +2 -0
+	if (buf)
+		kfree(buf);

kfree(NULL); is valid - you don't need this check.

Almost every place I can think of does just this, so I do not see why this particular piece of code should be different.

Bye,
Oleg

#3 lore raw
Quoted 10 lines · Oleg Drokin

Hello!

Quoted 3 lines · Russell King

init/do_mounts.c line 659

Quoted 2 lines
init/do_mounts.c +2 -0
+	if (buf)
+		kfree(buf);

kfree(NULL); is valid - you don't need this check.

Almost every place I can think of does just this, so I do not see why this particular piece of code should be different.

Since when has that been a valid argument? :)

Signature · 1 line
Jens Axboe
#4 lore raw

Hello!

Quoted 6 lines · Jens Axboe
Quoted 5 lines
Quoted 3 lines

init/do_mounts.c line 659

Quoted 2 lines
init/do_mounts.c +2 -0
+	if (buf)
+		kfree(buf);

kfree(NULL); is valid - you don't need this check.

Almost every place I can think of does just this, so I do not see why this particular piece of code should be different.

Since when has that been a valid argument? :)

Well, my argument is code uniformness which was always valid as long as it does not introduce any bugs, I think.
Do you propose somebody should go and fix all
if ( something )

	kfree(something);

pieces of code to read just
kfree(something); ?

Bye,
Oleg

#5 lore raw
Quoted 7 lines · Oleg Drokin

Hello!

Quoted 3 lines · Jens Axboe
Quoted 3 lines
Quoted 3 lines

init/do_mounts.c line 659

Quoted 2 lines
init/do_mounts.c +2 -0
+	if (buf)
+		kfree(buf);

kfree(NULL); is valid - you don't need this check.

Quoted 6 lines
Quoted 3 lines
Quoted 2 lines

Almost every place I can think of does just this, so I do not see why this particular piece of code should be different.

Since when has that been a valid argument? :)

Well, my argument is code uniformness which was always valid as long as it does not introduce any bugs, I think.

I agree with that.

Quoted 5 lines

Do you propose somebody should go and fix all
if ( something )

	kfree(something);

pieces of code to read just
kfree(something); ?

No that would just be another pointless exercise in causing more annoyance for someone who has to look through patches finding that one hunk that breaks stuff. The recent spelling changes come to mind.

But just because you don't seem to have seen any kfree(NULL) in the kernel does not mean they are not there. And should a good trend not allow to grow?

Signature · 1 line
Jens Axboe
#9 lore raw
In reply to Oleg Drokin
Quoted 7 lines · Oleg Drokin

Well, my argument is code uniformness which was always valid as long as it does not introduce any bugs, I think.
Do you propose somebody should go and fix all
if ( something )

	kfree(something);

pieces of code to read just
kfree(something); ?

It's defined that kfree(NULL) is valid. I tried the above mentioned 'cleanup' a while ago, way too noisy.

Zwane

#6 lore raw
In reply to Jens Axboe
Quoted 3 lines · Jens Axboe

No that would just be another pointless exercise in causing more annoyance for someone who has to look through patches finding that one hunk that breaks stuff. The recent spelling changes come to mind.

How we should do such global small cleanups? Maybe grep the source and bring the list of affected files to maintainers' attention, letting the to gradually push changes to Linus...

I suspect "bring the list to maintainers' attention" will be a trickier part ;)

Quoted 3 lines

But just because you don't seem to have seen any kfree(NULL) in the kernel does not mean they are not there. And should a good trend not allow to grow?

"if(p) free(p)" => "free(p)" is mostly ok, less code.

But free is called now unconditionally. Make an exception for performance-critical places where p is almost always 0. --
vda

#7 lore raw
Quoted 13 lines · Denis Vlasenko
Quoted 3 lines · Jens Axboe

No that would just be another pointless exercise in causing more annoyance for someone who has to look through patches finding that one hunk that breaks stuff. The recent spelling changes come to mind.

How we should do such global small cleanups? Maybe grep the source and bring the list of affected files to maintainers' attention, letting the to gradually push changes to Linus...

I suspect "bring the list to maintainers' attention" will be a trickier part ;)

Quoted 8 lines
Quoted 3 lines

But just because you don't seem to have seen any kfree(NULL) in the kernel does not mean they are not there. And should a good trend not allow to grow?

"if(p) free(p)" => "free(p)" is mostly ok, less code.

But free is called now unconditionally. Make an exception for performance-critical places where p is almost always 0.

The one implementation I looked at carefully (SAS/C) looked like this:

 	free(p);
	---------
	if (p)
		malloc(p);

so

	if (p) free(p);
	-------------
	if (p)
	     if (p)
		malloc(p);

which seems fairly worthless.

john

#10 lore raw
In reply to Oleg Drokin
Quoted 8 lines · Oleg Drokin
Quoted 4 lines · Russell King

init/do_mounts.c line 659

Quoted 2 lines
init/do_mounts.c +2 -0
+	if (buf)
+		kfree(buf);

kfree(NULL); is valid - you don't need this check.

Almost every place I can think of does just this, so I do not see why this particular piece of code should be different.

Then the other code should be fixed. This is bloat.

Signature · 4 lines
Dr. Horst H. von Brand                   User #22616 counter.li.org
Departamento de Informatica                     Fono: +56 32 654431
Universidad Tecnica Federico Santa Maria              +56 32 654239
Casilla 110-V, Valparaiso, Chile                Fax:  +56 32 797513
#8 lore raw
In reply to Jens Axboe
Quoted 1 line · Jens Axboe

On Fri, Mar 14 2003, Oleg Drokin wrote:

[...]

Quoted 9 lines
Quoted 5 lines

Do you propose somebody should go and fix all
if ( something )

	kfree(something);

pieces of code to read just
kfree(something); ?

No that would just be another pointless exercise in causing more annoyance for someone who has to look through patches finding that one hunk that breaks stuff. The recent spelling changes come to mind.

By that standard, most janitorial patches should be ditched. That way, no cruft will ever be removed.

Instead of this extremist position, some way should be found that minimizes this kind of friction.

Signature · 4 lines
Dr. Horst H. von Brand                   User #22616 counter.li.org
Departamento de Informatica                     Fono: +56 32 654431
Universidad Tecnica Federico Santa Maria              +56 32 654239
Casilla 110-V, Valparaiso, Chile                Fax:  +56 32 797513