xref: /titanic_53/usr/src/uts/common/syscall/open.c (revision c4d3e299d9d0295322679b4d484560411b6822d5)
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 /*
234a0fa546SMarek 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();
70*c4d3e299SBrent Paulson 	char startchar;
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 		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 
98da6c28aaSamw 	/*
99*c4d3e299SBrent Paulson 	 * Handle __openattrdirat() requests
100da6c28aaSamw 	 */
101da6c28aaSamw 	if (filemode & FXATTRDIROPEN) {
102*c4d3e299SBrent Paulson 		if (auditing && (startvp != NULL))
103da6c28aaSamw 			audit_setfsat_path(1);
104da6c28aaSamw 
105da6c28aaSamw 		if (error = lookupnameat(fname, seg, FOLLOW,
106da6c28aaSamw 		    NULLVPP, &vp, startvp))
107da6c28aaSamw 			return (set_errno(error));
108*c4d3e299SBrent Paulson 		if (startvp != NULL)
109da6c28aaSamw 			VN_RELE(startvp);
110da6c28aaSamw 
111da6c28aaSamw 		startvp = vp;
112da6c28aaSamw 	}
113da6c28aaSamw 
114da6c28aaSamw 	/*
115da6c28aaSamw 	 * Do we need to go into extended attribute space?
116da6c28aaSamw 	 */
117*c4d3e299SBrent Paulson 	if (filemode & FXATTR) {
118*c4d3e299SBrent Paulson 		if (startfd == AT_FDCWD) {
119*c4d3e299SBrent Paulson 			if (copyin(fname, &startchar, sizeof (char)))
120*c4d3e299SBrent Paulson 				return (set_errno(EFAULT));
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 			/*
123*c4d3e299SBrent Paulson 			 * If startchar == '/' then no extended attributes
124*c4d3e299SBrent Paulson 			 * are looked up.
1257c478bd9Sstevel@tonic-gate 			 */
126*c4d3e299SBrent Paulson 			if (startchar == '/') {
127*c4d3e299SBrent Paulson 				startvp = NULL;
128*c4d3e299SBrent Paulson 			} else {
1297c478bd9Sstevel@tonic-gate 				mutex_enter(&p->p_lock);
1307c478bd9Sstevel@tonic-gate 				startvp = PTOU(p)->u_cdir;
1317c478bd9Sstevel@tonic-gate 				VN_HOLD(startvp);
1327c478bd9Sstevel@tonic-gate 				mutex_exit(&p->p_lock);
1337c478bd9Sstevel@tonic-gate 			}
134*c4d3e299SBrent Paulson 		}
135*c4d3e299SBrent Paulson 
136*c4d3e299SBrent Paulson 		/*
137*c4d3e299SBrent Paulson 		 * Make sure we have a valid extended attribute request.
138*c4d3e299SBrent Paulson 		 * We must either have a real fd or AT_FDCWD and a relative
139*c4d3e299SBrent Paulson 		 * pathname.
140*c4d3e299SBrent Paulson 		 */
141*c4d3e299SBrent Paulson 		if (startvp == NULL) {
142*c4d3e299SBrent Paulson 			goto noxattr;
143*c4d3e299SBrent Paulson 		}
144*c4d3e299SBrent Paulson 	}
145*c4d3e299SBrent Paulson 
146*c4d3e299SBrent Paulson 	if (filemode & (FXATTR|FXATTRDIROPEN)) {
147*c4d3e299SBrent Paulson 		vattr_t vattr;
148*c4d3e299SBrent Paulson 
149*c4d3e299SBrent Paulson 		if (error = pn_get(fname, UIO_USERSPACE, &pn)) {
150*c4d3e299SBrent Paulson 			goto out;
151*c4d3e299SBrent Paulson 		}
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 		/*
154da6c28aaSamw 		 * In order to access hidden attribute directory the
155da6c28aaSamw 		 * user must be able to stat() the file
1567c478bd9Sstevel@tonic-gate 		 */
157da6c28aaSamw 		vattr.va_mask = AT_ALL;
158da6c28aaSamw 		if (error = VOP_GETATTR(startvp, &vattr, 0, CRED(), NULL)) {
1597c478bd9Sstevel@tonic-gate 			pn_free(&pn);
1607c478bd9Sstevel@tonic-gate 			goto out;
1617c478bd9Sstevel@tonic-gate 		}
1627c478bd9Sstevel@tonic-gate 
163da6c28aaSamw 		if ((startvp->v_vfsp->vfs_flag & VFS_XATTR) != 0 ||
1649660e5cbSJanice Chang 		    vfs_has_feature(startvp->v_vfsp, VFSFT_SYSATTR_VIEWS)) {
1657c478bd9Sstevel@tonic-gate 			error = VOP_LOOKUP(startvp, "", &sdvp, &pn,
16693aeed83Smarks 			    (filemode & FXATTRDIROPEN) ? LOOKUP_XATTR :
167da6c28aaSamw 			    LOOKUP_XATTR|CREATE_XATTR_DIR, rootvp, CRED(),
168da6c28aaSamw 			    NULL, NULL, NULL);
1697c478bd9Sstevel@tonic-gate 		} else {
1707c478bd9Sstevel@tonic-gate 			error = EINVAL;
1717c478bd9Sstevel@tonic-gate 		}
172da6c28aaSamw 
173da6c28aaSamw 		/*
174*c4d3e299SBrent Paulson 		 * For __openattrdirat() use "." as filename to open
175da6c28aaSamw 		 * as part of vn_openat()
176da6c28aaSamw 		 */
177da6c28aaSamw 		if (error == 0 && (filemode & FXATTRDIROPEN)) {
178da6c28aaSamw 			open_filename = ".";
179da6c28aaSamw 			seg = UIO_SYSSPACE;
180da6c28aaSamw 		}
181da6c28aaSamw 
1827c478bd9Sstevel@tonic-gate 		pn_free(&pn);
1837c478bd9Sstevel@tonic-gate 		if (error != 0)
1847c478bd9Sstevel@tonic-gate 			goto out;
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 		VN_RELE(startvp);
1877c478bd9Sstevel@tonic-gate 		startvp = sdvp;
1887c478bd9Sstevel@tonic-gate 	}
1897c478bd9Sstevel@tonic-gate 
190*c4d3e299SBrent Paulson noxattr:
191da6c28aaSamw 	if ((filemode & (FREAD|FWRITE|FXATTRDIROPEN)) != 0) {
1927c478bd9Sstevel@tonic-gate 		if ((filemode & (FNONBLOCK|FNDELAY)) == (FNONBLOCK|FNDELAY))
1937c478bd9Sstevel@tonic-gate 			filemode &= ~FNDELAY;
1947c478bd9Sstevel@tonic-gate 		error = falloc((vnode_t *)NULL, filemode, &fp, &fd);
1957c478bd9Sstevel@tonic-gate 		if (error == 0) {
196*c4d3e299SBrent Paulson 			if (auditing && (startvp != NULL))
1977c478bd9Sstevel@tonic-gate 				audit_setfsat_path(1);
1987c478bd9Sstevel@tonic-gate 			/*
1997c478bd9Sstevel@tonic-gate 			 * Last arg is a don't-care term if
2007c478bd9Sstevel@tonic-gate 			 * !(filemode & FCREAT).
2017c478bd9Sstevel@tonic-gate 			 */
202da6c28aaSamw 			error = vn_openat(open_filename, seg, filemode,
203da6c28aaSamw 			    (int)(createmode & MODEMASK),
204da6c28aaSamw 			    &vp, CRCREAT, PTOU(curproc)->u_cmask,
205da6c28aaSamw 			    startvp, fd);
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 			if (startvp != NULL)
2087c478bd9Sstevel@tonic-gate 				VN_RELE(startvp);
2097c478bd9Sstevel@tonic-gate 			if (error == 0) {
2107c478bd9Sstevel@tonic-gate 				if ((vp->v_flag & VDUP) == 0) {
2117c478bd9Sstevel@tonic-gate 					fp->f_vnode = vp;
2127c478bd9Sstevel@tonic-gate 					mutex_exit(&fp->f_tlock);
2137c478bd9Sstevel@tonic-gate 					/*
2147c478bd9Sstevel@tonic-gate 					 * We must now fill in the slot
2157c478bd9Sstevel@tonic-gate 					 * falloc reserved.
2167c478bd9Sstevel@tonic-gate 					 */
2177c478bd9Sstevel@tonic-gate 					setf(fd, fp);
2187c478bd9Sstevel@tonic-gate 					return (fd);
2197c478bd9Sstevel@tonic-gate 				} else {
2207c478bd9Sstevel@tonic-gate 					/*
2217c478bd9Sstevel@tonic-gate 					 * Special handling for /dev/fd.
2227c478bd9Sstevel@tonic-gate 					 * Give up the file pointer
2237c478bd9Sstevel@tonic-gate 					 * and dup the indicated file descriptor
2247c478bd9Sstevel@tonic-gate 					 * (in v_rdev). This is ugly, but I've
2257c478bd9Sstevel@tonic-gate 					 * seen worse.
2267c478bd9Sstevel@tonic-gate 					 */
2277c478bd9Sstevel@tonic-gate 					unfalloc(fp);
2287c478bd9Sstevel@tonic-gate 					dupfd = getminor(vp->v_rdev);
2297c478bd9Sstevel@tonic-gate 					type = vp->v_type;
2307c478bd9Sstevel@tonic-gate 					mutex_enter(&vp->v_lock);
2317c478bd9Sstevel@tonic-gate 					vp->v_flag &= ~VDUP;
2327c478bd9Sstevel@tonic-gate 					mutex_exit(&vp->v_lock);
2337c478bd9Sstevel@tonic-gate 					VN_RELE(vp);
2347c478bd9Sstevel@tonic-gate 					if (type != VCHR)
2357c478bd9Sstevel@tonic-gate 						return (set_errno(EINVAL));
2367c478bd9Sstevel@tonic-gate 					if ((fp = getf(dupfd)) == NULL) {
2377c478bd9Sstevel@tonic-gate 						setf(fd, NULL);
2387c478bd9Sstevel@tonic-gate 						return (set_errno(EBADF));
2397c478bd9Sstevel@tonic-gate 					}
2407c478bd9Sstevel@tonic-gate 					mutex_enter(&fp->f_tlock);
2417c478bd9Sstevel@tonic-gate 					fp->f_count++;
2427c478bd9Sstevel@tonic-gate 					mutex_exit(&fp->f_tlock);
2437c478bd9Sstevel@tonic-gate 					setf(fd, fp);
2447c478bd9Sstevel@tonic-gate 					releasef(dupfd);
2457c478bd9Sstevel@tonic-gate 				}
2467c478bd9Sstevel@tonic-gate 				return (fd);
2477c478bd9Sstevel@tonic-gate 			} else {
2487c478bd9Sstevel@tonic-gate 				setf(fd, NULL);
2497c478bd9Sstevel@tonic-gate 				unfalloc(fp);
2507c478bd9Sstevel@tonic-gate 				return (set_errno(error));
2517c478bd9Sstevel@tonic-gate 			}
2527c478bd9Sstevel@tonic-gate 		}
2537c478bd9Sstevel@tonic-gate 	} else {
2547c478bd9Sstevel@tonic-gate 		error = EINVAL;
2557c478bd9Sstevel@tonic-gate 	}
2567c478bd9Sstevel@tonic-gate out:
2577c478bd9Sstevel@tonic-gate 	if (startvp != NULL)
2587c478bd9Sstevel@tonic-gate 		VN_RELE(startvp);
2597c478bd9Sstevel@tonic-gate 	return (set_errno(error));
2607c478bd9Sstevel@tonic-gate }
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate #define	OPENMODE32(fmode)	((int)((fmode)-FOPEN))
2637c478bd9Sstevel@tonic-gate #define	OPENMODE64(fmode)	(OPENMODE32(fmode) | FOFFMAX)
264da6c28aaSamw #define	OPENMODEATTRDIR		FXATTRDIROPEN
2657c478bd9Sstevel@tonic-gate #ifdef _LP64
2667c478bd9Sstevel@tonic-gate #define	OPENMODE(fmode)		OPENMODE64(fmode)
2677c478bd9Sstevel@tonic-gate #else
2687c478bd9Sstevel@tonic-gate #define	OPENMODE		OPENMODE32
2697c478bd9Sstevel@tonic-gate #endif
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate /*
2727c478bd9Sstevel@tonic-gate  * Open a file.
2737c478bd9Sstevel@tonic-gate  */
2747c478bd9Sstevel@tonic-gate int
2757c478bd9Sstevel@tonic-gate openat(int fd, char *path, int fmode, int cmode)
2767c478bd9Sstevel@tonic-gate {
2777c478bd9Sstevel@tonic-gate 	return (copen(fd, path, OPENMODE(fmode), cmode));
2787c478bd9Sstevel@tonic-gate }
2797c478bd9Sstevel@tonic-gate 
2808fd04b83SRoger A. Faulkner int
2818fd04b83SRoger A. Faulkner open(char *path, int fmode, int cmode)
2828fd04b83SRoger A. Faulkner {
2838fd04b83SRoger A. Faulkner 	return (openat(AT_FDCWD, path, fmode, cmode));
2848fd04b83SRoger A. Faulkner }
2858fd04b83SRoger A. Faulkner 
2867c478bd9Sstevel@tonic-gate #if defined(_ILP32) || defined(_SYSCALL32_IMPL)
2877c478bd9Sstevel@tonic-gate /*
2888fd04b83SRoger A. Faulkner  * Open for large files in 32-bit environment. Sets the FOFFMAX flag.
2897c478bd9Sstevel@tonic-gate  */
2907c478bd9Sstevel@tonic-gate int
2917c478bd9Sstevel@tonic-gate openat64(int fd, char *path, int fmode, int cmode)
2927c478bd9Sstevel@tonic-gate {
2937c478bd9Sstevel@tonic-gate 	return (copen(fd, path, OPENMODE64(fmode), cmode));
2947c478bd9Sstevel@tonic-gate }
2957c478bd9Sstevel@tonic-gate 
2968fd04b83SRoger A. Faulkner int
2978fd04b83SRoger A. Faulkner open64(char *path, int fmode, int cmode)
2988fd04b83SRoger A. Faulkner {
2998fd04b83SRoger A. Faulkner 	return (openat64(AT_FDCWD, path, fmode, cmode));
3008fd04b83SRoger A. Faulkner }
3018fd04b83SRoger A. Faulkner 
3027c478bd9Sstevel@tonic-gate #endif	/* _ILP32 || _SYSCALL32_IMPL */
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
3057c478bd9Sstevel@tonic-gate /*
3068fd04b83SRoger A. Faulkner  * Open for 32-bit compatibility on 64-bit kernel
3077c478bd9Sstevel@tonic-gate  */
3087c478bd9Sstevel@tonic-gate int
3097c478bd9Sstevel@tonic-gate openat32(int fd, char *path, int fmode, int cmode)
3107c478bd9Sstevel@tonic-gate {
3117c478bd9Sstevel@tonic-gate 	return (copen(fd, path, OPENMODE32(fmode), cmode));
3127c478bd9Sstevel@tonic-gate }
313da6c28aaSamw 
314da6c28aaSamw int
3158fd04b83SRoger A. Faulkner open32(char *path, int fmode, int cmode)
316da6c28aaSamw {
3178fd04b83SRoger A. Faulkner 	return (openat32(AT_FDCWD, path, fmode, cmode));
318da6c28aaSamw }
3198fd04b83SRoger A. Faulkner 
3208fd04b83SRoger A. Faulkner #endif	/* _SYSCALL32_IMPL */
321