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