xref: /openbsd-src/regress/lib/libc/sys/t_dup.c (revision fcde59b201a29a2b4570b00b71e7aa25d61cb5c1)
1 /*	$OpenBSD: t_dup.c,v 1.2 2020/11/09 23:18:51 bluhm Exp $	*/
2 /* $NetBSD: t_dup.c,v 1.9 2017/01/13 20:31:53 christos Exp $ */
3 
4 /*-
5  * Copyright (c) 2011 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Jukka Ruohonen.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include "macros.h"
34 
35 #include <sys/cdefs.h>
36 __RCSID("$NetBSD: t_dup.c,v 1.9 2017/01/13 20:31:53 christos Exp $");
37 
38 #include <sys/resource.h>
39 #include <sys/stat.h>
40 #include <sys/wait.h>
41 
42 #include "atf-c.h"
43 #include <errno.h>
44 #include <fcntl.h>
45 #include <limits.h>
46 #include <stdbool.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <unistd.h>
51 #include <sysexits.h>
52 
53 static char	path[] = "dup";
54 static void	check_mode(bool, bool, bool);
55 
56 static void
57 check_mode(bool _dup, bool _dup2, bool _dup3)
58 {
59 	int mode[3] = { O_RDONLY, O_WRONLY, O_RDWR   };
60 	int perm[5] = { 0700, 0400, 0600, 0444, 0666 };
61 	struct stat st, st1;
62 	int fd, fd1, fd2;
63 	size_t i, j;
64 
65 	/*
66 	 * Check that a duplicated descriptor
67 	 * retains the mode of the original file.
68 	 */
69 	for (i = 0; i < __arraycount(mode); i++) {
70 
71 		for (j = 0; j < __arraycount(perm); j++) {
72 
73 			fd1 = open(path, mode[i] | O_CREAT, perm[j]);
74 			fd2 = open("/etc/passwd", O_RDONLY);
75 
76 			ATF_REQUIRE(fd1 >= 0);
77 			ATF_REQUIRE(fd2 >= 0);
78 
79 			if (_dup != false)
80 				fd = dup(fd1);
81 			else if (_dup2 != false)
82 				fd = dup2(fd1, fd2);
83 			else if (_dup3 != false)
84 				fd = dup3(fd1, fd2, O_CLOEXEC);
85 			else {
86 				fd = -1;
87 			}
88 
89 			ATF_REQUIRE(fd >= 0);
90 
91 			(void)memset(&st, 0, sizeof(struct stat));
92 			(void)memset(&st1, 0, sizeof(struct stat));
93 
94 			ATF_REQUIRE(fstat(fd, &st) == 0);
95 			ATF_REQUIRE(fstat(fd1, &st1) == 0);
96 
97 			if (st.st_mode != st1.st_mode)
98 				atf_tc_fail("invalid mode");
99 
100 			(void)close(fd);
101 			(void)close(fd1);
102 			(void)close(fd2);
103 			(void)unlink(path);
104 		}
105 	}
106 }
107 
108 ATF_TC(dup2_basic);
109 ATF_TC_HEAD(dup2_basic, tc)
110 {
111 	atf_tc_set_md_var(tc, "descr", "A basic test of dup2(2)");
112 }
113 
114 ATF_TC_BODY(dup2_basic, tc)
115 {
116 	int fd, fd1, fd2;
117 
118 	fd1 = open("/etc/passwd", O_RDONLY);
119 	fd2 = open("/etc/passwd", O_RDONLY);
120 
121 	ATF_REQUIRE(fd1 >= 0);
122 	ATF_REQUIRE(fd2 >= 0);
123 
124 	fd = dup2(fd1, fd2);
125 	ATF_REQUIRE(fd >= 0);
126 
127 	if (fd != fd2)
128 		atf_tc_fail("invalid descriptor");
129 
130 	(void)close(fd);
131 	(void)close(fd1);
132 
133 	ATF_REQUIRE(close(fd2) != 0);
134 }
135 
136 ATF_TC(dup2_err);
137 ATF_TC_HEAD(dup2_err, tc)
138 {
139 	atf_tc_set_md_var(tc, "descr", "Test error conditions of dup2(2)");
140 }
141 
142 ATF_TC_BODY(dup2_err, tc)
143 {
144 	int fd;
145 
146 	fd = open("/etc/passwd", O_RDONLY);
147 	ATF_REQUIRE(fd >= 0);
148 
149 	errno = 0;
150 	ATF_REQUIRE_ERRNO(EBADF, dup2(-1, -1) == -1);
151 
152 	errno = 0;
153 	ATF_REQUIRE_ERRNO(EBADF, dup2(fd, -1) == -1);
154 
155 	errno = 0;
156 	ATF_REQUIRE_ERRNO(EBADF, dup2(-1, fd) == -1);
157 
158 	/*
159 	 * Note that this should not fail with EINVAL.
160 	 */
161 	ATF_REQUIRE(dup2(fd, fd) != -1);
162 
163 	(void)close(fd);
164 }
165 
166 ATF_TC(dup2_max);
167 ATF_TC_HEAD(dup2_max, tc)
168 {
169 	atf_tc_set_md_var(tc, "descr", "Test dup2(2) against limits");
170 }
171 
172 ATF_TC_BODY(dup2_max, tc)
173 {
174 	struct rlimit res;
175 
176 	(void)memset(&res, 0, sizeof(struct rlimit));
177 	(void)getrlimit(RLIMIT_NOFILE, &res);
178 
179 	errno = 0;
180 	ATF_REQUIRE_ERRNO(EBADF, dup2(STDERR_FILENO, res.rlim_cur + 1) == -1);
181 }
182 
183 ATF_TC_WITH_CLEANUP(dup2_mode);
184 ATF_TC_HEAD(dup2_mode, tc)
185 {
186 	atf_tc_set_md_var(tc, "descr", "A basic test of dup2(2)");
187 }
188 
189 ATF_TC_BODY(dup2_mode, tc)
190 {
191 	check_mode(false, true, false);
192 }
193 
194 ATF_TC_CLEANUP(dup2_mode, tc)
195 {
196 	(void)unlink(path);
197 }
198 
199 
200 ATF_TC(dup3_err);
201 ATF_TC_HEAD(dup3_err, tc)
202 {
203 	atf_tc_set_md_var(tc, "descr",
204 	    "Test error conditions of dup3(2) (PR lib/45148)");
205 }
206 
207 ATF_TC_BODY(dup3_err, tc)
208 {
209 	int fd;
210 
211 	fd = open("/etc/passwd", O_RDONLY);
212 	ATF_REQUIRE(fd >= 0);
213 
214 	errno = 0;
215 #ifdef __OpenBSD__
216 	ATF_REQUIRE(dup3(fd, fd, O_CLOEXEC) == -1);
217 #else
218 	ATF_REQUIRE(dup3(fd, fd, O_CLOEXEC) != -1);
219 #endif
220 
221 	errno = 0;
222 #ifdef __OpenBSD__
223 	ATF_REQUIRE_ERRNO(EINVAL, dup3(-1, -1, O_CLOEXEC) == -1);
224 #else
225 	ATF_REQUIRE_ERRNO(EBADF, dup3(-1, -1, O_CLOEXEC) == -1);
226 #endif
227 
228 	errno = 0;
229 	ATF_REQUIRE_ERRNO(EBADF, dup3(fd, -1, O_CLOEXEC) == -1);
230 
231 	errno = 0;
232 	ATF_REQUIRE_ERRNO(EBADF, dup3(-1, fd, O_CLOEXEC) == -1);
233 
234 	errno = 0;
235 	ATF_REQUIRE_ERRNO(EINVAL, dup3(fd, 1, O_NOFOLLOW) == -1);
236 
237 	(void)close(fd);
238 }
239 
240 ATF_TC(dup3_max);
241 ATF_TC_HEAD(dup3_max, tc)
242 {
243 	atf_tc_set_md_var(tc, "descr", "Test dup3(2) against limits");
244 }
245 
246 ATF_TC_BODY(dup3_max, tc)
247 {
248 	struct rlimit res;
249 
250 	(void)memset(&res, 0, sizeof(struct rlimit));
251 	(void)getrlimit(RLIMIT_NOFILE, &res);
252 
253 	errno = 0;
254 	ATF_REQUIRE_ERRNO(EBADF, dup3(STDERR_FILENO,
255 		res.rlim_cur + 1, O_CLOEXEC) == -1);
256 }
257 
258 ATF_TC_WITH_CLEANUP(dup3_mode);
259 ATF_TC_HEAD(dup3_mode, tc)
260 {
261 	atf_tc_set_md_var(tc, "descr", "A basic test of dup3(2)");
262 }
263 
264 ATF_TC_BODY(dup3_mode, tc)
265 {
266 	check_mode(false, false, true);
267 }
268 
269 ATF_TC_CLEANUP(dup3_mode, tc)
270 {
271 	(void)unlink(path);
272 }
273 
274 ATF_TC(dup_err);
275 ATF_TC_HEAD(dup_err, tc)
276 {
277 	atf_tc_set_md_var(tc, "descr", "Test error conditions of dup(2)");
278 }
279 
280 ATF_TC_BODY(dup_err, tc)
281 {
282 
283 	errno = 0;
284 	ATF_REQUIRE_ERRNO(EBADF, dup(-1) == -1);
285 }
286 
287 ATF_TC_WITH_CLEANUP(dup_max);
288 ATF_TC_HEAD(dup_max, tc)
289 {
290 	atf_tc_set_md_var(tc, "descr", "Test dup(2) against limits");
291 }
292 
293 ATF_TC_BODY(dup_max, tc)
294 {
295 	struct rlimit res;
296 	int *buf, fd, sta;
297 	size_t i, n;
298 	pid_t pid;
299 
300 	pid = fork();
301 	ATF_REQUIRE(pid >= 0);
302 
303 	if (pid == 0) {
304 
305 		/*
306 		 * Open a temporary file until the
307 		 * maximum number of open files is
308 		 * reached. Ater that dup(2) family
309 		 * should fail with EMFILE.
310 		 */
311 #ifdef __OpenBSD__
312 		(void)closefrom(STDERR_FILENO + 1);
313 #else
314 		(void)closefrom(0);
315 #endif
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 #ifdef __OpenBSD__
329 		buf[0] = open(path, O_CREAT|O_EXCL|O_RDWR, S_IRUSR|S_IWUSR);
330 #else
331 		buf[0] = mkstemp(path);
332 #endif
333 
334 		if (buf[0] < 0)
335 			_exit(EX_OSERR);
336 
337 #ifdef __OpenBSD__
338 		for (i = 1; i < n - (STDERR_FILENO + 1); i++) {
339 #else
340 		for (i = 1; i < n; i++) {
341 #endif
342 
343 			buf[i] = open(path, O_RDONLY);
344 
345 			if (buf[i] < 0)
346 				_exit(EX_OSERR);
347 		}
348 
349 		errno = 0;
350 		fd = dup(buf[0]);
351 
352 		if (fd != -1 || errno != EMFILE)
353 			_exit(EX_DATAERR);
354 
355 		_exit(EXIT_SUCCESS);
356 	}
357 
358 	(void)wait(&sta);
359 
360 	if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS) {
361 
362 		if (WEXITSTATUS(sta) == EX_OSERR)
363 			atf_tc_fail("system call error");
364 
365 		if (WEXITSTATUS(sta) == EX_DATAERR)
366 			atf_tc_fail("dup(2) dupped more than RLIMIT_NOFILE");
367 
368 		atf_tc_fail("unknown error");
369 	}
370 
371 	(void)unlink(path);
372 }
373 
374 ATF_TC_CLEANUP(dup_max, tc)
375 {
376 	(void)unlink(path);
377 }
378 
379 ATF_TC_WITH_CLEANUP(dup_mode);
380 ATF_TC_HEAD(dup_mode, tc)
381 {
382 	atf_tc_set_md_var(tc, "descr", "A basic test of dup(2)");
383 }
384 
385 ATF_TC_BODY(dup_mode, tc)
386 {
387 	check_mode(true, false, false);
388 }
389 
390 ATF_TC_CLEANUP(dup_mode, tc)
391 {
392 	(void)unlink(path);
393 }
394 
395 ATF_TP_ADD_TCS(tp)
396 {
397 
398 	ATF_TP_ADD_TC(tp, dup2_basic);
399 	ATF_TP_ADD_TC(tp, dup2_err);
400 	ATF_TP_ADD_TC(tp, dup2_max);
401 	ATF_TP_ADD_TC(tp, dup2_mode);
402 	ATF_TP_ADD_TC(tp, dup3_err);
403 	ATF_TP_ADD_TC(tp, dup3_max);
404 	ATF_TP_ADD_TC(tp, dup3_mode);
405 	ATF_TP_ADD_TC(tp, dup_err);
406 	ATF_TP_ADD_TC(tp, dup_max);
407 	ATF_TP_ADD_TC(tp, dup_mode);
408 
409 	return atf_no_error();
410 }
411