xref: /OK3568_Linux_fs/kernel/fs/fsopen.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /* Filesystem access-by-fd.
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Copyright (C) 2017 Red Hat, Inc. All Rights Reserved.
5*4882a593Smuzhiyun  * Written by David Howells (dhowells@redhat.com)
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun #include <linux/fs_context.h>
9*4882a593Smuzhiyun #include <linux/fs_parser.h>
10*4882a593Smuzhiyun #include <linux/slab.h>
11*4882a593Smuzhiyun #include <linux/uaccess.h>
12*4882a593Smuzhiyun #include <linux/syscalls.h>
13*4882a593Smuzhiyun #include <linux/security.h>
14*4882a593Smuzhiyun #include <linux/anon_inodes.h>
15*4882a593Smuzhiyun #include <linux/namei.h>
16*4882a593Smuzhiyun #include <linux/file.h>
17*4882a593Smuzhiyun #include <uapi/linux/mount.h>
18*4882a593Smuzhiyun #include "internal.h"
19*4882a593Smuzhiyun #include "mount.h"
20*4882a593Smuzhiyun 
21*4882a593Smuzhiyun /*
22*4882a593Smuzhiyun  * Allow the user to read back any error, warning or informational messages.
23*4882a593Smuzhiyun  */
fscontext_read(struct file * file,char __user * _buf,size_t len,loff_t * pos)24*4882a593Smuzhiyun static ssize_t fscontext_read(struct file *file,
25*4882a593Smuzhiyun 			      char __user *_buf, size_t len, loff_t *pos)
26*4882a593Smuzhiyun {
27*4882a593Smuzhiyun 	struct fs_context *fc = file->private_data;
28*4882a593Smuzhiyun 	struct fc_log *log = fc->log.log;
29*4882a593Smuzhiyun 	unsigned int logsize = ARRAY_SIZE(log->buffer);
30*4882a593Smuzhiyun 	ssize_t ret;
31*4882a593Smuzhiyun 	char *p;
32*4882a593Smuzhiyun 	bool need_free;
33*4882a593Smuzhiyun 	int index, n;
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun 	ret = mutex_lock_interruptible(&fc->uapi_mutex);
36*4882a593Smuzhiyun 	if (ret < 0)
37*4882a593Smuzhiyun 		return ret;
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun 	if (log->head == log->tail) {
40*4882a593Smuzhiyun 		mutex_unlock(&fc->uapi_mutex);
41*4882a593Smuzhiyun 		return -ENODATA;
42*4882a593Smuzhiyun 	}
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun 	index = log->tail & (logsize - 1);
45*4882a593Smuzhiyun 	p = log->buffer[index];
46*4882a593Smuzhiyun 	need_free = log->need_free & (1 << index);
47*4882a593Smuzhiyun 	log->buffer[index] = NULL;
48*4882a593Smuzhiyun 	log->need_free &= ~(1 << index);
49*4882a593Smuzhiyun 	log->tail++;
50*4882a593Smuzhiyun 	mutex_unlock(&fc->uapi_mutex);
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun 	ret = -EMSGSIZE;
53*4882a593Smuzhiyun 	n = strlen(p);
54*4882a593Smuzhiyun 	if (n > len)
55*4882a593Smuzhiyun 		goto err_free;
56*4882a593Smuzhiyun 	ret = -EFAULT;
57*4882a593Smuzhiyun 	if (copy_to_user(_buf, p, n) != 0)
58*4882a593Smuzhiyun 		goto err_free;
59*4882a593Smuzhiyun 	ret = n;
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun err_free:
62*4882a593Smuzhiyun 	if (need_free)
63*4882a593Smuzhiyun 		kfree(p);
64*4882a593Smuzhiyun 	return ret;
65*4882a593Smuzhiyun }
66*4882a593Smuzhiyun 
fscontext_release(struct inode * inode,struct file * file)67*4882a593Smuzhiyun static int fscontext_release(struct inode *inode, struct file *file)
68*4882a593Smuzhiyun {
69*4882a593Smuzhiyun 	struct fs_context *fc = file->private_data;
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun 	if (fc) {
72*4882a593Smuzhiyun 		file->private_data = NULL;
73*4882a593Smuzhiyun 		put_fs_context(fc);
74*4882a593Smuzhiyun 	}
75*4882a593Smuzhiyun 	return 0;
76*4882a593Smuzhiyun }
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun const struct file_operations fscontext_fops = {
79*4882a593Smuzhiyun 	.read		= fscontext_read,
80*4882a593Smuzhiyun 	.release	= fscontext_release,
81*4882a593Smuzhiyun 	.llseek		= no_llseek,
82*4882a593Smuzhiyun };
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun /*
85*4882a593Smuzhiyun  * Attach a filesystem context to a file and an fd.
86*4882a593Smuzhiyun  */
fscontext_create_fd(struct fs_context * fc,unsigned int o_flags)87*4882a593Smuzhiyun static int fscontext_create_fd(struct fs_context *fc, unsigned int o_flags)
88*4882a593Smuzhiyun {
89*4882a593Smuzhiyun 	int fd;
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun 	fd = anon_inode_getfd("[fscontext]", &fscontext_fops, fc,
92*4882a593Smuzhiyun 			      O_RDWR | o_flags);
93*4882a593Smuzhiyun 	if (fd < 0)
94*4882a593Smuzhiyun 		put_fs_context(fc);
95*4882a593Smuzhiyun 	return fd;
96*4882a593Smuzhiyun }
97*4882a593Smuzhiyun 
fscontext_alloc_log(struct fs_context * fc)98*4882a593Smuzhiyun static int fscontext_alloc_log(struct fs_context *fc)
99*4882a593Smuzhiyun {
100*4882a593Smuzhiyun 	fc->log.log = kzalloc(sizeof(*fc->log.log), GFP_KERNEL);
101*4882a593Smuzhiyun 	if (!fc->log.log)
102*4882a593Smuzhiyun 		return -ENOMEM;
103*4882a593Smuzhiyun 	refcount_set(&fc->log.log->usage, 1);
104*4882a593Smuzhiyun 	fc->log.log->owner = fc->fs_type->owner;
105*4882a593Smuzhiyun 	return 0;
106*4882a593Smuzhiyun }
107*4882a593Smuzhiyun 
108*4882a593Smuzhiyun /*
109*4882a593Smuzhiyun  * Open a filesystem by name so that it can be configured for mounting.
110*4882a593Smuzhiyun  *
111*4882a593Smuzhiyun  * We are allowed to specify a container in which the filesystem will be
112*4882a593Smuzhiyun  * opened, thereby indicating which namespaces will be used (notably, which
113*4882a593Smuzhiyun  * network namespace will be used for network filesystems).
114*4882a593Smuzhiyun  */
SYSCALL_DEFINE2(fsopen,const char __user *,_fs_name,unsigned int,flags)115*4882a593Smuzhiyun SYSCALL_DEFINE2(fsopen, const char __user *, _fs_name, unsigned int, flags)
116*4882a593Smuzhiyun {
117*4882a593Smuzhiyun 	struct file_system_type *fs_type;
118*4882a593Smuzhiyun 	struct fs_context *fc;
119*4882a593Smuzhiyun 	const char *fs_name;
120*4882a593Smuzhiyun 	int ret;
121*4882a593Smuzhiyun 
122*4882a593Smuzhiyun 	if (!ns_capable(current->nsproxy->mnt_ns->user_ns, CAP_SYS_ADMIN))
123*4882a593Smuzhiyun 		return -EPERM;
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun 	if (flags & ~FSOPEN_CLOEXEC)
126*4882a593Smuzhiyun 		return -EINVAL;
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun 	fs_name = strndup_user(_fs_name, PAGE_SIZE);
129*4882a593Smuzhiyun 	if (IS_ERR(fs_name))
130*4882a593Smuzhiyun 		return PTR_ERR(fs_name);
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun 	fs_type = get_fs_type(fs_name);
133*4882a593Smuzhiyun 	kfree(fs_name);
134*4882a593Smuzhiyun 	if (!fs_type)
135*4882a593Smuzhiyun 		return -ENODEV;
136*4882a593Smuzhiyun 
137*4882a593Smuzhiyun 	fc = fs_context_for_mount(fs_type, 0);
138*4882a593Smuzhiyun 	put_filesystem(fs_type);
139*4882a593Smuzhiyun 	if (IS_ERR(fc))
140*4882a593Smuzhiyun 		return PTR_ERR(fc);
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun 	fc->phase = FS_CONTEXT_CREATE_PARAMS;
143*4882a593Smuzhiyun 
144*4882a593Smuzhiyun 	ret = fscontext_alloc_log(fc);
145*4882a593Smuzhiyun 	if (ret < 0)
146*4882a593Smuzhiyun 		goto err_fc;
147*4882a593Smuzhiyun 
148*4882a593Smuzhiyun 	return fscontext_create_fd(fc, flags & FSOPEN_CLOEXEC ? O_CLOEXEC : 0);
149*4882a593Smuzhiyun 
150*4882a593Smuzhiyun err_fc:
151*4882a593Smuzhiyun 	put_fs_context(fc);
152*4882a593Smuzhiyun 	return ret;
153*4882a593Smuzhiyun }
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun /*
156*4882a593Smuzhiyun  * Pick a superblock into a context for reconfiguration.
157*4882a593Smuzhiyun  */
SYSCALL_DEFINE3(fspick,int,dfd,const char __user *,path,unsigned int,flags)158*4882a593Smuzhiyun SYSCALL_DEFINE3(fspick, int, dfd, const char __user *, path, unsigned int, flags)
159*4882a593Smuzhiyun {
160*4882a593Smuzhiyun 	struct fs_context *fc;
161*4882a593Smuzhiyun 	struct path target;
162*4882a593Smuzhiyun 	unsigned int lookup_flags;
163*4882a593Smuzhiyun 	int ret;
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun 	if (!ns_capable(current->nsproxy->mnt_ns->user_ns, CAP_SYS_ADMIN))
166*4882a593Smuzhiyun 		return -EPERM;
167*4882a593Smuzhiyun 
168*4882a593Smuzhiyun 	if ((flags & ~(FSPICK_CLOEXEC |
169*4882a593Smuzhiyun 		       FSPICK_SYMLINK_NOFOLLOW |
170*4882a593Smuzhiyun 		       FSPICK_NO_AUTOMOUNT |
171*4882a593Smuzhiyun 		       FSPICK_EMPTY_PATH)) != 0)
172*4882a593Smuzhiyun 		return -EINVAL;
173*4882a593Smuzhiyun 
174*4882a593Smuzhiyun 	lookup_flags = LOOKUP_FOLLOW | LOOKUP_AUTOMOUNT;
175*4882a593Smuzhiyun 	if (flags & FSPICK_SYMLINK_NOFOLLOW)
176*4882a593Smuzhiyun 		lookup_flags &= ~LOOKUP_FOLLOW;
177*4882a593Smuzhiyun 	if (flags & FSPICK_NO_AUTOMOUNT)
178*4882a593Smuzhiyun 		lookup_flags &= ~LOOKUP_AUTOMOUNT;
179*4882a593Smuzhiyun 	if (flags & FSPICK_EMPTY_PATH)
180*4882a593Smuzhiyun 		lookup_flags |= LOOKUP_EMPTY;
181*4882a593Smuzhiyun 	ret = user_path_at(dfd, path, lookup_flags, &target);
182*4882a593Smuzhiyun 	if (ret < 0)
183*4882a593Smuzhiyun 		goto err;
184*4882a593Smuzhiyun 
185*4882a593Smuzhiyun 	ret = -EINVAL;
186*4882a593Smuzhiyun 	if (target.mnt->mnt_root != target.dentry)
187*4882a593Smuzhiyun 		goto err_path;
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun 	fc = fs_context_for_reconfigure(target.dentry, 0, 0);
190*4882a593Smuzhiyun 	if (IS_ERR(fc)) {
191*4882a593Smuzhiyun 		ret = PTR_ERR(fc);
192*4882a593Smuzhiyun 		goto err_path;
193*4882a593Smuzhiyun 	}
194*4882a593Smuzhiyun 
195*4882a593Smuzhiyun 	fc->phase = FS_CONTEXT_RECONF_PARAMS;
196*4882a593Smuzhiyun 
197*4882a593Smuzhiyun 	ret = fscontext_alloc_log(fc);
198*4882a593Smuzhiyun 	if (ret < 0)
199*4882a593Smuzhiyun 		goto err_fc;
200*4882a593Smuzhiyun 
201*4882a593Smuzhiyun 	path_put(&target);
202*4882a593Smuzhiyun 	return fscontext_create_fd(fc, flags & FSPICK_CLOEXEC ? O_CLOEXEC : 0);
203*4882a593Smuzhiyun 
204*4882a593Smuzhiyun err_fc:
205*4882a593Smuzhiyun 	put_fs_context(fc);
206*4882a593Smuzhiyun err_path:
207*4882a593Smuzhiyun 	path_put(&target);
208*4882a593Smuzhiyun err:
209*4882a593Smuzhiyun 	return ret;
210*4882a593Smuzhiyun }
211*4882a593Smuzhiyun 
212*4882a593Smuzhiyun /*
213*4882a593Smuzhiyun  * Check the state and apply the configuration.  Note that this function is
214*4882a593Smuzhiyun  * allowed to 'steal' the value by setting param->xxx to NULL before returning.
215*4882a593Smuzhiyun  */
vfs_fsconfig_locked(struct fs_context * fc,int cmd,struct fs_parameter * param)216*4882a593Smuzhiyun static int vfs_fsconfig_locked(struct fs_context *fc, int cmd,
217*4882a593Smuzhiyun 			       struct fs_parameter *param)
218*4882a593Smuzhiyun {
219*4882a593Smuzhiyun 	struct super_block *sb;
220*4882a593Smuzhiyun 	int ret;
221*4882a593Smuzhiyun 
222*4882a593Smuzhiyun 	ret = finish_clean_context(fc);
223*4882a593Smuzhiyun 	if (ret)
224*4882a593Smuzhiyun 		return ret;
225*4882a593Smuzhiyun 	switch (cmd) {
226*4882a593Smuzhiyun 	case FSCONFIG_CMD_CREATE:
227*4882a593Smuzhiyun 		if (fc->phase != FS_CONTEXT_CREATE_PARAMS)
228*4882a593Smuzhiyun 			return -EBUSY;
229*4882a593Smuzhiyun 		if (!mount_capable(fc))
230*4882a593Smuzhiyun 			return -EPERM;
231*4882a593Smuzhiyun 		fc->phase = FS_CONTEXT_CREATING;
232*4882a593Smuzhiyun 		ret = vfs_get_tree(fc);
233*4882a593Smuzhiyun 		if (ret)
234*4882a593Smuzhiyun 			break;
235*4882a593Smuzhiyun 		sb = fc->root->d_sb;
236*4882a593Smuzhiyun 		ret = security_sb_kern_mount(sb);
237*4882a593Smuzhiyun 		if (unlikely(ret)) {
238*4882a593Smuzhiyun 			fc_drop_locked(fc);
239*4882a593Smuzhiyun 			break;
240*4882a593Smuzhiyun 		}
241*4882a593Smuzhiyun 		up_write(&sb->s_umount);
242*4882a593Smuzhiyun 		fc->phase = FS_CONTEXT_AWAITING_MOUNT;
243*4882a593Smuzhiyun 		return 0;
244*4882a593Smuzhiyun 	case FSCONFIG_CMD_RECONFIGURE:
245*4882a593Smuzhiyun 		if (fc->phase != FS_CONTEXT_RECONF_PARAMS)
246*4882a593Smuzhiyun 			return -EBUSY;
247*4882a593Smuzhiyun 		fc->phase = FS_CONTEXT_RECONFIGURING;
248*4882a593Smuzhiyun 		sb = fc->root->d_sb;
249*4882a593Smuzhiyun 		if (!ns_capable(sb->s_user_ns, CAP_SYS_ADMIN)) {
250*4882a593Smuzhiyun 			ret = -EPERM;
251*4882a593Smuzhiyun 			break;
252*4882a593Smuzhiyun 		}
253*4882a593Smuzhiyun 		down_write(&sb->s_umount);
254*4882a593Smuzhiyun 		ret = reconfigure_super(fc);
255*4882a593Smuzhiyun 		up_write(&sb->s_umount);
256*4882a593Smuzhiyun 		if (ret)
257*4882a593Smuzhiyun 			break;
258*4882a593Smuzhiyun 		vfs_clean_context(fc);
259*4882a593Smuzhiyun 		return 0;
260*4882a593Smuzhiyun 	default:
261*4882a593Smuzhiyun 		if (fc->phase != FS_CONTEXT_CREATE_PARAMS &&
262*4882a593Smuzhiyun 		    fc->phase != FS_CONTEXT_RECONF_PARAMS)
263*4882a593Smuzhiyun 			return -EBUSY;
264*4882a593Smuzhiyun 
265*4882a593Smuzhiyun 		return vfs_parse_fs_param(fc, param);
266*4882a593Smuzhiyun 	}
267*4882a593Smuzhiyun 	fc->phase = FS_CONTEXT_FAILED;
268*4882a593Smuzhiyun 	return ret;
269*4882a593Smuzhiyun }
270*4882a593Smuzhiyun 
271*4882a593Smuzhiyun /**
272*4882a593Smuzhiyun  * sys_fsconfig - Set parameters and trigger actions on a context
273*4882a593Smuzhiyun  * @fd: The filesystem context to act upon
274*4882a593Smuzhiyun  * @cmd: The action to take
275*4882a593Smuzhiyun  * @_key: Where appropriate, the parameter key to set
276*4882a593Smuzhiyun  * @_value: Where appropriate, the parameter value to set
277*4882a593Smuzhiyun  * @aux: Additional information for the value
278*4882a593Smuzhiyun  *
279*4882a593Smuzhiyun  * This system call is used to set parameters on a context, including
280*4882a593Smuzhiyun  * superblock settings, data source and security labelling.
281*4882a593Smuzhiyun  *
282*4882a593Smuzhiyun  * Actions include triggering the creation of a superblock and the
283*4882a593Smuzhiyun  * reconfiguration of the superblock attached to the specified context.
284*4882a593Smuzhiyun  *
285*4882a593Smuzhiyun  * When setting a parameter, @cmd indicates the type of value being proposed
286*4882a593Smuzhiyun  * and @_key indicates the parameter to be altered.
287*4882a593Smuzhiyun  *
288*4882a593Smuzhiyun  * @_value and @aux are used to specify the value, should a value be required:
289*4882a593Smuzhiyun  *
290*4882a593Smuzhiyun  * (*) fsconfig_set_flag: No value is specified.  The parameter must be boolean
291*4882a593Smuzhiyun  *     in nature.  The key may be prefixed with "no" to invert the
292*4882a593Smuzhiyun  *     setting. @_value must be NULL and @aux must be 0.
293*4882a593Smuzhiyun  *
294*4882a593Smuzhiyun  * (*) fsconfig_set_string: A string value is specified.  The parameter can be
295*4882a593Smuzhiyun  *     expecting boolean, integer, string or take a path.  A conversion to an
296*4882a593Smuzhiyun  *     appropriate type will be attempted (which may include looking up as a
297*4882a593Smuzhiyun  *     path).  @_value points to a NUL-terminated string and @aux must be 0.
298*4882a593Smuzhiyun  *
299*4882a593Smuzhiyun  * (*) fsconfig_set_binary: A binary blob is specified.  @_value points to the
300*4882a593Smuzhiyun  *     blob and @aux indicates its size.  The parameter must be expecting a
301*4882a593Smuzhiyun  *     blob.
302*4882a593Smuzhiyun  *
303*4882a593Smuzhiyun  * (*) fsconfig_set_path: A non-empty path is specified.  The parameter must be
304*4882a593Smuzhiyun  *     expecting a path object.  @_value points to a NUL-terminated string that
305*4882a593Smuzhiyun  *     is the path and @aux is a file descriptor at which to start a relative
306*4882a593Smuzhiyun  *     lookup or AT_FDCWD.
307*4882a593Smuzhiyun  *
308*4882a593Smuzhiyun  * (*) fsconfig_set_path_empty: As fsconfig_set_path, but with AT_EMPTY_PATH
309*4882a593Smuzhiyun  *     implied.
310*4882a593Smuzhiyun  *
311*4882a593Smuzhiyun  * (*) fsconfig_set_fd: An open file descriptor is specified.  @_value must be
312*4882a593Smuzhiyun  *     NULL and @aux indicates the file descriptor.
313*4882a593Smuzhiyun  */
SYSCALL_DEFINE5(fsconfig,int,fd,unsigned int,cmd,const char __user *,_key,const void __user *,_value,int,aux)314*4882a593Smuzhiyun SYSCALL_DEFINE5(fsconfig,
315*4882a593Smuzhiyun 		int, fd,
316*4882a593Smuzhiyun 		unsigned int, cmd,
317*4882a593Smuzhiyun 		const char __user *, _key,
318*4882a593Smuzhiyun 		const void __user *, _value,
319*4882a593Smuzhiyun 		int, aux)
320*4882a593Smuzhiyun {
321*4882a593Smuzhiyun 	struct fs_context *fc;
322*4882a593Smuzhiyun 	struct fd f;
323*4882a593Smuzhiyun 	int ret;
324*4882a593Smuzhiyun 	int lookup_flags = 0;
325*4882a593Smuzhiyun 
326*4882a593Smuzhiyun 	struct fs_parameter param = {
327*4882a593Smuzhiyun 		.type	= fs_value_is_undefined,
328*4882a593Smuzhiyun 	};
329*4882a593Smuzhiyun 
330*4882a593Smuzhiyun 	if (fd < 0)
331*4882a593Smuzhiyun 		return -EINVAL;
332*4882a593Smuzhiyun 
333*4882a593Smuzhiyun 	switch (cmd) {
334*4882a593Smuzhiyun 	case FSCONFIG_SET_FLAG:
335*4882a593Smuzhiyun 		if (!_key || _value || aux)
336*4882a593Smuzhiyun 			return -EINVAL;
337*4882a593Smuzhiyun 		break;
338*4882a593Smuzhiyun 	case FSCONFIG_SET_STRING:
339*4882a593Smuzhiyun 		if (!_key || !_value || aux)
340*4882a593Smuzhiyun 			return -EINVAL;
341*4882a593Smuzhiyun 		break;
342*4882a593Smuzhiyun 	case FSCONFIG_SET_BINARY:
343*4882a593Smuzhiyun 		if (!_key || !_value || aux <= 0 || aux > 1024 * 1024)
344*4882a593Smuzhiyun 			return -EINVAL;
345*4882a593Smuzhiyun 		break;
346*4882a593Smuzhiyun 	case FSCONFIG_SET_PATH:
347*4882a593Smuzhiyun 	case FSCONFIG_SET_PATH_EMPTY:
348*4882a593Smuzhiyun 		if (!_key || !_value || (aux != AT_FDCWD && aux < 0))
349*4882a593Smuzhiyun 			return -EINVAL;
350*4882a593Smuzhiyun 		break;
351*4882a593Smuzhiyun 	case FSCONFIG_SET_FD:
352*4882a593Smuzhiyun 		if (!_key || _value || aux < 0)
353*4882a593Smuzhiyun 			return -EINVAL;
354*4882a593Smuzhiyun 		break;
355*4882a593Smuzhiyun 	case FSCONFIG_CMD_CREATE:
356*4882a593Smuzhiyun 	case FSCONFIG_CMD_RECONFIGURE:
357*4882a593Smuzhiyun 		if (_key || _value || aux)
358*4882a593Smuzhiyun 			return -EINVAL;
359*4882a593Smuzhiyun 		break;
360*4882a593Smuzhiyun 	default:
361*4882a593Smuzhiyun 		return -EOPNOTSUPP;
362*4882a593Smuzhiyun 	}
363*4882a593Smuzhiyun 
364*4882a593Smuzhiyun 	f = fdget(fd);
365*4882a593Smuzhiyun 	if (!f.file)
366*4882a593Smuzhiyun 		return -EBADF;
367*4882a593Smuzhiyun 	ret = -EINVAL;
368*4882a593Smuzhiyun 	if (f.file->f_op != &fscontext_fops)
369*4882a593Smuzhiyun 		goto out_f;
370*4882a593Smuzhiyun 
371*4882a593Smuzhiyun 	fc = f.file->private_data;
372*4882a593Smuzhiyun 	if (fc->ops == &legacy_fs_context_ops) {
373*4882a593Smuzhiyun 		switch (cmd) {
374*4882a593Smuzhiyun 		case FSCONFIG_SET_BINARY:
375*4882a593Smuzhiyun 		case FSCONFIG_SET_PATH:
376*4882a593Smuzhiyun 		case FSCONFIG_SET_PATH_EMPTY:
377*4882a593Smuzhiyun 		case FSCONFIG_SET_FD:
378*4882a593Smuzhiyun 			ret = -EOPNOTSUPP;
379*4882a593Smuzhiyun 			goto out_f;
380*4882a593Smuzhiyun 		}
381*4882a593Smuzhiyun 	}
382*4882a593Smuzhiyun 
383*4882a593Smuzhiyun 	if (_key) {
384*4882a593Smuzhiyun 		param.key = strndup_user(_key, 256);
385*4882a593Smuzhiyun 		if (IS_ERR(param.key)) {
386*4882a593Smuzhiyun 			ret = PTR_ERR(param.key);
387*4882a593Smuzhiyun 			goto out_f;
388*4882a593Smuzhiyun 		}
389*4882a593Smuzhiyun 	}
390*4882a593Smuzhiyun 
391*4882a593Smuzhiyun 	switch (cmd) {
392*4882a593Smuzhiyun 	case FSCONFIG_SET_FLAG:
393*4882a593Smuzhiyun 		param.type = fs_value_is_flag;
394*4882a593Smuzhiyun 		break;
395*4882a593Smuzhiyun 	case FSCONFIG_SET_STRING:
396*4882a593Smuzhiyun 		param.type = fs_value_is_string;
397*4882a593Smuzhiyun 		param.string = strndup_user(_value, 256);
398*4882a593Smuzhiyun 		if (IS_ERR(param.string)) {
399*4882a593Smuzhiyun 			ret = PTR_ERR(param.string);
400*4882a593Smuzhiyun 			goto out_key;
401*4882a593Smuzhiyun 		}
402*4882a593Smuzhiyun 		param.size = strlen(param.string);
403*4882a593Smuzhiyun 		break;
404*4882a593Smuzhiyun 	case FSCONFIG_SET_BINARY:
405*4882a593Smuzhiyun 		param.type = fs_value_is_blob;
406*4882a593Smuzhiyun 		param.size = aux;
407*4882a593Smuzhiyun 		param.blob = memdup_user_nul(_value, aux);
408*4882a593Smuzhiyun 		if (IS_ERR(param.blob)) {
409*4882a593Smuzhiyun 			ret = PTR_ERR(param.blob);
410*4882a593Smuzhiyun 			goto out_key;
411*4882a593Smuzhiyun 		}
412*4882a593Smuzhiyun 		break;
413*4882a593Smuzhiyun 	case FSCONFIG_SET_PATH_EMPTY:
414*4882a593Smuzhiyun 		lookup_flags = LOOKUP_EMPTY;
415*4882a593Smuzhiyun 		fallthrough;
416*4882a593Smuzhiyun 	case FSCONFIG_SET_PATH:
417*4882a593Smuzhiyun 		param.type = fs_value_is_filename;
418*4882a593Smuzhiyun 		param.name = getname_flags(_value, lookup_flags, NULL);
419*4882a593Smuzhiyun 		if (IS_ERR(param.name)) {
420*4882a593Smuzhiyun 			ret = PTR_ERR(param.name);
421*4882a593Smuzhiyun 			goto out_key;
422*4882a593Smuzhiyun 		}
423*4882a593Smuzhiyun 		param.dirfd = aux;
424*4882a593Smuzhiyun 		param.size = strlen(param.name->name);
425*4882a593Smuzhiyun 		break;
426*4882a593Smuzhiyun 	case FSCONFIG_SET_FD:
427*4882a593Smuzhiyun 		param.type = fs_value_is_file;
428*4882a593Smuzhiyun 		ret = -EBADF;
429*4882a593Smuzhiyun 		param.file = fget(aux);
430*4882a593Smuzhiyun 		if (!param.file)
431*4882a593Smuzhiyun 			goto out_key;
432*4882a593Smuzhiyun 		break;
433*4882a593Smuzhiyun 	default:
434*4882a593Smuzhiyun 		break;
435*4882a593Smuzhiyun 	}
436*4882a593Smuzhiyun 
437*4882a593Smuzhiyun 	ret = mutex_lock_interruptible(&fc->uapi_mutex);
438*4882a593Smuzhiyun 	if (ret == 0) {
439*4882a593Smuzhiyun 		ret = vfs_fsconfig_locked(fc, cmd, &param);
440*4882a593Smuzhiyun 		mutex_unlock(&fc->uapi_mutex);
441*4882a593Smuzhiyun 	}
442*4882a593Smuzhiyun 
443*4882a593Smuzhiyun 	/* Clean up the our record of any value that we obtained from
444*4882a593Smuzhiyun 	 * userspace.  Note that the value may have been stolen by the LSM or
445*4882a593Smuzhiyun 	 * filesystem, in which case the value pointer will have been cleared.
446*4882a593Smuzhiyun 	 */
447*4882a593Smuzhiyun 	switch (cmd) {
448*4882a593Smuzhiyun 	case FSCONFIG_SET_STRING:
449*4882a593Smuzhiyun 	case FSCONFIG_SET_BINARY:
450*4882a593Smuzhiyun 		kfree(param.string);
451*4882a593Smuzhiyun 		break;
452*4882a593Smuzhiyun 	case FSCONFIG_SET_PATH:
453*4882a593Smuzhiyun 	case FSCONFIG_SET_PATH_EMPTY:
454*4882a593Smuzhiyun 		if (param.name)
455*4882a593Smuzhiyun 			putname(param.name);
456*4882a593Smuzhiyun 		break;
457*4882a593Smuzhiyun 	case FSCONFIG_SET_FD:
458*4882a593Smuzhiyun 		if (param.file)
459*4882a593Smuzhiyun 			fput(param.file);
460*4882a593Smuzhiyun 		break;
461*4882a593Smuzhiyun 	default:
462*4882a593Smuzhiyun 		break;
463*4882a593Smuzhiyun 	}
464*4882a593Smuzhiyun out_key:
465*4882a593Smuzhiyun 	kfree(param.key);
466*4882a593Smuzhiyun out_f:
467*4882a593Smuzhiyun 	fdput(f);
468*4882a593Smuzhiyun 	return ret;
469*4882a593Smuzhiyun }
470