xref: /titanic_44/usr/src/uts/common/syscall/open.c (revision 4a0fa5460e94a33980ceffce0ba3db8802570449)
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  */
218fd04b83SRoger A. Faulkner 
227c478bd9Sstevel@tonic-gate /*
23*4a0fa546SMarek Pospisil  * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
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>
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate /*
528fd04b83SRoger A. Faulkner  * Common code for openat().  Check permissions, allocate an open
538fd04b83SRoger A. Faulkner  * file structure, and call the device open routine (if any).
547c478bd9Sstevel@tonic-gate  */
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate static int
577c478bd9Sstevel@tonic-gate copen(int startfd, char *fname, int filemode, int createmode)
587c478bd9Sstevel@tonic-gate {
597c478bd9Sstevel@tonic-gate 	struct pathname pn;
607c478bd9Sstevel@tonic-gate 	vnode_t *vp, *sdvp;
617c478bd9Sstevel@tonic-gate 	file_t *fp, *startfp;
627c478bd9Sstevel@tonic-gate 	enum vtype type;
637c478bd9Sstevel@tonic-gate 	int error;
647c478bd9Sstevel@tonic-gate 	int fd, dupfd;
657c478bd9Sstevel@tonic-gate 	vnode_t *startvp;
667c478bd9Sstevel@tonic-gate 	proc_t *p = curproc;
67da6c28aaSamw 	uio_seg_t seg = UIO_USERSPACE;
68da6c28aaSamw 	char *open_filename = fname;
69005d3febSMarek Pospisil 	uint32_t auditing = AU_AUDITING();
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) {
103005d3febSMarek Pospisil 		if (auditing)
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 ||
1569660e5cbSJanice 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) {
187005d3febSMarek Pospisil 			if (auditing)
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 ((vp->v_flag & VDUP) == 0) {
2037c478bd9Sstevel@tonic-gate 					fp->f_vnode = vp;
2047c478bd9Sstevel@tonic-gate 					mutex_exit(&fp->f_tlock);
2057c478bd9Sstevel@tonic-gate 					/*
2067c478bd9Sstevel@tonic-gate 					 * We must now fill in the slot
2077c478bd9Sstevel@tonic-gate 					 * falloc reserved.
2087c478bd9Sstevel@tonic-gate 					 */
2097c478bd9Sstevel@tonic-gate 					setf(fd, fp);
2107c478bd9Sstevel@tonic-gate 					return (fd);
2117c478bd9Sstevel@tonic-gate 				} else {
2127c478bd9Sstevel@tonic-gate 					/*
2137c478bd9Sstevel@tonic-gate 					 * Special handling for /dev/fd.
2147c478bd9Sstevel@tonic-gate 					 * Give up the file pointer
2157c478bd9Sstevel@tonic-gate 					 * and dup the indicated file descriptor
2167c478bd9Sstevel@tonic-gate 					 * (in v_rdev). This is ugly, but I've
2177c478bd9Sstevel@tonic-gate 					 * seen worse.
2187c478bd9Sstevel@tonic-gate 					 */
2197c478bd9Sstevel@tonic-gate 					unfalloc(fp);
2207c478bd9Sstevel@tonic-gate 					dupfd = getminor(vp->v_rdev);
2217c478bd9Sstevel@tonic-gate 					type = vp->v_type;
2227c478bd9Sstevel@tonic-gate 					mutex_enter(&vp->v_lock);
2237c478bd9Sstevel@tonic-gate 					vp->v_flag &= ~VDUP;
2247c478bd9Sstevel@tonic-gate 					mutex_exit(&vp->v_lock);
2257c478bd9Sstevel@tonic-gate 					VN_RELE(vp);
2267c478bd9Sstevel@tonic-gate 					if (type != VCHR)
2277c478bd9Sstevel@tonic-gate 						return (set_errno(EINVAL));
2287c478bd9Sstevel@tonic-gate 					if ((fp = getf(dupfd)) == NULL) {
2297c478bd9Sstevel@tonic-gate 						setf(fd, NULL);
2307c478bd9Sstevel@tonic-gate 						return (set_errno(EBADF));
2317c478bd9Sstevel@tonic-gate 					}
2327c478bd9Sstevel@tonic-gate 					mutex_enter(&fp->f_tlock);
2337c478bd9Sstevel@tonic-gate 					fp->f_count++;
2347c478bd9Sstevel@tonic-gate 					mutex_exit(&fp->f_tlock);
2357c478bd9Sstevel@tonic-gate 					setf(fd, fp);
2367c478bd9Sstevel@tonic-gate 					releasef(dupfd);
2377c478bd9Sstevel@tonic-gate 				}
2387c478bd9Sstevel@tonic-gate 				return (fd);
2397c478bd9Sstevel@tonic-gate 			} else {
2407c478bd9Sstevel@tonic-gate 				setf(fd, NULL);
2417c478bd9Sstevel@tonic-gate 				unfalloc(fp);
2427c478bd9Sstevel@tonic-gate 				return (set_errno(error));
2437c478bd9Sstevel@tonic-gate 			}
2447c478bd9Sstevel@tonic-gate 		}
2457c478bd9Sstevel@tonic-gate 	} else {
2467c478bd9Sstevel@tonic-gate 		error = EINVAL;
2477c478bd9Sstevel@tonic-gate 	}
2487c478bd9Sstevel@tonic-gate out:
2497c478bd9Sstevel@tonic-gate 	if (startvp != NULL)
2507c478bd9Sstevel@tonic-gate 		VN_RELE(startvp);
2517c478bd9Sstevel@tonic-gate 	return (set_errno(error));
2527c478bd9Sstevel@tonic-gate }
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate #define	OPENMODE32(fmode)	((int)((fmode)-FOPEN))
2557c478bd9Sstevel@tonic-gate #define	OPENMODE64(fmode)	(OPENMODE32(fmode) | FOFFMAX)
256da6c28aaSamw #define	OPENMODEATTRDIR		FXATTRDIROPEN
2577c478bd9Sstevel@tonic-gate #ifdef _LP64
2587c478bd9Sstevel@tonic-gate #define	OPENMODE(fmode)		OPENMODE64(fmode)
2597c478bd9Sstevel@tonic-gate #else
2607c478bd9Sstevel@tonic-gate #define	OPENMODE		OPENMODE32
2617c478bd9Sstevel@tonic-gate #endif
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate /*
2647c478bd9Sstevel@tonic-gate  * Open a file.
2657c478bd9Sstevel@tonic-gate  */
2667c478bd9Sstevel@tonic-gate int
2677c478bd9Sstevel@tonic-gate openat(int fd, char *path, int fmode, int cmode)
2687c478bd9Sstevel@tonic-gate {
2697c478bd9Sstevel@tonic-gate 	return (copen(fd, path, OPENMODE(fmode), cmode));
2707c478bd9Sstevel@tonic-gate }
2717c478bd9Sstevel@tonic-gate 
2728fd04b83SRoger A. Faulkner int
2738fd04b83SRoger A. Faulkner open(char *path, int fmode, int cmode)
2748fd04b83SRoger A. Faulkner {
2758fd04b83SRoger A. Faulkner 	return (openat(AT_FDCWD, path, fmode, cmode));
2768fd04b83SRoger A. Faulkner }
2778fd04b83SRoger A. Faulkner 
2787c478bd9Sstevel@tonic-gate #if defined(_ILP32) || defined(_SYSCALL32_IMPL)
2797c478bd9Sstevel@tonic-gate /*
2808fd04b83SRoger A. Faulkner  * Open for large files in 32-bit environment. Sets the FOFFMAX flag.
2817c478bd9Sstevel@tonic-gate  */
2827c478bd9Sstevel@tonic-gate int
2837c478bd9Sstevel@tonic-gate openat64(int fd, char *path, int fmode, int cmode)
2847c478bd9Sstevel@tonic-gate {
2857c478bd9Sstevel@tonic-gate 	return (copen(fd, path, OPENMODE64(fmode), cmode));
2867c478bd9Sstevel@tonic-gate }
2877c478bd9Sstevel@tonic-gate 
2888fd04b83SRoger A. Faulkner int
2898fd04b83SRoger A. Faulkner open64(char *path, int fmode, int cmode)
2908fd04b83SRoger A. Faulkner {
2918fd04b83SRoger A. Faulkner 	return (openat64(AT_FDCWD, path, fmode, cmode));
2928fd04b83SRoger A. Faulkner }
2938fd04b83SRoger A. Faulkner 
2947c478bd9Sstevel@tonic-gate #endif	/* _ILP32 || _SYSCALL32_IMPL */
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
2977c478bd9Sstevel@tonic-gate /*
2988fd04b83SRoger A. Faulkner  * Open for 32-bit compatibility on 64-bit kernel
2997c478bd9Sstevel@tonic-gate  */
3007c478bd9Sstevel@tonic-gate int
3017c478bd9Sstevel@tonic-gate openat32(int fd, char *path, int fmode, int cmode)
3027c478bd9Sstevel@tonic-gate {
3037c478bd9Sstevel@tonic-gate 	return (copen(fd, path, OPENMODE32(fmode), cmode));
3047c478bd9Sstevel@tonic-gate }
305da6c28aaSamw 
306da6c28aaSamw int
3078fd04b83SRoger A. Faulkner open32(char *path, int fmode, int cmode)
308da6c28aaSamw {
3098fd04b83SRoger A. Faulkner 	return (openat32(AT_FDCWD, path, fmode, cmode));
310da6c28aaSamw }
3118fd04b83SRoger A. Faulkner 
3128fd04b83SRoger A. Faulkner #endif	/* _SYSCALL32_IMPL */
313