aboutsummaryrefslogtreecommitdiff
path: root/lib/libc/secure
diff options
context:
space:
mode:
authorKonstantin Belousov <kib@FreeBSD.org>2018-04-24 15:59:39 +0000
committerKonstantin Belousov <kib@FreeBSD.org>2018-04-24 15:59:39 +0000
commit989b861f5c2c59add8a840f45468ee86fb8b9748 (patch)
treedf831e23d85561a4b353ef45a917b5cfc232a945 /lib/libc/secure
parente07db02261c157e4fa9f7d633ae13a3313696924 (diff)
Notes
Diffstat (limited to 'lib/libc/secure')
-rw-r--r--lib/libc/secure/stack_protector.c20
1 files changed, 16 insertions, 4 deletions
diff --git a/lib/libc/secure/stack_protector.c b/lib/libc/secure/stack_protector.c
index d55813e00f1d..34bb7d0332a3 100644
--- a/lib/libc/secure/stack_protector.c
+++ b/lib/libc/secure/stack_protector.c
@@ -54,15 +54,27 @@ static void
__guard_setup(void)
{
static const int mib[2] = { CTL_KERN, KERN_ARND };
+ volatile long tmp_stack_chk_guard[nitems(__stack_chk_guard)];
size_t len;
- int error;
+ int error, idx;
if (__stack_chk_guard[0] != 0)
return;
- error = _elf_aux_info(AT_CANARY, __stack_chk_guard,
- sizeof(__stack_chk_guard));
- if (error == 0 && __stack_chk_guard[0] != 0)
+ /*
+ * Avoid using functions which might have stack protection
+ * enabled, to update the __stack_chk_guard. First fetch the
+ * data into a temporal array, then do manual volatile copy to
+ * not allow optimizer to call memcpy() behind us.
+ */
+ error = _elf_aux_info(AT_CANARY, (void *)tmp_stack_chk_guard,
+ sizeof(tmp_stack_chk_guard));
+ if (error == 0 && tmp_stack_chk_guard[0] != 0) {
+ for (idx = 0; idx < nitems(__stack_chk_guard); idx++) {
+ __stack_chk_guard[idx] = tmp_stack_chk_guard[idx];
+ tmp_stack_chk_guard[idx] = 0;
+ }
return;
+ }
len = sizeof(__stack_chk_guard);
if (__sysctl(mib, nitems(mib), __stack_chk_guard, &len, NULL, 0) ==