xref: /onnv-gate/usr/src/uts/common/syscall/access.c (revision 12789:82cffaae72d5)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52051Sprabahar  * Common Development and Distribution License (the "License").
62051Sprabahar  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21*12789SRoger.Faulkner@Oracle.COM 
220Sstevel@tonic-gate /*
2312689SBrent.Paulson@Oracle.COM  * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
270Sstevel@tonic-gate /*	  All Rights Reserved  	*/
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
310Sstevel@tonic-gate  * under license from the Regents of the University of California.
320Sstevel@tonic-gate  */
330Sstevel@tonic-gate 
340Sstevel@tonic-gate #include <sys/param.h>
350Sstevel@tonic-gate #include <sys/isa_defs.h>
360Sstevel@tonic-gate #include <sys/types.h>
370Sstevel@tonic-gate #include <sys/sysmacros.h>
380Sstevel@tonic-gate #include <sys/cred_impl.h>
390Sstevel@tonic-gate #include <sys/systm.h>
400Sstevel@tonic-gate #include <sys/errno.h>
410Sstevel@tonic-gate #include <sys/pathname.h>
420Sstevel@tonic-gate #include <sys/vnode.h>
430Sstevel@tonic-gate #include <sys/uio.h>
440Sstevel@tonic-gate #include <sys/cmn_err.h>
450Sstevel@tonic-gate #include <sys/debug.h>
463855Ssn199410 #include <sys/file.h>
472051Sprabahar #include <fs/fs_subr.h>
483855Ssn199410 #include <c2/audit.h>
499880SSumanth.Naropanth@Sun.COM #include <sys/fcntl.h>
500Sstevel@tonic-gate 
510Sstevel@tonic-gate /*
520Sstevel@tonic-gate  * Determine accessibility of file.
530Sstevel@tonic-gate  */
540Sstevel@tonic-gate 
550Sstevel@tonic-gate #define	E_OK	010	/* use effective ids */
560Sstevel@tonic-gate #define	R_OK	004
570Sstevel@tonic-gate #define	W_OK	002
580Sstevel@tonic-gate #define	X_OK	001
590Sstevel@tonic-gate 
603855Ssn199410 static int
caccess(char * fname,int fmode,vnode_t * startvp)613855Ssn199410 caccess(char *fname, int fmode, vnode_t *startvp)
620Sstevel@tonic-gate {
630Sstevel@tonic-gate 	vnode_t *vp;
640Sstevel@tonic-gate 	cred_t *tmpcr;
650Sstevel@tonic-gate 	int error;
660Sstevel@tonic-gate 	int mode;
670Sstevel@tonic-gate 	int eok;
680Sstevel@tonic-gate 	cred_t *cr;
692051Sprabahar 	int estale_retry = 0;
700Sstevel@tonic-gate 
710Sstevel@tonic-gate 	if (fmode & ~(E_OK|R_OK|W_OK|X_OK))
72*12789SRoger.Faulkner@Oracle.COM 		return (EINVAL);
730Sstevel@tonic-gate 
740Sstevel@tonic-gate 	mode = ((fmode & (R_OK|W_OK|X_OK)) << 6);
750Sstevel@tonic-gate 
760Sstevel@tonic-gate 	cr = CRED();
770Sstevel@tonic-gate 
780Sstevel@tonic-gate 	/* OK to use effective uid/gid, i.e., no need to crdup(CRED())? */
790Sstevel@tonic-gate 	eok = (fmode & E_OK) ||
805753Sgww 	    (cr->cr_uid == cr->cr_ruid && cr->cr_gid == cr->cr_rgid);
810Sstevel@tonic-gate 
820Sstevel@tonic-gate 	if (eok)
830Sstevel@tonic-gate 		tmpcr = cr;
840Sstevel@tonic-gate 	else {
850Sstevel@tonic-gate 		tmpcr = crdup(cr);
860Sstevel@tonic-gate 		tmpcr->cr_uid = cr->cr_ruid;
870Sstevel@tonic-gate 		tmpcr->cr_gid = cr->cr_rgid;
880Sstevel@tonic-gate 		tmpcr->cr_ruid = cr->cr_uid;
890Sstevel@tonic-gate 		tmpcr->cr_rgid = cr->cr_gid;
900Sstevel@tonic-gate 	}
910Sstevel@tonic-gate 
920Sstevel@tonic-gate lookup:
939668SCasper.Dik@Sun.COM 	if (error = lookupnameatcred(fname, UIO_USERSPACE, FOLLOW, NULLVPP, &vp,
949668SCasper.Dik@Sun.COM 	    startvp, tmpcr)) {
952051Sprabahar 		if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
960Sstevel@tonic-gate 			goto lookup;
970Sstevel@tonic-gate 		if (!eok)
980Sstevel@tonic-gate 			crfree(tmpcr);
99*12789SRoger.Faulkner@Oracle.COM 		return (error);
1000Sstevel@tonic-gate 	}
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate 	if (mode) {
1035331Samw 		error = VOP_ACCESS(vp, mode, 0, tmpcr, NULL);
1040Sstevel@tonic-gate 		if (error) {
1052051Sprabahar 			if ((error == ESTALE) &&
1062051Sprabahar 			    fs_need_estale_retry(estale_retry++)) {
1070Sstevel@tonic-gate 				VN_RELE(vp);
1080Sstevel@tonic-gate 				goto lookup;
1090Sstevel@tonic-gate 			}
1100Sstevel@tonic-gate 		}
1110Sstevel@tonic-gate 	}
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate 	if (!eok)
1140Sstevel@tonic-gate 		crfree(tmpcr);
1150Sstevel@tonic-gate 	VN_RELE(vp);
1160Sstevel@tonic-gate 	return (error);
1170Sstevel@tonic-gate }
1183855Ssn199410 
1193855Ssn199410 int
faccessat(int fd,char * fname,int fmode,int flag)1209880SSumanth.Naropanth@Sun.COM faccessat(int fd, char *fname, int fmode, int flag)
1213855Ssn199410 {
122*12789SRoger.Faulkner@Oracle.COM 	vnode_t *startvp;
1233855Ssn199410 	int error;
1243855Ssn199410 
1259880SSumanth.Naropanth@Sun.COM 	if ((flag & ~AT_EACCESS) != 0)
1269880SSumanth.Naropanth@Sun.COM 		return (set_errno(EINVAL));
1279880SSumanth.Naropanth@Sun.COM 
128*12789SRoger.Faulkner@Oracle.COM 	if (fname == NULL)
129*12789SRoger.Faulkner@Oracle.COM 		return (set_errno(EFAULT));
130*12789SRoger.Faulkner@Oracle.COM 	if ((error = fgetstartvp(fd, fname, &startvp)) != 0)
131*12789SRoger.Faulkner@Oracle.COM 		return (set_errno(error));
132*12789SRoger.Faulkner@Oracle.COM 	if (AU_AUDITING() && startvp != NULL)
1333855Ssn199410 		audit_setfsat_path(1);
1343855Ssn199410 
1359880SSumanth.Naropanth@Sun.COM 	/* Do not allow E_OK unless AT_EACCESS flag is set */
1369880SSumanth.Naropanth@Sun.COM 	fmode &= ~E_OK;
1379880SSumanth.Naropanth@Sun.COM 	if (flag & AT_EACCESS)
1389880SSumanth.Naropanth@Sun.COM 		fmode |= E_OK;
1399880SSumanth.Naropanth@Sun.COM 
140*12789SRoger.Faulkner@Oracle.COM 	error = caccess(fname, fmode, startvp);
141*12789SRoger.Faulkner@Oracle.COM 	if (startvp != NULL)
142*12789SRoger.Faulkner@Oracle.COM 		VN_RELE(startvp);
143*12789SRoger.Faulkner@Oracle.COM 	if (error)
144*12789SRoger.Faulkner@Oracle.COM 		return (set_errno(error));
145*12789SRoger.Faulkner@Oracle.COM 	return (0);
1463855Ssn199410 }
14711798SRoger.Faulkner@Sun.COM 
14811798SRoger.Faulkner@Sun.COM int
access(char * fname,int fmode)14911798SRoger.Faulkner@Sun.COM access(char *fname, int fmode)
15011798SRoger.Faulkner@Sun.COM {
15111798SRoger.Faulkner@Sun.COM 	return (faccessat(AT_FDCWD, fname, fmode, 0));
15211798SRoger.Faulkner@Sun.COM }
153