1 /* $NetBSD: t_dup.c,v 1.9 2017/01/13 20:31:53 christos Exp $ */
2 
3 /*-
4  * Copyright (c) 2011 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jukka Ruohonen.
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_dup.c,v 1.9 2017/01/13 20:31:53 christos Exp $");
33 
34 #include <sys/resource.h>
35 #include <sys/stat.h>
36 #include <sys/wait.h>
37 
38 #include <atf-c.h>
39 #include <errno.h>
40 #include <fcntl.h>
41 #include <limits.h>
42 #include <stdbool.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <unistd.h>
47 #include <sysexits.h>
48 
49 static char	path[] = "dup";
50 #ifdef __NetBSD__
51 static void	check_mode(bool, bool, bool);
52 #endif
53 
54 static void
55 check_mode(bool _dup, bool _dup2, bool _dup3)
56 {
57 	int mode[3] = { O_RDONLY, O_WRONLY, O_RDWR   };
58 	int perm[5] = { 0700, 0400, 0600, 0444, 0666 };
59 	struct stat st, st1;
60 	int fd, fd1, fd2;
61 	size_t i, j;
62 
63 	/*
64 	 * Check that a duplicated descriptor
65 	 * retains the mode of the original file.
66 	 */
67 	for (i = 0; i < __arraycount(mode); i++) {
68 
69 		for (j = 0; j < __arraycount(perm); j++) {
70 
71 			fd1 = open(path, mode[i] | O_CREAT, perm[j]);
72 			fd2 = open("/etc/passwd", O_RDONLY);
73 
74 			ATF_REQUIRE(fd1 >= 0);
75 			ATF_REQUIRE(fd2 >= 0);
76 
77 			if (_dup != false)
78 				fd = dup(fd1);
79 			else if (_dup2 != false)
80 				fd = dup2(fd1, fd2);
81 			else if (_dup3 != false)
82 				fd = dup3(fd1, fd2, O_CLOEXEC);
83 			else {
84 				fd = -1;
85 			}
86 
87 			ATF_REQUIRE(fd >= 0);
88 
89 			(void)memset(&st, 0, sizeof(struct stat));
90 			(void)memset(&st1, 0, sizeof(struct stat));
91 
92 			ATF_REQUIRE(fstat(fd, &st) == 0);
93 			ATF_REQUIRE(fstat(fd1, &st1) == 0);
94 
95 			if (st.st_mode != st1.st_mode)
96 				atf_tc_fail("invalid mode");
97 
98 			(void)close(fd);
99 			(void)close(fd1);
100 			(void)close(fd2);
101 			(void)unlink(path);
102 		}
103 	}
104 }
105 
106 ATF_TC(dup2_basic);
107 ATF_TC_HEAD(dup2_basic, tc)
108 {
109 	atf_tc_set_md_var(tc, "descr", "A basic test of dup2(2)");
110 }
111 
112 ATF_TC_BODY(dup2_basic, tc)
113 {
114 	int fd, fd1, fd2;
115 
116 	fd1 = open("/etc/passwd", O_RDONLY);
117 	fd2 = open("/etc/passwd", O_RDONLY);
118 
119 	ATF_REQUIRE(fd1 >= 0);
120 	ATF_REQUIRE(fd2 >= 0);
121 
122 	fd = dup2(fd1, fd2);
123 	ATF_REQUIRE(fd >= 0);
124 
125 	if (fd != fd2)
126 		atf_tc_fail("invalid descriptor");
127 
128 	(void)close(fd);
129 	(void)close(fd1);
130 
131 	ATF_REQUIRE(close(fd2) != 0);
132 }
133 
134 ATF_TC(dup2_err);
135 ATF_TC_HEAD(dup2_err, tc)
136 {
137 	atf_tc_set_md_var(tc, "descr", "Test error conditions of dup2(2)");
138 }
139 
140 ATF_TC_BODY(dup2_err, tc)
141 {
142 	int fd;
143 
144 	fd = open("/etc/passwd", O_RDONLY);
145 	ATF_REQUIRE(fd >= 0);
146 
147 	errno = 0;
148 	ATF_REQUIRE_ERRNO(EBADF, dup2(-1, -1) == -1);
149 
150 	errno = 0;
151 	ATF_REQUIRE_ERRNO(EBADF, dup2(fd, -1) == -1);
152 
153 	errno = 0;
154 	ATF_REQUIRE_ERRNO(EBADF, dup2(-1, fd) == -1);
155 
156 	/*
157 	 * Note that this should not fail with EINVAL.
158 	 */
159 	ATF_REQUIRE(dup2(fd, fd) != -1);
160 
161 	(void)close(fd);
162 }
163 
164 ATF_TC(dup2_max);
165 ATF_TC_HEAD(dup2_max, tc)
166 {
167 	atf_tc_set_md_var(tc, "descr", "Test dup2(2) against limits");
168 }
169 
170 ATF_TC_BODY(dup2_max, tc)
171 {
172 	struct rlimit res;
173 
174 	(void)memset(&res, 0, sizeof(struct rlimit));
175 	(void)getrlimit(RLIMIT_NOFILE, &res);
176 
177 	errno = 0;
178 	ATF_REQUIRE_ERRNO(EBADF, dup2(STDERR_FILENO, res.rlim_cur + 1) == -1);
179 }
180 
181 ATF_TC_WITH_CLEANUP(dup2_mode);
182 ATF_TC_HEAD(dup2_mode, tc)
183 {
184 	atf_tc_set_md_var(tc, "descr", "A basic test of dup2(2)");
185 }
186 
187 ATF_TC_BODY(dup2_mode, tc)
188 {
189 	check_mode(false, true, false);
190 }
191 
192 ATF_TC_CLEANUP(dup2_mode, tc)
193 {
194 	(void)unlink(path);
195 }
196 
197 
198 ATF_TC(dup3_err);
199 ATF_TC_HEAD(dup3_err, tc)
200 {
201 	atf_tc_set_md_var(tc, "descr",
202 	    "Test error conditions of dup3(2) (PR lib/45148)");
203 }
204 
205 ATF_TC_BODY(dup3_err, tc)
206 {
207 	int fd;
208 
209 	fd = open("/etc/passwd", O_RDONLY);
210 	ATF_REQUIRE(fd >= 0);
211 
212 	errno = 0;
213 #if defined(__FreeBSD__) || defined(__linux__)
214 	/*
215 	 * FreeBSD and linux return EINVAL, because...
216 	 *
217 	 * [EINVAL] The oldd argument is equal to the newd argument.
218 	 */
219 	ATF_REQUIRE(dup3(fd, fd, O_CLOEXEC) == -1);
220 #else
221 	ATF_REQUIRE(dup3(fd, fd, O_CLOEXEC) != -1);
222 #endif
223 
224 	errno = 0;
225 #if defined(__FreeBSD__) || defined(__linux__)
226 	ATF_REQUIRE_ERRNO(EINVAL, dup3(-1, -1, O_CLOEXEC) == -1);
227 	ATF_REQUIRE_ERRNO(EBADF, dup3(fd, -1, O_CLOEXEC) == -1);
228 #else
229 	ATF_REQUIRE_ERRNO(EBADF, dup3(-1, -1, O_CLOEXEC) == -1);
230 #endif
231 
232 	errno = 0;
233 	ATF_REQUIRE_ERRNO(EBADF, dup3(fd, -1, O_CLOEXEC) == -1);
234 
235 	errno = 0;
236 	ATF_REQUIRE_ERRNO(EBADF, dup3(-1, fd, O_CLOEXEC) == -1);
237 
238 	errno = 0;
239 	ATF_REQUIRE_ERRNO(EINVAL, dup3(fd, 1, O_NOFOLLOW) == -1);
240 
241 	(void)close(fd);
242 }
243 
244 ATF_TC(dup3_max);
245 ATF_TC_HEAD(dup3_max, tc)
246 {
247 	atf_tc_set_md_var(tc, "descr", "Test dup3(2) against limits");
248 }
249 
250 ATF_TC_BODY(dup3_max, tc)
251 {
252 	struct rlimit res;
253 
254 	(void)memset(&res, 0, sizeof(struct rlimit));
255 	(void)getrlimit(RLIMIT_NOFILE, &res);
256 
257 	errno = 0;
258 	ATF_REQUIRE_ERRNO(EBADF, dup3(STDERR_FILENO,
259 		res.rlim_cur + 1, O_CLOEXEC) == -1);
260 }
261 
262 ATF_TC_WITH_CLEANUP(dup3_mode);
263 ATF_TC_HEAD(dup3_mode, tc)
264 {
265 	atf_tc_set_md_var(tc, "descr", "A basic test of dup3(2)");
266 }
267 
268 ATF_TC_BODY(dup3_mode, tc)
269 {
270 	check_mode(false, false, true);
271 }
272 
273 ATF_TC_CLEANUP(dup3_mode, tc)
274 {
275 	(void)unlink(path);
276 }
277 
278 ATF_TC(dup_err);
279 ATF_TC_HEAD(dup_err, tc)
280 {
281 	atf_tc_set_md_var(tc, "descr", "Test error conditions of dup(2)");
282 }
283 
284 ATF_TC_BODY(dup_err, tc)
285 {
286 
287 	errno = 0;
288 	ATF_REQUIRE_ERRNO(EBADF, dup(-1) == -1);
289 }
290 
291 ATF_TC_WITH_CLEANUP(dup_max);
292 ATF_TC_HEAD(dup_max, tc)
293 {
294 	atf_tc_set_md_var(tc, "descr", "Test dup(2) against limits");
295 }
296 
297 ATF_TC_BODY(dup_max, tc)
298 {
299 	struct rlimit res;
300 	int *buf, fd, sta;
301 	size_t i, n;
302 	pid_t pid;
303 
304 	pid = fork();
305 	ATF_REQUIRE(pid >= 0);
306 
307 	if (pid == 0) {
308 
309 		/*
310 		 * Open a temporary file until the
311 		 * maximum number of open files is
312 		 * reached. Ater that dup(2) family
313 		 * should fail with EMFILE.
314 		 */
315 		(void)closefrom(0);
316 		(void)memset(&res, 0, sizeof(struct rlimit));
317 
318 		n = 10;
319 		res.rlim_cur = res.rlim_max = n;
320 		if (setrlimit(RLIMIT_NOFILE, &res) != 0)
321 			_exit(EX_OSERR);
322 
323 		buf = calloc(n, sizeof(int));
324 
325 		if (buf == NULL)
326 			_exit(EX_OSERR);
327 
328 		buf[0] = mkstemp(path);
329 
330 		if (buf[0] < 0)
331 			_exit(EX_OSERR);
332 
333 		for (i = 1; i < n; i++) {
334 
335 			buf[i] = open(path, O_RDONLY);
336 
337 			if (buf[i] < 0)
338 				_exit(EX_OSERR);
339 		}
340 
341 		errno = 0;
342 		fd = dup(buf[0]);
343 
344 		if (fd != -1 || errno != EMFILE)
345 			_exit(EX_DATAERR);
346 
347 		_exit(EXIT_SUCCESS);
348 	}
349 
350 	(void)wait(&sta);
351 
352 	if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS) {
353 
354 		if (WEXITSTATUS(sta) == EX_OSERR)
355 			atf_tc_fail("system call error");
356 
357 		if (WEXITSTATUS(sta) == EX_DATAERR)
358 			atf_tc_fail("dup(2) dupped more than RLIMIT_NOFILE");
359 
360 		atf_tc_fail("unknown error");
361 	}
362 
363 	(void)unlink(path);
364 }
365 
366 ATF_TC_CLEANUP(dup_max, tc)
367 {
368 	(void)unlink(path);
369 }
370 
371 ATF_TC_WITH_CLEANUP(dup_mode);
372 ATF_TC_HEAD(dup_mode, tc)
373 {
374 	atf_tc_set_md_var(tc, "descr", "A basic test of dup(2)");
375 }
376 
377 ATF_TC_BODY(dup_mode, tc)
378 {
379 	check_mode(true, false, false);
380 }
381 
382 ATF_TC_CLEANUP(dup_mode, tc)
383 {
384 	(void)unlink(path);
385 }
386 
387 ATF_TP_ADD_TCS(tp)
388 {
389 
390 	ATF_TP_ADD_TC(tp, dup2_basic);
391 	ATF_TP_ADD_TC(tp, dup2_err);
392 	ATF_TP_ADD_TC(tp, dup2_max);
393 	ATF_TP_ADD_TC(tp, dup2_mode);
394 	ATF_TP_ADD_TC(tp, dup3_err);
395 	ATF_TP_ADD_TC(tp, dup3_max);
396 	ATF_TP_ADD_TC(tp, dup3_mode);
397 	ATF_TP_ADD_TC(tp, dup_err);
398 	ATF_TP_ADD_TC(tp, dup_max);
399 	ATF_TP_ADD_TC(tp, dup_mode);
400 
401 	return atf_no_error();
402 }
403