xref: /illumos-gate/usr/src/uts/common/syscall/open.c (revision 3e95bd4ab92abca814bd28e854607d1975c7dc88)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
24  */
25 
26 /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
27 /*	  All Rights Reserved  	*/
28 
29 /*
30  * Portions of this source code were derived from Berkeley 4.3 BSD
31  * under license from the Regents of the University of California.
32  */
33 
34 #include <sys/param.h>
35 #include <sys/isa_defs.h>
36 #include <sys/types.h>
37 #include <sys/sysmacros.h>
38 #include <sys/user.h>
39 #include <sys/systm.h>
40 #include <sys/errno.h>
41 #include <sys/fcntl.h>
42 #include <sys/stat.h>
43 #include <sys/vnode.h>
44 #include <sys/vfs.h>
45 #include <sys/file.h>
46 #include <sys/mode.h>
47 #include <sys/uio.h>
48 #include <sys/debug.h>
49 #include <c2/audit.h>
50 
51 /*
52  * Common code for openat().  Check permissions, allocate an open
53  * file structure, and call the device open routine (if any).
54  */
55 
56 static int
57 copen(int startfd, char *fname, int filemode, int createmode)
58 {
59 	struct pathname pn;
60 	vnode_t *vp, *sdvp;
61 	file_t *fp, *startfp;
62 	enum vtype type;
63 	int error;
64 	int fd, dupfd;
65 	vnode_t *startvp;
66 	proc_t *p = curproc;
67 	uio_seg_t seg = UIO_USERSPACE;
68 	char *open_filename = fname;
69 	uint32_t auditing = AU_AUDITING();
70 
71 	if (startfd == AT_FDCWD) {
72 		/*
73 		 * Regular open()
74 		 */
75 		startvp = NULL;
76 	} else {
77 		/*
78 		 * We're here via openat()
79 		 */
80 		char startchar;
81 
82 		if (copyin(fname, &startchar, sizeof (char)))
83 			return (set_errno(EFAULT));
84 
85 		/*
86 		 * if startchar is / then startfd is ignored
87 		 */
88 		if (startchar == '/')
89 			startvp = NULL;
90 		else {
91 			if ((startfp = getf(startfd)) == NULL)
92 				return (set_errno(EBADF));
93 			startvp = startfp->f_vnode;
94 			VN_HOLD(startvp);
95 			releasef(startfd);
96 		}
97 	}
98 
99 	/*
100 	 * Handle openattrdirat request
101 	 */
102 	if (filemode & FXATTRDIROPEN) {
103 		if (auditing)
104 			audit_setfsat_path(1);
105 
106 		if (error = lookupnameat(fname, seg, FOLLOW,
107 		    NULLVPP, &vp, startvp))
108 			return (set_errno(error));
109 		if (startvp) {
110 			VN_RELE(startvp);
111 			startvp = NULL;
112 		}
113 
114 		startvp = vp;
115 	}
116 
117 	/*
118 	 * Do we need to go into extended attribute space?
119 	 */
120 	if (filemode & (FXATTR|FXATTRDIROPEN)) {
121 		vattr_t vattr;
122 
123 		/*
124 		 * Make sure we have a valid request.
125 		 * We must either have a real fd or AT_FDCWD
126 		 */
127 
128 		if (startfd != AT_FDCWD && startvp == NULL) {
129 			error = EINVAL;
130 			goto out;
131 		}
132 
133 		if (error = pn_get(fname, UIO_USERSPACE, &pn)) {
134 			goto out;
135 		}
136 
137 		if (startfd == AT_FDCWD && !(filemode & FXATTRDIROPEN)) {
138 			mutex_enter(&p->p_lock);
139 			startvp = PTOU(p)->u_cdir;
140 			VN_HOLD(startvp);
141 			mutex_exit(&p->p_lock);
142 		}
143 
144 		/*
145 		 * In order to access hidden attribute directory the
146 		 * user must be able to stat() the file
147 		 */
148 
149 		vattr.va_mask = AT_ALL;
150 		if (error = VOP_GETATTR(startvp, &vattr, 0, CRED(), NULL)) {
151 			pn_free(&pn);
152 			goto out;
153 		}
154 
155 		if ((startvp->v_vfsp->vfs_flag & VFS_XATTR) != 0 ||
156 		    vfs_has_feature(startvp->v_vfsp, VFSFT_SYSATTR_VIEWS)) {
157 			error = VOP_LOOKUP(startvp, "", &sdvp, &pn,
158 			    (filemode & FXATTRDIROPEN) ? LOOKUP_XATTR :
159 			    LOOKUP_XATTR|CREATE_XATTR_DIR, rootvp, CRED(),
160 			    NULL, NULL, NULL);
161 		} else {
162 			error = EINVAL;
163 		}
164 
165 		/*
166 		 * For openattrdirat use "." as filename to open
167 		 * as part of vn_openat()
168 		 */
169 		if (error == 0 && (filemode & FXATTRDIROPEN)) {
170 			open_filename = ".";
171 			seg = UIO_SYSSPACE;
172 		}
173 
174 		pn_free(&pn);
175 		if (error != 0)
176 			goto out;
177 
178 		VN_RELE(startvp);
179 		startvp = sdvp;
180 	}
181 
182 	if ((filemode & (FREAD|FWRITE|FXATTRDIROPEN)) != 0) {
183 		if ((filemode & (FNONBLOCK|FNDELAY)) == (FNONBLOCK|FNDELAY))
184 			filemode &= ~FNDELAY;
185 		error = falloc((vnode_t *)NULL, filemode, &fp, &fd);
186 		if (error == 0) {
187 			if (auditing)
188 				audit_setfsat_path(1);
189 			/*
190 			 * Last arg is a don't-care term if
191 			 * !(filemode & FCREAT).
192 			 */
193 
194 			error = vn_openat(open_filename, seg, filemode,
195 			    (int)(createmode & MODEMASK),
196 			    &vp, CRCREAT, PTOU(curproc)->u_cmask,
197 			    startvp, fd);
198 
199 			if (startvp != NULL)
200 				VN_RELE(startvp);
201 			if (error == 0) {
202 				if ((vp->v_flag & VDUP) == 0) {
203 					fp->f_vnode = vp;
204 					mutex_exit(&fp->f_tlock);
205 					/*
206 					 * We must now fill in the slot
207 					 * falloc reserved.
208 					 */
209 					setf(fd, fp);
210 					return (fd);
211 				} else {
212 					/*
213 					 * Special handling for /dev/fd.
214 					 * Give up the file pointer
215 					 * and dup the indicated file descriptor
216 					 * (in v_rdev). This is ugly, but I've
217 					 * seen worse.
218 					 */
219 					unfalloc(fp);
220 					dupfd = getminor(vp->v_rdev);
221 					type = vp->v_type;
222 					mutex_enter(&vp->v_lock);
223 					vp->v_flag &= ~VDUP;
224 					mutex_exit(&vp->v_lock);
225 					VN_RELE(vp);
226 					if (type != VCHR)
227 						return (set_errno(EINVAL));
228 					if ((fp = getf(dupfd)) == NULL) {
229 						setf(fd, NULL);
230 						return (set_errno(EBADF));
231 					}
232 					mutex_enter(&fp->f_tlock);
233 					fp->f_count++;
234 					mutex_exit(&fp->f_tlock);
235 					setf(fd, fp);
236 					releasef(dupfd);
237 				}
238 				return (fd);
239 			} else {
240 				setf(fd, NULL);
241 				unfalloc(fp);
242 				return (set_errno(error));
243 			}
244 		}
245 	} else {
246 		error = EINVAL;
247 	}
248 out:
249 	if (startvp != NULL)
250 		VN_RELE(startvp);
251 	return (set_errno(error));
252 }
253 
254 #define	OPENMODE32(fmode)	((int)((fmode)-FOPEN))
255 #define	OPENMODE64(fmode)	(OPENMODE32(fmode) | FOFFMAX)
256 #define	OPENMODEATTRDIR		FXATTRDIROPEN
257 #ifdef _LP64
258 #define	OPENMODE(fmode)		OPENMODE64(fmode)
259 #else
260 #define	OPENMODE		OPENMODE32
261 #endif
262 
263 /*
264  * Open a file.
265  */
266 int
267 openat(int fd, char *path, int fmode, int cmode)
268 {
269 	return (copen(fd, path, OPENMODE(fmode), cmode));
270 }
271 
272 int
273 open(char *path, int fmode, int cmode)
274 {
275 	return (openat(AT_FDCWD, path, fmode, cmode));
276 }
277 
278 #if defined(_ILP32) || defined(_SYSCALL32_IMPL)
279 /*
280  * Open for large files in 32-bit environment. Sets the FOFFMAX flag.
281  */
282 int
283 openat64(int fd, char *path, int fmode, int cmode)
284 {
285 	return (copen(fd, path, OPENMODE64(fmode), cmode));
286 }
287 
288 int
289 open64(char *path, int fmode, int cmode)
290 {
291 	return (openat64(AT_FDCWD, path, fmode, cmode));
292 }
293 
294 #endif	/* _ILP32 || _SYSCALL32_IMPL */
295 
296 #ifdef _SYSCALL32_IMPL
297 /*
298  * Open for 32-bit compatibility on 64-bit kernel
299  */
300 int
301 openat32(int fd, char *path, int fmode, int cmode)
302 {
303 	return (copen(fd, path, OPENMODE32(fmode), cmode));
304 }
305 
306 int
307 open32(char *path, int fmode, int cmode)
308 {
309 	return (openat32(AT_FDCWD, path, fmode, cmode));
310 }
311 
312 #endif	/* _SYSCALL32_IMPL */
313