
11b3a0c2c5db
115412c37195
#explicit zero 1131456ac632
#brk 1158ebc243ea
#random 115323b39f7e
#undefined 116a91d9dddb
#merge 11a042b5a0ba
#vla formatting 119ec639de1b
#pthread 1149571a0a49
#read only 11149cc5ccb8
#zero 112e613ccbe7
#fork mmap 11e239c7dff8
#memprot pthread 110b03d92b7f
#xor 11de08419b82
#junk 11897d4903e2
#guard 11648cd68ca3
#ptrhread guard 110bc4dbcbd2
#stack rand 10aa9cc05d07
10a8cdbb6352
#explicit zero 10b28302c668
#brk 109f8be7d07c
#random 10cb91a7ee3a
#undefined 1008279e2fdd
#merge 106a18bd565d
#vla formatting 102f392c2d08
#pthread 108bbce1bc50
#read only 10725f61db82
#zero 104cd257135f
#fork mmap 109220cf622b
#memprot pthread 108ef71d1ffd
#memprot exit 100eaef1abbd
#xor 1064f1cc2148
#junk 105c42a527cf
#guard 105cc8c34e60
#pthread guard 107f61cc8a1c
#stack rand 9abdf523d26
9e4b9b31e6f
#explicit zero 9a3a22a63d2
#brk 97444dbc3cf
#random 9dcd3b72ac9
#undefined 9543e1df342
#merge 9611e5691f7
#vla formatting 98de97ce864
#pthread 9a475717042
#read only 97f0947cc0e
#zero 9e9751d3370
#fork mmap 983cd86d0d5
#memprot pthread 91ebb165455
#memprot exit 9488ba483cf
#xor 9f9351d884b
#junk 985e5bca0a5
#move Signed-off-by: Tad <tad@spotco.us>
76 lines
3.6 KiB
Diff
76 lines
3.6 KiB
Diff
From 5c42a527cf958ca3c81613178618d452e806994f Mon Sep 17 00:00:00 2001
|
|
From: Renlord <me@renlord.com>
|
|
Date: Thu, 12 Sep 2019 14:51:51 +1000
|
|
Subject: [PATCH] add guard page(s) between static_tls and stack
|
|
|
|
---
|
|
libc/bionic/pthread_create.cpp | 29 +++++++++++++++++++----------
|
|
1 file changed, 19 insertions(+), 10 deletions(-)
|
|
|
|
diff --git a/libc/bionic/pthread_create.cpp b/libc/bionic/pthread_create.cpp
|
|
index 4cf14ad72b..6bbf970a0b 100644
|
|
--- a/libc/bionic/pthread_create.cpp
|
|
+++ b/libc/bionic/pthread_create.cpp
|
|
@@ -218,9 +218,10 @@ int __init_thread(pthread_internal_t* thread) {
|
|
ThreadMapping __allocate_thread_mapping(size_t stack_size, size_t stack_guard_size) {
|
|
const StaticTlsLayout& layout = __libc_shared_globals()->static_tls_layout;
|
|
|
|
- // Allocate in order: stack guard, stack, static TLS, guard page.
|
|
+ // Allocate in order: stack guard, stack, guard page, static TLS, guard page.
|
|
size_t mmap_size;
|
|
if (__builtin_add_overflow(stack_size, stack_guard_size, &mmap_size)) return {};
|
|
+ if (__builtin_add_overflow(mmap_size, PTHREAD_GUARD_SIZE, &mmap_size)) return {};
|
|
if (__builtin_add_overflow(mmap_size, layout.size(), &mmap_size)) return {};
|
|
if (__builtin_add_overflow(mmap_size, PTHREAD_GUARD_SIZE, &mmap_size)) return {};
|
|
|
|
@@ -229,8 +230,8 @@ ThreadMapping __allocate_thread_mapping(size_t stack_size, size_t stack_guard_si
|
|
mmap_size = __BIONIC_ALIGN(mmap_size, PAGE_SIZE);
|
|
if (mmap_size < unaligned_size) return {};
|
|
|
|
- // Create a new private anonymous map. Make the entire mapping PROT_NONE, then carve out a
|
|
- // read+write area in the middle.
|
|
+ // Create a new private anonymous map. Make the entire mapping PROT_NONE, then carve out
|
|
+ // read+write areas for the stack and static TLS
|
|
const int flags = MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE;
|
|
char* const space = static_cast<char*>(mmap(nullptr, mmap_size, PROT_NONE, flags, -1, 0));
|
|
if (space == MAP_FAILED) {
|
|
@@ -240,13 +241,21 @@ ThreadMapping __allocate_thread_mapping(size_t stack_size, size_t stack_guard_si
|
|
mmap_size, strerror(errno));
|
|
return {};
|
|
}
|
|
- const size_t writable_size = mmap_size - stack_guard_size - PTHREAD_GUARD_SIZE;
|
|
- if (mprotect(space + stack_guard_size,
|
|
- writable_size,
|
|
- PROT_READ | PROT_WRITE) != 0) {
|
|
+
|
|
+ if (mprotect(space + stack_guard_size, stack_size, PROT_READ | PROT_WRITE) != 0) {
|
|
async_safe_format_log(ANDROID_LOG_WARN, "libc",
|
|
"pthread_create failed: couldn't mprotect R+W %zu-byte thread mapping region: %s",
|
|
- writable_size, strerror(errno));
|
|
+ stack_size, strerror(errno));
|
|
+ munmap(space, mmap_size);
|
|
+ return {};
|
|
+ }
|
|
+
|
|
+ char* const static_tls_space = space + stack_guard_size + stack_size + PTHREAD_GUARD_SIZE;
|
|
+
|
|
+ if (mprotect(static_tls_space, layout.size(), PROT_READ | PROT_WRITE) != 0) {
|
|
+ async_safe_format_log(ANDROID_LOG_WARN, "libc",
|
|
+ "pthread_create failed: couldn't mprotect R+W %zu-byte static TLS mapping region: %s",
|
|
+ layout.size(), strerror(errno));
|
|
munmap(space, mmap_size);
|
|
return {};
|
|
}
|
|
@@ -254,9 +263,9 @@ ThreadMapping __allocate_thread_mapping(size_t stack_size, size_t stack_guard_si
|
|
ThreadMapping result = {};
|
|
result.mmap_base = space;
|
|
result.mmap_size = mmap_size;
|
|
- result.static_tls = space + mmap_size - PTHREAD_GUARD_SIZE - layout.size();
|
|
+ result.static_tls = static_tls_space;
|
|
result.stack_base = space;
|
|
- result.stack_top = result.static_tls;
|
|
+ result.stack_top = space + stack_guard_size + stack_size;
|
|
return result;
|
|
}
|
|
|