unveil.go (view raw)
1//go:build openbsd
2// +build openbsd
3
4//
5// Copyright (c) 2019 Ted Unangst <tedu@tedunangst.com>
6//
7// Permission to use, copy, modify, and distribute this software for any
8// purpose with or without fee is hereby granted, provided that the above
9// copyright notice and this permission notice appear in all copies.
10//
11// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18
19package main
20
21/*
22#include <stdlib.h>
23#include <unistd.h>
24*/
25import "C"
26
27import (
28 "fmt"
29 "unsafe"
30)
31
32func Unveil(path string, perms string) {
33 cpath := C.CString(path)
34 defer C.free(unsafe.Pointer(cpath))
35 cperms := C.CString(perms)
36 defer C.free(unsafe.Pointer(cperms))
37
38 rv, err := C.unveil(cpath, cperms)
39 if rv != 0 {
40 elog.Fatalf("unveil(%s, %s) failure (%d)", path, perms, err)
41 }
42}
43
44func Pledge(promises string) {
45 cpromises := C.CString(promises)
46 defer C.free(unsafe.Pointer(cpromises))
47
48 rv, err := C.pledge(cpromises, nil)
49 if rv != 0 {
50 elog.Fatalf("pledge(%s) failure (%d)", promises, err)
51 }
52}
53
54func init() {
55 preservehooks = append(preservehooks, func() {
56 Unveil("/etc/ssl", "r")
57 if viewDir != dataDir {
58 Unveil(viewDir, "r")
59 }
60 Unveil(dataDir, "rwc")
61 C.unveil(nil, nil)
62 Pledge("stdio rpath wpath cpath flock dns inet unix")
63 })
64 backendhooks = append(backendhooks, func() {
65 C.unveil(nil, nil)
66 Pledge("stdio unix")
67 })
68}