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
;
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
,
85 * Attach a filesystem context to a file and an fd.
87 static int fscontext_create_fd(struct fs_context
*fc
, unsigned int o_flags
)
91 fd
= anon_inode_getfd("[fscontext]", &fscontext_fops
, fc
,
98 static int fscontext_alloc_log(struct fs_context
*fc
)
100 fc
->log
= kzalloc(sizeof(*fc
->log
), GFP_KERNEL
);
103 refcount_set(&fc
->log
->usage
, 1);
104 fc
->log
->owner
= fc
->fs_type
->owner
;
109 * Open a filesystem by name so that it can be configured for mounting.
111 * We are allowed to specify a container in which the filesystem will be
112 * opened, thereby indicating which namespaces will be used (notably, which
113 * network namespace will be used for network filesystems).
115 SYSCALL_DEFINE2(fsopen
, const char __user
*, _fs_name
, unsigned int, flags
)
117 struct file_system_type
*fs_type
;
118 struct fs_context
*fc
;
122 if (!ns_capable(current
->nsproxy
->mnt_ns
->user_ns
, CAP_SYS_ADMIN
))
125 if (flags
& ~FSOPEN_CLOEXEC
)
128 fs_name
= strndup_user(_fs_name
, PAGE_SIZE
);
130 return PTR_ERR(fs_name
);
132 fs_type
= get_fs_type(fs_name
);
137 fc
= fs_context_for_mount(fs_type
, 0);
138 put_filesystem(fs_type
);
142 fc
->phase
= FS_CONTEXT_CREATE_PARAMS
;
144 ret
= fscontext_alloc_log(fc
);
148 return fscontext_create_fd(fc
, flags
& FSOPEN_CLOEXEC
? O_CLOEXEC
: 0);
156 * Pick a superblock into a context for reconfiguration.
158 SYSCALL_DEFINE3(fspick
, int, dfd
, const char __user
*, path
, unsigned int, flags
)
160 struct fs_context
*fc
;
162 unsigned int lookup_flags
;
165 if (!ns_capable(current
->nsproxy
->mnt_ns
->user_ns
, CAP_SYS_ADMIN
))
168 if ((flags
& ~(FSPICK_CLOEXEC
|
169 FSPICK_SYMLINK_NOFOLLOW
|
170 FSPICK_NO_AUTOMOUNT
|
171 FSPICK_EMPTY_PATH
)) != 0)
174 lookup_flags
= LOOKUP_FOLLOW
| LOOKUP_AUTOMOUNT
;
175 if (flags
& FSPICK_SYMLINK_NOFOLLOW
)
176 lookup_flags
&= ~LOOKUP_FOLLOW
;
177 if (flags
& FSPICK_NO_AUTOMOUNT
)
178 lookup_flags
&= ~LOOKUP_AUTOMOUNT
;
179 if (flags
& FSPICK_EMPTY_PATH
)
180 lookup_flags
|= LOOKUP_EMPTY
;
181 ret
= user_path_at(dfd
, path
, lookup_flags
, &target
);
186 if (target
.mnt
->mnt_root
!= target
.dentry
)
189 fc
= fs_context_for_reconfigure(target
.dentry
, 0, 0);
195 fc
->phase
= FS_CONTEXT_RECONF_PARAMS
;
197 ret
= fscontext_alloc_log(fc
);
202 return fscontext_create_fd(fc
, flags
& FSPICK_CLOEXEC
? O_CLOEXEC
: 0);
213 * Check the state and apply the configuration. Note that this function is
214 * allowed to 'steal' the value by setting param->xxx to NULL before returning.
216 static int vfs_fsconfig_locked(struct fs_context
*fc
, int cmd
,
217 struct fs_parameter
*param
)
219 struct super_block
*sb
;
222 ret
= finish_clean_context(fc
);
226 case FSCONFIG_CMD_CREATE
:
227 if (fc
->phase
!= FS_CONTEXT_CREATE_PARAMS
)
229 fc
->phase
= FS_CONTEXT_CREATING
;
230 ret
= vfs_get_tree(fc
);
234 ret
= security_sb_kern_mount(sb
);
239 up_write(&sb
->s_umount
);
240 fc
->phase
= FS_CONTEXT_AWAITING_MOUNT
;
242 case FSCONFIG_CMD_RECONFIGURE
:
243 if (fc
->phase
!= FS_CONTEXT_RECONF_PARAMS
)
245 fc
->phase
= FS_CONTEXT_RECONFIGURING
;
247 if (!ns_capable(sb
->s_user_ns
, CAP_SYS_ADMIN
)) {
251 down_write(&sb
->s_umount
);
252 ret
= reconfigure_super(fc
);
253 up_write(&sb
->s_umount
);
256 vfs_clean_context(fc
);
259 if (fc
->phase
!= FS_CONTEXT_CREATE_PARAMS
&&
260 fc
->phase
!= FS_CONTEXT_RECONF_PARAMS
)
263 return vfs_parse_fs_param(fc
, param
);
265 fc
->phase
= FS_CONTEXT_FAILED
;
270 * sys_fsconfig - Set parameters and trigger actions on a context
271 * @fd: The filesystem context to act upon
272 * @cmd: The action to take
273 * @_key: Where appropriate, the parameter key to set
274 * @_value: Where appropriate, the parameter value to set
275 * @aux: Additional information for the value
277 * This system call is used to set parameters on a context, including
278 * superblock settings, data source and security labelling.
280 * Actions include triggering the creation of a superblock and the
281 * reconfiguration of the superblock attached to the specified context.
283 * When setting a parameter, @cmd indicates the type of value being proposed
284 * and @_key indicates the parameter to be altered.
286 * @_value and @aux are used to specify the value, should a value be required:
288 * (*) fsconfig_set_flag: No value is specified. The parameter must be boolean
289 * in nature. The key may be prefixed with "no" to invert the
290 * setting. @_value must be NULL and @aux must be 0.
292 * (*) fsconfig_set_string: A string value is specified. The parameter can be
293 * expecting boolean, integer, string or take a path. A conversion to an
294 * appropriate type will be attempted (which may include looking up as a
295 * path). @_value points to a NUL-terminated string and @aux must be 0.
297 * (*) fsconfig_set_binary: A binary blob is specified. @_value points to the
298 * blob and @aux indicates its size. The parameter must be expecting a
301 * (*) fsconfig_set_path: A non-empty path is specified. The parameter must be
302 * expecting a path object. @_value points to a NUL-terminated string that
303 * is the path and @aux is a file descriptor at which to start a relative
304 * lookup or AT_FDCWD.
306 * (*) fsconfig_set_path_empty: As fsconfig_set_path, but with AT_EMPTY_PATH
309 * (*) fsconfig_set_fd: An open file descriptor is specified. @_value must be
310 * NULL and @aux indicates the file descriptor.
312 SYSCALL_DEFINE5(fsconfig
,
315 const char __user
*, _key
,
316 const void __user
*, _value
,
319 struct fs_context
*fc
;
323 struct fs_parameter param
= {
324 .type
= fs_value_is_undefined
,
331 case FSCONFIG_SET_FLAG
:
332 if (!_key
|| _value
|| aux
)
335 case FSCONFIG_SET_STRING
:
336 if (!_key
|| !_value
|| aux
)
339 case FSCONFIG_SET_BINARY
:
340 if (!_key
|| !_value
|| aux
<= 0 || aux
> 1024 * 1024)
343 case FSCONFIG_SET_PATH
:
344 case FSCONFIG_SET_PATH_EMPTY
:
345 if (!_key
|| !_value
|| (aux
!= AT_FDCWD
&& aux
< 0))
348 case FSCONFIG_SET_FD
:
349 if (!_key
|| _value
|| aux
< 0)
352 case FSCONFIG_CMD_CREATE
:
353 case FSCONFIG_CMD_RECONFIGURE
:
354 if (_key
|| _value
|| aux
)
365 if (f
.file
->f_op
!= &fscontext_fops
)
368 fc
= f
.file
->private_data
;
369 if (fc
->ops
== &legacy_fs_context_ops
) {
371 case FSCONFIG_SET_BINARY
:
372 case FSCONFIG_SET_PATH
:
373 case FSCONFIG_SET_PATH_EMPTY
:
374 case FSCONFIG_SET_FD
:
381 param
.key
= strndup_user(_key
, 256);
382 if (IS_ERR(param
.key
)) {
383 ret
= PTR_ERR(param
.key
);
389 case FSCONFIG_SET_FLAG
:
390 param
.type
= fs_value_is_flag
;
392 case FSCONFIG_SET_STRING
:
393 param
.type
= fs_value_is_string
;
394 param
.string
= strndup_user(_value
, 256);
395 if (IS_ERR(param
.string
)) {
396 ret
= PTR_ERR(param
.string
);
399 param
.size
= strlen(param
.string
);
401 case FSCONFIG_SET_BINARY
:
402 param
.type
= fs_value_is_blob
;
404 param
.blob
= memdup_user_nul(_value
, aux
);
405 if (IS_ERR(param
.blob
)) {
406 ret
= PTR_ERR(param
.blob
);
410 case FSCONFIG_SET_PATH
:
411 param
.type
= fs_value_is_filename
;
412 param
.name
= getname_flags(_value
, 0, NULL
);
413 if (IS_ERR(param
.name
)) {
414 ret
= PTR_ERR(param
.name
);
418 param
.size
= strlen(param
.name
->name
);
420 case FSCONFIG_SET_PATH_EMPTY
:
421 param
.type
= fs_value_is_filename_empty
;
422 param
.name
= getname_flags(_value
, LOOKUP_EMPTY
, NULL
);
423 if (IS_ERR(param
.name
)) {
424 ret
= PTR_ERR(param
.name
);
428 param
.size
= strlen(param
.name
->name
);
430 case FSCONFIG_SET_FD
:
431 param
.type
= fs_value_is_file
;
433 param
.file
= fget(aux
);
441 ret
= mutex_lock_interruptible(&fc
->uapi_mutex
);
443 ret
= vfs_fsconfig_locked(fc
, cmd
, ¶m
);
444 mutex_unlock(&fc
->uapi_mutex
);
447 /* Clean up the our record of any value that we obtained from
448 * userspace. Note that the value may have been stolen by the LSM or
449 * filesystem, in which case the value pointer will have been cleared.
452 case FSCONFIG_SET_STRING
:
453 case FSCONFIG_SET_BINARY
:
456 case FSCONFIG_SET_PATH
:
457 case FSCONFIG_SET_PATH_EMPTY
:
461 case FSCONFIG_SET_FD
: