Skip to content

Commit 24dcb3d

Browse files
dhowellsAl Viro
authored andcommitted
vfs: syscall: Add fsopen() to prepare for superblock creation
Provide an fsopen() system call that starts the process of preparing to create a superblock that will then be mountable, using an fd as a context handle. fsopen() is given the name of the filesystem that will be used: int mfd = fsopen(const char *fsname, unsigned int flags); where flags can be 0 or FSOPEN_CLOEXEC. For example: sfd = fsopen("ext4", FSOPEN_CLOEXEC); fsconfig(sfd, FSCONFIG_SET_PATH, "source", "/dev/sda1", AT_FDCWD); fsconfig(sfd, FSCONFIG_SET_FLAG, "noatime", NULL, 0); fsconfig(sfd, FSCONFIG_SET_FLAG, "acl", NULL, 0); fsconfig(sfd, FSCONFIG_SET_FLAG, "user_xattr", NULL, 0); fsconfig(sfd, FSCONFIG_SET_STRING, "sb", "1", 0); fsconfig(sfd, FSCONFIG_CMD_CREATE, NULL, NULL, 0); fsinfo(sfd, NULL, ...); // query new superblock attributes mfd = fsmount(sfd, FSMOUNT_CLOEXEC, MS_RELATIME); move_mount(mfd, "", sfd, AT_FDCWD, "/mnt", MOVE_MOUNT_F_EMPTY_PATH); sfd = fsopen("afs", -1); fsconfig(fd, FSCONFIG_SET_STRING, "source", "#grand.central.org:root.cell", 0); fsconfig(fd, FSCONFIG_CMD_CREATE, NULL, NULL, 0); mfd = fsmount(sfd, 0, MS_NODEV); move_mount(mfd, "", sfd, AT_FDCWD, "/mnt", MOVE_MOUNT_F_EMPTY_PATH); If an error is reported at any step, an error message may be available to be read() back (ENODATA will be reported if there isn't an error available) in the form: "e <subsys>:<problem>" "e SELinux:Mount on mountpoint not permitted" Once fsmount() has been called, further fsconfig() calls will incur EBUSY, even if the fsmount() fails. read() is still possible to retrieve error information. The fsopen() syscall creates a mount context and hangs it of the fd that it returns. Netlink is not used because it is optional and would make the core VFS dependent on the networking layer and also potentially add network namespace issues. Note that, for the moment, the caller must have SYS_CAP_ADMIN to use fsopen(). Signed-off-by: David Howells <dhowells@redhat.com> cc: linux-api@vger.kernel.org Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
1 parent dadd229 commit 24dcb3d

8 files changed

Lines changed: 118 additions & 2 deletions

File tree

‎arch/x86/entry/syscalls/syscall_32.tbl‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,8 @@
400400
386 i386 rseq sys_rseq __ia32_sys_rseq
401401
387 i386 open_tree sys_open_tree __ia32_sys_open_tree
402402
388 i386 move_mount sys_move_mount __ia32_sys_move_mount
403-
# don't use numbers 389 through 392, add new calls at the end
403+
389 i386 fsopen sys_fsopen __ia32_sys_fsopen
404+
# don't use numbers 390 through 392, add new calls at the end
404405
393 i386 semget sys_semget __ia32_sys_semget
405406
394 i386 semctl sys_semctl __ia32_compat_sys_semctl
406407
395 i386 shmget sys_shmget __ia32_sys_shmget

‎arch/x86/entry/syscalls/syscall_64.tbl‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,7 @@
345345
334 common rseq __x64_sys_rseq
346346
335 common open_tree __x64_sys_open_tree
347347
336 common move_mount __x64_sys_move_mount
348+
337 common fsopen __x64_sys_fsopen
348349
# don't use numbers 387 through 423, add new calls after the last
349350
# 'common' entry
350351
424 common pidfd_send_signal __x64_sys_pidfd_send_signal

‎fs/Makefile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ obj-y := open.o read_write.o file_table.o super.o \
1313
seq_file.o xattr.o libfs.o fs-writeback.o \
1414
pnode.o splice.o sync.o utimes.o d_path.o \
1515
stack.o fs_struct.o statfs.o fs_pin.o nsfs.o \
16-
fs_types.o fs_context.o fs_parser.o
16+
fs_types.o fs_context.o fs_parser.o fsopen.o
1717

1818
ifeq ($(CONFIG_BLOCK),y)
1919
obj-y += buffer.o block_dev.o direct-io.o mpage.o

‎fs/fs_context.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,8 @@ static struct fs_context *alloc_fs_context(struct file_system_type *fs_type,
271271
fc->cred = get_current_cred();
272272
fc->net_ns = get_net(current->nsproxy->net_ns);
273273

274+
mutex_init(&fc->uapi_mutex);
275+
274276
switch (purpose) {
275277
case FS_CONTEXT_FOR_MOUNT:
276278
fc->user_ns = get_user_ns(fc->cred->user_ns);
@@ -353,6 +355,8 @@ struct fs_context *vfs_dup_fs_context(struct fs_context *src_fc)
353355
if (!fc)
354356
return ERR_PTR(-ENOMEM);
355357

358+
mutex_init(&fc->uapi_mutex);
359+
356360
fc->fs_private = NULL;
357361
fc->s_fs_info = NULL;
358362
fc->source = NULL;

‎fs/fsopen.c‎

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/* Filesystem access-by-fd.
2+
*
3+
* Copyright (C) 2017 Red Hat, Inc. All Rights Reserved.
4+
* Written by David Howells (dhowells@redhat.com)
5+
*
6+
* This program is free software; you can redistribute it and/or
7+
* modify it under the terms of the GNU General Public Licence
8+
* as published by the Free Software Foundation; either version
9+
* 2 of the Licence, or (at your option) any later version.
10+
*/
11+
12+
#include <linux/fs_context.h>
13+
#include <linux/slab.h>
14+
#include <linux/uaccess.h>
15+
#include <linux/syscalls.h>
16+
#include <linux/security.h>
17+
#include <linux/anon_inodes.h>
18+
#include <linux/namei.h>
19+
#include <linux/file.h>
20+
#include <uapi/linux/mount.h>
21+
#include "mount.h"
22+
23+
static int fscontext_release(struct inode *inode, struct file *file)
24+
{
25+
struct fs_context *fc = file->private_data;
26+
27+
if (fc) {
28+
file->private_data = NULL;
29+
put_fs_context(fc);
30+
}
31+
return 0;
32+
}
33+
34+
const struct file_operations fscontext_fops = {
35+
.release = fscontext_release,
36+
.llseek = no_llseek,
37+
};
38+
39+
/*
40+
* Attach a filesystem context to a file and an fd.
41+
*/
42+
static int fscontext_create_fd(struct fs_context *fc, unsigned int o_flags)
43+
{
44+
int fd;
45+
46+
fd = anon_inode_getfd("fscontext", &fscontext_fops, fc,
47+
O_RDWR | o_flags);
48+
if (fd < 0)
49+
put_fs_context(fc);
50+
return fd;
51+
}
52+
53+
/*
54+
* Open a filesystem by name so that it can be configured for mounting.
55+
*
56+
* We are allowed to specify a container in which the filesystem will be
57+
* opened, thereby indicating which namespaces will be used (notably, which
58+
* network namespace will be used for network filesystems).
59+
*/
60+
SYSCALL_DEFINE2(fsopen, const char __user *, _fs_name, unsigned int, flags)
61+
{
62+
struct file_system_type *fs_type;
63+
struct fs_context *fc;
64+
const char *fs_name;
65+
66+
if (!ns_capable(current->nsproxy->mnt_ns->user_ns, CAP_SYS_ADMIN))
67+
return -EPERM;
68+
69+
if (flags & ~FSOPEN_CLOEXEC)
70+
return -EINVAL;
71+
72+
fs_name = strndup_user(_fs_name, PAGE_SIZE);
73+
if (IS_ERR(fs_name))
74+
return PTR_ERR(fs_name);
75+
76+
fs_type = get_fs_type(fs_name);
77+
kfree(fs_name);
78+
if (!fs_type)
79+
return -ENODEV;
80+
81+
fc = fs_context_for_mount(fs_type, 0);
82+
put_filesystem(fs_type);
83+
if (IS_ERR(fc))
84+
return PTR_ERR(fc);
85+
86+
fc->phase = FS_CONTEXT_CREATE_PARAMS;
87+
return fscontext_create_fd(fc, flags & FSOPEN_CLOEXEC ? O_CLOEXEC : 0);
88+
}

‎include/linux/fs_context.h‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <linux/kernel.h>
1616
#include <linux/errno.h>
1717
#include <linux/security.h>
18+
#include <linux/mutex.h>
1819

1920
struct cred;
2021
struct dentry;
@@ -34,6 +35,19 @@ enum fs_context_purpose {
3435
FS_CONTEXT_FOR_RECONFIGURE, /* Superblock reconfiguration (remount) */
3536
};
3637

38+
/*
39+
* Userspace usage phase for fsopen/fspick.
40+
*/
41+
enum fs_context_phase {
42+
FS_CONTEXT_CREATE_PARAMS, /* Loading params for sb creation */
43+
FS_CONTEXT_CREATING, /* A superblock is being created */
44+
FS_CONTEXT_AWAITING_MOUNT, /* Superblock created, awaiting fsmount() */
45+
FS_CONTEXT_AWAITING_RECONF, /* Awaiting initialisation for reconfiguration */
46+
FS_CONTEXT_RECONF_PARAMS, /* Loading params for reconfiguration */
47+
FS_CONTEXT_RECONFIGURING, /* Reconfiguring the superblock */
48+
FS_CONTEXT_FAILED, /* Failed to correctly transition a context */
49+
};
50+
3751
/*
3852
* Type of parameter value.
3953
*/
@@ -74,6 +88,7 @@ struct fs_parameter {
7488
*/
7589
struct fs_context {
7690
const struct fs_context_operations *ops;
91+
struct mutex uapi_mutex; /* Userspace access mutex */
7792
struct file_system_type *fs_type;
7893
void *fs_private; /* The filesystem's context */
7994
struct dentry *root; /* The root and superblock */
@@ -88,6 +103,7 @@ struct fs_context {
88103
unsigned int sb_flags_mask; /* Superblock flags that were changed */
89104
unsigned int lsm_flags; /* Information flags from the fs to the LSM */
90105
enum fs_context_purpose purpose:8;
106+
enum fs_context_phase phase:8; /* The phase the context is in */
91107
bool need_free:1; /* Need to call ops->free() */
92108
bool global:1; /* Goes into &init_user_ns */
93109
};

‎include/linux/syscalls.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -989,6 +989,7 @@ asmlinkage long sys_open_tree(int dfd, const char __user *path, unsigned flags);
989989
asmlinkage long sys_move_mount(int from_dfd, const char __user *from_path,
990990
int to_dfd, const char __user *to_path,
991991
unsigned int ms_flags);
992+
asmlinkage long sys_fsopen(const char __user *fs_name, unsigned int flags);
992993
asmlinkage long sys_pidfd_send_signal(int pidfd, int sig,
993994
siginfo_t __user *info,
994995
unsigned int flags);

‎include/uapi/linux/mount.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,9 @@
7272
#define MOVE_MOUNT_T_EMPTY_PATH 0x00000040 /* Empty to path permitted */
7373
#define MOVE_MOUNT__MASK 0x00000077
7474

75+
/*
76+
* fsopen() flags.
77+
*/
78+
#define FSOPEN_CLOEXEC 0x00000001
79+
7580
#endif /* _UAPI_LINUX_MOUNT_H */

0 commit comments

Comments
 (0)