Skip to content

Commit 3cd6a4b

Browse files
committed
rrsync: resolve -log FILE per component and open it before rsync runs
rrsync now follows FILE one component at a time and refuses it if any step enters DIR, since from there the client decides where the path leads even when the final path is outside DIR. It also opens FILE before running rsync, so a log that cannot be written stops the transfer, and hands rsync the open file where it already pins paths. Write errors after that remain rsync's --log-file behaviour, as the man page now says.
1 parent 6f36e15 commit 3cd6a4b

3 files changed

Lines changed: 80 additions & 14 deletions

File tree

‎support/rrsync‎

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -489,25 +489,61 @@ def safe_open_logfile():
489489
return None
490490
return os.fdopen(fd, 'a')
491491

492+
def resolve_log_path(path, dir_st):
493+
"""Resolve the -log FILE one component at a time, as the kernel does.
494+
495+
Refuse it if any step enters DIR (by device and inode, so a bind mount of
496+
DIR counts too): from there the client decides where the path leads, even
497+
when the final path is outside DIR."""
498+
todo = [c for c in path.split('/') if c]
499+
done = '/'
500+
hops = 0
501+
while todo:
502+
comp = todo.pop(0)
503+
if comp == '.':
504+
continue
505+
if comp == '..':
506+
done = os.path.dirname(done)
507+
continue
508+
cand = os.path.join(done, comp)
509+
try:
510+
st = os.lstat(cand)
511+
except FileNotFoundError:
512+
return os.path.join(cand, *todo) # the open below creates or fails
513+
except OSError as e:
514+
die("cannot resolve the -log FILE", cand + ':', e.strerror)
515+
if stat.S_ISLNK(st.st_mode):
516+
hops += 1
517+
if hops > 40:
518+
die("too many symlinks in the -log FILE path")
519+
target = os.readlink(cand)
520+
if target.startswith('/'):
521+
done = '/'
522+
todo = [c for c in target.split('/') if c] + todo
523+
continue
524+
if os.path.samestat(st, dir_st):
525+
die("the -log FILE must be outside the restricted directory")
526+
done = cand
527+
return done
528+
492529
def main():
493530
if not os.path.isdir(args.dir):
494531
die("Restricted directory does not exist!")
495532

496533
if args.log:
497534
if args.dir == '/':
498535
die("-log cannot be used when DIR is /")
499-
# rsync gets the resolved path, so a symlink the client plants in DIR
500-
# later cannot redirect it; DIR may not be among its ancestors.
501-
args.log = os.path.realpath(args.log)
502-
dir_st = os.stat(args.dir)
503-
path = args.log
504-
while path != '/':
505-
try:
506-
if os.path.samestat(os.stat(path), dir_st):
507-
die("the -log FILE must be outside the restricted directory")
508-
except OSError:
509-
pass # a missing component cannot be DIR
510-
path = os.path.dirname(path)
536+
args.log = resolve_log_path(args.log, os.stat(args.dir))
537+
# Opened here so a log that cannot be written stops the transfer, and
538+
# handed to rsync as the open file where rrsync pins paths.
539+
try:
540+
fd = os.open(args.log, os.O_WRONLY | os.O_APPEND | os.O_CREAT | os.O_NOFOLLOW, 0o644)
541+
except OSError as e:
542+
die("cannot open the -log FILE", args.log + ':', e.strerror)
543+
make_inheritable(fd)
544+
pinned_fds.append(fd)
545+
if HAVE_PROC_SELF_FD:
546+
args.log = '/proc/self/fd/%d' % fd
511547

512548
# The format of the environment variables set by sshd:
513549
# SSH_ORIGINAL_COMMAND:

‎support/rrsync.1.md‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,11 @@ The remainder of this manpage is dedicated to using the rrsync script.
112112
in its default "%i %n%L" format, so each file sent, received or deleted
113113
gets a line. _FILE_ can be relative to the user's home directory or an
114114
absolute path. The client's own `--log-file` option is refused while this
115-
is in force. _FILE_ is resolved before use and must be outside _DIR_,
116-
and `-log` cannot be used when _DIR_ is `/`.
115+
is in force. _FILE_ is resolved one component at a time and refused if
116+
any step enters _DIR_, and `-log` cannot be used when _DIR_ is `/`.
117+
rrsync opens _FILE_ before running rsync and refuses the transfer if it
118+
cannot. After that, write errors are handled as for rsync's
119+
`--log-file`: they are not reported and do not stop the transfer.
117120
118121
0. `-help`, `-h`
119122

‎testsuite/rrsync-log_test.py‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,33 @@
112112
f'stderr={into.stderr!r}')
113113
assert not (root / 'transfer.log').exists()
114114

115+
# Nor may the path pass through DIR on its way out: the client's symlink in
116+
# DIR would decide where the log is written, even outside DIR.
117+
stolen = base / 'stolen'
118+
os.symlink(stolen, root / 'f')
119+
os.symlink(root / 'f', base / 'reg')
120+
reg_log = shlex.quote(str(base / 'reg'))
121+
reg_env = {**env, 'RRSYNC_FLAGS': f'-wo -no-lock -log {reg_log}'}
122+
reg = subprocess.run(
123+
rsync_argv('-r', '-e', rsh, f'{src}/', 'ignored:'),
124+
env=reg_env, capture_output=True, text=True, timeout=20)
125+
assert reg.returncode != 0 and 'must be outside the restricted directory' in reg.stderr, (
126+
f'rrsync accepted a -log path that passes through DIR: rc={reg.returncode}, '
127+
f'stderr={reg.stderr!r}')
128+
assert not stolen.exists()
129+
130+
# A log that cannot be opened stops the transfer instead of being skipped.
131+
(root / 'sub' / 'b.txt').unlink()
132+
nolog = shlex.quote(str(base / 'missing-dir' / 'transfer.log'))
133+
nolog_env = {**env, 'RRSYNC_FLAGS': f'-wo -no-lock -log {nolog}'}
134+
unlogged = subprocess.run(
135+
rsync_argv('-r', '-e', rsh, f'{src}/', 'ignored:'),
136+
env=nolog_env, capture_output=True, text=True, timeout=20)
137+
assert unlogged.returncode != 0 and 'cannot open the -log FILE' in unlogged.stderr, (
138+
f'rrsync ran a transfer it could not log: rc={unlogged.returncode}, '
139+
f'stderr={unlogged.stderr!r}')
140+
assert not (root / 'sub' / 'b.txt').exists()
141+
115142
# With DIR / no path is outside it.
116143
slash_env = {**env, 'RRSYNC_ROOT': '/'}
117144
slash = subprocess.run(

0 commit comments

Comments
 (0)