Debian Codegraph @

testsuite/chroot-basis-forge-inner-module_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # Finding (codex review of [16-19]): the generator-side alt-dest basis stat is
3 # confined for a /./ inner-module chroot, but the RECEIVER's delta-basis open
4 # (secure_basis_open) bypassed confinement for any am_chrooted daemon. The
5 # receiver trusts the peer-supplied fnamecmp_type (read from the inbound stream),
6 # so a malicious push client can force an alternate-basis index whose basis_dir
7 # escapes the inner module through a symlinked parent (--compare-dest=../linkparent
8 # -> outside). The receiver then opens an outside-inner-module file as the delta
9 # basis and copies its matched blocks into the in-module destination -- an
10 # out-of-module read materialized into a file the client can later pull.
11 #
12 # This drives the attack with the pure-Python sender (rsync_proto) instead of a
13 # recompiled malicious binary: push file "f" and, in the transfer phase, forge
14 # fnamecmp_type = FNAMECMP_FUZZY + 1 (alt-dest index 0) with xname "f" and a
15 # delta that matches block 0 of that forged basis. The pre-existing dest matches
16 # the source's first block (so a block-0 match is legitimate); with the forged
17 # index the receiver applies that match against the OUTSIDE basis, and the wrong
18 # whole-file checksum then trips "failed verification" -- but only AFTER the
19 # receiver opened and read the out-of-module file, which is the confinement
20 # violation. So the tell is in the daemon log: "failed verification" means the
21 # escaping basis was opened (RED on the unconfined receiver); once
22 # secure_basis_open confines the inner-module case the open is refused (the
23 # forged match has no basis file) and no out-of-module read occurs.
24 #
25 # Needs root (the /./ chroot) and a real TCP daemon (the Python sender).
26
27 import os
28 import time
29
30 from rsyncfns import (
31 SCRATCHDIR, claim_ports, get_rootuid, get_testuid, makepath, require_tcp,
32 rmtree, start_test_daemon, test_fail, test_skipped, write_daemon_conf,
33 )
34 import rsync_proto as rp
35
36 PORT = 12973
37 require_tcp("the pure-Python sender needs a real TCP daemon; run with --use-tcp")
38 if get_testuid() != get_rootuid():
39 test_skipped("the /./ inner-module chroot regression requires root")
40 claim_ports(PORT)
41
42 base = SCRATCHDIR / 'chroot-basis-forge'
43 outer = base / 'outer'
44 inner = outer / 'inner'
45 outside = outer / 'outside'
46 src = base / 'src'
47 rmtree(base)
48 makepath(inner, outside, src)
49 os.symlink('../outside', inner / 'linkparent')
50
51 BLK = 700
52 SECRET = b'S' * BLK + b'Y' # outside basis: first block is the secret
53 PUBLIC = b'A' * BLK # pre-existing dest == generator's FNAME basis
54 SOURCE = b'A' * BLK + b'X' # block 0 matches dest, so a block-0 match is sent
55
56 (outside / 'f').write_bytes(SECRET)
57 (outside / 'dest').mkdir() # cover both file->dirname resolutions
58 (outside / 'dest' / 'f').write_bytes(SECRET)
59 (src / 'f').write_bytes(SOURCE)
60 (inner / 'dest').mkdir()
61 (inner / 'dest' / 'f').write_bytes(PUBLIC)
62
63 conf = write_daemon_conf([
64 ('mod', {'path': str(outer) + '/./inner', 'read only': 'no',
65 'use chroot': 'yes', 'munge symlinks': 'no'}),
66 ], name='chroot-basis-forge.conf')
67 log = SCRATCHDIR / 'rsyncd.log'
68 before = log.read_text(errors='replace') if log.exists() else ''
69 start_test_daemon(conf, PORT)
70
71 s = rp.DaemonSender('127.0.0.1', PORT)
72 s.handshake('mod', ['--server', '-le.LsfxCIu', '--no-whole-file',
73 '--compare-dest=../linkparent', '.', 'dest/'],
74 greeting_version=30)
75 # Push file "f"; its content is delivered in the transfer phase, not the flist.
76 s.send_flat_flist([rp.FileEntry('f', mode=rp.S_IFREG | 0o644, length=len(SOURCE))])
77 # Forge alt-dest basis index 0 (FNAMECMP_FUZZY+1) with basename "f"; match block 0
78 # of that basis then send the literal 'X' tail.
79 s.run_forged_transfer(rp.FNAMECMP_FUZZY + 1, b'f', literal_tail=b'X')
80 s.drain(timeout=3.0)
81 s.close()
82
83 leaked = False
84 for _ in range(50):
85 new = log.read_text(errors='replace')[len(before):]
86 if 'failed verification' in new:
87 leaked = True
88 break
89 time.sleep(0.1)
90
91 if leaked:
92 new = log.read_text(errors='replace')[len(before):]
93 test_fail("receiver opened and read an outside-inner-module file as the delta "
94 "basis via a forged fnamecmp_type (the reconstruction failed the "
95 "whole-file checksum -- but the out-of-module read already "
96 "happened); secure_basis_open did not confine the /./ chroot.\n"
97 + new)
98
99 print("chroot-basis-forge-inner-module: a forged alt-dest basis index cannot "
100 "open a file outside the inner module")