tools/nolibc: add support for stack protector
Subsystems (2)
Thomas Weißschuh
maintainerv2 cover tools/nolibc: add support for stack protector
This is useful when using nolibc for security-critical tools. Using nolibc has the advantage that the code is easily auditable and sandboxable with seccomp as no unexpected syscalls are used. Using compiler-assistent stack protection provides another security mechanism.
For this to work the compiler and libc have to collaborate.
This patch adds the following parts to nolibc that are required by the compiler:
* __stack_chk_guard: random sentinel value
* __stack_chk_fail: handler for detected stack smashes
In addition an initialization function is added that randomizes the sentinel value.
Only support for global guards is implemented.
Register guards are useful in multi-threaded context which nolibc does not provide support for.
---
Changes in v2:
- Code and comments style fixes
- Only use raw syscalls in stackprotector functions
- Remove need for dedicated entrypoint and exec() during tests
- Add more rationale
- Shuffle some code around between commits
- Provide compatibility with the -fno-stack-protector patch
- Remove RFC status
- Link to v1: https://lore.kernel.org/r/[email protected]
This series is based on the current rcu/dev branch of Pauls rcu tree.
---
Thomas Weißschuh (8):
tools/nolibc: add definitions for standard fds tools/nolibc: add helpers for wait() signal exits tools/nolibc: tests: constify test_names tools/nolibc: add support for stack protector tools/nolibc: tests: fold in no-stack-protector cflags tools/nolibc: tests: add test for -fstack-protector tools/nolibc: i386: add stackprotector support tools/nolibc: x86_64: add stackprotector support
tools/include/nolibc/Makefile | 4 +- tools/include/nolibc/arch-i386.h | 7 ++- tools/include/nolibc/arch-x86_64.h | 5 +++ tools/include/nolibc/nolibc.h | 1 + tools/include/nolibc/stackprotector.h | 53 +++++++++++++++++++++++ tools/include/nolibc/types.h | 2 + tools/include/nolibc/unistd.h | 5 +++ tools/testing/selftests/nolibc/Makefile | 11 ++++- tools/testing/selftests/nolibc/nolibc-test.c | 64 ++++++++++++++++++++++++++-- 9 files changed, 144 insertions(+), 8 deletions(-)
---
base-commit: a9b8406e51603238941dbc6fa1437f8915254ebb
change-id: 20230223-nolibc-stackprotector-d4d5f48ff771
Best regards,
Signature · 1 line
Thomas Weißschuh <[email protected]>
Thomas Weißschuh
maintainerPATCH v2 1/8 tools/nolibc: add definitions for standard fds
These are useful for users and will also be used in an upcoming testcase.
--- tools/include/nolibc/unistd.h | 5 +++++ 1 file changed, 5 insertions(+)
tools/include/nolibc/unistd.h +5 -0
@@ -13,6 +13,11 @@#include "sys.h" +#define STDIN_FILENO 0 +#define STDOUT_FILENO 1 +#define STDERR_FILENO 2 + +static __attribute__((unused))int msleep (unsigned int msecs) {
Signature · 1 line
2.40.0
Thomas Weißschuh
maintainerPATCH v2 2/8 tools/nolibc: add helpers for wait() signal exits
In reply to Thomas WeißschuhThese are useful for users and will also be used in an upcoming testcase.
--- tools/include/nolibc/types.h | 2 ++ 1 file changed, 2 insertions(+)
tools/include/nolibc/types.h +2 -0
@@ -97,6 +97,8 @@/* Macros used on waitpid()'s return status */ #define WEXITSTATUS (status) (((status) & 0xff00) >> 8)#define WIFEXITED (status) (((status) & 0x7f) == 0) +#define WTERMSIG (status) ((status) & 0x7f) +#define WIFSIGNALED (status) ((status) - 1 < 0xff)/* waitpid() flags */ #define WNOHANG 1
Signature · 1 line
2.40.0
Thomas Weißschuh
maintainerPATCH v2 3/8 tools/nolibc: tests: constify test_names
In reply to Thomas WeißschuhNothing ever modifies this structure.
--- tools/testing/selftests/nolibc/nolibc-test.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/nolibc/nolibc-test.c +1 -1
@@ -717,7 +717,7 @@ int prepare(void) }/* This is the definition of known test names, with their functions */ -static struct test test_names[] = { +static const struct test test_names[] = {/* add new tests here */ { .name ="syscall" , .func = run_syscall }, { .name ="stdlib" , .func = run_stdlib },
Signature · 1 line
2.40.0
Thomas Weißschuh
maintainerPATCH v2 4/8 tools/nolibc: add support for stack protector
In reply to Thomas WeißschuhThis is useful when using nolibc for security-critical tools. Using nolibc has the advantage that the code is easily auditable and sandboxable with seccomp as no unexpected syscalls are used. Using compiler-assistent stack protection provides another security mechanism.
For this to work the compiler and libc have to collaborate.
This patch adds the following parts to nolibc that are required by the compiler:
* __stack_chk_guard: random sentinel value
* __stack_chk_fail: handler for detected stack smashes
In addition an initialization function is added that randomizes the sentinel value.
Only support for global guards is implemented.
Register guards are useful in multi-threaded context which nolibc does not provide support for.
--- tools/include/nolibc/Makefile | 4 +-- tools/include/nolibc/nolibc.h | 1 + tools/include/nolibc/stackprotector.h | 53 +++++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 2 deletions(-)
tools/include/nolibc/Makefile +2 -2
@@ -25,8 +25,8 @@ endif nolibc_arch := $(patsubst arm64,aarch64,$(ARCH)) arch_file := arch-$(nolibc_arch).h -all_files := ctype.h errno.h nolibc.h signal.h std.h stdint.h stdio.h stdlib.h \ - string.h sys.h time.h types.h unistd.h +all_files := ctype.h errno.h nolibc.h signal.h stackprotector.h std.h stdint.h \ + stdio.h stdlib.h string.h sys.h time.h types.h unistd.h # install all headers needed to support a bare-metal compiler all: headers
tools/include/nolibc/nolibc.h +1 -0
@@ -104,6 +104,7 @@#include "string.h" #include "time.h" #include "unistd.h" +#include "stackprotector.h" /* Used by programs to avoid std includes */ #define NOLIBC
tools/include/nolibc/stackprotector.h new file +53 -0
53 lines not on this page · raw message
Signature · 1 line
2.40.0
Thomas Weißschuh
maintainerPATCH v2 5/8 tools/nolibc: tests: fold in no-stack-protector cflags
In reply to Thomas WeißschuhFor the cflags to enable stack protectors to work properly they need to be specified after -fno-stack-protector.
To do this fold all cflags into a single variable and move -fno-stack-protector before the arch-specific cflags.
--- tools/testing/selftests/nolibc/Makefile | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
tools/testing/selftests/nolibc/Makefile +3 -2
@@ -77,8 +77,9 @@ Q=@ endif CFLAGS_s390 = -m64 -CFLAGS ?= -Os -fno-ident -fno-asynchronous-unwind-tables $(CFLAGS_$(ARCH)) -CFLAGS += $(call cc-option,-fno-stack-protector) +CFLAGS ?= -Os -fno-ident -fno-asynchronous-unwind-tables \ + $(call cc-option,-fno-stack-protector) \ + $(CFLAGS_$(ARCH)) LDFLAGS := -s help:
Signature · 1 line
2.40.0
Thomas Weißschuh
maintainerPATCH v2 6/8 tools/nolibc: tests: add test for -fstack-protector
In reply to Thomas WeißschuhTest the previously introduce stack protector functionality in nolibc.
--- tools/testing/selftests/nolibc/Makefile | 3 ++ tools/testing/selftests/nolibc/nolibc-test.c | 62 +++++++++++++++++++++++++++- 2 files changed, 63 insertions(+), 2 deletions(-)
tools/testing/selftests/nolibc/Makefile +3 -0
@@ -76,6 +76,9 @@ else Q=@ endif +CFLAGS_STACKPROTECTOR = -DNOLIBC_STACKPROTECTOR \ + $(call cc-option,-mstack-protector-guard=global) \ + $(call cc-option,-fstack-protector-all) CFLAGS_s390 = -m64 CFLAGS ?= -Os -fno-ident -fno-asynchronous-unwind-tables \ $(call cc-option,-fno-stack-protector) \
tools/testing/selftests/nolibc/nolibc-test.c +60 -2
74 lines not on this page · raw message
Signature · 1 line
2.40.0
Thomas Weißschuh
maintainerPATCH v2 7/8 tools/nolibc: i386: add stackprotector support
In reply to Thomas WeißschuhEnable the new stackprotector support for i386.
--- tools/include/nolibc/arch-i386.h | 7 ++++++- tools/testing/selftests/nolibc/Makefile | 1 + 2 files changed, 7 insertions(+), 1 deletion(-)
tools/include/nolibc/arch-i386.h +6 -1
@@ -181,6 +181,8 @@ struct sys_stat_struct { char **environ __attribute__((weak)); const unsigned long *_auxv __attribute__((weak)); +#define __ARCH_SUPPORTS_STACK_PROTECTOR +/* startup code */ /* * i386 System V ABI mandates:
@@ -188,9 +190,12 @@ const unsigned long *_auxv __attribute__((weak));* 2) The deepest stack frame should be set to zero * */ -void __attribute__((weak,noreturn,optimize ("omit-frame-pointer" )))_start (void ) +void __attribute__((weak,noreturn,optimize ("omit-frame-pointer" ),no_stack_protector))_start (void ) { __asm__volatile ( +#ifdef NOLIBC_STACKPROTECTOR + "call __stack_chk_init\n" // initialize stack protector +#endif"pop %eax\n" // argc (first arg, %eax) "mov %esp, %ebx\n" // argv[] (second arg, %ebx) "lea 4(%ebx,%eax,4),%ecx\n" // then a NULL then envp (third arg, %ecx)
tools/testing/selftests/nolibc/Makefile +1 -0
@@ -79,6 +79,7 @@ endif CFLAGS_STACKPROTECTOR = -DNOLIBC_STACKPROTECTOR \ $(call cc-option,-mstack-protector-guard=global) \ $(call cc-option,-fstack-protector-all) +CFLAGS_i386 = $(CFLAGS_STACKPROTECTOR) CFLAGS_s390 = -m64 CFLAGS ?= -Os -fno-ident -fno-asynchronous-unwind-tables \ $(call cc-option,-fno-stack-protector) \
Signature · 1 line
2.40.0
Thomas Weißschuh
maintainerPATCH v2 8/8 tools/nolibc: x86_64: add stackprotector support
In reply to Thomas WeißschuhEnable the new stackprotector support for x86_64.
--- tools/include/nolibc/arch-x86_64.h | 5 +++++ tools/testing/selftests/nolibc/Makefile | 2 ++ 2 files changed, 7 insertions(+)
tools/include/nolibc/arch-x86_64.h +5 -0
@@ -181,6 +181,8 @@ struct sys_stat_struct { char **environ __attribute__((weak)); const unsigned long *_auxv __attribute__((weak)); +#define __ARCH_SUPPORTS_STACK_PROTECTOR +/* startup code */ /* * x86-64 System V ABI mandates:
@@ -191,6 +193,9 @@ const unsigned long *_auxv __attribute__((weak));void __attribute__((weak,noreturn,optimize ("omit-frame-pointer" )))_start (void ) { __asm__volatile ( +#ifdef NOLIBC_STACKPROTECTOR + "call __stack_chk_init\n" // initialize stack protector +#endif"pop %rdi\n" // argc (first arg, %rdi) "mov %rsp, %rsi\n" // argv[] (second arg, %rsi) "lea 8(%rsi,%rdi,8),%rdx\n" // then a NULL then envp (third arg, %rdx)
tools/testing/selftests/nolibc/Makefile +2 -0
@@ -80,6 +80,8 @@ CFLAGS_STACKPROTECTOR = -DNOLIBC_STACKPROTECTOR \ $(call cc-option,-mstack-protector-guard=global) \ $(call cc-option,-fstack-protector-all) CFLAGS_i386 = $(CFLAGS_STACKPROTECTOR) +CFLAGS_x86_64 = $(CFLAGS_STACKPROTECTOR) +CFLAGS_x86 = $(CFLAGS_STACKPROTECTOR) CFLAGS_s390 = -m64 CFLAGS ?= -Os -fno-ident -fno-asynchronous-unwind-tables \ $(call cc-option,-fno-stack-protector) \
Signature · 1 line
2.40.0
Willy Tarreau
maintainerRe: [PATCH v2 8/8] tools/nolibc: x86_64: add stackprotector support
Hi Thomas,
Quoted 1 line · Thomas Weißschuh
Enable the new stackprotector support for
x86_64.
(...)
tools/testing/selftests/nolibc/Makefile CFLAGS_STACKPROTECTOR = -DNOLIBC_STACKPROTECTOR \ · line 87
Quoted 13 lines
tools/testing/selftests/nolibc/Makefile +2
-0@@ -80,6 +80,8 @@ CFLAGS_STACKPROTECTOR = -DNOLIBC_STACKPROTECTOR \ $(call cc-option,-mstack-protector-guard=global) \ $(call cc-option,-fstack-protector-all) CFLAGS_i386 = $(CFLAGS_STACKPROTECTOR) +CFLAGS_x86_64 = $(CFLAGS_STACKPROTECTOR) +CFLAGS_x86 = $(CFLAGS_STACKPROTECTOR) CFLAGS_s390 = -m64 CFLAGS ?= -Os -fno-ident -fno-asynchronous-unwind-tables \ $(call cc-option,-fno-stack-protector) \
This change is making it almost impossible for me to pass external CFLAGS without forcefully disabling the automatic detection of stackprot. I need to do it for some archs (e.g. "-march=armv5t -mthumb") or even to change optimization levels.
I figured that the simplest way to recover that functionality for me consists in using a dedicated variable to assign stack protector per supported architecure and concatenating it to the per-arch CFLAGS like this:
diff --git a/tools/testing/selftests/nolibc/Makefile b/tools/testing/selftests/nolibc/Makefile
index 543555f4cbdc..bbce57420465 100644
--- a/tools/testing/selftests/nolibc/Makefile
+++ b/tools/testing/selftests/nolibc/Makefile
@@ -79,13 +79,13 @@ endif
CFLAGS_STACKPROTECTOR = -DNOLIBC_STACKPROTECTOR \
$(call cc-option,-mstack-protector-guard=global) \
$(call cc-option,-fstack-protector-all)
-CFLAGS_i386 = $(CFLAGS_STACKPROTECTOR)
-CFLAGS_x86_64 = $(CFLAGS_STACKPROTECTOR)
-CFLAGS_x86 = $(CFLAGS_STACKPROTECTOR)
+CFLAGS_STKP_i386 = $(CFLAGS_STACKPROTECTOR)
+CFLAGS_STKP_x86_64 = $(CFLAGS_STACKPROTECTOR)
+CFLAGS_STKP_x86 = $(CFLAGS_STACKPROTECTOR)
CFLAGS_s390 = -m64
CFLAGS ?= -Os -fno-ident -fno-asynchronous-unwind-tables \
$(call cc-option,-fno-stack-protector) \
- $(CFLAGS_$(ARCH))
+ $(CFLAGS_STKP_$(ARCH)) $(CFLAGS_$(ARCH))
LDFLAGS := -shelp:
And now with this it works again for me on all archs, with all of them showing "SKIPPED" for the -fstackprotector line except i386/x86_64 which show "OK".
Are you OK with this approach ? And if so, do you want to respin it or do you want me to retrofit it into your 3 patches that introduce this change (it's easy enough so I really don't care) ?
Thanks!
Willy
Thomas Weißschuh
maintainerRe: [PATCH v2 8/8] tools/nolibc: x86_64: add stackprotector support
Hi Willy,
Quoted 50 lines · Willy Tarreau
Quoted 1 line · Thomas Weißschuh
Enable the new stackprotector support for
x86_64.(...)
tools/testing/selftests/nolibc/Makefile CFLAGS_STACKPROTECTOR = -DNOLIBC_STACKPROTECTOR \ · line 87
Quoted 13 lines
tools/testing/selftests/nolibc/Makefile +2
-0@@ -80,6 +80,8 @@ CFLAGS_STACKPROTECTOR = -DNOLIBC_STACKPROTECTOR \ $(call cc-option,-mstack-protector-guard=global) \ $(call cc-option,-fstack-protector-all) CFLAGS_i386 = $(CFLAGS_STACKPROTECTOR) +CFLAGS_x86_64 = $(CFLAGS_STACKPROTECTOR) +CFLAGS_x86 = $(CFLAGS_STACKPROTECTOR) CFLAGS_s390 = -m64 CFLAGS ?= -Os -fno-ident -fno-asynchronous-unwind-tables \ $(call cc-option,-fno-stack-protector) \This change is making it almost impossible for me to pass external CFLAGS without forcefully disabling the automatic detection of stackprot. I need to do it for some archs (e.g. "-march=armv5t -mthumb") or even to change optimization levels.
I figured that the simplest way to recover that functionality for me consists in using a dedicated variable to assign stack protector per supported architecure and concatenating it to the per-arch CFLAGS like this:
diff --git a/tools/testing/selftests/nolibc/Makefile b/tools/testing/selftests/nolibc/Makefile index 543555f4cbdc..bbce57420465 100644 --- a/tools/testing/selftests/nolibc/Makefile +++ b/tools/testing/selftests/nolibc/Makefile @@ -79,13 +79,13 @@ endif CFLAGS_STACKPROTECTOR = -DNOLIBC_STACKPROTECTOR \ $(call cc-option,-mstack-protector-guard=global) \ $(call cc-option,-fstack-protector-all) -CFLAGS_i386 = $(CFLAGS_STACKPROTECTOR) -CFLAGS_x86_64 = $(CFLAGS_STACKPROTECTOR) -CFLAGS_x86 = $(CFLAGS_STACKPROTECTOR) +CFLAGS_STKP_i386 = $(CFLAGS_STACKPROTECTOR) +CFLAGS_STKP_x86_64 = $(CFLAGS_STACKPROTECTOR) +CFLAGS_STKP_x86 = $(CFLAGS_STACKPROTECTOR) CFLAGS_s390 = -m64 CFLAGS ?= -Os -fno-ident -fno-asynchronous-unwind-tables \ $(call cc-option,-fno-stack-protector) \ - $(CFLAGS_$(ARCH)) + $(CFLAGS_STKP_$(ARCH)) $(CFLAGS_$(ARCH)) LDFLAGS := -shelp:
Quoted 7 lines
And now with this it works again for me on all archs, with all of them showing "SKIPPED" for the -fstackprotector line except i386/x86_64 which show "OK".
Are you OK with this approach ? And if so, do you want to respin it or do you want me to retrofit it into your 3 patches that introduce this change (it's easy enough so I really don't care) ?
Looks good to me.
If nothing else needs to be changed feel free to fix it up on your side.
Thanks,
Thomas
Willy Tarreau
maintainerRe: [PATCH v2 8/8] tools/nolibc: x86_64: add stackprotector support
Quoted 57 lines · Thomas Weißschuh
Hi Willy,
Quoted 54 lines · Willy Tarreau
Quoted 1 line · Thomas Weißschuh
Enable the new stackprotector support for
x86_64.(...)
tools/testing/selftests/nolibc/Makefile CFLAGS_STACKPROTECTOR = -DNOLIBC_STACKPROTECTOR \ · line 87
Quoted 13 lines
tools/testing/selftests/nolibc/Makefile +2
-0@@ -80,6 +80,8 @@ CFLAGS_STACKPROTECTOR = -DNOLIBC_STACKPROTECTOR \ $(call cc-option,-mstack-protector-guard=global) \ $(call cc-option,-fstack-protector-all) CFLAGS_i386 = $(CFLAGS_STACKPROTECTOR) +CFLAGS_x86_64 = $(CFLAGS_STACKPROTECTOR) +CFLAGS_x86 = $(CFLAGS_STACKPROTECTOR) CFLAGS_s390 = -m64 CFLAGS ?= -Os -fno-ident -fno-asynchronous-unwind-tables \ $(call cc-option,-fno-stack-protector) \This change is making it almost impossible for me to pass external CFLAGS without forcefully disabling the automatic detection of stackprot. I need to do it for some archs (e.g. "-march=armv5t -mthumb") or even to change optimization levels.
I figured that the simplest way to recover that functionality for me consists in using a dedicated variable to assign stack protector per supported architecure and concatenating it to the per-arch CFLAGS like this:
diff --git a/tools/testing/selftests/nolibc/Makefile b/tools/testing/selftests/nolibc/Makefile index 543555f4cbdc..bbce57420465 100644 --- a/tools/testing/selftests/nolibc/Makefile +++ b/tools/testing/selftests/nolibc/Makefile @@ -79,13 +79,13 @@ endif CFLAGS_STACKPROTECTOR = -DNOLIBC_STACKPROTECTOR \ $(call cc-option,-mstack-protector-guard=global) \ $(call cc-option,-fstack-protector-all) -CFLAGS_i386 = $(CFLAGS_STACKPROTECTOR) -CFLAGS_x86_64 = $(CFLAGS_STACKPROTECTOR) -CFLAGS_x86 = $(CFLAGS_STACKPROTECTOR) +CFLAGS_STKP_i386 = $(CFLAGS_STACKPROTECTOR) +CFLAGS_STKP_x86_64 = $(CFLAGS_STACKPROTECTOR) +CFLAGS_STKP_x86 = $(CFLAGS_STACKPROTECTOR) CFLAGS_s390 = -m64 CFLAGS ?= -Os -fno-ident -fno-asynchronous-unwind-tables \ $(call cc-option,-fno-stack-protector) \ - $(CFLAGS_$(ARCH)) + $(CFLAGS_STKP_$(ARCH)) $(CFLAGS_$(ARCH)) LDFLAGS := -shelp:
And now with this it works again for me on all archs, with all of them showing "SKIPPED" for the -fstackprotector line except i386/x86_64 which show "OK".
Quoted 7 lines
Quoted 3 lines
Are you OK with this approach ? And if so, do you want to respin it or do you want me to retrofit it into your 3 patches that introduce this change (it's easy enough so I really don't care) ?
Looks good to me.
If nothing else needs to be changed feel free to fix it up on your side.
Perfect, will do it then. Thanks!
Willy
Willy Tarreau
maintainerHi Thomas,
Quoted 5 lines · Thomas Weißschuh
This is useful when using nolibc for security-critical tools. Using nolibc has the advantage that the code is easily auditable and sandboxable with seccomp as no unexpected syscalls are used. Using compiler-assistent stack protection provides another security mechanism.
(...)
Thanks for this. I had a quick look over the patches and at first glance it looks OK. I'll give it a try before this week-end on all supported archs to rule out any potential side effect, and will queue it.
cheers,
Willy