xref: /openbmc/linux/fs/fsopen.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
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 	.llseek		= no_llseek,
8224dcb3d9SDavid Howells };
8324dcb3d9SDavid Howells 
8424dcb3d9SDavid Howells /*
8524dcb3d9SDavid Howells  * Attach a filesystem context to a file and an fd.
8624dcb3d9SDavid Howells  */
fscontext_create_fd(struct fs_context * fc,unsigned int o_flags)8724dcb3d9SDavid Howells static int fscontext_create_fd(struct fs_context *fc, unsigned int o_flags)
8824dcb3d9SDavid Howells {
8924dcb3d9SDavid Howells 	int fd;
9024dcb3d9SDavid Howells 
911cdc415fSChristian Brauner 	fd = anon_inode_getfd("[fscontext]", &fscontext_fops, fc,
9224dcb3d9SDavid Howells 			      O_RDWR | o_flags);
9324dcb3d9SDavid Howells 	if (fd < 0)
9424dcb3d9SDavid Howells 		put_fs_context(fc);
9524dcb3d9SDavid Howells 	return fd;
9624dcb3d9SDavid Howells }
9724dcb3d9SDavid Howells 
fscontext_alloc_log(struct fs_context * fc)98007ec26cSDavid Howells static int fscontext_alloc_log(struct fs_context *fc)
99007ec26cSDavid Howells {
100cc3c0b53SAl Viro 	fc->log.log = kzalloc(sizeof(*fc->log.log), GFP_KERNEL);
101cc3c0b53SAl Viro 	if (!fc->log.log)
102007ec26cSDavid Howells 		return -ENOMEM;
103cc3c0b53SAl Viro 	refcount_set(&fc->log.log->usage, 1);
104cc3c0b53SAl Viro 	fc->log.log->owner = fc->fs_type->owner;
105007ec26cSDavid Howells 	return 0;
106007ec26cSDavid Howells }
107007ec26cSDavid Howells 
10824dcb3d9SDavid Howells /*
10924dcb3d9SDavid Howells  * Open a filesystem by name so that it can be configured for mounting.
11024dcb3d9SDavid Howells  *
11124dcb3d9SDavid Howells  * We are allowed to specify a container in which the filesystem will be
11224dcb3d9SDavid Howells  * opened, thereby indicating which namespaces will be used (notably, which
11324dcb3d9SDavid Howells  * network namespace will be used for network filesystems).
11424dcb3d9SDavid Howells  */
SYSCALL_DEFINE2(fsopen,const char __user *,_fs_name,unsigned int,flags)11524dcb3d9SDavid Howells SYSCALL_DEFINE2(fsopen, const char __user *, _fs_name, unsigned int, flags)
11624dcb3d9SDavid Howells {
11724dcb3d9SDavid Howells 	struct file_system_type *fs_type;
11824dcb3d9SDavid Howells 	struct fs_context *fc;
11924dcb3d9SDavid Howells 	const char *fs_name;
120007ec26cSDavid Howells 	int ret;
12124dcb3d9SDavid Howells 
122a5f85d78SAl Viro 	if (!may_mount())
12324dcb3d9SDavid Howells 		return -EPERM;
12424dcb3d9SDavid Howells 
12524dcb3d9SDavid Howells 	if (flags & ~FSOPEN_CLOEXEC)
12624dcb3d9SDavid Howells 		return -EINVAL;
12724dcb3d9SDavid Howells 
12824dcb3d9SDavid Howells 	fs_name = strndup_user(_fs_name, PAGE_SIZE);
12924dcb3d9SDavid Howells 	if (IS_ERR(fs_name))
13024dcb3d9SDavid Howells 		return PTR_ERR(fs_name);
13124dcb3d9SDavid Howells 
13224dcb3d9SDavid Howells 	fs_type = get_fs_type(fs_name);
13324dcb3d9SDavid Howells 	kfree(fs_name);
13424dcb3d9SDavid Howells 	if (!fs_type)
13524dcb3d9SDavid Howells 		return -ENODEV;
13624dcb3d9SDavid Howells 
13724dcb3d9SDavid Howells 	fc = fs_context_for_mount(fs_type, 0);
13824dcb3d9SDavid Howells 	put_filesystem(fs_type);
13924dcb3d9SDavid Howells 	if (IS_ERR(fc))
14024dcb3d9SDavid Howells 		return PTR_ERR(fc);
14124dcb3d9SDavid Howells 
14224dcb3d9SDavid Howells 	fc->phase = FS_CONTEXT_CREATE_PARAMS;
143007ec26cSDavid Howells 
144007ec26cSDavid Howells 	ret = fscontext_alloc_log(fc);
145007ec26cSDavid Howells 	if (ret < 0)
146007ec26cSDavid Howells 		goto err_fc;
147007ec26cSDavid Howells 
14824dcb3d9SDavid Howells 	return fscontext_create_fd(fc, flags & FSOPEN_CLOEXEC ? O_CLOEXEC : 0);
149007ec26cSDavid Howells 
150007ec26cSDavid Howells err_fc:
151007ec26cSDavid Howells 	put_fs_context(fc);
152007ec26cSDavid Howells 	return ret;
15324dcb3d9SDavid Howells }
154ecdab150SDavid Howells 
155ecdab150SDavid Howells /*
156cf3cba4aSDavid Howells  * Pick a superblock into a context for reconfiguration.
157cf3cba4aSDavid Howells  */
SYSCALL_DEFINE3(fspick,int,dfd,const char __user *,path,unsigned int,flags)158cf3cba4aSDavid Howells SYSCALL_DEFINE3(fspick, int, dfd, const char __user *, path, unsigned int, flags)
159cf3cba4aSDavid Howells {
160cf3cba4aSDavid Howells 	struct fs_context *fc;
161cf3cba4aSDavid Howells 	struct path target;
162cf3cba4aSDavid Howells 	unsigned int lookup_flags;
163cf3cba4aSDavid Howells 	int ret;
164cf3cba4aSDavid Howells 
165a5f85d78SAl Viro 	if (!may_mount())
166cf3cba4aSDavid Howells 		return -EPERM;
167cf3cba4aSDavid Howells 
168cf3cba4aSDavid Howells 	if ((flags & ~(FSPICK_CLOEXEC |
169cf3cba4aSDavid Howells 		       FSPICK_SYMLINK_NOFOLLOW |
170cf3cba4aSDavid Howells 		       FSPICK_NO_AUTOMOUNT |
171cf3cba4aSDavid Howells 		       FSPICK_EMPTY_PATH)) != 0)
172cf3cba4aSDavid Howells 		return -EINVAL;
173cf3cba4aSDavid Howells 
174cf3cba4aSDavid Howells 	lookup_flags = LOOKUP_FOLLOW | LOOKUP_AUTOMOUNT;
175cf3cba4aSDavid Howells 	if (flags & FSPICK_SYMLINK_NOFOLLOW)
176cf3cba4aSDavid Howells 		lookup_flags &= ~LOOKUP_FOLLOW;
177cf3cba4aSDavid Howells 	if (flags & FSPICK_NO_AUTOMOUNT)
178cf3cba4aSDavid Howells 		lookup_flags &= ~LOOKUP_AUTOMOUNT;
179cf3cba4aSDavid Howells 	if (flags & FSPICK_EMPTY_PATH)
180cf3cba4aSDavid Howells 		lookup_flags |= LOOKUP_EMPTY;
181cf3cba4aSDavid Howells 	ret = user_path_at(dfd, path, lookup_flags, &target);
182cf3cba4aSDavid Howells 	if (ret < 0)
183cf3cba4aSDavid Howells 		goto err;
184cf3cba4aSDavid Howells 
185cf3cba4aSDavid Howells 	ret = -EINVAL;
186cf3cba4aSDavid Howells 	if (target.mnt->mnt_root != target.dentry)
187cf3cba4aSDavid Howells 		goto err_path;
188cf3cba4aSDavid Howells 
189cf3cba4aSDavid Howells 	fc = fs_context_for_reconfigure(target.dentry, 0, 0);
190cf3cba4aSDavid Howells 	if (IS_ERR(fc)) {
191cf3cba4aSDavid Howells 		ret = PTR_ERR(fc);
192cf3cba4aSDavid Howells 		goto err_path;
193cf3cba4aSDavid Howells 	}
194cf3cba4aSDavid Howells 
195cf3cba4aSDavid Howells 	fc->phase = FS_CONTEXT_RECONF_PARAMS;
196cf3cba4aSDavid Howells 
197cf3cba4aSDavid Howells 	ret = fscontext_alloc_log(fc);
198cf3cba4aSDavid Howells 	if (ret < 0)
199cf3cba4aSDavid Howells 		goto err_fc;
200cf3cba4aSDavid Howells 
201cf3cba4aSDavid Howells 	path_put(&target);
202cf3cba4aSDavid Howells 	return fscontext_create_fd(fc, flags & FSPICK_CLOEXEC ? O_CLOEXEC : 0);
203cf3cba4aSDavid Howells 
204cf3cba4aSDavid Howells err_fc:
205cf3cba4aSDavid Howells 	put_fs_context(fc);
206cf3cba4aSDavid Howells err_path:
207cf3cba4aSDavid Howells 	path_put(&target);
208cf3cba4aSDavid Howells err:
209cf3cba4aSDavid Howells 	return ret;
210cf3cba4aSDavid Howells }
211cf3cba4aSDavid Howells 
vfs_cmd_create(struct fs_context * fc,bool exclusive)212*22ed7ecdSChristian Brauner static int vfs_cmd_create(struct fs_context *fc, bool exclusive)
213dae8b08dSChristian Brauner {
214dae8b08dSChristian Brauner 	struct super_block *sb;
215dae8b08dSChristian Brauner 	int ret;
216dae8b08dSChristian Brauner 
217dae8b08dSChristian Brauner 	if (fc->phase != FS_CONTEXT_CREATE_PARAMS)
218dae8b08dSChristian Brauner 		return -EBUSY;
219dae8b08dSChristian Brauner 
220dae8b08dSChristian Brauner 	if (!mount_capable(fc))
221dae8b08dSChristian Brauner 		return -EPERM;
222dae8b08dSChristian Brauner 
223*22ed7ecdSChristian Brauner 	/* require the new mount api */
224*22ed7ecdSChristian Brauner 	if (exclusive && fc->ops == &legacy_fs_context_ops)
225*22ed7ecdSChristian Brauner 		return -EOPNOTSUPP;
226*22ed7ecdSChristian Brauner 
227dae8b08dSChristian Brauner 	fc->phase = FS_CONTEXT_CREATING;
228*22ed7ecdSChristian Brauner 	fc->exclusive = exclusive;
229dae8b08dSChristian Brauner 
230dae8b08dSChristian Brauner 	ret = vfs_get_tree(fc);
231dae8b08dSChristian Brauner 	if (ret) {
232dae8b08dSChristian Brauner 		fc->phase = FS_CONTEXT_FAILED;
233dae8b08dSChristian Brauner 		return ret;
234dae8b08dSChristian Brauner 	}
235dae8b08dSChristian Brauner 
236dae8b08dSChristian Brauner 	sb = fc->root->d_sb;
237dae8b08dSChristian Brauner 	ret = security_sb_kern_mount(sb);
238dae8b08dSChristian Brauner 	if (unlikely(ret)) {
239dae8b08dSChristian Brauner 		fc_drop_locked(fc);
240dae8b08dSChristian Brauner 		fc->phase = FS_CONTEXT_FAILED;
241dae8b08dSChristian Brauner 		return ret;
242dae8b08dSChristian Brauner 	}
243dae8b08dSChristian Brauner 
244dae8b08dSChristian Brauner 	/* vfs_get_tree() callchains will have grabbed @s_umount */
245dae8b08dSChristian Brauner 	up_write(&sb->s_umount);
246dae8b08dSChristian Brauner 	fc->phase = FS_CONTEXT_AWAITING_MOUNT;
247dae8b08dSChristian Brauner 	return 0;
248dae8b08dSChristian Brauner }
249dae8b08dSChristian Brauner 
vfs_cmd_reconfigure(struct fs_context * fc)25011a51d8cSChristian Brauner static int vfs_cmd_reconfigure(struct fs_context *fc)
25111a51d8cSChristian Brauner {
25211a51d8cSChristian Brauner 	struct super_block *sb;
25311a51d8cSChristian Brauner 	int ret;
25411a51d8cSChristian Brauner 
25511a51d8cSChristian Brauner 	if (fc->phase != FS_CONTEXT_RECONF_PARAMS)
25611a51d8cSChristian Brauner 		return -EBUSY;
25711a51d8cSChristian Brauner 
25811a51d8cSChristian Brauner 	fc->phase = FS_CONTEXT_RECONFIGURING;
25911a51d8cSChristian Brauner 
26011a51d8cSChristian Brauner 	sb = fc->root->d_sb;
26111a51d8cSChristian Brauner 	if (!ns_capable(sb->s_user_ns, CAP_SYS_ADMIN)) {
26211a51d8cSChristian Brauner 		fc->phase = FS_CONTEXT_FAILED;
26311a51d8cSChristian Brauner 		return -EPERM;
26411a51d8cSChristian Brauner 	}
26511a51d8cSChristian Brauner 
26611a51d8cSChristian Brauner 	down_write(&sb->s_umount);
26711a51d8cSChristian Brauner 	ret = reconfigure_super(fc);
26811a51d8cSChristian Brauner 	up_write(&sb->s_umount);
26911a51d8cSChristian Brauner 	if (ret) {
27011a51d8cSChristian Brauner 		fc->phase = FS_CONTEXT_FAILED;
27111a51d8cSChristian Brauner 		return ret;
27211a51d8cSChristian Brauner 	}
27311a51d8cSChristian Brauner 
27411a51d8cSChristian Brauner 	vfs_clean_context(fc);
27511a51d8cSChristian Brauner 	return 0;
27611a51d8cSChristian Brauner }
27711a51d8cSChristian Brauner 
278cf3cba4aSDavid Howells /*
279ecdab150SDavid Howells  * Check the state and apply the configuration.  Note that this function is
280ecdab150SDavid Howells  * allowed to 'steal' the value by setting param->xxx to NULL before returning.
281ecdab150SDavid Howells  */
vfs_fsconfig_locked(struct fs_context * fc,int cmd,struct fs_parameter * param)282ecdab150SDavid Howells static int vfs_fsconfig_locked(struct fs_context *fc, int cmd,
283ecdab150SDavid Howells 			       struct fs_parameter *param)
284ecdab150SDavid Howells {
285ecdab150SDavid Howells 	int ret;
286ecdab150SDavid Howells 
287ecdab150SDavid Howells 	ret = finish_clean_context(fc);
288ecdab150SDavid Howells 	if (ret)
289ecdab150SDavid Howells 		return ret;
290ecdab150SDavid Howells 	switch (cmd) {
291ecdab150SDavid Howells 	case FSCONFIG_CMD_CREATE:
292*22ed7ecdSChristian Brauner 		return vfs_cmd_create(fc, false);
293*22ed7ecdSChristian Brauner 	case FSCONFIG_CMD_CREATE_EXCL:
294*22ed7ecdSChristian Brauner 		return vfs_cmd_create(fc, true);
295ecdab150SDavid Howells 	case FSCONFIG_CMD_RECONFIGURE:
29611a51d8cSChristian Brauner 		return vfs_cmd_reconfigure(fc);
297ecdab150SDavid Howells 	default:
298ecdab150SDavid Howells 		if (fc->phase != FS_CONTEXT_CREATE_PARAMS &&
299ecdab150SDavid Howells 		    fc->phase != FS_CONTEXT_RECONF_PARAMS)
300ecdab150SDavid Howells 			return -EBUSY;
301ecdab150SDavid Howells 
302ecdab150SDavid Howells 		return vfs_parse_fs_param(fc, param);
303ecdab150SDavid Howells 	}
304ecdab150SDavid Howells }
305ecdab150SDavid Howells 
306ecdab150SDavid Howells /**
307ecdab150SDavid Howells  * sys_fsconfig - Set parameters and trigger actions on a context
308ecdab150SDavid Howells  * @fd: The filesystem context to act upon
309ecdab150SDavid Howells  * @cmd: The action to take
310ecdab150SDavid Howells  * @_key: Where appropriate, the parameter key to set
311ecdab150SDavid Howells  * @_value: Where appropriate, the parameter value to set
312ecdab150SDavid Howells  * @aux: Additional information for the value
313ecdab150SDavid Howells  *
314ecdab150SDavid Howells  * This system call is used to set parameters on a context, including
315ecdab150SDavid Howells  * superblock settings, data source and security labelling.
316ecdab150SDavid Howells  *
317ecdab150SDavid Howells  * Actions include triggering the creation of a superblock and the
318ecdab150SDavid Howells  * reconfiguration of the superblock attached to the specified context.
319ecdab150SDavid Howells  *
320ecdab150SDavid Howells  * When setting a parameter, @cmd indicates the type of value being proposed
321ecdab150SDavid Howells  * and @_key indicates the parameter to be altered.
322ecdab150SDavid Howells  *
323ecdab150SDavid Howells  * @_value and @aux are used to specify the value, should a value be required:
324ecdab150SDavid Howells  *
325ecdab150SDavid Howells  * (*) fsconfig_set_flag: No value is specified.  The parameter must be boolean
326ecdab150SDavid Howells  *     in nature.  The key may be prefixed with "no" to invert the
327ecdab150SDavid Howells  *     setting. @_value must be NULL and @aux must be 0.
328ecdab150SDavid Howells  *
329ecdab150SDavid Howells  * (*) fsconfig_set_string: A string value is specified.  The parameter can be
330ecdab150SDavid Howells  *     expecting boolean, integer, string or take a path.  A conversion to an
331ecdab150SDavid Howells  *     appropriate type will be attempted (which may include looking up as a
332ecdab150SDavid Howells  *     path).  @_value points to a NUL-terminated string and @aux must be 0.
333ecdab150SDavid Howells  *
334ecdab150SDavid Howells  * (*) fsconfig_set_binary: A binary blob is specified.  @_value points to the
335ecdab150SDavid Howells  *     blob and @aux indicates its size.  The parameter must be expecting a
336ecdab150SDavid Howells  *     blob.
337ecdab150SDavid Howells  *
338ecdab150SDavid Howells  * (*) fsconfig_set_path: A non-empty path is specified.  The parameter must be
339ecdab150SDavid Howells  *     expecting a path object.  @_value points to a NUL-terminated string that
340ecdab150SDavid Howells  *     is the path and @aux is a file descriptor at which to start a relative
341ecdab150SDavid Howells  *     lookup or AT_FDCWD.
342ecdab150SDavid Howells  *
343ecdab150SDavid Howells  * (*) fsconfig_set_path_empty: As fsconfig_set_path, but with AT_EMPTY_PATH
344ecdab150SDavid Howells  *     implied.
345ecdab150SDavid Howells  *
346ecdab150SDavid Howells  * (*) fsconfig_set_fd: An open file descriptor is specified.  @_value must be
347ecdab150SDavid Howells  *     NULL and @aux indicates the file descriptor.
348ecdab150SDavid Howells  */
SYSCALL_DEFINE5(fsconfig,int,fd,unsigned int,cmd,const char __user *,_key,const void __user *,_value,int,aux)349ecdab150SDavid Howells SYSCALL_DEFINE5(fsconfig,
350ecdab150SDavid Howells 		int, fd,
351ecdab150SDavid Howells 		unsigned int, cmd,
352ecdab150SDavid Howells 		const char __user *, _key,
353ecdab150SDavid Howells 		const void __user *, _value,
354ecdab150SDavid Howells 		int, aux)
355ecdab150SDavid Howells {
356ecdab150SDavid Howells 	struct fs_context *fc;
357ecdab150SDavid Howells 	struct fd f;
358ecdab150SDavid Howells 	int ret;
359aa1918f9SAl Viro 	int lookup_flags = 0;
360ecdab150SDavid Howells 
361ecdab150SDavid Howells 	struct fs_parameter param = {
362ecdab150SDavid Howells 		.type	= fs_value_is_undefined,
363ecdab150SDavid Howells 	};
364ecdab150SDavid Howells 
365ecdab150SDavid Howells 	if (fd < 0)
366ecdab150SDavid Howells 		return -EINVAL;
367ecdab150SDavid Howells 
368ecdab150SDavid Howells 	switch (cmd) {
369ecdab150SDavid Howells 	case FSCONFIG_SET_FLAG:
370ecdab150SDavid Howells 		if (!_key || _value || aux)
371ecdab150SDavid Howells 			return -EINVAL;
372ecdab150SDavid Howells 		break;
373ecdab150SDavid Howells 	case FSCONFIG_SET_STRING:
374ecdab150SDavid Howells 		if (!_key || !_value || aux)
375ecdab150SDavid Howells 			return -EINVAL;
376ecdab150SDavid Howells 		break;
377ecdab150SDavid Howells 	case FSCONFIG_SET_BINARY:
378ecdab150SDavid Howells 		if (!_key || !_value || aux <= 0 || aux > 1024 * 1024)
379ecdab150SDavid Howells 			return -EINVAL;
380ecdab150SDavid Howells 		break;
381ecdab150SDavid Howells 	case FSCONFIG_SET_PATH:
382ecdab150SDavid Howells 	case FSCONFIG_SET_PATH_EMPTY:
383ecdab150SDavid Howells 		if (!_key || !_value || (aux != AT_FDCWD && aux < 0))
384ecdab150SDavid Howells 			return -EINVAL;
385ecdab150SDavid Howells 		break;
386ecdab150SDavid Howells 	case FSCONFIG_SET_FD:
387ecdab150SDavid Howells 		if (!_key || _value || aux < 0)
388ecdab150SDavid Howells 			return -EINVAL;
389ecdab150SDavid Howells 		break;
390ecdab150SDavid Howells 	case FSCONFIG_CMD_CREATE:
391*22ed7ecdSChristian Brauner 	case FSCONFIG_CMD_CREATE_EXCL:
392ecdab150SDavid Howells 	case FSCONFIG_CMD_RECONFIGURE:
393ecdab150SDavid Howells 		if (_key || _value || aux)
394ecdab150SDavid Howells 			return -EINVAL;
395ecdab150SDavid Howells 		break;
396ecdab150SDavid Howells 	default:
397ecdab150SDavid Howells 		return -EOPNOTSUPP;
398ecdab150SDavid Howells 	}
399ecdab150SDavid Howells 
400ecdab150SDavid Howells 	f = fdget(fd);
401ecdab150SDavid Howells 	if (!f.file)
402ecdab150SDavid Howells 		return -EBADF;
403ecdab150SDavid Howells 	ret = -EINVAL;
404ecdab150SDavid Howells 	if (f.file->f_op != &fscontext_fops)
405ecdab150SDavid Howells 		goto out_f;
406ecdab150SDavid Howells 
407ecdab150SDavid Howells 	fc = f.file->private_data;
408ecdab150SDavid Howells 	if (fc->ops == &legacy_fs_context_ops) {
409ecdab150SDavid Howells 		switch (cmd) {
410ecdab150SDavid Howells 		case FSCONFIG_SET_BINARY:
411ecdab150SDavid Howells 		case FSCONFIG_SET_PATH:
412ecdab150SDavid Howells 		case FSCONFIG_SET_PATH_EMPTY:
413ecdab150SDavid Howells 		case FSCONFIG_SET_FD:
414ecdab150SDavid Howells 			ret = -EOPNOTSUPP;
415ecdab150SDavid Howells 			goto out_f;
416ecdab150SDavid Howells 		}
417ecdab150SDavid Howells 	}
418ecdab150SDavid Howells 
419ecdab150SDavid Howells 	if (_key) {
420ecdab150SDavid Howells 		param.key = strndup_user(_key, 256);
421ecdab150SDavid Howells 		if (IS_ERR(param.key)) {
422ecdab150SDavid Howells 			ret = PTR_ERR(param.key);
423ecdab150SDavid Howells 			goto out_f;
424ecdab150SDavid Howells 		}
425ecdab150SDavid Howells 	}
426ecdab150SDavid Howells 
427ecdab150SDavid Howells 	switch (cmd) {
428ecdab150SDavid Howells 	case FSCONFIG_SET_FLAG:
429ecdab150SDavid Howells 		param.type = fs_value_is_flag;
430ecdab150SDavid Howells 		break;
431ecdab150SDavid Howells 	case FSCONFIG_SET_STRING:
432ecdab150SDavid Howells 		param.type = fs_value_is_string;
433ecdab150SDavid Howells 		param.string = strndup_user(_value, 256);
434ecdab150SDavid Howells 		if (IS_ERR(param.string)) {
435ecdab150SDavid Howells 			ret = PTR_ERR(param.string);
436ecdab150SDavid Howells 			goto out_key;
437ecdab150SDavid Howells 		}
438ecdab150SDavid Howells 		param.size = strlen(param.string);
439ecdab150SDavid Howells 		break;
440ecdab150SDavid Howells 	case FSCONFIG_SET_BINARY:
441ecdab150SDavid Howells 		param.type = fs_value_is_blob;
442ecdab150SDavid Howells 		param.size = aux;
443ecdab150SDavid Howells 		param.blob = memdup_user_nul(_value, aux);
444ecdab150SDavid Howells 		if (IS_ERR(param.blob)) {
445ecdab150SDavid Howells 			ret = PTR_ERR(param.blob);
446ecdab150SDavid Howells 			goto out_key;
447ecdab150SDavid Howells 		}
448ecdab150SDavid Howells 		break;
449aa1918f9SAl Viro 	case FSCONFIG_SET_PATH_EMPTY:
450aa1918f9SAl Viro 		lookup_flags = LOOKUP_EMPTY;
451df561f66SGustavo A. R. Silva 		fallthrough;
452ecdab150SDavid Howells 	case FSCONFIG_SET_PATH:
453ecdab150SDavid Howells 		param.type = fs_value_is_filename;
454aa1918f9SAl Viro 		param.name = getname_flags(_value, lookup_flags, NULL);
455ecdab150SDavid Howells 		if (IS_ERR(param.name)) {
456ecdab150SDavid Howells 			ret = PTR_ERR(param.name);
457ecdab150SDavid Howells 			goto out_key;
458ecdab150SDavid Howells 		}
459ecdab150SDavid Howells 		param.dirfd = aux;
460ecdab150SDavid Howells 		param.size = strlen(param.name->name);
461ecdab150SDavid Howells 		break;
462ecdab150SDavid Howells 	case FSCONFIG_SET_FD:
463ecdab150SDavid Howells 		param.type = fs_value_is_file;
464ecdab150SDavid Howells 		ret = -EBADF;
465ecdab150SDavid Howells 		param.file = fget(aux);
466ecdab150SDavid Howells 		if (!param.file)
467ecdab150SDavid Howells 			goto out_key;
468ecdab150SDavid Howells 		break;
469ecdab150SDavid Howells 	default:
470ecdab150SDavid Howells 		break;
471ecdab150SDavid Howells 	}
472ecdab150SDavid Howells 
473ecdab150SDavid Howells 	ret = mutex_lock_interruptible(&fc->uapi_mutex);
474ecdab150SDavid Howells 	if (ret == 0) {
475ecdab150SDavid Howells 		ret = vfs_fsconfig_locked(fc, cmd, &param);
476ecdab150SDavid Howells 		mutex_unlock(&fc->uapi_mutex);
477ecdab150SDavid Howells 	}
478ecdab150SDavid Howells 
479ecdab150SDavid Howells 	/* Clean up the our record of any value that we obtained from
480ecdab150SDavid Howells 	 * userspace.  Note that the value may have been stolen by the LSM or
481ecdab150SDavid Howells 	 * filesystem, in which case the value pointer will have been cleared.
482ecdab150SDavid Howells 	 */
483ecdab150SDavid Howells 	switch (cmd) {
484ecdab150SDavid Howells 	case FSCONFIG_SET_STRING:
485ecdab150SDavid Howells 	case FSCONFIG_SET_BINARY:
486ecdab150SDavid Howells 		kfree(param.string);
487ecdab150SDavid Howells 		break;
488ecdab150SDavid Howells 	case FSCONFIG_SET_PATH:
489ecdab150SDavid Howells 	case FSCONFIG_SET_PATH_EMPTY:
490ecdab150SDavid Howells 		if (param.name)
491ecdab150SDavid Howells 			putname(param.name);
492ecdab150SDavid Howells 		break;
493ecdab150SDavid Howells 	case FSCONFIG_SET_FD:
494ecdab150SDavid Howells 		if (param.file)
495ecdab150SDavid Howells 			fput(param.file);
496ecdab150SDavid Howells 		break;
497ecdab150SDavid Howells 	default:
498ecdab150SDavid Howells 		break;
499ecdab150SDavid Howells 	}
500ecdab150SDavid Howells out_key:
501ecdab150SDavid Howells 	kfree(param.key);
502ecdab150SDavid Howells out_f:
503ecdab150SDavid Howells 	fdput(f);
504ecdab150SDavid Howells 	return ret;
505ecdab150SDavid Howells }
506