//go:build linux package tests import ( "fmt" "net" "os" "os/exec" "os/user" "path/filepath" "strconv" "syscall" "testing" "time" "hth/internal/config" "hth/internal/grpcserver" "hth/internal/testutil" ) func TestObserveControlSocketDirModes(t *testing.T) { dir := t.TempDir() obsDir := filepath.Join(dir, "observe") ctlDir := filepath.Join(dir, "control") cfg := &config.Config{ RuntimeRoot: dir, Sockets: config.Sockets{ Observe: filepath.Join(obsDir, "observe.sock"), Control: filepath.Join(ctlDir, "control.sock"), ObservePerm: "0660", ControlPerm: "0660", }, } if err := grpcserver.PrepareSocketsForTest(cfg); err != nil { t.Fatal(err) } ci, err := os.Stat(ctlDir) if err != nil { t.Fatal(err) } if ci.Mode().Perm()&0o007 != 0 { t.Fatalf("control dir should not be world-accessible: %v", ci.Mode()) } } func TestRealUsersSocketIsolation(t *testing.T) { if os.Geteuid() != 0 { t.Fatal("P0-11 requires root to create observer/controller/untrusted accounts") } t.Setenv("PATH", "/usr/sbin:/sbin:"+os.Getenv("PATH")) if _, err := exec.LookPath("useradd"); err != nil { t.Fatalf("useradd not available: %v", err) } if _, err := exec.LookPath("groupadd"); err != nil { t.Fatalf("groupadd not available: %v", err) } suffix := strconv.Itoa(os.Getpid()) obsG, ctlG := "htog"+suffix, "htcg"+suffix obsU, ctlU, unU := "htou"+suffix, "htcu"+suffix, "htuu"+suffix run := func(name string, args ...string) error { cmd := exec.Command(name, args...) cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr return cmd.Run() } must := func(err error) { t.Helper() if err != nil { t.Fatal(err) } } must(run("groupadd", "-f", obsG)) must(run("groupadd", "-f", ctlG)) unG := "htug" + suffix must(run("groupadd", "-f", unG)) must(run("useradd", "-M", "-N", "-g", obsG, obsU)) must(run("useradd", "-M", "-N", "-g", ctlG, "-G", obsG, ctlU)) must(run("useradd", "-M", "-N", "-g", unG, unU)) t.Cleanup(func() { _ = exec.Command("userdel", "-f", obsU).Run() _ = exec.Command("userdel", "-f", ctlU).Run() _ = exec.Command("userdel", "-f", unU).Run() _ = exec.Command("groupdel", obsG).Run() _ = exec.Command("groupdel", ctlG).Run() _ = exec.Command("groupdel", unG).Run() }) root := filepath.Join(os.TempDir(), "hth-uds-"+suffix) must(os.MkdirAll(root, 0o0755)) must(os.Chmod(root, 0o0755)) t.Cleanup(func() { _ = os.RemoveAll(root) }) obsDir := filepath.Join(root, "observe") ctlDir := filepath.Join(root, "control") obsSock := filepath.Join(obsDir, "observe.sock") ctlSock := filepath.Join(ctlDir, "control.sock") must(os.MkdirAll(obsDir, 0o2750)) must(os.MkdirAll(ctlDir, 0o2750)) must(os.Chmod(root, 0o0751)) og, err := user.LookupGroup(obsG) if err != nil { t.Fatal(err) } cg, err := user.LookupGroup(ctlG) if err != nil { t.Fatal(err) } ogid, _ := strconv.Atoi(og.Gid) cgid, _ := strconv.Atoi(cg.Gid) must(os.Chown(obsDir, 0, ogid)) must(os.Chown(ctlDir, 0, cgid)) must(os.Chmod(obsDir, 0o2750)) must(os.Chmod(ctlDir, 0o2750)) yaml := fmt.Sprintf(` api_version: hth.api.v1 device_id: prod2u-0 backend: mock profile: %s runtime_root: %s sockets: observe: %s control: %s observe_perm: "0660" control_perm: "0660" observe_group: %s control_group: %s poll: temperature_ms: 80 motor_status_ms: 50 watchdog: heartbeat_period_ms: 200 lease: min_ttl_ms: 50 max_ttl_ms: 2000 safety: fan: 10 ptc: 0 door_close_percent: 0 ring_buffer_size: 16 subscriber_buffer_size: 2 grpc: reflection: false mock: seed: 1 startup_complete: true `, testutil.ProfilePath(t), root, obsSock, ctlSock, obsG, ctlG) cfgPath := filepath.Join(root, "hthd.yaml") must(os.WriteFile(cfgPath, []byte(yaml), 0o644)) a := startAppFromConfig(t, cfgPath) _ = a deadline := time.Now().Add(3 * time.Second) for time.Now().Before(deadline) { if _, err := os.Stat(obsSock); err == nil { if _, err := os.Stat(ctlSock); err == nil { break } } time.Sleep(20 * time.Millisecond) } must(os.Chmod(root, 0o0755)) must(os.Chmod(obsDir, 0o2750)) must(os.Chmod(ctlDir, 0o2750)) stObs, err := os.Stat(obsSock) if err != nil { t.Fatal(err) } if stObs.Mode()&0o007 != 0 { t.Fatalf("observe socket is world-accessible: %v", stObs.Mode()) } if sys, ok := stObs.Sys().(*syscall.Stat_t); ok { t.Logf("observe socket uid=%d gid=%d mode=%v want gid=%d", sys.Uid, sys.Gid, stObs.Mode(), ogid) if int(sys.Gid) != ogid { t.Fatalf("observe socket gid %d != %d (chown failed)", sys.Gid, ogid) } } helper := buildUDSConnect(t) ou, err := user.Lookup(obsU) if err != nil { t.Fatal(err) } cu, err := user.Lookup(ctlU) if err != nil { t.Fatal(err) } uu, err := user.Lookup(unU) if err != nil { t.Fatal(err) } type row struct { name string u *user.User groups []uint32 obsWant bool ctlWant bool } uid := func(u *user.User) uint32 { n, _ := strconv.Atoi(u.Uid) return uint32(n) } gid := func(u *user.User) uint32 { n, _ := strconv.Atoi(u.Gid) return uint32(n) } rows := []row{ {name: "observer", u: ou, groups: []uint32{uint32(ogid)}, obsWant: true, ctlWant: false}, {name: "controller", u: cu, groups: []uint32{uint32(cgid), uint32(ogid)}, obsWant: true, ctlWant: true}, {name: "untrusted", u: uu, groups: nil, obsWant: false, ctlWant: false}, } for _, r := range rows { gotObs := canConnectAs(t, helper, uid(r.u), gid(r.u), r.groups, obsSock) gotCtl := canConnectAs(t, helper, uid(r.u), gid(r.u), r.groups, ctlSock) if gotObs != r.obsWant || gotCtl != r.ctlWant { t.Errorf("%s: observe got %v want %v; control got %v want %v", r.name, gotObs, r.obsWant, gotCtl, r.ctlWant) } } // Untrusted must fail at the kernel, not merely by missing Control RPCs on Observe. c, err := net.DialTimeout("unix", obsSock, 200*time.Millisecond) if err != nil { t.Fatal("root should still connect observe:", err) } c.Close() } func buildUDSConnect(t *testing.T) string { t.Helper() dir := filepath.Join(os.TempDir(), "hth-udsconnect-"+strconv.Itoa(os.Getpid())) if err := os.MkdirAll(dir, 0o755); err != nil { t.Fatal(err) } if err := os.Chmod(dir, 0o755); err != nil { t.Fatal(err) } bin := filepath.Join(dir, "udsconnect") src := filepath.Join(testutil.HostRoot(t), "tools", "udsconnect", "main.go") cmd := exec.Command("go", "build", "-o", bin, src) cmd.Env = os.Environ() out, err := cmd.CombinedOutput() if err != nil { t.Fatalf("build udsconnect: %v %s", err, out) } if err := os.Chmod(bin, 0o755); err != nil { t.Fatal(err) } t.Cleanup(func() { _ = os.Remove(bin); _ = os.Remove(dir) }) return bin } func canConnectAs(t *testing.T, helper string, uid, gid uint32, groups []uint32, path string) bool { t.Helper() cmd := exec.Command(helper, path) cmd.SysProcAttr = &syscall.SysProcAttr{ Credential: &syscall.Credential{Uid: uid, Gid: gid, Groups: groups}, } out, err := cmd.CombinedOutput() if err != nil { t.Logf("connect uid=%d gid=%d path=%s: %v %s", uid, gid, path, err, out) return false } return true }