diff options
Diffstat (limited to 'src/env')
-rw-r--r-- | src/env/__init_tls.c | 12 | ||||
-rw-r--r-- | src/env/__libc_start_main.c | 25 | ||||
-rw-r--r-- | src/env/__stack_chk_fail.c | 11 | ||||
-rw-r--r-- | src/env/secure_getenv.c | 8 |
4 files changed, 50 insertions, 6 deletions
diff --git a/src/env/__init_tls.c b/src/env/__init_tls.c index 842886f6..a93141ed 100644 --- a/src/env/__init_tls.c +++ b/src/env/__init_tls.c @@ -1,3 +1,4 @@ +#define SYSCALL_NO_TLS 1 #include <elf.h> #include <limits.h> #include <sys/mman.h> @@ -8,6 +9,8 @@ #include "atomic.h" #include "syscall.h" +volatile int __thread_list_lock; + int __init_tp(void *p) { pthread_t td = p; @@ -16,9 +19,11 @@ int __init_tp(void *p) if (r < 0) return -1; if (!r) libc.can_do_threads = 1; td->detach_state = DT_JOINABLE; - td->tid = __syscall(SYS_set_tid_address, &td->detach_state); + td->tid = __syscall(SYS_set_tid_address, &__thread_list_lock); td->locale = &libc.global_locale; td->robust_list.head = &td->robust_list.head; + td->sysinfo = __sysinfo; + td->next = td->prev = td; return 0; } @@ -62,7 +67,7 @@ void *__copy_tls(unsigned char *mem) } #endif dtv[0] = libc.tls_cnt; - td->dtv = td->dtv_copy = dtv; + td->dtv = dtv; return td; } @@ -110,7 +115,8 @@ static void static_init_tls(size_t *aux) & (main_tls.align-1); #ifdef TLS_ABOVE_TP main_tls.offset = GAP_ABOVE_TP; - main_tls.offset += -GAP_ABOVE_TP & (main_tls.align-1); + main_tls.offset += (-GAP_ABOVE_TP + (uintptr_t)main_tls.image) + & (main_tls.align-1); #else main_tls.offset = main_tls.size; #endif diff --git a/src/env/__libc_start_main.c b/src/env/__libc_start_main.c index 58da9e83..c5b277bd 100644 --- a/src/env/__libc_start_main.c +++ b/src/env/__libc_start_main.c @@ -17,6 +17,9 @@ weak_alias(dummy1, __init_ssp); #define AUX_CNT 38 +#ifdef __GNUC__ +__attribute__((__noinline__)) +#endif void __init_libc(char **envp, char *pn) { size_t i, *auxv, aux[AUX_CNT] = { 0 }; @@ -25,7 +28,7 @@ void __init_libc(char **envp, char *pn) libc.auxv = auxv = (void *)(envp+i+1); for (i=0; auxv[i]; i+=2) if (auxv[i]<AUX_CNT) aux[auxv[i]] = auxv[i+1]; __hwcap = aux[AT_HWCAP]; - __sysinfo = aux[AT_SYSINFO]; + if (aux[AT_SYSINFO]) __sysinfo = aux[AT_SYSINFO]; libc.page_size = aux[AT_PAGESZ]; if (!pn) pn = (void*)aux[AT_EXECFN]; @@ -63,11 +66,29 @@ static void libc_start_init(void) weak_alias(libc_start_init, __libc_start_init); -int __libc_start_main(int (*main)(int,char **,char **), int argc, char **argv) +typedef int lsm2_fn(int (*)(int,char **,char **), int, char **); +static lsm2_fn libc_start_main_stage2; + +int __libc_start_main(int (*main)(int,char **,char **), int argc, char **argv, + void (*init_dummy)(), void(*fini_dummy)(), void(*ldso_dummy)()) { char **envp = argv+argc+1; + /* External linkage, and explicit noinline attribute if available, + * are used to prevent the stack frame used during init from + * persisting for the entire process lifetime. */ __init_libc(envp, argv[0]); + + /* Barrier against hoisting application code or anything using ssp + * or thread pointer prior to its initialization above. */ + lsm2_fn *stage2 = libc_start_main_stage2; + __asm__ ( "" : "+r"(stage2) : : "memory" ); + return stage2(main, argc, argv); +} + +static int libc_start_main_stage2(int (*main)(int,char **,char **), int argc, char **argv) +{ + char **envp = argv+argc+1; __libc_start_init(); /* Pass control to the application */ diff --git a/src/env/__stack_chk_fail.c b/src/env/__stack_chk_fail.c index e32596d1..e5352602 100644 --- a/src/env/__stack_chk_fail.c +++ b/src/env/__stack_chk_fail.c @@ -9,7 +9,16 @@ void __init_ssp(void *entropy) if (entropy) memcpy(&__stack_chk_guard, entropy, sizeof(uintptr_t)); else __stack_chk_guard = (uintptr_t)&__stack_chk_guard * 1103515245; - __pthread_self()->CANARY = __stack_chk_guard; +#if UINTPTR_MAX >= 0xffffffffffffffff + /* Sacrifice 8 bits of entropy on 64bit to prevent leaking/ + * overwriting the canary via string-manipulation functions. + * The NULL byte is on the second byte so that off-by-ones can + * still be detected. Endianness is taken care of + * automatically. */ + ((char *)&__stack_chk_guard)[1] = 0; +#endif + + __pthread_self()->canary = __stack_chk_guard; } void __stack_chk_fail(void) diff --git a/src/env/secure_getenv.c b/src/env/secure_getenv.c new file mode 100644 index 00000000..72322f81 --- /dev/null +++ b/src/env/secure_getenv.c @@ -0,0 +1,8 @@ +#define _GNU_SOURCE +#include <stdlib.h> +#include "libc.h" + +char *secure_getenv(const char *name) +{ + return libc.secure ? NULL : getenv(name); +} |