Debian Codegraph @

t_secure_relpath.c

raw symbols SCIP
1 /*
2 * Test harness for secure_relative_open()'s front-door input
3 * validation. Codex audit Finding 5 noted that the existing check
4 *
5 * if (strncmp(relpath, "../", 3) == 0 || strstr(relpath, "/../"))
6 *
7 * catches "../foo" and "foo/../bar" but misses bare ".." (an actual
8 * one-level escape on platforms that fall back to the per-component
9 * walk), as well as "a/..", "foo/..", and any other form that
10 * decomposes to a ".." component when split on "/". The kernel-
11 * enforced RESOLVE_BENEATH (Linux 5.6+) and O_RESOLVE_BENEATH
12 * (FreeBSD 13+, macOS 15+) reject these in-kernel; the per-
13 * component fallback used on NetBSD, OpenBSD, Solaris, Cygwin and
14 * pre-5.6 Linux does not, so the validation must happen at the
15 * front door.
16 *
17 * This helper invokes secure_relative_open() with each suspect
18 * input and checks both the failure (rc < 0) and the errno
19 * (EINVAL means "rejected at the front door"). Pre-fix, the kernel
20 * may reject with a different errno (EXDEV from RESOLVE_BENEATH);
21 * post-fix, the front-door check catches every variant up front
22 * with a consistent EINVAL across platforms.
23 *
24 * Not linked into rsync itself.
25 */
26
27 #include "rsync.h"
28
29 #include <sys/stat.h>
30
31 int dry_run = 0;
32 int am_root = 0;
33 int am_sender = 0;
34 int read_only = 0;
35 int list_only = 0;
36 int copy_links = 0;
37 int copy_unsafe_links = 0;
38 extern int am_daemon, am_chrooted;
39
40 short info_levels[COUNT_INFO], debug_levels[COUNT_DEBUG];
41
42 static int errs = 0;
43
44 static void check_relpath(const char *relpath)
45 {
46 int fd;
47 int saved_errno;
48
49 errno = 0;
50 fd = secure_relative_open(NULL, relpath, O_RDONLY | O_DIRECTORY, 0);
51 saved_errno = errno;
52
53 if (fd >= 0) {
54 fprintf(stderr,
55 "FAIL [relpath=%-12s]: returned valid fd %d (escape) -- expected -1 EINVAL\n",
56 relpath, fd);
57 close(fd);
58 errs++;
59 return;
60 }
61
62 if (saved_errno != EINVAL) {
63 fprintf(stderr,
64 "FAIL [relpath=%-12s]: rejected but errno=%d (%s), expected EINVAL\n",
65 relpath, saved_errno, strerror(saved_errno));
66 errs++;
67 return;
68 }
69
70 fprintf(stderr, "OK [relpath=%-12s]: rejected with EINVAL\n", relpath);
71 }
72
73 static void check_basedir(const char *basedir)
74 {
75 int fd;
76 int saved_errno;
77
78 errno = 0;
79 fd = secure_relative_open(basedir, "ok", O_RDONLY | O_DIRECTORY, 0);
80 saved_errno = errno;
81
82 if (fd >= 0) {
83 fprintf(stderr,
84 "FAIL [basedir=%-12s]: returned valid fd %d -- expected -1 EINVAL\n",
85 basedir, fd);
86 close(fd);
87 errs++;
88 return;
89 }
90
91 if (saved_errno != EINVAL) {
92 fprintf(stderr,
93 "FAIL [basedir=%-12s]: rejected but errno=%d (%s), expected EINVAL\n",
94 basedir, saved_errno, strerror(saved_errno));
95 errs++;
96 return;
97 }
98
99 fprintf(stderr, "OK [basedir=%-12s]: rejected with EINVAL\n", basedir);
100 }
101
102 static void check_beneath_dotdot(void)
103 {
104 STRUCT_STAT ast, fst;
105 int anchor, fd;
106
107 anchor = open(".", O_RDONLY | O_DIRECTORY);
108 if (anchor < 0 || fstat(anchor, &ast) < 0) {
109 perror("open/fstat anchor");
110 errs++;
111 return;
112 }
113
114 fd = secure_relative_open_at_beneath(anchor, "alias/../subdir",
115 O_RDONLY | O_DIRECTORY, 0);
116 if (fd < 0 || fstat(fd, &fst) < 0 || fst.st_dev != ast.st_dev
117 || fst.st_ino == ast.st_ino) {
118 fprintf(stderr, "FAIL [beneath safe]: in-anchor '..' was not resolved\n");
119 errs++;
120 } else
121 fprintf(stderr, "OK [beneath safe]: in-anchor '..' resolved\n");
122 if (fd >= 0)
123 close(fd);
124
125 /* A bare final ".." must be refused whatever flags the caller passes. The
126 * leaf fast paths in secure_walk_at() openat() the final component
127 * directly, so before they learned to defer a literal ".." to ds_descend()
128 * an O_NOFOLLOW caller got back the anchor's own parent, and a caller
129 * without O_DIRECTORY opened it transiently. Only the O_DIRECTORY form
130 * was ever refused. */
131 {
132 static const struct { const char *label; int flags; } dotdot_cases[] = {
133 { "O_DIRECTORY", O_RDONLY | O_DIRECTORY },
134 { "O_DIRECTORY|O_NOFOLLOW", O_RDONLY | O_DIRECTORY | O_NOFOLLOW },
135 { "no O_DIRECTORY", O_RDONLY },
136 };
137 unsigned ci;
138 for (ci = 0; ci < sizeof dotdot_cases / sizeof *dotdot_cases; ci++) {
139 int dfd;
140 errno = 0;
141 dfd = secure_relative_open_at_beneath(anchor, "..",
142 dotdot_cases[ci].flags, 0);
143 if (dfd >= 0) {
144 STRUCT_STAT dst;
145 int above = fstat(dfd, &dst) == 0 && dst.st_ino != ast.st_ino;
146 fprintf(stderr, "FAIL [beneath bare-dotdot %s]: rc=%d%s\n",
147 dotdot_cases[ci].label, dfd,
148 above ? " -- resolved ABOVE the anchor" : "");
149 errs++;
150 close(dfd);
151 } else if (errno != ELOOP) {
152 fprintf(stderr, "FAIL [beneath bare-dotdot %s]: errno=%d, expected ELOOP\n",
153 dotdot_cases[ci].label, errno);
154 errs++;
155 } else
156 fprintf(stderr, "OK [beneath bare-dotdot %s]: refused with ELOOP\n",
157 dotdot_cases[ci].label);
158 }
159 }
160
161 errno = 0;
162 fd = secure_relative_open_at_beneath(anchor, "../outside",
163 O_RDONLY | O_DIRECTORY, 0);
164 if (fd >= 0 || errno != ELOOP) {
165 fprintf(stderr, "FAIL [beneath escape]: rc=%d errno=%d, expected -1/ELOOP\n",
166 fd, errno);
167 if (fd >= 0)
168 close(fd);
169 errs++;
170 } else
171 fprintf(stderr, "OK [beneath escape]: climb above anchor refused\n");
172
173 close(anchor);
174 }
175
176 int main(int argc, char **argv)
177 {
178 if (argc != 2) {
179 fprintf(stderr, "usage: %s <test-dir>\n", argv[0]);
180 return 2;
181 }
182 if (chdir(argv[1]) < 0) {
183 perror("chdir");
184 return 2;
185 }
186
187 /* secure_relative_open's daemon-only confinement protections only
188 * fire when am_daemon && !am_chrooted (the threat model is the
189 * daemon-no-chroot deployment), but the front-door input
190 * validation runs unconditionally. We set am_daemon anyway so the
191 * helper exercises the same code shape the receiver does. */
192 am_daemon = 1;
193 am_chrooted = 0;
194
195 mkdir("subdir", 0755);
196 symlink("subdir", "alias");
197
198 /* Each of these relpaths must be rejected with EINVAL at the
199 * secure_relative_open() front door. ".." is the actual one-level
200 * escape; the others ("subdir/..", "subdir/../subdir") resolve
201 * back to the start dir on systems that allow them, but we still
202 * reject them as defence-in-depth: a path containing a ".." token
203 * is suspicious and the caller should normalise before passing
204 * it in. The "../foo" / "foo/../bar" / "/foo" / "/" cases are
205 * regression checks for the existing checks. */
206 check_relpath("..");
207 check_relpath("../foo");
208 check_relpath("subdir/..");
209 check_relpath("subdir/../subdir");
210 check_relpath("foo/../bar");
211 check_relpath("/foo");
212 check_relpath("/");
213
214 /* Same checks against basedir (which the codex Finding 2 fix
215 * routes through the same RESOLVE_BENEATH-equivalent). Absolute
216 * basedirs are operator-trusted and intentionally not validated
217 * here. */
218 check_basedir("..");
219 check_basedir("../subdir");
220 check_basedir("subdir/..");
221 check_basedir("foo/../bar");
222
223 /* A followed symlink target is the one caller that legitimately carries
224 * literal '..'. Its dedicated fd-anchored entry point must preserve an
225 * in-tree climb while refusing to pop above the anchor. */
226 check_beneath_dotdot();
227
228 if (errs)
229 fprintf(stderr, "\n%d failure(s)\n", errs);
230 return errs ? 1 : 0;
231 }