* ppc64 sbrk returns executable heap in 32-bit emulation mode
@ 2016-05-12 13:41 Florian Weimer
2016-05-16 6:24 ` Alan Modra
[not found] ` <20160516062425.GA24091__32035.8907142237$1463379977$gmane$org@bubble.grove.modra.org>
0 siblings, 2 replies; 7+ messages in thread
From: Florian Weimer @ 2016-05-12 13:41 UTC (permalink / raw)
To: linuxppc-dev
We noticed that on ppc64, the sbrk system call in the 32-bit subsystem
returns executable memory. I assume it is related to this, in
arch/powerpc/include/asm/page.h:
/*
* Unfortunately the PLT is in the BSS in the PPC32 ELF ABI,
* and needs to be executable. This means the whole heap ends
* up being executable.
*/
#define VM_DATA_DEFAULT_FLAGS32 (VM_READ | VM_WRITE | VM_EXEC | \
VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC)
What is the rationale for this? This comment must be *really* old,
because ld.so in glibc should make sure that the PLT is executable. And
for current binaries, .bss is *not* executable, contrary to what the
comment suggests.
Is this comment about pre-ELF binaries? If yes, would it possible to
change the default for ELF binaries?
Thanks,
Florian
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: ppc64 sbrk returns executable heap in 32-bit emulation mode
2016-05-12 13:41 ppc64 sbrk returns executable heap in 32-bit emulation mode Florian Weimer
@ 2016-05-16 6:24 ` Alan Modra
2016-05-16 8:51 ` Florian Weimer
[not found] ` <20160516062425.GA24091__32035.8907142237$1463379977$gmane$org@bubble.grove.modra.org>
1 sibling, 1 reply; 7+ messages in thread
From: Alan Modra @ 2016-05-16 6:24 UTC (permalink / raw)
To: Florian Weimer; +Cc: linuxppc-dev
On Thu, May 12, 2016 at 03:41:09PM +0200, Florian Weimer wrote:
> We noticed that on ppc64, the sbrk system call in the 32-bit subsystem
> returns executable memory. I assume it is related to this, in
> arch/powerpc/include/asm/page.h:
>
> /*
> * Unfortunately the PLT is in the BSS in the PPC32 ELF ABI,
> * and needs to be executable. This means the whole heap ends
> * up being executable.
> */
> #define VM_DATA_DEFAULT_FLAGS32 (VM_READ | VM_WRITE | VM_EXEC | \
> VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC)
>
>
> What is the rationale for this? This comment must be *really* old,
I think the comment is just plain wrong. ppc32 needs an executable
stack because it builds trampolines on the stack to support calling
nested functions. I presume that's why the heap is executable. (If
I'm wrong about heap+stack needing the same protection then I can't
think of any reason to require an executable heap.)
> because ld.so in glibc should make sure that the PLT is executable. And
> for current binaries, .bss is *not* executable, contrary to what the
> comment suggests.
>
> Is this comment about pre-ELF binaries? If yes, would it possible to
> change the default for ELF binaries?
>
> Thanks,
> Florian
--
Alan Modra
Australia Development Lab, IBM
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: ppc64 sbrk returns executable heap in 32-bit emulation mode
[not found] ` <20160516062425.GA24091__32035.8907142237$1463379977$gmane$org@bubble.grove.modra.org>
@ 2016-05-16 8:49 ` Andreas Schwab
2016-05-16 8:59 ` Florian Weimer
0 siblings, 1 reply; 7+ messages in thread
From: Andreas Schwab @ 2016-05-16 8:49 UTC (permalink / raw)
To: Alan Modra; +Cc: Florian Weimer, linuxppc-dev
Alan Modra <amodra@gmail.com> writes:
> On Thu, May 12, 2016 at 03:41:09PM +0200, Florian Weimer wrote:
>> We noticed that on ppc64, the sbrk system call in the 32-bit subsystem
>> returns executable memory. I assume it is related to this, in
>> arch/powerpc/include/asm/page.h:
>>
>> /*
>> * Unfortunately the PLT is in the BSS in the PPC32 ELF ABI,
>> * and needs to be executable. This means the whole heap ends
>> * up being executable.
>> */
>> #define VM_DATA_DEFAULT_FLAGS32 (VM_READ | VM_WRITE | VM_EXEC | \
>> VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC)
>>
>>
>> What is the rationale for this? This comment must be *really* old,
>
> I think the comment is just plain wrong. ppc32 needs an executable
> stack because it builds trampolines on the stack to support calling
> nested functions. I presume that's why the heap is executable.
The stack and the heap are separate mappings.
> (If
> I'm wrong about heap+stack needing the same protection then I can't
> think of any reason to require an executable heap.)
The heap and the BSS initially share the same page.
Andreas.
--
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756 01D3 44D5 214B 8276 4ED5
"And now for something completely different."
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: ppc64 sbrk returns executable heap in 32-bit emulation mode
2016-05-16 6:24 ` Alan Modra
@ 2016-05-16 8:51 ` Florian Weimer
0 siblings, 0 replies; 7+ messages in thread
From: Florian Weimer @ 2016-05-16 8:51 UTC (permalink / raw)
To: Alan Modra; +Cc: linuxppc-dev
On 05/16/2016 08:24 AM, Alan Modra wrote:
> On Thu, May 12, 2016 at 03:41:09PM +0200, Florian Weimer wrote:
>> We noticed that on ppc64, the sbrk system call in the 32-bit subsystem
>> returns executable memory. I assume it is related to this, in
>> arch/powerpc/include/asm/page.h:
>>
>> /*
>> * Unfortunately the PLT is in the BSS in the PPC32 ELF ABI,
>> * and needs to be executable. This means the whole heap ends
>> * up being executable.
>> */
>> #define VM_DATA_DEFAULT_FLAGS32 (VM_READ | VM_WRITE | VM_EXEC | \
>> VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC)
>>
>>
>> What is the rationale for this? This comment must be *really* old,
>
> I think the comment is just plain wrong. ppc32 needs an executable
> stack because it builds trampolines on the stack to support calling
> nested functions. I presume that's why the heap is executable. (If
> I'm wrong about heap+stack needing the same protection then I can't
> think of any reason to require an executable heap.)
But the stack is *not* executable. The program is marked correctly with
GNU_STACK:
GNU_STACK 0x000000 0x00000000 0x00000000 0x000000 0x000000 RW 0x10
If this program header were not there (or said RWX instead of RW), then
I expect the kernel would make the stack executable, for backwards
compatibility.
And the test reports this:
# Subtests with executable mappings or inconsistent results
exec MAP_ANONYMOUS with R|W, then mprotect R|X
exec MAP_ANONYMOUS with R|X, then mprotect R|W, mprotect R|X
FAIL exec malloc (small) (unexpected result)
FAIL exec malloc (page size) (unexpected result)
exec MAP_ANONYMOUS, with PROT_EXEC, without mprotect
exec file with MAP_PRIVATE, with PROT_EXEC, without mprotect,
without alias
exec file with MAP_SHARED, with PROT_EXEC, without mprotect,
without alias
exec shared memory with MAP_PRIVATE, with PROT_EXEC, without
mprotect, without alias
exec shared memory with MAP_SHARED, with PROT_EXEC, without
mprotect, without alias
exec file with MAP_SHARED, with PROT_EXEC, without mprotect,
with alias
exec shared memory with MAP_SHARED, with PROT_EXEC, without
mprotect, with alias
exec MAP_ANONYMOUS, with PROT_EXEC, with mprotect
exec file with MAP_PRIVATE, with PROT_EXEC, with mprotect,
without alias
exec file with MAP_SHARED, with PROT_EXEC, with mprotect,
without alias
exec shared memory with MAP_PRIVATE, with PROT_EXEC, with
mprotect, without alias
exec shared memory with MAP_SHARED, with PROT_EXEC, with
mprotect, without alias
exec file with MAP_SHARED, with PROT_EXEC, with mprotect, with
alias
exec shared memory with MAP_SHARED, with PROT_EXEC, with
mprotect, with alias
(Sorry for the line wrapping, also available at
<https://paste.fedoraproject.org/367036/>.)
The tests I'm using are these: <https://pagure.io/execmod-tests> It
also covers the stack, but it is not listed in the summary because it is
not executable.
Only the heap is executable. Everything else is set up as expected by
the kernel or by glibc.
Florian
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: ppc64 sbrk returns executable heap in 32-bit emulation mode
2016-05-16 8:49 ` Andreas Schwab
@ 2016-05-16 8:59 ` Florian Weimer
2016-05-16 9:09 ` Andreas Schwab
0 siblings, 1 reply; 7+ messages in thread
From: Florian Weimer @ 2016-05-16 8:59 UTC (permalink / raw)
To: Andreas Schwab, Alan Modra; +Cc: linuxppc-dev
On 05/16/2016 10:49 AM, Andreas Schwab wrote:
>> (If
>> I'm wrong about heap+stack needing the same protection then I can't
>> think of any reason to require an executable heap.)
>
> The heap and the BSS initially share the same page.
But my test says that at least part of .bss in the main executable is
*not* executable.
Any ideas why the kernel behaves the way it does? Can we change the
behavior at least for ELF binaries which use glibc (or something else
which indicates a non-legacy binary)?
Thanks,
Florian
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: ppc64 sbrk returns executable heap in 32-bit emulation mode
2016-05-16 8:59 ` Florian Weimer
@ 2016-05-16 9:09 ` Andreas Schwab
2016-05-16 16:27 ` Florian Weimer
0 siblings, 1 reply; 7+ messages in thread
From: Andreas Schwab @ 2016-05-16 9:09 UTC (permalink / raw)
To: Florian Weimer; +Cc: Alan Modra, linuxppc-dev
Florian Weimer <fweimer@redhat.com> writes:
> But my test says that at least part of .bss in the main executable is
> *not* executable.
Build with -mbss-plt -Wl,--bss-plt.
Andreas.
--
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756 01D3 44D5 214B 8276 4ED5
"And now for something completely different."
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: ppc64 sbrk returns executable heap in 32-bit emulation mode
2016-05-16 9:09 ` Andreas Schwab
@ 2016-05-16 16:27 ` Florian Weimer
0 siblings, 0 replies; 7+ messages in thread
From: Florian Weimer @ 2016-05-16 16:27 UTC (permalink / raw)
To: Andreas Schwab; +Cc: Alan Modra, linuxppc-dev
On 05/16/2016 11:09 AM, Andreas Schwab wrote:
> Florian Weimer <fweimer@redhat.com> writes:
>
>> But my test says that at least part of .bss in the main executable is
>> *not* executable.
>
> Build with -mbss-plt -Wl,--bss-plt.
This gives me:
FAIL exec .bss data (unexpected result)
FAIL exec .data (unexpected result)
FAIL exec malloc (small) (unexpected result)
FAIL exec malloc (page size) (unexpected result)
And this is somewhat expected: There is an RWX LOAD segment which covers
.bss and .data, and it also overlaps with the heap.
So maybe we can change the kernel heap default in case there is no RWX
LOAD segment, so that most binaries get a non-executable heap? What do
you think?
Thanks,
Florian
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2016-05-16 16:27 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-05-12 13:41 ppc64 sbrk returns executable heap in 32-bit emulation mode Florian Weimer
2016-05-16 6:24 ` Alan Modra
2016-05-16 8:51 ` Florian Weimer
[not found] ` <20160516062425.GA24091__32035.8907142237$1463379977$gmane$org@bubble.grove.modra.org>
2016-05-16 8:49 ` Andreas Schwab
2016-05-16 8:59 ` Florian Weimer
2016-05-16 9:09 ` Andreas Schwab
2016-05-16 16:27 ` Florian Weimer
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).