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