Skip to content

Commit 244e24a

Browse files
Add comments and unify guard page setup.
While currently only NetBSD seems to be affected, all systems implementing PAX MPROTECT in strict mode need this treatment, and it does not hurt others.
1 parent 7c2304d commit 244e24a

File tree

1 file changed

+13
-16
lines changed

1 file changed

+13
-16
lines changed

src/libstd/sys/unix/thread.rs

+13-16
Original file line numberDiff line numberDiff line change
@@ -326,23 +326,20 @@ pub mod guard {
326326
// Reallocate the last page of the stack.
327327
// This ensures SIGBUS will be raised on
328328
// stack overflow.
329-
if cfg!(target_os = "netbsd") {
330-
let result = mmap(stackaddr, PAGE_SIZE, PROT_READ | PROT_WRITE,
331-
MAP_PRIVATE | MAP_ANON | MAP_FIXED, -1, 0);
332-
if result != stackaddr || result == MAP_FAILED {
333-
panic!("failed to allocate a guard page");
334-
}
335-
let result = mprotect(stackaddr, PAGE_SIZE, 0);
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,
335+
MAP_PRIVATE | MAP_ANON | MAP_FIXED, -1, 0);
336+
if result != stackaddr || result == MAP_FAILED {
337+
panic!("failed to allocate a guard page");
338+
}
336339

337-
if result != 0 {
338-
panic!("unable to protect the guard page");
339-
}
340-
} else {
341-
let result = mmap(stackaddr, PAGE_SIZE, PROT_NONE,
342-
MAP_PRIVATE | MAP_ANON | MAP_FIXED, -1, 0);
343-
if result != stackaddr || result == MAP_FAILED {
344-
panic!("failed to allocate a guard page");
345-
}
340+
let result = mprotect(stackaddr, PAGE_SIZE, PROT_NONE);
341+
if result != 0 {
342+
panic!("failed to protect the guard page");
346343
}
347344

348345
let guardaddr = stackaddr as usize;

0 commit comments

Comments
 (0)