2022-01-22 22:30:02 +01:00
|
|
|
//go:build openbsd
|
2019-11-12 22:27:12 +01:00
|
|
|
// +build openbsd
|
|
|
|
|
|
|
|
//
|
|
|
|
// Copyright (c) 2019 Ted Unangst <tedu@tedunangst.com>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice appear in all copies.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
/*
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
*/
|
|
|
|
import "C"
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
2023-07-10 06:15:35 +02:00
|
|
|
func Unveil(path string, perms string) {
|
2019-11-12 22:27:12 +01:00
|
|
|
cpath := C.CString(path)
|
|
|
|
defer C.free(unsafe.Pointer(cpath))
|
|
|
|
cperms := C.CString(perms)
|
|
|
|
defer C.free(unsafe.Pointer(cperms))
|
|
|
|
|
|
|
|
rv, err := C.unveil(cpath, cperms)
|
|
|
|
if rv != 0 {
|
2023-07-10 06:16:17 +02:00
|
|
|
elog.Fatalf("unveil(%s, %s) failure (%d)", path, perms, err)
|
2019-11-12 22:27:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-10 06:15:35 +02:00
|
|
|
func Pledge(promises string) {
|
2019-11-12 22:27:12 +01:00
|
|
|
cpromises := C.CString(promises)
|
|
|
|
defer C.free(unsafe.Pointer(cpromises))
|
|
|
|
|
|
|
|
rv, err := C.pledge(cpromises, nil)
|
|
|
|
if rv != 0 {
|
2023-07-10 06:15:35 +02:00
|
|
|
elog.Fatalf("pledge(%s) failure (%d)", promises, err)
|
2019-11-12 22:27:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
preservehooks = append(preservehooks, func() {
|
|
|
|
Unveil("/etc/ssl", "r")
|
|
|
|
if viewDir != dataDir {
|
|
|
|
Unveil(viewDir, "r")
|
|
|
|
}
|
|
|
|
Unveil(dataDir, "rwc")
|
|
|
|
C.unveil(nil, nil)
|
|
|
|
Pledge("stdio rpath wpath cpath flock dns inet unix")
|
|
|
|
})
|
2019-11-27 21:58:41 +01:00
|
|
|
backendhooks = append(backendhooks, func() {
|
2022-02-06 07:02:50 +01:00
|
|
|
C.unveil(nil, nil)
|
2019-11-27 21:58:41 +01:00
|
|
|
Pledge("stdio unix")
|
|
|
|
})
|
2019-11-12 22:27:12 +01:00
|
|
|
}
|