elf: fix "start_code" evaluation
Subsystems (2)
PATCH 1/1 elf: fix "start_code" evaluation
Only executable ELF program headers should change ->start_code.
---
fs/binfmt_elf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/binfmt_elf.c +1 -1
@@ -1026,7 +1026,7 @@ static int load_elf_binary(struct linux_binprm *bprm) } } k = elf_ppnt->p_vaddr; -if (k < start_code) +if ((elf_ppnt->p_flags &PF_X ) && k < start_code) start_code = k;if (start_data < k) start_data = k;
Quoted 7 lines · On Thu, 23 May 2019 20:57:36 +0300 Alexey Dobriyan <[email protected]> wrote:
Only executable ELF program headers should change ->start_code.
...
fs/binfmt_elf.c static int load_elf_binary(struct linux_binprm *bprm) · line 1032
Quoted 8 lines
fs/binfmt_elf.c +1
-1@@ -1026,7 +1026,7 @@ static int load_elf_binary(struct linux_binprm *bprm) } } k = elf_ppnt->p_vaddr; -if (k < start_code) +if ((elf_ppnt->p_flags &PF_X ) && k < start_code) start_code = k;if (start_data < k) start_data = k;
What problem does this solve? How does it alter runtime behaviour? How do we know it won't break anything?
Quoted 6 lines · Andrew Morton
Quoted 4 lines · On Thu, 23 May 2019 20:57:36 +0300 Alexey Dobriyan <[email protected]> wrote:
Only executable ELF program headers should change ->start_code.
...
Quoted 13 lines
fs/binfmt_elf.c static int load_elf_binary(struct linux_binprm *bprm) · line 1032
Quoted 11 lines
fs/binfmt_elf.c +1
-1@@ -1026,7 +1026,7 @@ static int load_elf_binary(struct linux_binprm *bprm) } } k = elf_ppnt->p_vaddr; -if (k < start_code) +if ((elf_ppnt->p_flags &PF_X ) && k < start_code) start_code = k;if (start_data < k) start_data = k;What problem does this solve?
It is a bug. Look at the ->end_code update:
if ((elf_ppnt->p_flags & PF_X) && end_code < k) end_code = k;
Quoted 1 line
How does it alter runtime behaviour?
It makes "VmExe" and "VmLib" accounting more accurate for common case.
Quoted 1 line
How do we know it won't break anything?
We don't. Some distros are unaffected because they ship binaries with first PT_LOAD segment being executable (Debian 8). Some don't.
Regardless, these fields are lies: ELF binary can have multiple disjoint PT_LOAD segments, but all those ->start and ->end fields assume everything is mapped together.
Hopefully nobody actually uses them for anything serious.