all repos — honk @ 852b5c2ad40ad85cc13d52468598e02c8c38677d

my fork of honk

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	"unsafe"
29)
30
31func Unveil(path string, perms string) {
32	cpath := C.CString(path)
33	defer C.free(unsafe.Pointer(cpath))
34	cperms := C.CString(perms)
35	defer C.free(unsafe.Pointer(cperms))
36
37	rv, err := C.unveil(cpath, cperms)
38	if rv != 0 {
39		elog.Fatalf("unveil(%s, %s) failure (%d)", path, perms, err)
40	}
41}
42
43func Pledge(promises string) {
44	cpromises := C.CString(promises)
45	defer C.free(unsafe.Pointer(cpromises))
46
47	rv, err := C.pledge(cpromises, nil)
48	if rv != 0 {
49		elog.Fatalf("pledge(%s) failure (%d)", promises, err)
50	}
51}
52
53func init() {
54	preservehooks = append(preservehooks, func() {
55		Unveil("/etc/ssl", "r")
56		if viewDir != dataDir {
57			Unveil(viewDir, "r")
58		}
59		Unveil(dataDir, "rwc")
60		C.unveil(nil, nil)
61		Pledge("stdio rpath wpath cpath flock dns inet unix")
62	})
63	backendhooks = append(backendhooks, func() {
64		C.unveil(nil, nil)
65		Pledge("stdio unix")
66	})
67}