| 1 |
From 324a1f0716aa75d311f879864f48408d5723d963 Mon Sep 17 00:00:00 2001 |
| 2 |
From: Zen Dodd <[email protected]> |
| 3 |
Date: Sun, 16 Aug 2026 21:51:07 +1000 |
| 4 |
Subject: [PATCH] rrsync: support fd pins in user namespaces (#1048) |
| 5 |
|
| 6 |
* rrsync: support fd pins in user namespaces |
| 7 |
* rrsync: support /dev/fd pins in user namespaces |
| 8 |
--- |
| 9 |
syscall.c | 26 +++-- |
| 10 |
testsuite/rrsync-userns-procfs_test.py | 133 +++++++++++++++++++++++++ |
| 11 |
testsuite/skiplist/cygwin.txt | 1 + |
| 12 |
testsuite/skiplist/macos.txt | 1 + |
| 13 |
4 files changed, 153 insertions(+), 8 deletions(-) |
| 14 |
create mode 100644 testsuite/rrsync-userns-procfs_test.py |
| 15 |
|
| 16 |
diff --git a/syscall.c b/syscall.c |
| 17 |
index 5e92edca0..68a7eea31 100644 |
| 18 |
--- a/syscall.c |
| 19 |
+++ b/syscall.c |
| 20 |
@@ -143,13 +143,18 @@ static const char *confinement_root(unsigned int *lenp) |
| 21 |
return confine_root; |
| 22 |
} |
| 23 |
|
| 24 |
-/* Split the "/proc/<self|pid>/fd" prefix off `p`, returning the tail -- "" for |
| 25 |
- * the pin directory itself, otherwise a string starting with '/'. NULL when `p` |
| 26 |
- * is not in the fd-pin namespace at all. */ |
| 27 |
+/* Split a recognised fd-pin prefix off `p`, returning the tail -- "" for the |
| 28 |
+ * pin directory itself, otherwise a string starting with '/'. NULL when `p` |
| 29 |
+ * is not in an fd-pin namespace. */ |
| 30 |
static const char *fd_pin_tail(const char *p) |
| 31 |
{ |
| 32 |
const char *s; |
| 33 |
|
| 34 |
+ if (strncmp(p, "/dev/fd", 7) == 0) { |
| 35 |
+ s = p + 7; |
| 36 |
+ return (*s == '\0' || *s == '/') ? s : NULL; |
| 37 |
+ } |
| 38 |
+ |
| 39 |
if (strncmp(p, "/proc/", 6) != 0) |
| 40 |
return NULL; |
| 41 |
s = p + 6; |
| 42 |
@@ -168,8 +173,8 @@ static const char *fd_pin_tail(const char *p) |
| 43 |
return (*s == '\0' || *s == '/') ? s : NULL; |
| 44 |
} |
| 45 |
|
| 46 |
-/* An EXACT pin entry, "/proc/self/fd/7" -- the one spelling whose target is what |
| 47 |
- * confinement must judge. rrsync also writes a pinned parent as |
| 48 |
+/* An EXACT pin entry, such as "/proc/self/fd/7" or "/dev/fd/7", whose target is |
| 49 |
+ * what confinement must judge. rrsync also writes a pinned parent as |
| 50 |
* ".../fd/7/<leaf>", but the walk resolves the magic link itself and checks the |
| 51 |
* components past it, so only the bare entry is resolved here. Requiring all |
| 52 |
* digits keeps a planted name like ".../fd/outside-secret" out. */ |
| 53 |
@@ -401,9 +406,14 @@ static int ona_open(const char *path, int flags, mode_t mode, char *out_abs, siz |
| 54 |
} |
| 55 |
|
| 56 |
if (S_ISLNK(lst.st_mode)) { |
| 57 |
- /* Symlink: untrusted owner is refused; trusted owner |
| 58 |
- * is followed via readlinkat + splice. */ |
| 59 |
- if (lst.st_uid != 0 && lst.st_uid != trusted_uid) { |
| 60 |
+ /* Symlink: untrusted owner is refused; trusted owner is followed |
| 61 |
+ * via readlinkat + splice. In a user namespace the /proc/self and |
| 62 |
+ * /dev/fd symlinks may report the overflow uid, so |
| 63 |
+ * allow those exact components while traversing a recognised pin. */ |
| 64 |
+ int namespace_pin = pin_transit |
| 65 |
+ && ((strcmp(abspath, "/proc") == 0 && strcmp(comp, "self") == 0) |
| 66 |
+ || (strcmp(abspath, "/dev") == 0 && strcmp(comp, "fd") == 0)); |
| 67 |
+ if (!namespace_pin && lst.st_uid != 0 && lst.st_uid != trusted_uid) { |
| 68 |
saved_errno = ELOOP; |
| 69 |
goto out; |
| 70 |
} |
| 71 |
diff --git a/testsuite/rrsync-userns-procfs_test.py b/testsuite/rrsync-userns-procfs_test.py |
| 72 |
new file mode 100644 |
| 73 |
index 000000000..fa2aaf3dc |
| 74 |
--- /dev/null |
| 75 |
+++ b/testsuite/rrsync-userns-procfs_test.py |
| 76 |
@@ -0,0 +1,133 @@ |
| 77 |
+#!/usr/bin/env python3 |
| 78 |
+"""A confined fd pin must remain usable inside a Linux user namespace.""" |
| 79 |
+ |
| 80 |
+import os |
| 81 |
+import shlex |
| 82 |
+import shutil |
| 83 |
+import subprocess |
| 84 |
+import sys |
| 85 |
+import tempfile |
| 86 |
+from pathlib import Path |
| 87 |
+ |
| 88 |
+from rsyncfns import makepath, rmtree, rsync_argv, test_fail, test_skipped |
| 89 |
+ |
| 90 |
+ |
| 91 |
+if not sys.platform.startswith('linux'): |
| 92 |
+ test_skipped('rrsync-userns-procfs is Linux-specific') |
| 93 |
+ |
| 94 |
+if not os.environ.get('RSYNC_USERNS_PROCFS'): |
| 95 |
+ unshare = shutil.which('unshare') |
| 96 |
+ if unshare is None: |
| 97 |
+ test_skipped('unshare is unavailable') |
| 98 |
+ env = os.environ.copy() |
| 99 |
+ env['RSYNC_USERNS_PROCFS'] = '1' |
| 100 |
+ launch_dir = Path(tempfile.mkdtemp(prefix='rsync-userns-launch-')) |
| 101 |
+ launch_dir.chmod(0o755) |
| 102 |
+ testdir = Path(__file__).resolve().parent |
| 103 |
+ child_test = launch_dir / Path(__file__).name |
| 104 |
+ for source in (Path(__file__), testdir / 'rsyncfns.py', |
| 105 |
+ testdir / 'exitcodes.py'): |
| 106 |
+ shutil.copy2(source, launch_dir / source.name) |
| 107 |
+ rsync_cmd = shlex.split(env['RSYNC']) |
| 108 |
+ for i, arg in enumerate(rsync_cmd): |
| 109 |
+ if Path(arg).name in ('rsync', 'rsync.exe') and Path(arg).is_file(): |
| 110 |
+ staged_rsync = launch_dir / Path(arg).name |
| 111 |
+ shutil.copy2(arg, staged_rsync) |
| 112 |
+ staged_rsync.chmod(0o755) |
| 113 |
+ rsync_cmd[i] = str(staged_rsync) |
| 114 |
+ break |
| 115 |
+ else: |
| 116 |
+ rmtree(launch_dir) |
| 117 |
+ test_fail(f'cannot locate the rsync executable in {env["RSYNC"]!r}') |
| 118 |
+ env['RSYNC'] = shlex.join(rsync_cmd) |
| 119 |
+ launcher = [] |
| 120 |
+ if os.geteuid() == 0: |
| 121 |
+ setpriv = shutil.which('setpriv') |
| 122 |
+ if setpriv is None: |
| 123 |
+ test_skipped('setpriv is unavailable for the root-run testsuite') |
| 124 |
+ launcher = [setpriv, '--reuid=65534', '--regid=65534', '--clear-groups'] |
| 125 |
+ unshare_argv = [unshare, '--user', '--map-root-user', '--mount', '--pid', |
| 126 |
+ '--fork', '--mount-proc'] |
| 127 |
+ probe = subprocess.run( |
| 128 |
+ launcher + unshare_argv + ['true'], |
| 129 |
+ stdin=subprocess.DEVNULL, |
| 130 |
+ stdout=subprocess.DEVNULL, |
| 131 |
+ stderr=subprocess.DEVNULL, |
| 132 |
+ ) |
| 133 |
+ if probe.returncode != 0: |
| 134 |
+ rmtree(launch_dir) |
| 135 |
+ print(f'user namespaces unavailable (rc={probe.returncode})') |
| 136 |
+ raise SystemExit(0) |
| 137 |
+ try: |
| 138 |
+ proc = subprocess.run( |
| 139 |
+ launcher + unshare_argv |
| 140 |
+ + [sys.executable, str(child_test)], |
| 141 |
+ env=env, |
| 142 |
+ timeout=30, |
| 143 |
+ ) |
| 144 |
+ except subprocess.TimeoutExpired: |
| 145 |
+ test_fail('user-namespace regression test timed out') |
| 146 |
+ finally: |
| 147 |
+ rmtree(launch_dir) |
| 148 |
+ if proc.returncode != 0: |
| 149 |
+ test_fail(f'user-namespace regression test failed (rc={proc.returncode})') |
| 150 |
+ print('rrsync fd pin works inside a user namespace') |
| 151 |
+ raise SystemExit(0) |
| 152 |
+ |
| 153 |
+proc_uid = os.lstat('/proc/self').st_uid |
| 154 |
+if proc_uid in (0, os.geteuid()): |
| 155 |
+ test_skipped('/proc/self does not expose an overflow uid in this namespace') |
| 156 |
+ |
| 157 |
+base = Path(tempfile.mkdtemp(prefix='rsync-userns-procfs-')) |
| 158 |
+src = base / 'src' |
| 159 |
+dest = base / 'dest' |
| 160 |
+outside = base / 'outside' |
| 161 |
+makepath(src, dest, outside) |
| 162 |
+(src / 'file').write_text('content\n') |
| 163 |
+ |
| 164 |
+fd_roots = ['/proc/self/fd'] |
| 165 |
+if Path('/dev/fd').exists(): |
| 166 |
+ fd_roots.append('/dev/fd') |
| 167 |
+ |
| 168 |
+dest_fd = os.open(dest, os.O_RDONLY | os.O_DIRECTORY) |
| 169 |
+try: |
| 170 |
+ for index, fd_root in enumerate(fd_roots): |
| 171 |
+ log_file = dest / f'rsync-{index}.log' |
| 172 |
+ proc = subprocess.run( |
| 173 |
+ rsync_argv('-a', f'--confine-root={dest}', |
| 174 |
+ f'--log-file={fd_root}/{dest_fd}/{log_file.name}', |
| 175 |
+ str(src) + '/', str(dest) + '/'), |
| 176 |
+ pass_fds=(dest_fd,), |
| 177 |
+ capture_output=True, |
| 178 |
+ text=True, |
| 179 |
+ ) |
| 180 |
+ ctx = (f'fd_root={fd_root!r}, rc={proc.returncode}, ' |
| 181 |
+ f'stderr={proc.stderr.strip()[:300]!r}') |
| 182 |
+ if proc.returncode != 0: |
| 183 |
+ test_fail(f'confined transfer through an fd pin failed ({ctx})') |
| 184 |
+ if not log_file.is_file(): |
| 185 |
+ test_fail(f'confined log path through an fd pin was rejected ({ctx})') |
| 186 |
+ if (dest / 'file').read_text() != 'content\n': |
| 187 |
+ test_fail(f'confined transfer did not deliver the file ({ctx})') |
| 188 |
+finally: |
| 189 |
+ os.close(dest_fd) |
| 190 |
+ |
| 191 |
+outside_list = outside / 'files-from' |
| 192 |
+outside_list.write_text('file\n') |
| 193 |
+for fd_root in fd_roots: |
| 194 |
+ outside_fd = os.open(outside_list, os.O_RDONLY) |
| 195 |
+ try: |
| 196 |
+ proc = subprocess.run( |
| 197 |
+ rsync_argv('-a', f'--confine-root={dest}', |
| 198 |
+ f'--files-from={fd_root}/{outside_fd}', |
| 199 |
+ str(src) + '/', str(dest) + '/'), |
| 200 |
+ pass_fds=(outside_fd,), |
| 201 |
+ capture_output=True, |
| 202 |
+ text=True, |
| 203 |
+ ) |
| 204 |
+ finally: |
| 205 |
+ os.close(outside_fd) |
| 206 |
+ if proc.returncode == 0 or 'failed to open files-from file' not in proc.stderr: |
| 207 |
+ test_fail(f'outside {fd_root} pin was not observably refused: ' |
| 208 |
+ f'rc={proc.returncode}, stderr={proc.stderr!r}') |
| 209 |
+rmtree(base) |
| 210 |
diff --git a/testsuite/skiplist/cygwin.txt b/testsuite/skiplist/cygwin.txt |
| 211 |
index ba499cc54..46060e16f 100644 |
| 212 |
--- a/testsuite/skiplist/cygwin.txt |
| 213 |
+++ b/testsuite/skiplist/cygwin.txt |
| 214 |
@@ -56,6 +56,7 @@ rename-mixed-parent-transfer |
| 215 |
rrsync-sender-leaf-flip |
| 216 |
rrsync-sender-parent-pin |
| 217 |
rrsync-symlink |
| 218 |
+rrsync-userns-procfs |
| 219 |
sender-remove-source-root-anchor |
| 220 |
simd-checksum |
| 221 |
source-change-size-continues |
| 222 |
diff --git a/testsuite/skiplist/macos.txt b/testsuite/skiplist/macos.txt |
| 223 |
index 3e7653ce8..5952c8143 100644 |
| 224 |
--- a/testsuite/skiplist/macos.txt |
| 225 |
+++ b/testsuite/skiplist/macos.txt |
| 226 |
@@ -26,6 +26,7 @@ readonly-partial-abort-mode-regression # |
| 227 |
rrsync-sender-leaf-flip |
| 228 |
rrsync-sender-parent-pin |
| 229 |
rrsync-symlink |
| 230 |
+rrsync-userns-procfs |
| 231 |
sender-remove-source-root-anchor |
| 232 |
simd-checksum |
| 233 |
source-change-size-continues |