1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Filesystem access-by-fd.
4 * Copyright (C) 2017 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
8 #include <linux/fs_context.h>
9 #include <linux/fs_parser.h>
10 #include <linux/slab.h>
11 #include <linux/uaccess.h>
12 #include <linux/syscalls.h>
13 #include <linux/security.h>
14 #include <linux/anon_inodes.h>
15 #include <linux/namei.h>
16 #include <linux/file.h>
17 #include <uapi/linux/mount.h>
22 * Allow the user to read back any error, warning or informational messages.
24 static ssize_t
fscontext_read(struct file
*file
,
25 char __user
*_buf
, size_t len
, loff_t
*pos
)
27 struct fs_context
*fc
= file
->private_data
;
28 struct fc_log
*log
= fc
->log
.log
;
29 unsigned int logsize
= ARRAY_SIZE(log
->buffer
);
35 ret
= mutex_lock_interruptible(&fc
->uapi_mutex
);
39 if (log
->head
== log
->tail
) {
40 mutex_unlock(&fc
->uapi_mutex
);
44 index
= log
->tail
& (logsize
- 1);
45 p
= log
->buffer
[index
];
46 need_free
= log
->need_free
& (1 << index
);
47 log
->buffer
[index
] = NULL
;
48 log
->need_free
&= ~(1 << index
);
50 mutex_unlock(&fc
->uapi_mutex
);
57 if (copy_to_user(_buf
, p
, n
) != 0)
67 static int fscontext_release(struct inode
*inode
, struct file
*file
)
69 struct fs_context
*fc
= file
->private_data
;
72 file
->private_data
= NULL
;
78 const struct file_operations fscontext_fops
= {
79 .read
= fscontext_read
,
80 .release
= fscontext_release
,
84 * Attach a filesystem context to a file and an fd.
86 static int fscontext_create_fd(struct fs_context
*fc
, unsigned int o_flags
)
90 fd
= anon_inode_getfd("[fscontext]", &fscontext_fops
, fc
,
97 static int fscontext_alloc_log(struct fs_context
*fc
)
99 fc
->log
.log
= kzalloc(sizeof(*fc
->log
.log
), GFP_KERNEL
);
102 refcount_set(&fc
->log
.log
->usage
, 1);
103 fc
->log
.log
->owner
= fc
->fs_type
->owner
;
108 * Open a filesystem by name so that it can be configured for mounting.
110 * We are allowed to specify a container in which the filesystem will be
111 * opened, thereby indicating which namespaces will be used (notably, which
112 * network namespace will be used for network filesystems).
114 SYSCALL_DEFINE2(fsopen
, const char __user
*, _fs_name
, unsigned int, flags
)
116 struct file_system_type
*fs_type
;
117 struct fs_context
*fc
;
124 if (flags
& ~FSOPEN_CLOEXEC
)
127 fs_name
= strndup_user(_fs_name
, PAGE_SIZE
);
129 return PTR_ERR(fs_name
);
131 fs_type
= get_fs_type(fs_name
);
136 fc
= fs_context_for_mount(fs_type
, 0);
137 put_filesystem(fs_type
);
141 fc
->phase
= FS_CONTEXT_CREATE_PARAMS
;
143 ret
= fscontext_alloc_log(fc
);
147 return fscontext_create_fd(fc
, flags
& FSOPEN_CLOEXEC
? O_CLOEXEC
: 0);
155 * Pick a superblock into a context for reconfiguration.
157 SYSCALL_DEFINE3(fspick
, int, dfd
, const char __user
*, path
, unsigned int, flags
)
159 struct fs_context
*fc
;
161 unsigned int lookup_flags
;
167 if ((flags
& ~(FSPICK_CLOEXEC
|
168 FSPICK_SYMLINK_NOFOLLOW
|
169 FSPICK_NO_AUTOMOUNT
|
170 FSPICK_EMPTY_PATH
)) != 0)
173 lookup_flags
= LOOKUP_FOLLOW
| LOOKUP_AUTOMOUNT
;
174 if (flags
& FSPICK_SYMLINK_NOFOLLOW
)
175 lookup_flags
&= ~LOOKUP_FOLLOW
;
176 if (flags
& FSPICK_NO_AUTOMOUNT
)
177 lookup_flags
&= ~LOOKUP_AUTOMOUNT
;
178 if (flags
& FSPICK_EMPTY_PATH
)
179 lookup_flags
|= LOOKUP_EMPTY
;
180 ret
= user_path_at(dfd
, path
, lookup_flags
, &target
);
185 if (target
.mnt
->mnt_root
!= target
.dentry
)
188 fc
= fs_context_for_reconfigure(target
.dentry
, 0, 0);
194 fc
->phase
= FS_CONTEXT_RECONF_PARAMS
;
196 ret
= fscontext_alloc_log(fc
);
201 return fscontext_create_fd(fc
, flags
& FSPICK_CLOEXEC
? O_CLOEXEC
: 0);
211 static int vfs_cmd_create(struct fs_context
*fc
, bool exclusive
)
213 struct super_block
*sb
;
216 if (fc
->phase
!= FS_CONTEXT_CREATE_PARAMS
)
219 if (!mount_capable(fc
))
222 fc
->phase
= FS_CONTEXT_CREATING
;
223 fc
->exclusive
= exclusive
;
225 ret
= vfs_get_tree(fc
);
227 fc
->phase
= FS_CONTEXT_FAILED
;
232 ret
= security_sb_kern_mount(sb
);
235 fc
->phase
= FS_CONTEXT_FAILED
;
239 /* vfs_get_tree() callchains will have grabbed @s_umount */
240 up_write(&sb
->s_umount
);
241 fc
->phase
= FS_CONTEXT_AWAITING_MOUNT
;
245 static int vfs_cmd_reconfigure(struct fs_context
*fc
)
247 struct super_block
*sb
;
250 if (fc
->phase
!= FS_CONTEXT_RECONF_PARAMS
)
253 fc
->phase
= FS_CONTEXT_RECONFIGURING
;
256 if (!ns_capable(sb
->s_user_ns
, CAP_SYS_ADMIN
)) {
257 fc
->phase
= FS_CONTEXT_FAILED
;
261 down_write(&sb
->s_umount
);
262 ret
= reconfigure_super(fc
);
263 up_write(&sb
->s_umount
);
265 fc
->phase
= FS_CONTEXT_FAILED
;
269 vfs_clean_context(fc
);
274 * Check the state and apply the configuration. Note that this function is
275 * allowed to 'steal' the value by setting param->xxx to NULL before returning.
277 static int vfs_fsconfig_locked(struct fs_context
*fc
, int cmd
,
278 struct fs_parameter
*param
)
282 ret
= finish_clean_context(fc
);
286 case FSCONFIG_CMD_CREATE
:
287 return vfs_cmd_create(fc
, false);
288 case FSCONFIG_CMD_CREATE_EXCL
:
289 return vfs_cmd_create(fc
, true);
290 case FSCONFIG_CMD_RECONFIGURE
:
291 return vfs_cmd_reconfigure(fc
);
293 if (fc
->phase
!= FS_CONTEXT_CREATE_PARAMS
&&
294 fc
->phase
!= FS_CONTEXT_RECONF_PARAMS
)
297 return vfs_parse_fs_param(fc
, param
);
302 * sys_fsconfig - Set parameters and trigger actions on a context
303 * @fd: The filesystem context to act upon
304 * @cmd: The action to take
305 * @_key: Where appropriate, the parameter key to set
306 * @_value: Where appropriate, the parameter value to set
307 * @aux: Additional information for the value
309 * This system call is used to set parameters on a context, including
310 * superblock settings, data source and security labelling.
312 * Actions include triggering the creation of a superblock and the
313 * reconfiguration of the superblock attached to the specified context.
315 * When setting a parameter, @cmd indicates the type of value being proposed
316 * and @_key indicates the parameter to be altered.
318 * @_value and @aux are used to specify the value, should a value be required:
320 * (*) fsconfig_set_flag: No value is specified. The parameter must be boolean
321 * in nature. The key may be prefixed with "no" to invert the
322 * setting. @_value must be NULL and @aux must be 0.
324 * (*) fsconfig_set_string: A string value is specified. The parameter can be
325 * expecting boolean, integer, string or take a path. A conversion to an
326 * appropriate type will be attempted (which may include looking up as a
327 * path). @_value points to a NUL-terminated string and @aux must be 0.
329 * (*) fsconfig_set_binary: A binary blob is specified. @_value points to the
330 * blob and @aux indicates its size. The parameter must be expecting a
333 * (*) fsconfig_set_path: A non-empty path is specified. The parameter must be
334 * expecting a path object. @_value points to a NUL-terminated string that
335 * is the path and @aux is a file descriptor at which to start a relative
336 * lookup or AT_FDCWD.
338 * (*) fsconfig_set_path_empty: As fsconfig_set_path, but with AT_EMPTY_PATH
341 * (*) fsconfig_set_fd: An open file descriptor is specified. @_value must be
342 * NULL and @aux indicates the file descriptor.
344 SYSCALL_DEFINE5(fsconfig
,
347 const char __user
*, _key
,
348 const void __user
*, _value
,
351 struct fs_context
*fc
;
353 int lookup_flags
= 0;
355 struct fs_parameter param
= {
356 .type
= fs_value_is_undefined
,
363 case FSCONFIG_SET_FLAG
:
364 if (!_key
|| _value
|| aux
)
367 case FSCONFIG_SET_STRING
:
368 if (!_key
|| !_value
|| aux
)
371 case FSCONFIG_SET_BINARY
:
372 if (!_key
|| !_value
|| aux
<= 0 || aux
> 1024 * 1024)
375 case FSCONFIG_SET_PATH
:
376 case FSCONFIG_SET_PATH_EMPTY
:
377 if (!_key
|| !_value
|| (aux
!= AT_FDCWD
&& aux
< 0))
380 case FSCONFIG_SET_FD
:
381 if (!_key
|| _value
|| aux
< 0)
384 case FSCONFIG_CMD_CREATE
:
385 case FSCONFIG_CMD_CREATE_EXCL
:
386 case FSCONFIG_CMD_RECONFIGURE
:
387 if (_key
|| _value
|| aux
)
397 if (fd_file(f
)->f_op
!= &fscontext_fops
)
400 fc
= fd_file(f
)->private_data
;
401 if (fc
->ops
== &legacy_fs_context_ops
) {
403 case FSCONFIG_SET_BINARY
:
404 case FSCONFIG_SET_PATH
:
405 case FSCONFIG_SET_PATH_EMPTY
:
406 case FSCONFIG_SET_FD
:
407 case FSCONFIG_CMD_CREATE_EXCL
:
413 param
.key
= strndup_user(_key
, 256);
414 if (IS_ERR(param
.key
))
415 return PTR_ERR(param
.key
);
419 case FSCONFIG_SET_FLAG
:
420 param
.type
= fs_value_is_flag
;
422 case FSCONFIG_SET_STRING
:
423 param
.type
= fs_value_is_string
;
424 param
.string
= strndup_user(_value
, 256);
425 if (IS_ERR(param
.string
)) {
426 ret
= PTR_ERR(param
.string
);
429 param
.size
= strlen(param
.string
);
431 case FSCONFIG_SET_BINARY
:
432 param
.type
= fs_value_is_blob
;
434 param
.blob
= memdup_user_nul(_value
, aux
);
435 if (IS_ERR(param
.blob
)) {
436 ret
= PTR_ERR(param
.blob
);
440 case FSCONFIG_SET_PATH_EMPTY
:
441 lookup_flags
= LOOKUP_EMPTY
;
443 case FSCONFIG_SET_PATH
:
444 param
.type
= fs_value_is_filename
;
445 param
.name
= getname_flags(_value
, lookup_flags
);
446 if (IS_ERR(param
.name
)) {
447 ret
= PTR_ERR(param
.name
);
451 param
.size
= strlen(param
.name
->name
);
453 case FSCONFIG_SET_FD
:
454 param
.type
= fs_value_is_file
;
456 param
.file
= fget(aux
);
465 ret
= mutex_lock_interruptible(&fc
->uapi_mutex
);
467 ret
= vfs_fsconfig_locked(fc
, cmd
, ¶m
);
468 mutex_unlock(&fc
->uapi_mutex
);
471 /* Clean up the our record of any value that we obtained from
472 * userspace. Note that the value may have been stolen by the LSM or
473 * filesystem, in which case the value pointer will have been cleared.
476 case FSCONFIG_SET_STRING
:
477 case FSCONFIG_SET_BINARY
:
480 case FSCONFIG_SET_PATH
:
481 case FSCONFIG_SET_PATH_EMPTY
:
485 case FSCONFIG_SET_FD
: