]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/cddl/contrib/opensolaris/uts/common/fs/zfs/zfs_acl.c
MFC ZFS version 28 and related revisions:
[FreeBSD/stable/8.git] / sys / cddl / contrib / opensolaris / uts / common / fs / zfs / zfs_acl.c
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 (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23  */
24
25 #include <sys/types.h>
26 #include <sys/param.h>
27 #include <sys/time.h>
28 #include <sys/systm.h>
29 #include <sys/sysmacros.h>
30 #include <sys/resource.h>
31 #include <sys/vfs.h>
32 #include <sys/vnode.h>
33 #include <sys/file.h>
34 #include <sys/stat.h>
35 #include <sys/kmem.h>
36 #include <sys/cmn_err.h>
37 #include <sys/errno.h>
38 #include <sys/unistd.h>
39 #include <sys/sdt.h>
40 #include <sys/fs/zfs.h>
41 #include <sys/policy.h>
42 #include <sys/zfs_znode.h>
43 #include <sys/zfs_fuid.h>
44 #include <sys/zfs_acl.h>
45 #include <sys/zfs_dir.h>
46 #include <sys/zfs_vfsops.h>
47 #include <sys/dmu.h>
48 #include <sys/dnode.h>
49 #include <sys/zap.h>
50 #include <sys/sa.h>
51 #include <acl/acl_common.h>
52
53 #define ALLOW   ACE_ACCESS_ALLOWED_ACE_TYPE
54 #define DENY    ACE_ACCESS_DENIED_ACE_TYPE
55 #define MAX_ACE_TYPE    ACE_SYSTEM_ALARM_CALLBACK_OBJECT_ACE_TYPE
56 #define MIN_ACE_TYPE    ALLOW
57
58 #define OWNING_GROUP            (ACE_GROUP|ACE_IDENTIFIER_GROUP)
59 #define EVERYONE_ALLOW_MASK (ACE_READ_ACL|ACE_READ_ATTRIBUTES | \
60     ACE_READ_NAMED_ATTRS|ACE_SYNCHRONIZE)
61 #define EVERYONE_DENY_MASK (ACE_WRITE_ACL|ACE_WRITE_OWNER | \
62     ACE_WRITE_ATTRIBUTES|ACE_WRITE_NAMED_ATTRS)
63 #define OWNER_ALLOW_MASK (ACE_WRITE_ACL | ACE_WRITE_OWNER | \
64     ACE_WRITE_ATTRIBUTES|ACE_WRITE_NAMED_ATTRS)
65
66 #define ZFS_CHECKED_MASKS (ACE_READ_ACL|ACE_READ_ATTRIBUTES|ACE_READ_DATA| \
67     ACE_READ_NAMED_ATTRS|ACE_WRITE_DATA|ACE_WRITE_ATTRIBUTES| \
68     ACE_WRITE_NAMED_ATTRS|ACE_APPEND_DATA|ACE_EXECUTE|ACE_WRITE_OWNER| \
69     ACE_WRITE_ACL|ACE_DELETE|ACE_DELETE_CHILD|ACE_SYNCHRONIZE)
70
71 #define WRITE_MASK_DATA (ACE_WRITE_DATA|ACE_APPEND_DATA|ACE_WRITE_NAMED_ATTRS)
72 #define WRITE_MASK_ATTRS (ACE_WRITE_ACL|ACE_WRITE_OWNER|ACE_WRITE_ATTRIBUTES| \
73     ACE_DELETE|ACE_DELETE_CHILD)
74 #define WRITE_MASK (WRITE_MASK_DATA|WRITE_MASK_ATTRS)
75
76 #define OGE_CLEAR       (ACE_READ_DATA|ACE_LIST_DIRECTORY|ACE_WRITE_DATA| \
77     ACE_ADD_FILE|ACE_APPEND_DATA|ACE_ADD_SUBDIRECTORY|ACE_EXECUTE)
78
79 #define OKAY_MASK_BITS (ACE_READ_DATA|ACE_LIST_DIRECTORY|ACE_WRITE_DATA| \
80     ACE_ADD_FILE|ACE_APPEND_DATA|ACE_ADD_SUBDIRECTORY|ACE_EXECUTE)
81
82 #define ALL_INHERIT     (ACE_FILE_INHERIT_ACE|ACE_DIRECTORY_INHERIT_ACE | \
83     ACE_NO_PROPAGATE_INHERIT_ACE|ACE_INHERIT_ONLY_ACE|ACE_INHERITED_ACE)
84
85 #define RESTRICTED_CLEAR        (ACE_WRITE_ACL|ACE_WRITE_OWNER)
86
87 #define V4_ACL_WIDE_FLAGS (ZFS_ACL_AUTO_INHERIT|ZFS_ACL_DEFAULTED|\
88     ZFS_ACL_PROTECTED)
89
90 #define ZFS_ACL_WIDE_FLAGS (V4_ACL_WIDE_FLAGS|ZFS_ACL_TRIVIAL|ZFS_INHERIT_ACE|\
91     ZFS_ACL_OBJ_ACE)
92
93 #define ALL_MODE_EXECS (S_IXUSR | S_IXGRP | S_IXOTH)
94
95 static uint16_t
96 zfs_ace_v0_get_type(void *acep)
97 {
98         return (((zfs_oldace_t *)acep)->z_type);
99 }
100
101 static uint16_t
102 zfs_ace_v0_get_flags(void *acep)
103 {
104         return (((zfs_oldace_t *)acep)->z_flags);
105 }
106
107 static uint32_t
108 zfs_ace_v0_get_mask(void *acep)
109 {
110         return (((zfs_oldace_t *)acep)->z_access_mask);
111 }
112
113 static uint64_t
114 zfs_ace_v0_get_who(void *acep)
115 {
116         return (((zfs_oldace_t *)acep)->z_fuid);
117 }
118
119 static void
120 zfs_ace_v0_set_type(void *acep, uint16_t type)
121 {
122         ((zfs_oldace_t *)acep)->z_type = type;
123 }
124
125 static void
126 zfs_ace_v0_set_flags(void *acep, uint16_t flags)
127 {
128         ((zfs_oldace_t *)acep)->z_flags = flags;
129 }
130
131 static void
132 zfs_ace_v0_set_mask(void *acep, uint32_t mask)
133 {
134         ((zfs_oldace_t *)acep)->z_access_mask = mask;
135 }
136
137 static void
138 zfs_ace_v0_set_who(void *acep, uint64_t who)
139 {
140         ((zfs_oldace_t *)acep)->z_fuid = who;
141 }
142
143 /*ARGSUSED*/
144 static size_t
145 zfs_ace_v0_size(void *acep)
146 {
147         return (sizeof (zfs_oldace_t));
148 }
149
150 static size_t
151 zfs_ace_v0_abstract_size(void)
152 {
153         return (sizeof (zfs_oldace_t));
154 }
155
156 static int
157 zfs_ace_v0_mask_off(void)
158 {
159         return (offsetof(zfs_oldace_t, z_access_mask));
160 }
161
162 /*ARGSUSED*/
163 static int
164 zfs_ace_v0_data(void *acep, void **datap)
165 {
166         *datap = NULL;
167         return (0);
168 }
169
170 static acl_ops_t zfs_acl_v0_ops = {
171         zfs_ace_v0_get_mask,
172         zfs_ace_v0_set_mask,
173         zfs_ace_v0_get_flags,
174         zfs_ace_v0_set_flags,
175         zfs_ace_v0_get_type,
176         zfs_ace_v0_set_type,
177         zfs_ace_v0_get_who,
178         zfs_ace_v0_set_who,
179         zfs_ace_v0_size,
180         zfs_ace_v0_abstract_size,
181         zfs_ace_v0_mask_off,
182         zfs_ace_v0_data
183 };
184
185 static uint16_t
186 zfs_ace_fuid_get_type(void *acep)
187 {
188         return (((zfs_ace_hdr_t *)acep)->z_type);
189 }
190
191 static uint16_t
192 zfs_ace_fuid_get_flags(void *acep)
193 {
194         return (((zfs_ace_hdr_t *)acep)->z_flags);
195 }
196
197 static uint32_t
198 zfs_ace_fuid_get_mask(void *acep)
199 {
200         return (((zfs_ace_hdr_t *)acep)->z_access_mask);
201 }
202
203 static uint64_t
204 zfs_ace_fuid_get_who(void *args)
205 {
206         uint16_t entry_type;
207         zfs_ace_t *acep = args;
208
209         entry_type = acep->z_hdr.z_flags & ACE_TYPE_FLAGS;
210
211         if (entry_type == ACE_OWNER || entry_type == OWNING_GROUP ||
212             entry_type == ACE_EVERYONE)
213                 return (-1);
214         return (((zfs_ace_t *)acep)->z_fuid);
215 }
216
217 static void
218 zfs_ace_fuid_set_type(void *acep, uint16_t type)
219 {
220         ((zfs_ace_hdr_t *)acep)->z_type = type;
221 }
222
223 static void
224 zfs_ace_fuid_set_flags(void *acep, uint16_t flags)
225 {
226         ((zfs_ace_hdr_t *)acep)->z_flags = flags;
227 }
228
229 static void
230 zfs_ace_fuid_set_mask(void *acep, uint32_t mask)
231 {
232         ((zfs_ace_hdr_t *)acep)->z_access_mask = mask;
233 }
234
235 static void
236 zfs_ace_fuid_set_who(void *arg, uint64_t who)
237 {
238         zfs_ace_t *acep = arg;
239
240         uint16_t entry_type = acep->z_hdr.z_flags & ACE_TYPE_FLAGS;
241
242         if (entry_type == ACE_OWNER || entry_type == OWNING_GROUP ||
243             entry_type == ACE_EVERYONE)
244                 return;
245         acep->z_fuid = who;
246 }
247
248 static size_t
249 zfs_ace_fuid_size(void *acep)
250 {
251         zfs_ace_hdr_t *zacep = acep;
252         uint16_t entry_type;
253
254         switch (zacep->z_type) {
255         case ACE_ACCESS_ALLOWED_OBJECT_ACE_TYPE:
256         case ACE_ACCESS_DENIED_OBJECT_ACE_TYPE:
257         case ACE_SYSTEM_AUDIT_OBJECT_ACE_TYPE:
258         case ACE_SYSTEM_ALARM_OBJECT_ACE_TYPE:
259                 return (sizeof (zfs_object_ace_t));
260         case ALLOW:
261         case DENY:
262                 entry_type =
263                     (((zfs_ace_hdr_t *)acep)->z_flags & ACE_TYPE_FLAGS);
264                 if (entry_type == ACE_OWNER ||
265                     entry_type == OWNING_GROUP ||
266                     entry_type == ACE_EVERYONE)
267                         return (sizeof (zfs_ace_hdr_t));
268                 /*FALLTHROUGH*/
269         default:
270                 return (sizeof (zfs_ace_t));
271         }
272 }
273
274 static size_t
275 zfs_ace_fuid_abstract_size(void)
276 {
277         return (sizeof (zfs_ace_hdr_t));
278 }
279
280 static int
281 zfs_ace_fuid_mask_off(void)
282 {
283         return (offsetof(zfs_ace_hdr_t, z_access_mask));
284 }
285
286 static int
287 zfs_ace_fuid_data(void *acep, void **datap)
288 {
289         zfs_ace_t *zacep = acep;
290         zfs_object_ace_t *zobjp;
291
292         switch (zacep->z_hdr.z_type) {
293         case ACE_ACCESS_ALLOWED_OBJECT_ACE_TYPE:
294         case ACE_ACCESS_DENIED_OBJECT_ACE_TYPE:
295         case ACE_SYSTEM_AUDIT_OBJECT_ACE_TYPE:
296         case ACE_SYSTEM_ALARM_OBJECT_ACE_TYPE:
297                 zobjp = acep;
298                 *datap = (caddr_t)zobjp + sizeof (zfs_ace_t);
299                 return (sizeof (zfs_object_ace_t) - sizeof (zfs_ace_t));
300         default:
301                 *datap = NULL;
302                 return (0);
303         }
304 }
305
306 static acl_ops_t zfs_acl_fuid_ops = {
307         zfs_ace_fuid_get_mask,
308         zfs_ace_fuid_set_mask,
309         zfs_ace_fuid_get_flags,
310         zfs_ace_fuid_set_flags,
311         zfs_ace_fuid_get_type,
312         zfs_ace_fuid_set_type,
313         zfs_ace_fuid_get_who,
314         zfs_ace_fuid_set_who,
315         zfs_ace_fuid_size,
316         zfs_ace_fuid_abstract_size,
317         zfs_ace_fuid_mask_off,
318         zfs_ace_fuid_data
319 };
320
321 /*
322  * The following three functions are provided for compatibility with
323  * older ZPL version in order to determine if the file use to have
324  * an external ACL and what version of ACL previously existed on the
325  * file.  Would really be nice to not need this, sigh.
326  */
327 uint64_t
328 zfs_external_acl(znode_t *zp)
329 {
330         zfs_acl_phys_t acl_phys;
331         int error;
332
333         if (zp->z_is_sa)
334                 return (0);
335
336         /*
337          * Need to deal with a potential
338          * race where zfs_sa_upgrade could cause
339          * z_isa_sa to change.
340          *
341          * If the lookup fails then the state of z_is_sa should have
342          * changed.
343          */
344
345         if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_ZNODE_ACL(zp->z_zfsvfs),
346             &acl_phys, sizeof (acl_phys))) == 0)
347                 return (acl_phys.z_acl_extern_obj);
348         else {
349                 /*
350                  * after upgrade the SA_ZPL_ZNODE_ACL should have been
351                  * removed
352                  */
353                 VERIFY(zp->z_is_sa && error == ENOENT);
354                 return (0);
355         }
356 }
357
358 /*
359  * Determine size of ACL in bytes
360  *
361  * This is more complicated than it should be since we have to deal
362  * with old external ACLs.
363  */
364 static int
365 zfs_acl_znode_info(znode_t *zp, int *aclsize, int *aclcount,
366     zfs_acl_phys_t *aclphys)
367 {
368         zfsvfs_t *zfsvfs = zp->z_zfsvfs;
369         uint64_t acl_count;
370         int size;
371         int error;
372
373         ASSERT(MUTEX_HELD(&zp->z_acl_lock));
374         if (zp->z_is_sa) {
375                 if ((error = sa_size(zp->z_sa_hdl, SA_ZPL_DACL_ACES(zfsvfs),
376                     &size)) != 0)
377                         return (error);
378                 *aclsize = size;
379                 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_DACL_COUNT(zfsvfs),
380                     &acl_count, sizeof (acl_count))) != 0)
381                         return (error);
382                 *aclcount = acl_count;
383         } else {
384                 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_ZNODE_ACL(zfsvfs),
385                     aclphys, sizeof (*aclphys))) != 0)
386                         return (error);
387
388                 if (aclphys->z_acl_version == ZFS_ACL_VERSION_INITIAL) {
389                         *aclsize = ZFS_ACL_SIZE(aclphys->z_acl_size);
390                         *aclcount = aclphys->z_acl_size;
391                 } else {
392                         *aclsize = aclphys->z_acl_size;
393                         *aclcount = aclphys->z_acl_count;
394                 }
395         }
396         return (0);
397 }
398
399 int
400 zfs_znode_acl_version(znode_t *zp)
401 {
402         zfs_acl_phys_t acl_phys;
403
404         if (zp->z_is_sa)
405                 return (ZFS_ACL_VERSION_FUID);
406         else {
407                 int error;
408
409                 /*
410                  * Need to deal with a potential
411                  * race where zfs_sa_upgrade could cause
412                  * z_isa_sa to change.
413                  *
414                  * If the lookup fails then the state of z_is_sa should have
415                  * changed.
416                  */
417                 if ((error = sa_lookup(zp->z_sa_hdl,
418                     SA_ZPL_ZNODE_ACL(zp->z_zfsvfs),
419                     &acl_phys, sizeof (acl_phys))) == 0)
420                         return (acl_phys.z_acl_version);
421                 else {
422                         /*
423                          * After upgrade SA_ZPL_ZNODE_ACL should have
424                          * been removed.
425                          */
426                         VERIFY(zp->z_is_sa && error == ENOENT);
427                         return (ZFS_ACL_VERSION_FUID);
428                 }
429         }
430 }
431
432 static int
433 zfs_acl_version(int version)
434 {
435         if (version < ZPL_VERSION_FUID)
436                 return (ZFS_ACL_VERSION_INITIAL);
437         else
438                 return (ZFS_ACL_VERSION_FUID);
439 }
440
441 static int
442 zfs_acl_version_zp(znode_t *zp)
443 {
444         return (zfs_acl_version(zp->z_zfsvfs->z_version));
445 }
446
447 zfs_acl_t *
448 zfs_acl_alloc(int vers)
449 {
450         zfs_acl_t *aclp;
451
452         aclp = kmem_zalloc(sizeof (zfs_acl_t), KM_SLEEP);
453         list_create(&aclp->z_acl, sizeof (zfs_acl_node_t),
454             offsetof(zfs_acl_node_t, z_next));
455         aclp->z_version = vers;
456         if (vers == ZFS_ACL_VERSION_FUID)
457                 aclp->z_ops = zfs_acl_fuid_ops;
458         else
459                 aclp->z_ops = zfs_acl_v0_ops;
460         return (aclp);
461 }
462
463 zfs_acl_node_t *
464 zfs_acl_node_alloc(size_t bytes)
465 {
466         zfs_acl_node_t *aclnode;
467
468         aclnode = kmem_zalloc(sizeof (zfs_acl_node_t), KM_SLEEP);
469         if (bytes) {
470                 aclnode->z_acldata = kmem_alloc(bytes, KM_SLEEP);
471                 aclnode->z_allocdata = aclnode->z_acldata;
472                 aclnode->z_allocsize = bytes;
473                 aclnode->z_size = bytes;
474         }
475
476         return (aclnode);
477 }
478
479 static void
480 zfs_acl_node_free(zfs_acl_node_t *aclnode)
481 {
482         if (aclnode->z_allocsize)
483                 kmem_free(aclnode->z_allocdata, aclnode->z_allocsize);
484         kmem_free(aclnode, sizeof (zfs_acl_node_t));
485 }
486
487 static void
488 zfs_acl_release_nodes(zfs_acl_t *aclp)
489 {
490         zfs_acl_node_t *aclnode;
491
492         while (aclnode = list_head(&aclp->z_acl)) {
493                 list_remove(&aclp->z_acl, aclnode);
494                 zfs_acl_node_free(aclnode);
495         }
496         aclp->z_acl_count = 0;
497         aclp->z_acl_bytes = 0;
498 }
499
500 void
501 zfs_acl_free(zfs_acl_t *aclp)
502 {
503         zfs_acl_release_nodes(aclp);
504         list_destroy(&aclp->z_acl);
505         kmem_free(aclp, sizeof (zfs_acl_t));
506 }
507
508 static boolean_t
509 zfs_acl_valid_ace_type(uint_t type, uint_t flags)
510 {
511         uint16_t entry_type;
512
513         switch (type) {
514         case ALLOW:
515         case DENY:
516         case ACE_SYSTEM_AUDIT_ACE_TYPE:
517         case ACE_SYSTEM_ALARM_ACE_TYPE:
518                 entry_type = flags & ACE_TYPE_FLAGS;
519                 return (entry_type == ACE_OWNER ||
520                     entry_type == OWNING_GROUP ||
521                     entry_type == ACE_EVERYONE || entry_type == 0 ||
522                     entry_type == ACE_IDENTIFIER_GROUP);
523         default:
524                 if (type >= MIN_ACE_TYPE && type <= MAX_ACE_TYPE)
525                         return (B_TRUE);
526         }
527         return (B_FALSE);
528 }
529
530 static boolean_t
531 zfs_ace_valid(vtype_t obj_type, zfs_acl_t *aclp, uint16_t type, uint16_t iflags)
532 {
533         /*
534          * first check type of entry
535          */
536
537         if (!zfs_acl_valid_ace_type(type, iflags))
538                 return (B_FALSE);
539
540         switch (type) {
541         case ACE_ACCESS_ALLOWED_OBJECT_ACE_TYPE:
542         case ACE_ACCESS_DENIED_OBJECT_ACE_TYPE:
543         case ACE_SYSTEM_AUDIT_OBJECT_ACE_TYPE:
544         case ACE_SYSTEM_ALARM_OBJECT_ACE_TYPE:
545                 if (aclp->z_version < ZFS_ACL_VERSION_FUID)
546                         return (B_FALSE);
547                 aclp->z_hints |= ZFS_ACL_OBJ_ACE;
548         }
549
550         /*
551          * next check inheritance level flags
552          */
553
554         if (obj_type == VDIR &&
555             (iflags & (ACE_FILE_INHERIT_ACE|ACE_DIRECTORY_INHERIT_ACE)))
556                 aclp->z_hints |= ZFS_INHERIT_ACE;
557
558         if (iflags & (ACE_INHERIT_ONLY_ACE|ACE_NO_PROPAGATE_INHERIT_ACE)) {
559                 if ((iflags & (ACE_FILE_INHERIT_ACE|
560                     ACE_DIRECTORY_INHERIT_ACE)) == 0) {
561                         return (B_FALSE);
562                 }
563         }
564
565         return (B_TRUE);
566 }
567
568 static void *
569 zfs_acl_next_ace(zfs_acl_t *aclp, void *start, uint64_t *who,
570     uint32_t *access_mask, uint16_t *iflags, uint16_t *type)
571 {
572         zfs_acl_node_t *aclnode;
573
574         ASSERT(aclp);
575
576         if (start == NULL) {
577                 aclnode = list_head(&aclp->z_acl);
578                 if (aclnode == NULL)
579                         return (NULL);
580
581                 aclp->z_next_ace = aclnode->z_acldata;
582                 aclp->z_curr_node = aclnode;
583                 aclnode->z_ace_idx = 0;
584         }
585
586         aclnode = aclp->z_curr_node;
587
588         if (aclnode == NULL)
589                 return (NULL);
590
591         if (aclnode->z_ace_idx >= aclnode->z_ace_count) {
592                 aclnode = list_next(&aclp->z_acl, aclnode);
593                 if (aclnode == NULL)
594                         return (NULL);
595                 else {
596                         aclp->z_curr_node = aclnode;
597                         aclnode->z_ace_idx = 0;
598                         aclp->z_next_ace = aclnode->z_acldata;
599                 }
600         }
601
602         if (aclnode->z_ace_idx < aclnode->z_ace_count) {
603                 void *acep = aclp->z_next_ace;
604                 size_t ace_size;
605
606                 /*
607                  * Make sure we don't overstep our bounds
608                  */
609                 ace_size = aclp->z_ops.ace_size(acep);
610
611                 if (((caddr_t)acep + ace_size) >
612                     ((caddr_t)aclnode->z_acldata + aclnode->z_size)) {
613                         return (NULL);
614                 }
615
616                 *iflags = aclp->z_ops.ace_flags_get(acep);
617                 *type = aclp->z_ops.ace_type_get(acep);
618                 *access_mask = aclp->z_ops.ace_mask_get(acep);
619                 *who = aclp->z_ops.ace_who_get(acep);
620                 aclp->z_next_ace = (caddr_t)aclp->z_next_ace + ace_size;
621                 aclnode->z_ace_idx++;
622
623                 return ((void *)acep);
624         }
625         return (NULL);
626 }
627
628 /*ARGSUSED*/
629 static uint64_t
630 zfs_ace_walk(void *datap, uint64_t cookie, int aclcnt,
631     uint16_t *flags, uint16_t *type, uint32_t *mask)
632 {
633         zfs_acl_t *aclp = datap;
634         zfs_ace_hdr_t *acep = (zfs_ace_hdr_t *)(uintptr_t)cookie;
635         uint64_t who;
636
637         acep = zfs_acl_next_ace(aclp, acep, &who, mask,
638             flags, type);
639         return ((uint64_t)(uintptr_t)acep);
640 }
641
642 static zfs_acl_node_t *
643 zfs_acl_curr_node(zfs_acl_t *aclp)
644 {
645         ASSERT(aclp->z_curr_node);
646         return (aclp->z_curr_node);
647 }
648
649 /*
650  * Copy ACE to internal ZFS format.
651  * While processing the ACL each ACE will be validated for correctness.
652  * ACE FUIDs will be created later.
653  */
654 int
655 zfs_copy_ace_2_fuid(zfsvfs_t *zfsvfs, vtype_t obj_type, zfs_acl_t *aclp,
656     void *datap, zfs_ace_t *z_acl, uint64_t aclcnt, size_t *size,
657     zfs_fuid_info_t **fuidp, cred_t *cr)
658 {
659         int i;
660         uint16_t entry_type;
661         zfs_ace_t *aceptr = z_acl;
662         ace_t *acep = datap;
663         zfs_object_ace_t *zobjacep;
664         ace_object_t *aceobjp;
665
666         for (i = 0; i != aclcnt; i++) {
667                 aceptr->z_hdr.z_access_mask = acep->a_access_mask;
668                 aceptr->z_hdr.z_flags = acep->a_flags;
669                 aceptr->z_hdr.z_type = acep->a_type;
670                 entry_type = aceptr->z_hdr.z_flags & ACE_TYPE_FLAGS;
671                 if (entry_type != ACE_OWNER && entry_type != OWNING_GROUP &&
672                     entry_type != ACE_EVERYONE) {
673                         aceptr->z_fuid = zfs_fuid_create(zfsvfs, acep->a_who,
674                             cr, (entry_type == 0) ?
675                             ZFS_ACE_USER : ZFS_ACE_GROUP, fuidp);
676                 }
677
678                 /*
679                  * Make sure ACE is valid
680                  */
681                 if (zfs_ace_valid(obj_type, aclp, aceptr->z_hdr.z_type,
682                     aceptr->z_hdr.z_flags) != B_TRUE)
683                         return (EINVAL);
684
685                 switch (acep->a_type) {
686                 case ACE_ACCESS_ALLOWED_OBJECT_ACE_TYPE:
687                 case ACE_ACCESS_DENIED_OBJECT_ACE_TYPE:
688                 case ACE_SYSTEM_AUDIT_OBJECT_ACE_TYPE:
689                 case ACE_SYSTEM_ALARM_OBJECT_ACE_TYPE:
690                         zobjacep = (zfs_object_ace_t *)aceptr;
691                         aceobjp = (ace_object_t *)acep;
692
693                         bcopy(aceobjp->a_obj_type, zobjacep->z_object_type,
694                             sizeof (aceobjp->a_obj_type));
695                         bcopy(aceobjp->a_inherit_obj_type,
696                             zobjacep->z_inherit_type,
697                             sizeof (aceobjp->a_inherit_obj_type));
698                         acep = (ace_t *)((caddr_t)acep + sizeof (ace_object_t));
699                         break;
700                 default:
701                         acep = (ace_t *)((caddr_t)acep + sizeof (ace_t));
702                 }
703
704                 aceptr = (zfs_ace_t *)((caddr_t)aceptr +
705                     aclp->z_ops.ace_size(aceptr));
706         }
707
708         *size = (caddr_t)aceptr - (caddr_t)z_acl;
709
710         return (0);
711 }
712
713 /*
714  * Copy ZFS ACEs to fixed size ace_t layout
715  */
716 static void
717 zfs_copy_fuid_2_ace(zfsvfs_t *zfsvfs, zfs_acl_t *aclp, cred_t *cr,
718     void *datap, int filter)
719 {
720         uint64_t who;
721         uint32_t access_mask;
722         uint16_t iflags, type;
723         zfs_ace_hdr_t *zacep = NULL;
724         ace_t *acep = datap;
725         ace_object_t *objacep;
726         zfs_object_ace_t *zobjacep;
727         size_t ace_size;
728         uint16_t entry_type;
729
730         while (zacep = zfs_acl_next_ace(aclp, zacep,
731             &who, &access_mask, &iflags, &type)) {
732
733                 switch (type) {
734                 case ACE_ACCESS_ALLOWED_OBJECT_ACE_TYPE:
735                 case ACE_ACCESS_DENIED_OBJECT_ACE_TYPE:
736                 case ACE_SYSTEM_AUDIT_OBJECT_ACE_TYPE:
737                 case ACE_SYSTEM_ALARM_OBJECT_ACE_TYPE:
738                         if (filter) {
739                                 continue;
740                         }
741                         zobjacep = (zfs_object_ace_t *)zacep;
742                         objacep = (ace_object_t *)acep;
743                         bcopy(zobjacep->z_object_type,
744                             objacep->a_obj_type,
745                             sizeof (zobjacep->z_object_type));
746                         bcopy(zobjacep->z_inherit_type,
747                             objacep->a_inherit_obj_type,
748                             sizeof (zobjacep->z_inherit_type));
749                         ace_size = sizeof (ace_object_t);
750                         break;
751                 default:
752                         ace_size = sizeof (ace_t);
753                         break;
754                 }
755
756                 entry_type = (iflags & ACE_TYPE_FLAGS);
757                 if ((entry_type != ACE_OWNER &&
758                     entry_type != OWNING_GROUP &&
759                     entry_type != ACE_EVERYONE)) {
760                         acep->a_who = zfs_fuid_map_id(zfsvfs, who,
761                             cr, (entry_type & ACE_IDENTIFIER_GROUP) ?
762                             ZFS_ACE_GROUP : ZFS_ACE_USER);
763                 } else {
764                         acep->a_who = (uid_t)(int64_t)who;
765                 }
766                 acep->a_access_mask = access_mask;
767                 acep->a_flags = iflags;
768                 acep->a_type = type;
769                 acep = (ace_t *)((caddr_t)acep + ace_size);
770         }
771 }
772
773 static int
774 zfs_copy_ace_2_oldace(vtype_t obj_type, zfs_acl_t *aclp, ace_t *acep,
775     zfs_oldace_t *z_acl, int aclcnt, size_t *size)
776 {
777         int i;
778         zfs_oldace_t *aceptr = z_acl;
779
780         for (i = 0; i != aclcnt; i++, aceptr++) {
781                 aceptr->z_access_mask = acep[i].a_access_mask;
782                 aceptr->z_type = acep[i].a_type;
783                 aceptr->z_flags = acep[i].a_flags;
784                 aceptr->z_fuid = acep[i].a_who;
785                 /*
786                  * Make sure ACE is valid
787                  */
788                 if (zfs_ace_valid(obj_type, aclp, aceptr->z_type,
789                     aceptr->z_flags) != B_TRUE)
790                         return (EINVAL);
791         }
792         *size = (caddr_t)aceptr - (caddr_t)z_acl;
793         return (0);
794 }
795
796 /*
797  * convert old ACL format to new
798  */
799 void
800 zfs_acl_xform(znode_t *zp, zfs_acl_t *aclp, cred_t *cr)
801 {
802         zfs_oldace_t *oldaclp;
803         int i;
804         uint16_t type, iflags;
805         uint32_t access_mask;
806         uint64_t who;
807         void *cookie = NULL;
808         zfs_acl_node_t *newaclnode;
809
810         ASSERT(aclp->z_version == ZFS_ACL_VERSION_INITIAL);
811         /*
812          * First create the ACE in a contiguous piece of memory
813          * for zfs_copy_ace_2_fuid().
814          *
815          * We only convert an ACL once, so this won't happen
816          * everytime.
817          */
818         oldaclp = kmem_alloc(sizeof (zfs_oldace_t) * aclp->z_acl_count,
819             KM_SLEEP);
820         i = 0;
821         while (cookie = zfs_acl_next_ace(aclp, cookie, &who,
822             &access_mask, &iflags, &type)) {
823                 oldaclp[i].z_flags = iflags;
824                 oldaclp[i].z_type = type;
825                 oldaclp[i].z_fuid = who;
826                 oldaclp[i++].z_access_mask = access_mask;
827         }
828
829         newaclnode = zfs_acl_node_alloc(aclp->z_acl_count *
830             sizeof (zfs_object_ace_t));
831         aclp->z_ops = zfs_acl_fuid_ops;
832         VERIFY(zfs_copy_ace_2_fuid(zp->z_zfsvfs, ZTOV(zp)->v_type, aclp,
833             oldaclp, newaclnode->z_acldata, aclp->z_acl_count,
834             &newaclnode->z_size, NULL, cr) == 0);
835         newaclnode->z_ace_count = aclp->z_acl_count;
836         aclp->z_version = ZFS_ACL_VERSION;
837         kmem_free(oldaclp, aclp->z_acl_count * sizeof (zfs_oldace_t));
838
839         /*
840          * Release all previous ACL nodes
841          */
842
843         zfs_acl_release_nodes(aclp);
844
845         list_insert_head(&aclp->z_acl, newaclnode);
846
847         aclp->z_acl_bytes = newaclnode->z_size;
848         aclp->z_acl_count = newaclnode->z_ace_count;
849
850 }
851
852 /*
853  * Convert unix access mask to v4 access mask
854  */
855 static uint32_t
856 zfs_unix_to_v4(uint32_t access_mask)
857 {
858         uint32_t new_mask = 0;
859
860         if (access_mask & S_IXOTH)
861                 new_mask |= ACE_EXECUTE;
862         if (access_mask & S_IWOTH)
863                 new_mask |= ACE_WRITE_DATA;
864         if (access_mask & S_IROTH)
865                 new_mask |= ACE_READ_DATA;
866         return (new_mask);
867 }
868
869 static void
870 zfs_set_ace(zfs_acl_t *aclp, void *acep, uint32_t access_mask,
871     uint16_t access_type, uint64_t fuid, uint16_t entry_type)
872 {
873         uint16_t type = entry_type & ACE_TYPE_FLAGS;
874
875         aclp->z_ops.ace_mask_set(acep, access_mask);
876         aclp->z_ops.ace_type_set(acep, access_type);
877         aclp->z_ops.ace_flags_set(acep, entry_type);
878         if ((type != ACE_OWNER && type != OWNING_GROUP &&
879             type != ACE_EVERYONE))
880                 aclp->z_ops.ace_who_set(acep, fuid);
881 }
882
883 /*
884  * Determine mode of file based on ACL.
885  * Also, create FUIDs for any User/Group ACEs
886  */
887 uint64_t
888 zfs_mode_compute(uint64_t fmode, zfs_acl_t *aclp,
889     uint64_t *pflags, uint64_t fuid, uint64_t fgid)
890 {
891         int             entry_type;
892         mode_t          mode;
893         mode_t          seen = 0;
894         zfs_ace_hdr_t   *acep = NULL;
895         uint64_t        who;
896         uint16_t        iflags, type;
897         uint32_t        access_mask;
898         boolean_t       an_exec_denied = B_FALSE;
899
900         mode = (fmode & (S_IFMT | S_ISUID | S_ISGID | S_ISVTX));
901
902         while (acep = zfs_acl_next_ace(aclp, acep, &who,
903             &access_mask, &iflags, &type)) {
904
905                 if (!zfs_acl_valid_ace_type(type, iflags))
906                         continue;
907
908                 entry_type = (iflags & ACE_TYPE_FLAGS);
909
910                 /*
911                  * Skip over owner@, group@ or everyone@ inherit only ACEs
912                  */
913                 if ((iflags & ACE_INHERIT_ONLY_ACE) &&
914                     (entry_type == ACE_OWNER || entry_type == ACE_EVERYONE ||
915                     entry_type == OWNING_GROUP))
916                         continue;
917
918                 if (entry_type == ACE_OWNER || (entry_type == 0 &&
919                     who == fuid)) {
920                         if ((access_mask & ACE_READ_DATA) &&
921                             (!(seen & S_IRUSR))) {
922                                 seen |= S_IRUSR;
923                                 if (type == ALLOW) {
924                                         mode |= S_IRUSR;
925                                 }
926                         }
927                         if ((access_mask & ACE_WRITE_DATA) &&
928                             (!(seen & S_IWUSR))) {
929                                 seen |= S_IWUSR;
930                                 if (type == ALLOW) {
931                                         mode |= S_IWUSR;
932                                 }
933                         }
934                         if ((access_mask & ACE_EXECUTE) &&
935                             (!(seen & S_IXUSR))) {
936                                 seen |= S_IXUSR;
937                                 if (type == ALLOW) {
938                                         mode |= S_IXUSR;
939                                 }
940                         }
941                 } else if (entry_type == OWNING_GROUP ||
942                     (entry_type == ACE_IDENTIFIER_GROUP && who == fgid)) {
943                         if ((access_mask & ACE_READ_DATA) &&
944                             (!(seen & S_IRGRP))) {
945                                 seen |= S_IRGRP;
946                                 if (type == ALLOW) {
947                                         mode |= S_IRGRP;
948                                 }
949                         }
950                         if ((access_mask & ACE_WRITE_DATA) &&
951                             (!(seen & S_IWGRP))) {
952                                 seen |= S_IWGRP;
953                                 if (type == ALLOW) {
954                                         mode |= S_IWGRP;
955                                 }
956                         }
957                         if ((access_mask & ACE_EXECUTE) &&
958                             (!(seen & S_IXGRP))) {
959                                 seen |= S_IXGRP;
960                                 if (type == ALLOW) {
961                                         mode |= S_IXGRP;
962                                 }
963                         }
964                 } else if (entry_type == ACE_EVERYONE) {
965                         if ((access_mask & ACE_READ_DATA)) {
966                                 if (!(seen & S_IRUSR)) {
967                                         seen |= S_IRUSR;
968                                         if (type == ALLOW) {
969                                                 mode |= S_IRUSR;
970                                         }
971                                 }
972                                 if (!(seen & S_IRGRP)) {
973                                         seen |= S_IRGRP;
974                                         if (type == ALLOW) {
975                                                 mode |= S_IRGRP;
976                                         }
977                                 }
978                                 if (!(seen & S_IROTH)) {
979                                         seen |= S_IROTH;
980                                         if (type == ALLOW) {
981                                                 mode |= S_IROTH;
982                                         }
983                                 }
984                         }
985                         if ((access_mask & ACE_WRITE_DATA)) {
986                                 if (!(seen & S_IWUSR)) {
987                                         seen |= S_IWUSR;
988                                         if (type == ALLOW) {
989                                                 mode |= S_IWUSR;
990                                         }
991                                 }
992                                 if (!(seen & S_IWGRP)) {
993                                         seen |= S_IWGRP;
994                                         if (type == ALLOW) {
995                                                 mode |= S_IWGRP;
996                                         }
997                                 }
998                                 if (!(seen & S_IWOTH)) {
999                                         seen |= S_IWOTH;
1000                                         if (type == ALLOW) {
1001                                                 mode |= S_IWOTH;
1002                                         }
1003                                 }
1004                         }
1005                         if ((access_mask & ACE_EXECUTE)) {
1006                                 if (!(seen & S_IXUSR)) {
1007                                         seen |= S_IXUSR;
1008                                         if (type == ALLOW) {
1009                                                 mode |= S_IXUSR;
1010                                         }
1011                                 }
1012                                 if (!(seen & S_IXGRP)) {
1013                                         seen |= S_IXGRP;
1014                                         if (type == ALLOW) {
1015                                                 mode |= S_IXGRP;
1016                                         }
1017                                 }
1018                                 if (!(seen & S_IXOTH)) {
1019                                         seen |= S_IXOTH;
1020                                         if (type == ALLOW) {
1021                                                 mode |= S_IXOTH;
1022                                         }
1023                                 }
1024                         }
1025                 } else {
1026                         /*
1027                          * Only care if this IDENTIFIER_GROUP or
1028                          * USER ACE denies execute access to someone,
1029                          * mode is not affected
1030                          */
1031                         if ((access_mask & ACE_EXECUTE) && type == DENY)
1032                                 an_exec_denied = B_TRUE;
1033                 }
1034         }
1035
1036         /*
1037          * Failure to allow is effectively a deny, so execute permission
1038          * is denied if it was never mentioned or if we explicitly
1039          * weren't allowed it.
1040          */
1041         if (!an_exec_denied &&
1042             ((seen & ALL_MODE_EXECS) != ALL_MODE_EXECS ||
1043             (mode & ALL_MODE_EXECS) != ALL_MODE_EXECS))
1044                 an_exec_denied = B_TRUE;
1045
1046         if (an_exec_denied)
1047                 *pflags &= ~ZFS_NO_EXECS_DENIED;
1048         else
1049                 *pflags |= ZFS_NO_EXECS_DENIED;
1050
1051         return (mode);
1052 }
1053
1054 /*
1055  * Read an external acl object.  If the intent is to modify, always
1056  * create a new acl and leave any cached acl in place.
1057  */
1058 static int
1059 zfs_acl_node_read(znode_t *zp, boolean_t have_lock, zfs_acl_t **aclpp,
1060     boolean_t will_modify)
1061 {
1062         zfs_acl_t       *aclp;
1063         int             aclsize;
1064         int             acl_count;
1065         zfs_acl_node_t  *aclnode;
1066         zfs_acl_phys_t  znode_acl;
1067         int             version;
1068         int             error;
1069         boolean_t       drop_lock = B_FALSE;
1070
1071         ASSERT(MUTEX_HELD(&zp->z_acl_lock));
1072
1073         if (zp->z_acl_cached && !will_modify) {
1074                 *aclpp = zp->z_acl_cached;
1075                 return (0);
1076         }
1077
1078         /*
1079          * close race where znode could be upgrade while trying to
1080          * read the znode attributes.
1081          *
1082          * But this could only happen if the file isn't already an SA
1083          * znode
1084          */
1085         if (!zp->z_is_sa && !have_lock) {
1086                 mutex_enter(&zp->z_lock);
1087                 drop_lock = B_TRUE;
1088         }
1089         version = zfs_znode_acl_version(zp);
1090
1091         if ((error = zfs_acl_znode_info(zp, &aclsize,
1092             &acl_count, &znode_acl)) != 0) {
1093                 goto done;
1094         }
1095
1096         aclp = zfs_acl_alloc(version);
1097
1098         aclp->z_acl_count = acl_count;
1099         aclp->z_acl_bytes = aclsize;
1100
1101         aclnode = zfs_acl_node_alloc(aclsize);
1102         aclnode->z_ace_count = aclp->z_acl_count;
1103         aclnode->z_size = aclsize;
1104
1105         if (!zp->z_is_sa) {
1106                 if (znode_acl.z_acl_extern_obj) {
1107                         error = dmu_read(zp->z_zfsvfs->z_os,
1108                             znode_acl.z_acl_extern_obj, 0, aclnode->z_size,
1109                             aclnode->z_acldata, DMU_READ_PREFETCH);
1110                 } else {
1111                         bcopy(znode_acl.z_ace_data, aclnode->z_acldata,
1112                             aclnode->z_size);
1113                 }
1114         } else {
1115                 error = sa_lookup(zp->z_sa_hdl, SA_ZPL_DACL_ACES(zp->z_zfsvfs),
1116                     aclnode->z_acldata, aclnode->z_size);
1117         }
1118
1119         if (error != 0) {
1120                 zfs_acl_free(aclp);
1121                 zfs_acl_node_free(aclnode);
1122                 /* convert checksum errors into IO errors */
1123                 if (error == ECKSUM)
1124                         error = EIO;
1125                 goto done;
1126         }
1127
1128         list_insert_head(&aclp->z_acl, aclnode);
1129
1130         *aclpp = aclp;
1131         if (!will_modify)
1132                 zp->z_acl_cached = aclp;
1133 done:
1134         if (drop_lock)
1135                 mutex_exit(&zp->z_lock);
1136         return (error);
1137 }
1138
1139 /*ARGSUSED*/
1140 void
1141 zfs_acl_data_locator(void **dataptr, uint32_t *length, uint32_t buflen,
1142     boolean_t start, void *userdata)
1143 {
1144         zfs_acl_locator_cb_t *cb = (zfs_acl_locator_cb_t *)userdata;
1145
1146         if (start) {
1147                 cb->cb_acl_node = list_head(&cb->cb_aclp->z_acl);
1148         } else {
1149                 cb->cb_acl_node = list_next(&cb->cb_aclp->z_acl,
1150                     cb->cb_acl_node);
1151         }
1152         *dataptr = cb->cb_acl_node->z_acldata;
1153         *length = cb->cb_acl_node->z_size;
1154 }
1155
1156 int
1157 zfs_acl_chown_setattr(znode_t *zp)
1158 {
1159         int error;
1160         zfs_acl_t *aclp;
1161
1162         ASSERT(MUTEX_HELD(&zp->z_lock));
1163         ASSERT(MUTEX_HELD(&zp->z_acl_lock));
1164
1165         if ((error = zfs_acl_node_read(zp, B_TRUE, &aclp, B_FALSE)) == 0)
1166                 zp->z_mode = zfs_mode_compute(zp->z_mode, aclp,
1167                     &zp->z_pflags, zp->z_uid, zp->z_gid);
1168         return (error);
1169 }
1170
1171 /*
1172  * common code for setting ACLs.
1173  *
1174  * This function is called from zfs_mode_update, zfs_perm_init, and zfs_setacl.
1175  * zfs_setacl passes a non-NULL inherit pointer (ihp) to indicate that it's
1176  * already checked the acl and knows whether to inherit.
1177  */
1178 int
1179 zfs_aclset_common(znode_t *zp, zfs_acl_t *aclp, cred_t *cr, dmu_tx_t *tx)
1180 {
1181         int                     error;
1182         zfsvfs_t                *zfsvfs = zp->z_zfsvfs;
1183         dmu_object_type_t       otype;
1184         zfs_acl_locator_cb_t    locate = { 0 };
1185         uint64_t                mode;
1186         sa_bulk_attr_t          bulk[5];
1187         uint64_t                ctime[2];
1188         int                     count = 0;
1189
1190         mode = zp->z_mode;
1191
1192         mode = zfs_mode_compute(mode, aclp, &zp->z_pflags,
1193             zp->z_uid, zp->z_gid);
1194
1195         zp->z_mode = mode;
1196         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL,
1197             &mode, sizeof (mode));
1198         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
1199             &zp->z_pflags, sizeof (zp->z_pflags));
1200         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
1201             &ctime, sizeof (ctime));
1202
1203         if (zp->z_acl_cached) {
1204                 zfs_acl_free(zp->z_acl_cached);
1205                 zp->z_acl_cached = NULL;
1206         }
1207
1208         /*
1209          * Upgrade needed?
1210          */
1211         if (!zfsvfs->z_use_fuids) {
1212                 otype = DMU_OT_OLDACL;
1213         } else {
1214                 if ((aclp->z_version == ZFS_ACL_VERSION_INITIAL) &&
1215                     (zfsvfs->z_version >= ZPL_VERSION_FUID))
1216                         zfs_acl_xform(zp, aclp, cr);
1217                 ASSERT(aclp->z_version >= ZFS_ACL_VERSION_FUID);
1218                 otype = DMU_OT_ACL;
1219         }
1220
1221         /*
1222          * Arrgh, we have to handle old on disk format
1223          * as well as newer (preferred) SA format.
1224          */
1225
1226         if (zp->z_is_sa) { /* the easy case, just update the ACL attribute */
1227                 locate.cb_aclp = aclp;
1228                 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_DACL_ACES(zfsvfs),
1229                     zfs_acl_data_locator, &locate, aclp->z_acl_bytes);
1230                 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_DACL_COUNT(zfsvfs),
1231                     NULL, &aclp->z_acl_count, sizeof (uint64_t));
1232         } else { /* Painful legacy way */
1233                 zfs_acl_node_t *aclnode;
1234                 uint64_t off = 0;
1235                 zfs_acl_phys_t acl_phys;
1236                 uint64_t aoid;
1237
1238                 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_ZNODE_ACL(zfsvfs),
1239                     &acl_phys, sizeof (acl_phys))) != 0)
1240                         return (error);
1241
1242                 aoid = acl_phys.z_acl_extern_obj;
1243
1244                 if (aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1245                         /*
1246                          * If ACL was previously external and we are now
1247                          * converting to new ACL format then release old
1248                          * ACL object and create a new one.
1249                          */
1250                         if (aoid &&
1251                             aclp->z_version != acl_phys.z_acl_version) {
1252                                 error = dmu_object_free(zfsvfs->z_os, aoid, tx);
1253                                 if (error)
1254                                         return (error);
1255                                 aoid = 0;
1256                         }
1257                         if (aoid == 0) {
1258                                 aoid = dmu_object_alloc(zfsvfs->z_os,
1259                                     otype, aclp->z_acl_bytes,
1260                                     otype == DMU_OT_ACL ?
1261                                     DMU_OT_SYSACL : DMU_OT_NONE,
1262                                     otype == DMU_OT_ACL ?
1263                                     DN_MAX_BONUSLEN : 0, tx);
1264                         } else {
1265                                 (void) dmu_object_set_blocksize(zfsvfs->z_os,
1266                                     aoid, aclp->z_acl_bytes, 0, tx);
1267                         }
1268                         acl_phys.z_acl_extern_obj = aoid;
1269                         for (aclnode = list_head(&aclp->z_acl); aclnode;
1270                             aclnode = list_next(&aclp->z_acl, aclnode)) {
1271                                 if (aclnode->z_ace_count == 0)
1272                                         continue;
1273                                 dmu_write(zfsvfs->z_os, aoid, off,
1274                                     aclnode->z_size, aclnode->z_acldata, tx);
1275                                 off += aclnode->z_size;
1276                         }
1277                 } else {
1278                         void *start = acl_phys.z_ace_data;
1279                         /*
1280                          * Migrating back embedded?
1281                          */
1282                         if (acl_phys.z_acl_extern_obj) {
1283                                 error = dmu_object_free(zfsvfs->z_os,
1284                                     acl_phys.z_acl_extern_obj, tx);
1285                                 if (error)
1286                                         return (error);
1287                                 acl_phys.z_acl_extern_obj = 0;
1288                         }
1289
1290                         for (aclnode = list_head(&aclp->z_acl); aclnode;
1291                             aclnode = list_next(&aclp->z_acl, aclnode)) {
1292                                 if (aclnode->z_ace_count == 0)
1293                                         continue;
1294                                 bcopy(aclnode->z_acldata, start,
1295                                     aclnode->z_size);
1296                                 start = (caddr_t)start + aclnode->z_size;
1297                         }
1298                 }
1299                 /*
1300                  * If Old version then swap count/bytes to match old
1301                  * layout of znode_acl_phys_t.
1302                  */
1303                 if (aclp->z_version == ZFS_ACL_VERSION_INITIAL) {
1304                         acl_phys.z_acl_size = aclp->z_acl_count;
1305                         acl_phys.z_acl_count = aclp->z_acl_bytes;
1306                 } else {
1307                         acl_phys.z_acl_size = aclp->z_acl_bytes;
1308                         acl_phys.z_acl_count = aclp->z_acl_count;
1309                 }
1310                 acl_phys.z_acl_version = aclp->z_version;
1311
1312                 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ZNODE_ACL(zfsvfs), NULL,
1313                     &acl_phys, sizeof (acl_phys));
1314         }
1315
1316         /*
1317          * Replace ACL wide bits, but first clear them.
1318          */
1319         zp->z_pflags &= ~ZFS_ACL_WIDE_FLAGS;
1320
1321         zp->z_pflags |= aclp->z_hints;
1322
1323         if (ace_trivial_common(aclp, 0, zfs_ace_walk) == 0)
1324                 zp->z_pflags |= ZFS_ACL_TRIVIAL;
1325
1326         zfs_tstamp_update_setup(zp, STATE_CHANGED, NULL, ctime, B_TRUE);
1327         return (sa_bulk_update(zp->z_sa_hdl, bulk, count, tx));
1328 }
1329
1330 /*
1331  * Update access mask for prepended ACE
1332  *
1333  * This applies the "groupmask" value for aclmode property.
1334  */
1335 static void
1336 zfs_acl_prepend_fixup(zfs_acl_t *aclp, void  *acep, void  *origacep,
1337     mode_t mode, uint64_t owner)
1338 {
1339         int     rmask, wmask, xmask;
1340         int     user_ace;
1341         uint16_t aceflags;
1342         uint32_t origmask, acepmask;
1343         uint64_t fuid;
1344
1345         aceflags = aclp->z_ops.ace_flags_get(acep);
1346         fuid = aclp->z_ops.ace_who_get(acep);
1347         origmask = aclp->z_ops.ace_mask_get(origacep);
1348         acepmask = aclp->z_ops.ace_mask_get(acep);
1349
1350         user_ace = (!(aceflags &
1351             (ACE_OWNER|ACE_GROUP|ACE_IDENTIFIER_GROUP)));
1352
1353         if (user_ace && (fuid == owner)) {
1354                 rmask = S_IRUSR;
1355                 wmask = S_IWUSR;
1356                 xmask = S_IXUSR;
1357         } else {
1358                 rmask = S_IRGRP;
1359                 wmask = S_IWGRP;
1360                 xmask = S_IXGRP;
1361         }
1362
1363         if (origmask & ACE_READ_DATA) {
1364                 if (mode & rmask) {
1365                         acepmask &= ~ACE_READ_DATA;
1366                 } else {
1367                         acepmask |= ACE_READ_DATA;
1368                 }
1369         }
1370
1371         if (origmask & ACE_WRITE_DATA) {
1372                 if (mode & wmask) {
1373                         acepmask &= ~ACE_WRITE_DATA;
1374                 } else {
1375                         acepmask |= ACE_WRITE_DATA;
1376                 }
1377         }
1378
1379         if (origmask & ACE_APPEND_DATA) {
1380                 if (mode & wmask) {
1381                         acepmask &= ~ACE_APPEND_DATA;
1382                 } else {
1383                         acepmask |= ACE_APPEND_DATA;
1384                 }
1385         }
1386
1387         if (origmask & ACE_EXECUTE) {
1388                 if (mode & xmask) {
1389                         acepmask &= ~ACE_EXECUTE;
1390                 } else {
1391                         acepmask |= ACE_EXECUTE;
1392                 }
1393         }
1394         aclp->z_ops.ace_mask_set(acep, acepmask);
1395 }
1396
1397 static void
1398 zfs_acl_chmod(zfsvfs_t *zfsvfs, uint64_t mode, zfs_acl_t *aclp)
1399 {
1400         void            *acep = NULL;
1401         uint64_t        who;
1402         int             new_count, new_bytes;
1403         int             ace_size;
1404         int             entry_type;
1405         uint16_t        iflags, type;
1406         uint32_t        access_mask;
1407         zfs_acl_node_t  *newnode;
1408         size_t          abstract_size = aclp->z_ops.ace_abstract_size();
1409         void            *zacep;
1410         uint32_t        owner, group, everyone;
1411         uint32_t        deny1, deny2, allow0;
1412
1413         new_count = new_bytes = 0;
1414
1415         acl_trivial_access_masks((mode_t)mode, &allow0, &deny1, &deny2,
1416             &owner, &group, &everyone);
1417
1418         newnode = zfs_acl_node_alloc((abstract_size * 6) + aclp->z_acl_bytes);
1419
1420         zacep = newnode->z_acldata;
1421         if (allow0) {
1422                 zfs_set_ace(aclp, zacep, allow0, ALLOW, -1, ACE_OWNER);
1423                 zacep = (void *)((uintptr_t)zacep + abstract_size);
1424                 new_count++;
1425                 new_bytes += abstract_size;
1426         } if (deny1) {
1427                 zfs_set_ace(aclp, zacep, deny1, DENY, -1, ACE_OWNER);
1428                 zacep = (void *)((uintptr_t)zacep + abstract_size);
1429                 new_count++;
1430                 new_bytes += abstract_size;
1431         }
1432         if (deny2) {
1433                 zfs_set_ace(aclp, zacep, deny2, DENY, -1, OWNING_GROUP);
1434                 zacep = (void *)((uintptr_t)zacep + abstract_size);
1435                 new_count++;
1436                 new_bytes += abstract_size;
1437         }
1438
1439         while (acep = zfs_acl_next_ace(aclp, acep, &who, &access_mask,
1440             &iflags, &type)) {
1441                 uint16_t inherit_flags;
1442
1443                 entry_type = (iflags & ACE_TYPE_FLAGS);
1444                 inherit_flags = (iflags & ALL_INHERIT);
1445
1446                 if ((entry_type == ACE_OWNER || entry_type == ACE_EVERYONE ||
1447                     (entry_type == OWNING_GROUP)) &&
1448                     ((inherit_flags & ACE_INHERIT_ONLY_ACE) == 0)) {
1449                         continue;
1450                 }
1451
1452                 if ((type != ALLOW && type != DENY) ||
1453                     (inherit_flags & ACE_INHERIT_ONLY_ACE)) {
1454                         if (inherit_flags)
1455                                 aclp->z_hints |= ZFS_INHERIT_ACE;
1456                         switch (type) {
1457                         case ACE_ACCESS_ALLOWED_OBJECT_ACE_TYPE:
1458                         case ACE_ACCESS_DENIED_OBJECT_ACE_TYPE:
1459                         case ACE_SYSTEM_AUDIT_OBJECT_ACE_TYPE:
1460                         case ACE_SYSTEM_ALARM_OBJECT_ACE_TYPE:
1461                                 aclp->z_hints |= ZFS_ACL_OBJ_ACE;
1462                                 break;
1463                         }
1464                 } else {
1465
1466                         /*
1467                          * Limit permissions to be no greater than
1468                          * group permissions
1469                          */
1470                         if (type == ALLOW && zfsvfs->z_acl_inherit == ZFS_ACL_RESTRICTED) {
1471                                 if (!(mode & S_IRGRP))
1472                                         access_mask &= ~ACE_READ_DATA;
1473                                 if (!(mode & S_IWGRP))
1474                                         access_mask &=
1475                                             ~(ACE_WRITE_DATA|ACE_APPEND_DATA);
1476                                 if (!(mode & S_IXGRP))
1477                                         access_mask &= ~ACE_EXECUTE;
1478                                 access_mask &=
1479                                     ~(ACE_WRITE_OWNER|ACE_WRITE_ACL|
1480                                     ACE_WRITE_ATTRIBUTES|ACE_WRITE_NAMED_ATTRS);
1481                         }
1482                 }
1483                 zfs_set_ace(aclp, zacep, access_mask, type, who, iflags);
1484                 ace_size = aclp->z_ops.ace_size(acep);
1485                 zacep = (void *)((uintptr_t)zacep + ace_size);
1486                 new_count++;
1487                 new_bytes += ace_size;
1488         }
1489         zfs_set_ace(aclp, zacep, owner, 0, -1, ACE_OWNER);
1490         zacep = (void *)((uintptr_t)zacep + abstract_size);
1491         zfs_set_ace(aclp, zacep, group, 0, -1, OWNING_GROUP);
1492         zacep = (void *)((uintptr_t)zacep + abstract_size);
1493         zfs_set_ace(aclp, zacep, everyone, 0, -1, ACE_EVERYONE);
1494
1495         new_count += 3;
1496         new_bytes += abstract_size * 3;
1497         zfs_acl_release_nodes(aclp);
1498         aclp->z_acl_count = new_count;
1499         aclp->z_acl_bytes = new_bytes;
1500         newnode->z_ace_count = new_count;
1501         newnode->z_size = new_bytes;
1502         list_insert_tail(&aclp->z_acl, newnode);
1503 }
1504
1505 void
1506 zfs_acl_chmod_setattr(znode_t *zp, zfs_acl_t **aclp, uint64_t mode)
1507 {
1508         mutex_enter(&zp->z_acl_lock);
1509         mutex_enter(&zp->z_lock);
1510         *aclp = zfs_acl_alloc(zfs_acl_version_zp(zp));
1511         (*aclp)->z_hints = zp->z_pflags & V4_ACL_WIDE_FLAGS;
1512         zfs_acl_chmod(zp->z_zfsvfs, mode, *aclp);
1513         mutex_exit(&zp->z_lock);
1514         mutex_exit(&zp->z_acl_lock);
1515         ASSERT(*aclp);
1516 }
1517
1518 /*
1519  * strip off write_owner and write_acl
1520  */
1521 static void
1522 zfs_restricted_update(zfsvfs_t *zfsvfs, zfs_acl_t *aclp, void *acep)
1523 {
1524         uint32_t mask = aclp->z_ops.ace_mask_get(acep);
1525
1526         if ((zfsvfs->z_acl_inherit == ZFS_ACL_RESTRICTED) &&
1527             (aclp->z_ops.ace_type_get(acep) == ALLOW)) {
1528                 mask &= ~RESTRICTED_CLEAR;
1529                 aclp->z_ops.ace_mask_set(acep, mask);
1530         }
1531 }
1532
1533 /*
1534  * Should ACE be inherited?
1535  */
1536 static int
1537 zfs_ace_can_use(vtype_t vtype, uint16_t acep_flags)
1538 {
1539         int     iflags = (acep_flags & 0xf);
1540
1541         if ((vtype == VDIR) && (iflags & ACE_DIRECTORY_INHERIT_ACE))
1542                 return (1);
1543         else if (iflags & ACE_FILE_INHERIT_ACE)
1544                 return (!((vtype == VDIR) &&
1545                     (iflags & ACE_NO_PROPAGATE_INHERIT_ACE)));
1546         return (0);
1547 }
1548
1549 /*
1550  * inherit inheritable ACEs from parent
1551  */
1552 static zfs_acl_t *
1553 zfs_acl_inherit(zfsvfs_t *zfsvfs, vtype_t vtype, zfs_acl_t *paclp,
1554     uint64_t mode, boolean_t *need_chmod)
1555 {
1556         void            *pacep;
1557         void            *acep;
1558         zfs_acl_node_t  *aclnode;
1559         zfs_acl_t       *aclp = NULL;
1560         uint64_t        who;
1561         uint32_t        access_mask;
1562         uint16_t        iflags, newflags, type;
1563         size_t          ace_size;
1564         void            *data1, *data2;
1565         size_t          data1sz, data2sz;
1566         boolean_t       vdir = vtype == VDIR;
1567         boolean_t       vreg = vtype == VREG;
1568         boolean_t       passthrough, passthrough_x, noallow;
1569
1570         passthrough_x =
1571             zfsvfs->z_acl_inherit == ZFS_ACL_PASSTHROUGH_X;
1572         passthrough = passthrough_x ||
1573             zfsvfs->z_acl_inherit == ZFS_ACL_PASSTHROUGH;
1574         noallow =
1575             zfsvfs->z_acl_inherit == ZFS_ACL_NOALLOW;
1576
1577         *need_chmod = B_TRUE;
1578         pacep = NULL;
1579         aclp = zfs_acl_alloc(paclp->z_version);
1580         if (zfsvfs->z_acl_inherit == ZFS_ACL_DISCARD || vtype == VLNK)
1581                 return (aclp);
1582         while (pacep = zfs_acl_next_ace(paclp, pacep, &who,
1583             &access_mask, &iflags, &type)) {
1584
1585                 /*
1586                  * don't inherit bogus ACEs
1587                  */
1588                 if (!zfs_acl_valid_ace_type(type, iflags))
1589                         continue;
1590
1591                 if (noallow && type == ALLOW)
1592                         continue;
1593
1594                 ace_size = aclp->z_ops.ace_size(pacep);
1595
1596                 if (!zfs_ace_can_use(vtype, iflags))
1597                         continue;
1598
1599                 /*
1600                  * If owner@, group@, or everyone@ inheritable
1601                  * then zfs_acl_chmod() isn't needed.
1602                  */
1603                 if (passthrough &&
1604                     ((iflags & (ACE_OWNER|ACE_EVERYONE)) ||
1605                     ((iflags & OWNING_GROUP) ==
1606                     OWNING_GROUP)) && (vreg || (vdir && (iflags &
1607                     ACE_DIRECTORY_INHERIT_ACE)))) {
1608                         *need_chmod = B_FALSE;
1609                 }
1610
1611                 if (!vdir && passthrough_x &&
1612                     ((mode & (S_IXUSR | S_IXGRP | S_IXOTH)) == 0)) {
1613                         access_mask &= ~ACE_EXECUTE;
1614                 }
1615
1616                 aclnode = zfs_acl_node_alloc(ace_size);
1617                 list_insert_tail(&aclp->z_acl, aclnode);
1618                 acep = aclnode->z_acldata;
1619
1620                 zfs_set_ace(aclp, acep, access_mask, type,
1621                     who, iflags|ACE_INHERITED_ACE);
1622
1623                 /*
1624                  * Copy special opaque data if any
1625                  */
1626                 if ((data1sz = paclp->z_ops.ace_data(pacep, &data1)) != 0) {
1627                         VERIFY((data2sz = aclp->z_ops.ace_data(acep,
1628                             &data2)) == data1sz);
1629                         bcopy(data1, data2, data2sz);
1630                 }
1631
1632                 aclp->z_acl_count++;
1633                 aclnode->z_ace_count++;
1634                 aclp->z_acl_bytes += aclnode->z_size;
1635                 newflags = aclp->z_ops.ace_flags_get(acep);
1636
1637                 if (vdir)
1638                         aclp->z_hints |= ZFS_INHERIT_ACE;
1639
1640                 if ((iflags & ACE_NO_PROPAGATE_INHERIT_ACE) || !vdir) {
1641                         newflags &= ~ALL_INHERIT;
1642                         aclp->z_ops.ace_flags_set(acep,
1643                             newflags|ACE_INHERITED_ACE);
1644                         zfs_restricted_update(zfsvfs, aclp, acep);
1645                         continue;
1646                 }
1647
1648                 ASSERT(vdir);
1649
1650                 /*
1651                  * If only FILE_INHERIT is set then turn on
1652                  * inherit_only
1653                  */
1654                 if ((iflags & (ACE_FILE_INHERIT_ACE |
1655                     ACE_DIRECTORY_INHERIT_ACE)) == ACE_FILE_INHERIT_ACE) {
1656                         newflags |= ACE_INHERIT_ONLY_ACE;
1657                         aclp->z_ops.ace_flags_set(acep,
1658                             newflags|ACE_INHERITED_ACE);
1659                 } else {
1660                         newflags &= ~ACE_INHERIT_ONLY_ACE;
1661                         aclp->z_ops.ace_flags_set(acep,
1662                             newflags|ACE_INHERITED_ACE);
1663                 }
1664         }
1665         return (aclp);
1666 }
1667
1668 /*
1669  * Create file system object initial permissions
1670  * including inheritable ACEs.
1671  */
1672 int
1673 zfs_acl_ids_create(znode_t *dzp, int flag, vattr_t *vap, cred_t *cr,
1674     vsecattr_t *vsecp, zfs_acl_ids_t *acl_ids)
1675 {
1676         int             error;
1677         zfsvfs_t        *zfsvfs = dzp->z_zfsvfs;
1678         zfs_acl_t       *paclp;
1679         gid_t           gid;
1680         boolean_t       need_chmod = B_TRUE;
1681         boolean_t       inherited = B_FALSE;
1682
1683         bzero(acl_ids, sizeof (zfs_acl_ids_t));
1684         acl_ids->z_mode = MAKEIMODE(vap->va_type, vap->va_mode);
1685
1686         if (vsecp)
1687                 if ((error = zfs_vsec_2_aclp(zfsvfs, vap->va_type, vsecp, cr,
1688                     &acl_ids->z_fuidp, &acl_ids->z_aclp)) != 0)
1689                         return (error);
1690         /*
1691          * Determine uid and gid.
1692          */
1693         if ((flag & IS_ROOT_NODE) || zfsvfs->z_replay ||
1694             ((flag & IS_XATTR) && (vap->va_type == VDIR))) {
1695                 acl_ids->z_fuid = zfs_fuid_create(zfsvfs,
1696                     (uint64_t)vap->va_uid, cr,
1697                     ZFS_OWNER, &acl_ids->z_fuidp);
1698                 acl_ids->z_fgid = zfs_fuid_create(zfsvfs,
1699                     (uint64_t)vap->va_gid, cr,
1700                     ZFS_GROUP, &acl_ids->z_fuidp);
1701                 gid = vap->va_gid;
1702         } else {
1703                 acl_ids->z_fuid = zfs_fuid_create_cred(zfsvfs, ZFS_OWNER,
1704                     cr, &acl_ids->z_fuidp);
1705                 acl_ids->z_fgid = 0;
1706                 if (vap->va_mask & AT_GID)  {
1707                         acl_ids->z_fgid = zfs_fuid_create(zfsvfs,
1708                             (uint64_t)vap->va_gid,
1709                             cr, ZFS_GROUP, &acl_ids->z_fuidp);
1710                         gid = vap->va_gid;
1711                         if (acl_ids->z_fgid != dzp->z_gid &&
1712                             !groupmember(vap->va_gid, cr) &&
1713                             secpolicy_vnode_create_gid(cr) != 0)
1714                                 acl_ids->z_fgid = 0;
1715                 }
1716                 if (acl_ids->z_fgid == 0) {
1717                         if (dzp->z_mode & S_ISGID) {
1718                                 char            *domain;
1719                                 uint32_t        rid;
1720
1721                                 acl_ids->z_fgid = dzp->z_gid;
1722                                 gid = zfs_fuid_map_id(zfsvfs, acl_ids->z_fgid,
1723                                     cr, ZFS_GROUP);
1724
1725                                 if (zfsvfs->z_use_fuids &&
1726                                     IS_EPHEMERAL(acl_ids->z_fgid)) {
1727                                         domain = zfs_fuid_idx_domain(
1728                                             &zfsvfs->z_fuid_idx,
1729                                             FUID_INDEX(acl_ids->z_fgid));
1730                                         rid = FUID_RID(acl_ids->z_fgid);
1731                                         zfs_fuid_node_add(&acl_ids->z_fuidp,
1732                                             domain, rid,
1733                                             FUID_INDEX(acl_ids->z_fgid),
1734                                             acl_ids->z_fgid, ZFS_GROUP);
1735                                 }
1736                         } else {
1737                                 acl_ids->z_fgid = zfs_fuid_create_cred(zfsvfs,
1738                                     ZFS_GROUP, cr, &acl_ids->z_fuidp);
1739 #ifdef __FreeBSD__
1740                                 gid = acl_ids->z_fgid = dzp->z_gid;
1741 #else
1742                                 gid = crgetgid(cr);
1743 #endif
1744                         }
1745                 }
1746         }
1747
1748         /*
1749          * If we're creating a directory, and the parent directory has the
1750          * set-GID bit set, set in on the new directory.
1751          * Otherwise, if the user is neither privileged nor a member of the
1752          * file's new group, clear the file's set-GID bit.
1753          */
1754
1755         if (!(flag & IS_ROOT_NODE) && (dzp->z_mode & S_ISGID) &&
1756             (vap->va_type == VDIR)) {
1757                 acl_ids->z_mode |= S_ISGID;
1758         } else {
1759                 if ((acl_ids->z_mode & S_ISGID) &&
1760                     secpolicy_vnode_setids_setgids(ZTOV(dzp), cr, gid) != 0)
1761                         acl_ids->z_mode &= ~S_ISGID;
1762         }
1763
1764         if (acl_ids->z_aclp == NULL) {
1765                 mutex_enter(&dzp->z_acl_lock);
1766                 mutex_enter(&dzp->z_lock);
1767                 if (!(flag & IS_ROOT_NODE) && (ZTOV(dzp)->v_type == VDIR &&
1768                     (dzp->z_pflags & ZFS_INHERIT_ACE)) &&
1769                     !(dzp->z_pflags & ZFS_XATTR)) {
1770                         VERIFY(0 == zfs_acl_node_read(dzp, B_TRUE,
1771                             &paclp, B_FALSE));
1772                         acl_ids->z_aclp = zfs_acl_inherit(zfsvfs,
1773                             vap->va_type, paclp, acl_ids->z_mode, &need_chmod);
1774                         inherited = B_TRUE;
1775                 } else {
1776                         acl_ids->z_aclp =
1777                             zfs_acl_alloc(zfs_acl_version_zp(dzp));
1778                         acl_ids->z_aclp->z_hints |= ZFS_ACL_TRIVIAL;
1779                 }
1780                 mutex_exit(&dzp->z_lock);
1781                 mutex_exit(&dzp->z_acl_lock);
1782                 if (need_chmod) {
1783                         acl_ids->z_aclp->z_hints |= (vap->va_type == VDIR) ?
1784                             ZFS_ACL_AUTO_INHERIT : 0;
1785                         zfs_acl_chmod(zfsvfs, acl_ids->z_mode, acl_ids->z_aclp);
1786                 }
1787         }
1788
1789         if (inherited || vsecp) {
1790                 acl_ids->z_mode = zfs_mode_compute(acl_ids->z_mode,
1791                     acl_ids->z_aclp, &acl_ids->z_aclp->z_hints,
1792                     acl_ids->z_fuid, acl_ids->z_fgid);
1793                 if (ace_trivial_common(acl_ids->z_aclp, 0, zfs_ace_walk) == 0)
1794                         acl_ids->z_aclp->z_hints |= ZFS_ACL_TRIVIAL;
1795         }
1796
1797         return (0);
1798 }
1799
1800 /*
1801  * Free ACL and fuid_infop, but not the acl_ids structure
1802  */
1803 void
1804 zfs_acl_ids_free(zfs_acl_ids_t *acl_ids)
1805 {
1806         if (acl_ids->z_aclp)
1807                 zfs_acl_free(acl_ids->z_aclp);
1808         if (acl_ids->z_fuidp)
1809                 zfs_fuid_info_free(acl_ids->z_fuidp);
1810         acl_ids->z_aclp = NULL;
1811         acl_ids->z_fuidp = NULL;
1812 }
1813
1814 boolean_t
1815 zfs_acl_ids_overquota(zfsvfs_t *zfsvfs, zfs_acl_ids_t *acl_ids)
1816 {
1817         return (zfs_fuid_overquota(zfsvfs, B_FALSE, acl_ids->z_fuid) ||
1818             zfs_fuid_overquota(zfsvfs, B_TRUE, acl_ids->z_fgid));
1819 }
1820
1821 /*
1822  * Retrieve a files ACL
1823  */
1824 int
1825 zfs_getacl(znode_t *zp, vsecattr_t *vsecp, boolean_t skipaclchk, cred_t *cr)
1826 {
1827         zfs_acl_t       *aclp;
1828         ulong_t         mask;
1829         int             error;
1830         int             count = 0;
1831         int             largeace = 0;
1832
1833         mask = vsecp->vsa_mask & (VSA_ACE | VSA_ACECNT |
1834             VSA_ACE_ACLFLAGS | VSA_ACE_ALLTYPES);
1835
1836         if (mask == 0)
1837                 return (ENOSYS);
1838
1839         if (error = zfs_zaccess(zp, ACE_READ_ACL, 0, skipaclchk, cr))
1840                 return (error);
1841
1842         mutex_enter(&zp->z_acl_lock);
1843
1844         error = zfs_acl_node_read(zp, B_FALSE, &aclp, B_FALSE);
1845         if (error != 0) {
1846                 mutex_exit(&zp->z_acl_lock);
1847                 return (error);
1848         }
1849
1850         /*
1851          * Scan ACL to determine number of ACEs
1852          */
1853         if ((zp->z_pflags & ZFS_ACL_OBJ_ACE) && !(mask & VSA_ACE_ALLTYPES)) {
1854                 void *zacep = NULL;
1855                 uint64_t who;
1856                 uint32_t access_mask;
1857                 uint16_t type, iflags;
1858
1859                 while (zacep = zfs_acl_next_ace(aclp, zacep,
1860                     &who, &access_mask, &iflags, &type)) {
1861                         switch (type) {
1862                         case ACE_ACCESS_ALLOWED_OBJECT_ACE_TYPE:
1863                         case ACE_ACCESS_DENIED_OBJECT_ACE_TYPE:
1864                         case ACE_SYSTEM_AUDIT_OBJECT_ACE_TYPE:
1865                         case ACE_SYSTEM_ALARM_OBJECT_ACE_TYPE:
1866                                 largeace++;
1867                                 continue;
1868                         default:
1869                                 count++;
1870                         }
1871                 }
1872                 vsecp->vsa_aclcnt = count;
1873         } else
1874                 count = (int)aclp->z_acl_count;
1875
1876         if (mask & VSA_ACECNT) {
1877                 vsecp->vsa_aclcnt = count;
1878         }
1879
1880         if (mask & VSA_ACE) {
1881                 size_t aclsz;
1882
1883                 aclsz = count * sizeof (ace_t) +
1884                     sizeof (ace_object_t) * largeace;
1885
1886                 vsecp->vsa_aclentp = kmem_alloc(aclsz, KM_SLEEP);
1887                 vsecp->vsa_aclentsz = aclsz;
1888
1889                 if (aclp->z_version == ZFS_ACL_VERSION_FUID)
1890                         zfs_copy_fuid_2_ace(zp->z_zfsvfs, aclp, cr,
1891                             vsecp->vsa_aclentp, !(mask & VSA_ACE_ALLTYPES));
1892                 else {
1893                         zfs_acl_node_t *aclnode;
1894                         void *start = vsecp->vsa_aclentp;
1895
1896                         for (aclnode = list_head(&aclp->z_acl); aclnode;
1897                             aclnode = list_next(&aclp->z_acl, aclnode)) {
1898                                 bcopy(aclnode->z_acldata, start,
1899                                     aclnode->z_size);
1900                                 start = (caddr_t)start + aclnode->z_size;
1901                         }
1902                         ASSERT((caddr_t)start - (caddr_t)vsecp->vsa_aclentp ==
1903                             aclp->z_acl_bytes);
1904                 }
1905         }
1906         if (mask & VSA_ACE_ACLFLAGS) {
1907                 vsecp->vsa_aclflags = 0;
1908                 if (zp->z_pflags & ZFS_ACL_DEFAULTED)
1909                         vsecp->vsa_aclflags |= ACL_DEFAULTED;
1910                 if (zp->z_pflags & ZFS_ACL_PROTECTED)
1911                         vsecp->vsa_aclflags |= ACL_PROTECTED;
1912                 if (zp->z_pflags & ZFS_ACL_AUTO_INHERIT)
1913                         vsecp->vsa_aclflags |= ACL_AUTO_INHERIT;
1914         }
1915
1916         mutex_exit(&zp->z_acl_lock);
1917
1918         return (0);
1919 }
1920
1921 int
1922 zfs_vsec_2_aclp(zfsvfs_t *zfsvfs, vtype_t obj_type,
1923     vsecattr_t *vsecp, cred_t *cr, zfs_fuid_info_t **fuidp, zfs_acl_t **zaclp)
1924 {
1925         zfs_acl_t *aclp;
1926         zfs_acl_node_t *aclnode;
1927         int aclcnt = vsecp->vsa_aclcnt;
1928         int error;
1929
1930         if (vsecp->vsa_aclcnt > MAX_ACL_ENTRIES || vsecp->vsa_aclcnt <= 0)
1931                 return (EINVAL);
1932
1933         aclp = zfs_acl_alloc(zfs_acl_version(zfsvfs->z_version));
1934
1935         aclp->z_hints = 0;
1936         aclnode = zfs_acl_node_alloc(aclcnt * sizeof (zfs_object_ace_t));
1937         if (aclp->z_version == ZFS_ACL_VERSION_INITIAL) {
1938                 if ((error = zfs_copy_ace_2_oldace(obj_type, aclp,
1939                     (ace_t *)vsecp->vsa_aclentp, aclnode->z_acldata,
1940                     aclcnt, &aclnode->z_size)) != 0) {
1941                         zfs_acl_free(aclp);
1942                         zfs_acl_node_free(aclnode);
1943                         return (error);
1944                 }
1945         } else {
1946                 if ((error = zfs_copy_ace_2_fuid(zfsvfs, obj_type, aclp,
1947                     vsecp->vsa_aclentp, aclnode->z_acldata, aclcnt,
1948                     &aclnode->z_size, fuidp, cr)) != 0) {
1949                         zfs_acl_free(aclp);
1950                         zfs_acl_node_free(aclnode);
1951                         return (error);
1952                 }
1953         }
1954         aclp->z_acl_bytes = aclnode->z_size;
1955         aclnode->z_ace_count = aclcnt;
1956         aclp->z_acl_count = aclcnt;
1957         list_insert_head(&aclp->z_acl, aclnode);
1958
1959         /*
1960          * If flags are being set then add them to z_hints
1961          */
1962         if (vsecp->vsa_mask & VSA_ACE_ACLFLAGS) {
1963                 if (vsecp->vsa_aclflags & ACL_PROTECTED)
1964                         aclp->z_hints |= ZFS_ACL_PROTECTED;
1965                 if (vsecp->vsa_aclflags & ACL_DEFAULTED)
1966                         aclp->z_hints |= ZFS_ACL_DEFAULTED;
1967                 if (vsecp->vsa_aclflags & ACL_AUTO_INHERIT)
1968                         aclp->z_hints |= ZFS_ACL_AUTO_INHERIT;
1969         }
1970
1971         *zaclp = aclp;
1972
1973         return (0);
1974 }
1975
1976 /*
1977  * Set a files ACL
1978  */
1979 int
1980 zfs_setacl(znode_t *zp, vsecattr_t *vsecp, boolean_t skipaclchk, cred_t *cr)
1981 {
1982         zfsvfs_t        *zfsvfs = zp->z_zfsvfs;
1983         zilog_t         *zilog = zfsvfs->z_log;
1984         ulong_t         mask = vsecp->vsa_mask & (VSA_ACE | VSA_ACECNT);
1985         dmu_tx_t        *tx;
1986         int             error;
1987         zfs_acl_t       *aclp;
1988         zfs_fuid_info_t *fuidp = NULL;
1989         boolean_t       fuid_dirtied;
1990         uint64_t        acl_obj;
1991
1992         if (mask == 0)
1993                 return (ENOSYS);
1994
1995         if (zp->z_pflags & ZFS_IMMUTABLE)
1996                 return (EPERM);
1997
1998         if (error = zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr))
1999                 return (error);
2000
2001         error = zfs_vsec_2_aclp(zfsvfs, ZTOV(zp)->v_type, vsecp, cr, &fuidp,
2002             &aclp);
2003         if (error)
2004                 return (error);
2005
2006         /*
2007          * If ACL wide flags aren't being set then preserve any
2008          * existing flags.
2009          */
2010         if (!(vsecp->vsa_mask & VSA_ACE_ACLFLAGS)) {
2011                 aclp->z_hints |=
2012                     (zp->z_pflags & V4_ACL_WIDE_FLAGS);
2013         }
2014 top:
2015         mutex_enter(&zp->z_acl_lock);
2016         mutex_enter(&zp->z_lock);
2017
2018         tx = dmu_tx_create(zfsvfs->z_os);
2019
2020         dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
2021
2022         fuid_dirtied = zfsvfs->z_fuid_dirty;
2023         if (fuid_dirtied)
2024                 zfs_fuid_txhold(zfsvfs, tx);
2025
2026         /*
2027          * If old version and ACL won't fit in bonus and we aren't
2028          * upgrading then take out necessary DMU holds
2029          */
2030
2031         if ((acl_obj = zfs_external_acl(zp)) != 0) {
2032                 if (zfsvfs->z_version >= ZPL_VERSION_FUID &&
2033                     zfs_znode_acl_version(zp) <= ZFS_ACL_VERSION_INITIAL) {
2034                         dmu_tx_hold_free(tx, acl_obj, 0,
2035                             DMU_OBJECT_END);
2036                         dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
2037                             aclp->z_acl_bytes);
2038                 } else {
2039                         dmu_tx_hold_write(tx, acl_obj, 0, aclp->z_acl_bytes);
2040                 }
2041         } else if (!zp->z_is_sa && aclp->z_acl_bytes > ZFS_ACE_SPACE) {
2042                 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, aclp->z_acl_bytes);
2043         }
2044
2045         zfs_sa_upgrade_txholds(tx, zp);
2046         error = dmu_tx_assign(tx, TXG_NOWAIT);
2047         if (error) {
2048                 mutex_exit(&zp->z_acl_lock);
2049                 mutex_exit(&zp->z_lock);
2050
2051                 if (error == ERESTART) {
2052                         dmu_tx_wait(tx);
2053                         dmu_tx_abort(tx);
2054                         goto top;
2055                 }
2056                 dmu_tx_abort(tx);
2057                 zfs_acl_free(aclp);
2058                 return (error);
2059         }
2060
2061         error = zfs_aclset_common(zp, aclp, cr, tx);
2062         ASSERT(error == 0);
2063         ASSERT(zp->z_acl_cached == NULL);
2064         zp->z_acl_cached = aclp;
2065
2066         if (fuid_dirtied)
2067                 zfs_fuid_sync(zfsvfs, tx);
2068
2069         zfs_log_acl(zilog, tx, zp, vsecp, fuidp);
2070
2071         if (fuidp)
2072                 zfs_fuid_info_free(fuidp);
2073         dmu_tx_commit(tx);
2074 done:
2075         mutex_exit(&zp->z_lock);
2076         mutex_exit(&zp->z_acl_lock);
2077
2078         return (error);
2079 }
2080
2081 /*
2082  * Check accesses of interest (AoI) against attributes of the dataset
2083  * such as read-only.  Returns zero if no AoI conflict with dataset
2084  * attributes, otherwise an appropriate errno is returned.
2085  */
2086 static int
2087 zfs_zaccess_dataset_check(znode_t *zp, uint32_t v4_mode)
2088 {
2089         if ((v4_mode & WRITE_MASK) &&
2090             (zp->z_zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) &&
2091             (!IS_DEVVP(ZTOV(zp)) ||
2092             (IS_DEVVP(ZTOV(zp)) && (v4_mode & WRITE_MASK_ATTRS)))) {
2093                 return (EROFS);
2094         }
2095
2096         /*
2097          * Only check for READONLY on non-directories.
2098          */
2099         if ((v4_mode & WRITE_MASK_DATA) &&
2100             (((ZTOV(zp)->v_type != VDIR) &&
2101             (zp->z_pflags & (ZFS_READONLY | ZFS_IMMUTABLE))) ||
2102             (ZTOV(zp)->v_type == VDIR &&
2103             (zp->z_pflags & ZFS_IMMUTABLE)))) {
2104                 return (EPERM);
2105         }
2106
2107 #ifdef sun
2108         if ((v4_mode & (ACE_DELETE | ACE_DELETE_CHILD)) &&
2109             (zp->z_pflags & ZFS_NOUNLINK)) {
2110                 return (EPERM);
2111         }
2112 #else
2113         /*
2114          * In FreeBSD we allow to modify directory's content is ZFS_NOUNLINK
2115          * (sunlnk) is set. We just don't allow directory removal, which is
2116          * handled in zfs_zaccess_delete().
2117          */
2118         if ((v4_mode & ACE_DELETE) &&
2119             (zp->z_pflags & ZFS_NOUNLINK)) {
2120                 return (EPERM);
2121         }
2122 #endif
2123
2124         if (((v4_mode & (ACE_READ_DATA|ACE_EXECUTE)) &&
2125             (zp->z_pflags & ZFS_AV_QUARANTINED))) {
2126                 return (EACCES);
2127         }
2128
2129         return (0);
2130 }
2131
2132 /*
2133  * The primary usage of this function is to loop through all of the
2134  * ACEs in the znode, determining what accesses of interest (AoI) to
2135  * the caller are allowed or denied.  The AoI are expressed as bits in
2136  * the working_mode parameter.  As each ACE is processed, bits covered
2137  * by that ACE are removed from the working_mode.  This removal
2138  * facilitates two things.  The first is that when the working mode is
2139  * empty (= 0), we know we've looked at all the AoI. The second is
2140  * that the ACE interpretation rules don't allow a later ACE to undo
2141  * something granted or denied by an earlier ACE.  Removing the
2142  * discovered access or denial enforces this rule.  At the end of
2143  * processing the ACEs, all AoI that were found to be denied are
2144  * placed into the working_mode, giving the caller a mask of denied
2145  * accesses.  Returns:
2146  *      0               if all AoI granted
2147  *      EACCESS         if the denied mask is non-zero
2148  *      other error     if abnormal failure (e.g., IO error)
2149  *
2150  * A secondary usage of the function is to determine if any of the
2151  * AoI are granted.  If an ACE grants any access in
2152  * the working_mode, we immediately short circuit out of the function.
2153  * This mode is chosen by setting anyaccess to B_TRUE.  The
2154  * working_mode is not a denied access mask upon exit if the function
2155  * is used in this manner.
2156  */
2157 static int
2158 zfs_zaccess_aces_check(znode_t *zp, uint32_t *working_mode,
2159     boolean_t anyaccess, cred_t *cr)
2160 {
2161         zfsvfs_t        *zfsvfs = zp->z_zfsvfs;
2162         zfs_acl_t       *aclp;
2163         int             error;
2164         uid_t           uid = crgetuid(cr);
2165         uint64_t        who;
2166         uint16_t        type, iflags;
2167         uint16_t        entry_type;
2168         uint32_t        access_mask;
2169         uint32_t        deny_mask = 0;
2170         zfs_ace_hdr_t   *acep = NULL;
2171         boolean_t       checkit;
2172         uid_t           gowner;
2173         uid_t           fowner;
2174
2175         zfs_fuid_map_ids(zp, cr, &fowner, &gowner);
2176
2177         mutex_enter(&zp->z_acl_lock);
2178
2179         error = zfs_acl_node_read(zp, B_FALSE, &aclp, B_FALSE);
2180         if (error != 0) {
2181                 mutex_exit(&zp->z_acl_lock);
2182                 return (error);
2183         }
2184
2185         ASSERT(zp->z_acl_cached);
2186
2187         while (acep = zfs_acl_next_ace(aclp, acep, &who, &access_mask,
2188             &iflags, &type)) {
2189                 uint32_t mask_matched;
2190
2191                 if (!zfs_acl_valid_ace_type(type, iflags))
2192                         continue;
2193
2194                 if (ZTOV(zp)->v_type == VDIR && (iflags & ACE_INHERIT_ONLY_ACE))
2195                         continue;
2196
2197                 /* Skip ACE if it does not affect any AoI */
2198                 mask_matched = (access_mask & *working_mode);
2199                 if (!mask_matched)
2200                         continue;
2201
2202                 entry_type = (iflags & ACE_TYPE_FLAGS);
2203
2204                 checkit = B_FALSE;
2205
2206                 switch (entry_type) {
2207                 case ACE_OWNER:
2208                         if (uid == fowner)
2209                                 checkit = B_TRUE;
2210                         break;
2211                 case OWNING_GROUP:
2212                         who = gowner;
2213                         /*FALLTHROUGH*/
2214                 case ACE_IDENTIFIER_GROUP:
2215                         checkit = zfs_groupmember(zfsvfs, who, cr);
2216                         break;
2217                 case ACE_EVERYONE:
2218                         checkit = B_TRUE;
2219                         break;
2220
2221                 /* USER Entry */
2222                 default:
2223                         if (entry_type == 0) {
2224                                 uid_t newid;
2225
2226                                 newid = zfs_fuid_map_id(zfsvfs, who, cr,
2227                                     ZFS_ACE_USER);
2228                                 if (newid != IDMAP_WK_CREATOR_OWNER_UID &&
2229                                     uid == newid)
2230                                         checkit = B_TRUE;
2231                                 break;
2232                         } else {
2233                                 mutex_exit(&zp->z_acl_lock);
2234                                 return (EIO);
2235                         }
2236                 }
2237
2238                 if (checkit) {
2239                         if (type == DENY) {
2240                                 DTRACE_PROBE3(zfs__ace__denies,
2241                                     znode_t *, zp,
2242                                     zfs_ace_hdr_t *, acep,
2243                                     uint32_t, mask_matched);
2244                                 deny_mask |= mask_matched;
2245                         } else {
2246                                 DTRACE_PROBE3(zfs__ace__allows,
2247                                     znode_t *, zp,
2248                                     zfs_ace_hdr_t *, acep,
2249                                     uint32_t, mask_matched);
2250                                 if (anyaccess) {
2251                                         mutex_exit(&zp->z_acl_lock);
2252                                         return (0);
2253                                 }
2254                         }
2255                         *working_mode &= ~mask_matched;
2256                 }
2257
2258                 /* Are we done? */
2259                 if (*working_mode == 0)
2260                         break;
2261         }
2262
2263         mutex_exit(&zp->z_acl_lock);
2264
2265         /* Put the found 'denies' back on the working mode */
2266         if (deny_mask) {
2267                 *working_mode |= deny_mask;
2268                 return (EACCES);
2269         } else if (*working_mode) {
2270                 return (-1);
2271         }
2272
2273         return (0);
2274 }
2275
2276 /*
2277  * Return true if any access whatsoever granted, we don't actually
2278  * care what access is granted.
2279  */
2280 boolean_t
2281 zfs_has_access(znode_t *zp, cred_t *cr)
2282 {
2283         uint32_t have = ACE_ALL_PERMS;
2284
2285         if (zfs_zaccess_aces_check(zp, &have, B_TRUE, cr) != 0) {
2286                 uid_t owner;
2287
2288                 owner = zfs_fuid_map_id(zp->z_zfsvfs, zp->z_uid, cr, ZFS_OWNER);
2289                 return (secpolicy_vnode_any_access(cr, ZTOV(zp), owner) == 0);
2290         }
2291         return (B_TRUE);
2292 }
2293
2294 static int
2295 zfs_zaccess_common(znode_t *zp, uint32_t v4_mode, uint32_t *working_mode,
2296     boolean_t *check_privs, boolean_t skipaclchk, cred_t *cr)
2297 {
2298         zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2299         int err;
2300
2301         *working_mode = v4_mode;
2302         *check_privs = B_TRUE;
2303
2304         /*
2305          * Short circuit empty requests
2306          */
2307         if (v4_mode == 0 || zfsvfs->z_replay) {
2308                 *working_mode = 0;
2309                 return (0);
2310         }
2311
2312         if ((err = zfs_zaccess_dataset_check(zp, v4_mode)) != 0) {
2313                 *check_privs = B_FALSE;
2314                 return (err);
2315         }
2316
2317         /*
2318          * The caller requested that the ACL check be skipped.  This
2319          * would only happen if the caller checked VOP_ACCESS() with a
2320          * 32 bit ACE mask and already had the appropriate permissions.
2321          */
2322         if (skipaclchk) {
2323                 *working_mode = 0;
2324                 return (0);
2325         }
2326
2327         return (zfs_zaccess_aces_check(zp, working_mode, B_FALSE, cr));
2328 }
2329
2330 static int
2331 zfs_zaccess_append(znode_t *zp, uint32_t *working_mode, boolean_t *check_privs,
2332     cred_t *cr)
2333 {
2334         if (*working_mode != ACE_WRITE_DATA)
2335                 return (EACCES);
2336
2337         return (zfs_zaccess_common(zp, ACE_APPEND_DATA, working_mode,
2338             check_privs, B_FALSE, cr));
2339 }
2340
2341 int
2342 zfs_fastaccesschk_execute(znode_t *zdp, cred_t *cr)
2343 {
2344         boolean_t owner = B_FALSE;
2345         boolean_t groupmbr = B_FALSE;
2346         boolean_t is_attr;
2347         uid_t uid = crgetuid(cr);
2348         int error;
2349
2350         if (zdp->z_pflags & ZFS_AV_QUARANTINED)
2351                 return (EACCES);
2352
2353         is_attr = ((zdp->z_pflags & ZFS_XATTR) &&
2354             (ZTOV(zdp)->v_type == VDIR));
2355         if (is_attr)
2356                 goto slow;
2357
2358
2359         mutex_enter(&zdp->z_acl_lock);
2360
2361         if (zdp->z_pflags & ZFS_NO_EXECS_DENIED) {
2362                 mutex_exit(&zdp->z_acl_lock);
2363                 return (0);
2364         }
2365
2366         if (FUID_INDEX(zdp->z_uid) != 0 || FUID_INDEX(zdp->z_gid) != 0) {
2367                 mutex_exit(&zdp->z_acl_lock);
2368                 goto slow;
2369         }
2370
2371         if (uid == zdp->z_uid) {
2372                 owner = B_TRUE;
2373                 if (zdp->z_mode & S_IXUSR) {
2374                         mutex_exit(&zdp->z_acl_lock);
2375                         return (0);
2376                 } else {
2377                         mutex_exit(&zdp->z_acl_lock);
2378                         goto slow;
2379                 }
2380         }
2381         if (groupmember(zdp->z_gid, cr)) {
2382                 groupmbr = B_TRUE;
2383                 if (zdp->z_mode & S_IXGRP) {
2384                         mutex_exit(&zdp->z_acl_lock);
2385                         return (0);
2386                 } else {
2387                         mutex_exit(&zdp->z_acl_lock);
2388                         goto slow;
2389                 }
2390         }
2391         if (!owner && !groupmbr) {
2392                 if (zdp->z_mode & S_IXOTH) {
2393                         mutex_exit(&zdp->z_acl_lock);
2394                         return (0);
2395                 }
2396         }
2397
2398         mutex_exit(&zdp->z_acl_lock);
2399
2400 slow:
2401         DTRACE_PROBE(zfs__fastpath__execute__access__miss);
2402         ZFS_ENTER(zdp->z_zfsvfs);
2403         error = zfs_zaccess(zdp, ACE_EXECUTE, 0, B_FALSE, cr);
2404         ZFS_EXIT(zdp->z_zfsvfs);
2405         return (error);
2406 }
2407
2408 /*
2409  * Determine whether Access should be granted/denied.
2410  * The least priv subsytem is always consulted as a basic privilege
2411  * can define any form of access.
2412  */
2413 int
2414 zfs_zaccess(znode_t *zp, int mode, int flags, boolean_t skipaclchk, cred_t *cr)
2415 {
2416         uint32_t        working_mode;
2417         int             error;
2418         int             is_attr;
2419         boolean_t       check_privs;
2420         znode_t         *xzp;
2421         znode_t         *check_zp = zp;
2422         mode_t          needed_bits;
2423         uid_t           owner;
2424
2425         is_attr = ((zp->z_pflags & ZFS_XATTR) && (ZTOV(zp)->v_type == VDIR));
2426
2427 #ifdef __FreeBSD__
2428         /*
2429          * In FreeBSD, we don't care about permissions of individual ADS.
2430          * Note that not checking them is not just an optimization - without
2431          * this shortcut, EA operations may bogusly fail with EACCES.
2432          */
2433         if (zp->z_pflags & ZFS_XATTR)
2434                 return (0);
2435 #else
2436         /*
2437          * If attribute then validate against base file
2438          */
2439         if (is_attr) {
2440                 uint64_t        parent;
2441
2442                 if ((error = sa_lookup(zp->z_sa_hdl,
2443                     SA_ZPL_PARENT(zp->z_zfsvfs), &parent,
2444                     sizeof (parent))) != 0)
2445                         return (error);
2446
2447                 if ((error = zfs_zget(zp->z_zfsvfs,
2448                     parent, &xzp)) != 0)        {
2449                         return (error);
2450                 }
2451
2452                 check_zp = xzp;
2453
2454                 /*
2455                  * fixup mode to map to xattr perms
2456                  */
2457
2458                 if (mode & (ACE_WRITE_DATA|ACE_APPEND_DATA)) {
2459                         mode &= ~(ACE_WRITE_DATA|ACE_APPEND_DATA);
2460                         mode |= ACE_WRITE_NAMED_ATTRS;
2461                 }
2462
2463                 if (mode & (ACE_READ_DATA|ACE_EXECUTE)) {
2464                         mode &= ~(ACE_READ_DATA|ACE_EXECUTE);
2465                         mode |= ACE_READ_NAMED_ATTRS;
2466                 }
2467         }
2468 #endif
2469
2470         owner = zfs_fuid_map_id(zp->z_zfsvfs, zp->z_uid, cr, ZFS_OWNER);
2471         /*
2472          * Map the bits required to the standard vnode flags VREAD|VWRITE|VEXEC
2473          * in needed_bits.  Map the bits mapped by working_mode (currently
2474          * missing) in missing_bits.
2475          * Call secpolicy_vnode_access2() with (needed_bits & ~checkmode),
2476          * needed_bits.
2477          */
2478         needed_bits = 0;
2479
2480         working_mode = mode;
2481         if ((working_mode & (ACE_READ_ACL|ACE_READ_ATTRIBUTES)) &&
2482             owner == crgetuid(cr))
2483                 working_mode &= ~(ACE_READ_ACL|ACE_READ_ATTRIBUTES);
2484
2485         if (working_mode & (ACE_READ_DATA|ACE_READ_NAMED_ATTRS|
2486             ACE_READ_ACL|ACE_READ_ATTRIBUTES|ACE_SYNCHRONIZE))
2487                 needed_bits |= VREAD;
2488         if (working_mode & (ACE_WRITE_DATA|ACE_WRITE_NAMED_ATTRS|
2489             ACE_APPEND_DATA|ACE_WRITE_ATTRIBUTES|ACE_SYNCHRONIZE))
2490                 needed_bits |= VWRITE;
2491         if (working_mode & ACE_EXECUTE)
2492                 needed_bits |= VEXEC;
2493
2494         if ((error = zfs_zaccess_common(check_zp, mode, &working_mode,
2495             &check_privs, skipaclchk, cr)) == 0) {
2496                 if (is_attr)
2497                         VN_RELE(ZTOV(xzp));
2498                 return (secpolicy_vnode_access2(cr, ZTOV(zp), owner,
2499                     needed_bits, needed_bits));
2500         }
2501
2502         if (error && !check_privs) {
2503                 if (is_attr)
2504                         VN_RELE(ZTOV(xzp));
2505                 return (error);
2506         }
2507
2508         if (error && (flags & V_APPEND)) {
2509                 error = zfs_zaccess_append(zp, &working_mode, &check_privs, cr);
2510         }
2511
2512         if (error && check_privs) {
2513                 mode_t          checkmode = 0;
2514
2515                 /*
2516                  * First check for implicit owner permission on
2517                  * read_acl/read_attributes
2518                  */
2519
2520                 error = 0;
2521                 ASSERT(working_mode != 0);
2522
2523                 if ((working_mode & (ACE_READ_ACL|ACE_READ_ATTRIBUTES) &&
2524                     owner == crgetuid(cr)))
2525                         working_mode &= ~(ACE_READ_ACL|ACE_READ_ATTRIBUTES);
2526
2527                 if (working_mode & (ACE_READ_DATA|ACE_READ_NAMED_ATTRS|
2528                     ACE_READ_ACL|ACE_READ_ATTRIBUTES|ACE_SYNCHRONIZE))
2529                         checkmode |= VREAD;
2530                 if (working_mode & (ACE_WRITE_DATA|ACE_WRITE_NAMED_ATTRS|
2531                     ACE_APPEND_DATA|ACE_WRITE_ATTRIBUTES|ACE_SYNCHRONIZE))
2532                         checkmode |= VWRITE;
2533                 if (working_mode & ACE_EXECUTE)
2534                         checkmode |= VEXEC;
2535
2536                 error = secpolicy_vnode_access2(cr, ZTOV(check_zp), owner,
2537                     needed_bits & ~checkmode, needed_bits);
2538
2539                 if (error == 0 && (working_mode & ACE_WRITE_OWNER))
2540                         error = secpolicy_vnode_chown(ZTOV(check_zp), cr, owner);
2541                 if (error == 0 && (working_mode & ACE_WRITE_ACL))
2542                         error = secpolicy_vnode_setdac(ZTOV(check_zp), cr, owner);
2543
2544                 if (error == 0 && (working_mode &
2545                     (ACE_DELETE|ACE_DELETE_CHILD)))
2546                         error = secpolicy_vnode_remove(ZTOV(check_zp), cr);
2547
2548                 if (error == 0 && (working_mode & ACE_SYNCHRONIZE)) {
2549                         error = secpolicy_vnode_chown(ZTOV(check_zp), cr, owner);
2550                 }
2551                 if (error == 0) {
2552                         /*
2553                          * See if any bits other than those already checked
2554                          * for are still present.  If so then return EACCES
2555                          */
2556                         if (working_mode & ~(ZFS_CHECKED_MASKS)) {
2557                                 error = EACCES;
2558                         }
2559                 }
2560         } else if (error == 0) {
2561                 error = secpolicy_vnode_access2(cr, ZTOV(zp), owner,
2562                     needed_bits, needed_bits);
2563         }
2564
2565
2566         if (is_attr)
2567                 VN_RELE(ZTOV(xzp));
2568
2569         return (error);
2570 }
2571
2572 /*
2573  * Translate traditional unix VREAD/VWRITE/VEXEC mode into
2574  * native ACL format and call zfs_zaccess()
2575  */
2576 int
2577 zfs_zaccess_rwx(znode_t *zp, mode_t mode, int flags, cred_t *cr)
2578 {
2579         return (zfs_zaccess(zp, zfs_unix_to_v4(mode >> 6), flags, B_FALSE, cr));
2580 }
2581
2582 /*
2583  * Access function for secpolicy_vnode_setattr
2584  */
2585 int
2586 zfs_zaccess_unix(znode_t *zp, mode_t mode, cred_t *cr)
2587 {
2588         int v4_mode = zfs_unix_to_v4(mode >> 6);
2589
2590         return (zfs_zaccess(zp, v4_mode, 0, B_FALSE, cr));
2591 }
2592
2593 static int
2594 zfs_delete_final_check(znode_t *zp, znode_t *dzp,
2595     mode_t available_perms, cred_t *cr)
2596 {
2597         int error;
2598         uid_t downer;
2599
2600         downer = zfs_fuid_map_id(dzp->z_zfsvfs, dzp->z_uid, cr, ZFS_OWNER);
2601
2602         error = secpolicy_vnode_access2(cr, ZTOV(dzp),
2603             downer, available_perms, VWRITE|VEXEC);
2604
2605         if (error == 0)
2606                 error = zfs_sticky_remove_access(dzp, zp, cr);
2607
2608         return (error);
2609 }
2610
2611 /*
2612  * Determine whether Access should be granted/deny, without
2613  * consulting least priv subsystem.
2614  *
2615  *
2616  * The following chart is the recommended NFSv4 enforcement for
2617  * ability to delete an object.
2618  *
2619  *      -------------------------------------------------------
2620  *      |   Parent Dir  |           Target Object Permissions |
2621  *      |  permissions  |                                     |
2622  *      -------------------------------------------------------
2623  *      |               | ACL Allows | ACL Denies| Delete     |
2624  *      |               |  Delete    |  Delete   | unspecified|
2625  *      -------------------------------------------------------
2626  *      |  ACL Allows   | Permit     | Permit    | Permit     |
2627  *      |  DELETE_CHILD |                                     |
2628  *      -------------------------------------------------------
2629  *      |  ACL Denies   | Permit     | Deny      | Deny       |
2630  *      |  DELETE_CHILD |            |           |            |
2631  *      -------------------------------------------------------
2632  *      | ACL specifies |            |           |            |
2633  *      | only allow    | Permit     | Permit    | Permit     |
2634  *      | write and     |            |           |            |
2635  *      | execute       |            |           |            |
2636  *      -------------------------------------------------------
2637  *      | ACL denies    |            |           |            |
2638  *      | write and     | Permit     | Deny      | Deny       |
2639  *      | execute       |            |           |            |
2640  *      -------------------------------------------------------
2641  *         ^
2642  *         |
2643  *         No search privilege, can't even look up file?
2644  *
2645  */
2646 int
2647 zfs_zaccess_delete(znode_t *dzp, znode_t *zp, cred_t *cr)
2648 {
2649         uint32_t dzp_working_mode = 0;
2650         uint32_t zp_working_mode = 0;
2651         int dzp_error, zp_error;
2652         mode_t available_perms;
2653         boolean_t dzpcheck_privs = B_TRUE;
2654         boolean_t zpcheck_privs = B_TRUE;
2655
2656         /*
2657          * We want specific DELETE permissions to
2658          * take precedence over WRITE/EXECUTE.  We don't
2659          * want an ACL such as this to mess us up.
2660          * user:joe:write_data:deny,user:joe:delete:allow
2661          *
2662          * However, deny permissions may ultimately be overridden
2663          * by secpolicy_vnode_access().
2664          *
2665          * We will ask for all of the necessary permissions and then
2666          * look at the working modes from the directory and target object
2667          * to determine what was found.
2668          */
2669
2670         if (zp->z_pflags & (ZFS_IMMUTABLE | ZFS_NOUNLINK))
2671                 return (EPERM);
2672
2673         /*
2674          * First row
2675          * If the directory permissions allow the delete, we are done.
2676          */
2677         if ((dzp_error = zfs_zaccess_common(dzp, ACE_DELETE_CHILD,
2678             &dzp_working_mode, &dzpcheck_privs, B_FALSE, cr)) == 0)
2679                 return (0);
2680
2681         /*
2682          * If target object has delete permission then we are done
2683          */
2684         if ((zp_error = zfs_zaccess_common(zp, ACE_DELETE, &zp_working_mode,
2685             &zpcheck_privs, B_FALSE, cr)) == 0)
2686                 return (0);
2687
2688         ASSERT(dzp_error && zp_error);
2689
2690         if (!dzpcheck_privs)
2691                 return (dzp_error);
2692         if (!zpcheck_privs)
2693                 return (zp_error);
2694
2695         /*
2696          * Second row
2697          *
2698          * If directory returns EACCES then delete_child was denied
2699          * due to deny delete_child.  In this case send the request through
2700          * secpolicy_vnode_remove().  We don't use zfs_delete_final_check()
2701          * since that *could* allow the delete based on write/execute permission
2702          * and we want delete permissions to override write/execute.
2703          */
2704
2705         if (dzp_error == EACCES)
2706                 return (secpolicy_vnode_remove(ZTOV(dzp), cr)); /* XXXPJD: s/dzp/zp/ ? */
2707
2708         /*
2709          * Third Row
2710          * only need to see if we have write/execute on directory.
2711          */
2712
2713         dzp_error = zfs_zaccess_common(dzp, ACE_EXECUTE|ACE_WRITE_DATA,
2714             &dzp_working_mode, &dzpcheck_privs, B_FALSE, cr);
2715
2716         if (dzp_error != 0 && !dzpcheck_privs)
2717                 return (dzp_error);
2718
2719         /*
2720          * Fourth row
2721          */
2722
2723         available_perms = (dzp_working_mode & ACE_WRITE_DATA) ? 0 : VWRITE;
2724         available_perms |= (dzp_working_mode & ACE_EXECUTE) ? 0 : VEXEC;
2725
2726         return (zfs_delete_final_check(zp, dzp, available_perms, cr));
2727
2728 }
2729
2730 int
2731 zfs_zaccess_rename(znode_t *sdzp, znode_t *szp, znode_t *tdzp,
2732     znode_t *tzp, cred_t *cr)
2733 {
2734         int add_perm;
2735         int error;
2736
2737         if (szp->z_pflags & ZFS_AV_QUARANTINED)
2738                 return (EACCES);
2739
2740         add_perm = (ZTOV(szp)->v_type == VDIR) ?
2741             ACE_ADD_SUBDIRECTORY : ACE_ADD_FILE;
2742
2743         /*
2744          * Rename permissions are combination of delete permission +
2745          * add file/subdir permission.
2746          *
2747          * BSD operating systems also require write permission
2748          * on the directory being moved from one parent directory
2749          * to another.
2750          */
2751         if (ZTOV(szp)->v_type == VDIR && ZTOV(sdzp) != ZTOV(tdzp)) {
2752                 if (error = zfs_zaccess(szp, ACE_WRITE_DATA, 0, B_FALSE, cr))
2753                         return (error);
2754         }
2755
2756         /*
2757          * first make sure we do the delete portion.
2758          *
2759          * If that succeeds then check for add_file/add_subdir permissions
2760          */
2761
2762         if (error = zfs_zaccess_delete(sdzp, szp, cr))
2763                 return (error);
2764
2765         /*
2766          * If we have a tzp, see if we can delete it?
2767          */
2768         if (tzp) {
2769                 if (error = zfs_zaccess_delete(tdzp, tzp, cr))
2770                         return (error);
2771         }
2772
2773         /*
2774          * Now check for add permissions
2775          */
2776         error = zfs_zaccess(tdzp, add_perm, 0, B_FALSE, cr);
2777
2778         return (error);
2779 }