Skip to content

Commit 92a2a09

Browse files
sairex53steadytao
andauthored
rrsync: fix uploads to existing file destinations (#1136)
* rrsync: pin the parent of existing receiver file arguments Passing an existing destination as the leaf's /proc/self/fd/N makes rsync lstat the procfs link and try to replace it, failing with EPERM. Use the held parent fd plus the leaf name, as for a missing destination. Keep directory arguments pinned directly and verify the parent's leaf still matches the already validated inode before releasing its fd. Reuse pin_dir() for the parent checks and execute-only directory support. Add end-to-end coverage for overwrite, creation, directories, pulls, rejected paths, an in-place leaf replacement and a parent rename. Based on the parent-pin approach described by JeanFrancoisGagne in #1107. Fixes #1107. Assisted-by: OpenAI Codex Signed-off-by: Eduard Zateev <hackerowskiy@gmail.com> * testsuite: expect rrsync file-argument skips * testsuite: allow slow aligned overflow scans --------- Signed-off-by: Eduard Zateev <hackerowskiy@gmail.com> Co-authored-by: Zen Dodd <mail@steadytao.com>
1 parent f62f687 commit 92a2a09

5 files changed

Lines changed: 170 additions & 2 deletions

File tree

‎support/rrsync‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ def create_pinned_dir(path, orig_arg, mode):
376376
pinned_fds.append(fd)
377377
return '/proc/self/fd/%d' % fd
378378

379-
# sender_pinned_arg() verdicts that are not a rewritten argument.
379+
# Pinned-argument verdicts that are not a rewritten argument.
380380
KEEP_LEAF_PIN = 'keep' # hand rsync the leaf's own /proc/self/fd/N
381381
LEAF_PIN_UNUSABLE = 'unusable' # no pin for this shape; keep the plain name
382382

@@ -453,6 +453,22 @@ def sender_pinned_arg(fd, arg, orig_arg, has_slash, has_slash_dot):
453453
die('post-pin path changed (race?):', orig_arg, check)
454454
return pinned
455455

456+
def receiver_pinned_arg(fd, arg, orig_arg):
457+
"""Keep existing file destinations writable beneath a pinned parent."""
458+
leaf_st = os.fstat(fd)
459+
if stat.S_ISDIR(leaf_st.st_mode):
460+
return KEEP_LEAF_PIN
461+
462+
parent, leaf = os.path.split(arg)
463+
dfd = pin_dir(parent, orig_arg)
464+
try:
465+
st = os.stat(leaf, dir_fd=dfd, follow_symlinks=False)
466+
except OSError as e:
467+
die('post-pin stat failed (race?):', orig_arg, e.strerror)
468+
if (st.st_dev, st.st_ino) != (leaf_st.st_dev, leaf_st.st_ino):
469+
die('post-pin path changed (race?):', orig_arg, leaf)
470+
return '/proc/self/fd/%d/%s' % (dfd, leaf)
471+
456472
def safe_open_logfile():
457473
nofollow = getattr(os, 'O_NOFOLLOW', 0)
458474
try:
@@ -928,6 +944,8 @@ def validated_arg(opt, arg, typ=3, wild=False):
928944
pinned = sender_pinned_arg(fd, logical, orig_arg,
929945
arg_has_trailing_slash,
930946
arg_has_trailing_slash_dot)
947+
elif opt == 'arg':
948+
pinned = receiver_pinned_arg(fd, real_arg, orig_arg)
931949
else:
932950
pinned = KEEP_LEAF_PIN
933951
if pinned == KEEP_LEAF_PIN:

‎testsuite/aligned_overflow_test.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ def drain_stream():
156156
)
157157
receiver.send_data(buf)
158158

159-
rc = proc.wait(timeout=120)
159+
rc = proc.wait(timeout=240)
160160
t.join(timeout=5)
161161
finally:
162162
if csock is not None:
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
#!/usr/bin/env python3
2+
"""Receiver file arguments must remain beneath a pinned parent."""
3+
4+
import os
5+
import shlex
6+
import subprocess
7+
import time
8+
9+
from rsyncfns import (
10+
SCRATCHDIR, makepath, patched_rrsync, proc_self_fd_pins, rmtree,
11+
rsync_argv, test_fail, test_skipped,
12+
)
13+
14+
if not proc_self_fd_pins():
15+
test_skipped('requires /proc/self/fd directory pins')
16+
17+
base = SCRATCHDIR / 'rrsync-receiver-file'
18+
rmtree(base)
19+
restricted = base / 'restricted'
20+
outside = base / 'outside'
21+
source = base / 'source'
22+
downloads = base / 'downloads'
23+
makepath(restricted, outside, downloads)
24+
source.write_text('replacement contents\n')
25+
26+
# The optional gate pauses the real server after rrsync validates its argument.
27+
shim = base / 'rsync-shim'
28+
shim.write_text('''#!/usr/bin/env python3
29+
import os, pathlib, sys, time
30+
ready = os.environ.get('RRSYNC_TEST_READY')
31+
if ready:
32+
pathlib.Path(ready).touch()
33+
go = pathlib.Path(os.environ['RRSYNC_TEST_GO'])
34+
end = time.monotonic() + 10
35+
while not go.exists():
36+
if time.monotonic() >= end:
37+
sys.exit('timed out waiting for test gate')
38+
time.sleep(0.01)
39+
cmd = %r
40+
os.execvp(cmd[0], cmd + sys.argv[1:])
41+
''' % rsync_argv())
42+
shim.chmod(0o755)
43+
rrsync = patched_rrsync(base, rsync_path=str(shim))
44+
rsh = base / 'fake-rsh'
45+
rsh.write_text('#!/bin/sh\nshift\nSSH_ORIGINAL_COMMAND="$*"\n'
46+
'export SSH_ORIGINAL_COMMAND\nexec %s -no-lock %s\n'
47+
% (shlex.quote(str(rrsync)), shlex.quote(str(restricted))))
48+
rsh.chmod(0o755)
49+
50+
51+
def command(src, dest, *options):
52+
return rsync_argv('-ac', '-e', str(rsh), *options, str(src), str(dest))
53+
54+
55+
def transfer(src, dest, *options, succeeds=True):
56+
proc = subprocess.run(command(src, dest, *options), capture_output=True,
57+
text=True, timeout=20)
58+
if (proc.returncode == 0) != succeeds:
59+
test_fail(f'transfer {src} -> {dest}: rc={proc.returncode}, '
60+
f'{proc.stdout!r} {proc.stderr!r}')
61+
return proc
62+
63+
64+
def check_contents(path):
65+
if path.is_symlink() or not path.is_file() or path.read_text() != source.read_text():
66+
test_fail(f'wrong file contents or type: {path}')
67+
68+
69+
for option in ((), ('--inplace',)):
70+
destination = restricted / 'existing'
71+
destination.write_text('old\n')
72+
transfer(source, 'dummy:existing', *option)
73+
check_contents(destination)
74+
new = restricted / 'new'
75+
new.unlink(missing_ok=True)
76+
transfer(source, 'dummy:new', *option)
77+
check_contents(new)
78+
79+
makepath(restricted / 'directory')
80+
transfer(source, 'dummy:directory/')
81+
check_contents(restricted / 'directory' / source.name)
82+
transfer('dummy:existing', str(downloads) + '/')
83+
check_contents(downloads / 'existing')
84+
85+
marker = outside / 'untouched'
86+
marker.write_text('outside fixture\n')
87+
transfer(source, 'dummy:../outside/untouched', succeeds=False)
88+
(restricted / 'outside-link').symlink_to(outside, target_is_directory=True)
89+
transfer(source, 'dummy:outside-link/untouched', succeeds=False)
90+
if marker.read_text() != 'outside fixture\n':
91+
test_fail('a rejected destination changed the outside fixture')
92+
93+
94+
def gated_transfer(name, change):
95+
ready = base / 'ready'
96+
go = base / 'go'
97+
ready.unlink(missing_ok=True)
98+
go.unlink(missing_ok=True)
99+
env = {**os.environ, 'RRSYNC_TEST_READY': str(ready), 'RRSYNC_TEST_GO': str(go)}
100+
proc = subprocess.Popen(command(source, f'dummy:{name}', '--inplace'),
101+
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
102+
text=True, env=env)
103+
try:
104+
end = time.monotonic() + 10
105+
while not ready.exists():
106+
if proc.poll() is not None or time.monotonic() >= end:
107+
test_fail('receiver did not reach the post-validation gate')
108+
time.sleep(0.01)
109+
change()
110+
go.touch()
111+
stdout, stderr = proc.communicate(timeout=20)
112+
if marker.read_text() != 'outside fixture\n':
113+
test_fail(f'in-place transfer changed the outside fixture: {stdout} {stderr}')
114+
return proc.returncode
115+
finally:
116+
if proc.poll() is None:
117+
proc.kill()
118+
proc.communicate()
119+
120+
121+
leaf = restricted / 'raced-leaf'
122+
leaf.write_text('old\n')
123+
124+
125+
def replace_leaf():
126+
leaf.unlink()
127+
leaf.symlink_to(marker)
128+
129+
130+
rc = gated_transfer('raced-leaf', replace_leaf)
131+
if rc == 0:
132+
check_contents(leaf)
133+
134+
parent = restricted / 'raced-parent'
135+
makepath(parent)
136+
(parent / 'untouched').write_text('old\n')
137+
saved_parent = restricted / 'saved-parent'
138+
139+
140+
def replace_parent():
141+
parent.rename(saved_parent)
142+
parent.symlink_to(outside, target_is_directory=True)
143+
144+
145+
if gated_transfer('raced-parent/untouched', replace_parent) == 0:
146+
check_contents(saved_parent / 'untouched')
147+
148+
print('rrsync receiver file overwrite, creation, directory, pull and path checks passed')

‎testsuite/skiplist/cygwin.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ pseudo-paths
5858
pseudo-paths-daemon
5959
read-batch-pipe
6060
rename-mixed-parent-transfer
61+
rrsync-receiver-file-arg
6162
rrsync-sender-leaf-flip
6263
rrsync-sender-parent-pin
6364
rrsync-symlink

‎testsuite/skiplist/macos.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ pseudo-paths # Linux-only: procfs pseudo-paths
2626
pseudo-paths-daemon
2727
read-batch-pipe
2828
readonly-partial-abort-mode-regression #
29+
rrsync-receiver-file-arg
2930
rrsync-sender-leaf-flip
3031
rrsync-sender-parent-pin
3132
rrsync-symlink

0 commit comments

Comments
 (0)