rsync icon indicating copy to clipboard operation
rsync copied to clipboard

rrsync (3.5.x): receiver push onto an existing file always fails (dest passed as bare /proc/self/fd/N)

Open frecar opened this issue 2 months ago • 4 comments

rrsync (python rewrite, 3.5.x): receiver push onto an existing file always fails

Version: rsync 3.5.0 / 3.5.1 support/rrsync (verified byte-identical modulo shebang against Debian's 3.5.0+ds1-0+deb13u1).

Symptom: any push that overwrites an existing file inside the restricted dir fails:

rsync: [generator] delete_file: unlink(4) failed: Operation not permitted (1)
could not make way for new regular file: 4
rsync error: some files/attrs were not transferred (see previous errors) (code 23)

Pushes that create a new name succeed. --inplace does not help. Bisected to the 3.4.x -> 3.5.0 upgrade (old perl rrsync overwrote fine).

Root cause: in validated_arg(), the elif fd is not None: branch (path already exists) falls through to pinned = KEEP_LEAF_PIN for the receiver case, so the exec'd rsync --server gets the destination as bare /proc/self/fd/N — an O_RDONLY fd of the old file. rsync lstat()s that magic link as a symlink and tries to replace it, but unlink of a /proc/self/fd entry fails EPERM. (The FileNotFoundError branch for new dests already uses the correct /proc/self/fd/<parent>/<leaf> spelling — only the exists-branch is wrong.)

Minimal repro (receiver side only; sender version irrelevant):

mkdir -p /tmp/rrbox/sub && echo old > /tmp/rrbox/sub/f && echo new > /tmp/src
cat > /tmp/shim.sh <<'SHIM'
#!/bin/sh
host="$1"; shift
SSH_ORIGINAL_COMMAND="$*"; export SSH_ORIGINAL_COMMAND
exec python3 /usr/bin/rrsync -wo /tmp/rrbox
SHIM
chmod +x /tmp/shim.sh
sleep 1  # ensure distinct mtimes, else the quick-check skip masks the bug
rsync -a --rsh=/tmp/shim.sh /tmp/src localhost:/sub/f   # -> exit 23
rsync -a --rsh=/tmp/shim.sh /tmp/src localhost:/sub/g   # -> exit 0 (new name works)

Suggested fix: for not am_sender and opt == 'arg' with an existing non-directory, spell the dest beneath the pinned parent (/proc/self/fd/<parentfd>/<leaf>, same primitive as the new-dest branch) instead of KEEP_LEAF_PIN. Directories can keep the leaf pin (rsync only opens/chdirs those). I verified this spelling end-to-end: overwrite transfers data=N and replaces content, new-file pushes still work, and ../symlink escapes are still rejected.

Happy to submit this as a PR if the approach looks right.

frecar avatar Sep 29 '26 08:09 frecar

Also running in to this after the Debian Trixie update. Do you have a diff you can share? I don't quite understand the fix description.

chennin avatar Sep 29 '26 17:09 chennin

Seems correct. Please open a PR. For an existing non-directory receiver destination, pin the parent and pass /proc/self/fd/<parentfd>/<leaf>, matching the missing-leaf path. Preserve the existing .., symlink-escape and parent-type checks and add overwrite, new-file, directory and escape regressions.

steadytao avatar Sep 30 '26 12:09 steadytao

Here is a diff for the fix described above, for anyone who needs it before a release. I tested it against v3.5.1's support/rrsync on Debian 13 with rsync 3.5.0+ds1-0+deb13u1, whose rrsync matches v3.5.1's apart from the shebang.

Reproduce with the stock rrsync:

T=$(mktemp -d); mkdir -p $T/root $T/src
printf 'new contents\n' > $T/src/f.txt; printf 'old\n' > $T/root/f.txt
printf '#!/bin/sh\nshift\nSSH_ORIGINAL_COMMAND="$*" exec /usr/bin/rrsync -no-lock %s/root\n' $T > $T/fake-ssh; chmod +x $T/fake-ssh
rsync -a -e $T/fake-ssh -i $T/src/f.txt x:f.txt; echo "rc=$?"
rsync -a -e $T/fake-ssh -i $T/src/f.txt x:./; echo "rc=$?"
cat $T/root/f.txt; rm -rf $T
rsync: [generator] delete_file: unlink(3) failed: Operation not permitted (1)
could not make way for new regular file: 3
rc=23
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1394) [sender=3.5.0]
<f.s....... f.txt
rc=0
new contents

With the diff applied, in the same setup:

  • the upload over the existing file replaces it, rc 0
  • a new file and a pull still work
  • an arg reaching outside the tree through a symlink is still refused
@@ -678,6 +678,21 @@
         sys.exit(child.returncode)
 
 
+def receiver_file_arg(real_arg, orig_arg):
+    # An existing non-directory receiver arg is spelled beneath its pinned
+    # parent, as a new file already is, so rsync's receiver writes the file
+    # by name instead of treating /proc/self/fd as the destination dir.
+    pfd = os.open(os.path.dirname(real_arg) or '/',
+                  os.O_RDONLY | os.O_NOFOLLOW | os.O_DIRECTORY)
+    ppath = os.readlink('/proc/self/fd/%d' % pfd)
+    if ppath != args.dir and not ppath.startswith(args.dir_slash):
+        os.close(pfd)
+        die('post-pin path escaped tree (race?):', orig_arg, ppath)
+    os.set_inheritable(pfd, True)
+    pinned_fds.append(pfd)
+    return '/proc/self/fd/%d/%s' % (pfd, os.path.basename(real_arg))
+
+
 def validated_arg(opt, arg, typ=3, wild=False):
@@ -923,6 +938,8 @@
                         pinned = sender_pinned_arg(fd, logical, orig_arg,
                                                    arg_has_trailing_slash,
                                                    arg_has_trailing_slash_dot)
+                    elif opt == 'arg' and not stat.S_ISDIR(os.fstat(fd).st_mode):
+                        pinned = receiver_file_arg(real_arg, orig_arg)
                     else:
                         pinned = KEEP_LEAF_PIN

The leaf is reached by name beneath the held parent, which keeps the parent-flip protection 3.5.0 added. I did not test a leaf swapped for a symlink between the check and the write under --inplace, and the regression tests asked for above are not part of this diff.

JeanFrancoisGagne avatar Oct 18 '26 12:10 JeanFrancoisGagne

The parent-pin direction is right but this still needs a PR and automated coverage for existing-file overwrite, new-file creation, directories, pulls, .., symlink escape and an --inplace leaf-replacement race.

steadytao avatar Oct 19 '26 09:10 steadytao