Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion receiver.c
Original file line number Diff line number Diff line change
Expand Up @@ -1113,7 +1113,8 @@ int recv_files(int f_in, int f_out, char *local_name)
* partial updates rejects the unsafe peer option; otherwise (a pre-30
* peer, where no in-place partial redirect is possible, or a pull
* client) fall back to a safe no-basis update. */
if (am_daemon && inplace_partial) {
extern char *confine_root;
if ((am_daemon || confine_root) && inplace_partial) {
rprintf(FERROR,
"rsync: refusing unconfined partial basis for %s\n", fname);
exit_cleanup(RERR_PROTOCOL);
Expand Down
2 changes: 1 addition & 1 deletion syscall.c
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ static int ona_open(const char *path, int flags, mode_t mode, char *out_abs, siz
saved_errno = ENOTDIR;
goto out;
}
if (confine_root) {
if (confine_root && (operator_path_resolve || filesfrom_owner_walk_active())) {
/* Anonymous objects cannot be proven to reside beneath
* the confinement root. */
saved_errno = ENOENT;
Expand Down
118 changes: 118 additions & 0 deletions testsuite/tests/path/confine-root-dest-symlink.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
#!/usr/bin/env python3
"""Regression test for --confine-root main-transfer destination confinement.

Note: The symlink checks here do not represent a pure untrusted-symlink
vulnerability, as they rely on a pre-existing ("trusted") symlink placed
inside the root (or local privilege to swap it in). Rather, they test that
incomplete confinement around trusted in-tree symlinks still does not permit
resolving or writing to targets outside --confine-root.
"""

import os
import platform
import subprocess
import time

from harness.mutation import race_budget, start_c_flipper, stop_flipper
from harness.process import quote_command
from harness.environment import SCRATCHDIR
from harness.filesystem import rmtree
from harness.results import test_fail, test_skipped
from harness.rsync import rsync_argv
from harness import metadata

metadata(features={'dirfd_pinning'}, transports={'pipe'})

_CYGWIN = platform.system().startswith('CYGWIN')

base = SCRATCHDIR / 'confine-root-dest'
rmtree(base)
base.mkdir(parents=True, exist_ok=True)

dir_root = base / 'dir'
outside = base / 'outside'
src_dir = base / 'src_files'
sub = dir_root / 'sub'
sublink = dir_root / 'sublink'
rsh = base / 'rsh.sh'

rsh.write_text(f"""#!/bin/sh
while [ $# -gt 0 ] && [ "$1" != "--server" ]; do
shift
done
cd "{dir_root}" || exit 1
exec {quote_command(rsync_argv())} --confine-root="{dir_root}" "$@"
""")
os.chmod(rsh, 0o755)


def build():
for d in (dir_root, outside, src_dir):
rmtree(d)
d.mkdir(parents=True, exist_ok=True)
sub.mkdir(parents=True, exist_ok=True)
(src_dir / 'test').write_text("PAYLOAD\n")
(outside / 'test').write_text("VICTIM\n")
os.symlink(str(outside), sublink)


def push_server(remote_dest, *extra):
subprocess.run(
rsync_argv('-I', *extra, '-e', str(rsh),
str(src_dir / 'test'), f'h:{remote_dest}'),
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
)


# 0. Sanity check: valid in-tree server transfer must succeed.
build()

push_server('sub/ok_server')
if not (sub / 'ok_server').is_file() or (sub / 'ok_server').read_text() != "PAYLOAD\n":
test_fail("sanity failed: server transfer into confined sub/ok_server failed")

# 1. Deterministic checks.
build()

# 1a. Pre-existing ("trusted") in-tree symlink pointing outside the root:
# Not a remote symlink-creation bug, but verifies that a trusted symlink inside
# the tree is still strictly bounded by --confine-root and cannot write outside.
push_server('sublink/test')
if (outside / 'test').read_text() != "VICTIM\n":
test_fail("server confine-root failed to block write through in-tree symlink sublink/test")

# 1b. Server relative '..' escape: ../outside/test
push_server('../outside/test')
if (outside / 'test').read_text() != "VICTIM\n":
test_fail("server confine-root failed to block relative destination ../outside/test")

# 1c. Server new file creation outside DIR (O_CREAT path)
push_server('../outside/newfile')
if (outside / 'newfile').exists():
test_fail("server confine-root failed to block creating ../outside/newfile")

# 1d. Server new directory creation outside DIR
push_server('../outside/newdir/test')
if (outside / 'newdir').exists():
test_fail("server confine-root failed to block creating ../outside/newdir")

# 2. Live race check.
deadline = time.monotonic() + race_budget(10.0)
flip = None
try:
while time.monotonic() < deadline:
if flip is not None:
stop_flipper(flip)
flip = None

build()
flip = start_c_flipper(sub, sublink)
for _ in range(20):
push_server('sub/test')
if (outside / 'test').read_text() != "VICTIM\n":
if _CYGWIN:
test_skipped('Cygwin destination transfer followed a swapped parent symlink', capability='dirfd_pinning')
test_fail("confine-root race escape: wrote outside/test during parent flip")
finally:
if flip is not None:
stop_flipper(flip)
97 changes: 97 additions & 0 deletions testsuite/tests/path/confine-root-source.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#!/usr/bin/env python3
"""Regression test for --confine-root sender source confinement.

Verifies that a server sender running under --confine-root refuses to
traverse out of the confined root via a pre-existing ("trusted") in-tree
symlink or a relative '..' source argument during a pull transfer.
"""

import os
import subprocess

from harness.process import quote_command
from harness.environment import SCRATCHDIR
from harness.filesystem import rmtree
from harness.results import test_fail
from harness.rsync import rsync_argv
from harness import metadata

metadata(transports={'pipe'})

base = SCRATCHDIR / 'confine-root-source'
rmtree(base)
base.mkdir(parents=True, exist_ok=True)

dir_root = base / 'dir'
outside = base / 'outside'
dest_dir = base / 'dest'
sub = dir_root / 'sub'
sublink = dir_root / 'sublink'
rsh = base / 'rsh.sh'

rsh.write_text(f"""#!/bin/sh
while [ $# -gt 0 ] && [ "$1" != "--server" ]; do
shift
done
cd "{dir_root}" || exit 1
exec {quote_command(rsync_argv())} --confine-root="{dir_root}" "$@"
""")
os.chmod(rsh, 0o755)


def build():
for d in (dir_root, outside, dest_dir):
rmtree(d)
d.mkdir(parents=True, exist_ok=True)
sub.mkdir(parents=True, exist_ok=True)
(sub / 'ok_file').write_text("ALLOWED\n")
(outside / 'secret').write_text("SECRET\n")
os.symlink(str(outside), sublink)


def pull_server(remote_src, *extra):
rmtree(dest_dir)
dest_dir.mkdir(parents=True, exist_ok=True)
return subprocess.run(
rsync_argv('-I', *extra, '-e', str(rsh),
f'h:{remote_src}', f'{dest_dir}/'),
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
)


# 0. Sanity check: valid in-tree sender pull must succeed.
build()

pull_server('sub/ok_file')
if not (dest_dir / 'ok_file').is_file() or (dest_dir / 'ok_file').read_text() != "ALLOWED\n":
test_fail("sanity failed: pull from confined sender sub/ok_file failed")

# 1. Static source symlink and traversal checks.
build()

# 1a. Pre-existing ("trusted") in-tree symlink pointing outside the root
# (pulling a file through the symlinked directory: sublink/secret)
pull_server('sublink/secret')
if (dest_dir / 'secret').exists():
test_fail("sender confine-root failed to block pull through in-tree symlink sublink/secret")

# 1b. Pre-existing ("trusted") in-tree symlink as a directory source with
# trailing slash (forces change_dir into the symlink target: sublink/)
pull_server('sublink/', '-r')
if (dest_dir / 'secret').exists():
test_fail("sender confine-root failed to block recursive pull of symlinked dir sublink/")

# 1c. Dereferencing in-tree symlink via --copy-links (-L)
pull_server('sublink/secret', '-L')
if (dest_dir / 'secret').exists():
test_fail("sender confine-root -L failed to block pull through sublink/secret")

# 1d. Relative '..' source escape: ../outside/secret
pull_server('../outside/secret')
if (dest_dir / 'secret').exists():
test_fail("sender confine-root failed to block relative source ../outside/secret")

# 1e. Nested '..' source escape: sub/../../outside/secret
pull_server('sub/../../outside/secret')
if (dest_dir / 'secret').exists():
test_fail("sender confine-root failed to block nested relative source sub/../../outside/secret")
Loading
Loading