Debian Codegraph @

testsuite/sender-readlink-atfd_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 import os
3 import subprocess
4 import time
5
6 from rsyncfns import (
7 race_budget, SCRATCHDIR, rmtree, rsync_argv,
8 start_path_flipper, start_test_daemon, stop_flipper, test_fail,
9 write_daemon_conf,
10 )
11
12 # The sender's secure scan-dir open resolves on held dirfds with O_NOFOLLOW
13 # (race-free by construction on every platform), so a flipped parent component
14 # cannot redirect the readlink outside the module.
15
16 base = SCRATCHDIR / 'sender-readlink'
17 mod = base / 'module'
18 outside = base / 'outside'
19 dest = base / 'dest'
20 rmtree(base)
21 (mod / 'real').mkdir(parents=True)
22 outside.mkdir(parents=True)
23 dest.mkdir(parents=True)
24
25 os.symlink('inside-target', mod / 'real' / 'link')
26 os.symlink('OUTSIDE-LINK-TARGET', outside / 'link')
27 os.symlink(outside, mod / 'evil')
28
29 conf = write_daemon_conf([
30 ('src', {'path': str(mod), 'read only': 'yes', 'use chroot': 'no'}),
31 ])
32 url = start_test_daemon(conf, 12938)
33
34 flip = start_path_flipper(mod / 'real', mod / 'evil')
35 leaked = False
36 deadline = time.monotonic() + race_budget()
37 try:
38 while time.monotonic() < deadline:
39 rmtree(dest)
40 dest.mkdir(parents=True)
41 subprocess.run(
42 rsync_argv('-a', f'{url}src/real/link', str(dest) + '/'),
43 stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, text=True)
44 link = dest / 'link'
45 if os.path.islink(link) and os.readlink(link) == 'OUTSIDE-LINK-TARGET':
46 leaked = True
47 break
48 finally:
49 stop_flipper(flip)
50
51 if leaked:
52 test_fail("daemon sender readlink followed a raced parent symlink and sent the outside symlink target")
53
54 print("sender-readlink-atfd: symlink target reads did not leak through a raced parent")