1 /* $OpenBSD: t_stat.c,v 1.4 2021/12/13 16:56:48 deraadt Exp $ */ 2 /* $NetBSD: t_stat.c,v 1.6 2019/07/16 17:29:18 martin 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/stat.h> 36 #include <sys/socket.h> 37 #include <sys/types.h> 38 39 #include <arpa/inet.h> 40 #include <netinet/in.h> 41 42 #include "atf-c.h" 43 #include <errno.h> 44 #include <fcntl.h> 45 #include <fts.h> 46 #include <limits.h> 47 #include <string.h> 48 #include <unistd.h> 49 50 #include <stdio.h> 51 52 static const char *path = "stat"; 53 54 ATF_TC_WITH_CLEANUP(stat_chflags); 55 ATF_TC_HEAD(stat_chflags, tc) 56 { 57 atf_tc_set_md_var(tc, "descr", "Test chflags(2) with stat(2)"); 58 } 59 60 ATF_TC_BODY(stat_chflags, tc) 61 { 62 struct stat sa, sb; 63 int fd; 64 65 (void)memset(&sa, 0, sizeof(struct stat)); 66 (void)memset(&sb, 0, sizeof(struct stat)); 67 68 fd = open(path, O_RDONLY | O_CREAT, 0600); 69 70 ATF_REQUIRE(fd != -1); 71 ATF_REQUIRE(stat(path, &sa) == 0); 72 ATF_REQUIRE(chflags(path, UF_NODUMP) == 0); 73 ATF_REQUIRE(stat(path, &sb) == 0); 74 75 if (sa.st_flags == sb.st_flags) 76 atf_tc_fail("stat(2) did not detect chflags(2)"); 77 78 ATF_REQUIRE(close(fd) == 0); 79 ATF_REQUIRE(unlink(path) == 0); 80 } 81 82 ATF_TC_CLEANUP(stat_chflags, tc) 83 { 84 (void)unlink(path); 85 } 86 87 ATF_TC(stat_dir); 88 ATF_TC_HEAD(stat_dir, tc) 89 { 90 atf_tc_set_md_var(tc, "descr", "Test stat(2) with directories"); 91 } 92 93 ATF_TC_BODY(stat_dir, tc) 94 { 95 const short depth = 2; 96 struct stat sa, sb; 97 char *argv[2]; 98 FTSENT *ftse; 99 FTS *fts; 100 int ops; 101 102 argv[1] = NULL; 103 argv[0] = __UNCONST("/"); 104 105 ops = FTS_NOCHDIR; 106 ops |= FTS_PHYSICAL; 107 108 fts = fts_open(argv, ops, NULL); 109 ATF_REQUIRE(fts != NULL); 110 111 while ((ftse = fts_read(fts)) != NULL) { 112 113 if (ftse->fts_level < 1) 114 continue; 115 116 if (ftse->fts_level > depth) { 117 (void)fts_set(fts, ftse, FTS_SKIP); 118 continue; 119 } 120 121 switch(ftse->fts_info) { 122 123 case FTS_DP: 124 125 (void)memset(&sa, 0, sizeof(struct stat)); 126 (void)memset(&sb, 0, sizeof(struct stat)); 127 128 ATF_REQUIRE(stat(ftse->fts_parent->fts_path,&sa) == 0); 129 ATF_REQUIRE(chdir(ftse->fts_path) == 0); 130 ATF_REQUIRE(stat(".", &sb) == 0); 131 132 /* 133 * The previous two stat(2) calls 134 * should be for the same directory. 135 */ 136 if (sa.st_dev != sb.st_dev || sa.st_ino != sb.st_ino) 137 atf_tc_fail("inconsistent stat(2)"); 138 139 /* 140 * Check that fts(3)'s stat(2) 141 * call equals the manual one. 142 */ 143 if (sb.st_ino != ftse->fts_statp->st_ino) 144 atf_tc_fail("stat(2) and fts(3) differ"); 145 146 break; 147 148 default: 149 break; 150 } 151 } 152 153 (void)fts_close(fts); 154 } 155 156 ATF_TC(stat_err); 157 ATF_TC_HEAD(stat_err, tc) 158 { 159 atf_tc_set_md_var(tc, "descr", "Test errors from the stat(2) family"); 160 } 161 162 ATF_TC_BODY(stat_err, tc) 163 { 164 char buf[NAME_MAX + 1]; 165 struct stat st; 166 167 (void)memset(buf, 'x', sizeof(buf)); 168 169 errno = 0; 170 ATF_REQUIRE_ERRNO(EBADF, fstat(-1, &st) == -1); 171 172 errno = 0; 173 ATF_REQUIRE_ERRNO(ENAMETOOLONG, stat(buf, &st) == -1); 174 175 errno = 0; 176 ATF_REQUIRE_ERRNO(ENAMETOOLONG, lstat(buf, &st) == -1); 177 178 errno = 0; 179 ATF_REQUIRE_ERRNO(EFAULT, stat((void *)-1, &st) == -1); 180 181 errno = 0; 182 ATF_REQUIRE_ERRNO(EFAULT, lstat((void *)-1, &st) == -1); 183 184 errno = 0; 185 ATF_REQUIRE_ERRNO(EFAULT, stat("/etc/passwd", (void *)-1) == -1); 186 187 errno = 0; 188 ATF_REQUIRE_ERRNO(EFAULT, lstat("/etc/passwd", (void *)-1) == -1); 189 190 errno = 0; 191 ATF_REQUIRE_ERRNO(ENOENT, stat("/a/b/c/d/e/f/g/h/i/j/k", &st) == -1); 192 193 errno = 0; 194 ATF_REQUIRE_ERRNO(ENOENT, lstat("/a/b/c/d/e/f/g/h/i/j/k", &st) == -1); 195 } 196 197 ATF_TC_WITH_CLEANUP(stat_mtime); 198 ATF_TC_HEAD(stat_mtime, tc) 199 { 200 atf_tc_set_md_var(tc, "descr", "Test modification times with stat(2)"); 201 } 202 203 ATF_TC_BODY(stat_mtime, tc) 204 { 205 struct stat sa, sb; 206 int fd[3]; 207 size_t i; 208 209 for (i = 0; i < __arraycount(fd); i++) { 210 211 (void)memset(&sa, 0, sizeof(struct stat)); 212 (void)memset(&sb, 0, sizeof(struct stat)); 213 214 fd[i] = open(path, O_WRONLY | O_CREAT, 0600); 215 216 ATF_REQUIRE(fd[i] != -1); 217 ATF_REQUIRE(write(fd[i], "X", 1) == 1); 218 ATF_REQUIRE(stat(path, &sa) == 0); 219 220 (void)sleep(1); 221 222 ATF_REQUIRE(write(fd[i], "X", 1) == 1); 223 ATF_REQUIRE(stat(path, &sb) == 0); 224 225 ATF_REQUIRE(close(fd[i]) == 0); 226 ATF_REQUIRE(unlink(path) == 0); 227 228 if (sa.st_mtime == sb.st_mtime) 229 atf_tc_fail("mtimes did not change"); 230 } 231 } 232 233 ATF_TC_CLEANUP(stat_mtime, tc) 234 { 235 (void)unlink(path); 236 } 237 238 ATF_TC_WITH_CLEANUP(stat_perm); 239 ATF_TC_HEAD(stat_perm, tc) 240 { 241 atf_tc_set_md_var(tc, "descr", "Test permissions with stat(2)"); 242 atf_tc_set_md_var(tc, "require.user", "root"); 243 } 244 245 ATF_TC_BODY(stat_perm, tc) 246 { 247 struct stat sa, sb; 248 gid_t gid; 249 uid_t uid; 250 int fd; 251 252 (void)memset(&sa, 0, sizeof(struct stat)); 253 (void)memset(&sb, 0, sizeof(struct stat)); 254 255 uid = getuid(); 256 gid = getgid(); 257 258 fd = open(path, O_RDONLY | O_CREAT, 0600); 259 260 ATF_REQUIRE(fd != -1); 261 ATF_REQUIRE(fstat(fd, &sa) == 0); 262 ATF_REQUIRE(stat(path, &sb) == 0); 263 264 if (gid != sa.st_gid || sa.st_gid != sb.st_gid) 265 atf_tc_fail("invalid GID"); 266 267 if (uid != sa.st_uid || sa.st_uid != sb.st_uid) 268 atf_tc_fail("invalid UID"); 269 270 ATF_REQUIRE(close(fd) == 0); 271 ATF_REQUIRE(unlink(path) == 0); 272 } 273 274 ATF_TC_CLEANUP(stat_perm, tc) 275 { 276 (void)unlink(path); 277 } 278 279 ATF_TC_WITH_CLEANUP(stat_size); 280 ATF_TC_HEAD(stat_size, tc) 281 { 282 atf_tc_set_md_var(tc, "descr", "Test file sizes with stat(2)"); 283 } 284 285 ATF_TC_BODY(stat_size, tc) 286 { 287 struct stat sa, sb, sc; 288 const size_t n = 10; 289 size_t i; 290 int fd; 291 292 fd = open(path, O_WRONLY | O_CREAT, 0600); 293 ATF_REQUIRE(fd >= 0); 294 295 for (i = 0; i < n; i++) { 296 297 (void)memset(&sa, 0, sizeof(struct stat)); 298 (void)memset(&sb, 0, sizeof(struct stat)); 299 (void)memset(&sc, 0, sizeof(struct stat)); 300 301 ATF_REQUIRE(fstat(fd, &sa) == 0); 302 ATF_REQUIRE(write(fd, "X", 1) == 1); 303 ATF_REQUIRE(fstat(fd, &sb) == 0); 304 ATF_REQUIRE(stat(path, &sc) == 0); 305 306 if (sa.st_size + 1 != sb.st_size) 307 atf_tc_fail("invalid file size"); 308 309 if (sb.st_size != sc.st_size) 310 atf_tc_fail("stat(2) and fstat(2) mismatch"); 311 } 312 313 ATF_REQUIRE(close(fd) == 0); 314 ATF_REQUIRE(unlink(path) == 0); 315 } 316 317 ATF_TC_CLEANUP(stat_size, tc) 318 { 319 (void)unlink(path); 320 } 321 322 ATF_TC(stat_socket); 323 ATF_TC_HEAD(stat_socket, tc) 324 { 325 atf_tc_set_md_var(tc, "descr", "Test fstat(2) with " 326 "a socket (PR kern/46077)"); 327 } 328 329 ATF_TC_BODY(stat_socket, tc) 330 { 331 struct sockaddr_in addr; 332 struct stat st; 333 uint32_t iaddr; 334 int fd, flags; 335 336 (void)memset(&st, 0, sizeof(struct stat)); 337 (void)memset(&addr, 0, sizeof(struct sockaddr_in)); 338 339 fd = socket(AF_INET, SOCK_STREAM, 0); 340 ATF_REQUIRE(fd >= 0); 341 342 flags = fcntl(fd, F_GETFL); 343 344 ATF_REQUIRE(flags != -1); 345 ATF_REQUIRE(fcntl(fd, F_SETFL, flags | O_NONBLOCK) != -1); 346 ATF_REQUIRE(inet_pton(AF_INET, "127.0.0.1", &iaddr) == 1); 347 348 addr.sin_port = htons(42); 349 addr.sin_family = AF_INET; 350 addr.sin_addr.s_addr = iaddr; 351 352 errno = 0; 353 354 ATF_REQUIRE_ERRNO(EINPROGRESS, 355 connect(fd, (struct sockaddr *)&addr, 356 sizeof(struct sockaddr_in)) == -1); 357 358 errno = 0; 359 360 if (fstat(fd, &st) != 0 || errno != 0) 361 atf_tc_fail("fstat(2) failed for a EINPROGRESS socket"); 362 363 (void)close(fd); 364 } 365 366 ATF_TC_WITH_CLEANUP(stat_symlink); 367 ATF_TC_HEAD(stat_symlink, tc) 368 { 369 atf_tc_set_md_var(tc, "descr", "Test symbolic links with stat(2)"); 370 } 371 372 ATF_TC_BODY(stat_symlink, tc) 373 { 374 const char *pathlink = "pathlink"; 375 struct stat sa, sb; 376 int fd; 377 378 (void)memset(&sa, 0, sizeof(struct stat)); 379 (void)memset(&sb, 0, sizeof(struct stat)); 380 381 fd = open(path, O_WRONLY | O_CREAT, 0600); 382 383 ATF_REQUIRE(fd >= 0); 384 ATF_REQUIRE(symlink(path, pathlink) == 0); 385 ATF_REQUIRE(stat(pathlink, &sa) == 0); 386 ATF_REQUIRE(lstat(pathlink, &sb) == 0); 387 388 if (S_ISLNK(sa.st_mode) != 0) 389 atf_tc_fail("stat(2) detected symbolic link"); 390 391 if (S_ISLNK(sb.st_mode) == 0) 392 atf_tc_fail("lstat(2) did not detect symbolic link"); 393 394 if (sa.st_mode == sb.st_mode) 395 atf_tc_fail("inconsistencies between stat(2) and lstat(2)"); 396 397 (void)close(fd); 398 ATF_REQUIRE(unlink(path) == 0); 399 ATF_REQUIRE(unlink(pathlink) == 0); 400 } 401 402 ATF_TC_CLEANUP(stat_symlink, tc) 403 { 404 (void)unlink(path); 405 } 406 407 ATF_TP_ADD_TCS(tp) 408 { 409 410 ATF_TP_ADD_TC(tp, stat_chflags); 411 ATF_TP_ADD_TC(tp, stat_dir); 412 ATF_TP_ADD_TC(tp, stat_err); 413 ATF_TP_ADD_TC(tp, stat_mtime); 414 ATF_TP_ADD_TC(tp, stat_perm); 415 ATF_TP_ADD_TC(tp, stat_size); 416 ATF_TP_ADD_TC(tp, stat_socket); 417 ATF_TP_ADD_TC(tp, stat_symlink); 418 419 return atf_no_error(); 420 } 421