Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unix: Set a guard page at the end of signal stacks #69969

Merged
merged 3 commits into from
Mar 19, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
return a pointer to the end of the valid part of the sigstack, no fur…
…ther

also unmap the whole thing when cleaning up, rather than leaving a spare
page floating around.
iximeow committed Mar 13, 2020
commit 0ca2ed364662351da9e8a1f4ba54e027e1f420df
12 changes: 7 additions & 5 deletions src/libstd/sys/unix/stack_overflow.rs
Original file line number Diff line number Diff line change
@@ -47,6 +47,7 @@ mod imp {
use libc::{sigaltstack, SIGSTKSZ, SS_DISABLE};
use libc::{MAP_ANON, MAP_PRIVATE, PROT_NONE, PROT_READ, PROT_WRITE, SIGSEGV};

use crate::sys::unix::os::page_size;
use crate::sys_common::thread_info;

#[cfg(any(target_os = "linux", target_os = "android"))]
@@ -137,17 +138,16 @@ mod imp {
}

unsafe fn get_stackp() -> *mut libc::c_void {
let page_size = crate::sys::unix::os::page_size();
let stackp =
mmap(ptr::null_mut(), SIGSTKSZ + page_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
mmap(ptr::null_mut(), SIGSTKSZ + page_size(), PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
if stackp == MAP_FAILED {
panic!("failed to allocate an alternative stack");
}
let guard_result = libc::mprotect(stackp, page_size, PROT_NONE);
let guard_result = libc::mprotect(stackp, page_size(), PROT_NONE);
if guard_result != 0 {
panic!("failed to set up alternative stack guard page");
}
stackp
stackp.add(page_size())
}

#[cfg(any(
@@ -195,7 +195,9 @@ mod imp {
ss_size: SIGSTKSZ,
};
sigaltstack(&stack, ptr::null_mut());
munmap(handler._data, SIGSTKSZ);
// We know from `get_stackp` that the alternate stack we installed is part of a mapping
// that started one page earlier, so walk back a page and unmap from there.
munmap(handler._data.sub(page_size()), SIGSTKSZ + page_size());
}
}
}