Sandbox the systemd unit: DynamicUser, read-only FS, no capabilities

The Linux install now mirrors the Windows virtual-account hardening: the
unit runs with DynamicUser=yes (transient per-service UID, no login),
ProtectSystem=strict with only StateDirectory writable (the install dir,
so self-update can rewrite the binary), NoNewPrivileges, empty
capability sets, restricted address families and a @system-service
syscall filter. Install copies the binary to /var/lib/gpu-turnstile and
the config to /etc/gpu-turnstile.env; Remove cleans up the unit and
binary but keeps the config.
This commit is contained in:
mram
2026-09-21 00:11:25 +02:00
parent 97624470eb
commit a88955e35c
4 changed files with 115 additions and 21 deletions
+7 -3
View File
@@ -127,9 +127,13 @@ gpu-turnstile --remove-service
The unit (`/etc/systemd/system/gpu-turnstile.service`) is `Type=notify`: The unit (`/etc/systemd/system/gpu-turnstile.service`) is `Type=notify`:
`systemctl start` blocks until the listeners are actually bound, a 30 s `systemctl start` blocks until the listeners are actually bound, a 30 s
watchdog restarts the process if it wedges, and logs land in the journal watchdog restarts the process if it wedges, and logs land in the journal
(`journalctl -u gpu-turnstile -f`) unless `LOG_FILE` is set. Put the (`journalctl -u gpu-turnstile -f`) unless `LOG_FILE` is set. Install
config in a `gpu-turnstile.env` next to the binary (or pass copies the binary to `/var/lib/gpu-turnstile/` and the config to
`-config /path` during install). The notify integration is a no-op in `/etc/gpu-turnstile.env` (edit that one after installing). The service
runs sandboxed with `DynamicUser=yes` — a transient low-privilege UID,
read-only filesystem except its install dir (so self-update keeps
working), no capabilities, syscall-filtered: same least-privilege idea as
the Windows virtual account. The notify integration is a no-op in
containers and interactive shells. containers and interactive shells.
**Auto-update is on by default**: the binary checks the repo's latest **Auto-update is on by default**: the binary checks the repo's latest
+16 -6
View File
@@ -195,12 +195,22 @@ install|remove` does the same thing.
### Linux (systemd) ### Linux (systemd)
- `--install-service` writes `/etc/systemd/system/gpu-turnstile.service` - `--install-service` copies the binary to `/var/lib/gpu-turnstile/`,
with `ExecStart` pointing at the current executable and the `-config` copies the config to `/etc/gpu-turnstile.env` if none exists there yet,
file, then runs `systemctl daemon-reload` and `enable --now`. The unit writes `/etc/systemd/system/gpu-turnstile.service`, then runs `systemctl
runs as root (it must be able to overwrite its own binary for daemon-reload` and `enable --now`. `--remove-service` removes the unit
self-updates); harden with `ProtectSystem=strict` plus a writable and the installed binary; the `/etc` config stays.
`ReadWritePaths` if desired. - **Sandboxing** mirrors the Windows virtual account: the unit runs with
`DynamicUser=yes` — a transient per-service UID with no login, no home
and no password, managed entirely by systemd. `ProtectSystem=strict`
makes the filesystem read-only except `StateDirectory=gpu-turnstile`
(the install dir, so self-updates can rewrite the binary), plus
`NoNewPrivileges`, `ProtectHome`, `PrivateTmp`, `ProtectKernel*`,
`ProtectControlGroups`, `RestrictNamespaces`, `RestrictSUIDSGID`,
`RestrictRealtime`, `LockPersonality`, `MemoryDenyWriteExecute`, empty
capability sets, `RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6` and
`SystemCallFilter=@system-service`. The proxy needs only outbound
TCP/UDP and the notify socket, so it loses nothing.
- The unit is `Type=notify`: the binary sends `READY=1` via - The unit is `Type=notify`: the binary sends `READY=1` via
`github.com/coreos/go-systemd` only after the listeners are bound, so `github.com/coreos/go-systemd` only after the listeners are bound, so
`systemctl start` blocks until the proxy accepts connections. A 30 s `systemctl start` blocks until the proxy accepts connections. A 30 s
+84 -10
View File
@@ -2,12 +2,13 @@
// Package service integrates gpu-turnstile with systemd on Linux: running // Package service integrates gpu-turnstile with systemd on Linux: running
// under a unit with readiness notification and watchdog, plus // under a unit with readiness notification and watchdog, plus
// install/remove helpers that manage a system unit. // install/remove helpers that manage a hardened system unit.
package service package service
import ( import (
"context" "context"
"fmt" "fmt"
"io"
"os" "os"
"os/exec" "os/exec"
"os/signal" "os/signal"
@@ -21,6 +22,14 @@ const Name = "gpu-turnstile"
// unitPath is where Install writes the unit file. // unitPath is where Install writes the unit file.
const unitPath = "/etc/systemd/system/" + Name + ".service" const unitPath = "/etc/systemd/system/" + Name + ".service"
// stateDir holds the installed binary (and staged updates); the unit's
// StateDirectory= directive makes systemd own it and grant the dynamic
// user write access. etcConfig is the config file the unit loads.
const (
stateDir = "/var/lib/" + Name
etcConfig = "/etc/" + Name + ".env"
)
// IsService reports whether the process was started by systemd. // IsService reports whether the process was started by systemd.
func IsService() bool { return os.Getenv("INVOCATION_ID") != "" } func IsService() bool { return os.Getenv("INVOCATION_ID") != "" }
@@ -33,10 +42,17 @@ func Run(run func(ctx context.Context) error) error {
return run(ctx) return run(ctx)
} }
// renderUnit builds the systemd unit: Type=notify so systemctl start blocks // renderUnit builds the hardened systemd unit: Type=notify so systemctl
// until the listeners are bound, a 30s watchdog, and restart-on-failure // start blocks until the listeners are bound, a 30s watchdog, and
// with a 5s delay — which is also what brings up a staged update after the // restart-on-failure with a 5s delay — which is also what brings up a
// updater exits with a non-zero code. // staged update after the updater exits with a non-zero code.
//
// Sandboxing mirrors the Windows virtual account: DynamicUser=yes gives
// the service a transient per-service UID with no login and no home, the
// filesystem is read-only except StateDirectory (the install dir, so
// self-updates can rewrite the binary), and the usual no-privilege-escalation
// directives apply. The proxy needs nothing but outbound TCP/UDP and the
// notify socket, so it loses nothing.
func renderUnit(exePath, configPath string) string { func renderUnit(exePath, configPath string) string {
return fmt.Sprintf(`[Unit] return fmt.Sprintf(`[Unit]
Description=gpu-turnstile GPU arbitration proxy for Ollama and ComfyUI Description=gpu-turnstile GPU arbitration proxy for Ollama and ComfyUI
@@ -50,13 +66,55 @@ ExecStart=%q -config %q
Restart=on-failure Restart=on-failure
RestartSec=5s RestartSec=5s
DynamicUser=yes
StateDirectory=%s
ProtectSystem=strict
ProtectHome=yes
PrivateTmp=yes
NoNewPrivileges=yes
ProtectKernelTunables=yes
ProtectKernelModules=yes
ProtectKernelLogs=yes
ProtectControlGroups=yes
ProtectClock=yes
RestrictNamespaces=yes
RestrictSUIDSGID=yes
RestrictRealtime=yes
LockPersonality=yes
MemoryDenyWriteExecute=yes
CapabilityBoundingSet=
AmbientCapabilities=
RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6
SystemCallFilter=@system-service
SystemCallErrorNumber=EPERM
[Install] [Install]
WantedBy=multi-user.target WantedBy=multi-user.target
`, exePath, configPath) `, exePath, configPath, Name)
} }
// Install writes the unit for the current executable and the given config // copyFile copies src to dst, creating dst with the given mode.
// file, then enables and starts it. Needs root. func copyFile(src, dst string, mode os.FileMode) error {
in, err := os.Open(src)
if err != nil {
return err
}
defer in.Close()
out, err := os.OpenFile(dst, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, mode)
if err != nil {
return err
}
defer out.Close()
if _, err := io.Copy(out, in); err != nil {
return err
}
return out.Close()
}
// Install copies the current executable into /var/lib/gpu-turnstile, makes
// sure /etc/gpu-turnstile.env exists (copied from the given config file if
// provided), writes the hardened unit, then enables and starts it. Needs
// root.
func Install(configPath string) error { func Install(configPath string) error {
exe, err := os.Executable() exe, err := os.Executable()
if err != nil { if err != nil {
@@ -65,7 +123,21 @@ func Install(configPath string) error {
if abs, absErr := filepath.Abs(exe); absErr == nil { if abs, absErr := filepath.Abs(exe); absErr == nil {
exe = abs exe = abs
} }
if err := os.WriteFile(unitPath, []byte(renderUnit(exe, configPath)), 0o644); err != nil { if err := os.MkdirAll(stateDir, 0o755); err != nil {
return fmt.Errorf("create %s (run as root): %w", stateDir, err)
}
installedExe := filepath.Join(stateDir, Name)
if exe != installedExe {
if err := copyFile(exe, installedExe, 0o755); err != nil {
return fmt.Errorf("install binary to %s: %w", installedExe, err)
}
}
if _, err := os.Stat(etcConfig); os.IsNotExist(err) && configPath != "" {
// Missing config is not fatal: the service fails fast with a clear
// "no consumer URL" error until the user writes one.
copyFile(configPath, etcConfig, 0o644) //nolint:errcheck // best effort
}
if err := os.WriteFile(unitPath, []byte(renderUnit(installedExe, etcConfig)), 0o644); err != nil {
return fmt.Errorf("write %s (run as root): %w", unitPath, err) return fmt.Errorf("write %s (run as root): %w", unitPath, err)
} }
if out, err := exec.Command("systemctl", "daemon-reload").CombinedOutput(); err != nil { if out, err := exec.Command("systemctl", "daemon-reload").CombinedOutput(); err != nil {
@@ -77,12 +149,14 @@ func Install(configPath string) error {
return nil return nil
} }
// Remove stops and disables the service and deletes the unit file. // Remove stops and disables the service and deletes the unit file and the
// installed binary. The config file in /etc is left in place (user data).
func Remove() error { func Remove() error {
exec.Command("systemctl", "disable", "--now", Name+".service").Run() // ignore: may not exist exec.Command("systemctl", "disable", "--now", Name+".service").Run() // ignore: may not exist
if err := os.Remove(unitPath); err != nil && !os.IsNotExist(err) { if err := os.Remove(unitPath); err != nil && !os.IsNotExist(err) {
return fmt.Errorf("remove %s: %w", unitPath, err) return fmt.Errorf("remove %s: %w", unitPath, err)
} }
os.RemoveAll(stateDir) // installed binary + staged updates; ignore error
if out, err := exec.Command("systemctl", "daemon-reload").CombinedOutput(); err != nil { if out, err := exec.Command("systemctl", "daemon-reload").CombinedOutput(); err != nil {
return fmt.Errorf("systemctl daemon-reload: %w (%s)", err, out) return fmt.Errorf("systemctl daemon-reload: %w (%s)", err, out)
} }
+8 -2
View File
@@ -8,13 +8,19 @@ import (
) )
func TestRenderUnit(t *testing.T) { func TestRenderUnit(t *testing.T) {
unit := renderUnit("/usr/local/bin/gpu-turnstile", "/etc/gpu-turnstile.env") unit := renderUnit("/var/lib/gpu-turnstile/gpu-turnstile", "/etc/gpu-turnstile.env")
for _, want := range []string{ for _, want := range []string{
"Type=notify", "Type=notify",
"WatchdogSec=30s", "WatchdogSec=30s",
`ExecStart="/usr/local/bin/gpu-turnstile" -config "/etc/gpu-turnstile.env"`, `ExecStart="/var/lib/gpu-turnstile/gpu-turnstile" -config "/etc/gpu-turnstile.env"`,
"Restart=on-failure", "Restart=on-failure",
"WantedBy=multi-user.target", "WantedBy=multi-user.target",
"DynamicUser=yes",
"StateDirectory=gpu-turnstile",
"ProtectSystem=strict",
"NoNewPrivileges=yes",
"RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6",
"SystemCallFilter=@system-service",
} { } {
if !strings.Contains(unit, want) { if !strings.Contains(unit, want) {
t.Fatalf("unit missing %q:\n%s", want, unit) t.Fatalf("unit missing %q:\n%s", want, unit)