4 * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
7 #include <linux/init.h>
8 #include <linux/sched.h>
9 #include <linux/slab.h>
11 #include <linux/namei.h>
12 #include <linux/vs_base.h>
18 * Convert from filesystem to in-memory representation.
20 static struct posix_acl *
21 ext2_acl_from_disk(const void *value, size_t size)
23 const char *end = (char *)value + size;
25 struct posix_acl *acl;
29 if (size < sizeof(ext2_acl_header))
30 return ERR_PTR(-EINVAL);
31 if (((ext2_acl_header *)value)->a_version !=
32 cpu_to_le32(EXT2_ACL_VERSION))
33 return ERR_PTR(-EINVAL);
34 value = (char *)value + sizeof(ext2_acl_header);
35 count = ext2_acl_count(size);
37 return ERR_PTR(-EINVAL);
40 acl = posix_acl_alloc(count, GFP_KERNEL);
42 return ERR_PTR(-ENOMEM);
43 for (n=0; n < count; n++) {
44 ext2_acl_entry *entry =
45 (ext2_acl_entry *)value;
46 if ((char *)value + sizeof(ext2_acl_entry_short) > end)
48 acl->a_entries[n].e_tag = le16_to_cpu(entry->e_tag);
49 acl->a_entries[n].e_perm = le16_to_cpu(entry->e_perm);
50 switch(acl->a_entries[n].e_tag) {
55 value = (char *)value +
56 sizeof(ext2_acl_entry_short);
57 acl->a_entries[n].e_id = ACL_UNDEFINED_ID;
62 value = (char *)value + sizeof(ext2_acl_entry);
63 if ((char *)value > end)
65 acl->a_entries[n].e_id =
66 le32_to_cpu(entry->e_id);
78 posix_acl_release(acl);
79 return ERR_PTR(-EINVAL);
83 * Convert from in-memory to filesystem representation.
86 ext2_acl_to_disk(const struct posix_acl *acl, size_t *size)
88 ext2_acl_header *ext_acl;
92 *size = ext2_acl_size(acl->a_count);
93 ext_acl = (ext2_acl_header *)kmalloc(sizeof(ext2_acl_header) +
94 acl->a_count * sizeof(ext2_acl_entry), GFP_KERNEL);
96 return ERR_PTR(-ENOMEM);
97 ext_acl->a_version = cpu_to_le32(EXT2_ACL_VERSION);
98 e = (char *)ext_acl + sizeof(ext2_acl_header);
99 for (n=0; n < acl->a_count; n++) {
100 ext2_acl_entry *entry = (ext2_acl_entry *)e;
101 entry->e_tag = cpu_to_le16(acl->a_entries[n].e_tag);
102 entry->e_perm = cpu_to_le16(acl->a_entries[n].e_perm);
103 switch(acl->a_entries[n].e_tag) {
107 cpu_to_le32(acl->a_entries[n].e_id);
108 e += sizeof(ext2_acl_entry);
115 e += sizeof(ext2_acl_entry_short);
122 return (char *)ext_acl;
126 return ERR_PTR(-EINVAL);
129 static inline struct posix_acl *
130 ext2_iget_acl(struct inode *inode, struct posix_acl **i_acl)
132 struct posix_acl *acl = EXT2_ACL_NOT_CACHED;
134 spin_lock(&inode->i_lock);
135 if (*i_acl != EXT2_ACL_NOT_CACHED)
136 acl = posix_acl_dup(*i_acl);
137 spin_unlock(&inode->i_lock);
143 ext2_iset_acl(struct inode *inode, struct posix_acl **i_acl,
144 struct posix_acl *acl)
146 spin_lock(&inode->i_lock);
147 if (*i_acl != EXT2_ACL_NOT_CACHED)
148 posix_acl_release(*i_acl);
149 *i_acl = posix_acl_dup(acl);
150 spin_unlock(&inode->i_lock);
154 * inode->i_sem: don't care
156 static struct posix_acl *
157 ext2_get_acl(struct inode *inode, int type)
159 struct ext2_inode_info *ei = EXT2_I(inode);
162 struct posix_acl *acl;
165 if (!test_opt(inode->i_sb, POSIX_ACL))
169 case ACL_TYPE_ACCESS:
170 acl = ext2_iget_acl(inode, &ei->i_acl);
171 if (acl != EXT2_ACL_NOT_CACHED)
173 name_index = EXT2_XATTR_INDEX_POSIX_ACL_ACCESS;
176 case ACL_TYPE_DEFAULT:
177 acl = ext2_iget_acl(inode, &ei->i_default_acl);
178 if (acl != EXT2_ACL_NOT_CACHED)
180 name_index = EXT2_XATTR_INDEX_POSIX_ACL_DEFAULT;
184 return ERR_PTR(-EINVAL);
186 retval = ext2_xattr_get(inode, name_index, "", NULL, 0);
188 value = kmalloc(retval, GFP_KERNEL);
190 return ERR_PTR(-ENOMEM);
191 retval = ext2_xattr_get(inode, name_index, "", value, retval);
194 acl = ext2_acl_from_disk(value, retval);
195 else if (retval == -ENODATA || retval == -ENOSYS)
198 acl = ERR_PTR(retval);
204 case ACL_TYPE_ACCESS:
205 ext2_iset_acl(inode, &ei->i_acl, acl);
208 case ACL_TYPE_DEFAULT:
209 ext2_iset_acl(inode, &ei->i_default_acl, acl);
220 ext2_set_acl(struct inode *inode, int type, struct posix_acl *acl)
222 struct ext2_inode_info *ei = EXT2_I(inode);
228 if (S_ISLNK(inode->i_mode))
230 if (!test_opt(inode->i_sb, POSIX_ACL))
234 case ACL_TYPE_ACCESS:
235 name_index = EXT2_XATTR_INDEX_POSIX_ACL_ACCESS;
237 mode_t mode = inode->i_mode;
238 error = posix_acl_equiv_mode(acl, &mode);
242 inode->i_mode = mode;
243 mark_inode_dirty(inode);
250 case ACL_TYPE_DEFAULT:
251 name_index = EXT2_XATTR_INDEX_POSIX_ACL_DEFAULT;
252 if (!S_ISDIR(inode->i_mode))
253 return acl ? -EACCES : 0;
260 if (acl->a_count > EXT2_ACL_MAX_ENTRIES)
262 value = ext2_acl_to_disk(acl, &size);
264 return (int)PTR_ERR(value);
267 error = ext2_xattr_set(inode, name_index, "", value, size, 0);
273 case ACL_TYPE_ACCESS:
274 ext2_iset_acl(inode, &ei->i_acl, acl);
277 case ACL_TYPE_DEFAULT:
278 ext2_iset_acl(inode, &ei->i_default_acl, acl);
286 * Inode operation permission().
288 * inode->i_sem: don't care
291 ext2_permission(struct inode *inode, int mask, struct nameidata *nd)
293 int mode = inode->i_mode;
295 /* Prevent vservers from escaping chroot() barriers */
296 if (IS_BARRIER(inode) && !vx_check(0, VX_ADMIN))
298 /* Nobody gets write access to a read-only fs */
299 if ((mask & MAY_WRITE) && (IS_RDONLY(inode) ||
300 (nd && MNT_IS_RDONLY(nd->mnt))) &&
301 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
303 /* Nobody gets write access to an immutable file */
304 if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode))
306 if (current->fsuid == inode->i_uid) {
308 } else if (test_opt(inode->i_sb, POSIX_ACL)) {
309 struct posix_acl *acl;
311 /* The access ACL cannot grant access if the group class
312 permission bits don't contain all requested permissions. */
313 if (((mode >> 3) & mask & S_IRWXO) != mask)
315 acl = ext2_get_acl(inode, ACL_TYPE_ACCESS);
317 int error = posix_acl_permission(inode, acl, mask);
318 posix_acl_release(acl);
319 if (error == -EACCES)
320 goto check_capabilities;
326 if (in_group_p(inode->i_gid))
329 if ((mode & mask & S_IRWXO) == mask)
333 /* Allowed to override Discretionary Access Control? */
334 if (!(mask & MAY_EXEC) ||
335 (inode->i_mode & S_IXUGO) || S_ISDIR(inode->i_mode))
336 if (capable(CAP_DAC_OVERRIDE))
338 /* Read and search granted if capable(CAP_DAC_READ_SEARCH) */
339 if (capable(CAP_DAC_READ_SEARCH) && ((mask == MAY_READ) ||
340 (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE))))
346 * Initialize the ACLs of a new inode. Called from ext2_new_inode.
349 * inode->i_sem: up (access to inode is still exclusive)
352 ext2_init_acl(struct inode *inode, struct inode *dir)
354 struct posix_acl *acl = NULL;
357 if (!S_ISLNK(inode->i_mode)) {
358 if (test_opt(dir->i_sb, POSIX_ACL)) {
359 acl = ext2_get_acl(dir, ACL_TYPE_DEFAULT);
364 inode->i_mode &= ~current->fs->umask;
366 if (test_opt(inode->i_sb, POSIX_ACL) && acl) {
367 struct posix_acl *clone;
370 if (S_ISDIR(inode->i_mode)) {
371 error = ext2_set_acl(inode, ACL_TYPE_DEFAULT, acl);
375 clone = posix_acl_clone(acl, GFP_KERNEL);
379 mode = inode->i_mode;
380 error = posix_acl_create_masq(clone, &mode);
382 inode->i_mode = mode;
384 /* This is an extended ACL */
385 error = ext2_set_acl(inode,
386 ACL_TYPE_ACCESS, clone);
389 posix_acl_release(clone);
392 posix_acl_release(acl);
397 * Does chmod for an inode that may have an Access Control List. The
398 * inode->i_mode field must be updated to the desired value by the caller
399 * before calling this function.
400 * Returns 0 on success, or a negative error number.
402 * We change the ACL rather than storing some ACL entries in the file
403 * mode permission bits (which would be more efficient), because that
404 * would break once additional permissions (like ACL_APPEND, ACL_DELETE
405 * for directories) are added. There are no more bits available in the
411 ext2_acl_chmod(struct inode *inode)
413 struct posix_acl *acl, *clone;
416 if (!test_opt(inode->i_sb, POSIX_ACL))
418 if (S_ISLNK(inode->i_mode))
420 acl = ext2_get_acl(inode, ACL_TYPE_ACCESS);
421 if (IS_ERR(acl) || !acl)
423 clone = posix_acl_clone(acl, GFP_KERNEL);
424 posix_acl_release(acl);
427 error = posix_acl_chmod_masq(clone, inode->i_mode);
429 error = ext2_set_acl(inode, ACL_TYPE_ACCESS, clone);
430 posix_acl_release(clone);
435 * Extended attribut handlers
438 ext2_xattr_list_acl_access(char *list, struct inode *inode,
439 const char *name, int name_len)
441 const size_t size = sizeof(XATTR_NAME_ACL_ACCESS);
443 if (!test_opt(inode->i_sb, POSIX_ACL))
446 memcpy(list, XATTR_NAME_ACL_ACCESS, size);
451 ext2_xattr_list_acl_default(char *list, struct inode *inode,
452 const char *name, int name_len)
454 const size_t size = sizeof(XATTR_NAME_ACL_DEFAULT);
456 if (!test_opt(inode->i_sb, POSIX_ACL))
459 memcpy(list, XATTR_NAME_ACL_DEFAULT, size);
464 ext2_xattr_get_acl(struct inode *inode, int type, void *buffer, size_t size)
466 struct posix_acl *acl;
469 if (!test_opt(inode->i_sb, POSIX_ACL))
472 acl = ext2_get_acl(inode, type);
477 error = posix_acl_to_xattr(acl, buffer, size);
478 posix_acl_release(acl);
484 ext2_xattr_get_acl_access(struct inode *inode, const char *name,
485 void *buffer, size_t size)
487 if (strcmp(name, "") != 0)
489 return ext2_xattr_get_acl(inode, ACL_TYPE_ACCESS, buffer, size);
493 ext2_xattr_get_acl_default(struct inode *inode, const char *name,
494 void *buffer, size_t size)
496 if (strcmp(name, "") != 0)
498 return ext2_xattr_get_acl(inode, ACL_TYPE_DEFAULT, buffer, size);
502 ext2_xattr_set_acl(struct inode *inode, int type, const void *value,
505 struct posix_acl *acl;
508 if (!test_opt(inode->i_sb, POSIX_ACL))
510 if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
514 acl = posix_acl_from_xattr(value, size);
518 error = posix_acl_valid(acl);
520 goto release_and_out;
525 error = ext2_set_acl(inode, type, acl);
528 posix_acl_release(acl);
533 ext2_xattr_set_acl_access(struct inode *inode, const char *name,
534 const void *value, size_t size, int flags)
536 if (strcmp(name, "") != 0)
538 return ext2_xattr_set_acl(inode, ACL_TYPE_ACCESS, value, size);
542 ext2_xattr_set_acl_default(struct inode *inode, const char *name,
543 const void *value, size_t size, int flags)
545 if (strcmp(name, "") != 0)
547 return ext2_xattr_set_acl(inode, ACL_TYPE_DEFAULT, value, size);
550 struct ext2_xattr_handler ext2_xattr_acl_access_handler = {
551 .prefix = XATTR_NAME_ACL_ACCESS,
552 .list = ext2_xattr_list_acl_access,
553 .get = ext2_xattr_get_acl_access,
554 .set = ext2_xattr_set_acl_access,
557 struct ext2_xattr_handler ext2_xattr_acl_default_handler = {
558 .prefix = XATTR_NAME_ACL_DEFAULT,
559 .list = ext2_xattr_list_acl_default,
560 .get = ext2_xattr_get_acl_default,
561 .set = ext2_xattr_set_acl_default,
567 ext2_xattr_unregister(EXT2_XATTR_INDEX_POSIX_ACL_ACCESS,
568 &ext2_xattr_acl_access_handler);
569 ext2_xattr_unregister(EXT2_XATTR_INDEX_POSIX_ACL_DEFAULT,
570 &ext2_xattr_acl_default_handler);
578 error = ext2_xattr_register(EXT2_XATTR_INDEX_POSIX_ACL_ACCESS,
579 &ext2_xattr_acl_access_handler);
582 error = ext2_xattr_register(EXT2_XATTR_INDEX_POSIX_ACL_DEFAULT,
583 &ext2_xattr_acl_default_handler);