uds_perm_linux_test.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. //go:build linux
  2. package tests
  3. import (
  4. "fmt"
  5. "net"
  6. "os"
  7. "os/exec"
  8. "os/user"
  9. "path/filepath"
  10. "strconv"
  11. "syscall"
  12. "testing"
  13. "time"
  14. "hth/internal/config"
  15. "hth/internal/grpcserver"
  16. "hth/internal/testutil"
  17. )
  18. func TestObserveControlSocketDirModes(t *testing.T) {
  19. dir := t.TempDir()
  20. obsDir := filepath.Join(dir, "observe")
  21. ctlDir := filepath.Join(dir, "control")
  22. cfg := &config.Config{
  23. RuntimeRoot: dir,
  24. Sockets: config.Sockets{
  25. Observe: filepath.Join(obsDir, "observe.sock"),
  26. Control: filepath.Join(ctlDir, "control.sock"),
  27. ObservePerm: "0660",
  28. ControlPerm: "0660",
  29. },
  30. }
  31. if err := grpcserver.PrepareSocketsForTest(cfg); err != nil {
  32. t.Fatal(err)
  33. }
  34. ci, err := os.Stat(ctlDir)
  35. if err != nil {
  36. t.Fatal(err)
  37. }
  38. if ci.Mode().Perm()&0o007 != 0 {
  39. t.Fatalf("control dir should not be world-accessible: %v", ci.Mode())
  40. }
  41. }
  42. func TestRealUsersSocketIsolation(t *testing.T) {
  43. if os.Geteuid() != 0 {
  44. t.Fatal("P0-11 requires root to create observer/controller/untrusted accounts")
  45. }
  46. if _, err := exec.LookPath("useradd"); err != nil {
  47. t.Fatalf("useradd not available: %v", err)
  48. }
  49. if _, err := exec.LookPath("groupadd"); err != nil {
  50. t.Fatalf("groupadd not available: %v", err)
  51. }
  52. suffix := strconv.Itoa(os.Getpid())
  53. obsG, ctlG := "htog"+suffix, "htcg"+suffix
  54. obsU, ctlU, unU := "htou"+suffix, "htcu"+suffix, "htuu"+suffix
  55. run := func(name string, args ...string) error {
  56. cmd := exec.Command(name, args...)
  57. cmd.Stdout = os.Stdout
  58. cmd.Stderr = os.Stderr
  59. return cmd.Run()
  60. }
  61. must := func(err error) {
  62. t.Helper()
  63. if err != nil {
  64. t.Fatal(err)
  65. }
  66. }
  67. must(run("groupadd", "-f", obsG))
  68. must(run("groupadd", "-f", ctlG))
  69. unG := "htug" + suffix
  70. must(run("groupadd", "-f", unG))
  71. must(run("useradd", "-M", "-N", "-g", obsG, obsU))
  72. must(run("useradd", "-M", "-N", "-g", ctlG, "-G", obsG, ctlU))
  73. must(run("useradd", "-M", "-N", "-g", unG, unU))
  74. t.Cleanup(func() {
  75. _ = exec.Command("userdel", "-f", obsU).Run()
  76. _ = exec.Command("userdel", "-f", ctlU).Run()
  77. _ = exec.Command("userdel", "-f", unU).Run()
  78. _ = exec.Command("groupdel", obsG).Run()
  79. _ = exec.Command("groupdel", ctlG).Run()
  80. _ = exec.Command("groupdel", unG).Run()
  81. })
  82. root := filepath.Join(os.TempDir(), "hth-uds-"+suffix)
  83. must(os.MkdirAll(root, 0o0755))
  84. must(os.Chmod(root, 0o0755))
  85. t.Cleanup(func() { _ = os.RemoveAll(root) })
  86. obsDir := filepath.Join(root, "observe")
  87. ctlDir := filepath.Join(root, "control")
  88. obsSock := filepath.Join(obsDir, "observe.sock")
  89. ctlSock := filepath.Join(ctlDir, "control.sock")
  90. must(os.MkdirAll(obsDir, 0o2750))
  91. must(os.MkdirAll(ctlDir, 0o2750))
  92. must(os.Chmod(root, 0o0751))
  93. og, err := user.LookupGroup(obsG)
  94. if err != nil {
  95. t.Fatal(err)
  96. }
  97. cg, err := user.LookupGroup(ctlG)
  98. if err != nil {
  99. t.Fatal(err)
  100. }
  101. ogid, _ := strconv.Atoi(og.Gid)
  102. cgid, _ := strconv.Atoi(cg.Gid)
  103. must(os.Chown(obsDir, 0, ogid))
  104. must(os.Chown(ctlDir, 0, cgid))
  105. must(os.Chmod(obsDir, 0o2750))
  106. must(os.Chmod(ctlDir, 0o2750))
  107. yaml := fmt.Sprintf(`
  108. api_version: hth.api.v1
  109. device_id: prod2u-0
  110. backend: mock
  111. profile: %s
  112. runtime_root: %s
  113. sockets:
  114. observe: %s
  115. control: %s
  116. observe_perm: "0660"
  117. control_perm: "0660"
  118. observe_group: %s
  119. control_group: %s
  120. poll:
  121. temperature_ms: 80
  122. motor_status_ms: 50
  123. watchdog:
  124. heartbeat_period_ms: 200
  125. lease:
  126. min_ttl_ms: 50
  127. max_ttl_ms: 2000
  128. safety:
  129. fan: 10
  130. ptc: 0
  131. door_close_percent: 0
  132. ring_buffer_size: 16
  133. subscriber_buffer_size: 2
  134. grpc:
  135. reflection: false
  136. mock:
  137. seed: 1
  138. startup_complete: true
  139. `, testutil.ProfilePath(t), root, obsSock, ctlSock, obsG, ctlG)
  140. cfgPath := filepath.Join(root, "hthd.yaml")
  141. must(os.WriteFile(cfgPath, []byte(yaml), 0o644))
  142. a := startAppFromConfig(t, cfgPath)
  143. _ = a
  144. deadline := time.Now().Add(3 * time.Second)
  145. for time.Now().Before(deadline) {
  146. if _, err := os.Stat(obsSock); err == nil {
  147. if _, err := os.Stat(ctlSock); err == nil {
  148. break
  149. }
  150. }
  151. time.Sleep(20 * time.Millisecond)
  152. }
  153. must(os.Chmod(root, 0o0755))
  154. must(os.Chmod(obsDir, 0o2750))
  155. must(os.Chmod(ctlDir, 0o2750))
  156. stObs, err := os.Stat(obsSock)
  157. if err != nil {
  158. t.Fatal(err)
  159. }
  160. if stObs.Mode()&0o007 != 0 {
  161. t.Fatalf("observe socket is world-accessible: %v", stObs.Mode())
  162. }
  163. if sys, ok := stObs.Sys().(*syscall.Stat_t); ok {
  164. t.Logf("observe socket uid=%d gid=%d mode=%v want gid=%d", sys.Uid, sys.Gid, stObs.Mode(), ogid)
  165. if int(sys.Gid) != ogid {
  166. t.Fatalf("observe socket gid %d != %d (chown failed)", sys.Gid, ogid)
  167. }
  168. }
  169. helper := buildUDSConnect(t)
  170. ou, err := user.Lookup(obsU)
  171. if err != nil {
  172. t.Fatal(err)
  173. }
  174. cu, err := user.Lookup(ctlU)
  175. if err != nil {
  176. t.Fatal(err)
  177. }
  178. uu, err := user.Lookup(unU)
  179. if err != nil {
  180. t.Fatal(err)
  181. }
  182. type row struct {
  183. name string
  184. u *user.User
  185. groups []uint32
  186. obsWant bool
  187. ctlWant bool
  188. }
  189. uid := func(u *user.User) uint32 {
  190. n, _ := strconv.Atoi(u.Uid)
  191. return uint32(n)
  192. }
  193. gid := func(u *user.User) uint32 {
  194. n, _ := strconv.Atoi(u.Gid)
  195. return uint32(n)
  196. }
  197. rows := []row{
  198. {name: "observer", u: ou, groups: []uint32{uint32(ogid)}, obsWant: true, ctlWant: false},
  199. {name: "controller", u: cu, groups: []uint32{uint32(cgid), uint32(ogid)}, obsWant: true, ctlWant: true},
  200. {name: "untrusted", u: uu, groups: nil, obsWant: false, ctlWant: false},
  201. }
  202. for _, r := range rows {
  203. gotObs := canConnectAs(t, helper, uid(r.u), gid(r.u), r.groups, obsSock)
  204. gotCtl := canConnectAs(t, helper, uid(r.u), gid(r.u), r.groups, ctlSock)
  205. if gotObs != r.obsWant || gotCtl != r.ctlWant {
  206. t.Errorf("%s: observe got %v want %v; control got %v want %v", r.name, gotObs, r.obsWant, gotCtl, r.ctlWant)
  207. }
  208. }
  209. // Untrusted must fail at the kernel, not merely by missing Control RPCs on Observe.
  210. c, err := net.DialTimeout("unix", obsSock, 200*time.Millisecond)
  211. if err != nil {
  212. t.Fatal("root should still connect observe:", err)
  213. }
  214. c.Close()
  215. }
  216. func buildUDSConnect(t *testing.T) string {
  217. t.Helper()
  218. dir := filepath.Join(os.TempDir(), "hth-udsconnect-"+strconv.Itoa(os.Getpid()))
  219. if err := os.MkdirAll(dir, 0o755); err != nil {
  220. t.Fatal(err)
  221. }
  222. if err := os.Chmod(dir, 0o755); err != nil {
  223. t.Fatal(err)
  224. }
  225. bin := filepath.Join(dir, "udsconnect")
  226. src := filepath.Join(testutil.HostRoot(t), "tools", "udsconnect", "main.go")
  227. cmd := exec.Command("go", "build", "-o", bin, src)
  228. cmd.Env = os.Environ()
  229. out, err := cmd.CombinedOutput()
  230. if err != nil {
  231. t.Fatalf("build udsconnect: %v %s", err, out)
  232. }
  233. if err := os.Chmod(bin, 0o755); err != nil {
  234. t.Fatal(err)
  235. }
  236. t.Cleanup(func() { _ = os.Remove(bin); _ = os.Remove(dir) })
  237. return bin
  238. }
  239. func canConnectAs(t *testing.T, helper string, uid, gid uint32, groups []uint32, path string) bool {
  240. t.Helper()
  241. cmd := exec.Command(helper, path)
  242. cmd.SysProcAttr = &syscall.SysProcAttr{
  243. Credential: &syscall.Credential{Uid: uid, Gid: gid, Groups: groups},
  244. }
  245. out, err := cmd.CombinedOutput()
  246. if err != nil {
  247. t.Logf("connect uid=%d gid=%d path=%s: %v %s", uid, gid, path, err, out)
  248. return false
  249. }
  250. return true
  251. }