blob: 034c5ea80dd14f694c4f65ed9034a9adc93396e0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2001-2002,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
20#include "xfs_types.h"
21#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "xfs_inum.h"
Nathan Scotta844f452005-11-02 14:38:42 +110023#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_dir2.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110026#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_ialloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_dir2_sf.h"
Nathan Scotta844f452005-11-02 14:38:42 +110029#include "xfs_attr_sf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "xfs_dinode.h"
31#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110032#include "xfs_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "xfs_acl.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_attr.h"
Christoph Hellwig739bfb22007-08-29 10:58:01 +100035#include "xfs_vnodeops.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080037#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/posix_acl_xattr.h>
39
Nathan Scott67fcaa72006-06-09 17:00:52 +100040STATIC int xfs_acl_setmode(bhv_vnode_t *, xfs_acl_t *, int *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041STATIC void xfs_acl_filter_mode(mode_t, xfs_acl_t *);
42STATIC void xfs_acl_get_endian(xfs_acl_t *);
43STATIC int xfs_acl_access(uid_t, gid_t, xfs_acl_t *, mode_t, cred_t *);
44STATIC int xfs_acl_invalid(xfs_acl_t *);
45STATIC void xfs_acl_sync_mode(mode_t, xfs_acl_t *);
Nathan Scott67fcaa72006-06-09 17:00:52 +100046STATIC void xfs_acl_get_attr(bhv_vnode_t *, xfs_acl_t *, int, int, int *);
47STATIC void xfs_acl_set_attr(bhv_vnode_t *, xfs_acl_t *, int, int *);
48STATIC int xfs_acl_allow_set(bhv_vnode_t *, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50kmem_zone_t *xfs_acl_zone;
51
52
53/*
54 * Test for existence of access ACL attribute as efficiently as possible.
55 */
56int
57xfs_acl_vhasacl_access(
Nathan Scott67fcaa72006-06-09 17:00:52 +100058 bhv_vnode_t *vp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059{
60 int error;
61
62 xfs_acl_get_attr(vp, NULL, _ACL_TYPE_ACCESS, ATTR_KERNOVAL, &error);
63 return (error == 0);
64}
65
66/*
67 * Test for existence of default ACL attribute as efficiently as possible.
68 */
69int
70xfs_acl_vhasacl_default(
Nathan Scott67fcaa72006-06-09 17:00:52 +100071 bhv_vnode_t *vp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
73 int error;
74
Christoph Hellwig0432dab2005-09-02 16:46:51 +100075 if (!VN_ISDIR(vp))
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 return 0;
77 xfs_acl_get_attr(vp, NULL, _ACL_TYPE_DEFAULT, ATTR_KERNOVAL, &error);
78 return (error == 0);
79}
80
81/*
82 * Convert from extended attribute representation to in-memory for XFS.
83 */
84STATIC int
85posix_acl_xattr_to_xfs(
86 posix_acl_xattr_header *src,
87 size_t size,
88 xfs_acl_t *dest)
89{
90 posix_acl_xattr_entry *src_entry;
91 xfs_acl_entry_t *dest_entry;
92 int n;
93
94 if (!src || !dest)
95 return EINVAL;
96
97 if (size < sizeof(posix_acl_xattr_header))
98 return EINVAL;
99
100 if (src->a_version != cpu_to_le32(POSIX_ACL_XATTR_VERSION))
101 return EOPNOTSUPP;
102
103 memset(dest, 0, sizeof(xfs_acl_t));
104 dest->acl_cnt = posix_acl_xattr_count(size);
105 if (dest->acl_cnt < 0 || dest->acl_cnt > XFS_ACL_MAX_ENTRIES)
106 return EINVAL;
107
108 /*
109 * acl_set_file(3) may request that we set default ACLs with
110 * zero length -- defend (gracefully) against that here.
111 */
112 if (!dest->acl_cnt)
113 return 0;
114
115 src_entry = (posix_acl_xattr_entry *)((char *)src + sizeof(*src));
116 dest_entry = &dest->acl_entry[0];
117
118 for (n = 0; n < dest->acl_cnt; n++, src_entry++, dest_entry++) {
119 dest_entry->ae_perm = le16_to_cpu(src_entry->e_perm);
120 if (_ACL_PERM_INVALID(dest_entry->ae_perm))
121 return EINVAL;
122 dest_entry->ae_tag = le16_to_cpu(src_entry->e_tag);
123 switch(dest_entry->ae_tag) {
124 case ACL_USER:
125 case ACL_GROUP:
126 dest_entry->ae_id = le32_to_cpu(src_entry->e_id);
127 break;
128 case ACL_USER_OBJ:
129 case ACL_GROUP_OBJ:
130 case ACL_MASK:
131 case ACL_OTHER:
132 dest_entry->ae_id = ACL_UNDEFINED_ID;
133 break;
134 default:
135 return EINVAL;
136 }
137 }
138 if (xfs_acl_invalid(dest))
139 return EINVAL;
140
141 return 0;
142}
143
144/*
Nathan Scott380b5dc2005-11-02 11:43:18 +1100145 * Comparison function called from xfs_sort().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 * Primary key is ae_tag, secondary key is ae_id.
147 */
148STATIC int
149xfs_acl_entry_compare(
150 const void *va,
151 const void *vb)
152{
153 xfs_acl_entry_t *a = (xfs_acl_entry_t *)va,
154 *b = (xfs_acl_entry_t *)vb;
155
156 if (a->ae_tag == b->ae_tag)
157 return (a->ae_id - b->ae_id);
158 return (a->ae_tag - b->ae_tag);
159}
160
161/*
162 * Convert from in-memory XFS to extended attribute representation.
163 */
164STATIC int
165posix_acl_xfs_to_xattr(
166 xfs_acl_t *src,
167 posix_acl_xattr_header *dest,
168 size_t size)
169{
170 int n;
171 size_t new_size = posix_acl_xattr_size(src->acl_cnt);
172 posix_acl_xattr_entry *dest_entry;
173 xfs_acl_entry_t *src_entry;
174
175 if (size < new_size)
176 return -ERANGE;
177
178 /* Need to sort src XFS ACL by <ae_tag,ae_id> */
Nathan Scott380b5dc2005-11-02 11:43:18 +1100179 xfs_sort(src->acl_entry, src->acl_cnt, sizeof(src->acl_entry[0]),
180 xfs_acl_entry_compare);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182 dest->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION);
183 dest_entry = &dest->a_entries[0];
184 src_entry = &src->acl_entry[0];
185 for (n = 0; n < src->acl_cnt; n++, dest_entry++, src_entry++) {
186 dest_entry->e_perm = cpu_to_le16(src_entry->ae_perm);
187 if (_ACL_PERM_INVALID(src_entry->ae_perm))
188 return -EINVAL;
189 dest_entry->e_tag = cpu_to_le16(src_entry->ae_tag);
190 switch (src_entry->ae_tag) {
191 case ACL_USER:
192 case ACL_GROUP:
193 dest_entry->e_id = cpu_to_le32(src_entry->ae_id);
194 break;
195 case ACL_USER_OBJ:
196 case ACL_GROUP_OBJ:
197 case ACL_MASK:
198 case ACL_OTHER:
199 dest_entry->e_id = cpu_to_le32(ACL_UNDEFINED_ID);
200 break;
201 default:
202 return -EINVAL;
203 }
204 }
205 return new_size;
206}
207
208int
209xfs_acl_vget(
Nathan Scott67fcaa72006-06-09 17:00:52 +1000210 bhv_vnode_t *vp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 void *acl,
212 size_t size,
213 int kind)
214{
215 int error;
216 xfs_acl_t *xfs_acl = NULL;
217 posix_acl_xattr_header *ext_acl = acl;
218 int flags = 0;
219
220 VN_HOLD(vp);
221 if(size) {
222 if (!(_ACL_ALLOC(xfs_acl))) {
223 error = ENOMEM;
224 goto out;
225 }
226 memset(xfs_acl, 0, sizeof(xfs_acl_t));
227 } else
228 flags = ATTR_KERNOVAL;
229
230 xfs_acl_get_attr(vp, xfs_acl, kind, flags, &error);
231 if (error)
232 goto out;
233
234 if (!size) {
235 error = -posix_acl_xattr_size(XFS_ACL_MAX_ENTRIES);
236 } else {
237 if (xfs_acl_invalid(xfs_acl)) {
238 error = EINVAL;
239 goto out;
240 }
241 if (kind == _ACL_TYPE_ACCESS) {
Nathan Scott8285fb52006-06-09 17:07:12 +1000242 bhv_vattr_t va;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244 va.va_mask = XFS_AT_MODE;
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000245 error = xfs_getattr(xfs_vtoi(vp), &va, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 if (error)
247 goto out;
248 xfs_acl_sync_mode(va.va_mode, xfs_acl);
249 }
250 error = -posix_acl_xfs_to_xattr(xfs_acl, ext_acl, size);
251 }
252out:
253 VN_RELE(vp);
254 if(xfs_acl)
255 _ACL_FREE(xfs_acl);
256 return -error;
257}
258
259int
260xfs_acl_vremove(
Nathan Scott67fcaa72006-06-09 17:00:52 +1000261 bhv_vnode_t *vp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 int kind)
263{
264 int error;
265
266 VN_HOLD(vp);
267 error = xfs_acl_allow_set(vp, kind);
268 if (!error) {
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000269 error = xfs_attr_remove(xfs_vtoi(vp),
270 kind == _ACL_TYPE_DEFAULT?
Nathan Scott67fcaa72006-06-09 17:00:52 +1000271 SGI_ACL_DEFAULT: SGI_ACL_FILE,
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000272 ATTR_ROOT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 if (error == ENOATTR)
274 error = 0; /* 'scool */
275 }
276 VN_RELE(vp);
277 return -error;
278}
279
280int
281xfs_acl_vset(
Nathan Scott67fcaa72006-06-09 17:00:52 +1000282 bhv_vnode_t *vp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 void *acl,
284 size_t size,
285 int kind)
286{
287 posix_acl_xattr_header *ext_acl = acl;
288 xfs_acl_t *xfs_acl;
289 int error;
290 int basicperms = 0; /* more than std unix perms? */
291
292 if (!acl)
293 return -EINVAL;
294
295 if (!(_ACL_ALLOC(xfs_acl)))
296 return -ENOMEM;
297
298 error = posix_acl_xattr_to_xfs(ext_acl, size, xfs_acl);
299 if (error) {
300 _ACL_FREE(xfs_acl);
301 return -error;
302 }
303 if (!xfs_acl->acl_cnt) {
304 _ACL_FREE(xfs_acl);
305 return 0;
306 }
307
308 VN_HOLD(vp);
309 error = xfs_acl_allow_set(vp, kind);
310 if (error)
311 goto out;
312
313 /* Incoming ACL exists, set file mode based on its value */
314 if (kind == _ACL_TYPE_ACCESS)
315 xfs_acl_setmode(vp, xfs_acl, &basicperms);
316
317 /*
318 * If we have more than std unix permissions, set up the actual attr.
319 * Otherwise, delete any existing attr. This prevents us from
320 * having actual attrs for permissions that can be stored in the
321 * standard permission bits.
322 */
323 if (!basicperms) {
324 xfs_acl_set_attr(vp, xfs_acl, kind, &error);
325 } else {
326 xfs_acl_vremove(vp, _ACL_TYPE_ACCESS);
327 }
328
329out:
330 VN_RELE(vp);
331 _ACL_FREE(xfs_acl);
332 return -error;
333}
334
335int
336xfs_acl_iaccess(
337 xfs_inode_t *ip,
338 mode_t mode,
339 cred_t *cr)
340{
341 xfs_acl_t *acl;
342 int rval;
343
344 if (!(_ACL_ALLOC(acl)))
345 return -1;
346
347 /* If the file has no ACL return -1. */
348 rval = sizeof(xfs_acl_t);
349 if (xfs_attr_fetch(ip, SGI_ACL_FILE, SGI_ACL_FILE_SIZE,
350 (char *)acl, &rval, ATTR_ROOT | ATTR_KERNACCESS, cr)) {
351 _ACL_FREE(acl);
352 return -1;
353 }
354 xfs_acl_get_endian(acl);
355
356 /* If the file has an empty ACL return -1. */
357 if (acl->acl_cnt == XFS_ACL_NOT_PRESENT) {
358 _ACL_FREE(acl);
359 return -1;
360 }
361
362 /* Synchronize ACL with mode bits */
363 xfs_acl_sync_mode(ip->i_d.di_mode, acl);
364
365 rval = xfs_acl_access(ip->i_d.di_uid, ip->i_d.di_gid, acl, mode, cr);
366 _ACL_FREE(acl);
367 return rval;
368}
369
370STATIC int
371xfs_acl_allow_set(
Nathan Scott67fcaa72006-06-09 17:00:52 +1000372 bhv_vnode_t *vp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 int kind)
374{
Nathan Scott8285fb52006-06-09 17:07:12 +1000375 bhv_vattr_t va;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 int error;
377
378 if (vp->v_inode.i_flags & (S_IMMUTABLE|S_APPEND))
379 return EPERM;
Christoph Hellwig0432dab2005-09-02 16:46:51 +1000380 if (kind == _ACL_TYPE_DEFAULT && !VN_ISDIR(vp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 return ENOTDIR;
382 if (vp->v_vfsp->vfs_flag & VFS_RDONLY)
383 return EROFS;
384 va.va_mask = XFS_AT_UID;
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000385 error = xfs_getattr(xfs_vtoi(vp), &va, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 if (error)
387 return error;
388 if (va.va_uid != current->fsuid && !capable(CAP_FOWNER))
389 return EPERM;
390 return error;
391}
392
393/*
394 * The access control process to determine the access permission:
395 * if uid == file owner id, use the file owner bits.
396 * if gid == file owner group id, use the file group bits.
Nathan Scottc41564b2006-03-29 08:55:14 +1000397 * scan ACL for a matching user or group, and use matched entry
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 * permission. Use total permissions of all matching group entries,
399 * until all acl entries are exhausted. The final permission produced
400 * by matching acl entry or entries needs to be & with group permission.
401 * if not owner, owning group, or matching entry in ACL, use file
402 * other bits.
403 */
404STATIC int
405xfs_acl_capability_check(
406 mode_t mode,
407 cred_t *cr)
408{
409 if ((mode & ACL_READ) && !capable_cred(cr, CAP_DAC_READ_SEARCH))
410 return EACCES;
411 if ((mode & ACL_WRITE) && !capable_cred(cr, CAP_DAC_OVERRIDE))
412 return EACCES;
413 if ((mode & ACL_EXECUTE) && !capable_cred(cr, CAP_DAC_OVERRIDE))
414 return EACCES;
415
416 return 0;
417}
418
419/*
420 * Note: cr is only used here for the capability check if the ACL test fails.
421 * It is not used to find out the credentials uid or groups etc, as was
422 * done in IRIX. It is assumed that the uid and groups for the current
423 * thread are taken from "current" instead of the cr parameter.
424 */
425STATIC int
426xfs_acl_access(
427 uid_t fuid,
428 gid_t fgid,
429 xfs_acl_t *fap,
430 mode_t md,
431 cred_t *cr)
432{
433 xfs_acl_entry_t matched;
434 int i, allows;
435 int maskallows = -1; /* true, but not 1, either */
436 int seen_userobj = 0;
437
438 matched.ae_tag = 0; /* Invalid type */
Christoph Hellwig5bde1ba92005-11-02 15:06:18 +1100439 matched.ae_perm = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 md >>= 6; /* Normalize the bits for comparison */
441
442 for (i = 0; i < fap->acl_cnt; i++) {
443 /*
444 * Break out if we've got a user_obj entry or
445 * a user entry and the mask (and have processed USER_OBJ)
446 */
447 if (matched.ae_tag == ACL_USER_OBJ)
448 break;
449 if (matched.ae_tag == ACL_USER) {
450 if (maskallows != -1 && seen_userobj)
451 break;
452 if (fap->acl_entry[i].ae_tag != ACL_MASK &&
453 fap->acl_entry[i].ae_tag != ACL_USER_OBJ)
454 continue;
455 }
456 /* True if this entry allows the requested access */
457 allows = ((fap->acl_entry[i].ae_perm & md) == md);
458
459 switch (fap->acl_entry[i].ae_tag) {
460 case ACL_USER_OBJ:
461 seen_userobj = 1;
462 if (fuid != current->fsuid)
463 continue;
464 matched.ae_tag = ACL_USER_OBJ;
465 matched.ae_perm = allows;
466 break;
467 case ACL_USER:
468 if (fap->acl_entry[i].ae_id != current->fsuid)
469 continue;
470 matched.ae_tag = ACL_USER;
471 matched.ae_perm = allows;
472 break;
473 case ACL_GROUP_OBJ:
474 if ((matched.ae_tag == ACL_GROUP_OBJ ||
475 matched.ae_tag == ACL_GROUP) && !allows)
476 continue;
477 if (!in_group_p(fgid))
478 continue;
479 matched.ae_tag = ACL_GROUP_OBJ;
480 matched.ae_perm = allows;
481 break;
482 case ACL_GROUP:
483 if ((matched.ae_tag == ACL_GROUP_OBJ ||
484 matched.ae_tag == ACL_GROUP) && !allows)
485 continue;
486 if (!in_group_p(fap->acl_entry[i].ae_id))
487 continue;
488 matched.ae_tag = ACL_GROUP;
489 matched.ae_perm = allows;
490 break;
491 case ACL_MASK:
492 maskallows = allows;
493 break;
494 case ACL_OTHER:
495 if (matched.ae_tag != 0)
496 continue;
497 matched.ae_tag = ACL_OTHER;
498 matched.ae_perm = allows;
499 break;
500 }
501 }
502 /*
503 * First possibility is that no matched entry allows access.
504 * The capability to override DAC may exist, so check for it.
505 */
506 switch (matched.ae_tag) {
507 case ACL_OTHER:
508 case ACL_USER_OBJ:
509 if (matched.ae_perm)
510 return 0;
511 break;
512 case ACL_USER:
513 case ACL_GROUP_OBJ:
514 case ACL_GROUP:
515 if (maskallows && matched.ae_perm)
516 return 0;
517 break;
518 case 0:
519 break;
520 }
521
522 return xfs_acl_capability_check(md, cr);
523}
524
525/*
526 * ACL validity checker.
527 * This acl validation routine checks each ACL entry read in makes sense.
528 */
529STATIC int
530xfs_acl_invalid(
531 xfs_acl_t *aclp)
532{
533 xfs_acl_entry_t *entry, *e;
534 int user = 0, group = 0, other = 0, mask = 0;
535 int mask_required = 0;
536 int i, j;
537
538 if (!aclp)
539 goto acl_invalid;
540
541 if (aclp->acl_cnt > XFS_ACL_MAX_ENTRIES)
542 goto acl_invalid;
543
544 for (i = 0; i < aclp->acl_cnt; i++) {
545 entry = &aclp->acl_entry[i];
546 switch (entry->ae_tag) {
547 case ACL_USER_OBJ:
548 if (user++)
549 goto acl_invalid;
550 break;
551 case ACL_GROUP_OBJ:
552 if (group++)
553 goto acl_invalid;
554 break;
555 case ACL_OTHER:
556 if (other++)
557 goto acl_invalid;
558 break;
559 case ACL_USER:
560 case ACL_GROUP:
561 for (j = i + 1; j < aclp->acl_cnt; j++) {
562 e = &aclp->acl_entry[j];
563 if (e->ae_id == entry->ae_id &&
564 e->ae_tag == entry->ae_tag)
565 goto acl_invalid;
566 }
567 mask_required++;
568 break;
569 case ACL_MASK:
570 if (mask++)
571 goto acl_invalid;
572 break;
573 default:
574 goto acl_invalid;
575 }
576 }
577 if (!user || !group || !other || (mask_required && !mask))
578 goto acl_invalid;
579 else
580 return 0;
581acl_invalid:
582 return EINVAL;
583}
584
585/*
586 * Do ACL endian conversion.
587 */
588STATIC void
589xfs_acl_get_endian(
590 xfs_acl_t *aclp)
591{
592 xfs_acl_entry_t *ace, *end;
593
594 INT_SET(aclp->acl_cnt, ARCH_CONVERT, aclp->acl_cnt);
595 end = &aclp->acl_entry[0]+aclp->acl_cnt;
596 for (ace = &aclp->acl_entry[0]; ace < end; ace++) {
597 INT_SET(ace->ae_tag, ARCH_CONVERT, ace->ae_tag);
598 INT_SET(ace->ae_id, ARCH_CONVERT, ace->ae_id);
599 INT_SET(ace->ae_perm, ARCH_CONVERT, ace->ae_perm);
600 }
601}
602
603/*
604 * Get the ACL from the EA and do endian conversion.
605 */
606STATIC void
607xfs_acl_get_attr(
Nathan Scott67fcaa72006-06-09 17:00:52 +1000608 bhv_vnode_t *vp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 xfs_acl_t *aclp,
610 int kind,
611 int flags,
612 int *error)
613{
614 int len = sizeof(xfs_acl_t);
615
616 ASSERT((flags & ATTR_KERNOVAL) ? (aclp == NULL) : 1);
617 flags |= ATTR_ROOT;
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000618 *error = xfs_attr_get(xfs_vtoi(vp),
619 kind == _ACL_TYPE_ACCESS ?
Nathan Scott67fcaa72006-06-09 17:00:52 +1000620 SGI_ACL_FILE : SGI_ACL_DEFAULT,
621 (char *)aclp, &len, flags, sys_cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 if (*error || (flags & ATTR_KERNOVAL))
623 return;
624 xfs_acl_get_endian(aclp);
625}
626
627/*
628 * Set the EA with the ACL and do endian conversion.
629 */
630STATIC void
631xfs_acl_set_attr(
Nathan Scott67fcaa72006-06-09 17:00:52 +1000632 bhv_vnode_t *vp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 xfs_acl_t *aclp,
634 int kind,
635 int *error)
636{
637 xfs_acl_entry_t *ace, *newace, *end;
638 xfs_acl_t *newacl;
639 int len;
640
641 if (!(_ACL_ALLOC(newacl))) {
642 *error = ENOMEM;
643 return;
644 }
645
646 len = sizeof(xfs_acl_t) -
647 (sizeof(xfs_acl_entry_t) * (XFS_ACL_MAX_ENTRIES - aclp->acl_cnt));
648 end = &aclp->acl_entry[0]+aclp->acl_cnt;
649 for (ace = &aclp->acl_entry[0], newace = &newacl->acl_entry[0];
650 ace < end;
651 ace++, newace++) {
652 INT_SET(newace->ae_tag, ARCH_CONVERT, ace->ae_tag);
653 INT_SET(newace->ae_id, ARCH_CONVERT, ace->ae_id);
654 INT_SET(newace->ae_perm, ARCH_CONVERT, ace->ae_perm);
655 }
656 INT_SET(newacl->acl_cnt, ARCH_CONVERT, aclp->acl_cnt);
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000657 *error = xfs_attr_set(xfs_vtoi(vp),
658 kind == _ACL_TYPE_ACCESS ?
Nathan Scott67fcaa72006-06-09 17:00:52 +1000659 SGI_ACL_FILE: SGI_ACL_DEFAULT,
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000660 (char *)newacl, len, ATTR_ROOT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 _ACL_FREE(newacl);
662}
663
664int
665xfs_acl_vtoacl(
Nathan Scott67fcaa72006-06-09 17:00:52 +1000666 bhv_vnode_t *vp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 xfs_acl_t *access_acl,
668 xfs_acl_t *default_acl)
669{
Nathan Scott8285fb52006-06-09 17:07:12 +1000670 bhv_vattr_t va;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 int error = 0;
672
673 if (access_acl) {
674 /*
675 * Get the Access ACL and the mode. If either cannot
676 * be obtained for some reason, invalidate the access ACL.
677 */
678 xfs_acl_get_attr(vp, access_acl, _ACL_TYPE_ACCESS, 0, &error);
679 if (!error) {
680 /* Got the ACL, need the mode... */
681 va.va_mask = XFS_AT_MODE;
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000682 error = xfs_getattr(xfs_vtoi(vp), &va, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 }
684
685 if (error)
686 access_acl->acl_cnt = XFS_ACL_NOT_PRESENT;
687 else /* We have a good ACL and the file mode, synchronize. */
688 xfs_acl_sync_mode(va.va_mode, access_acl);
689 }
690
691 if (default_acl) {
692 xfs_acl_get_attr(vp, default_acl, _ACL_TYPE_DEFAULT, 0, &error);
693 if (error)
694 default_acl->acl_cnt = XFS_ACL_NOT_PRESENT;
695 }
696 return error;
697}
698
699/*
700 * This function retrieves the parent directory's acl, processes it
701 * and lets the child inherit the acl(s) that it should.
702 */
703int
704xfs_acl_inherit(
Nathan Scott67fcaa72006-06-09 17:00:52 +1000705 bhv_vnode_t *vp,
Nathan Scott8285fb52006-06-09 17:07:12 +1000706 bhv_vattr_t *vap,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 xfs_acl_t *pdaclp)
708{
709 xfs_acl_t *cacl;
710 int error = 0;
711 int basicperms = 0;
712
713 /*
714 * If the parent does not have a default ACL, or it's an
715 * invalid ACL, we're done.
716 */
717 if (!vp)
718 return 0;
719 if (!pdaclp || xfs_acl_invalid(pdaclp))
720 return 0;
721
722 /*
723 * Copy the default ACL of the containing directory to
724 * the access ACL of the new file and use the mode that
725 * was passed in to set up the correct initial values for
726 * the u::,g::[m::], and o:: entries. This is what makes
727 * umask() "work" with ACL's.
728 */
729
730 if (!(_ACL_ALLOC(cacl)))
731 return ENOMEM;
732
733 memcpy(cacl, pdaclp, sizeof(xfs_acl_t));
734 xfs_acl_filter_mode(vap->va_mode, cacl);
735 xfs_acl_setmode(vp, cacl, &basicperms);
736
737 /*
738 * Set the Default and Access ACL on the file. The mode is already
739 * set on the file, so we don't need to worry about that.
740 *
741 * If the new file is a directory, its default ACL is a copy of
742 * the containing directory's default ACL.
743 */
Christoph Hellwig0432dab2005-09-02 16:46:51 +1000744 if (VN_ISDIR(vp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 xfs_acl_set_attr(vp, pdaclp, _ACL_TYPE_DEFAULT, &error);
746 if (!error && !basicperms)
747 xfs_acl_set_attr(vp, cacl, _ACL_TYPE_ACCESS, &error);
748 _ACL_FREE(cacl);
749 return error;
750}
751
752/*
753 * Set up the correct mode on the file based on the supplied ACL. This
754 * makes sure that the mode on the file reflects the state of the
755 * u::,g::[m::], and o:: entries in the ACL. Since the mode is where
756 * the ACL is going to get the permissions for these entries, we must
757 * synchronize the mode whenever we set the ACL on a file.
758 */
759STATIC int
760xfs_acl_setmode(
Nathan Scott67fcaa72006-06-09 17:00:52 +1000761 bhv_vnode_t *vp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 xfs_acl_t *acl,
763 int *basicperms)
764{
Nathan Scott8285fb52006-06-09 17:07:12 +1000765 bhv_vattr_t va;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 xfs_acl_entry_t *ap;
767 xfs_acl_entry_t *gap = NULL;
768 int i, error, nomask = 1;
769
770 *basicperms = 1;
771
772 if (acl->acl_cnt == XFS_ACL_NOT_PRESENT)
773 return 0;
774
775 /*
776 * Copy the u::, g::, o::, and m:: bits from the ACL into the
777 * mode. The m:: bits take precedence over the g:: bits.
778 */
779 va.va_mask = XFS_AT_MODE;
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000780 error = xfs_getattr(xfs_vtoi(vp), &va, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 if (error)
782 return error;
783
784 va.va_mask = XFS_AT_MODE;
785 va.va_mode &= ~(S_IRWXU|S_IRWXG|S_IRWXO);
786 ap = acl->acl_entry;
787 for (i = 0; i < acl->acl_cnt; ++i) {
788 switch (ap->ae_tag) {
789 case ACL_USER_OBJ:
790 va.va_mode |= ap->ae_perm << 6;
791 break;
792 case ACL_GROUP_OBJ:
793 gap = ap;
794 break;
795 case ACL_MASK: /* more than just standard modes */
796 nomask = 0;
797 va.va_mode |= ap->ae_perm << 3;
798 *basicperms = 0;
799 break;
800 case ACL_OTHER:
801 va.va_mode |= ap->ae_perm;
802 break;
803 default: /* more than just standard modes */
804 *basicperms = 0;
805 break;
806 }
807 ap++;
808 }
809
810 /* Set the group bits from ACL_GROUP_OBJ if there's no ACL_MASK */
811 if (gap && nomask)
812 va.va_mode |= gap->ae_perm << 3;
813
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000814 return xfs_setattr(xfs_vtoi(vp), &va, 0, sys_cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815}
816
817/*
818 * The permissions for the special ACL entries (u::, g::[m::], o::) are
819 * actually stored in the file mode (if there is both a group and a mask,
820 * the group is stored in the ACL entry and the mask is stored on the file).
821 * This allows the mode to remain automatically in sync with the ACL without
822 * the need for a call-back to the ACL system at every point where the mode
823 * could change. This function takes the permissions from the specified mode
824 * and places it in the supplied ACL.
825 *
826 * This implementation draws its validity from the fact that, when the ACL
827 * was assigned, the mode was copied from the ACL.
828 * If the mode did not change, therefore, the mode remains exactly what was
829 * taken from the special ACL entries at assignment.
830 * If a subsequent chmod() was done, the POSIX spec says that the change in
831 * mode must cause an update to the ACL seen at user level and used for
832 * access checks. Before and after a mode change, therefore, the file mode
833 * most accurately reflects what the special ACL entries should permit/deny.
834 *
835 * CAVEAT: If someone sets the SGI_ACL_FILE attribute directly,
836 * the existing mode bits will override whatever is in the
837 * ACL. Similarly, if there is a pre-existing ACL that was
838 * never in sync with its mode (owing to a bug in 6.5 and
839 * before), it will now magically (or mystically) be
840 * synchronized. This could cause slight astonishment, but
841 * it is better than inconsistent permissions.
842 *
843 * The supplied ACL is a template that may contain any combination
844 * of special entries. These are treated as place holders when we fill
845 * out the ACL. This routine does not add or remove special entries, it
846 * simply unites each special entry with its associated set of permissions.
847 */
848STATIC void
849xfs_acl_sync_mode(
850 mode_t mode,
851 xfs_acl_t *acl)
852{
853 int i, nomask = 1;
854 xfs_acl_entry_t *ap;
855 xfs_acl_entry_t *gap = NULL;
856
857 /*
858 * Set ACL entries. POSIX1003.1eD16 requires that the MASK
859 * be set instead of the GROUP entry, if there is a MASK.
860 */
861 for (ap = acl->acl_entry, i = 0; i < acl->acl_cnt; ap++, i++) {
862 switch (ap->ae_tag) {
863 case ACL_USER_OBJ:
864 ap->ae_perm = (mode >> 6) & 0x7;
865 break;
866 case ACL_GROUP_OBJ:
867 gap = ap;
868 break;
869 case ACL_MASK:
870 nomask = 0;
871 ap->ae_perm = (mode >> 3) & 0x7;
872 break;
873 case ACL_OTHER:
874 ap->ae_perm = mode & 0x7;
875 break;
876 default:
877 break;
878 }
879 }
880 /* Set the ACL_GROUP_OBJ if there's no ACL_MASK */
881 if (gap && nomask)
882 gap->ae_perm = (mode >> 3) & 0x7;
883}
884
885/*
886 * When inheriting an Access ACL from a directory Default ACL,
887 * the ACL bits are set to the intersection of the ACL default
888 * permission bits and the file permission bits in mode. If there
889 * are no permission bits on the file then we must not give them
890 * the ACL. This is what what makes umask() work with ACLs.
891 */
892STATIC void
893xfs_acl_filter_mode(
894 mode_t mode,
895 xfs_acl_t *acl)
896{
897 int i, nomask = 1;
898 xfs_acl_entry_t *ap;
899 xfs_acl_entry_t *gap = NULL;
900
901 /*
902 * Set ACL entries. POSIX1003.1eD16 requires that the MASK
903 * be merged with GROUP entry, if there is a MASK.
904 */
905 for (ap = acl->acl_entry, i = 0; i < acl->acl_cnt; ap++, i++) {
906 switch (ap->ae_tag) {
907 case ACL_USER_OBJ:
908 ap->ae_perm &= (mode >> 6) & 0x7;
909 break;
910 case ACL_GROUP_OBJ:
911 gap = ap;
912 break;
913 case ACL_MASK:
914 nomask = 0;
915 ap->ae_perm &= (mode >> 3) & 0x7;
916 break;
917 case ACL_OTHER:
918 ap->ae_perm &= mode & 0x7;
919 break;
920 default:
921 break;
922 }
923 }
924 /* Set the ACL_GROUP_OBJ if there's no ACL_MASK */
925 if (gap && nomask)
926 gap->ae_perm &= (mode >> 3) & 0x7;
927}