Debian Codegraph @

testsuite/macos-setgid-ordinary-mode-regression_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 """A failed setgid request must not discard the ordinary directory mode.
3
4 On macOS, mkdir(..., 02750) for a caller-owned directory whose inherited group
5 is not one of the caller's groups succeeds with setgid cleared and mode 0750.
6 The hardened 3.5 receiver stages a new directory and later uses
7 fchmodat(..., AT_SYMLINK_NOFOLLOW), which returns EPERM without applying any of
8 the ordinary bits. A pinned-descriptor fchmod preserves the former semantics.
9 """
10
11 import ctypes
12 import errno
13 import os
14 import tempfile
15 import shutil
16 import atexit
17 import pathlib
18 import platform
19 import subprocess
20
21 from rsyncfns import SCRATCHDIR, rmtree, rsync_argv, test_fail, test_skipped
22
23
24 if platform.system() != "Darwin":
25 test_skipped("macOS-specific fchmodat setgid behavior")
26
27 AT_FDCWD = -2
28 AT_SYMLINK_NOFOLLOW = 0x0020
29 libc = ctypes.CDLL(None, use_errno=True)
30 libc.fchmodat.argtypes = [ctypes.c_int, ctypes.c_char_p, ctypes.c_uint, ctypes.c_int]
31
32
33 def mode(path):
34 return path.stat().st_mode & 0o7777
35
36
37 # macOS gives a new directory its parent's group, and the bug only shows when
38 # that group is one the caller cannot grant. The build tree usually satisfies
39 # that, but not always -- a checkout under a home directory inherits a group the
40 # user IS in, and the test then skipped silently. Fall back to /private/tmp,
41 # which is group wheel, so this runs rather than skipping wherever it can.
42 def _scratch_with_ungrantable_group():
43 """A scratch dir whose group we cannot grant, else None.
44
45 The build tree is tried first; a checkout under a home directory
46 inherits a group the user IS in, so /private/tmp (group wheel) is the
47 fallback. That one is outside SCRATCHDIR, which the harness cleans, so
48 it gets a unique name and an atexit hook -- a fixed name in a sticky
49 world-writable directory would let concurrent runs delete each other's
50 live fixture, and a leftover owned by another user would make every
51 later run skip.
52 """
53 cand = SCRATCHDIR / "macos-setgid-ordinary-mode-regression"
54 rmtree(cand)
55 cand.mkdir(parents=True)
56 if cand.stat().st_gid not in os.getgroups():
57 return cand
58 rmtree(cand)
59
60 try:
61 tmp = pathlib.Path(tempfile.mkdtemp(prefix="rsync-setgid-", dir="/private/tmp"))
62 except OSError:
63 return None
64 atexit.register(shutil.rmtree, tmp, True)
65 if tmp.stat().st_gid not in os.getgroups():
66 return tmp
67 return None
68
69
70 base = _scratch_with_ungrantable_group()
71 if base is None:
72 test_skipped("no scratch parent whose group this user cannot grant "
73 "(running as root, or every candidate group is granted)")
74
75 mprobe = base / "mprobe"
76 mprobe.mkdir(mode=0o2750)
77 if mode(mprobe) != 0o750:
78 test_skipped("mkdir does not apply ordinary bits while clearing setgid")
79
80 faprobe = base / "fchmodat-probe"
81 faprobe.mkdir(mode=0o700)
82 ctypes.set_errno(0)
83 frc = libc.fchmodat(AT_FDCWD, os.fsencode(faprobe), 0o2750,
84 AT_SYMLINK_NOFOLLOW)
85 if frc == 0 or ctypes.get_errno() != errno.EPERM or mode(faprobe) != 0o700:
86 test_skipped("fchmodat no-follow does not expose the macOS differential")
87
88 fdprobe = base / "fchmod-probe"
89 fdprobe.mkdir(mode=0o700)
90 fd = os.open(fdprobe, os.O_RDONLY)
91 try:
92 os.fchmod(fd, 0o2750)
93 finally:
94 os.close(fd)
95 if mode(fdprobe) != 0o750:
96 test_skipped("descriptor chmod does not preserve the legacy mkdir semantics")
97
98 src = base / "src"
99 dest = base / "dest"
100 src.mkdir(mode=0o755)
101 (src / "child").mkdir(mode=0o755)
102 (src / "child" / "payload").write_text("ordinary-mode-preserved\n")
103
104 proc = subprocess.run(
105 rsync_argv("-a", "--chmod=D2750,F0640", str(src) + "/", str(dest) + "/"),
106 text=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
107 )
108 observed = (mode(dest), mode(dest / "child"))
109 if proc.returncode != 0 or observed != (0o750, 0o750):
110 test_fail(
111 "setgid failure discarded requested ordinary directory permissions: "
112 f"rc={proc.returncode}, root/child modes={tuple(oct(v) for v in observed)}, "
113 f"output={proc.stdout!r}"
114 )
115
116 print("PASS: ungrantable setgid did not discard the requested 0750 mode")