Debian Codegraph @

testsuite/copy-dest-symlink-readleak_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # --copy-dest parent-component symlink-race: out-of-tree basis read-leak.
3 #
4 # With --copy-dest, rsync stats the alt-dest basis and, on an exact match, copies
5 # it into the destination instead of transferring. basis_link_stat() refuses a
6 # foreign-owned basis symlink at stat time, but copy_file() opened an ABSOLUTE
7 # basis with bare do_open_nofollow (leaf-only -- parents followed). A non-root
8 # attacker who flips a basis parent between a real dir and a foreign-owned symlink
9 # -> outside wins the window between the stat and the open: the copy then reads an
10 # out-of-tree file and writes its content into the destination (an attacker can
11 # read a root-readable file's content into a place they control).
12 #
13 # The fix re-resolves an absolute source's parents through the ownership walk in
14 # copy_file(), refusing the flipped foreign parent at open time. RED on stock
15 # 3.4.x / --insecure-links, GREEN here. Root+nobody gated.
16
17 import os
18 import subprocess
19 import time
20
21 from rsyncfns import (
22 SCRATCHDIR, race_budget, find_attacker_uid,
23 rmtree, rsync_argv, start_c_flipper, stop_flipper, test_fail, test_skipped,
24 )
25
26 if os.geteuid() != 0:
27 test_skipped("requires root to plant a copy-dest symlink owned by a non-self uid")
28 ATT_UID = find_attacker_uid()
29 if ATT_UID is None:
30 test_skipped("no untrusted-uid user available for cross-uid plant")
31
32 NFILES = 60
33 PINNED = 1000000000 # identical mtime so the basis is an exact match
34
35 base = SCRATCHDIR / 'copydest-readleak'
36 src = base / 'src'
37 dest = base / 'dest'
38 cdroot = base / 'cdroot'
39 cd = cdroot / 'cd' # the basis dir the attacker flips
40 cdlink = cdroot / '.cdlink' # symlink -> outside, swapped in for cd
41 outside = base / 'outside'
42
43 SECRET = 'OUT-OF-TREE-SECRET\n'
44
45
46 def build():
47 rmtree(base)
48 for d in (src, dest, cdroot, outside):
49 d.mkdir(parents=True)
50 cd.mkdir()
51 for i in range(NFILES):
52 (src / f'f{i}').write_text('SRC\n')
53 (cd / f'f{i}').write_text('BAS\n') # in-tree basis (exact match)
54 (outside / f'f{i}').write_text(SECRET) # out-of-tree content to leak
55 for p in (src / f'f{i}', cd / f'f{i}', outside / f'f{i}'):
56 os.utime(p, (PINNED, PINNED))
57 os.symlink(str(outside), cdlink)
58 os.lchown(cdlink, ATT_UID, ATT_UID)
59
60
61 def push():
62 return subprocess.run(
63 rsync_argv('-rt', f'--copy-dest={cd}', f'{src}/', f'{dest}/'),
64 stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
65
66
67 def leaked():
68 for i in range(NFILES):
69 try:
70 if (dest / f'f{i}').read_text() == SECRET:
71 return f'f{i}'
72 except OSError:
73 pass
74 return None
75
76
77 # Positive control: a clean push with cd a real dir copies the in-tree basis.
78 build()
79 proc = push()
80 if proc.returncode != 0:
81 test_fail("positive control: clean --copy-dest transfer failed "
82 f"(rc={proc.returncode}):\n{proc.stdout or ''}")
83 for i in range(NFILES):
84 target = dest / f'f{i}'
85 if not target.is_file():
86 test_fail(f"positive control: --copy-dest did not create {target}")
87 got = target.read_text()
88 if got != 'BAS\n':
89 test_fail(
90 "positive control: clean --copy-dest did not copy the in-tree "
91 f"basis for {target} (got {got!r}); the test would not exercise "
92 "copy_file()'s basis read path")
93
94 flip = start_c_flipper(cd, cdlink)
95 try:
96 deadline = time.monotonic() + race_budget(10.0)
97 while time.monotonic() < deadline:
98 rmtree(dest)
99 dest.mkdir()
100 push()
101 hit = leaked()
102 if hit:
103 test_fail(
104 "--copy-dest parent symlink race: out-of-tree basis content was "
105 f"read into the destination ({hit}) -- copy_file followed a "
106 "flipped foreign-owned --copy-dest parent symlink (read-leak).")
107 finally:
108 stop_flipper(flip)
109
110 # No read-leak within the race budget.