xref: /titanic_52/usr/src/uts/common/syscall/open.c (revision 7eea693d6b672899726e75993fddc4e95b52647f)
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 2008 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 			    (filemode & FXATTRDIROPEN) ? LOOKUP_XATTR :
161 			    LOOKUP_XATTR|CREATE_XATTR_DIR, rootvp, CRED(),
162 			    NULL, NULL, NULL);
163 		} else {
164 			error = EINVAL;
165 		}
166 
167 		/*
168 		 * For openattrdirat use "." as filename to open
169 		 * as part of vn_openat()
170 		 */
171 		if (error == 0 && (filemode & FXATTRDIROPEN)) {
172 			open_filename = ".";
173 			seg = UIO_SYSSPACE;
174 		}
175 
176 		pn_free(&pn);
177 		if (error != 0)
178 			goto out;
179 
180 		VN_RELE(startvp);
181 		startvp = sdvp;
182 	}
183 
184 	if ((filemode & (FREAD|FWRITE|FXATTRDIROPEN)) != 0) {
185 		if ((filemode & (FNONBLOCK|FNDELAY)) == (FNONBLOCK|FNDELAY))
186 			filemode &= ~FNDELAY;
187 		error = falloc((vnode_t *)NULL, filemode, &fp, &fd);
188 		if (error == 0) {
189 			if (audit_active)
190 				audit_setfsat_path(1);
191 			/*
192 			 * Last arg is a don't-care term if
193 			 * !(filemode & FCREAT).
194 			 */
195 
196 			error = vn_openat(open_filename, seg, filemode,
197 			    (int)(createmode & MODEMASK),
198 			    &vp, CRCREAT, PTOU(curproc)->u_cmask,
199 			    startvp, fd);
200 
201 			if (startvp != NULL)
202 				VN_RELE(startvp);
203 			if (error == 0) {
204 				if (audit_active)
205 					audit_copen(fd, fp, vp);
206 				if ((vp->v_flag & VDUP) == 0) {
207 					fp->f_vnode = vp;
208 					mutex_exit(&fp->f_tlock);
209 					/*
210 					 * We must now fill in the slot
211 					 * falloc reserved.
212 					 */
213 					setf(fd, fp);
214 					return (fd);
215 				} else {
216 					/*
217 					 * Special handling for /dev/fd.
218 					 * Give up the file pointer
219 					 * and dup the indicated file descriptor
220 					 * (in v_rdev). This is ugly, but I've
221 					 * seen worse.
222 					 */
223 					unfalloc(fp);
224 					dupfd = getminor(vp->v_rdev);
225 					type = vp->v_type;
226 					mutex_enter(&vp->v_lock);
227 					vp->v_flag &= ~VDUP;
228 					mutex_exit(&vp->v_lock);
229 					VN_RELE(vp);
230 					if (type != VCHR)
231 						return (set_errno(EINVAL));
232 					if ((fp = getf(dupfd)) == NULL) {
233 						setf(fd, NULL);
234 						return (set_errno(EBADF));
235 					}
236 					mutex_enter(&fp->f_tlock);
237 					fp->f_count++;
238 					mutex_exit(&fp->f_tlock);
239 					setf(fd, fp);
240 					releasef(dupfd);
241 				}
242 				return (fd);
243 			} else {
244 				setf(fd, NULL);
245 				unfalloc(fp);
246 				return (set_errno(error));
247 			}
248 		}
249 	} else {
250 		error = EINVAL;
251 	}
252 out:
253 	if (startvp != NULL)
254 		VN_RELE(startvp);
255 	return (set_errno(error));
256 }
257 
258 #define	OPENMODE32(fmode)	((int)((fmode)-FOPEN))
259 #define	CREATMODE32		(FWRITE|FCREAT|FTRUNC)
260 #define	OPENMODE64(fmode)	(OPENMODE32(fmode) | FOFFMAX)
261 #define	OPENMODEATTRDIR		FXATTRDIROPEN
262 #define	CREATMODE64		(CREATMODE32 | FOFFMAX)
263 #ifdef _LP64
264 #define	OPENMODE(fmode)		OPENMODE64(fmode)
265 #define	CREATMODE		CREATMODE64
266 #else
267 #define	OPENMODE		OPENMODE32
268 #define	CREATMODE		CREATMODE32
269 #endif
270 
271 /*
272  * Open a file.
273  */
274 int
275 open(char *fname, int fmode, int cmode)
276 {
277 	return (copen(AT_FDCWD, fname, OPENMODE(fmode), cmode));
278 }
279 
280 /*
281  * Create a file.
282  */
283 int
284 creat(char *fname, int cmode)
285 {
286 	return (copen(AT_FDCWD, fname, CREATMODE, cmode));
287 }
288 
289 int
290 openat(int fd, char *path, int fmode, int cmode)
291 {
292 	return (copen(fd, path, OPENMODE(fmode), cmode));
293 }
294 
295 #if defined(_ILP32) || defined(_SYSCALL32_IMPL)
296 /*
297  * Open and Creat for large files in 32-bit environment. Sets the FOFFMAX flag.
298  */
299 int
300 open64(char *fname, int fmode, int cmode)
301 {
302 	return (copen(AT_FDCWD, fname, OPENMODE64(fmode), cmode));
303 }
304 
305 int
306 creat64(char *fname, int cmode)
307 {
308 	return (copen(AT_FDCWD, fname, CREATMODE64, cmode));
309 }
310 
311 int
312 openat64(int fd, char *path, int fmode, int cmode)
313 {
314 	return (copen(fd, path, OPENMODE64(fmode), cmode));
315 }
316 
317 #endif	/* _ILP32 || _SYSCALL32_IMPL */
318 
319 #ifdef _SYSCALL32_IMPL
320 /*
321  * Open and Creat for 32-bit compatibility on 64-bit kernel
322  */
323 int
324 open32(char *fname, int fmode, int cmode)
325 {
326 	return (copen(AT_FDCWD, fname, OPENMODE32(fmode), cmode));
327 }
328 
329 int
330 creat32(char *fname, int cmode)
331 {
332 	return (copen(AT_FDCWD, fname, CREATMODE32, cmode));
333 }
334 
335 int
336 openat32(int fd, char *path, int fmode, int cmode)
337 {
338 	return (copen(fd, path, OPENMODE32(fmode), cmode));
339 }
340 
341 #endif	/* _SYSCALL32_IMPL */
342 
343 /*
344  * Special interface to open hidden attribute directory.
345  */
346 int
347 openattrdirat(int fd, char *fname)
348 {
349 	return (copen(fd, fname, OPENMODEATTRDIR, 0));
350 }
351