Skip to content

fix(spored): run --pre-stop as the instance user, not root (#63) - #185

Merged
scttfrdmn merged 1 commit into
mainfrom
fix/63-pre-stop-run-as-user
Jun 16, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
fix/63-pre-stop-run-as-user

Conversation

@scttfrdmn

Copy link
Copy Markdown
Contributor

Fixes #63 — silent data loss (confirmed by #184: a real 48h g5.12xlarge run lost ~110k detections / ~$270 because --pre-stop 'aws s3 sync ~/output …' ran as root → ~ = /root → synced an empty dir and "succeeded" copying nothing; the NVMe was then wiped on terminate).

Root cause

spored is a root systemd service, and sysShellCommand was a bare sh -c <hook> — so the hook inherited root's $HOME=/root. Any hook using ~, $HOME, or a relative path silently targeted the wrong location. It hits the flag's own documented example (aws s3 sync ~/results …).

Fix (mirrors the #61 --command pattern, su - $LOCAL_USERNAME)

  1. LaunchConfig gains Username; the launcher tags spawn:local-username — CLI Linux path (cmd/launch.go, only when TargetOS != windows) and the headless pkg/launcher provision path. Windows left unset (single user, no su).
  2. provider.Config gains LocalUsername; pkg/provider/ec2.go maps the spawn:local-username tag.
  3. runPreStop validates the username (security.ValidateUsername — never interpolate an untrusted tag into a shell user) and runs the hook via su - <user> -c when set. Empty / invalid / absent (older instances) → falls back to the legacy root sh -c, so this is non-breaking. sysShellCommand gains the user arg on sys_linux.go + sys_other.go; sys_windows.go ignores it.

Tests

  • TestBuildTags_LocalUsername — tag written when Username set, omitted otherwise.
  • TestSysShellCommand_RunAsUser / _NoUserFallsBackToRoot (linux build tag) — argv is su - <user> -c <hook> vs bare sh -c.
  • Builds clean on linux/darwin/windows; go vet clean all three; full pkg/agent ./pkg/aws ./pkg/provider ./pkg/launcher ./cmd green.

Note: the linux-only sysShellCommand test executes on the orion linux runners (can't run a //go:build linux test binary on the darwin dev box, but it compiles under GOOS=linux go vet).

Fixes #63. (Related #184 follow-ups — a "pre-stop synced 0 bytes" loud signal and the reaper-bypass gap — are separable and not in this PR.)

spored is a root systemd service, so the pre-stop hook ran with root's
environment: ~/$HOME resolved to /root. A hook like
`aws s3 sync ~/output s3://…` synced the empty /root/output and "succeeded"
copying nothing — silent data loss on ephemeral storage (surfaced by #184, a
real 48h GPU run that lost ~110k detections).

Fix mirrors the #61 --command pattern (su - $LOCAL_USERNAME):
- LaunchConfig gains Username; the launcher tags spawn:local-username (CLI Linux
  path + pkg/launcher provision path). Windows is left unset (single user, no su).
- provider.Config gains LocalUsername; ec2.go maps the spawn:local-username tag.
- runPreStop validates the username (security.ValidateUsername — never
  interpolate an untrusted tag into a shell user) and runs the hook via
  `su - <user> -c` when set; empty/invalid/absent (older instances) falls back to
  the legacy root shell. sysShellCommand gains the user arg on linux/other;
  Windows ignores it.

Tests: buildTags writes spawn:local-username (and omits it when unset);
linux sysShellCommand wraps in `su - <user> -c` vs bare `sh -c`. Builds on
linux/darwin/windows; vet clean.

Fixes #63
@scttfrdmn
scttfrdmn merged commit f6081c4 into main Jun 16, 2026
6 checks passed
@codecov

codecov Bot commented Jun 16, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 35.48387% with 20 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
pkg/agent/agent.go 0.00% 12 Missing ⚠️
cmd/launch.go 0.00% 6 Missing ⚠️
pkg/provider/ec2.go 0.00% 2 Missing ⚠️

📢 Thoughts on this report? Let us know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

--pre-stop runs as root, should run as the instance user

1 participant