Skip to contentSkip to conversation

binfmt_elf: Do not move brk for INTERP-less ET_EXEC

PATCH In review Kees Cook 2 participants 4 messages Sep 26, 2019 – Sep 27, 2019

Subsystems (2)
lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH 1/1 binfmt_elf: Do not move brk for INTERP-less ET_EXEC

When brk was moved for binaries without an interpreter, it should have been limited to ET_DYN only. In other words, the special case was an ET_DYN that lacks an INTERP, not just an executable that lacks INTERP. The bug manifested for giant static executables, where the brk would end up in the middle of the text area on 32-bit architectures.

Fixes bbdc6076d2e5 ("binfmt_elf: move brk out of mmap when doing direct loader exec")

---
Richard, are you able to test this? I'm able to run the gitea binary with this change, and my INTERP-less ET_DYN tests (from the original bug) continue to pass as well.

---
 fs/binfmt_elf.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
fs/binfmt_elf.c +2 -1
index cec3b4146440..ad4c6b1d5074 100644
@@ -1121,7 +1121,8 @@ static int load_elf_binary(struct linux_binprm *bprm)
 		 * (since it grows up, and may collide early with the stack
 		 * growing down), and into the unused ELF_ET_DYN_BASE region.
 		 */
-		if (IS_ENABLED(CONFIG_ARCH_HAS_ELF_RANDOMIZE) && !interpreter)
+		if (IS_ENABLED(CONFIG_ARCH_HAS_ELF_RANDOMIZE) &&
+		    loc->elf_ex.e_type == ET_DYN && !interpreter)
 			current->mm->brk = current->mm->start_brk =
 				ELF_ET_DYN_BASE;
 
Signature · 5 lines
2.17.1


-- 
Kees Cook
#1 lore raw

Hi Kees,

Thanks for the quick patch. It seems my binaries start up well, and work as expected, as before.

Thanks again for the quick response.

Regards,
Richard Kojedzinszky

2019-09-26 19:15 időpontban Kees Cook ezt írta:

Quoted 36 lines

When brk was moved for binaries without an interpreter, it should have been limited to ET_DYN only. In other words, the special case was an ET_DYN that lacks an INTERP, not just an executable that lacks INTERP. The bug manifested for giant static executables, where the brk would end up in the middle of the text area on 32-bit architectures.

Fixes bbdc6076d2e5 ("binfmt_elf: move brk out of mmap when doing

direct loader exec")

---
Richard, are you able to test this? I'm able to run the gitea binary with this change, and my INTERP-less ET_DYN tests (from the original bug) continue to pass as well.

---
 fs/binfmt_elf.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
fs/binfmt_elf.c +2 -2
index cec3b4146440..ad4c6b1d5074 100644
@@ -1121,7 +1121,8 @@ static int load_elf_binary(struct linux_binprm 
 *bprm)
 		 * (since it grows up, and may collide early with the stack
 		 * growing down), and into the unused ELF_ET_DYN_BASE region.
 		 */
-		if (IS_ENABLED(CONFIG_ARCH_HAS_ELF_RANDOMIZE) && !interpreter)
+		if (IS_ENABLED(CONFIG_ARCH_HAS_ELF_RANDOMIZE) &&
+		    loc->elf_ex.e_type == ET_DYN && !interpreter)
 			current->mm->brk = current->mm->start_brk =
 				ELF_ET_DYN_BASE;
 
--

2.17.1

author
#2 lore raw
Quoted 4 lines · Richard Kojedzinszky

Thanks for the quick patch. It seems my binaries start up well, and work as expected, as before.

Thanks again for the quick response.

Awesome; thanks for the testing (and sorry for the breakage)! :)

-Kees

Quoted 41 lines

Regards,
Richard Kojedzinszky

2019-09-26 19:15 időpontban Kees Cook ezt írta:

Quoted 36 lines

When brk was moved for binaries without an interpreter, it should have been limited to ET_DYN only. In other words, the special case was an ET_DYN that lacks an INTERP, not just an executable that lacks INTERP. The bug manifested for giant static executables, where the brk would end up in the middle of the text area on 32-bit architectures.

Fixes bbdc6076d2e5 ("binfmt_elf: move brk out of mmap when doing

direct loader exec")

---
Richard, are you able to test this? I'm able to run the gitea binary with this change, and my INTERP-less ET_DYN tests (from the original bug) continue to pass as well.

---
 fs/binfmt_elf.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
fs/binfmt_elf.c +2 -2
index cec3b4146440..ad4c6b1d5074 100644
@@ -1121,7 +1121,8 @@ static int load_elf_binary(struct linux_binprm
 *bprm)
 		 * (since it grows up, and may collide early with the stack
 		 * growing down), and into the unused ELF_ET_DYN_BASE region.
 		 */
-		if (IS_ENABLED(CONFIG_ARCH_HAS_ELF_RANDOMIZE) && !interpreter)
+		if (IS_ENABLED(CONFIG_ARCH_HAS_ELF_RANDOMIZE) &&
+		    loc->elf_ex.e_type == ET_DYN && !interpreter)
 			current->mm->brk = current->mm->start_brk =
 				ELF_ET_DYN_BASE;
 
--

2.17.1

Signature · 1 line
Kees Cook
#3 lore raw

Hi,

Thanks for the fix, again. I am not familiar with the code here, I suspect the intent was to fix or improve something. That happens sometimes, that things get broken. I am happy that I could report it, and it got fixed quickly.

Thanks again,
Richard

2019-09-26 20:37 időpontban Kees Cook ezt írta:

Quoted 51 lines · Richard Kojedzinszky
Quoted 4 lines

Thanks for the quick patch. It seems my binaries start up well, and work as expected, as before.

Thanks again for the quick response.

Awesome; thanks for the testing (and sorry for the breakage)! :)

-Kees

Quoted 41 lines

Regards,
Richard Kojedzinszky

2019-09-26 19:15 időpontban Kees Cook ezt írta:

Quoted 36 lines

When brk was moved for binaries without an interpreter, it should have been limited to ET_DYN only. In other words, the special case was an ET_DYN that lacks an INTERP, not just an executable that lacks INTERP. The bug manifested for giant static executables, where the brk would end up in the middle of the text area on 32-bit architectures.

Fixes bbdc6076d2e5 ("binfmt_elf: move brk out of mmap when doing

direct loader exec")

---
Richard, are you able to test this? I'm able to run the gitea binary with this change, and my INTERP-less ET_DYN tests (from the original bug) continue to pass as well.

---
 fs/binfmt_elf.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
fs/binfmt_elf.c +2 -2
index cec3b4146440..ad4c6b1d5074 100644
@@ -1121,7 +1121,8 @@ static int load_elf_binary(struct linux_binprm
 *bprm)
 		 * (since it grows up, and may collide early with the stack
 		 * growing down), and into the unused ELF_ET_DYN_BASE region.
 		 */
-		if (IS_ENABLED(CONFIG_ARCH_HAS_ELF_RANDOMIZE) && !interpreter)
+		if (IS_ENABLED(CONFIG_ARCH_HAS_ELF_RANDOMIZE) &&
+		    loc->elf_ex.e_type == ET_DYN && !interpreter)
 			current->mm->brk = current->mm->start_brk =
 				ELF_ET_DYN_BASE;
 
--

2.17.1