1b4d0d230SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
224dcb3d9SDavid Howells /* Filesystem access-by-fd.
324dcb3d9SDavid Howells *
424dcb3d9SDavid Howells * Copyright (C) 2017 Red Hat, Inc. All Rights Reserved.
524dcb3d9SDavid Howells * Written by David Howells (dhowells@redhat.com)
624dcb3d9SDavid Howells */
724dcb3d9SDavid Howells
824dcb3d9SDavid Howells #include <linux/fs_context.h>
9ecdab150SDavid Howells #include <linux/fs_parser.h>
1024dcb3d9SDavid Howells #include <linux/slab.h>
1124dcb3d9SDavid Howells #include <linux/uaccess.h>
1224dcb3d9SDavid Howells #include <linux/syscalls.h>
1324dcb3d9SDavid Howells #include <linux/security.h>
1424dcb3d9SDavid Howells #include <linux/anon_inodes.h>
1524dcb3d9SDavid Howells #include <linux/namei.h>
1624dcb3d9SDavid Howells #include <linux/file.h>
1724dcb3d9SDavid Howells #include <uapi/linux/mount.h>
18ecdab150SDavid Howells #include "internal.h"
1924dcb3d9SDavid Howells #include "mount.h"
2024dcb3d9SDavid Howells
21007ec26cSDavid Howells /*
22007ec26cSDavid Howells * Allow the user to read back any error, warning or informational messages.
23007ec26cSDavid Howells */
fscontext_read(struct file * file,char __user * _buf,size_t len,loff_t * pos)24007ec26cSDavid Howells static ssize_t fscontext_read(struct file *file,
25007ec26cSDavid Howells char __user *_buf, size_t len, loff_t *pos)
26007ec26cSDavid Howells {
27007ec26cSDavid Howells struct fs_context *fc = file->private_data;
28cc3c0b53SAl Viro struct fc_log *log = fc->log.log;
29007ec26cSDavid Howells unsigned int logsize = ARRAY_SIZE(log->buffer);
30007ec26cSDavid Howells ssize_t ret;
31007ec26cSDavid Howells char *p;
32007ec26cSDavid Howells bool need_free;
33007ec26cSDavid Howells int index, n;
34007ec26cSDavid Howells
35007ec26cSDavid Howells ret = mutex_lock_interruptible(&fc->uapi_mutex);
36007ec26cSDavid Howells if (ret < 0)
37007ec26cSDavid Howells return ret;
38007ec26cSDavid Howells
39007ec26cSDavid Howells if (log->head == log->tail) {
40007ec26cSDavid Howells mutex_unlock(&fc->uapi_mutex);
41007ec26cSDavid Howells return -ENODATA;
42007ec26cSDavid Howells }
43007ec26cSDavid Howells
44007ec26cSDavid Howells index = log->tail & (logsize - 1);
45007ec26cSDavid Howells p = log->buffer[index];
46007ec26cSDavid Howells need_free = log->need_free & (1 << index);
47007ec26cSDavid Howells log->buffer[index] = NULL;
48007ec26cSDavid Howells log->need_free &= ~(1 << index);
49007ec26cSDavid Howells log->tail++;
50007ec26cSDavid Howells mutex_unlock(&fc->uapi_mutex);
51007ec26cSDavid Howells
52007ec26cSDavid Howells ret = -EMSGSIZE;
53007ec26cSDavid Howells n = strlen(p);
54007ec26cSDavid Howells if (n > len)
55007ec26cSDavid Howells goto err_free;
56007ec26cSDavid Howells ret = -EFAULT;
57007ec26cSDavid Howells if (copy_to_user(_buf, p, n) != 0)
58007ec26cSDavid Howells goto err_free;
59007ec26cSDavid Howells ret = n;
60007ec26cSDavid Howells
61007ec26cSDavid Howells err_free:
62007ec26cSDavid Howells if (need_free)
63007ec26cSDavid Howells kfree(p);
64007ec26cSDavid Howells return ret;
65007ec26cSDavid Howells }
66007ec26cSDavid Howells
fscontext_release(struct inode * inode,struct file * file)6724dcb3d9SDavid Howells static int fscontext_release(struct inode *inode, struct file *file)
6824dcb3d9SDavid Howells {
6924dcb3d9SDavid Howells struct fs_context *fc = file->private_data;
7024dcb3d9SDavid Howells
7124dcb3d9SDavid Howells if (fc) {
7224dcb3d9SDavid Howells file->private_data = NULL;
7324dcb3d9SDavid Howells put_fs_context(fc);
7424dcb3d9SDavid Howells }
7524dcb3d9SDavid Howells return 0;
7624dcb3d9SDavid Howells }
7724dcb3d9SDavid Howells
7824dcb3d9SDavid Howells const struct file_operations fscontext_fops = {
79007ec26cSDavid Howells .read = fscontext_read,
8024dcb3d9SDavid Howells .release = fscontext_release,
8124dcb3d9SDavid Howells };
8224dcb3d9SDavid Howells
8324dcb3d9SDavid Howells /*
8424dcb3d9SDavid Howells * Attach a filesystem context to a file and an fd.
8524dcb3d9SDavid Howells */
fscontext_create_fd(struct fs_context * fc,unsigned int o_flags)8624dcb3d9SDavid Howells static int fscontext_create_fd(struct fs_context *fc, unsigned int o_flags)
8724dcb3d9SDavid Howells {
8824dcb3d9SDavid Howells int fd;
8924dcb3d9SDavid Howells
9024dcb3d9SDavid Howells fd = anon_inode_getfd("[fscontext]", &fscontext_fops, fc,
911cdc415fSChristian Brauner O_RDWR | o_flags);
9224dcb3d9SDavid Howells if (fd < 0)
9324dcb3d9SDavid Howells put_fs_context(fc);
9424dcb3d9SDavid Howells return fd;
9524dcb3d9SDavid Howells }
9624dcb3d9SDavid Howells
fscontext_alloc_log(struct fs_context * fc)9724dcb3d9SDavid Howells static int fscontext_alloc_log(struct fs_context *fc)
98007ec26cSDavid Howells {
99007ec26cSDavid Howells fc->log.log = kzalloc(sizeof(*fc->log.log), GFP_KERNEL);
100cc3c0b53SAl Viro if (!fc->log.log)
101cc3c0b53SAl Viro return -ENOMEM;
102007ec26cSDavid Howells refcount_set(&fc->log.log->usage, 1);
103cc3c0b53SAl Viro fc->log.log->owner = fc->fs_type->owner;
104cc3c0b53SAl Viro return 0;
105007ec26cSDavid Howells }
106007ec26cSDavid Howells
107007ec26cSDavid Howells /*
10824dcb3d9SDavid Howells * Open a filesystem by name so that it can be configured for mounting.
10924dcb3d9SDavid Howells *
11024dcb3d9SDavid Howells * We are allowed to specify a container in which the filesystem will be
11124dcb3d9SDavid Howells * opened, thereby indicating which namespaces will be used (notably, which
11224dcb3d9SDavid Howells * network namespace will be used for network filesystems).
11324dcb3d9SDavid Howells */
SYSCALL_DEFINE2(fsopen,const char __user *,_fs_name,unsigned int,flags)11424dcb3d9SDavid Howells SYSCALL_DEFINE2(fsopen, const char __user *, _fs_name, unsigned int, flags)
11524dcb3d9SDavid Howells {
11624dcb3d9SDavid Howells struct file_system_type *fs_type;
11724dcb3d9SDavid Howells struct fs_context *fc;
11824dcb3d9SDavid Howells const char *fs_name;
11924dcb3d9SDavid Howells int ret;
120007ec26cSDavid Howells
12124dcb3d9SDavid Howells if (!may_mount())
122a5f85d78SAl Viro return -EPERM;
12324dcb3d9SDavid Howells
12424dcb3d9SDavid Howells if (flags & ~FSOPEN_CLOEXEC)
12524dcb3d9SDavid Howells return -EINVAL;
12624dcb3d9SDavid Howells
12724dcb3d9SDavid Howells fs_name = strndup_user(_fs_name, PAGE_SIZE);
12824dcb3d9SDavid Howells if (IS_ERR(fs_name))
12924dcb3d9SDavid Howells return PTR_ERR(fs_name);
13024dcb3d9SDavid Howells
13124dcb3d9SDavid Howells fs_type = get_fs_type(fs_name);
13224dcb3d9SDavid Howells kfree(fs_name);
13324dcb3d9SDavid Howells if (!fs_type)
13424dcb3d9SDavid Howells return -ENODEV;
13524dcb3d9SDavid Howells
13624dcb3d9SDavid Howells fc = fs_context_for_mount(fs_type, 0);
13724dcb3d9SDavid Howells put_filesystem(fs_type);
13824dcb3d9SDavid Howells if (IS_ERR(fc))
13924dcb3d9SDavid Howells return PTR_ERR(fc);
14024dcb3d9SDavid Howells
14124dcb3d9SDavid Howells fc->phase = FS_CONTEXT_CREATE_PARAMS;
14224dcb3d9SDavid Howells
143007ec26cSDavid Howells ret = fscontext_alloc_log(fc);
144007ec26cSDavid Howells if (ret < 0)
145007ec26cSDavid Howells goto err_fc;
146007ec26cSDavid Howells
147007ec26cSDavid Howells return fscontext_create_fd(fc, flags & FSOPEN_CLOEXEC ? O_CLOEXEC : 0);
14824dcb3d9SDavid Howells
149007ec26cSDavid Howells err_fc:
150007ec26cSDavid Howells put_fs_context(fc);
151007ec26cSDavid Howells return ret;
152007ec26cSDavid Howells }
15324dcb3d9SDavid Howells
154ecdab150SDavid Howells /*
155ecdab150SDavid Howells * Pick a superblock into a context for reconfiguration.
156cf3cba4aSDavid Howells */
SYSCALL_DEFINE3(fspick,int,dfd,const char __user *,path,unsigned int,flags)157cf3cba4aSDavid Howells SYSCALL_DEFINE3(fspick, int, dfd, const char __user *, path, unsigned int, flags)
158cf3cba4aSDavid Howells {
159cf3cba4aSDavid Howells struct fs_context *fc;
160cf3cba4aSDavid Howells struct path target;
161cf3cba4aSDavid Howells unsigned int lookup_flags;
162cf3cba4aSDavid Howells int ret;
163cf3cba4aSDavid Howells
164cf3cba4aSDavid Howells if (!may_mount())
165a5f85d78SAl Viro return -EPERM;
166cf3cba4aSDavid Howells
167cf3cba4aSDavid Howells if ((flags & ~(FSPICK_CLOEXEC |
168cf3cba4aSDavid Howells FSPICK_SYMLINK_NOFOLLOW |
169cf3cba4aSDavid Howells FSPICK_NO_AUTOMOUNT |
170cf3cba4aSDavid Howells FSPICK_EMPTY_PATH)) != 0)
171cf3cba4aSDavid Howells return -EINVAL;
172cf3cba4aSDavid Howells
173cf3cba4aSDavid Howells lookup_flags = LOOKUP_FOLLOW | LOOKUP_AUTOMOUNT;
174cf3cba4aSDavid Howells if (flags & FSPICK_SYMLINK_NOFOLLOW)
175cf3cba4aSDavid Howells lookup_flags &= ~LOOKUP_FOLLOW;
176cf3cba4aSDavid Howells if (flags & FSPICK_NO_AUTOMOUNT)
177cf3cba4aSDavid Howells lookup_flags &= ~LOOKUP_AUTOMOUNT;
178cf3cba4aSDavid Howells if (flags & FSPICK_EMPTY_PATH)
179cf3cba4aSDavid Howells lookup_flags |= LOOKUP_EMPTY;
180cf3cba4aSDavid Howells ret = user_path_at(dfd, path, lookup_flags, &target);
181cf3cba4aSDavid Howells if (ret < 0)
182cf3cba4aSDavid Howells goto err;
183cf3cba4aSDavid Howells
184cf3cba4aSDavid Howells ret = -EINVAL;
185cf3cba4aSDavid Howells if (target.mnt->mnt_root != target.dentry)
186cf3cba4aSDavid Howells goto err_path;
187cf3cba4aSDavid Howells
188cf3cba4aSDavid Howells fc = fs_context_for_reconfigure(target.dentry, 0, 0);
189cf3cba4aSDavid Howells if (IS_ERR(fc)) {
190cf3cba4aSDavid Howells ret = PTR_ERR(fc);
191cf3cba4aSDavid Howells goto err_path;
192cf3cba4aSDavid Howells }
193cf3cba4aSDavid Howells
194cf3cba4aSDavid Howells fc->phase = FS_CONTEXT_RECONF_PARAMS;
195cf3cba4aSDavid Howells
196cf3cba4aSDavid Howells ret = fscontext_alloc_log(fc);
197cf3cba4aSDavid Howells if (ret < 0)
198cf3cba4aSDavid Howells goto err_fc;
199cf3cba4aSDavid Howells
200cf3cba4aSDavid Howells path_put(&target);
201cf3cba4aSDavid Howells return fscontext_create_fd(fc, flags & FSPICK_CLOEXEC ? O_CLOEXEC : 0);
202cf3cba4aSDavid Howells
203cf3cba4aSDavid Howells err_fc:
204cf3cba4aSDavid Howells put_fs_context(fc);
205cf3cba4aSDavid Howells err_path:
206cf3cba4aSDavid Howells path_put(&target);
207cf3cba4aSDavid Howells err:
208cf3cba4aSDavid Howells return ret;
209cf3cba4aSDavid Howells }
210cf3cba4aSDavid Howells
vfs_cmd_create(struct fs_context * fc,bool exclusive)211cf3cba4aSDavid Howells static int vfs_cmd_create(struct fs_context *fc, bool exclusive)
21222ed7ecdSChristian Brauner {
213dae8b08dSChristian Brauner struct super_block *sb;
214dae8b08dSChristian Brauner int ret;
215dae8b08dSChristian Brauner
216dae8b08dSChristian Brauner if (fc->phase != FS_CONTEXT_CREATE_PARAMS)
217dae8b08dSChristian Brauner return -EBUSY;
218dae8b08dSChristian Brauner
219dae8b08dSChristian Brauner if (!mount_capable(fc))
220dae8b08dSChristian Brauner return -EPERM;
221dae8b08dSChristian Brauner
222dae8b08dSChristian Brauner fc->phase = FS_CONTEXT_CREATING;
223dae8b08dSChristian Brauner fc->exclusive = exclusive;
22422ed7ecdSChristian Brauner
225dae8b08dSChristian Brauner ret = vfs_get_tree(fc);
226dae8b08dSChristian Brauner if (ret) {
227dae8b08dSChristian Brauner fc->phase = FS_CONTEXT_FAILED;
228dae8b08dSChristian Brauner return ret;
229dae8b08dSChristian Brauner }
230dae8b08dSChristian Brauner
231dae8b08dSChristian Brauner sb = fc->root->d_sb;
232dae8b08dSChristian Brauner ret = security_sb_kern_mount(sb);
233dae8b08dSChristian Brauner if (unlikely(ret)) {
234dae8b08dSChristian Brauner fc_drop_locked(fc);
235dae8b08dSChristian Brauner fc->phase = FS_CONTEXT_FAILED;
236dae8b08dSChristian Brauner return ret;
237dae8b08dSChristian Brauner }
238dae8b08dSChristian Brauner
239dae8b08dSChristian Brauner /* vfs_get_tree() callchains will have grabbed @s_umount */
240dae8b08dSChristian Brauner up_write(&sb->s_umount);
241dae8b08dSChristian Brauner fc->phase = FS_CONTEXT_AWAITING_MOUNT;
242dae8b08dSChristian Brauner return 0;
243dae8b08dSChristian Brauner }
244dae8b08dSChristian Brauner
vfs_cmd_reconfigure(struct fs_context * fc)245dae8b08dSChristian Brauner static int vfs_cmd_reconfigure(struct fs_context *fc)
24611a51d8cSChristian Brauner {
24711a51d8cSChristian Brauner struct super_block *sb;
24811a51d8cSChristian Brauner int ret;
24911a51d8cSChristian Brauner
25011a51d8cSChristian Brauner if (fc->phase != FS_CONTEXT_RECONF_PARAMS)
25111a51d8cSChristian Brauner return -EBUSY;
25211a51d8cSChristian Brauner
25311a51d8cSChristian Brauner fc->phase = FS_CONTEXT_RECONFIGURING;
25411a51d8cSChristian Brauner
25511a51d8cSChristian Brauner sb = fc->root->d_sb;
25611a51d8cSChristian Brauner if (!ns_capable(sb->s_user_ns, CAP_SYS_ADMIN)) {
25711a51d8cSChristian Brauner fc->phase = FS_CONTEXT_FAILED;
25811a51d8cSChristian Brauner return -EPERM;
25911a51d8cSChristian Brauner }
26011a51d8cSChristian Brauner
26111a51d8cSChristian Brauner down_write(&sb->s_umount);
26211a51d8cSChristian Brauner ret = reconfigure_super(fc);
26311a51d8cSChristian Brauner up_write(&sb->s_umount);
26411a51d8cSChristian Brauner if (ret) {
26511a51d8cSChristian Brauner fc->phase = FS_CONTEXT_FAILED;
26611a51d8cSChristian Brauner return ret;
26711a51d8cSChristian Brauner }
26811a51d8cSChristian Brauner
26911a51d8cSChristian Brauner vfs_clean_context(fc);
27011a51d8cSChristian Brauner return 0;
27111a51d8cSChristian Brauner }
27211a51d8cSChristian Brauner
27311a51d8cSChristian Brauner /*
274cf3cba4aSDavid Howells * Check the state and apply the configuration. Note that this function is
275ecdab150SDavid Howells * allowed to 'steal' the value by setting param->xxx to NULL before returning.
276ecdab150SDavid Howells */
vfs_fsconfig_locked(struct fs_context * fc,int cmd,struct fs_parameter * param)277ecdab150SDavid Howells static int vfs_fsconfig_locked(struct fs_context *fc, int cmd,
278ecdab150SDavid Howells struct fs_parameter *param)
279ecdab150SDavid Howells {
280ecdab150SDavid Howells int ret;
281ecdab150SDavid Howells
282ecdab150SDavid Howells ret = finish_clean_context(fc);
283ecdab150SDavid Howells if (ret)
284ecdab150SDavid Howells return ret;
285ecdab150SDavid Howells switch (cmd) {
286ecdab150SDavid Howells case FSCONFIG_CMD_CREATE:
287ecdab150SDavid Howells return vfs_cmd_create(fc, false);
28822ed7ecdSChristian Brauner case FSCONFIG_CMD_CREATE_EXCL:
28922ed7ecdSChristian Brauner return vfs_cmd_create(fc, true);
29022ed7ecdSChristian Brauner case FSCONFIG_CMD_RECONFIGURE:
291ecdab150SDavid Howells return vfs_cmd_reconfigure(fc);
29211a51d8cSChristian Brauner default:
293ecdab150SDavid Howells if (fc->phase != FS_CONTEXT_CREATE_PARAMS &&
294ecdab150SDavid Howells fc->phase != FS_CONTEXT_RECONF_PARAMS)
295ecdab150SDavid Howells return -EBUSY;
296ecdab150SDavid Howells
297ecdab150SDavid Howells return vfs_parse_fs_param(fc, param);
298ecdab150SDavid Howells }
299ecdab150SDavid Howells }
300ecdab150SDavid Howells
301ecdab150SDavid Howells /**
302ecdab150SDavid Howells * sys_fsconfig - Set parameters and trigger actions on a context
303ecdab150SDavid Howells * @fd: The filesystem context to act upon
304ecdab150SDavid Howells * @cmd: The action to take
305ecdab150SDavid Howells * @_key: Where appropriate, the parameter key to set
306ecdab150SDavid Howells * @_value: Where appropriate, the parameter value to set
307ecdab150SDavid Howells * @aux: Additional information for the value
308ecdab150SDavid Howells *
309ecdab150SDavid Howells * This system call is used to set parameters on a context, including
310ecdab150SDavid Howells * superblock settings, data source and security labelling.
311ecdab150SDavid Howells *
312ecdab150SDavid Howells * Actions include triggering the creation of a superblock and the
313ecdab150SDavid Howells * reconfiguration of the superblock attached to the specified context.
314ecdab150SDavid Howells *
315ecdab150SDavid Howells * When setting a parameter, @cmd indicates the type of value being proposed
316ecdab150SDavid Howells * and @_key indicates the parameter to be altered.
317ecdab150SDavid Howells *
318ecdab150SDavid Howells * @_value and @aux are used to specify the value, should a value be required:
319ecdab150SDavid Howells *
320ecdab150SDavid Howells * (*) fsconfig_set_flag: No value is specified. The parameter must be boolean
321ecdab150SDavid Howells * in nature. The key may be prefixed with "no" to invert the
322ecdab150SDavid Howells * setting. @_value must be NULL and @aux must be 0.
323ecdab150SDavid Howells *
324ecdab150SDavid Howells * (*) fsconfig_set_string: A string value is specified. The parameter can be
325ecdab150SDavid Howells * expecting boolean, integer, string or take a path. A conversion to an
326ecdab150SDavid Howells * appropriate type will be attempted (which may include looking up as a
327ecdab150SDavid Howells * path). @_value points to a NUL-terminated string and @aux must be 0.
328ecdab150SDavid Howells *
329ecdab150SDavid Howells * (*) fsconfig_set_binary: A binary blob is specified. @_value points to the
330ecdab150SDavid Howells * blob and @aux indicates its size. The parameter must be expecting a
331ecdab150SDavid Howells * blob.
332ecdab150SDavid Howells *
333ecdab150SDavid Howells * (*) fsconfig_set_path: A non-empty path is specified. The parameter must be
334ecdab150SDavid Howells * expecting a path object. @_value points to a NUL-terminated string that
335ecdab150SDavid Howells * is the path and @aux is a file descriptor at which to start a relative
336ecdab150SDavid Howells * lookup or AT_FDCWD.
337ecdab150SDavid Howells *
338ecdab150SDavid Howells * (*) fsconfig_set_path_empty: As fsconfig_set_path, but with AT_EMPTY_PATH
339ecdab150SDavid Howells * implied.
340ecdab150SDavid Howells *
341ecdab150SDavid Howells * (*) fsconfig_set_fd: An open file descriptor is specified. @_value must be
342ecdab150SDavid Howells * NULL and @aux indicates the file descriptor.
343ecdab150SDavid Howells */
SYSCALL_DEFINE5(fsconfig,int,fd,unsigned int,cmd,const char __user *,_key,const void __user *,_value,int,aux)344ecdab150SDavid Howells SYSCALL_DEFINE5(fsconfig,
345ecdab150SDavid Howells int, fd,
346ecdab150SDavid Howells unsigned int, cmd,
347ecdab150SDavid Howells const char __user *, _key,
348ecdab150SDavid Howells const void __user *, _value,
349ecdab150SDavid Howells int, aux)
350ecdab150SDavid Howells {
351ecdab150SDavid Howells struct fs_context *fc;
352ecdab150SDavid Howells struct fd f;
353ecdab150SDavid Howells int ret;
354ecdab150SDavid Howells int lookup_flags = 0;
355aa1918f9SAl Viro
356ecdab150SDavid Howells struct fs_parameter param = {
357ecdab150SDavid Howells .type = fs_value_is_undefined,
358ecdab150SDavid Howells };
359ecdab150SDavid Howells
360ecdab150SDavid Howells if (fd < 0)
361ecdab150SDavid Howells return -EINVAL;
362ecdab150SDavid Howells
363ecdab150SDavid Howells switch (cmd) {
364ecdab150SDavid Howells case FSCONFIG_SET_FLAG:
365ecdab150SDavid Howells if (!_key || _value || aux)
366ecdab150SDavid Howells return -EINVAL;
367ecdab150SDavid Howells break;
368ecdab150SDavid Howells case FSCONFIG_SET_STRING:
369ecdab150SDavid Howells if (!_key || !_value || aux)
370ecdab150SDavid Howells return -EINVAL;
371ecdab150SDavid Howells break;
372ecdab150SDavid Howells case FSCONFIG_SET_BINARY:
373ecdab150SDavid Howells if (!_key || !_value || aux <= 0 || aux > 1024 * 1024)
374ecdab150SDavid Howells return -EINVAL;
375ecdab150SDavid Howells break;
376ecdab150SDavid Howells case FSCONFIG_SET_PATH:
377ecdab150SDavid Howells case FSCONFIG_SET_PATH_EMPTY:
378ecdab150SDavid Howells if (!_key || !_value || (aux != AT_FDCWD && aux < 0))
379ecdab150SDavid Howells return -EINVAL;
380ecdab150SDavid Howells break;
381ecdab150SDavid Howells case FSCONFIG_SET_FD:
382ecdab150SDavid Howells if (!_key || _value || aux < 0)
383ecdab150SDavid Howells return -EINVAL;
384ecdab150SDavid Howells break;
385ecdab150SDavid Howells case FSCONFIG_CMD_CREATE:
386ecdab150SDavid Howells case FSCONFIG_CMD_CREATE_EXCL:
38722ed7ecdSChristian Brauner case FSCONFIG_CMD_RECONFIGURE:
388ecdab150SDavid Howells if (_key || _value || aux)
389ecdab150SDavid Howells return -EINVAL;
390ecdab150SDavid Howells break;
391ecdab150SDavid Howells default:
392ecdab150SDavid Howells return -EOPNOTSUPP;
393ecdab150SDavid Howells }
394ecdab150SDavid Howells
395ecdab150SDavid Howells f = fdget(fd);
396ecdab150SDavid Howells if (!fd_file(f))
397*1da91ea8SAl Viro return -EBADF;
398ecdab150SDavid Howells ret = -EINVAL;
399ecdab150SDavid Howells if (fd_file(f)->f_op != &fscontext_fops)
400*1da91ea8SAl Viro goto out_f;
401ecdab150SDavid Howells
402ecdab150SDavid Howells fc = fd_file(f)->private_data;
403*1da91ea8SAl Viro if (fc->ops == &legacy_fs_context_ops) {
404ecdab150SDavid Howells switch (cmd) {
405ecdab150SDavid Howells case FSCONFIG_SET_BINARY:
406ecdab150SDavid Howells case FSCONFIG_SET_PATH:
407ecdab150SDavid Howells case FSCONFIG_SET_PATH_EMPTY:
408ecdab150SDavid Howells case FSCONFIG_SET_FD:
409ecdab150SDavid Howells case FSCONFIG_CMD_CREATE_EXCL:
410ef44c8abSHongbo Li ret = -EOPNOTSUPP;
411ecdab150SDavid Howells goto out_f;
412ecdab150SDavid Howells }
413ecdab150SDavid Howells }
414ecdab150SDavid Howells
415ecdab150SDavid Howells if (_key) {
416ecdab150SDavid Howells param.key = strndup_user(_key, 256);
417ecdab150SDavid Howells if (IS_ERR(param.key)) {
418ecdab150SDavid Howells ret = PTR_ERR(param.key);
419ecdab150SDavid Howells goto out_f;
420ecdab150SDavid Howells }
421ecdab150SDavid Howells }
422ecdab150SDavid Howells
423ecdab150SDavid Howells switch (cmd) {
424ecdab150SDavid Howells case FSCONFIG_SET_FLAG:
425ecdab150SDavid Howells param.type = fs_value_is_flag;
426ecdab150SDavid Howells break;
427ecdab150SDavid Howells case FSCONFIG_SET_STRING:
428ecdab150SDavid Howells param.type = fs_value_is_string;
429ecdab150SDavid Howells param.string = strndup_user(_value, 256);
430ecdab150SDavid Howells if (IS_ERR(param.string)) {
431ecdab150SDavid Howells ret = PTR_ERR(param.string);
432ecdab150SDavid Howells goto out_key;
433ecdab150SDavid Howells }
434ecdab150SDavid Howells param.size = strlen(param.string);
435ecdab150SDavid Howells break;
436ecdab150SDavid Howells case FSCONFIG_SET_BINARY:
437ecdab150SDavid Howells param.type = fs_value_is_blob;
438ecdab150SDavid Howells param.size = aux;
439ecdab150SDavid Howells param.blob = memdup_user_nul(_value, aux);
440ecdab150SDavid Howells if (IS_ERR(param.blob)) {
441ecdab150SDavid Howells ret = PTR_ERR(param.blob);
442ecdab150SDavid Howells goto out_key;
443ecdab150SDavid Howells }
444ecdab150SDavid Howells break;
445ecdab150SDavid Howells case FSCONFIG_SET_PATH_EMPTY:
446aa1918f9SAl Viro lookup_flags = LOOKUP_EMPTY;
447aa1918f9SAl Viro fallthrough;
448df561f66SGustavo A. R. Silva case FSCONFIG_SET_PATH:
449ecdab150SDavid Howells param.type = fs_value_is_filename;
450ecdab150SDavid Howells param.name = getname_flags(_value, lookup_flags);
451dff60734SMateusz Guzik if (IS_ERR(param.name)) {
452ecdab150SDavid Howells ret = PTR_ERR(param.name);
453ecdab150SDavid Howells goto out_key;
454ecdab150SDavid Howells }
455ecdab150SDavid Howells param.dirfd = aux;
456ecdab150SDavid Howells param.size = strlen(param.name->name);
457ecdab150SDavid Howells break;
458ecdab150SDavid Howells case FSCONFIG_SET_FD:
459ecdab150SDavid Howells param.type = fs_value_is_file;
460ecdab150SDavid Howells ret = -EBADF;
461ecdab150SDavid Howells param.file = fget(aux);
462ecdab150SDavid Howells if (!param.file)
463ecdab150SDavid Howells goto out_key;
464ecdab150SDavid Howells param.dirfd = aux;
4659cf16b38SChristian Brauner break;
466ecdab150SDavid Howells default:
467ecdab150SDavid Howells break;
468ecdab150SDavid Howells }
469ecdab150SDavid Howells
470ecdab150SDavid Howells ret = mutex_lock_interruptible(&fc->uapi_mutex);
471ecdab150SDavid Howells if (ret == 0) {
472ecdab150SDavid Howells ret = vfs_fsconfig_locked(fc, cmd, ¶m);
473ecdab150SDavid Howells mutex_unlock(&fc->uapi_mutex);
474ecdab150SDavid Howells }
475ecdab150SDavid Howells
476ecdab150SDavid Howells /* Clean up the our record of any value that we obtained from
477ecdab150SDavid Howells * userspace. Note that the value may have been stolen by the LSM or
478ecdab150SDavid Howells * filesystem, in which case the value pointer will have been cleared.
479ecdab150SDavid Howells */
480ecdab150SDavid Howells switch (cmd) {
481ecdab150SDavid Howells case FSCONFIG_SET_STRING:
482ecdab150SDavid Howells case FSCONFIG_SET_BINARY:
483ecdab150SDavid Howells kfree(param.string);
484ecdab150SDavid Howells break;
485ecdab150SDavid Howells case FSCONFIG_SET_PATH:
486ecdab150SDavid Howells case FSCONFIG_SET_PATH_EMPTY:
487ecdab150SDavid Howells if (param.name)
488ecdab150SDavid Howells putname(param.name);
489ecdab150SDavid Howells break;
490ecdab150SDavid Howells case FSCONFIG_SET_FD:
491ecdab150SDavid Howells if (param.file)
492ecdab150SDavid Howells fput(param.file);
493ecdab150SDavid Howells break;
494ecdab150SDavid Howells default:
495ecdab150SDavid Howells break;
496ecdab150SDavid Howells }
497ecdab150SDavid Howells out_key:
498ecdab150SDavid Howells kfree(param.key);
499ecdab150SDavid Howells out_f:
500ecdab150SDavid Howells fdput(f);
501ecdab150SDavid Howells return ret;
502ecdab150SDavid Howells }
503ecdab150SDavid Howells