fuse: optional supplementary group in create requests

Permission to create an object (create, mkdir, symlink, mknod) needs to
take supplementary groups into account.

Add a supplementary group request extension.  This can contain an arbitrary
number of group IDs and can be added to any request.  This extension is not
added to any request by default.

Add FUSE_CREATE_SUPP_GROUP init flag to enable supplementary group info in
creation requests.  This adds just a single supplementary group that
matches the parent group in the case described above.  In other cases the
extension is not added.

Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
This commit is contained in:
Miklos Szeredi 2022-11-10 15:46:33 +01:00
Родитель 15d937d7ca
Коммит 8ed7cb3f27
4 изменённых файлов: 84 добавлений и 4 удалений

Просмотреть файл

@ -521,7 +521,63 @@ out_err:
return err;
}
static int get_create_ext(struct fuse_args *args, struct dentry *dentry,
static void *extend_arg(struct fuse_in_arg *buf, u32 bytes)
{
void *p;
u32 newlen = buf->size + bytes;
p = krealloc(buf->value, newlen, GFP_KERNEL);
if (!p) {
kfree(buf->value);
buf->size = 0;
buf->value = NULL;
return NULL;
}
memset(p + buf->size, 0, bytes);
buf->value = p;
buf->size = newlen;
return p + newlen - bytes;
}
static u32 fuse_ext_size(size_t size)
{
return FUSE_REC_ALIGN(sizeof(struct fuse_ext_header) + size);
}
/*
* This adds just a single supplementary group that matches the parent's group.
*/
static int get_create_supp_group(struct inode *dir, struct fuse_in_arg *ext)
{
struct fuse_conn *fc = get_fuse_conn(dir);
struct fuse_ext_header *xh;
struct fuse_supp_groups *sg;
kgid_t kgid = dir->i_gid;
gid_t parent_gid = from_kgid(fc->user_ns, kgid);
u32 sg_len = fuse_ext_size(sizeof(*sg) + sizeof(sg->groups[0]));
if (parent_gid == (gid_t) -1 || gid_eq(kgid, current_fsgid()) ||
!in_group_p(kgid))
return 0;
xh = extend_arg(ext, sg_len);
if (!xh)
return -ENOMEM;
xh->size = sg_len;
xh->type = FUSE_EXT_GROUPS;
sg = (struct fuse_supp_groups *) &xh[1];
sg->nr_groups = 1;
sg->groups[0] = parent_gid;
return 0;
}
static int get_create_ext(struct fuse_args *args,
struct inode *dir, struct dentry *dentry,
umode_t mode)
{
struct fuse_conn *fc = get_fuse_conn_super(dentry->d_sb);
@ -530,6 +586,8 @@ static int get_create_ext(struct fuse_args *args, struct dentry *dentry,
if (fc->init_security)
err = get_security_context(dentry, mode, &ext);
if (!err && fc->create_supp_group)
err = get_create_supp_group(dir, &ext);
if (!err && ext.size) {
WARN_ON(args->in_numargs >= ARRAY_SIZE(args->in_args));
@ -612,7 +670,7 @@ static int fuse_create_open(struct inode *dir, struct dentry *entry,
args.out_args[1].size = sizeof(outopen);
args.out_args[1].value = &outopen;
err = get_create_ext(&args, entry, mode);
err = get_create_ext(&args, dir, entry, mode);
if (err)
goto out_put_forget_req;
@ -739,7 +797,7 @@ static int create_new_entry(struct fuse_mount *fm, struct fuse_args *args,
args->out_args[0].value = &outarg;
if (args->opcode != FUSE_LINK) {
err = get_create_ext(args, entry, mode);
err = get_create_ext(args, dir, entry, mode);
if (err)
goto out_put_forget_req;
}

Просмотреть файл

@ -783,6 +783,9 @@ struct fuse_conn {
/* Initialize security xattrs when creating a new inode */
unsigned int init_security:1;
/* Add supplementary group info when creating a new inode */
unsigned int create_supp_group:1;
/* Does the filesystem support per inode DAX? */
unsigned int inode_dax:1;

Просмотреть файл

@ -1201,6 +1201,8 @@ static void process_init_reply(struct fuse_mount *fm, struct fuse_args *args,
fc->setxattr_ext = 1;
if (flags & FUSE_SECURITY_CTX)
fc->init_security = 1;
if (flags & FUSE_CREATE_SUPP_GROUP)
fc->create_supp_group = 1;
} else {
ra_pages = fc->max_read / PAGE_SIZE;
fc->no_lock = 1;
@ -1246,7 +1248,7 @@ void fuse_send_init(struct fuse_mount *fm)
FUSE_ABORT_ERROR | FUSE_MAX_PAGES | FUSE_CACHE_SYMLINKS |
FUSE_NO_OPENDIR_SUPPORT | FUSE_EXPLICIT_INVAL_DATA |
FUSE_HANDLE_KILLPRIV_V2 | FUSE_SETXATTR_EXT | FUSE_INIT_EXT |
FUSE_SECURITY_CTX;
FUSE_SECURITY_CTX | FUSE_CREATE_SUPP_GROUP;
#ifdef CONFIG_FUSE_DAX
if (fm->fc->dax)
flags |= FUSE_MAP_ALIGNMENT;

Просмотреть файл

@ -204,6 +204,8 @@
* - add total_extlen to fuse_in_header
* - add FUSE_MAX_NR_SECCTX
* - add extension header
* - add FUSE_EXT_GROUPS
* - add FUSE_CREATE_SUPP_GROUP
*/
#ifndef _LINUX_FUSE_H
@ -365,6 +367,8 @@ struct fuse_file_lock {
* FUSE_SECURITY_CTX: add security context to create, mkdir, symlink, and
* mknod
* FUSE_HAS_INODE_DAX: use per inode DAX
* FUSE_CREATE_SUPP_GROUP: add supplementary group info to create, mkdir,
* symlink and mknod (single group that matches parent)
*/
#define FUSE_ASYNC_READ (1 << 0)
#define FUSE_POSIX_LOCKS (1 << 1)
@ -401,6 +405,7 @@ struct fuse_file_lock {
/* bits 32..63 get shifted down 32 bits into the flags2 field */
#define FUSE_SECURITY_CTX (1ULL << 32)
#define FUSE_HAS_INODE_DAX (1ULL << 33)
#define FUSE_CREATE_SUPP_GROUP (1ULL << 34)
/**
* CUSE INIT request/reply flags
@ -509,10 +514,12 @@ struct fuse_file_lock {
/**
* extension type
* FUSE_MAX_NR_SECCTX: maximum value of &fuse_secctx_header.nr_secctx
* FUSE_EXT_GROUPS: &fuse_supp_groups extension
*/
enum fuse_ext_type {
/* Types 0..31 are reserved for fuse_secctx_header */
FUSE_MAX_NR_SECCTX = 31,
FUSE_EXT_GROUPS = 32,
};
enum fuse_opcode {
@ -1073,4 +1080,14 @@ struct fuse_ext_header {
uint32_t type;
};
/**
* struct fuse_supp_groups - Supplementary group extension
* @nr_groups: number of supplementary groups
* @groups: flexible array of group IDs
*/
struct fuse_supp_groups {
uint32_t nr_groups;
uint32_t groups[];
};
#endif /* _LINUX_FUSE_H */