]> www.infradead.org Git - users/willy/linux.git/commitdiff
vfs: syscall: Add fsmount() to create a mount for a superblock
authorDavid Howells <dhowells@redhat.com>
Fri, 7 Sep 2018 06:41:01 +0000 (07:41 +0100)
committerDavid Howells <dhowells@redhat.com>
Tue, 23 Oct 2018 16:39:02 +0000 (17:39 +0100)
Provide a system call by which a filesystem opened with fsopen() and
configured by a series of fsconfig() calls can have a detached mount object
created for it.  This mount object can then be attached to the VFS mount
hierarchy using move_mount() by passing the returned file descriptor as the
from directory fd.

The system call looks like:

int mfd = fsmount(int fsfd, unsigned int flags,
  unsigned int ms_flags);

where fsfd is the file descriptor returned by fsopen().  flags can be 0 or
FSMOUNT_CLOEXEC.  ms_flags is a bitwise-OR of the following flags:

MS_RDONLY
MS_NOSUID
MS_NODEV
MS_NOEXEC
MS_NOATIME
MS_NODIRATIME
MS_RELATIME
MS_STRICTATIME

MS_UNBINDABLE
MS_PRIVATE
MS_SLAVE
MS_SHARED

In the event that fsmount() fails, it may be possible to get an error
message by calling read() on fsfd.  If no message is available, ENODATA
will be reported.

Signed-off-by: David Howells <dhowells@redhat.com>
cc: linux-api@vger.kernel.org

arch/x86/entry/syscalls/syscall_32.tbl
arch/x86/entry/syscalls/syscall_64.tbl
fs/namespace.c
include/linux/syscalls.h
include/uapi/linux/mount.h

index f9970310c12680d0851c0fc2ca87fe1f1f36d8ef..c78b68256f8afbdad60cce2f518a15a8a367e831 100644 (file)
 388    i386    move_mount              sys_move_mount                  __ia32_sys_move_mount
 389    i386    fsopen                  sys_fsopen                      __ia32_sys_fsopen
 390    i386    fsconfig                sys_fsconfig                    __ia32_sys_fsconfig
+391    i386    fsmount                 sys_fsmount                     __ia32_sys_fsmount
index 4185d36e03bb5021d17374c5cc13cf44ed54e62f..d44ead5d436847cecee5b9a033d69d229babf8ac 100644 (file)
 336    common  move_mount              __x64_sys_move_mount
 337    common  fsopen                  __x64_sys_fsopen
 338    common  fsconfig                __x64_sys_fsconfig
+339    common  fsmount                 __x64_sys_fsmount
 
 #
 # x32-specific system call numbers start at 512 to avoid cache impact
index 15133eec8aca6ffec2675b362f5dd094400e2d34..7fb265914535919f81a8bcd1fc7d5e414dac071d 100644 (file)
@@ -2488,7 +2488,7 @@ static int do_move_mount(struct path *old_path, struct path *new_path)
 
        attached = mnt_has_parent(old);
        /*
-        * We need to allow open_tree(OPEN_TREE_CLONE) followed by
+        * We need to allow open_tree(OPEN_TREE_CLONE) or fsmount() followed by
         * move_mount(), but mustn't allow "/" to be moved.
         */
        if (old->mnt_ns && !attached)
@@ -3363,9 +3363,134 @@ struct vfsmount *kern_mount(struct file_system_type *type)
 EXPORT_SYMBOL_GPL(kern_mount);
 
 /*
- * Move a mount from one place to another.
- * In combination with open_tree(OPEN_TREE_CLONE [| AT_RECURSIVE]) it can be
- * used to copy a mount subtree.
+ * Create a kernel mount representation for a new, prepared superblock
+ * (specified by fs_fd) and attach to an open_tree-like file descriptor.
+ */
+SYSCALL_DEFINE3(fsmount, int, fs_fd, unsigned int, flags,
+               unsigned int, attr_flags)
+{
+       struct fs_context *fc;
+       struct file *file;
+       struct path newmount;
+       struct fd f;
+       unsigned int mnt_flags = 0;
+       long ret;
+
+       if (!may_mount())
+               return -EPERM;
+
+       if ((flags & ~(FSMOUNT_CLOEXEC)) != 0)
+               return -EINVAL;
+
+       if (attr_flags & ~(MOUNT_ATTR_RDONLY |
+                          MOUNT_ATTR_NOSUID |
+                          MOUNT_ATTR_NODEV |
+                          MOUNT_ATTR_NOEXEC |
+                          MOUNT_ATTR__ATIME |
+                          MOUNT_ATTR_NODIRATIME))
+               return -EINVAL;
+
+       if (attr_flags & MOUNT_ATTR_RDONLY)
+               mnt_flags |= MNT_READONLY;
+       if (attr_flags & MOUNT_ATTR_NOSUID)
+               mnt_flags |= MNT_NOSUID;
+       if (attr_flags & MOUNT_ATTR_NODEV)
+               mnt_flags |= MNT_NODEV;
+       if (attr_flags & MOUNT_ATTR_NOEXEC)
+               mnt_flags |= MNT_NOEXEC;
+       if (attr_flags & MOUNT_ATTR_NODIRATIME)
+               mnt_flags |= MNT_NODIRATIME;
+
+       switch (attr_flags & MOUNT_ATTR__ATIME) {
+       case MOUNT_ATTR_STRICTATIME:
+               break;
+       case MOUNT_ATTR_NOATIME:
+               mnt_flags |= MNT_NOATIME;
+               break;
+       case MOUNT_ATTR_RELATIME:
+               mnt_flags |= MNT_RELATIME;
+               break;
+       default:
+               return -EINVAL;
+       }
+
+       f = fdget(fs_fd);
+       if (!f.file)
+               return -EBADF;
+
+       ret = -EINVAL;
+       if (f.file->f_op != &fscontext_fops)
+               goto err_fsfd;
+
+       fc = f.file->private_data;
+
+       /* There must be a valid superblock or we can't mount it */
+       ret = -EINVAL;
+       if (!fc->root)
+               goto err_fsfd;
+
+       ret = -EPERM;
+       if (mount_too_revealing(fc->root->d_sb, &mnt_flags)) {
+               pr_warn("VFS: Mount too revealing\n");
+               goto err_fsfd;
+       }
+
+       ret = mutex_lock_interruptible(&fc->uapi_mutex);
+       if (ret < 0)
+               goto err_fsfd;
+
+       ret = -EBUSY;
+       if (fc->phase != FS_CONTEXT_AWAITING_MOUNT)
+               goto err_unlock;
+
+       ret = -EPERM;
+       if ((fc->sb_flags & SB_MANDLOCK) && !may_mandlock())
+               goto err_unlock;
+
+       newmount.mnt = vfs_create_mount(fc, mnt_flags);
+       if (IS_ERR(newmount.mnt)) {
+               ret = PTR_ERR(newmount.mnt);
+               goto err_unlock;
+       }
+       newmount.dentry = dget(fc->root);
+
+       /* We've done the mount bit - now move the file context into more or
+        * less the same state as if we'd done an fspick().  We don't want to
+        * do any memory allocation or anything like that at this point as we
+        * don't want to have to handle any errors incurred.
+        */
+       vfs_clean_context(fc);
+
+       /* Attach to an apparent O_PATH fd with a note that we need to unmount
+        * it, not just simply put it.
+        */
+       file = dentry_open(&newmount, O_PATH, fc->cred);
+       if (IS_ERR(file)) {
+               ret = PTR_ERR(file);
+               goto err_path;
+       }
+       file->f_mode |= FMODE_NEED_UNMOUNT;
+
+       ret = get_unused_fd_flags((flags & FSMOUNT_CLOEXEC) ? O_CLOEXEC : 0);
+       if (ret >= 0)
+               fd_install(ret, file);
+       else
+               fput(file);
+
+err_path:
+       path_put(&newmount);
+err_unlock:
+       mutex_unlock(&fc->uapi_mutex);
+err_fsfd:
+       fdput(f);
+       return ret;
+}
+
+/*
+ * Move a mount from one place to another.  In combination with
+ * fsopen()/fsmount() this is used to install a new mount and in combination
+ * with open_tree(OPEN_TREE_CLONE [| AT_RECURSIVE]) it can be used to copy
+ * a mount subtree.
  *
  * Note the flags value is a combination of MOVE_MOUNT_* flags.
  */
index 4ab15fdf8aea70f4ccbbf2c17a9debbd103a5ddf..4697fad47789af91a07bda6c25e6ecd978af0b5a 100644 (file)
@@ -913,6 +913,7 @@ asmlinkage long sys_move_mount(int from_dfd, const char __user *from_path,
 asmlinkage long sys_fsopen(const char __user *fs_name, unsigned int flags);
 asmlinkage long sys_fsconfig(int fs_fd, unsigned int cmd, const char __user *key,
                             const void __user *value, int aux);
+asmlinkage long sys_fsmount(int fs_fd, unsigned int flags, unsigned int ms_flags);
 
 /*
  * Architecture-specific system calls
index 4b90ba9d1770d3d71e0045a668282b89ab141da1..3888d3b91dc5035f86f3575e7760f387db1aa299 100644 (file)
@@ -91,4 +91,22 @@ enum fsconfig_command {
        FSCONFIG_CMD_RECONFIGURE = 7,   /* Invoke superblock reconfiguration */
 };
 
+/*
+ * fsmount() flags.
+ */
+#define FSMOUNT_CLOEXEC                0x00000001
+
+/*
+ * Mount attributes.
+ */
+#define MOUNT_ATTR_RDONLY      0x00000001 /* Mount read-only */
+#define MOUNT_ATTR_NOSUID      0x00000002 /* Ignore suid and sgid bits */
+#define MOUNT_ATTR_NODEV       0x00000004 /* Disallow access to device special files */
+#define MOUNT_ATTR_NOEXEC      0x00000008 /* Disallow program execution */
+#define MOUNT_ATTR__ATIME      0x00000070 /* Setting on how atime should be updated */
+#define MOUNT_ATTR_RELATIME    0x00000000 /* - Update atime relative to mtime/ctime. */
+#define MOUNT_ATTR_NOATIME     0x00000010 /* - Do not update access times. */
+#define MOUNT_ATTR_STRICTATIME 0x00000020 /* - Always perform atime updates */
+#define MOUNT_ATTR_NODIRATIME  0x00000080 /* Do not update directory access times */
+
 #endif /* _UAPI_LINUX_MOUNT_H */