Skip to content

Commit b705916

Browse files
committed
syscall: use O_SEARCH for held directory traversal on macOS
Since 3.5.0 the receiver resolves its destination one component at a time with held directory fds. Without O_PATH these are opened O_RDONLY|O_DIRECTORY, so on macOS every ancestor of the destination must be readable, not just searchable. A destination below a mode 0711 home, or a sandbox that denies listing $HOME, fails with change_dir#1. Use O_SEARCH on macOS: from macOS 13 it needs only search permission, and older kernels ignore it, keeping the O_RDONLY|O_DIRECTORY behaviour. Not on illumos/Solaris, which checks search permission before O_DIRECTORY, so probing a regular file fails with EACCES rather than the ENOTDIR the resolvers expect. Run the search-only tests on macOS 13+ and FreeBSD. Their root runs there skip for lack of setpriv, so mark them fleet_nonroot and add targeted non-root steps to those CI jobs.
1 parent 5479ce9 commit b705916

9 files changed

Lines changed: 64 additions & 16 deletions

File tree

‎.github/workflows/freebsd-build.yml‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ jobs:
4242
./rsync --version
4343
make check
4444
./runtests.py --rsync-bin=`pwd`/rsync --use-tcp -j 8
45+
# Non-root, targeted: without setpriv the root runs skip the
46+
# search-only tests. Run them as uid 65534 from a copy under /tmp,
47+
# which that uid can traverse, without root's PATH (/root/bin).
48+
rm -rf /tmp/rsync-nonroot && cp -Rp . /tmp/rsync-nonroot
49+
rm -rf /tmp/rsync-nonroot/testtmp && chown -R 65534:65534 /tmp/rsync-nonroot
50+
(cd /tmp/rsync-nonroot && env HOME=/tmp/rsync-nonroot PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin python3 -c 'import os, sys; os.setgroups([]); os.setgid(65534); os.setuid(65534); os.execv(sys.executable, [sys.executable] + sys.argv[1:])' \
51+
runtests.py --rsync-bin=/tmp/rsync-nonroot/rsync search-only-destination search-only-held-dirfd)
4552
./rsync-ssl --no-motd download.samba.org::rsyncftp/ || true
4653
- name: save artifact
4754
uses: actions/upload-artifact@v4

‎.github/workflows/macos-build.yml‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ jobs:
5353
# Second run with daemon tests over a real loopback rsyncd; the default
5454
# 'make check' above uses the secure stdio-pipe transport.
5555
run: sudo ./runtests.py --rsync-bin="$PWD/rsync" --use-tcp -j 8
56+
- name: check (non-root, targeted)
57+
# The runs above are root and macOS has no setpriv, so the search-only
58+
# tests skip there; run them as the unprivileged 'runner' user. Explicit
59+
# test names bypass RSYNC_EXPECT_SKIPPED.
60+
run: |
61+
sudo rm -rf testtmp # prior root steps left it root-owned
62+
./runtests.py --rsync-bin="$PWD/rsync" \
63+
search-only-destination search-only-held-dirfd
5664
- name: ssl file list
5765
run: rsync-ssl --no-motd download.samba.org::rsyncftp/ || true
5866
- name: save artifact

‎.github/workflows/ubuntu-build.yml‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,15 @@ jobs:
6060
# design); the only test with no other CI coverage (Benjamin #2).
6161
# ownership-depth -- non-root takes the group-only remap path.
6262
# daemon -- non-root takes the default-config path.
63+
# search-only-* -- search-only parents need traversal without read.
6364
# CONVENTION: a new test that requires/meaningfully exercises a non-root
6465
# path must be added to the list below (kept in sync with the fleet
6566
# harness's nonroot_tests).
6667
run: |
6768
sudo rm -rf testtmp # prior root steps left it root-owned
6869
./runtests.py --rsync-bin="$PWD/rsync" \
69-
daemon-namecvt-empty-response ownership-depth daemon
70+
daemon-namecvt-empty-response ownership-depth daemon \
71+
search-only-destination search-only-held-dirfd
7072
- name: ssl file list
7173
run: rsync-ssl --no-motd download.samba.org::rsyncftp/ || true
7274
- name: save artifact

‎SECURITY.md‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,9 +199,14 @@ leaf symlink into a file read.
199199

200200
`secure_relative_open()` resolves a path with a single portable mechanism on
201201
every platform: a per-component walk on a stack of held directory fds. On
202-
Linux, anchors and traversal components use
203-
`O_PATH|O_DIRECTORY|O_NOFOLLOW`; other platforms retain the
204-
`O_RDONLY|O_DIRECTORY` fallback. Descending into a real subdirectory pushes
202+
systems with `O_PATH` (Linux, FreeBSD 13.1+), anchors and traversal components
203+
use `O_PATH|O_DIRECTORY|O_NOFOLLOW`, even though FreeBSD also defines
204+
`O_SEARCH`. macOS uses `O_SEARCH|O_NOFOLLOW` (`O_SEARCH` includes
205+
`O_DIRECTORY`), which needs only search permission from macOS 13; older kernels
206+
ignore it and require read permission. Other platforms retain the
207+
`O_RDONLY|O_DIRECTORY` fallback, including illumos/Solaris, whose `O_SEARCH`
208+
checks search permission before `O_DIRECTORY` and so fails a regular file with
209+
`EACCES` rather than the `ENOTDIR` the walk expects. Descending into a real subdirectory pushes
205210
its fd, a `..` pops back to the already-held parent (a pop at the anchor is
206211
refused), and an in-tree directory symlink is followed by reading its target
207212
and walking that off the same stack (absolute targets refused, symlink hops

‎syscall.c‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,18 @@ extern int operator_path_resolve; /* defined below; fwd-declared for the exclude
7878
extern int filesfrom_owner_walk_override; /* defined below; used by files-from implied dirs */
7979

8080
/* A directory fd used only for pathname traversal, fchdir(), or as *at()
81-
* authority does not need read permission on Linux. Keep the portable
82-
* O_RDONLY fallback for systems without O_PATH. */
81+
* authority does not need read permission: use O_PATH where available (Linux,
82+
* FreeBSD 13.1+), and O_SEARCH (which includes O_DIRECTORY) on macOS, where it
83+
* needs only search permission from macOS 13 and older kernels ignore it. Not
84+
* elsewhere: illumos checks search permission before O_DIRECTORY, so probing a
85+
* regular file fails with EACCES instead of the ENOTDIR the resolvers expect.
86+
* Keep the portable O_RDONLY fallback for other systems. */
8387
static int directory_traverse_flags(void)
8488
{
8589
#if defined O_PATH && defined O_DIRECTORY
8690
return O_PATH | O_DIRECTORY;
91+
#elif defined O_SEARCH && defined __APPLE__
92+
return O_SEARCH;
8793
#elif defined O_DIRECTORY
8894
return O_RDONLY | O_DIRECTORY;
8995
#else

‎testsuite/rsyncfns.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,18 @@ def require_asan(reason: str, which: str = None) -> 'None':
897897
test_skipped(reason)
898898

899899

900+
def require_search_only_traversal() -> 'None':
901+
"""Skip the test (exit 77) unless rsync's held directory fds need only
902+
search permission: O_PATH on Linux and FreeBSD, O_SEARCH on macOS 13+
903+
(Darwin 22). Older macOS ignores O_SEARCH, and other systems open them
904+
O_RDONLY, so traversal there still needs read permission."""
905+
if sys.platform.startswith(('linux', 'freebsd')):
906+
return
907+
if sys.platform == 'darwin' and int(platform.release().split('.')[0]) >= 22:
908+
return
909+
test_skipped('held directory fds need read permission on this platform')
910+
911+
900912
def rsh_cmd(cmd: str = None, *opts: str) -> str:
901913
"""Build an RSYNC_RSH / --rsh value, quoted for rsync's own tokenizer.
902914

‎testsuite/search-only-destination_test.py‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,19 @@
88
import os
99
import shutil
1010
import subprocess
11-
import sys
1211
import tempfile
1312
from pathlib import Path
1413

15-
from rsyncfns import SCRATCHDIR, rmtree, rsync_argv, test_fail, test_skipped
14+
from rsyncfns import (
15+
SCRATCHDIR, require_search_only_traversal, rmtree, rsync_argv, test_fail,
16+
test_skipped,
17+
)
1618

17-
if not sys.platform.startswith('linux'):
18-
test_skipped('search-only-destination is Linux-specific')
19+
require_search_only_traversal()
20+
21+
# Root runs need setpriv to drop privileges, which macOS and FreeBSD lack;
22+
# the fleet harness and the workflows' non-root steps cover them.
23+
fleet_nonroot = True
1924

2025
launcher = []
2126
if os.geteuid() == 0:

‎testsuite/search-only-held-dirfd_test.py‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,19 @@
1010
import os
1111
import shutil
1212
import subprocess
13-
import sys
1413
import tempfile
1514
from pathlib import Path
1615

1716
from rsyncfns import (
18-
SCRATCHDIR, forced_protocol, rmtree, rsync_argv, test_fail, test_skipped,
17+
SCRATCHDIR, forced_protocol, require_search_only_traversal, rmtree,
18+
rsync_argv, test_fail, test_skipped,
1919
)
2020

21-
if not sys.platform.startswith('linux'):
22-
test_skipped('search-only held-dirfd coverage is Linux-specific')
21+
require_search_only_traversal()
22+
23+
# Root runs need setpriv to drop privileges, which macOS and FreeBSD lack;
24+
# the fleet harness and the workflows' non-root steps cover them.
25+
fleet_nonroot = True
2326

2427
launcher = []
2528
if os.geteuid() == 0:

‎testsuite/skiplist/macos.txt‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ rrsync-sender-leaf-flip
3232
rrsync-sender-parent-pin
3333
rrsync-symlink
3434
rrsync-userns-procfs
35-
search-only-destination
36-
search-only-held-dirfd
35+
search-only-destination # root run lacks setpriv; see the non-root step
36+
search-only-held-dirfd # root run lacks setpriv; see the non-root step
3737
sender-remove-source-root-anchor
3838
simd-checksum
3939
source-change-size-continues

0 commit comments

Comments
 (0)