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