1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */ 28 /* All Rights Reserved */ 29 30 /* 31 * Portions of this source code were derived from Berkeley 4.3 BSD 32 * under license from the Regents of the University of California. 33 */ 34 35 #pragma ident "%Z%%M% %I% %E% SMI" 36 37 /* 38 * Get file attribute information through a file name or a file descriptor. 39 */ 40 41 #include <sys/param.h> 42 #include <sys/isa_defs.h> 43 #include <sys/types.h> 44 #include <sys/sysmacros.h> 45 #include <sys/cred.h> 46 #include <sys/systm.h> 47 #include <sys/errno.h> 48 #include <sys/fcntl.h> 49 #include <sys/pathname.h> 50 #include <sys/stat.h> 51 #include <sys/vfs.h> 52 #include <sys/vnode.h> 53 #include <sys/mode.h> 54 #include <sys/file.h> 55 #include <sys/proc.h> 56 #include <sys/uio.h> 57 #include <sys/ioreq.h> 58 #include <sys/debug.h> 59 #include <sys/cmn_err.h> 60 #include <c2/audit.h> 61 62 /* 63 * Get the vp to be stated and the cred to be used for the call 64 * to VOP_GETATTR 65 */ 66 67 /* 68 * nmflag has the following values 69 * 70 * 1 - Always do lookup. i.e. stat, lstat. 71 * 2 - Name is optional i.e. fstatat 72 * 0 - Don't lookup name, vp is in file_p. i.e. fstat 73 * 74 */ 75 static int 76 cstatat_getvp(int fd, char *name, int nmflag, 77 int follow, vnode_t **vp, cred_t **cred) 78 { 79 vnode_t *startvp; 80 file_t *fp; 81 int error; 82 cred_t *cr; 83 84 *vp = NULL; 85 86 /* 87 * Only return EFAULT for fstatat when fd == AT_FDCWD && name == NULL 88 */ 89 90 if (fd == AT_FDCWD) { 91 if (name != NULL || nmflag != 2) { 92 startvp = NULL; 93 cr = CRED(); 94 crhold(cr); 95 } else 96 return (EFAULT); 97 } else { 98 char startchar; 99 100 if (nmflag == 1 || (nmflag == 2 && name != NULL)) { 101 if (copyin(name, &startchar, sizeof (char))) 102 return (EFAULT); 103 } else { 104 startchar = '\0'; 105 } 106 if (startchar != '/' || nmflag == 0) { 107 if ((fp = getf(fd)) == NULL) { 108 return (EBADF); 109 } 110 startvp = fp->f_vnode; 111 cr = fp->f_cred; 112 crhold(cr); 113 VN_HOLD(startvp); 114 releasef(fd); 115 } else { 116 startvp = NULL; 117 cr = CRED(); 118 crhold(cr); 119 } 120 } 121 *cred = cr; 122 123 #ifdef C2_AUDIT 124 if (audit_active) 125 audit_setfsat_path(1); 126 #endif /* C2_AUDIT */ 127 128 129 if (nmflag == 1 || (nmflag == 2 && name != NULL)) { 130 lookup: 131 if (error = lookupnameat(name, UIO_USERSPACE, follow, NULLVPP, 132 vp, startvp)) { 133 if (error == ESTALE) 134 goto lookup; 135 if (startvp != NULL) 136 VN_RELE(startvp); 137 crfree(cr); 138 return (error); 139 } 140 if (startvp != NULL) 141 VN_RELE(startvp); 142 } else { 143 *vp = startvp; 144 } 145 146 return (0); 147 } 148 149 /* 150 * Native syscall interfaces: 151 * 152 * N-bit kernel, N-bit applications, N-bit file offsets 153 */ 154 155 static int cstatat(int, char *, int, struct stat *, int, int); 156 static int cstat(vnode_t *vp, struct stat *, int, cred_t *); 157 158 int 159 stat(char *fname, struct stat *sb) 160 { 161 return (cstatat(AT_FDCWD, fname, 1, sb, 0, ATTR_REAL)); 162 } 163 164 int 165 lstat(char *fname, struct stat *sb) 166 { 167 return (cstatat(AT_FDCWD, fname, 1, sb, AT_SYMLINK_NOFOLLOW, 0)); 168 } 169 170 /* 171 * fstat can and should be fast, do an inline implementation here. 172 */ 173 #define FSTAT_BODY(fd, sb, statfn) \ 174 { \ 175 file_t *fp; \ 176 int error; \ 177 \ 178 if ((fp = getf(fd)) == NULL) \ 179 return (set_errno(EBADF)); \ 180 if (audit_active) \ 181 audit_setfsat_path(1); \ 182 error = statfn(fp->f_vnode, sb, 0, fp->f_cred); \ 183 releasef(fd); \ 184 if (error) \ 185 return (set_errno(error)); \ 186 return (0); \ 187 } 188 189 int 190 fstat(int fd, struct stat *sb) 191 { 192 FSTAT_BODY(fd, sb, cstat) 193 } 194 195 int 196 fstatat(int fd, char *name, struct stat *sb, int flags) 197 { 198 return (cstatat(fd, name, 2, sb, flags, 0)); 199 } 200 201 #if defined(__i386) || defined(__i386_COMPAT) 202 203 /* 204 * Handle all the "extended" stat operations in the same way; 205 * validate the version, then call the real handler. 206 */ 207 208 #define XSTAT_BODY(ver, f, s, fn) \ 209 return (ver != _STAT_VER ? set_errno(EINVAL) : fn(f, s)); 210 211 #endif /* __i386 || __i386_COMPAT */ 212 213 #if defined(__i386) 214 215 /* 216 * Syscalls for i386 applications that issue {,l,f}xstat() directly 217 */ 218 int 219 xstat(int version, char *fname, struct stat *sb) 220 { 221 XSTAT_BODY(version, fname, sb, stat) 222 } 223 224 int 225 lxstat(int version, char *fname, struct stat *sb) 226 { 227 XSTAT_BODY(version, fname, sb, lstat) 228 } 229 230 int 231 fxstat(int version, int fd, struct stat *sb) 232 { 233 XSTAT_BODY(version, fd, sb, fstat) 234 } 235 236 #endif /* __i386 */ 237 238 /* 239 * Common code for stat(), lstat(), and fstat(). 240 * (32-bit kernel, 32-bit applications, 32-bit files) 241 * (64-bit kernel, 64-bit applications, 64-bit files) 242 */ 243 static int 244 cstat(vnode_t *vp, struct stat *ubp, int flag, cred_t *cr) 245 { 246 struct vfssw *vswp; 247 struct stat sb; 248 vattr_t vattr; 249 int error; 250 251 vattr.va_mask = AT_STAT | AT_NBLOCKS | AT_BLKSIZE | AT_SIZE; 252 if ((error = VOP_GETATTR(vp, &vattr, flag, cr)) != 0) 253 return (error); 254 #ifdef _ILP32 255 /* 256 * (32-bit kernel, 32-bit applications, 32-bit files) 257 * NOTE: 32-bit kernel maintains a 64-bit unsigend va_size. 258 * 259 * st_size of devices (VBLK and VCHR special files) is a special case. 260 * POSIX does not define size behavior for special files, so the 261 * following Solaris specific behavior is not a violation. Solaris 262 * returns the size of the device. 263 * 264 * For compatibility with 32-bit programs which happen to do stat() on 265 * a (mknod) bigger than 2GB we suppress the large file EOVERFLOW and 266 * instead we return the value MAXOFF32_T (LONG_MAX). 267 * 268 * 32-bit applications that care about the size of devices should be 269 * built 64-bit or use a large file interface (lfcompile(5) or lf64(5)). 270 */ 271 if ((vattr.va_size > MAXOFF32_T) && 272 ((vp->v_type == VBLK) || (vp->v_type == VCHR))) { 273 /* OVERFLOW | UNKNOWN_SIZE */ 274 vattr.va_size = MAXOFF32_T; 275 } 276 #endif /* _ILP32 */ 277 if (vattr.va_size > MAXOFF_T || vattr.va_nblocks > LONG_MAX || 278 vattr.va_nodeid > ULONG_MAX) 279 return (EOVERFLOW); 280 281 bzero(&sb, sizeof (sb)); 282 sb.st_dev = vattr.va_fsid; 283 sb.st_ino = (ino_t)vattr.va_nodeid; 284 sb.st_mode = VTTOIF(vattr.va_type) | vattr.va_mode; 285 sb.st_nlink = vattr.va_nlink; 286 sb.st_uid = vattr.va_uid; 287 sb.st_gid = vattr.va_gid; 288 sb.st_rdev = vattr.va_rdev; 289 sb.st_size = (off_t)vattr.va_size; 290 sb.st_atim = vattr.va_atime; 291 sb.st_mtim = vattr.va_mtime; 292 sb.st_ctim = vattr.va_ctime; 293 sb.st_blksize = vattr.va_blksize; 294 sb.st_blocks = (blkcnt_t)vattr.va_nblocks; 295 if (vp->v_vfsp != NULL) { 296 vswp = &vfssw[vp->v_vfsp->vfs_fstype]; 297 if (vswp->vsw_name && *vswp->vsw_name) 298 (void) strcpy(sb.st_fstype, vswp->vsw_name); 299 } 300 if (copyout(&sb, ubp, sizeof (sb))) 301 return (EFAULT); 302 return (0); 303 } 304 305 static int 306 cstatat(int fd, char *name, int nmflag, struct stat *sb, int follow, int flags) 307 { 308 vnode_t *vp; 309 int error; 310 cred_t *cred; 311 int link_follow; 312 313 link_follow = (follow == AT_SYMLINK_NOFOLLOW) ? NO_FOLLOW : FOLLOW; 314 lookup: 315 if (error = cstatat_getvp(fd, name, nmflag, link_follow, &vp, &cred)) 316 return (set_errno(error)); 317 error = cstat(vp, sb, flags, cred); 318 crfree(cred); 319 VN_RELE(vp); 320 out: 321 if (error != 0) { 322 if (error == ESTALE && 323 (nmflag == 1 || (nmflag == 2 && name != NULL))) 324 goto lookup; 325 return (set_errno(error)); 326 } 327 return (0); 328 } 329 330 #if defined(_SYSCALL32_IMPL) 331 332 /* 333 * 64-bit kernel, 32-bit applications, 32-bit file offsets 334 */ 335 static int cstatat32(int, char *, int, struct stat32 *, int, int); 336 static int cstat32(vnode_t *, struct stat32 *, int, cred_t *); 337 338 int 339 stat32(char *fname, struct stat32 *sb) 340 { 341 return (cstatat32(AT_FDCWD, fname, 1, sb, 0, ATTR_REAL)); 342 } 343 344 int 345 lstat32(char *fname, struct stat32 *sb) 346 { 347 return (cstatat32(AT_FDCWD, fname, 1, sb, AT_SYMLINK_NOFOLLOW, 0)); 348 } 349 350 int 351 fstat32(int fd, struct stat32 *sb) 352 { 353 FSTAT_BODY(fd, sb, cstat32) 354 } 355 356 int 357 fstatat32(int fd, char *name, struct stat32 *sb, int flag) 358 { 359 return (cstatat32(fd, name, 2, sb, flag, 0)); 360 } 361 362 #if defined(__i386_COMPAT) 363 364 /* 365 * Syscalls for i386 applications that issue {,l,f}xstat() directly 366 */ 367 int 368 xstat32(int version, char *fname, struct stat32 *sb) 369 { 370 XSTAT_BODY(version, fname, sb, stat32) 371 } 372 373 int 374 lxstat32(int version, char *fname, struct stat32 *sb) 375 { 376 XSTAT_BODY(version, fname, sb, lstat32) 377 } 378 379 int 380 fxstat32(int version, int fd, struct stat32 *sb) 381 { 382 XSTAT_BODY(version, fd, sb, fstat32) 383 } 384 385 #endif /* __i386_COMPAT */ 386 387 static int 388 cstat32(vnode_t *vp, struct stat32 *ubp, int flag, struct cred *cr) 389 { 390 struct vfssw *vswp; 391 struct stat32 sb; 392 vattr_t vattr; 393 int error; 394 dev32_t st_dev, st_rdev; 395 396 vattr.va_mask = AT_STAT | AT_NBLOCKS | AT_BLKSIZE | AT_SIZE; 397 if (error = VOP_GETATTR(vp, &vattr, flag, cr)) 398 return (error); 399 400 /* devices are a special case, see comments in cstat */ 401 if ((vattr.va_size > MAXOFF32_T) && 402 ((vp->v_type == VBLK) || (vp->v_type == VCHR))) { 403 /* OVERFLOW | UNKNOWN_SIZE */ 404 vattr.va_size = MAXOFF32_T; 405 } 406 407 /* check for large values */ 408 if (!cmpldev(&st_dev, vattr.va_fsid) || 409 !cmpldev(&st_rdev, vattr.va_rdev) || 410 vattr.va_size > MAXOFF32_T || 411 vattr.va_nblocks > INT32_MAX || 412 vattr.va_nodeid > UINT32_MAX || 413 TIMESPEC_OVERFLOW(&(vattr.va_atime)) || 414 TIMESPEC_OVERFLOW(&(vattr.va_mtime)) || 415 TIMESPEC_OVERFLOW(&(vattr.va_ctime))) 416 return (EOVERFLOW); 417 418 bzero(&sb, sizeof (sb)); 419 sb.st_dev = st_dev; 420 sb.st_ino = (ino32_t)vattr.va_nodeid; 421 sb.st_mode = VTTOIF(vattr.va_type) | vattr.va_mode; 422 sb.st_nlink = vattr.va_nlink; 423 sb.st_uid = vattr.va_uid; 424 sb.st_gid = vattr.va_gid; 425 sb.st_rdev = st_rdev; 426 sb.st_size = (off32_t)vattr.va_size; 427 TIMESPEC_TO_TIMESPEC32(&(sb.st_atim), &(vattr.va_atime)); 428 TIMESPEC_TO_TIMESPEC32(&(sb.st_mtim), &(vattr.va_mtime)); 429 TIMESPEC_TO_TIMESPEC32(&(sb.st_ctim), &(vattr.va_ctime)); 430 sb.st_blksize = vattr.va_blksize; 431 sb.st_blocks = (blkcnt32_t)vattr.va_nblocks; 432 if (vp->v_vfsp != NULL) { 433 vswp = &vfssw[vp->v_vfsp->vfs_fstype]; 434 if (vswp->vsw_name && *vswp->vsw_name) 435 (void) strcpy(sb.st_fstype, vswp->vsw_name); 436 } 437 if (copyout(&sb, ubp, sizeof (sb))) 438 return (EFAULT); 439 return (0); 440 } 441 442 static int 443 cstatat32(int fd, char *name, int nmflag, struct stat32 *sb, 444 int follow, int flags) 445 { 446 vnode_t *vp; 447 int error; 448 cred_t *cred; 449 int link_follow; 450 451 link_follow = (follow == AT_SYMLINK_NOFOLLOW) ? NO_FOLLOW : FOLLOW; 452 lookup: 453 if (error = cstatat_getvp(fd, name, nmflag, link_follow, &vp, &cred)) 454 return (set_errno(error)); 455 error = cstat32(vp, sb, flags, cred); 456 crfree(cred); 457 VN_RELE(vp); 458 out: 459 if (error != 0) { 460 if (error == ESTALE && 461 (nmflag == 1 || (nmflag == 2 && name != NULL))) 462 goto lookup; 463 return (set_errno(error)); 464 } 465 return (0); 466 } 467 468 #endif /* _SYSCALL32_IMPL */ 469 470 #if defined(_ILP32) 471 472 /* 473 * 32-bit kernel, 32-bit applications, 64-bit file offsets. 474 * 475 * These routines are implemented differently on 64-bit kernels. 476 */ 477 static int cstatat64(int, char *, int, struct stat64 *, int, int); 478 static int cstat64(vnode_t *, struct stat64 *, int, cred_t *); 479 480 int 481 stat64(char *fname, struct stat64 *sb) 482 { 483 return (cstatat64(AT_FDCWD, fname, 1, sb, 0, ATTR_REAL)); 484 } 485 486 int 487 lstat64(char *fname, struct stat64 *sb) 488 { 489 return (cstatat64(AT_FDCWD, fname, 1, sb, AT_SYMLINK_NOFOLLOW, 0)); 490 } 491 492 int 493 fstat64(int fd, struct stat64 *sb) 494 { 495 FSTAT_BODY(fd, sb, cstat64) 496 } 497 498 int 499 fstatat64(int fd, char *name, struct stat64 *sb, int flags) 500 { 501 return (cstatat64(fd, name, 2, sb, flags, 0)); 502 } 503 504 static int 505 cstat64(vnode_t *vp, struct stat64 *ubp, int flag, cred_t *cr) 506 { 507 struct vfssw *vswp; 508 struct stat64 lsb; 509 vattr_t vattr; 510 int error; 511 512 vattr.va_mask = AT_STAT | AT_NBLOCKS | AT_BLKSIZE | AT_SIZE; 513 if (error = VOP_GETATTR(vp, &vattr, flag, cr)) 514 return (error); 515 516 bzero(&lsb, sizeof (lsb)); 517 lsb.st_dev = vattr.va_fsid; 518 lsb.st_ino = vattr.va_nodeid; 519 lsb.st_mode = VTTOIF(vattr.va_type) | vattr.va_mode; 520 lsb.st_nlink = vattr.va_nlink; 521 lsb.st_uid = vattr.va_uid; 522 lsb.st_gid = vattr.va_gid; 523 lsb.st_rdev = vattr.va_rdev; 524 lsb.st_size = vattr.va_size; 525 lsb.st_atim = vattr.va_atime; 526 lsb.st_mtim = vattr.va_mtime; 527 lsb.st_ctim = vattr.va_ctime; 528 lsb.st_blksize = vattr.va_blksize; 529 lsb.st_blocks = vattr.va_nblocks; 530 if (vp->v_vfsp != NULL) { 531 vswp = &vfssw[vp->v_vfsp->vfs_fstype]; 532 if (vswp->vsw_name && *vswp->vsw_name) 533 (void) strcpy(lsb.st_fstype, vswp->vsw_name); 534 } 535 if (copyout(&lsb, ubp, sizeof (lsb))) 536 return (EFAULT); 537 return (0); 538 } 539 540 static int 541 cstatat64(int fd, char *name, int nmflag, struct stat64 *sb, 542 int follow, int flags) 543 { 544 vnode_t *vp; 545 int error; 546 cred_t *cred; 547 int link_follow; 548 549 link_follow = (follow == AT_SYMLINK_NOFOLLOW) ? NO_FOLLOW : FOLLOW; 550 lookup: 551 if (error = cstatat_getvp(fd, name, nmflag, link_follow, &vp, &cred)) 552 return (set_errno(error)); 553 error = cstat64(vp, sb, flags, cred); 554 crfree(cred); 555 VN_RELE(vp); 556 out: 557 if (error != 0) { 558 if (error == ESTALE && 559 (nmflag == 1 || (nmflag == 2 && name != NULL))) 560 goto lookup; 561 return (set_errno(error)); 562 } 563 return (0); 564 } 565 566 #endif /* _ILP32 */ 567 568 #if defined(_SYSCALL32_IMPL) 569 570 /* 571 * 64-bit kernel, 32-bit applications, 64-bit file offsets. 572 * 573 * We'd really like to call the "native" stat calls for these ones, 574 * but the problem is that the 64-bit ABI defines the 'stat64' structure 575 * differently from the way the 32-bit ABI defines it. 576 */ 577 578 static int cstatat64_32(int, char *, int, struct stat64_32 *, int, int); 579 static int cstat64_32(vnode_t *, struct stat64_32 *, int, cred_t *); 580 581 int 582 stat64_32(char *fname, struct stat64_32 *sb) 583 { 584 return (cstatat64_32(AT_FDCWD, fname, 1, sb, 0, ATTR_REAL)); 585 } 586 587 int 588 lstat64_32(char *fname, struct stat64_32 *sb) 589 { 590 return (cstatat64_32(AT_FDCWD, fname, 1, sb, AT_SYMLINK_NOFOLLOW, 0)); 591 } 592 593 int 594 fstat64_32(int fd, struct stat64_32 *sb) 595 { 596 FSTAT_BODY(fd, sb, cstat64_32) 597 } 598 599 int 600 fstatat64_32(int fd, char *name, struct stat64_32 *sb, int flag) 601 { 602 return (cstatat64_32(fd, name, 2, sb, flag, 0)); 603 } 604 605 static int 606 cstat64_32(vnode_t *vp, struct stat64_32 *ubp, int flag, cred_t *cr) 607 { 608 struct vfssw *vswp; 609 struct stat64_32 lsb; 610 vattr_t vattr; 611 int error; 612 dev32_t st_dev, st_rdev; 613 614 vattr.va_mask = AT_STAT | AT_NBLOCKS | AT_BLKSIZE | AT_SIZE; 615 if (error = VOP_GETATTR(vp, &vattr, flag, cr)) 616 return (error); 617 618 if (!cmpldev(&st_dev, vattr.va_fsid) || 619 !cmpldev(&st_rdev, vattr.va_rdev) || 620 TIMESPEC_OVERFLOW(&(vattr.va_atime)) || 621 TIMESPEC_OVERFLOW(&(vattr.va_mtime)) || 622 TIMESPEC_OVERFLOW(&(vattr.va_ctime))) 623 return (EOVERFLOW); 624 625 bzero(&lsb, sizeof (lsb)); 626 lsb.st_dev = st_dev; 627 lsb.st_ino = vattr.va_nodeid; 628 lsb.st_mode = VTTOIF(vattr.va_type) | vattr.va_mode; 629 lsb.st_nlink = vattr.va_nlink; 630 lsb.st_uid = vattr.va_uid; 631 lsb.st_gid = vattr.va_gid; 632 lsb.st_rdev = st_rdev; 633 lsb.st_size = vattr.va_size; 634 TIMESPEC_TO_TIMESPEC32(&(lsb.st_atim), &(vattr.va_atime)); 635 TIMESPEC_TO_TIMESPEC32(&(lsb.st_mtim), &(vattr.va_mtime)); 636 TIMESPEC_TO_TIMESPEC32(&(lsb.st_ctim), &(vattr.va_ctime)); 637 lsb.st_blksize = vattr.va_blksize; 638 lsb.st_blocks = vattr.va_nblocks; 639 if (vp->v_vfsp != NULL) { 640 vswp = &vfssw[vp->v_vfsp->vfs_fstype]; 641 if (vswp->vsw_name && *vswp->vsw_name) 642 (void) strcpy(lsb.st_fstype, vswp->vsw_name); 643 } 644 if (copyout(&lsb, ubp, sizeof (lsb))) 645 return (EFAULT); 646 return (0); 647 } 648 649 static int 650 cstatat64_32(int fd, char *name, int nmflag, struct stat64_32 *sb, 651 int follow, int flags) 652 { 653 vnode_t *vp; 654 int error; 655 cred_t *cred; 656 int link_follow; 657 658 link_follow = (follow == AT_SYMLINK_NOFOLLOW) ? NO_FOLLOW : FOLLOW; 659 lookup: 660 if (error = cstatat_getvp(fd, name, nmflag, link_follow, &vp, &cred)) 661 return (set_errno(error)); 662 error = cstat64_32(vp, sb, flags, cred); 663 crfree(cred); 664 VN_RELE(vp); 665 out: 666 if (error != 0) { 667 if (error == ESTALE && 668 (nmflag == 1 || (nmflag == 2 && name != NULL))) 669 goto lookup; 670 return (set_errno(error)); 671 } 672 return (0); 673 } 674 675 #endif /* _SYSCALL32_IMPL */ 676