xref: /titanic_41/usr/src/uts/common/syscall/access.c (revision 70818f5837509317d1f5dac4d82d7b5a2d547c29)
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/cred_impl.h>
41 #include <sys/systm.h>
42 #include <sys/errno.h>
43 #include <sys/pathname.h>
44 #include <sys/vnode.h>
45 #include <sys/uio.h>
46 #include <sys/cmn_err.h>
47 #include <sys/debug.h>
48 #include <sys/file.h>
49 #include <fs/fs_subr.h>
50 #include <c2/audit.h>
51 
52 /*
53  * Determine accessibility of file.
54  */
55 
56 #define	E_OK	010	/* use effective ids */
57 #define	R_OK	004
58 #define	W_OK	002
59 #define	X_OK	001
60 
61 static int
62 caccess(char *fname, int fmode, vnode_t *startvp)
63 {
64 	vnode_t *vp;
65 	cred_t *tmpcr;
66 	int error;
67 	int mode;
68 	int eok;
69 	cred_t *cr;
70 	int estale_retry = 0;
71 
72 	if (fmode & ~(E_OK|R_OK|W_OK|X_OK))
73 		return (set_errno(EINVAL));
74 
75 	mode = ((fmode & (R_OK|W_OK|X_OK)) << 6);
76 
77 	cr = CRED();
78 
79 	/* OK to use effective uid/gid, i.e., no need to crdup(CRED())? */
80 	eok = (fmode & E_OK) ||
81 	    (cr->cr_uid == cr->cr_ruid && cr->cr_gid == cr->cr_rgid);
82 
83 	if (eok)
84 		tmpcr = cr;
85 	else {
86 		tmpcr = crdup(cr);
87 		tmpcr->cr_uid = cr->cr_ruid;
88 		tmpcr->cr_gid = cr->cr_rgid;
89 		tmpcr->cr_ruid = cr->cr_uid;
90 		tmpcr->cr_rgid = cr->cr_gid;
91 	}
92 
93 lookup:
94 	if (error = lookupnameat(fname, UIO_USERSPACE, FOLLOW, NULLVPP, &vp,
95 	    startvp)) {
96 		if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
97 			goto lookup;
98 		if (!eok)
99 			crfree(tmpcr);
100 		return (set_errno(error));
101 	}
102 
103 	if (mode) {
104 		error = VOP_ACCESS(vp, mode, 0, tmpcr, NULL);
105 		if (error) {
106 			if ((error == ESTALE) &&
107 			    fs_need_estale_retry(estale_retry++)) {
108 				VN_RELE(vp);
109 				goto lookup;
110 			}
111 			(void) set_errno(error);
112 		}
113 	}
114 
115 	if (!eok)
116 		crfree(tmpcr);
117 	VN_RELE(vp);
118 	return (error);
119 }
120 
121 int
122 access(char *fname, int fmode)
123 {
124 	return (caccess(fname, fmode, NULL));
125 }
126 
127 int
128 accessat(int fd, char *fname, int fmode)
129 {
130 	file_t *dirfp;
131 	vnode_t *dirvp;
132 	int error;
133 	char startchar;
134 
135 	if (fd == AT_FDCWD && fname == NULL)
136 		return (set_errno(EFAULT));
137 
138 	if (fname != NULL) {
139 		if (copyin(fname, &startchar, sizeof (char)))
140 			return (set_errno(EFAULT));
141 	} else
142 		startchar = '\0';
143 
144 	if (fd == AT_FDCWD) {
145 		dirvp = NULL;
146 	} else {
147 		if (startchar != '/') {
148 			if ((dirfp = getf(fd)) == NULL) {
149 				return (set_errno(EBADF));
150 			}
151 			dirvp = dirfp->f_vnode;
152 			VN_HOLD(dirvp);
153 			releasef(fd);
154 		} else {
155 			dirvp = NULL;
156 		}
157 	}
158 
159 	if (audit_active)
160 		audit_setfsat_path(1);
161 
162 	error = caccess(fname, fmode, dirvp);
163 	if (dirvp != NULL)
164 		VN_RELE(dirvp);
165 
166 	return (error);
167 }
168