Skip to content

Commit acd3871

Browse files
committed
Auto merge of #50331 - MartinHusemann:master, r=Kimundi
Map the stack guard page with max protection on NetBSD 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. Fixes #50313
2 parents 9e3caa2 + 244e24a commit acd3871

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/libstd/sys/unix/thread.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -326,13 +326,22 @@ pub mod guard {
326326
// Reallocate the last page of the stack.
327327
// This ensures SIGBUS will be raised on
328328
// stack overflow.
329-
let result = mmap(stackaddr, PAGE_SIZE, PROT_NONE,
329+
// Systems which enforce strict PAX MPROTECT do not allow
330+
// to mprotect() a mapping with less restrictive permissions
331+
// than the initial mmap() used, so we mmap() here with
332+
// read/write permissions and only then mprotect() it to
333+
// no permissions at all. See issue #50313.
334+
let result = mmap(stackaddr, PAGE_SIZE, PROT_READ | PROT_WRITE,
330335
MAP_PRIVATE | MAP_ANON | MAP_FIXED, -1, 0);
331-
332336
if result != stackaddr || result == MAP_FAILED {
333337
panic!("failed to allocate a guard page");
334338
}
335339

340+
let result = mprotect(stackaddr, PAGE_SIZE, PROT_NONE);
341+
if result != 0 {
342+
panic!("failed to protect the guard page");
343+
}
344+
336345
let guardaddr = stackaddr as usize;
337346
let offset = if cfg!(target_os = "freebsd") {
338347
2

0 commit comments

Comments
 (0)