Skip to contentSkip to conversation

kbuild: add '-fno-stack-check' to barebox build options

PATCH Applied Ahmad Fatoum 3 participants 5 messages Feb 23, 2021 – Mar 22, 2021 Kbuild

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 kbuild: add '-fno-stack-check' to barebox build options

This ports over Linux commit 3ce120b16cc5 ("kbuild: add '-fno-stack-check' to kernel build options"):

Author: Linus Torvalds <[email protected]>

kbuild: add '-fno-stack-check' to kernel build options

It appears that hardened gentoo enables "-fstack-check" by default for gcc.

That doesn't work _at_all_ for the kernel, because the kernel stack doesn't act like a user stack at all: it's much smaller, and it doesn't auto-expand on use. So the extra "probe one page below the stack" code generated by -fstack-check just breaks the kernel in horrible ways, causing infinite double faults etc.

[ I have to say, that the particular code gcc generates looks very
stupid even for user space where it works, but that's a separate issue. ]

I am porting it, because it may become more acute with the incoming support for multiple stacks (via initjmp).

---
 Makefile | 3 +++
 1 file changed, 3 insertions(+)
Makefile +3 -0
index f3c85cff9430..fa7fbbeb35f0 100644
@@ -622,6 +622,9 @@ KBUILD_CFLAGS += $(call cc-option, -fno-delete-null-pointer-checks,)
 # disable invalid "can't wrap" optimizations for signed / pointers
 KBUILD_CFLAGS	+= $(call cc-option,-fno-strict-overflow)
 
+# Make sure -fstack-check isn't enabled (like gentoo apparently did)
+KBUILD_CFLAGS  += $(call cc-option,-fno-stack-check)
+
 KBUILD_CFLAGS   += $(call cc-disable-warning, address-of-packed-member)
 
 # Align the bit size of userspace programs with the kernel
Signature · 7 lines
2.30.0


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#1 lore raw

PATCH 2/2 kbuild: add '-fcf-protection=none' to barebox build options

Ubuntu 19.10 enables this by default for x86 architectures. The option instructs the compiler to generate special nop instruction at all places where execution may jump to. This not only means the incoming setjmp implementations will need to be adjusted[1], but in case the option is extended to expose similar features of other microarches, this could lead to breakages there with newer compilers. Only safe way to use it seems to disable it wholesale and enable it selectively for platforms that are aware of it. So do the disabling part.

[1]: https://github.com/ipxe/ipxe/commit/e8393c372

---
 Makefile | 4 ++++
 1 file changed, 4 insertions(+)
Makefile +4 -0
index fa7fbbeb35f0..261c68c1865d 100644
@@ -625,6 +625,10 @@ KBUILD_CFLAGS	+= $(call cc-option,-fno-strict-overflow)
 # Make sure -fstack-check isn't enabled (like gentoo apparently did)
 KBUILD_CFLAGS  += $(call cc-option,-fno-stack-check)
 
+# ensure -fcf-protection is disabled as it is incompatible with our sjlj
+# Platforms that have their setjmp appropriately implemented may override this
+KBUILD_CFLAGS += $(call cc-option,-fcf-protection=none)
+
 KBUILD_CFLAGS   += $(call cc-disable-warning, address-of-packed-member)
 
 # Align the bit size of userspace programs with the kernel
Signature · 7 lines
2.30.0


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
Applied
#2 lore raw
In reply to Ahmad Fatoum
Quoted 20 lines · Ahmad Fatoum

This ports over Linux commit 3ce120b16cc5 ("kbuild: add '-fno-stack-check' to kernel build options"):

Author: Linus Torvalds <[email protected]>

kbuild: add '-fno-stack-check' to kernel build options

It appears that hardened gentoo enables "-fstack-check" by default for gcc.

That doesn't work _at_all_ for the kernel, because the kernel stack doesn't act like a user stack at all: it's much smaller, and it doesn't auto-expand on use. So the extra "probe one page below the stack" code generated by -fstack-check just breaks the kernel in horrible ways, causing infinite double faults etc.

[ I have to say, that the particular code gcc generates looks very
stupid even for user space where it works, but that's a separate issue. ]

Quoted 7 lines

I am porting it, because it may become more acute with the incoming support for multiple stacks (via initjmp).

---

Applied, thanks

Sascha

Quoted 26 lines
 Makefile | 3 +++
 1 file changed, 3 insertions(+)
Makefile +3 -0
index f3c85cff9430..fa7fbbeb35f0 100644
@@ -622,6 +622,9 @@ KBUILD_CFLAGS += $(call cc-option, -fno-delete-null-pointer-checks,)
 # disable invalid "can't wrap" optimizations for signed / pointers
 KBUILD_CFLAGS	+= $(call cc-option,-fno-strict-overflow)
 
+# Make sure -fstack-check isn't enabled (like gentoo apparently did)
+KBUILD_CFLAGS  += $(call cc-option,-fno-stack-check)
+
 KBUILD_CFLAGS   += $(call cc-disable-warning, address-of-packed-member)
 
 # Align the bit size of userspace programs with the kernel
Signature · 7 lines
2.30.0


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
Signature · 9 lines
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#3 lore raw

Hello Sascha,

Quoted 23 lines · Sascha Hauer
Quoted 22 lines · Ahmad Fatoum

This ports over Linux commit 3ce120b16cc5 ("kbuild: add '-fno-stack-check' to kernel build options"):

Author: Linus Torvalds <[email protected]>

kbuild: add '-fno-stack-check' to kernel build options

It appears that hardened gentoo enables "-fstack-check" by default for gcc.

That doesn't work _at_all_ for the kernel, because the kernel stack doesn't act like a user stack at all: it's much smaller, and it doesn't auto-expand on use. So the extra "probe one page below the stack" code generated by -fstack-check just breaks the kernel in horrible ways, causing infinite double faults etc.

[ I have to say, that the particular code gcc generates looks very
stupid even for user space where it works, but that's a separate issue. ]

Quoted 7 lines
Quoted 5 lines

I am porting it, because it may become more acute with the incoming support for multiple stacks (via initjmp).

---

Applied, thanks

Can't find both patches in next.

Cheers,
Ahmad

Quoted 30 lines

Sascha

Quoted 26 lines
 Makefile | 3 +++
 1 file changed, 3 insertions(+)
Makefile +3 -0
index f3c85cff9430..fa7fbbeb35f0 100644
@@ -622,6 +622,9 @@ KBUILD_CFLAGS += $(call cc-option, -fno-delete-null-pointer-checks,)
 # disable invalid "can't wrap" optimizations for signed / pointers
 KBUILD_CFLAGS	+= $(call cc-option,-fno-strict-overflow)
 
+# Make sure -fstack-check isn't enabled (like gentoo apparently did)
+KBUILD_CFLAGS  += $(call cc-option,-fno-stack-check)
+
 KBUILD_CFLAGS   += $(call cc-disable-warning, address-of-packed-member)
 
 # Align the bit size of userspace programs with the kernel
Signature · 7 lines
2.30.0


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
Signature · 9 lines
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#4 lore raw
Quoted 29 lines · Ahmad Fatoum

Hello Sascha,

Quoted 26 lines · Sascha Hauer
Quoted 25 lines · Ahmad Fatoum

This ports over Linux commit 3ce120b16cc5 ("kbuild: add '-fno-stack-check' to kernel build options"):

Author: Linus Torvalds <[email protected]>

kbuild: add '-fno-stack-check' to kernel build options

It appears that hardened gentoo enables "-fstack-check" by default for gcc.

That doesn't work _at_all_ for the kernel, because the kernel stack doesn't act like a user stack at all: it's much smaller, and it doesn't auto-expand on use. So the extra "probe one page below the stack" code generated by -fstack-check just breaks the kernel in horrible ways, causing infinite double faults etc.

[ I have to say, that the particular code gcc generates looks very
stupid even for user space where it works, but that's a separate issue. ]

I am porting it, because it may become more acute with the incoming support for multiple stacks (via initjmp).

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

---

Applied, thanks

Can't find both patches in next.

They should be there now.

Sascha

Signature · 9 lines
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox