-
Notifications
You must be signed in to change notification settings - Fork 3
/
gosh_store.go
134 lines (115 loc) · 3.03 KB
/
gosh_store.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package main
import (
"log/slog"
"os"
"os/signal"
"golang.org/x/sys/unix"
)
// ensureStoreDir makes sure that a store directory exists and it holds the
// correct permissions.
func ensureStoreDir(path, username, groupname string) error {
_, stat := os.Stat(path)
if os.IsNotExist(stat) {
err := os.Mkdir(path, 0700)
if err != nil {
return err
}
}
err := os.Chmod(path, 0700)
if err != nil {
return err
}
uid, gid, err := uidGidForUserGroup(username, groupname)
if err != nil {
return err
}
err = os.Chown(path, uid, gid)
if err != nil {
return err
}
return nil
}
func mainStore(conf Config) {
slog.Debug("Starting store child", slog.Any("config", conf.Store))
var idGenerator func() (string, error)
switch conf.Store.IdGenerator.Type {
case "random":
idGenerator = randomIdGenerator(conf.Store.IdGenerator.Length)
case "wordlist":
var err error
idGenerator, err = wordlistIdGenerator(conf.Store.IdGenerator.File, conf.Store.IdGenerator.Length)
if err != nil {
slog.Error("Failed to create wordlist ID generator", slog.Any("error", err))
os.Exit(1)
}
default:
slog.Error("Failed to configure an ID generator as the type is unknown",
slog.String("type", conf.Store.IdGenerator.Type))
os.Exit(1)
}
err := ensureStoreDir(conf.Store.Path, conf.User, conf.Group)
if err != nil {
slog.Error("Failed to prepare store directory", slog.Any("error", err))
os.Exit(1)
}
err = posixPermDrop(conf.Store.Path, conf.User, conf.Group)
if err != nil {
slog.Error("Failed to drop permissions", slog.Any("error", err))
os.Exit(1)
}
err = restrict(restrict_linux_seccomp,
[]string{
"@system-service",
"~@chown",
"~@clock",
"~@cpu-emulation",
"~@debug",
"~@keyring",
"~@memlock",
"~@module",
"~@mount",
"~@privileged",
"~@reboot",
"~@sandbox",
"~@setuid",
"~@swap",
/* @process */ "~execve", "~execveat", "~fork", "~kill",
/* @network-io */ "~bind", "~connect", "~listen",
"fstatat", // for aarch64, same as newfstatat
})
if err != nil {
slog.Error("Failed to apply seccomp-bpf filter", slog.Any("error", err))
os.Exit(1)
}
err = restrict(restrict_openbsd_pledge,
"stdio rpath wpath cpath flock unix sendfd recvfd error",
"")
if err != nil {
slog.Error("Failed to pledge", slog.Any("error", err))
os.Exit(1)
}
store, err := NewStore("/", idGenerator, true)
if err != nil {
slog.Error("Failed to create store", slog.Any("error", err))
os.Exit(1)
}
rpcConn, err := unixConnFromFile(os.NewFile(3, ""))
if err != nil {
slog.Error("Failed to create Unix Domain Socket from FD", slog.Any("error", err))
os.Exit(1)
}
fdConn, err := unixConnFromFile(os.NewFile(4, ""))
if err != nil {
slog.Error("Failed to create Unix Domain Socket from FD", slog.Any("error", err))
os.Exit(1)
}
rpcStore := NewStoreRpcServer(store, rpcConn, fdConn)
sigint := make(chan os.Signal, 1)
signal.Notify(sigint, unix.SIGINT)
<-sigint
err = rpcStore.Close()
if err != nil {
slog.Error("Failed to close RPC Store", slog.Any("error", err))
os.Exit(1)
}
}