Skip to contentSkip to conversation

emu: fix executable stack marking

PATCH In review Michael Chang 2 participants 3 messages Feb 3, 2021 – Feb 5, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 emu: fix executable stack marking

The gcc by default assumes executable stack is required if the source object file doesn't have .note.GNU-stack section in place. If any of the source objects doesn't incorporate the GNU-stack note, the resulting program will have executable stack flag set in PT_GNU_STACK program header to instruct program loader or kernel to set up the exeutable stack when program loads to memory.

Usually the .note.GNU-stack section will be generated by gcc automatically if it finds that executable stack is not required. However it doesn't take care of generating .note.GNU-stack section for those object files built from assembler sources. This leads to unnecessary risk of security of exploiting the executable stack because those assembler sources don't actually require stack to be executable to work.

The grub-emu and grub-emu-lite are found to flag stack as executable revealed by execstack tool.

 $ mkdir -p build-emu && cd build-emu
 $ ../configure --with-platform=emu && make
 $ execstack -q grub-core/grub-emu grub-core/grub-emu-lite
 X grub-core/grub-emu
 X grub-core/grub-emu-lite

This patch will add the missing GNU-stack note to the assembler source used by both utilities, therefore the result doesn't count on gcc default behavior and the executable stack is disabled.

 $ execstack -q grub-core/grub-emu grub-core/grub-emu-lite
 - grub-core/grub-emu
 - grub-core/grub-emu-lite
---
 grub-core/kern/emu/cache_s.S | 5 +++++
 grub-core/lib/setjmp.S       | 4 ++++
 2 files changed, 9 insertions(+)
grub-core/kern/emu/cache_s.S +5 -0
index 2245cddc3..99d901ae0 100644
@@ -2,6 +2,11 @@
 #error "This source is only meant for grub-emu platform"
 #endif
 
+/* An executable stack is not required for these functions */
+#if defined (__linux__) && defined (__ELF__)
+.section .note.GNU-stack,"",@progbits
+#endif
+
 #if defined(__i386__) || defined(__x86_64__)
 /* Nothing is necessary.  */
 #elif defined(__sparc__)
grub-core/lib/setjmp.S +4 -0
index aa297ab0a..6128f0a9d 100644
@@ -1,3 +1,7 @@
+/* An executable stack is not required for these functions */
+#if defined (__linux__) && defined (__ELF__)
+.section .note.GNU-stack,"",@progbits
+#endif
 #if defined(__i386__)
 #include "./i386/setjmp.S"
 #elif defined(__x86_64__)
Signature · 1 line
2.26.2
#1 lore raw

Hi Michael,

Quoted 24 lines · On Wed, 3 Feb 2021 21:03:44 +0800 Michael Chang via Grub-devel…

The gcc by default assumes executable stack is required if the source object file doesn't have .note.GNU-stack section in place. If any of the source objects doesn't incorporate the GNU-stack note, the resulting program will have executable stack flag set in PT_GNU_STACK program header to instruct program loader or kernel to set up the exeutable stack when program loads to memory.

Usually the .note.GNU-stack section will be generated by gcc automatically if it finds that executable stack is not required. However it doesn't take care of generating .note.GNU-stack section for those object files built from assembler sources. This leads to unnecessary risk of security of exploiting the executable stack because those assembler sources don't actually require stack to be executable to work.

The grub-emu and grub-emu-lite are found to flag stack as executable revealed by execstack tool.

 $ mkdir -p build-emu && cd build-emu
 $ ../configure --with-platform=emu && make
 $ execstack -q grub-core/grub-emu grub-core/grub-emu-lite
 X grub-core/grub-emu
 X grub-core/grub-emu-lite
Quoted 7 lines

This patch will add the missing GNU-stack note to the assembler source used by both utilities, therefore the result doesn't count on gcc default behavior and the executable stack is disabled.

 $ execstack -q grub-core/grub-emu grub-core/grub-emu-lite
 - grub-core/grub-emu
 - grub-core/grub-emu-lite

Am I correct in thinking that this isn't as useful for the bootloader itself because, I assume, the grub linker doesn't look at that section header or flag. I'm wondering if it might be worthwhile to do this for grub modules for instance.

Glenn

#2 lore raw
Quoted 33 lines · Glenn Washburn

Hi Michael,

Quoted 28 lines · On Wed, 3 Feb 2021 21:03:44 +0800 Michael Chang via Grub-devel…

The gcc by default assumes executable stack is required if the source object file doesn't have .note.GNU-stack section in place. If any of the source objects doesn't incorporate the GNU-stack note, the resulting program will have executable stack flag set in PT_GNU_STACK program header to instruct program loader or kernel to set up the exeutable stack when program loads to memory.

Usually the .note.GNU-stack section will be generated by gcc automatically if it finds that executable stack is not required. However it doesn't take care of generating .note.GNU-stack section for those object files built from assembler sources. This leads to unnecessary risk of security of exploiting the executable stack because those assembler sources don't actually require stack to be executable to work.

The grub-emu and grub-emu-lite are found to flag stack as executable revealed by execstack tool.

 $ mkdir -p build-emu && cd build-emu
 $ ../configure --with-platform=emu && make
 $ execstack -q grub-core/grub-emu grub-core/grub-emu-lite
 X grub-core/grub-emu
 X grub-core/grub-emu-lite

This patch will add the missing GNU-stack note to the assembler source used by both utilities, therefore the result doesn't count on gcc default behavior and the executable stack is disabled.

Quoted 8 lines
Quoted 3 lines
 $ execstack -q grub-core/grub-emu grub-core/grub-emu-lite
 - grub-core/grub-emu
 - grub-core/grub-emu-lite

Am I correct in thinking that this isn't as useful for the bootloader itself because, I assume, the grub linker doesn't look at that section header or flag. I'm wondering if it might be worthwhile to do this for grub modules for instance.

The .note.GNU-stack section does exist in host's .module files but gets stripped out by genmod.sh during the build process for the target grub modules (*.mod). It could be intentional for the good of taking less memory footprint, as you have mentioned that bootloader itself don't use it thus it is reasonable to do so.

But I could imagine that it is unpleasant to see scanelf to output "bad things" of grub modules.

!WX --- --- ./grub-core/read.mod

I think we can attribute this to be a cosmetic issue and the "fix" may be otherwise unfavourable to the grub itself.

Thanks,
Michael

Quoted 3 lines

Glenn