Skip to contentSkip to conversation

fs/binfmt_elf.c: fix a wrong free

PATCH Superseded WANG Cong 2 participants 5 messages May 3, 2008 – May 4, 2008

Subsystems (2)
lore
Share & tools
lore

WANG Cong

author
#0 lore raw

PATCH 1/1 fs/binfmt_elf.c: fix a wrong free

Fix a wrong free in fs/binfmt_elf.c::elf_core_dump().

Cc Alexander Viro <[email protected]>
Cc Eric Youngdale <[email protected]>
---
 fs/binfmt_elf.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
fs/binfmt_elf.c +1 -1
index b25707f..43254e3 100644
@@ -2032,10 +2032,10 @@ static int elf_core_dump(long signr, struct pt_regs *regs, struct file *file, un
 
 end_coredump:
 	set_fs(fs);
+	free_note_info(&info);
 
 cleanup:
 	kfree(elf);
-	free_note_info(&info);
 	return has_dumped;
 }
 
Signature · 1 line
1.5.4.1
#1 lore raw
Quoted 15 lines · WANG Cong

Fix a wrong free in fs/binfmt_elf.c::elf_core_dump().

Cc Alexander Viro <[email protected]>
Cc Eric Youngdale <[email protected]>
---
 fs/binfmt_elf.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
fs/binfmt_elf.c +0 -0
index b25707f..43254e3 100644

fs/binfmt_elf.c static int elf_core_dump(long signr, struct pt_regs *regs, struct file *file, un · line 2040

Quoted 10 lines
fs/binfmt_elf.c +1 -1
@@ -2032,10 +2032,10 @@ static int elf_core_dump(long signr, struct pt_regs *regs, struct file *file, un
 
 end_coredump:
 	set_fs(fs);
+	free_note_info(&info);
 
 cleanup:
 	kfree(elf);
-	free_note_info(&info);
 	return has_dumped;
 }

Looks like fill_note_info() requires that you call free_note_info() if it fails; otherwise we'll leak memory. So perhaps something like the following totally untested patch?

Pekka

fs/binfmt_elf.c +4 -2
index b25707f..febd208 100644
@@ -1917,7 +1917,7 @@ static int elf_core_dump(long signr, struct pt_regs *regs, struct file *file, un
 	 */
 	if (!fill_note_info(elf, segs + 1, /* including notes section */
 			    &info, signr, regs))
-		goto cleanup;
+		goto cleanup_note;
 
 	has_dumped = 1;
 	current->flags |= PF_DUMPCORE;
@@ -2033,9 +2033,11 @@ static int elf_core_dump(long signr, struct pt_regs *regs, struct file *file, un
 end_coredump:
 	set_fs(fs);
 
+cleanup_note:
+	free_note_info(&info);
+
 cleanup:
 	kfree(elf);
-	free_note_info(&info);
 	return has_dumped;
 }
 
author
#2 lore raw
Quoted 21 lines · Pekka J Enberg
Quoted 20 lines · WANG Cong

Fix a wrong free in fs/binfmt_elf.c::elf_core_dump().

Cc Alexander Viro <[email protected]>
Cc Eric Youngdale <[email protected]>
---
 fs/binfmt_elf.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
fs/binfmt_elf.c +1 -0
index b25707f..43254e3 100644
@@ -2032,10 +2032,10 @@ static int elf_core_dump(long signr, struct pt_regs *regs, struct file *file, un
 
 end_coredump:
 	set_fs(fs);
+	free_note_info(&info);
 
Quoted 10 lines

fs/binfmt_elf.c static int elf_core_dump(long signr, struct pt_regs *regs, struct file *file, un · line 2040

Quoted 5 lines
fs/binfmt_elf.c +0 -1
 cleanup:
 	kfree(elf);
-	free_note_info(&info);
 	return has_dumped;
 }

Looks like fill_note_info() requires that you call free_note_info() if it fails; otherwise we'll leak memory. So perhaps something like the following totally untested patch?

Hi, Pekka!

Thanks for your comments. Yes, it seems that fill_note_info() is ugly. :-) How about the below one?

---

fs/binfmt_elf.c +3 -2
index f6d5a9d..357b503 100644
@@ -1900,7 +1900,7 @@ static int elf_core_dump(long signr, struct pt_regs *regs, struct file *file, un
 	/* alloc memory for large data structures: too large to be on stack */
 	elf = kmalloc(sizeof(*elf), GFP_KERNEL);
 	if (!elf)
-		goto cleanup;
+		goto ret;
 	
 	segs = current->mm->map_count;
 #ifdef ELF_CORE_EXTRA_PHDRS
@@ -2034,8 +2034,9 @@ end_coredump:
 	set_fs(fs);
 
 cleanup:
-	kfree(elf);
 	free_note_info(&info);
+	kfree(elf);
+ret:
 	return has_dumped;
 }
 
#3 lore raw
Quoted 22 lines · WANG Cong

Hi, Pekka!

Thanks for your comments. Yes, it seems that fill_note_info() is ugly. :-) How about the below one?

---

fs/binfmt_elf.c +1 -1
index f6d5a9d..357b503 100644
@@ -1900,7 +1900,7 @@ static int elf_core_dump(long signr, struct pt_regs *regs, struct file *file, un
 	/* alloc memory for large data structures: too large to be on stack */
 	elf = kmalloc(sizeof(*elf), GFP_KERNEL);
 	if (!elf)
-		goto cleanup;
+		goto ret;
 
 	segs = current->mm->map_count;
 #ifdef ELF_CORE_EXTRA_PHDRS

fs/binfmt_elf.c end_coredump: · line 2040

Quoted 8 lines
fs/binfmt_elf.c +2 -1
@@ -2034,8 +2034,9 @@ end_coredump:
 	set_fs(fs);
 
 cleanup:
-	kfree(elf);
 	free_note_info(&info);
+	kfree(elf);
+ret:
 	return has_dumped;

Looks good although 'ret' is usually reserved for the variable that contains the return value so you might want to consider using the more idiomatic 'out' as the label name.

Pekka

author
#4 lore raw
Quoted 4 lines · Pekka Enberg

Looks good although 'ret' is usually reserved for the variable that contains the return value so you might want to consider using the more idiomatic 'out' as the label name.

Thanks. Done.

---

fs/binfmt_elf.c +3 -2
index f6d5a9d..357b503 100644
@@ -1900,7 +1900,7 @@ static int elf_core_dump(long signr, struct pt_regs *regs, struct file *file, un
 	/* alloc memory for large data structures: too large to be on stack */
 	elf = kmalloc(sizeof(*elf), GFP_KERNEL);
 	if (!elf)
-		goto cleanup;
+		goto out;
 	
 	segs = current->mm->map_count;
 #ifdef ELF_CORE_EXTRA_PHDRS
@@ -2034,8 +2034,9 @@ end_coredump:
 	set_fs(fs);
 
 cleanup:
-	kfree(elf);
 	free_note_info(&info);
+	kfree(elf);
+out:
 	return has_dumped;
 }