diff --git a/unix/pledge_openbsd.go b/unix/pledge_openbsd.go index eb48294b2..77210d4f4 100644 --- a/unix/pledge_openbsd.go +++ b/unix/pledge_openbsd.go @@ -8,8 +8,6 @@ import ( "errors" "fmt" "strconv" - "syscall" - "unsafe" ) // Pledge implements the pledge syscall. @@ -20,6 +18,10 @@ import ( // execpromises must be empty when Pledge is called on OpenBSD // releases predating 6.3, otherwise an error will be returned. // +// On OpenBSD 6.3 and later, an empty execpromises removes all +// execpromises from the process. Use PledgePromises to only set +// promises without setting execpromises. +// // For more information see pledge(2). func Pledge(promises, execpromises string) error { maj, min, err := majmin() @@ -27,35 +29,35 @@ func Pledge(promises, execpromises string) error { return err } - err = pledgeAvailable(maj, min, execpromises) + // OS support for execpromises is required only when execpromises is not + // the empty string. + err = supportsExecpromises(maj, min, execpromises != "") if err != nil { return err } - pptr, err := syscall.BytePtrFromString(promises) + _promises, err := BytePtrFromString(promises) if err != nil { return err } - // This variable will hold either a nil unsafe.Pointer or - // an unsafe.Pointer to a string (execpromises). - var expr unsafe.Pointer + // This variable will hold either a nil pointer or a pointer to the + // NUL-terminated execpromises string. + var _execpromises *byte - // If we're running on OpenBSD > 6.2, pass execpromises to the syscall. - if maj > 6 || (maj == 6 && min > 2) { - exptr, err := syscall.BytePtrFromString(execpromises) + // If we're running on OpenBSD >= 6.3, pass execpromises to the syscall. + // While an empty execpromises string is required by this API on + // OpenBSD <= 6.2 and has no effect, the empty execpromises string + // removes all execpromises on OpenBSD >= 6.3. + if maj > 6 || (maj == 6 && min >= 3) { + exptr, err := BytePtrFromString(execpromises) if err != nil { return err } - expr = unsafe.Pointer(exptr) - } - - _, _, e := syscall.Syscall(SYS_PLEDGE, uintptr(unsafe.Pointer(pptr)), uintptr(expr), 0) - if e != 0 { - return e + _execpromises = exptr } - return nil + return pledge(_promises, _execpromises) } // PledgePromises implements the pledge syscall. @@ -64,36 +66,21 @@ func Pledge(promises, execpromises string) error { // // For more information see pledge(2). func PledgePromises(promises string) error { - maj, min, err := majmin() - if err != nil { - return err - } - - err = pledgeAvailable(maj, min, "") - if err != nil { - return err - } - - // This variable holds the execpromises and is always nil. - var expr unsafe.Pointer - - pptr, err := syscall.BytePtrFromString(promises) + _promises, err := BytePtrFromString(promises) if err != nil { return err } - _, _, e := syscall.Syscall(SYS_PLEDGE, uintptr(unsafe.Pointer(pptr)), uintptr(expr), 0) - if e != 0 { - return e - } - - return nil + return pledge(_promises, nil) } // PledgeExecpromises implements the pledge syscall. // // This changes the execpromises and leaves the promises untouched. // +// The pledge syscall does not accept execpromises on OpenBSD releases +// before 6.3. +// // For more information see pledge(2). func PledgeExecpromises(execpromises string) error { maj, min, err := majmin() @@ -101,25 +88,17 @@ func PledgeExecpromises(execpromises string) error { return err } - err = pledgeAvailable(maj, min, execpromises) + err = supportsExecpromises(maj, min, true) if err != nil { return err } - // This variable holds the promises and is always nil. - var pptr unsafe.Pointer - - exptr, err := syscall.BytePtrFromString(execpromises) + _execpromises, err := BytePtrFromString(execpromises) if err != nil { return err } - _, _, e := syscall.Syscall(SYS_PLEDGE, uintptr(pptr), uintptr(unsafe.Pointer(exptr)), 0) - if e != 0 { - return e - } - - return nil + return pledge(nil, _execpromises) } // majmin returns major and minor version number for an OpenBSD system. @@ -145,17 +124,12 @@ func majmin() (major int, minor int, err error) { return } -// pledgeAvailable checks for availability of the pledge(2) syscall -// based on the running OpenBSD version. -func pledgeAvailable(maj, min int, execpromises string) error { - // If OpenBSD <= 5.9, pledge is not available. - if (maj == 5 && min != 9) || maj < 5 { - return fmt.Errorf("pledge syscall is not available on OpenBSD %d.%d", maj, min) - } - +// supportsExecpromises checks for availability of the execpromises argument to +// the pledge(2) syscall based on the running OpenBSD version. +func supportsExecpromises(maj, min int, required bool) error { // If OpenBSD <= 6.2 and execpromises is not empty, // return an error - execpromises is not available before 6.3 - if (maj < 6 || (maj == 6 && min <= 2)) && execpromises != "" { + if (maj < 6 || (maj == 6 && min <= 2)) && required { return fmt.Errorf("cannot use execpromises on OpenBSD %d.%d", maj, min) } diff --git a/unix/syscall_openbsd.go b/unix/syscall_openbsd.go index 9b67b908e..781a09240 100644 --- a/unix/syscall_openbsd.go +++ b/unix/syscall_openbsd.go @@ -314,6 +314,8 @@ func Uname(uname *Utsname) error { //sys readlen(fd int, buf *byte, nbuf int) (n int, err error) = SYS_READ //sys writelen(fd int, buf *byte, nbuf int) (n int, err error) = SYS_WRITE //sys utimensat(dirfd int, path string, times *[2]Timespec, flags int) (err error) +//sys pledge(promises *byte, execpromises *byte) (err error) +//sys unveil(path *byte, flags *byte) (err error) /* * Unimplemented diff --git a/unix/unveil_openbsd.go b/unix/unveil_openbsd.go index 168d5ae77..e039823d6 100644 --- a/unix/unveil_openbsd.go +++ b/unix/unveil_openbsd.go @@ -4,39 +4,24 @@ package unix -import ( - "syscall" - "unsafe" -) - // Unveil implements the unveil syscall. // For more information see unveil(2). // Note that the special case of blocking further // unveil calls is handled by UnveilBlock. func Unveil(path string, flags string) error { - pathPtr, err := syscall.BytePtrFromString(path) + _path, err := BytePtrFromString(path) if err != nil { return err } - flagsPtr, err := syscall.BytePtrFromString(flags) + _flags, err := BytePtrFromString(flags) if err != nil { return err } - _, _, e := syscall.Syscall(SYS_UNVEIL, uintptr(unsafe.Pointer(pathPtr)), uintptr(unsafe.Pointer(flagsPtr)), 0) - if e != 0 { - return e - } - return nil + return unveil(_path, _flags) } // UnveilBlock blocks future unveil calls. // For more information see unveil(2). func UnveilBlock() error { - // Both pointers must be nil. - var pathUnsafe, flagsUnsafe unsafe.Pointer - _, _, e := syscall.Syscall(SYS_UNVEIL, uintptr(pathUnsafe), uintptr(flagsUnsafe), 0) - if e != 0 { - return e - } - return nil + return unveil(nil, nil) } diff --git a/unix/zsyscall_openbsd_386.go b/unix/zsyscall_openbsd_386.go index caeb807bd..0f37994e2 100644 --- a/unix/zsyscall_openbsd_386.go +++ b/unix/zsyscall_openbsd_386.go @@ -2233,3 +2233,31 @@ func utimensat(dirfd int, path string, times *[2]Timespec, flags int) (err error var libc_utimensat_trampoline_addr uintptr //go:cgo_import_dynamic libc_utimensat utimensat "libc.so" + +// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT + +func pledge(promises *byte, execpromises *byte) (err error) { + _, _, e1 := syscall_syscall(libc_pledge_trampoline_addr, uintptr(unsafe.Pointer(promises)), uintptr(unsafe.Pointer(execpromises)), 0) + if e1 != 0 { + err = errnoErr(e1) + } + return +} + +var libc_pledge_trampoline_addr uintptr + +//go:cgo_import_dynamic libc_pledge pledge "libc.so" + +// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT + +func unveil(path *byte, flags *byte) (err error) { + _, _, e1 := syscall_syscall(libc_unveil_trampoline_addr, uintptr(unsafe.Pointer(path)), uintptr(unsafe.Pointer(flags)), 0) + if e1 != 0 { + err = errnoErr(e1) + } + return +} + +var libc_unveil_trampoline_addr uintptr + +//go:cgo_import_dynamic libc_unveil unveil "libc.so" diff --git a/unix/zsyscall_openbsd_386.s b/unix/zsyscall_openbsd_386.s index 087444250..c0d07dd20 100644 --- a/unix/zsyscall_openbsd_386.s +++ b/unix/zsyscall_openbsd_386.s @@ -667,3 +667,13 @@ TEXT libc_utimensat_trampoline<>(SB),NOSPLIT,$0-0 JMP libc_utimensat(SB) GLOBL ·libc_utimensat_trampoline_addr(SB), RODATA, $4 DATA ·libc_utimensat_trampoline_addr(SB)/4, $libc_utimensat_trampoline<>(SB) + +TEXT libc_pledge_trampoline<>(SB),NOSPLIT,$0-0 + JMP libc_pledge(SB) +GLOBL ·libc_pledge_trampoline_addr(SB), RODATA, $4 +DATA ·libc_pledge_trampoline_addr(SB)/4, $libc_pledge_trampoline<>(SB) + +TEXT libc_unveil_trampoline<>(SB),NOSPLIT,$0-0 + JMP libc_unveil(SB) +GLOBL ·libc_unveil_trampoline_addr(SB), RODATA, $4 +DATA ·libc_unveil_trampoline_addr(SB)/4, $libc_unveil_trampoline<>(SB) diff --git a/unix/zsyscall_openbsd_amd64.go b/unix/zsyscall_openbsd_amd64.go index a05e5f4ff..e633adb46 100644 --- a/unix/zsyscall_openbsd_amd64.go +++ b/unix/zsyscall_openbsd_amd64.go @@ -2233,3 +2233,31 @@ func utimensat(dirfd int, path string, times *[2]Timespec, flags int) (err error var libc_utimensat_trampoline_addr uintptr //go:cgo_import_dynamic libc_utimensat utimensat "libc.so" + +// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT + +func pledge(promises *byte, execpromises *byte) (err error) { + _, _, e1 := syscall_syscall(libc_pledge_trampoline_addr, uintptr(unsafe.Pointer(promises)), uintptr(unsafe.Pointer(execpromises)), 0) + if e1 != 0 { + err = errnoErr(e1) + } + return +} + +var libc_pledge_trampoline_addr uintptr + +//go:cgo_import_dynamic libc_pledge pledge "libc.so" + +// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT + +func unveil(path *byte, flags *byte) (err error) { + _, _, e1 := syscall_syscall(libc_unveil_trampoline_addr, uintptr(unsafe.Pointer(path)), uintptr(unsafe.Pointer(flags)), 0) + if e1 != 0 { + err = errnoErr(e1) + } + return +} + +var libc_unveil_trampoline_addr uintptr + +//go:cgo_import_dynamic libc_unveil unveil "libc.so" diff --git a/unix/zsyscall_openbsd_amd64.s b/unix/zsyscall_openbsd_amd64.s index 5782cd108..a7bc6dc73 100644 --- a/unix/zsyscall_openbsd_amd64.s +++ b/unix/zsyscall_openbsd_amd64.s @@ -667,3 +667,13 @@ TEXT libc_utimensat_trampoline<>(SB),NOSPLIT,$0-0 JMP libc_utimensat(SB) GLOBL ·libc_utimensat_trampoline_addr(SB), RODATA, $8 DATA ·libc_utimensat_trampoline_addr(SB)/8, $libc_utimensat_trampoline<>(SB) + +TEXT libc_pledge_trampoline<>(SB),NOSPLIT,$0-0 + JMP libc_pledge(SB) +GLOBL ·libc_pledge_trampoline_addr(SB), RODATA, $8 +DATA ·libc_pledge_trampoline_addr(SB)/8, $libc_pledge_trampoline<>(SB) + +TEXT libc_unveil_trampoline<>(SB),NOSPLIT,$0-0 + JMP libc_unveil(SB) +GLOBL ·libc_unveil_trampoline_addr(SB), RODATA, $8 +DATA ·libc_unveil_trampoline_addr(SB)/8, $libc_unveil_trampoline<>(SB)