| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
import os |
| 23 |
import platform |
| 24 |
import subprocess |
| 25 |
import time |
| 26 |
|
| 27 |
from rsyncfns import ( |
| 28 |
race_budget, SCRATCHDIR, rmtree, rsync_argv, test_fail, test_skipped, |
| 29 |
xattr_set, xattrs_supported, |
| 30 |
) |
| 31 |
|
| 32 |
MARKER = 'user.marker' |
| 33 |
N = 120 |
| 34 |
|
| 35 |
if platform.system() != 'Linux': |
| 36 |
test_skipped("parent-flip xattr race is checked on Linux (os.*xattr)") |
| 37 |
if not xattrs_supported(): |
| 38 |
test_skipped("rsync built without xattr support (or no xattr tooling)") |
| 39 |
|
| 40 |
base = SCRATCHDIR / 'copy-xattrs-race' |
| 41 |
src = base / 'src' |
| 42 |
cdbasis = base / 'cdbasis' |
| 43 |
dest = base / 'dest' |
| 44 |
outside = base / 'outside' |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
DEEP = '/'.join(['d'] * 70) |
| 51 |
|
| 52 |
rmtree(base) |
| 53 |
(src / DEEP / 'sub').mkdir(parents=True) |
| 54 |
(cdbasis / DEEP / 'sub').mkdir(parents=True) |
| 55 |
(dest / DEEP).mkdir(parents=True) |
| 56 |
outside.mkdir() |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
payload = ('x' * 4096 + '\n') * 16 |
| 63 |
try: |
| 64 |
for i in range(N): |
| 65 |
s = src / DEEP / 'sub' / f'f{i}' |
| 66 |
b = cdbasis / DEEP / 'sub' / f'f{i}' |
| 67 |
s.write_text(payload) |
| 68 |
b.write_text(payload) |
| 69 |
os.chmod(s, 0o644) |
| 70 |
os.chmod(b, 0o644) |
| 71 |
st = s.stat() |
| 72 |
os.utime(b, (st.st_atime, st.st_mtime)) |
| 73 |
xattr_set('marker', 'PWNED', s, b) |
| 74 |
|
| 75 |
|
| 76 |
(outside / f'f{i}').write_text('') |
| 77 |
except OSError as e: |
| 78 |
test_skipped(f"filesystem does not support user xattrs ({e})") |
| 79 |
|
| 80 |
sub = dest / DEEP / 'sub' |
| 81 |
link = dest / DEEP / '.sublink' |
| 82 |
link.symlink_to(outside) |
| 83 |
|
| 84 |
|
| 85 |
def push(): |
| 86 |
subprocess.run( |
| 87 |
rsync_argv('-rtpX', '--inplace', f'--copy-dest={cdbasis}', |
| 88 |
f'{src}/', f'{dest}/'), |
| 89 |
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, |
| 90 |
) |
| 91 |
|
| 92 |
|
| 93 |
def outside_marked(): |
| 94 |
return sorted(p.name for p in outside.iterdir() |
| 95 |
if p.is_file() and not p.is_symlink() |
| 96 |
and MARKER in os.listxattr(str(p))) |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
rmtree(sub) |
| 102 |
sub.mkdir() |
| 103 |
push() |
| 104 |
if MARKER not in os.listxattr(str(sub / 'f0')): |
| 105 |
test_fail("positive control: a normal --copy-dest -X push did not copy the " |
| 106 |
"basis marker xattr into dest/sub/f0, so the copy_xattrs race " |
| 107 |
"window would be vacuous") |
| 108 |
|
| 109 |
for i in range(N): |
| 110 |
try: |
| 111 |
os.removexattr(str(outside / f'f{i}'), MARKER) |
| 112 |
except OSError: |
| 113 |
pass |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
flip_code = ( |
| 121 |
"import os, sys, time\n" |
| 122 |
"sub, link = sys.argv[1], sys.argv[2]\n" |
| 123 |
"scratch = sub + '.flip'\n" |
| 124 |
"parent = os.getppid()\n" |
| 125 |
"deadline = time.time() + 120\n" |
| 126 |
"while time.time() < deadline and os.getppid() == parent:\n" |
| 127 |
" try:\n" |
| 128 |
" os.makedirs(sub, exist_ok=True)\n" |
| 129 |
" os.rename(sub, scratch); os.rename(link, sub); os.rename(scratch, link)\n" |
| 130 |
" os.rename(sub, scratch); os.rename(link, sub); os.rename(scratch, link)\n" |
| 131 |
" except OSError:\n" |
| 132 |
" pass\n" |
| 133 |
) |
| 134 |
flip = subprocess.Popen(['python3', '-c', flip_code, str(sub), str(link)]) |
| 135 |
try: |
| 136 |
deadline = time.monotonic() + race_budget(10.0) |
| 137 |
while time.monotonic() < deadline: |
| 138 |
|
| 139 |
|
| 140 |
rmtree(sub) |
| 141 |
try: |
| 142 |
sub.unlink() |
| 143 |
except OSError: |
| 144 |
pass |
| 145 |
push() |
| 146 |
marked = outside_marked() |
| 147 |
if marked: |
| 148 |
test_fail( |
| 149 |
"metadata xattr parent-symlink race: the attacker-chosen marker " |
| 150 |
f"xattr was written onto files OUTSIDE the destination tree " |
| 151 |
f"({marked}) -- a confined receiver's set_file_attrs() fell back to " |
| 152 |
"a path-based lsetxattr and followed a flipped dest/sub symlink.") |
| 153 |
finally: |
| 154 |
flip.terminate() |
| 155 |
try: |
| 156 |
flip.wait(timeout=5) |
| 157 |
except subprocess.TimeoutExpired: |
| 158 |
flip.kill() |
| 159 |
|
| 160 |
|