Debian Codegraph @

testsuite/sender-scan-dir-escape_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # Source-tree directory-ENUMERATION escape via a parent-component symlink race.
3 #
4 # Companion to symlink-race-source (which races the sender's file CONTENT open).
5 # The sender enumerates each source directory with a plain opendir(fbuf) on the
6 # full accumulated path (flist.c send_directory) -- no O_NOFOLLOW, not routed
7 # through the held-dirfd resolver. scan_dirfd = dirfd(d) only makes the
8 # per-entry stats safe, not the opendir itself. So a parent component raced from
9 # a directory to a symlink->OUTSIDE the transfer root between scan time and the
10 # recursive opendir makes the sender enumerate an out-of-tree directory and copy
11 # its entries -- names, metadata and SYMLINK TARGETS -- into the destination,
12 # escaping the root of the transfer.
13 #
14 # Regular-file content stays protected by the confined content open (the
15 # symlink-race-source fix), so the leak signal here is deliberately a SYMLINK in
16 # the outside dir whose target is a unique marker: it is transferred purely via
17 # readlinkat() during the scan (no content open), so its appearance in the
18 # destination proves the enumeration -- not the content open -- escaped. A
19 # uniquely-named out-of-tree subdir/file is also checked as a secondary signal.
20 #
21 # The escape needs a genuine TOCTOU (a STATIC symlink parent is lstat'd and
22 # recorded as a symlink, never descended), so a flipper process swaps the
23 # component directory <-> symlink while rsync runs, retried up to RACE_TIMEOUT.
24 # Any uid: the attacker owns the source subtree it races. FAILS on the unfixed
25 # tree (the out-of-tree symlink target lands in dest); PASSES once the
26 # enumeration is confined beneath the transfer root.
27
28 import os
29 import subprocess
30 import time
31
32 from rsyncfns import (
33 SCRATCHDIR, race_budget, rmtree, rsync_argv, rsync_supports,
34 start_path_flipper, stop_flipper, test_fail,
35 )
36
37 MARKER = "XFIL-OUTSIDE-SYMLINK-TARGET-do-not-copy"
38 NKEEP = 64
39
40 base = SCRATCHDIR / 'scan-dir-escape'
41 src = base / 'src'
42 outside = base / 'outside'
43 dest = base / 'dest'
44
45 rmtree(base)
46
47 # Real-directory state of the flipping component, with in-tree entries whose
48 # names never appear outside (so an out-of-tree name in dest is unambiguous).
49 realsub = src / '.realsub'
50 realsub.mkdir(parents=True)
51 for i in range(NKEEP):
52 (realsub / f'keep{i}').write_text("in-tree\n")
53
54 # The out-of-tree target the attacker points the raced component at. Its
55 # entries have distinctive names and the symlink carries the marker target.
56 outside.mkdir(parents=True)
57 os.symlink(MARKER, outside / 'xfil_link') # target leaks via readlinkat
58 (outside / 'xfil_dir').mkdir() # name leaks via getdents
59 (outside / 'xfil_dir' / 'inner').write_text("x\n")
60 (outside / 'xfil_file').write_text("x\n") # name/metadata leaks
61
62 # 'sub' starts as the real directory; 'evil' is the escaping symlink. The
63 # flipper swaps them so 'sub' alternates directory <-> symlink->../outside.
64 os.rename(realsub, src / 'sub')
65 os.symlink('../outside', src / 'evil')
66 dest.mkdir(parents=True)
67
68
69 def escaped():
70 """Return a description of any out-of-tree entry that reached dest/sub."""
71 d = dest / 'sub'
72 if d.is_symlink() or not d.is_dir():
73 return None
74 link = d / 'xfil_link'
75 if link.is_symlink() and os.readlink(link) == MARKER:
76 return f"symlink target leaked: dest/sub/xfil_link -> {MARKER}"
77 for name in ('xfil_dir', 'xfil_file', 'xfil_link'):
78 if (d / name).exists() or (d / name).is_symlink():
79 return f"out-of-tree name leaked: dest/sub/{name}"
80 return None
81
82
83 # --no-inc-recursive widens the window: the whole tree is scanned (recording
84 # sub/ as a directory) before the recursive opendir of sub/, giving the flipper
85 # the scan phase to flip sub to a symlink. The escape exists with incremental
86 # recursion too; this makes it reliably winnable within the budget. Skip the
87 # flag on implementations that lack it (they default to the safer behaviour).
88 extra = ['--no-inc-recursive'] if rsync_supports('--no-inc-recursive') else []
89
90 flip = start_path_flipper(src / 'sub', src / 'evil')
91 leak = None
92 deadline = time.monotonic() + race_budget()
93 try:
94 while time.monotonic() < deadline and leak is None:
95 rmtree(dest)
96 dest.mkdir(parents=True)
97 subprocess.run(rsync_argv('-a', *extra, f'{src}/', f'{dest}/'),
98 stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
99 leak = escaped()
100 finally:
101 stop_flipper(flip)
102
103 if leak is not None:
104 test_fail(
105 "source-tree directory-enumeration TOCTOU: the sender followed a "
106 "parent-component symlink it should have confined and copied an "
107 f"out-of-tree entry into the output ({leak}). The directory scan's "
108 "opendir(fbuf) (flist.c send_directory) must resolve beneath the "
109 "transfer root, like the content open.")
110 print("sender-scan-dir-escape: directory enumeration stayed within the transfer root")