Debian Codegraph @

testsuite/sender-remove-source-secure_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 import os
3 import platform
4 import subprocess
5 import time
6
7 from rsyncfns import (
8 race_budget, SCRATCHDIR, rmtree, rsync_argv,
9 start_path_flipper, start_test_daemon, stop_flipper, test_fail, test_xfail,
10 write_daemon_conf,
11 )
12
13 _CYGWIN = platform.system().startswith('CYGWIN')
14
15 # The sender's secure parent-dir open resolves on held dirfds with O_NOFOLLOW
16 # (race-free by construction on every platform), so a flipped parent component
17 # cannot redirect the --remove-source-files unlink outside the module.
18
19 base = SCRATCHDIR / 'sender-remove-source'
20 mod = base / 'module'
21 outside = base / 'outside'
22 dest = base / 'dest'
23 rmtree(base)
24 (mod / 'real').mkdir(parents=True)
25 outside.mkdir(parents=True)
26 dest.mkdir(parents=True)
27
28 inside_file = mod / 'real' / 'file'
29 outside_file = outside / 'file'
30 inside_file.write_text('payload\n')
31 outside_file.write_text('payload\n')
32 st = inside_file.stat()
33 os.utime(outside_file, (st.st_atime, st.st_mtime))
34 os.symlink(outside, mod / 'evil')
35
36 conf = write_daemon_conf([
37 ('src', {'path': str(mod), 'read only': 'no', 'use chroot': 'no'}),
38 ])
39 url = start_test_daemon(conf, 12937)
40
41 flip = start_path_flipper(mod / 'real', mod / 'evil')
42 deadline = time.monotonic() + race_budget()
43 try:
44 while time.monotonic() < deadline and outside_file.exists():
45 if not inside_file.exists() and (mod / 'real').is_dir() and not os.path.islink(mod / 'real'):
46 try:
47 inside_file.write_text('payload\n')
48 os.utime(inside_file, (st.st_atime, st.st_mtime))
49 except FileNotFoundError:
50 pass
51 subprocess.run(
52 rsync_argv('-a', '--remove-source-files', f'{url}src/real/file', str(dest) + '/'),
53 stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, text=True)
54 finally:
55 stop_flipper(flip)
56
57 if not outside_file.exists():
58 if _CYGWIN:
59 # Cygwin resolves directory descriptors by path name rather than a pinned
60 # inode and emulates symlinks as special files, so the confined sender's
61 # held-fd O_NOFOLLOW walk -- though compiled in -- does not enforce the
62 # parent-component pin here, and a raced parent flip can still redirect
63 # the --remove-source-files unlink. Documented Cygwin platform residual
64 # (see SECURITY.md and the matching xfail in symlink-race-source_test.py);
65 # Cygwin is a dev/interop target, not a privilege boundary host.
66 test_xfail("cygwin: --remove-source-files parent-flip race still unlinks "
67 "the outside victim -- documented Cygwin platform residual")
68 test_fail("daemon sender --remove-source-files cleanup unlinked the outside victim through a raced parent symlink")
69
70 print("sender-remove-source-secure: remove-source cleanup did not unlink outside the module")