xref: /openbmc/linux/fs/fuse/ioctl.c (revision 0da908c2)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2017 Red Hat, Inc.
4  */
5 
6 #include "fuse_i.h"
7 
8 #include <linux/uio.h>
9 #include <linux/compat.h>
10 #include <linux/fileattr.h>
11 
12 static ssize_t fuse_send_ioctl(struct fuse_mount *fm, struct fuse_args *args)
13 {
14 	ssize_t ret = fuse_simple_request(fm, args);
15 
16 	/* Translate ENOSYS, which shouldn't be returned from fs */
17 	if (ret == -ENOSYS)
18 		ret = -ENOTTY;
19 
20 	return ret;
21 }
22 
23 /*
24  * CUSE servers compiled on 32bit broke on 64bit kernels because the
25  * ABI was defined to be 'struct iovec' which is different on 32bit
26  * and 64bit.  Fortunately we can determine which structure the server
27  * used from the size of the reply.
28  */
29 static int fuse_copy_ioctl_iovec_old(struct iovec *dst, void *src,
30 				     size_t transferred, unsigned count,
31 				     bool is_compat)
32 {
33 #ifdef CONFIG_COMPAT
34 	if (count * sizeof(struct compat_iovec) == transferred) {
35 		struct compat_iovec *ciov = src;
36 		unsigned i;
37 
38 		/*
39 		 * With this interface a 32bit server cannot support
40 		 * non-compat (i.e. ones coming from 64bit apps) ioctl
41 		 * requests
42 		 */
43 		if (!is_compat)
44 			return -EINVAL;
45 
46 		for (i = 0; i < count; i++) {
47 			dst[i].iov_base = compat_ptr(ciov[i].iov_base);
48 			dst[i].iov_len = ciov[i].iov_len;
49 		}
50 		return 0;
51 	}
52 #endif
53 
54 	if (count * sizeof(struct iovec) != transferred)
55 		return -EIO;
56 
57 	memcpy(dst, src, transferred);
58 	return 0;
59 }
60 
61 /* Make sure iov_length() won't overflow */
62 static int fuse_verify_ioctl_iov(struct fuse_conn *fc, struct iovec *iov,
63 				 size_t count)
64 {
65 	size_t n;
66 	u32 max = fc->max_pages << PAGE_SHIFT;
67 
68 	for (n = 0; n < count; n++, iov++) {
69 		if (iov->iov_len > (size_t) max)
70 			return -ENOMEM;
71 		max -= iov->iov_len;
72 	}
73 	return 0;
74 }
75 
76 static int fuse_copy_ioctl_iovec(struct fuse_conn *fc, struct iovec *dst,
77 				 void *src, size_t transferred, unsigned count,
78 				 bool is_compat)
79 {
80 	unsigned i;
81 	struct fuse_ioctl_iovec *fiov = src;
82 
83 	if (fc->minor < 16) {
84 		return fuse_copy_ioctl_iovec_old(dst, src, transferred,
85 						 count, is_compat);
86 	}
87 
88 	if (count * sizeof(struct fuse_ioctl_iovec) != transferred)
89 		return -EIO;
90 
91 	for (i = 0; i < count; i++) {
92 		/* Did the server supply an inappropriate value? */
93 		if (fiov[i].base != (unsigned long) fiov[i].base ||
94 		    fiov[i].len != (unsigned long) fiov[i].len)
95 			return -EIO;
96 
97 		dst[i].iov_base = (void __user *) (unsigned long) fiov[i].base;
98 		dst[i].iov_len = (size_t) fiov[i].len;
99 
100 #ifdef CONFIG_COMPAT
101 		if (is_compat &&
102 		    (ptr_to_compat(dst[i].iov_base) != fiov[i].base ||
103 		     (compat_size_t) dst[i].iov_len != fiov[i].len))
104 			return -EIO;
105 #endif
106 	}
107 
108 	return 0;
109 }
110 
111 
112 /*
113  * For ioctls, there is no generic way to determine how much memory
114  * needs to be read and/or written.  Furthermore, ioctls are allowed
115  * to dereference the passed pointer, so the parameter requires deep
116  * copying but FUSE has no idea whatsoever about what to copy in or
117  * out.
118  *
119  * This is solved by allowing FUSE server to retry ioctl with
120  * necessary in/out iovecs.  Let's assume the ioctl implementation
121  * needs to read in the following structure.
122  *
123  * struct a {
124  *	char	*buf;
125  *	size_t	buflen;
126  * }
127  *
128  * On the first callout to FUSE server, inarg->in_size and
129  * inarg->out_size will be NULL; then, the server completes the ioctl
130  * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
131  * the actual iov array to
132  *
133  * { { .iov_base = inarg.arg,	.iov_len = sizeof(struct a) } }
134  *
135  * which tells FUSE to copy in the requested area and retry the ioctl.
136  * On the second round, the server has access to the structure and
137  * from that it can tell what to look for next, so on the invocation,
138  * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
139  *
140  * { { .iov_base = inarg.arg,	.iov_len = sizeof(struct a)	},
141  *   { .iov_base = a.buf,	.iov_len = a.buflen		} }
142  *
143  * FUSE will copy both struct a and the pointed buffer from the
144  * process doing the ioctl and retry ioctl with both struct a and the
145  * buffer.
146  *
147  * This time, FUSE server has everything it needs and completes ioctl
148  * without FUSE_IOCTL_RETRY which finishes the ioctl call.
149  *
150  * Copying data out works the same way.
151  *
152  * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
153  * automatically initializes in and out iovs by decoding @cmd with
154  * _IOC_* macros and the server is not allowed to request RETRY.  This
155  * limits ioctl data transfers to well-formed ioctls and is the forced
156  * behavior for all FUSE servers.
157  */
158 long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
159 		   unsigned int flags)
160 {
161 	struct fuse_file *ff = file->private_data;
162 	struct fuse_mount *fm = ff->fm;
163 	struct fuse_ioctl_in inarg = {
164 		.fh = ff->fh,
165 		.cmd = cmd,
166 		.arg = arg,
167 		.flags = flags
168 	};
169 	struct fuse_ioctl_out outarg;
170 	struct iovec *iov_page = NULL;
171 	struct iovec *in_iov = NULL, *out_iov = NULL;
172 	unsigned int in_iovs = 0, out_iovs = 0, max_pages;
173 	size_t in_size, out_size, c;
174 	ssize_t transferred;
175 	int err, i;
176 	struct iov_iter ii;
177 	struct fuse_args_pages ap = {};
178 
179 #if BITS_PER_LONG == 32
180 	inarg.flags |= FUSE_IOCTL_32BIT;
181 #else
182 	if (flags & FUSE_IOCTL_COMPAT) {
183 		inarg.flags |= FUSE_IOCTL_32BIT;
184 #ifdef CONFIG_X86_X32_ABI
185 		if (in_x32_syscall())
186 			inarg.flags |= FUSE_IOCTL_COMPAT_X32;
187 #endif
188 	}
189 #endif
190 
191 	/* assume all the iovs returned by client always fits in a page */
192 	BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE);
193 
194 	err = -ENOMEM;
195 	ap.pages = fuse_pages_alloc(fm->fc->max_pages, GFP_KERNEL, &ap.descs);
196 	iov_page = (struct iovec *) __get_free_page(GFP_KERNEL);
197 	if (!ap.pages || !iov_page)
198 		goto out;
199 
200 	fuse_page_descs_length_init(ap.descs, 0, fm->fc->max_pages);
201 
202 	/*
203 	 * If restricted, initialize IO parameters as encoded in @cmd.
204 	 * RETRY from server is not allowed.
205 	 */
206 	if (!(flags & FUSE_IOCTL_UNRESTRICTED)) {
207 		struct iovec *iov = iov_page;
208 
209 		iov->iov_base = (void __user *)arg;
210 		iov->iov_len = _IOC_SIZE(cmd);
211 
212 		if (_IOC_DIR(cmd) & _IOC_WRITE) {
213 			in_iov = iov;
214 			in_iovs = 1;
215 		}
216 
217 		if (_IOC_DIR(cmd) & _IOC_READ) {
218 			out_iov = iov;
219 			out_iovs = 1;
220 		}
221 	}
222 
223  retry:
224 	inarg.in_size = in_size = iov_length(in_iov, in_iovs);
225 	inarg.out_size = out_size = iov_length(out_iov, out_iovs);
226 
227 	/*
228 	 * Out data can be used either for actual out data or iovs,
229 	 * make sure there always is at least one page.
230 	 */
231 	out_size = max_t(size_t, out_size, PAGE_SIZE);
232 	max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE);
233 
234 	/* make sure there are enough buffer pages and init request with them */
235 	err = -ENOMEM;
236 	if (max_pages > fm->fc->max_pages)
237 		goto out;
238 	while (ap.num_pages < max_pages) {
239 		ap.pages[ap.num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
240 		if (!ap.pages[ap.num_pages])
241 			goto out;
242 		ap.num_pages++;
243 	}
244 
245 
246 	/* okay, let's send it to the client */
247 	ap.args.opcode = FUSE_IOCTL;
248 	ap.args.nodeid = ff->nodeid;
249 	ap.args.in_numargs = 1;
250 	ap.args.in_args[0].size = sizeof(inarg);
251 	ap.args.in_args[0].value = &inarg;
252 	if (in_size) {
253 		ap.args.in_numargs++;
254 		ap.args.in_args[1].size = in_size;
255 		ap.args.in_pages = true;
256 
257 		err = -EFAULT;
258 		iov_iter_init(&ii, ITER_SOURCE, in_iov, in_iovs, in_size);
259 		for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= ap.num_pages); i++) {
260 			c = copy_page_from_iter(ap.pages[i], 0, PAGE_SIZE, &ii);
261 			if (c != PAGE_SIZE && iov_iter_count(&ii))
262 				goto out;
263 		}
264 	}
265 
266 	ap.args.out_numargs = 2;
267 	ap.args.out_args[0].size = sizeof(outarg);
268 	ap.args.out_args[0].value = &outarg;
269 	ap.args.out_args[1].size = out_size;
270 	ap.args.out_pages = true;
271 	ap.args.out_argvar = true;
272 
273 	transferred = fuse_send_ioctl(fm, &ap.args);
274 	err = transferred;
275 	if (transferred < 0)
276 		goto out;
277 
278 	/* did it ask for retry? */
279 	if (outarg.flags & FUSE_IOCTL_RETRY) {
280 		void *vaddr;
281 
282 		/* no retry if in restricted mode */
283 		err = -EIO;
284 		if (!(flags & FUSE_IOCTL_UNRESTRICTED))
285 			goto out;
286 
287 		in_iovs = outarg.in_iovs;
288 		out_iovs = outarg.out_iovs;
289 
290 		/*
291 		 * Make sure things are in boundary, separate checks
292 		 * are to protect against overflow.
293 		 */
294 		err = -ENOMEM;
295 		if (in_iovs > FUSE_IOCTL_MAX_IOV ||
296 		    out_iovs > FUSE_IOCTL_MAX_IOV ||
297 		    in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV)
298 			goto out;
299 
300 		vaddr = kmap_local_page(ap.pages[0]);
301 		err = fuse_copy_ioctl_iovec(fm->fc, iov_page, vaddr,
302 					    transferred, in_iovs + out_iovs,
303 					    (flags & FUSE_IOCTL_COMPAT) != 0);
304 		kunmap_local(vaddr);
305 		if (err)
306 			goto out;
307 
308 		in_iov = iov_page;
309 		out_iov = in_iov + in_iovs;
310 
311 		err = fuse_verify_ioctl_iov(fm->fc, in_iov, in_iovs);
312 		if (err)
313 			goto out;
314 
315 		err = fuse_verify_ioctl_iov(fm->fc, out_iov, out_iovs);
316 		if (err)
317 			goto out;
318 
319 		goto retry;
320 	}
321 
322 	err = -EIO;
323 	if (transferred > inarg.out_size)
324 		goto out;
325 
326 	err = -EFAULT;
327 	iov_iter_init(&ii, ITER_DEST, out_iov, out_iovs, transferred);
328 	for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= ap.num_pages); i++) {
329 		c = copy_page_to_iter(ap.pages[i], 0, PAGE_SIZE, &ii);
330 		if (c != PAGE_SIZE && iov_iter_count(&ii))
331 			goto out;
332 	}
333 	err = 0;
334  out:
335 	free_page((unsigned long) iov_page);
336 	while (ap.num_pages)
337 		__free_page(ap.pages[--ap.num_pages]);
338 	kfree(ap.pages);
339 
340 	return err ? err : outarg.result;
341 }
342 EXPORT_SYMBOL_GPL(fuse_do_ioctl);
343 
344 long fuse_ioctl_common(struct file *file, unsigned int cmd,
345 		       unsigned long arg, unsigned int flags)
346 {
347 	struct inode *inode = file_inode(file);
348 	struct fuse_conn *fc = get_fuse_conn(inode);
349 
350 	if (!fuse_allow_current_process(fc))
351 		return -EACCES;
352 
353 	if (fuse_is_bad(inode))
354 		return -EIO;
355 
356 	return fuse_do_ioctl(file, cmd, arg, flags);
357 }
358 
359 long fuse_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
360 {
361 	return fuse_ioctl_common(file, cmd, arg, 0);
362 }
363 
364 long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
365 			    unsigned long arg)
366 {
367 	return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT);
368 }
369 
370 static int fuse_priv_ioctl(struct inode *inode, struct fuse_file *ff,
371 			   unsigned int cmd, void *ptr, size_t size)
372 {
373 	struct fuse_mount *fm = ff->fm;
374 	struct fuse_ioctl_in inarg;
375 	struct fuse_ioctl_out outarg;
376 	FUSE_ARGS(args);
377 	int err;
378 
379 	memset(&inarg, 0, sizeof(inarg));
380 	inarg.fh = ff->fh;
381 	inarg.cmd = cmd;
382 
383 #if BITS_PER_LONG == 32
384 	inarg.flags |= FUSE_IOCTL_32BIT;
385 #endif
386 	if (S_ISDIR(inode->i_mode))
387 		inarg.flags |= FUSE_IOCTL_DIR;
388 
389 	if (_IOC_DIR(cmd) & _IOC_READ)
390 		inarg.out_size = size;
391 	if (_IOC_DIR(cmd) & _IOC_WRITE)
392 		inarg.in_size = size;
393 
394 	args.opcode = FUSE_IOCTL;
395 	args.nodeid = ff->nodeid;
396 	args.in_numargs = 2;
397 	args.in_args[0].size = sizeof(inarg);
398 	args.in_args[0].value = &inarg;
399 	args.in_args[1].size = inarg.in_size;
400 	args.in_args[1].value = ptr;
401 	args.out_numargs = 2;
402 	args.out_args[0].size = sizeof(outarg);
403 	args.out_args[0].value = &outarg;
404 	args.out_args[1].size = inarg.out_size;
405 	args.out_args[1].value = ptr;
406 
407 	err = fuse_send_ioctl(fm, &args);
408 	if (!err) {
409 		if (outarg.result < 0)
410 			err = outarg.result;
411 		else if (outarg.flags & FUSE_IOCTL_RETRY)
412 			err = -EIO;
413 	}
414 	return err;
415 }
416 
417 static struct fuse_file *fuse_priv_ioctl_prepare(struct inode *inode)
418 {
419 	struct fuse_mount *fm = get_fuse_mount(inode);
420 	bool isdir = S_ISDIR(inode->i_mode);
421 
422 	if (!S_ISREG(inode->i_mode) && !isdir)
423 		return ERR_PTR(-ENOTTY);
424 
425 	return fuse_file_open(fm, get_node_id(inode), O_RDONLY, isdir);
426 }
427 
428 static void fuse_priv_ioctl_cleanup(struct inode *inode, struct fuse_file *ff)
429 {
430 	fuse_file_release(inode, ff, O_RDONLY, NULL, S_ISDIR(inode->i_mode));
431 }
432 
433 int fuse_fileattr_get(struct dentry *dentry, struct fileattr *fa)
434 {
435 	struct inode *inode = d_inode(dentry);
436 	struct fuse_file *ff;
437 	unsigned int flags;
438 	struct fsxattr xfa;
439 	int err;
440 
441 	ff = fuse_priv_ioctl_prepare(inode);
442 	if (IS_ERR(ff))
443 		return PTR_ERR(ff);
444 
445 	if (fa->flags_valid) {
446 		err = fuse_priv_ioctl(inode, ff, FS_IOC_GETFLAGS,
447 				      &flags, sizeof(flags));
448 		if (err)
449 			goto cleanup;
450 
451 		fileattr_fill_flags(fa, flags);
452 	} else {
453 		err = fuse_priv_ioctl(inode, ff, FS_IOC_FSGETXATTR,
454 				      &xfa, sizeof(xfa));
455 		if (err)
456 			goto cleanup;
457 
458 		fileattr_fill_xflags(fa, xfa.fsx_xflags);
459 		fa->fsx_extsize = xfa.fsx_extsize;
460 		fa->fsx_nextents = xfa.fsx_nextents;
461 		fa->fsx_projid = xfa.fsx_projid;
462 		fa->fsx_cowextsize = xfa.fsx_cowextsize;
463 	}
464 cleanup:
465 	fuse_priv_ioctl_cleanup(inode, ff);
466 
467 	return err;
468 }
469 
470 int fuse_fileattr_set(struct user_namespace *mnt_userns,
471 		      struct dentry *dentry, struct fileattr *fa)
472 {
473 	struct inode *inode = d_inode(dentry);
474 	struct fuse_file *ff;
475 	unsigned int flags = fa->flags;
476 	struct fsxattr xfa;
477 	int err;
478 
479 	ff = fuse_priv_ioctl_prepare(inode);
480 	if (IS_ERR(ff))
481 		return PTR_ERR(ff);
482 
483 	if (fa->flags_valid) {
484 		err = fuse_priv_ioctl(inode, ff, FS_IOC_SETFLAGS,
485 				      &flags, sizeof(flags));
486 		if (err)
487 			goto cleanup;
488 	} else {
489 		memset(&xfa, 0, sizeof(xfa));
490 		xfa.fsx_xflags = fa->fsx_xflags;
491 		xfa.fsx_extsize = fa->fsx_extsize;
492 		xfa.fsx_nextents = fa->fsx_nextents;
493 		xfa.fsx_projid = fa->fsx_projid;
494 		xfa.fsx_cowextsize = fa->fsx_cowextsize;
495 
496 		err = fuse_priv_ioctl(inode, ff, FS_IOC_FSSETXATTR,
497 				      &xfa, sizeof(xfa));
498 	}
499 
500 cleanup:
501 	fuse_priv_ioctl_cleanup(inode, ff);
502 
503 	return err;
504 }
505