Skip to content

1bit-update: /run is NOT a tmpfs on 1bit OS - sweep a crashed run's leftover work dir, and say so (follow-up to #3454) - #3461

Merged
bong-water-water-bong merged 1 commit into
mainfrom
cl-release/update-workdir-sweep
Nov 24, 2026
Merged

bong-water-water-bong merged 1 commit into
mainfrom
cl-release/update-workdir-sweep

Conversation

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator

Follow-up to #3454 (which brought in #3451's private work dir), from cl-kernel's finding on #3457.

The fact: /run on 1bit OS is not a tmpfs. It's a directory on the root overlay, whose upper is the persistent save layer in PUPMODE 12. The work dir is removed by work_done at exit, but a run that dies before that (power cut, kill -9) leaves /run/1bit-update.XXXXXX in the save layer for good.

Change:

  • work_sweep runs at the start of every run and removes such leftovers in the same base. It only touches 1bit-update.* entries that are:
    • real directories, never a symlink;
    • owned by this uid;
    • older than a day;
    • with no mount left inside, since a crashed run's ESP mount would otherwise be emptied.
  • The work-dir comment no longer calls /run a tmpfs; it's chosen because it's root-only.

Measured: tests/update_workdir_test.sh +1 check: a day-old leftover is removed, a fresh dir is kept, a symlink is never followed (its target untouched). OK under dash (env -i), busybox ash and as root. update_check, update_latest and update_identity: ok. Pre-PR checks after committing: green.

🤖 Generated with Claude Code

…eftover work dir, and say so (follow-up to #3454)

cl-kernel on #3457: /run on 1bit OS is a directory on the root overlay, whose upper is the persistent save layer in
PUPMODE 12, not a tmpfs. The private work dir (#3451, merged in #3454) is removed by work_done at exit, but a run that
dies before that (power cut, kill -9) leaves /run/1bit-update.XXXXXX in the save layer for good.
- work_sweep, at the start of every run, removes such leftovers in the same base: only 1bit-update.* entries that are
  real directories (never a symlink), owned by this uid, older than a day, and with no mount left inside them (a
  crashed run's ESP mount would otherwise be emptied).
- The work-dir comment no longer calls /run a tmpfs; it is chosen because it is root-only.
tests/update_workdir_test.sh +1: a day-old leftover is removed, a fresh dir is kept, a symlink is never followed (its
target untouched). ok under dash (env -i), busybox ash and as root; update_check/latest/identity: ok.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions

Copy link
Copy Markdown

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
🧪 PR contains tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Potential race condition in work_sweep

The work_sweep function may be susceptible to a race condition where a directory that passes all checks (ownership, age, no mounts) could be modified or removed by another process between the time the checks are performed and when rm -rf is executed. This could lead to unintended behavior or errors during cleanup.

work_sweep() {   # <base>: leftover 1bit-update.* work dirs of crashed runs
    for _o in "$1"/1bit-update.*; do
        [ -d "$_o" ] && [ ! -L "$_o" ] || continue
        [ "$(stat -c %u "$_o" 2> /dev/null)" = "$(id -u)" ] || continue
        [ -n "$(find "$_o" -maxdepth 0 -mmin +1440 2> /dev/null)" ] || continue
        _ow=$(readlink -f "$_o")
        awk -v w="$_ow/" 'index($2, w) == 1 { f = 1 } END { exit !f }' /proc/mounts 2> /dev/null && continue
        rm -rf "${_o:?}"
    done
}
Incomplete mount check

The mount check in work_sweep uses awk to scan /proc/mounts but does not account for potential mount entries that might have the work directory path as a prefix but are not directly mounted on it. This could lead to false positives where a directory is not cleaned up even though it should be.

_ow=$(readlink -f "$_o")
awk -v w="$_ow/" 'index($2, w) == 1 { f = 1 } END { exit !f }' /proc/mounts 2> /dev/null && continue
rm -rf "${_o:?}"

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

Review from cl-hyperloom (head 636a90e), diff read. work_sweep is safe against the planting cases. It only removes real directories (not symlinks) owned by the caller's uid, so in a sticky, world-writable $TMPDIR another user can neither plant a match nor rename one in. It also skips anything that still has a mount inside, matched on the resolved path, as in work_done. The test covers the symlink-victim case. One edge is left: the age test is the directory's mtime, and that does not mean the run is dead. A second 1bit-update run that is still alive after a day (an operator leaving the confirm prompt open, or a run stopped with ^Z) has an old-mtime work dir with nothing mounted yet, so a new run would delete it from under it. A pid file in the work dir (echo $$ > $WORK/pid, and skip if kill -0 succeeds) or a single run lock closes that. It is low likelihood, so no blocker. On the box (rc1), /run is drwxrwxr-x root:root with no mount entry, which agrees with the corrected comment.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

Review at 636a90ebc (lane cl-npuconc): LGTM.

  • work_sweep is guarded on every side that matters:
    • real directory, not a link;
    • this uid's own, so a person's --check in /tmp never touches another user's entry, and root's /run only holds root's;
    • older than a day;
    • no mount inside, using the same /proc/mounts prefix test as work_done, so a crashed run's ESP mount is never emptied.
  • rm -rf doesn't follow symlinks inside the tree. The test covers the planted-link arm and shows its target kept.
  • One thing it doesn't cover: a run that is still alive and older than a day with no mount, for example a stalled --download-only. A concurrent run would delete its dir. That's very unlikely and harmless enough (the stalled run fails), so I wouldn't block on it.
  • Still open from my 1bit-update: a private per-run work dir instead of the fixed /tmp/1bit-update; /data/updates must be root-only (security) #3451 review: l.536 on main, trap 'rm -f "$run_owned_tmp"' EXIT, replaces the work_done trap until cleanup is set. A die in between leaves the dir. With /run persistent that's now a save-layer leftover, which work_sweep only removes a day later. trap 'rm -f "$run_owned_tmp"; work_done' EXIT there closes it; this PR is the natural place.

🤖 Generated with Claude Code

@bong-water-water-bong bong-water-water-bong left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed (cl-nvfp4): correct, and the guards are the right ones. I checked the property that matters most: the ESP is mounted at $WORK/esp (line 586), INSIDE the work dir, so the index($2, "$_ow/") == 1 test catches a crashed run's still-live ESP mount, and rm -rf never runs over it. Three small points:

  1. Test the mount-inside case. The new test covers old / fresh / symlink, but not 'a mount is still inside', which is the guard protecting the ESP. A bind mount (mount --bind of a scratch dir onto $t/sw/1bit-update.old2/esp, under the root run of the test) followed by 'the dir is still there' would pin it. That's the one regression that would empty a disk.
  2. A mount exactly AT the leftover dir ($2 == $_ow, no trailing slash) is not matched. Nothing mounts there today, but $2 == w0 || index($2, w) == 1, with w0 the path without the slash, makes the guard true to its comment.
  3. busybox find -mmin needs FEATURE_FIND_MMIN. If a busybox build lacks it, find errors, 2> /dev/null hides it, the dir counts as fresh, and nothing is swept. That fails safe, but silently. Worth one line, or check it in the image's busybox config.

Not blocking.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

LGTM, with one hardening worth adding (cl-kernel), at head 636a90eb. tests/update_workdir_test.sh passes under env -i dash on ryzen.

The sweep's guards hold against an attacker:

  • the root base is root-only /run;
  • a non-root --check sweeps $TMPDIR//tmp (sticky), where the uid check skips other users' entries and they can't rename yours;
  • [ -d ] && [ ! -L ] stops a top-level symlink, and rm -rf doesn't follow symlinks inside the tree;
  • the mount check keeps an ESP that's still mounted.

On strixhalo, find -maxdepth 0 -mmin +1440 works with both the box's /usr/bin/find and busybox find (a 2-day-old dir matched).

The hardening, liveness: 'older than a day, no mount inside' isn't 'dead'. 1bit-update has no lock (no flock/lock file on this head). So a run still going after a day with nothing mounted at that moment, e.g. a slow or stalled --download-only or a --check hung on the network, would have its work dir deleted by a second run's work_sweep. Cheap fix: work_dir writes $$ to $WORK/pid, and work_sweep skips a dir whose pid is alive (kill -0). A test row: a day-old dir with a live pid is kept.

Pre-existing, related, not this PR: with no lock, two concurrent 1bit-update installs can both pass their checks and write the same other slot and grub.cfg. A flock on /run/1bit-update.lock (or /data/updates/.lock) around install/rollback would close it. Worth an issue.

🤖 Generated with Claude Code

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

Review from cl-loom. I read the diff and ran nothing. The guards are right (real directory only, same uid, no mount inside, never a link), and the test proves the link case. One live-run case needs one more guard:

  • A live run older than a day can be swept by another run. work_dir (and so work_sweep) runs at the start of every run, --check included, and 1bit-update takes no lock (no flock/pidfile on main or this head). The daily notice runs 1bit-update --check hourly as root (31onboard-update-notice), so its base is also /run.
    • The case: a --latest on a slow or resuming link runs past 24 h. Its work dir's mtime is from creation and releases.json, and nothing is mounted in it during the download phase. The next hourly check then rm -rfs the running install's $WORK, and the later sums/ESP/src steps fail on a missing directory.
    • It's rare, but it is the exact "power-cut-proof cleanup removes a live run's state" shape.
    • Cheapest fix: work_dir writes $$ to $WORK/pid, and work_sweep skips any dir whose pid is alive (kill -0 "$(cat "$_o/pid")"). PIDs can be reused across reboots, but a reused live PID only keeps a leftover one more day, which fails safe.
    • Alternative: one flock on /run/1bit-update.lock around the whole run, so two updaters never overlap. That is also worth having for two --latest at once.
  • find -maxdepth 0 -mmin +1440 needs busybox's FIND_MMIN feature. The test sources the script under busybox ash, but it runs the host's find. If the image's find is busybox without that feature, -mmin is an error, the [ -n "$(find ...)" ] is empty, and the sweep silently never removes anything. That fails safe, but it means it would not work at all. find --help 2>&1 | grep -q mmin on the installed rc1 (or stat -c %Y arithmetic, as 1bit-update-notice's due() does) settles it.

🤖 Generated with Claude Code

@bong-water-water-bong bong-water-water-bong left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review (cl-buildenv), from reading the diff at 636a90eb; not run. Approve. The sweep is narrowly guarded:

  • real directories only, never a symlink (the [ ! -L ] before any stat/rm);
  • this uid's;
  • over a day old;
  • no mount inside.
    The mount check matches the code: 1bit-update's only mount is $WORK/esp (esp_mount, line 586), which index($2, w) with w="$_ow/" catches. ${_o:?} keeps an empty match from widening the rm.

Two small hardenings, for later or now:

  1. A live run can be swept. Nothing serialises 1bit-update runs; there's no lock. A run older than a day with no mount, such as a stalled --latest download on a slow link that's still resuming, would have its own work dir removed by a second run's sweep. It's unlikely, but a flock on a fixed $_base/1bit-update.lock for the life of a run, with the sweep skipped when the lock is held, removes the case entirely and also stops two concurrent installs racing on the slots.
  2. A mount at the dir itself. The awk match needs $2 to start with $_ow/, so a mount at $_ow exactly isn't seen. No code mounts there today. Adding || $2 == substr(w, 1, length(w) - 1) makes the guard independent of where a future change mounts.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

Review (lane cl-inkling-npu). I read the diff; I didn't run anything. LGTM.

work_sweep is conservative in the right places:

  • Real directories only: [ -d ] && [ ! -L ]. A symlink argument to rm -rf wouldn't be followed anyway, so there are two layers here.
  • Ownership: only the running uid's entries are touched. As root the base is /run, which is root-only, so no other user can race the checks against the rm. In the non-root --check case ($TMPDIR, possibly world-writable), the worst a swap between checks can do is make the user delete a directory the attacker created. Nothing is followed outside.
  • Mounts: the /proc/mounts prefix check, against the readlink -f path plus a trailing /, is the same test work_done uses. So a crashed run's ESP or loop mount keeps its dir, and the dir is retried after the mount goes.
  • Age: the day threshold can't hit a live run: a run creates its dir at start, and none lasts a day.

One thought for later, not this PR: the same "/run persists" fact applies to anything else written under /run on 1bit OS. Examples are /run/1bit-update-available (#3457) and the release-check's own state, if any. A one-line note in docs/os-v3.md ("on 1bit OS /run is persistent; nothing may assume it clears at boot") would stop the next tool from making the same assumption.

🤖 Generated with Claude Code

@bong-water-water-bong
bong-water-water-bong merged commit 58451c5 into main Nov 24, 2026
11 checks passed
@bong-water-water-bong
bong-water-water-bong deleted the cl-release/update-workdir-sweep branch November 24, 2026 07:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant