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
fuse_send_ioctl(struct fuse_mount * fm,struct fuse_args * args,struct fuse_ioctl_out * outarg)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 */
fuse_copy_ioctl_iovec_old(struct iovec * dst,void * src,size_t transferred,unsigned count,bool is_compat)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 */
fuse_verify_ioctl_iov(struct fuse_conn * fc,struct iovec * iov,size_t count)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
fuse_copy_ioctl_iovec(struct fuse_conn * fc,struct iovec * dst,void * src,size_t transferred,unsigned count,bool is_compat)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 */
fuse_do_ioctl(struct file * file,unsigned int cmd,unsigned long arg,unsigned int flags)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
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, WRITE, 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_atomic(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_atomic(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, READ, 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
fuse_ioctl_common(struct file * file,unsigned int cmd,unsigned long arg,unsigned int flags)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 #ifdef CONFIG_FUSE_BPF
364 {
365 struct fuse_file *ff = file->private_data;
366
367 /* TODO - this is simply passthrough, not a proper BPF filter */
368 if (ff->backing_file)
369 return fuse_backing_ioctl(file, cmd, arg, flags);
370 }
371 #endif
372 return fuse_do_ioctl(file, cmd, arg, flags);
373 }
374
fuse_file_ioctl(struct file * file,unsigned int cmd,unsigned long arg)375 long fuse_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
376 {
377 return fuse_ioctl_common(file, cmd, arg, 0);
378 }
379
fuse_file_compat_ioctl(struct file * file,unsigned int cmd,unsigned long arg)380 long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
381 unsigned long arg)
382 {
383 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT);
384 }
385
fuse_priv_ioctl(struct inode * inode,struct fuse_file * ff,unsigned int cmd,void * ptr,size_t size)386 static int fuse_priv_ioctl(struct inode *inode, struct fuse_file *ff,
387 unsigned int cmd, void *ptr, size_t size)
388 {
389 struct fuse_mount *fm = ff->fm;
390 struct fuse_ioctl_in inarg;
391 struct fuse_ioctl_out outarg;
392 FUSE_ARGS(args);
393 int err;
394
395 memset(&inarg, 0, sizeof(inarg));
396 inarg.fh = ff->fh;
397 inarg.cmd = cmd;
398
399 #if BITS_PER_LONG == 32
400 inarg.flags |= FUSE_IOCTL_32BIT;
401 #endif
402 if (S_ISDIR(inode->i_mode))
403 inarg.flags |= FUSE_IOCTL_DIR;
404
405 if (_IOC_DIR(cmd) & _IOC_READ)
406 inarg.out_size = size;
407 if (_IOC_DIR(cmd) & _IOC_WRITE)
408 inarg.in_size = size;
409
410 args.opcode = FUSE_IOCTL;
411 args.nodeid = ff->nodeid;
412 args.in_numargs = 2;
413 args.in_args[0].size = sizeof(inarg);
414 args.in_args[0].value = &inarg;
415 args.in_args[1].size = inarg.in_size;
416 args.in_args[1].value = ptr;
417 args.out_numargs = 2;
418 args.out_args[1].size = inarg.out_size;
419 args.out_args[1].value = ptr;
420
421 err = fuse_send_ioctl(fm, &args, &outarg);
422 if (!err) {
423 if (outarg.result < 0)
424 err = outarg.result;
425 else if (outarg.flags & FUSE_IOCTL_RETRY)
426 err = -EIO;
427 }
428 return err;
429 }
430
fuse_priv_ioctl_prepare(struct inode * inode)431 static struct fuse_file *fuse_priv_ioctl_prepare(struct inode *inode)
432 {
433 struct fuse_mount *fm = get_fuse_mount(inode);
434 bool isdir = S_ISDIR(inode->i_mode);
435
436 if (!fuse_allow_current_process(fm->fc))
437 return ERR_PTR(-EACCES);
438
439 if (fuse_is_bad(inode))
440 return ERR_PTR(-EIO);
441
442 if (!S_ISREG(inode->i_mode) && !isdir)
443 return ERR_PTR(-ENOTTY);
444
445 return fuse_file_open(fm, get_node_id(inode), O_RDONLY, isdir);
446 }
447
fuse_priv_ioctl_cleanup(struct inode * inode,struct fuse_file * ff)448 static void fuse_priv_ioctl_cleanup(struct inode *inode, struct fuse_file *ff)
449 {
450 fuse_file_release(inode, ff, O_RDONLY, NULL, S_ISDIR(inode->i_mode));
451 }
452
fuse_fileattr_get(struct dentry * dentry,struct fileattr * fa)453 int fuse_fileattr_get(struct dentry *dentry, struct fileattr *fa)
454 {
455 struct inode *inode = d_inode(dentry);
456 struct fuse_file *ff;
457 unsigned int flags;
458 struct fsxattr xfa;
459 int err;
460
461 ff = fuse_priv_ioctl_prepare(inode);
462 if (IS_ERR(ff))
463 return PTR_ERR(ff);
464
465 if (fa->flags_valid) {
466 err = fuse_priv_ioctl(inode, ff, FS_IOC_GETFLAGS,
467 &flags, sizeof(flags));
468 if (err)
469 goto cleanup;
470
471 fileattr_fill_flags(fa, flags);
472 } else {
473 err = fuse_priv_ioctl(inode, ff, FS_IOC_FSGETXATTR,
474 &xfa, sizeof(xfa));
475 if (err)
476 goto cleanup;
477
478 fileattr_fill_xflags(fa, xfa.fsx_xflags);
479 fa->fsx_extsize = xfa.fsx_extsize;
480 fa->fsx_nextents = xfa.fsx_nextents;
481 fa->fsx_projid = xfa.fsx_projid;
482 fa->fsx_cowextsize = xfa.fsx_cowextsize;
483 }
484 cleanup:
485 fuse_priv_ioctl_cleanup(inode, ff);
486
487 return err;
488 }
489
fuse_fileattr_set(struct user_namespace * mnt_userns,struct dentry * dentry,struct fileattr * fa)490 int fuse_fileattr_set(struct user_namespace *mnt_userns,
491 struct dentry *dentry, struct fileattr *fa)
492 {
493 struct inode *inode = d_inode(dentry);
494 struct fuse_file *ff;
495 unsigned int flags = fa->flags;
496 struct fsxattr xfa;
497 int err;
498
499 ff = fuse_priv_ioctl_prepare(inode);
500 if (IS_ERR(ff))
501 return PTR_ERR(ff);
502
503 if (fa->flags_valid) {
504 err = fuse_priv_ioctl(inode, ff, FS_IOC_SETFLAGS,
505 &flags, sizeof(flags));
506 if (err)
507 goto cleanup;
508 } else {
509 memset(&xfa, 0, sizeof(xfa));
510 xfa.fsx_xflags = fa->fsx_xflags;
511 xfa.fsx_extsize = fa->fsx_extsize;
512 xfa.fsx_nextents = fa->fsx_nextents;
513 xfa.fsx_projid = fa->fsx_projid;
514 xfa.fsx_cowextsize = fa->fsx_cowextsize;
515
516 err = fuse_priv_ioctl(inode, ff, FS_IOC_FSSETXATTR,
517 &xfa, sizeof(xfa));
518 }
519
520 cleanup:
521 fuse_priv_ioctl_cleanup(inode, ff);
522
523 return err;
524 }
525