diff options
Diffstat (limited to 'fs/posix_acl.c')
| -rw-r--r-- | fs/posix_acl.c | 533 | 
1 files changed, 507 insertions, 26 deletions
diff --git a/fs/posix_acl.c b/fs/posix_acl.c index 8bd2135b7f8..0855f772cd4 100644 --- a/fs/posix_acl.c +++ b/fs/posix_acl.c @@ -1,10 +1,8 @@  /* - * linux/fs/posix_acl.c + * Copyright (C) 2002,2003 by Andreas Gruenbacher <a.gruenbacher@computer.org>   * - *  Copyright (C) 2002 by Andreas Gruenbacher <a.gruenbacher@computer.org> - * - *  Fixes from William Schumacher incorporated on 15 March 2001. - *     (Reported by Charles Bertsch, <CBertsch@microtest.com>). + * Fixes from William Schumacher incorporated on 15 March 2001. + *    (Reported by Charles Bertsch, <CBertsch@microtest.com>).   */  /* @@ -18,15 +16,112 @@  #include <linux/fs.h>  #include <linux/sched.h>  #include <linux/posix_acl.h> +#include <linux/posix_acl_xattr.h> +#include <linux/xattr.h>  #include <linux/export.h> +#include <linux/user_namespace.h> -#include <linux/errno.h> +struct posix_acl **acl_by_type(struct inode *inode, int type) +{ +	switch (type) { +	case ACL_TYPE_ACCESS: +		return &inode->i_acl; +	case ACL_TYPE_DEFAULT: +		return &inode->i_default_acl; +	default: +		BUG(); +	} +} +EXPORT_SYMBOL(acl_by_type); -EXPORT_SYMBOL(posix_acl_init); -EXPORT_SYMBOL(posix_acl_alloc); -EXPORT_SYMBOL(posix_acl_valid); -EXPORT_SYMBOL(posix_acl_equiv_mode); -EXPORT_SYMBOL(posix_acl_from_mode); +struct posix_acl *get_cached_acl(struct inode *inode, int type) +{ +	struct posix_acl **p = acl_by_type(inode, type); +	struct posix_acl *acl = ACCESS_ONCE(*p); +	if (acl) { +		spin_lock(&inode->i_lock); +		acl = *p; +		if (acl != ACL_NOT_CACHED) +			acl = posix_acl_dup(acl); +		spin_unlock(&inode->i_lock); +	} +	return acl; +} +EXPORT_SYMBOL(get_cached_acl); + +struct posix_acl *get_cached_acl_rcu(struct inode *inode, int type) +{ +	return rcu_dereference(*acl_by_type(inode, type)); +} +EXPORT_SYMBOL(get_cached_acl_rcu); + +void set_cached_acl(struct inode *inode, int type, struct posix_acl *acl) +{ +	struct posix_acl **p = acl_by_type(inode, type); +	struct posix_acl *old; +	spin_lock(&inode->i_lock); +	old = *p; +	rcu_assign_pointer(*p, posix_acl_dup(acl)); +	spin_unlock(&inode->i_lock); +	if (old != ACL_NOT_CACHED) +		posix_acl_release(old); +} +EXPORT_SYMBOL(set_cached_acl); + +void forget_cached_acl(struct inode *inode, int type) +{ +	struct posix_acl **p = acl_by_type(inode, type); +	struct posix_acl *old; +	spin_lock(&inode->i_lock); +	old = *p; +	*p = ACL_NOT_CACHED; +	spin_unlock(&inode->i_lock); +	if (old != ACL_NOT_CACHED) +		posix_acl_release(old); +} +EXPORT_SYMBOL(forget_cached_acl); + +void forget_all_cached_acls(struct inode *inode) +{ +	struct posix_acl *old_access, *old_default; +	spin_lock(&inode->i_lock); +	old_access = inode->i_acl; +	old_default = inode->i_default_acl; +	inode->i_acl = inode->i_default_acl = ACL_NOT_CACHED; +	spin_unlock(&inode->i_lock); +	if (old_access != ACL_NOT_CACHED) +		posix_acl_release(old_access); +	if (old_default != ACL_NOT_CACHED) +		posix_acl_release(old_default); +} +EXPORT_SYMBOL(forget_all_cached_acls); + +struct posix_acl *get_acl(struct inode *inode, int type) +{ +	struct posix_acl *acl; + +	acl = get_cached_acl(inode, type); +	if (acl != ACL_NOT_CACHED) +		return acl; + +	if (!IS_POSIXACL(inode)) +		return NULL; + +	/* +	 * A filesystem can force a ACL callback by just never filling the +	 * ACL cache. But normally you'd fill the cache either at inode +	 * instantiation time, or on the first ->get_acl call. +	 * +	 * If the filesystem doesn't have a get_acl() function at all, we'll +	 * just create the negative cache entry. +	 */ +	if (!inode->i_op->get_acl) { +		set_cached_acl(inode, type, NULL); +		return NULL; +	} +	return inode->i_op->get_acl(inode, type); +} +EXPORT_SYMBOL(get_acl);  /*   * Init a fresh posix_acl @@ -37,6 +132,7 @@ posix_acl_init(struct posix_acl *acl, int count)  	atomic_set(&acl->a_refcount, 1);  	acl->a_count = count;  } +EXPORT_SYMBOL(posix_acl_init);  /*   * Allocate a new ACL with the specified number of entries. @@ -51,6 +147,7 @@ posix_acl_alloc(int count, gfp_t flags)  		posix_acl_init(acl, count);  	return acl;  } +EXPORT_SYMBOL(posix_acl_alloc);  /*   * Clone an ACL. @@ -78,8 +175,6 @@ posix_acl_valid(const struct posix_acl *acl)  {  	const struct posix_acl_entry *pa, *pe;  	int state = ACL_USER_OBJ; -	kuid_t prev_uid = INVALID_UID; -	kgid_t prev_gid = INVALID_GID;  	int needs_mask = 0;  	FOREACH_ACL_ENTRY(pa, acl, pe) { @@ -98,10 +193,6 @@ posix_acl_valid(const struct posix_acl *acl)  					return -EINVAL;  				if (!uid_valid(pa->e_uid))  					return -EINVAL; -				if (uid_valid(prev_uid) && -				    uid_lte(pa->e_uid, prev_uid)) -					return -EINVAL; -				prev_uid = pa->e_uid;  				needs_mask = 1;  				break; @@ -117,10 +208,6 @@ posix_acl_valid(const struct posix_acl *acl)  					return -EINVAL;  				if (!gid_valid(pa->e_gid))  					return -EINVAL; -				if (gid_valid(prev_gid) && -				    gid_lte(pa->e_gid, prev_gid)) -					return -EINVAL; -				prev_gid = pa->e_gid;  				needs_mask = 1;  				break; @@ -146,6 +233,7 @@ posix_acl_valid(const struct posix_acl *acl)  		return 0;  	return -EINVAL;  } +EXPORT_SYMBOL(posix_acl_valid);  /*   * Returns 0 if the acl can be exactly represented in the traditional @@ -158,6 +246,12 @@ posix_acl_equiv_mode(const struct posix_acl *acl, umode_t *mode_p)  	umode_t mode = 0;  	int not_equiv = 0; +	/* +	 * A null ACL can always be presented as mode bits. +	 */ +	if (!acl) +		return 0; +  	FOREACH_ACL_ENTRY(pa, acl, pe) {  		switch (pa->e_tag) {  			case ACL_USER_OBJ: @@ -186,6 +280,7 @@ posix_acl_equiv_mode(const struct posix_acl *acl, umode_t *mode_p)                  *mode_p = (*mode_p & ~S_IRWXUGO) | mode;          return not_equiv;  } +EXPORT_SYMBOL(posix_acl_equiv_mode);  /*   * Create an ACL representing the file mode permission bits of an inode. @@ -207,6 +302,7 @@ posix_acl_from_mode(umode_t mode, gfp_t flags)  	acl->a_entries[2].e_perm = (mode & S_IRWXO);  	return acl;  } +EXPORT_SYMBOL(posix_acl_from_mode);  /*   * Return 0 if current is granted want access to the inode @@ -338,7 +434,7 @@ static int posix_acl_create_masq(struct posix_acl *acl, umode_t *mode_p)  /*   * Modify the ACL for the chmod syscall.   */ -static int posix_acl_chmod_masq(struct posix_acl *acl, umode_t mode) +static int __posix_acl_chmod_masq(struct posix_acl *acl, umode_t mode)  {  	struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL;  	struct posix_acl_entry *pa, *pe; @@ -384,7 +480,7 @@ static int posix_acl_chmod_masq(struct posix_acl *acl, umode_t mode)  }  int -posix_acl_create(struct posix_acl **acl, gfp_t gfp, umode_t *mode_p) +__posix_acl_create(struct posix_acl **acl, gfp_t gfp, umode_t *mode_p)  {  	struct posix_acl *clone = posix_acl_clone(*acl, gfp);  	int err = -ENOMEM; @@ -399,15 +495,15 @@ posix_acl_create(struct posix_acl **acl, gfp_t gfp, umode_t *mode_p)  	*acl = clone;  	return err;  } -EXPORT_SYMBOL(posix_acl_create); +EXPORT_SYMBOL(__posix_acl_create);  int -posix_acl_chmod(struct posix_acl **acl, gfp_t gfp, umode_t mode) +__posix_acl_chmod(struct posix_acl **acl, gfp_t gfp, umode_t mode)  {  	struct posix_acl *clone = posix_acl_clone(*acl, gfp);  	int err = -ENOMEM;  	if (clone) { -		err = posix_acl_chmod_masq(clone, mode); +		err = __posix_acl_chmod_masq(clone, mode);  		if (err) {  			posix_acl_release(clone);  			clone = NULL; @@ -417,4 +513,389 @@ posix_acl_chmod(struct posix_acl **acl, gfp_t gfp, umode_t mode)  	*acl = clone;  	return err;  } +EXPORT_SYMBOL(__posix_acl_chmod); + +int +posix_acl_chmod(struct inode *inode, umode_t mode) +{ +	struct posix_acl *acl; +	int ret = 0; + +	if (!IS_POSIXACL(inode)) +		return 0; +	if (!inode->i_op->set_acl) +		return -EOPNOTSUPP; + +	acl = get_acl(inode, ACL_TYPE_ACCESS); +	if (IS_ERR_OR_NULL(acl)) { +		if (acl == ERR_PTR(-EOPNOTSUPP)) +			return 0; +		return PTR_ERR(acl); +	} + +	ret = __posix_acl_chmod(&acl, GFP_KERNEL, mode); +	if (ret) +		return ret; +	ret = inode->i_op->set_acl(inode, acl, ACL_TYPE_ACCESS); +	posix_acl_release(acl); +	return ret; +}  EXPORT_SYMBOL(posix_acl_chmod); + +int +posix_acl_create(struct inode *dir, umode_t *mode, +		struct posix_acl **default_acl, struct posix_acl **acl) +{ +	struct posix_acl *p; +	int ret; + +	if (S_ISLNK(*mode) || !IS_POSIXACL(dir)) +		goto no_acl; + +	p = get_acl(dir, ACL_TYPE_DEFAULT); +	if (IS_ERR(p)) { +		if (p == ERR_PTR(-EOPNOTSUPP)) +			goto apply_umask; +		return PTR_ERR(p); +	} + +	if (!p) +		goto apply_umask; + +	*acl = posix_acl_clone(p, GFP_NOFS); +	if (!*acl) +		return -ENOMEM; + +	ret = posix_acl_create_masq(*acl, mode); +	if (ret < 0) { +		posix_acl_release(*acl); +		return -ENOMEM; +	} + +	if (ret == 0) { +		posix_acl_release(*acl); +		*acl = NULL; +	} + +	if (!S_ISDIR(*mode)) { +		posix_acl_release(p); +		*default_acl = NULL; +	} else { +		*default_acl = p; +	} +	return 0; + +apply_umask: +	*mode &= ~current_umask(); +no_acl: +	*default_acl = NULL; +	*acl = NULL; +	return 0; +} +EXPORT_SYMBOL_GPL(posix_acl_create); + +/* + * Fix up the uids and gids in posix acl extended attributes in place. + */ +static void posix_acl_fix_xattr_userns( +	struct user_namespace *to, struct user_namespace *from, +	void *value, size_t size) +{ +	posix_acl_xattr_header *header = (posix_acl_xattr_header *)value; +	posix_acl_xattr_entry *entry = (posix_acl_xattr_entry *)(header+1), *end; +	int count; +	kuid_t uid; +	kgid_t gid; + +	if (!value) +		return; +	if (size < sizeof(posix_acl_xattr_header)) +		return; +	if (header->a_version != cpu_to_le32(POSIX_ACL_XATTR_VERSION)) +		return; + +	count = posix_acl_xattr_count(size); +	if (count < 0) +		return; +	if (count == 0) +		return; + +	for (end = entry + count; entry != end; entry++) { +		switch(le16_to_cpu(entry->e_tag)) { +		case ACL_USER: +			uid = make_kuid(from, le32_to_cpu(entry->e_id)); +			entry->e_id = cpu_to_le32(from_kuid(to, uid)); +			break; +		case ACL_GROUP: +			gid = make_kgid(from, le32_to_cpu(entry->e_id)); +			entry->e_id = cpu_to_le32(from_kgid(to, gid)); +			break; +		default: +			break; +		} +	} +} + +void posix_acl_fix_xattr_from_user(void *value, size_t size) +{ +	struct user_namespace *user_ns = current_user_ns(); +	if (user_ns == &init_user_ns) +		return; +	posix_acl_fix_xattr_userns(&init_user_ns, user_ns, value, size); +} + +void posix_acl_fix_xattr_to_user(void *value, size_t size) +{ +	struct user_namespace *user_ns = current_user_ns(); +	if (user_ns == &init_user_ns) +		return; +	posix_acl_fix_xattr_userns(user_ns, &init_user_ns, value, size); +} + +/* + * Convert from extended attribute to in-memory representation. + */ +struct posix_acl * +posix_acl_from_xattr(struct user_namespace *user_ns, +		     const void *value, size_t size) +{ +	posix_acl_xattr_header *header = (posix_acl_xattr_header *)value; +	posix_acl_xattr_entry *entry = (posix_acl_xattr_entry *)(header+1), *end; +	int count; +	struct posix_acl *acl; +	struct posix_acl_entry *acl_e; + +	if (!value) +		return NULL; +	if (size < sizeof(posix_acl_xattr_header)) +		 return ERR_PTR(-EINVAL); +	if (header->a_version != cpu_to_le32(POSIX_ACL_XATTR_VERSION)) +		return ERR_PTR(-EOPNOTSUPP); + +	count = posix_acl_xattr_count(size); +	if (count < 0) +		return ERR_PTR(-EINVAL); +	if (count == 0) +		return NULL; +	 +	acl = posix_acl_alloc(count, GFP_NOFS); +	if (!acl) +		return ERR_PTR(-ENOMEM); +	acl_e = acl->a_entries; +	 +	for (end = entry + count; entry != end; acl_e++, entry++) { +		acl_e->e_tag  = le16_to_cpu(entry->e_tag); +		acl_e->e_perm = le16_to_cpu(entry->e_perm); + +		switch(acl_e->e_tag) { +			case ACL_USER_OBJ: +			case ACL_GROUP_OBJ: +			case ACL_MASK: +			case ACL_OTHER: +				break; + +			case ACL_USER: +				acl_e->e_uid = +					make_kuid(user_ns, +						  le32_to_cpu(entry->e_id)); +				if (!uid_valid(acl_e->e_uid)) +					goto fail; +				break; +			case ACL_GROUP: +				acl_e->e_gid = +					make_kgid(user_ns, +						  le32_to_cpu(entry->e_id)); +				if (!gid_valid(acl_e->e_gid)) +					goto fail; +				break; + +			default: +				goto fail; +		} +	} +	return acl; + +fail: +	posix_acl_release(acl); +	return ERR_PTR(-EINVAL); +} +EXPORT_SYMBOL (posix_acl_from_xattr); + +/* + * Convert from in-memory to extended attribute representation. + */ +int +posix_acl_to_xattr(struct user_namespace *user_ns, const struct posix_acl *acl, +		   void *buffer, size_t size) +{ +	posix_acl_xattr_header *ext_acl = (posix_acl_xattr_header *)buffer; +	posix_acl_xattr_entry *ext_entry; +	int real_size, n; + +	real_size = posix_acl_xattr_size(acl->a_count); +	if (!buffer) +		return real_size; +	if (real_size > size) +		return -ERANGE; + +	ext_entry = ext_acl->a_entries; +	ext_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION); + +	for (n=0; n < acl->a_count; n++, ext_entry++) { +		const struct posix_acl_entry *acl_e = &acl->a_entries[n]; +		ext_entry->e_tag  = cpu_to_le16(acl_e->e_tag); +		ext_entry->e_perm = cpu_to_le16(acl_e->e_perm); +		switch(acl_e->e_tag) { +		case ACL_USER: +			ext_entry->e_id = +				cpu_to_le32(from_kuid(user_ns, acl_e->e_uid)); +			break; +		case ACL_GROUP: +			ext_entry->e_id = +				cpu_to_le32(from_kgid(user_ns, acl_e->e_gid)); +			break; +		default: +			ext_entry->e_id = cpu_to_le32(ACL_UNDEFINED_ID); +			break; +		} +	} +	return real_size; +} +EXPORT_SYMBOL (posix_acl_to_xattr); + +static int +posix_acl_xattr_get(struct dentry *dentry, const char *name, +		void *value, size_t size, int type) +{ +	struct posix_acl *acl; +	int error; + +	if (!IS_POSIXACL(dentry->d_inode)) +		return -EOPNOTSUPP; +	if (S_ISLNK(dentry->d_inode->i_mode)) +		return -EOPNOTSUPP; + +	acl = get_acl(dentry->d_inode, type); +	if (IS_ERR(acl)) +		return PTR_ERR(acl); +	if (acl == NULL) +		return -ENODATA; + +	error = posix_acl_to_xattr(&init_user_ns, acl, value, size); +	posix_acl_release(acl); + +	return error; +} + +static int +posix_acl_xattr_set(struct dentry *dentry, const char *name, +		const void *value, size_t size, int flags, int type) +{ +	struct inode *inode = dentry->d_inode; +	struct posix_acl *acl = NULL; +	int ret; + +	if (!IS_POSIXACL(inode)) +		return -EOPNOTSUPP; +	if (!inode->i_op->set_acl) +		return -EOPNOTSUPP; + +	if (type == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode)) +		return value ? -EACCES : 0; +	if (!inode_owner_or_capable(inode)) +		return -EPERM; + +	if (value) { +		acl = posix_acl_from_xattr(&init_user_ns, value, size); +		if (IS_ERR(acl)) +			return PTR_ERR(acl); + +		if (acl) { +			ret = posix_acl_valid(acl); +			if (ret) +				goto out; +		} +	} + +	ret = inode->i_op->set_acl(inode, acl, type); +out: +	posix_acl_release(acl); +	return ret; +} + +static size_t +posix_acl_xattr_list(struct dentry *dentry, char *list, size_t list_size, +		const char *name, size_t name_len, int type) +{ +	const char *xname; +	size_t size; + +	if (!IS_POSIXACL(dentry->d_inode)) +		return -EOPNOTSUPP; +	if (S_ISLNK(dentry->d_inode->i_mode)) +		return -EOPNOTSUPP; + +	if (type == ACL_TYPE_ACCESS) +		xname = POSIX_ACL_XATTR_ACCESS; +	else +		xname = POSIX_ACL_XATTR_DEFAULT; + +	size = strlen(xname) + 1; +	if (list && size <= list_size) +		memcpy(list, xname, size); +	return size; +} + +const struct xattr_handler posix_acl_access_xattr_handler = { +	.prefix = POSIX_ACL_XATTR_ACCESS, +	.flags = ACL_TYPE_ACCESS, +	.list = posix_acl_xattr_list, +	.get = posix_acl_xattr_get, +	.set = posix_acl_xattr_set, +}; +EXPORT_SYMBOL_GPL(posix_acl_access_xattr_handler); + +const struct xattr_handler posix_acl_default_xattr_handler = { +	.prefix = POSIX_ACL_XATTR_DEFAULT, +	.flags = ACL_TYPE_DEFAULT, +	.list = posix_acl_xattr_list, +	.get = posix_acl_xattr_get, +	.set = posix_acl_xattr_set, +}; +EXPORT_SYMBOL_GPL(posix_acl_default_xattr_handler); + +int simple_set_acl(struct inode *inode, struct posix_acl *acl, int type) +{ +	int error; + +	if (type == ACL_TYPE_ACCESS) { +		error = posix_acl_equiv_mode(acl, &inode->i_mode); +		if (error < 0) +			return 0; +		if (error == 0) +			acl = NULL; +	} + +	inode->i_ctime = CURRENT_TIME; +	set_cached_acl(inode, type, acl); +	return 0; +} + +int simple_acl_create(struct inode *dir, struct inode *inode) +{ +	struct posix_acl *default_acl, *acl; +	int error; + +	error = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl); +	if (error) +		return error; + +	set_cached_acl(inode, ACL_TYPE_DEFAULT, default_acl); +	set_cached_acl(inode, ACL_TYPE_ACCESS, acl); + +	if (default_acl) +		posix_acl_release(default_acl); +	if (acl) +		posix_acl_release(acl); +	return 0; +}  | 
