Skip to contentSkip to conversation

binfmt_elf: Fix PIE execution with randomization disabled

RFC PATCH In review Josh Boyer 5 participants 19 messages Sep 29, 2011 – Oct 8, 2011

Subsystems (2)
lore
Share & tools
lore

Josh Boyer

author
#0 lore raw

RFC PATCH 1/1 binfmt_elf: Fix PIE execution with randomization disabled

We've had a bug report[1] of some PIE programs getting a SIGKILL upon exec if you disable address randomization with:

echo 0 > /proc/sys/kernel/randomize_va_space

I tracked this down to get_unmapped_area_prot returning -ENOMEM because the address being passed in is larger than TASK_SIZE - len for the bss section of the test executable. That filters back to set_brk returning an error to load_elf_binary and the SIGKILL being sent around line 872 of binfmt_elf.c.

H.J. submitted an upstream bug report [2] as well, but got no feedback and we can't view it with kernel.org being down anyway. He came up with the patch below as well, which is what I'm sending on for comments. The changelog is my addition, so if that is wrong yell at me.

I wanted to get some more eyes on this, because the current code sets load_bias to 0 unconditionally on CONFIG_X86 or CONFIG_ARM. I have no idea why that is. The original execshield patches had an #ifdef on __i386__ but the patch that was commited to add PIE support has the CONFIG_X86 setting.

Thoughts welcome.

[1] https://bugzilla.redhat.com/show_bug.cgi?id=708563
[2] http://bugzilla.kernel.org/show_bug.cgi?id=36372

josh

---

From: H.J. Lu <[email protected]>

Set the load_bias for PIE executables to a non-zero address if no virtual address is specified. This prevents us from running out of room for all the various loadable segments when ASLR is disabled.

---

fs/binfmt_elf.c +7 -2
index 303983f..069ee29 100644
@@ -794,9 +794,14 @@ static int load_elf_binary(struct linux_binprm *bprm, struct pt_regs *regs)
 			/* Try and get dynamic programs out of the way of the
 			 * default mmap base, as well as whatever program they
 			 * might try to exec.  This is because the brk will
-			 * follow the loader, and is not movable.  */
+			 * follow the loader, and is not movable.  Don't use
+			 * 0 load address since we may not have room for
+			 * all loadable segements.  */
 #if defined(CONFIG_X86) || defined(CONFIG_ARM)
-			load_bias = 0;
+			if (vaddr)
+				load_bias = 0;
+			else
+				load_bias = ELF_PAGESTART(ELF_ET_DYN_BASE);
 #else
 			load_bias = ELF_PAGESTART(ELF_ET_DYN_BASE - vaddr);
 #endif
#1 lore raw
Quoted 16 lines · On Thu, 29 Sep 2011 15:53:59 -0400 Josh Boyer <[email protected]> wrote:

We've had a bug report[1] of some PIE programs getting a SIGKILL upon exec if you disable address randomization with:

echo 0 > /proc/sys/kernel/randomize_va_space

I tracked this down to get_unmapped_area_prot returning -ENOMEM because the address being passed in is larger than TASK_SIZE - len for the bss section of the test executable. That filters back to set_brk returning an error to load_elf_binary and the SIGKILL being sent around line 872 of binfmt_elf.c.

H.J. submitted an upstream bug report [2] as well, but got no feedback and we can't view it with kernel.org being down anyway. He came up with the patch below as well, which is what I'm sending on for comments. The changelog is my addition, so if that is wrong yell at me.

Quoted 6 lines

I wanted to get some more eyes on this, because the current code sets load_bias to 0 unconditionally on CONFIG_X86 or CONFIG_ARM. I have no idea why that is. The original execshield patches had an #ifdef on __i386__ but the patch that was commited to add PIE support has the CONFIG_X86 setting.

It appears that Nicolas understood what's going on in there when he wrote e4eab08d6050ad0 ("ARM: 6342/1: fix ASLR of PIE executables"). Alas, that patch's changelog is rather useless.

Help?

Also, please: review and test?

Quoted 40 lines

[1] https://bugzilla.redhat.com/show_bug.cgi?id=708563
[2] http://bugzilla.kernel.org/show_bug.cgi?id=36372

josh

---

From: H.J. Lu <[email protected]>

Set the load_bias for PIE executables to a non-zero address if no virtual address is specified. This prevents us from running out of room for all the various loadable segments when ASLR is disabled.

---

fs/binfmt_elf.c +7 -2
index 303983f..069ee29 100644
@@ -794,9 +794,14 @@ static int load_elf_binary(struct linux_binprm *bprm, struct pt_regs *regs)
 			/* Try and get dynamic programs out of the way of the
 			 * default mmap base, as well as whatever program they
 			 * might try to exec.  This is because the brk will
-			 * follow the loader, and is not movable.  */
+			 * follow the loader, and is not movable.  Don't use
+			 * 0 load address since we may not have room for
+			 * all loadable segements.  */
 #if defined(CONFIG_X86) || defined(CONFIG_ARM)
-			load_bias = 0;
+			if (vaddr)
+				load_bias = 0;
+			else
+				load_bias = ELF_PAGESTART(ELF_ET_DYN_BASE);
 #else
 			load_bias = ELF_PAGESTART(ELF_ET_DYN_BASE - vaddr);
 #endif
#2 lore raw
Quoted 26 lines
Quoted 16 lines · On Thu, 29 Sep 2011 15:53:59 -0400 Josh Boyer <[email protected]> wrote:

We've had a bug report[1] of some PIE programs getting a SIGKILL upon exec if you disable address randomization with:

echo 0 > /proc/sys/kernel/randomize_va_space

I tracked this down to get_unmapped_area_prot returning -ENOMEM because the address being passed in is larger than TASK_SIZE - len for the bss section of the test executable. That filters back to set_brk returning an error to load_elf_binary and the SIGKILL being sent around line 872 of binfmt_elf.c.

H.J. submitted an upstream bug report [2] as well, but got no feedback and we can't view it with kernel.org being down anyway. He came up with the patch below as well, which is what I'm sending on for comments. The changelog is my addition, so if that is wrong yell at me.

Quoted 6 lines

I wanted to get some more eyes on this, because the current code sets load_bias to 0 unconditionally on CONFIG_X86 or CONFIG_ARM. I have no idea why that is. The original execshield patches had an #ifdef on __i386__ but the patch that was commited to add PIE support has the CONFIG_X86 setting.

Quoted 3 lines

It appears that Nicolas understood what's going on in there when he wrote e4eab08d6050ad0 ("ARM: 6342/1: fix ASLR of PIE executables"). Alas, that patch's changelog is rather useless.

There is a very small testcase in the kernel bug report. If I remember it correctly, it has something to do with the size of bss section. It fails with about 180MB bss section.

H.J.

Quoted 45 lines

Help?

Also, please: review and test?

fs/binfmt_elf.c static int load_elf_binary(struct linux_binprm *bprm,

Quoted 24 lines

[1] https://bugzilla.redhat.com/show_bug.cgi?id=708563
[2] http://bugzilla.kernel.org/show_bug.cgi?id=36372

josh

---

From: H.J. Lu <[email protected]>

Set the load_bias for PIE executables to a non-zero address if no virtual address is specified. This prevents us from running out of room for all the various loadable segments when ASLR is disabled.

---

fs/binfmt_elf.c +0 -0
index 303983f..069ee29 100644
@@ -794,9 +794,14 @@ static int load_elf_binary(struct linux_binprm *bprm,

struct pt_regs *regs)

Quoted 16 lines
fs/binfmt_elf.c +7 -2
 			/* Try and get dynamic programs out of the way of the
 			 * default mmap base, as well as whatever program they
 			 * might try to exec.  This is because the brk will
-			 * follow the loader, and is not movable.  */
+			 * follow the loader, and is not movable.  Don't use
+			 * 0 load address since we may not have room for
+			 * all loadable segements.  */
 #if defined(CONFIG_X86) || defined(CONFIG_ARM)
-			load_bias = 0;
+			if (vaddr)
+				load_bias = 0;
+			else
+				load_bias = ELF_PAGESTART(ELF_ET_DYN_BASE);
 #else
 			load_bias = ELF_PAGESTART(ELF_ET_DYN_BASE - vaddr);
 #endif
#3 lore raw
In reply to Andrew Morton
Quoted 26 lines · Andrew Morton
Quoted 16 lines · On Thu, 29 Sep 2011 15:53:59 -0400 Josh Boyer <[email protected]> wrote:

We've had a bug report[1] of some PIE programs getting a SIGKILL upon exec if you disable address randomization with:

echo 0 > /proc/sys/kernel/randomize_va_space

I tracked this down to get_unmapped_area_prot returning -ENOMEM because the address being passed in is larger than TASK_SIZE - len for the bss section of the test executable. That filters back to set_brk returning an error to load_elf_binary and the SIGKILL being sent around line 872 of binfmt_elf.c.

H.J. submitted an upstream bug report [2] as well, but got no feedback and we can't view it with kernel.org being down anyway. He came up with the patch below as well, which is what I'm sending on for comments. The changelog is my addition, so if that is wrong yell at me.

Quoted 6 lines

I wanted to get some more eyes on this, because the current code sets load_bias to 0 unconditionally on CONFIG_X86 or CONFIG_ARM. I have no idea why that is. The original execshield patches had an #ifdef on __i386__ but the patch that was commited to add PIE support has the CONFIG_X86 setting.

Quoted 5 lines

It appears that Nicolas understood what's going on in there when he wrote e4eab08d6050ad0 ("ARM: 6342/1: fix ASLR of PIE executables"). Alas, that patch's changelog is rather useless.

Help?

Well, in order to obtain randomization, the addr argument to elf_map() must be zero to eventually let arch_get_unmapped_area() do its job of selecting a random address.

Since only X86 supported ASLR at the time, I simply did the same for ARM i.e. let load_bias be set to 0 so elf_map() would get a zero address.

Quoted 11 lines

Also, please: review and test?

fs/binfmt_elf.c static int load_elf_binary(struct linux_binprm *bprm, struct pt_regs *regs) · line 807

Quoted 9 lines

#if defined(CONFIG_X86) || defined(CONFIG_ARM)

fs/binfmt_elf.c +4 -1
-			load_bias = 0;
+			if (vaddr)
+				load_bias = 0;
+			else
+				load_bias = ELF_PAGESTART(ELF_ET_DYN_BASE);
 #else
 			load_bias = ELF_PAGESTART(ELF_ET_DYN_BASE - vaddr);
 #endif

Simply looking at this patch, I don't see how the second argument to elf_map() called as follows could ever be zero anymore, effectively breaking ASLR.

                error = elf_map(bprm->file, load_bias + vaddr, elf_ppnt,
                                elf_prot, elf_flags, 0);

Nicolas

author
#4 lore raw
Quoted 32 lines · Nicolas Pitre
Quoted 30 lines · Andrew Morton
Quoted 16 lines · On Thu, 29 Sep 2011 15:53:59 -0400 Josh Boyer <[email protected]> wrote:

We've had a bug report[1] of some PIE programs getting a SIGKILL upon exec if you disable address randomization with:

echo 0 > /proc/sys/kernel/randomize_va_space

I tracked this down to get_unmapped_area_prot returning -ENOMEM because the address being passed in is larger than TASK_SIZE - len for the bss section of the test executable. That filters back to set_brk returning an error to load_elf_binary and the SIGKILL being sent around line 872 of binfmt_elf.c.

H.J. submitted an upstream bug report [2] as well, but got no feedback and we can't view it with kernel.org being down anyway. He came up with the patch below as well, which is what I'm sending on for comments. The changelog is my addition, so if that is wrong yell at me.

Quoted 6 lines

I wanted to get some more eyes on this, because the current code sets load_bias to 0 unconditionally on CONFIG_X86 or CONFIG_ARM. I have no idea why that is. The original execshield patches had an #ifdef on __i386__ but the patch that was commited to add PIE support has the CONFIG_X86 setting.

It appears that Nicolas understood what's going on in there when he wrote e4eab08d6050ad0 ("ARM: 6342/1: fix ASLR of PIE executables"). Alas, that patch's changelog is rather useless.

Quoted 5 lines
Quoted 1 line

Help?

Well, in order to obtain randomization, the addr argument to elf_map() must be zero to eventually let arch_get_unmapped_area() do its job of selecting a random address.

Hm. But it doesn't do that if ASLR is disabled at runtime, so the address causes issues?

Quoted 2 lines

Since only X86 supported ASLR at the time, I simply did the same for ARM i.e. let load_bias be set to 0 so elf_map() would get a zero address.

So as of right now, only X86 and ARM support ASLR? Maybe we could change the define to be more descriptive. Something like ARCH_HAS_ASLR?

Quoted 15 lines
Quoted 11 lines

Also, please: review and test?

fs/binfmt_elf.c static int load_elf_binary(struct linux_binprm *bprm, struct pt_regs *regs) · line 807

Quoted 9 lines

#if defined(CONFIG_X86) || defined(CONFIG_ARM)

fs/binfmt_elf.c +4 -1
-			load_bias = 0;
+			if (vaddr)
+				load_bias = 0;
+			else
+				load_bias = ELF_PAGESTART(ELF_ET_DYN_BASE);
 #else
 			load_bias = ELF_PAGESTART(ELF_ET_DYN_BASE - vaddr);
 #endif

Simply looking at this patch, I don't see how the second argument to elf_map() called as follows could ever be zero anymore, effectively breaking ASLR.

Perhaps another check here for randomize? Something like:

#if defined(CONFIG_X86) || defined(CONFIG_ARM)
		if (current->flags & PF_RANDOMIZE)
			load_bias = 0;
		else if (vaddr)
			load_bias = 0;
		else
			load_bias = ELF_PAGESTART(ELF_ET_DYN_BASE);
#else
		load_bias = ELF_PAGESTART(ELF_ET_DYN_BASE - vaddr);

#endif

If that's stupid, then feel free to tell me. I won't pretend like I understand what is going on here yet, but based on the explanation you provided that might work.

josh

#5 lore raw
Quoted 11 lines · Josh Boyer

Perhaps another check here for randomize? Something like:

#if defined(CONFIG_X86) || defined(CONFIG_ARM)
		if (current->flags & PF_RANDOMIZE)
			load_bias = 0;
		else if (vaddr)
			load_bias = 0;
		else
			load_bias = ELF_PAGESTART(ELF_ET_DYN_BASE);
#else
		load_bias = ELF_PAGESTART(ELF_ET_DYN_BASE - vaddr);
Quoted 5 lines

#endif

If that's stupid, then feel free to tell me. I won't pretend like I understand what is going on here yet, but based on the explanation you provided that might work.

I have just verified my hunch that the original patch from H.J. / Josh breaks ASLR completely, so Andrew, please drop it for now.

I am now looking into how to fix things properly.

Josh, looking at what you are proposing -- do you see any reason to make the behavior different in #else branch and in !(current->flags & PF_RANDOMIZE) case?

Signature · 2 lines
Jiri Kosina
SUSE Labs
author
#6 lore raw
Quoted 15 lines · Jiri Kosina
Quoted 13 lines · Josh Boyer

Perhaps another check here for randomize? Something like:

#if defined(CONFIG_X86) || defined(CONFIG_ARM)
		if (current->flags & PF_RANDOMIZE)
			load_bias = 0;
		else if (vaddr)
			load_bias = 0;
		else
			load_bias = ELF_PAGESTART(ELF_ET_DYN_BASE);
#else
		load_bias = ELF_PAGESTART(ELF_ET_DYN_BASE - vaddr);

#endif

Quoted 6 lines
Quoted 3 lines

If that's stupid, then feel free to tell me. I won't pretend like I understand what is going on here yet, but based on the explanation you provided that might work.

I have just verified my hunch that the original patch from H.J. / Josh breaks ASLR completely, so Andrew, please drop it for now.

Yes, please drop the original.

Quoted 5 lines

I am now looking into how to fix things properly.

Josh, looking at what you are proposing -- do you see any reason to make the behavior different in #else branch and in !(current->flags & PF_RANDOMIZE) case?

I was mostly just trying to adapt H.J.'s patch to account for the PF_RANDOMIZE case. Looking at it a bit more, I'm not sure why they would need to be different. H.J., do you recall why you made that change originally?

josh

#7 lore raw

PATCH v2 1/1 binfmt_elf: Fix PIE execution with randomization disabled (was Re: [RFC PATCH] binfmt_elf: Fix PIE execution with randomization disabled)

Quoted 13 lines · Josh Boyer
Quoted 13 lines
Quoted 13 lines

Perhaps another check here for randomize? Something like:

#if defined(CONFIG_X86) || defined(CONFIG_ARM)
		if (current->flags & PF_RANDOMIZE)
			load_bias = 0;
		else if (vaddr)
			load_bias = 0;
		else
			load_bias = ELF_PAGESTART(ELF_ET_DYN_BASE);
#else
		load_bias = ELF_PAGESTART(ELF_ET_DYN_BASE - vaddr);

#endif

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

If that's stupid, then feel free to tell me. I won't pretend like I understand what is going on here yet, but based on the explanation you provided that might work.

I have just verified my hunch that the original patch from H.J. / Josh breaks ASLR completely, so Andrew, please drop it for now.

Yes, please drop the original.

Thanks.

Quoted 10 lines
Quoted 5 lines

I am now looking into how to fix things properly.

Josh, looking at what you are proposing -- do you see any reason to make the behavior different in #else branch and in !(current->flags & PF_RANDOMIZE) case?

I was mostly just trying to adapt H.J.'s patch to account for the PF_RANDOMIZE case. Looking at it a bit more, I'm not sure why they would need to be different. H.J., do you recall why you made that change originally?

How about the patch below instead? It survives my testing, and I believe it handles both cases properly.

Confirmation from the original bug reporter would obviously be a nice bonus too :)

From: Jiri Kosina <[email protected]>
Subject: [PATCH] binfmt_elf: fix PIE execution with randomization disabled

The case of address space randomization being disabled in runtime through randomize_va_space sysctl is not treated properly in load_elf_binary(), resulting in SIGKILL coming at exec() time for certain PIE-linked binaries in case the randomization has been disabled at runtime prior to calling exec().

Handle the randomize_va_space == 0 case the same way as if we were not supporting .text randomization at all.

Based on original patch by H.J. Lu <[email protected]> and Josh Boyer <[email protected]>

Cc Ingo Molnar <[email protected]>
Cc Jiri Kosina <[email protected]>
Cc Nicolas Pitre <[email protected]>
Cc Russell King <[email protected]>
---
 fs/binfmt_elf.c |    5 ++++-
 1 files changed, 4 insertions(+), 1 deletions(-)
fs/binfmt_elf.c +4 -1
index dd0fdfc..bb11fe4 100644
@@ -795,7 +795,10 @@ static int load_elf_binary(struct linux_binprm *bprm, struct pt_regs *regs)
 			 * might try to exec.  This is because the brk will
 			 * follow the loader, and is not movable.  */
 #if defined(CONFIG_X86) || defined(CONFIG_ARM)
-			load_bias = 0;
+			if (current->flags & PF_RANDOMIZE)
+				load_bias = 0;
+			else
+				load_bias = ELF_PAGESTART(ELF_ET_DYN_BASE - vaddr);
 #else
 			load_bias = ELF_PAGESTART(ELF_ET_DYN_BASE - vaddr);
 #endif
author Acked-by on v2 1/1
#8 lore raw

Re: [PATCH v2] binfmt_elf: Fix PIE execution with randomization disabled (was Re: [RFC PATCH] binfmt_elf: Fix PIE execution with randomization disabled)

Quoted 10 lines · Jiri Kosina
Quoted 4 lines

I was mostly just trying to adapt H.J.'s patch to account for the PF_RANDOMIZE case. Looking at it a bit more, I'm not sure why they would need to be different. H.J., do you recall why you made that change originally?

How about the patch below instead? It survives my testing, and I believe it handles both cases properly.

Confirmation from the original bug reporter would obviously be a nice bonus too :)

I built an F15 kernel with this patch included. The testcase included in the original bug report seems to run with and without randomization enabled. Looking at the ldd output on the binary shows that both cases are working appropriately as well.

I'm happy to add my Acked-by below, but it would be nice if H.J. confirmed as well.

Quoted 12 lines

From: Jiri Kosina <[email protected]>
Subject: [PATCH] binfmt_elf: fix PIE execution with randomization disabled

The case of address space randomization being disabled in runtime through randomize_va_space sysctl is not treated properly in load_elf_binary(), resulting in SIGKILL coming at exec() time for certain PIE-linked binaries in case the randomization has been disabled at runtime prior to calling exec().

Handle the randomize_va_space == 0 case the same way as if we were not supporting .text randomization at all.

Quoted 8 lines

Based on original patch by H.J. Lu <[email protected]> and Josh Boyer <[email protected]>

Cc Ingo Molnar <[email protected]>
Cc Jiri Kosina <[email protected]>
Cc Nicolas Pitre <[email protected]>
Cc Russell King <[email protected]>
Quoted 21 lines
---
 fs/binfmt_elf.c |    5 ++++-
 1 files changed, 4 insertions(+), 1 deletions(-)
fs/binfmt_elf.c +4 -1
index dd0fdfc..bb11fe4 100644
@@ -795,7 +795,10 @@ static int load_elf_binary(struct linux_binprm *bprm, struct pt_regs *regs)
 			 * might try to exec.  This is because the brk will
 			 * follow the loader, and is not movable.  */
 #if defined(CONFIG_X86) || defined(CONFIG_ARM)
-			load_bias = 0;
+			if (current->flags & PF_RANDOMIZE)
+				load_bias = 0;
+			else
+				load_bias = ELF_PAGESTART(ELF_ET_DYN_BASE - vaddr);
 #else
 			load_bias = ELF_PAGESTART(ELF_ET_DYN_BASE - vaddr);
 #endif
Acked-by on v2 1/1
#9 lore raw

Re: [PATCH v2] binfmt_elf: Fix PIE execution with randomization disabled (was Re: [RFC PATCH] binfmt_elf: Fix PIE execution with randomization disabled)

In reply to Jiri Kosina
Quoted 21 lines · Jiri Kosina

How about the patch below instead? It survives my testing, and I believe it handles both cases properly.

Confirmation from the original bug reporter would obviously be a nice bonus too :)

From: Jiri Kosina <[email protected]>
Subject: [PATCH] binfmt_elf: fix PIE execution with randomization disabled

The case of address space randomization being disabled in runtime through randomize_va_space sysctl is not treated properly in load_elf_binary(), resulting in SIGKILL coming at exec() time for certain PIE-linked binaries in case the randomization has been disabled at runtime prior to calling exec().

Handle the randomize_va_space == 0 case the same way as if we were not supporting .text randomization at all.

Quoted 8 lines

Based on original patch by H.J. Lu <[email protected]> and Josh Boyer <[email protected]>

Cc Ingo Molnar <[email protected]>
Cc Jiri Kosina <[email protected]>
Cc Nicolas Pitre <[email protected]>
Cc Russell King <[email protected]>

However I'd suggest a change to deal with style issues:

Quoted 6 lines
fs/binfmt_elf.c +0 -0
index dd0fdfc..bb11fe4 100644
@@ -795,7 +795,10 @@ static int load_elf_binary(struct linux_binprm *bprm, struct pt_regs *regs)
 			 * might try to exec.  This is because the brk will

fs/binfmt_elf.c static int load_elf_binary(struct linux_binprm *bprm, struct pt_regs *regs) · line 804

Quoted 10 lines
fs/binfmt_elf.c +4 -1
 			 * follow the loader, and is not movable.  */
 #if defined(CONFIG_X86) || defined(CONFIG_ARM)
-			load_bias = 0;
+			if (current->flags & PF_RANDOMIZE)
+				load_bias = 0;
+			else
+				load_bias = ELF_PAGESTART(ELF_ET_DYN_BASE - vaddr);
 #else
 			load_bias = ELF_PAGESTART(ELF_ET_DYN_BASE - vaddr);
 #endif

What about this instead, saving 3 lines out of 8:

			load_bias = ELF_PAGESTART(ELF_ET_DYN_BASE - vaddr);
#if defined(CONFIG_X86) || defined(CONFIG_ARM)
			if (current->flags & PF_RANDOMIZE)
				load_bias = 0;

#endif

?

Nicolas

#10 lore raw

RE: [PATCH v2] binfmt_elf: Fix PIE execution with randomization disabled (was Re: [RFC PATCH] binfmt_elf: Fix PIE execution with randomization disabled)

Why not

#if defined(CONFIG_X86) || defined(CONFIG_ARM)
#define CHECK_PF_RANDOMIZE 1
#else
#define CHECK_PF_RANDOMIZE 0

#endif
			if (CHECK_PF_RANDOMIZE && (current->flags & PF_RANDOMIZE))
				load_bias = 0;
			else
				load_bias = ELF_PAGESTART(ELF_ET_DYN_BASE - vaddr);

H.J.

Quoted 77 lines

-----Original Message-----
From: Nicolas Pitre [mailto:[email protected]]
Sent: Monday, October 03, 2011 8:56 AM
To: Jiri Kosina
Cc: Josh Boyer; Andrew Morton; Ingo Molnar; Lu, Hongjiu; linux- [email protected]; Andrew Morton; Russell King
Subject: Re: [PATCH v2] binfmt_elf: Fix PIE execution with randomization disabled (was Re: [RFC PATCH] binfmt_elf: Fix PIE execution with randomization disabled)

Quoted 21 lines · Jiri Kosina

How about the patch below instead? It survives my testing, and I believe it handles both cases properly.

Confirmation from the original bug reporter would obviously be a nice bonus too :)

From: Jiri Kosina <[email protected]>
Subject: [PATCH] binfmt_elf: fix PIE execution with randomization disabled

The case of address space randomization being disabled in runtime through randomize_va_space sysctl is not treated properly in load_elf_binary(), resulting in SIGKILL coming at exec() time for certain PIE-linked binaries in case the randomization has been disabled at runtime prior to calling exec().

Handle the randomize_va_space == 0 case the same way as if we were not supporting .text randomization at all.

Quoted 8 lines

Based on original patch by H.J. Lu <[email protected]> and Josh Boyer <[email protected]>

Cc Ingo Molnar <[email protected]>
Cc Jiri Kosina <[email protected]>
Cc Nicolas Pitre <[email protected]>
Cc Russell King <[email protected]>

However I'd suggest a change to deal with style issues:

fs/binfmt_elf.c static int load_elf_binary(struct linux_binprm *bprm,

Quoted 5 lines
fs/binfmt_elf.c +0 -0
index dd0fdfc..bb11fe4 100644
@@ -795,7 +795,10 @@ static int load_elf_binary(struct linux_binprm *bprm,

struct pt_regs *regs)

fs/binfmt_elf.c static int load_elf_binary(struct linux_binprm *bprm, struct pt_regs *regs) · line 804

Quoted 11 lines
fs/binfmt_elf.c +4 -1
 			 * might try to exec.  This is because the brk will
 			 * follow the loader, and is not movable.  */
 #if defined(CONFIG_X86) || defined(CONFIG_ARM)
-			load_bias = 0;
+			if (current->flags & PF_RANDOMIZE)
+				load_bias = 0;
+			else
+				load_bias = ELF_PAGESTART(ELF_ET_DYN_BASE - vaddr);
 #else
 			load_bias = ELF_PAGESTART(ELF_ET_DYN_BASE - vaddr);
 #endif

What about this instead, saving 3 lines out of 8:

			load_bias = ELF_PAGESTART(ELF_ET_DYN_BASE - vaddr);
#if defined(CONFIG_X86) || defined(CONFIG_ARM)
			if (current->flags & PF_RANDOMIZE)
				load_bias = 0;

#endif

?

Nicolas

#11 lore raw

RE: [PATCH v2] binfmt_elf: Fix PIE execution with randomization disabled (was Re: [RFC PATCH] binfmt_elf: Fix PIE execution with randomization disabled)

Quoted 6 lines · Hongjiu

Why not

#if defined(CONFIG_X86) || defined(CONFIG_ARM)
#define CHECK_PF_RANDOMIZE 1
#else
#define CHECK_PF_RANDOMIZE 0

Quoted 6 lines
#endif
			if (CHECK_PF_RANDOMIZE && (current->flags & PF_RANDOMIZE))
				load_bias = 0;
			else
				load_bias = ELF_PAGESTART(ELF_ET_DYN_BASE - vaddr);

Whatever. I don't have a strong opinion on the shed color.

Quoted 83 lines
Quoted 70 lines

-----Original Message-----
From: Nicolas Pitre [mailto:[email protected]]
Sent: Monday, October 03, 2011 8:56 AM
To: Jiri Kosina
Cc: Josh Boyer; Andrew Morton; Ingo Molnar; Lu, Hongjiu; linux- [email protected]; Andrew Morton; Russell King
Subject: Re: [PATCH v2] binfmt_elf: Fix PIE execution with randomization disabled (was Re: [RFC PATCH] binfmt_elf: Fix PIE execution with randomization disabled)

Quoted 21 lines · Jiri Kosina

How about the patch below instead? It survives my testing, and I believe it handles both cases properly.

Confirmation from the original bug reporter would obviously be a nice bonus too :)

From: Jiri Kosina <[email protected]>
Subject: [PATCH] binfmt_elf: fix PIE execution with randomization disabled

The case of address space randomization being disabled in runtime through randomize_va_space sysctl is not treated properly in load_elf_binary(), resulting in SIGKILL coming at exec() time for certain PIE-linked binaries in case the randomization has been disabled at runtime prior to calling exec().

Handle the randomize_va_space == 0 case the same way as if we were not supporting .text randomization at all.

Quoted 8 lines

Based on original patch by H.J. Lu <[email protected]> and Josh Boyer <[email protected]>

Cc Ingo Molnar <[email protected]>
Cc Jiri Kosina <[email protected]>
Cc Nicolas Pitre <[email protected]>
Cc Russell King <[email protected]>

However I'd suggest a change to deal with style issues:

fs/binfmt_elf.c static int load_elf_binary(struct linux_binprm *bprm,

Quoted 5 lines
fs/binfmt_elf.c +0 -0
index dd0fdfc..bb11fe4 100644
@@ -795,7 +795,10 @@ static int load_elf_binary(struct linux_binprm *bprm,

struct pt_regs *regs)

fs/binfmt_elf.c static int load_elf_binary(struct linux_binprm *bprm, struct pt_regs *regs) · line 804

Quoted 11 lines
fs/binfmt_elf.c +4 -1
 			 * might try to exec.  This is because the brk will
 			 * follow the loader, and is not movable.  */
 #if defined(CONFIG_X86) || defined(CONFIG_ARM)
-			load_bias = 0;
+			if (current->flags & PF_RANDOMIZE)
+				load_bias = 0;
+			else
+				load_bias = ELF_PAGESTART(ELF_ET_DYN_BASE - vaddr);
 #else
 			load_bias = ELF_PAGESTART(ELF_ET_DYN_BASE - vaddr);
 #endif

What about this instead, saving 3 lines out of 8:

			load_bias = ELF_PAGESTART(ELF_ET_DYN_BASE - vaddr);
#if defined(CONFIG_X86) || defined(CONFIG_ARM)
			if (current->flags & PF_RANDOMIZE)
				load_bias = 0;
Quoted 7 lines

#endif

?

Nicolas

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

#12 lore raw

RE: [PATCH v2] binfmt_elf: Fix PIE execution with randomization disabled (was Re: [RFC PATCH] binfmt_elf: Fix PIE execution with randomization disabled)

In reply to Lu, Hongjiu
Quoted 11 lines · Hongjiu

Why not

#if defined(CONFIG_X86) || defined(CONFIG_ARM)
#define CHECK_PF_RANDOMIZE 1
#else
#define CHECK_PF_RANDOMIZE 0

#endif
			if (CHECK_PF_RANDOMIZE && (current->flags & PF_RANDOMIZE))
				load_bias = 0;
			else
				load_bias = ELF_PAGESTART(ELF_ET_DYN_BASE - vaddr);

If anything, that is not particularly good name for that macro (as we are checking current->flags for PF_RANDOMIZE at other places indepenedntly on this).

Personally, I find my original patch the most readable one, but I don't care much, it's really bike-shedding.

Signature · 2 lines
Jiri Kosina
SUSE Labs
#13 lore raw

Re: [PATCH v2] binfmt_elf: Fix PIE execution with randomization disabled (was Re: [RFC PATCH] binfmt_elf: Fix PIE execution with randomization disabled)

In reply to Jiri Kosina
Quoted 31 lines · On Mon, 3 Oct 2011 17:11:47 +0200 (CEST) Jiri Kosina <[email protected]> wrote:

From: Jiri Kosina <[email protected]>
Subject: [PATCH] binfmt_elf: fix PIE execution with randomization disabled

The case of address space randomization being disabled in runtime through randomize_va_space sysctl is not treated properly in load_elf_binary(), resulting in SIGKILL coming at exec() time for certain PIE-linked binaries in case the randomization has been disabled at runtime prior to calling exec().

Handle the randomize_va_space == 0 case the same way as if we were not supporting .text randomization at all.

Based on original patch by H.J. Lu <[email protected]> and Josh Boyer <[email protected]>

Cc Ingo Molnar <[email protected]>
Cc Jiri Kosina <[email protected]>
Cc Nicolas Pitre <[email protected]>
Cc Russell King <[email protected]>
---
 fs/binfmt_elf.c |    5 ++++-
 1 files changed, 4 insertions(+), 1 deletions(-)
fs/binfmt_elf.c +0 -0
index dd0fdfc..bb11fe4 100644
@@ -795,7 +795,10 @@ static int load_elf_binary(struct linux_binprm *bprm, struct pt_regs *regs)
 			 * might try to exec.  This is because the brk will

fs/binfmt_elf.c static int load_elf_binary(struct linux_binprm *bprm, struct pt_regs *regs) · line 804

Quoted 10 lines
fs/binfmt_elf.c +4 -1
 			 * follow the loader, and is not movable.  */
 #if defined(CONFIG_X86) || defined(CONFIG_ARM)
-			load_bias = 0;
+			if (current->flags & PF_RANDOMIZE)
+				load_bias = 0;
+			else
+				load_bias = ELF_PAGESTART(ELF_ET_DYN_BASE - vaddr);
 #else
 			load_bias = ELF_PAGESTART(ELF_ET_DYN_BASE - vaddr);
 #endif

Guys, it took several people several days and 10+ emails to work out what's happening in there, and the first attempt to fix it was buggy. This is all a huuuuge signal that the code is unobvious, hard to understand, hard to maintain.

Please, let's get a good code comment in there while it's fresh in your minds. So the next person who comes along doesn't have the same amount of difficulty?

#14 lore raw

Re: [PATCH v2] binfmt_elf: Fix PIE execution with randomization disabled (was Re: [RFC PATCH] binfmt_elf: Fix PIE execution with randomization disabled)

Quoted 37 lines · Andrew Morton
Quoted 37 lines

From: Jiri Kosina <[email protected]>
Subject: [PATCH] binfmt_elf: fix PIE execution with randomization disabled

The case of address space randomization being disabled in runtime through randomize_va_space sysctl is not treated properly in load_elf_binary(), resulting in SIGKILL coming at exec() time for certain PIE-linked binaries in case the randomization has been disabled at runtime prior to calling exec().

Handle the randomize_va_space == 0 case the same way as if we were not supporting .text randomization at all.

Based on original patch by H.J. Lu <[email protected]> and Josh Boyer <[email protected]>

Cc Ingo Molnar <[email protected]>
Cc Jiri Kosina <[email protected]>
Cc Nicolas Pitre <[email protected]>
Cc Russell King <[email protected]>
---
 fs/binfmt_elf.c |    5 ++++-
 1 files changed, 4 insertions(+), 1 deletions(-)
fs/binfmt_elf.c +4 -1
index dd0fdfc..bb11fe4 100644
@@ -795,7 +795,10 @@ static int load_elf_binary(struct linux_binprm *bprm, struct pt_regs *regs)
 			 * might try to exec.  This is because the brk will
 			 * follow the loader, and is not movable.  */
 #if defined(CONFIG_X86) || defined(CONFIG_ARM)
-			load_bias = 0;
+			if (current->flags & PF_RANDOMIZE)
+				load_bias = 0;
+			else
+				load_bias = ELF_PAGESTART(ELF_ET_DYN_BASE - vaddr);
Quoted 8 lines

fs/binfmt_elf.c static int load_elf_binary(struct linux_binprm *bprm, struct pt_regs *regs) · line 804

Quoted 3 lines
fs/binfmt_elf.c +0 -0
 #else
 			load_bias = ELF_PAGESTART(ELF_ET_DYN_BASE - vaddr);
 #endif

Guys, it took several people several days and 10+ emails to work out what's happening in there, and the first attempt to fix it was buggy. This is all a huuuuge signal that the code is unobvious, hard to understand, hard to maintain.

It's unfortunately true. Rewriting the ELF binary loader is on my list of very-long-term TODOs. It has became quite a monster over decades.

Quoted 3 lines

Please, let's get a good code comment in there while it's fresh in your minds. So the next person who comes along doesn't have the same amount of difficulty?

I have to run now, I will resend the patch to you with an explanatory comment added in about 8 hours from now.

Thanks,

Signature · 2 lines
Jiri Kosina
SUSE Labs
Tested-by Josh Boyer Acked-by Nicolas Pitre
#15 lore raw

Re: [PATCH v3] binfmt_elf: Fix PIE execution with randomization disabled (was Re: [RFC PATCH] binfmt_elf: Fix PIE execution with randomization disabled)

Quoted 11 lines · Jiri Kosina
Quoted 4 lines

Guys, it took several people several days and 10+ emails to work out what's happening in there, and the first attempt to fix it was buggy. This is all a huuuuge signal that the code is unobvious, hard to understand, hard to maintain.

It's unfortunately true. Rewriting the ELF binary loader is on my list of very-long-term TODOs. It has became quite a monster over decades.

Quoted 3 lines

Please, let's get a good code comment in there while it's fresh in your minds. So the next person who comes along doesn't have the same amount of difficulty?

Andrew,

find the updated patch below. Please consider applying.

I believe Cc to stable is appropriate as well.

Thanks a lot Josh for verification of the fix.

From: Jiri Kosina <[email protected]>
Subject: [PATCH] binfmt_elf: fix PIE execution with randomization disabled

The case of address space randomization being disabled in runtime through randomize_va_space sysctl is not treated properly in load_elf_binary(), resulting in SIGKILL coming at exec() time for certain PIE-linked binaries in case the randomization has been disabled at runtime prior to calling exec().

Handle the randomize_va_space == 0 case the same way as if we were not supporting .text randomization at all.

Based on original patch by H.J. Lu <[email protected]> and. Josh Boyer <[email protected]>

---
 fs/binfmt_elf.c |   11 ++++++++++-
 1 files changed, 10 insertions(+), 1 deletions(-)
fs/binfmt_elf.c +10 -1
index dd0fdfc..21ac5ee 100644
@@ -795,7 +795,16 @@ static int load_elf_binary(struct linux_binprm *bprm, struct pt_regs *regs)
 			 * might try to exec.  This is because the brk will
 			 * follow the loader, and is not movable.  */
 #if defined(CONFIG_X86) || defined(CONFIG_ARM)
-			load_bias = 0;
+			/* Memory randomization might have been switched off
+			 * in runtime via sysctl.
+			 * If that is the case, retain the original non-zero
+			 * load_bias value in order to establish proper
+			 * non-randomized mappings.
+			 */
+			if (current->flags & PF_RANDOMIZE)
+				load_bias = 0;
+			else
+				load_bias = ELF_PAGESTART(ELF_ET_DYN_BASE - vaddr);
 #else
 			load_bias = ELF_PAGESTART(ELF_ET_DYN_BASE - vaddr);
 #endif
Signature · 2 lines
Jiri Kosina
SUSE Labs
#16 lore raw

Re: [PATCH v2] binfmt_elf: Fix PIE execution with randomization disabled (was Re: [RFC PATCH] binfmt_elf: Fix PIE execution with randomization disabled)

In reply to Andrew Morton
Quoted 27 lines · Andrew Morton
Quoted 11 lines
---
 fs/binfmt_elf.c |    5 ++++-
 1 files changed, 4 insertions(+), 1 deletions(-)
fs/binfmt_elf.c +0 -0
index dd0fdfc..bb11fe4 100644
@@ -795,7 +795,10 @@ static int load_elf_binary(struct linux_binprm *bprm, struct pt_regs *regs)
 			 * might try to exec.  This is because the brk will

fs/binfmt_elf.c static int load_elf_binary(struct linux_binprm *bprm, struct pt_regs *regs) · line 804

Quoted 10 lines
fs/binfmt_elf.c +4 -1
 			 * follow the loader, and is not movable.  */
 #if defined(CONFIG_X86) || defined(CONFIG_ARM)
-			load_bias = 0;
+			if (current->flags & PF_RANDOMIZE)
+				load_bias = 0;
+			else
+				load_bias = ELF_PAGESTART(ELF_ET_DYN_BASE - vaddr);
 #else
 			load_bias = ELF_PAGESTART(ELF_ET_DYN_BASE - vaddr);
 #endif

Guys, it took several people several days and 10+ emails to work out what's happening in there, and the first attempt to fix it was buggy. This is all a huuuuge signal that the code is unobvious, hard to understand, hard to maintain.

Quoted 3 lines

Please, let's get a good code comment in there while it's fresh in your minds. So the next person who comes along doesn't have the same amount of difficulty?

BTW, after thinking about this a little bit more, maybe the patch below would be worthwile and could save some mis-routed e-mails and bugreports in the future ...

From: Jiri Kosina <[email protected]>
Subject: [PATCH] MAINTAINERS: add ASLR maintainer

Since achieving the full ASLR by merging the PIE randomization in commit cc503c1b43 ("x86: PIE executable randomization"), I have been dealing with most (if not all) of the bugreports reported against userspace address space randomization, so it might be a good idea to provide a decent contact point in MAINTAINERS.

---
 MAINTAINERS |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)
MAINTAINERS +4 -0
index ace8f9c..c362845 100644
@@ -317,6 +317,10 @@ W:	http://wiki.analog.com/AD7879
 S:	Supported
 F:	drivers/input/touchscreen/ad7879.c
 
+ADDRESS SPACE LAYOUT RANDOMIZATION (ASLR)
+M:	Jiri Kosina <[email protected]>
+S:	Maintained
+
 ADM1025 HARDWARE MONITOR DRIVER
 M:	Jean Delvare <[email protected]>
 L:	[email protected]
Signature · 1 line
1.7.3.1
#17 lore raw
In reply to Josh Boyer
Quoted 50 lines · Josh Boyer

We've had a bug report[1] of some PIE programs getting a SIGKILL upon exec if you disable address randomization with:

echo 0 > /proc/sys/kernel/randomize_va_space

I tracked this down to get_unmapped_area_prot returning -ENOMEM because the address being passed in is larger than TASK_SIZE - len for the bss section of the test executable. That filters back to set_brk returning an error to load_elf_binary and the SIGKILL being sent around line 872 of binfmt_elf.c.

H.J. submitted an upstream bug report [2] as well, but got no feedback and we can't view it with kernel.org being down anyway. He came up with the patch below as well, which is what I'm sending on for comments. The changelog is my addition, so if that is wrong yell at me.

I wanted to get some more eyes on this, because the current code sets load_bias to 0 unconditionally on CONFIG_X86 or CONFIG_ARM. I have no idea why that is. The original execshield patches had an #ifdef on __i386__ but the patch that was commited to add PIE support has the CONFIG_X86 setting.

Thoughts welcome.

[1] https://bugzilla.redhat.com/show_bug.cgi?id=708563
[2] http://bugzilla.kernel.org/show_bug.cgi?id=36372

josh

---

From: H.J. Lu <[email protected]>

Set the load_bias for PIE executables to a non-zero address if no virtual address is specified. This prevents us from running out of room for all the various loadable segments when ASLR is disabled.

---

fs/binfmt_elf.c +0 -0
index 303983f..069ee29 100644
@@ -794,9 +794,14 @@ static int load_elf_binary(struct linux_binprm *bprm, struct pt_regs *regs)
 			/* Try and get dynamic programs out of the way of the
 			 * default mmap base, as well as whatever program they
 			 * might try to exec.  This is because the brk will

fs/binfmt_elf.c static int load_elf_binary(struct linux_binprm *bprm, struct pt_regs *regs) · line 804

Quoted 10 lines
fs/binfmt_elf.c +7 -2
-			 * follow the loader, and is not movable.  */
+			 * follow the loader, and is not movable.  Don't use
+			 * 0 load address since we may not have room for
+			 * all loadable segements.  */
 #if defined(CONFIG_X86) || defined(CONFIG_ARM)
-			load_bias = 0;
+			if (vaddr)
+				load_bias = 0;
+			else
+				load_bias = ELF_PAGESTART(ELF_ET_DYN_BASE);

If you do this unconditionally, I can't see how ASLR could be working at all -- this patch causes that elf_addr() is always called with non-zero 'addr' argument, right? (haven't verified by actually testing the patch yet, but I fail to see how come this simply doesn't break ASLR completely).

Checking for PF_RANDOMIZE flag should be the way to go here.

Signature · 2 lines
Jiri Kosina
SUSE Labs
author
#18 lore raw
Quoted 69 lines · Jiri Kosina
Quoted 50 lines · Josh Boyer

We've had a bug report[1] of some PIE programs getting a SIGKILL upon exec if you disable address randomization with:

echo 0 > /proc/sys/kernel/randomize_va_space

I tracked this down to get_unmapped_area_prot returning -ENOMEM because the address being passed in is larger than TASK_SIZE - len for the bss section of the test executable. That filters back to set_brk returning an error to load_elf_binary and the SIGKILL being sent around line 872 of binfmt_elf.c.

H.J. submitted an upstream bug report [2] as well, but got no feedback and we can't view it with kernel.org being down anyway. He came up with the patch below as well, which is what I'm sending on for comments. The changelog is my addition, so if that is wrong yell at me.

I wanted to get some more eyes on this, because the current code sets load_bias to 0 unconditionally on CONFIG_X86 or CONFIG_ARM. I have no idea why that is. The original execshield patches had an #ifdef on __i386__ but the patch that was commited to add PIE support has the CONFIG_X86 setting.

Thoughts welcome.

[1] https://bugzilla.redhat.com/show_bug.cgi?id=708563
[2] http://bugzilla.kernel.org/show_bug.cgi?id=36372

josh

---

From: H.J. Lu <[email protected]>

Set the load_bias for PIE executables to a non-zero address if no virtual address is specified. This prevents us from running out of room for all the various loadable segments when ASLR is disabled.

---

fs/binfmt_elf.c +0 -0
index 303983f..069ee29 100644
@@ -794,9 +794,14 @@ static int load_elf_binary(struct linux_binprm *bprm, struct pt_regs *regs)
 			/* Try and get dynamic programs out of the way of the
 			 * default mmap base, as well as whatever program they
 			 * might try to exec.  This is because the brk will

fs/binfmt_elf.c static int load_elf_binary(struct linux_binprm *bprm, struct pt_regs *regs) · line 804

Quoted 10 lines
fs/binfmt_elf.c +7 -2
-			 * follow the loader, and is not movable.  */
+			 * follow the loader, and is not movable.  Don't use
+			 * 0 load address since we may not have room for
+			 * all loadable segements.  */
 #if defined(CONFIG_X86) || defined(CONFIG_ARM)
-			load_bias = 0;
+			if (vaddr)
+				load_bias = 0;
+			else
+				load_bias = ELF_PAGESTART(ELF_ET_DYN_BASE);

If you do this unconditionally, I can't see how ASLR could be working at all -- this patch causes that elf_addr() is always called with non-zero 'addr' argument, right? (haven't verified by actually testing the patch yet, but I fail to see how come this simply doesn't break ASLR completely).

Quoted 1 line

Checking for PF_RANDOMIZE flag should be the way to go here.

I sent out a pseudo-patch that did that (I think). Could you take a look and let me know if that seems more appropriate?

josh