Skip to content
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/diagnostics.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ jobs:
python3 -B -S .github/scripts/test-transfers.py
- name: Tests
id: test
run: python3 -B -S testsuite/tools/check.py . ./rsync pipe,root,self-peer,linux,non-asan,no-userns test-results/python37 metadata profile process cli-transfers runner check-recipe -j2
run: python3 -B -S testsuite/tools/check.py . ./rsync pipe,root,self-peer,linux,non-asan,no-userns test-results/python37 metadata profile process mutation cli-transfers runner check-recipe -j2
- name: Upload results
if: ${{ !cancelled() && (steps.test.outcome == 'success' || steps.test.outcome == 'failure') }}
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
Expand Down
39 changes: 30 additions & 9 deletions syscall.c
Original file line number Diff line number Diff line change
Expand Up @@ -521,14 +521,28 @@ static int ona_open(const char *path, int flags, mode_t mode, char *out_abs, siz
memcpy(comp, remaining, comp_len);
comp[comp_len] = '\0';
int is_last = (slash == NULL);
int next = -1;
mode_t comp_mode;

/* Inspect this component without following symlinks. */
STRUCT_STAT lst;
if (fstatat(dfd, comp, &lst, AT_SYMLINK_NOFOLLOW) < 0) {
int stat_errno = errno;
/* Android mount containers can be traversable even when getattr
* reports ENOENT. A directory fd still supplies the required type
* and no-follow guarantees. */
if (stat_errno == ENOENT && !is_last) {
next = openat(dfd, comp, dir_traverse_flags | O_NOFOLLOW);
if (next < 0) {
saved_errno = errno;
goto out;
}
comp_mode = S_IFDIR; /* enforced by the open flags */
}
/* The leaf may not exist yet (O_CREAT case). Allow it
* and openat with O_NOFOLLOW so a race-planted leaf
* symlink at this instant is still refused. */
if (is_last && errno == ENOENT && (flags & O_CREAT)) {
else if (is_last && stat_errno == ENOENT && (flags & O_CREAT)) {
if (abspath_step(abspath, sizeof abspath, comp, comp_len) < 0) {
saved_errno = errno;
goto out;
Expand All @@ -541,11 +555,14 @@ static int ona_open(const char *path, int flags, mode_t mode, char *out_abs, siz
saved_errno = errno;
goto out;
}
saved_errno = errno;
goto out;
}
else {
saved_errno = stat_errno;
goto out;
}
} else
comp_mode = lst.st_mode;

if (S_ISLNK(lst.st_mode)) {
if (S_ISLNK(comp_mode)) {
/* Symlink: untrusted owner is refused; trusted owner is followed
* via readlinkat + splice. In a user namespace the /proc/self,
* /dev/fd and /dev/std* symlinks may report the overflow uid, so
Expand Down Expand Up @@ -678,23 +695,27 @@ static int ona_open(const char *path, int flags, mode_t mode, char *out_abs, siz
goto out;
}

if (!S_ISDIR(lst.st_mode)) {
if (!S_ISDIR(comp_mode)) {
saved_errno = ENOTDIR;
goto out;
}
/* track the resolved path so a target outside the module is refused */
if (abspath_step(abspath, sizeof abspath, comp, comp_len) < 0) {
saved_errno = errno;
if (next >= 0) close(next);
goto out;
}
if (!pin_transit && abspath_outside_confinement(abspath, 0)) {
saved_errno = ELOOP;
if (next >= 0) close(next);
goto out;
}
int next = openat(dfd, comp, dir_traverse_flags | O_NOFOLLOW);
if (next < 0) {
saved_errno = errno;
goto out;
next = openat(dfd, comp, dir_traverse_flags | O_NOFOLLOW);
if (next < 0) {
saved_errno = errno;
goto out;
}
}
if (dfd_owns) close(dfd);
dfd = next;
Expand Down
2 changes: 2 additions & 0 deletions testsuite/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ Tests declare applicability with `@requires(...)` or a top-level `metadata(...)`

Regression tests should fail against the faulty implementation and check the affected behaviour directly. Fixtures must keep their data inside the test's scratch directory, tolerate parallel execution and stop their child processes. Sleeps cannot establish ordering or prove that a race was exercised.

Tests which build mutated executables may report unavailable build capabilities as unsupported. Missing or ambiguous source replacement targets, missing compiler-flag replacements and failed configured builds are test failures rather than unavailable capabilities.

Profiles under profiles/ distinguish tests which do not apply from missing capabilities. Root and nonroot profiles must match the effective UID. Peer profiles identify the archived release and wire protocol. Historical binaries and their build records are under [old_versions](../old_versions/README.md); --rsync-bin2 selects the secondary executable and --archived-role identifies which side runs the archived release.

The tools/check.py launcher takes a build directory, binary, profile list and fresh output directory followed by runner options. It saves run.log and receipt.json, then checks the receipt against the executable and profiles.
Expand Down
14 changes: 6 additions & 8 deletions testsuite/harness/mutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,9 +329,9 @@ def build_patched_rsync(name, replacements, append_cflags=None):
for relpath, old, new in replacements:
path = work / relpath
text = path.read_text()
if old not in text:
test_skipped(f"{name}: could not find patch target in {relpath}: {old!r}",
capability='native_build')
matches = text.count(old)
if matches != 1:
test_fail(f'{name}: expected one patch target in {relpath}, found {matches}')
path.write_text(text.replace(old, new, 1))

obj = (work / relpath).with_suffix('.o')
Expand Down Expand Up @@ -361,8 +361,7 @@ def build_patched_rsync(name, replacements, append_cflags=None):
mk = mkpath.read_text()
mk2 = re.sub(r'(?m)^(CFLAGS=.*)$', r'\1 ' + append_cflags, mk, count=1)
if mk2 == mk:
test_skipped(f"{name}: could not append {append_cflags!r} to CFLAGS in the Makefile",
capability='native_build')
test_fail(f"{name}: could not append {append_cflags!r} to CFLAGS in the Makefile")
mkpath.write_text(mk2)

for obj in work.rglob('*.o'):
Expand All @@ -373,8 +372,7 @@ def build_patched_rsync(name, replacements, append_cflags=None):
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
rsync = work / 'rsync'
if build.returncode != 0 or not rsync.is_file() or not os.access(rsync, os.X_OK):
test_skipped(
test_fail(
f"{name}: patched rsync build failed (rc={build.returncode}). "
"Tail of build output:\n" + '\n'.join(build.stdout.splitlines()[-20:]),
capability='native_build')
"Tail of build output:\n" + '\n'.join(build.stdout.splitlines()[-20:]))
return rsync
107 changes: 107 additions & 0 deletions testsuite/tests/path/unstatable-intermediate-dir.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#!/usr/bin/env python3

import errno
import os
import subprocess

from harness.environment import SCRATCHDIR
from harness.filesystem import rmtree
from harness.mutation import build_patched_rsync
from harness.results import test_fail
from harness.rsync import rsync_argv_for
from harness import metadata

metadata(features={'dirfd_pinning', 'native_build', 'symlink'}, transports={'pipe'},
mutates={'filesystem', 'process'}, tags={'path', 'regression', 'security'})

# Fail only the metadata lookup; real openat calls must still enforce directory
# traversal and no-follow checks while reproducing Android's ENOENT.
PATCH_OLD = '\t\tif (fstatat(dfd, comp, &lst, AT_SYMLINK_NOFOLLOW) < 0) {'
PATCH_NEW = '''\t\tif ((strcmp(comp, "emulated") == 0
\t\t ? (errno = ENOENT, -1)
\t\t : fstatat(dfd, comp, &lst, AT_SYMLINK_NOFOLLOW)) < 0) {'''
probe_rsync = build_patched_rsync(
'unstatable-intermediate-rsync', [('syscall.c', PATCH_OLD, PATCH_NEW)])

base = SCRATCHDIR / 'unstatable-intermediate-dir'
src = base / 'src'
storage = base / 'storage'
storage_root = storage / 'emulated' / '0'
docs = storage_root / 'Documents'
dest = base / 'dest'

def reset():
rmtree(base)
for path in (src, docs, dest):
path.mkdir(parents=True)

def run(*args, cwd=None):
return subprocess.run(
rsync_argv_for(probe_rsync, '-I', '-i', *map(str, args)),
cwd=cwd, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
)

def require_copy(label, destination, copied, cwd=None):
source = src / 'file'
source.write_text(label + '\n')
result = run(source, destination, cwd=cwd)
if result.returncode != 0 or not copied.is_file() or copied.read_text() != label + '\n':
test_fail(f'{label}: transfer failed: rc={result.returncode}, stderr={result.stderr!r}')

reset()
require_copy('absolute', str(docs) + '/', docs / 'file')

reset()
require_copy('relative', './', docs / 'file', cwd=docs)

reset()
require_copy('directory-leaf', storage_root, storage_root / 'file')

reset()
sdcard = base / 'sdcard'
sdcard.symlink_to(storage_root)
require_copy('trusted-symlink', str(sdcard / 'Documents') + '/', docs / 'file')

reset()
source = src / 'file'
source.write_text('missing\n')
missing = storage / 'emulated' / '999' / 'Documents'
result = run(source, str(missing) + '/')
if (result.returncode == 0 or missing.exists()
or os.strerror(errno.ENOENT).encode() not in result.stderr):
test_fail(f'missing descendant did not fail with ENOENT: {result.stderr!r}')

reset()
source = src / 'file'
source.write_text('symlink\n')
real_storage = base / 'real-storage'
(real_storage / '0' / 'Documents').mkdir(parents=True)
rmtree(storage / 'emulated')
(storage / 'emulated').symlink_to(real_storage)
result = run(source, str(docs) + '/')
# NetBSD reports EFTYPE when O_NOFOLLOW refuses an intermediate symlink.
refusal_codes = {errno.ENOTDIR, errno.ELOOP, getattr(errno, 'EFTYPE', errno.ELOOP)}
errors = tuple(os.strerror(code).encode() for code in refusal_codes)
if result.returncode == 0 or not any(error in result.stderr for error in errors):
test_fail(f'intermediate symlink was not refused with the openat error: {result.stderr!r}')
if os.strerror(errno.ENOENT).encode() in result.stderr:
test_fail(f'openat error was replaced with ENOENT: {result.stderr!r}')
if (real_storage / '0' / 'Documents' / 'file').exists():
test_fail('intermediate symlink refusal still delivered the file')

reset()
outside = base / 'outside'
outside.mkdir()
(outside / 'secret').write_text('secret\n')
(storage_root / 'escape').symlink_to(outside)
files_from = storage_root / 'files.list'
files_from.write_text('escape/secret\n')
result = run(
'-r', f'--confine-root={storage_root}', f'--files-from={files_from}',
storage_root, str(dest) + '/',
)
if (result.returncode == 0 or (dest / 'secret').exists() or (dest / 'escape' / 'secret').exists()
or os.strerror(errno.ELOOP).encode() not in result.stderr):
test_fail(f'confinement did not reject the escape: {result.stderr!r}')

print('unstatable-intermediate-dir: hidden intermediate directories remain confined')
90 changes: 90 additions & 0 deletions testsuite/tests/selftests/mutation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#!/usr/bin/env python3

from contextlib import ExitStack, redirect_stderr
from io import StringIO
from pathlib import Path
from types import SimpleNamespace
from unittest.mock import patch

from harness import Exit, TestContext, requires, run
from harness import environment, mutation
from harness.results import test_fail


def build_fixture(context, name, text='before\n', makefile='CFLAGS=-O2\n',
status=0, output=True, executable=True, missing_make=False,
platform='linux', append_cflags=None):
root = context.scratch / name
source = root / 'source'
source.mkdir(parents=True)
(source / 'fixture.c').write_text(text)
if makefile is not None:
(source / 'Makefile').write_text(makefile)
(source / 'rsync').write_text('stale binary\n')

def build(command, cwd, **kwargs):
work = Path(cwd)
if (work / 'rsync').exists():
test_fail('patched build retained a stale executable')
if (work / 'fixture.c').read_text() != 'after\n':
test_fail('patched build received the wrong source')
if append_cflags and append_cflags not in (work / 'Makefile').read_text():
test_fail('patched build lost its extra compiler flags')
if output:
(work / 'rsync').write_text('built binary\n')
return SimpleNamespace(returncode=status, stdout='fixture compiler output\n')

def which(command):
return None if missing_make and command == 'make' else '/fixture/' + command

with ExitStack() as stack:
stack.enter_context(patch.object(environment, 'SCRATCHDIR', root))
stack.enter_context(patch.object(environment, 'SRCDIR', source))
stack.enter_context(patch.object(environment, 'TOOLDIR', source))
stack.enter_context(patch.object(mutation.sys, 'platform', platform))
stack.enter_context(patch.object(mutation.platform, 'system', return_value='Linux'))
stack.enter_context(patch.object(mutation.shutil, 'which', side_effect=which))
stack.enter_context(patch.object(mutation.os, 'access', return_value=executable))
stack.enter_context(patch.object(mutation.subprocess, 'run', side_effect=build))
stack.enter_context(patch.object(mutation, 'test_skipped', side_effect=SystemExit(Exit.SKIP)))
return mutation.build_patched_rsync(
'patched', [('fixture.c', 'before\n', 'after\n')], append_cflags=append_cflags)


def require_exit(action, expected, reason=''):
output = StringIO()
status = None
with redirect_stderr(output):
try:
action()
except SystemExit as error:
status = error.code
if status != expected or reason not in output.getvalue():
test_fail(f'patched build returned {status}, expected {expected}: {output.getvalue()}')


@requires(transports={'pipe'}, mutates={'filesystem'}, tags={'harness'})
def test(context: TestContext):
for name, options, reason in (
('missing-target', {'text': 'different\n'}, 'patch target'),
('ambiguous-target', {'text': 'before\nbefore\n'}, 'patch target'),
('missing-flags', {'makefile': 'CPPFLAGS=-DTEST\n', 'append_cflags': '-fwrapv'}, 'CFLAGS'),
('failed-build', {'status': 7}, 'patched rsync build failed'),
('missing-output', {'output': False}, 'patched rsync build failed'),
('nonexecutable-output', {'executable': False}, 'patched rsync build failed')):
require_exit(lambda: build_fixture(context, name, **options), Exit.FAIL, reason)

for name, options in (
('no-makefile', {'makefile': None}),
('no-make', {'missing_make': True}),
('cygwin', {'platform': 'cygwin'})):
require_exit(lambda: build_fixture(context, name, **options), Exit.SKIP)

for name, options in (('built', {}), ('extra-flags', {'append_cflags': '-fwrapv'})):
result = build_fixture(context, name, **options)
if result.read_text() != 'built binary\n':
test_fail('patched build did not return the newly built executable')


if __name__ == '__main__':
run(test)
Loading