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>
17 * Convert from filesystem to in-memory representation.
19 static struct posix_acl *
20 ext2_acl_from_disk(const void *value, size_t size)
22 const char *end = (char *)value + size;
24 struct posix_acl *acl;
28 if (size < sizeof(ext2_acl_header))
29 return ERR_PTR(-EINVAL);
30 if (((ext2_acl_header *)value)->a_version !=
31 cpu_to_le32(EXT2_ACL_VERSION))
32 return ERR_PTR(-EINVAL);
33 value = (char *)value + sizeof(ext2_acl_header);
34 count = ext2_acl_count(size);
36 return ERR_PTR(-EINVAL);
39 acl = posix_acl_alloc(count, GFP_KERNEL);
41 return ERR_PTR(-ENOMEM);
42 for (n=0; n < count; n++) {
43 ext2_acl_entry *entry =
44 (ext2_acl_entry *)value;
45 if ((char *)value + sizeof(ext2_acl_entry_short) > end)
47 acl->a_entries[n].e_tag = le16_to_cpu(entry->e_tag);
48 acl->a_entries[n].e_perm = le16_to_cpu(entry->e_perm);
49 switch(acl->a_entries[n].e_tag) {
54 value = (char *)value +
55 sizeof(ext2_acl_entry_short);
56 acl->a_entries[n].e_id = ACL_UNDEFINED_ID;
61 value = (char *)value + sizeof(ext2_acl_entry);
62 if ((char *)value > end)
64 acl->a_entries[n].e_id =
65 le32_to_cpu(entry->e_id);
77 posix_acl_release(acl);
78 return ERR_PTR(-EINVAL);
82 * Convert from in-memory to filesystem representation.
85 ext2_acl_to_disk(const struct posix_acl *acl, size_t *size)
87 ext2_acl_header *ext_acl;
91 *size = ext2_acl_size(acl->a_count);
92 ext_acl = (ext2_acl_header *)kmalloc(sizeof(ext2_acl_header) +
93 acl->a_count * sizeof(ext2_acl_entry), GFP_KERNEL);
95 return ERR_PTR(-ENOMEM);
96 ext_acl->a_version = cpu_to_le32(EXT2_ACL_VERSION);
97 e = (char *)ext_acl + sizeof(ext2_acl_header);
98 for (n=0; n < acl->a_count; n++) {
99 ext2_acl_entry *entry = (ext2_acl_entry *)e;
100 entry->e_tag = cpu_to_le16(acl->a_entries[n].e_tag);
101 entry->e_perm = cpu_to_le16(acl->a_entries[n].e_perm);
102 switch(acl->a_entries[n].e_tag) {
106 cpu_to_le32(acl->a_entries[n].e_id);
107 e += sizeof(ext2_acl_entry);
114 e += sizeof(ext2_acl_entry_short);
121 return (char *)ext_acl;
125 return ERR_PTR(-EINVAL);
128 static inline struct posix_acl *
129 ext2_iget_acl(struct inode *inode, struct posix_acl **i_acl)
131 struct posix_acl *acl = EXT2_ACL_NOT_CACHED;
133 spin_lock(&inode->i_lock);
134 if (*i_acl != EXT2_ACL_NOT_CACHED)
135 acl = posix_acl_dup(*i_acl);
136 spin_unlock(&inode->i_lock);
142 ext2_iset_acl(struct inode *inode, struct posix_acl **i_acl,
143 struct posix_acl *acl)
145 spin_lock(&inode->i_lock);
146 if (*i_acl != EXT2_ACL_NOT_CACHED)
147 posix_acl_release(*i_acl);
148 *i_acl = posix_acl_dup(acl);
149 spin_unlock(&inode->i_lock);
153 * inode->i_sem: don't care
155 static struct posix_acl *
156 ext2_get_acl(struct inode *inode, int type)
158 struct ext2_inode_info *ei = EXT2_I(inode);
161 struct posix_acl *acl;
164 if (!test_opt(inode->i_sb, POSIX_ACL))
168 case ACL_TYPE_ACCESS:
169 acl = ext2_iget_acl(inode, &ei->i_acl);
170 if (acl != EXT2_ACL_NOT_CACHED)
172 name_index = EXT2_XATTR_INDEX_POSIX_ACL_ACCESS;
175 case ACL_TYPE_DEFAULT:
176 acl = ext2_iget_acl(inode, &ei->i_default_acl);
177 if (acl != EXT2_ACL_NOT_CACHED)
179 name_index = EXT2_XATTR_INDEX_POSIX_ACL_DEFAULT;
183 return ERR_PTR(-EINVAL);
185 retval = ext2_xattr_get(inode, name_index, "", NULL, 0);
187 value = kmalloc(retval, GFP_KERNEL);
189 return ERR_PTR(-ENOMEM);
190 retval = ext2_xattr_get(inode, name_index, "", value, retval);
193 acl = ext2_acl_from_disk(value, retval);
194 else if (retval == -ENODATA || retval == -ENOSYS)
197 acl = ERR_PTR(retval);
203 case ACL_TYPE_ACCESS:
204 ext2_iset_acl(inode, &ei->i_acl, acl);
207 case ACL_TYPE_DEFAULT:
208 ext2_iset_acl(inode, &ei->i_default_acl, acl);
219 ext2_set_acl(struct inode *inode, int type, struct posix_acl *acl)
221 struct ext2_inode_info *ei = EXT2_I(inode);
227 if (S_ISLNK(inode->i_mode))
229 if (!test_opt(inode->i_sb, POSIX_ACL))
233 case ACL_TYPE_ACCESS:
234 name_index = EXT2_XATTR_INDEX_POSIX_ACL_ACCESS;
236 mode_t mode = inode->i_mode;
237 error = posix_acl_equiv_mode(acl, &mode);
241 inode->i_mode = mode;
242 mark_inode_dirty(inode);
249 case ACL_TYPE_DEFAULT:
250 name_index = EXT2_XATTR_INDEX_POSIX_ACL_DEFAULT;
251 if (!S_ISDIR(inode->i_mode))
252 return acl ? -EACCES : 0;
259 if (acl->a_count > EXT2_ACL_MAX_ENTRIES)
261 value = ext2_acl_to_disk(acl, &size);
263 return (int)PTR_ERR(value);
266 error = ext2_xattr_set(inode, name_index, "", value, size, 0);
272 case ACL_TYPE_ACCESS:
273 ext2_iset_acl(inode, &ei->i_acl, acl);
276 case ACL_TYPE_DEFAULT:
277 ext2_iset_acl(inode, &ei->i_default_acl, acl);
285 * Inode operation permission().
287 * inode->i_sem: don't care
290 ext2_permission(struct inode *inode, int mask, struct nameidata *nd)
292 int mode = inode->i_mode;
294 /* Nobody gets write access to a read-only fs */
295 if ((mask & MAY_WRITE) && (IS_RDONLY(inode) ||
296 (nd && MNT_IS_RDONLY(nd->mnt))) &&
297 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
299 /* Nobody gets write access to an immutable file */
300 if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode))
302 if (current->fsuid == inode->i_uid) {
304 } else if (test_opt(inode->i_sb, POSIX_ACL)) {
305 struct posix_acl *acl;
307 /* The access ACL cannot grant access if the group class
308 permission bits don't contain all requested permissions. */
309 if (((mode >> 3) & mask & S_IRWXO) != mask)
311 acl = ext2_get_acl(inode, ACL_TYPE_ACCESS);
313 int error = posix_acl_permission(inode, acl, mask);
314 posix_acl_release(acl);
315 if (error == -EACCES)
316 goto check_capabilities;
322 if (in_group_p(inode->i_gid))
325 if ((mode & mask & S_IRWXO) == mask)
329 /* Allowed to override Discretionary Access Control? */
330 if (!(mask & MAY_EXEC) ||
331 (inode->i_mode & S_IXUGO) || S_ISDIR(inode->i_mode))
332 if (capable(CAP_DAC_OVERRIDE))
334 /* Read and search granted if capable(CAP_DAC_READ_SEARCH) */
335 if (capable(CAP_DAC_READ_SEARCH) && ((mask == MAY_READ) ||
336 (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE))))
342 * Initialize the ACLs of a new inode. Called from ext2_new_inode.
345 * inode->i_sem: up (access to inode is still exclusive)
348 ext2_init_acl(struct inode *inode, struct inode *dir)
350 struct posix_acl *acl = NULL;
353 if (!S_ISLNK(inode->i_mode)) {
354 if (test_opt(dir->i_sb, POSIX_ACL)) {
355 acl = ext2_get_acl(dir, ACL_TYPE_DEFAULT);
360 inode->i_mode &= ~current->fs->umask;
362 if (test_opt(inode->i_sb, POSIX_ACL) && acl) {
363 struct posix_acl *clone;
366 if (S_ISDIR(inode->i_mode)) {
367 error = ext2_set_acl(inode, ACL_TYPE_DEFAULT, acl);
371 clone = posix_acl_clone(acl, GFP_KERNEL);
375 mode = inode->i_mode;
376 error = posix_acl_create_masq(clone, &mode);
378 inode->i_mode = mode;
380 /* This is an extended ACL */
381 error = ext2_set_acl(inode,
382 ACL_TYPE_ACCESS, clone);
385 posix_acl_release(clone);
388 posix_acl_release(acl);
393 * Does chmod for an inode that may have an Access Control List. The
394 * inode->i_mode field must be updated to the desired value by the caller
395 * before calling this function.
396 * Returns 0 on success, or a negative error number.
398 * We change the ACL rather than storing some ACL entries in the file
399 * mode permission bits (which would be more efficient), because that
400 * would break once additional permissions (like ACL_APPEND, ACL_DELETE
401 * for directories) are added. There are no more bits available in the
407 ext2_acl_chmod(struct inode *inode)
409 struct posix_acl *acl, *clone;
412 if (!test_opt(inode->i_sb, POSIX_ACL))
414 if (S_ISLNK(inode->i_mode))
416 acl = ext2_get_acl(inode, ACL_TYPE_ACCESS);
417 if (IS_ERR(acl) || !acl)
419 clone = posix_acl_clone(acl, GFP_KERNEL);
420 posix_acl_release(acl);
423 error = posix_acl_chmod_masq(clone, inode->i_mode);
425 error = ext2_set_acl(inode, ACL_TYPE_ACCESS, clone);
426 posix_acl_release(clone);
431 * Extended attribut handlers
434 ext2_xattr_list_acl_access(char *list, struct inode *inode,
435 const char *name, int name_len)
437 const size_t size = sizeof(XATTR_NAME_ACL_ACCESS);
439 if (!test_opt(inode->i_sb, POSIX_ACL))
442 memcpy(list, XATTR_NAME_ACL_ACCESS, size);
447 ext2_xattr_list_acl_default(char *list, struct inode *inode,
448 const char *name, int name_len)
450 const size_t size = sizeof(XATTR_NAME_ACL_DEFAULT);
452 if (!test_opt(inode->i_sb, POSIX_ACL))
455 memcpy(list, XATTR_NAME_ACL_DEFAULT, size);
460 ext2_xattr_get_acl(struct inode *inode, int type, void *buffer, size_t size)
462 struct posix_acl *acl;
465 if (!test_opt(inode->i_sb, POSIX_ACL))
468 acl = ext2_get_acl(inode, type);
473 error = posix_acl_to_xattr(acl, buffer, size);
474 posix_acl_release(acl);
480 ext2_xattr_get_acl_access(struct inode *inode, const char *name,
481 void *buffer, size_t size)
483 if (strcmp(name, "") != 0)
485 return ext2_xattr_get_acl(inode, ACL_TYPE_ACCESS, buffer, size);
489 ext2_xattr_get_acl_default(struct inode *inode, const char *name,
490 void *buffer, size_t size)
492 if (strcmp(name, "") != 0)
494 return ext2_xattr_get_acl(inode, ACL_TYPE_DEFAULT, buffer, size);
498 ext2_xattr_set_acl(struct inode *inode, int type, const void *value,
501 struct posix_acl *acl;
504 if (!test_opt(inode->i_sb, POSIX_ACL))
506 if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
510 acl = posix_acl_from_xattr(value, size);
514 error = posix_acl_valid(acl);
516 goto release_and_out;
521 error = ext2_set_acl(inode, type, acl);
524 posix_acl_release(acl);
529 ext2_xattr_set_acl_access(struct inode *inode, const char *name,
530 const void *value, size_t size, int flags)
532 if (strcmp(name, "") != 0)
534 return ext2_xattr_set_acl(inode, ACL_TYPE_ACCESS, value, size);
538 ext2_xattr_set_acl_default(struct inode *inode, const char *name,
539 const void *value, size_t size, int flags)
541 if (strcmp(name, "") != 0)
543 return ext2_xattr_set_acl(inode, ACL_TYPE_DEFAULT, value, size);
546 struct ext2_xattr_handler ext2_xattr_acl_access_handler = {
547 .prefix = XATTR_NAME_ACL_ACCESS,
548 .list = ext2_xattr_list_acl_access,
549 .get = ext2_xattr_get_acl_access,
550 .set = ext2_xattr_set_acl_access,
553 struct ext2_xattr_handler ext2_xattr_acl_default_handler = {
554 .prefix = XATTR_NAME_ACL_DEFAULT,
555 .list = ext2_xattr_list_acl_default,
556 .get = ext2_xattr_get_acl_default,
557 .set = ext2_xattr_set_acl_default,
563 ext2_xattr_unregister(EXT2_XATTR_INDEX_POSIX_ACL_ACCESS,
564 &ext2_xattr_acl_access_handler);
565 ext2_xattr_unregister(EXT2_XATTR_INDEX_POSIX_ACL_DEFAULT,
566 &ext2_xattr_acl_default_handler);
574 error = ext2_xattr_register(EXT2_XATTR_INDEX_POSIX_ACL_ACCESS,
575 &ext2_xattr_acl_access_handler);
578 error = ext2_xattr_register(EXT2_XATTR_INDEX_POSIX_ACL_DEFAULT,
579 &ext2_xattr_acl_default_handler);