Debian Codegraph @

testsuite/relative-mkpath-dir-symlink_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # --relative directory-entry creation escapes the tree via make_path().
3 #
4 # Companion to relative-mkpath-symlink_test.py for the SECOND make_path() call
5 # site flagged in the May 2026 follow-up. When the receiver creates a directory
6 # entry under --relative and the leaf do_mkdir_at() fails with ENOENT (a parent
7 # component is missing), generator.c falls back to:
8 # make_path(fname, MKP_DROP_NAME | MKP_SKIP_SLASH) (recv_generator, the
9 # directory-creation site)
10 # and then retries do_mkdir_at(). make_path() builds the parent chain with a
11 # PLAIN do_mkdir() on the full multi-component path, so a symlink planted at any
12 # parent component is followed and the new directories are created at the
13 # symlink's target, OUTSIDE the destination tree.
14 #
15 # Trigger: transfer a directory whose mid-level parent is absent under a planted
16 # parent symlink, with --no-implied-dirs so the mid parent is not pre-created.
17 # do_mkdir_at(dest/A/B/C) fails ENOENT (B missing under the dest/A symlink),
18 # make_path() then creates dest/A/B == outside/B, escaping the tree.
19 #
20 # Deterministic static plant; any uid; platform-independent. Fixed: the
21 # do_mkdir_at() ENOENT fallback's make_path() now builds the parent chain through
22 # the held-dirfd do_mkdir_at(), confining each component beneath the destination.
23 # This is a regression guard -- it asserts nothing escapes and fails hard if that
24 # confinement is ever lost.
25
26 import subprocess
27
28 from rsyncfns import SCRATCHDIR, rmtree, rsync_argv, test_fail
29
30
31 srcroot = SCRATCHDIR / 'srcroot'
32 dest = SCRATCHDIR / 'dest'
33 outside = SCRATCHDIR / 'outside'
34 for d in (srcroot, dest, outside):
35 rmtree(d)
36 d.mkdir(parents=True)
37
38 # A directory three levels below the implied-dir boundary; 'B' (the mid parent)
39 # is what make_path must create under the planted dest/A symlink.
40 (srcroot / 'A' / 'B' / 'C').mkdir(parents=True)
41 (srcroot / 'A' / 'B' / 'C' / 'file').write_text("PAYLOAD\n")
42
43 # Attacker-planted parent symlink pointing OUTSIDE the destination tree.
44 (dest / 'A').symlink_to(outside)
45
46 # -R replicates A/B/C/ under dest; --no-implied-dirs leaves B uncreated, so the
47 # dir-entry do_mkdir_at() fails ENOENT and the make_path() fallback runs.
48 subprocess.run(
49 rsync_argv('-aR', '--no-implied-dirs', f'{srcroot}/./A/B/C/', f'{dest}/'),
50 stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
51 )
52
53 try:
54 leaked = sorted(p.name for p in outside.iterdir())
55 except OSError:
56 leaked = []
57
58 if leaked:
59 test_fail(
60 "--relative make_path() (dir-entry fallback) escaped the destination "
61 f"tree: parent creation landed in {outside} ({leaked}) by following a "
62 "planted dest/A -> outside symlink -- the do_mkdir_at() ENOENT fallback's "
63 "make_path() must stay routed through the held-dirfd do_mkdir_at().")
64 # No escape -> the dir-entry make_path fallback was confined beneath the dest.