Skip to content

Commit d38cc52

Browse files
authored
Add support for -fstack-protector (#22229)
Fixes: #9780
1 parent c890466 commit d38cc52

File tree

6 files changed

+26
-1
lines changed

6 files changed

+26
-1
lines changed

system/lib/libc/musl/src/env/__stack_chk_fail.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ void __init_ssp(void *entropy)
2323

2424
void __stack_chk_fail(void)
2525
{
26+
#if defined(__EMSCRIPTEN__) && !defined(NDEBUG)
27+
// Report the reason for the crash, at least in debug builds.
28+
// This is the same message that glibc outputs (even in release builds).
29+
emscripten_err("*** stack smashing detected ***");
30+
#endif
2631
a_crash();
2732
}
2833

test/core/test_dlfcn_self.exports

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ __progname_full
99
__sig_actions
1010
__sig_pending
1111
__signgam
12+
__stack_chk_guard
1213
__threwValue
1314
__timezone
1415
__tzname

test/other/test_stack_protector.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <alloca.h>
2+
#include <memory.h>
3+
#include <string.h>
4+
#include <stdio.h>
5+
6+
char mystring[] = "Hello world";
7+
8+
int main() {
9+
char buf[10];
10+
// This is not valid since mystring is larger than `buf`
11+
strcpy(buf, mystring);
12+
printf("buf: %s\n", buf);
13+
return 0;
14+
}

test/other/test_stack_protector.out

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
buf: Hello world
2+
*** stack smashing detected ***

test/test_other.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14937,3 +14937,6 @@ def test_std_promise_link(self, *args):
1493714937
}
1493814938
''')
1493914939
self.run_process([EMXX, 'src.cpp', '-pthread'] + list(args))
14940+
14941+
def test_stack_protector(self):
14942+
self.do_other_test('test_stack_protector.c', emcc_args=['-fstack-protector'], assert_returncode=NON_ZERO)

tools/system_libs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1103,7 +1103,7 @@ def get_files(self):
11031103
'fork.c', 'vfork.c', 'posix_spawn.c', 'posix_spawnp.c', 'execve.c', 'waitid.c', 'system.c',
11041104
'_Fork.c',
11051105
# 'env' exclusion
1106-
'__reset_tls.c', '__init_tls.c', '__libc_start_main.c', '__stack_chk_fail.c',
1106+
'__reset_tls.c', '__init_tls.c', '__libc_start_main.c',
11071107
]
11081108

11091109
ignore += LIBC_SOCKETS

0 commit comments

Comments
 (0)