diff options
| author | Ophestra <cat@gensokyo.uk> | 2025-10-03 02:26:14 +0900 |
|---|---|---|
| committer | Ophestra <cat@gensokyo.uk> | 2025-10-03 02:26:14 +0900 |
| commit | d16da6da8c4880d90dc35fa9006806f7c0395d7f (patch) | |
| tree | d4ca8fed4e14bcffe63268daa6d8efd1923ff03b /internal | |
| parent | e58181a930064385c881b7ee1cac4914dd435121 (diff) | |
system: enforce absolute paths
This is less error-prone, and is quite easy to integrate considering internal/app has already migrated to container.Absolute.
Closes #11.
Signed-off-by: Ophestra <cat@gensokyo.uk>
Diffstat (limited to 'internal')
| -rw-r--r-- | internal/app/app_test.go | 81 | ||||
| -rw-r--r-- | internal/app/finalise.go | 52 |
2 files changed, 66 insertions, 67 deletions
diff --git a/internal/app/app_test.go b/internal/app/app_test.go index 585f95b2..5c8e9e7a 100644 --- a/internal/app/app_test.go +++ b/internal/app/app_test.go @@ -1,7 +1,6 @@ package app import ( - "context" "encoding/json" "io/fs" "os" @@ -37,12 +36,12 @@ func TestApp(t *testing.T) { 0xbd, 0x01, 0x78, 0x0e, 0xb9, 0xa6, 0x07, 0xac, }, - system.New(context.TODO(), container.NewMsg(nil), 1000000). - Ensure("/tmp/hakurei.0", 0711). - Ensure("/tmp/hakurei.0/runtime", 0700).UpdatePermType(system.User, "/tmp/hakurei.0/runtime", acl.Execute). - Ensure("/tmp/hakurei.0/runtime/0", 0700).UpdatePermType(system.User, "/tmp/hakurei.0/runtime/0", acl.Read, acl.Write, acl.Execute). - Ensure("/tmp/hakurei.0/tmpdir", 0700).UpdatePermType(system.User, "/tmp/hakurei.0/tmpdir", acl.Execute). - Ensure("/tmp/hakurei.0/tmpdir/0", 01700).UpdatePermType(system.User, "/tmp/hakurei.0/tmpdir/0", acl.Read, acl.Write, acl.Execute), + system.New(t.Context(), container.NewMsg(nil), 1000000). + Ensure(m("/tmp/hakurei.0"), 0711). + Ensure(m("/tmp/hakurei.0/runtime"), 0700).UpdatePermType(system.User, m("/tmp/hakurei.0/runtime"), acl.Execute). + Ensure(m("/tmp/hakurei.0/runtime/0"), 0700).UpdatePermType(system.User, m("/tmp/hakurei.0/runtime/0"), acl.Read, acl.Write, acl.Execute). + Ensure(m("/tmp/hakurei.0/tmpdir"), 0700).UpdatePermType(system.User, m("/tmp/hakurei.0/tmpdir"), acl.Execute). + Ensure(m("/tmp/hakurei.0/tmpdir/0"), 01700).UpdatePermType(system.User, m("/tmp/hakurei.0/tmpdir/0"), acl.Read, acl.Write, acl.Execute), &container.Params{ Dir: m("/home/chronos"), Path: m("/run/current-system/sw/bin/zsh"), @@ -129,20 +128,20 @@ func TestApp(t *testing.T) { 0x82, 0xd4, 0x13, 0x36, 0x9b, 0x64, 0xce, 0x7c, }, - system.New(context.TODO(), container.NewMsg(nil), 1000009). - Ensure("/tmp/hakurei.0", 0711). - Ensure("/tmp/hakurei.0/runtime", 0700).UpdatePermType(system.User, "/tmp/hakurei.0/runtime", acl.Execute). - Ensure("/tmp/hakurei.0/runtime/9", 0700).UpdatePermType(system.User, "/tmp/hakurei.0/runtime/9", acl.Read, acl.Write, acl.Execute). - Ensure("/tmp/hakurei.0/tmpdir", 0700).UpdatePermType(system.User, "/tmp/hakurei.0/tmpdir", acl.Execute). - Ensure("/tmp/hakurei.0/tmpdir/9", 01700).UpdatePermType(system.User, "/tmp/hakurei.0/tmpdir/9", acl.Read, acl.Write, acl.Execute). - Ephemeral(system.Process, "/tmp/hakurei.0/ebf083d1b175911782d413369b64ce7c", 0711). - Wayland(new(*os.File), "/tmp/hakurei.0/ebf083d1b175911782d413369b64ce7c/wayland", "/run/user/1971/wayland-0", "org.chromium.Chromium", "ebf083d1b175911782d413369b64ce7c"). - Ensure("/run/user/1971/hakurei", 0700).UpdatePermType(system.User, "/run/user/1971/hakurei", acl.Execute). - Ensure("/run/user/1971", 0700).UpdatePermType(system.User, "/run/user/1971", acl.Execute). // this is ordered as is because the previous Ensure only calls mkdir if XDG_RUNTIME_DIR is unset - Ephemeral(system.Process, "/run/user/1971/hakurei/ebf083d1b175911782d413369b64ce7c", 0700).UpdatePermType(system.Process, "/run/user/1971/hakurei/ebf083d1b175911782d413369b64ce7c", acl.Execute). - Link("/run/user/1971/pulse/native", "/run/user/1971/hakurei/ebf083d1b175911782d413369b64ce7c/pulse"). - CopyFile(new([]byte), "/home/ophestra/xdg/config/pulse/cookie", 256, 256). - MustProxyDBus("/tmp/hakurei.0/ebf083d1b175911782d413369b64ce7c/bus", &dbus.Config{ + system.New(t.Context(), container.NewMsg(nil), 1000009). + Ensure(m("/tmp/hakurei.0"), 0711). + Ensure(m("/tmp/hakurei.0/runtime"), 0700).UpdatePermType(system.User, m("/tmp/hakurei.0/runtime"), acl.Execute). + Ensure(m("/tmp/hakurei.0/runtime/9"), 0700).UpdatePermType(system.User, m("/tmp/hakurei.0/runtime/9"), acl.Read, acl.Write, acl.Execute). + Ensure(m("/tmp/hakurei.0/tmpdir"), 0700).UpdatePermType(system.User, m("/tmp/hakurei.0/tmpdir"), acl.Execute). + Ensure(m("/tmp/hakurei.0/tmpdir/9"), 01700).UpdatePermType(system.User, m("/tmp/hakurei.0/tmpdir/9"), acl.Read, acl.Write, acl.Execute). + Ephemeral(system.Process, m("/tmp/hakurei.0/ebf083d1b175911782d413369b64ce7c"), 0711). + Wayland(new(*os.File), m("/tmp/hakurei.0/ebf083d1b175911782d413369b64ce7c/wayland"), m("/run/user/1971/wayland-0"), "org.chromium.Chromium", "ebf083d1b175911782d413369b64ce7c"). + Ensure(m("/run/user/1971/hakurei"), 0700).UpdatePermType(system.User, m("/run/user/1971/hakurei"), acl.Execute). + Ensure(m("/run/user/1971"), 0700).UpdatePermType(system.User, m("/run/user/1971"), acl.Execute). // this is ordered as is because the previous Ensure only calls mkdir if XDG_RUNTIME_DIR is unset + Ephemeral(system.Process, m("/run/user/1971/hakurei/ebf083d1b175911782d413369b64ce7c"), 0700).UpdatePermType(system.Process, m("/run/user/1971/hakurei/ebf083d1b175911782d413369b64ce7c"), acl.Execute). + Link(m("/run/user/1971/pulse/native"), m("/run/user/1971/hakurei/ebf083d1b175911782d413369b64ce7c/pulse")). + CopyFile(new([]byte), m("/home/ophestra/xdg/config/pulse/cookie"), 256, 256). + MustProxyDBus(m("/tmp/hakurei.0/ebf083d1b175911782d413369b64ce7c/bus"), &dbus.Config{ Talk: []string{ "org.freedesktop.Notifications", "org.freedesktop.FileManager1", @@ -164,7 +163,7 @@ func TestApp(t *testing.T) { "org.freedesktop.portal.*": "@/org/freedesktop/portal/*", }, Filter: true, - }, "/tmp/hakurei.0/ebf083d1b175911782d413369b64ce7c/system_bus_socket", &dbus.Config{ + }, m("/tmp/hakurei.0/ebf083d1b175911782d413369b64ce7c/system_bus_socket"), &dbus.Config{ Talk: []string{ "org.bluez", "org.freedesktop.Avahi", @@ -172,8 +171,8 @@ func TestApp(t *testing.T) { }, Filter: true, }). - UpdatePerm("/tmp/hakurei.0/ebf083d1b175911782d413369b64ce7c/bus", acl.Read, acl.Write). - UpdatePerm("/tmp/hakurei.0/ebf083d1b175911782d413369b64ce7c/system_bus_socket", acl.Read, acl.Write), + UpdatePerm(m("/tmp/hakurei.0/ebf083d1b175911782d413369b64ce7c/bus"), acl.Read, acl.Write). + UpdatePerm(m("/tmp/hakurei.0/ebf083d1b175911782d413369b64ce7c/system_bus_socket"), acl.Read, acl.Write), &container.Params{ Dir: m("/home/chronos"), Path: m("/run/current-system/sw/bin/zsh"), @@ -280,20 +279,20 @@ func TestApp(t *testing.T) { 0x4c, 0xf0, 0x73, 0xbd, 0xb4, 0x6e, 0xb5, 0xc1, }, - system.New(context.TODO(), container.NewMsg(nil), 1000001). - Ensure("/tmp/hakurei.0", 0711). - Ensure("/tmp/hakurei.0/runtime", 0700).UpdatePermType(system.User, "/tmp/hakurei.0/runtime", acl.Execute). - Ensure("/tmp/hakurei.0/runtime/1", 0700).UpdatePermType(system.User, "/tmp/hakurei.0/runtime/1", acl.Read, acl.Write, acl.Execute). - Ensure("/tmp/hakurei.0/tmpdir", 0700).UpdatePermType(system.User, "/tmp/hakurei.0/tmpdir", acl.Execute). - Ensure("/tmp/hakurei.0/tmpdir/1", 01700).UpdatePermType(system.User, "/tmp/hakurei.0/tmpdir/1", acl.Read, acl.Write, acl.Execute). - Ensure("/run/user/1971/hakurei", 0700).UpdatePermType(system.User, "/run/user/1971/hakurei", acl.Execute). - Ensure("/run/user/1971", 0700).UpdatePermType(system.User, "/run/user/1971", acl.Execute). // this is ordered as is because the previous Ensure only calls mkdir if XDG_RUNTIME_DIR is unset - UpdatePermType(hst.EWayland, "/run/user/1971/wayland-0", acl.Read, acl.Write, acl.Execute). - Ephemeral(system.Process, "/run/user/1971/hakurei/8e2c76b066dabe574cf073bdb46eb5c1", 0700).UpdatePermType(system.Process, "/run/user/1971/hakurei/8e2c76b066dabe574cf073bdb46eb5c1", acl.Execute). - Link("/run/user/1971/pulse/native", "/run/user/1971/hakurei/8e2c76b066dabe574cf073bdb46eb5c1/pulse"). - CopyFile(nil, "/home/ophestra/xdg/config/pulse/cookie", 256, 256). - Ephemeral(system.Process, "/tmp/hakurei.0/8e2c76b066dabe574cf073bdb46eb5c1", 0711). - MustProxyDBus("/tmp/hakurei.0/8e2c76b066dabe574cf073bdb46eb5c1/bus", &dbus.Config{ + system.New(t.Context(), container.NewMsg(nil), 1000001). + Ensure(m("/tmp/hakurei.0"), 0711). + Ensure(m("/tmp/hakurei.0/runtime"), 0700).UpdatePermType(system.User, m("/tmp/hakurei.0/runtime"), acl.Execute). + Ensure(m("/tmp/hakurei.0/runtime/1"), 0700).UpdatePermType(system.User, m("/tmp/hakurei.0/runtime/1"), acl.Read, acl.Write, acl.Execute). + Ensure(m("/tmp/hakurei.0/tmpdir"), 0700).UpdatePermType(system.User, m("/tmp/hakurei.0/tmpdir"), acl.Execute). + Ensure(m("/tmp/hakurei.0/tmpdir/1"), 01700).UpdatePermType(system.User, m("/tmp/hakurei.0/tmpdir/1"), acl.Read, acl.Write, acl.Execute). + Ensure(m("/run/user/1971/hakurei"), 0700).UpdatePermType(system.User, m("/run/user/1971/hakurei"), acl.Execute). + Ensure(m("/run/user/1971"), 0700).UpdatePermType(system.User, m("/run/user/1971"), acl.Execute). // this is ordered as is because the previous Ensure only calls mkdir if XDG_RUNTIME_DIR is unset + UpdatePermType(hst.EWayland, m("/run/user/1971/wayland-0"), acl.Read, acl.Write, acl.Execute). + Ephemeral(system.Process, m("/run/user/1971/hakurei/8e2c76b066dabe574cf073bdb46eb5c1"), 0700).UpdatePermType(system.Process, m("/run/user/1971/hakurei/8e2c76b066dabe574cf073bdb46eb5c1"), acl.Execute). + Link(m("/run/user/1971/pulse/native"), m("/run/user/1971/hakurei/8e2c76b066dabe574cf073bdb46eb5c1/pulse")). + CopyFile(nil, m("/home/ophestra/xdg/config/pulse/cookie"), 256, 256). + Ephemeral(system.Process, m("/tmp/hakurei.0/8e2c76b066dabe574cf073bdb46eb5c1"), 0711). + MustProxyDBus(m("/tmp/hakurei.0/8e2c76b066dabe574cf073bdb46eb5c1/bus"), &dbus.Config{ Talk: []string{ "org.freedesktop.FileManager1", "org.freedesktop.Notifications", "org.freedesktop.ScreenSaver", "org.freedesktop.secrets", @@ -306,7 +305,7 @@ func TestApp(t *testing.T) { }, Call: map[string]string{}, Broadcast: map[string]string{}, Filter: true, - }, "/tmp/hakurei.0/8e2c76b066dabe574cf073bdb46eb5c1/system_bus_socket", &dbus.Config{ + }, m("/tmp/hakurei.0/8e2c76b066dabe574cf073bdb46eb5c1/system_bus_socket"), &dbus.Config{ Talk: []string{ "org.bluez", "org.freedesktop.Avahi", @@ -314,8 +313,8 @@ func TestApp(t *testing.T) { }, Filter: true, }). - UpdatePerm("/tmp/hakurei.0/8e2c76b066dabe574cf073bdb46eb5c1/bus", acl.Read, acl.Write). - UpdatePerm("/tmp/hakurei.0/8e2c76b066dabe574cf073bdb46eb5c1/system_bus_socket", acl.Read, acl.Write), + UpdatePerm(m("/tmp/hakurei.0/8e2c76b066dabe574cf073bdb46eb5c1/bus"), acl.Read, acl.Write). + UpdatePerm(m("/tmp/hakurei.0/8e2c76b066dabe574cf073bdb46eb5c1/system_bus_socket"), acl.Read, acl.Write), &container.Params{ Uid: 1971, Gid: 100, diff --git a/internal/app/finalise.go b/internal/app/finalise.go index fbe4bb38..a33bcc91 100644 --- a/internal/app/finalise.go +++ b/internal/app/finalise.go @@ -76,10 +76,10 @@ func (share *shareHost) ensureRuntimeDir() { return } share.useRuntimeDir = true - share.seal.sys.Ensure(share.sc.RunDirPath.String(), 0700) - share.seal.sys.UpdatePermType(system.User, share.sc.RunDirPath.String(), acl.Execute) - share.seal.sys.Ensure(share.sc.RuntimePath.String(), 0700) // ensure this dir in case XDG_RUNTIME_DIR is unset - share.seal.sys.UpdatePermType(system.User, share.sc.RuntimePath.String(), acl.Execute) + share.seal.sys.Ensure(share.sc.RunDirPath, 0700) + share.seal.sys.UpdatePermType(system.User, share.sc.RunDirPath, acl.Execute) + share.seal.sys.Ensure(share.sc.RuntimePath, 0700) // ensure this dir in case XDG_RUNTIME_DIR is unset + share.seal.sys.UpdatePermType(system.User, share.sc.RuntimePath, acl.Execute) } // instance returns a process-specific share path within tmpdir @@ -88,7 +88,7 @@ func (share *shareHost) instance() *container.Absolute { return share.sharePath } share.sharePath = share.sc.SharePath.Append(share.seal.id.String()) - share.seal.sys.Ephemeral(system.Process, share.sharePath.String(), 0711) + share.seal.sys.Ephemeral(system.Process, share.sharePath, 0711) return share.sharePath } @@ -99,8 +99,8 @@ func (share *shareHost) runtime() *container.Absolute { } share.ensureRuntimeDir() share.runtimeSharePath = share.sc.RunDirPath.Append(share.seal.id.String()) - share.seal.sys.Ephemeral(system.Process, share.runtimeSharePath.String(), 0700) - share.seal.sys.UpdatePerm(share.runtimeSharePath.String(), acl.Execute) + share.seal.sys.Ephemeral(system.Process, share.runtimeSharePath, 0700) + share.seal.sys.UpdatePerm(share.runtimeSharePath, acl.Execute) return share.runtimeSharePath } @@ -308,26 +308,26 @@ func (k *outcome) finalise(ctx context.Context, msg container.Msg, config *hst.C k.runDirPath = share.sc.RunDirPath k.sys = system.New(k.ctx, msg, k.user.uid.unwrap()) - k.sys.Ensure(share.sc.SharePath.String(), 0711) + k.sys.Ensure(share.sc.SharePath, 0711) { runtimeDir := share.sc.SharePath.Append("runtime") - k.sys.Ensure(runtimeDir.String(), 0700) - k.sys.UpdatePermType(system.User, runtimeDir.String(), acl.Execute) + k.sys.Ensure(runtimeDir, 0700) + k.sys.UpdatePermType(system.User, runtimeDir, acl.Execute) runtimeDirInst := runtimeDir.Append(k.user.identity.String()) - k.sys.Ensure(runtimeDirInst.String(), 0700) - k.sys.UpdatePermType(system.User, runtimeDirInst.String(), acl.Read, acl.Write, acl.Execute) + k.sys.Ensure(runtimeDirInst, 0700) + k.sys.UpdatePermType(system.User, runtimeDirInst, acl.Read, acl.Write, acl.Execute) k.container.Tmpfs(container.AbsFHSRunUser, 1<<12, 0755) k.container.Bind(runtimeDirInst, innerRuntimeDir, container.BindWritable) } { tmpdir := share.sc.SharePath.Append("tmpdir") - k.sys.Ensure(tmpdir.String(), 0700) - k.sys.UpdatePermType(system.User, tmpdir.String(), acl.Execute) + k.sys.Ensure(tmpdir, 0700) + k.sys.UpdatePermType(system.User, tmpdir, acl.Execute) tmpdirInst := tmpdir.Append(k.user.identity.String()) - k.sys.Ensure(tmpdirInst.String(), 01700) - k.sys.UpdatePermType(system.User, tmpdirInst.String(), acl.Read, acl.Write, acl.Execute) + k.sys.Ensure(tmpdirInst, 01700) + k.sys.UpdatePermType(system.User, tmpdirInst, acl.Read, acl.Write, acl.Execute) // mount inner /tmp from share so it shares persistence and storage behaviour of host /tmp k.container.Bind(tmpdirInst, container.AbsFHSTmp, container.BindWritable) } @@ -376,13 +376,13 @@ func (k *outcome) finalise(ctx context.Context, msg container.Msg, config *hst.C } // downstream socket paths outerPath := share.instance().Append("wayland") - k.sys.Wayland(&k.sync, outerPath.String(), socketPath.String(), appID, k.id.String()) + k.sys.Wayland(&k.sync, outerPath, socketPath, appID, k.id.String()) k.container.Bind(outerPath, innerPath, 0) } else { // bind mount wayland socket (insecure) msg.Verbose("direct wayland access, PROCEED WITH CAUTION") share.ensureRuntimeDir() k.container.Bind(socketPath, innerPath, 0) - k.sys.UpdatePermType(hst.EWayland, socketPath.String(), acl.Read, acl.Write, acl.Execute) + k.sys.UpdatePermType(hst.EWayland, socketPath, acl.Read, acl.Write, acl.Execute) } } @@ -410,7 +410,7 @@ func (k *outcome) finalise(ctx context.Context, msg container.Msg, config *hst.C return &hst.AppError{Step: fmt.Sprintf("access X11 socket %q", socketPath), Err: err} } } else { - k.sys.UpdatePermType(hst.EX11, socketPath.String(), acl.Read, acl.Write, acl.Execute) + k.sys.UpdatePermType(hst.EX11, socketPath, acl.Read, acl.Write, acl.Execute) if !config.Container.HostAbstract { d = "unix:" + socketPath.String() } @@ -450,7 +450,7 @@ func (k *outcome) finalise(ctx context.Context, msg container.Msg, config *hst.C // hard link pulse socket into target-executable share innerPulseRuntimeDir := share.runtime().Append("pulse") innerPulseSocket := innerRuntimeDir.Append("pulse", "native") - k.sys.Link(pulseSocket.String(), innerPulseRuntimeDir.String()) + k.sys.Link(pulseSocket, innerPulseRuntimeDir) k.container.Bind(innerPulseRuntimeDir, innerPulseSocket, 0) k.env[pulseServer] = "unix:" + innerPulseSocket.String() @@ -518,7 +518,7 @@ func (k *outcome) finalise(ctx context.Context, msg container.Msg, config *hst.C k.env[pulseCookie] = innerDst.String() var payload *[]byte k.container.PlaceP(innerDst, &payload) - k.sys.CopyFile(payload, paCookiePath.String(), 256, 256) + k.sys.CopyFile(payload, paCookiePath, 256, 256) } else { msg.Verbose("cannot locate PulseAudio cookie (tried " + "$PULSE_COOKIE, " + @@ -539,7 +539,7 @@ func (k *outcome) finalise(ctx context.Context, msg container.Msg, config *hst.C // configure dbus proxy if f, err := k.sys.ProxyDBus( config.SessionBus, config.SystemBus, - sessionPath.String(), systemPath.String(), + sessionPath, systemPath, ); err != nil { return err } else { @@ -550,12 +550,12 @@ func (k *outcome) finalise(ctx context.Context, msg container.Msg, config *hst.C sessionInner := innerRuntimeDir.Append("bus") k.env[dbusSessionBusAddress] = "unix:path=" + sessionInner.String() k.container.Bind(sessionPath, sessionInner, 0) - k.sys.UpdatePerm(sessionPath.String(), acl.Read, acl.Write) + k.sys.UpdatePerm(sessionPath, acl.Read, acl.Write) if config.SystemBus != nil { systemInner := container.AbsFHSRun.Append("dbus/system_bus_socket") k.env[dbusSystemBusAddress] = "unix:path=" + systemInner.String() k.container.Bind(systemPath, systemInner, 0) - k.sys.UpdatePerm(systemPath.String(), acl.Read, acl.Write) + k.sys.UpdatePerm(systemPath, acl.Read, acl.Write) } } @@ -569,7 +569,7 @@ func (k *outcome) finalise(ctx context.Context, msg container.Msg, config *hst.C } if p.Ensure { - k.sys.Ensure(p.Path.String(), 0700) + k.sys.Ensure(p.Path, 0700) } perms := make(acl.Perms, 0, 3) @@ -582,7 +582,7 @@ func (k *outcome) finalise(ctx context.Context, msg container.Msg, config *hst.C if p.Execute { perms = append(perms, acl.Execute) } - k.sys.UpdatePermType(system.User, p.Path.String(), perms...) + k.sys.UpdatePermType(system.User, p.Path, perms...) } // flatten and sort env for deterministic behaviour |
