Skip to content

heavy: minors from the #764 reviews (#767) - #770

Merged
scuttletoby merged 2 commits into
mainfrom
heavy-767-minors
Nov 21, 2026
Merged

scuttletoby merged 2 commits into
mainfrom
heavy-767-minors

Conversation

@scuttletoby

Copy link
Copy Markdown
Owner

What and why

Fixes the minors and the small nits from the two #764 reviews, at the root.

  1. No folders through a HOME anyone can write (lens A minor). As root with HOME=/tmp (root's, but 1777), another user could plant /tmp/.local as a link first, and root would make empty root-owned folders where it points.
    • state_base (in bin/heavy and lib/heavy_sweep.py, the same rule) now also requires a home that no group or other user can write, and not / (lens B nit 1). So HOME=/tmp and HOME=/ fall through to the passwd home.
    • Before making ~/.local/state, heavy makes ~/.local with mkdir and checks it with lstat: a real folder this user owns. A planted .local link is never followed, even in a home only root can write.
  2. Timeout check (lens B minor). It now also catches iter(p.stdout.readline, '') (any uncalled read of a pipe), enumerate/map/zip/filter/''.join and similar over a pipe, import subprocess as sp, getoutput/getstatusoutput (no timeout at all), os.waitpid/os.wait4 without WNOHANG, and a bound wait/communicate passed on (addCleanup(p.wait)). The claim is narrowed: the check promises the forms in its sample table (test_the_check_finds_each_listed_form), not every way Python can wait. The two existing os.waitpid uses now go through a new wait_pid helper with a time limit.
  3. Note cleanup checks the parent folders (both lenses). prune_notes now requires labelled/, heavy/ and the base each to be a real folder of this user's that no one else can write, like heavy's own folder rule.
  4. Nits.
    • Without pidfds and cgroup.kill, heavy still looks for 1 s (SETTLE) after a step that sent nothing, so processes that systemd's failed stop already signalled are not named "still running" while they end. Still no 30 s grace wait.
    • README: how to give back a root-owned ~/.local/state an older heavy left (sudo chown -R "$USER:" ~/.local/state).
    • A test pins lstat in the note cleanup (lens B nit 3): a note that links to an old empty file stays.

Lens B nits 2 (a dry run does not count notes) and 4 (the HEAVY_KEEP absence check) are left out to keep this small.

Tests

Run on this branch (each under the machine's capacity gate):

  • heavy.test.py Parts + Hygiene: 13 OK
  • sweep.test.py: 82 OK, 1 skipped (includes RedCheck with two new rules)
  • install.test.py 16 OK, queue.test.py 70 OK, measure.test.py 47 OK
  • The real-systemd heavy.test.py run (HEAVY_REAL_TESTS=1) is left to CI.

Red on the old code. The new tests against origin/main's bin/heavy and lib/heavy_sweep.py:

  • the state-folder test fails (1777 home used)
  • the settle test fails ([pid] named as still running)
  • the new note-cleanup test fails (a note behind a linked base is removed)
  • the old timeout check gets 15 of the 51 sample forms wrong

Each fix line goes red when removed (each in a scratch copy, named test run):

Removed or changed Test that goes red
not st.st_mode & 0o022 on the home (heavy) test_the_state_folder_is_made_only_in_a_home_this_user_owns
the same (sweep) same
realpath(home) != '/' (heavy) same
the same (sweep) same
the .local lstat check in own_folder same (mocked root, planted link)
SETTLE = 1 to 0 test_processes_that_end_a_moment_later_are_not_named_still_running
each new check rule (drains, join, uncalled read, import as, getoutput, waitpid, bound wait) test_the_check_finds_each_listed_form
wait_pid back to os.waitpid(pid, 0) test_every_wait_for_a_test_process_has_a_timeout
prune parent folders, prune lstat RedCheck MUTATIONS (automatic)

Root is only mocked (getuid/geteuid 0, and this user's files read as root's); nothing runs as root.

State question.

  • Runs again: the state folder is reused; a second sweep removes nothing more.
  • Stopped half-way: a .local made but no state yet is checked and used next time; a note behind a linked or open parent stays until the folder is fixed.
  • Old, empty or missing state: a missing home folder, a 1777 home, /, a planted .local link and a root-owned ~/.local/state from an older heavy (skipped; README says how to fix) are each covered.

fork-seam-check.cjs --config-ref origin/main origin/main HEAD passes; vp fmt --check on the README passes.

General vs project

All general: fork-kit/heavy has no project facts, and nothing here is project-specific.

Closes #767

🤖 Generated with Claude Code

- As root, a HOME others can write to (/tmp) or / is not used for the
  state folder; ~/.local must be a real folder of this user's before
  heavy makes ~/.local/state under it (a planted link is never followed).
- The timeout check covers iter(pipe.readline), enumerate/map/zip/
  filter/join over a pipe, `import subprocess as sp`, getoutput,
  os.waitpid/wait4 without WNOHANG and a bound wait/communicate passed
  on; its claim is narrowed to the listed forms. The two os.waitpid
  uses in the tests now go through a timed wait_pid.
- Note cleanup checks labelled/, heavy/ and the base, like heavy's own
  folder rule.
- With nothing sent by hand, a 1 s look instead of none, so processes
  that end anyway are not named as still running.
- README: how to give back a root-owned ~/.local/state.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@scuttletoby

Copy link
Copy Markdown
Owner Author

Lens A (safety and bypass), head c2e1eff: SAFE

Verdict: SAFE. Nothing blocking and nothing major. I found two minors and two nits. Each one fails closed or needs root to be set up oddly, and none is new in this PR.

What I ran (temp worktree, each run gated on t3-capacity 1536 and run under heavy)

  • sweep.test.py: 82 OK, 1 skipped. heavy.test.py (Parts + Hygiene): 68 run, 55 real-manager tests skipped by design, OK.
  • I probed state_base() (both copies) and own_folder(..., make_base=True) in-process, in throwaway homes. Nothing ran as root.

HOME and folder making (probe results)

  • Homes that are rejected and fall back to the passwd home, in both copies: a 1777 folder (the /tmp case), HOME=/, and a symlink to /. HOME='' also falls back.
  • Homes that are kept: a symlink to a real 0700 home, and <home>/../home. That is right, because stat follows the link and realpath catches /.
  • sudo -E: the user's home is not root's, so it is skipped and /root is used. This is unchanged from heavy: minors from the #760 reviews #764.
  • own_folder cases:
    • .local a symlink: returns '' and makes nothing at the link's target.
    • .local a dangling symlink: mkdir gets EEXIST and does not follow it, then lstat refuses it.
    • .local a regular file, or .local/state a symlink: returns ''.
    • Fresh home: makes all four folders, each 0700.
  • Check-then-use (lstat, then mkdir/listdir): home must now be owned by this user and not writable by others, and .local, state and heavy/ must be real folders owned by this user. So for a normal setup, only this user or root can swap a folder between the lstat and the next mkdir. The mkdir of a folder that exists as a link never follows it. Minor 1 and nit 2 are the two leftover gaps.

Note cleanup parents (lib/heavy_sweep.py:949-951)

  • labelled/, heavy/ and the base are each checked with lstat for a real folder, owner and no group/other write, before listdir and unlink. The unlink removes only a regular, empty, old heavy-*.scope file, and lstat means a link is never followed.
  • .local itself is not checked here (heavy checks it). Only the same user can change it, so this has no impact (nit 1).

SETTLE (bin/heavy:348)

  • After a step that sent nothing, heavy only re-reads cgroup.procs for up to 1 s. Signals still go only through pidfd plus in_cgroup (signal_in), or through cgroup.kill. The pids stop_scope returns are only printed (bin/heavy:640, :1015, :1041). The os.kill(child.pid) after it targets heavy's own unreaped child, so that pid cannot be reused yet.
  • So a wrong or reused pid is never signalled. The only cost is at most about 2 s more on a failed stop without pidfds.

README chown advice

  • sudo chown -R "$USER:" ~/.local/state is safe:
    • ~ and $USER are expanded by the user's own shell.
    • GNU chown -R defaults to -P with no dereference, so neither a top-level link nor a link inside the tree is followed.
    • The folder was root-only (0700), so no one else could have planted hard links or links in it.
  • It is incomplete, though (minor 2).

State question

  • Runs again: the folders are reused (the half_way probe, with .local/state present and no heavy/, finished the chain).
  • Killed half-way: with .local made and state not, the next run gets EEXIST, lstat passes, and it goes on.
  • Old root-owned ~/.local/state: own_folder returns '', which fails closed.
  • Empty or missing HOME: the passwd home is used, and with no passwd entry heavy uses no second folder.
  • Upgrade from the current install: existing user-owned folders are reused.
    • Users whose ~/.local is a symlink (dotfile setups) lose the ~/.local/state fallback. /run/user still works.
    • docker-label removes a note it wrote to a folder outside HEAVY_MARKS (dev:ino), so nothing is lost. That fails closed. It is worth one README word if it matters.

Blocking

None.

Major

None.

Minor

  1. bin/heavy:400-402: the mode of .local is not checked, only its type and owner.
    • Probe: a 0777 .local owned by this user is accepted, and the folders are made under it.
    • Scenario: another user can rename state or heavy and put a symlink in its place between lstat(state) and mkdir(state/heavy). heavy (root, if root's .local were 0777) would then make its folders at the link's target, and the lstat of the new folder passes, because it follows the swapped parent.
    • Not new: main did not check .local at all. It needs a world- or group-writable .local.
    • Fix: add st.st_mode & 0o022 to that check, as the other three levels have. Or, to close every swap race, walk with O_DIRECTORY|O_NOFOLLOW fds and mkdir/fstat through dir_fd. One caveat: on a umask-002 system, a .local that some other tool made is 0775 and would then be refused (this fails closed).
  2. README.md:63-64: the chown advice covers only ~/.local/state.
    • The heavy: fix the #727 review minors; note why the queue test failed #760 heavy (git show 60d0a72e7b:fork-kit/heavy/bin/heavy, line 385) called os.makedirs(base) as root with the user's HOME. When ~/.local was missing, it made ~/.local root-owned 0700 too.
    • In that case the new heavy refuses .local ('' , fails closed). The user's own tools that write to ~/.local (pip --user, XDG data) are also broken, and the README line does not fix it.
    • Fix: add "and, if ~/.local itself is root's, sudo chown "$USER:" ~/.local" (no -R needed).

Nits

  1. lib/heavy_sweep.py:949-951: the prune checks the base but not .local, while heavy's own_folder does. The README now says "the same rule". Only the same user can link .local, so there is no impact. Adding dirname(base) with the type and owner check would make the two rules match.
  2. bin/heavy:443, lib/heavy_sweep.py:1249: os.stat(home) checks the home path once, and the later mkdirs resolve it again. A HOME that is a symlink, or that has an ancestor another user can write, can be swapped in between.
    • A symlink planted in /tmp by another user is blocked by fs.protected_symlinks=1 (it is 1 on this host and is the default on most distros). The other case needs root to pick such a HOME on purpose.
    • Using realpath(home) for the returned base (or the fd walk in minor 1) would narrow it.

Read-only review: nothing pushed, installed or run as root; temp worktree removed.

@scuttletoby

Copy link
Copy Markdown
Owner Author

Lens B (correctness and tests), head c2e1eff: SAFE

Verdict: SAFE. Nothing blocking and nothing major. All three items and the nits are fixed as the body says. Each fix line I removed made its test go red. The findings below are small gaps around ~/.local and in the wording.

What I ran

All runs were in a temporary worktree, each gated on t3-capacity 1536 and run under heavy. Nothing ran as root, and nothing was installed.

  • heavy.test.py Parts and Hygiene: 13 OK. sweep.test.py: 82 OK, 1 skipped (includes RedCheck with the two new mutations).
  • New tests on the old code (origin/main's bin/heavy and lib/heavy_sweep.py, the rest unchanged):
    • the state-folder test is red (the 1777 home is used)
    • the settle test is red ([4194304] is named as still running)
    • the new sweep test is red (the note behind a linked base is removed)
  • Fix lines removed, each in its own scratch copy of fork-kit/, plus one unchanged control copy that passed:
Removed or changed Result
sweep state_base: not st.st_mode & 0o022 red (state-folder test, the 1777 row)
heavy own_folder: the .local lstat check (if False:) red ('.../root-home/.local/state/heavy/labelled' != '')
heavy state_base: realpath(home) != '/' red (the HOME=/ root row)
SETTLE = 1 to 0 red (settle test)

The two copies of the folder rule

  • state_base: bin/heavy:425-449 and lib/heavy_sweep.py:1236-1255 do the same thing. Diffed, the only difference is try/except KeyError against contextlib.suppress(KeyError). A probe gave the same answer from both for every home below.
  • The folder levels: labelled/, heavy/ and the base are checked the same way by own_folder, note_in and prune_notes (lstat, a folder, this uid, & 0o022 clear). The one difference is at ~/.local (nit 1).

No false refusal for a normal user? (probed with bin/heavy loaded in-process, folders in a temporary directory)

HOME Second notes folder
a 0755 home yes
HOME a symlink to a 0755 home yes (os.stat follows it; the realpath is not /)
a 2755 (setgid) home yes
<home>/./ yes
a 0770 home (group can write) no: falls back to the passwd home, as designed
~/.local a symlink to the user's own folder (stow, HPC scratch) no (minor 1)
an existing ~/.local/state that is 0775 (umask 002) no (not new: main does the same)

NFS or odd owners: a home owned by someone else, such as nobody under idmap, is skipped the same way as before. That is not new.

Timeout check

  • The table: every sample form passes, and today's files have no findings. measure.test.py and fake_tools.py are also clean, although Hygiene does not read them (nit 3).
  • Named forms written differently, all caught:
    • '\n'.join(l for l in p.stdout)
    • iter(p.stderr.readline, b'')
    • import subprocess as sp; sp.getstatusoutput(...)
    • import subprocess as s2; addCleanup(s2.run, [...])
    • for i, l in enumerate(p.stdout)
    • os.wait4(pid, 0)
    • addCleanup(proc.communicate, b'x')
    • os.wait() (caught by the wait() rule)
  • The claim: now narrowed to the table, which holds. Forms outside the table still get through: with subprocess.Popen(...) as q: (its exit waits with no time limit), from os import waitpid, import os as o, os.wait3, os.waitid, p.stdout.buffer.read(), read1, readinto, os.read(p.stdout.fileno(), n), shutil.copyfileobj(p.stdout, f), and out = p.stdout; out.read(). None of them is in today's files. One false alarm is possible: executor.submit(ev.wait) is flagged.

SETTLE

  • Bounded: it runs only after a step that sent nothing, so there are at most 2 steps (TERM and KILL), each at most 1 s plus one 50 ms poll. left is re-read on each poll, so the returned list is the latest one, and an empty list stops at once. When pidfds are missing but cgroup.kill is there, the KILL now comes 1 s later than before. That is harmless.

State question

  • Runs again: .local, the base and heavy/labelled are reused (FileExistsError is passed over, then lstat-checked). A second sweep removes nothing more (tested).
  • Stopped half-way: a .local made with no state yet is checked again and finished on the next run. A note behind a linked or open parent stays (tested).
  • Old, empty or missing state: a missing home, a 1777 home, /, a planted .local link and a root-owned ~/.local/state are each covered and fail closed. Two gaps: the README fix misses an old root-owned ~/.local (minor 2), and ~/.local is now made 0700 where makedirs made it 0755 or 0775. That is harmless.

Blocking

None.

Major

None.

Minor

  1. fork-kit/heavy/bin/heavy:395-402 (own_folder, make_base): a symlinked ~/.local is refused, even one that points at the user's own folder.
    • Who it hits: ~/.local -> /scratch/$USER/.local (HPC homes with small quotas) and stow-style dotfiles are common. Those users silently get no second notes folder. That costs nothing while /run/user works, but it removes the fallback this folder exists for.
    • Why the check adds little now: a planted link needs a home that someone else can write, and state_base now refuses such a home.
    • Suggested fix: follow the link, then require the target to be a folder this user owns that no one else can write. That still blocks root following a link into a victim's or a root-owned folder, because the owner or mode check fails there. Or keep the refusal but name it in heavy doctor or docker-label's reason.
    • Documented: the README says "~/.local must be a real folder (not a link)", so this is a design choice to confirm, not a bug.
  2. fork-kit/heavy/README.md:64: the repair line fixes only ~/.local/state.
    • Why it falls short: an older heavy (os.makedirs(base) as root with a user's HOME) also made ~/.local root-owned when it was missing. The new .local check then refuses it, and the user cannot make ~/.local/share either.
    • Suggested fix: sudo chown "$USER:" ~/.local && sudo chown -R "$USER:" ~/.local/state. The chown on .local is not recursive, and it is a no-op when .local is already the user's.

Nits

  1. One rule, two copies, at ~/.local: heavy (bin/heavy:401) checks .local (a real folder of this uid, with no mode check), while note_in (lib/heavy_sweep.py:1263) and prune_notes (:950) do not check it at all. The effect is harmless: docker-label's note there is rejected by the HEAVY_MARKS dev:ino check, and the prune touches only same-user files. But a 0777 ~/.local passes heavy's check, while every other level requires & 0o022 clear. Add the mode check, so all levels use the same rule.
  2. README:72 says "a 1 s look": it is up to 1 s per step, so 2 s in all when neither pidfds nor cgroup.kill is there.
  3. tests/heavy.test.py:1380 (Hygiene.FILES, not new): it omits measure.test.py and fake_tools.py. Both are clean today. A glob over tests/*.py would keep a new test file from being missed.
  4. untimed docstring (tests/heavy.test.py:1310-1318): "any read of a process's stdout or stderr" is wider than the rules. .buffer.read(), read1, readinto and os.read(fileno) get through. Say "read/readline/readlines of" instead.
  5. The HOME=/ root row (tests/heavy.test.py state-folder test): under real root (CI), a regression would make /.local/state/heavy/labelled on the runner before the test fails. Mocking os.mkdir for the / row, or using STATE_ROOT there, would keep a failing run clean.

Read-only review: nothing pushed or installed, and nothing run as root. The temporary worktree and scratch copies were removed.

@scuttletoby
scuttletoby merged commit 2ccce20 into main Nov 21, 2026
30 checks passed
scuttletoby added a commit that referenced this pull request Nov 21, 2026
- As root, a HOME others can write to (/tmp) or / is not used for the
  state folder; ~/.local must be a real folder of this user's before
  heavy makes ~/.local/state under it (a planted link is never followed).
- The timeout check covers iter(pipe.readline), enumerate/map/zip/
  filter/join over a pipe, `import subprocess as sp`, getoutput,
  os.waitpid/wait4 without WNOHANG and a bound wait/communicate passed
  on; its claim is narrowed to the listed forms. The two os.waitpid
  uses in the tests now go through a timed wait_pid.
- Note cleanup checks labelled/, heavy/ and the base, like heavy's own
  folder rule.
- With nothing sent by hand, a 1 s look instead of none, so processes
  that end anyway are not named as still running.
- README: how to give back a root-owned ~/.local/state.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
scuttletoby added a commit that referenced this pull request Nov 21, 2026
- As root, a HOME others can write to (/tmp) or / is not used for the
  state folder; ~/.local must be a real folder of this user's before
  heavy makes ~/.local/state under it (a planted link is never followed).
- The timeout check covers iter(pipe.readline), enumerate/map/zip/
  filter/join over a pipe, `import subprocess as sp`, getoutput,
  os.waitpid/wait4 without WNOHANG and a bound wait/communicate passed
  on; its claim is narrowed to the listed forms. The two os.waitpid
  uses in the tests now go through a timed wait_pid.
- Note cleanup checks labelled/, heavy/ and the base, like heavy's own
  folder rule.
- With nothing sent by hand, a 1 s look instead of none, so processes
  that end anyway are not named as still running.
- README: how to give back a root-owned ~/.local/state.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
scuttletoby added a commit that referenced this pull request Nov 21, 2026
- As root, a HOME others can write to (/tmp) or / is not used for the
  state folder; ~/.local must be a real folder of this user's before
  heavy makes ~/.local/state under it (a planted link is never followed).
- The timeout check covers iter(pipe.readline), enumerate/map/zip/
  filter/join over a pipe, `import subprocess as sp`, getoutput,
  os.waitpid/wait4 without WNOHANG and a bound wait/communicate passed
  on; its claim is narrowed to the listed forms. The two os.waitpid
  uses in the tests now go through a timed wait_pid.
- Note cleanup checks labelled/, heavy/ and the base, like heavy's own
  folder rule.
- With nothing sent by hand, a 1 s look instead of none, so processes
  that end anyway are not named as still running.
- README: how to give back a root-owned ~/.local/state.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
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.

heavy: minors from the #764 reviews (world-writable root HOME, timeout-check forms, note cleanup parents)

1 participant