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