xref: /freebsd/contrib/netbsd-tests/lib/libc/c063/t_o_search.c (revision 14d3b069199d6a780cb83f55dfff11ed1ccea69d)
1 /*	$NetBSD: t_o_search.c,v 1.9 2020/02/06 12:18:06 martin Exp $ */
2 
3 /*-
4  * Copyright (c) 2012 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Emmanuel Dreyfus.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_o_search.c,v 1.9 2020/02/06 12:18:06 martin Exp $");
33 
34 #include <atf-c.h>
35 
36 #include <sys/types.h>
37 #ifdef __FreeBSD__
38 #include <sys/mount.h>
39 #else
40 #include <sys/statvfs.h>
41 #endif
42 #include <sys/stat.h>
43 
44 #include <dirent.h>
45 #include <errno.h>
46 #include <fcntl.h>
47 #include <limits.h>
48 #include <paths.h>
49 #include <stdio.h>
50 #include <string.h>
51 #include <unistd.h>
52 #include <pwd.h>
53 
54 /*
55  * dholland 20130112: disable tests that require O_SEARCH semantics
56  * until a decision is reached about the semantics of O_SEARCH and a
57  * non-broken implementation is available.
58  */
59 #if defined(__FreeBSD__) || (O_MASK & O_SEARCH) != 0
60 #define USE_O_SEARCH
61 #endif
62 
63 #define DIR "dir"
64 #define FILE "dir/o_search"
65 #define BASEFILE "o_search"
66 
67 
68 ATF_TC(o_search_perm1);
69 ATF_TC_HEAD(o_search_perm1, tc)
70 {
71 	atf_tc_set_md_var(tc, "descr", "See that openat enforces search permission");
72 	atf_tc_set_md_var(tc, "require.user", "unprivileged");
73 }
74 ATF_TC_BODY(o_search_perm1, tc)
75 {
76 	int dfd;
77 	int fd;
78 
79 	ATF_REQUIRE(mkdir(DIR, 0755) == 0);
80 	ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1);
81 	ATF_REQUIRE(close(fd) == 0);
82 
83 	ATF_REQUIRE((dfd = open(DIR, O_RDONLY, 0)) != -1);
84 
85 	ATF_REQUIRE((fd = openat(dfd, BASEFILE, O_RDWR, 0)) != -1);
86 	ATF_REQUIRE(close(fd) == 0);
87 
88 	ATF_REQUIRE(fchmod(dfd, 0644) == 0);
89 
90 	ATF_REQUIRE((fd = openat(dfd, BASEFILE, O_RDWR, 0)) == -1);
91 	ATF_REQUIRE(errno == EACCES);
92 
93 	ATF_REQUIRE(close(dfd) == 0);
94 }
95 
96 #ifdef USE_O_SEARCH
97 
98 ATF_TC(o_search_root_flag1);
99 ATF_TC_HEAD(o_search_root_flag1, tc)
100 {
101 	atf_tc_set_md_var(tc, "descr", "See that root openat honours O_SEARCH");
102 	atf_tc_set_md_var(tc, "require.user", "root");
103 }
104 ATF_TC_BODY(o_search_root_flag1, tc)
105 {
106 	int dfd;
107 	int fd;
108 
109 	ATF_REQUIRE(mkdir(DIR, 0755) == 0);
110 	ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1);
111 	ATF_REQUIRE(close(fd) == 0);
112 
113 	ATF_REQUIRE((dfd = open(DIR, O_RDONLY|O_SEARCH, 0)) != -1);
114 
115 	ATF_REQUIRE((fd = openat(dfd, BASEFILE, O_RDWR, 0)) != -1);
116 	ATF_REQUIRE(close(fd) == 0);
117 
118 	ATF_REQUIRE(fchmod(dfd, 0644) == 0);
119 
120 	ATF_REQUIRE((fd = openat(dfd, BASEFILE, O_RDWR, 0)) != -1);
121 	ATF_REQUIRE(close(fd) == 0);
122 
123 	ATF_REQUIRE(fchmod(dfd, 0444) == 0);
124 
125 	ATF_REQUIRE((fd = openat(dfd, BASEFILE, O_RDWR, 0)) != -1);
126 
127 	ATF_REQUIRE(close(dfd) == 0);
128 }
129 
130 ATF_TC(o_search_unpriv_flag1);
131 ATF_TC_HEAD(o_search_unpriv_flag1, tc)
132 {
133 	atf_tc_set_md_var(tc, "descr", "See that openat honours O_SEARCH");
134 	atf_tc_set_md_var(tc, "require.user", "unprivileged");
135 }
136 ATF_TC_BODY(o_search_unpriv_flag1, tc)
137 {
138 	int dfd;
139 	int fd;
140 
141 	ATF_REQUIRE(mkdir(DIR, 0755) == 0);
142 	ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1);
143 	ATF_REQUIRE(close(fd) == 0);
144 
145 	ATF_REQUIRE((dfd = open(DIR, O_RDONLY|O_SEARCH, 0)) != -1);
146 
147 	ATF_REQUIRE((fd = openat(dfd, BASEFILE, O_RDWR, 0)) != -1);
148 	ATF_REQUIRE(close(fd) == 0);
149 
150 	ATF_REQUIRE(fchmod(dfd, 0644) == 0);
151 
152 	ATF_REQUIRE((fd = openat(dfd, BASEFILE, O_RDWR, 0)) != -1);
153 	ATF_REQUIRE(close(fd) == 0);
154 
155 	ATF_REQUIRE(fchmod(dfd, 0444) == 0);
156 
157 	ATF_REQUIRE((fd = openat(dfd, BASEFILE, O_RDWR, 0)) != -1);
158 
159 	ATF_REQUIRE(close(dfd) == 0);
160 }
161 
162 #endif /* USE_O_SEARCH */
163 
164 ATF_TC(o_search_perm2);
165 ATF_TC_HEAD(o_search_perm2, tc)
166 {
167 	atf_tc_set_md_var(tc, "descr", "See that faccessat enforces search permission");
168 	atf_tc_set_md_var(tc, "require.user", "unprivileged");
169 }
170 ATF_TC_BODY(o_search_perm2, tc)
171 {
172 	int dfd;
173 	int fd;
174 	ATF_REQUIRE(mkdir(DIR, 0755) == 0);
175 	ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1);
176 	ATF_REQUIRE(close(fd) == 0);
177 
178 	ATF_REQUIRE((dfd = open(DIR, O_RDONLY, 0)) != -1);
179 
180 	ATF_REQUIRE(faccessat(dfd, BASEFILE, W_OK, 0) == 0);
181 
182 	ATF_REQUIRE(fchmod(dfd, 0644) == 0);
183 
184 	ATF_REQUIRE(faccessat(dfd, BASEFILE, W_OK, 0) == -1);
185 	ATF_REQUIRE(errno == EACCES);
186 
187 	ATF_REQUIRE(close(dfd) == 0);
188 }
189 
190 #ifdef USE_O_SEARCH
191 
192 ATF_TC(o_search_root_flag2);
193 ATF_TC_HEAD(o_search_root_flag2, tc)
194 {
195 	atf_tc_set_md_var(tc, "descr", "See that root fstatat honours O_SEARCH");
196 	atf_tc_set_md_var(tc, "require.user", "root");
197 }
198 ATF_TC_BODY(o_search_root_flag2, tc)
199 {
200 	int dfd;
201 	int fd;
202 
203 	ATF_REQUIRE(mkdir(DIR, 0755) == 0);
204 	ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1);
205 	ATF_REQUIRE(close(fd) == 0);
206 
207 	ATF_REQUIRE((dfd = open(DIR, O_RDONLY|O_SEARCH, 0)) != -1);
208 
209 	ATF_REQUIRE(faccessat(dfd, BASEFILE, W_OK, 0) == 0);
210 
211 	ATF_REQUIRE(fchmod(dfd, 0644) == 0);
212 
213 	ATF_REQUIRE(faccessat(dfd, BASEFILE, W_OK, 0) == 0);
214 
215 	ATF_REQUIRE(fchmod(dfd, 0444) == 0);
216 
217 	ATF_REQUIRE(faccessat(dfd, BASEFILE, W_OK, 0) == 0);
218 
219 	ATF_REQUIRE(close(dfd) == 0);
220 }
221 
222 ATF_TC(o_search_unpriv_flag2);
223 ATF_TC_HEAD(o_search_unpriv_flag2, tc)
224 {
225 	atf_tc_set_md_var(tc, "descr", "See that fstatat honours O_SEARCH");
226 	atf_tc_set_md_var(tc, "require.user", "unprivileged");
227 }
228 ATF_TC_BODY(o_search_unpriv_flag2, tc)
229 {
230 	int dfd;
231 	int fd;
232 
233 	ATF_REQUIRE(mkdir(DIR, 0755) == 0);
234 	ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1);
235 	ATF_REQUIRE(close(fd) == 0);
236 
237 	ATF_REQUIRE((dfd = open(DIR, O_RDONLY|O_SEARCH, 0)) != -1);
238 
239 	ATF_REQUIRE(faccessat(dfd, BASEFILE, W_OK, 0) == 0);
240 
241 	ATF_REQUIRE(fchmod(dfd, 0644) == 0);
242 
243 	ATF_REQUIRE(faccessat(dfd, BASEFILE, W_OK, 0) == 0);
244 
245 	ATF_REQUIRE(fchmod(dfd, 0444) == 0);
246 
247 	ATF_REQUIRE(faccessat(dfd, BASEFILE, W_OK, 0) == 0);
248 
249 	ATF_REQUIRE(close(dfd) == 0);
250 }
251 
252 #endif /* USE_O_SEARCH */
253 
254 
255 ATF_TC(o_search_notdir);
256 ATF_TC_HEAD(o_search_notdir, tc)
257 {
258 	atf_tc_set_md_var(tc, "descr", "See that openat fails with non dir fd");
259 }
260 ATF_TC_BODY(o_search_notdir, tc)
261 {
262 	int dfd;
263 	int fd;
264 
265 	ATF_REQUIRE(mkdir(DIR, 0755) == 0);
266 	ATF_REQUIRE((dfd = open(FILE, O_CREAT|O_SEARCH, 0644)) != -1);
267 	ATF_REQUIRE((fd = openat(dfd, BASEFILE, O_RDWR, 0)) == -1);
268 	ATF_REQUIRE(errno == ENOTDIR);
269 	ATF_REQUIRE(close(dfd) == 0);
270 }
271 
272 #ifdef USE_O_SEARCH
273 ATF_TC(o_search_nord);
274 ATF_TC_HEAD(o_search_nord, tc)
275 {
276 	atf_tc_set_md_var(tc, "descr", "See that openat succeeds with no read permission");
277 	atf_tc_set_md_var(tc, "require.user", "unprivileged");
278 }
279 ATF_TC_BODY(o_search_nord, tc)
280 {
281 	int dfd, fd;
282 
283 	ATF_REQUIRE(mkdir(DIR, 0755) == 0);
284 	ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1);
285 	ATF_REQUIRE(close(fd) == 0);
286 
287 	ATF_REQUIRE(chmod(DIR, 0100) == 0);
288 	ATF_REQUIRE((dfd = open(DIR, O_SEARCH, 0)) != -1);
289 
290 	ATF_REQUIRE(faccessat(dfd, BASEFILE, W_OK, 0) != -1);
291 
292 	ATF_REQUIRE(close(dfd) == 0);
293 }
294 
295 ATF_TC(o_search_getdents);
296 ATF_TC_HEAD(o_search_getdents, tc)
297 {
298 	atf_tc_set_md_var(tc, "descr", "See that O_SEARCH forbids getdents");
299 }
300 ATF_TC_BODY(o_search_getdents, tc)
301 {
302 	char buf[1024];
303 	int dfd;
304 
305 	ATF_REQUIRE(mkdir(DIR, 0755) == 0);
306 	ATF_REQUIRE((dfd = open(DIR, O_SEARCH, 0)) != -1);
307 	ATF_REQUIRE(getdents(dfd, buf, sizeof(buf)) < 0);
308 	ATF_REQUIRE(close(dfd) == 0);
309 }
310 
311 ATF_TC(o_search_revokex);
312 ATF_TC_HEAD(o_search_revokex, tc)
313 {
314 	atf_tc_set_md_var(tc, "descr", "See that *at behaves after chmod -x");
315 	atf_tc_set_md_var(tc, "require.user", "unprivileged");
316 }
317 ATF_TC_BODY(o_search_revokex, tc)
318 {
319 	int dfd, fd;
320 	struct stat sb;
321 
322 	ATF_REQUIRE(mkdir(DIR, 0755) == 0);
323 	ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1);
324 	ATF_REQUIRE(close(fd) == 0);
325 
326 	ATF_REQUIRE((dfd = open(DIR, O_SEARCH, 0)) != -1);
327 
328 	/* Drop permissions. The kernel must still not check the exec bit. */
329 	ATF_REQUIRE(chmod(DIR, 0000) == 0);
330 	{
331 		const char *fstypename;
332 #ifdef __FreeBSD__
333 		struct statfs st;
334 
335 		fstatfs(dfd, &st);
336 		fstypename = st.f_fstypename;
337 #else
338 		struct statvfs vst;
339 
340 		fstatvfs(dfd, &vst);
341 		fstypename = vst.f_fstypename;
342 #endif
343 		if (strcmp(fstypename, "nfs") == 0)
344 			atf_tc_expect_fail(
345 			    "NFS protocol cannot observe O_SEARCH semantics");
346 	}
347 	ATF_REQUIRE(fstatat(dfd, BASEFILE, &sb, 0) == 0);
348 
349 	ATF_REQUIRE(close(dfd) == 0);
350 }
351 #endif /* USE_O_SEARCH */
352 
353 ATF_TP_ADD_TCS(tp)
354 {
355 
356 	ATF_TP_ADD_TC(tp, o_search_perm1);
357 #ifdef USE_O_SEARCH
358 	ATF_TP_ADD_TC(tp, o_search_root_flag1);
359 	ATF_TP_ADD_TC(tp, o_search_unpriv_flag1);
360 #endif
361 	ATF_TP_ADD_TC(tp, o_search_perm2);
362 #ifdef USE_O_SEARCH
363 	ATF_TP_ADD_TC(tp, o_search_root_flag2);
364 	ATF_TP_ADD_TC(tp, o_search_unpriv_flag2);
365 #endif
366 	ATF_TP_ADD_TC(tp, o_search_notdir);
367 #ifdef USE_O_SEARCH
368 	ATF_TP_ADD_TC(tp, o_search_nord);
369 	ATF_TP_ADD_TC(tp, o_search_getdents);
370 	ATF_TP_ADD_TC(tp, o_search_revokex);
371 #endif
372 
373 	return atf_no_error();
374 }
375