1 /* Filesystem access-by-fd.
3 * Copyright (C) 2017 Red Hat, Inc. All Rights Reserved.
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.
12 #include <linux/fs_context.h>
13 #include <linux/fs_parser.h>
14 #include <linux/slab.h>
15 #include <linux/uaccess.h>
16 #include <linux/syscalls.h>
17 #include <linux/security.h>
18 #include <linux/anon_inodes.h>
19 #include <linux/namei.h>
20 #include <linux/file.h>
21 #include <uapi/linux/mount.h>
26 * Allow the user to read back any error, warning or informational messages.
28 static ssize_t fscontext_read(struct file *file,
29 char __user *_buf, size_t len, loff_t *pos)
31 struct fs_context *fc = file->private_data;
32 struct fc_log *log = fc->log;
33 unsigned int logsize = ARRAY_SIZE(log->buffer);
39 ret = mutex_lock_interruptible(&fc->uapi_mutex);
43 if (log->head == log->tail) {
44 mutex_unlock(&fc->uapi_mutex);
48 index = log->tail & (logsize - 1);
49 p = log->buffer[index];
50 need_free = log->need_free & (1 << index);
51 log->buffer[index] = NULL;
52 log->need_free &= ~(1 << index);
54 mutex_unlock(&fc->uapi_mutex);
61 if (copy_to_user(_buf, p, n) != 0)
71 static int fscontext_release(struct inode *inode, struct file *file)
73 struct fs_context *fc = file->private_data;
76 file->private_data = NULL;
82 const struct file_operations fscontext_fops = {
83 .read = fscontext_read,
84 .release = fscontext_release,
89 * Attach a filesystem context to a file and an fd.
91 static int fscontext_create_fd(struct fs_context *fc, unsigned int o_flags)
95 fd = anon_inode_getfd("[fscontext]", &fscontext_fops, fc,
102 static int fscontext_alloc_log(struct fs_context *fc)
104 fc->log = kzalloc(sizeof(*fc->log), GFP_KERNEL);
107 refcount_set(&fc->log->usage, 1);
108 fc->log->owner = fc->fs_type->owner;
113 * Open a filesystem by name so that it can be configured for mounting.
115 * We are allowed to specify a container in which the filesystem will be
116 * opened, thereby indicating which namespaces will be used (notably, which
117 * network namespace will be used for network filesystems).
119 SYSCALL_DEFINE2(fsopen, const char __user *, _fs_name, unsigned int, flags)
121 struct file_system_type *fs_type;
122 struct fs_context *fc;
126 if (!ns_capable(current->nsproxy->mnt_ns->user_ns, CAP_SYS_ADMIN))
129 if (flags & ~FSOPEN_CLOEXEC)
132 fs_name = strndup_user(_fs_name, PAGE_SIZE);
134 return PTR_ERR(fs_name);
136 fs_type = get_fs_type(fs_name);
141 fc = fs_context_for_mount(fs_type, 0);
142 put_filesystem(fs_type);
146 fc->phase = FS_CONTEXT_CREATE_PARAMS;
148 ret = fscontext_alloc_log(fc);
152 return fscontext_create_fd(fc, flags & FSOPEN_CLOEXEC ? O_CLOEXEC : 0);
160 * Pick a superblock into a context for reconfiguration.
162 SYSCALL_DEFINE3(fspick, int, dfd, const char __user *, path, unsigned int, flags)
164 struct fs_context *fc;
166 unsigned int lookup_flags;
169 if (!ns_capable(current->nsproxy->mnt_ns->user_ns, CAP_SYS_ADMIN))
172 if ((flags & ~(FSPICK_CLOEXEC |
173 FSPICK_SYMLINK_NOFOLLOW |
174 FSPICK_NO_AUTOMOUNT |
175 FSPICK_EMPTY_PATH)) != 0)
178 lookup_flags = LOOKUP_FOLLOW | LOOKUP_AUTOMOUNT;
179 if (flags & FSPICK_SYMLINK_NOFOLLOW)
180 lookup_flags &= ~LOOKUP_FOLLOW;
181 if (flags & FSPICK_NO_AUTOMOUNT)
182 lookup_flags &= ~LOOKUP_AUTOMOUNT;
183 if (flags & FSPICK_EMPTY_PATH)
184 lookup_flags |= LOOKUP_EMPTY;
185 ret = user_path_at(dfd, path, lookup_flags, &target);
190 if (target.mnt->mnt_root != target.dentry)
193 fc = fs_context_for_reconfigure(target.dentry, 0, 0);
199 fc->phase = FS_CONTEXT_RECONF_PARAMS;
201 ret = fscontext_alloc_log(fc);
206 return fscontext_create_fd(fc, flags & FSPICK_CLOEXEC ? O_CLOEXEC : 0);
217 * Check the state and apply the configuration. Note that this function is
218 * allowed to 'steal' the value by setting param->xxx to NULL before returning.
220 static int vfs_fsconfig_locked(struct fs_context *fc, int cmd,
221 struct fs_parameter *param)
223 struct super_block *sb;
226 ret = finish_clean_context(fc);
230 case FSCONFIG_CMD_CREATE:
231 if (fc->phase != FS_CONTEXT_CREATE_PARAMS)
233 fc->phase = FS_CONTEXT_CREATING;
234 ret = vfs_get_tree(fc);
238 ret = security_sb_kern_mount(sb);
243 up_write(&sb->s_umount);
244 fc->phase = FS_CONTEXT_AWAITING_MOUNT;
246 case FSCONFIG_CMD_RECONFIGURE:
247 if (fc->phase != FS_CONTEXT_RECONF_PARAMS)
249 fc->phase = FS_CONTEXT_RECONFIGURING;
251 if (!ns_capable(sb->s_user_ns, CAP_SYS_ADMIN)) {
255 down_write(&sb->s_umount);
256 ret = reconfigure_super(fc);
257 up_write(&sb->s_umount);
260 vfs_clean_context(fc);
263 if (fc->phase != FS_CONTEXT_CREATE_PARAMS &&
264 fc->phase != FS_CONTEXT_RECONF_PARAMS)
267 return vfs_parse_fs_param(fc, param);
269 fc->phase = FS_CONTEXT_FAILED;
274 * sys_fsconfig - Set parameters and trigger actions on a context
275 * @fd: The filesystem context to act upon
276 * @cmd: The action to take
277 * @_key: Where appropriate, the parameter key to set
278 * @_value: Where appropriate, the parameter value to set
279 * @aux: Additional information for the value
281 * This system call is used to set parameters on a context, including
282 * superblock settings, data source and security labelling.
284 * Actions include triggering the creation of a superblock and the
285 * reconfiguration of the superblock attached to the specified context.
287 * When setting a parameter, @cmd indicates the type of value being proposed
288 * and @_key indicates the parameter to be altered.
290 * @_value and @aux are used to specify the value, should a value be required:
292 * (*) fsconfig_set_flag: No value is specified. The parameter must be boolean
293 * in nature. The key may be prefixed with "no" to invert the
294 * setting. @_value must be NULL and @aux must be 0.
296 * (*) fsconfig_set_string: A string value is specified. The parameter can be
297 * expecting boolean, integer, string or take a path. A conversion to an
298 * appropriate type will be attempted (which may include looking up as a
299 * path). @_value points to a NUL-terminated string and @aux must be 0.
301 * (*) fsconfig_set_binary: A binary blob is specified. @_value points to the
302 * blob and @aux indicates its size. The parameter must be expecting a
305 * (*) fsconfig_set_path: A non-empty path is specified. The parameter must be
306 * expecting a path object. @_value points to a NUL-terminated string that
307 * is the path and @aux is a file descriptor at which to start a relative
308 * lookup or AT_FDCWD.
310 * (*) fsconfig_set_path_empty: As fsconfig_set_path, but with AT_EMPTY_PATH
313 * (*) fsconfig_set_fd: An open file descriptor is specified. @_value must be
314 * NULL and @aux indicates the file descriptor.
316 SYSCALL_DEFINE5(fsconfig,
319 const char __user *, _key,
320 const void __user *, _value,
323 struct fs_context *fc;
327 struct fs_parameter param = {
328 .type = fs_value_is_undefined,
335 case FSCONFIG_SET_FLAG:
336 if (!_key || _value || aux)
339 case FSCONFIG_SET_STRING:
340 if (!_key || !_value || aux)
343 case FSCONFIG_SET_BINARY:
344 if (!_key || !_value || aux <= 0 || aux > 1024 * 1024)
347 case FSCONFIG_SET_PATH:
348 case FSCONFIG_SET_PATH_EMPTY:
349 if (!_key || !_value || (aux != AT_FDCWD && aux < 0))
352 case FSCONFIG_SET_FD:
353 if (!_key || _value || aux < 0)
356 case FSCONFIG_CMD_CREATE:
357 case FSCONFIG_CMD_RECONFIGURE:
358 if (_key || _value || aux)
369 if (f.file->f_op != &fscontext_fops)
372 fc = f.file->private_data;
373 if (fc->ops == &legacy_fs_context_ops) {
375 case FSCONFIG_SET_BINARY:
376 case FSCONFIG_SET_PATH:
377 case FSCONFIG_SET_PATH_EMPTY:
378 case FSCONFIG_SET_FD:
385 param.key = strndup_user(_key, 256);
386 if (IS_ERR(param.key)) {
387 ret = PTR_ERR(param.key);
393 case FSCONFIG_SET_FLAG:
394 param.type = fs_value_is_flag;
396 case FSCONFIG_SET_STRING:
397 param.type = fs_value_is_string;
398 param.string = strndup_user(_value, 256);
399 if (IS_ERR(param.string)) {
400 ret = PTR_ERR(param.string);
403 param.size = strlen(param.string);
405 case FSCONFIG_SET_BINARY:
406 param.type = fs_value_is_blob;
408 param.blob = memdup_user_nul(_value, aux);
409 if (IS_ERR(param.blob)) {
410 ret = PTR_ERR(param.blob);
414 case FSCONFIG_SET_PATH:
415 param.type = fs_value_is_filename;
416 param.name = getname_flags(_value, 0, NULL);
417 if (IS_ERR(param.name)) {
418 ret = PTR_ERR(param.name);
422 param.size = strlen(param.name->name);
424 case FSCONFIG_SET_PATH_EMPTY:
425 param.type = fs_value_is_filename_empty;
426 param.name = getname_flags(_value, LOOKUP_EMPTY, NULL);
427 if (IS_ERR(param.name)) {
428 ret = PTR_ERR(param.name);
432 param.size = strlen(param.name->name);
434 case FSCONFIG_SET_FD:
435 param.type = fs_value_is_file;
437 param.file = fget(aux);
445 ret = mutex_lock_interruptible(&fc->uapi_mutex);
447 ret = vfs_fsconfig_locked(fc, cmd, ¶m);
448 mutex_unlock(&fc->uapi_mutex);
451 /* Clean up the our record of any value that we obtained from
452 * userspace. Note that the value may have been stolen by the LSM or
453 * filesystem, in which case the value pointer will have been cleared.
456 case FSCONFIG_SET_STRING:
457 case FSCONFIG_SET_BINARY:
460 case FSCONFIG_SET_PATH:
461 case FSCONFIG_SET_PATH_EMPTY:
465 case FSCONFIG_SET_FD: