17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*ae115bc7Smrj * Common Development and Distribution License (the "License"). 6*ae115bc7Smrj * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* 22*ae115bc7Smrj * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */ 277c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate /* 307c478bd9Sstevel@tonic-gate * Portions of this source code were derived from Berkeley 4.3 BSD 317c478bd9Sstevel@tonic-gate * under license from the Regents of the University of California. 327c478bd9Sstevel@tonic-gate */ 337c478bd9Sstevel@tonic-gate 347c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 357c478bd9Sstevel@tonic-gate 367c478bd9Sstevel@tonic-gate #include <sys/param.h> 377c478bd9Sstevel@tonic-gate #include <sys/isa_defs.h> 387c478bd9Sstevel@tonic-gate #include <sys/types.h> 397c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h> 407c478bd9Sstevel@tonic-gate #include <sys/user.h> 417c478bd9Sstevel@tonic-gate #include <sys/systm.h> 427c478bd9Sstevel@tonic-gate #include <sys/errno.h> 437c478bd9Sstevel@tonic-gate #include <sys/fcntl.h> 447c478bd9Sstevel@tonic-gate #include <sys/stat.h> 457c478bd9Sstevel@tonic-gate #include <sys/vnode.h> 467c478bd9Sstevel@tonic-gate #include <sys/vfs.h> 477c478bd9Sstevel@tonic-gate #include <sys/file.h> 487c478bd9Sstevel@tonic-gate #include <sys/mode.h> 497c478bd9Sstevel@tonic-gate #include <sys/uio.h> 507c478bd9Sstevel@tonic-gate #include <sys/debug.h> 517c478bd9Sstevel@tonic-gate #include <c2/audit.h> 527c478bd9Sstevel@tonic-gate 537c478bd9Sstevel@tonic-gate /* 547c478bd9Sstevel@tonic-gate * Common code for open()/openat() and creat(). Check permissions, allocate 557c478bd9Sstevel@tonic-gate * an open file structure, and call the device open routine (if any). 567c478bd9Sstevel@tonic-gate */ 577c478bd9Sstevel@tonic-gate 587c478bd9Sstevel@tonic-gate static int 597c478bd9Sstevel@tonic-gate copen(int startfd, char *fname, int filemode, int createmode) 607c478bd9Sstevel@tonic-gate { 617c478bd9Sstevel@tonic-gate struct pathname pn; 627c478bd9Sstevel@tonic-gate vnode_t *vp, *sdvp; 637c478bd9Sstevel@tonic-gate file_t *fp, *startfp; 647c478bd9Sstevel@tonic-gate enum vtype type; 657c478bd9Sstevel@tonic-gate int error; 667c478bd9Sstevel@tonic-gate int fd, dupfd; 677c478bd9Sstevel@tonic-gate vnode_t *startvp; 687c478bd9Sstevel@tonic-gate proc_t *p = curproc; 697c478bd9Sstevel@tonic-gate 707c478bd9Sstevel@tonic-gate if (startfd == AT_FDCWD) { 717c478bd9Sstevel@tonic-gate /* 727c478bd9Sstevel@tonic-gate * Regular open() 737c478bd9Sstevel@tonic-gate */ 747c478bd9Sstevel@tonic-gate startvp = NULL; 757c478bd9Sstevel@tonic-gate } else { 767c478bd9Sstevel@tonic-gate /* 777c478bd9Sstevel@tonic-gate * We're here via openat() 787c478bd9Sstevel@tonic-gate */ 797c478bd9Sstevel@tonic-gate char startchar; 807c478bd9Sstevel@tonic-gate 817c478bd9Sstevel@tonic-gate if (copyin(fname, &startchar, sizeof (char))) 827c478bd9Sstevel@tonic-gate return (set_errno(EFAULT)); 837c478bd9Sstevel@tonic-gate 847c478bd9Sstevel@tonic-gate /* 857c478bd9Sstevel@tonic-gate * if startchar is / then startfd is ignored 867c478bd9Sstevel@tonic-gate */ 877c478bd9Sstevel@tonic-gate if (startchar == '/') 887c478bd9Sstevel@tonic-gate startvp = NULL; 897c478bd9Sstevel@tonic-gate else { 907c478bd9Sstevel@tonic-gate if ((startfp = getf(startfd)) == NULL) 917c478bd9Sstevel@tonic-gate return (set_errno(EBADF)); 927c478bd9Sstevel@tonic-gate startvp = startfp->f_vnode; 937c478bd9Sstevel@tonic-gate VN_HOLD(startvp); 947c478bd9Sstevel@tonic-gate releasef(startfd); 957c478bd9Sstevel@tonic-gate } 967c478bd9Sstevel@tonic-gate } 977c478bd9Sstevel@tonic-gate 987c478bd9Sstevel@tonic-gate if (filemode & FXATTR) { 997c478bd9Sstevel@tonic-gate 1007c478bd9Sstevel@tonic-gate /* 1017c478bd9Sstevel@tonic-gate * Make sure we have a valid request. 1027c478bd9Sstevel@tonic-gate * We must either have a real fd or AT_FDCWD 1037c478bd9Sstevel@tonic-gate */ 1047c478bd9Sstevel@tonic-gate 1057c478bd9Sstevel@tonic-gate if (startfd != AT_FDCWD && startvp == NULL) { 1067c478bd9Sstevel@tonic-gate error = EINVAL; 1077c478bd9Sstevel@tonic-gate goto out; 1087c478bd9Sstevel@tonic-gate } 1097c478bd9Sstevel@tonic-gate 1107c478bd9Sstevel@tonic-gate if (error = pn_get(fname, UIO_USERSPACE, &pn)) { 1117c478bd9Sstevel@tonic-gate goto out; 1127c478bd9Sstevel@tonic-gate } 1137c478bd9Sstevel@tonic-gate 1147c478bd9Sstevel@tonic-gate if (startfd == AT_FDCWD) { 1157c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock); 1167c478bd9Sstevel@tonic-gate startvp = PTOU(p)->u_cdir; 1177c478bd9Sstevel@tonic-gate VN_HOLD(startvp); 1187c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock); 1197c478bd9Sstevel@tonic-gate } 1207c478bd9Sstevel@tonic-gate 1217c478bd9Sstevel@tonic-gate /* 1227c478bd9Sstevel@tonic-gate * Verify permission to put attributes on file 1237c478bd9Sstevel@tonic-gate */ 1247c478bd9Sstevel@tonic-gate 1257c478bd9Sstevel@tonic-gate if ((VOP_ACCESS(startvp, VREAD, 0, CRED()) != 0) && 1267c478bd9Sstevel@tonic-gate (VOP_ACCESS(startvp, VWRITE, 0, CRED()) != 0) && 1277c478bd9Sstevel@tonic-gate (VOP_ACCESS(startvp, VEXEC, 0, CRED()) != 0)) { 1287c478bd9Sstevel@tonic-gate error = EACCES; 1297c478bd9Sstevel@tonic-gate pn_free(&pn); 1307c478bd9Sstevel@tonic-gate goto out; 1317c478bd9Sstevel@tonic-gate } 1327c478bd9Sstevel@tonic-gate 1337c478bd9Sstevel@tonic-gate if ((startvp->v_vfsp->vfs_flag & VFS_XATTR) != 0) { 1347c478bd9Sstevel@tonic-gate error = VOP_LOOKUP(startvp, "", &sdvp, &pn, 1357c478bd9Sstevel@tonic-gate LOOKUP_XATTR|CREATE_XATTR_DIR, rootvp, CRED()); 1367c478bd9Sstevel@tonic-gate } else { 1377c478bd9Sstevel@tonic-gate error = EINVAL; 1387c478bd9Sstevel@tonic-gate } 1397c478bd9Sstevel@tonic-gate pn_free(&pn); 1407c478bd9Sstevel@tonic-gate if (error != 0) 1417c478bd9Sstevel@tonic-gate goto out; 1427c478bd9Sstevel@tonic-gate 1437c478bd9Sstevel@tonic-gate VN_RELE(startvp); 1447c478bd9Sstevel@tonic-gate startvp = sdvp; 1457c478bd9Sstevel@tonic-gate } 1467c478bd9Sstevel@tonic-gate 1477c478bd9Sstevel@tonic-gate if ((filemode & (FREAD|FWRITE)) != 0) { 1487c478bd9Sstevel@tonic-gate if ((filemode & (FNONBLOCK|FNDELAY)) == (FNONBLOCK|FNDELAY)) 1497c478bd9Sstevel@tonic-gate filemode &= ~FNDELAY; 1507c478bd9Sstevel@tonic-gate error = falloc((vnode_t *)NULL, filemode, &fp, &fd); 1517c478bd9Sstevel@tonic-gate if (error == 0) { 1527c478bd9Sstevel@tonic-gate #ifdef C2_AUDIT 1537c478bd9Sstevel@tonic-gate if (audit_active) 1547c478bd9Sstevel@tonic-gate audit_setfsat_path(1); 1557c478bd9Sstevel@tonic-gate #endif /* C2_AUDIT */ 1567c478bd9Sstevel@tonic-gate /* 1577c478bd9Sstevel@tonic-gate * Last arg is a don't-care term if 1587c478bd9Sstevel@tonic-gate * !(filemode & FCREAT). 1597c478bd9Sstevel@tonic-gate */ 1607c478bd9Sstevel@tonic-gate error = vn_openat(fname, UIO_USERSPACE, filemode, 1617c478bd9Sstevel@tonic-gate (int)(createmode & MODEMASK), &vp, CRCREAT, 162*ae115bc7Smrj PTOU(curproc)->u_cmask, startvp); 1637c478bd9Sstevel@tonic-gate 1647c478bd9Sstevel@tonic-gate if (startvp != NULL) 1657c478bd9Sstevel@tonic-gate VN_RELE(startvp); 1667c478bd9Sstevel@tonic-gate if (error == 0) { 1677c478bd9Sstevel@tonic-gate #ifdef C2_AUDIT 1687c478bd9Sstevel@tonic-gate if (audit_active) 1697c478bd9Sstevel@tonic-gate audit_copen(fd, fp, vp); 1707c478bd9Sstevel@tonic-gate #endif /* C2_AUDIT */ 1717c478bd9Sstevel@tonic-gate if ((vp->v_flag & VDUP) == 0) { 1727c478bd9Sstevel@tonic-gate fp->f_vnode = vp; 1737c478bd9Sstevel@tonic-gate mutex_exit(&fp->f_tlock); 1747c478bd9Sstevel@tonic-gate /* 1757c478bd9Sstevel@tonic-gate * We must now fill in the slot 1767c478bd9Sstevel@tonic-gate * falloc reserved. 1777c478bd9Sstevel@tonic-gate */ 1787c478bd9Sstevel@tonic-gate setf(fd, fp); 1797c478bd9Sstevel@tonic-gate return (fd); 1807c478bd9Sstevel@tonic-gate } else { 1817c478bd9Sstevel@tonic-gate /* 1827c478bd9Sstevel@tonic-gate * Special handling for /dev/fd. 1837c478bd9Sstevel@tonic-gate * Give up the file pointer 1847c478bd9Sstevel@tonic-gate * and dup the indicated file descriptor 1857c478bd9Sstevel@tonic-gate * (in v_rdev). This is ugly, but I've 1867c478bd9Sstevel@tonic-gate * seen worse. 1877c478bd9Sstevel@tonic-gate */ 1887c478bd9Sstevel@tonic-gate unfalloc(fp); 1897c478bd9Sstevel@tonic-gate dupfd = getminor(vp->v_rdev); 1907c478bd9Sstevel@tonic-gate type = vp->v_type; 1917c478bd9Sstevel@tonic-gate mutex_enter(&vp->v_lock); 1927c478bd9Sstevel@tonic-gate vp->v_flag &= ~VDUP; 1937c478bd9Sstevel@tonic-gate mutex_exit(&vp->v_lock); 1947c478bd9Sstevel@tonic-gate VN_RELE(vp); 1957c478bd9Sstevel@tonic-gate if (type != VCHR) 1967c478bd9Sstevel@tonic-gate return (set_errno(EINVAL)); 1977c478bd9Sstevel@tonic-gate if ((fp = getf(dupfd)) == NULL) { 1987c478bd9Sstevel@tonic-gate setf(fd, NULL); 1997c478bd9Sstevel@tonic-gate return (set_errno(EBADF)); 2007c478bd9Sstevel@tonic-gate } 2017c478bd9Sstevel@tonic-gate mutex_enter(&fp->f_tlock); 2027c478bd9Sstevel@tonic-gate fp->f_count++; 2037c478bd9Sstevel@tonic-gate mutex_exit(&fp->f_tlock); 2047c478bd9Sstevel@tonic-gate setf(fd, fp); 2057c478bd9Sstevel@tonic-gate releasef(dupfd); 2067c478bd9Sstevel@tonic-gate } 2077c478bd9Sstevel@tonic-gate return (fd); 2087c478bd9Sstevel@tonic-gate } else { 2097c478bd9Sstevel@tonic-gate setf(fd, NULL); 2107c478bd9Sstevel@tonic-gate unfalloc(fp); 2117c478bd9Sstevel@tonic-gate return (set_errno(error)); 2127c478bd9Sstevel@tonic-gate } 2137c478bd9Sstevel@tonic-gate } 2147c478bd9Sstevel@tonic-gate } else { 2157c478bd9Sstevel@tonic-gate error = EINVAL; 2167c478bd9Sstevel@tonic-gate } 2177c478bd9Sstevel@tonic-gate out: 2187c478bd9Sstevel@tonic-gate if (startvp != NULL) 2197c478bd9Sstevel@tonic-gate VN_RELE(startvp); 2207c478bd9Sstevel@tonic-gate return (set_errno(error)); 2217c478bd9Sstevel@tonic-gate } 2227c478bd9Sstevel@tonic-gate 2237c478bd9Sstevel@tonic-gate #define OPENMODE32(fmode) ((int)((fmode)-FOPEN)) 2247c478bd9Sstevel@tonic-gate #define CREATMODE32 (FWRITE|FCREAT|FTRUNC) 2257c478bd9Sstevel@tonic-gate #define OPENMODE64(fmode) (OPENMODE32(fmode) | FOFFMAX) 2267c478bd9Sstevel@tonic-gate #define CREATMODE64 (CREATMODE32 | FOFFMAX) 2277c478bd9Sstevel@tonic-gate #ifdef _LP64 2287c478bd9Sstevel@tonic-gate #define OPENMODE(fmode) OPENMODE64(fmode) 2297c478bd9Sstevel@tonic-gate #define CREATMODE CREATMODE64 2307c478bd9Sstevel@tonic-gate #else 2317c478bd9Sstevel@tonic-gate #define OPENMODE OPENMODE32 2327c478bd9Sstevel@tonic-gate #define CREATMODE CREATMODE32 2337c478bd9Sstevel@tonic-gate #endif 2347c478bd9Sstevel@tonic-gate 2357c478bd9Sstevel@tonic-gate /* 2367c478bd9Sstevel@tonic-gate * Open a file. 2377c478bd9Sstevel@tonic-gate */ 2387c478bd9Sstevel@tonic-gate int 2397c478bd9Sstevel@tonic-gate open(char *fname, int fmode, int cmode) 2407c478bd9Sstevel@tonic-gate { 2417c478bd9Sstevel@tonic-gate return (copen(AT_FDCWD, fname, OPENMODE(fmode), cmode)); 2427c478bd9Sstevel@tonic-gate } 2437c478bd9Sstevel@tonic-gate 2447c478bd9Sstevel@tonic-gate /* 2457c478bd9Sstevel@tonic-gate * Create a file. 2467c478bd9Sstevel@tonic-gate */ 2477c478bd9Sstevel@tonic-gate int 2487c478bd9Sstevel@tonic-gate creat(char *fname, int cmode) 2497c478bd9Sstevel@tonic-gate { 2507c478bd9Sstevel@tonic-gate return (copen(AT_FDCWD, fname, CREATMODE, cmode)); 2517c478bd9Sstevel@tonic-gate } 2527c478bd9Sstevel@tonic-gate 2537c478bd9Sstevel@tonic-gate int 2547c478bd9Sstevel@tonic-gate openat(int fd, char *path, int fmode, int cmode) 2557c478bd9Sstevel@tonic-gate { 2567c478bd9Sstevel@tonic-gate return (copen(fd, path, OPENMODE(fmode), cmode)); 2577c478bd9Sstevel@tonic-gate } 2587c478bd9Sstevel@tonic-gate 2597c478bd9Sstevel@tonic-gate #if defined(_ILP32) || defined(_SYSCALL32_IMPL) 2607c478bd9Sstevel@tonic-gate /* 2617c478bd9Sstevel@tonic-gate * Open and Creat for large files in 32-bit environment. Sets the FOFFMAX flag. 2627c478bd9Sstevel@tonic-gate */ 2637c478bd9Sstevel@tonic-gate int 2647c478bd9Sstevel@tonic-gate open64(char *fname, int fmode, int cmode) 2657c478bd9Sstevel@tonic-gate { 2667c478bd9Sstevel@tonic-gate return (copen(AT_FDCWD, fname, OPENMODE64(fmode), cmode)); 2677c478bd9Sstevel@tonic-gate } 2687c478bd9Sstevel@tonic-gate 2697c478bd9Sstevel@tonic-gate int 2707c478bd9Sstevel@tonic-gate creat64(char *fname, int cmode) 2717c478bd9Sstevel@tonic-gate { 2727c478bd9Sstevel@tonic-gate return (copen(AT_FDCWD, fname, CREATMODE64, cmode)); 2737c478bd9Sstevel@tonic-gate } 2747c478bd9Sstevel@tonic-gate 2757c478bd9Sstevel@tonic-gate int 2767c478bd9Sstevel@tonic-gate openat64(int fd, char *path, int fmode, int cmode) 2777c478bd9Sstevel@tonic-gate { 2787c478bd9Sstevel@tonic-gate return (copen(fd, path, OPENMODE64(fmode), cmode)); 2797c478bd9Sstevel@tonic-gate } 2807c478bd9Sstevel@tonic-gate 2817c478bd9Sstevel@tonic-gate #endif /* _ILP32 || _SYSCALL32_IMPL */ 2827c478bd9Sstevel@tonic-gate 2837c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL 2847c478bd9Sstevel@tonic-gate /* 2857c478bd9Sstevel@tonic-gate * Open and Creat for 32-bit compatibility on 64-bit kernel 2867c478bd9Sstevel@tonic-gate */ 2877c478bd9Sstevel@tonic-gate int 2887c478bd9Sstevel@tonic-gate open32(char *fname, int fmode, int cmode) 2897c478bd9Sstevel@tonic-gate { 2907c478bd9Sstevel@tonic-gate return (copen(AT_FDCWD, fname, OPENMODE32(fmode), cmode)); 2917c478bd9Sstevel@tonic-gate } 2927c478bd9Sstevel@tonic-gate 2937c478bd9Sstevel@tonic-gate int 2947c478bd9Sstevel@tonic-gate creat32(char *fname, int cmode) 2957c478bd9Sstevel@tonic-gate { 2967c478bd9Sstevel@tonic-gate return (copen(AT_FDCWD, fname, CREATMODE32, cmode)); 2977c478bd9Sstevel@tonic-gate } 2987c478bd9Sstevel@tonic-gate 2997c478bd9Sstevel@tonic-gate int 3007c478bd9Sstevel@tonic-gate openat32(int fd, char *path, int fmode, int cmode) 3017c478bd9Sstevel@tonic-gate { 3027c478bd9Sstevel@tonic-gate return (copen(fd, path, OPENMODE32(fmode), cmode)); 3037c478bd9Sstevel@tonic-gate } 3047c478bd9Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */ 305