From: David Howells <dhowells@redhat.com> Date: 2018-09-21 22:19:53
Hi Al,
Here are a set of patches to create a filesystem context prior to setting
up a new mount, populating it with the parsed options/binary data, creating
the superblock and then effecting the mount. This is also used for remount
since much of the parsing stuff is common in many filesystems.
This allows namespaces and other information to be conveyed through the
mount procedure.
This also allows Miklós Szeredi's idea of doing:
fd = fsopen("nfs");
fsconfig(fd, FSCONFIG_SET_STRING, "option", "val", 0);
fsconfig(fd, FSCONFIG_CMD_CREATE, NULL, NULL, 0);
mfd = fsmount(fd, MS_NODEV);
move_mount(mfd, "", AT_FDCWD, "/mnt", MOVE_MOUNT_F_EMPTY_PATH);
that he presented at LSF-2017 to be implemented (see the relevant patches
in the series).
I didn't use netlink as that would make the core kernel depend on
CONFIG_NET and CONFIG_NETLINK and would introduce network namespacing
issues.
I've implemented filesystem context handling for procfs, nfs, mqueue,
cpuset, kernfs, sysfs, cgroup and afs filesystems.
Unconverted filesystems are handled by a legacy filesystem wrapper.
====================
WHY DO WE WANT THIS?
====================
Firstly, there's a bunch of problems with the mount(2) syscall:
(1) It's actually six or seven different interfaces rolled into one and
weird combinations of flags make it do different things beyond the
original specification of the syscall.
(2) It produces a particularly large and diverse set of errors, which have
to be mapped back to a small error code. Yes, there's dmesg - if you
have it configured - but you can't necessarily see that if you're
doing a mount inside of a container.
(3) It copies a PAGE_SIZE block of data for each of the type, device name
and options.
(4) The size of the buffers is PAGE_SIZE - and this is arch dependent.
(5) You can't mount into another mount namespace. I could, for example,
build a container without having to be in that container's namespace
if I can do it from outside.
(6) It's not really geared for the specification of multiple sources, but
some filesystems really want that - overlayfs, for example.
and some problems in the internal kernel api:
(1) There's no defined way to supply namespace configuration for the
superblock - so, for instance, I can't say that I want to create a
superblock in a particular network namespace (on automount, say).
NFS hacks around this by creating multiple shadow file_system_types
with different ->mount() ops.
(2) When calling mount internally, unless you have NFS-like hacks, you
have to generate or otherwise provide text config data which then gets
parsed, when some of the time you could bypass the parsing stage
entirely.
(3) The amount of data in the data buffer is not known, but the data
buffer might be on a kernel stack somewhere, leading to the
possibility of tripping the stack underrun guard.
and other issues too:
(1) Superblock remount in some filesystems applies options on an as-parsed
basis, so if there's a parse failure, a partial alteration with no
rollback is effected.
(2) Under some circumstances, the mount data may get copied multiple times
so that it can have multiple parsers applied to it or because it has
to be parsed multiple times - for instance, once to get the
preliminary info required to access the on-disk superblock and then
again to update the superblock record in the kernel.
I want to be able to add support for a bunch of things:
(1) UID, GID and Project ID mapping/translation. I want to be able to
install a translation table of some sort on the superblock to
translate source identifiers (which may be foreign numeric UIDs/GIDs,
text names, GUIDs) into system identifiers. This needs to be done
before the superblock is published[*].
Note that this may, for example, involve using the context and the
superblock held therein to issue an RPC to a server to look up
translations.
[*] By "published" I mean made available through mount so that other
userspace processes can access it by path.
Maybe specifying a translation range element with something like:
fsconfig(fd, fsconfig_translate_uid, "<srcuid> <nsuid> <count>", 0, 0);
The translation information also needs to propagate over an automount
in some circumstances.
(2) Namespace configuration. I want to be able to tell the superblock
creation process what namespaces should be applied when it created (in
particular the userns and netns) for containerisation purposes, e.g.:
fsconfig(fd, FSCONFIG_SET_NAMESPACE, "user", 0, userns_fd);
fsconfig(fd, FSCONFIG_SET_NAMESPACE, "net", 0, netns_fd);
(3) Namespace propagation. I want to have a properly defined mechanism
for propagating namespace configuration over automounts within the
kernel. This will be particularly useful for network filesystems.
(4) Pre-mount attribute query. A chunk of the changes is actually the
fsinfo() syscall to query attributes of the filesystem beyond what's
available in statx() and statfs(). This will allow a created
superblock to be queried before it is published.
(5) Upcall for configuration. I would like to be able to query
configuration that's stored in userspace when an automount is made.
For instance, to look up network parameters for NFS or to find a cache
selector for fscache.
The internal fs_context could be passed to the upcall process or the
kernel could read a config file directly if named appropriately for the
superblock, perhaps:
[/etc/fscontext.d/afs/example.com/cell.cfg]
realm = EXAMPLE.COM
translation = uid,3000,4000,100
fscache = tag=fred
(6) Event notifications. I want to be able to install a watch on a
superblock before it is published to catch things like quota events
and EIO.
(7) Large and binary parameters. There might be at some point a need to
pass large/binary objects like Microsoft PACs around. If I understand
PACs correctly, you can obtain these from the Kerberos server and then
pass them to the file server when you connect.
Having it possible to pass large or binary objects as individual
fsconfig calls make parsing these trivial. OTOH, some or all of this
can potentially be handled with the use of the keyrings interface - as
the afs filesystem does for passing kerberos tokens around; it's just
that that seems overkill for a parameter you may only need once.
===================
SIGNIFICANT CHANGES
===================
ver #12:
(*) Rebased on v4.19-rc3.
(*) Added three new context purposes: mount for hidden root, reconfigure
for unmount, reconfigure for emergency remount.
(*) Added a parameter for the new purpose into vfs_dup_fs_context().
(*) Moved the reconfiguration hook from struct super_operations to struct
fs_context_operations so they can be handled through the legacy
wrapper. mount -o remount now goes through that.
(*) Changed the parameter description in the following ways:
- Nominated one master name for each parameter, held in a simple
string pointer array. This makes it easy to simply look up a name
for that parameter for logging.
- Added a table of additional names for parameters. The name chosen
can be used to influence the action of the parameter.
- Noted which parameter is the source specifier, if there is one.
(*) Use correct user_ns for a new pidns superblock.
(*) Fix mqueue to not crash on mounting.
(*) Make VFS sample programs dependent on X86 to avoid errors in
autobuilders due to unset syscall IDs in other arches.
(*) [Miklós] Fixed subtype handling.
ver #11:
(*) Fixed AppArmor.
(*) Capitalised all the UAPI constants.
(*) Explicitly numbered the FSCONFIG_* UAPI constants.
(*) Removed all the places ANON_INODES is selected.
(*) Fixed a bug whereby the context gets freed twice (which broke mounts of
procfs).
(*) Split fsinfo() off into its own patch series.
ver #10:
(*) Renamed "option" to "parameter" in a number of places.
(*) Replaced the use of write() to drive the configuration with an fsconfig()
syscall. This also allows at-style paths and fds to be presented as typed
object.
(*) Routed the key=value parameter concept all the way through from the
fsconfig() system call to the LSM and filesystem.
(*) Added a parameter-description concept and helper functions to help
interpret a parameter and possibly convert the value.
(*) Made it possible to query the parameter description using the fsinfo()
syscall. Added a test-fs-query sample to dump the parameters used by a
filesystem.
ver #9:
(*) Dropped the fd cookie stuff and the FMODE_*/O_* split stuff.
(*) Al added an open_tree() system call to allow a mount tree to be picked
referenced or cloned into an O_PATH-style fd. This can then be used
with sys_move_mount(). Dropped the O_CLONE_MOUNT and O_NON_RECURSIVE
open() flags.
(*) Brought error logging back in, though only in the fs_context and not
in the task_struct.
(*) Separated MS_REMOUNT|MS_BIND handling from MS_REMOUNT handling.
(*) Used anon_inodes for the fd returned by fsopen() and fspick(). This
requires making it unconditional.
(*) Fixed lots of bugs. Especial thanks to Al and Eric Biggers for
finding them and providing patches.
(*) Wrote manual pages, which I'll post separately.
ver #8:
(*) Changed the way fsmount() mounts into the namespace according to some
of Al's ideas.
(*) Put better typing on the fd cookie obtained from __fdget() & co..
(*) Stored the fd cookie in struct nameidata rather than the dfd number.
(*) Changed sys_fsmount() to return an O_PATH-style fd rather than
actually mounting into the mount namespace.
(*) Separated internal FMODE_* handling from O_* handling to free up
certain O_* flag numbers.
(*) Added two new open flags (O_CLONE_MOUNT and O_NON_RECURSIVE) for use
with open(O_PATH) to copy a mount or mount-subtree to an O_PATH fd.
(*) Added a new syscall, sys_move_mount(), to move a mount from an
dfd+path source to a dfd+path destination.
(*) Added a file->f_mode flag (FMODE_NEED_UNMOUNT) that indicates that the
vfsmount attached to file->f_path needs 'unmounting' if set.
(*) Made sys_move_mount() clear FMODE_NEED_UNMOUNT if successful.
[!] This doesn't work quite right.
(*) Added a new syscall, fsinfo(), to query information about a
filesystem. The idea being that this will, in future, work with the
fd from fsopen() too and permit querying of the parameters and
metadata before fsmount() is called.
ver #7:
(*) Undo an incorrect MS_* -> SB_* conversion.
(*) Pass the mount data buffer size to all the mount-related functions that
take the data pointer. This fixes a problem where someone (say SELinux)
tries to copy the mount data, assuming it to be a page in size, and
overruns the buffer - thereby incurring an oops by hitting a guard page.
(*) Made the AFS filesystem use them as an example. This is a much easier to
deal with than with NFS or Ext4 as there are very few mount options.
ver #6:
(*) Dropped the supplementary error string facility for the moment.
(*) Dropped the NFS patches for the moment.
(*) Dropped the reserved file descriptor argument from fsopen() and
replaced it with three reserved pointers that must be NULL.
ver #5:
(*) Renamed sb_config -> fs_context and adjusted variable names.
(*) Differentiated the flags in sb->s_flags (now named SB_*) from those
passed to mount(2) (named MS_*).
(*) Renamed __vfs_new_fs_context() to vfs_new_fs_context() and made the
caller always provide a struct file_system_type pointer and the
parameters required.
(*) Got rid of vfs_submount_fc() in favour of passing
FS_CONTEXT_FOR_SUBMOUNT to vfs_new_fs_context(). The purpose is now
used more.
(*) Call ->validate() on the remount path.
(*) Got rid of the inode locking in sys_fsmount().
(*) Call security_sb_mountpoint() in the mount(2) path.
ver #4:
(*) Split the sb_config patch up somewhat.
(*) Made the supplementary error string facility something attached to the
task_struct rather than the sb_config so that error messages can be
obtained from NFS doing a mount-root-and-pathwalk inside the
nfs_get_tree() operation.
Further, made this managed and read by prctl rather than through the
mount fd so that it's more generally available.
ver #3:
(*) Rebased on 4.12-rc1.
(*) Split the NFS patch up somewhat.
ver #2:
(*) Removed the ->fill_super() from sb_config_operations and passed it in
directly to functions that want to call it. NFS now calls
nfs_fill_super() directly rather than jumping through a pointer to it
since there's only the one option at the moment.
(*) Removed ->mnt_ns and ->sb from sb_config and moved ->pid_ns into
proc_sb_config.
(*) Renamed create_super -> get_tree.
(*) Renamed struct mount_context to struct sb_config and amended various
variable names.
(*) sys_fsmount() acquired AT_* flags and MS_* flags (for MNT_* flags)
arguments.
ver #1:
(*) Split the sb_config stuff out into its own header.
(*) Support non-context aware filesystems through a special set of
sb_config operations.
(*) Stored the created superblock and root dentry into the sb_config after
creation rather than directly into a vfsmount. This allows some
arguments to be removed to various NFS functions.
(*) Added an explicit superblock-creation step. This allows a created
superblock to then be mounted multiple times.
(*) Added a flag to say that the sb_config is degraded and cannot have
another go at having a superblock creation whilst getting rid of the
one that says it's already mounted.
Possible further developments:
(*) Implement sb reconfiguration (for now it returns ENOANO).
(*) Implement mount context support in more filesystems, ext4 being next
on my list.
(*) Move the walk-from-root stuff that nfs has to generic code so that you
can do something akin to:
mount /dev/sda1:/foo/bar /mnt
See nfs_follow_remote_path() and mount_subtree(). This is slightly
tricky in NFS as we have to prevent referral loops.
(*) Work out how to get at the error message incurred by submounts
encountered during nfs_follow_remote_path().
Should the error message be moved to task_struct and made more
general, perhaps retrieved with a prctl() function?
(*) Clean up/consolidate the security functions. Possibly add a
validation hook to be called at the same time as the mount context
validate op.
The patches can be found here also:
https://git.kernel.org/pub/scm/linux/kernel/git/dhowells/linux-fs.git
on branch:
mount-api
David
---
Al Viro (2):
vfs: syscall: Add open_tree(2) to reference or clone a mount
teach move_mount(2) to work with OPEN_TREE_CLONE
David Howells (32):
vfs: syscall: Add move_mount(2) to move mounts around
vfs: Suppress MS_* flag defs within the kernel unless explicitly enabled
vfs: Introduce the basic header for the new mount API's filesystem context
vfs: Introduce logging functions
vfs: Add configuration parser helpers
vfs: Add LSM hooks for the new mount API
vfs: Put security flags into the fs_context struct
selinux: Implement the new mount API LSM hooks
smack: Implement filesystem context security hooks
apparmor: Implement security hooks for the new mount API
tomoyo: Implement security hooks for the new mount API
vfs: Separate changing mount flags full remount
vfs: Implement a filesystem superblock creation/configuration context
vfs: Remove unused code after filesystem context changes
procfs: Move proc_fill_super() to fs/proc/root.c
proc: Add fs_context support to procfs
ipc: Convert mqueue fs to fs_context
cpuset: Use fs_context
kernfs, sysfs, cgroup, intel_rdt: Support fs_context
hugetlbfs: Convert to fs_context
vfs: Remove kern_mount_data()
vfs: Provide documentation for new mount API
Make anon_inodes unconditional
vfs: syscall: Add fsopen() to prepare for superblock creation
vfs: Implement logging through fs_context
vfs: Add some logging to the core users of the fs_context log
vfs: syscall: Add fsconfig() for configuring and managing a context
vfs: syscall: Add fsmount() to create a mount for a superblock
vfs: syscall: Add fspick() to select a superblock for reconfiguration
afs: Add fs_context support
afs: Use fs_context to pass parameters over automount
vfs: Add a sample program for the new mount API
Documentation/filesystems/mount_api.txt | 741 +++++++++++++++++++++++++
arch/arc/kernel/setup.c | 1
arch/arm/kernel/atags_parse.c | 1
arch/arm/kvm/Kconfig | 1
arch/arm64/kvm/Kconfig | 1
arch/mips/kvm/Kconfig | 1
arch/powerpc/kvm/Kconfig | 1
arch/s390/kvm/Kconfig | 1
arch/sh/kernel/setup.c | 1
arch/sparc/kernel/setup_32.c | 1
arch/sparc/kernel/setup_64.c | 1
arch/x86/Kconfig | 1
arch/x86/entry/syscalls/syscall_32.tbl | 6
arch/x86/entry/syscalls/syscall_64.tbl | 6
arch/x86/kernel/cpu/intel_rdt.h | 15 +
arch/x86/kernel/cpu/intel_rdt_rdtgroup.c | 183 ++++--
arch/x86/kernel/setup.c | 1
arch/x86/kvm/Kconfig | 1
drivers/base/Kconfig | 1
drivers/base/devtmpfs.c | 1
drivers/char/tpm/Kconfig | 1
drivers/dma-buf/Kconfig | 1
drivers/gpio/Kconfig | 1
drivers/iio/Kconfig | 1
drivers/infiniband/Kconfig | 1
drivers/vfio/Kconfig | 1
fs/Kconfig | 7
fs/Makefile | 5
fs/afs/internal.h | 9
fs/afs/mntpt.c | 148 +++--
fs/afs/super.c | 470 +++++++++-------
fs/afs/volume.c | 4
fs/f2fs/super.c | 2
fs/file_table.c | 9
fs/filesystems.c | 4
fs/fs_context.c | 769 ++++++++++++++++++++++++++
fs/fs_parser.c | 555 +++++++++++++++++++
fs/fsopen.c | 563 +++++++++++++++++++
fs/hugetlbfs/inode.c | 391 ++++++++-----
fs/internal.h | 19 +
fs/kernfs/mount.c | 88 +--
fs/libfs.c | 20 +
fs/namei.c | 4
fs/namespace.c | 895 +++++++++++++++++++++++-------
fs/notify/fanotify/Kconfig | 1
fs/notify/inotify/Kconfig | 1
fs/pnode.c | 1
fs/proc/inode.c | 50 --
fs/proc/internal.h | 5
fs/proc/root.c | 256 ++++++---
fs/super.c | 464 ++++++++++++----
fs/sysfs/mount.c | 67 ++
include/linux/cgroup.h | 3
include/linux/errno.h | 1
include/linux/fs.h | 23 +
include/linux/fs_context.h | 215 +++++++
include/linux/fs_parser.h | 119 ++++
include/linux/kernfs.h | 41 +
include/linux/lsm_hooks.h | 79 ++-
include/linux/module.h | 6
include/linux/mount.h | 5
include/linux/security.h | 65 ++
include/linux/syscalls.h | 9
include/uapi/linux/fcntl.h | 2
include/uapi/linux/fs.h | 82 +--
include/uapi/linux/mount.h | 75 +++
init/Kconfig | 10
init/do_mounts.c | 1
init/do_mounts_initrd.c | 1
ipc/mqueue.c | 107 +++-
ipc/namespace.c | 2
kernel/cgroup/cgroup-internal.h | 50 +-
kernel/cgroup/cgroup-v1.c | 345 ++++++------
kernel/cgroup/cgroup.c | 264 ++++++---
kernel/cgroup/cpuset.c | 69 ++
samples/Kconfig | 10
samples/Makefile | 2
samples/statx/Makefile | 7
samples/statx/test-statx.c | 258 ---------
samples/vfs/Makefile | 10
samples/vfs/test-fsmount.c | 118 ++++
samples/vfs/test-statx.c | 258 +++++++++
security/apparmor/include/mount.h | 11
security/apparmor/lsm.c | 108 ++++
security/apparmor/mount.c | 47 ++
security/security.c | 56 ++
security/selinux/hooks.c | 387 ++++++++++---
security/selinux/include/security.h | 16 -
security/smack/smack.h | 21 -
security/smack/smack_lsm.c | 365 +++++++++++-
security/tomoyo/common.h | 3
security/tomoyo/mount.c | 46 ++
security/tomoyo/tomoyo.c | 15 +
93 files changed, 7191 insertions(+), 1900 deletions(-)
create mode 100644 Documentation/filesystems/mount_api.txt
create mode 100644 fs/fs_context.c
create mode 100644 fs/fs_parser.c
create mode 100644 fs/fsopen.c
create mode 100644 include/linux/fs_context.h
create mode 100644 include/linux/fs_parser.h
create mode 100644 include/uapi/linux/mount.h
delete mode 100644 samples/statx/Makefile
delete mode 100644 samples/statx/test-statx.c
create mode 100644 samples/vfs/Makefile
create mode 100644 samples/vfs/test-fsmount.c
create mode 100644 samples/vfs/test-statx.c
From: David Howells <dhowells@redhat.com> Date: 2018-09-21 16:34:12
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 | 1
arch/x86/entry/syscalls/syscall_64.tbl | 1
fs/namespace.c | 125 +++++++++++++++++++++++++++++++-
include/linux/syscalls.h | 1
include/uapi/linux/fs.h | 2 +
5 files changed, 126 insertions(+), 4 deletions(-)
@@ -347,6 +347,7 @@ 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
@@ -3329,9 +3329,126 @@ struct vfsmount *kern_mount(struct file_system_type *type)EXPORT_SYMBOL_GPL(kern_mount);/*-*Moveamountfromoneplacetoanother.-*Incombinationwithopen_tree(OPEN_TREE_CLONE[|AT_RECURSIVE])itcanbe-*usedtocopyamountsubtree.+*Createakernelmountrepresentationforanew,preparedsuperblock+*(specifiedbyfs_fd)andattachtoanopen_tree-likefiledescriptor.+*/+SYSCALL_DEFINE3(fsmount,int,fs_fd,unsignedint,flags,unsignedint,ms_flags)+{+structfs_context*fc;+structfile*file;+structpathnewmount;+structfdf;+unsignedintmnt_flags=0;+longret;++if(!may_mount())+return-EPERM;++if((flags&~(FSMOUNT_CLOEXEC))!=0)+return-EINVAL;++if(ms_flags&~(MS_RDONLY|MS_NOSUID|MS_NODEV|MS_NOEXEC|+MS_NOATIME|MS_NODIRATIME|MS_RELATIME|+MS_STRICTATIME))+return-EINVAL;++if(ms_flags&MS_RDONLY)+mnt_flags|=MNT_READONLY;+if(ms_flags&MS_NOSUID)+mnt_flags|=MNT_NOSUID;+if(ms_flags&MS_NODEV)+mnt_flags|=MNT_NODEV;+if(ms_flags&MS_NOEXEC)+mnt_flags|=MNT_NOEXEC;+if(ms_flags&MS_NODIRATIME)+mnt_flags|=MNT_NODIRATIME;++if(ms_flags&MS_STRICTATIME){+if(ms_flags&MS_NOATIME)+return-EINVAL;+}elseif(ms_flags&MS_NOATIME){+mnt_flags|=MNT_NOATIME;+}else{+mnt_flags|=MNT_RELATIME;+}++f=fdget(fs_fd);+if(!f.file)+return-EBADF;++ret=-EINVAL;+if(f.file->f_op!=&fscontext_fops)+gotoerr_fsfd;++fc=f.file->private_data;++/* There must be a valid superblock or we can't mount it */+ret=-EINVAL;+if(!fc->root)+gotoerr_fsfd;++ret=-EPERM;+if(mount_too_revealing(fc->root->d_sb,&mnt_flags)){+pr_warn("VFS: Mount too revealing\n");+gotoerr_fsfd;+}++ret=mutex_lock_interruptible(&fc->uapi_mutex);+if(ret<0)+gotoerr_fsfd;++ret=-EBUSY;+if(fc->phase!=FS_CONTEXT_AWAITING_MOUNT)+gotoerr_unlock;++ret=-EPERM;+if((fc->sb_flags&SB_MANDLOCK)&&!may_mandlock())+gotoerr_unlock;++newmount.mnt=vfs_create_mount(fc,mnt_flags);+if(IS_ERR(newmount.mnt)){+ret=PTR_ERR(newmount.mnt);+gotoerr_unlock;+}+newmount.dentry=dget(fc->root);++/* We've done the mount bit - now move the file context into more or+*lessthesamestateasifwe'ddoneanfspick().Wedon'twantto+*doanymemoryallocationoranythinglikethatatthispointaswe+*don'twanttohavetohandleanyerrorsincurred.+*/+vfs_clean_context(fc);++/* Attach to an apparent O_PATH fd with a note that we need to unmount+*it,notjustsimplyputit.+*/+file=dentry_open(&newmount,O_PATH,fc->cred);+if(IS_ERR(file)){+ret=PTR_ERR(file);+gotoerr_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);+returnret;+}++/*+*Moveamountfromoneplacetoanother.Incombinationwith+*fsopen()/fsmount()thisisusedtoinstallanewmountandincombination+*withopen_tree(OPEN_TREE_CLONE[|AT_RECURSIVE])itcanbeusedtocopy+*amountsubtree.**NotetheflagsvalueisacombinationofMOVE_MOUNT_*flags.*/
From: David Howells <dhowells@redhat.com> Date: 2018-09-21 22:20:00
From: Al Viro <viro@zeniv.linux.org.uk>
open_tree(dfd, pathname, flags)
Returns an O_PATH-opened file descriptor or an error.
dfd and pathname specify the location to open, in usual
fashion (see e.g. fstatat(2)). flags should be an OR of
some of the following:
* AT_PATH_EMPTY, AT_NO_AUTOMOUNT, AT_SYMLINK_NOFOLLOW -
same meanings as usual
* OPEN_TREE_CLOEXEC - make the resulting descriptor
close-on-exec
* OPEN_TREE_CLONE or OPEN_TREE_CLONE | AT_RECURSIVE -
instead of opening the location in question, create a detached
mount tree matching the subtree rooted at location specified by
dfd/pathname. With AT_RECURSIVE the entire subtree is cloned,
without it - only the part within in the mount containing the
location in question. In other words, the same as mount --rbind
or mount --bind would've taken. The detached tree will be
dissolved on the final close of obtained file. Creation of such
detached trees requires the same capabilities as doing mount --bind.
Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
Signed-off-by: David Howells <dhowells@redhat.com>
cc: linux-api@vger.kernel.org
---
arch/x86/entry/syscalls/syscall_32.tbl | 1
arch/x86/entry/syscalls/syscall_64.tbl | 1
fs/file_table.c | 9 +-
fs/internal.h | 1
fs/namespace.c | 132 +++++++++++++++++++++++++++-----
include/linux/fs.h | 7 +-
include/linux/syscalls.h | 1
include/uapi/linux/fcntl.h | 2
include/uapi/linux/mount.h | 10 ++
9 files changed, 137 insertions(+), 27 deletions(-)
create mode 100644 include/uapi/linux/mount.h
@@ -343,6 +343,7 @@ 332 common statx __x64_sys_statx 333 common io_pgetevents __x64_sys_io_pgetevents 334 common rseq __x64_sys_rseq+335 common open_tree __x64_sys_open_tree # # x32-specific system call numbers start at 512 to avoid cache impact
@@ -90,5 +90,7 @@#define AT_STATX_FORCE_SYNC 0x2000 /* - Force the attributes to be sync'd with the server */#define AT_STATX_DONT_SYNC 0x4000 /* - Don't sync attributes with the server */+#define AT_RECURSIVE 0x8000 /* Apply to the entire subtree */+#endif /* _UAPI_LINUX_FCNTL_H */
@@ -0,0 +1,10 @@+#ifndef _UAPI_LINUX_MOUNT_H+#define _UAPI_LINUX_MOUNT_H++/*+*open_tree()flags.+*/+#define OPEN_TREE_CLONE 1 /* Clone the target tree and attach the clone */+#define OPEN_TREE_CLOEXEC O_CLOEXEC /* Close the file on execve() */++#endif /* _UAPI_LINUX_MOUNT_H */
From: David Howells <dhowells@redhat.com> Date: 2018-09-21 22:20:08
Add a move_mount() system call that will move a mount from one place to
another and, in the next commit, allow to attach an unattached mount tree.
The new system call looks like the following:
int move_mount(int from_dfd, const char *from_path,
int to_dfd, const char *to_path,
unsigned int flags);
Signed-off-by: David Howells <dhowells@redhat.com>
cc: linux-api@vger.kernel.org
---
arch/x86/entry/syscalls/syscall_32.tbl | 1
arch/x86/entry/syscalls/syscall_64.tbl | 1
fs/namespace.c | 102 ++++++++++++++++++++++++++------
include/linux/lsm_hooks.h | 6 ++
include/linux/security.h | 7 ++
include/linux/syscalls.h | 3 +
include/uapi/linux/mount.h | 11 +++
security/security.c | 5 ++
8 files changed, 118 insertions(+), 18 deletions(-)
@@ -344,6 +344,7 @@ 333 common io_pgetevents __x64_sys_io_pgetevents 334 common rseq __x64_sys_rseq 335 common open_tree __x64_sys_open_tree+336 common move_mount __x64_sys_move_mount # # x32-specific system call numbers start at 512 to avoid cache impact
@@ -2386,43 +2386,37 @@ static inline int tree_contains_unbindable(struct mount *mnt)return0;}-staticintdo_move_mount(structpath*path,constchar*old_name)+staticintdo_move_mount(structpath*old_path,structpath*new_path){-structpathold_path,parent_path;+structpathparent_path={.mnt=NULL,.dentry=NULL};structmount*p;structmount*old;structmountpoint*mp;interr;-if(!old_name||!*old_name)-return-EINVAL;-err=kern_path(old_name,LOOKUP_FOLLOW,&old_path);-if(err)-returnerr;-mp=lock_mount(path);+mp=lock_mount(new_path);err=PTR_ERR(mp);if(IS_ERR(mp))gotoout;-old=real_mount(old_path.mnt);-p=real_mount(path->mnt);+old=real_mount(old_path->mnt);+p=real_mount(new_path->mnt);err=-EINVAL;if(!check_mnt(p)||!check_mnt(old))gotoout1;-if(old->mnt.mnt_flags&MNT_LOCKED)+if(!mnt_has_parent(old))gotoout1;-err=-EINVAL;-if(old_path.dentry!=old_path.mnt->mnt_root)+if(old->mnt.mnt_flags&MNT_LOCKED)gotoout1;-if(!mnt_has_parent(old))+if(old_path->dentry!=old_path->mnt->mnt_root)gotoout1;-if(d_is_dir(path->dentry)!=-d_is_dir(old_path.dentry))+if(d_is_dir(new_path->dentry)!=+d_is_dir(old_path->dentry))gotoout1;/**Don'tmoveamountresidinginasharedparent.
@@ -7,4 +7,15 @@#define OPEN_TREE_CLONE 1 /* Clone the target tree and attach the clone */#define OPEN_TREE_CLOEXEC O_CLOEXEC /* Close the file on execve() */+/*+*move_mount()flags.+*/+#define MOVE_MOUNT_F_SYMLINKS 0x00000001 /* Follow symlinks on from path */+#define MOVE_MOUNT_F_AUTOMOUNTS 0x00000002 /* Follow automounts on from path */+#define MOVE_MOUNT_F_EMPTY_PATH 0x00000004 /* Empty from path permitted */+#define MOVE_MOUNT_T_SYMLINKS 0x00000010 /* Follow symlinks on to path */+#define MOVE_MOUNT_T_AUTOMOUNTS 0x00000020 /* Follow automounts on to path */+#define MOVE_MOUNT_T_EMPTY_PATH 0x00000040 /* Empty to path permitted */+#define MOVE_MOUNT__MASK 0x00000077+#endif /* _UAPI_LINUX_MOUNT_H */
From: David Howells <dhowells@redhat.com> Date: 2018-09-21 22:23:23
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
---
arch/x86/entry/syscalls/syscall_32.tbl | 1
arch/x86/entry/syscalls/syscall_64.tbl | 1
fs/Makefile | 2 -
fs/fs_context.c | 4 +
fs/fsopen.c | 87 ++++++++++++++++++++++++++++++++
include/linux/fs_context.h | 18 +++++++
include/linux/syscalls.h | 1
include/uapi/linux/fs.h | 5 ++
8 files changed, 118 insertions(+), 1 deletion(-)
create mode 100644 fs/fsopen.c
@@ -345,6 +345,7 @@ 334 common rseq __x64_sys_rseq 335 common open_tree __x64_sys_open_tree 336 common move_mount __x64_sys_move_mount+337 common fsopen __x64_sys_fsopen # # x32-specific system call numbers start at 512 to avoid cache impact
@@ -37,6 +38,19 @@ enum fs_context_purpose {FS_CONTEXT_FOR_EMERGENCY_RO,/* Emergency reconfiguration to R/O */};+/*+*Userspaceusagephaseforfsopen/fspick.+*/+enumfs_context_phase{+FS_CONTEXT_CREATE_PARAMS,/* Loading params for sb creation */+FS_CONTEXT_CREATING,/* A superblock is being created */+FS_CONTEXT_AWAITING_MOUNT,/* Superblock created, awaiting fsmount() */+FS_CONTEXT_AWAITING_RECONF,/* Awaiting initialisation for reconfiguration */+FS_CONTEXT_RECONF_PARAMS,/* Loading params for reconfiguration */+FS_CONTEXT_RECONFIGURING,/* Reconfiguring the superblock */+FS_CONTEXT_FAILED,/* Failed to correctly transition a context */+};+/**Typeofparametervalue.*/
@@ -77,6 +91,7 @@ struct fs_parameter {*/structfs_context{conststructfs_context_operations*ops;+structmutexuapi_mutex;/* Userspace access mutex */structfile_system_type*fs_type;void*fs_private;/* The filesystem's context */structdentry*root;/* The root and superblock */
@@ -91,6 +106,7 @@ struct fs_context {unsignedintsb_flags_mask;/* Superblock flags that were changed */unsignedintlsm_flags;/* Information flags from the fs to the LSM */enumfs_context_purposepurpose:8;+enumfs_context_phasephase:8;/* The phase the context is in */boolsloppy:1;/* T if unrecognised options are okay */boolsilent:1;/* T if "o silent" specified */boolneed_free:1;/* Need to call ops->free() */
From: David Howells <dhowells@redhat.com> Date: 2018-09-21 22:23:46
Add a syscall for configuring a filesystem creation context and triggering
actions upon it, to be used in conjunction with fsopen, fspick and fsmount.
long fsconfig(int fs_fd, unsigned int cmd, const char *key,
const void *value, int aux);
Where fs_fd indicates the context, cmd indicates the action to take, key
indicates the parameter name for parameter-setting actions and, if needed,
value points to a buffer containing the value and aux can give more
information for the value.
The following command IDs are proposed:
(*) FSCONFIG_SET_FLAG: No value is specified. The parameter must be
boolean in nature. The key may be prefixed with "no" to invert the
setting. value must be NULL and aux must be 0.
(*) FSCONFIG_SET_STRING: A string value is specified. The parameter can
be expecting boolean, integer, string or take a path. A conversion to
an appropriate type will be attempted (which may include looking up as
a path). value points to a NUL-terminated string and aux must be 0.
(*) FSCONFIG_SET_BINARY: A binary blob is specified. value points to
the blob and aux indicates its size. The parameter must be expecting
a blob.
(*) FSCONFIG_SET_PATH: A non-empty path is specified. The parameter must
be expecting a path object. value points to a NUL-terminated string
that is the path and aux is a file descriptor at which to start a
relative lookup or AT_FDCWD.
(*) FSCONFIG_SET_PATH_EMPTY: As fsconfig_set_path, but with AT_EMPTY_PATH
implied.
(*) FSCONFIG_SET_FD: An open file descriptor is specified. value must
be NULL and aux indicates the file descriptor.
(*) FSCONFIG_CMD_CREATE: Trigger superblock creation.
(*) FSCONFIG_CMD_RECONFIGURE: Trigger superblock reconfiguration.
For the "set" command IDs, the idea is that the file_system_type will point
to a list of parameters and the types of value that those parameters expect
to take. The core code can then do the parse and argument conversion and
then give the LSM and FS a cooked option or array of options to use.
Source specification is also done the same way same way, using special keys
"source", "source1", "source2", etc..
[!] Note that, for the moment, the key and value are just glued back
together and handed to the filesystem. Every filesystem that uses options
uses match_token() and co. to do this, and this will need to be changed -
but not all at once.
Example usage:
fd = fsopen("ext4", FSOPEN_CLOEXEC);
fsconfig(fd, fsconfig_set_path, "source", "/dev/sda1", AT_FDCWD);
fsconfig(fd, fsconfig_set_path_empty, "journal_path", "", journal_fd);
fsconfig(fd, fsconfig_set_fd, "journal_fd", "", journal_fd);
fsconfig(fd, fsconfig_set_flag, "user_xattr", NULL, 0);
fsconfig(fd, fsconfig_set_flag, "noacl", NULL, 0);
fsconfig(fd, fsconfig_set_string, "sb", "1", 0);
fsconfig(fd, fsconfig_set_string, "errors", "continue", 0);
fsconfig(fd, fsconfig_set_string, "data", "journal", 0);
fsconfig(fd, fsconfig_set_string, "context", "unconfined_u:...", 0);
fsconfig(fd, fsconfig_cmd_create, NULL, NULL, 0);
mfd = fsmount(fd, FSMOUNT_CLOEXEC, MS_NOEXEC);
or:
fd = fsopen("ext4", FSOPEN_CLOEXEC);
fsconfig(fd, fsconfig_set_string, "source", "/dev/sda1", 0);
fsconfig(fd, fsconfig_cmd_create, NULL, NULL, 0);
mfd = fsmount(fd, FSMOUNT_CLOEXEC, MS_NOEXEC);
or:
fd = fsopen("afs", FSOPEN_CLOEXEC);
fsconfig(fd, fsconfig_set_string, "source", "#grand.central.org:root.cell", 0);
fsconfig(fd, fsconfig_cmd_create, NULL, NULL, 0);
mfd = fsmount(fd, FSMOUNT_CLOEXEC, MS_NOEXEC);
or:
fd = fsopen("jffs2", FSOPEN_CLOEXEC);
fsconfig(fd, fsconfig_set_string, "source", "mtd0", 0);
fsconfig(fd, fsconfig_cmd_create, NULL, NULL, 0);
mfd = fsmount(fd, FSMOUNT_CLOEXEC, MS_NOEXEC);
Signed-off-by: David Howells <dhowells@redhat.com>
cc: linux-api@vger.kernel.org
---
arch/x86/entry/syscalls/syscall_32.tbl | 1
arch/x86/entry/syscalls/syscall_64.tbl | 1
fs/fsopen.c | 355 ++++++++++++++++++++++++++++++++
include/linux/syscalls.h | 2
include/uapi/linux/fs.h | 14 +
5 files changed, 373 insertions(+)
@@ -346,6 +346,7 @@ 335 common open_tree __x64_sys_open_tree 336 common move_mount __x64_sys_move_mount 337 common fsopen __x64_sys_fsopen+338 common fsconfig __x64_sys_fsconfig # # x32-specific system call numbers start at 512 to avoid cache impact
@@ -152,3 +154,356 @@ SYSCALL_DEFINE2(fsopen, const char __user *, _fs_name, unsigned int, flags)put_fs_context(fc);returnret;}++/*+*Checkthestateandapplytheconfiguration.Notethatthisfunctionis+*allowedto'steal'thevaluebysettingparam->xxxtoNULLbeforereturning.+*/+staticintvfs_fsconfig(structfs_context*fc,structfs_parameter*param)+{+intret;++/* We need to reinitialise the context if we have reconfiguration+*pendingaftercreationorapreviousreconfiguration.+*/+if(fc->phase==FS_CONTEXT_AWAITING_RECONF){+if(fc->fs_type->init_fs_context){+ret=fc->fs_type->init_fs_context(fc,fc->root);+if(ret<0){+fc->phase=FS_CONTEXT_FAILED;+returnret;+}+fc->need_free=true;+}else{+/* Leave legacy context ops in place */+}++/* Do the security check last because ->init_fs_context may+*changethenamespacesubscriptions.+*/+ret=security_fs_context_alloc(fc,fc->root);+if(ret<0){+fc->phase=FS_CONTEXT_FAILED;+returnret;+}++fc->phase=FS_CONTEXT_RECONF_PARAMS;+}++if(fc->phase!=FS_CONTEXT_CREATE_PARAMS&&+fc->phase!=FS_CONTEXT_RECONF_PARAMS)+return-EBUSY;++returnvfs_parse_fs_param(fc,param);+}++/*+*Reconfigureasuperblock.+*/+intvfs_reconfigure_sb(structfs_context*fc)+{+structsuper_block*sb=fc->root->d_sb;+intret;++if(fc->ops->validate){+ret=fc->ops->validate(fc);+if(ret<0)+returnret;+}++ret=security_fs_context_validate(fc);+if(ret)+returnret;++if(!ns_capable(sb->s_user_ns,CAP_SYS_ADMIN))+return-EPERM;++down_write(&sb->s_umount);+ret=reconfigure_super(fc);+up_write(&sb->s_umount);+returnret;+}++/*+*Cleanupacontextafterperforminganactiononitandputitintoastate+*fromwhereitcanbeusedtoreconfigureasuperblock.+*/+voidvfs_clean_context(structfs_context*fc)+{+if(fc->ops&&fc->ops->free)+fc->ops->free(fc);+fc->need_free=false;+fc->fs_private=NULL;+fc->s_fs_info=NULL;+fc->sb_flags=0;+fc->sloppy=false;+fc->silent=false;+security_fs_context_free(fc);+fc->security=NULL;+kfree(fc->subtype);+fc->subtype=NULL;+kfree(fc->source);+fc->source=NULL;++fc->purpose=FS_CONTEXT_FOR_RECONFIGURE;+fc->phase=FS_CONTEXT_AWAITING_RECONF;+}++/*+*Performanactiononacontext.+*/+staticintvfs_fsconfig_action(structfs_context*fc,enumfsconfig_commandcmd)+{+intret=-EINVAL;++switch(cmd){+caseFSCONFIG_CMD_CREATE:+if(fc->phase!=FS_CONTEXT_CREATE_PARAMS)+return-EBUSY;+fc->phase=FS_CONTEXT_CREATING;+ret=vfs_get_tree(fc);+if(ret==0)+fc->phase=FS_CONTEXT_AWAITING_MOUNT;+else+fc->phase=FS_CONTEXT_FAILED;+returnret;++caseFSCONFIG_CMD_RECONFIGURE:+if(fc->phase==FS_CONTEXT_AWAITING_RECONF){+/* This is probably pointless, since no changes have+*beenproposed.+*/+if(fc->fs_type->init_fs_context){+ret=fc->fs_type->init_fs_context(fc,fc->root);+if(ret<0){+fc->phase=FS_CONTEXT_FAILED;+returnret;+}+fc->need_free=true;+}+fc->phase=FS_CONTEXT_RECONF_PARAMS;+}++fc->phase=FS_CONTEXT_RECONFIGURING;+ret=vfs_reconfigure_sb(fc);+if(ret==0)+vfs_clean_context(fc);+else+fc->phase=FS_CONTEXT_FAILED;+returnret;++default:+return-EOPNOTSUPP;+}+}++/**+*sys_fsconfig-Setparametersandtriggeractionsonacontext+*@fd:Thefilesystemcontexttoactupon+*@cmd:Theactiontotake+*@_key:Whereappropriate,theparameterkeytoset+*@_value:Whereappropriate,theparametervaluetoset+*@aux:Additionalinformationforthevalue+*+*Thissystemcallisusedtosetparametersonacontext,including+*superblocksettings,datasourceandsecuritylabelling.+*+*Actionsincludetriggeringthecreationofasuperblockandthe+*reconfigurationofthesuperblockattachedtothespecifiedcontext.+*+*Whensettingaparameter,@cmdindicatesthetypeofvaluebeingproposed+*and@_keyindicatestheparametertobealtered.+*+*@_valueand@auxareusedtospecifythevalue,shouldavalueberequired:+*+*(*)fsconfig_set_flag:Novalueisspecified.Theparametermustbeboolean+*innature.Thekeymaybeprefixedwith"no"toinvertthe+*setting.@_valuemustbeNULLand@auxmustbe0.+*+*(*)fsconfig_set_string:Astringvalueisspecified.Theparametercanbe+*expectingboolean,integer,stringortakeapath.Aconversiontoan+*appropriatetypewillbeattempted(whichmayincludelookingupasa+*path).@_valuepointstoaNUL-terminatedstringand@auxmustbe0.+*+*(*)fsconfig_set_binary:Abinaryblobisspecified.@_valuepointstothe+*bloband@auxindicatesitssize.Theparametermustbeexpectinga+*blob.+*+*(*)fsconfig_set_path:Anon-emptypathisspecified.Theparametermustbe+*expectingapathobject.@_valuepointstoaNUL-terminatedstringthat+*isthepathand@auxisafiledescriptoratwhichtostartarelative+*lookuporAT_FDCWD.+*+*(*)fsconfig_set_path_empty:Asfsconfig_set_path,butwithAT_EMPTY_PATH+*implied.+*+*(*)fsconfig_set_fd:Anopenfiledescriptorisspecified.@_valuemustbe+*NULLand@auxindicatesthefiledescriptor.+*/+SYSCALL_DEFINE5(fsconfig,+int,fd,+unsignedint,cmd,+constchar__user*,_key,+constvoid__user*,_value,+int,aux)+{+structfs_context*fc;+structfdf;+intret;++structfs_parameterparam={+.type=fs_value_is_undefined,+};++if(fd<0)+return-EINVAL;++switch(cmd){+caseFSCONFIG_SET_FLAG:+if(!_key||_value||aux)+return-EINVAL;+break;+caseFSCONFIG_SET_STRING:+if(!_key||!_value||aux)+return-EINVAL;+break;+caseFSCONFIG_SET_BINARY:+if(!_key||!_value||aux<=0||aux>1024*1024)+return-EINVAL;+break;+caseFSCONFIG_SET_PATH:+caseFSCONFIG_SET_PATH_EMPTY:+if(!_key||!_value||(aux!=AT_FDCWD&&aux<0))+return-EINVAL;+break;+caseFSCONFIG_SET_FD:+if(!_key||_value||aux<0)+return-EINVAL;+break;+caseFSCONFIG_CMD_CREATE:+caseFSCONFIG_CMD_RECONFIGURE:+if(_key||_value||aux)+return-EINVAL;+break;+default:+return-EOPNOTSUPP;+}++f=fdget(fd);+if(!f.file)+return-EBADF;+ret=-EINVAL;+if(f.file->f_op!=&fscontext_fops)+gotoout_f;++fc=f.file->private_data;+if(fc->ops==&legacy_fs_context_ops){+switch(cmd){+caseFSCONFIG_SET_BINARY:+caseFSCONFIG_SET_PATH:+caseFSCONFIG_SET_PATH_EMPTY:+caseFSCONFIG_SET_FD:+ret=-EOPNOTSUPP;+gotoout_f;+}+}++if(_key){+param.key=strndup_user(_key,256);+if(IS_ERR(param.key)){+ret=PTR_ERR(param.key);+gotoout_f;+}+}++switch(cmd){+caseFSCONFIG_SET_STRING:+param.type=fs_value_is_string;+param.string=strndup_user(_value,256);+if(IS_ERR(param.string)){+ret=PTR_ERR(param.string);+gotoout_key;+}+param.size=strlen(param.string);+break;+caseFSCONFIG_SET_BINARY:+param.type=fs_value_is_blob;+param.size=aux;+param.blob=memdup_user_nul(_value,aux);+if(IS_ERR(param.blob)){+ret=PTR_ERR(param.blob);+gotoout_key;+}+break;+caseFSCONFIG_SET_PATH:+param.type=fs_value_is_filename;+param.name=getname_flags(_value,0,NULL);+if(IS_ERR(param.name)){+ret=PTR_ERR(param.name);+gotoout_key;+}+param.dirfd=aux;+param.size=strlen(param.name->name);+break;+caseFSCONFIG_SET_PATH_EMPTY:+param.type=fs_value_is_filename_empty;+param.name=getname_flags(_value,LOOKUP_EMPTY,NULL);+if(IS_ERR(param.name)){+ret=PTR_ERR(param.name);+gotoout_key;+}+param.dirfd=aux;+param.size=strlen(param.name->name);+break;+caseFSCONFIG_SET_FD:+param.type=fs_value_is_file;+ret=-EBADF;+param.file=fget(aux);+if(!param.file)+gotoout_key;+break;+default:+break;+}++ret=mutex_lock_interruptible(&fc->uapi_mutex);+if(ret==0){+switch(cmd){+caseFSCONFIG_CMD_CREATE:+caseFSCONFIG_CMD_RECONFIGURE:+ret=vfs_fsconfig_action(fc,cmd);+break;+default:+ret=vfs_fsconfig(fc,¶m);+break;+}+mutex_unlock(&fc->uapi_mutex);+}++/* Clean up the our record of any value that we obtained from+*userspace.NotethatthevaluemayhavebeenstolenbytheLSMor+*filesystem,inwhichcasethevaluepointerwillhavebeencleared.+*/+switch(cmd){+caseFSCONFIG_SET_STRING:+caseFSCONFIG_SET_BINARY:+kfree(param.string);+break;+caseFSCONFIG_SET_PATH:+caseFSCONFIG_SET_PATH_EMPTY:+if(param.name)+putname(param.name);+break;+caseFSCONFIG_SET_FD:+if(param.file)+fput(param.file);+break;+default:+break;+}+out_key:+kfree(param.key);+out_f:+fdput(f);+returnret;+}
@@ -349,4 +349,18 @@ typedef int __bitwise __kernel_rwf_t;*/#define FSOPEN_CLOEXEC 0x00000001+/*+*Thetypeoffsconfig()callmade.+*/+enumfsconfig_command{+FSCONFIG_SET_FLAG=0,/* Set parameter, supplying no value */+FSCONFIG_SET_STRING=1,/* Set parameter, supplying a string value */+FSCONFIG_SET_BINARY=2,/* Set parameter, supplying a binary blob value */+FSCONFIG_SET_PATH=3,/* Set parameter, supplying an object by path */+FSCONFIG_SET_PATH_EMPTY=4,/* Set parameter, supplying an object by (empty) path */+FSCONFIG_SET_FD=5,/* Set parameter, supplying an object by fd */+FSCONFIG_CMD_CREATE=6,/* Invoke superblock creation */+FSCONFIG_CMD_RECONFIGURE=7,/* Invoke superblock reconfiguration */+};+#endif /* _UAPI_LINUX_FS_H */
From: David Howells <dhowells@redhat.com> Date: 2018-09-21 22:24:02
Provide an fspick() system call that can be used to pick an existing
mountpoint into an fs_context which can thereafter be used to reconfigure a
superblock (equivalent of the superblock side of -o remount).
This looks like:
int fd = fspick(AT_FDCWD, "/mnt",
FSPICK_CLOEXEC | FSPICK_NO_AUTOMOUNT);
fsconfig(fd, FSCONFIG_SET_FLAG, "intr", NULL, 0);
fsconfig(fd, FSCONFIG_SET_FLAG, "noac", NULL, 0);
fsconfig(fd, FSCONFIG_CMD_RECONFIGURE, NULL, NULL, 0);
At the point of fspick being called, the file descriptor referring to the
filesystem context is in exactly the same state as the one that was created
by fsopen() after fsmount() has been successfully called.
Signed-off-by: David Howells <dhowells@redhat.com>
cc: linux-api@vger.kernel.org
---
arch/x86/entry/syscalls/syscall_32.tbl | 1 +
arch/x86/entry/syscalls/syscall_64.tbl | 1 +
fs/fsopen.c | 54 ++++++++++++++++++++++++++++++++
include/linux/syscalls.h | 1 +
include/uapi/linux/fs.h | 5 +++
5 files changed, 62 insertions(+)
@@ -348,6 +348,7 @@ 337 common fsopen __x64_sys_fsopen 338 common fsconfig __x64_sys_fsconfig 339 common fsmount __x64_sys_fsmount+340 common fspick __x64_sys_fspick # # x32-specific system call numbers start at 512 to avoid cache impact
From: Alan Jenkins <hidden> Date: 2018-10-12 22:22:41
On 21/09/2018 17:34, David Howells wrote:
quoted hunk
Provide an fspick() system call that can be used to pick an existing
mountpoint into an fs_context which can thereafter be used to reconfigure a
superblock (equivalent of the superblock side of -o remount).
This looks like:
int fd = fspick(AT_FDCWD, "/mnt",
FSPICK_CLOEXEC | FSPICK_NO_AUTOMOUNT);
fsconfig(fd, FSCONFIG_SET_FLAG, "intr", NULL, 0);
fsconfig(fd, FSCONFIG_SET_FLAG, "noac", NULL, 0);
fsconfig(fd, FSCONFIG_CMD_RECONFIGURE, NULL, NULL, 0);
At the point of fspick being called, the file descriptor referring to the
filesystem context is in exactly the same state as the one that was created
by fsopen() after fsmount() has been successfully called.
Signed-off-by: David Howells <dhowells@redhat.com>
cc: linux-api@vger.kernel.org
---
arch/x86/entry/syscalls/syscall_32.tbl | 1 +
arch/x86/entry/syscalls/syscall_64.tbl | 1 +
fs/fsopen.c | 54 ++++++++++++++++++++++++++++++++
include/linux/syscalls.h | 1 +
include/uapi/linux/fs.h | 5 +++
5 files changed, 62 insertions(+)
@@ -348,6 +348,7 @@ 337 common fsopen __x64_sys_fsopen 338 common fsconfig __x64_sys_fsconfig 339 common fsmount __x64_sys_fsmount+340 common fspick __x64_sys_fspick # # x32-specific system call numbers start at 512 to avoid cache impact
From: Al Viro <viro@ZenIV.linux.org.uk> Date: 2018-10-13 13:47:36
On Fri, Oct 12, 2018 at 03:49:50PM +0100, Alan Jenkins wrote:
quoted
+SYSCALL_DEFINE3(fspick, int, dfd, const char __user *, path, unsigned int, flags)+{+ struct fs_context *fc;+ struct path target;+ unsigned int lookup_flags;+ int ret;++ if (!ns_capable(current->nsproxy->mnt_ns->user_ns, CAP_SYS_ADMIN))+ return -EPERM;
This seems to accept basically any mount. Specifically: are you sure it's
OK to return a handle to a SB_NO_USER superblock?
Umm... As long as we don't try to do pathname resolution from its ->s_root,
shouldn't be a problem and I don't see anything that would do that. I might've
missed something, but...
From: Alan Jenkins <hidden> Date: 2018-10-13 17:21:33
On 13/10/2018 07:11, Al Viro wrote:
On Fri, Oct 12, 2018 at 03:49:50PM +0100, Alan Jenkins wrote:
quoted
quoted
+SYSCALL_DEFINE3(fspick, int, dfd, const char __user *, path, unsigned int, flags)+{+ struct fs_context *fc;+ struct path target;+ unsigned int lookup_flags;+ int ret;++ if (!ns_capable(current->nsproxy->mnt_ns->user_ns, CAP_SYS_ADMIN))+ return -EPERM;
This seems to accept basically any mount. Specifically: are you sure it's
OK to return a handle to a SB_NO_USER superblock?
Umm... As long as we don't try to do pathname resolution from its ->s_root,
shouldn't be a problem and I don't see anything that would do that. I might've
missed something, but...
Sorry, I guess SB_NOUSER was the wrong word. I was trying find if anything stopped things like
int memfd = memfd_create("foo", 0);
int fsfd = fspick(memfd, "", FSPICK_EMPTY_PATH);
fsconfig(fsfd, FSCONFIG_SET_FLAG, "ro", NULL, 0);
fsconfig(fsfd, FSCONFIG_SET_STRING, "size", "100M", 0);
fsconfig(fsfd, FSCONFIG_CMD_RECONFIGURE, NULL, NULL, 0);
So far I'm getting -EBUSY if I try to apply the "ro", -EINVAL if I try to apply the "size=100M". But if I don't apply either, then FSCONFIG_CMD_RECONFIGURE succeeds.
It seems worrying that it might let me set options on shm_mnt. Or at least letting me get as far as the -EBUSY check for the "ro" superblock flag.
I'm not sure why I'm getting the -EINVAL setting the "size" option. But it would be much more reassuring if I was getting -EPERM :-).
Alan
From: Andy Lutomirski <luto@amacapital.net> Date: 2018-10-14 06:43:10
On Sat, Oct 13, 2018 at 2:45 AM Alan Jenkins
[off-list ref] wrote:
On 13/10/2018 07:11, Al Viro wrote:
quoted
On Fri, Oct 12, 2018 at 03:49:50PM +0100, Alan Jenkins wrote:
quoted
quoted
+SYSCALL_DEFINE3(fspick, int, dfd, const char __user *, path, unsigned int, flags)+{+ struct fs_context *fc;+ struct path target;+ unsigned int lookup_flags;+ int ret;++ if (!ns_capable(current->nsproxy->mnt_ns->user_ns, CAP_SYS_ADMIN))+ return -EPERM;
This seems to accept basically any mount. Specifically: are you sure it's
OK to return a handle to a SB_NO_USER superblock?
Umm... As long as we don't try to do pathname resolution from its ->s_root,
shouldn't be a problem and I don't see anything that would do that. I might've
missed something, but...
Sorry, I guess SB_NOUSER was the wrong word. I was trying find if
anything stopped things like
int memfd = memfd_create("foo", 0);
int fsfd = fspick(memfd, "", FSPICK_EMPTY_PATH);
fsconfig(fsfd, FSCONFIG_SET_FLAG, "ro", NULL, 0);
fsconfig(fsfd, FSCONFIG_SET_STRING, "size", "100M", 0);
fsconfig(fsfd, FSCONFIG_CMD_RECONFIGURE, NULL, NULL, 0);
So far I'm getting -EBUSY if I try to apply the "ro", -EINVAL if I try
to apply the "size=100M". But if I don't apply either, then
FSCONFIG_CMD_RECONFIGURE succeeds.
It seems worrying that it might let me set options on shm_mnt. Or at
least letting me get as far as the -EBUSY check for the "ro" superblock
flag.
I'm not sure why I'm getting the -EINVAL setting the "size" option. But
it would be much more reassuring if I was getting -EPERM :-).
I would argue that the filesystem associated with a memfd, and even
the fact that there *is* a filesystem, is none of user code's
business. So that fspick() call should return -EINVAL or similar.
From: David Howells <dhowells@redhat.com> Date: 2018-10-17 13:15:01
Alan Jenkins [off-list ref] wrote:
Sorry, I guess SB_NOUSER was the wrong word. I was trying find if anything
stopped things like
int memfd = memfd_create("foo", 0);
int fsfd = fspick(memfd, "", FSPICK_EMPTY_PATH);
fsconfig(fsfd, FSCONFIG_SET_FLAG, "ro", NULL, 0);
fsconfig(fsfd, FSCONFIG_SET_STRING, "size", "100M", 0);
fsconfig(fsfd, FSCONFIG_CMD_RECONFIGURE, NULL, NULL, 0);
So far I'm getting -EBUSY if I try to apply the "ro", -EINVAL if I try to
apply the "size=100M". But if I don't apply either, then
FSCONFIG_CMD_RECONFIGURE succeeds.
I should probably check that the picked point is actually a mountpoint.
David
From: Alan Jenkins <hidden> Date: 2018-10-17 17:41:24
On 17/10/2018 16:45, David Howells wrote:
Alan Jenkins [off-list ref] wrote:
quoted
I agree. I'm happy to see this is using the same check as do_remount().
* change filesystem flags. dir should be a physical root of filesystem.
* If you've mounted a non-root directory somewhere and want to do remount
* on it - tough luck.
*/
Are you suggesting that it should only work at the ultimate root of a
superblock and not a bind mount somewhere within?
That's tricky to make work for NFS because s_root is a dummy dentry.
David
Retro-actively: I do not suggest that.
I tried to answer this question in my reply to Eric correcting me. Eric was right to correct me. I now understand the comment above do_remount() is incorrect. I re-reviewed your diff in light of that. I re-endorse your diff as a way to solve the problem I raised.
(I think it would be useful to remove the misleading comment above do_remount(), to avoid future confusion.)
From: Alan Jenkins <hidden> Date: 2018-10-17 22:27:27
On 17/10/2018 14:20, David Howells wrote:
David Howells [off-list ref] wrote:
quoted
I should probably check that the picked point is actually a mountpoint.
The root of the mount object at the path specified, that is, perhaps with
something like the attached.
David
I agree. I'm happy to see this is using the same check as do_remount().
* change filesystem flags. dir should be a physical root of filesystem.
* If you've mounted a non-root directory somewhere and want to do remount
* on it - tough luck.
*/
Thanks
Alan
From: David Howells <dhowells@redhat.com> Date: 2018-10-17 23:42:16
Alan Jenkins [off-list ref] wrote:
I agree. I'm happy to see this is using the same check as do_remount().
* change filesystem flags. dir should be a physical root of filesystem.
* If you've mounted a non-root directory somewhere and want to do remount
* on it - tough luck.
*/
Are you suggesting that it should only work at the ultimate root of a
superblock and not a bind mount somewhere within?
That's tricky to make work for NFS because s_root is a dummy dentry.
David
From: Alan Jenkins <hidden> Date: 2018-10-18 06:11:35
[resent, hopefully with slightly less formatting damage]
On 17/10/2018 16:45, David Howells wrote:
Alan Jenkins [off-list ref] wrote:
quoted
I agree. I'm happy to see this is using the same check as do_remount().
* change filesystem flags. dir should be a physical root of filesystem.
* If you've mounted a non-root directory somewhere and want to do remount
* on it - tough luck.
*/
Are you suggesting that it should only work at the ultimate root of a
superblock and not a bind mount somewhere within?
That's tricky to make work for NFS because s_root is a dummy dentry.
David
Retro-actively: I do not suggest that.
I tried to answer this question in my reply to Eric correcting me. Eric was right to correct me. I now understand the comment above do_remount() is incorrect. I re-reviewed your diff in light of that. I re-endorse your diff as a way to solve the problem I raised.
(I think it would be useful to remove the misleading comment above do_remount(), to avoid future confusion.)
quoted hunk
@@ -186,6 +186,10 @@ SYSCALL_DEFINE3(fspick, int, dfd, const char __user *, path, unsigned int, flags if (ret < 0) goto err;+ ret = -EINVAL;+ if (target.mnt->mnt_root != target.dentry)+ goto err_path;+ fc = vfs_new_fs_context(target.dentry->d_sb->s_type, target.dentry, 0, 0, FS_CONTEXT_FOR_RECONFIGURE); if (IS_ERR(fc)) {
( the "if" statement it adds to fspick() is equivalent to the second "if" statement in do_remount(): )
static int do_remount(struct path *path, int ms_flags, int sb_flags,
int mnt_flags, void *data)
{
int err;
struct super_block *sb = path->mnt->mnt_sb;
struct mount *mnt = real_mount(path->mnt);
if (!check_mnt(mnt))
return -EINVAL;
if (path->dentry != path->mnt->mnt_root)
return -EINVAL;
Thanks
Alan