Debian Codegraph @

testsuite/nondaemon-symlink-race_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # Non-daemon (local) transfer: parent-component symlink-race confinement.
3 #
4 # A NON-daemon transfer -- whose
5 # most dangerous instance is a root nightly backup, e.g. `rsync -a /src/ /dest/`
6 # -- writes into a destination tree that contains an attacker-planted parent
7 # component which is a symlink pointing OUTSIDE the tree. The receiver must
8 # confine resolution beneath the operator-named destination root and refuse to
9 # operate through the escaping symlink.
10 #
11 # Before the symlink-race hardening was broadened, the secure resolver was
12 # gated on `am_daemon && !am_chrooted`, so a plain local/remote-shell transfer
13 # fell through to bare rename()/link()/open() on full paths and followed the
14 # planted symlink right out of the tree. This test pins the broadened gate
15 # (all non-chrooted receivers) -- it FAILS against that older rsync and PASSES
16 # once the gate covers non-daemon transfers.
17 #
18 # Deterministic (no race window): the escaping symlink is pre-planted as the
19 # --backup-dir. When the local transfer overwrites an existing in-tree file it
20 # backs the old copy up through that backup-dir; pre-fix the backup lands
21 # OUTSIDE the destination tree, post-fix the secure resolver refuses the
22 # escaping symlink and nothing leaves the tree. Runs at any uid (the escape
23 # target is a sibling the running user can write); root is simply the
24 # highest-severity instance.
25
26 import os
27 import subprocess
28
29 from rsyncfns import (
30 SCRATCHDIR, find_attacker_uid, rmtree, rsync_argv, test_fail, test_skipped,
31 )
32
33 # The operator-path policy follows a symlink owned by uid 0 or the euid (the
34 # operator's OWN backup dir) and refuses a foreign-owned one, so the escape
35 # vector is an ATTACKER-owned backup-dir symlink with rsync run as root.
36 if os.geteuid() != 0:
37 test_skipped("requires root to plant a backup-dir symlink owned by a non-self uid")
38 ATT_UID = find_attacker_uid()
39 if ATT_UID is None:
40 test_skipped("no untrusted-uid user available for cross-uid plant")
41
42
43 # A non-daemon receiver resolves its write paths on held dirfds with O_NOFOLLOW
44 # (race-free by construction on every platform), so a parent-component symlink
45 # swapped in mid-transfer cannot redirect the write outside the destination tree.
46
47
48 dest = SCRATCHDIR / 'dest'
49 outside = SCRATCHDIR / 'outside'
50 src = SCRATCHDIR / 'src_files' # 'src' is reserved by runtests.py (symlink to srcdir)
51 for d in (dest, outside, src):
52 rmtree(d)
53 d.mkdir(parents=True)
54
55 # An existing in-tree file the transfer will overwrite (and therefore back up).
56 (dest / 'foo').write_text("INSIDE_TREE_DATA\n")
57
58 # The attacker-planted backup-dir: a foreign-owned symlink pointing outside the
59 # dest tree (owned by the attacker uid, not root/the operator).
60 os.symlink(str(outside), dest / 'bdir')
61 os.lchown(dest / 'bdir', ATT_UID, ATT_UID)
62
63 # New content so the push genuinely overwrites foo and triggers the backup.
64 (src / 'foo').write_text("NEW_PUSHED_DATA\n")
65
66 # Plain LOCAL (non-daemon) transfer with a backup-dir relative to the dest.
67 # The transfer may report an error on the fixed binary (the secure backup is
68 # refused), so ignore its exit status and judge solely by where files land.
69 subprocess.run(
70 rsync_argv('-t', '--backup', '--backup-dir=bdir', f'{src}/foo', f'{dest}/'),
71 stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
72 )
73
74 # The escape: the in-tree file was backed up out through the bdir symlink into
75 # the outside-the-tree directory.
76 if os.path.lexists(outside / 'foo'):
77 try:
78 got = (outside / 'foo').read_text().strip()
79 except OSError:
80 got = '<unreadable>'
81 test_fail("non-daemon transfer escaped the tree: backup of dest/foo landed "
82 f"in {outside}/foo (content: {got}); the receiver followed the "
83 "planted backup-dir symlink instead of confining beneath the dest")
84
85 leaked = os.listdir(outside)
86 if leaked:
87 test_fail(f"unexpected files escaped the destination tree into {outside}: {leaked}")