#!/bin/sh # Copyright (C) 2026 by Andrew Tridgell # This program is distributable under the terms of the GNU GPL (see # COPYING). # Regression test for the basedir-confinement gap in # secure_relative_open(). The function opens basedir with a plain # openat(AT_FDCWD, basedir, O_RDONLY | O_DIRECTORY), without # RESOLVE_BENEATH or a per-component O_NOFOLLOW walk, so a parent # symlink ON basedir is followed unrestrictedly. RESOLVE_BENEATH is # then applied only to relpath, anchored at the wrong directory. # # The receiver's basis-file lookup at receiver.c passes # basis_dir[fnamecmp_type] (from --copy-dest / --link-dest / # --compare-dest -- all sender-controllable in daemon mode) as # basedir. A daemon-module attacker with write access can plant a # symlink at module/cd -> /outside, then run --link-dest=cd to # make the daemon's basis-file lookup resolve into /outside, # leaking the contents of daemon-readable files via the rsync # delta-rolling read-disclosure primitive. # # We detect the escape by leveraging --link-dest: when basis # matches source exactly (content + mtime + mode), --link-dest # hard-links the destination to the basis file. With the bug, the # destination ends up as a hard link to the outside-the-module # file (same inode). With the fix, no basis is found and the # destination is a fresh copy (different inode). # # The vulnerable code path is the same on every platform # (including the per-component fallback on systems without # RESOLVE_BENEATH), so this test is not platform-gated. . "$suitedir/rsync.fns" mod="$scratchdir/module" outside="$scratchdir/outside" src="$scratchdir/src" conf="$scratchdir/test-rsyncd.conf" rm -rf "$mod" "$outside" "$src" mkdir -p "$mod" "$outside" "$src" # Portable inode-number helper (GNU coreutils stat -c, BSD stat -f). file_inode() { stat -c %i "$1" 2>/dev/null || stat -f %i "$1" } # Outside-the-module file an attacker would like the daemon to # treat as a basis. echo "OUTSIDE_SECRET_DATA" > "$outside/target.txt" chmod 0644 "$outside/target.txt" # The symlink trap planted in the module by the local attacker. ln -s "$outside" "$mod/cd" # Source file matches outside/target.txt exactly (content + mtime # + mode) so --link-dest will hard-link the destination to the # basis file iff the daemon's basedir lookup reaches outside/. echo "OUTSIDE_SECRET_DATA" > "$src/target.txt" touch -r "$outside/target.txt" "$src/target.txt" chmod 0644 "$src/target.txt" # When running as root the daemon would drop to "nobody" by # default, which can't write into the test scratch dir. Force the # daemon to keep our uid/gid in that case so the basis-link # transfer can actually create the destination file. (Non-root # can't specify uid/gid in rsyncd.conf -- comment them out then.) my_uid=`get_testuid` root_uid=`get_rootuid` root_gid=`get_rootgid` uid_setting="uid = $root_uid" gid_setting="gid = $root_gid" if test x"$my_uid" != x"$root_uid"; then uid_setting="#$uid_setting" gid_setting="#$gid_setting" fi cat > "$conf" </dev/null 2>&1 || true if [ ! -f "$mod/target.txt" ]; then test_fail "destination file was not created -- daemon transfer failed before the test could observe the basedir behaviour" fi outside_inode=$(file_inode "$outside/target.txt") dst_inode=$(file_inode "$mod/target.txt") if [ "$outside_inode" = "$dst_inode" ]; then test_fail "basedir-escape: --link-dest hard-linked module/target.txt to outside/target.txt (inode $outside_inode); daemon's basis-file lookup followed the parent symlink on the basedir" fi exit 0