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 */
210Sstevel@tonic-gate /*
225331Samw * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
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 #pragma ident "%Z%%M% %I% %E% SMI"
350Sstevel@tonic-gate
360Sstevel@tonic-gate #include <sys/param.h>
370Sstevel@tonic-gate #include <sys/isa_defs.h>
380Sstevel@tonic-gate #include <sys/types.h>
390Sstevel@tonic-gate #include <sys/sysmacros.h>
400Sstevel@tonic-gate #include <sys/cred.h>
410Sstevel@tonic-gate #include <sys/systm.h>
420Sstevel@tonic-gate #include <sys/errno.h>
430Sstevel@tonic-gate #include <sys/fcntl.h>
440Sstevel@tonic-gate #include <sys/pathname.h>
450Sstevel@tonic-gate #include <sys/vfs.h>
460Sstevel@tonic-gate #include <sys/vnode.h>
470Sstevel@tonic-gate #include <sys/file.h>
480Sstevel@tonic-gate #include <sys/mode.h>
490Sstevel@tonic-gate #include <sys/uio.h>
500Sstevel@tonic-gate #include <sys/kmem.h>
510Sstevel@tonic-gate #include <sys/filio.h>
520Sstevel@tonic-gate #include <sys/acl.h>
530Sstevel@tonic-gate #include <sys/cmn_err.h>
54789Sahrens #include <acl/acl_common.h>
550Sstevel@tonic-gate
560Sstevel@tonic-gate #include <sys/unistd.h>
570Sstevel@tonic-gate #include <sys/debug.h>
582051Sprabahar #include <fs/fs_subr.h>
590Sstevel@tonic-gate
600Sstevel@tonic-gate static int cacl(int cmd, int nentries, void *aclbufp,
610Sstevel@tonic-gate vnode_t *vp, int *rv);
620Sstevel@tonic-gate
630Sstevel@tonic-gate /*
640Sstevel@tonic-gate * Get/Set ACL of a file.
650Sstevel@tonic-gate */
660Sstevel@tonic-gate int
acl(const char * fname,int cmd,int nentries,void * aclbufp)670Sstevel@tonic-gate acl(const char *fname, int cmd, int nentries, void *aclbufp)
680Sstevel@tonic-gate {
690Sstevel@tonic-gate struct vnode *vp;
700Sstevel@tonic-gate int error;
710Sstevel@tonic-gate int rv = 0;
722051Sprabahar int estale_retry = 0;
730Sstevel@tonic-gate
740Sstevel@tonic-gate /* Sanity check arguments */
750Sstevel@tonic-gate if (fname == NULL)
760Sstevel@tonic-gate return (set_errno(EINVAL));
770Sstevel@tonic-gate lookup:
780Sstevel@tonic-gate error = lookupname((char *)fname, UIO_USERSPACE, FOLLOW, NULLVPP, &vp);
790Sstevel@tonic-gate if (error) {
802051Sprabahar if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
810Sstevel@tonic-gate goto lookup;
820Sstevel@tonic-gate return (set_errno(error));
830Sstevel@tonic-gate }
840Sstevel@tonic-gate
850Sstevel@tonic-gate error = cacl(cmd, nentries, aclbufp, vp, &rv);
860Sstevel@tonic-gate VN_RELE(vp);
870Sstevel@tonic-gate if (error) {
882051Sprabahar if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
890Sstevel@tonic-gate goto lookup;
900Sstevel@tonic-gate return (set_errno(error));
910Sstevel@tonic-gate }
920Sstevel@tonic-gate return (rv);
930Sstevel@tonic-gate }
940Sstevel@tonic-gate
950Sstevel@tonic-gate /*
960Sstevel@tonic-gate * Get/Set ACL of a file with facl system call.
970Sstevel@tonic-gate */
980Sstevel@tonic-gate int
facl(int fdes,int cmd,int nentries,void * aclbufp)990Sstevel@tonic-gate facl(int fdes, int cmd, int nentries, void *aclbufp)
1000Sstevel@tonic-gate {
1010Sstevel@tonic-gate file_t *fp;
1020Sstevel@tonic-gate int error;
1030Sstevel@tonic-gate int rv = 0;
1040Sstevel@tonic-gate
1050Sstevel@tonic-gate if ((fp = getf(fdes)) == NULL)
1060Sstevel@tonic-gate return (set_errno(EBADF));
1070Sstevel@tonic-gate if (fp->f_flag & FREVOKED) {
1080Sstevel@tonic-gate releasef(fdes);
1090Sstevel@tonic-gate return (set_errno(EBADF));
1100Sstevel@tonic-gate }
1110Sstevel@tonic-gate
1120Sstevel@tonic-gate error = cacl(cmd, nentries, aclbufp, fp->f_vnode, &rv);
1130Sstevel@tonic-gate releasef(fdes);
1140Sstevel@tonic-gate
1150Sstevel@tonic-gate if (error)
1160Sstevel@tonic-gate return (set_errno(error));
1170Sstevel@tonic-gate return (rv);
1180Sstevel@tonic-gate }
1190Sstevel@tonic-gate
1200Sstevel@tonic-gate
1210Sstevel@tonic-gate /*
1220Sstevel@tonic-gate * Common code for acl() and facl().
1230Sstevel@tonic-gate */
1240Sstevel@tonic-gate static int
cacl(int cmd,int nentries,void * aclbufp,vnode_t * vp,int * rv)1250Sstevel@tonic-gate cacl(int cmd, int nentries, void *aclbufp, vnode_t *vp, int *rv)
1260Sstevel@tonic-gate {
1270Sstevel@tonic-gate int error;
1280Sstevel@tonic-gate int aclbsize; /* size of acl list in bytes */
1290Sstevel@tonic-gate int dfaclbsize; /* size of default acl list in bytes */
1300Sstevel@tonic-gate int numacls;
1310Sstevel@tonic-gate caddr_t uaddrp;
1320Sstevel@tonic-gate aclent_t *aclp, *aaclp;
1330Sstevel@tonic-gate vsecattr_t vsecattr;
1345331Samw size_t entry_size;
1350Sstevel@tonic-gate
1360Sstevel@tonic-gate ASSERT(vp);
1370Sstevel@tonic-gate
1380Sstevel@tonic-gate bzero(&vsecattr, sizeof (vsecattr_t));
1390Sstevel@tonic-gate
1400Sstevel@tonic-gate switch (cmd) {
1410Sstevel@tonic-gate
1420Sstevel@tonic-gate case ACE_GETACLCNT:
1430Sstevel@tonic-gate case GETACLCNT:
1445331Samw if (cmd == GETACLCNT) {
1455331Samw entry_size = sizeof (aclent_t);
1460Sstevel@tonic-gate vsecattr.vsa_mask = VSA_ACLCNT | VSA_DFACLCNT;
1475331Samw } else {
1485331Samw entry_size = sizeof (ace_t);
1490Sstevel@tonic-gate vsecattr.vsa_mask = VSA_ACECNT;
1505331Samw }
1515331Samw if (error = VOP_GETSECATTR(vp, &vsecattr, 0, CRED(), NULL))
1520Sstevel@tonic-gate return (error);
1530Sstevel@tonic-gate *rv = vsecattr.vsa_aclcnt + vsecattr.vsa_dfaclcnt;
1540Sstevel@tonic-gate if (vsecattr.vsa_aclcnt && vsecattr.vsa_aclentp) {
1550Sstevel@tonic-gate kmem_free(vsecattr.vsa_aclentp,
1565331Samw vsecattr.vsa_aclcnt * entry_size);
1570Sstevel@tonic-gate }
1580Sstevel@tonic-gate if (vsecattr.vsa_dfaclcnt && vsecattr.vsa_dfaclentp) {
1590Sstevel@tonic-gate kmem_free(vsecattr.vsa_dfaclentp,
1605331Samw vsecattr.vsa_dfaclcnt * entry_size);
1610Sstevel@tonic-gate }
1620Sstevel@tonic-gate break;
1630Sstevel@tonic-gate case GETACL:
1640Sstevel@tonic-gate /*
1650Sstevel@tonic-gate * Minimum ACL size is three entries so might as well
1660Sstevel@tonic-gate * bail out here.
1670Sstevel@tonic-gate */
1680Sstevel@tonic-gate if (nentries < 3)
1690Sstevel@tonic-gate return (EINVAL);
1700Sstevel@tonic-gate /*
1710Sstevel@tonic-gate * NULL output buffer is also a pretty easy bail out.
1720Sstevel@tonic-gate */
1730Sstevel@tonic-gate if (aclbufp == NULL)
1740Sstevel@tonic-gate return (EFAULT);
1750Sstevel@tonic-gate vsecattr.vsa_mask = VSA_ACL | VSA_ACLCNT | VSA_DFACL |
1760Sstevel@tonic-gate VSA_DFACLCNT;
1775331Samw if (error = VOP_GETSECATTR(vp, &vsecattr, 0, CRED(), NULL))
1780Sstevel@tonic-gate return (error);
1790Sstevel@tonic-gate /* Check user's buffer is big enough */
1800Sstevel@tonic-gate numacls = vsecattr.vsa_aclcnt + vsecattr.vsa_dfaclcnt;
1810Sstevel@tonic-gate aclbsize = vsecattr.vsa_aclcnt * sizeof (aclent_t);
1820Sstevel@tonic-gate dfaclbsize = vsecattr.vsa_dfaclcnt * sizeof (aclent_t);
1830Sstevel@tonic-gate if (numacls > nentries) {
1840Sstevel@tonic-gate error = ENOSPC;
1850Sstevel@tonic-gate goto errout;
1860Sstevel@tonic-gate }
1870Sstevel@tonic-gate /* Sort the acl & default acl lists */
1880Sstevel@tonic-gate if (vsecattr.vsa_aclcnt > 1)
1890Sstevel@tonic-gate ksort((caddr_t)vsecattr.vsa_aclentp,
190*5753Sgww vsecattr.vsa_aclcnt, sizeof (aclent_t), cmp2acls);
1910Sstevel@tonic-gate if (vsecattr.vsa_dfaclcnt > 1)
1920Sstevel@tonic-gate ksort((caddr_t)vsecattr.vsa_dfaclentp,
193*5753Sgww vsecattr.vsa_dfaclcnt, sizeof (aclent_t), cmp2acls);
1940Sstevel@tonic-gate /* Copy out acl's */
1950Sstevel@tonic-gate uaddrp = (caddr_t)aclbufp;
1960Sstevel@tonic-gate if (aclbsize > 0) { /* bug #1262490 */
1970Sstevel@tonic-gate if (copyout(vsecattr.vsa_aclentp, uaddrp, aclbsize)) {
1980Sstevel@tonic-gate error = EFAULT;
1990Sstevel@tonic-gate goto errout;
2000Sstevel@tonic-gate }
2010Sstevel@tonic-gate }
2020Sstevel@tonic-gate /* Copy out default acl's */
2030Sstevel@tonic-gate if (dfaclbsize > 0) {
2040Sstevel@tonic-gate uaddrp += aclbsize;
2050Sstevel@tonic-gate if (copyout(vsecattr.vsa_dfaclentp,
2060Sstevel@tonic-gate uaddrp, dfaclbsize)) {
2070Sstevel@tonic-gate error = EFAULT;
2080Sstevel@tonic-gate goto errout;
2090Sstevel@tonic-gate }
2100Sstevel@tonic-gate }
2110Sstevel@tonic-gate *rv = numacls;
2120Sstevel@tonic-gate if (vsecattr.vsa_aclcnt) {
2130Sstevel@tonic-gate kmem_free(vsecattr.vsa_aclentp,
2140Sstevel@tonic-gate vsecattr.vsa_aclcnt * sizeof (aclent_t));
2150Sstevel@tonic-gate }
2160Sstevel@tonic-gate if (vsecattr.vsa_dfaclcnt) {
2170Sstevel@tonic-gate kmem_free(vsecattr.vsa_dfaclentp,
2180Sstevel@tonic-gate vsecattr.vsa_dfaclcnt * sizeof (aclent_t));
2190Sstevel@tonic-gate }
2200Sstevel@tonic-gate break;
2210Sstevel@tonic-gate
2220Sstevel@tonic-gate case ACE_GETACL:
2230Sstevel@tonic-gate if (aclbufp == NULL)
2240Sstevel@tonic-gate return (EFAULT);
2250Sstevel@tonic-gate
2260Sstevel@tonic-gate vsecattr.vsa_mask = VSA_ACE | VSA_ACECNT;
2275331Samw if (error = VOP_GETSECATTR(vp, &vsecattr, 0, CRED(), NULL))
2280Sstevel@tonic-gate return (error);
2290Sstevel@tonic-gate
2300Sstevel@tonic-gate aclbsize = vsecattr.vsa_aclcnt * sizeof (ace_t);
2310Sstevel@tonic-gate if (vsecattr.vsa_aclcnt > nentries) {
2320Sstevel@tonic-gate error = ENOSPC;
2330Sstevel@tonic-gate goto errout;
2340Sstevel@tonic-gate }
2350Sstevel@tonic-gate
2360Sstevel@tonic-gate if (aclbsize > 0) {
2370Sstevel@tonic-gate if ((error = copyout(vsecattr.vsa_aclentp,
2380Sstevel@tonic-gate aclbufp, aclbsize)) != 0) {
2390Sstevel@tonic-gate goto errout;
2400Sstevel@tonic-gate }
2410Sstevel@tonic-gate }
2420Sstevel@tonic-gate
2430Sstevel@tonic-gate *rv = vsecattr.vsa_aclcnt;
2440Sstevel@tonic-gate if (vsecattr.vsa_aclcnt) {
2455331Samw kmem_free(vsecattr.vsa_aclentp, vsecattr.vsa_aclentsz);
2460Sstevel@tonic-gate }
2470Sstevel@tonic-gate break;
2480Sstevel@tonic-gate
2490Sstevel@tonic-gate case SETACL:
2500Sstevel@tonic-gate /*
2510Sstevel@tonic-gate * Minimum ACL size is three entries so might as well
2520Sstevel@tonic-gate * bail out here. Also limit request size to prevent user
2530Sstevel@tonic-gate * from allocating too much kernel memory. Maximum size
2540Sstevel@tonic-gate * is MAX_ACL_ENTRIES for the ACL part and MAX_ACL_ENTRIES
2550Sstevel@tonic-gate * for the default ACL part. (bug 4058667)
2560Sstevel@tonic-gate */
2570Sstevel@tonic-gate if (nentries < 3 || nentries > (MAX_ACL_ENTRIES * 2))
2580Sstevel@tonic-gate return (EINVAL);
2590Sstevel@tonic-gate /*
2600Sstevel@tonic-gate * NULL output buffer is also an easy bail out.
2610Sstevel@tonic-gate */
2620Sstevel@tonic-gate if (aclbufp == NULL)
2630Sstevel@tonic-gate return (EFAULT);
2640Sstevel@tonic-gate vsecattr.vsa_mask = VSA_ACL;
2650Sstevel@tonic-gate aclbsize = nentries * sizeof (aclent_t);
2660Sstevel@tonic-gate vsecattr.vsa_aclentp = kmem_alloc(aclbsize, KM_SLEEP);
2670Sstevel@tonic-gate aaclp = vsecattr.vsa_aclentp;
2680Sstevel@tonic-gate vsecattr.vsa_aclcnt = nentries;
2690Sstevel@tonic-gate uaddrp = (caddr_t)aclbufp;
2700Sstevel@tonic-gate if (copyin(uaddrp, vsecattr.vsa_aclentp, aclbsize)) {
2710Sstevel@tonic-gate kmem_free(aaclp, aclbsize);
2720Sstevel@tonic-gate return (EFAULT);
2730Sstevel@tonic-gate }
2740Sstevel@tonic-gate /* Sort the acl list */
2750Sstevel@tonic-gate ksort((caddr_t)vsecattr.vsa_aclentp,
2760Sstevel@tonic-gate vsecattr.vsa_aclcnt, sizeof (aclent_t), cmp2acls);
2770Sstevel@tonic-gate
2780Sstevel@tonic-gate /* Break into acl and default acl lists */
2790Sstevel@tonic-gate for (numacls = 0, aclp = vsecattr.vsa_aclentp;
2800Sstevel@tonic-gate numacls < vsecattr.vsa_aclcnt;
2810Sstevel@tonic-gate aclp++, numacls++) {
2820Sstevel@tonic-gate if (aclp->a_type & ACL_DEFAULT)
2830Sstevel@tonic-gate break;
2840Sstevel@tonic-gate }
2850Sstevel@tonic-gate
2860Sstevel@tonic-gate /* Find where defaults start (if any) */
2870Sstevel@tonic-gate if (numacls < vsecattr.vsa_aclcnt) {
2880Sstevel@tonic-gate vsecattr.vsa_mask |= VSA_DFACL;
2890Sstevel@tonic-gate vsecattr.vsa_dfaclcnt = nentries - numacls;
2900Sstevel@tonic-gate vsecattr.vsa_dfaclentp = aclp;
2910Sstevel@tonic-gate vsecattr.vsa_aclcnt = numacls;
2920Sstevel@tonic-gate }
2930Sstevel@tonic-gate /* Adjust if they're all defaults */
2940Sstevel@tonic-gate if (vsecattr.vsa_aclcnt == 0) {
2950Sstevel@tonic-gate vsecattr.vsa_mask &= ~VSA_ACL;
2960Sstevel@tonic-gate vsecattr.vsa_aclentp = NULL;
2970Sstevel@tonic-gate }
2980Sstevel@tonic-gate /* Only directories can have defaults */
2990Sstevel@tonic-gate if (vsecattr.vsa_dfaclcnt && vp->v_type != VDIR) {
3000Sstevel@tonic-gate kmem_free(aaclp, aclbsize);
3010Sstevel@tonic-gate return (ENOTDIR);
3020Sstevel@tonic-gate }
3030Sstevel@tonic-gate (void) VOP_RWLOCK(vp, V_WRITELOCK_TRUE, NULL);
3045331Samw if (error = VOP_SETSECATTR(vp, &vsecattr, 0, CRED(), NULL)) {
3050Sstevel@tonic-gate kmem_free(aaclp, aclbsize);
3060Sstevel@tonic-gate VOP_RWUNLOCK(vp, V_WRITELOCK_TRUE, NULL);
3070Sstevel@tonic-gate return (error);
3080Sstevel@tonic-gate }
3090Sstevel@tonic-gate
3100Sstevel@tonic-gate /*
3110Sstevel@tonic-gate * Should return 0 upon success according to the man page
3120Sstevel@tonic-gate * and SVR4 semantics. (Bug #1214399: SETACL returns wrong rc)
3130Sstevel@tonic-gate */
3140Sstevel@tonic-gate *rv = 0;
3150Sstevel@tonic-gate kmem_free(aaclp, aclbsize);
3160Sstevel@tonic-gate VOP_RWUNLOCK(vp, V_WRITELOCK_TRUE, NULL);
3170Sstevel@tonic-gate break;
3180Sstevel@tonic-gate
3190Sstevel@tonic-gate case ACE_SETACL:
3203130Smarks if (nentries < 1 || nentries > MAX_ACL_ENTRIES)
3210Sstevel@tonic-gate return (EINVAL);
3220Sstevel@tonic-gate
3230Sstevel@tonic-gate if (aclbufp == NULL)
3240Sstevel@tonic-gate return (EFAULT);
3250Sstevel@tonic-gate
3260Sstevel@tonic-gate vsecattr.vsa_mask = VSA_ACE;
3270Sstevel@tonic-gate aclbsize = nentries * sizeof (ace_t);
3280Sstevel@tonic-gate vsecattr.vsa_aclentp = kmem_alloc(aclbsize, KM_SLEEP);
3290Sstevel@tonic-gate aaclp = vsecattr.vsa_aclentp;
3300Sstevel@tonic-gate vsecattr.vsa_aclcnt = nentries;
3315331Samw vsecattr.vsa_aclentsz = aclbsize;
3320Sstevel@tonic-gate uaddrp = (caddr_t)aclbufp;
3330Sstevel@tonic-gate if (copyin(uaddrp, vsecattr.vsa_aclentp, aclbsize)) {
3340Sstevel@tonic-gate kmem_free(aaclp, aclbsize);
3350Sstevel@tonic-gate return (EFAULT);
3360Sstevel@tonic-gate }
3370Sstevel@tonic-gate (void) VOP_RWLOCK(vp, V_WRITELOCK_TRUE, NULL);
3385331Samw if (error = VOP_SETSECATTR(vp, &vsecattr, 0, CRED(), NULL)) {
3390Sstevel@tonic-gate kmem_free(aaclp, aclbsize);
3400Sstevel@tonic-gate VOP_RWUNLOCK(vp, V_WRITELOCK_TRUE, NULL);
3410Sstevel@tonic-gate return (error);
3420Sstevel@tonic-gate }
3430Sstevel@tonic-gate *rv = 0;
3440Sstevel@tonic-gate kmem_free(aaclp, aclbsize);
3450Sstevel@tonic-gate VOP_RWUNLOCK(vp, V_WRITELOCK_TRUE, NULL);
3460Sstevel@tonic-gate break;
3470Sstevel@tonic-gate
3480Sstevel@tonic-gate default:
3490Sstevel@tonic-gate return (EINVAL);
3500Sstevel@tonic-gate }
3510Sstevel@tonic-gate
3520Sstevel@tonic-gate return (0);
3530Sstevel@tonic-gate
3540Sstevel@tonic-gate errout:
3550Sstevel@tonic-gate if (aclbsize && vsecattr.vsa_aclentp)
3560Sstevel@tonic-gate kmem_free(vsecattr.vsa_aclentp, aclbsize);
3570Sstevel@tonic-gate if (dfaclbsize && vsecattr.vsa_dfaclentp)
3580Sstevel@tonic-gate kmem_free(vsecattr.vsa_dfaclentp, dfaclbsize);
3590Sstevel@tonic-gate return (error);
3600Sstevel@tonic-gate }
361