Debian Codegraph @

t_chmod_secure.c

raw symbols SCIP
1 /*
2 * Test harness for do_chmod_at(). Confirms the symlink-TOCTOU
3 * primitive used by CVE-2026-29518 (and its incomplete-fix follow-up
4 * for chmod) is closed by do_chmod_at(): a parent directory component
5 * being a symlink that escapes the receiver's confinement must be
6 * rejected, while a parent symlink that resolves *within* the tree
7 * must still work (so legitimate dir-symlinks are not regressed).
8 *
9 * Not linked into rsync itself.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
14 */
15
16 #include "rsync.h"
17
18 #include <sys/stat.h>
19
20 int dry_run = 0;
21 int am_root = 0;
22 int am_sender = 0;
23 int read_only = 0;
24 int list_only = 0;
25 int copy_links = 0;
26 int copy_unsafe_links = 0;
27 extern int am_daemon, am_chrooted;
28
29 short info_levels[COUNT_INFO], debug_levels[COUNT_DEBUG];
30
31 static int errs = 0;
32
33
34 /* Does do_chmod_at()'s leaf handling refuse to follow a symlink at the final
35 * component? Yes wherever AT_SYMLINK_NOFOLLOW exists; otherwise the wrapper
36 * falls back to a following fchmodat() (documented limitation). Mirrors the
37 * #ifdef ladder in do_fchmodat_nofollow. */
38 static int leaf_chmod_nofollow_supported(void)
39 {
40 #if defined AT_SYMLINK_NOFOLLOW
41 return 1;
42 #else
43 return 0;
44 #endif
45 }
46
47 static void check(const char *label, int actual_rc, int expect_ok,
48 const char *path, mode_t expected_mode)
49 {
50 struct stat st;
51 int got_ok = (actual_rc == 0);
52 /* expect_ok < 0: rc is platform-dependent, don't assert it (leaf-symlink
53 * scenario); only the target-mode check below is portable. */
54 if (expect_ok >= 0 && got_ok != expect_ok) {
55 fprintf(stderr, "FAIL [%s]: rc=%d errno=%d (%s), expected %s\n",
56 label, actual_rc, errno, strerror(errno),
57 expect_ok ? "success" : "rejection");
58 errs++;
59 return;
60 }
61 if (path && stat(path, &st) < 0) {
62 fprintf(stderr, "FAIL [%s]: stat(%s) failed: %s\n",
63 label, path, strerror(errno));
64 errs++;
65 return;
66 }
67 if (path && (st.st_mode & 07777) != expected_mode) {
68 fprintf(stderr,
69 "FAIL [%s]: %s mode is 0%o, expected 0%o\n",
70 label, path, st.st_mode & 07777, expected_mode);
71 errs++;
72 return;
73 }
74 fprintf(stderr, "OK [%s]\n", label);
75 }
76
77 int main(int argc, char **argv)
78 {
79 if (argc != 2) {
80 fprintf(stderr, "usage: %s <module-dir>\n", argv[0]);
81 return 2;
82 }
83 if (chdir(argv[1]) < 0) {
84 perror("chdir");
85 return 2;
86 }
87
88 /* Simulate the daemon-without-chroot deployment that do_chmod_at()
89 * defends. With am_daemon=0 or am_chrooted=1 the wrapper falls
90 * through to plain do_chmod() and the symlink-race test would be
91 * meaningless. */
92 am_daemon = 1;
93 am_chrooted = 0;
94
95 /* Test layout (all inside the directory we just chdir'd to):
96 *
97 * ./realdir/sentinel -- regular target file
98 * ./inside_link -> realdir -- legitimate dir-symlink within the tree
99 * ./escape_link -> ../trap -- attacker swap, target outside tree
100 * ../trap/sentinel -- the file the attacker wants to alter
101 *
102 * The shell wrapper that calls this helper has set both sentinel
103 * files to mode 0600 so we have a clean baseline to compare.
104 */
105
106 /* Scenario A: legitimate parent dir-symlink within the tree.
107 *
108 * The within-tree symlink is followed and the chmod must succeed on
109 * every platform: the kernel RESOLVE_BENEATH paths (Linux 5.6+ openat2,
110 * FreeBSD 13+ / macOS 15+ O_RESOLVE_BENEATH) and, since the #715/-K
111 * fallback fix, the per-component O_NOFOLLOW walk too (OpenBSD, NetBSD,
112 * Solaris, older Cygwin, HPE NonStop, pre-5.6 Linux) -- which now follows
113 * an in-tree directory symlink whose target is relative and ".."-free.
114 * Escapes are still rejected on both paths (Scenario B). */
115 int rc = do_chmod_at("inside_link/sentinel", 0640);
116 check("A: legit dir-symlink within tree (followed)",
117 rc, 1, "realdir/sentinel", 0640);
118
119 /* Scenario B: parent symlink escapes the tree -- chmod must be
120 * rejected and the outside file's mode must be unchanged. */
121 rc = do_chmod_at("escape_link/sentinel", 0666);
122 check("B: parent symlink escapes tree (the attack)",
123 rc, 0, "../trap/sentinel", 0600);
124
125 /* Scenario C: plain relative path with no symlink components,
126 * regression check that the safe wrapper doesn't break the
127 * normal case. */
128 rc = do_chmod_at("realdir/sentinel", 0644);
129 check("C: plain relative path (regression check)",
130 rc, 1, "realdir/sentinel", 0644);
131
132 /* Scenario D: top-level file, no parent directory component.
133 * Falls back to do_chmod(); should succeed. */
134 rc = do_chmod_at("topfile", 0640);
135 check("D: top-level file, no parent component",
136 rc, 1, "topfile", 0640);
137
138 /* Scenario E: the LEAF component is an escaping symlink -- the chmod-
139 * specific TOCTOU, distinct from the parent races A/B. realdir is a real
140 * directory, isolating the O_NOFOLLOW leaf guard. rc is platform-dependent
141 * (refused on Linux, lchmod-the-symlink on *BSD/macOS), so assert only that
142 * the outside target's mode is unchanged. */
143 if (leaf_chmod_nofollow_supported()) {
144 rc = do_chmod_at("realdir/leaflink", 0666);
145 check("E: leaf component is an escaping symlink (must not be followed)",
146 rc, -1, "../trap/sentinel", 0600);
147 } else {
148 fprintf(stderr, "INFO: leaf-nofollow chmod unsupported here; "
149 "do_chmod_at follows a leaf symlink (documented limitation), "
150 "skipping scenario E\n");
151 }
152
153 if (errs)
154 fprintf(stderr, "%d failure(s)\n", errs);
155 return errs ? 1 : 0;
156 }