2019-05-19 20:08:55 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
2013-12-20 21:16:37 +08:00
|
|
|
* Copyright (C) 2002,2003 by Andreas Gruenbacher <a.gruenbacher@computer.org>
|
2005-04-17 06:20:36 +08:00
|
|
|
*
|
2013-12-20 21:16:37 +08:00
|
|
|
* Fixes from William Schumacher incorporated on 15 March 2001.
|
|
|
|
* (Reported by Charles Bertsch, <CBertsch@microtest.com>).
|
2005-04-17 06:20:36 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file contains generic functions for manipulating
|
|
|
|
* POSIX 1003.1e draft standard 17 ACLs.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/slab.h>
|
2011-07-27 07:09:06 +08:00
|
|
|
#include <linux/atomic.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
#include <linux/fs.h>
|
|
|
|
#include <linux/sched.h>
|
2017-02-03 00:54:15 +08:00
|
|
|
#include <linux/cred.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
#include <linux/posix_acl.h>
|
2013-12-20 21:16:37 +08:00
|
|
|
#include <linux/posix_acl_xattr.h>
|
2013-12-20 21:16:40 +08:00
|
|
|
#include <linux/xattr.h>
|
2011-11-17 12:57:37 +08:00
|
|
|
#include <linux/export.h>
|
2013-12-20 21:16:37 +08:00
|
|
|
#include <linux/user_namespace.h>
|
2021-08-19 04:08:24 +08:00
|
|
|
#include <linux/namei.h>
|
2021-12-03 19:16:59 +08:00
|
|
|
#include <linux/mnt_idmapping.h>
|
2022-09-09 21:00:31 +08:00
|
|
|
#include <linux/iversion.h>
|
2022-09-22 23:17:06 +08:00
|
|
|
#include <linux/security.h>
|
|
|
|
#include <linux/evm.h>
|
|
|
|
#include <linux/fsnotify.h>
|
|
|
|
|
|
|
|
#include "internal.h"
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2016-03-24 21:38:38 +08:00
|
|
|
static struct posix_acl **acl_by_type(struct inode *inode, int type)
|
2014-01-22 07:48:42 +08:00
|
|
|
{
|
|
|
|
switch (type) {
|
|
|
|
case ACL_TYPE_ACCESS:
|
|
|
|
return &inode->i_acl;
|
|
|
|
case ACL_TYPE_DEFAULT:
|
|
|
|
return &inode->i_default_acl;
|
|
|
|
default:
|
|
|
|
BUG();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct posix_acl *get_cached_acl(struct inode *inode, int type)
|
|
|
|
{
|
|
|
|
struct posix_acl **p = acl_by_type(inode, type);
|
2016-03-24 21:38:37 +08:00
|
|
|
struct posix_acl *acl;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
rcu_read_lock();
|
|
|
|
acl = rcu_dereference(*p);
|
|
|
|
if (!acl || is_uncached_acl(acl) ||
|
2017-11-29 19:19:31 +08:00
|
|
|
refcount_inc_not_zero(&acl->a_refcount))
|
2016-03-24 21:38:37 +08:00
|
|
|
break;
|
|
|
|
rcu_read_unlock();
|
|
|
|
cpu_relax();
|
2014-01-22 07:48:42 +08:00
|
|
|
}
|
2016-03-24 21:38:37 +08:00
|
|
|
rcu_read_unlock();
|
2014-01-22 07:48:42 +08:00
|
|
|
return acl;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(get_cached_acl);
|
|
|
|
|
|
|
|
struct posix_acl *get_cached_acl_rcu(struct inode *inode, int type)
|
|
|
|
{
|
2021-08-19 04:08:24 +08:00
|
|
|
struct posix_acl *acl = rcu_dereference(*acl_by_type(inode, type));
|
|
|
|
|
|
|
|
if (acl == ACL_DONT_CACHE) {
|
|
|
|
struct posix_acl *ret;
|
|
|
|
|
2022-09-22 23:17:00 +08:00
|
|
|
ret = inode->i_op->get_inode_acl(inode, type, LOOKUP_RCU);
|
2021-08-19 04:08:24 +08:00
|
|
|
if (!IS_ERR(ret))
|
|
|
|
acl = ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return acl;
|
2014-01-22 07:48:42 +08:00
|
|
|
}
|
|
|
|
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;
|
2016-03-24 21:38:37 +08:00
|
|
|
|
|
|
|
old = xchg(p, posix_acl_dup(acl));
|
|
|
|
if (!is_uncached_acl(old))
|
2014-01-22 07:48:42 +08:00
|
|
|
posix_acl_release(old);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(set_cached_acl);
|
|
|
|
|
2016-03-24 21:38:37 +08:00
|
|
|
static void __forget_cached_acl(struct posix_acl **p)
|
2014-01-22 07:48:42 +08:00
|
|
|
{
|
|
|
|
struct posix_acl *old;
|
2016-03-24 21:38:37 +08:00
|
|
|
|
|
|
|
old = xchg(p, ACL_NOT_CACHED);
|
|
|
|
if (!is_uncached_acl(old))
|
2014-01-22 07:48:42 +08:00
|
|
|
posix_acl_release(old);
|
|
|
|
}
|
2016-03-24 21:38:37 +08:00
|
|
|
|
|
|
|
void forget_cached_acl(struct inode *inode, int type)
|
|
|
|
{
|
|
|
|
__forget_cached_acl(acl_by_type(inode, type));
|
|
|
|
}
|
2014-01-22 07:48:42 +08:00
|
|
|
EXPORT_SYMBOL(forget_cached_acl);
|
|
|
|
|
|
|
|
void forget_all_cached_acls(struct inode *inode)
|
|
|
|
{
|
2016-03-24 21:38:37 +08:00
|
|
|
__forget_cached_acl(&inode->i_acl);
|
|
|
|
__forget_cached_acl(&inode->i_default_acl);
|
2014-01-22 07:48:42 +08:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(forget_all_cached_acls);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2023-01-13 19:49:19 +08:00
|
|
|
static struct posix_acl *__get_acl(struct mnt_idmap *idmap,
|
2022-09-22 23:17:13 +08:00
|
|
|
struct dentry *dentry, struct inode *inode,
|
|
|
|
int type)
|
2013-12-20 21:16:38 +08:00
|
|
|
{
|
2022-12-02 00:01:03 +08:00
|
|
|
struct posix_acl *sentinel;
|
2016-03-24 21:38:37 +08:00
|
|
|
struct posix_acl **p;
|
2013-12-20 21:16:38 +08:00
|
|
|
struct posix_acl *acl;
|
|
|
|
|
2016-03-24 21:38:37 +08:00
|
|
|
/*
|
|
|
|
* The sentinel is used to detect when another operation like
|
2022-09-22 23:17:00 +08:00
|
|
|
* set_cached_acl() or forget_cached_acl() races with get_inode_acl().
|
2016-03-24 21:38:37 +08:00
|
|
|
* It is guaranteed that is_uncached_acl(sentinel) is true.
|
|
|
|
*/
|
|
|
|
|
2013-12-20 21:16:38 +08:00
|
|
|
acl = get_cached_acl(inode, type);
|
2016-03-24 21:38:37 +08:00
|
|
|
if (!is_uncached_acl(acl))
|
2013-12-20 21:16:38 +08:00
|
|
|
return acl;
|
|
|
|
|
|
|
|
if (!IS_POSIXACL(inode))
|
|
|
|
return NULL;
|
|
|
|
|
2016-03-24 21:38:37 +08:00
|
|
|
sentinel = uncached_acl_sentinel(current);
|
|
|
|
p = acl_by_type(inode, type);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the ACL isn't being read yet, set our sentinel. Otherwise, the
|
|
|
|
* current value of the ACL will not be ACL_NOT_CACHED and so our own
|
|
|
|
* sentinel will not be set; another task will update the cache. We
|
|
|
|
* could wait for that other task to complete its job, but it's easier
|
2022-09-22 23:17:00 +08:00
|
|
|
* to just call ->get_inode_acl to fetch the ACL ourself. (This is
|
|
|
|
* going to be an unlikely race.)
|
2016-03-24 21:38:37 +08:00
|
|
|
*/
|
2021-11-06 04:35:01 +08:00
|
|
|
cmpxchg(p, ACL_NOT_CACHED, sentinel);
|
2016-03-24 21:38:37 +08:00
|
|
|
|
2013-12-20 21:16:38 +08:00
|
|
|
/*
|
2022-09-22 23:17:13 +08:00
|
|
|
* Normally, the ACL returned by ->get{_inode}_acl will be cached.
|
2016-03-24 21:38:37 +08:00
|
|
|
* A filesystem can prevent that by calling
|
2022-09-22 23:17:13 +08:00
|
|
|
* forget_cached_acl(inode, type) in ->get{_inode}_acl.
|
2013-12-20 21:16:38 +08:00
|
|
|
*
|
2022-09-22 23:17:13 +08:00
|
|
|
* If the filesystem doesn't have a get{_inode}_ acl() function at all,
|
2022-09-22 23:17:00 +08:00
|
|
|
* we'll just create the negative cache entry.
|
2013-12-20 21:16:38 +08:00
|
|
|
*/
|
2022-09-22 23:17:13 +08:00
|
|
|
if (dentry && inode->i_op->get_acl) {
|
2023-01-13 19:49:19 +08:00
|
|
|
acl = inode->i_op->get_acl(idmap, dentry, type);
|
2022-09-22 23:17:13 +08:00
|
|
|
} else if (inode->i_op->get_inode_acl) {
|
|
|
|
acl = inode->i_op->get_inode_acl(inode, type, false);
|
|
|
|
} else {
|
2013-12-20 21:16:38 +08:00
|
|
|
set_cached_acl(inode, type, NULL);
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-03-24 21:38:37 +08:00
|
|
|
if (IS_ERR(acl)) {
|
|
|
|
/*
|
|
|
|
* Remove our sentinel so that we don't block future attempts
|
|
|
|
* to cache the ACL.
|
|
|
|
*/
|
|
|
|
cmpxchg(p, sentinel, ACL_NOT_CACHED);
|
|
|
|
return acl;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Cache the result, but only if our sentinel is still in place.
|
|
|
|
*/
|
|
|
|
posix_acl_dup(acl);
|
|
|
|
if (unlikely(cmpxchg(p, sentinel, acl) != sentinel))
|
|
|
|
posix_acl_release(acl);
|
|
|
|
return acl;
|
2013-12-20 21:16:38 +08:00
|
|
|
}
|
2022-09-22 23:17:13 +08:00
|
|
|
|
|
|
|
struct posix_acl *get_inode_acl(struct inode *inode, int type)
|
|
|
|
{
|
2023-01-13 19:49:19 +08:00
|
|
|
return __get_acl(&nop_mnt_idmap, NULL, inode, type);
|
2022-09-22 23:17:13 +08:00
|
|
|
}
|
2022-09-22 23:17:00 +08:00
|
|
|
EXPORT_SYMBOL(get_inode_acl);
|
2013-12-20 21:16:38 +08:00
|
|
|
|
2011-01-21 11:05:38 +08:00
|
|
|
/*
|
|
|
|
* Init a fresh posix_acl
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
posix_acl_init(struct posix_acl *acl, int count)
|
|
|
|
{
|
2017-11-29 19:19:31 +08:00
|
|
|
refcount_set(&acl->a_refcount, 1);
|
2011-01-21 11:05:38 +08:00
|
|
|
acl->a_count = count;
|
|
|
|
}
|
2014-01-22 07:48:42 +08:00
|
|
|
EXPORT_SYMBOL(posix_acl_init);
|
2011-01-21 11:05:38 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* Allocate a new ACL with the specified number of entries.
|
|
|
|
*/
|
|
|
|
struct posix_acl *
|
2005-10-07 14:46:04 +08:00
|
|
|
posix_acl_alloc(int count, gfp_t flags)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
const size_t size = sizeof(struct posix_acl) +
|
|
|
|
count * sizeof(struct posix_acl_entry);
|
|
|
|
struct posix_acl *acl = kmalloc(size, flags);
|
2011-01-21 11:05:38 +08:00
|
|
|
if (acl)
|
|
|
|
posix_acl_init(acl, count);
|
2005-04-17 06:20:36 +08:00
|
|
|
return acl;
|
|
|
|
}
|
2014-01-22 07:48:42 +08:00
|
|
|
EXPORT_SYMBOL(posix_acl_alloc);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Clone an ACL.
|
|
|
|
*/
|
2022-07-06 23:13:23 +08:00
|
|
|
struct posix_acl *
|
2005-10-07 14:46:04 +08:00
|
|
|
posix_acl_clone(const struct posix_acl *acl, gfp_t flags)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct posix_acl *clone = NULL;
|
|
|
|
|
|
|
|
if (acl) {
|
|
|
|
int size = sizeof(struct posix_acl) + acl->a_count *
|
|
|
|
sizeof(struct posix_acl_entry);
|
2006-10-01 14:27:21 +08:00
|
|
|
clone = kmemdup(acl, size, flags);
|
|
|
|
if (clone)
|
2017-11-29 19:19:31 +08:00
|
|
|
refcount_set(&clone->a_refcount, 1);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
return clone;
|
|
|
|
}
|
2022-07-06 23:13:23 +08:00
|
|
|
EXPORT_SYMBOL_GPL(posix_acl_clone);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Check if an acl is valid. Returns 0 if it is, or -E... otherwise.
|
|
|
|
*/
|
|
|
|
int
|
2016-06-28 05:04:06 +08:00
|
|
|
posix_acl_valid(struct user_namespace *user_ns, const struct posix_acl *acl)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
const struct posix_acl_entry *pa, *pe;
|
|
|
|
int state = ACL_USER_OBJ;
|
|
|
|
int needs_mask = 0;
|
|
|
|
|
|
|
|
FOREACH_ACL_ENTRY(pa, acl, pe) {
|
|
|
|
if (pa->e_perm & ~(ACL_READ|ACL_WRITE|ACL_EXECUTE))
|
|
|
|
return -EINVAL;
|
|
|
|
switch (pa->e_tag) {
|
|
|
|
case ACL_USER_OBJ:
|
|
|
|
if (state == ACL_USER_OBJ) {
|
|
|
|
state = ACL_USER;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
case ACL_USER:
|
|
|
|
if (state != ACL_USER)
|
|
|
|
return -EINVAL;
|
2016-06-28 05:04:06 +08:00
|
|
|
if (!kuid_has_mapping(user_ns, pa->e_uid))
|
2005-04-17 06:20:36 +08:00
|
|
|
return -EINVAL;
|
|
|
|
needs_mask = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ACL_GROUP_OBJ:
|
|
|
|
if (state == ACL_USER) {
|
|
|
|
state = ACL_GROUP;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
case ACL_GROUP:
|
|
|
|
if (state != ACL_GROUP)
|
|
|
|
return -EINVAL;
|
2016-06-28 05:04:06 +08:00
|
|
|
if (!kgid_has_mapping(user_ns, pa->e_gid))
|
2012-02-08 10:52:57 +08:00
|
|
|
return -EINVAL;
|
2005-04-17 06:20:36 +08:00
|
|
|
needs_mask = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ACL_MASK:
|
|
|
|
if (state != ACL_GROUP)
|
|
|
|
return -EINVAL;
|
|
|
|
state = ACL_OTHER;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ACL_OTHER:
|
|
|
|
if (state == ACL_OTHER ||
|
|
|
|
(state == ACL_GROUP && !needs_mask)) {
|
|
|
|
state = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (state == 0)
|
|
|
|
return 0;
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2014-01-22 07:48:42 +08:00
|
|
|
EXPORT_SYMBOL(posix_acl_valid);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns 0 if the acl can be exactly represented in the traditional
|
|
|
|
* file mode permission bits, or else 1. Returns -E... on error.
|
|
|
|
*/
|
|
|
|
int
|
2011-07-24 06:56:36 +08:00
|
|
|
posix_acl_equiv_mode(const struct posix_acl *acl, umode_t *mode_p)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
const struct posix_acl_entry *pa, *pe;
|
2011-07-24 06:56:36 +08:00
|
|
|
umode_t mode = 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
int not_equiv = 0;
|
|
|
|
|
2014-05-04 19:03:32 +08:00
|
|
|
/*
|
|
|
|
* A null ACL can always be presented as mode bits.
|
|
|
|
*/
|
|
|
|
if (!acl)
|
|
|
|
return 0;
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
FOREACH_ACL_ENTRY(pa, acl, pe) {
|
|
|
|
switch (pa->e_tag) {
|
|
|
|
case ACL_USER_OBJ:
|
|
|
|
mode |= (pa->e_perm & S_IRWXO) << 6;
|
|
|
|
break;
|
|
|
|
case ACL_GROUP_OBJ:
|
|
|
|
mode |= (pa->e_perm & S_IRWXO) << 3;
|
|
|
|
break;
|
|
|
|
case ACL_OTHER:
|
|
|
|
mode |= pa->e_perm & S_IRWXO;
|
|
|
|
break;
|
|
|
|
case ACL_MASK:
|
|
|
|
mode = (mode & ~S_IRWXG) |
|
|
|
|
((pa->e_perm & S_IRWXO) << 3);
|
|
|
|
not_equiv = 1;
|
|
|
|
break;
|
|
|
|
case ACL_USER:
|
|
|
|
case ACL_GROUP:
|
|
|
|
not_equiv = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (mode_p)
|
|
|
|
*mode_p = (*mode_p & ~S_IRWXUGO) | mode;
|
|
|
|
return not_equiv;
|
|
|
|
}
|
2014-01-22 07:48:42 +08:00
|
|
|
EXPORT_SYMBOL(posix_acl_equiv_mode);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Create an ACL representing the file mode permission bits of an inode.
|
|
|
|
*/
|
|
|
|
struct posix_acl *
|
2011-07-24 07:01:48 +08:00
|
|
|
posix_acl_from_mode(umode_t mode, gfp_t flags)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct posix_acl *acl = posix_acl_alloc(3, flags);
|
|
|
|
if (!acl)
|
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
|
|
|
|
acl->a_entries[0].e_tag = ACL_USER_OBJ;
|
|
|
|
acl->a_entries[0].e_perm = (mode & S_IRWXU) >> 6;
|
|
|
|
|
|
|
|
acl->a_entries[1].e_tag = ACL_GROUP_OBJ;
|
|
|
|
acl->a_entries[1].e_perm = (mode & S_IRWXG) >> 3;
|
|
|
|
|
|
|
|
acl->a_entries[2].e_tag = ACL_OTHER;
|
|
|
|
acl->a_entries[2].e_perm = (mode & S_IRWXO);
|
|
|
|
return acl;
|
|
|
|
}
|
2014-01-22 07:48:42 +08:00
|
|
|
EXPORT_SYMBOL(posix_acl_from_mode);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Return 0 if current is granted want access to the inode
|
|
|
|
* by the acl. Returns -E... otherwise.
|
|
|
|
*/
|
|
|
|
int
|
2023-01-13 19:49:24 +08:00
|
|
|
posix_acl_permission(struct mnt_idmap *idmap, struct inode *inode,
|
2021-01-21 21:19:24 +08:00
|
|
|
const struct posix_acl *acl, int want)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
const struct posix_acl_entry *pa, *pe, *mask_obj;
|
acl: handle idmapped mounts for idmapped filesystems
Ensure that POSIX ACLs checking, getting, and setting works correctly
for filesystems mountable with a filesystem idmapping ("fs_idmapping")
that want to support idmapped mounts ("mnt_idmapping").
Note that no filesystems mountable with an fs_idmapping do yet support
idmapped mounts. This is required infrastructure work to unblock this.
As we explained in detail in [1] the fs_idmapping is irrelevant for
getxattr() and setxattr() when mapping the ACL_{GROUP,USER} {g,u}ids
stored in the uapi struct posix_acl_xattr_entry in
posix_acl_fix_xattr_{from,to}_user().
But for acl_permission_check() and posix_acl_{g,s}etxattr_idmapped_mnt()
the fs_idmapping matters.
acl_permission_check():
During lookup POSIX ACLs are retrieved directly via i_op->get_acl() and
are returned via the kernel internal struct posix_acl which contains
e_{g,u}id members of type k{g,u}id_t that already take the
fs_idmapping into acccount.
For example, a POSIX ACL stored with u4 on the backing store is mapped
to k10000004 in the fs_idmapping. The mnt_idmapping remaps the POSIX ACL
to k20000004. In order to do that the fs_idmapping needs to be taken
into account but that doesn't happen yet (Again, this is a
counterfactual currently as fuse doesn't support idmapped mounts
currently. It's just used as a convenient example.):
fs_idmapping: u0:k10000000:r65536
mnt_idmapping: u0:v20000000:r65536
ACL_USER: k10000004
acl_permission_check()
-> check_acl()
-> get_acl()
-> i_op->get_acl() == fuse_get_acl()
-> posix_acl_from_xattr(u0:k10000000:r65536 /* fs_idmapping */, ...)
{
k10000004 = make_kuid(u0:k10000000:r65536 /* fs_idmapping */,
u4 /* ACL_USER */);
}
-> posix_acl_permission()
{
-1 = make_vfsuid(u0:v20000000:r65536 /* mnt_idmapping */,
&init_user_ns,
k10000004);
vfsuid_eq_kuid(-1, k10000004 /* caller_fsuid */)
}
In order to correctly map from the fs_idmapping into mnt_idmapping we
require the relevant fs_idmaping to be passed:
acl_permission_check()
-> check_acl()
-> get_acl()
-> i_op->get_acl() == fuse_get_acl()
-> posix_acl_from_xattr(u0:k10000000:r65536 /* fs_idmapping */, ...)
{
k10000004 = make_kuid(u0:k10000000:r65536 /* fs_idmapping */,
u4 /* ACL_USER */);
}
-> posix_acl_permission()
{
v20000004 = make_vfsuid(u0:v20000000:r65536 /* mnt_idmapping */,
u0:k10000000:r65536 /* fs_idmapping */,
k10000004);
vfsuid_eq_kuid(v20000004, k10000004 /* caller_fsuid */)
}
The initial_idmapping is only correct for the current situation because
all filesystems that currently support idmapped mounts do not support
being mounted with an fs_idmapping.
Note that ovl_get_acl() is used to retrieve the POSIX ACLs from the
relevant lower layer and the lower layer's mnt_idmapping needs to be
taken into account and so does the fs_idmapping. See 0c5fd887d2bb ("acl:
move idmapped mount fixup into vfs_{g,s}etxattr()") for more details.
For posix_acl_{g,s}etxattr_idmapped_mnt() it is not as obvious why the
fs_idmapping matters as it is for acl_permission_check(). Especially
because it doesn't matter for posix_acl_fix_xattr_{from,to}_user() (See
[1] for more context.).
Because posix_acl_{g,s}etxattr_idmapped_mnt() operate on the uapi
struct posix_acl_xattr_entry which contains {g,u}id_t values and thus
give the impression that the fs_idmapping is irrelevant as at this point
appropriate {g,u}id_t values have seemlingly been generated.
As we've stated multiple times this assumption is wrong and in fact the
uapi struct posix_acl_xattr_entry is taking idmappings into account
depending at what place it is operated on.
posix_acl_getxattr_idmapped_mnt()
When posix_acl_getxattr_idmapped_mnt() is called the values stored in
the uapi struct posix_acl_xattr_entry are mapped according to the
fs_idmapping. This happened when they were read from the backing store
and then translated from struct posix_acl into the uapi
struct posix_acl_xattr_entry during posix_acl_to_xattr().
In other words, the fs_idmapping matters as the values stored as
{g,u}id_t in the uapi struct posix_acl_xattr_entry have been generated
by it.
So we need to take the fs_idmapping into account during make_vfsuid()
in posix_acl_getxattr_idmapped_mnt().
posix_acl_setxattr_idmapped_mnt()
When posix_acl_setxattr_idmapped_mnt() is called the values stored as
{g,u}id_t in uapi struct posix_acl_xattr_entry are intended to be the
values that ultimately get turned back into a k{g,u}id_t in
posix_acl_from_xattr() (which turns the uapi
struct posix_acl_xattr_entry into the kernel internal struct posix_acl).
In other words, the fs_idmapping matters as the values stored as
{g,u}id_t in the uapi struct posix_acl_xattr_entry are intended to be
the values that will be undone in the fs_idmapping when writing to the
backing store.
So we need to take the fs_idmapping into account during from_vfsuid()
in posix_acl_setxattr_idmapped_mnt().
Link: https://lore.kernel.org/all/20220801145520.1532837-1-brauner@kernel.org [1]
Fixes: 0c5fd887d2bb ("acl: move idmapped mount fixup into vfs_{g,s}etxattr()")
Cc: Seth Forshee <sforshee@digitalocean.com>
Signed-off-by: Christian Brauner (Microsoft) <brauner@kernel.org>
Reviewed-by: Seth Forshee <sforshee@digitalocean.com>
Link: https://lore.kernel.org/r/20220816113514.43304-1-brauner@kernel.org
2022-08-16 19:35:13 +08:00
|
|
|
struct user_namespace *fs_userns = i_user_ns(inode);
|
2023-01-13 19:49:24 +08:00
|
|
|
struct user_namespace *mnt_userns = mnt_idmap_owner(idmap);
|
2005-04-17 06:20:36 +08:00
|
|
|
int found = 0;
|
2022-07-09 18:23:20 +08:00
|
|
|
vfsuid_t vfsuid;
|
|
|
|
vfsgid_t vfsgid;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2020-06-08 03:19:06 +08:00
|
|
|
want &= MAY_READ | MAY_WRITE | MAY_EXEC;
|
2011-10-24 01:43:32 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
FOREACH_ACL_ENTRY(pa, acl, pe) {
|
|
|
|
switch(pa->e_tag) {
|
|
|
|
case ACL_USER_OBJ:
|
|
|
|
/* (May have been checked already) */
|
2022-07-09 18:23:20 +08:00
|
|
|
vfsuid = i_uid_into_vfsuid(mnt_userns, inode);
|
|
|
|
if (vfsuid_eq_kuid(vfsuid, current_fsuid()))
|
2005-04-17 06:20:36 +08:00
|
|
|
goto check_perm;
|
|
|
|
break;
|
|
|
|
case ACL_USER:
|
acl: handle idmapped mounts for idmapped filesystems
Ensure that POSIX ACLs checking, getting, and setting works correctly
for filesystems mountable with a filesystem idmapping ("fs_idmapping")
that want to support idmapped mounts ("mnt_idmapping").
Note that no filesystems mountable with an fs_idmapping do yet support
idmapped mounts. This is required infrastructure work to unblock this.
As we explained in detail in [1] the fs_idmapping is irrelevant for
getxattr() and setxattr() when mapping the ACL_{GROUP,USER} {g,u}ids
stored in the uapi struct posix_acl_xattr_entry in
posix_acl_fix_xattr_{from,to}_user().
But for acl_permission_check() and posix_acl_{g,s}etxattr_idmapped_mnt()
the fs_idmapping matters.
acl_permission_check():
During lookup POSIX ACLs are retrieved directly via i_op->get_acl() and
are returned via the kernel internal struct posix_acl which contains
e_{g,u}id members of type k{g,u}id_t that already take the
fs_idmapping into acccount.
For example, a POSIX ACL stored with u4 on the backing store is mapped
to k10000004 in the fs_idmapping. The mnt_idmapping remaps the POSIX ACL
to k20000004. In order to do that the fs_idmapping needs to be taken
into account but that doesn't happen yet (Again, this is a
counterfactual currently as fuse doesn't support idmapped mounts
currently. It's just used as a convenient example.):
fs_idmapping: u0:k10000000:r65536
mnt_idmapping: u0:v20000000:r65536
ACL_USER: k10000004
acl_permission_check()
-> check_acl()
-> get_acl()
-> i_op->get_acl() == fuse_get_acl()
-> posix_acl_from_xattr(u0:k10000000:r65536 /* fs_idmapping */, ...)
{
k10000004 = make_kuid(u0:k10000000:r65536 /* fs_idmapping */,
u4 /* ACL_USER */);
}
-> posix_acl_permission()
{
-1 = make_vfsuid(u0:v20000000:r65536 /* mnt_idmapping */,
&init_user_ns,
k10000004);
vfsuid_eq_kuid(-1, k10000004 /* caller_fsuid */)
}
In order to correctly map from the fs_idmapping into mnt_idmapping we
require the relevant fs_idmaping to be passed:
acl_permission_check()
-> check_acl()
-> get_acl()
-> i_op->get_acl() == fuse_get_acl()
-> posix_acl_from_xattr(u0:k10000000:r65536 /* fs_idmapping */, ...)
{
k10000004 = make_kuid(u0:k10000000:r65536 /* fs_idmapping */,
u4 /* ACL_USER */);
}
-> posix_acl_permission()
{
v20000004 = make_vfsuid(u0:v20000000:r65536 /* mnt_idmapping */,
u0:k10000000:r65536 /* fs_idmapping */,
k10000004);
vfsuid_eq_kuid(v20000004, k10000004 /* caller_fsuid */)
}
The initial_idmapping is only correct for the current situation because
all filesystems that currently support idmapped mounts do not support
being mounted with an fs_idmapping.
Note that ovl_get_acl() is used to retrieve the POSIX ACLs from the
relevant lower layer and the lower layer's mnt_idmapping needs to be
taken into account and so does the fs_idmapping. See 0c5fd887d2bb ("acl:
move idmapped mount fixup into vfs_{g,s}etxattr()") for more details.
For posix_acl_{g,s}etxattr_idmapped_mnt() it is not as obvious why the
fs_idmapping matters as it is for acl_permission_check(). Especially
because it doesn't matter for posix_acl_fix_xattr_{from,to}_user() (See
[1] for more context.).
Because posix_acl_{g,s}etxattr_idmapped_mnt() operate on the uapi
struct posix_acl_xattr_entry which contains {g,u}id_t values and thus
give the impression that the fs_idmapping is irrelevant as at this point
appropriate {g,u}id_t values have seemlingly been generated.
As we've stated multiple times this assumption is wrong and in fact the
uapi struct posix_acl_xattr_entry is taking idmappings into account
depending at what place it is operated on.
posix_acl_getxattr_idmapped_mnt()
When posix_acl_getxattr_idmapped_mnt() is called the values stored in
the uapi struct posix_acl_xattr_entry are mapped according to the
fs_idmapping. This happened when they were read from the backing store
and then translated from struct posix_acl into the uapi
struct posix_acl_xattr_entry during posix_acl_to_xattr().
In other words, the fs_idmapping matters as the values stored as
{g,u}id_t in the uapi struct posix_acl_xattr_entry have been generated
by it.
So we need to take the fs_idmapping into account during make_vfsuid()
in posix_acl_getxattr_idmapped_mnt().
posix_acl_setxattr_idmapped_mnt()
When posix_acl_setxattr_idmapped_mnt() is called the values stored as
{g,u}id_t in uapi struct posix_acl_xattr_entry are intended to be the
values that ultimately get turned back into a k{g,u}id_t in
posix_acl_from_xattr() (which turns the uapi
struct posix_acl_xattr_entry into the kernel internal struct posix_acl).
In other words, the fs_idmapping matters as the values stored as
{g,u}id_t in the uapi struct posix_acl_xattr_entry are intended to be
the values that will be undone in the fs_idmapping when writing to the
backing store.
So we need to take the fs_idmapping into account during from_vfsuid()
in posix_acl_setxattr_idmapped_mnt().
Link: https://lore.kernel.org/all/20220801145520.1532837-1-brauner@kernel.org [1]
Fixes: 0c5fd887d2bb ("acl: move idmapped mount fixup into vfs_{g,s}etxattr()")
Cc: Seth Forshee <sforshee@digitalocean.com>
Signed-off-by: Christian Brauner (Microsoft) <brauner@kernel.org>
Reviewed-by: Seth Forshee <sforshee@digitalocean.com>
Link: https://lore.kernel.org/r/20220816113514.43304-1-brauner@kernel.org
2022-08-16 19:35:13 +08:00
|
|
|
vfsuid = make_vfsuid(mnt_userns, fs_userns,
|
2021-12-03 19:17:07 +08:00
|
|
|
pa->e_uid);
|
2022-07-09 18:23:20 +08:00
|
|
|
if (vfsuid_eq_kuid(vfsuid, current_fsuid()))
|
2005-04-17 06:20:36 +08:00
|
|
|
goto mask;
|
|
|
|
break;
|
|
|
|
case ACL_GROUP_OBJ:
|
2022-07-09 18:23:20 +08:00
|
|
|
vfsgid = i_gid_into_vfsgid(mnt_userns, inode);
|
|
|
|
if (vfsgid_in_group_p(vfsgid)) {
|
2005-04-17 06:20:36 +08:00
|
|
|
found = 1;
|
|
|
|
if ((pa->e_perm & want) == want)
|
|
|
|
goto mask;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ACL_GROUP:
|
acl: handle idmapped mounts for idmapped filesystems
Ensure that POSIX ACLs checking, getting, and setting works correctly
for filesystems mountable with a filesystem idmapping ("fs_idmapping")
that want to support idmapped mounts ("mnt_idmapping").
Note that no filesystems mountable with an fs_idmapping do yet support
idmapped mounts. This is required infrastructure work to unblock this.
As we explained in detail in [1] the fs_idmapping is irrelevant for
getxattr() and setxattr() when mapping the ACL_{GROUP,USER} {g,u}ids
stored in the uapi struct posix_acl_xattr_entry in
posix_acl_fix_xattr_{from,to}_user().
But for acl_permission_check() and posix_acl_{g,s}etxattr_idmapped_mnt()
the fs_idmapping matters.
acl_permission_check():
During lookup POSIX ACLs are retrieved directly via i_op->get_acl() and
are returned via the kernel internal struct posix_acl which contains
e_{g,u}id members of type k{g,u}id_t that already take the
fs_idmapping into acccount.
For example, a POSIX ACL stored with u4 on the backing store is mapped
to k10000004 in the fs_idmapping. The mnt_idmapping remaps the POSIX ACL
to k20000004. In order to do that the fs_idmapping needs to be taken
into account but that doesn't happen yet (Again, this is a
counterfactual currently as fuse doesn't support idmapped mounts
currently. It's just used as a convenient example.):
fs_idmapping: u0:k10000000:r65536
mnt_idmapping: u0:v20000000:r65536
ACL_USER: k10000004
acl_permission_check()
-> check_acl()
-> get_acl()
-> i_op->get_acl() == fuse_get_acl()
-> posix_acl_from_xattr(u0:k10000000:r65536 /* fs_idmapping */, ...)
{
k10000004 = make_kuid(u0:k10000000:r65536 /* fs_idmapping */,
u4 /* ACL_USER */);
}
-> posix_acl_permission()
{
-1 = make_vfsuid(u0:v20000000:r65536 /* mnt_idmapping */,
&init_user_ns,
k10000004);
vfsuid_eq_kuid(-1, k10000004 /* caller_fsuid */)
}
In order to correctly map from the fs_idmapping into mnt_idmapping we
require the relevant fs_idmaping to be passed:
acl_permission_check()
-> check_acl()
-> get_acl()
-> i_op->get_acl() == fuse_get_acl()
-> posix_acl_from_xattr(u0:k10000000:r65536 /* fs_idmapping */, ...)
{
k10000004 = make_kuid(u0:k10000000:r65536 /* fs_idmapping */,
u4 /* ACL_USER */);
}
-> posix_acl_permission()
{
v20000004 = make_vfsuid(u0:v20000000:r65536 /* mnt_idmapping */,
u0:k10000000:r65536 /* fs_idmapping */,
k10000004);
vfsuid_eq_kuid(v20000004, k10000004 /* caller_fsuid */)
}
The initial_idmapping is only correct for the current situation because
all filesystems that currently support idmapped mounts do not support
being mounted with an fs_idmapping.
Note that ovl_get_acl() is used to retrieve the POSIX ACLs from the
relevant lower layer and the lower layer's mnt_idmapping needs to be
taken into account and so does the fs_idmapping. See 0c5fd887d2bb ("acl:
move idmapped mount fixup into vfs_{g,s}etxattr()") for more details.
For posix_acl_{g,s}etxattr_idmapped_mnt() it is not as obvious why the
fs_idmapping matters as it is for acl_permission_check(). Especially
because it doesn't matter for posix_acl_fix_xattr_{from,to}_user() (See
[1] for more context.).
Because posix_acl_{g,s}etxattr_idmapped_mnt() operate on the uapi
struct posix_acl_xattr_entry which contains {g,u}id_t values and thus
give the impression that the fs_idmapping is irrelevant as at this point
appropriate {g,u}id_t values have seemlingly been generated.
As we've stated multiple times this assumption is wrong and in fact the
uapi struct posix_acl_xattr_entry is taking idmappings into account
depending at what place it is operated on.
posix_acl_getxattr_idmapped_mnt()
When posix_acl_getxattr_idmapped_mnt() is called the values stored in
the uapi struct posix_acl_xattr_entry are mapped according to the
fs_idmapping. This happened when they were read from the backing store
and then translated from struct posix_acl into the uapi
struct posix_acl_xattr_entry during posix_acl_to_xattr().
In other words, the fs_idmapping matters as the values stored as
{g,u}id_t in the uapi struct posix_acl_xattr_entry have been generated
by it.
So we need to take the fs_idmapping into account during make_vfsuid()
in posix_acl_getxattr_idmapped_mnt().
posix_acl_setxattr_idmapped_mnt()
When posix_acl_setxattr_idmapped_mnt() is called the values stored as
{g,u}id_t in uapi struct posix_acl_xattr_entry are intended to be the
values that ultimately get turned back into a k{g,u}id_t in
posix_acl_from_xattr() (which turns the uapi
struct posix_acl_xattr_entry into the kernel internal struct posix_acl).
In other words, the fs_idmapping matters as the values stored as
{g,u}id_t in the uapi struct posix_acl_xattr_entry are intended to be
the values that will be undone in the fs_idmapping when writing to the
backing store.
So we need to take the fs_idmapping into account during from_vfsuid()
in posix_acl_setxattr_idmapped_mnt().
Link: https://lore.kernel.org/all/20220801145520.1532837-1-brauner@kernel.org [1]
Fixes: 0c5fd887d2bb ("acl: move idmapped mount fixup into vfs_{g,s}etxattr()")
Cc: Seth Forshee <sforshee@digitalocean.com>
Signed-off-by: Christian Brauner (Microsoft) <brauner@kernel.org>
Reviewed-by: Seth Forshee <sforshee@digitalocean.com>
Link: https://lore.kernel.org/r/20220816113514.43304-1-brauner@kernel.org
2022-08-16 19:35:13 +08:00
|
|
|
vfsgid = make_vfsgid(mnt_userns, fs_userns,
|
2021-12-03 19:17:07 +08:00
|
|
|
pa->e_gid);
|
2022-07-09 18:23:20 +08:00
|
|
|
if (vfsgid_in_group_p(vfsgid)) {
|
2005-04-17 06:20:36 +08:00
|
|
|
found = 1;
|
|
|
|
if ((pa->e_perm & want) == want)
|
|
|
|
goto mask;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ACL_MASK:
|
|
|
|
break;
|
|
|
|
case ACL_OTHER:
|
|
|
|
if (found)
|
|
|
|
return -EACCES;
|
|
|
|
else
|
|
|
|
goto check_perm;
|
|
|
|
default:
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -EIO;
|
|
|
|
|
|
|
|
mask:
|
|
|
|
for (mask_obj = pa+1; mask_obj != pe; mask_obj++) {
|
|
|
|
if (mask_obj->e_tag == ACL_MASK) {
|
|
|
|
if ((pa->e_perm & mask_obj->e_perm & want) == want)
|
|
|
|
return 0;
|
|
|
|
return -EACCES;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
check_perm:
|
|
|
|
if ((pa->e_perm & want) == want)
|
|
|
|
return 0;
|
|
|
|
return -EACCES;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Modify acl when creating a new inode. The caller must ensure the acl is
|
|
|
|
* only referenced once.
|
|
|
|
*
|
|
|
|
* mode_p initially must contain the mode parameter to the open() / creat()
|
|
|
|
* system calls. All permissions that are not granted by the acl are removed.
|
|
|
|
* The permissions in the acl are changed to reflect the mode_p parameter.
|
|
|
|
*/
|
2011-07-24 06:37:50 +08:00
|
|
|
static int posix_acl_create_masq(struct posix_acl *acl, umode_t *mode_p)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct posix_acl_entry *pa, *pe;
|
|
|
|
struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL;
|
2011-07-24 06:37:50 +08:00
|
|
|
umode_t mode = *mode_p;
|
2005-04-17 06:20:36 +08:00
|
|
|
int not_equiv = 0;
|
|
|
|
|
|
|
|
/* assert(atomic_read(acl->a_refcount) == 1); */
|
|
|
|
|
|
|
|
FOREACH_ACL_ENTRY(pa, acl, pe) {
|
|
|
|
switch(pa->e_tag) {
|
|
|
|
case ACL_USER_OBJ:
|
|
|
|
pa->e_perm &= (mode >> 6) | ~S_IRWXO;
|
|
|
|
mode &= (pa->e_perm << 6) | ~S_IRWXU;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ACL_USER:
|
|
|
|
case ACL_GROUP:
|
|
|
|
not_equiv = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ACL_GROUP_OBJ:
|
|
|
|
group_obj = pa;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ACL_OTHER:
|
|
|
|
pa->e_perm &= mode | ~S_IRWXO;
|
|
|
|
mode &= pa->e_perm | ~S_IRWXO;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ACL_MASK:
|
|
|
|
mask_obj = pa;
|
|
|
|
not_equiv = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mask_obj) {
|
|
|
|
mask_obj->e_perm &= (mode >> 3) | ~S_IRWXO;
|
|
|
|
mode &= (mask_obj->e_perm << 3) | ~S_IRWXG;
|
|
|
|
} else {
|
|
|
|
if (!group_obj)
|
|
|
|
return -EIO;
|
|
|
|
group_obj->e_perm &= (mode >> 3) | ~S_IRWXO;
|
|
|
|
mode &= (group_obj->e_perm << 3) | ~S_IRWXG;
|
|
|
|
}
|
|
|
|
|
|
|
|
*mode_p = (*mode_p & ~S_IRWXUGO) | mode;
|
|
|
|
return not_equiv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Modify the ACL for the chmod syscall.
|
|
|
|
*/
|
2013-12-20 21:16:41 +08:00
|
|
|
static int __posix_acl_chmod_masq(struct posix_acl *acl, umode_t mode)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL;
|
|
|
|
struct posix_acl_entry *pa, *pe;
|
|
|
|
|
|
|
|
/* assert(atomic_read(acl->a_refcount) == 1); */
|
|
|
|
|
|
|
|
FOREACH_ACL_ENTRY(pa, acl, pe) {
|
|
|
|
switch(pa->e_tag) {
|
|
|
|
case ACL_USER_OBJ:
|
|
|
|
pa->e_perm = (mode & S_IRWXU) >> 6;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ACL_USER:
|
|
|
|
case ACL_GROUP:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ACL_GROUP_OBJ:
|
|
|
|
group_obj = pa;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ACL_MASK:
|
|
|
|
mask_obj = pa;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ACL_OTHER:
|
|
|
|
pa->e_perm = (mode & S_IRWXO);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mask_obj) {
|
|
|
|
mask_obj->e_perm = (mode & S_IRWXG) >> 3;
|
|
|
|
} else {
|
|
|
|
if (!group_obj)
|
|
|
|
return -EIO;
|
|
|
|
group_obj->e_perm = (mode & S_IRWXG) >> 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2011-07-23 12:18:02 +08:00
|
|
|
|
2011-07-23 15:10:32 +08:00
|
|
|
int
|
2013-12-20 21:16:42 +08:00
|
|
|
__posix_acl_create(struct posix_acl **acl, gfp_t gfp, umode_t *mode_p)
|
2011-07-23 15:10:32 +08:00
|
|
|
{
|
|
|
|
struct posix_acl *clone = posix_acl_clone(*acl, gfp);
|
|
|
|
int err = -ENOMEM;
|
|
|
|
if (clone) {
|
|
|
|
err = posix_acl_create_masq(clone, mode_p);
|
|
|
|
if (err < 0) {
|
|
|
|
posix_acl_release(clone);
|
|
|
|
clone = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
posix_acl_release(*acl);
|
|
|
|
*acl = clone;
|
|
|
|
return err;
|
|
|
|
}
|
2013-12-20 21:16:42 +08:00
|
|
|
EXPORT_SYMBOL(__posix_acl_create);
|
2011-07-23 15:10:32 +08:00
|
|
|
|
2011-07-23 12:18:02 +08:00
|
|
|
int
|
2013-12-20 21:16:41 +08:00
|
|
|
__posix_acl_chmod(struct posix_acl **acl, gfp_t gfp, umode_t mode)
|
2011-07-23 12:18:02 +08:00
|
|
|
{
|
|
|
|
struct posix_acl *clone = posix_acl_clone(*acl, gfp);
|
|
|
|
int err = -ENOMEM;
|
|
|
|
if (clone) {
|
2013-12-20 21:16:41 +08:00
|
|
|
err = __posix_acl_chmod_masq(clone, mode);
|
2011-07-23 12:18:02 +08:00
|
|
|
if (err) {
|
|
|
|
posix_acl_release(clone);
|
|
|
|
clone = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
posix_acl_release(*acl);
|
|
|
|
*acl = clone;
|
|
|
|
return err;
|
|
|
|
}
|
2013-12-20 21:16:41 +08:00
|
|
|
EXPORT_SYMBOL(__posix_acl_chmod);
|
|
|
|
|
2021-01-21 21:19:27 +08:00
|
|
|
/**
|
|
|
|
* posix_acl_chmod - chmod a posix acl
|
|
|
|
*
|
2023-01-13 19:49:20 +08:00
|
|
|
* @idmap: idmap of the mount @inode was found from
|
2022-09-23 16:29:39 +08:00
|
|
|
* @dentry: dentry to check permissions on
|
2021-01-21 21:19:27 +08:00
|
|
|
* @mode: the new mode of @inode
|
|
|
|
*
|
2023-01-13 19:49:20 +08:00
|
|
|
* If the dentry has been found through an idmapped mount the idmap of
|
|
|
|
* the vfsmount must be passed through @idmap. This function will then
|
|
|
|
* take care to map the inode according to @idmap before checking
|
2021-01-21 21:19:27 +08:00
|
|
|
* permissions. On non-idmapped mounts or if permission checking is to be
|
2023-01-13 19:49:20 +08:00
|
|
|
* performed on the raw inode simply passs @nop_mnt_idmap.
|
2021-01-21 21:19:27 +08:00
|
|
|
*/
|
2013-12-20 21:16:41 +08:00
|
|
|
int
|
2023-01-13 19:49:20 +08:00
|
|
|
posix_acl_chmod(struct mnt_idmap *idmap, struct dentry *dentry,
|
2021-01-21 21:19:27 +08:00
|
|
|
umode_t mode)
|
2013-12-20 21:16:41 +08:00
|
|
|
{
|
2022-09-23 16:29:39 +08:00
|
|
|
struct inode *inode = d_inode(dentry);
|
2013-12-20 21:16:41 +08:00
|
|
|
struct posix_acl *acl;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (!IS_POSIXACL(inode))
|
|
|
|
return 0;
|
|
|
|
if (!inode->i_op->set_acl)
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
|
2022-09-22 23:17:00 +08:00
|
|
|
acl = get_inode_acl(inode, ACL_TYPE_ACCESS);
|
2014-02-01 03:25:19 +08:00
|
|
|
if (IS_ERR_OR_NULL(acl)) {
|
|
|
|
if (acl == ERR_PTR(-EOPNOTSUPP))
|
|
|
|
return 0;
|
2013-12-20 21:16:41 +08:00
|
|
|
return PTR_ERR(acl);
|
2014-02-01 03:25:19 +08:00
|
|
|
}
|
2013-12-20 21:16:41 +08:00
|
|
|
|
2013-12-20 21:16:42 +08:00
|
|
|
ret = __posix_acl_chmod(&acl, GFP_KERNEL, mode);
|
2013-12-20 21:16:41 +08:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
2023-01-13 19:49:20 +08:00
|
|
|
ret = inode->i_op->set_acl(idmap, dentry, acl, ACL_TYPE_ACCESS);
|
2013-12-20 21:16:41 +08:00
|
|
|
posix_acl_release(acl);
|
|
|
|
return ret;
|
|
|
|
}
|
2011-07-23 12:18:02 +08:00
|
|
|
EXPORT_SYMBOL(posix_acl_chmod);
|
2013-12-20 21:16:37 +08:00
|
|
|
|
2013-12-20 21:16:42 +08:00
|
|
|
int
|
|
|
|
posix_acl_create(struct inode *dir, umode_t *mode,
|
|
|
|
struct posix_acl **default_acl, struct posix_acl **acl)
|
|
|
|
{
|
|
|
|
struct posix_acl *p;
|
2015-06-19 07:00:55 +08:00
|
|
|
struct posix_acl *clone;
|
2013-12-20 21:16:42 +08:00
|
|
|
int ret;
|
|
|
|
|
2015-06-19 07:00:55 +08:00
|
|
|
*acl = NULL;
|
|
|
|
*default_acl = NULL;
|
|
|
|
|
2013-12-20 21:16:42 +08:00
|
|
|
if (S_ISLNK(*mode) || !IS_POSIXACL(dir))
|
2015-06-19 07:00:55 +08:00
|
|
|
return 0;
|
2013-12-20 21:16:42 +08:00
|
|
|
|
2022-09-22 23:17:00 +08:00
|
|
|
p = get_inode_acl(dir, ACL_TYPE_DEFAULT);
|
2015-06-19 07:00:55 +08:00
|
|
|
if (!p || p == ERR_PTR(-EOPNOTSUPP)) {
|
|
|
|
*mode &= ~current_umask();
|
|
|
|
return 0;
|
2013-12-20 21:16:42 +08:00
|
|
|
}
|
2015-06-19 07:00:55 +08:00
|
|
|
if (IS_ERR(p))
|
|
|
|
return PTR_ERR(p);
|
2013-12-20 21:16:42 +08:00
|
|
|
|
2016-09-16 18:44:21 +08:00
|
|
|
ret = -ENOMEM;
|
2015-06-19 07:00:55 +08:00
|
|
|
clone = posix_acl_clone(p, GFP_NOFS);
|
|
|
|
if (!clone)
|
2016-09-16 18:44:21 +08:00
|
|
|
goto err_release;
|
2013-12-20 21:16:42 +08:00
|
|
|
|
2015-06-19 07:00:55 +08:00
|
|
|
ret = posix_acl_create_masq(clone, mode);
|
2015-02-09 13:45:25 +08:00
|
|
|
if (ret < 0)
|
2016-09-16 18:44:21 +08:00
|
|
|
goto err_release_clone;
|
2013-12-20 21:16:42 +08:00
|
|
|
|
2015-06-19 07:00:55 +08:00
|
|
|
if (ret == 0)
|
|
|
|
posix_acl_release(clone);
|
|
|
|
else
|
|
|
|
*acl = clone;
|
2013-12-20 21:16:42 +08:00
|
|
|
|
2015-06-19 07:00:55 +08:00
|
|
|
if (!S_ISDIR(*mode))
|
2013-12-20 21:16:42 +08:00
|
|
|
posix_acl_release(p);
|
2015-06-19 07:00:55 +08:00
|
|
|
else
|
2013-12-20 21:16:42 +08:00
|
|
|
*default_acl = p;
|
|
|
|
|
|
|
|
return 0;
|
2015-02-09 13:45:25 +08:00
|
|
|
|
2016-09-16 18:44:21 +08:00
|
|
|
err_release_clone:
|
2015-06-19 07:00:55 +08:00
|
|
|
posix_acl_release(clone);
|
2016-09-16 18:44:21 +08:00
|
|
|
err_release:
|
2015-02-09 13:45:25 +08:00
|
|
|
posix_acl_release(p);
|
2016-09-16 18:44:21 +08:00
|
|
|
return ret;
|
2013-12-20 21:16:42 +08:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(posix_acl_create);
|
|
|
|
|
2016-09-19 23:39:09 +08:00
|
|
|
/**
|
|
|
|
* posix_acl_update_mode - update mode in set_acl
|
2023-01-13 19:49:24 +08:00
|
|
|
* @idmap: idmap of the mount @inode was found from
|
2021-01-21 21:19:27 +08:00
|
|
|
* @inode: target inode
|
|
|
|
* @mode_p: mode (pointer) for update
|
|
|
|
* @acl: acl pointer
|
2016-09-19 23:39:09 +08:00
|
|
|
*
|
|
|
|
* Update the file mode when setting an ACL: compute the new file permission
|
|
|
|
* bits based on the ACL. In addition, if the ACL is equivalent to the new
|
2020-01-05 05:00:05 +08:00
|
|
|
* file mode, set *@acl to NULL to indicate that no ACL should be set.
|
2016-09-19 23:39:09 +08:00
|
|
|
*
|
2020-01-05 05:00:05 +08:00
|
|
|
* As with chmod, clear the setgid bit if the caller is not in the owning group
|
2016-09-19 23:39:09 +08:00
|
|
|
* or capable of CAP_FSETID (see inode_change_ok).
|
|
|
|
*
|
2023-01-13 19:49:24 +08:00
|
|
|
* If the inode has been found through an idmapped mount the idmap of
|
|
|
|
* the vfsmount must be passed through @idmap. This function will then
|
|
|
|
* take care to map the inode according to @idmap before checking
|
2021-01-21 21:19:27 +08:00
|
|
|
* permissions. On non-idmapped mounts or if permission checking is to be
|
2023-01-13 19:49:24 +08:00
|
|
|
* performed on the raw inode simply passs @nop_mnt_idmap.
|
2021-01-21 21:19:27 +08:00
|
|
|
*
|
2016-09-19 23:39:09 +08:00
|
|
|
* Called from set_acl inode operations.
|
|
|
|
*/
|
2023-01-13 19:49:24 +08:00
|
|
|
int posix_acl_update_mode(struct mnt_idmap *idmap,
|
2021-01-21 21:19:27 +08:00
|
|
|
struct inode *inode, umode_t *mode_p,
|
2016-09-19 23:39:09 +08:00
|
|
|
struct posix_acl **acl)
|
|
|
|
{
|
2023-01-13 19:49:24 +08:00
|
|
|
struct user_namespace *mnt_userns = mnt_idmap_owner(idmap);
|
2016-09-19 23:39:09 +08:00
|
|
|
umode_t mode = inode->i_mode;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
error = posix_acl_equiv_mode(*acl, &mode);
|
|
|
|
if (error < 0)
|
|
|
|
return error;
|
|
|
|
if (error == 0)
|
|
|
|
*acl = NULL;
|
2022-07-09 18:23:20 +08:00
|
|
|
if (!vfsgid_in_group_p(i_gid_into_vfsgid(mnt_userns, inode)) &&
|
2021-01-21 21:19:27 +08:00
|
|
|
!capable_wrt_inode_uidgid(mnt_userns, inode, CAP_FSETID))
|
2016-09-19 23:39:09 +08:00
|
|
|
mode &= ~S_ISGID;
|
|
|
|
*mode_p = mode;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(posix_acl_update_mode);
|
|
|
|
|
2013-12-20 21:16:37 +08:00
|
|
|
/*
|
|
|
|
* Fix up the uids and gids in posix acl extended attributes in place.
|
|
|
|
*/
|
2022-08-29 20:38:41 +08:00
|
|
|
static int posix_acl_fix_xattr_common(const void *value, size_t size)
|
acl: move idmapped mount fixup into vfs_{g,s}etxattr()
This cycle we added support for mounting overlayfs on top of idmapped mounts.
Recently I've started looking into potential corner cases when trying to add
additional tests and I noticed that reporting for POSIX ACLs is currently wrong
when using idmapped layers with overlayfs mounted on top of it.
I'm going to give a rather detailed explanation to both the origin of the
problem and the solution.
Let's assume the user creates the following directory layout and they have a
rootfs /var/lib/lxc/c1/rootfs. The files in this rootfs are owned as you would
expect files on your host system to be owned. For example, ~/.bashrc for your
regular user would be owned by 1000:1000 and /root/.bashrc would be owned by
0:0. IOW, this is just regular boring filesystem tree on an ext4 or xfs
filesystem.
The user chooses to set POSIX ACLs using the setfacl binary granting the user
with uid 4 read, write, and execute permissions for their .bashrc file:
setfacl -m u:4:rwx /var/lib/lxc/c2/rootfs/home/ubuntu/.bashrc
Now they to expose the whole rootfs to a container using an idmapped mount. So
they first create:
mkdir -pv /vol/contpool/{ctrover,merge,lowermap,overmap}
mkdir -pv /vol/contpool/ctrover/{over,work}
chown 10000000:10000000 /vol/contpool/ctrover/{over,work}
The user now creates an idmapped mount for the rootfs:
mount-idmapped/mount-idmapped --map-mount=b:0:10000000:65536 \
/var/lib/lxc/c2/rootfs \
/vol/contpool/lowermap
This for example makes it so that /var/lib/lxc/c2/rootfs/home/ubuntu/.bashrc
which is owned by uid and gid 1000 as being owned by uid and gid 10001000 at
/vol/contpool/lowermap/home/ubuntu/.bashrc.
Assume the user wants to expose these idmapped mounts through an overlayfs
mount to a container.
mount -t overlay overlay \
-o lowerdir=/vol/contpool/lowermap, \
upperdir=/vol/contpool/overmap/over, \
workdir=/vol/contpool/overmap/work \
/vol/contpool/merge
The user can do this in two ways:
(1) Mount overlayfs in the initial user namespace and expose it to the
container.
(2) Mount overlayfs on top of the idmapped mounts inside of the container's
user namespace.
Let's assume the user chooses the (1) option and mounts overlayfs on the host
and then changes into a container which uses the idmapping 0:10000000:65536
which is the same used for the two idmapped mounts.
Now the user tries to retrieve the POSIX ACLs using the getfacl command
getfacl -n /vol/contpool/lowermap/home/ubuntu/.bashrc
and to their surprise they see:
# file: vol/contpool/merge/home/ubuntu/.bashrc
# owner: 1000
# group: 1000
user::rw-
user:4294967295:rwx
group::r--
mask::rwx
other::r--
indicating the the uid wasn't correctly translated according to the idmapped
mount. The problem is how we currently translate POSIX ACLs. Let's inspect the
callchain in this example:
idmapped mount /vol/contpool/merge: 0:10000000:65536
caller's idmapping: 0:10000000:65536
overlayfs idmapping (ofs->creator_cred): 0:0:4k /* initial idmapping */
sys_getxattr()
-> path_getxattr()
-> getxattr()
-> do_getxattr()
|> vfs_getxattr()
| -> __vfs_getxattr()
| -> handler->get == ovl_posix_acl_xattr_get()
| -> ovl_xattr_get()
| -> vfs_getxattr()
| -> __vfs_getxattr()
| -> handler->get() /* lower filesystem callback */
|> posix_acl_fix_xattr_to_user()
{
4 = make_kuid(&init_user_ns, 4);
4 = mapped_kuid_fs(&init_user_ns /* no idmapped mount */, 4);
/* FAILURE */
-1 = from_kuid(0:10000000:65536 /* caller's idmapping */, 4);
}
If the user chooses to use option (2) and mounts overlayfs on top of idmapped
mounts inside the container things don't look that much better:
idmapped mount /vol/contpool/merge: 0:10000000:65536
caller's idmapping: 0:10000000:65536
overlayfs idmapping (ofs->creator_cred): 0:10000000:65536
sys_getxattr()
-> path_getxattr()
-> getxattr()
-> do_getxattr()
|> vfs_getxattr()
| -> __vfs_getxattr()
| -> handler->get == ovl_posix_acl_xattr_get()
| -> ovl_xattr_get()
| -> vfs_getxattr()
| -> __vfs_getxattr()
| -> handler->get() /* lower filesystem callback */
|> posix_acl_fix_xattr_to_user()
{
4 = make_kuid(&init_user_ns, 4);
4 = mapped_kuid_fs(&init_user_ns, 4);
/* FAILURE */
-1 = from_kuid(0:10000000:65536 /* caller's idmapping */, 4);
}
As is easily seen the problem arises because the idmapping of the lower mount
isn't taken into account as all of this happens in do_gexattr(). But
do_getxattr() is always called on an overlayfs mount and inode and thus cannot
possible take the idmapping of the lower layers into account.
This problem is similar for fscaps but there the translation happens as part of
vfs_getxattr() already. Let's walk through an fscaps overlayfs callchain:
setcap 'cap_net_raw+ep' /var/lib/lxc/c2/rootfs/home/ubuntu/.bashrc
The expected outcome here is that we'll receive the cap_net_raw capability as
we are able to map the uid associated with the fscap to 0 within our container.
IOW, we want to see 0 as the result of the idmapping translations.
If the user chooses option (1) we get the following callchain for fscaps:
idmapped mount /vol/contpool/merge: 0:10000000:65536
caller's idmapping: 0:10000000:65536
overlayfs idmapping (ofs->creator_cred): 0:0:4k /* initial idmapping */
sys_getxattr()
-> path_getxattr()
-> getxattr()
-> do_getxattr()
-> vfs_getxattr()
-> xattr_getsecurity()
-> security_inode_getsecurity() ________________________________
-> cap_inode_getsecurity() | |
{ V |
10000000 = make_kuid(0:0:4k /* overlayfs idmapping */, 10000000); |
10000000 = mapped_kuid_fs(0:0:4k /* no idmapped mount */, 10000000); |
/* Expected result is 0 and thus that we own the fscap. */ |
0 = from_kuid(0:10000000:65536 /* caller's idmapping */, 10000000); |
} |
-> vfs_getxattr_alloc() |
-> handler->get == ovl_other_xattr_get() |
-> vfs_getxattr() |
-> xattr_getsecurity() |
-> security_inode_getsecurity() |
-> cap_inode_getsecurity() |
{ |
0 = make_kuid(0:0:4k /* lower s_user_ns */, 0); |
10000000 = mapped_kuid_fs(0:10000000:65536 /* idmapped mount */, 0); |
10000000 = from_kuid(0:0:4k /* overlayfs idmapping */, 10000000); |
|____________________________________________________________________|
}
-> vfs_getxattr_alloc()
-> handler->get == /* lower filesystem callback */
And if the user chooses option (2) we get:
idmapped mount /vol/contpool/merge: 0:10000000:65536
caller's idmapping: 0:10000000:65536
overlayfs idmapping (ofs->creator_cred): 0:10000000:65536
sys_getxattr()
-> path_getxattr()
-> getxattr()
-> do_getxattr()
-> vfs_getxattr()
-> xattr_getsecurity()
-> security_inode_getsecurity() _______________________________
-> cap_inode_getsecurity() | |
{ V |
10000000 = make_kuid(0:10000000:65536 /* overlayfs idmapping */, 0); |
10000000 = mapped_kuid_fs(0:0:4k /* no idmapped mount */, 10000000); |
/* Expected result is 0 and thus that we own the fscap. */ |
0 = from_kuid(0:10000000:65536 /* caller's idmapping */, 10000000); |
} |
-> vfs_getxattr_alloc() |
-> handler->get == ovl_other_xattr_get() |
|-> vfs_getxattr() |
-> xattr_getsecurity() |
-> security_inode_getsecurity() |
-> cap_inode_getsecurity() |
{ |
0 = make_kuid(0:0:4k /* lower s_user_ns */, 0); |
10000000 = mapped_kuid_fs(0:10000000:65536 /* idmapped mount */, 0); |
0 = from_kuid(0:10000000:65536 /* overlayfs idmapping */, 10000000); |
|____________________________________________________________________|
}
-> vfs_getxattr_alloc()
-> handler->get == /* lower filesystem callback */
We can see how the translation happens correctly in those cases as the
conversion happens within the vfs_getxattr() helper.
For POSIX ACLs we need to do something similar. However, in contrast to fscaps
we cannot apply the fix directly to the kernel internal posix acl data
structure as this would alter the cached values and would also require a rework
of how we currently deal with POSIX ACLs in general which almost never take the
filesystem idmapping into account (the noteable exception being FUSE but even
there the implementation is special) and instead retrieve the raw values based
on the initial idmapping.
The correct values are then generated right before returning to userspace. The
fix for this is to move taking the mount's idmapping into account directly in
vfs_getxattr() instead of having it be part of posix_acl_fix_xattr_to_user().
To this end we split out two small and unexported helpers
posix_acl_getxattr_idmapped_mnt() and posix_acl_setxattr_idmapped_mnt(). The
former to be called in vfs_getxattr() and the latter to be called in
vfs_setxattr().
Let's go back to the original example. Assume the user chose option (1) and
mounted overlayfs on top of idmapped mounts on the host:
idmapped mount /vol/contpool/merge: 0:10000000:65536
caller's idmapping: 0:10000000:65536
overlayfs idmapping (ofs->creator_cred): 0:0:4k /* initial idmapping */
sys_getxattr()
-> path_getxattr()
-> getxattr()
-> do_getxattr()
|> vfs_getxattr()
| |> __vfs_getxattr()
| | -> handler->get == ovl_posix_acl_xattr_get()
| | -> ovl_xattr_get()
| | -> vfs_getxattr()
| | |> __vfs_getxattr()
| | | -> handler->get() /* lower filesystem callback */
| | |> posix_acl_getxattr_idmapped_mnt()
| | {
| | 4 = make_kuid(&init_user_ns, 4);
| | 10000004 = mapped_kuid_fs(0:10000000:65536 /* lower idmapped mount */, 4);
| | 10000004 = from_kuid(&init_user_ns, 10000004);
| | |_______________________
| | } |
| | |
| |> posix_acl_getxattr_idmapped_mnt() |
| { |
| V
| 10000004 = make_kuid(&init_user_ns, 10000004);
| 10000004 = mapped_kuid_fs(&init_user_ns /* no idmapped mount */, 10000004);
| 10000004 = from_kuid(&init_user_ns, 10000004);
| } |_________________________________________________
| |
| |
|> posix_acl_fix_xattr_to_user() |
{ V
10000004 = make_kuid(0:0:4k /* init_user_ns */, 10000004);
/* SUCCESS */
4 = from_kuid(0:10000000:65536 /* caller's idmapping */, 10000004);
}
And similarly if the user chooses option (1) and mounted overayfs on top of
idmapped mounts inside the container:
idmapped mount /vol/contpool/merge: 0:10000000:65536
caller's idmapping: 0:10000000:65536
overlayfs idmapping (ofs->creator_cred): 0:10000000:65536
sys_getxattr()
-> path_getxattr()
-> getxattr()
-> do_getxattr()
|> vfs_getxattr()
| |> __vfs_getxattr()
| | -> handler->get == ovl_posix_acl_xattr_get()
| | -> ovl_xattr_get()
| | -> vfs_getxattr()
| | |> __vfs_getxattr()
| | | -> handler->get() /* lower filesystem callback */
| | |> posix_acl_getxattr_idmapped_mnt()
| | {
| | 4 = make_kuid(&init_user_ns, 4);
| | 10000004 = mapped_kuid_fs(0:10000000:65536 /* lower idmapped mount */, 4);
| | 10000004 = from_kuid(&init_user_ns, 10000004);
| | |_______________________
| | } |
| | |
| |> posix_acl_getxattr_idmapped_mnt() |
| { V
| 10000004 = make_kuid(&init_user_ns, 10000004);
| 10000004 = mapped_kuid_fs(&init_user_ns /* no idmapped mount */, 10000004);
| 10000004 = from_kuid(0(&init_user_ns, 10000004);
| |_________________________________________________
| } |
| |
|> posix_acl_fix_xattr_to_user() |
{ V
10000004 = make_kuid(0:0:4k /* init_user_ns */, 10000004);
/* SUCCESS */
4 = from_kuid(0:10000000:65536 /* caller's idmappings */, 10000004);
}
The last remaining problem we need to fix here is ovl_get_acl(). During
ovl_permission() overlayfs will call:
ovl_permission()
-> generic_permission()
-> acl_permission_check()
-> check_acl()
-> get_acl()
-> inode->i_op->get_acl() == ovl_get_acl()
> get_acl() /* on the underlying filesystem)
->inode->i_op->get_acl() == /*lower filesystem callback */
-> posix_acl_permission()
passing through the get_acl request to the underlying filesystem. This will
retrieve the acls stored in the lower filesystem without taking the idmapping
of the underlying mount into account as this would mean altering the cached
values for the lower filesystem. So we block using ACLs for now until we
decided on a nice way to fix this. Note this limitation both in the
documentation and in the code.
The most straightforward solution would be to have ovl_get_acl() simply
duplicate the ACLs, update the values according to the idmapped mount and
return it to acl_permission_check() so it can be used in posix_acl_permission()
forgetting them afterwards. This is a bit heavy handed but fairly
straightforward otherwise.
Link: https://github.com/brauner/mount-idmapped/issues/9
Link: https://lore.kernel.org/r/20220708090134.385160-2-brauner@kernel.org
Cc: Seth Forshee <sforshee@digitalocean.com>
Cc: Amir Goldstein <amir73il@gmail.com>
Cc: Vivek Goyal <vgoyal@redhat.com>
Cc: Christoph Hellwig <hch@lst.de>
Cc: Aleksa Sarai <cyphar@cyphar.com>
Cc: Miklos Szeredi <mszeredi@redhat.com>
Cc: linux-unionfs@vger.kernel.org
Cc: linux-fsdevel@vger.kernel.org
Reviewed-by: Seth Forshee <sforshee@digitalocean.com>
Signed-off-by: Christian Brauner (Microsoft) <brauner@kernel.org>
2022-07-07 00:30:59 +08:00
|
|
|
{
|
2022-08-29 20:38:41 +08:00
|
|
|
const struct posix_acl_xattr_header *header = value;
|
acl: move idmapped mount fixup into vfs_{g,s}etxattr()
This cycle we added support for mounting overlayfs on top of idmapped mounts.
Recently I've started looking into potential corner cases when trying to add
additional tests and I noticed that reporting for POSIX ACLs is currently wrong
when using idmapped layers with overlayfs mounted on top of it.
I'm going to give a rather detailed explanation to both the origin of the
problem and the solution.
Let's assume the user creates the following directory layout and they have a
rootfs /var/lib/lxc/c1/rootfs. The files in this rootfs are owned as you would
expect files on your host system to be owned. For example, ~/.bashrc for your
regular user would be owned by 1000:1000 and /root/.bashrc would be owned by
0:0. IOW, this is just regular boring filesystem tree on an ext4 or xfs
filesystem.
The user chooses to set POSIX ACLs using the setfacl binary granting the user
with uid 4 read, write, and execute permissions for their .bashrc file:
setfacl -m u:4:rwx /var/lib/lxc/c2/rootfs/home/ubuntu/.bashrc
Now they to expose the whole rootfs to a container using an idmapped mount. So
they first create:
mkdir -pv /vol/contpool/{ctrover,merge,lowermap,overmap}
mkdir -pv /vol/contpool/ctrover/{over,work}
chown 10000000:10000000 /vol/contpool/ctrover/{over,work}
The user now creates an idmapped mount for the rootfs:
mount-idmapped/mount-idmapped --map-mount=b:0:10000000:65536 \
/var/lib/lxc/c2/rootfs \
/vol/contpool/lowermap
This for example makes it so that /var/lib/lxc/c2/rootfs/home/ubuntu/.bashrc
which is owned by uid and gid 1000 as being owned by uid and gid 10001000 at
/vol/contpool/lowermap/home/ubuntu/.bashrc.
Assume the user wants to expose these idmapped mounts through an overlayfs
mount to a container.
mount -t overlay overlay \
-o lowerdir=/vol/contpool/lowermap, \
upperdir=/vol/contpool/overmap/over, \
workdir=/vol/contpool/overmap/work \
/vol/contpool/merge
The user can do this in two ways:
(1) Mount overlayfs in the initial user namespace and expose it to the
container.
(2) Mount overlayfs on top of the idmapped mounts inside of the container's
user namespace.
Let's assume the user chooses the (1) option and mounts overlayfs on the host
and then changes into a container which uses the idmapping 0:10000000:65536
which is the same used for the two idmapped mounts.
Now the user tries to retrieve the POSIX ACLs using the getfacl command
getfacl -n /vol/contpool/lowermap/home/ubuntu/.bashrc
and to their surprise they see:
# file: vol/contpool/merge/home/ubuntu/.bashrc
# owner: 1000
# group: 1000
user::rw-
user:4294967295:rwx
group::r--
mask::rwx
other::r--
indicating the the uid wasn't correctly translated according to the idmapped
mount. The problem is how we currently translate POSIX ACLs. Let's inspect the
callchain in this example:
idmapped mount /vol/contpool/merge: 0:10000000:65536
caller's idmapping: 0:10000000:65536
overlayfs idmapping (ofs->creator_cred): 0:0:4k /* initial idmapping */
sys_getxattr()
-> path_getxattr()
-> getxattr()
-> do_getxattr()
|> vfs_getxattr()
| -> __vfs_getxattr()
| -> handler->get == ovl_posix_acl_xattr_get()
| -> ovl_xattr_get()
| -> vfs_getxattr()
| -> __vfs_getxattr()
| -> handler->get() /* lower filesystem callback */
|> posix_acl_fix_xattr_to_user()
{
4 = make_kuid(&init_user_ns, 4);
4 = mapped_kuid_fs(&init_user_ns /* no idmapped mount */, 4);
/* FAILURE */
-1 = from_kuid(0:10000000:65536 /* caller's idmapping */, 4);
}
If the user chooses to use option (2) and mounts overlayfs on top of idmapped
mounts inside the container things don't look that much better:
idmapped mount /vol/contpool/merge: 0:10000000:65536
caller's idmapping: 0:10000000:65536
overlayfs idmapping (ofs->creator_cred): 0:10000000:65536
sys_getxattr()
-> path_getxattr()
-> getxattr()
-> do_getxattr()
|> vfs_getxattr()
| -> __vfs_getxattr()
| -> handler->get == ovl_posix_acl_xattr_get()
| -> ovl_xattr_get()
| -> vfs_getxattr()
| -> __vfs_getxattr()
| -> handler->get() /* lower filesystem callback */
|> posix_acl_fix_xattr_to_user()
{
4 = make_kuid(&init_user_ns, 4);
4 = mapped_kuid_fs(&init_user_ns, 4);
/* FAILURE */
-1 = from_kuid(0:10000000:65536 /* caller's idmapping */, 4);
}
As is easily seen the problem arises because the idmapping of the lower mount
isn't taken into account as all of this happens in do_gexattr(). But
do_getxattr() is always called on an overlayfs mount and inode and thus cannot
possible take the idmapping of the lower layers into account.
This problem is similar for fscaps but there the translation happens as part of
vfs_getxattr() already. Let's walk through an fscaps overlayfs callchain:
setcap 'cap_net_raw+ep' /var/lib/lxc/c2/rootfs/home/ubuntu/.bashrc
The expected outcome here is that we'll receive the cap_net_raw capability as
we are able to map the uid associated with the fscap to 0 within our container.
IOW, we want to see 0 as the result of the idmapping translations.
If the user chooses option (1) we get the following callchain for fscaps:
idmapped mount /vol/contpool/merge: 0:10000000:65536
caller's idmapping: 0:10000000:65536
overlayfs idmapping (ofs->creator_cred): 0:0:4k /* initial idmapping */
sys_getxattr()
-> path_getxattr()
-> getxattr()
-> do_getxattr()
-> vfs_getxattr()
-> xattr_getsecurity()
-> security_inode_getsecurity() ________________________________
-> cap_inode_getsecurity() | |
{ V |
10000000 = make_kuid(0:0:4k /* overlayfs idmapping */, 10000000); |
10000000 = mapped_kuid_fs(0:0:4k /* no idmapped mount */, 10000000); |
/* Expected result is 0 and thus that we own the fscap. */ |
0 = from_kuid(0:10000000:65536 /* caller's idmapping */, 10000000); |
} |
-> vfs_getxattr_alloc() |
-> handler->get == ovl_other_xattr_get() |
-> vfs_getxattr() |
-> xattr_getsecurity() |
-> security_inode_getsecurity() |
-> cap_inode_getsecurity() |
{ |
0 = make_kuid(0:0:4k /* lower s_user_ns */, 0); |
10000000 = mapped_kuid_fs(0:10000000:65536 /* idmapped mount */, 0); |
10000000 = from_kuid(0:0:4k /* overlayfs idmapping */, 10000000); |
|____________________________________________________________________|
}
-> vfs_getxattr_alloc()
-> handler->get == /* lower filesystem callback */
And if the user chooses option (2) we get:
idmapped mount /vol/contpool/merge: 0:10000000:65536
caller's idmapping: 0:10000000:65536
overlayfs idmapping (ofs->creator_cred): 0:10000000:65536
sys_getxattr()
-> path_getxattr()
-> getxattr()
-> do_getxattr()
-> vfs_getxattr()
-> xattr_getsecurity()
-> security_inode_getsecurity() _______________________________
-> cap_inode_getsecurity() | |
{ V |
10000000 = make_kuid(0:10000000:65536 /* overlayfs idmapping */, 0); |
10000000 = mapped_kuid_fs(0:0:4k /* no idmapped mount */, 10000000); |
/* Expected result is 0 and thus that we own the fscap. */ |
0 = from_kuid(0:10000000:65536 /* caller's idmapping */, 10000000); |
} |
-> vfs_getxattr_alloc() |
-> handler->get == ovl_other_xattr_get() |
|-> vfs_getxattr() |
-> xattr_getsecurity() |
-> security_inode_getsecurity() |
-> cap_inode_getsecurity() |
{ |
0 = make_kuid(0:0:4k /* lower s_user_ns */, 0); |
10000000 = mapped_kuid_fs(0:10000000:65536 /* idmapped mount */, 0); |
0 = from_kuid(0:10000000:65536 /* overlayfs idmapping */, 10000000); |
|____________________________________________________________________|
}
-> vfs_getxattr_alloc()
-> handler->get == /* lower filesystem callback */
We can see how the translation happens correctly in those cases as the
conversion happens within the vfs_getxattr() helper.
For POSIX ACLs we need to do something similar. However, in contrast to fscaps
we cannot apply the fix directly to the kernel internal posix acl data
structure as this would alter the cached values and would also require a rework
of how we currently deal with POSIX ACLs in general which almost never take the
filesystem idmapping into account (the noteable exception being FUSE but even
there the implementation is special) and instead retrieve the raw values based
on the initial idmapping.
The correct values are then generated right before returning to userspace. The
fix for this is to move taking the mount's idmapping into account directly in
vfs_getxattr() instead of having it be part of posix_acl_fix_xattr_to_user().
To this end we split out two small and unexported helpers
posix_acl_getxattr_idmapped_mnt() and posix_acl_setxattr_idmapped_mnt(). The
former to be called in vfs_getxattr() and the latter to be called in
vfs_setxattr().
Let's go back to the original example. Assume the user chose option (1) and
mounted overlayfs on top of idmapped mounts on the host:
idmapped mount /vol/contpool/merge: 0:10000000:65536
caller's idmapping: 0:10000000:65536
overlayfs idmapping (ofs->creator_cred): 0:0:4k /* initial idmapping */
sys_getxattr()
-> path_getxattr()
-> getxattr()
-> do_getxattr()
|> vfs_getxattr()
| |> __vfs_getxattr()
| | -> handler->get == ovl_posix_acl_xattr_get()
| | -> ovl_xattr_get()
| | -> vfs_getxattr()
| | |> __vfs_getxattr()
| | | -> handler->get() /* lower filesystem callback */
| | |> posix_acl_getxattr_idmapped_mnt()
| | {
| | 4 = make_kuid(&init_user_ns, 4);
| | 10000004 = mapped_kuid_fs(0:10000000:65536 /* lower idmapped mount */, 4);
| | 10000004 = from_kuid(&init_user_ns, 10000004);
| | |_______________________
| | } |
| | |
| |> posix_acl_getxattr_idmapped_mnt() |
| { |
| V
| 10000004 = make_kuid(&init_user_ns, 10000004);
| 10000004 = mapped_kuid_fs(&init_user_ns /* no idmapped mount */, 10000004);
| 10000004 = from_kuid(&init_user_ns, 10000004);
| } |_________________________________________________
| |
| |
|> posix_acl_fix_xattr_to_user() |
{ V
10000004 = make_kuid(0:0:4k /* init_user_ns */, 10000004);
/* SUCCESS */
4 = from_kuid(0:10000000:65536 /* caller's idmapping */, 10000004);
}
And similarly if the user chooses option (1) and mounted overayfs on top of
idmapped mounts inside the container:
idmapped mount /vol/contpool/merge: 0:10000000:65536
caller's idmapping: 0:10000000:65536
overlayfs idmapping (ofs->creator_cred): 0:10000000:65536
sys_getxattr()
-> path_getxattr()
-> getxattr()
-> do_getxattr()
|> vfs_getxattr()
| |> __vfs_getxattr()
| | -> handler->get == ovl_posix_acl_xattr_get()
| | -> ovl_xattr_get()
| | -> vfs_getxattr()
| | |> __vfs_getxattr()
| | | -> handler->get() /* lower filesystem callback */
| | |> posix_acl_getxattr_idmapped_mnt()
| | {
| | 4 = make_kuid(&init_user_ns, 4);
| | 10000004 = mapped_kuid_fs(0:10000000:65536 /* lower idmapped mount */, 4);
| | 10000004 = from_kuid(&init_user_ns, 10000004);
| | |_______________________
| | } |
| | |
| |> posix_acl_getxattr_idmapped_mnt() |
| { V
| 10000004 = make_kuid(&init_user_ns, 10000004);
| 10000004 = mapped_kuid_fs(&init_user_ns /* no idmapped mount */, 10000004);
| 10000004 = from_kuid(0(&init_user_ns, 10000004);
| |_________________________________________________
| } |
| |
|> posix_acl_fix_xattr_to_user() |
{ V
10000004 = make_kuid(0:0:4k /* init_user_ns */, 10000004);
/* SUCCESS */
4 = from_kuid(0:10000000:65536 /* caller's idmappings */, 10000004);
}
The last remaining problem we need to fix here is ovl_get_acl(). During
ovl_permission() overlayfs will call:
ovl_permission()
-> generic_permission()
-> acl_permission_check()
-> check_acl()
-> get_acl()
-> inode->i_op->get_acl() == ovl_get_acl()
> get_acl() /* on the underlying filesystem)
->inode->i_op->get_acl() == /*lower filesystem callback */
-> posix_acl_permission()
passing through the get_acl request to the underlying filesystem. This will
retrieve the acls stored in the lower filesystem without taking the idmapping
of the underlying mount into account as this would mean altering the cached
values for the lower filesystem. So we block using ACLs for now until we
decided on a nice way to fix this. Note this limitation both in the
documentation and in the code.
The most straightforward solution would be to have ovl_get_acl() simply
duplicate the ACLs, update the values according to the idmapped mount and
return it to acl_permission_check() so it can be used in posix_acl_permission()
forgetting them afterwards. This is a bit heavy handed but fairly
straightforward otherwise.
Link: https://github.com/brauner/mount-idmapped/issues/9
Link: https://lore.kernel.org/r/20220708090134.385160-2-brauner@kernel.org
Cc: Seth Forshee <sforshee@digitalocean.com>
Cc: Amir Goldstein <amir73il@gmail.com>
Cc: Vivek Goyal <vgoyal@redhat.com>
Cc: Christoph Hellwig <hch@lst.de>
Cc: Aleksa Sarai <cyphar@cyphar.com>
Cc: Miklos Szeredi <mszeredi@redhat.com>
Cc: linux-unionfs@vger.kernel.org
Cc: linux-fsdevel@vger.kernel.org
Reviewed-by: Seth Forshee <sforshee@digitalocean.com>
Signed-off-by: Christian Brauner (Microsoft) <brauner@kernel.org>
2022-07-07 00:30:59 +08:00
|
|
|
int count;
|
|
|
|
|
|
|
|
if (!header)
|
|
|
|
return -EINVAL;
|
|
|
|
if (size < sizeof(struct posix_acl_xattr_header))
|
|
|
|
return -EINVAL;
|
|
|
|
if (header->a_version != cpu_to_le32(POSIX_ACL_XATTR_VERSION))
|
2022-08-29 20:38:41 +08:00
|
|
|
return -EOPNOTSUPP;
|
acl: move idmapped mount fixup into vfs_{g,s}etxattr()
This cycle we added support for mounting overlayfs on top of idmapped mounts.
Recently I've started looking into potential corner cases when trying to add
additional tests and I noticed that reporting for POSIX ACLs is currently wrong
when using idmapped layers with overlayfs mounted on top of it.
I'm going to give a rather detailed explanation to both the origin of the
problem and the solution.
Let's assume the user creates the following directory layout and they have a
rootfs /var/lib/lxc/c1/rootfs. The files in this rootfs are owned as you would
expect files on your host system to be owned. For example, ~/.bashrc for your
regular user would be owned by 1000:1000 and /root/.bashrc would be owned by
0:0. IOW, this is just regular boring filesystem tree on an ext4 or xfs
filesystem.
The user chooses to set POSIX ACLs using the setfacl binary granting the user
with uid 4 read, write, and execute permissions for their .bashrc file:
setfacl -m u:4:rwx /var/lib/lxc/c2/rootfs/home/ubuntu/.bashrc
Now they to expose the whole rootfs to a container using an idmapped mount. So
they first create:
mkdir -pv /vol/contpool/{ctrover,merge,lowermap,overmap}
mkdir -pv /vol/contpool/ctrover/{over,work}
chown 10000000:10000000 /vol/contpool/ctrover/{over,work}
The user now creates an idmapped mount for the rootfs:
mount-idmapped/mount-idmapped --map-mount=b:0:10000000:65536 \
/var/lib/lxc/c2/rootfs \
/vol/contpool/lowermap
This for example makes it so that /var/lib/lxc/c2/rootfs/home/ubuntu/.bashrc
which is owned by uid and gid 1000 as being owned by uid and gid 10001000 at
/vol/contpool/lowermap/home/ubuntu/.bashrc.
Assume the user wants to expose these idmapped mounts through an overlayfs
mount to a container.
mount -t overlay overlay \
-o lowerdir=/vol/contpool/lowermap, \
upperdir=/vol/contpool/overmap/over, \
workdir=/vol/contpool/overmap/work \
/vol/contpool/merge
The user can do this in two ways:
(1) Mount overlayfs in the initial user namespace and expose it to the
container.
(2) Mount overlayfs on top of the idmapped mounts inside of the container's
user namespace.
Let's assume the user chooses the (1) option and mounts overlayfs on the host
and then changes into a container which uses the idmapping 0:10000000:65536
which is the same used for the two idmapped mounts.
Now the user tries to retrieve the POSIX ACLs using the getfacl command
getfacl -n /vol/contpool/lowermap/home/ubuntu/.bashrc
and to their surprise they see:
# file: vol/contpool/merge/home/ubuntu/.bashrc
# owner: 1000
# group: 1000
user::rw-
user:4294967295:rwx
group::r--
mask::rwx
other::r--
indicating the the uid wasn't correctly translated according to the idmapped
mount. The problem is how we currently translate POSIX ACLs. Let's inspect the
callchain in this example:
idmapped mount /vol/contpool/merge: 0:10000000:65536
caller's idmapping: 0:10000000:65536
overlayfs idmapping (ofs->creator_cred): 0:0:4k /* initial idmapping */
sys_getxattr()
-> path_getxattr()
-> getxattr()
-> do_getxattr()
|> vfs_getxattr()
| -> __vfs_getxattr()
| -> handler->get == ovl_posix_acl_xattr_get()
| -> ovl_xattr_get()
| -> vfs_getxattr()
| -> __vfs_getxattr()
| -> handler->get() /* lower filesystem callback */
|> posix_acl_fix_xattr_to_user()
{
4 = make_kuid(&init_user_ns, 4);
4 = mapped_kuid_fs(&init_user_ns /* no idmapped mount */, 4);
/* FAILURE */
-1 = from_kuid(0:10000000:65536 /* caller's idmapping */, 4);
}
If the user chooses to use option (2) and mounts overlayfs on top of idmapped
mounts inside the container things don't look that much better:
idmapped mount /vol/contpool/merge: 0:10000000:65536
caller's idmapping: 0:10000000:65536
overlayfs idmapping (ofs->creator_cred): 0:10000000:65536
sys_getxattr()
-> path_getxattr()
-> getxattr()
-> do_getxattr()
|> vfs_getxattr()
| -> __vfs_getxattr()
| -> handler->get == ovl_posix_acl_xattr_get()
| -> ovl_xattr_get()
| -> vfs_getxattr()
| -> __vfs_getxattr()
| -> handler->get() /* lower filesystem callback */
|> posix_acl_fix_xattr_to_user()
{
4 = make_kuid(&init_user_ns, 4);
4 = mapped_kuid_fs(&init_user_ns, 4);
/* FAILURE */
-1 = from_kuid(0:10000000:65536 /* caller's idmapping */, 4);
}
As is easily seen the problem arises because the idmapping of the lower mount
isn't taken into account as all of this happens in do_gexattr(). But
do_getxattr() is always called on an overlayfs mount and inode and thus cannot
possible take the idmapping of the lower layers into account.
This problem is similar for fscaps but there the translation happens as part of
vfs_getxattr() already. Let's walk through an fscaps overlayfs callchain:
setcap 'cap_net_raw+ep' /var/lib/lxc/c2/rootfs/home/ubuntu/.bashrc
The expected outcome here is that we'll receive the cap_net_raw capability as
we are able to map the uid associated with the fscap to 0 within our container.
IOW, we want to see 0 as the result of the idmapping translations.
If the user chooses option (1) we get the following callchain for fscaps:
idmapped mount /vol/contpool/merge: 0:10000000:65536
caller's idmapping: 0:10000000:65536
overlayfs idmapping (ofs->creator_cred): 0:0:4k /* initial idmapping */
sys_getxattr()
-> path_getxattr()
-> getxattr()
-> do_getxattr()
-> vfs_getxattr()
-> xattr_getsecurity()
-> security_inode_getsecurity() ________________________________
-> cap_inode_getsecurity() | |
{ V |
10000000 = make_kuid(0:0:4k /* overlayfs idmapping */, 10000000); |
10000000 = mapped_kuid_fs(0:0:4k /* no idmapped mount */, 10000000); |
/* Expected result is 0 and thus that we own the fscap. */ |
0 = from_kuid(0:10000000:65536 /* caller's idmapping */, 10000000); |
} |
-> vfs_getxattr_alloc() |
-> handler->get == ovl_other_xattr_get() |
-> vfs_getxattr() |
-> xattr_getsecurity() |
-> security_inode_getsecurity() |
-> cap_inode_getsecurity() |
{ |
0 = make_kuid(0:0:4k /* lower s_user_ns */, 0); |
10000000 = mapped_kuid_fs(0:10000000:65536 /* idmapped mount */, 0); |
10000000 = from_kuid(0:0:4k /* overlayfs idmapping */, 10000000); |
|____________________________________________________________________|
}
-> vfs_getxattr_alloc()
-> handler->get == /* lower filesystem callback */
And if the user chooses option (2) we get:
idmapped mount /vol/contpool/merge: 0:10000000:65536
caller's idmapping: 0:10000000:65536
overlayfs idmapping (ofs->creator_cred): 0:10000000:65536
sys_getxattr()
-> path_getxattr()
-> getxattr()
-> do_getxattr()
-> vfs_getxattr()
-> xattr_getsecurity()
-> security_inode_getsecurity() _______________________________
-> cap_inode_getsecurity() | |
{ V |
10000000 = make_kuid(0:10000000:65536 /* overlayfs idmapping */, 0); |
10000000 = mapped_kuid_fs(0:0:4k /* no idmapped mount */, 10000000); |
/* Expected result is 0 and thus that we own the fscap. */ |
0 = from_kuid(0:10000000:65536 /* caller's idmapping */, 10000000); |
} |
-> vfs_getxattr_alloc() |
-> handler->get == ovl_other_xattr_get() |
|-> vfs_getxattr() |
-> xattr_getsecurity() |
-> security_inode_getsecurity() |
-> cap_inode_getsecurity() |
{ |
0 = make_kuid(0:0:4k /* lower s_user_ns */, 0); |
10000000 = mapped_kuid_fs(0:10000000:65536 /* idmapped mount */, 0); |
0 = from_kuid(0:10000000:65536 /* overlayfs idmapping */, 10000000); |
|____________________________________________________________________|
}
-> vfs_getxattr_alloc()
-> handler->get == /* lower filesystem callback */
We can see how the translation happens correctly in those cases as the
conversion happens within the vfs_getxattr() helper.
For POSIX ACLs we need to do something similar. However, in contrast to fscaps
we cannot apply the fix directly to the kernel internal posix acl data
structure as this would alter the cached values and would also require a rework
of how we currently deal with POSIX ACLs in general which almost never take the
filesystem idmapping into account (the noteable exception being FUSE but even
there the implementation is special) and instead retrieve the raw values based
on the initial idmapping.
The correct values are then generated right before returning to userspace. The
fix for this is to move taking the mount's idmapping into account directly in
vfs_getxattr() instead of having it be part of posix_acl_fix_xattr_to_user().
To this end we split out two small and unexported helpers
posix_acl_getxattr_idmapped_mnt() and posix_acl_setxattr_idmapped_mnt(). The
former to be called in vfs_getxattr() and the latter to be called in
vfs_setxattr().
Let's go back to the original example. Assume the user chose option (1) and
mounted overlayfs on top of idmapped mounts on the host:
idmapped mount /vol/contpool/merge: 0:10000000:65536
caller's idmapping: 0:10000000:65536
overlayfs idmapping (ofs->creator_cred): 0:0:4k /* initial idmapping */
sys_getxattr()
-> path_getxattr()
-> getxattr()
-> do_getxattr()
|> vfs_getxattr()
| |> __vfs_getxattr()
| | -> handler->get == ovl_posix_acl_xattr_get()
| | -> ovl_xattr_get()
| | -> vfs_getxattr()
| | |> __vfs_getxattr()
| | | -> handler->get() /* lower filesystem callback */
| | |> posix_acl_getxattr_idmapped_mnt()
| | {
| | 4 = make_kuid(&init_user_ns, 4);
| | 10000004 = mapped_kuid_fs(0:10000000:65536 /* lower idmapped mount */, 4);
| | 10000004 = from_kuid(&init_user_ns, 10000004);
| | |_______________________
| | } |
| | |
| |> posix_acl_getxattr_idmapped_mnt() |
| { |
| V
| 10000004 = make_kuid(&init_user_ns, 10000004);
| 10000004 = mapped_kuid_fs(&init_user_ns /* no idmapped mount */, 10000004);
| 10000004 = from_kuid(&init_user_ns, 10000004);
| } |_________________________________________________
| |
| |
|> posix_acl_fix_xattr_to_user() |
{ V
10000004 = make_kuid(0:0:4k /* init_user_ns */, 10000004);
/* SUCCESS */
4 = from_kuid(0:10000000:65536 /* caller's idmapping */, 10000004);
}
And similarly if the user chooses option (1) and mounted overayfs on top of
idmapped mounts inside the container:
idmapped mount /vol/contpool/merge: 0:10000000:65536
caller's idmapping: 0:10000000:65536
overlayfs idmapping (ofs->creator_cred): 0:10000000:65536
sys_getxattr()
-> path_getxattr()
-> getxattr()
-> do_getxattr()
|> vfs_getxattr()
| |> __vfs_getxattr()
| | -> handler->get == ovl_posix_acl_xattr_get()
| | -> ovl_xattr_get()
| | -> vfs_getxattr()
| | |> __vfs_getxattr()
| | | -> handler->get() /* lower filesystem callback */
| | |> posix_acl_getxattr_idmapped_mnt()
| | {
| | 4 = make_kuid(&init_user_ns, 4);
| | 10000004 = mapped_kuid_fs(0:10000000:65536 /* lower idmapped mount */, 4);
| | 10000004 = from_kuid(&init_user_ns, 10000004);
| | |_______________________
| | } |
| | |
| |> posix_acl_getxattr_idmapped_mnt() |
| { V
| 10000004 = make_kuid(&init_user_ns, 10000004);
| 10000004 = mapped_kuid_fs(&init_user_ns /* no idmapped mount */, 10000004);
| 10000004 = from_kuid(0(&init_user_ns, 10000004);
| |_________________________________________________
| } |
| |
|> posix_acl_fix_xattr_to_user() |
{ V
10000004 = make_kuid(0:0:4k /* init_user_ns */, 10000004);
/* SUCCESS */
4 = from_kuid(0:10000000:65536 /* caller's idmappings */, 10000004);
}
The last remaining problem we need to fix here is ovl_get_acl(). During
ovl_permission() overlayfs will call:
ovl_permission()
-> generic_permission()
-> acl_permission_check()
-> check_acl()
-> get_acl()
-> inode->i_op->get_acl() == ovl_get_acl()
> get_acl() /* on the underlying filesystem)
->inode->i_op->get_acl() == /*lower filesystem callback */
-> posix_acl_permission()
passing through the get_acl request to the underlying filesystem. This will
retrieve the acls stored in the lower filesystem without taking the idmapping
of the underlying mount into account as this would mean altering the cached
values for the lower filesystem. So we block using ACLs for now until we
decided on a nice way to fix this. Note this limitation both in the
documentation and in the code.
The most straightforward solution would be to have ovl_get_acl() simply
duplicate the ACLs, update the values according to the idmapped mount and
return it to acl_permission_check() so it can be used in posix_acl_permission()
forgetting them afterwards. This is a bit heavy handed but fairly
straightforward otherwise.
Link: https://github.com/brauner/mount-idmapped/issues/9
Link: https://lore.kernel.org/r/20220708090134.385160-2-brauner@kernel.org
Cc: Seth Forshee <sforshee@digitalocean.com>
Cc: Amir Goldstein <amir73il@gmail.com>
Cc: Vivek Goyal <vgoyal@redhat.com>
Cc: Christoph Hellwig <hch@lst.de>
Cc: Aleksa Sarai <cyphar@cyphar.com>
Cc: Miklos Szeredi <mszeredi@redhat.com>
Cc: linux-unionfs@vger.kernel.org
Cc: linux-fsdevel@vger.kernel.org
Reviewed-by: Seth Forshee <sforshee@digitalocean.com>
Signed-off-by: Christian Brauner (Microsoft) <brauner@kernel.org>
2022-07-07 00:30:59 +08:00
|
|
|
|
|
|
|
count = posix_acl_xattr_count(size);
|
|
|
|
if (count < 0)
|
|
|
|
return -EINVAL;
|
|
|
|
if (count == 0)
|
2022-08-29 20:38:41 +08:00
|
|
|
return 0;
|
acl: move idmapped mount fixup into vfs_{g,s}etxattr()
This cycle we added support for mounting overlayfs on top of idmapped mounts.
Recently I've started looking into potential corner cases when trying to add
additional tests and I noticed that reporting for POSIX ACLs is currently wrong
when using idmapped layers with overlayfs mounted on top of it.
I'm going to give a rather detailed explanation to both the origin of the
problem and the solution.
Let's assume the user creates the following directory layout and they have a
rootfs /var/lib/lxc/c1/rootfs. The files in this rootfs are owned as you would
expect files on your host system to be owned. For example, ~/.bashrc for your
regular user would be owned by 1000:1000 and /root/.bashrc would be owned by
0:0. IOW, this is just regular boring filesystem tree on an ext4 or xfs
filesystem.
The user chooses to set POSIX ACLs using the setfacl binary granting the user
with uid 4 read, write, and execute permissions for their .bashrc file:
setfacl -m u:4:rwx /var/lib/lxc/c2/rootfs/home/ubuntu/.bashrc
Now they to expose the whole rootfs to a container using an idmapped mount. So
they first create:
mkdir -pv /vol/contpool/{ctrover,merge,lowermap,overmap}
mkdir -pv /vol/contpool/ctrover/{over,work}
chown 10000000:10000000 /vol/contpool/ctrover/{over,work}
The user now creates an idmapped mount for the rootfs:
mount-idmapped/mount-idmapped --map-mount=b:0:10000000:65536 \
/var/lib/lxc/c2/rootfs \
/vol/contpool/lowermap
This for example makes it so that /var/lib/lxc/c2/rootfs/home/ubuntu/.bashrc
which is owned by uid and gid 1000 as being owned by uid and gid 10001000 at
/vol/contpool/lowermap/home/ubuntu/.bashrc.
Assume the user wants to expose these idmapped mounts through an overlayfs
mount to a container.
mount -t overlay overlay \
-o lowerdir=/vol/contpool/lowermap, \
upperdir=/vol/contpool/overmap/over, \
workdir=/vol/contpool/overmap/work \
/vol/contpool/merge
The user can do this in two ways:
(1) Mount overlayfs in the initial user namespace and expose it to the
container.
(2) Mount overlayfs on top of the idmapped mounts inside of the container's
user namespace.
Let's assume the user chooses the (1) option and mounts overlayfs on the host
and then changes into a container which uses the idmapping 0:10000000:65536
which is the same used for the two idmapped mounts.
Now the user tries to retrieve the POSIX ACLs using the getfacl command
getfacl -n /vol/contpool/lowermap/home/ubuntu/.bashrc
and to their surprise they see:
# file: vol/contpool/merge/home/ubuntu/.bashrc
# owner: 1000
# group: 1000
user::rw-
user:4294967295:rwx
group::r--
mask::rwx
other::r--
indicating the the uid wasn't correctly translated according to the idmapped
mount. The problem is how we currently translate POSIX ACLs. Let's inspect the
callchain in this example:
idmapped mount /vol/contpool/merge: 0:10000000:65536
caller's idmapping: 0:10000000:65536
overlayfs idmapping (ofs->creator_cred): 0:0:4k /* initial idmapping */
sys_getxattr()
-> path_getxattr()
-> getxattr()
-> do_getxattr()
|> vfs_getxattr()
| -> __vfs_getxattr()
| -> handler->get == ovl_posix_acl_xattr_get()
| -> ovl_xattr_get()
| -> vfs_getxattr()
| -> __vfs_getxattr()
| -> handler->get() /* lower filesystem callback */
|> posix_acl_fix_xattr_to_user()
{
4 = make_kuid(&init_user_ns, 4);
4 = mapped_kuid_fs(&init_user_ns /* no idmapped mount */, 4);
/* FAILURE */
-1 = from_kuid(0:10000000:65536 /* caller's idmapping */, 4);
}
If the user chooses to use option (2) and mounts overlayfs on top of idmapped
mounts inside the container things don't look that much better:
idmapped mount /vol/contpool/merge: 0:10000000:65536
caller's idmapping: 0:10000000:65536
overlayfs idmapping (ofs->creator_cred): 0:10000000:65536
sys_getxattr()
-> path_getxattr()
-> getxattr()
-> do_getxattr()
|> vfs_getxattr()
| -> __vfs_getxattr()
| -> handler->get == ovl_posix_acl_xattr_get()
| -> ovl_xattr_get()
| -> vfs_getxattr()
| -> __vfs_getxattr()
| -> handler->get() /* lower filesystem callback */
|> posix_acl_fix_xattr_to_user()
{
4 = make_kuid(&init_user_ns, 4);
4 = mapped_kuid_fs(&init_user_ns, 4);
/* FAILURE */
-1 = from_kuid(0:10000000:65536 /* caller's idmapping */, 4);
}
As is easily seen the problem arises because the idmapping of the lower mount
isn't taken into account as all of this happens in do_gexattr(). But
do_getxattr() is always called on an overlayfs mount and inode and thus cannot
possible take the idmapping of the lower layers into account.
This problem is similar for fscaps but there the translation happens as part of
vfs_getxattr() already. Let's walk through an fscaps overlayfs callchain:
setcap 'cap_net_raw+ep' /var/lib/lxc/c2/rootfs/home/ubuntu/.bashrc
The expected outcome here is that we'll receive the cap_net_raw capability as
we are able to map the uid associated with the fscap to 0 within our container.
IOW, we want to see 0 as the result of the idmapping translations.
If the user chooses option (1) we get the following callchain for fscaps:
idmapped mount /vol/contpool/merge: 0:10000000:65536
caller's idmapping: 0:10000000:65536
overlayfs idmapping (ofs->creator_cred): 0:0:4k /* initial idmapping */
sys_getxattr()
-> path_getxattr()
-> getxattr()
-> do_getxattr()
-> vfs_getxattr()
-> xattr_getsecurity()
-> security_inode_getsecurity() ________________________________
-> cap_inode_getsecurity() | |
{ V |
10000000 = make_kuid(0:0:4k /* overlayfs idmapping */, 10000000); |
10000000 = mapped_kuid_fs(0:0:4k /* no idmapped mount */, 10000000); |
/* Expected result is 0 and thus that we own the fscap. */ |
0 = from_kuid(0:10000000:65536 /* caller's idmapping */, 10000000); |
} |
-> vfs_getxattr_alloc() |
-> handler->get == ovl_other_xattr_get() |
-> vfs_getxattr() |
-> xattr_getsecurity() |
-> security_inode_getsecurity() |
-> cap_inode_getsecurity() |
{ |
0 = make_kuid(0:0:4k /* lower s_user_ns */, 0); |
10000000 = mapped_kuid_fs(0:10000000:65536 /* idmapped mount */, 0); |
10000000 = from_kuid(0:0:4k /* overlayfs idmapping */, 10000000); |
|____________________________________________________________________|
}
-> vfs_getxattr_alloc()
-> handler->get == /* lower filesystem callback */
And if the user chooses option (2) we get:
idmapped mount /vol/contpool/merge: 0:10000000:65536
caller's idmapping: 0:10000000:65536
overlayfs idmapping (ofs->creator_cred): 0:10000000:65536
sys_getxattr()
-> path_getxattr()
-> getxattr()
-> do_getxattr()
-> vfs_getxattr()
-> xattr_getsecurity()
-> security_inode_getsecurity() _______________________________
-> cap_inode_getsecurity() | |
{ V |
10000000 = make_kuid(0:10000000:65536 /* overlayfs idmapping */, 0); |
10000000 = mapped_kuid_fs(0:0:4k /* no idmapped mount */, 10000000); |
/* Expected result is 0 and thus that we own the fscap. */ |
0 = from_kuid(0:10000000:65536 /* caller's idmapping */, 10000000); |
} |
-> vfs_getxattr_alloc() |
-> handler->get == ovl_other_xattr_get() |
|-> vfs_getxattr() |
-> xattr_getsecurity() |
-> security_inode_getsecurity() |
-> cap_inode_getsecurity() |
{ |
0 = make_kuid(0:0:4k /* lower s_user_ns */, 0); |
10000000 = mapped_kuid_fs(0:10000000:65536 /* idmapped mount */, 0); |
0 = from_kuid(0:10000000:65536 /* overlayfs idmapping */, 10000000); |
|____________________________________________________________________|
}
-> vfs_getxattr_alloc()
-> handler->get == /* lower filesystem callback */
We can see how the translation happens correctly in those cases as the
conversion happens within the vfs_getxattr() helper.
For POSIX ACLs we need to do something similar. However, in contrast to fscaps
we cannot apply the fix directly to the kernel internal posix acl data
structure as this would alter the cached values and would also require a rework
of how we currently deal with POSIX ACLs in general which almost never take the
filesystem idmapping into account (the noteable exception being FUSE but even
there the implementation is special) and instead retrieve the raw values based
on the initial idmapping.
The correct values are then generated right before returning to userspace. The
fix for this is to move taking the mount's idmapping into account directly in
vfs_getxattr() instead of having it be part of posix_acl_fix_xattr_to_user().
To this end we split out two small and unexported helpers
posix_acl_getxattr_idmapped_mnt() and posix_acl_setxattr_idmapped_mnt(). The
former to be called in vfs_getxattr() and the latter to be called in
vfs_setxattr().
Let's go back to the original example. Assume the user chose option (1) and
mounted overlayfs on top of idmapped mounts on the host:
idmapped mount /vol/contpool/merge: 0:10000000:65536
caller's idmapping: 0:10000000:65536
overlayfs idmapping (ofs->creator_cred): 0:0:4k /* initial idmapping */
sys_getxattr()
-> path_getxattr()
-> getxattr()
-> do_getxattr()
|> vfs_getxattr()
| |> __vfs_getxattr()
| | -> handler->get == ovl_posix_acl_xattr_get()
| | -> ovl_xattr_get()
| | -> vfs_getxattr()
| | |> __vfs_getxattr()
| | | -> handler->get() /* lower filesystem callback */
| | |> posix_acl_getxattr_idmapped_mnt()
| | {
| | 4 = make_kuid(&init_user_ns, 4);
| | 10000004 = mapped_kuid_fs(0:10000000:65536 /* lower idmapped mount */, 4);
| | 10000004 = from_kuid(&init_user_ns, 10000004);
| | |_______________________
| | } |
| | |
| |> posix_acl_getxattr_idmapped_mnt() |
| { |
| V
| 10000004 = make_kuid(&init_user_ns, 10000004);
| 10000004 = mapped_kuid_fs(&init_user_ns /* no idmapped mount */, 10000004);
| 10000004 = from_kuid(&init_user_ns, 10000004);
| } |_________________________________________________
| |
| |
|> posix_acl_fix_xattr_to_user() |
{ V
10000004 = make_kuid(0:0:4k /* init_user_ns */, 10000004);
/* SUCCESS */
4 = from_kuid(0:10000000:65536 /* caller's idmapping */, 10000004);
}
And similarly if the user chooses option (1) and mounted overayfs on top of
idmapped mounts inside the container:
idmapped mount /vol/contpool/merge: 0:10000000:65536
caller's idmapping: 0:10000000:65536
overlayfs idmapping (ofs->creator_cred): 0:10000000:65536
sys_getxattr()
-> path_getxattr()
-> getxattr()
-> do_getxattr()
|> vfs_getxattr()
| |> __vfs_getxattr()
| | -> handler->get == ovl_posix_acl_xattr_get()
| | -> ovl_xattr_get()
| | -> vfs_getxattr()
| | |> __vfs_getxattr()
| | | -> handler->get() /* lower filesystem callback */
| | |> posix_acl_getxattr_idmapped_mnt()
| | {
| | 4 = make_kuid(&init_user_ns, 4);
| | 10000004 = mapped_kuid_fs(0:10000000:65536 /* lower idmapped mount */, 4);
| | 10000004 = from_kuid(&init_user_ns, 10000004);
| | |_______________________
| | } |
| | |
| |> posix_acl_getxattr_idmapped_mnt() |
| { V
| 10000004 = make_kuid(&init_user_ns, 10000004);
| 10000004 = mapped_kuid_fs(&init_user_ns /* no idmapped mount */, 10000004);
| 10000004 = from_kuid(0(&init_user_ns, 10000004);
| |_________________________________________________
| } |
| |
|> posix_acl_fix_xattr_to_user() |
{ V
10000004 = make_kuid(0:0:4k /* init_user_ns */, 10000004);
/* SUCCESS */
4 = from_kuid(0:10000000:65536 /* caller's idmappings */, 10000004);
}
The last remaining problem we need to fix here is ovl_get_acl(). During
ovl_permission() overlayfs will call:
ovl_permission()
-> generic_permission()
-> acl_permission_check()
-> check_acl()
-> get_acl()
-> inode->i_op->get_acl() == ovl_get_acl()
> get_acl() /* on the underlying filesystem)
->inode->i_op->get_acl() == /*lower filesystem callback */
-> posix_acl_permission()
passing through the get_acl request to the underlying filesystem. This will
retrieve the acls stored in the lower filesystem without taking the idmapping
of the underlying mount into account as this would mean altering the cached
values for the lower filesystem. So we block using ACLs for now until we
decided on a nice way to fix this. Note this limitation both in the
documentation and in the code.
The most straightforward solution would be to have ovl_get_acl() simply
duplicate the ACLs, update the values according to the idmapped mount and
return it to acl_permission_check() so it can be used in posix_acl_permission()
forgetting them afterwards. This is a bit heavy handed but fairly
straightforward otherwise.
Link: https://github.com/brauner/mount-idmapped/issues/9
Link: https://lore.kernel.org/r/20220708090134.385160-2-brauner@kernel.org
Cc: Seth Forshee <sforshee@digitalocean.com>
Cc: Amir Goldstein <amir73il@gmail.com>
Cc: Vivek Goyal <vgoyal@redhat.com>
Cc: Christoph Hellwig <hch@lst.de>
Cc: Aleksa Sarai <cyphar@cyphar.com>
Cc: Miklos Szeredi <mszeredi@redhat.com>
Cc: linux-unionfs@vger.kernel.org
Cc: linux-fsdevel@vger.kernel.org
Reviewed-by: Seth Forshee <sforshee@digitalocean.com>
Signed-off-by: Christian Brauner (Microsoft) <brauner@kernel.org>
2022-07-07 00:30:59 +08:00
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
acl: add vfs_set_acl_prepare()
Various filesystems store POSIX ACLs on the backing store in their uapi
format. Such filesystems need to translate from the uapi POSIX ACL
format into the VFS format during i_op->get_acl(). The VFS provides the
posix_acl_from_xattr() helper for this task.
But the usage of posix_acl_from_xattr() is currently ambiguous. It is
intended to transform from a uapi POSIX ACL to the VFS represenation.
For example, when retrieving POSIX ACLs for permission checking during
lookup or when calling getxattr() to retrieve system.posix_acl_{access,default}.
Calling posix_acl_from_xattr() during i_op->get_acl() will map the raw
{g,u}id values stored as ACL_{GROUP,USER} entries in the uapi POSIX ACL
format into k{g,u}id_t in the filesystem's idmapping and return a struct
posix_acl ready to be returned to the VFS for caching and to perform
permission checks on.
However, posix_acl_from_xattr() is also called during setxattr() for all
filesystems that rely on VFS provides posix_acl_{access,default}_xattr_handler.
The posix_acl_xattr_set() handler which is used for the ->set() method
of posix_acl_{access,default}_xattr_handler uses posix_acl_from_xattr()
to translate from the uapi POSIX ACL format to the VFS format so that it
can be passed to the i_op->set_acl() handler of the filesystem or for
direct caching in case no i_op->set_acl() handler is defined.
During setxattr() the {g,u}id values stored as ACL_{GROUP,USER} entries
in the uapi POSIX ACL format aren't raw {g,u}id values that need to be
mapped according to the filesystem's idmapping. Instead they are {g,u}id
values in the caller's idmapping which have been generated during
posix_acl_fix_xattr_from_user(). In other words, they are k{g,u}id_t
which are passed as raw {g,u}id values abusing the uapi POSIX ACL format
(Please note that this type safety violation has existed since the
introduction of k{g,u}id_t. Please see [1] for more details.).
So when posix_acl_from_xattr() is called in posix_acl_xattr_set() the
filesystem idmapping is completely irrelevant. Instead, we abuse the
initial idmapping to recover the k{g,u}id_t base on the value stored in
raw {g,u}id as ACL_{GROUP,USER} in the uapi POSIX ACL format.
We need to clearly distinguish betweeen these two operations as it is
really easy to confuse for filesystems as can be seen in ntfs3.
In order to do this we factor out make_posix_acl() which takes callbacks
allowing callers to pass dedicated methods to generate the correct
k{g,u}id_t. This is just an internal static helper which is not exposed
to any filesystems but it neatly encapsulates the basic logic of walking
through a uapi POSIX ACL and returning an allocated VFS POSIX ACL with
the correct k{g,u}id_t values.
The posix_acl_from_xattr() helper can then be implemented as a simple
call to make_posix_acl() with callbacks that generate the correct
k{g,u}id_t from the raw {g,u}id values in ACL_{GROUP,USER} entries in
the uapi POSIX ACL format as read from the backing store.
For setxattr() we add a new helper vfs_set_acl_prepare() which has
callbacks to map the POSIX ACLs from the uapi format with the k{g,u}id_t
values stored in raw {g,u}id format in ACL_{GROUP,USER} entries into the
correct k{g,u}id_t values in the filesystem idmapping. In contrast to
posix_acl_from_xattr() the vfs_set_acl_prepare() helper needs to take
the mount idmapping into account. The differences are explained in more
detail in the kernel doc for the new functions.
In follow up patches we will remove all abuses of posix_acl_from_xattr()
for setxattr() operations and replace it with calls to vfs_set_acl_prepare().
The new vfs_set_acl_prepare() helper allows us to deal with the
ambiguity in how the POSI ACL uapi struct stores {g,u}id values
depending on whether this is a getxattr() or setxattr() operation.
This also allows us to remove the posix_acl_setxattr_idmapped_mnt()
helper reducing the abuse of the POSIX ACL uapi format to pass values
that should be distinct types in {g,u}id values stored as
ACL_{GROUP,USER} entries.
The removal of posix_acl_setxattr_idmapped_mnt() in turn allows us to
re-constify the value parameter of vfs_setxattr() which in turn allows
us to avoid the nasty cast from a const void pointer to a non-const void
pointer on ovl_do_setxattr().
Ultimately, the plan is to get rid of the type violations completely and
never pass the values from k{g,u}id_t as raw {g,u}id in ACL_{GROUP,USER}
entries in uapi POSIX ACL format. But that's a longer way to go and this
is a preparatory step.
Link: https://lore.kernel.org/all/20220801145520.1532837-1-brauner@kernel.org [1]
Co-Developed-by: Seth Forshee <sforshee@digitalocean.com>
Signed-off-by: Christian Brauner (Microsoft) <brauner@kernel.org>
2022-08-29 20:38:42 +08:00
|
|
|
/**
|
2022-09-22 23:17:27 +08:00
|
|
|
* posix_acl_from_xattr - convert POSIX ACLs from backing store to VFS format
|
|
|
|
* @userns: the filesystem's idmapping
|
acl: add vfs_set_acl_prepare()
Various filesystems store POSIX ACLs on the backing store in their uapi
format. Such filesystems need to translate from the uapi POSIX ACL
format into the VFS format during i_op->get_acl(). The VFS provides the
posix_acl_from_xattr() helper for this task.
But the usage of posix_acl_from_xattr() is currently ambiguous. It is
intended to transform from a uapi POSIX ACL to the VFS represenation.
For example, when retrieving POSIX ACLs for permission checking during
lookup or when calling getxattr() to retrieve system.posix_acl_{access,default}.
Calling posix_acl_from_xattr() during i_op->get_acl() will map the raw
{g,u}id values stored as ACL_{GROUP,USER} entries in the uapi POSIX ACL
format into k{g,u}id_t in the filesystem's idmapping and return a struct
posix_acl ready to be returned to the VFS for caching and to perform
permission checks on.
However, posix_acl_from_xattr() is also called during setxattr() for all
filesystems that rely on VFS provides posix_acl_{access,default}_xattr_handler.
The posix_acl_xattr_set() handler which is used for the ->set() method
of posix_acl_{access,default}_xattr_handler uses posix_acl_from_xattr()
to translate from the uapi POSIX ACL format to the VFS format so that it
can be passed to the i_op->set_acl() handler of the filesystem or for
direct caching in case no i_op->set_acl() handler is defined.
During setxattr() the {g,u}id values stored as ACL_{GROUP,USER} entries
in the uapi POSIX ACL format aren't raw {g,u}id values that need to be
mapped according to the filesystem's idmapping. Instead they are {g,u}id
values in the caller's idmapping which have been generated during
posix_acl_fix_xattr_from_user(). In other words, they are k{g,u}id_t
which are passed as raw {g,u}id values abusing the uapi POSIX ACL format
(Please note that this type safety violation has existed since the
introduction of k{g,u}id_t. Please see [1] for more details.).
So when posix_acl_from_xattr() is called in posix_acl_xattr_set() the
filesystem idmapping is completely irrelevant. Instead, we abuse the
initial idmapping to recover the k{g,u}id_t base on the value stored in
raw {g,u}id as ACL_{GROUP,USER} in the uapi POSIX ACL format.
We need to clearly distinguish betweeen these two operations as it is
really easy to confuse for filesystems as can be seen in ntfs3.
In order to do this we factor out make_posix_acl() which takes callbacks
allowing callers to pass dedicated methods to generate the correct
k{g,u}id_t. This is just an internal static helper which is not exposed
to any filesystems but it neatly encapsulates the basic logic of walking
through a uapi POSIX ACL and returning an allocated VFS POSIX ACL with
the correct k{g,u}id_t values.
The posix_acl_from_xattr() helper can then be implemented as a simple
call to make_posix_acl() with callbacks that generate the correct
k{g,u}id_t from the raw {g,u}id values in ACL_{GROUP,USER} entries in
the uapi POSIX ACL format as read from the backing store.
For setxattr() we add a new helper vfs_set_acl_prepare() which has
callbacks to map the POSIX ACLs from the uapi format with the k{g,u}id_t
values stored in raw {g,u}id format in ACL_{GROUP,USER} entries into the
correct k{g,u}id_t values in the filesystem idmapping. In contrast to
posix_acl_from_xattr() the vfs_set_acl_prepare() helper needs to take
the mount idmapping into account. The differences are explained in more
detail in the kernel doc for the new functions.
In follow up patches we will remove all abuses of posix_acl_from_xattr()
for setxattr() operations and replace it with calls to vfs_set_acl_prepare().
The new vfs_set_acl_prepare() helper allows us to deal with the
ambiguity in how the POSI ACL uapi struct stores {g,u}id values
depending on whether this is a getxattr() or setxattr() operation.
This also allows us to remove the posix_acl_setxattr_idmapped_mnt()
helper reducing the abuse of the POSIX ACL uapi format to pass values
that should be distinct types in {g,u}id values stored as
ACL_{GROUP,USER} entries.
The removal of posix_acl_setxattr_idmapped_mnt() in turn allows us to
re-constify the value parameter of vfs_setxattr() which in turn allows
us to avoid the nasty cast from a const void pointer to a non-const void
pointer on ovl_do_setxattr().
Ultimately, the plan is to get rid of the type violations completely and
never pass the values from k{g,u}id_t as raw {g,u}id in ACL_{GROUP,USER}
entries in uapi POSIX ACL format. But that's a longer way to go and this
is a preparatory step.
Link: https://lore.kernel.org/all/20220801145520.1532837-1-brauner@kernel.org [1]
Co-Developed-by: Seth Forshee <sforshee@digitalocean.com>
Signed-off-by: Christian Brauner (Microsoft) <brauner@kernel.org>
2022-08-29 20:38:42 +08:00
|
|
|
* @value: the uapi representation of POSIX ACLs
|
|
|
|
* @size: the size of @void
|
|
|
|
*
|
2022-09-22 23:17:27 +08:00
|
|
|
* Filesystems that store POSIX ACLs in the unaltered uapi format should use
|
|
|
|
* posix_acl_from_xattr() when reading them from the backing store and
|
|
|
|
* converting them into the struct posix_acl VFS format. The helper is
|
|
|
|
* specifically intended to be called from the acl inode operation.
|
|
|
|
*
|
|
|
|
* The posix_acl_from_xattr() function will map the raw {g,u}id values stored
|
|
|
|
* in ACL_{GROUP,USER} entries into idmapping in @userns.
|
|
|
|
*
|
|
|
|
* Note that posix_acl_from_xattr() does not take idmapped mounts into account.
|
|
|
|
* If it did it calling it from the get acl inode operation would return POSIX
|
|
|
|
* ACLs mapped according to an idmapped mount which would mean that the value
|
|
|
|
* couldn't be cached for the filesystem. Idmapped mounts are taken into
|
|
|
|
* account on the fly during permission checking or right at the VFS -
|
|
|
|
* userspace boundary before reporting them to the user.
|
acl: add vfs_set_acl_prepare()
Various filesystems store POSIX ACLs on the backing store in their uapi
format. Such filesystems need to translate from the uapi POSIX ACL
format into the VFS format during i_op->get_acl(). The VFS provides the
posix_acl_from_xattr() helper for this task.
But the usage of posix_acl_from_xattr() is currently ambiguous. It is
intended to transform from a uapi POSIX ACL to the VFS represenation.
For example, when retrieving POSIX ACLs for permission checking during
lookup or when calling getxattr() to retrieve system.posix_acl_{access,default}.
Calling posix_acl_from_xattr() during i_op->get_acl() will map the raw
{g,u}id values stored as ACL_{GROUP,USER} entries in the uapi POSIX ACL
format into k{g,u}id_t in the filesystem's idmapping and return a struct
posix_acl ready to be returned to the VFS for caching and to perform
permission checks on.
However, posix_acl_from_xattr() is also called during setxattr() for all
filesystems that rely on VFS provides posix_acl_{access,default}_xattr_handler.
The posix_acl_xattr_set() handler which is used for the ->set() method
of posix_acl_{access,default}_xattr_handler uses posix_acl_from_xattr()
to translate from the uapi POSIX ACL format to the VFS format so that it
can be passed to the i_op->set_acl() handler of the filesystem or for
direct caching in case no i_op->set_acl() handler is defined.
During setxattr() the {g,u}id values stored as ACL_{GROUP,USER} entries
in the uapi POSIX ACL format aren't raw {g,u}id values that need to be
mapped according to the filesystem's idmapping. Instead they are {g,u}id
values in the caller's idmapping which have been generated during
posix_acl_fix_xattr_from_user(). In other words, they are k{g,u}id_t
which are passed as raw {g,u}id values abusing the uapi POSIX ACL format
(Please note that this type safety violation has existed since the
introduction of k{g,u}id_t. Please see [1] for more details.).
So when posix_acl_from_xattr() is called in posix_acl_xattr_set() the
filesystem idmapping is completely irrelevant. Instead, we abuse the
initial idmapping to recover the k{g,u}id_t base on the value stored in
raw {g,u}id as ACL_{GROUP,USER} in the uapi POSIX ACL format.
We need to clearly distinguish betweeen these two operations as it is
really easy to confuse for filesystems as can be seen in ntfs3.
In order to do this we factor out make_posix_acl() which takes callbacks
allowing callers to pass dedicated methods to generate the correct
k{g,u}id_t. This is just an internal static helper which is not exposed
to any filesystems but it neatly encapsulates the basic logic of walking
through a uapi POSIX ACL and returning an allocated VFS POSIX ACL with
the correct k{g,u}id_t values.
The posix_acl_from_xattr() helper can then be implemented as a simple
call to make_posix_acl() with callbacks that generate the correct
k{g,u}id_t from the raw {g,u}id values in ACL_{GROUP,USER} entries in
the uapi POSIX ACL format as read from the backing store.
For setxattr() we add a new helper vfs_set_acl_prepare() which has
callbacks to map the POSIX ACLs from the uapi format with the k{g,u}id_t
values stored in raw {g,u}id format in ACL_{GROUP,USER} entries into the
correct k{g,u}id_t values in the filesystem idmapping. In contrast to
posix_acl_from_xattr() the vfs_set_acl_prepare() helper needs to take
the mount idmapping into account. The differences are explained in more
detail in the kernel doc for the new functions.
In follow up patches we will remove all abuses of posix_acl_from_xattr()
for setxattr() operations and replace it with calls to vfs_set_acl_prepare().
The new vfs_set_acl_prepare() helper allows us to deal with the
ambiguity in how the POSI ACL uapi struct stores {g,u}id values
depending on whether this is a getxattr() or setxattr() operation.
This also allows us to remove the posix_acl_setxattr_idmapped_mnt()
helper reducing the abuse of the POSIX ACL uapi format to pass values
that should be distinct types in {g,u}id values stored as
ACL_{GROUP,USER} entries.
The removal of posix_acl_setxattr_idmapped_mnt() in turn allows us to
re-constify the value parameter of vfs_setxattr() which in turn allows
us to avoid the nasty cast from a const void pointer to a non-const void
pointer on ovl_do_setxattr().
Ultimately, the plan is to get rid of the type violations completely and
never pass the values from k{g,u}id_t as raw {g,u}id in ACL_{GROUP,USER}
entries in uapi POSIX ACL format. But that's a longer way to go and this
is a preparatory step.
Link: https://lore.kernel.org/all/20220801145520.1532837-1-brauner@kernel.org [1]
Co-Developed-by: Seth Forshee <sforshee@digitalocean.com>
Signed-off-by: Christian Brauner (Microsoft) <brauner@kernel.org>
2022-08-29 20:38:42 +08:00
|
|
|
*
|
|
|
|
* Return: Allocated struct posix_acl on success, NULL for a valid header but
|
|
|
|
* without actual POSIX ACL entries, or ERR_PTR() encoded error code.
|
2013-12-20 21:16:37 +08:00
|
|
|
*/
|
2022-09-22 23:17:27 +08:00
|
|
|
struct posix_acl *posix_acl_from_xattr(struct user_namespace *userns,
|
|
|
|
const void *value, size_t size)
|
2013-12-20 21:16:37 +08:00
|
|
|
{
|
2016-09-27 19:03:22 +08:00
|
|
|
const struct posix_acl_xattr_header *header = value;
|
|
|
|
const struct posix_acl_xattr_entry *entry = (const void *)(header + 1), *end;
|
2013-12-20 21:16:37 +08:00
|
|
|
int count;
|
|
|
|
struct posix_acl *acl;
|
|
|
|
struct posix_acl_entry *acl_e;
|
|
|
|
|
2022-08-29 20:38:41 +08:00
|
|
|
count = posix_acl_fix_xattr_common(value, size);
|
2013-12-20 21:16:37 +08:00
|
|
|
if (count < 0)
|
2022-08-29 20:38:41 +08:00
|
|
|
return ERR_PTR(count);
|
2013-12-20 21:16:37 +08:00
|
|
|
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:
|
2022-09-22 23:17:27 +08:00
|
|
|
acl_e->e_uid = make_kuid(userns,
|
|
|
|
le32_to_cpu(entry->e_id));
|
2013-12-20 21:16:37 +08:00
|
|
|
if (!uid_valid(acl_e->e_uid))
|
|
|
|
goto fail;
|
|
|
|
break;
|
|
|
|
case ACL_GROUP:
|
2022-09-22 23:17:27 +08:00
|
|
|
acl_e->e_gid = make_kgid(userns,
|
|
|
|
le32_to_cpu(entry->e_id));
|
2013-12-20 21:16:37 +08:00
|
|
|
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)
|
|
|
|
{
|
2016-09-27 19:03:22 +08:00
|
|
|
struct posix_acl_xattr_header *ext_acl = buffer;
|
|
|
|
struct posix_acl_xattr_entry *ext_entry;
|
2013-12-20 21:16:37 +08:00
|
|
|
int real_size, n;
|
|
|
|
|
|
|
|
real_size = posix_acl_xattr_size(acl->a_count);
|
|
|
|
if (!buffer)
|
|
|
|
return real_size;
|
|
|
|
if (real_size > size)
|
|
|
|
return -ERANGE;
|
2014-02-14 17:05:49 +08:00
|
|
|
|
2016-09-27 19:03:22 +08:00
|
|
|
ext_entry = (void *)(ext_acl + 1);
|
2013-12-20 21:16:37 +08:00
|
|
|
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);
|
2013-12-20 21:16:40 +08:00
|
|
|
|
2022-09-22 23:17:13 +08:00
|
|
|
/**
|
|
|
|
* vfs_posix_acl_to_xattr - convert from kernel to userspace representation
|
2022-10-28 15:56:20 +08:00
|
|
|
* @idmap: idmap of the mount
|
2022-09-22 23:17:13 +08:00
|
|
|
* @inode: inode the posix acls are set on
|
|
|
|
* @acl: the posix acls as represented by the vfs
|
|
|
|
* @buffer: the buffer into which to convert @acl
|
|
|
|
* @size: size of @buffer
|
|
|
|
*
|
|
|
|
* This converts @acl from the VFS representation in the filesystem idmapping
|
|
|
|
* to the uapi form reportable to userspace. And mount and caller idmappings
|
|
|
|
* are handled appropriately.
|
|
|
|
*
|
|
|
|
* Return: On success, the size of the stored uapi posix acls, on error a
|
|
|
|
* negative errno.
|
|
|
|
*/
|
2022-10-28 15:56:20 +08:00
|
|
|
static ssize_t vfs_posix_acl_to_xattr(struct mnt_idmap *idmap,
|
2022-10-28 16:00:26 +08:00
|
|
|
struct inode *inode,
|
|
|
|
const struct posix_acl *acl, void *buffer,
|
|
|
|
size_t size)
|
2022-09-22 23:17:13 +08:00
|
|
|
|
|
|
|
{
|
|
|
|
struct posix_acl_xattr_header *ext_acl = buffer;
|
|
|
|
struct posix_acl_xattr_entry *ext_entry;
|
|
|
|
struct user_namespace *fs_userns, *caller_userns;
|
2022-10-28 15:56:20 +08:00
|
|
|
struct user_namespace *mnt_userns;
|
2022-09-22 23:17:13 +08:00
|
|
|
ssize_t real_size, n;
|
|
|
|
vfsuid_t vfsuid;
|
|
|
|
vfsgid_t vfsgid;
|
|
|
|
|
|
|
|
real_size = posix_acl_xattr_size(acl->a_count);
|
|
|
|
if (!buffer)
|
|
|
|
return real_size;
|
|
|
|
if (real_size > size)
|
|
|
|
return -ERANGE;
|
|
|
|
|
|
|
|
ext_entry = (void *)(ext_acl + 1);
|
|
|
|
ext_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION);
|
|
|
|
|
|
|
|
fs_userns = i_user_ns(inode);
|
|
|
|
caller_userns = current_user_ns();
|
2022-10-28 15:56:20 +08:00
|
|
|
mnt_userns = mnt_idmap_owner(idmap);
|
2022-09-22 23:17:13 +08:00
|
|
|
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:
|
|
|
|
vfsuid = make_vfsuid(mnt_userns, fs_userns, acl_e->e_uid);
|
|
|
|
ext_entry->e_id = cpu_to_le32(from_kuid(
|
|
|
|
caller_userns, vfsuid_into_kuid(vfsuid)));
|
|
|
|
break;
|
|
|
|
case ACL_GROUP:
|
|
|
|
vfsgid = make_vfsgid(mnt_userns, fs_userns, acl_e->e_gid);
|
|
|
|
ext_entry->e_id = cpu_to_le32(from_kgid(
|
|
|
|
caller_userns, vfsgid_into_kgid(vfsgid)));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ext_entry->e_id = cpu_to_le32(ACL_UNDEFINED_ID);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return real_size;
|
|
|
|
}
|
|
|
|
|
2016-06-23 05:57:25 +08:00
|
|
|
int
|
2023-01-13 19:49:20 +08:00
|
|
|
set_posix_acl(struct mnt_idmap *idmap, struct dentry *dentry,
|
2021-01-21 21:19:27 +08:00
|
|
|
int type, struct posix_acl *acl)
|
2013-12-20 21:16:40 +08:00
|
|
|
{
|
2022-09-23 16:29:39 +08:00
|
|
|
struct inode *inode = d_inode(dentry);
|
|
|
|
|
2013-12-20 21:16:40 +08:00
|
|
|
if (!IS_POSIXACL(inode))
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
if (!inode->i_op->set_acl)
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
|
2016-06-23 05:57:25 +08:00
|
|
|
if (type == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode))
|
|
|
|
return acl ? -EACCES : 0;
|
2023-01-13 19:49:26 +08:00
|
|
|
if (!inode_owner_or_capable(idmap, inode))
|
2013-12-20 21:16:40 +08:00
|
|
|
return -EPERM;
|
|
|
|
|
2016-06-23 05:57:25 +08:00
|
|
|
if (acl) {
|
2016-07-30 06:54:19 +08:00
|
|
|
int ret = posix_acl_valid(inode->i_sb->s_user_ns, acl);
|
2016-06-23 05:57:25 +08:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
2023-01-13 19:49:20 +08:00
|
|
|
return inode->i_op->set_acl(idmap, dentry, acl, type);
|
2016-06-23 05:57:25 +08:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(set_posix_acl);
|
|
|
|
|
2015-12-02 21:44:43 +08:00
|
|
|
static bool
|
|
|
|
posix_acl_xattr_list(struct dentry *dentry)
|
2013-12-20 21:16:40 +08:00
|
|
|
{
|
2015-12-02 21:44:43 +08:00
|
|
|
return IS_POSIXACL(d_backing_inode(dentry));
|
2013-12-20 21:16:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const struct xattr_handler posix_acl_access_xattr_handler = {
|
2015-12-02 21:44:36 +08:00
|
|
|
.name = XATTR_NAME_POSIX_ACL_ACCESS,
|
2013-12-20 21:16:40 +08:00
|
|
|
.flags = ACL_TYPE_ACCESS,
|
|
|
|
.list = posix_acl_xattr_list,
|
|
|
|
};
|
|
|
|
EXPORT_SYMBOL_GPL(posix_acl_access_xattr_handler);
|
|
|
|
|
|
|
|
const struct xattr_handler posix_acl_default_xattr_handler = {
|
2015-12-02 21:44:36 +08:00
|
|
|
.name = XATTR_NAME_POSIX_ACL_DEFAULT,
|
2013-12-20 21:16:40 +08:00
|
|
|
.flags = ACL_TYPE_DEFAULT,
|
|
|
|
.list = posix_acl_xattr_list,
|
|
|
|
};
|
|
|
|
EXPORT_SYMBOL_GPL(posix_acl_default_xattr_handler);
|
2013-12-20 21:16:54 +08:00
|
|
|
|
2023-01-13 19:49:20 +08:00
|
|
|
int simple_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,
|
2021-01-21 21:19:43 +08:00
|
|
|
struct posix_acl *acl, int type)
|
2013-12-20 21:16:54 +08:00
|
|
|
{
|
|
|
|
int error;
|
2022-09-23 16:29:39 +08:00
|
|
|
struct inode *inode = d_inode(dentry);
|
2013-12-20 21:16:54 +08:00
|
|
|
|
|
|
|
if (type == ACL_TYPE_ACCESS) {
|
2023-01-13 19:49:24 +08:00
|
|
|
error = posix_acl_update_mode(idmap, inode,
|
2017-01-09 09:34:48 +08:00
|
|
|
&inode->i_mode, &acl);
|
|
|
|
if (error)
|
|
|
|
return error;
|
2013-12-20 21:16:54 +08:00
|
|
|
}
|
|
|
|
|
2016-09-14 22:48:04 +08:00
|
|
|
inode->i_ctime = current_time(inode);
|
2022-09-09 21:00:31 +08:00
|
|
|
if (IS_I_VERSION(inode))
|
|
|
|
inode_inc_iversion(inode);
|
2013-12-20 21:16:54 +08:00
|
|
|
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;
|
|
|
|
}
|
2022-09-22 23:17:06 +08:00
|
|
|
|
2023-01-13 19:49:24 +08:00
|
|
|
static int vfs_set_acl_idmapped_mnt(struct mnt_idmap *idmap,
|
2022-09-22 23:17:06 +08:00
|
|
|
struct user_namespace *fs_userns,
|
|
|
|
struct posix_acl *acl)
|
|
|
|
{
|
2023-01-13 19:49:24 +08:00
|
|
|
struct user_namespace *mnt_userns = mnt_idmap_owner(idmap);
|
|
|
|
|
2022-09-22 23:17:06 +08:00
|
|
|
for (int n = 0; n < acl->a_count; n++) {
|
|
|
|
struct posix_acl_entry *acl_e = &acl->a_entries[n];
|
|
|
|
|
|
|
|
switch (acl_e->e_tag) {
|
|
|
|
case ACL_USER:
|
|
|
|
acl_e->e_uid = from_vfsuid(mnt_userns, fs_userns,
|
|
|
|
VFSUIDT_INIT(acl_e->e_uid));
|
|
|
|
break;
|
|
|
|
case ACL_GROUP:
|
|
|
|
acl_e->e_gid = from_vfsgid(mnt_userns, fs_userns,
|
|
|
|
VFSGIDT_INIT(acl_e->e_gid));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* vfs_set_acl - set posix acls
|
2023-01-13 19:49:20 +08:00
|
|
|
* @idmap: idmap of the mount
|
2022-09-22 23:17:06 +08:00
|
|
|
* @dentry: the dentry based on which to set the posix acls
|
|
|
|
* @acl_name: the name of the posix acl
|
|
|
|
* @kacl: the posix acls in the appropriate VFS format
|
|
|
|
*
|
|
|
|
* This function sets @kacl. The caller must all posix_acl_release() on @kacl
|
|
|
|
* afterwards.
|
|
|
|
*
|
|
|
|
* Return: On success 0, on error negative errno.
|
|
|
|
*/
|
2023-01-13 19:49:20 +08:00
|
|
|
int vfs_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,
|
2022-09-22 23:17:06 +08:00
|
|
|
const char *acl_name, struct posix_acl *kacl)
|
|
|
|
{
|
|
|
|
int acl_type;
|
|
|
|
int error;
|
|
|
|
struct inode *inode = d_inode(dentry);
|
|
|
|
struct inode *delegated_inode = NULL;
|
|
|
|
|
|
|
|
acl_type = posix_acl_type(acl_name);
|
|
|
|
if (acl_type < 0)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (kacl) {
|
|
|
|
/*
|
|
|
|
* If we're on an idmapped mount translate from mount specific
|
|
|
|
* vfs{g,u}id_t into global filesystem k{g,u}id_t.
|
|
|
|
* Afterwards we can cache the POSIX ACLs filesystem wide and -
|
|
|
|
* if this is a filesystem with a backing store - ultimately
|
|
|
|
* translate them to backing store values.
|
|
|
|
*/
|
2023-01-13 19:49:24 +08:00
|
|
|
error = vfs_set_acl_idmapped_mnt(idmap, i_user_ns(inode), kacl);
|
2022-09-22 23:17:06 +08:00
|
|
|
if (error)
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
retry_deleg:
|
|
|
|
inode_lock(inode);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We only care about restrictions the inode struct itself places upon
|
|
|
|
* us otherwise POSIX ACLs aren't subject to any VFS restrictions.
|
|
|
|
*/
|
2023-01-13 19:49:22 +08:00
|
|
|
error = may_write_xattr(idmap, inode);
|
2022-09-22 23:17:06 +08:00
|
|
|
if (error)
|
|
|
|
goto out_inode_unlock;
|
|
|
|
|
2023-01-13 19:49:24 +08:00
|
|
|
error = security_inode_set_acl(idmap, dentry, acl_name, kacl);
|
2022-09-22 23:17:06 +08:00
|
|
|
if (error)
|
|
|
|
goto out_inode_unlock;
|
|
|
|
|
|
|
|
error = try_break_deleg(inode, &delegated_inode);
|
|
|
|
if (error)
|
|
|
|
goto out_inode_unlock;
|
|
|
|
|
|
|
|
if (inode->i_opflags & IOP_XATTR)
|
2023-01-13 19:49:20 +08:00
|
|
|
error = set_posix_acl(idmap, dentry, acl_type, kacl);
|
2022-09-22 23:17:06 +08:00
|
|
|
else if (unlikely(is_bad_inode(inode)))
|
|
|
|
error = -EIO;
|
|
|
|
else
|
|
|
|
error = -EOPNOTSUPP;
|
|
|
|
if (!error) {
|
|
|
|
fsnotify_xattr(dentry);
|
|
|
|
evm_inode_post_set_acl(dentry, acl_name, kacl);
|
|
|
|
}
|
|
|
|
|
|
|
|
out_inode_unlock:
|
|
|
|
inode_unlock(inode);
|
|
|
|
|
|
|
|
if (delegated_inode) {
|
|
|
|
error = break_deleg_wait(&delegated_inode);
|
|
|
|
if (!error)
|
|
|
|
goto retry_deleg;
|
|
|
|
}
|
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(vfs_set_acl);
|
2022-09-22 23:17:13 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* vfs_get_acl - get posix acls
|
2023-01-13 19:49:19 +08:00
|
|
|
* @idmap: idmap of the mount
|
2022-09-22 23:17:13 +08:00
|
|
|
* @dentry: the dentry based on which to retrieve the posix acls
|
|
|
|
* @acl_name: the name of the posix acl
|
|
|
|
*
|
|
|
|
* This function retrieves @kacl from the filesystem. The caller must all
|
|
|
|
* posix_acl_release() on @kacl.
|
|
|
|
*
|
|
|
|
* Return: On success POSIX ACLs in VFS format, on error negative errno.
|
|
|
|
*/
|
2023-01-13 19:49:19 +08:00
|
|
|
struct posix_acl *vfs_get_acl(struct mnt_idmap *idmap,
|
2022-09-22 23:17:13 +08:00
|
|
|
struct dentry *dentry, const char *acl_name)
|
|
|
|
{
|
|
|
|
struct inode *inode = d_inode(dentry);
|
|
|
|
struct posix_acl *acl;
|
|
|
|
int acl_type, error;
|
|
|
|
|
|
|
|
acl_type = posix_acl_type(acl_name);
|
|
|
|
if (acl_type < 0)
|
|
|
|
return ERR_PTR(-EINVAL);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The VFS has no restrictions on reading POSIX ACLs so calling
|
|
|
|
* something like xattr_permission() isn't needed. Only LSMs get a say.
|
|
|
|
*/
|
2023-01-13 19:49:24 +08:00
|
|
|
error = security_inode_get_acl(idmap, dentry, acl_name);
|
2022-09-22 23:17:13 +08:00
|
|
|
if (error)
|
|
|
|
return ERR_PTR(error);
|
|
|
|
|
|
|
|
if (!IS_POSIXACL(inode))
|
|
|
|
return ERR_PTR(-EOPNOTSUPP);
|
|
|
|
if (S_ISLNK(inode->i_mode))
|
|
|
|
return ERR_PTR(-EOPNOTSUPP);
|
|
|
|
|
2023-01-13 19:49:19 +08:00
|
|
|
acl = __get_acl(idmap, dentry, inode, acl_type);
|
2022-09-22 23:17:13 +08:00
|
|
|
if (IS_ERR(acl))
|
|
|
|
return acl;
|
|
|
|
if (!acl)
|
|
|
|
return ERR_PTR(-ENODATA);
|
|
|
|
|
|
|
|
return acl;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(vfs_get_acl);
|
2022-09-22 23:17:14 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* vfs_remove_acl - remove posix acls
|
2023-01-13 19:49:20 +08:00
|
|
|
* @idmap: idmap of the mount
|
2022-09-22 23:17:14 +08:00
|
|
|
* @dentry: the dentry based on which to retrieve the posix acls
|
|
|
|
* @acl_name: the name of the posix acl
|
|
|
|
*
|
|
|
|
* This function removes posix acls.
|
|
|
|
*
|
|
|
|
* Return: On success 0, on error negative errno.
|
|
|
|
*/
|
2023-01-13 19:49:20 +08:00
|
|
|
int vfs_remove_acl(struct mnt_idmap *idmap, struct dentry *dentry,
|
2022-09-22 23:17:14 +08:00
|
|
|
const char *acl_name)
|
|
|
|
{
|
|
|
|
int acl_type;
|
|
|
|
int error;
|
|
|
|
struct inode *inode = d_inode(dentry);
|
|
|
|
struct inode *delegated_inode = NULL;
|
|
|
|
|
|
|
|
acl_type = posix_acl_type(acl_name);
|
|
|
|
if (acl_type < 0)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
retry_deleg:
|
|
|
|
inode_lock(inode);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We only care about restrictions the inode struct itself places upon
|
|
|
|
* us otherwise POSIX ACLs aren't subject to any VFS restrictions.
|
|
|
|
*/
|
2023-01-13 19:49:22 +08:00
|
|
|
error = may_write_xattr(idmap, inode);
|
2022-09-22 23:17:14 +08:00
|
|
|
if (error)
|
|
|
|
goto out_inode_unlock;
|
|
|
|
|
2023-01-13 19:49:24 +08:00
|
|
|
error = security_inode_remove_acl(idmap, dentry, acl_name);
|
2022-09-22 23:17:14 +08:00
|
|
|
if (error)
|
|
|
|
goto out_inode_unlock;
|
|
|
|
|
|
|
|
error = try_break_deleg(inode, &delegated_inode);
|
|
|
|
if (error)
|
|
|
|
goto out_inode_unlock;
|
|
|
|
|
|
|
|
if (inode->i_opflags & IOP_XATTR)
|
2023-01-13 19:49:20 +08:00
|
|
|
error = set_posix_acl(idmap, dentry, acl_type, NULL);
|
2022-09-22 23:17:14 +08:00
|
|
|
else if (unlikely(is_bad_inode(inode)))
|
|
|
|
error = -EIO;
|
|
|
|
else
|
|
|
|
error = -EOPNOTSUPP;
|
|
|
|
if (!error) {
|
|
|
|
fsnotify_xattr(dentry);
|
2023-01-13 19:49:24 +08:00
|
|
|
evm_inode_post_remove_acl(idmap, dentry, acl_name);
|
2022-09-22 23:17:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
out_inode_unlock:
|
|
|
|
inode_unlock(inode);
|
|
|
|
|
|
|
|
if (delegated_inode) {
|
|
|
|
error = break_deleg_wait(&delegated_inode);
|
|
|
|
if (!error)
|
|
|
|
goto retry_deleg;
|
|
|
|
}
|
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(vfs_remove_acl);
|
2022-09-22 23:17:22 +08:00
|
|
|
|
2022-10-28 15:56:20 +08:00
|
|
|
int do_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,
|
2022-09-22 23:17:22 +08:00
|
|
|
const char *acl_name, const void *kvalue, size_t size)
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
struct posix_acl *acl = NULL;
|
|
|
|
|
|
|
|
if (size) {
|
|
|
|
/*
|
|
|
|
* Note that posix_acl_from_xattr() uses GFP_NOFS when it
|
|
|
|
* probably doesn't need to here.
|
|
|
|
*/
|
|
|
|
acl = posix_acl_from_xattr(current_user_ns(), kvalue, size);
|
|
|
|
if (IS_ERR(acl))
|
|
|
|
return PTR_ERR(acl);
|
|
|
|
}
|
|
|
|
|
2023-01-13 19:49:20 +08:00
|
|
|
error = vfs_set_acl(idmap, dentry, acl_name, acl);
|
2022-09-22 23:17:22 +08:00
|
|
|
posix_acl_release(acl);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2022-10-28 15:56:20 +08:00
|
|
|
ssize_t do_get_acl(struct mnt_idmap *idmap, struct dentry *dentry,
|
2022-09-22 23:17:22 +08:00
|
|
|
const char *acl_name, void *kvalue, size_t size)
|
|
|
|
{
|
|
|
|
ssize_t error;
|
|
|
|
struct posix_acl *acl;
|
|
|
|
|
2023-01-13 19:49:19 +08:00
|
|
|
acl = vfs_get_acl(idmap, dentry, acl_name);
|
2022-09-22 23:17:22 +08:00
|
|
|
if (IS_ERR(acl))
|
|
|
|
return PTR_ERR(acl);
|
|
|
|
|
2022-10-28 15:56:20 +08:00
|
|
|
error = vfs_posix_acl_to_xattr(idmap, d_inode(dentry),
|
2022-09-22 23:17:22 +08:00
|
|
|
acl, kvalue, size);
|
|
|
|
posix_acl_release(acl);
|
|
|
|
return error;
|
|
|
|
}
|