Skip to content

Commit 2d58774

Browse files
genkeys, yggdrasilctl: Use pledge(2) on OpenBSD (#1193)
Restrict system operations of CLI tools with https://man.openbsd.org/pledge.2. https://pkg.go.dev/suah.dev/protect abstracts the OS specific code, i.e. is a NOOP on non-OpenBSD systems. This PR is to gauge upstream interest in this direction; my OpenBSD port of yggdrasil already pledges the daemon, resulting in minimal runtime privileges, but there are still a few rough edges: https://github.com/jasperla/openbsd-wip/blob/master/net/yggdrasil/patches/patch-cmd_yggdrasil_main_go#L80 --------- Co-authored-by: Neil <[email protected]>
1 parent b2b0396 commit 2d58774

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

cmd/genkeys/main.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import (
1818
"runtime"
1919
"time"
2020

21+
"suah.dev/protect"
22+
2123
"github.com/yggdrasil-network/yggdrasil-go/src/address"
2224
)
2325

@@ -27,6 +29,10 @@ type keySet struct {
2729
}
2830

2931
func main() {
32+
if err := protect.Pledge("stdio"); err != nil {
33+
panic(err)
34+
}
35+
3036
threads := runtime.GOMAXPROCS(0)
3137
fmt.Println("Threads:", threads)
3238
start := time.Now()

cmd/yggdrasilctl/main.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313
"strings"
1414
"time"
1515

16+
"suah.dev/protect"
17+
1618
"github.com/olekukonko/tablewriter"
1719
"github.com/yggdrasil-network/yggdrasil-go/src/admin"
1820
"github.com/yggdrasil-network/yggdrasil-go/src/core"
@@ -22,6 +24,11 @@ import (
2224
)
2325

2426
func main() {
27+
// read config, speak DNS/TCP and/or over a UNIX socket
28+
if err := protect.Pledge("stdio rpath inet unix dns"); err != nil {
29+
panic(err)
30+
}
31+
2532
// makes sure we can use defer and still return an error code to the OS
2633
os.Exit(run())
2734
}
@@ -78,6 +85,11 @@ func run() int {
7885
panic(err)
7986
}
8087

88+
// config and socket are done, work without unprivileges
89+
if err := protect.Pledge("stdio"); err != nil {
90+
panic(err)
91+
}
92+
8193
logger.Println("Connected")
8294
defer conn.Close()
8395

0 commit comments

Comments
 (0)