| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 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 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 102 |
for name, want, what in checks: |
| 103 |
if (fout / name).read_text().strip() != want: |
| 104 |
test_fail(what) |