| 1 |
|
| 2 |
"""Fake-super backup fallback must retain an inert FIFO placeholder.""" |
| 3 |
|
| 4 |
import os |
| 5 |
import platform |
| 6 |
import stat |
| 7 |
import subprocess |
| 8 |
|
| 9 |
from rsyncfns import ( |
| 10 |
SCRATCHDIR, rmtree, rsync_argv, test_fail, test_skipped, xattrs_supported, |
| 11 |
) |
| 12 |
|
| 13 |
|
| 14 |
system = platform.system() |
| 15 |
if system not in ('Darwin', 'Linux'): |
| 16 |
test_skipped('deterministic fast-path failure needs dyld/ELF interposing') |
| 17 |
if not xattrs_supported(): |
| 18 |
test_skipped('fake-super FIFO setup requires xattr support') |
| 19 |
|
| 20 |
base = (SCRATCHDIR / 'fake-super-backup-fifo-regression').resolve() |
| 21 |
rmtree(base) |
| 22 |
src = base / 'src' |
| 23 |
mod = base / 'mod' |
| 24 |
dest_sub = mod / 'slot' |
| 25 |
backup_root = base / 'backup' |
| 26 |
backup_sub = backup_root / 'slot' |
| 27 |
load_marker = base / 'hook-loaded' |
| 28 |
fallback_marker = base / 'backup-fast-path-failed' |
| 29 |
for directory in (src / 'slot', dest_sub, backup_sub): |
| 30 |
directory.mkdir(parents=True) |
| 31 |
|
| 32 |
source_victim = src / 'slot' / 'victim' |
| 33 |
os.mkfifo(source_victim, 0o600) |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
hook_src = base / 'fake-super-backup-hook.c' |
| 39 |
hook_lib = base / ('fake-super-backup-hook.dylib' |
| 40 |
if system == 'Darwin' else 'fake-super-backup-hook.so') |
| 41 |
hook_src.write_text(r''' |
| 42 |
#include <sys/types.h> |
| 43 |
#include <sys/stat.h> |
| 44 |
#include <sys/syscall.h> |
| 45 |
#include <errno.h> |
| 46 |
#include <fcntl.h> |
| 47 |
#include <stdio.h> |
| 48 |
#include <stdlib.h> |
| 49 |
#include <string.h> |
| 50 |
#include <unistd.h> |
| 51 |
|
| 52 |
static int claim(const char *path) |
| 53 |
{ |
| 54 |
int fd; |
| 55 |
if (!path || (fd = open(path, O_WRONLY | O_CREAT | O_EXCL, 0600)) < 0) |
| 56 |
return 0; |
| 57 |
close(fd); |
| 58 |
return 1; |
| 59 |
} |
| 60 |
|
| 61 |
static const char *leaf(const char *path) |
| 62 |
{ |
| 63 |
const char *slash = path ? strrchr(path, '/') : NULL; |
| 64 |
return slash ? slash + 1 : path; |
| 65 |
} |
| 66 |
|
| 67 |
static int victim_pair(const char *oldname, const char *newname) |
| 68 |
{ |
| 69 |
oldname = leaf(oldname); |
| 70 |
newname = leaf(newname); |
| 71 |
return oldname && newname && strcmp(oldname, "victim") == 0 |
| 72 |
&& strcmp(newname, "victim") == 0; |
| 73 |
} |
| 74 |
|
| 75 |
static int fail_pair(const char *oldname, const char *newname, int err) |
| 76 |
{ |
| 77 |
if (victim_pair(oldname, newname) |
| 78 |
&& claim(getenv("RSYNC_FAKE_BACKUP_LINK_MARKER"))) { |
| 79 |
errno = err; |
| 80 |
return 1; |
| 81 |
} |
| 82 |
return 0; |
| 83 |
} |
| 84 |
|
| 85 |
static int hook_link(const char *oldname, const char *newname) |
| 86 |
{ |
| 87 |
if (fail_pair(oldname, newname, EPERM)) |
| 88 |
return -1; |
| 89 |
#ifdef SYS_link |
| 90 |
return (int)syscall(SYS_link, oldname, newname); |
| 91 |
#else |
| 92 |
return (int)syscall(SYS_linkat, AT_FDCWD, oldname, |
| 93 |
AT_FDCWD, newname, 0); |
| 94 |
#endif |
| 95 |
} |
| 96 |
|
| 97 |
static int hook_rename(const char *oldname, const char *newname) |
| 98 |
{ |
| 99 |
if (fail_pair(oldname, newname, EXDEV)) |
| 100 |
return -1; |
| 101 |
#ifdef SYS_rename |
| 102 |
return (int)syscall(SYS_rename, oldname, newname); |
| 103 |
#else |
| 104 |
return (int)syscall(SYS_renameat, AT_FDCWD, oldname, |
| 105 |
AT_FDCWD, newname); |
| 106 |
#endif |
| 107 |
} |
| 108 |
|
| 109 |
static int hook_linkat(int oldfd, const char *oldname, |
| 110 |
int newfd, const char *newname, int flags) |
| 111 |
{ |
| 112 |
if (fail_pair(oldname, newname, EPERM)) |
| 113 |
return -1; |
| 114 |
return (int)syscall(SYS_linkat, oldfd, oldname, newfd, newname, flags); |
| 115 |
} |
| 116 |
|
| 117 |
static int hook_renameat(int oldfd, const char *oldname, |
| 118 |
int newfd, const char *newname) |
| 119 |
{ |
| 120 |
if (fail_pair(oldname, newname, EXDEV)) |
| 121 |
return -1; |
| 122 |
return (int)syscall(SYS_renameat, oldfd, oldname, newfd, newname); |
| 123 |
} |
| 124 |
|
| 125 |
__attribute__((constructor)) static void hook_loaded(void) |
| 126 |
{ |
| 127 |
claim(getenv("RSYNC_FAKE_BACKUP_LOAD_MARKER")); |
| 128 |
} |
| 129 |
|
| 130 |
#ifdef __APPLE__ |
| 131 |
__attribute__((used)) static struct { |
| 132 |
const void *replacement; |
| 133 |
const void *replacee; |
| 134 |
} interposers[] __attribute__((section("__DATA,__interpose"))) = { |
| 135 |
{ (const void *)hook_link, (const void *)link }, |
| 136 |
{ (const void *)hook_rename, (const void *)rename }, |
| 137 |
{ (const void *)hook_linkat, (const void *)linkat }, |
| 138 |
{ (const void *)hook_renameat, (const void *)renameat }, |
| 139 |
}; |
| 140 |
#else |
| 141 |
int link(const char *oldname, const char *newname) |
| 142 |
{ |
| 143 |
return hook_link(oldname, newname); |
| 144 |
} |
| 145 |
|
| 146 |
int rename(const char *oldname, const char *newname) |
| 147 |
{ |
| 148 |
return hook_rename(oldname, newname); |
| 149 |
} |
| 150 |
|
| 151 |
int linkat(int oldfd, const char *oldname, |
| 152 |
int newfd, const char *newname, int flags) |
| 153 |
{ |
| 154 |
return hook_linkat(oldfd, oldname, newfd, newname, flags); |
| 155 |
} |
| 156 |
|
| 157 |
int renameat(int oldfd, const char *oldname, |
| 158 |
int newfd, const char *newname) |
| 159 |
{ |
| 160 |
return hook_renameat(oldfd, oldname, newfd, newname); |
| 161 |
} |
| 162 |
#endif |
| 163 |
''') |
| 164 |
|
| 165 |
build_args = (['cc', '-dynamiclib'] if system == 'Darwin' |
| 166 |
else ['cc', '-shared', '-fPIC']) |
| 167 |
build = subprocess.run( |
| 168 |
[*build_args, '-o', str(hook_lib), str(hook_src)], |
| 169 |
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True) |
| 170 |
if build.returncode != 0: |
| 171 |
test_skipped(f'cannot build dyld interposer: {build.stdout!r}') |
| 172 |
|
| 173 |
seed = subprocess.run( |
| 174 |
rsync_argv('-rtI', '--specials', '--fake-super', f'{src}/', f'{mod}/'), |
| 175 |
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True) |
| 176 |
if seed.returncode != 0: |
| 177 |
test_fail(f'initial fake-super FIFO upload failed: {seed.stdout!r}') |
| 178 |
placeholder = dest_sub / 'victim' |
| 179 |
if not stat.S_ISREG(os.lstat(placeholder).st_mode): |
| 180 |
test_fail('initial FIFO was not represented by an inert regular file') |
| 181 |
|
| 182 |
source_victim.unlink() |
| 183 |
source_victim.write_text('NEW-REMOTE-DATA\n') |
| 184 |
|
| 185 |
result = subprocess.run( |
| 186 |
rsync_argv('-rtI', '--specials', '--fake-super', '--backup', |
| 187 |
f'--backup-dir={backup_root}', '--no-inc-recursive', |
| 188 |
f'{src}/', f'{mod}/'), |
| 189 |
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, |
| 190 |
env={**os.environ, |
| 191 |
('DYLD_INSERT_LIBRARIES' if system == 'Darwin' else 'LD_PRELOAD'): |
| 192 |
str(hook_lib), |
| 193 |
'RSYNC_FAKE_BACKUP_LOAD_MARKER': str(load_marker), |
| 194 |
'RSYNC_FAKE_BACKUP_LINK_MARKER': str(fallback_marker)}) |
| 195 |
if result.returncode != 0: |
| 196 |
test_fail(f'fake-super backup failed: {result.stdout!r}') |
| 197 |
if not load_marker.exists() or not fallback_marker.exists(): |
| 198 |
test_fail(f'positive control did not force the backup fallback: {result.stdout!r}') |
| 199 |
|
| 200 |
backup = backup_sub / 'victim' |
| 201 |
backup_mode = os.lstat(backup).st_mode |
| 202 |
if stat.S_ISFIFO(backup_mode): |
| 203 |
test_fail('fake-super backup materialized a live FIFO') |
| 204 |
if not stat.S_ISREG(backup_mode): |
| 205 |
test_fail(f'unexpected fake-super backup type: {backup_mode:o}') |
| 206 |
|
| 207 |
print('fake-super backup remained an inert regular placeholder') |