xref: /titanic_50/usr/src/uts/common/syscall/acl.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
28*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate /*
31*7c478bd9Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
32*7c478bd9Sstevel@tonic-gate  * under license from the Regents of the University of California.
33*7c478bd9Sstevel@tonic-gate  */
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
38*7c478bd9Sstevel@tonic-gate #include <sys/isa_defs.h>
39*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
40*7c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
41*7c478bd9Sstevel@tonic-gate #include <sys/cred.h>
42*7c478bd9Sstevel@tonic-gate #include <sys/systm.h>
43*7c478bd9Sstevel@tonic-gate #include <sys/errno.h>
44*7c478bd9Sstevel@tonic-gate #include <sys/fcntl.h>
45*7c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
46*7c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
47*7c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
48*7c478bd9Sstevel@tonic-gate #include <sys/file.h>
49*7c478bd9Sstevel@tonic-gate #include <sys/mode.h>
50*7c478bd9Sstevel@tonic-gate #include <sys/uio.h>
51*7c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
52*7c478bd9Sstevel@tonic-gate #include <sys/filio.h>
53*7c478bd9Sstevel@tonic-gate #include <sys/acl.h>
54*7c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate #include <sys/unistd.h>
57*7c478bd9Sstevel@tonic-gate #include <sys/debug.h>
58*7c478bd9Sstevel@tonic-gate 
59*7c478bd9Sstevel@tonic-gate static int cacl(int cmd, int nentries, void *aclbufp,
60*7c478bd9Sstevel@tonic-gate     vnode_t *vp, int *rv);
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate /*
63*7c478bd9Sstevel@tonic-gate  * Get/Set ACL of a file.
64*7c478bd9Sstevel@tonic-gate  */
65*7c478bd9Sstevel@tonic-gate int
66*7c478bd9Sstevel@tonic-gate acl(const char *fname, int cmd, int nentries, void *aclbufp)
67*7c478bd9Sstevel@tonic-gate {
68*7c478bd9Sstevel@tonic-gate 	struct vnode *vp;
69*7c478bd9Sstevel@tonic-gate 	int error;
70*7c478bd9Sstevel@tonic-gate 	int rv = 0;
71*7c478bd9Sstevel@tonic-gate 
72*7c478bd9Sstevel@tonic-gate 	/* Sanity check arguments */
73*7c478bd9Sstevel@tonic-gate 	if (fname == NULL)
74*7c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
75*7c478bd9Sstevel@tonic-gate lookup:
76*7c478bd9Sstevel@tonic-gate 	error = lookupname((char *)fname, UIO_USERSPACE, FOLLOW, NULLVPP, &vp);
77*7c478bd9Sstevel@tonic-gate 	if (error) {
78*7c478bd9Sstevel@tonic-gate 		if (error == ESTALE)
79*7c478bd9Sstevel@tonic-gate 			goto lookup;
80*7c478bd9Sstevel@tonic-gate 		return (set_errno(error));
81*7c478bd9Sstevel@tonic-gate 	}
82*7c478bd9Sstevel@tonic-gate 
83*7c478bd9Sstevel@tonic-gate 	error = cacl(cmd, nentries, aclbufp, vp, &rv);
84*7c478bd9Sstevel@tonic-gate 	VN_RELE(vp);
85*7c478bd9Sstevel@tonic-gate 	if (error) {
86*7c478bd9Sstevel@tonic-gate 		if (error == ESTALE)
87*7c478bd9Sstevel@tonic-gate 			goto lookup;
88*7c478bd9Sstevel@tonic-gate 		return (set_errno(error));
89*7c478bd9Sstevel@tonic-gate 	}
90*7c478bd9Sstevel@tonic-gate 	return (rv);
91*7c478bd9Sstevel@tonic-gate }
92*7c478bd9Sstevel@tonic-gate 
93*7c478bd9Sstevel@tonic-gate /*
94*7c478bd9Sstevel@tonic-gate  * Get/Set ACL of a file with facl system call.
95*7c478bd9Sstevel@tonic-gate  */
96*7c478bd9Sstevel@tonic-gate int
97*7c478bd9Sstevel@tonic-gate facl(int fdes, int cmd, int nentries, void *aclbufp)
98*7c478bd9Sstevel@tonic-gate {
99*7c478bd9Sstevel@tonic-gate 	file_t *fp;
100*7c478bd9Sstevel@tonic-gate 	int error;
101*7c478bd9Sstevel@tonic-gate 	int rv = 0;
102*7c478bd9Sstevel@tonic-gate 
103*7c478bd9Sstevel@tonic-gate 	if ((fp = getf(fdes)) == NULL)
104*7c478bd9Sstevel@tonic-gate 		return (set_errno(EBADF));
105*7c478bd9Sstevel@tonic-gate #ifdef C2_AUDIT
106*7c478bd9Sstevel@tonic-gate 	if (fp->f_flag & FREVOKED) {
107*7c478bd9Sstevel@tonic-gate 		releasef(fdes);
108*7c478bd9Sstevel@tonic-gate 		return (set_errno(EBADF));
109*7c478bd9Sstevel@tonic-gate 	}
110*7c478bd9Sstevel@tonic-gate #endif /* C2_AUDIT */
111*7c478bd9Sstevel@tonic-gate 
112*7c478bd9Sstevel@tonic-gate 	error = cacl(cmd, nentries, aclbufp, fp->f_vnode, &rv);
113*7c478bd9Sstevel@tonic-gate 	releasef(fdes);
114*7c478bd9Sstevel@tonic-gate 
115*7c478bd9Sstevel@tonic-gate 	if (error)
116*7c478bd9Sstevel@tonic-gate 		return (set_errno(error));
117*7c478bd9Sstevel@tonic-gate 	return (rv);
118*7c478bd9Sstevel@tonic-gate }
119*7c478bd9Sstevel@tonic-gate 
120*7c478bd9Sstevel@tonic-gate 
121*7c478bd9Sstevel@tonic-gate /*
122*7c478bd9Sstevel@tonic-gate  * Common code for acl() and facl().
123*7c478bd9Sstevel@tonic-gate  */
124*7c478bd9Sstevel@tonic-gate static int
125*7c478bd9Sstevel@tonic-gate cacl(int cmd, int nentries, void *aclbufp, vnode_t *vp, int *rv)
126*7c478bd9Sstevel@tonic-gate {
127*7c478bd9Sstevel@tonic-gate 	int		error;
128*7c478bd9Sstevel@tonic-gate 	int		aclbsize;	/* size of acl list in bytes */
129*7c478bd9Sstevel@tonic-gate 	int		dfaclbsize;	/* size of default acl list in bytes */
130*7c478bd9Sstevel@tonic-gate 	int		numacls;
131*7c478bd9Sstevel@tonic-gate 	caddr_t		uaddrp;
132*7c478bd9Sstevel@tonic-gate 	aclent_t	*aclp, *aaclp;
133*7c478bd9Sstevel@tonic-gate 	vsecattr_t	vsecattr;
134*7c478bd9Sstevel@tonic-gate 
135*7c478bd9Sstevel@tonic-gate 	ASSERT(vp);
136*7c478bd9Sstevel@tonic-gate 
137*7c478bd9Sstevel@tonic-gate 	bzero(&vsecattr, sizeof (vsecattr_t));
138*7c478bd9Sstevel@tonic-gate 
139*7c478bd9Sstevel@tonic-gate 	switch (cmd) {
140*7c478bd9Sstevel@tonic-gate 
141*7c478bd9Sstevel@tonic-gate 	case ACE_GETACLCNT:
142*7c478bd9Sstevel@tonic-gate 	case GETACLCNT:
143*7c478bd9Sstevel@tonic-gate 		if (cmd == GETACLCNT)
144*7c478bd9Sstevel@tonic-gate 			vsecattr.vsa_mask = VSA_ACLCNT | VSA_DFACLCNT;
145*7c478bd9Sstevel@tonic-gate 		else
146*7c478bd9Sstevel@tonic-gate 			vsecattr.vsa_mask = VSA_ACECNT;
147*7c478bd9Sstevel@tonic-gate 		if (error = VOP_GETSECATTR(vp, &vsecattr, 0, CRED()))
148*7c478bd9Sstevel@tonic-gate 			return (error);
149*7c478bd9Sstevel@tonic-gate 		*rv = vsecattr.vsa_aclcnt + vsecattr.vsa_dfaclcnt;
150*7c478bd9Sstevel@tonic-gate 		if (vsecattr.vsa_aclcnt && vsecattr.vsa_aclentp) {
151*7c478bd9Sstevel@tonic-gate 			kmem_free(vsecattr.vsa_aclentp,
152*7c478bd9Sstevel@tonic-gate 			    vsecattr.vsa_aclcnt * sizeof (aclent_t));
153*7c478bd9Sstevel@tonic-gate 		}
154*7c478bd9Sstevel@tonic-gate 		if (vsecattr.vsa_dfaclcnt && vsecattr.vsa_dfaclentp) {
155*7c478bd9Sstevel@tonic-gate 			kmem_free(vsecattr.vsa_dfaclentp,
156*7c478bd9Sstevel@tonic-gate 			    vsecattr.vsa_dfaclcnt * sizeof (aclent_t));
157*7c478bd9Sstevel@tonic-gate 		}
158*7c478bd9Sstevel@tonic-gate 		break;
159*7c478bd9Sstevel@tonic-gate 	case GETACL:
160*7c478bd9Sstevel@tonic-gate 		/*
161*7c478bd9Sstevel@tonic-gate 		 * Minimum ACL size is three entries so might as well
162*7c478bd9Sstevel@tonic-gate 		 * bail out here.
163*7c478bd9Sstevel@tonic-gate 		 */
164*7c478bd9Sstevel@tonic-gate 		if (nentries < 3)
165*7c478bd9Sstevel@tonic-gate 			return (EINVAL);
166*7c478bd9Sstevel@tonic-gate 		/*
167*7c478bd9Sstevel@tonic-gate 		 * NULL output buffer is also a pretty easy bail out.
168*7c478bd9Sstevel@tonic-gate 		 */
169*7c478bd9Sstevel@tonic-gate 		if (aclbufp == NULL)
170*7c478bd9Sstevel@tonic-gate 			return (EFAULT);
171*7c478bd9Sstevel@tonic-gate 		vsecattr.vsa_mask = VSA_ACL | VSA_ACLCNT | VSA_DFACL |
172*7c478bd9Sstevel@tonic-gate 		    VSA_DFACLCNT;
173*7c478bd9Sstevel@tonic-gate 		if (error = VOP_GETSECATTR(vp, &vsecattr, 0, CRED()))
174*7c478bd9Sstevel@tonic-gate 			return (error);
175*7c478bd9Sstevel@tonic-gate 		/* Check user's buffer is big enough */
176*7c478bd9Sstevel@tonic-gate 		numacls = vsecattr.vsa_aclcnt + vsecattr.vsa_dfaclcnt;
177*7c478bd9Sstevel@tonic-gate 		aclbsize = vsecattr.vsa_aclcnt * sizeof (aclent_t);
178*7c478bd9Sstevel@tonic-gate 		dfaclbsize = vsecattr.vsa_dfaclcnt * sizeof (aclent_t);
179*7c478bd9Sstevel@tonic-gate 		if (numacls > nentries) {
180*7c478bd9Sstevel@tonic-gate 			error = ENOSPC;
181*7c478bd9Sstevel@tonic-gate 			goto errout;
182*7c478bd9Sstevel@tonic-gate 		}
183*7c478bd9Sstevel@tonic-gate 		/* Sort the acl & default acl lists */
184*7c478bd9Sstevel@tonic-gate 		if (vsecattr.vsa_aclcnt > 1)
185*7c478bd9Sstevel@tonic-gate 			ksort((caddr_t)vsecattr.vsa_aclentp,
186*7c478bd9Sstevel@tonic-gate 			vsecattr.vsa_aclcnt, sizeof (aclent_t), cmp2acls);
187*7c478bd9Sstevel@tonic-gate 		if (vsecattr.vsa_dfaclcnt > 1)
188*7c478bd9Sstevel@tonic-gate 			ksort((caddr_t)vsecattr.vsa_dfaclentp,
189*7c478bd9Sstevel@tonic-gate 			vsecattr.vsa_dfaclcnt, sizeof (aclent_t), cmp2acls);
190*7c478bd9Sstevel@tonic-gate 		/* Copy out acl's */
191*7c478bd9Sstevel@tonic-gate 		uaddrp = (caddr_t)aclbufp;
192*7c478bd9Sstevel@tonic-gate 		if (aclbsize > 0) {	/* bug #1262490 */
193*7c478bd9Sstevel@tonic-gate 			if (copyout(vsecattr.vsa_aclentp, uaddrp, aclbsize)) {
194*7c478bd9Sstevel@tonic-gate 				error = EFAULT;
195*7c478bd9Sstevel@tonic-gate 				goto errout;
196*7c478bd9Sstevel@tonic-gate 			}
197*7c478bd9Sstevel@tonic-gate 		}
198*7c478bd9Sstevel@tonic-gate 		/* Copy out default acl's */
199*7c478bd9Sstevel@tonic-gate 		if (dfaclbsize > 0) {
200*7c478bd9Sstevel@tonic-gate 			uaddrp += aclbsize;
201*7c478bd9Sstevel@tonic-gate 			if (copyout(vsecattr.vsa_dfaclentp,
202*7c478bd9Sstevel@tonic-gate 			    uaddrp, dfaclbsize)) {
203*7c478bd9Sstevel@tonic-gate 				error = EFAULT;
204*7c478bd9Sstevel@tonic-gate 				goto errout;
205*7c478bd9Sstevel@tonic-gate 			}
206*7c478bd9Sstevel@tonic-gate 		}
207*7c478bd9Sstevel@tonic-gate 		*rv = numacls;
208*7c478bd9Sstevel@tonic-gate 		if (vsecattr.vsa_aclcnt) {
209*7c478bd9Sstevel@tonic-gate 			kmem_free(vsecattr.vsa_aclentp,
210*7c478bd9Sstevel@tonic-gate 			    vsecattr.vsa_aclcnt * sizeof (aclent_t));
211*7c478bd9Sstevel@tonic-gate 		}
212*7c478bd9Sstevel@tonic-gate 		if (vsecattr.vsa_dfaclcnt) {
213*7c478bd9Sstevel@tonic-gate 			kmem_free(vsecattr.vsa_dfaclentp,
214*7c478bd9Sstevel@tonic-gate 			    vsecattr.vsa_dfaclcnt * sizeof (aclent_t));
215*7c478bd9Sstevel@tonic-gate 		}
216*7c478bd9Sstevel@tonic-gate 		break;
217*7c478bd9Sstevel@tonic-gate 
218*7c478bd9Sstevel@tonic-gate 	case ACE_GETACL:
219*7c478bd9Sstevel@tonic-gate 		if (nentries < 3)
220*7c478bd9Sstevel@tonic-gate 			return (EINVAL);
221*7c478bd9Sstevel@tonic-gate 
222*7c478bd9Sstevel@tonic-gate 		if (aclbufp == NULL)
223*7c478bd9Sstevel@tonic-gate 			return (EFAULT);
224*7c478bd9Sstevel@tonic-gate 
225*7c478bd9Sstevel@tonic-gate 		vsecattr.vsa_mask = VSA_ACE | VSA_ACECNT;
226*7c478bd9Sstevel@tonic-gate 		if (error = VOP_GETSECATTR(vp, &vsecattr, 0, CRED()))
227*7c478bd9Sstevel@tonic-gate 			return (error);
228*7c478bd9Sstevel@tonic-gate 
229*7c478bd9Sstevel@tonic-gate 		aclbsize = vsecattr.vsa_aclcnt * sizeof (ace_t);
230*7c478bd9Sstevel@tonic-gate 		if (vsecattr.vsa_aclcnt > nentries) {
231*7c478bd9Sstevel@tonic-gate 			error = ENOSPC;
232*7c478bd9Sstevel@tonic-gate 			goto errout;
233*7c478bd9Sstevel@tonic-gate 		}
234*7c478bd9Sstevel@tonic-gate 
235*7c478bd9Sstevel@tonic-gate 		if (aclbsize > 0) {
236*7c478bd9Sstevel@tonic-gate 			if ((error = copyout(vsecattr.vsa_aclentp,
237*7c478bd9Sstevel@tonic-gate 			    aclbufp, aclbsize)) != 0) {
238*7c478bd9Sstevel@tonic-gate 				goto errout;
239*7c478bd9Sstevel@tonic-gate 			}
240*7c478bd9Sstevel@tonic-gate 		}
241*7c478bd9Sstevel@tonic-gate 
242*7c478bd9Sstevel@tonic-gate 		*rv = vsecattr.vsa_aclcnt;
243*7c478bd9Sstevel@tonic-gate 		if (vsecattr.vsa_aclcnt) {
244*7c478bd9Sstevel@tonic-gate 			kmem_free(vsecattr.vsa_aclentp,
245*7c478bd9Sstevel@tonic-gate 			    vsecattr.vsa_aclcnt * sizeof (ace_t));
246*7c478bd9Sstevel@tonic-gate 		}
247*7c478bd9Sstevel@tonic-gate 		break;
248*7c478bd9Sstevel@tonic-gate 
249*7c478bd9Sstevel@tonic-gate 	case SETACL:
250*7c478bd9Sstevel@tonic-gate 		/*
251*7c478bd9Sstevel@tonic-gate 		 * Minimum ACL size is three entries so might as well
252*7c478bd9Sstevel@tonic-gate 		 * bail out here.  Also limit request size to prevent user
253*7c478bd9Sstevel@tonic-gate 		 * from allocating too much kernel memory.  Maximum size
254*7c478bd9Sstevel@tonic-gate 		 * is MAX_ACL_ENTRIES for the ACL part and MAX_ACL_ENTRIES
255*7c478bd9Sstevel@tonic-gate 		 * for the default ACL part. (bug 4058667)
256*7c478bd9Sstevel@tonic-gate 		 */
257*7c478bd9Sstevel@tonic-gate 		if (nentries < 3 || nentries > (MAX_ACL_ENTRIES * 2))
258*7c478bd9Sstevel@tonic-gate 			return (EINVAL);
259*7c478bd9Sstevel@tonic-gate 		/*
260*7c478bd9Sstevel@tonic-gate 		 * NULL output buffer is also an easy bail out.
261*7c478bd9Sstevel@tonic-gate 		 */
262*7c478bd9Sstevel@tonic-gate 		if (aclbufp == NULL)
263*7c478bd9Sstevel@tonic-gate 			return (EFAULT);
264*7c478bd9Sstevel@tonic-gate 		vsecattr.vsa_mask = VSA_ACL;
265*7c478bd9Sstevel@tonic-gate 		aclbsize = nentries * sizeof (aclent_t);
266*7c478bd9Sstevel@tonic-gate 		vsecattr.vsa_aclentp = kmem_alloc(aclbsize, KM_SLEEP);
267*7c478bd9Sstevel@tonic-gate 		aaclp = vsecattr.vsa_aclentp;
268*7c478bd9Sstevel@tonic-gate 		vsecattr.vsa_aclcnt = nentries;
269*7c478bd9Sstevel@tonic-gate 		uaddrp = (caddr_t)aclbufp;
270*7c478bd9Sstevel@tonic-gate 		if (copyin(uaddrp, vsecattr.vsa_aclentp, aclbsize)) {
271*7c478bd9Sstevel@tonic-gate 			kmem_free(aaclp, aclbsize);
272*7c478bd9Sstevel@tonic-gate 			return (EFAULT);
273*7c478bd9Sstevel@tonic-gate 		}
274*7c478bd9Sstevel@tonic-gate 		/* Sort the acl list */
275*7c478bd9Sstevel@tonic-gate 		ksort((caddr_t)vsecattr.vsa_aclentp,
276*7c478bd9Sstevel@tonic-gate 		    vsecattr.vsa_aclcnt, sizeof (aclent_t), cmp2acls);
277*7c478bd9Sstevel@tonic-gate 
278*7c478bd9Sstevel@tonic-gate 		/* Break into acl and default acl lists */
279*7c478bd9Sstevel@tonic-gate 		for (numacls = 0, aclp = vsecattr.vsa_aclentp;
280*7c478bd9Sstevel@tonic-gate 		    numacls < vsecattr.vsa_aclcnt;
281*7c478bd9Sstevel@tonic-gate 		    aclp++, numacls++) {
282*7c478bd9Sstevel@tonic-gate 			if (aclp->a_type & ACL_DEFAULT)
283*7c478bd9Sstevel@tonic-gate 				break;
284*7c478bd9Sstevel@tonic-gate 		}
285*7c478bd9Sstevel@tonic-gate 
286*7c478bd9Sstevel@tonic-gate 		/* Find where defaults start (if any) */
287*7c478bd9Sstevel@tonic-gate 		if (numacls < vsecattr.vsa_aclcnt) {
288*7c478bd9Sstevel@tonic-gate 			vsecattr.vsa_mask |= VSA_DFACL;
289*7c478bd9Sstevel@tonic-gate 			vsecattr.vsa_dfaclcnt = nentries - numacls;
290*7c478bd9Sstevel@tonic-gate 			vsecattr.vsa_dfaclentp = aclp;
291*7c478bd9Sstevel@tonic-gate 			vsecattr.vsa_aclcnt = numacls;
292*7c478bd9Sstevel@tonic-gate 		}
293*7c478bd9Sstevel@tonic-gate 		/* Adjust if they're all defaults */
294*7c478bd9Sstevel@tonic-gate 		if (vsecattr.vsa_aclcnt == 0) {
295*7c478bd9Sstevel@tonic-gate 			vsecattr.vsa_mask &= ~VSA_ACL;
296*7c478bd9Sstevel@tonic-gate 			vsecattr.vsa_aclentp = NULL;
297*7c478bd9Sstevel@tonic-gate 		}
298*7c478bd9Sstevel@tonic-gate 		/* Only directories can have defaults */
299*7c478bd9Sstevel@tonic-gate 		if (vsecattr.vsa_dfaclcnt && vp->v_type != VDIR) {
300*7c478bd9Sstevel@tonic-gate 			kmem_free(aaclp, aclbsize);
301*7c478bd9Sstevel@tonic-gate 			return (ENOTDIR);
302*7c478bd9Sstevel@tonic-gate 		}
303*7c478bd9Sstevel@tonic-gate 		(void) VOP_RWLOCK(vp, V_WRITELOCK_TRUE, NULL);
304*7c478bd9Sstevel@tonic-gate 		if (error = VOP_SETSECATTR(vp, &vsecattr, 0, CRED())) {
305*7c478bd9Sstevel@tonic-gate 			kmem_free(aaclp, aclbsize);
306*7c478bd9Sstevel@tonic-gate 			VOP_RWUNLOCK(vp, V_WRITELOCK_TRUE, NULL);
307*7c478bd9Sstevel@tonic-gate 			return (error);
308*7c478bd9Sstevel@tonic-gate 		}
309*7c478bd9Sstevel@tonic-gate 
310*7c478bd9Sstevel@tonic-gate 		/*
311*7c478bd9Sstevel@tonic-gate 		 * Should return 0 upon success according to the man page
312*7c478bd9Sstevel@tonic-gate 		 * and SVR4 semantics. (Bug #1214399: SETACL returns wrong rc)
313*7c478bd9Sstevel@tonic-gate 		 */
314*7c478bd9Sstevel@tonic-gate 		*rv = 0;
315*7c478bd9Sstevel@tonic-gate 		kmem_free(aaclp, aclbsize);
316*7c478bd9Sstevel@tonic-gate 		VOP_RWUNLOCK(vp, V_WRITELOCK_TRUE, NULL);
317*7c478bd9Sstevel@tonic-gate 		break;
318*7c478bd9Sstevel@tonic-gate 
319*7c478bd9Sstevel@tonic-gate 	case ACE_SETACL:
320*7c478bd9Sstevel@tonic-gate 		if (nentries < 3 || nentries > (MAX_ACL_ENTRIES * 2))
321*7c478bd9Sstevel@tonic-gate 			return (EINVAL);
322*7c478bd9Sstevel@tonic-gate 
323*7c478bd9Sstevel@tonic-gate 		if (aclbufp == NULL)
324*7c478bd9Sstevel@tonic-gate 			return (EFAULT);
325*7c478bd9Sstevel@tonic-gate 
326*7c478bd9Sstevel@tonic-gate 		vsecattr.vsa_mask = VSA_ACE;
327*7c478bd9Sstevel@tonic-gate 		aclbsize = nentries * sizeof (ace_t);
328*7c478bd9Sstevel@tonic-gate 		vsecattr.vsa_aclentp = kmem_alloc(aclbsize, KM_SLEEP);
329*7c478bd9Sstevel@tonic-gate 		aaclp = vsecattr.vsa_aclentp;
330*7c478bd9Sstevel@tonic-gate 		vsecattr.vsa_aclcnt = nentries;
331*7c478bd9Sstevel@tonic-gate 		uaddrp = (caddr_t)aclbufp;
332*7c478bd9Sstevel@tonic-gate 		if (copyin(uaddrp, vsecattr.vsa_aclentp, aclbsize)) {
333*7c478bd9Sstevel@tonic-gate 			kmem_free(aaclp, aclbsize);
334*7c478bd9Sstevel@tonic-gate 			return (EFAULT);
335*7c478bd9Sstevel@tonic-gate 		}
336*7c478bd9Sstevel@tonic-gate 		(void) VOP_RWLOCK(vp, V_WRITELOCK_TRUE, NULL);
337*7c478bd9Sstevel@tonic-gate 		if (error = VOP_SETSECATTR(vp, &vsecattr, 0, CRED())) {
338*7c478bd9Sstevel@tonic-gate 			kmem_free(aaclp, aclbsize);
339*7c478bd9Sstevel@tonic-gate 			VOP_RWUNLOCK(vp, V_WRITELOCK_TRUE, NULL);
340*7c478bd9Sstevel@tonic-gate 			return (error);
341*7c478bd9Sstevel@tonic-gate 		}
342*7c478bd9Sstevel@tonic-gate 		*rv = 0;
343*7c478bd9Sstevel@tonic-gate 		kmem_free(aaclp, aclbsize);
344*7c478bd9Sstevel@tonic-gate 		VOP_RWUNLOCK(vp, V_WRITELOCK_TRUE, NULL);
345*7c478bd9Sstevel@tonic-gate 		break;
346*7c478bd9Sstevel@tonic-gate 
347*7c478bd9Sstevel@tonic-gate 	default:
348*7c478bd9Sstevel@tonic-gate 		return (EINVAL);
349*7c478bd9Sstevel@tonic-gate 	}
350*7c478bd9Sstevel@tonic-gate 
351*7c478bd9Sstevel@tonic-gate 	return (0);
352*7c478bd9Sstevel@tonic-gate 
353*7c478bd9Sstevel@tonic-gate errout:
354*7c478bd9Sstevel@tonic-gate 	if (aclbsize && vsecattr.vsa_aclentp)
355*7c478bd9Sstevel@tonic-gate 		kmem_free(vsecattr.vsa_aclentp, aclbsize);
356*7c478bd9Sstevel@tonic-gate 	if (dfaclbsize && vsecattr.vsa_dfaclentp)
357*7c478bd9Sstevel@tonic-gate 		kmem_free(vsecattr.vsa_dfaclentp, dfaclbsize);
358*7c478bd9Sstevel@tonic-gate 	return (error);
359*7c478bd9Sstevel@tonic-gate }
360*7c478bd9Sstevel@tonic-gate 
361*7c478bd9Sstevel@tonic-gate 
362*7c478bd9Sstevel@tonic-gate /*
363*7c478bd9Sstevel@tonic-gate  * Generic shellsort, from K&R (1st ed, p 58.), somewhat modified.
364*7c478bd9Sstevel@tonic-gate  * v = Ptr to array/vector of objs
365*7c478bd9Sstevel@tonic-gate  * n = # objs in the array
366*7c478bd9Sstevel@tonic-gate  * s = size of each obj (must be multiples of a word size)
367*7c478bd9Sstevel@tonic-gate  * f = ptr to function to compare two objs
368*7c478bd9Sstevel@tonic-gate  *	returns (-1 = less than, 0 = equal, 1 = greater than
369*7c478bd9Sstevel@tonic-gate  */
370*7c478bd9Sstevel@tonic-gate void
371*7c478bd9Sstevel@tonic-gate ksort(caddr_t v, int n, int s, int (*f)())
372*7c478bd9Sstevel@tonic-gate {
373*7c478bd9Sstevel@tonic-gate 	int g, i, j, ii;
374*7c478bd9Sstevel@tonic-gate 	unsigned int *p1, *p2;
375*7c478bd9Sstevel@tonic-gate 	unsigned int tmp;
376*7c478bd9Sstevel@tonic-gate 
377*7c478bd9Sstevel@tonic-gate 	/* No work to do */
378*7c478bd9Sstevel@tonic-gate 	if (v == NULL || n <= 1)
379*7c478bd9Sstevel@tonic-gate 		return;
380*7c478bd9Sstevel@tonic-gate 
381*7c478bd9Sstevel@tonic-gate 	/* Sanity check on arguments */
382*7c478bd9Sstevel@tonic-gate 	ASSERT(((uintptr_t)v & 0x3) == 0 && (s & 0x3) == 0);
383*7c478bd9Sstevel@tonic-gate 	ASSERT(s > 0);
384*7c478bd9Sstevel@tonic-gate 	for (g = n / 2; g > 0; g /= 2) {
385*7c478bd9Sstevel@tonic-gate 		for (i = g; i < n; i++) {
386*7c478bd9Sstevel@tonic-gate 			for (j = i - g; j >= 0 &&
387*7c478bd9Sstevel@tonic-gate 				(*f)(v + j * s, v + (j + g) * s) == 1;
388*7c478bd9Sstevel@tonic-gate 					j -= g) {
389*7c478bd9Sstevel@tonic-gate 				p1 = (unsigned *)(v + j * s);
390*7c478bd9Sstevel@tonic-gate 				p2 = (unsigned *)(v + (j + g) * s);
391*7c478bd9Sstevel@tonic-gate 				for (ii = 0; ii < s / 4; ii++) {
392*7c478bd9Sstevel@tonic-gate 					tmp = *p1;
393*7c478bd9Sstevel@tonic-gate 					*p1++ = *p2;
394*7c478bd9Sstevel@tonic-gate 					*p2++ = tmp;
395*7c478bd9Sstevel@tonic-gate 				}
396*7c478bd9Sstevel@tonic-gate 			}
397*7c478bd9Sstevel@tonic-gate 		}
398*7c478bd9Sstevel@tonic-gate 	}
399*7c478bd9Sstevel@tonic-gate }
400*7c478bd9Sstevel@tonic-gate 
401*7c478bd9Sstevel@tonic-gate /*
402*7c478bd9Sstevel@tonic-gate  * Compare two acls, all fields.  Returns:
403*7c478bd9Sstevel@tonic-gate  * -1 (less than)
404*7c478bd9Sstevel@tonic-gate  *  0 (equal)
405*7c478bd9Sstevel@tonic-gate  * +1 (greater than)
406*7c478bd9Sstevel@tonic-gate  */
407*7c478bd9Sstevel@tonic-gate int
408*7c478bd9Sstevel@tonic-gate cmp2acls(void *a, void *b)
409*7c478bd9Sstevel@tonic-gate {
410*7c478bd9Sstevel@tonic-gate 	aclent_t *x = (aclent_t *)a;
411*7c478bd9Sstevel@tonic-gate 	aclent_t *y = (aclent_t *)b;
412*7c478bd9Sstevel@tonic-gate 
413*7c478bd9Sstevel@tonic-gate 	/* Compare types */
414*7c478bd9Sstevel@tonic-gate 	if (x->a_type < y->a_type)
415*7c478bd9Sstevel@tonic-gate 		return (-1);
416*7c478bd9Sstevel@tonic-gate 	if (x->a_type > y->a_type)
417*7c478bd9Sstevel@tonic-gate 		return (1);
418*7c478bd9Sstevel@tonic-gate 	/* Equal types; compare id's */
419*7c478bd9Sstevel@tonic-gate 	if (x->a_id < y->a_id)
420*7c478bd9Sstevel@tonic-gate 		return (-1);
421*7c478bd9Sstevel@tonic-gate 	if (x->a_id > y->a_id)
422*7c478bd9Sstevel@tonic-gate 		return (1);
423*7c478bd9Sstevel@tonic-gate 	/* Equal ids; compare perms */
424*7c478bd9Sstevel@tonic-gate 	if (x->a_perm < y->a_perm)
425*7c478bd9Sstevel@tonic-gate 		return (-1);
426*7c478bd9Sstevel@tonic-gate 	if (x->a_perm > y->a_perm)
427*7c478bd9Sstevel@tonic-gate 		return (1);
428*7c478bd9Sstevel@tonic-gate 	/* Totally equal */
429*7c478bd9Sstevel@tonic-gate 	return (0);
430*7c478bd9Sstevel@tonic-gate }
431