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 */ 217c478bd9Sstevel@tonic-gate /* 2293aeed83Smarks * Copyright 2008 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 #include <sys/param.h> 357c478bd9Sstevel@tonic-gate #include <sys/isa_defs.h> 367c478bd9Sstevel@tonic-gate #include <sys/types.h> 377c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h> 387c478bd9Sstevel@tonic-gate #include <sys/user.h> 397c478bd9Sstevel@tonic-gate #include <sys/systm.h> 407c478bd9Sstevel@tonic-gate #include <sys/errno.h> 417c478bd9Sstevel@tonic-gate #include <sys/fcntl.h> 427c478bd9Sstevel@tonic-gate #include <sys/stat.h> 437c478bd9Sstevel@tonic-gate #include <sys/vnode.h> 447c478bd9Sstevel@tonic-gate #include <sys/vfs.h> 457c478bd9Sstevel@tonic-gate #include <sys/file.h> 467c478bd9Sstevel@tonic-gate #include <sys/mode.h> 477c478bd9Sstevel@tonic-gate #include <sys/uio.h> 487c478bd9Sstevel@tonic-gate #include <sys/debug.h> 497c478bd9Sstevel@tonic-gate #include <c2/audit.h> 50da6c28aaSamw #include <sys/cmn_err.h> 517c478bd9Sstevel@tonic-gate 527c478bd9Sstevel@tonic-gate /* 537c478bd9Sstevel@tonic-gate * Common code for open()/openat() and creat(). Check permissions, allocate 547c478bd9Sstevel@tonic-gate * an open file structure, and call the device open routine (if any). 557c478bd9Sstevel@tonic-gate */ 567c478bd9Sstevel@tonic-gate 577c478bd9Sstevel@tonic-gate static int 587c478bd9Sstevel@tonic-gate copen(int startfd, char *fname, int filemode, int createmode) 597c478bd9Sstevel@tonic-gate { 607c478bd9Sstevel@tonic-gate struct pathname pn; 617c478bd9Sstevel@tonic-gate vnode_t *vp, *sdvp; 627c478bd9Sstevel@tonic-gate file_t *fp, *startfp; 637c478bd9Sstevel@tonic-gate enum vtype type; 647c478bd9Sstevel@tonic-gate int error; 657c478bd9Sstevel@tonic-gate int fd, dupfd; 667c478bd9Sstevel@tonic-gate vnode_t *startvp; 677c478bd9Sstevel@tonic-gate proc_t *p = curproc; 68da6c28aaSamw uio_seg_t seg = UIO_USERSPACE; 69da6c28aaSamw char *open_filename = fname; 707c478bd9Sstevel@tonic-gate 717c478bd9Sstevel@tonic-gate if (startfd == AT_FDCWD) { 727c478bd9Sstevel@tonic-gate /* 737c478bd9Sstevel@tonic-gate * Regular open() 747c478bd9Sstevel@tonic-gate */ 757c478bd9Sstevel@tonic-gate startvp = NULL; 767c478bd9Sstevel@tonic-gate } else { 777c478bd9Sstevel@tonic-gate /* 787c478bd9Sstevel@tonic-gate * We're here via openat() 797c478bd9Sstevel@tonic-gate */ 807c478bd9Sstevel@tonic-gate char startchar; 817c478bd9Sstevel@tonic-gate 827c478bd9Sstevel@tonic-gate if (copyin(fname, &startchar, sizeof (char))) 837c478bd9Sstevel@tonic-gate return (set_errno(EFAULT)); 847c478bd9Sstevel@tonic-gate 857c478bd9Sstevel@tonic-gate /* 867c478bd9Sstevel@tonic-gate * if startchar is / then startfd is ignored 877c478bd9Sstevel@tonic-gate */ 887c478bd9Sstevel@tonic-gate if (startchar == '/') 897c478bd9Sstevel@tonic-gate startvp = NULL; 907c478bd9Sstevel@tonic-gate else { 917c478bd9Sstevel@tonic-gate if ((startfp = getf(startfd)) == NULL) 927c478bd9Sstevel@tonic-gate return (set_errno(EBADF)); 937c478bd9Sstevel@tonic-gate startvp = startfp->f_vnode; 947c478bd9Sstevel@tonic-gate VN_HOLD(startvp); 957c478bd9Sstevel@tonic-gate releasef(startfd); 967c478bd9Sstevel@tonic-gate } 977c478bd9Sstevel@tonic-gate } 987c478bd9Sstevel@tonic-gate 99da6c28aaSamw /* 100da6c28aaSamw * Handle openattrdirat request 101da6c28aaSamw */ 102da6c28aaSamw if (filemode & FXATTRDIROPEN) { 103da6c28aaSamw if (audit_active) 104da6c28aaSamw audit_setfsat_path(1); 105da6c28aaSamw 106da6c28aaSamw if (error = lookupnameat(fname, seg, FOLLOW, 107da6c28aaSamw NULLVPP, &vp, startvp)) 108da6c28aaSamw return (set_errno(error)); 109da6c28aaSamw if (startvp) { 110da6c28aaSamw VN_RELE(startvp); 111da6c28aaSamw startvp = NULL; 112da6c28aaSamw } 113da6c28aaSamw 114da6c28aaSamw startvp = vp; 115da6c28aaSamw } 116da6c28aaSamw 117da6c28aaSamw /* 118da6c28aaSamw * Do we need to go into extended attribute space? 119da6c28aaSamw */ 120da6c28aaSamw if (filemode & (FXATTR|FXATTRDIROPEN)) { 121da6c28aaSamw vattr_t vattr; 1227c478bd9Sstevel@tonic-gate 1237c478bd9Sstevel@tonic-gate /* 1247c478bd9Sstevel@tonic-gate * Make sure we have a valid request. 1257c478bd9Sstevel@tonic-gate * We must either have a real fd or AT_FDCWD 1267c478bd9Sstevel@tonic-gate */ 1277c478bd9Sstevel@tonic-gate 1287c478bd9Sstevel@tonic-gate if (startfd != AT_FDCWD && startvp == NULL) { 1297c478bd9Sstevel@tonic-gate error = EINVAL; 1307c478bd9Sstevel@tonic-gate goto out; 1317c478bd9Sstevel@tonic-gate } 1327c478bd9Sstevel@tonic-gate 1337c478bd9Sstevel@tonic-gate if (error = pn_get(fname, UIO_USERSPACE, &pn)) { 1347c478bd9Sstevel@tonic-gate goto out; 1357c478bd9Sstevel@tonic-gate } 1367c478bd9Sstevel@tonic-gate 137da6c28aaSamw if (startfd == AT_FDCWD && !(filemode & FXATTRDIROPEN)) { 1387c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock); 1397c478bd9Sstevel@tonic-gate startvp = PTOU(p)->u_cdir; 1407c478bd9Sstevel@tonic-gate VN_HOLD(startvp); 1417c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock); 1427c478bd9Sstevel@tonic-gate } 1437c478bd9Sstevel@tonic-gate 1447c478bd9Sstevel@tonic-gate /* 145da6c28aaSamw * In order to access hidden attribute directory the 146da6c28aaSamw * user must be able to stat() the file 1477c478bd9Sstevel@tonic-gate */ 1487c478bd9Sstevel@tonic-gate 149da6c28aaSamw vattr.va_mask = AT_ALL; 150da6c28aaSamw if (error = VOP_GETATTR(startvp, &vattr, 0, CRED(), NULL)) { 1517c478bd9Sstevel@tonic-gate pn_free(&pn); 1527c478bd9Sstevel@tonic-gate goto out; 1537c478bd9Sstevel@tonic-gate } 1547c478bd9Sstevel@tonic-gate 155da6c28aaSamw if ((startvp->v_vfsp->vfs_flag & VFS_XATTR) != 0 || 156*9660e5cbSJanice Chang vfs_has_feature(startvp->v_vfsp, VFSFT_SYSATTR_VIEWS)) { 1577c478bd9Sstevel@tonic-gate error = VOP_LOOKUP(startvp, "", &sdvp, &pn, 15893aeed83Smarks (filemode & FXATTRDIROPEN) ? LOOKUP_XATTR : 159da6c28aaSamw LOOKUP_XATTR|CREATE_XATTR_DIR, rootvp, CRED(), 160da6c28aaSamw NULL, NULL, NULL); 1617c478bd9Sstevel@tonic-gate } else { 1627c478bd9Sstevel@tonic-gate error = EINVAL; 1637c478bd9Sstevel@tonic-gate } 164da6c28aaSamw 165da6c28aaSamw /* 166da6c28aaSamw * For openattrdirat use "." as filename to open 167da6c28aaSamw * as part of vn_openat() 168da6c28aaSamw */ 169da6c28aaSamw if (error == 0 && (filemode & FXATTRDIROPEN)) { 170da6c28aaSamw open_filename = "."; 171da6c28aaSamw seg = UIO_SYSSPACE; 172da6c28aaSamw } 173da6c28aaSamw 1747c478bd9Sstevel@tonic-gate pn_free(&pn); 1757c478bd9Sstevel@tonic-gate if (error != 0) 1767c478bd9Sstevel@tonic-gate goto out; 1777c478bd9Sstevel@tonic-gate 1787c478bd9Sstevel@tonic-gate VN_RELE(startvp); 1797c478bd9Sstevel@tonic-gate startvp = sdvp; 1807c478bd9Sstevel@tonic-gate } 1817c478bd9Sstevel@tonic-gate 182da6c28aaSamw if ((filemode & (FREAD|FWRITE|FXATTRDIROPEN)) != 0) { 1837c478bd9Sstevel@tonic-gate if ((filemode & (FNONBLOCK|FNDELAY)) == (FNONBLOCK|FNDELAY)) 1847c478bd9Sstevel@tonic-gate filemode &= ~FNDELAY; 1857c478bd9Sstevel@tonic-gate error = falloc((vnode_t *)NULL, filemode, &fp, &fd); 1867c478bd9Sstevel@tonic-gate if (error == 0) { 1877c478bd9Sstevel@tonic-gate if (audit_active) 1887c478bd9Sstevel@tonic-gate audit_setfsat_path(1); 1897c478bd9Sstevel@tonic-gate /* 1907c478bd9Sstevel@tonic-gate * Last arg is a don't-care term if 1917c478bd9Sstevel@tonic-gate * !(filemode & FCREAT). 1927c478bd9Sstevel@tonic-gate */ 193da6c28aaSamw 194da6c28aaSamw error = vn_openat(open_filename, seg, filemode, 195da6c28aaSamw (int)(createmode & MODEMASK), 196da6c28aaSamw &vp, CRCREAT, PTOU(curproc)->u_cmask, 197da6c28aaSamw startvp, fd); 1987c478bd9Sstevel@tonic-gate 1997c478bd9Sstevel@tonic-gate if (startvp != NULL) 2007c478bd9Sstevel@tonic-gate VN_RELE(startvp); 2017c478bd9Sstevel@tonic-gate if (error == 0) { 2027c478bd9Sstevel@tonic-gate if (audit_active) 2037c478bd9Sstevel@tonic-gate audit_copen(fd, fp, vp); 2047c478bd9Sstevel@tonic-gate if ((vp->v_flag & VDUP) == 0) { 2057c478bd9Sstevel@tonic-gate fp->f_vnode = vp; 2067c478bd9Sstevel@tonic-gate mutex_exit(&fp->f_tlock); 2077c478bd9Sstevel@tonic-gate /* 2087c478bd9Sstevel@tonic-gate * We must now fill in the slot 2097c478bd9Sstevel@tonic-gate * falloc reserved. 2107c478bd9Sstevel@tonic-gate */ 2117c478bd9Sstevel@tonic-gate setf(fd, fp); 2127c478bd9Sstevel@tonic-gate return (fd); 2137c478bd9Sstevel@tonic-gate } else { 2147c478bd9Sstevel@tonic-gate /* 2157c478bd9Sstevel@tonic-gate * Special handling for /dev/fd. 2167c478bd9Sstevel@tonic-gate * Give up the file pointer 2177c478bd9Sstevel@tonic-gate * and dup the indicated file descriptor 2187c478bd9Sstevel@tonic-gate * (in v_rdev). This is ugly, but I've 2197c478bd9Sstevel@tonic-gate * seen worse. 2207c478bd9Sstevel@tonic-gate */ 2217c478bd9Sstevel@tonic-gate unfalloc(fp); 2227c478bd9Sstevel@tonic-gate dupfd = getminor(vp->v_rdev); 2237c478bd9Sstevel@tonic-gate type = vp->v_type; 2247c478bd9Sstevel@tonic-gate mutex_enter(&vp->v_lock); 2257c478bd9Sstevel@tonic-gate vp->v_flag &= ~VDUP; 2267c478bd9Sstevel@tonic-gate mutex_exit(&vp->v_lock); 2277c478bd9Sstevel@tonic-gate VN_RELE(vp); 2287c478bd9Sstevel@tonic-gate if (type != VCHR) 2297c478bd9Sstevel@tonic-gate return (set_errno(EINVAL)); 2307c478bd9Sstevel@tonic-gate if ((fp = getf(dupfd)) == NULL) { 2317c478bd9Sstevel@tonic-gate setf(fd, NULL); 2327c478bd9Sstevel@tonic-gate return (set_errno(EBADF)); 2337c478bd9Sstevel@tonic-gate } 2347c478bd9Sstevel@tonic-gate mutex_enter(&fp->f_tlock); 2357c478bd9Sstevel@tonic-gate fp->f_count++; 2367c478bd9Sstevel@tonic-gate mutex_exit(&fp->f_tlock); 2377c478bd9Sstevel@tonic-gate setf(fd, fp); 2387c478bd9Sstevel@tonic-gate releasef(dupfd); 2397c478bd9Sstevel@tonic-gate } 2407c478bd9Sstevel@tonic-gate return (fd); 2417c478bd9Sstevel@tonic-gate } else { 2427c478bd9Sstevel@tonic-gate setf(fd, NULL); 2437c478bd9Sstevel@tonic-gate unfalloc(fp); 2447c478bd9Sstevel@tonic-gate return (set_errno(error)); 2457c478bd9Sstevel@tonic-gate } 2467c478bd9Sstevel@tonic-gate } 2477c478bd9Sstevel@tonic-gate } else { 2487c478bd9Sstevel@tonic-gate error = EINVAL; 2497c478bd9Sstevel@tonic-gate } 2507c478bd9Sstevel@tonic-gate out: 2517c478bd9Sstevel@tonic-gate if (startvp != NULL) 2527c478bd9Sstevel@tonic-gate VN_RELE(startvp); 2537c478bd9Sstevel@tonic-gate return (set_errno(error)); 2547c478bd9Sstevel@tonic-gate } 2557c478bd9Sstevel@tonic-gate 2567c478bd9Sstevel@tonic-gate #define OPENMODE32(fmode) ((int)((fmode)-FOPEN)) 2577c478bd9Sstevel@tonic-gate #define CREATMODE32 (FWRITE|FCREAT|FTRUNC) 2587c478bd9Sstevel@tonic-gate #define OPENMODE64(fmode) (OPENMODE32(fmode) | FOFFMAX) 259da6c28aaSamw #define OPENMODEATTRDIR FXATTRDIROPEN 2607c478bd9Sstevel@tonic-gate #define CREATMODE64 (CREATMODE32 | FOFFMAX) 2617c478bd9Sstevel@tonic-gate #ifdef _LP64 2627c478bd9Sstevel@tonic-gate #define OPENMODE(fmode) OPENMODE64(fmode) 2637c478bd9Sstevel@tonic-gate #define CREATMODE CREATMODE64 2647c478bd9Sstevel@tonic-gate #else 2657c478bd9Sstevel@tonic-gate #define OPENMODE OPENMODE32 2667c478bd9Sstevel@tonic-gate #define CREATMODE CREATMODE32 2677c478bd9Sstevel@tonic-gate #endif 2687c478bd9Sstevel@tonic-gate 2697c478bd9Sstevel@tonic-gate /* 2707c478bd9Sstevel@tonic-gate * Open a file. 2717c478bd9Sstevel@tonic-gate */ 2727c478bd9Sstevel@tonic-gate int 2737c478bd9Sstevel@tonic-gate open(char *fname, int fmode, int cmode) 2747c478bd9Sstevel@tonic-gate { 2757c478bd9Sstevel@tonic-gate return (copen(AT_FDCWD, fname, OPENMODE(fmode), cmode)); 2767c478bd9Sstevel@tonic-gate } 2777c478bd9Sstevel@tonic-gate 2787c478bd9Sstevel@tonic-gate /* 2797c478bd9Sstevel@tonic-gate * Create a file. 2807c478bd9Sstevel@tonic-gate */ 2817c478bd9Sstevel@tonic-gate int 2827c478bd9Sstevel@tonic-gate creat(char *fname, int cmode) 2837c478bd9Sstevel@tonic-gate { 2847c478bd9Sstevel@tonic-gate return (copen(AT_FDCWD, fname, CREATMODE, cmode)); 2857c478bd9Sstevel@tonic-gate } 2867c478bd9Sstevel@tonic-gate 2877c478bd9Sstevel@tonic-gate int 2887c478bd9Sstevel@tonic-gate openat(int fd, char *path, int fmode, int cmode) 2897c478bd9Sstevel@tonic-gate { 2907c478bd9Sstevel@tonic-gate return (copen(fd, path, OPENMODE(fmode), cmode)); 2917c478bd9Sstevel@tonic-gate } 2927c478bd9Sstevel@tonic-gate 2937c478bd9Sstevel@tonic-gate #if defined(_ILP32) || defined(_SYSCALL32_IMPL) 2947c478bd9Sstevel@tonic-gate /* 2957c478bd9Sstevel@tonic-gate * Open and Creat for large files in 32-bit environment. Sets the FOFFMAX flag. 2967c478bd9Sstevel@tonic-gate */ 2977c478bd9Sstevel@tonic-gate int 2987c478bd9Sstevel@tonic-gate open64(char *fname, int fmode, int cmode) 2997c478bd9Sstevel@tonic-gate { 3007c478bd9Sstevel@tonic-gate return (copen(AT_FDCWD, fname, OPENMODE64(fmode), cmode)); 3017c478bd9Sstevel@tonic-gate } 3027c478bd9Sstevel@tonic-gate 3037c478bd9Sstevel@tonic-gate int 3047c478bd9Sstevel@tonic-gate creat64(char *fname, int cmode) 3057c478bd9Sstevel@tonic-gate { 3067c478bd9Sstevel@tonic-gate return (copen(AT_FDCWD, fname, CREATMODE64, cmode)); 3077c478bd9Sstevel@tonic-gate } 3087c478bd9Sstevel@tonic-gate 3097c478bd9Sstevel@tonic-gate int 3107c478bd9Sstevel@tonic-gate openat64(int fd, char *path, int fmode, int cmode) 3117c478bd9Sstevel@tonic-gate { 3127c478bd9Sstevel@tonic-gate return (copen(fd, path, OPENMODE64(fmode), cmode)); 3137c478bd9Sstevel@tonic-gate } 3147c478bd9Sstevel@tonic-gate 3157c478bd9Sstevel@tonic-gate #endif /* _ILP32 || _SYSCALL32_IMPL */ 3167c478bd9Sstevel@tonic-gate 3177c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL 3187c478bd9Sstevel@tonic-gate /* 3197c478bd9Sstevel@tonic-gate * Open and Creat for 32-bit compatibility on 64-bit kernel 3207c478bd9Sstevel@tonic-gate */ 3217c478bd9Sstevel@tonic-gate int 3227c478bd9Sstevel@tonic-gate open32(char *fname, int fmode, int cmode) 3237c478bd9Sstevel@tonic-gate { 3247c478bd9Sstevel@tonic-gate return (copen(AT_FDCWD, fname, OPENMODE32(fmode), cmode)); 3257c478bd9Sstevel@tonic-gate } 3267c478bd9Sstevel@tonic-gate 3277c478bd9Sstevel@tonic-gate int 3287c478bd9Sstevel@tonic-gate creat32(char *fname, int cmode) 3297c478bd9Sstevel@tonic-gate { 3307c478bd9Sstevel@tonic-gate return (copen(AT_FDCWD, fname, CREATMODE32, cmode)); 3317c478bd9Sstevel@tonic-gate } 3327c478bd9Sstevel@tonic-gate 3337c478bd9Sstevel@tonic-gate int 3347c478bd9Sstevel@tonic-gate openat32(int fd, char *path, int fmode, int cmode) 3357c478bd9Sstevel@tonic-gate { 3367c478bd9Sstevel@tonic-gate return (copen(fd, path, OPENMODE32(fmode), cmode)); 3377c478bd9Sstevel@tonic-gate } 338da6c28aaSamw 3397c478bd9Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */ 340da6c28aaSamw 341da6c28aaSamw /* 342da6c28aaSamw * Special interface to open hidden attribute directory. 343da6c28aaSamw */ 344da6c28aaSamw int 345da6c28aaSamw openattrdirat(int fd, char *fname) 346da6c28aaSamw { 347da6c28aaSamw return (copen(fd, fname, OPENMODEATTRDIR, 0)); 348da6c28aaSamw } 349