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