From 452781d9d1851044eac1ade5377cf1b1a97f47dd Mon Sep 17 00:00:00 2001 From: scuttletoby <123843277+scuttletoby@users.noreply.github.com> Date: Fri, 20 Nov 2026 01:54:38 +0000 Subject: [PATCH 1/4] fork-ci: git-clean wipe stays in the workspace under a concurrent change (#428) - empty(): going up by `..` must land in the recorded parent (dev, ino); a folder moved out mid-wipe is emptied and the walk stops red there. - A read-only folder is chmod-ed with AT_SYMLINK_NOFOLLOW, never through a symlink swapped in for it. - A symlinked work folder (_work, above RUNNER_WORKSPACE) is refused. - Another file system mounted inside the workspace is not entered. - Each folder is listed once (was re-listed after each child). - README and docstring say what is true now; the folders above _work are named under "Not covered". - Tests: race tests (move out, symlink swap before open and before chmod), symlinked _work, mount point, one listing per folder; run_cli timeout so a looping wipe fails instead of hanging. - weakened-tests refusal note no longer claims "another head"; the head gate's ls-remote sets GIT_ATTR_NOSYSTEM; pr_queue comment reflowed. Fixes #428 Co-Authored-By: Claude Opus 5.5 --- fork-ci/RELEASE-NOTES.md | 12 ++ fork-ci/actions/git-clean/README.md | 23 ++-- fork-ci/actions/git-clean/git_clean.py | 83 +++++++++---- fork-ci/actions/git-clean/git_clean.test.py | 114 +++++++++++++++++- fork-ci/actions/head-check/head_gate.py | 3 +- fork-ci/actions/head-check/head_gate.test.py | 1 + fork-ci/actions/pr-queue/pr_queue.py | 4 +- .../actions/weakened-tests/weakened_tests.py | 2 +- .../weakened-tests/weakened_tests.test.py | 8 ++ 9 files changed, 210 insertions(+), 40 deletions(-) diff --git a/fork-ci/RELEASE-NOTES.md b/fork-ci/RELEASE-NOTES.md index 5e2d24ed30eed..6b377e194bcfb 100644 --- a/fork-ci/RELEASE-NOTES.md +++ b/fork-ci/RELEASE-NOTES.md @@ -13,6 +13,18 @@ nothing. ## After ci/v14 +### git-clean: the wipe stays in the workspace while another process changes it (#428) + +- Going back up by `..` must land in the folder the wipe came from (device and inode): a folder moved out of the + workspace while the wipe is inside it is emptied, and the step stops red there instead of emptying where it landed. +- A read-only folder is made writable without following a symlink swapped in for it (`fchmodat` with + `AT_SYMLINK_NOFOLLOW`); a work folder (`_work`, above `RUNNER_WORKSPACE`) that is a symlink is refused; another + file system mounted inside the workspace is not entered (the step fails with its path). +- Each folder is listed once (a wide folder was re-listed after each child: 6,000 folders took 20 s). +- weakened-tests: the refusal note no longer says the earlier report was for another head (wrong on a re-run). + +**Upgrade:** nothing. A mount inside a self-hosted workspace now fails the step until it is unmounted. + ### pr-queue: a person's auto-merge switch during an update stands (#518) - The queue reads the pull request right before turning auto-merge off: one that is off by then (or closed) is left diff --git a/fork-ci/actions/git-clean/README.md b/fork-ci/actions/git-clean/README.md index 8fe5838588040..d3cd7be2c5427 100644 --- a/fork-ci/actions/git-clean/README.md +++ b/fork-ci/actions/git-clean/README.md @@ -7,9 +7,13 @@ workspace, and an earlier job may have run pull request code there. That code ca of its own steps runs pull request code. - **Self-hosted runner:** it empties the workspace (`GITHUB_WORKSPACE`), so the checkout starts from nothing, as on a - GitHub-hosted runner. Symlinks are removed as links, never followed, and the workspace's real path must be its own - path below the runner's work folder (no symlink from `RUNNER_WORKSPACE` down): nothing outside the workspace is - touched. The walk keeps one folder open at a time, so a tree of any depth is removed. + GitHub-hosted runner. Symlinks are removed as links, never followed (a folder's mode is changed without following + one either), and the workspace's real path must be its own path below the runner's work folder (no symlink from the + work folder down). Another file system mounted inside the workspace is not entered: the step fails with its path. + If another process moves a folder out of the workspace while the wipe is inside it, the wipe empties that folder + (it was the workspace's) and stops red there, before it removes anything where the folder now is. Nothing else + outside the workspace is touched. The walk keeps one folder open at a time and lists each folder once, so a tree of + any depth or width is removed. - **GitHub-hosted runner:** each job starts on a fresh disk, so nothing is removed. - **Both:** it then refuses a `.git` at the checkout path whose config `actions/checkout` does not write (below). @@ -56,10 +60,11 @@ No outputs. A refusal fails the step, so the checkout and every later step do no - `GITHUB_WORKSPACE` that is not an absolute folder, is a symlink, is not inside `RUNNER_WORKSPACE` (which a self-hosted runner always sets), has a real path other than its path below the runner's work folder (an earlier job - made `RUNNER_WORKSPACE` or a folder below it a symlink), or holds `HOME`, `RUNNER_TEMP`, `RUNNER_TOOL_CACHE` or the - action itself: nothing is removed. -- A delete that fails (a file of another user, for example from a container): the step fails with the path. What is - left is removed by the next run once the file can be deleted. + made `RUNNER_WORKSPACE` or a folder below it a symlink), sits in a work folder (`_work`) that is a symlink, or + holds `HOME`, `RUNNER_TEMP`, `RUNNER_TOOL_CACHE` or the action itself: nothing is removed. +- A delete that fails (a file of another user, for example from a container), a file system mounted inside the + workspace, or a folder swapped for a symlink or moved out of the workspace during the wipe: the step fails with the + path. What is left is removed by the next run once the file can be deleted (or the mount is gone). - A checkout path that is absolute or leads outside the workspace. - `/.git` that is a file or a symlink (a worktree or submodule: git would read another repository), or that holds `commondir`, `info/grafts`, `objects/info/alternates`, `objects/info/http-alternates` or `info/attributes` (they @@ -95,4 +100,6 @@ The refusal says what to do either way: a key an earlier job left needs git-clea The runner user's own files outside the workspace (`~/.gitconfig`, the system git config) are not removed: the fork-ci git calls ignore them, `actions/checkout` does not. A runner that takes pull requests from people who are not trusted -also needs a job home that does not outlive the job. +also needs a job home that does not outlive the job. The folders above the work folder (`_work`), such as the +runner's install folder, are trusted as they are: a symlink there is followed (the runner's own path may hold one), +so the runner user must not be able to rename them from a job. diff --git a/fork-ci/actions/git-clean/git_clean.py b/fork-ci/actions/git-clean/git_clean.py index a99da519e13db..06d1eaf996ef9 100644 --- a/fork-ci/actions/git-clean/git_clean.py +++ b/fork-ci/actions/git-clean/git_clean.py @@ -11,9 +11,11 @@ before any of its own steps runs pull request code. prepare: on a self-hosted runner (RUNNER_ENVIRONMENT is not `github-hosted`) every entry of GITHUB_WORKSPACE is -removed, symlinks as links (never what they point to), so nothing outside the workspace is touched. The workspace's -real path must be its path below the runner's work folder (no symlink from RUNNER_WORKSPACE down). A GitHub-hosted -runner starts each job on a fresh disk, so nothing is removed there. Then `check` runs on each path. +removed, symlinks as links (never what they point to); no other file system mounted below it is entered, and a folder +another process moves out of the workspace during the wipe stops it there (empty), so nothing else outside the +workspace is touched. The workspace's real path must be its path below the runner's work folder (no symlink from the +work folder down). A GitHub-hosted runner starts each job on a fresh disk, so nothing is removed there. Then `check` +runs on each path. check: `DIR/.git` must be a folder (not a .git file or symlink, which point git elsewhere), with no `commondir`, none of the files that change history or objects (UNTRUSTED_FILES; `shallow` only as a depth-1 checkout writes it), and @@ -23,7 +25,7 @@ Exit 0: done; 2: refused (the step fails, so the job's checkout and git calls never run). """ -import argparse, os, re, stat, subprocess, sys, tempfile +import argparse, errno, os, re, stat, subprocess, sys, tempfile # Every fork-ci git call that reads a checkout runs with this environment and these flags (the actions' modules carry # copies: git_clean.test.py pins them to these). No global or system config or attributes (an earlier job may have @@ -194,6 +196,8 @@ def workspace(env): runner, ws = os.path.normpath(runner), os.path.normpath(ws) if os.path.commonpath([ws, runner]) != runner or ws == runner: return None, f'GITHUB_WORKSPACE {ws} is not inside RUNNER_WORKSPACE {runner}' + if os.path.islink(os.path.dirname(runner)): + return None, f'the runner work folder {os.path.dirname(runner)} (above RUNNER_WORKSPACE) is a symlink' work = os.path.realpath(os.path.dirname(runner)) expected = os.path.join(work, os.path.basename(runner), os.path.relpath(ws, runner)) if real != expected: @@ -215,45 +219,72 @@ def workspace(env): def empty(top): """Remove everything below the folder `top`, depth first with one folder open at a time and by name relative to - it (no recursion, no path longer than a name: any depth works). A symlink is removed as a link, never followed; a - folder is made writable before it is entered (a read-only one too). Raises OSError with the path that failed.""" - fd, down = os.open(top, DIR_FLAGS), [] + it (no recursion, no path longer than a name: any depth works). Each folder is listed once. A symlink is removed + as a link, never followed; a folder is made writable before it is entered (a read-only one too), never through a + symlink. Another file system mounted below `top` is not entered. Going back up by `..` must land in the folder the + walk came from (the same device and inode): a folder moved out of `top` while the walk is inside it is emptied + (it was `top`'s), but the walk stops there, before it removes anything where it landed. Raises OSError with the + path that failed.""" + fd, down = os.open(top, DIR_FLAGS), [] # down: (name, the folder it is in, that folder's folders left to enter) + + def where(name): + return os.path.join(top, *(d[0] for d in down), name) + + def clear(): + """Remove the folder's files and symlinks; [(name, lstat)] of its folders.""" + folders = [] + for name in sorted(os.listdir(fd)): + try: + st = os.lstat(name, dir_fd=fd) + if stat.S_ISDIR(st.st_mode): + folders.append((name, st)) + else: + os.unlink(name, dir_fd=fd) + except OSError as e: + e.filename = where(name) + raise + return folders[::-1] # entered in name order (pop) + try: + here = os.fstat(fd) + device, here = here.st_dev, (here.st_dev, here.st_ino) + left = clear() while True: - folder = None - for name in sorted(os.listdir(fd)): - try: - st = os.lstat(name, dir_fd=fd) - if stat.S_ISDIR(st.st_mode): - folder = folder or (name, st) - else: - os.unlink(name, dir_fd=fd) - except OSError as e: - e.filename = os.path.join(top, *down, name) - raise - if folder: - name, st = folder + if left: + name, st = left.pop() try: if st.st_mode & 0o700 != 0o700: - os.chmod(name, stat.S_IMODE(st.st_mode) | 0o700, dir_fd=fd) + try: # fchmodat(AT_SYMLINK_NOFOLLOW): a folder swapped for a symlink is refused, not followed + os.chmod(name, stat.S_IMODE(st.st_mode) | 0o700, dir_fd=fd, follow_symlinks=False) + except (NotImplementedError, ValueError): # what Python raises for a symlink there + raise OSError(errno.ELOOP, 'its mode cannot be changed without following a symlink (it ' + 'is one now, or this system cannot)') child = os.open(name, DIR_FLAGS, dir_fd=fd) + inner = os.fstat(child) + if inner.st_dev != device: + os.close(child) + raise OSError(errno.EXDEV, 'another file system is mounted here: it is not entered') except OSError as e: - e.filename = os.path.join(top, *down, name) + e.filename = where(name) raise + down.append((name, here, left)) os.close(fd) - fd = child - down.append(name) + fd, here = child, (inner.st_dev, inner.st_ino) + left = clear() elif not down: return else: # this folder is empty: back up to its parent and remove it parent = os.open('..', DIR_FLAGS, dir_fd=fd) os.close(fd) fd = parent - name = down.pop() + name, here, left = down.pop() try: + st = os.fstat(fd) + if (st.st_dev, st.st_ino) != here: + raise OSError(errno.ESTALE, 'moved out of the workspace during the wipe: stopped there') os.rmdir(name, dir_fd=fd) except OSError as e: - e.filename = os.path.join(top, *down, name) + e.filename = where(name) raise finally: os.close(fd) diff --git a/fork-ci/actions/git-clean/git_clean.test.py b/fork-ci/actions/git-clean/git_clean.test.py index 66f8b23f9934f..012586dc86a74 100644 --- a/fork-ci/actions/git-clean/git_clean.test.py +++ b/fork-ci/actions/git-clean/git_clean.test.py @@ -7,7 +7,7 @@ checkout, `check`) a config that actions/checkout does not write is refused. Every marker file is one the planted command would write: a test passes only when it is absent. Nothing here reaches the network. """ -import io, importlib.util, json, os, shutil, stat, subprocess, sys, tempfile, unittest, unittest.mock +import io, importlib.util, json, os, shutil, stat, subprocess, sys, tempfile, types, unittest, unittest.mock sys.dont_write_bytecode = True HERE = os.path.dirname(os.path.abspath(__file__)) @@ -69,7 +69,7 @@ def env(self, runner='self-hosted', **kw): def run_cli(self, *argv, env=None): """(exit code, stdout+stderr) of git_clean.py as the action runs it.""" r = subprocess.run([sys.executable, '-I', os.path.join(HERE, 'git_clean.py'), *argv], cwd=self.ws, - text=True, capture_output=True, env=env or self.env()) + text=True, capture_output=True, env=env or self.env(), timeout=120) # a loop fails, not hangs return r.returncode, r.stdout + r.stderr def checkout(self, path='.'): @@ -595,6 +595,116 @@ def flaky(p, *a, **kw): self.assertEqual(os.listdir(self.ws), []) self.assert_outside_kept() + def tree(self, *files): + for f in files: + os.makedirs(os.path.dirname(os.path.join(self.ws, f)), exist_ok=True) + with open(os.path.join(self.ws, f), 'w') as out: + out.write('x') + + def test_a_folder_moved_out_during_the_wipe_stops_it_there(self): + """Another process moves the folder the wipe is in out of the workspace: going up by `..` then lands outside. + The moved folder is emptied (it was the workspace's), nothing where it landed is; the next run finishes.""" + self.tree('a/b/trigger', 'a/b/z', 'a/c/f') + moved, real = os.path.join(self.outside, 'b'), os.unlink + + def unlink(name, *a, **kw): + if name == 'trigger' and not os.path.exists(moved): + os.rename(os.path.join(self.ws, 'a', 'b'), moved) + return real(name, *a, **kw) + + with unittest.mock.patch.object(G.os, 'unlink', unlink): + why = G.wipe(self.env()) + self.assertIn('moved out of the workspace', why or '') + self.assertEqual(sorted(os.listdir(self.outside)), ['b', 'keep']) + self.assertEqual(os.listdir(moved), []) + os.rmdir(moved) + self.assert_outside_kept() + self.assertIsNone(G.wipe(self.env())) # runs again: what is left goes + self.assertEqual(os.listdir(self.ws), []) + + def swap_for_symlink(self, folder, target): + """lstat that, once, swaps the folder `folder` for a symlink to `target` right after it reported a folder.""" + real, path = os.lstat, os.path.join(self.ws, folder) + + def lstat(name, *a, **kw): + st = real(name, *a, **kw) + if kw.get('dir_fd') is not None and name == os.path.basename(path) and not os.path.islink(path): + os.chmod(path, 0o700) # a read-only folder cannot be moved; its owner can make it writable first + os.rename(path, os.path.join(self.d, 'swapped')) + os.symlink(target, path) + return st + return unittest.mock.patch.object(G.os, 'lstat', lstat) + + def test_a_folder_swapped_for_a_symlink_is_not_entered(self): # O_NOFOLLOW on every folder open + self.tree('sub/victim/f') + with self.swap_for_symlink('sub/victim', self.outside): + why = G.wipe(self.env()) + self.assertIn('could not remove', why or '') + self.assert_outside_kept() + self.assertIsNone(G.wipe(self.env())) # runs again: the symlink is removed as a link + self.assertEqual(os.listdir(self.ws), []) + self.assert_outside_kept() + + def test_a_read_only_folder_swapped_for_a_symlink_keeps_the_targets_mode(self): + """The chmod that makes a read-only folder writable must not follow a symlink swapped in for it.""" + keep = os.path.join(self.outside, 'keep') + os.chmod(keep, 0o600) + self.tree('sub/locked/f') + os.chmod(os.path.join(self.ws, 'sub', 'locked'), 0o500) + with self.swap_for_symlink('sub/locked', keep): + why = G.wipe(self.env()) + self.assertIn('could not remove', why or '') + self.assertEqual(stat.S_IMODE(os.stat(keep).st_mode), 0o600) + self.assert_outside_kept() + self.assertIsNone(G.wipe(self.env())) + self.assertEqual((os.listdir(self.ws), stat.S_IMODE(os.stat(keep).st_mode)), ([], 0o600)) + + def test_a_symlinked_work_folder_is_refused(self): + """An earlier job swapped the runner's work folder (_work, above RUNNER_WORKSPACE) for a symlink: realpath + follows it on both sides, so it is refused by name.""" + elsewhere = os.path.join(self.d, 'elsewhere') + os.makedirs(os.path.join(elsewhere, 'proj', 'proj')) + with open(os.path.join(elsewhere, 'proj', 'proj', 'f'), 'w') as f: + f.write('x') + work = os.path.join(self.d, '_work') + self.rm(work) + os.symlink(elsewhere, work) + code, out = self.run_cli('prepare', '--path', '.') + self.assertEqual(code, 2, out) + self.assertIn('(above RUNNER_WORKSPACE) is a symlink', out) + self.assertEqual(os.listdir(os.path.join(elsewhere, 'proj', 'proj')), ['f']) + + def test_each_folder_is_listed_once(self): # a re-listing after each child is quadratic in a wide folder + self.tree(*(f'w{i:03}/in/f' for i in range(40)), 'top') + real, listed = os.listdir, [] + + def listdir(p='.'): + listed.append(p) + return real(p) + + with unittest.mock.patch.object(G.os, 'listdir', listdir): + self.assertIsNone(G.wipe(self.env())) + self.assertEqual(os.listdir(self.ws), []) + self.assertEqual(len([p for p in listed if isinstance(p, int)]), 1 + 40 + 40) + + def test_another_file_system_is_not_entered(self): + """A mount point inside the workspace (root sets one up; a test cannot): the folder reports another device.""" + self.tree('mnt/f', 'other/f') + mount, real = os.stat(os.path.join(self.ws, 'mnt')).st_ino, os.fstat + + def fstat(fd): + st = real(fd) + return types.SimpleNamespace(st_dev=st.st_dev + 1, st_ino=st.st_ino) if st.st_ino == mount else st + + with unittest.mock.patch.object(G.os, 'fstat', fstat): + why = G.wipe(self.env()) + self.assertIn('another file system is mounted here', why or '') + self.assertIn(os.path.join(self.ws, 'mnt'), why) + self.assertEqual(os.listdir(os.path.join(self.ws, 'mnt')), ['f']) + self.assertEqual(G.wipe(self.env()), why) # runs again: refused the same way while the mount is there + self.assertIsNone(G.wipe(self.env())) # unmounted: the next run empties it + self.assertEqual(os.listdir(self.ws), []) + def test_path_outside_the_workspace_is_refused(self): for p in ('/etc', '../x', 'a/../../x', self.ws, os.path.join(self.ws, 'sub')): # absolute, even inside with self.subTest(path=p): diff --git a/fork-ci/actions/head-check/head_gate.py b/fork-ci/actions/head-check/head_gate.py index b6247f5dd2df9..2c45db08dac60 100644 --- a/fork-ci/actions/head-check/head_gate.py +++ b/fork-ci/actions/head-check/head_gate.py @@ -286,7 +286,8 @@ def pull_head_by_git(repo, number, env): environment, never on the command line), for when the REST API is down or rate limited. Up to READ_TRIES tries.""" # Not pr_queue.git_env: it drops GIT_CONFIG_COUNT/KEY_/VALUE_, which carry this call's own auth header. clean = {k: v for k, v in os.environ.items() if k in ('PATH', 'HOME', 'LANG', 'TMPDIR')} - clean.update(GIT_TERMINAL_PROMPT='0', GIT_CONFIG_NOSYSTEM='1', GIT_CONFIG_GLOBAL=os.devnull) + clean.update(GIT_TERMINAL_PROMPT='0', GIT_CONFIG_NOSYSTEM='1', GIT_CONFIG_GLOBAL=os.devnull, + GIT_ATTR_NOSYSTEM='1') token = env.get('GH_TOKEN', '') if token: basic = base64.b64encode(f'x-access-token:{token}'.encode()).decode() diff --git a/fork-ci/actions/head-check/head_gate.test.py b/fork-ci/actions/head-check/head_gate.test.py index 12525ec0164d6..5b0f48b4637bd 100644 --- a/fork-ci/actions/head-check/head_gate.test.py +++ b/fork-ci/actions/head-check/head_gate.test.py @@ -392,6 +392,7 @@ def run(argv, **kw): self.assertEqual(argv[-3:], ['--', f'https://github.com/{REPO}.git', 'refs/pull/7/head']) self.assertNotIn('secret-token', ' '.join(argv)) self.assertEqual((env['GIT_CONFIG_KEY_0'], env['GIT_TERMINAL_PROMPT']), ('http.extraheader', '0')) + self.assertEqual({k: env[k] for k in G.pr_queue.GIT_ENV}, G.pr_queue.GIT_ENV) # no system attributes either self.assertIn(base64.b64encode(b'x-access-token:secret-token').decode(), env['GIT_CONFIG_VALUE_0']) for said in ('', f'{self.H}\trefs/pull/8/head\n', 'abc\trefs/pull/7/head\n'): with self.subTest(said=said), unittest.mock.patch.object(G.subprocess, 'run', lambda *a, **k: answer(0, said)): diff --git a/fork-ci/actions/pr-queue/pr_queue.py b/fork-ci/actions/pr-queue/pr_queue.py index 40f052976f55c..9a64f40cc907c 100755 --- a/fork-ci/actions/pr-queue/pr_queue.py +++ b/fork-ci/actions/pr-queue/pr_queue.py @@ -83,8 +83,8 @@ GIT_SAFE = ('-c', 'core.hooksPath=/dev/null', '-c', 'core.fsmonitor=false', '-c', 'core.commitGraph=false', '-c', 'core.attributesFile=/dev/null', '--no-replace-objects') # ... and no global or system config or attributes: on a self-hosted runner an earlier job may have written -# ~/.gitconfig or ~/.config/git/attributes. The same -# two as ../git-clean/git_clean.py's (git_clean.test.py pins GIT_SAFE and GIT_ENV to its). +# ~/.gitconfig or ~/.config/git/attributes. GIT_SAFE and GIT_ENV are the same as ../git-clean/git_clean.py's +# (git_clean.test.py pins them to its). GIT_ENV = {'GIT_CONFIG_GLOBAL': os.devnull, 'GIT_CONFIG_NOSYSTEM': '1', 'GIT_ATTR_NOSYSTEM': '1'} # The tree git reads `.gitattributes` from, for every call git_env makes: the empty tree, so no attribute file of any # commit counts. Without it a bare repository's HEAD (git 2.43 to 2.45 read `HEAD:.gitattributes` there) or an diff --git a/fork-ci/actions/weakened-tests/weakened_tests.py b/fork-ci/actions/weakened-tests/weakened_tests.py index e81dd70f0a0f0..c63f15596cdfd 100644 --- a/fork-ci/actions/weakened-tests/weakened_tests.py +++ b/fork-ci/actions/weakened-tests/weakened_tests.py @@ -323,7 +323,7 @@ def code(text): REFUSED_TEXT = ('**No report for head {head}:** git-clean refused the checkout\'s git config (the job log says why), ' - 'so nothing was checked. An earlier report here was for another head.') + 'so nothing was checked. It replaces any earlier report here.') def render(items, reason, why, merge_base, head, refused=False): diff --git a/fork-ci/actions/weakened-tests/weakened_tests.test.py b/fork-ci/actions/weakened-tests/weakened_tests.test.py index d21b503ee139b..0aee9e43cbee7 100644 --- a/fork-ci/actions/weakened-tests/weakened_tests.test.py +++ b/fork-ci/actions/weakened-tests/weakened_tests.test.py @@ -514,6 +514,14 @@ def test_status_refused_and_the_old_report_is_replaced(self): self.assertNotIn('old list', gh.ours()[0]['body']) self.assertIn('git-clean refused', c.summary) + def test_a_refusal_on_the_head_of_the_earlier_report_does_not_call_it_another_head(self): # a re-run + gh = FakeGh(comments=[{'id': 3, 'login': BOT, 'body': MARK + f'\nHead `{self.repo.base[:12]}`: a list'}]) + self.run_refused(gh, self.repo.base) + body = gh.ours()[0]['body'] + self.assertIn('git-clean refused', body) + self.assertNotIn('another head', body) + self.assertIn('replaces any earlier report', body) + def test_no_comment_yet_posts_none_and_again_edits_nothing(self): gh = FakeGh() self.run_refused(gh) From 617292e6882a25ce38b31bf2c6dcc3c445213458 Mon Sep 17 00:00:00 2001 From: scuttletoby <123843277+scuttletoby@users.noreply.github.com> Date: Fri, 20 Nov 2026 02:16:48 +0000 Subject: [PATCH 2/4] fork-ci: git-clean opens the workspace without a symlink; old glibc; mounts (#428) Answers both reviews of the wipe: - A read-only folder is made writable on glibc older than 2.32 too: where fchmodat(AT_SYMLINK_NOFOLLOW) is refused, open the name O_PATH|O_NOFOLLOW, require a folder and chmod it through /proc/self/fd, as newer glibc does. - The workspace is opened from the runner work folder one name at a time with O_NOFOLLOW, and the walk starts from that fd, so a symlink swapped in after the path checks is refused instead of followed. - A mount point inside the workspace (from /proc/self/mountinfo, a bind mount too) refuses the wipe before anything is removed. - The _work symlink refusal names the fix (config.sh --work); README and release notes describe mounts, btrfs subvolumes and the upgrade steps. Co-Authored-By: Claude Opus 5.5 --- fork-ci/RELEASE-NOTES.md | 17 ++- fork-ci/actions/git-clean/README.md | 40 ++++-- fork-ci/actions/git-clean/git_clean.py | 150 +++++++++++++++----- fork-ci/actions/git-clean/git_clean.test.py | 131 ++++++++++++++++- 4 files changed, 288 insertions(+), 50 deletions(-) diff --git a/fork-ci/RELEASE-NOTES.md b/fork-ci/RELEASE-NOTES.md index 6b377e194bcfb..4f4a53fc2b50e 100644 --- a/fork-ci/RELEASE-NOTES.md +++ b/fork-ci/RELEASE-NOTES.md @@ -18,12 +18,23 @@ nothing. - Going back up by `..` must land in the folder the wipe came from (device and inode): a folder moved out of the workspace while the wipe is inside it is emptied, and the step stops red there instead of emptying where it landed. - A read-only folder is made writable without following a symlink swapped in for it (`fchmodat` with - `AT_SYMLINK_NOFOLLOW`); a work folder (`_work`, above `RUNNER_WORKSPACE`) that is a symlink is refused; another - file system mounted inside the workspace is not entered (the step fails with its path). + `AT_SYMLINK_NOFOLLOW`; on glibc older than 2.32, which refuses that for every path, through an `O_PATH` fd as newer + glibc does itself). +- The workspace is opened from the runner's work folder one name at a time, never through a symlink, so a folder + swapped for a symlink after the path checks is refused instead of followed. A work folder (`_work`, above + `RUNNER_WORKSPACE`) that is a symlink is refused. +- A mount point inside the workspace (from `/proc/self/mountinfo`, a bind mount too) refuses the step before anything + is removed. A folder on another device met during the walk (a btrfs subvolume, a mount made meanwhile) is not + entered: the step fails with its path. - Each folder is listed once (a wide folder was re-listed after each child: 6,000 folders took 20 s). - weakened-tests: the refusal note no longer says the earlier report was for another head (wrong on a re-run). -**Upgrade:** nothing. A mount inside a self-hosted workspace now fails the step until it is unmounted. +**Upgrade:** check each self-hosted runner that uses git-clean before you move the pin. Each of these was emptied +before and now fails the step on every job until it is fixed: +- a work folder (`_work`) that is a symlink (for example to a bigger disk): configure the runner with the real path + (`./config.sh --work `) or mount the disk there instead; +- a mount point inside a workspace: unmount it; +- a btrfs subvolume inside a workspace: remove it once by hand (`btrfs subvolume delete `). ### pr-queue: a person's auto-merge switch during an update stands (#518) diff --git a/fork-ci/actions/git-clean/README.md b/fork-ci/actions/git-clean/README.md index d3cd7be2c5427..f9c5599f6ff64 100644 --- a/fork-ci/actions/git-clean/README.md +++ b/fork-ci/actions/git-clean/README.md @@ -7,13 +7,21 @@ workspace, and an earlier job may have run pull request code there. That code ca of its own steps runs pull request code. - **Self-hosted runner:** it empties the workspace (`GITHUB_WORKSPACE`), so the checkout starts from nothing, as on a - GitHub-hosted runner. Symlinks are removed as links, never followed (a folder's mode is changed without following - one either), and the workspace's real path must be its own path below the runner's work folder (no symlink from the - work folder down). Another file system mounted inside the workspace is not entered: the step fails with its path. - If another process moves a folder out of the workspace while the wipe is inside it, the wipe empties that folder - (it was the workspace's) and stops red there, before it removes anything where the folder now is. Nothing else - outside the workspace is touched. The walk keeps one folder open at a time and lists each folder once, so a tree of - any depth or width is removed. + GitHub-hosted runner. Symlinks are removed as links, never followed (a read-only folder is made writable without + following one either, also on glibc older than 2.32, where it needs `/proc`). The workspace's real path must be its + own path below the runner's work folder (no symlink from the work folder down), and the wipe then opens it from the + work folder one name at a time, never through a symlink, so a symlink swapped in after the check is refused, not + followed. + - **Mounts:** a mount point inside the workspace (any kind, a bind mount of a folder on the same disk too; read from + `/proc/self/mountinfo` on Linux) refuses the step before anything is removed. A folder on another device that + the walk meets anyway (a mount made during the wipe, or a btrfs subvolume, which has its own device but is not a + mount) is not entered: the step fails with its path. A subvolume stays until someone removes it by hand + (`btrfs subvolume delete`). A bind mount made during the wipe (that needs root) has the workspace's device: it is + entered and its source emptied before its `rmdir` fails. + - **Moves:** if another process moves a folder out of the workspace while the wipe is inside it, the wipe empties + that folder (it was the workspace's) and stops red there, before it removes anything where the folder now is. + - Apart from these, nothing outside the workspace is touched. The walk keeps one folder open at a time and lists + each folder once, so a tree of any depth or width is removed. - **GitHub-hosted runner:** each job starts on a fresh disk, so nothing is removed. - **Both:** it then refuses a `.git` at the checkout path whose config `actions/checkout` does not write (below). @@ -60,11 +68,13 @@ No outputs. A refusal fails the step, so the checkout and every later step do no - `GITHUB_WORKSPACE` that is not an absolute folder, is a symlink, is not inside `RUNNER_WORKSPACE` (which a self-hosted runner always sets), has a real path other than its path below the runner's work folder (an earlier job - made `RUNNER_WORKSPACE` or a folder below it a symlink), sits in a work folder (`_work`) that is a symlink, or - holds `HOME`, `RUNNER_TEMP`, `RUNNER_TOOL_CACHE` or the action itself: nothing is removed. -- A delete that fails (a file of another user, for example from a container), a file system mounted inside the - workspace, or a folder swapped for a symlink or moved out of the workspace during the wipe: the step fails with the - path. What is left is removed by the next run once the file can be deleted (or the mount is gone). + made `RUNNER_WORKSPACE` or a folder below it a symlink), sits in a work folder (`_work`) that is a symlink (configure + the runner with the real path instead: `config.sh --work `), holds `HOME`, `RUNNER_TEMP`, + `RUNNER_TOOL_CACHE` or the action itself, or holds a mount point: nothing is removed. +- A delete that fails (a file of another user, for example from a container), a folder on another device (a mount + made during the wipe, a btrfs subvolume), or a folder swapped for a symlink or moved out of the workspace during the + wipe: the step fails with the path. What is left is removed by the next run once the file can be deleted (or the + mount or subvolume is gone). - A checkout path that is absolute or leads outside the workspace. - `/.git` that is a file or a symlink (a worktree or submodule: git would read another repository), or that holds `commondir`, `info/grafts`, `objects/info/alternates`, `objects/info/http-alternates` or `info/attributes` (they @@ -88,7 +98,8 @@ The refusal says what to do either way: a key an earlier job left needs git-clea ## Runs again, stops half-way, old or empty state -- **Runs again:** an empty workspace is emptied again (nothing to do) and passes. +- **Runs again:** an empty workspace is emptied again (nothing to do) and passes. A refusal (a symlinked `_work`, a + mount, a subvolume) repeats on every run until a person fixes it; it never removes anything it refused. - **Stops half-way:** a delete that fails stops the step red, so no checkout runs on a half-empty workspace. The next run removes what is left, at any depth. - **Old workspace** (from before git-clean, or any earlier job): emptied like any other; a `.git` in it never reaches @@ -102,4 +113,5 @@ The runner user's own files outside the workspace (`~/.gitconfig`, the system gi git calls ignore them, `actions/checkout` does not. A runner that takes pull requests from people who are not trusted also needs a job home that does not outlive the job. The folders above the work folder (`_work`), such as the runner's install folder, are trusted as they are: a symlink there is followed (the runner's own path may hold one), -so the runner user must not be able to rename them from a job. +so the runner user must not be able to rename them from a job. A bind mount made inside the workspace while the wipe +runs (root only) is entered (see Mounts above). diff --git a/fork-ci/actions/git-clean/git_clean.py b/fork-ci/actions/git-clean/git_clean.py index 06d1eaf996ef9..d03a0fbc1b154 100644 --- a/fork-ci/actions/git-clean/git_clean.py +++ b/fork-ci/actions/git-clean/git_clean.py @@ -11,11 +11,12 @@ before any of its own steps runs pull request code. prepare: on a self-hosted runner (RUNNER_ENVIRONMENT is not `github-hosted`) every entry of GITHUB_WORKSPACE is -removed, symlinks as links (never what they point to); no other file system mounted below it is entered, and a folder -another process moves out of the workspace during the wipe stops it there (empty), so nothing else outside the -workspace is touched. The workspace's real path must be its path below the runner's work folder (no symlink from the -work folder down). A GitHub-hosted runner starts each job on a fresh disk, so nothing is removed there. Then `check` -runs on each path. +removed, symlinks as links (never what they point to). The workspace's real path must be its path below the runner's +work folder (no symlink from the work folder down), and the wipe opens it from there one name at a time without +following a symlink. A mount point inside the workspace (in /proc/self/mountinfo, a bind mount too) refuses the wipe +before anything is removed; a folder on another device found during the walk (a mount made since, a btrfs subvolume) +is not entered; a folder another process moves out of the workspace during the wipe stops it there (empty). A +GitHub-hosted runner starts each job on a fresh disk, so nothing is removed there. Then `check` runs on each path. check: `DIR/.git` must be a folder (not a .git file or symlink, which point git elsewhere), with no `commondir`, none of the files that change history or objects (UNTRUSTED_FILES; `shallow` only as a depth-1 checkout writes it), and @@ -181,7 +182,9 @@ def shallow_problems(dot, env): def workspace(env): """(the workspace's real path, None) or (None, why it is refused). Its real path must be its own path below the runner's work folder (the parent of RUNNER_WORKSPACE): an earlier job that swapped RUNNER_WORKSPACE or a folder - below it for a symlink would otherwise point the wipe elsewhere, since realpath follows the link on both sides.""" + below it for a symlink would otherwise point the wipe elsewhere, since realpath follows the link on both sides. + These are checks of paths; the wipe then opens the workspace itself without following a symlink + (open_workspace), so a swap after them cannot point it elsewhere either.""" ws = env.get('GITHUB_WORKSPACE') or '' if not ws or not os.path.isabs(ws): return None, f'GITHUB_WORKSPACE {ws!r} is not an absolute path' @@ -194,10 +197,11 @@ def workspace(env): if not os.path.isabs(runner): return None, f'RUNNER_WORKSPACE {runner!r} is not an absolute path' runner, ws = os.path.normpath(runner), os.path.normpath(ws) - if os.path.commonpath([ws, runner]) != runner or ws == runner: + if os.path.commonpath([ws, runner]) != runner or ws == runner or runner == '/': return None, f'GITHUB_WORKSPACE {ws} is not inside RUNNER_WORKSPACE {runner}' if os.path.islink(os.path.dirname(runner)): - return None, f'the runner work folder {os.path.dirname(runner)} (above RUNNER_WORKSPACE) is a symlink' + return None, (f'the runner work folder {os.path.dirname(runner)} (above RUNNER_WORKSPACE) is a symlink: ' + f'configure the runner with the real path instead (config.sh --work )') work = os.path.realpath(os.path.dirname(runner)) expected = os.path.join(work, os.path.basename(runner), os.path.relpath(ws, runner)) if real != expected: @@ -211,29 +215,111 @@ def workspace(env): here = os.path.realpath(os.path.dirname(os.path.abspath(__file__))) if os.path.commonpath([real, here]) == real: return None, f'GITHUB_WORKSPACE {real} holds this action ({here})' + mounted = mounts_inside(real) + if mounted: + more = f' and {len(mounted) - 1} more' if len(mounted) > 1 else '' + return None, f'{mounted[0]}{more} is mounted inside the workspace: unmount it first' return real, None +MOUNTINFO = '/proc/self/mountinfo' + + +def mounts_inside(real): + """The mount points strictly inside the folder `real` (sorted), from MOUNTINFO; [] where it cannot be read (not + Linux). It names a bind mount of a folder of the same file system too, which the walk's device check cannot see.""" + try: + with open(MOUNTINFO, 'rb') as f: + lines = f.read().splitlines() + except OSError: + return [] + found = set() + for line in lines: + fields = line.split(b' ') + if len(fields) < 5: + continue + point = os.fsdecode(re.sub(rb'\\([0-7]{3})', lambda m: bytes([int(m.group(1), 8)]), fields[4])) + if os.path.isabs(point) and point != real and os.path.commonpath([point, real]) == real: + found.add(point) + return sorted(found) + + DIR_FLAGS = os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW -def empty(top): - """Remove everything below the folder `top`, depth first with one folder open at a time and by name relative to - it (no recursion, no path longer than a name: any depth works). Each folder is listed once. A symlink is removed - as a link, never followed; a folder is made writable before it is entered (a read-only one too), never through a - symlink. Another file system mounted below `top` is not entered. Going back up by `..` must land in the folder the +def open_workspace(env): + """An fd of the workspace, opened from the runner's work folder one name at a time, each with O_NOFOLLOW: a folder + swapped for a symlink after workspace() checked the paths is refused, not followed. Whatever folder the fd is, it + sits at the workspace's own place below the work folder (a folder another process moved there is emptied; one it + moved away is not touched). Raises OSError with the path that failed.""" + runner = os.path.normpath(env['RUNNER_WORKSPACE']) + ws = os.path.normpath(env['GITHUB_WORKSPACE']) + path = os.path.dirname(runner) + parts = [os.path.basename(runner), *os.path.relpath(ws, runner).split(os.sep)] + if any(p in ('', '.', '..') for p in parts): + raise OSError(errno.EINVAL, 'not a plain path below the runner work folder', ws) + try: + fd = os.open(path, DIR_FLAGS) # the work folder itself: a symlink there is refused (workspace() said so) + except OSError as e: + e.filename = path + raise + try: + for part in parts: + path = os.path.join(path, part) + try: + child = os.open(part, DIR_FLAGS, dir_fd=fd) + except OSError as e: + e.filename = path + raise + os.close(fd) + fd = child + except BaseException: + os.close(fd) + raise + return fd + + +def make_writable(name, mode, fd): + """chmod the folder `name` in the folder `fd` to `mode`, never through a symlink: fchmodat(AT_SYMLINK_NOFOLLOW). + Where that is refused (a symlink there; glibc older than 2.32 refuses it for every path), what glibc 2.32+ does + itself: open the name with O_PATH|O_NOFOLLOW, require a folder, chmod that fd through /proc/self/fd.""" + try: + os.chmod(name, mode, dir_fd=fd, follow_symlinks=False) + return + except (NotImplementedError, ValueError): # what Python raises when fchmodat answers ENOTSUP + pass + except OSError as e: + if e.errno not in (errno.ENOTSUP, errno.EOPNOTSUPP): + raise + if not hasattr(os, 'O_PATH'): + raise OSError(errno.ENOTSUP, 'its mode cannot be changed without following a symlink on this system') + p = os.open(name, os.O_PATH | os.O_NOFOLLOW, dir_fd=fd) + try: + if not stat.S_ISDIR(os.fstat(p).st_mode): + raise OSError(errno.ELOOP, 'it is not a folder now (a symlink?): its mode is not changed') + os.chmod(f'/proc/self/fd/{p}', mode) + finally: + os.close(p) + + +def empty(fd, top): + """Remove everything below the folder open as `fd` (`top` is its path, for messages; `fd` is closed), depth first + with one folder open at a time and by name relative to it (no recursion, no path longer than a name: any depth + works). Each folder is listed once. A symlink is removed as a link, never followed; a folder is made writable + before it is entered (a read-only one too), never through a symlink. A folder on another device than `top` (a file + system mounted there, a btrfs subvolume) is not entered. Going back up by `..` must land in the folder the walk came from (the same device and inode): a folder moved out of `top` while the walk is inside it is emptied - (it was `top`'s), but the walk stops there, before it removes anything where it landed. Raises OSError with the - path that failed.""" - fd, down = os.open(top, DIR_FLAGS), [] # down: (name, the folder it is in, that folder's folders left to enter) + (it was `top`'s), but the walk stops there, before it removes anything where it landed. Returns how many entries + `top` held; raises OSError with the path that failed.""" + down = [] # down: (name, the folder it is in, that folder's folders left to enter) def where(name): return os.path.join(top, *(d[0] for d in down), name) def clear(): - """Remove the folder's files and symlinks; [(name, lstat)] of its folders.""" - folders = [] - for name in sorted(os.listdir(fd)): + """Remove the folder's files and symlinks; ([(name, lstat)] of its folders, how many entries it held).""" + folders, names = [], sorted(os.listdir(fd)) + for name in names: try: st = os.lstat(name, dir_fd=fd) if stat.S_ISDIR(st.st_mode): @@ -243,36 +329,32 @@ def clear(): except OSError as e: e.filename = where(name) raise - return folders[::-1] # entered in name order (pop) + return folders[::-1], len(names) # folders entered in name order (pop) try: here = os.fstat(fd) device, here = here.st_dev, (here.st_dev, here.st_ino) - left = clear() + left, count = clear() while True: if left: name, st = left.pop() try: - if st.st_mode & 0o700 != 0o700: - try: # fchmodat(AT_SYMLINK_NOFOLLOW): a folder swapped for a symlink is refused, not followed - os.chmod(name, stat.S_IMODE(st.st_mode) | 0o700, dir_fd=fd, follow_symlinks=False) - except (NotImplementedError, ValueError): # what Python raises for a symlink there - raise OSError(errno.ELOOP, 'its mode cannot be changed without following a symlink (it ' - 'is one now, or this system cannot)') + if st.st_mode & 0o700 != 0o700: # a folder swapped for a symlink is refused, not followed + make_writable(name, stat.S_IMODE(st.st_mode) | 0o700, fd) child = os.open(name, DIR_FLAGS, dir_fd=fd) inner = os.fstat(child) if inner.st_dev != device: os.close(child) - raise OSError(errno.EXDEV, 'another file system is mounted here: it is not entered') + raise OSError(errno.EXDEV, 'another device (a mount or btrfs subvolume): it is not entered') except OSError as e: e.filename = where(name) raise down.append((name, here, left)) os.close(fd) fd, here = child, (inner.st_dev, inner.st_ino) - left = clear() + left, _ = clear() elif not down: - return + return count else: # this folder is empty: back up to its parent and remove it parent = os.open('..', DIR_FLAGS, dir_fd=fd) os.close(fd) @@ -300,8 +382,12 @@ def wipe(env): if why: return f'refused to empty the workspace: {why}' try: - count = len(os.listdir(real)) - empty(real) + fd = open_workspace(env) + except OSError as e: + return (f'refused to empty the workspace: {getattr(e, "filename", None) or real} cannot be opened without ' + f'following a symlink ({e.strerror or e}): it changed after it was checked') + try: + count = empty(fd, real) except OSError as e: return f'could not remove {getattr(e, "filename", None) or real} ({e.strerror or e}): the workspace is half empty' say(f'git-clean: emptied the workspace {real} ({count} entries)') diff --git a/fork-ci/actions/git-clean/git_clean.test.py b/fork-ci/actions/git-clean/git_clean.test.py index 012586dc86a74..1d89644eeca46 100644 --- a/fork-ci/actions/git-clean/git_clean.test.py +++ b/fork-ci/actions/git-clean/git_clean.test.py @@ -672,6 +672,7 @@ def test_a_symlinked_work_folder_is_refused(self): code, out = self.run_cli('prepare', '--path', '.') self.assertEqual(code, 2, out) self.assertIn('(above RUNNER_WORKSPACE) is a symlink', out) + self.assertIn('config.sh --work ', out) # the refusal says how to fix it self.assertEqual(os.listdir(os.path.join(elsewhere, 'proj', 'proj')), ['f']) def test_each_folder_is_listed_once(self): # a re-listing after each child is quadratic in a wide folder @@ -687,6 +688,134 @@ def listdir(p='.'): self.assertEqual(os.listdir(self.ws), []) self.assertEqual(len([p for p in listed if isinstance(p, int)]), 1 + 40 + 40) + def old_glibc(self): + """os.chmod as Python behaves on glibc older than 2.32: fchmodat(AT_SYMLINK_NOFOLLOW) answers ENOTSUP for every + path, which Python raises as ValueError (with dir_fd).""" + real = os.chmod + + def chmod(path, mode, *, dir_fd=None, follow_symlinks=True): + if not follow_symlinks: + raise ValueError('chmod: cannot use dir_fd and follow_symlinks together') + return real(path, mode, dir_fd=dir_fd, follow_symlinks=follow_symlinks) + return unittest.mock.patch.object(G.os, 'chmod', chmod) + + def test_old_glibc_read_only_folders_are_emptied(self): + """A read-only folder (a Go module cache, `chmod -R a-w`) is emptied on glibc < 2.32 too, through the O_PATH + fallback: otherwise it stops every run red until a person deletes it.""" + self.fill() # a 0o000 folder holding a 0o500 one + self.tree('gomod/pkg@v1/go.mod') + os.chmod(os.path.join(self.ws, 'gomod', 'pkg@v1'), 0o555) + os.chmod(os.path.join(self.ws, 'gomod'), 0o555) + with self.old_glibc(): + self.assertIsNone(G.wipe(self.env())) + self.assertEqual(os.listdir(self.ws), []) + self.assert_outside_kept() + + def test_old_glibc_a_read_only_folder_swapped_for_a_symlink_keeps_the_targets_mode(self): + """The fallback opens the name with O_NOFOLLOW and requires a folder: a symlink swapped in is not followed.""" + for target in ('keep', ''): # a file, and a folder, outside + with self.subTest(target=target): + victim = os.path.join(self.outside, target) + os.chmod(victim, 0o600 if target else 0o755) + self.tree('sub/locked/f') + os.chmod(os.path.join(self.ws, 'sub', 'locked'), 0o500) + with self.old_glibc(), self.swap_for_symlink('sub/locked', victim): + why = G.wipe(self.env()) + self.assertIn('could not remove', why or '') + self.assertIn('its mode is not changed', why) + self.assertEqual(stat.S_IMODE(os.stat(victim).st_mode), 0o600 if target else 0o755) + self.assert_outside_kept() + with self.old_glibc(): + self.assertIsNone(G.wipe(self.env())) # runs again: the symlink goes as a link + self.assertEqual(os.listdir(self.ws), []) + self.rm(os.path.join(self.d, 'swapped')) + + def swap_after_the_check(self, swapped): + """Another process swaps the folder `swapped` (the work folder, RUNNER_WORKSPACE or the workspace) for a symlink + right after workspace() checked the paths: the wipe opens the workspace one name at a time from the work + folder, never through a symlink. Nothing where the link points is emptied; the next run is refused by path + until the link is gone, then empties the workspace.""" + self.tree('f', 'sub/g') + elsewhere = os.path.join(self.d, 'elsewhere') + inner = os.path.normpath(os.path.join(elsewhere, os.path.relpath(self.ws, swapped))) + os.makedirs(inner) + with open(os.path.join(inner, 'victim'), 'w') as f: + f.write('x') + aside, real = os.path.join(self.d, 'aside'), G.workspace + + def workspace(env): + out = real(env) + os.rename(swapped, aside) + os.symlink(elsewhere, swapped) + return out + + with unittest.mock.patch.object(G, 'workspace', workspace): + why = G.wipe(self.env()) + self.assertEqual(os.listdir(inner), ['victim']) + self.assertIn('cannot be opened without following a symlink', why or '') + self.assertIn(swapped, why) + self.assertIn('refused', G.wipe(self.env())) # runs again: refused by path while the link stays + self.assertEqual(os.listdir(inner), ['victim']) + os.unlink(swapped) + os.rename(aside, swapped) + self.assertEqual(sorted(os.listdir(self.ws)), ['f', 'sub']) # nothing was removed + self.assert_outside_kept() + self.assertIsNone(G.wipe(self.env())) # put back: the next run empties it + self.assertEqual((os.listdir(self.ws), os.listdir(inner)), ([], ['victim'])) + + def test_the_work_folder_swapped_after_the_check_is_not_followed(self): + self.swap_after_the_check(os.path.join(self.d, '_work')) + + def test_the_runner_workspace_swapped_after_the_check_is_not_followed(self): + self.swap_after_the_check(self.runner) + + def test_the_workspace_swapped_after_the_check_is_not_followed(self): # O_NOFOLLOW on the last name: as before + self.swap_after_the_check(self.ws) + + def test_a_workspace_moved_away_after_the_check_is_not_touched(self): + """The workspace is moved out and a new folder made in its place after the check: the wipe empties the folder + at the workspace's place, never the one moved away.""" + self.tree('f', 'sub/g') + aside, real = os.path.join(self.outside, 'moved'), G.workspace + + def workspace(env): + out = real(env) + os.rename(self.ws, aside) + os.mkdir(self.ws) + with open(os.path.join(self.ws, 'new'), 'w') as f: + f.write('x') + return out + + with unittest.mock.patch.object(G, 'workspace', workspace): + self.assertIsNone(G.wipe(self.env())) + self.assertEqual((os.listdir(self.ws), sorted(os.listdir(aside))), ([], ['f', 'sub'])) + + def test_a_mount_point_inside_the_workspace_is_refused_before_anything_is_removed(self): + """A bind mount of a folder on the same file system has the workspace's device, so the walk's device check + would enter it and empty its source: every mount point inside the workspace is refused up front (root mounts + one; a test cannot, so /proc/self/mountinfo is given).""" + self.tree('my mnt/f', 'other/f') + info = os.path.join(self.d, 'mountinfo') + esc = lambda p: p.replace('\\', '\\134').replace(' ', '\\040') + lines = [f'22 1 8:1 / / rw - ext4 /dev/sda1 rw', + f'40 22 8:1 {esc(self.outside)} {esc(self.ws)} rw - ext4 /dev/sda1 rw', # the workspace itself: fine + f'41 22 8:1 {esc(self.outside)} {esc(os.path.join(self.ws, "my mnt"))} rw - ext4 /dev/sda1 rw', + f'42 22 8:1 / {esc(self.outside)} rw - ext4 /dev/sda1 rw'] # outside: fine + with open(info, 'w') as f: + f.write('\n'.join(lines) + '\n') + before = sorted(os.listdir(self.ws)) + with unittest.mock.patch.object(G, 'MOUNTINFO', info): + why = G.wipe(self.env()) + self.assertEqual(why, f'refused to empty the workspace: {os.path.join(self.ws, "my mnt")} is mounted ' + f'inside the workspace: unmount it first') + self.assertEqual(sorted(os.listdir(self.ws)), before) # nothing removed + self.assertEqual(G.wipe(self.env()), why) # runs again: refused the same way + self.assertEqual(G.mounts_inside(self.ws), []) # this test machine: none + with unittest.mock.patch.object(G, 'MOUNTINFO', os.path.join(self.d, 'missing')): + self.assertIsNone(G.wipe(self.env())) # unmounted (or no /proc): the next run empties it + self.assertEqual(os.listdir(self.ws), []) + self.assert_outside_kept() + def test_another_file_system_is_not_entered(self): """A mount point inside the workspace (root sets one up; a test cannot): the folder reports another device.""" self.tree('mnt/f', 'other/f') @@ -698,7 +827,7 @@ def fstat(fd): with unittest.mock.patch.object(G.os, 'fstat', fstat): why = G.wipe(self.env()) - self.assertIn('another file system is mounted here', why or '') + self.assertIn('another device (a mount or btrfs subvolume)', why or '') self.assertIn(os.path.join(self.ws, 'mnt'), why) self.assertEqual(os.listdir(os.path.join(self.ws, 'mnt')), ['f']) self.assertEqual(G.wipe(self.env()), why) # runs again: refused the same way while the mount is there From 4b0aa4cb3724545e0737f832003fe8c50b895cdc Mon Sep 17 00:00:00 2001 From: scuttletoby <123843277+scuttletoby@users.noreply.github.com> Date: Fri, 20 Nov 2026 02:25:11 +0000 Subject: [PATCH 3/4] fork-ci: release notes list formatted (#428) Co-Authored-By: Claude Opus 5.5 --- fork-ci/RELEASE-NOTES.md | 1 + 1 file changed, 1 insertion(+) diff --git a/fork-ci/RELEASE-NOTES.md b/fork-ci/RELEASE-NOTES.md index 4f4a53fc2b50e..1ba07d38a3759 100644 --- a/fork-ci/RELEASE-NOTES.md +++ b/fork-ci/RELEASE-NOTES.md @@ -31,6 +31,7 @@ nothing. **Upgrade:** check each self-hosted runner that uses git-clean before you move the pin. Each of these was emptied before and now fails the step on every job until it is fixed: + - a work folder (`_work`) that is a symlink (for example to a bigger disk): configure the runner with the real path (`./config.sh --work `) or mount the disk there instead; - a mount point inside a workspace: unmount it; From 567de060920f13bce3748301fe185410c162c152 Mon Sep 17 00:00:00 2001 From: scuttletoby <123843277+scuttletoby@users.noreply.github.com> Date: Fri, 20 Nov 2026 02:45:19 +0000 Subject: [PATCH 4/4] git-clean: O_PATH above the workspace, device check before chmod, honest open errors - The work folder and the folders below it up to the workspace are opened with O_PATH: they need search permission only, as a path walk does. - A folder on another device is refused from its lstat before its mode is changed (the fstat check after the open stays). - wipe() calls a failed open a symlink swap only for ELOOP/ENOTDIR; other errors are named as they are. - New tests: a symlink swapped in between the O_PATH open and the chmod of the old-glibc fallback, a read-only folder on another device, search-only work folders, a workspace gone after the check. Each fails when its fix is undone. Co-Authored-By: Claude Opus 5.5 --- fork-ci/actions/git-clean/git_clean.py | 19 +++-- fork-ci/actions/git-clean/git_clean.test.py | 80 +++++++++++++++++++++ 2 files changed, 93 insertions(+), 6 deletions(-) diff --git a/fork-ci/actions/git-clean/git_clean.py b/fork-ci/actions/git-clean/git_clean.py index d03a0fbc1b154..8d9b2f5b52b1d 100644 --- a/fork-ci/actions/git-clean/git_clean.py +++ b/fork-ci/actions/git-clean/git_clean.py @@ -245,6 +245,8 @@ def mounts_inside(real): DIR_FLAGS = os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW +# the folders above the workspace are only passed through: O_PATH needs search permission there, not read permission +TRAVERSE = (os.O_PATH | os.O_DIRECTORY | os.O_NOFOLLOW) if hasattr(os, 'O_PATH') else DIR_FLAGS def open_workspace(env): @@ -259,15 +261,15 @@ def open_workspace(env): if any(p in ('', '.', '..') for p in parts): raise OSError(errno.EINVAL, 'not a plain path below the runner work folder', ws) try: - fd = os.open(path, DIR_FLAGS) # the work folder itself: a symlink there is refused (workspace() said so) + fd = os.open(path, TRAVERSE) # the work folder itself: a symlink there is refused (workspace() said so) except OSError as e: e.filename = path raise try: - for part in parts: + for i, part in enumerate(parts): path = os.path.join(path, part) - try: - child = os.open(part, DIR_FLAGS, dir_fd=fd) + try: # only the workspace itself is listed: it needs a full (readable) fd + child = os.open(part, DIR_FLAGS if i == len(parts) - 1 else TRAVERSE, dir_fd=fd) except OSError as e: e.filename = path raise @@ -339,6 +341,8 @@ def clear(): if left: name, st = left.pop() try: + if st.st_dev != device: # checked before its mode is touched; again from the open fd below + raise OSError(errno.EXDEV, 'another device (a mount or btrfs subvolume): it is not entered') if st.st_mode & 0o700 != 0o700: # a folder swapped for a symlink is refused, not followed make_writable(name, stat.S_IMODE(st.st_mode) | 0o700, fd) child = os.open(name, DIR_FLAGS, dir_fd=fd) @@ -384,8 +388,11 @@ def wipe(env): try: fd = open_workspace(env) except OSError as e: - return (f'refused to empty the workspace: {getattr(e, "filename", None) or real} cannot be opened without ' - f'following a symlink ({e.strerror or e}): it changed after it was checked') + where = getattr(e, 'filename', None) or real + if e.errno in (errno.ELOOP, errno.ENOTDIR): # O_NOFOLLOW / O_DIRECTORY met a symlink or a non-folder + return (f'refused to empty the workspace: {where} cannot be opened without following a symlink ' + f'({e.strerror or e}): it changed after it was checked') + return f'refused to empty the workspace: {where} cannot be opened ({e.strerror or e})' try: count = empty(fd, real) except OSError as e: diff --git a/fork-ci/actions/git-clean/git_clean.test.py b/fork-ci/actions/git-clean/git_clean.test.py index 1d89644eeca46..8361264152701 100644 --- a/fork-ci/actions/git-clean/git_clean.test.py +++ b/fork-ci/actions/git-clean/git_clean.test.py @@ -730,6 +730,36 @@ def test_old_glibc_a_read_only_folder_swapped_for_a_symlink_keeps_the_targets_mo self.assertEqual(os.listdir(self.ws), []) self.rm(os.path.join(self.d, 'swapped')) + def test_old_glibc_a_folder_swapped_after_the_o_path_open_keeps_the_targets_mode(self): + """The fallback changes the mode through the O_PATH fd it opened, never by name: a symlink swapped in between + that open and the chmod is not followed (the folder the fd holds, moved aside, gets the mode).""" + victim = os.path.join(self.outside, 'keep') + os.chmod(victim, 0o600) + self.tree('sub/locked/f') + locked = os.path.join(self.ws, 'sub', 'locked') + os.chmod(locked, 0o500) + inode, real, aside = os.stat(locked).st_ino, os.fstat, os.path.join(self.d, 'swapped') + + def fstat(fd): + st = real(fd) + if st.st_ino == inode and not os.path.lexists(aside): # make_writable checks the O_PATH fd: swap now + os.chmod(locked, 0o700) # a read-only folder cannot be moved; its owner can make it writable first + os.rename(locked, aside) + os.chmod(aside, 0o500) + os.symlink(victim, locked) + return st + + with self.old_glibc(), unittest.mock.patch.object(G.os, 'fstat', fstat): + why = G.wipe(self.env()) + self.assertIn('could not remove', why or '') + self.assertEqual(stat.S_IMODE(os.stat(victim).st_mode), 0o600) + self.assertEqual(stat.S_IMODE(os.stat(aside).st_mode), 0o700) # the fd's folder got it + self.assert_outside_kept() + with self.old_glibc(): + self.assertIsNone(G.wipe(self.env())) # runs again: the symlink goes as a link + self.assertEqual(os.listdir(self.ws), []) + self.rm(aside) + def swap_after_the_check(self, swapped): """Another process swaps the folder `swapped` (the work folder, RUNNER_WORKSPACE or the workspace) for a symlink right after workspace() checked the paths: the wipe opens the workspace one name at a time from the work @@ -834,6 +864,56 @@ def fstat(fd): self.assertIsNone(G.wipe(self.env())) # unmounted: the next run empties it self.assertEqual(os.listdir(self.ws), []) + def test_a_read_only_folder_on_another_device_keeps_its_mode(self): + """The device is checked from the folder's lstat before its mode is changed: a read-only mount keeps its mode.""" + self.tree('mnt/f') + mnt = os.path.join(self.ws, 'mnt') + os.chmod(mnt, 0o555) + self.addCleanup(os.chmod, mnt, 0o700) + real = os.lstat + + def lstat(name, *a, **kw): + st = real(name, *a, **kw) + if kw.get('dir_fd') is not None and name == 'mnt': + return types.SimpleNamespace(st_dev=st.st_dev + 1, st_ino=st.st_ino, st_mode=st.st_mode) + return st + + with unittest.mock.patch.object(G.os, 'lstat', lstat): + why = G.wipe(self.env()) + self.assertIn('another device (a mount or btrfs subvolume)', why or '') + self.assertIn(mnt, why) + self.assertEqual(stat.S_IMODE(os.stat(mnt).st_mode), 0o555) + self.assertEqual(os.listdir(mnt), ['f']) + + @unittest.skipIf(os.geteuid() == 0, 'root reads every folder') + def test_work_folders_with_search_permission_only_are_passed_through(self): + """The folders above the workspace need only search (x) permission, as a path walk does: they are opened with + O_PATH, only the workspace itself is listed.""" + self.tree('f') + work = os.path.join(self.d, '_work') + for p in (work, self.runner): + os.chmod(p, 0o311) + self.addCleanup(os.chmod, p, 0o700) + self.assertIsNone(G.wipe(self.env())) + os.chmod(self.runner, 0o700) + self.assertEqual(os.listdir(self.ws), []) + + def test_a_workspace_that_cannot_be_opened_says_why(self): + """Only a symlink or a non-folder met on the way is called a symlink swap; another error is named as it is.""" + self.tree('f') + aside, real = os.path.join(self.d, 'aside'), G.workspace + + def workspace(env): + out = real(env) + os.rename(self.ws, aside) # gone after the check, nothing in its place + return out + + with unittest.mock.patch.object(G, 'workspace', workspace): + why = G.wipe(self.env()) + self.assertIn(f'{self.ws} cannot be opened (', why or '') + self.assertNotIn('symlink', why) + self.assertEqual(os.listdir(aside), ['f']) + def test_path_outside_the_workspace_is_refused(self): for p in ('/etc', '../x', 'a/../../x', self.ws, os.path.join(self.ws, 'sub')): # absolute, even inside with self.subTest(path=p):