about summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorMartin Husemann <martin@NetBSD.org>2018-05-02 10:00:33 +0200
committerMartin Husemann <martin@NetBSD.org>2018-05-02 10:00:33 +0200
commit244e24a312925779f6c62ce7d6b315fa2c120a7c (patch)
treee676eac0067f91bdd3d808c7fdb22432a48636b2 /src/libstd
parent7c2304ddc6d716883415ac883f02256db6318644 (diff)
downloadrust-244e24a312925779f6c62ce7d6b315fa2c120a7c.tar.gz
rust-244e24a312925779f6c62ce7d6b315fa2c120a7c.zip
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.
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/sys/unix/thread.rs29
1 files changed, 13 insertions, 16 deletions
diff --git a/src/libstd/sys/unix/thread.rs b/src/libstd/sys/unix/thread.rs
index f32016ce1d6..7fdecc9c0c0 100644
--- a/src/libstd/sys/unix/thread.rs
+++ b/src/libstd/sys/unix/thread.rs
@@ -326,23 +326,20 @@ pub mod guard {
             // Reallocate the last page of the stack.
             // This ensures SIGBUS will be raised on
             // stack overflow.
-            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);
+            // Systems which enforce strict PAX MPROTECT do not allow
+            // to mprotect() a mapping with less restrictive permissions
+            // than the initial mmap() used, so we mmap() here with
+            // read/write permissions and only then mprotect() it to
+            // no permissions at all. See issue #50313.
+            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");
+            }
 
-                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 result = mprotect(stackaddr, PAGE_SIZE, PROT_NONE);
+            if result != 0 {
+                panic!("failed to protect the guard page");
             }
 
             let guardaddr = stackaddr as usize;