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