Skip to content

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

Closed

Description

@scttfrdmn

Summary

The --pre-stop hook runs as root (spored is a root systemd service, and runPreStop does exec.Command("sh", "-c", a.config.PreStop) — pkg/agent/agent.go:1351). It should run as the instance's primary user, matching how --command runs (su - $LOCAL_USERNAME after #61).

Why it matters

A pre-stop hook is the user's workload cleanup (the flag's own example is aws s3 sync /results s3://bucket/). Running it as root means:

  • Wrong $HOME / environment vs. how the workload ran.
  • Files it creates/copies are owned by root, not the user.
  • Privilege mismatch — it can touch things the workload couldn't, and may use different credential resolution than the user's session.

For consistency and least-surprise, pre-stop should execute in the same user context as --command and the interactive shell.

Fix sketch

spored doesn't currently know the instance's local username (no spawn:user tag / config field). So:

  1. Write a spawn:local-username tag at launch (the bootstrap already has $LOCAL_USERNAME).
  2. Read it into the agent config (pkg/provider/ec2.go tag→config mapping, alongside pre-stop/on-complete).
  3. runPreStop runs su - <user> -c '<pre-stop>' when the username is known, falling back to the current behavior if not (older instances without the tag).

Context

Surfaced during #61 Tier 2 work — --command was fixed to run as $LOCAL_USERNAME (it had been hardcoded to a nonexistent local user); pre-stop has the inverse problem (runs as root). Keep them consistent.

Activity

  1. added
    bug
    on Jun 6, 2026
  2. added
    area:spored
    on Jun 12, 2026
  3. scttfrdmn commented on Jun 16, 2026

    @scttfrdmn
    ContributorAuthor

    Severity escalation: this causes silent DATA LOSS, not just ownership/consistency

    Hit this in production (BuckAI/fieldwork SAM inference). A long GPU job wrote ~110k detections to instance-store scratch, with --pre-stop 'aws s3 sync ~/output s3://bucket/...' as the durability safety net. At TTL-terminate, pre-stop ran as root, so ~/output resolved to /root/output (empty) instead of the workload's /home/ec2-user/output. The sync 'succeeded' copying nothing; the instance-store data was wiped on terminate. 48h of g5.12xlarge ($270) and all results lost.

    The flag's own documented example is aws s3 sync /results s3://bucket/ — the canonical use is flushing the workload's output before teardown. Running as root breaks exactly that, silently, whenever the hook uses ~, $HOME, or a relative/login-shell-dependent path. So this isn't just least-surprise — it's a data-durability bug for the flag's primary purpose.

    Suggest re-labeling beyond consistency: pre-stop running in the wrong user context can silently lose the data it exists to save. The fix sketched above (run pre-stop as $LOCAL_USERNAME via the spawn:local-username tag, same as #61 did for --command) is the correct one.

  4. scttfrdmn commented on Jun 16, 2026

    @scttfrdmn
    ContributorAuthor

    Severity bump — this is silent data loss, not just consistency/ownership. Confirmed via #184 (closed as a dup): a real 48h g5.12xlarge SAM run lost 110k detections ($270 compute) because --pre-stop 'aws s3 sync ~/output s3://…' ran as root, so ~ → /root, and it synced the empty /root/output instead of the workload's /home/ec2-user/output. The sync "succeeded" copying nothing — no error, no warning, and the data was on instance-store NVMe (wiped on terminate). That's the worst failure shape: a hook that exists specifically to flush results silently flushes the wrong (empty) directory.

    It hits the flag's own documented example (aws s3 sync ~/results … / /results), so the canonical use case is the broken one.

    Verified the root cause in code matches the fix sketch exactly: sysShellCommand (pkg/agent/sys_linux.go:208) is a bare sh -c <command> with no uid/HOME switch, and spored is a root systemd service → root $HOME=/root. The --command path already solved the inverse problem in #61 with su - $LOCAL_USERNAME, so there's a known-good pattern to mirror.

    (spawn has no priority:* labels, so I can't bump a label — flagging in-thread instead. Adding area:storage since the impact is data persistence.)

    Recommend prioritizing. I can implement the 3-step fix as sketched (write spawn:local-username at launch → read into agent config → su - <user> -c in runPreStop, falling back to current behavior when the tag is absent for older instances). Want me to take it? It pairs naturally with the #184 follow-ups I'd flagged there (a loud "pre-stop synced 0 bytes / is best-effort" signal, and the reaper-bypass gap) — though those are separable.

  5. added
    area:storage
    on Jun 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:sporedarea:storagebug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions