Debian Codegraph @

testsuite/symlink-dest-backupdir_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # Destination config-path (--backup-dir) symlink confinement -- DETERMINISTIC.
3 #
4 # An attacker who controls a component of the destination plants a STATIC symlink
5 # as the backup directory, pointing OUTSIDE the destination tree. When the
6 # receiver backs up files it is about to overwrite, a plain resolution writes
7 # each backup through the symlink, outside the tree (root-owned writes to an
8 # arbitrary location in the worst case). The secure resolver must confine the
9 # backup write beneath the destination.
10 #
11 # No race is needed: unlike the bare write path -- where rsync re-creates a
12 # wrong-typed destination directory, normalising a flipped parent away -- the
13 # --backup-dir path follows the planted symlink directly, so a static symlink
14 # suffices. (--temp-dir / --partial-dir do NOT work as escape vectors: rsync
15 # normalises them the same way it does the destination directory, so this is the
16 # one config-path that escapes deterministically.) This complements the racy
17 # source/chdir tests and nondaemon-symlink-race (a single-file --backup-dir case)
18 # by confirming confinement across a small tree.
19
20 import os
21 import subprocess
22
23 from rsyncfns import (
24 SCRATCHDIR, find_attacker_uid, rmtree, rsync_argv, test_fail, test_skipped,
25 )
26
27 # Operator paths follow a uid0/euid-owned symlink (the operator's own backup
28 # dir) and refuse a foreign-owned one, so the escape vector is an ATTACKER-owned
29 # backup-dir symlink with rsync run as root.
30 if os.geteuid() != 0:
31 test_skipped("requires root to plant a backup-dir symlink owned by a non-self uid")
32 ATT_UID = find_attacker_uid()
33 if ATT_UID is None:
34 test_skipped("no untrusted-uid user available for cross-uid plant")
35
36
37 NFILES = 8
38
39 base = SCRATCHDIR / 'destbackup'
40 src = base / 'src'
41 dest = base / 'dest'
42 outside = base / 'outside'
43
44 rmtree(base)
45 # A small set of files the transfer will overwrite (so each triggers a backup).
46 # --backup-dir is resolved relative to the destination directory, so the planted
47 # symlink and the transfer target are both at the dest top level.
48 src.mkdir(parents=True)
49 dest.mkdir(parents=True)
50 for i in range(NFILES):
51 # Different sizes so the transfer actually overwrites (triggering a backup);
52 # equal-size content with matching mtimes would be skipped by the quick check.
53 (src / f'f{i}').write_text("NEW-PUSHED-CONTENT\n")
54 (dest / f'f{i}').write_text("OLD\n")
55 outside.mkdir(parents=True)
56 # Attacker-planted backup directory: a foreign-owned symlink pointing outside
57 # the dest tree (owned by the attacker uid, not root/the operator).
58 os.symlink('../outside', dest / 'bdir')
59 os.lchown(dest / 'bdir', ATT_UID, ATT_UID)
60
61 subprocess.run(
62 rsync_argv('-a', '--backup', '--backup-dir=bdir', f'{src}/', f'{dest}/'),
63 stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
64
65 leaked = []
66 for root, _dirs, files in os.walk(outside):
67 leaked += [os.path.join(root, f) for f in files]
68 if leaked:
69 test_fail(
70 "backup escaped the destination tree via the planted --backup-dir "
71 f"symlink: {sorted(leaked)} were created outside the tree. The secure "
72 "resolver failed to confine the backup write.")
73 # Nothing landed outside the tree -> the backup write was confined.