Skip to contentSkip to conversation

fs/binfmt_elf: free interpreter in load_elf_binary

PATCH Superseded Liu Shixin 1 participant 2 messages Nov 4, 2020 1 message not in the archive

Subsystems (3)
lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

Liu Shixin

author
#1 lore raw

PATCH 1/1 fs/binfmt_elf: free interpreter in load_elf_binary

The file interpreter is allocated in load_elf_binary, but not freed in the case interp_elf_ex is NULL.
Add a label “out_allow_write_access” so that the interpreter will be appropriately released in this case.

This memory leak is catched when kmemleak is enabled in kernel, the report looks like below:

unreferenced object 0xffff8b6e9fd41400 (size 488):
comm "service", pid 4095, jiffies 4300970844 (age 49.618s) hex dump (first 32 bytes):

    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    e0 08 be b9 6e 8b ff ff 00 13 04 b7 6e 8b ff ff  ....n.......n...
  backtrace:
    [<00000000eacadaa2>] kmem_cache_alloc+0x164/0x320
    [<0000000090fb7bf2>] __alloc_file+0x2a/0x140
    [<00000000ff8fab86>] alloc_empty_file+0x4b/0x100
    [<000000003ab9b00d>] path_openat+0x4a/0xe20
    [<0000000027e3a067>] do_filp_open+0xb9/0x150
    [<000000000edebcac>] do_open_execat+0xa6/0x250
    [<000000008845564e>] open_exec+0x31/0x60
    [<00000000e6e6e1ca>] load_elf_binary+0x1dd/0x1b60
    [<000000004515d8f0>] do_execveat_common.isra.39+0xaa0/0x1000
    [<000000002ca5e83f>] __x64_sys_execve+0x37/0x40
    [<00000000beb519e4>] do_syscall_64+0x56/0xa0
    [<000000009cf54d51>] entry_SYSCALL_64_after_hwframe+0x44/0xa9
Fixes 0693ffebcfe5 ("fs/binfmt_elf.c: allocate less for static executable")
---
 fs/binfmt_elf.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
fs/binfmt_elf.c +2 -1
index fa50e8936f5f..28e75cb45b26 100644
@@ -907,7 +907,7 @@ static int load_elf_binary(struct linux_binprm *bprm)
 		interp_elf_ex = kmalloc(sizeof(*interp_elf_ex), GFP_KERNEL);
 		if (!interp_elf_ex) {
 			retval = -ENOMEM;
-			goto out_free_ph;
+			goto out_allow_write_access;
 		}
 
 		/* Get the exec headers */
@@ -1316,6 +1316,7 @@ static int load_elf_binary(struct linux_binprm *bprm)
 out_free_dentry:
 	kfree(interp_elf_ex);
 	kfree(interp_elf_phdata);
+out_allow_write_access:
 	allow_write_access(interpreter);
 	if (interpreter)
 		fput(interpreter);
Signature · 1 line
2.25.1
author
#2 lore raw

PATCH v2 1/1 fs/binfmt_elf: free interpreter in load_elf_binary

In reply to a message not in the archive

The file interpreter is allocated in load_elf_binary, but not freed in the case interp_elf_ex is NULL.
Add a label “out_allow_write_access” so that the interpreter will be appropriately released in this case.

This memory leak is catched when kmemleak is enabled in kernel, the report looks like below:

unreferenced object 0xffff8b6e9fd41400 (size 488):
comm "service", pid 4095, jiffies 4300970844 (age 49.618s) hex dump (first 32 bytes):

    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    e0 08 be b9 6e 8b ff ff 00 13 04 b7 6e 8b ff ff  ....n.......n...
  backtrace:
    [<00000000eacadaa2>] kmem_cache_alloc+0x164/0x320
    [<0000000090fb7bf2>] __alloc_file+0x2a/0x140
    [<00000000ff8fab86>] alloc_empty_file+0x4b/0x100
    [<000000003ab9b00d>] path_openat+0x4a/0xe20
    [<0000000027e3a067>] do_filp_open+0xb9/0x150
    [<000000000edebcac>] do_open_execat+0xa6/0x250
    [<000000008845564e>] open_exec+0x31/0x60
    [<00000000e6e6e1ca>] load_elf_binary+0x1dd/0x1b60
    [<000000004515d8f0>] do_execveat_common.isra.39+0xaa0/0x1000
    [<000000002ca5e83f>] __x64_sys_execve+0x37/0x40
    [<00000000beb519e4>] do_syscall_64+0x56/0xa0
    [<000000009cf54d51>] entry_SYSCALL_64_after_hwframe+0x44/0xa9
Fixes 0693ffebcfe5 ("fs/binfmt_elf.c: allocate less for static executable")
---
 fs/binfmt_elf.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
fs/binfmt_elf.c +2 -1
index fa50e8936f5f..28e75cb45b26 100644
@@ -907,7 +907,7 @@ static int load_elf_binary(struct linux_binprm *bprm)
 		interp_elf_ex = kmalloc(sizeof(*interp_elf_ex), GFP_KERNEL);
 		if (!interp_elf_ex) {
 			retval = -ENOMEM;
-			goto out_free_ph;
+			goto out_allow_write_access;
 		}
 
 		/* Get the exec headers */
@@ -1316,6 +1316,7 @@ static int load_elf_binary(struct linux_binprm *bprm)
 out_free_dentry:
 	kfree(interp_elf_ex);
 	kfree(interp_elf_phdata);
+out_allow_write_access:
 	allow_write_access(interpreter);
 	if (interpreter)
 		fput(interpreter);
Signature · 1 line
2.25.1