aboutsummaryrefslogtreecommitdiffhomepage
path: root/cmd/hsu/main.go
diff options
context:
space:
mode:
authorOphestra <cat@gensokyo.uk>2025-06-25 03:59:52 +0900
committerOphestra <cat@gensokyo.uk>2025-06-25 04:57:41 +0900
commit87e008d56de974947ebb99c2cc40b25d3c2cf43e (patch)
tree31791911e5226d6ec04e3fac7d91b0bf53e63aa5 /cmd/hsu/main.go
parent399207321265307bb15f37d867f9370cd51c82a8 (diff)
treewide: rename to hakurei
Fortify makes little sense for a container tool. Signed-off-by: Ophestra <cat@gensokyo.uk>
Diffstat (limited to 'cmd/hsu/main.go')
-rw-r--r--cmd/hsu/main.go146
1 files changed, 146 insertions, 0 deletions
diff --git a/cmd/hsu/main.go b/cmd/hsu/main.go
new file mode 100644
index 00000000..cefcad48
--- /dev/null
+++ b/cmd/hsu/main.go
@@ -0,0 +1,146 @@
+package main
+
+import (
+ "bytes"
+ "fmt"
+ "log"
+ "os"
+ "path"
+ "slices"
+ "strconv"
+ "strings"
+ "syscall"
+)
+
+const (
+ hsuConfFile = "/etc/hsurc"
+ envShim = "HAKUREI_SHIM"
+ envAID = "HAKUREI_APP_ID"
+ envGroups = "HAKUREI_GROUPS"
+
+ PR_SET_NO_NEW_PRIVS = 0x26
+)
+
+func main() {
+ log.SetFlags(0)
+ log.SetPrefix("hsu: ")
+ log.SetOutput(os.Stderr)
+
+ if os.Geteuid() != 0 {
+ log.Fatal("this program must be owned by uid 0 and have the setuid bit set")
+ }
+
+ puid := os.Getuid()
+ if puid == 0 {
+ log.Fatal("this program must not be started by root")
+ }
+
+ var toolPath string
+ pexe := path.Join("/proc", strconv.Itoa(os.Getppid()), "exe")
+ if p, err := os.Readlink(pexe); err != nil {
+ log.Fatalf("cannot read parent executable path: %v", err)
+ } else if strings.HasSuffix(p, " (deleted)") {
+ log.Fatal("hakurei executable has been deleted")
+ } else if p != mustCheckPath(hmain) && p != mustCheckPath(fpkg) {
+ log.Fatal("this program must be started by hakurei")
+ } else {
+ toolPath = p
+ }
+
+ // uid = 1000000 +
+ // fid * 10000 +
+ // aid
+ uid := 1000000
+
+ // refuse to run if hsurc is not protected correctly
+ if s, err := os.Stat(hsuConfFile); err != nil {
+ log.Fatal(err)
+ } else if s.Mode().Perm() != 0400 {
+ log.Fatal("bad hsurc perm")
+ } else if st := s.Sys().(*syscall.Stat_t); st.Uid != 0 || st.Gid != 0 {
+ log.Fatal("hsurc must be owned by uid 0")
+ }
+
+ // authenticate before accepting user input
+ if f, err := os.Open(hsuConfFile); err != nil {
+ log.Fatal(err)
+ } else if fid, ok := mustParseConfig(f, puid); !ok {
+ log.Fatalf("uid %d is not in the hsurc file", puid)
+ } else {
+ uid += fid * 10000
+ }
+
+ // allowed aid range 0 to 9999
+ if as, ok := os.LookupEnv(envAID); !ok {
+ log.Fatal("HAKUREI_APP_ID not set")
+ } else if aid, err := parseUint32Fast(as); err != nil || aid < 0 || aid > 9999 {
+ log.Fatal("invalid aid")
+ } else {
+ uid += aid
+ }
+
+ // pass through setup fd to shim
+ var shimSetupFd string
+ if s, ok := os.LookupEnv(envShim); !ok {
+ // hakurei requests target uid
+ // print resolved uid and exit
+ fmt.Print(uid)
+ os.Exit(0)
+ } else if len(s) != 1 || s[0] > '9' || s[0] < '3' {
+ log.Fatal("HAKUREI_SHIM holds an invalid value")
+ } else {
+ shimSetupFd = s
+ }
+
+ // supplementary groups
+ var suppGroups, suppCurrent []int
+
+ if gs, ok := os.LookupEnv(envGroups); ok {
+ if cur, err := os.Getgroups(); err != nil {
+ log.Fatalf("cannot get groups: %v", err)
+ } else {
+ suppCurrent = cur
+ }
+
+ // parse space-separated list of group ids
+ gss := bytes.Split([]byte(gs), []byte{' '})
+ suppGroups = make([]int, len(gss)+1)
+ for i, s := range gss {
+ if gid, err := strconv.Atoi(string(s)); err != nil {
+ log.Fatalf("cannot parse %q: %v", string(s), err)
+ } else if gid > 0 && gid != uid && gid != os.Getgid() && slices.Contains(suppCurrent, gid) {
+ suppGroups[i] = gid
+ } else {
+ log.Fatalf("invalid gid %d", gid)
+ }
+ }
+ suppGroups[len(suppGroups)-1] = uid
+ } else {
+ suppGroups = []int{uid}
+ }
+
+ // final bounds check to catch any bugs
+ if uid < 1000000 || uid >= 2000000 {
+ panic("uid out of bounds")
+ }
+
+ // careful! users in the allowlist is effectively allowed to drop groups via hsu
+
+ if err := syscall.Setresgid(uid, uid, uid); err != nil {
+ log.Fatalf("cannot set gid: %v", err)
+ }
+ if err := syscall.Setgroups(suppGroups); err != nil {
+ log.Fatalf("cannot set supplementary groups: %v", err)
+ }
+ if err := syscall.Setresuid(uid, uid, uid); err != nil {
+ log.Fatalf("cannot set uid: %v", err)
+ }
+ if _, _, errno := syscall.AllThreadsSyscall(syscall.SYS_PRCTL, PR_SET_NO_NEW_PRIVS, 1, 0); errno != 0 {
+ log.Fatalf("cannot set no_new_privs flag: %s", errno.Error())
+ }
+ if err := syscall.Exec(toolPath, []string{"hakurei", "shim"}, []string{envShim + "=" + shimSetupFd}); err != nil {
+ log.Fatalf("cannot start shim: %v", err)
+ }
+
+ panic("unreachable")
+}