| 1 |
|
| 2 |
"""PoC: EACCES recovery must retain partial-dir ownership policy. |
| 3 |
|
| 4 |
The one-inplace partial path is initially opened through the operator ownership |
| 5 |
walk. Model an EACCES from that O_CREAT open, then replace the partial |
| 6 |
directory with an attacker symlink. The production recovery opens drop to the |
| 7 |
ordinary resolver, which follows the new parent without an ownership check and |
| 8 |
can overwrite a different module file through the returned descriptor. On |
| 9 |
Linux the first recovery is the fs.protected_regular compatibility retry; the |
| 10 |
generic read-only-file chmod-and-open recovery has the same policy requirement. |
| 11 |
""" |
| 12 |
|
| 13 |
import os |
| 14 |
import platform |
| 15 |
import subprocess |
| 16 |
|
| 17 |
from rsyncfns import ( |
| 18 |
SCRATCHDIR, rmtree, rsync_argv, test_fail, test_skipped, |
| 19 |
) |
| 20 |
|
| 21 |
|
| 22 |
if platform.system() != 'Darwin': |
| 23 |
test_skipped('deterministic partial EACCES recovery uses dyld interposing') |
| 24 |
|
| 25 |
base = SCRATCHDIR / 'partial-protected-regular-retry-policy' |
| 26 |
rmtree(base) |
| 27 |
src = base / 'src' |
| 28 |
mod = base / 'mod' |
| 29 |
partial = mod / 'pdir' |
| 30 |
held = mod / '.pdir-held' |
| 31 |
secret = mod / 'secret' |
| 32 |
load_marker = base / 'hook-loaded' |
| 33 |
eacces_marker = base / 'operator-open-eacces' |
| 34 |
retry_marker = base / 'unconfined-retry-open' |
| 35 |
foreign_check_marker = base / 'owner-check-would-refuse' |
| 36 |
for directory in (src, partial, secret): |
| 37 |
directory.mkdir(parents=True) |
| 38 |
|
| 39 |
old_partial = b'OLD-PARTIAL-BASIS-' * 4096 |
| 40 |
new_source = b'NEW-REMOTE-PAYLOAD' * 4096 |
| 41 |
secret_data = b'OTHER-USER-SECRET!' * 4096 |
| 42 |
if not (len(old_partial) == len(new_source) == len(secret_data)): |
| 43 |
raise AssertionError('PoC payloads must be equal length') |
| 44 |
(partial / 'victim').write_bytes(old_partial) |
| 45 |
(secret / 'victim').write_bytes(secret_data) |
| 46 |
(src / 'victim').write_bytes(new_source) |
| 47 |
|
| 48 |
hook_src = base / 'partial-protected-retry-hook.c' |
| 49 |
hook_lib = base / 'partial-protected-retry-hook.dylib' |
| 50 |
hook_src.write_text(r''' |
| 51 |
#include <sys/stat.h> |
| 52 |
#include <sys/syscall.h> |
| 53 |
#include <errno.h> |
| 54 |
#include <fcntl.h> |
| 55 |
#include <limits.h> |
| 56 |
#include <stdarg.h> |
| 57 |
#include <stdio.h> |
| 58 |
#include <stdlib.h> |
| 59 |
#include <string.h> |
| 60 |
#include <unistd.h> |
| 61 |
|
| 62 |
static int swapped; |
| 63 |
|
| 64 |
static void mark(const char *path) |
| 65 |
{ |
| 66 |
int fd; |
| 67 |
if (path && (fd = (int)syscall(SYS_open, path, |
| 68 |
O_WRONLY | O_CREAT | O_EXCL, 0600)) >= 0) |
| 69 |
close(fd); |
| 70 |
} |
| 71 |
|
| 72 |
static int fd_path_is(int fd, const char *wanted) |
| 73 |
{ |
| 74 |
char path[PATH_MAX]; |
| 75 |
return wanted && fcntl(fd, F_GETPATH, path) == 0 |
| 76 |
&& strcmp(path, wanted) == 0; |
| 77 |
} |
| 78 |
|
| 79 |
static void restore_partial(void) |
| 80 |
{ |
| 81 |
const char *partial = getenv("RSYNC_PARTIAL_RETRY_DIR"); |
| 82 |
const char *held = getenv("RSYNC_PARTIAL_RETRY_HELD"); |
| 83 |
if (swapped && partial && held) { |
| 84 |
unlink(partial); |
| 85 |
if (rename(held, partial) == 0) |
| 86 |
swapped = 0; |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
static int hook_openat(int dfd, const char *path, int flags, ...) |
| 91 |
{ |
| 92 |
const char *partial = getenv("RSYNC_PARTIAL_RETRY_DIR"); |
| 93 |
const char *held = getenv("RSYNC_PARTIAL_RETRY_HELD"); |
| 94 |
const char *secret = getenv("RSYNC_PARTIAL_RETRY_SECRET"); |
| 95 |
mode_t mode = 0; |
| 96 |
int fd, saved_errno; |
| 97 |
|
| 98 |
if (flags & O_CREAT) { |
| 99 |
va_list ap; |
| 100 |
va_start(ap, flags); |
| 101 |
mode = (mode_t)va_arg(ap, int); |
| 102 |
va_end(ap); |
| 103 |
} |
| 104 |
|
| 105 |
/* Model Linux fs.protected_regular: the operator-walk O_CREAT open of the |
| 106 |
* existing partial leaf gets EACCES. The attacker swaps the parent in the |
| 107 |
* exact interval before rsync's compatibility retry. */ |
| 108 |
if (!swapped && path && strcmp(path, "victim") == 0 |
| 109 |
&& (flags & O_ACCMODE) == O_WRONLY && (flags & O_CREAT) |
| 110 |
&& fd_path_is(dfd, partial)) { |
| 111 |
if (rename(partial, held) == 0 && symlink("secret", partial) == 0) { |
| 112 |
swapped = 1; |
| 113 |
mark(getenv("RSYNC_PARTIAL_RETRY_EACCES_MARKER")); |
| 114 |
errno = EACCES; |
| 115 |
return -1; |
| 116 |
} |
| 117 |
restore_partial(); |
| 118 |
} |
| 119 |
|
| 120 |
fd = (int)syscall(SYS_openat, dfd, path, flags, mode); |
| 121 |
saved_errno = errno; |
| 122 |
|
| 123 |
/* The vulnerable retry omits O_CREAT and resolves the swapped parent via |
| 124 |
* the ordinary path walker. Keep its returned fd alive but restore the |
| 125 |
* pathname immediately so later cleanup cannot hide the write target. */ |
| 126 |
if (swapped && fd >= 0 && path && strcmp(path, "victim") == 0 |
| 127 |
&& (flags & O_ACCMODE) == O_WRONLY && !(flags & O_CREAT) |
| 128 |
&& fd_path_is(dfd, secret)) { |
| 129 |
mark(getenv("RSYNC_PARTIAL_RETRY_OPEN_MARKER")); |
| 130 |
restore_partial(); |
| 131 |
} |
| 132 |
errno = saved_errno; |
| 133 |
return fd; |
| 134 |
} |
| 135 |
|
| 136 |
static int hook_open(const char *path, int flags, ...) |
| 137 |
{ |
| 138 |
const char *partial = getenv("RSYNC_PARTIAL_RETRY_DIR"); |
| 139 |
const char *held = getenv("RSYNC_PARTIAL_RETRY_HELD"); |
| 140 |
mode_t mode = 0; |
| 141 |
int fd, saved_errno; |
| 142 |
|
| 143 |
if (flags & O_CREAT) { |
| 144 |
va_list ap; |
| 145 |
va_start(ap, flags); |
| 146 |
mode = (mode_t)va_arg(ap, int); |
| 147 |
va_end(ap); |
| 148 |
} |
| 149 |
|
| 150 |
/* Public releases use plain open() for the first partial-file attempt. |
| 151 |
* Trigger the same EACCES/swap oracle so they serve as behavioral |
| 152 |
* controls: their older retry does not reopen the partial path. */ |
| 153 |
if (!swapped && path && strstr(path, "pdir/victim") |
| 154 |
&& (flags & O_ACCMODE) == O_WRONLY && (flags & O_CREAT)) { |
| 155 |
if (rename(partial, held) == 0 && symlink("secret", partial) == 0) { |
| 156 |
swapped = 1; |
| 157 |
mark(getenv("RSYNC_PARTIAL_RETRY_EACCES_MARKER")); |
| 158 |
errno = EACCES; |
| 159 |
return -1; |
| 160 |
} |
| 161 |
restore_partial(); |
| 162 |
} |
| 163 |
|
| 164 |
fd = (int)syscall(SYS_open, path, flags, mode); |
| 165 |
saved_errno = errno; |
| 166 |
|
| 167 |
/* A local receiver uses the same vulnerable policy transition but its |
| 168 |
* final fallback is plain open(). Restore the pathname after pinning the |
| 169 |
* redirected descriptor so later partial-file finalization cannot move |
| 170 |
* the target and obscure which file received the payload. */ |
| 171 |
if (swapped && fd >= 0 && path && strstr(path, "pdir/victim") |
| 172 |
&& (flags & O_ACCMODE) == O_WRONLY && !(flags & O_CREAT)) { |
| 173 |
mark(getenv("RSYNC_PARTIAL_RETRY_OPEN_MARKER")); |
| 174 |
restore_partial(); |
| 175 |
} |
| 176 |
errno = saved_errno; |
| 177 |
return fd; |
| 178 |
} |
| 179 |
|
| 180 |
static int hook_fstatat(int dfd, const char *path, struct stat *st, int flags) |
| 181 |
{ |
| 182 |
int rc = (int)syscall(SYS_fstatat64, dfd, path, st, flags); |
| 183 |
int saved_errno = errno; |
| 184 |
/* If the retry retained the operator walk it would inspect the raced |
| 185 |
* symlink. Model the attacker as a different uid so that check refuses it. */ |
| 186 |
if (rc == 0 && swapped && path && strcmp(path, "pdir") == 0 |
| 187 |
&& S_ISLNK(st->st_mode)) { |
| 188 |
st->st_uid = geteuid() + 1; |
| 189 |
mark(getenv("RSYNC_PARTIAL_RETRY_FOREIGN_MARKER")); |
| 190 |
} |
| 191 |
errno = saved_errno; |
| 192 |
return rc; |
| 193 |
} |
| 194 |
|
| 195 |
__attribute__((constructor)) static void hook_loaded(void) |
| 196 |
{ |
| 197 |
mark(getenv("RSYNC_PARTIAL_RETRY_LOAD_MARKER")); |
| 198 |
} |
| 199 |
|
| 200 |
__attribute__((destructor)) static void hook_unload(void) |
| 201 |
{ |
| 202 |
restore_partial(); |
| 203 |
} |
| 204 |
|
| 205 |
__attribute__((used)) static struct { |
| 206 |
const void *replacement; |
| 207 |
const void *replacee; |
| 208 |
} interposers[] __attribute__((section("__DATA,__interpose"))) = { |
| 209 |
{ (const void *)hook_open, (const void *)open }, |
| 210 |
{ (const void *)hook_openat, (const void *)openat }, |
| 211 |
{ (const void *)hook_fstatat, (const void *)fstatat }, |
| 212 |
}; |
| 213 |
''') |
| 214 |
|
| 215 |
build = subprocess.run( |
| 216 |
['cc', '-dynamiclib', '-o', str(hook_lib), str(hook_src)], |
| 217 |
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True) |
| 218 |
if build.returncode != 0: |
| 219 |
test_skipped(f'cannot build dyld partial retry interposer: {build.stdout!r}') |
| 220 |
|
| 221 |
env_assignments = { |
| 222 |
'DYLD_INSERT_LIBRARIES': str(hook_lib), |
| 223 |
'RSYNC_PARTIAL_RETRY_DIR': str(partial), |
| 224 |
'RSYNC_PARTIAL_RETRY_HELD': str(held), |
| 225 |
'RSYNC_PARTIAL_RETRY_SECRET': str(secret), |
| 226 |
'RSYNC_PARTIAL_RETRY_LOAD_MARKER': str(load_marker), |
| 227 |
'RSYNC_PARTIAL_RETRY_EACCES_MARKER': str(eacces_marker), |
| 228 |
'RSYNC_PARTIAL_RETRY_OPEN_MARKER': str(retry_marker), |
| 229 |
'RSYNC_PARTIAL_RETRY_FOREIGN_MARKER': str(foreign_check_marker), |
| 230 |
} |
| 231 |
run_env = os.environ.copy() |
| 232 |
run_env.update(env_assignments) |
| 233 |
|
| 234 |
result = subprocess.run( |
| 235 |
rsync_argv('-rtI', '--partial', '--partial-dir=pdir', |
| 236 |
'--no-inc-recursive', f'{src}/', f'{mod}/'), |
| 237 |
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, env=run_env) |
| 238 |
|
| 239 |
if held.exists(): |
| 240 |
if partial.is_symlink(): |
| 241 |
partial.unlink() |
| 242 |
held.rename(partial) |
| 243 |
if not load_marker.exists(): |
| 244 |
test_fail( |
| 245 |
'positive control failed: dyld retry interposer was not loaded: ' |
| 246 |
f'rc={result.returncode}, output={result.stdout!r}') |
| 247 |
if not eacces_marker.exists(): |
| 248 |
test_fail( |
| 249 |
'positive control failed: receiver did not open the existing partial ' |
| 250 |
f'file with O_CREAT (rc={result.returncode}, output={result.stdout!r})') |
| 251 |
observed = (secret / 'victim').read_bytes() |
| 252 |
if observed == new_source: |
| 253 |
test_fail( |
| 254 |
'EACCES recovery dropped partial-dir ownership policy and overwrote a ' |
| 255 |
'different module file through the raced partial-dir symlink') |
| 256 |
if observed != secret_data: |
| 257 |
test_fail(f'outside-policy target has unexpected contents after retry: ' |
| 258 |
f'rc={result.returncode}, output={result.stdout!r}') |
| 259 |
|
| 260 |
|
| 261 |
if not foreign_check_marker.exists(): |
| 262 |
test_fail('inconclusive: the recovery never inspected the raced partial-dir ' |
| 263 |
f'symlink, so no ownership check ran: rc={result.returncode}, ' |
| 264 |
f'output={result.stdout!r}') |
| 265 |
print('EACCES recovery retained partial-dir ownership policy') |