Skip to content

Commit

Permalink
Map the stack guard page with max protection on NetBSD
Browse files Browse the repository at this point in the history
On NetBSD the initial mmap() protection of a mapping can not be made
less restrictive with mprotect().

So when mapping a stack guard page, use the maximum protection
we ever want to use, then mprotect() it to the permission we
want it to have initially.
  • Loading branch information
MartinHusemann committed Apr 30, 2018
1 parent f900bcf commit 7c2304d
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/libstd/sys/unix/thread.rs
Expand Up @@ -326,11 +326,23 @@ pub mod guard {
// Reallocate the last page of the stack.
// This ensures SIGBUS will be raised on
// stack overflow.
let result = mmap(stackaddr, PAGE_SIZE, PROT_NONE,
MAP_PRIVATE | MAP_ANON | MAP_FIXED, -1, 0);
if cfg!(target_os = "netbsd") {
let result = mmap(stackaddr, PAGE_SIZE, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANON | MAP_FIXED, -1, 0);
if result != stackaddr || result == MAP_FAILED {
panic!("failed to allocate a guard page");
}
let result = mprotect(stackaddr, PAGE_SIZE, 0);

if result != stackaddr || result == MAP_FAILED {
panic!("failed to allocate a guard page");
if result != 0 {
panic!("unable to protect the guard page");
}
} else {
let result = mmap(stackaddr, PAGE_SIZE, PROT_NONE,
MAP_PRIVATE | MAP_ANON | MAP_FIXED, -1, 0);
if result != stackaddr || result == MAP_FAILED {
panic!("failed to allocate a guard page");
}
}

let guardaddr = stackaddr as usize;
Expand Down

0 comments on commit 7c2304d

Please sign in to comment.