xref: /illumos-gate/usr/src/uts/common/syscall/open.c (revision 99dda20867d903eec23291ba1ecb18a82d70096b)
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  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
35 
36 #include <sys/param.h>
37 #include <sys/isa_defs.h>
38 #include <sys/types.h>
39 #include <sys/sysmacros.h>
40 #include <sys/user.h>
41 #include <sys/systm.h>
42 #include <sys/errno.h>
43 #include <sys/fcntl.h>
44 #include <sys/stat.h>
45 #include <sys/vnode.h>
46 #include <sys/vfs.h>
47 #include <sys/file.h>
48 #include <sys/mode.h>
49 #include <sys/uio.h>
50 #include <sys/debug.h>
51 #include <c2/audit.h>
52 #include <sys/cmn_err.h>
53 
54 /*
55  * Common code for open()/openat() and creat().  Check permissions, allocate
56  * an open file structure, and call the device open routine (if any).
57  */
58 
59 static int
60 copen(int startfd, char *fname, int filemode, int createmode)
61 {
62 	struct pathname pn;
63 	vnode_t *vp, *sdvp;
64 	file_t *fp, *startfp;
65 	enum vtype type;
66 	int error;
67 	int fd, dupfd;
68 	vnode_t *startvp;
69 	proc_t *p = curproc;
70 	uio_seg_t seg = UIO_USERSPACE;
71 	char *open_filename = fname;
72 
73 	if (startfd == AT_FDCWD) {
74 		/*
75 		 * Regular open()
76 		 */
77 		startvp = NULL;
78 	} else {
79 		/*
80 		 * We're here via openat()
81 		 */
82 		char startchar;
83 
84 		if (copyin(fname, &startchar, sizeof (char)))
85 			return (set_errno(EFAULT));
86 
87 		/*
88 		 * if startchar is / then startfd is ignored
89 		 */
90 		if (startchar == '/')
91 			startvp = NULL;
92 		else {
93 			if ((startfp = getf(startfd)) == NULL)
94 				return (set_errno(EBADF));
95 			startvp = startfp->f_vnode;
96 			VN_HOLD(startvp);
97 			releasef(startfd);
98 		}
99 	}
100 
101 	/*
102 	 * Handle openattrdirat request
103 	 */
104 	if (filemode & FXATTRDIROPEN) {
105 			if (audit_active)
106 				audit_setfsat_path(1);
107 
108 		if (error = lookupnameat(fname, seg, FOLLOW,
109 		    NULLVPP, &vp, startvp))
110 			return (set_errno(error));
111 		if (startvp) {
112 			VN_RELE(startvp);
113 			startvp = NULL;
114 		}
115 
116 		startvp = vp;
117 	}
118 
119 	/*
120 	 * Do we need to go into extended attribute space?
121 	 */
122 	if (filemode & (FXATTR|FXATTRDIROPEN)) {
123 		vattr_t vattr;
124 
125 		/*
126 		 * Make sure we have a valid request.
127 		 * We must either have a real fd or AT_FDCWD
128 		 */
129 
130 		if (startfd != AT_FDCWD && startvp == NULL) {
131 			error = EINVAL;
132 			goto out;
133 		}
134 
135 		if (error = pn_get(fname, UIO_USERSPACE, &pn)) {
136 			goto out;
137 		}
138 
139 		if (startfd == AT_FDCWD && !(filemode & FXATTRDIROPEN)) {
140 			mutex_enter(&p->p_lock);
141 			startvp = PTOU(p)->u_cdir;
142 			VN_HOLD(startvp);
143 			mutex_exit(&p->p_lock);
144 		}
145 
146 		/*
147 		 * In order to access hidden attribute directory the
148 		 * user must be able to stat() the file
149 		 */
150 
151 		vattr.va_mask = AT_ALL;
152 		if (error = VOP_GETATTR(startvp, &vattr, 0, CRED(), NULL)) {
153 			pn_free(&pn);
154 			goto out;
155 		}
156 
157 		if ((startvp->v_vfsp->vfs_flag & VFS_XATTR) != 0 ||
158 		    vfs_has_feature(startvp->v_vfsp, VFSFT_XVATTR)) {
159 			error = VOP_LOOKUP(startvp, "", &sdvp, &pn,
160 			    LOOKUP_XATTR|CREATE_XATTR_DIR, rootvp, CRED(),
161 			    NULL, NULL, NULL);
162 		} else {
163 			error = EINVAL;
164 		}
165 
166 		/*
167 		 * For openattrdirat use "." as filename to open
168 		 * as part of vn_openat()
169 		 */
170 		if (error == 0 && (filemode & FXATTRDIROPEN)) {
171 			open_filename = ".";
172 			seg = UIO_SYSSPACE;
173 		}
174 
175 		pn_free(&pn);
176 		if (error != 0)
177 			goto out;
178 
179 		VN_RELE(startvp);
180 		startvp = sdvp;
181 	}
182 
183 	if ((filemode & (FREAD|FWRITE|FXATTRDIROPEN)) != 0) {
184 		if ((filemode & (FNONBLOCK|FNDELAY)) == (FNONBLOCK|FNDELAY))
185 			filemode &= ~FNDELAY;
186 		error = falloc((vnode_t *)NULL, filemode, &fp, &fd);
187 		if (error == 0) {
188 			if (audit_active)
189 				audit_setfsat_path(1);
190 			/*
191 			 * Last arg is a don't-care term if
192 			 * !(filemode & FCREAT).
193 			 */
194 
195 			error = vn_openat(open_filename, seg, filemode,
196 			    (int)(createmode & MODEMASK),
197 			    &vp, CRCREAT, PTOU(curproc)->u_cmask,
198 			    startvp, fd);
199 
200 			if (startvp != NULL)
201 				VN_RELE(startvp);
202 			if (error == 0) {
203 				if (audit_active)
204 					audit_copen(fd, fp, vp);
205 				if ((vp->v_flag & VDUP) == 0) {
206 					fp->f_vnode = vp;
207 					mutex_exit(&fp->f_tlock);
208 					/*
209 					 * We must now fill in the slot
210 					 * falloc reserved.
211 					 */
212 					setf(fd, fp);
213 					return (fd);
214 				} else {
215 					/*
216 					 * Special handling for /dev/fd.
217 					 * Give up the file pointer
218 					 * and dup the indicated file descriptor
219 					 * (in v_rdev). This is ugly, but I've
220 					 * seen worse.
221 					 */
222 					unfalloc(fp);
223 					dupfd = getminor(vp->v_rdev);
224 					type = vp->v_type;
225 					mutex_enter(&vp->v_lock);
226 					vp->v_flag &= ~VDUP;
227 					mutex_exit(&vp->v_lock);
228 					VN_RELE(vp);
229 					if (type != VCHR)
230 						return (set_errno(EINVAL));
231 					if ((fp = getf(dupfd)) == NULL) {
232 						setf(fd, NULL);
233 						return (set_errno(EBADF));
234 					}
235 					mutex_enter(&fp->f_tlock);
236 					fp->f_count++;
237 					mutex_exit(&fp->f_tlock);
238 					setf(fd, fp);
239 					releasef(dupfd);
240 				}
241 				return (fd);
242 			} else {
243 				setf(fd, NULL);
244 				unfalloc(fp);
245 				return (set_errno(error));
246 			}
247 		}
248 	} else {
249 		error = EINVAL;
250 	}
251 out:
252 	if (startvp != NULL)
253 		VN_RELE(startvp);
254 	return (set_errno(error));
255 }
256 
257 #define	OPENMODE32(fmode)	((int)((fmode)-FOPEN))
258 #define	CREATMODE32		(FWRITE|FCREAT|FTRUNC)
259 #define	OPENMODE64(fmode)	(OPENMODE32(fmode) | FOFFMAX)
260 #define	OPENMODEATTRDIR		FXATTRDIROPEN
261 #define	CREATMODE64		(CREATMODE32 | FOFFMAX)
262 #ifdef _LP64
263 #define	OPENMODE(fmode)		OPENMODE64(fmode)
264 #define	CREATMODE		CREATMODE64
265 #else
266 #define	OPENMODE		OPENMODE32
267 #define	CREATMODE		CREATMODE32
268 #endif
269 
270 /*
271  * Open a file.
272  */
273 int
274 open(char *fname, int fmode, int cmode)
275 {
276 	return (copen(AT_FDCWD, fname, OPENMODE(fmode), cmode));
277 }
278 
279 /*
280  * Create a file.
281  */
282 int
283 creat(char *fname, int cmode)
284 {
285 	return (copen(AT_FDCWD, fname, CREATMODE, cmode));
286 }
287 
288 int
289 openat(int fd, char *path, int fmode, int cmode)
290 {
291 	return (copen(fd, path, OPENMODE(fmode), cmode));
292 }
293 
294 #if defined(_ILP32) || defined(_SYSCALL32_IMPL)
295 /*
296  * Open and Creat for large files in 32-bit environment. Sets the FOFFMAX flag.
297  */
298 int
299 open64(char *fname, int fmode, int cmode)
300 {
301 	return (copen(AT_FDCWD, fname, OPENMODE64(fmode), cmode));
302 }
303 
304 int
305 creat64(char *fname, int cmode)
306 {
307 	return (copen(AT_FDCWD, fname, CREATMODE64, cmode));
308 }
309 
310 int
311 openat64(int fd, char *path, int fmode, int cmode)
312 {
313 	return (copen(fd, path, OPENMODE64(fmode), cmode));
314 }
315 
316 #endif	/* _ILP32 || _SYSCALL32_IMPL */
317 
318 #ifdef _SYSCALL32_IMPL
319 /*
320  * Open and Creat for 32-bit compatibility on 64-bit kernel
321  */
322 int
323 open32(char *fname, int fmode, int cmode)
324 {
325 	return (copen(AT_FDCWD, fname, OPENMODE32(fmode), cmode));
326 }
327 
328 int
329 creat32(char *fname, int cmode)
330 {
331 	return (copen(AT_FDCWD, fname, CREATMODE32, cmode));
332 }
333 
334 int
335 openat32(int fd, char *path, int fmode, int cmode)
336 {
337 	return (copen(fd, path, OPENMODE32(fmode), cmode));
338 }
339 
340 #endif	/* _SYSCALL32_IMPL */
341 
342 /*
343  * Special interface to open hidden attribute directory.
344  */
345 int
346 openattrdirat(int fd, char *fname)
347 {
348 	return (copen(fd, fname, OPENMODEATTRDIR, 0));
349 }
350