diff --git a/.github/block/scripts/runner-workspace-test.py b/.github/block/scripts/runner-workspace-test.py index 66a5a0a940..999e43d40d 100644 --- a/.github/block/scripts/runner-workspace-test.py +++ b/.github/block/scripts/runner-workspace-test.py @@ -21,7 +21,7 @@ def step(text, name): def code(block): - return textwrap.dedent(block.split(" run: |\n", 1)[1]).split("python3 - <<'PY'\n", 1)[1].rsplit("\nPY", 1)[0] + return textwrap.dedent(block.split(" run: |\n", 1)[1]).split("\n", 1)[1].rsplit("\nPY", 1)[0] def execute(source, workspace, temp, free=None): @@ -49,6 +49,8 @@ class RunnerWorkspaceTests(unittest.TestCase): self.assertNotIn("- name:", text.split("- name: Clean job workspace", 1)[1]) self.assertIn("working-directory: ${{ runner.temp }}", before) self.assertIn("working-directory: ${{ runner.temp }}", after) + self.assertIn("python3 -I -", before) + self.assertIn("python3 -I -", after) def test_low_space_refuses_and_boundary_admits(self): for path in WORKFLOWS: @@ -87,5 +89,18 @@ class RunnerWorkspaceTests(unittest.TestCase): alias = root / "alias"; alias.symlink_to(work, target_is_directory=True) self.assertEqual(execute(source, alias, root)[0], 1) + def test_cleanup_refuses_symlinked_ancestors_and_preserves_external_sentinel(self): + for path in WORKFLOWS: + with self.subTest(workflow=path.name), tempfile.TemporaryDirectory() as temp: + base = Path(temp) + external = base / "external/repo/repo"; external.mkdir(parents=True) + sentinel = external / "keep"; sentinel.write_text("outside checkout") + alias = base / "alias"; alias.symlink_to(base / "external", target_is_directory=True) + source = code(step(path.read_text(), "Clean job workspace")) + rc, out = execute(source, alias / "repo/repo", base) + self.assertTrue(sentinel.is_file(), "cleanup followed a workspace ancestor outside the checkout") + self.assertEqual(rc, 1, out) + self.assertIn("runner_workspace_cleanup_refused", out) + if __name__ == "__main__": unittest.main() diff --git a/.github/workflows/block-known-bugs.yml b/.github/workflows/block-known-bugs.yml index 76a105216e..7516554255 100644 --- a/.github/workflows/block-known-bugs.yml +++ b/.github/workflows/block-known-bugs.yml @@ -29,7 +29,7 @@ jobs: - name: Runner free space working-directory: ${{ runner.temp }} run: | - python3 - <<'PY' + python3 -I - <<'PY' import os import shutil import sys @@ -96,28 +96,44 @@ jobs: if: always() && runner.environment == 'self-hosted' working-directory: ${{ runner.temp }} run: | - python3 - <<'PY' + python3 -I - <<'PY' import os from pathlib import Path import shutil import sys - workspace = Path(os.environ["GITHUB_WORKSPACE"]) - root = workspace.resolve() - # Only this runner's repository checkout, never a parent, temp/cache root or - # a symlink supplied in place of the checkout itself. - if workspace.is_symlink() or root == Path(root.anchor) or root.name != root.parent.name or root == Path(os.environ["RUNNER_TEMP"]).resolve(): - print(f"runner_workspace_cleanup_refused path={str(workspace)!r}") + root = Path(os.environ["GITHUB_WORKSPACE"]) + # Refuse aliases rather than resolving them. Open EVERY component without + # following symlinks, then keep cleanup anchored to the verified directory fd. + if not root.is_absolute() or ".." in root.parts or root == Path(root.anchor) or root.name != root.parent.name or root == Path(os.environ["RUNNER_TEMP"]): + print(f"runner_workspace_cleanup_refused path={str(root)!r}") sys.exit(1) + flags = os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW + fd = os.open(root.anchor, flags) + previous = os.open(".", flags) errors = [] - if root.exists(): - for entry in os.scandir(root): + try: + for part in root.parts[1:]: + child = os.open(part, flags, dir_fd=fd) + os.close(fd) + fd = child + if not shutil.rmtree.avoids_symlink_attacks: + raise OSError("descriptor-safe directory removal is unavailable") + os.fchdir(fd) + for entry in os.scandir("."): try: if entry.is_dir(follow_symlinks=False): - shutil.rmtree(entry.path) + shutil.rmtree(entry.name) else: - os.unlink(entry.path) + os.unlink(entry.name) except OSError as err: errors.append(f"{entry.name}: {err}") + except OSError as err: + print(f"runner_workspace_cleanup_refused path={str(root)!r} error={err}") + sys.exit(1) + finally: + os.fchdir(previous) + os.close(previous) + os.close(fd) if errors: print("runner_workspace_cleanup_failed " + "; ".join(errors)) sys.exit(1) diff --git a/.github/workflows/block-post-merge-review.yml b/.github/workflows/block-post-merge-review.yml index 5d60328889..88635d575d 100644 --- a/.github/workflows/block-post-merge-review.yml +++ b/.github/workflows/block-post-merge-review.yml @@ -42,7 +42,7 @@ jobs: - name: Runner free space working-directory: ${{ runner.temp }} run: | - python3 - <<'PY' + python3 -I - <<'PY' import os import shutil import sys @@ -92,28 +92,44 @@ jobs: if: always() && runner.environment == 'self-hosted' working-directory: ${{ runner.temp }} run: | - python3 - <<'PY' + python3 -I - <<'PY' import os from pathlib import Path import shutil import sys - workspace = Path(os.environ["GITHUB_WORKSPACE"]) - root = workspace.resolve() - # Only this runner's repository checkout, never a parent, temp/cache root or - # a symlink supplied in place of the checkout itself. - if workspace.is_symlink() or root == Path(root.anchor) or root.name != root.parent.name or root == Path(os.environ["RUNNER_TEMP"]).resolve(): - print(f"runner_workspace_cleanup_refused path={str(workspace)!r}") + root = Path(os.environ["GITHUB_WORKSPACE"]) + # Refuse aliases rather than resolving them. Open EVERY component without + # following symlinks, then keep cleanup anchored to the verified directory fd. + if not root.is_absolute() or ".." in root.parts or root == Path(root.anchor) or root.name != root.parent.name or root == Path(os.environ["RUNNER_TEMP"]): + print(f"runner_workspace_cleanup_refused path={str(root)!r}") sys.exit(1) + flags = os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW + fd = os.open(root.anchor, flags) + previous = os.open(".", flags) errors = [] - if root.exists(): - for entry in os.scandir(root): + try: + for part in root.parts[1:]: + child = os.open(part, flags, dir_fd=fd) + os.close(fd) + fd = child + if not shutil.rmtree.avoids_symlink_attacks: + raise OSError("descriptor-safe directory removal is unavailable") + os.fchdir(fd) + for entry in os.scandir("."): try: if entry.is_dir(follow_symlinks=False): - shutil.rmtree(entry.path) + shutil.rmtree(entry.name) else: - os.unlink(entry.path) + os.unlink(entry.name) except OSError as err: errors.append(f"{entry.name}: {err}") + except OSError as err: + print(f"runner_workspace_cleanup_refused path={str(root)!r} error={err}") + sys.exit(1) + finally: + os.fchdir(previous) + os.close(previous) + os.close(fd) if errors: print("runner_workspace_cleanup_failed " + "; ".join(errors)) sys.exit(1) diff --git a/.github/workflows/block-release.yml b/.github/workflows/block-release.yml index 8463359aab..2c9e745f55 100644 --- a/.github/workflows/block-release.yml +++ b/.github/workflows/block-release.yml @@ -24,7 +24,7 @@ jobs: - name: Runner free space working-directory: ${{ runner.temp }} run: | - python3 - <<'PY' + python3 -I - <<'PY' import os import shutil import sys @@ -149,28 +149,44 @@ jobs: if: always() && runner.environment == 'self-hosted' working-directory: ${{ runner.temp }} run: | - python3 - <<'PY' + python3 -I - <<'PY' import os from pathlib import Path import shutil import sys - workspace = Path(os.environ["GITHUB_WORKSPACE"]) - root = workspace.resolve() - # Only this runner's repository checkout, never a parent, temp/cache root or - # a symlink supplied in place of the checkout itself. - if workspace.is_symlink() or root == Path(root.anchor) or root.name != root.parent.name or root == Path(os.environ["RUNNER_TEMP"]).resolve(): - print(f"runner_workspace_cleanup_refused path={str(workspace)!r}") + root = Path(os.environ["GITHUB_WORKSPACE"]) + # Refuse aliases rather than resolving them. Open EVERY component without + # following symlinks, then keep cleanup anchored to the verified directory fd. + if not root.is_absolute() or ".." in root.parts or root == Path(root.anchor) or root.name != root.parent.name or root == Path(os.environ["RUNNER_TEMP"]): + print(f"runner_workspace_cleanup_refused path={str(root)!r}") sys.exit(1) + flags = os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW + fd = os.open(root.anchor, flags) + previous = os.open(".", flags) errors = [] - if root.exists(): - for entry in os.scandir(root): + try: + for part in root.parts[1:]: + child = os.open(part, flags, dir_fd=fd) + os.close(fd) + fd = child + if not shutil.rmtree.avoids_symlink_attacks: + raise OSError("descriptor-safe directory removal is unavailable") + os.fchdir(fd) + for entry in os.scandir("."): try: if entry.is_dir(follow_symlinks=False): - shutil.rmtree(entry.path) + shutil.rmtree(entry.name) else: - os.unlink(entry.path) + os.unlink(entry.name) except OSError as err: errors.append(f"{entry.name}: {err}") + except OSError as err: + print(f"runner_workspace_cleanup_refused path={str(root)!r} error={err}") + sys.exit(1) + finally: + os.fchdir(previous) + os.close(previous) + os.close(fd) if errors: print("runner_workspace_cleanup_failed " + "; ".join(errors)) sys.exit(1) diff --git a/.github/workflows/block-spec-trace.yml b/.github/workflows/block-spec-trace.yml index f19b26d575..43cd2e6581 100644 --- a/.github/workflows/block-spec-trace.yml +++ b/.github/workflows/block-spec-trace.yml @@ -39,7 +39,7 @@ jobs: - name: Runner free space working-directory: ${{ runner.temp }} run: | - python3 - <<'PY' + python3 -I - <<'PY' import os import shutil import sys @@ -119,28 +119,44 @@ jobs: if: always() && runner.environment == 'self-hosted' working-directory: ${{ runner.temp }} run: | - python3 - <<'PY' + python3 -I - <<'PY' import os from pathlib import Path import shutil import sys - workspace = Path(os.environ["GITHUB_WORKSPACE"]) - root = workspace.resolve() - # Only this runner's repository checkout, never a parent, temp/cache root or - # a symlink supplied in place of the checkout itself. - if workspace.is_symlink() or root == Path(root.anchor) or root.name != root.parent.name or root == Path(os.environ["RUNNER_TEMP"]).resolve(): - print(f"runner_workspace_cleanup_refused path={str(workspace)!r}") + root = Path(os.environ["GITHUB_WORKSPACE"]) + # Refuse aliases rather than resolving them. Open EVERY component without + # following symlinks, then keep cleanup anchored to the verified directory fd. + if not root.is_absolute() or ".." in root.parts or root == Path(root.anchor) or root.name != root.parent.name or root == Path(os.environ["RUNNER_TEMP"]): + print(f"runner_workspace_cleanup_refused path={str(root)!r}") sys.exit(1) + flags = os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW + fd = os.open(root.anchor, flags) + previous = os.open(".", flags) errors = [] - if root.exists(): - for entry in os.scandir(root): + try: + for part in root.parts[1:]: + child = os.open(part, flags, dir_fd=fd) + os.close(fd) + fd = child + if not shutil.rmtree.avoids_symlink_attacks: + raise OSError("descriptor-safe directory removal is unavailable") + os.fchdir(fd) + for entry in os.scandir("."): try: if entry.is_dir(follow_symlinks=False): - shutil.rmtree(entry.path) + shutil.rmtree(entry.name) else: - os.unlink(entry.path) + os.unlink(entry.name) except OSError as err: errors.append(f"{entry.name}: {err}") + except OSError as err: + print(f"runner_workspace_cleanup_refused path={str(root)!r} error={err}") + sys.exit(1) + finally: + os.fchdir(previous) + os.close(previous) + os.close(fd) if errors: print("runner_workspace_cleanup_failed " + "; ".join(errors)) sys.exit(1) diff --git a/.github/workflows/block-test.yml b/.github/workflows/block-test.yml index 740b31d08d..37e0bb2ca5 100644 --- a/.github/workflows/block-test.yml +++ b/.github/workflows/block-test.yml @@ -61,7 +61,7 @@ jobs: - name: Runner free space working-directory: ${{ runner.temp }} run: | - python3 - <<'PY' + python3 -I - <<'PY' import os import shutil import sys @@ -399,28 +399,44 @@ jobs: if: always() && runner.environment == 'self-hosted' working-directory: ${{ runner.temp }} run: | - python3 - <<'PY' + python3 -I - <<'PY' import os from pathlib import Path import shutil import sys - workspace = Path(os.environ["GITHUB_WORKSPACE"]) - root = workspace.resolve() - # Only this runner's repository checkout, never a parent, temp/cache root or - # a symlink supplied in place of the checkout itself. - if workspace.is_symlink() or root == Path(root.anchor) or root.name != root.parent.name or root == Path(os.environ["RUNNER_TEMP"]).resolve(): - print(f"runner_workspace_cleanup_refused path={str(workspace)!r}") + root = Path(os.environ["GITHUB_WORKSPACE"]) + # Refuse aliases rather than resolving them. Open EVERY component without + # following symlinks, then keep cleanup anchored to the verified directory fd. + if not root.is_absolute() or ".." in root.parts or root == Path(root.anchor) or root.name != root.parent.name or root == Path(os.environ["RUNNER_TEMP"]): + print(f"runner_workspace_cleanup_refused path={str(root)!r}") sys.exit(1) + flags = os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW + fd = os.open(root.anchor, flags) + previous = os.open(".", flags) errors = [] - if root.exists(): - for entry in os.scandir(root): + try: + for part in root.parts[1:]: + child = os.open(part, flags, dir_fd=fd) + os.close(fd) + fd = child + if not shutil.rmtree.avoids_symlink_attacks: + raise OSError("descriptor-safe directory removal is unavailable") + os.fchdir(fd) + for entry in os.scandir("."): try: if entry.is_dir(follow_symlinks=False): - shutil.rmtree(entry.path) + shutil.rmtree(entry.name) else: - os.unlink(entry.path) + os.unlink(entry.name) except OSError as err: errors.append(f"{entry.name}: {err}") + except OSError as err: + print(f"runner_workspace_cleanup_refused path={str(root)!r} error={err}") + sys.exit(1) + finally: + os.fchdir(previous) + os.close(previous) + os.close(fd) if errors: print("runner_workspace_cleanup_failed " + "; ".join(errors)) sys.exit(1)