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 5ae115bc7Smrj * Common Development and Distribution License (the "License"). 6ae115bc7Smrj * 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 */ 21*8fd04b83SRoger A. Faulkner 227c478bd9Sstevel@tonic-gate /* 23*8fd04b83SRoger A. Faulkner * Copyright 2010 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */ 287c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 297c478bd9Sstevel@tonic-gate 307c478bd9Sstevel@tonic-gate /* 317c478bd9Sstevel@tonic-gate * Portions of this source code were derived from Berkeley 4.3 BSD 327c478bd9Sstevel@tonic-gate * under license from the Regents of the University of California. 337c478bd9Sstevel@tonic-gate */ 347c478bd9Sstevel@tonic-gate 357c478bd9Sstevel@tonic-gate #include <sys/param.h> 367c478bd9Sstevel@tonic-gate #include <sys/isa_defs.h> 377c478bd9Sstevel@tonic-gate #include <sys/types.h> 387c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h> 397c478bd9Sstevel@tonic-gate #include <sys/user.h> 407c478bd9Sstevel@tonic-gate #include <sys/systm.h> 417c478bd9Sstevel@tonic-gate #include <sys/errno.h> 427c478bd9Sstevel@tonic-gate #include <sys/fcntl.h> 437c478bd9Sstevel@tonic-gate #include <sys/stat.h> 447c478bd9Sstevel@tonic-gate #include <sys/vnode.h> 457c478bd9Sstevel@tonic-gate #include <sys/vfs.h> 467c478bd9Sstevel@tonic-gate #include <sys/file.h> 477c478bd9Sstevel@tonic-gate #include <sys/mode.h> 487c478bd9Sstevel@tonic-gate #include <sys/uio.h> 497c478bd9Sstevel@tonic-gate #include <sys/debug.h> 507c478bd9Sstevel@tonic-gate #include <c2/audit.h> 51da6c28aaSamw #include <sys/cmn_err.h> 527c478bd9Sstevel@tonic-gate 537c478bd9Sstevel@tonic-gate /* 54*8fd04b83SRoger A. Faulkner * Common code for openat(). Check permissions, allocate an open 55*8fd04b83SRoger A. Faulkner * 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; 69da6c28aaSamw uio_seg_t seg = UIO_USERSPACE; 70da6c28aaSamw char *open_filename = fname; 717c478bd9Sstevel@tonic-gate 727c478bd9Sstevel@tonic-gate if (startfd == AT_FDCWD) { 737c478bd9Sstevel@tonic-gate /* 747c478bd9Sstevel@tonic-gate * Regular open() 757c478bd9Sstevel@tonic-gate */ 767c478bd9Sstevel@tonic-gate startvp = NULL; 777c478bd9Sstevel@tonic-gate } else { 787c478bd9Sstevel@tonic-gate /* 797c478bd9Sstevel@tonic-gate * We're here via openat() 807c478bd9Sstevel@tonic-gate */ 817c478bd9Sstevel@tonic-gate char startchar; 827c478bd9Sstevel@tonic-gate 837c478bd9Sstevel@tonic-gate if (copyin(fname, &startchar, sizeof (char))) 847c478bd9Sstevel@tonic-gate return (set_errno(EFAULT)); 857c478bd9Sstevel@tonic-gate 867c478bd9Sstevel@tonic-gate /* 877c478bd9Sstevel@tonic-gate * if startchar is / then startfd is ignored 887c478bd9Sstevel@tonic-gate */ 897c478bd9Sstevel@tonic-gate if (startchar == '/') 907c478bd9Sstevel@tonic-gate startvp = NULL; 917c478bd9Sstevel@tonic-gate else { 927c478bd9Sstevel@tonic-gate if ((startfp = getf(startfd)) == NULL) 937c478bd9Sstevel@tonic-gate return (set_errno(EBADF)); 947c478bd9Sstevel@tonic-gate startvp = startfp->f_vnode; 957c478bd9Sstevel@tonic-gate VN_HOLD(startvp); 967c478bd9Sstevel@tonic-gate releasef(startfd); 977c478bd9Sstevel@tonic-gate } 987c478bd9Sstevel@tonic-gate } 997c478bd9Sstevel@tonic-gate 100da6c28aaSamw /* 101da6c28aaSamw * Handle openattrdirat request 102da6c28aaSamw */ 103da6c28aaSamw if (filemode & FXATTRDIROPEN) { 104da6c28aaSamw if (audit_active) 105da6c28aaSamw audit_setfsat_path(1); 106da6c28aaSamw 107da6c28aaSamw if (error = lookupnameat(fname, seg, FOLLOW, 108da6c28aaSamw NULLVPP, &vp, startvp)) 109da6c28aaSamw return (set_errno(error)); 110da6c28aaSamw if (startvp) { 111da6c28aaSamw VN_RELE(startvp); 112da6c28aaSamw startvp = NULL; 113da6c28aaSamw } 114da6c28aaSamw 115da6c28aaSamw startvp = vp; 116da6c28aaSamw } 117da6c28aaSamw 118da6c28aaSamw /* 119da6c28aaSamw * Do we need to go into extended attribute space? 120da6c28aaSamw */ 121da6c28aaSamw if (filemode & (FXATTR|FXATTRDIROPEN)) { 122da6c28aaSamw vattr_t vattr; 1237c478bd9Sstevel@tonic-gate 1247c478bd9Sstevel@tonic-gate /* 1257c478bd9Sstevel@tonic-gate * Make sure we have a valid request. 1267c478bd9Sstevel@tonic-gate * We must either have a real fd or AT_FDCWD 1277c478bd9Sstevel@tonic-gate */ 1287c478bd9Sstevel@tonic-gate 1297c478bd9Sstevel@tonic-gate if (startfd != AT_FDCWD && startvp == NULL) { 1307c478bd9Sstevel@tonic-gate error = EINVAL; 1317c478bd9Sstevel@tonic-gate goto out; 1327c478bd9Sstevel@tonic-gate } 1337c478bd9Sstevel@tonic-gate 1347c478bd9Sstevel@tonic-gate if (error = pn_get(fname, UIO_USERSPACE, &pn)) { 1357c478bd9Sstevel@tonic-gate goto out; 1367c478bd9Sstevel@tonic-gate } 1377c478bd9Sstevel@tonic-gate 138da6c28aaSamw if (startfd == AT_FDCWD && !(filemode & FXATTRDIROPEN)) { 1397c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock); 1407c478bd9Sstevel@tonic-gate startvp = PTOU(p)->u_cdir; 1417c478bd9Sstevel@tonic-gate VN_HOLD(startvp); 1427c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock); 1437c478bd9Sstevel@tonic-gate } 1447c478bd9Sstevel@tonic-gate 1457c478bd9Sstevel@tonic-gate /* 146da6c28aaSamw * In order to access hidden attribute directory the 147da6c28aaSamw * user must be able to stat() the file 1487c478bd9Sstevel@tonic-gate */ 1497c478bd9Sstevel@tonic-gate 150da6c28aaSamw vattr.va_mask = AT_ALL; 151da6c28aaSamw if (error = VOP_GETATTR(startvp, &vattr, 0, CRED(), NULL)) { 1527c478bd9Sstevel@tonic-gate pn_free(&pn); 1537c478bd9Sstevel@tonic-gate goto out; 1547c478bd9Sstevel@tonic-gate } 1557c478bd9Sstevel@tonic-gate 156da6c28aaSamw if ((startvp->v_vfsp->vfs_flag & VFS_XATTR) != 0 || 1579660e5cbSJanice Chang vfs_has_feature(startvp->v_vfsp, VFSFT_SYSATTR_VIEWS)) { 1587c478bd9Sstevel@tonic-gate error = VOP_LOOKUP(startvp, "", &sdvp, &pn, 15993aeed83Smarks (filemode & FXATTRDIROPEN) ? LOOKUP_XATTR : 160da6c28aaSamw LOOKUP_XATTR|CREATE_XATTR_DIR, rootvp, CRED(), 161da6c28aaSamw NULL, NULL, NULL); 1627c478bd9Sstevel@tonic-gate } else { 1637c478bd9Sstevel@tonic-gate error = EINVAL; 1647c478bd9Sstevel@tonic-gate } 165da6c28aaSamw 166da6c28aaSamw /* 167da6c28aaSamw * For openattrdirat use "." as filename to open 168da6c28aaSamw * as part of vn_openat() 169da6c28aaSamw */ 170da6c28aaSamw if (error == 0 && (filemode & FXATTRDIROPEN)) { 171da6c28aaSamw open_filename = "."; 172da6c28aaSamw seg = UIO_SYSSPACE; 173da6c28aaSamw } 174da6c28aaSamw 1757c478bd9Sstevel@tonic-gate pn_free(&pn); 1767c478bd9Sstevel@tonic-gate if (error != 0) 1777c478bd9Sstevel@tonic-gate goto out; 1787c478bd9Sstevel@tonic-gate 1797c478bd9Sstevel@tonic-gate VN_RELE(startvp); 1807c478bd9Sstevel@tonic-gate startvp = sdvp; 1817c478bd9Sstevel@tonic-gate } 1827c478bd9Sstevel@tonic-gate 183da6c28aaSamw if ((filemode & (FREAD|FWRITE|FXATTRDIROPEN)) != 0) { 1847c478bd9Sstevel@tonic-gate if ((filemode & (FNONBLOCK|FNDELAY)) == (FNONBLOCK|FNDELAY)) 1857c478bd9Sstevel@tonic-gate filemode &= ~FNDELAY; 1867c478bd9Sstevel@tonic-gate error = falloc((vnode_t *)NULL, filemode, &fp, &fd); 1877c478bd9Sstevel@tonic-gate if (error == 0) { 1887c478bd9Sstevel@tonic-gate if (audit_active) 1897c478bd9Sstevel@tonic-gate audit_setfsat_path(1); 1907c478bd9Sstevel@tonic-gate /* 1917c478bd9Sstevel@tonic-gate * Last arg is a don't-care term if 1927c478bd9Sstevel@tonic-gate * !(filemode & FCREAT). 1937c478bd9Sstevel@tonic-gate */ 194da6c28aaSamw 195da6c28aaSamw error = vn_openat(open_filename, seg, filemode, 196da6c28aaSamw (int)(createmode & MODEMASK), 197da6c28aaSamw &vp, CRCREAT, PTOU(curproc)->u_cmask, 198da6c28aaSamw startvp, fd); 1997c478bd9Sstevel@tonic-gate 2007c478bd9Sstevel@tonic-gate if (startvp != NULL) 2017c478bd9Sstevel@tonic-gate VN_RELE(startvp); 2027c478bd9Sstevel@tonic-gate if (error == 0) { 2037c478bd9Sstevel@tonic-gate if (audit_active) 2047c478bd9Sstevel@tonic-gate audit_copen(fd, fp, vp); 2057c478bd9Sstevel@tonic-gate if ((vp->v_flag & VDUP) == 0) { 2067c478bd9Sstevel@tonic-gate fp->f_vnode = vp; 2077c478bd9Sstevel@tonic-gate mutex_exit(&fp->f_tlock); 2087c478bd9Sstevel@tonic-gate /* 2097c478bd9Sstevel@tonic-gate * We must now fill in the slot 2107c478bd9Sstevel@tonic-gate * falloc reserved. 2117c478bd9Sstevel@tonic-gate */ 2127c478bd9Sstevel@tonic-gate setf(fd, fp); 2137c478bd9Sstevel@tonic-gate return (fd); 2147c478bd9Sstevel@tonic-gate } else { 2157c478bd9Sstevel@tonic-gate /* 2167c478bd9Sstevel@tonic-gate * Special handling for /dev/fd. 2177c478bd9Sstevel@tonic-gate * Give up the file pointer 2187c478bd9Sstevel@tonic-gate * and dup the indicated file descriptor 2197c478bd9Sstevel@tonic-gate * (in v_rdev). This is ugly, but I've 2207c478bd9Sstevel@tonic-gate * seen worse. 2217c478bd9Sstevel@tonic-gate */ 2227c478bd9Sstevel@tonic-gate unfalloc(fp); 2237c478bd9Sstevel@tonic-gate dupfd = getminor(vp->v_rdev); 2247c478bd9Sstevel@tonic-gate type = vp->v_type; 2257c478bd9Sstevel@tonic-gate mutex_enter(&vp->v_lock); 2267c478bd9Sstevel@tonic-gate vp->v_flag &= ~VDUP; 2277c478bd9Sstevel@tonic-gate mutex_exit(&vp->v_lock); 2287c478bd9Sstevel@tonic-gate VN_RELE(vp); 2297c478bd9Sstevel@tonic-gate if (type != VCHR) 2307c478bd9Sstevel@tonic-gate return (set_errno(EINVAL)); 2317c478bd9Sstevel@tonic-gate if ((fp = getf(dupfd)) == NULL) { 2327c478bd9Sstevel@tonic-gate setf(fd, NULL); 2337c478bd9Sstevel@tonic-gate return (set_errno(EBADF)); 2347c478bd9Sstevel@tonic-gate } 2357c478bd9Sstevel@tonic-gate mutex_enter(&fp->f_tlock); 2367c478bd9Sstevel@tonic-gate fp->f_count++; 2377c478bd9Sstevel@tonic-gate mutex_exit(&fp->f_tlock); 2387c478bd9Sstevel@tonic-gate setf(fd, fp); 2397c478bd9Sstevel@tonic-gate releasef(dupfd); 2407c478bd9Sstevel@tonic-gate } 2417c478bd9Sstevel@tonic-gate return (fd); 2427c478bd9Sstevel@tonic-gate } else { 2437c478bd9Sstevel@tonic-gate setf(fd, NULL); 2447c478bd9Sstevel@tonic-gate unfalloc(fp); 2457c478bd9Sstevel@tonic-gate return (set_errno(error)); 2467c478bd9Sstevel@tonic-gate } 2477c478bd9Sstevel@tonic-gate } 2487c478bd9Sstevel@tonic-gate } else { 2497c478bd9Sstevel@tonic-gate error = EINVAL; 2507c478bd9Sstevel@tonic-gate } 2517c478bd9Sstevel@tonic-gate out: 2527c478bd9Sstevel@tonic-gate if (startvp != NULL) 2537c478bd9Sstevel@tonic-gate VN_RELE(startvp); 2547c478bd9Sstevel@tonic-gate return (set_errno(error)); 2557c478bd9Sstevel@tonic-gate } 2567c478bd9Sstevel@tonic-gate 2577c478bd9Sstevel@tonic-gate #define OPENMODE32(fmode) ((int)((fmode)-FOPEN)) 2587c478bd9Sstevel@tonic-gate #define OPENMODE64(fmode) (OPENMODE32(fmode) | FOFFMAX) 259da6c28aaSamw #define OPENMODEATTRDIR FXATTRDIROPEN 2607c478bd9Sstevel@tonic-gate #ifdef _LP64 2617c478bd9Sstevel@tonic-gate #define OPENMODE(fmode) OPENMODE64(fmode) 2627c478bd9Sstevel@tonic-gate #else 2637c478bd9Sstevel@tonic-gate #define OPENMODE OPENMODE32 2647c478bd9Sstevel@tonic-gate #endif 2657c478bd9Sstevel@tonic-gate 2667c478bd9Sstevel@tonic-gate /* 2677c478bd9Sstevel@tonic-gate * Open a file. 2687c478bd9Sstevel@tonic-gate */ 2697c478bd9Sstevel@tonic-gate int 2707c478bd9Sstevel@tonic-gate openat(int fd, char *path, int fmode, int cmode) 2717c478bd9Sstevel@tonic-gate { 2727c478bd9Sstevel@tonic-gate return (copen(fd, path, OPENMODE(fmode), cmode)); 2737c478bd9Sstevel@tonic-gate } 2747c478bd9Sstevel@tonic-gate 275*8fd04b83SRoger A. Faulkner int 276*8fd04b83SRoger A. Faulkner open(char *path, int fmode, int cmode) 277*8fd04b83SRoger A. Faulkner { 278*8fd04b83SRoger A. Faulkner return (openat(AT_FDCWD, path, fmode, cmode)); 279*8fd04b83SRoger A. Faulkner } 280*8fd04b83SRoger A. Faulkner 2817c478bd9Sstevel@tonic-gate #if defined(_ILP32) || defined(_SYSCALL32_IMPL) 2827c478bd9Sstevel@tonic-gate /* 283*8fd04b83SRoger A. Faulkner * Open for large files in 32-bit environment. Sets the FOFFMAX flag. 2847c478bd9Sstevel@tonic-gate */ 2857c478bd9Sstevel@tonic-gate int 2867c478bd9Sstevel@tonic-gate openat64(int fd, char *path, int fmode, int cmode) 2877c478bd9Sstevel@tonic-gate { 2887c478bd9Sstevel@tonic-gate return (copen(fd, path, OPENMODE64(fmode), cmode)); 2897c478bd9Sstevel@tonic-gate } 2907c478bd9Sstevel@tonic-gate 291*8fd04b83SRoger A. Faulkner int 292*8fd04b83SRoger A. Faulkner open64(char *path, int fmode, int cmode) 293*8fd04b83SRoger A. Faulkner { 294*8fd04b83SRoger A. Faulkner return (openat64(AT_FDCWD, path, fmode, cmode)); 295*8fd04b83SRoger A. Faulkner } 296*8fd04b83SRoger A. Faulkner 2977c478bd9Sstevel@tonic-gate #endif /* _ILP32 || _SYSCALL32_IMPL */ 2987c478bd9Sstevel@tonic-gate 2997c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL 3007c478bd9Sstevel@tonic-gate /* 301*8fd04b83SRoger A. Faulkner * Open for 32-bit compatibility on 64-bit kernel 3027c478bd9Sstevel@tonic-gate */ 3037c478bd9Sstevel@tonic-gate int 3047c478bd9Sstevel@tonic-gate openat32(int fd, char *path, int fmode, int cmode) 3057c478bd9Sstevel@tonic-gate { 3067c478bd9Sstevel@tonic-gate return (copen(fd, path, OPENMODE32(fmode), cmode)); 3077c478bd9Sstevel@tonic-gate } 308da6c28aaSamw 309da6c28aaSamw int 310*8fd04b83SRoger A. Faulkner open32(char *path, int fmode, int cmode) 311da6c28aaSamw { 312*8fd04b83SRoger A. Faulkner return (openat32(AT_FDCWD, path, fmode, cmode)); 313da6c28aaSamw } 314*8fd04b83SRoger A. Faulkner 315*8fd04b83SRoger A. Faulkner #endif /* _SYSCALL32_IMPL */ 316