Debian Codegraph @

testsuite/symlink-mknod-fakesuper-symlink-race_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # Python rewrite of testsuite/symlink-mknod-fakesuper-symlink-race.test.
3 #
4 # Regression test for the fake-super branches of do_symlink_at() and
5 # do_mknod_at(). In --fake-super mode a symlink or device is stored as a
6 # regular placeholder file, so creating it resolves the final path component.
7 # The slashed-path branch always used openat(... O_NOFOLLOW), but a top-level
8 # (no-slash) path fell back to do_symlink()/do_mknod(), whose plain
9 # open(... O_CREAT|O_TRUNC) followed a pre-planted symlink at the basename and
10 # wrote outside the module.
11 #
12 # This is the same shape as the do_rename_at() mixed-parent bug: a no-slash
13 # path bypassed the safer branch. It is narrower -- exploiting it end-to-end
14 # needs a TOCTOU race to plant the basename symlink between the generator's
15 # check and the create -- but the openat(... O_NOFOLLOW) in the slashed branch
16 # exists precisely to win that race, and the bare branch silently lost it.
17 #
18 # The helper runs at the wrapper level (deterministic): --poc shows the pre-fix
19 # do_symlink()/do_mknod() fallback follows the planted basename symlink and
20 # clobbers a file outside the module; the regular run proves the fixed
21 # do_symlink_at()/do_mknod_at() refuse it.
22
23 import os
24 import subprocess
25
26 from rsyncfns import SCRATCHDIR, SRCDIR, TOOLDIR, rmtree, test_fail, test_skipped
27
28
29 def run_helper(args, label):
30 proc = subprocess.run([str(TOOLDIR / 't_symlink_secure'), *args])
31 if proc.returncode == 77:
32 test_skipped(f"t_symlink_secure {label} skipped")
33 if proc.returncode != 0:
34 test_fail(f"t_symlink_secure {label} reported failures (see stderr above)")
35
36
37 # Fake-super only stores symlinks as placeholder files -- the surface this PoC
38 # exploits -- where symlink xattrs are unavailable; elsewhere (e.g. FreeBSD)
39 # do_symlink() makes a real symlink and there is no escape to demonstrate.
40 # Mirror do_symlink()'s compile-time guard (and the helper's
41 # TEST_SYMLINK_PLACEHOLDER) so we set up and assert only what applies here. The
42 # device (do_mknod) placeholder is unconditional, so it is always exercised.
43 try:
44 _config_h = (TOOLDIR / 'config.h').read_text()
45 except OSError:
46 _config_h = ''
47 symlink_placeholders = ('#define NO_SYMLINK_XATTRS 1' in _config_h
48 or '#define NO_SYMLINK_USER_XATTRS 1' in _config_h)
49
50
51 # --- PoC tree: pre-fix fallback escapes -------------------------------------
52 poc = SCRATCHDIR / 'poc'
53 pmod = poc / 'module'
54 pout = poc / 'outside'
55 rmtree(poc)
56 pmod.mkdir(parents=True)
57 pout.mkdir(parents=True)
58 (pout / 'secret_nod').write_text("POC_VICTIM_NOD\n")
59 os.symlink('../outside/secret_nod', pmod / 'nodpath')
60 if symlink_placeholders:
61 (pout / 'secret_sym').write_text("POC_VICTIM_SYM\n")
62 os.symlink('../outside/secret_sym', pmod / 'sympath')
63
64 run_helper(['--poc', str(pmod)], '--poc')
65
66 # Independent confirmation the PoC really escaped the module.
67 if symlink_placeholders and (pout / 'secret_sym').read_text().strip() != "VULN_SYM_PAYLOAD":
68 test_fail("PoC did not write through the symlink for do_symlink")
69 if (pout / 'secret_nod').stat().st_size != 0:
70 test_fail("PoC did not truncate through the symlink for do_mknod")
71
72
73 # --- Regression tree: fixed wrappers refuse the escape ----------------------
74 fix = SCRATCHDIR / 'fix'
75 fmod = fix / 'module'
76 fout = fix / 'outside'
77 rmtree(fix)
78 (fmod / 'sub').mkdir(parents=True)
79 fout.mkdir(parents=True)
80 (fout / 'secret_nod').write_text("VICTIM_NOD\n")
81 (fout / 'secret_nod2').write_text("VICTIM_NOD2\n")
82 os.symlink('../outside/secret_nod', fmod / 'nodpath')
83 os.symlink('../../outside/secret_nod2', fmod / 'sub' / 'nodpath2')
84
85 checks = [
86 ('secret_nod', "VICTIM_NOD", "fixed do_mknod_at escaped (bare)"),
87 ('secret_nod2', "VICTIM_NOD2", "fixed do_mknod_at escaped (slashed)"),
88 ]
89 if symlink_placeholders:
90 (fout / 'secret_sym').write_text("VICTIM_SYM\n")
91 (fout / 'secret_sym2').write_text("VICTIM_SYM2\n")
92 os.symlink('../outside/secret_sym', fmod / 'sympath')
93 os.symlink('../../outside/secret_sym2', fmod / 'sub' / 'sympath2')
94 checks = [
95 ('secret_sym', "VICTIM_SYM", "fixed do_symlink_at escaped (bare)"),
96 ('secret_sym2', "VICTIM_SYM2", "fixed do_symlink_at escaped (slashed)"),
97 ] + checks
98
99 run_helper([str(fmod)], 'regression run')
100
101 # Independent confirmation nothing outside the module was touched.
102 for name, want, what in checks:
103 if (fout / name).read_text().strip() != want:
104 test_fail(what)