fuse: send security context of inode on file
authorVivek Goyal <vgoyal@redhat.com>
Thu, 11 Nov 2021 14:32:49 +0000 (09:32 -0500)
committerMiklos Szeredi <mszeredi@redhat.com>
Thu, 25 Nov 2021 13:05:18 +0000 (14:05 +0100)
When a new inode is created, send its security context to server along with
creation request (FUSE_CREAT, FUSE_MKNOD, FUSE_MKDIR and FUSE_SYMLINK).
This gives server an opportunity to create new file and set security
context (possibly atomically).  In all the configurations it might not be
possible to set context atomically.

Like nfs and ceph, use security_dentry_init_security() to dermine security
context of inode and send it with create, mkdir, mknod, and symlink
requests.

Following is the information sent to server.

fuse_sectx_header, fuse_secctx, xattr_name, security_context

 - struct fuse_secctx_header
   This contains total number of security contexts being sent and total
   size of all the security contexts (including size of
   fuse_secctx_header).

 - struct fuse_secctx
   This contains size of security context which follows this structure.
   There is one fuse_secctx instance per security context.

 - xattr name string
   This string represents name of xattr which should be used while setting
   security context.

 - security context
   This is the actual security context whose size is specified in
   fuse_secctx struct.

Also add the FUSE_SECURITY_CTX flag for the `flags` field of the
fuse_init_out struct.  When this flag is set the kernel will append the
security context for a newly created inode to the request (create, mkdir,
mknod, and symlink).  The server is responsible for ensuring that the inode
appears atomically (preferrably) with the requested security context.

For example, If the server is using SELinux and backed by a "real" linux
file system that supports extended attributes it can write the security
context value to /proc/thread-self/attr/fscreate before making the syscall
to create the inode.

This patch is based on patch from Chirantan Ekbote <chirantan@chromium.org>

Signed-off-by: Vivek Goyal <vgoyal@redhat.com>
Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
fs/fuse/dir.c
fs/fuse/fuse_i.h
fs/fuse/inode.c
include/uapi/linux/fuse.h

index 0654bfe..656e921 100644 (file)
@@ -17,6 +17,9 @@
 #include <linux/xattr.h>
 #include <linux/iversion.h>
 #include <linux/posix_acl.h>
+#include <linux/security.h>
+#include <linux/types.h>
+#include <linux/kernel.h>
 
 static void fuse_advise_use_readdirplus(struct inode *dir)
 {
@@ -456,6 +459,62 @@ static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry,
        return ERR_PTR(err);
 }
 
+static int get_security_context(struct dentry *entry, umode_t mode,
+                               void **security_ctx, u32 *security_ctxlen)
+{
+       struct fuse_secctx *fctx;
+       struct fuse_secctx_header *header;
+       void *ctx = NULL, *ptr;
+       u32 ctxlen, total_len = sizeof(*header);
+       int err, nr_ctx = 0;
+       const char *name;
+       size_t namelen;
+
+       err = security_dentry_init_security(entry, mode, &entry->d_name,
+                                           &name, &ctx, &ctxlen);
+       if (err) {
+               if (err != -EOPNOTSUPP)
+                       goto out_err;
+               /* No LSM is supporting this security hook. Ignore error */
+               ctxlen = 0;
+               ctx = NULL;
+       }
+
+       if (ctxlen) {
+               nr_ctx = 1;
+               namelen = strlen(name) + 1;
+               err = -EIO;
+               if (WARN_ON(namelen > XATTR_NAME_MAX + 1 || ctxlen > S32_MAX))
+                       goto out_err;
+               total_len += FUSE_REC_ALIGN(sizeof(*fctx) + namelen + ctxlen);
+       }
+
+       err = -ENOMEM;
+       header = ptr = kzalloc(total_len, GFP_KERNEL);
+       if (!ptr)
+               goto out_err;
+
+       header->nr_secctx = nr_ctx;
+       header->size = total_len;
+       ptr += sizeof(*header);
+       if (nr_ctx) {
+               fctx = ptr;
+               fctx->size = ctxlen;
+               ptr += sizeof(*fctx);
+
+               strcpy(ptr, name);
+               ptr += namelen;
+
+               memcpy(ptr, ctx, ctxlen);
+       }
+       *security_ctxlen = total_len;
+       *security_ctx = header;
+       err = 0;
+out_err:
+       kfree(ctx);
+       return err;
+}
+
 /*
  * Atomic create+open operation
  *
@@ -476,6 +535,8 @@ static int fuse_create_open(struct inode *dir, struct dentry *entry,
        struct fuse_entry_out outentry;
        struct fuse_inode *fi;
        struct fuse_file *ff;
+       void *security_ctx = NULL;
+       u32 security_ctxlen;
 
        /* Userspace expects S_IFREG in create mode */
        BUG_ON((mode & S_IFMT) != S_IFREG);
@@ -517,7 +578,20 @@ static int fuse_create_open(struct inode *dir, struct dentry *entry,
        args.out_args[0].value = &outentry;
        args.out_args[1].size = sizeof(outopen);
        args.out_args[1].value = &outopen;
+
+       if (fm->fc->init_security) {
+               err = get_security_context(entry, mode, &security_ctx,
+                                          &security_ctxlen);
+               if (err)
+                       goto out_put_forget_req;
+
+               args.in_numargs = 3;
+               args.in_args[2].size = security_ctxlen;
+               args.in_args[2].value = security_ctx;
+       }
+
        err = fuse_simple_request(fm, &args);
+       kfree(security_ctx);
        if (err)
                goto out_free_ff;
 
@@ -620,6 +694,8 @@ static int create_new_entry(struct fuse_mount *fm, struct fuse_args *args,
        struct dentry *d;
        int err;
        struct fuse_forget_link *forget;
+       void *security_ctx = NULL;
+       u32 security_ctxlen;
 
        if (fuse_is_bad(dir))
                return -EIO;
@@ -633,7 +709,22 @@ static int create_new_entry(struct fuse_mount *fm, struct fuse_args *args,
        args->out_numargs = 1;
        args->out_args[0].size = sizeof(outarg);
        args->out_args[0].value = &outarg;
+
+       if (fm->fc->init_security && args->opcode != FUSE_LINK) {
+               err = get_security_context(entry, mode, &security_ctx,
+                                          &security_ctxlen);
+               if (err)
+                       goto out_put_forget_req;
+
+               BUG_ON(args->in_numargs != 2);
+
+               args->in_numargs = 3;
+               args->in_args[2].size = security_ctxlen;
+               args->in_args[2].value = security_ctx;
+       }
+
        err = fuse_simple_request(fm, args);
+       kfree(security_ctx);
        if (err)
                goto out_put_forget_req;
 
index 198637b..c1a8b31 100644 (file)
@@ -765,6 +765,9 @@ struct fuse_conn {
        /* Propagate syncfs() to server */
        unsigned int sync_fs:1;
 
+       /* Initialize security xattrs when creating a new inode */
+       unsigned int init_security:1;
+
        /** The number of requests waiting for completion */
        atomic_t num_waiting;
 
index 5a1dad8..63ab454 100644 (file)
@@ -1178,6 +1178,8 @@ static void process_init_reply(struct fuse_mount *fm, struct fuse_args *args,
                        }
                        if (flags & FUSE_SETXATTR_EXT)
                                fc->setxattr_ext = 1;
+                       if (flags & FUSE_SECURITY_CTX)
+                               fc->init_security = 1;
                } else {
                        ra_pages = fc->max_read / PAGE_SIZE;
                        fc->no_lock = 1;
@@ -1222,7 +1224,8 @@ void fuse_send_init(struct fuse_mount *fm)
                FUSE_PARALLEL_DIROPS | FUSE_HANDLE_KILLPRIV | FUSE_POSIX_ACL |
                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_HANDLE_KILLPRIV_V2 | FUSE_SETXATTR_EXT | FUSE_INIT_EXT |
+               FUSE_SECURITY_CTX;
 #ifdef CONFIG_FUSE_DAX
        if (fm->fc->dax)
                flags |= FUSE_MAP_ALIGNMENT;
index 980f399..3f0ea63 100644 (file)
  *  7.36
  *  - extend fuse_init_in with reserved fields, add FUSE_INIT_EXT init flag
  *  - add flags2 to fuse_init_in and fuse_init_out
+ *  - add FUSE_SECURITY_CTX init flag
+ *  - add security context to create, mkdir, symlink, and mknod requests
  */
 
 #ifndef _LINUX_FUSE_H
@@ -347,6 +349,8 @@ struct fuse_file_lock {
  * FUSE_SETXATTR_EXT:  Server supports extended struct fuse_setxattr_in
  * FUSE_INIT_EXT: extended fuse_init_in request
  * FUSE_INIT_RESERVED: reserved, do not use
+ * FUSE_SECURITY_CTX:  add security context to create, mkdir, symlink, and
+ *                     mknod
  */
 #define FUSE_ASYNC_READ                (1 << 0)
 #define FUSE_POSIX_LOCKS       (1 << 1)
@@ -381,6 +385,7 @@ struct fuse_file_lock {
 #define FUSE_INIT_EXT          (1 << 30)
 #define FUSE_INIT_RESERVED     (1 << 31)
 /* bits 32..63 get shifted down 32 bits into the flags2 field */
+#define FUSE_SECURITY_CTX      (1ULL << 32)
 
 /**
  * CUSE INIT request/reply flags
@@ -877,9 +882,12 @@ struct fuse_dirent {
        char name[];
 };
 
-#define FUSE_NAME_OFFSET offsetof(struct fuse_dirent, name)
-#define FUSE_DIRENT_ALIGN(x) \
+/* Align variable length records to 64bit boundary */
+#define FUSE_REC_ALIGN(x) \
        (((x) + sizeof(uint64_t) - 1) & ~(sizeof(uint64_t) - 1))
+
+#define FUSE_NAME_OFFSET offsetof(struct fuse_dirent, name)
+#define FUSE_DIRENT_ALIGN(x) FUSE_REC_ALIGN(x)
 #define FUSE_DIRENT_SIZE(d) \
        FUSE_DIRENT_ALIGN(FUSE_NAME_OFFSET + (d)->namelen)
 
@@ -996,4 +1004,26 @@ struct fuse_syncfs_in {
        uint64_t        padding;
 };
 
+/*
+ * For each security context, send fuse_secctx with size of security context
+ * fuse_secctx will be followed by security context name and this in turn
+ * will be followed by actual context label.
+ * fuse_secctx, name, context
+ */
+struct fuse_secctx {
+       uint32_t        size;
+       uint32_t        padding;
+};
+
+/*
+ * Contains the information about how many fuse_secctx structures are being
+ * sent and what's the total size of all security contexts (including
+ * size of fuse_secctx_header).
+ *
+ */
+struct fuse_secctx_header {
+       uint32_t        size;
+       uint32_t        nr_secctx;
+};
+
 #endif /* _LINUX_FUSE_H */