Skip to content

Commit 748af6e

Browse files
tklausergopherbot
authored andcommitted
unix: pass PROT_MPROTECT(PROT_READ|PROT_WRITE) to initial Mmap on netbsd
On NetBSD PAX mprotect prohibits setting protection bits missing from the original mmap call unless explicitly requested with PROT_MPROTECT. Fixes golang/go#58660 Change-Id: I1e97e920bc617ed1674855adaae5047638a30394 Reviewed-on: https://go-review.googlesource.com/c/sys/+/470775 Run-TryBot: Tobias Klauser <[email protected]> Auto-Submit: Tobias Klauser <[email protected]> Reviewed-by: Than McIntosh <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Bryan Mills <[email protected]>
1 parent 972870e commit 748af6e

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

unix/mmap_unix_test.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,21 @@ import (
1515
)
1616

1717
func TestMmap(t *testing.T) {
18-
b, err := unix.Mmap(-1, 0, unix.Getpagesize(), unix.PROT_NONE, unix.MAP_ANON|unix.MAP_PRIVATE)
18+
mmapProt := unix.PROT_NONE
19+
mprotectProt := unix.PROT_READ | unix.PROT_WRITE
20+
// On NetBSD PAX mprotect prohibits setting protection bits
21+
// missing from the original mmap call unless explicitly
22+
// requested with PROT_MPROTECT.
23+
if runtime.GOOS == "netbsd" {
24+
// PROT_MPROTECT(x) is defined as ((x) << 3):
25+
// https://github.com/NetBSD/src/blob/aba449a55bf91b44bc68f542edd9afa341962b89/sys/sys/mman.h#L73
26+
mmapProt = mprotectProt << 3
27+
}
28+
b, err := unix.Mmap(-1, 0, unix.Getpagesize(), mmapProt, unix.MAP_ANON|unix.MAP_PRIVATE)
1929
if err != nil {
2030
t.Fatalf("Mmap: %v", err)
2131
}
22-
if err := unix.Mprotect(b, unix.PROT_READ|unix.PROT_WRITE); err != nil {
32+
if err := unix.Mprotect(b, mprotectProt); err != nil {
2333
t.Fatalf("Mprotect: %v", err)
2434
}
2535

0 commit comments

Comments
 (0)