Skip to content

Commit e7fd154

Browse files
oleg-nesterovtorvalds
authored andcommitted
coredump: format_corename() can leak cn->corename
do_coredump() assumes that format_corename() can only fail if expand_corename() fails and frees cn->corename. This is not true, for example cn_print_exe_file() can fail and in this case nobody frees cn->corename. Change do_coredump() to always do kfree(cn->corename) after it calls format_corename() (NULL is fine), change expand_corename() to do nothing if kmalloc() fails. Signed-off-by: Oleg Nesterov <oleg@redhat.com> Cc: Andi Kleen <andi@firstfloor.org> Cc: Colin Walters <walters@verbum.org> Cc: Denys Vlasenko <vda.linux@googlemail.com> Cc: Jiri Slaby <jslaby@suse.cz> Cc: Lennart Poettering <mzxreary@0pointer.de> Cc: Lucas De Marchi <lucas.de.marchi@gmail.com> Acked-by: Neil Horman <nhorman@tuxdriver.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 7f57cfa commit e7fd154

1 file changed

Lines changed: 7 additions & 11 deletions

File tree

‎fs/coredump.c‎

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,14 @@ static atomic_t call_count = ATOMIC_INIT(1);
5858

5959
static int expand_corename(struct core_name *cn)
6060
{
61-
char *old_corename = cn->corename;
61+
int size = CORENAME_MAX_SIZE * atomic_inc_return(&call_count);
62+
char *corename = krealloc(cn->corename, size, GFP_KERNEL);
6263

63-
cn->size = CORENAME_MAX_SIZE * atomic_inc_return(&call_count);
64-
cn->corename = krealloc(old_corename, cn->size, GFP_KERNEL);
65-
66-
if (!cn->corename) {
67-
kfree(old_corename);
64+
if (!corename)
6865
return -ENOMEM;
69-
}
7066

67+
cn->size = size;
68+
cn->corename = corename;
7169
return 0;
7270
}
7371

@@ -157,10 +155,9 @@ static int format_corename(struct core_name *cn, struct coredump_params *cprm)
157155
int pid_in_pattern = 0;
158156
int err = 0;
159157

158+
cn->used = 0;
160159
cn->size = CORENAME_MAX_SIZE * atomic_read(&call_count);
161160
cn->corename = kmalloc(cn->size, GFP_KERNEL);
162-
cn->used = 0;
163-
164161
if (!cn->corename)
165162
return -ENOMEM;
166163

@@ -549,7 +546,7 @@ void do_coredump(siginfo_t *siginfo)
549546
if (ispipe < 0) {
550547
printk(KERN_WARNING "format_corename failed\n");
551548
printk(KERN_WARNING "Aborting core\n");
552-
goto fail_corename;
549+
goto fail_unlock;
553550
}
554551

555552
if (cprm.limit == 1) {
@@ -669,7 +666,6 @@ void do_coredump(siginfo_t *siginfo)
669666
atomic_dec(&core_dump_count);
670667
fail_unlock:
671668
kfree(cn.corename);
672-
fail_corename:
673669
coredump_finish(mm, core_dumped);
674670
revert_creds(old_cred);
675671
fail_creds:

0 commit comments

Comments
 (0)