2006-10-11 15:20:50 +07:00
|
|
|
/*
|
2006-10-11 15:20:53 +07:00
|
|
|
* linux/fs/ext4/acl.c
|
2006-10-11 15:20:50 +07:00
|
|
|
*
|
|
|
|
* Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
|
|
|
|
*/
|
|
|
|
|
2017-05-25 05:24:07 +07:00
|
|
|
#include <linux/quotaops.h>
|
2008-04-30 05:13:32 +07:00
|
|
|
#include "ext4_jbd2.h"
|
|
|
|
#include "ext4.h"
|
2006-10-11 15:20:50 +07:00
|
|
|
#include "xattr.h"
|
|
|
|
#include "acl.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert from filesystem to in-memory representation.
|
|
|
|
*/
|
|
|
|
static struct posix_acl *
|
2006-10-11 15:20:53 +07:00
|
|
|
ext4_acl_from_disk(const void *value, size_t size)
|
2006-10-11 15:20:50 +07:00
|
|
|
{
|
|
|
|
const char *end = (char *)value + size;
|
|
|
|
int n, count;
|
|
|
|
struct posix_acl *acl;
|
|
|
|
|
|
|
|
if (!value)
|
|
|
|
return NULL;
|
2006-10-11 15:20:53 +07:00
|
|
|
if (size < sizeof(ext4_acl_header))
|
2006-10-11 15:20:50 +07:00
|
|
|
return ERR_PTR(-EINVAL);
|
2006-10-11 15:20:53 +07:00
|
|
|
if (((ext4_acl_header *)value)->a_version !=
|
|
|
|
cpu_to_le32(EXT4_ACL_VERSION))
|
2006-10-11 15:20:50 +07:00
|
|
|
return ERR_PTR(-EINVAL);
|
2006-10-11 15:20:53 +07:00
|
|
|
value = (char *)value + sizeof(ext4_acl_header);
|
|
|
|
count = ext4_acl_count(size);
|
2006-10-11 15:20:50 +07:00
|
|
|
if (count < 0)
|
|
|
|
return ERR_PTR(-EINVAL);
|
|
|
|
if (count == 0)
|
|
|
|
return NULL;
|
2008-04-30 09:02:02 +07:00
|
|
|
acl = posix_acl_alloc(count, GFP_NOFS);
|
2006-10-11 15:20:50 +07:00
|
|
|
if (!acl)
|
|
|
|
return ERR_PTR(-ENOMEM);
|
2008-07-27 03:15:44 +07:00
|
|
|
for (n = 0; n < count; n++) {
|
2006-10-11 15:20:53 +07:00
|
|
|
ext4_acl_entry *entry =
|
|
|
|
(ext4_acl_entry *)value;
|
|
|
|
if ((char *)value + sizeof(ext4_acl_entry_short) > end)
|
2006-10-11 15:20:50 +07:00
|
|
|
goto fail;
|
|
|
|
acl->a_entries[n].e_tag = le16_to_cpu(entry->e_tag);
|
|
|
|
acl->a_entries[n].e_perm = le16_to_cpu(entry->e_perm);
|
2008-07-27 03:15:44 +07:00
|
|
|
|
|
|
|
switch (acl->a_entries[n].e_tag) {
|
|
|
|
case ACL_USER_OBJ:
|
|
|
|
case ACL_GROUP_OBJ:
|
|
|
|
case ACL_MASK:
|
|
|
|
case ACL_OTHER:
|
|
|
|
value = (char *)value +
|
|
|
|
sizeof(ext4_acl_entry_short);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ACL_USER:
|
2012-09-11 10:44:54 +07:00
|
|
|
value = (char *)value + sizeof(ext4_acl_entry);
|
|
|
|
if ((char *)value > end)
|
|
|
|
goto fail;
|
|
|
|
acl->a_entries[n].e_uid =
|
|
|
|
make_kuid(&init_user_ns,
|
|
|
|
le32_to_cpu(entry->e_id));
|
|
|
|
break;
|
2008-07-27 03:15:44 +07:00
|
|
|
case ACL_GROUP:
|
|
|
|
value = (char *)value + sizeof(ext4_acl_entry);
|
|
|
|
if ((char *)value > end)
|
2006-10-11 15:20:50 +07:00
|
|
|
goto fail;
|
2012-09-11 10:44:54 +07:00
|
|
|
acl->a_entries[n].e_gid =
|
|
|
|
make_kgid(&init_user_ns,
|
|
|
|
le32_to_cpu(entry->e_id));
|
2008-07-27 03:15:44 +07:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
goto fail;
|
2006-10-11 15:20:50 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (value != end)
|
|
|
|
goto fail;
|
|
|
|
return acl;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
posix_acl_release(acl);
|
|
|
|
return ERR_PTR(-EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert from in-memory to filesystem representation.
|
|
|
|
*/
|
|
|
|
static void *
|
2006-10-11 15:20:53 +07:00
|
|
|
ext4_acl_to_disk(const struct posix_acl *acl, size_t *size)
|
2006-10-11 15:20:50 +07:00
|
|
|
{
|
2006-10-11 15:20:53 +07:00
|
|
|
ext4_acl_header *ext_acl;
|
2006-10-11 15:20:50 +07:00
|
|
|
char *e;
|
|
|
|
size_t n;
|
|
|
|
|
2006-10-11 15:20:53 +07:00
|
|
|
*size = ext4_acl_size(acl->a_count);
|
|
|
|
ext_acl = kmalloc(sizeof(ext4_acl_header) + acl->a_count *
|
2008-04-30 09:02:02 +07:00
|
|
|
sizeof(ext4_acl_entry), GFP_NOFS);
|
2006-10-11 15:20:50 +07:00
|
|
|
if (!ext_acl)
|
|
|
|
return ERR_PTR(-ENOMEM);
|
2006-10-11 15:20:53 +07:00
|
|
|
ext_acl->a_version = cpu_to_le32(EXT4_ACL_VERSION);
|
|
|
|
e = (char *)ext_acl + sizeof(ext4_acl_header);
|
2008-07-27 03:15:44 +07:00
|
|
|
for (n = 0; n < acl->a_count; n++) {
|
2012-09-11 10:44:54 +07:00
|
|
|
const struct posix_acl_entry *acl_e = &acl->a_entries[n];
|
2006-10-11 15:20:53 +07:00
|
|
|
ext4_acl_entry *entry = (ext4_acl_entry *)e;
|
2012-09-11 10:44:54 +07:00
|
|
|
entry->e_tag = cpu_to_le16(acl_e->e_tag);
|
|
|
|
entry->e_perm = cpu_to_le16(acl_e->e_perm);
|
|
|
|
switch (acl_e->e_tag) {
|
2008-07-27 03:15:44 +07:00
|
|
|
case ACL_USER:
|
2012-09-11 10:44:54 +07:00
|
|
|
entry->e_id = cpu_to_le32(
|
|
|
|
from_kuid(&init_user_ns, acl_e->e_uid));
|
|
|
|
e += sizeof(ext4_acl_entry);
|
|
|
|
break;
|
2008-07-27 03:15:44 +07:00
|
|
|
case ACL_GROUP:
|
2012-09-11 10:44:54 +07:00
|
|
|
entry->e_id = cpu_to_le32(
|
|
|
|
from_kgid(&init_user_ns, acl_e->e_gid));
|
2008-07-27 03:15:44 +07:00
|
|
|
e += sizeof(ext4_acl_entry);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ACL_USER_OBJ:
|
|
|
|
case ACL_GROUP_OBJ:
|
|
|
|
case ACL_MASK:
|
|
|
|
case ACL_OTHER:
|
|
|
|
e += sizeof(ext4_acl_entry_short);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
goto fail;
|
2006-10-11 15:20:50 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return (char *)ext_acl;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
kfree(ext_acl);
|
|
|
|
return ERR_PTR(-EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Inode operation get_posix_acl().
|
|
|
|
*
|
|
|
|
* inode->i_mutex: don't care
|
|
|
|
*/
|
2011-07-23 22:37:31 +07:00
|
|
|
struct posix_acl *
|
2006-10-11 15:20:53 +07:00
|
|
|
ext4_get_acl(struct inode *inode, int type)
|
2006-10-11 15:20:50 +07:00
|
|
|
{
|
|
|
|
int name_index;
|
|
|
|
char *value = NULL;
|
|
|
|
struct posix_acl *acl;
|
|
|
|
int retval;
|
|
|
|
|
2008-07-27 03:15:44 +07:00
|
|
|
switch (type) {
|
|
|
|
case ACL_TYPE_ACCESS:
|
|
|
|
name_index = EXT4_XATTR_INDEX_POSIX_ACL_ACCESS;
|
|
|
|
break;
|
|
|
|
case ACL_TYPE_DEFAULT:
|
|
|
|
name_index = EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT;
|
|
|
|
break;
|
|
|
|
default:
|
2009-06-09 23:11:54 +07:00
|
|
|
BUG();
|
2006-10-11 15:20:50 +07:00
|
|
|
}
|
2006-10-11 15:20:53 +07:00
|
|
|
retval = ext4_xattr_get(inode, name_index, "", NULL, 0);
|
2006-10-11 15:20:50 +07:00
|
|
|
if (retval > 0) {
|
2008-04-30 09:02:02 +07:00
|
|
|
value = kmalloc(retval, GFP_NOFS);
|
2006-10-11 15:20:50 +07:00
|
|
|
if (!value)
|
|
|
|
return ERR_PTR(-ENOMEM);
|
2006-10-11 15:20:53 +07:00
|
|
|
retval = ext4_xattr_get(inode, name_index, "", value, retval);
|
2006-10-11 15:20:50 +07:00
|
|
|
}
|
|
|
|
if (retval > 0)
|
2006-10-11 15:20:53 +07:00
|
|
|
acl = ext4_acl_from_disk(value, retval);
|
2006-10-11 15:20:50 +07:00
|
|
|
else if (retval == -ENODATA || retval == -ENOSYS)
|
|
|
|
acl = NULL;
|
|
|
|
else
|
|
|
|
acl = ERR_PTR(retval);
|
|
|
|
kfree(value);
|
|
|
|
|
|
|
|
return acl;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the access or default ACL of an inode.
|
|
|
|
*
|
2006-10-11 15:20:53 +07:00
|
|
|
* inode->i_mutex: down unless called from ext4_new_inode
|
2006-10-11 15:20:50 +07:00
|
|
|
*/
|
|
|
|
static int
|
2013-12-20 20:16:44 +07:00
|
|
|
__ext4_set_acl(handle_t *handle, struct inode *inode, int type,
|
2017-07-06 11:01:59 +07:00
|
|
|
struct posix_acl *acl, int xattr_flags)
|
2006-10-11 15:20:50 +07:00
|
|
|
{
|
|
|
|
int name_index;
|
|
|
|
void *value = NULL;
|
|
|
|
size_t size = 0;
|
|
|
|
int error;
|
|
|
|
|
2008-07-27 03:15:44 +07:00
|
|
|
switch (type) {
|
|
|
|
case ACL_TYPE_ACCESS:
|
|
|
|
name_index = EXT4_XATTR_INDEX_POSIX_ACL_ACCESS;
|
|
|
|
break;
|
2006-10-11 15:20:50 +07:00
|
|
|
|
2008-07-27 03:15:44 +07:00
|
|
|
case ACL_TYPE_DEFAULT:
|
|
|
|
name_index = EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT;
|
|
|
|
if (!S_ISDIR(inode->i_mode))
|
|
|
|
return acl ? -EACCES : 0;
|
|
|
|
break;
|
2006-10-11 15:20:50 +07:00
|
|
|
|
2008-07-27 03:15:44 +07:00
|
|
|
default:
|
|
|
|
return -EINVAL;
|
2006-10-11 15:20:50 +07:00
|
|
|
}
|
|
|
|
if (acl) {
|
2006-10-11 15:20:53 +07:00
|
|
|
value = ext4_acl_to_disk(acl, &size);
|
2006-10-11 15:20:50 +07:00
|
|
|
if (IS_ERR(value))
|
|
|
|
return (int)PTR_ERR(value);
|
|
|
|
}
|
|
|
|
|
2006-10-11 15:20:53 +07:00
|
|
|
error = ext4_xattr_set_handle(handle, inode, name_index, "",
|
2017-07-06 11:01:59 +07:00
|
|
|
value, size, xattr_flags);
|
2006-10-11 15:20:50 +07:00
|
|
|
|
|
|
|
kfree(value);
|
2017-07-31 09:43:41 +07:00
|
|
|
if (!error) {
|
2009-06-09 23:11:54 +07:00
|
|
|
set_cached_acl(inode, type, acl);
|
2017-07-31 09:43:41 +07:00
|
|
|
}
|
2008-07-27 03:15:44 +07:00
|
|
|
|
2006-10-11 15:20:50 +07:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2013-12-20 20:16:44 +07:00
|
|
|
ext4_set_acl(struct inode *inode, struct posix_acl *acl, int type)
|
2006-10-11 15:20:50 +07:00
|
|
|
{
|
2011-07-23 11:18:02 +07:00
|
|
|
handle_t *handle;
|
2017-06-22 09:28:40 +07:00
|
|
|
int error, credits, retries = 0;
|
|
|
|
size_t acl_size = acl ? ext4_acl_size(acl->a_count) : 0;
|
2017-07-31 10:33:01 +07:00
|
|
|
umode_t mode = inode->i_mode;
|
|
|
|
int update_mode = 0;
|
2011-07-23 11:18:02 +07:00
|
|
|
|
2017-05-25 05:24:07 +07:00
|
|
|
error = dquot_initialize(inode);
|
|
|
|
if (error)
|
|
|
|
return error;
|
2011-07-23 11:18:02 +07:00
|
|
|
retry:
|
2017-07-06 11:01:59 +07:00
|
|
|
error = ext4_xattr_set_credits(inode, acl_size, false /* is_create */,
|
|
|
|
&credits);
|
2017-06-22 22:44:55 +07:00
|
|
|
if (error)
|
|
|
|
return error;
|
|
|
|
|
2017-06-22 09:28:40 +07:00
|
|
|
handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits);
|
2013-12-20 20:16:44 +07:00
|
|
|
if (IS_ERR(handle))
|
|
|
|
return PTR_ERR(handle);
|
|
|
|
|
2017-07-31 10:33:01 +07:00
|
|
|
if ((type == ACL_TYPE_ACCESS) && acl) {
|
|
|
|
error = posix_acl_update_mode(inode, &mode, &acl);
|
|
|
|
if (error)
|
|
|
|
goto out_stop;
|
|
|
|
update_mode = 1;
|
|
|
|
}
|
|
|
|
|
2017-07-06 11:01:59 +07:00
|
|
|
error = __ext4_set_acl(handle, inode, type, acl, 0 /* xattr_flags */);
|
2017-07-31 10:33:01 +07:00
|
|
|
if (!error && update_mode) {
|
|
|
|
inode->i_mode = mode;
|
|
|
|
inode->i_ctime = current_time(inode);
|
|
|
|
ext4_mark_inode_dirty(handle, inode);
|
|
|
|
}
|
|
|
|
out_stop:
|
2011-07-23 11:18:02 +07:00
|
|
|
ext4_journal_stop(handle);
|
2013-12-20 20:16:44 +07:00
|
|
|
if (error == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
|
2011-07-23 11:18:02 +07:00
|
|
|
goto retry;
|
2006-10-11 15:20:50 +07:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2013-12-20 20:16:44 +07:00
|
|
|
* Initialize the ACLs of a new inode. Called from ext4_new_inode.
|
|
|
|
*
|
|
|
|
* dir->i_mutex: down
|
|
|
|
* inode->i_mutex: up (access to inode is still exclusive)
|
2006-10-11 15:20:50 +07:00
|
|
|
*/
|
2013-12-20 20:16:44 +07:00
|
|
|
int
|
|
|
|
ext4_init_acl(handle_t *handle, struct inode *inode, struct inode *dir)
|
2006-10-11 15:20:50 +07:00
|
|
|
{
|
2013-12-20 20:16:44 +07:00
|
|
|
struct posix_acl *default_acl, *acl;
|
2006-10-11 15:20:50 +07:00
|
|
|
int error;
|
|
|
|
|
2013-12-20 20:16:44 +07:00
|
|
|
error = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl);
|
|
|
|
if (error)
|
|
|
|
return error;
|
2006-10-11 15:20:50 +07:00
|
|
|
|
2013-12-20 20:16:44 +07:00
|
|
|
if (default_acl) {
|
|
|
|
error = __ext4_set_acl(handle, inode, ACL_TYPE_DEFAULT,
|
2017-07-06 11:01:59 +07:00
|
|
|
default_acl, XATTR_CREATE);
|
2013-12-20 20:16:44 +07:00
|
|
|
posix_acl_release(default_acl);
|
|
|
|
}
|
|
|
|
if (acl) {
|
|
|
|
if (!error)
|
|
|
|
error = __ext4_set_acl(handle, inode, ACL_TYPE_ACCESS,
|
2017-07-06 11:01:59 +07:00
|
|
|
acl, XATTR_CREATE);
|
2013-12-20 20:16:44 +07:00
|
|
|
posix_acl_release(acl);
|
2012-11-09 03:11:11 +07:00
|
|
|
}
|
2006-10-11 15:20:50 +07:00
|
|
|
return error;
|
|
|
|
}
|