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