Skip to content

Commit 041d97f

Browse files
committed
unix: Set a guard page at the end of signal stacks
This mitigates possible issues when signal stacks overflow, which could manifest as segfaults or in unlucky circumstances possible clobbering of other memory values as stack overflows tend to enable.
1 parent 23de827 commit 041d97f

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

src/libstd/sys/unix/stack_overflow.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ mod imp {
4545
use libc::{mmap, munmap};
4646
use libc::{sigaction, sighandler_t, SA_ONSTACK, SA_SIGINFO, SIGBUS, SIG_DFL};
4747
use libc::{sigaltstack, SIGSTKSZ, SS_DISABLE};
48-
use libc::{MAP_ANON, MAP_PRIVATE, PROT_READ, PROT_WRITE, SIGSEGV};
48+
use libc::{MAP_ANON, MAP_PRIVATE, PROT_NONE, PROT_READ, PROT_WRITE, SIGSEGV};
4949

5050
use crate::sys_common::thread_info;
5151

@@ -137,11 +137,16 @@ mod imp {
137137
}
138138

139139
unsafe fn get_stackp() -> *mut libc::c_void {
140+
let page_size = crate::sys::unix::os::page_size();
140141
let stackp =
141-
mmap(ptr::null_mut(), SIGSTKSZ, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
142+
mmap(ptr::null_mut(), SIGSTKSZ + page_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
142143
if stackp == MAP_FAILED {
143144
panic!("failed to allocate an alternative stack");
144145
}
146+
let guard_result = libc::mprotect(stackp, page_size, PROT_NONE);
147+
if guard_result != 0 {
148+
panic!("failed to set up alternative stack guard page");
149+
}
145150
stackp
146151
}
147152

0 commit comments

Comments
 (0)