1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2017 Red Hat, Inc.
9 #include <linux/compat.h>
10 #include <linux/fileattr.h>
11 #include <linux/fsverity.h>
13 #define FUSE_VERITY_ENABLE_ARG_MAX_PAGES 256
15 static ssize_t
fuse_send_ioctl(struct fuse_mount
*fm
, struct fuse_args
*args
,
16 struct fuse_ioctl_out
*outarg
)
20 args
->out_args
[0].size
= sizeof(*outarg
);
21 args
->out_args
[0].value
= outarg
;
23 ret
= fuse_simple_request(fm
, args
);
25 /* Translate ENOSYS, which shouldn't be returned from fs */
29 if (ret
>= 0 && outarg
->result
== -ENOSYS
)
30 outarg
->result
= -ENOTTY
;
36 * CUSE servers compiled on 32bit broke on 64bit kernels because the
37 * ABI was defined to be 'struct iovec' which is different on 32bit
38 * and 64bit. Fortunately we can determine which structure the server
39 * used from the size of the reply.
41 static int fuse_copy_ioctl_iovec_old(struct iovec
*dst
, void *src
,
42 size_t transferred
, unsigned count
,
46 if (count
* sizeof(struct compat_iovec
) == transferred
) {
47 struct compat_iovec
*ciov
= src
;
51 * With this interface a 32bit server cannot support
52 * non-compat (i.e. ones coming from 64bit apps) ioctl
58 for (i
= 0; i
< count
; i
++) {
59 dst
[i
].iov_base
= compat_ptr(ciov
[i
].iov_base
);
60 dst
[i
].iov_len
= ciov
[i
].iov_len
;
66 if (count
* sizeof(struct iovec
) != transferred
)
69 memcpy(dst
, src
, transferred
);
73 /* Make sure iov_length() won't overflow */
74 static int fuse_verify_ioctl_iov(struct fuse_conn
*fc
, struct iovec
*iov
,
78 u32 max
= fc
->max_pages
<< PAGE_SHIFT
;
80 for (n
= 0; n
< count
; n
++, iov
++) {
81 if (iov
->iov_len
> (size_t) max
)
88 static int fuse_copy_ioctl_iovec(struct fuse_conn
*fc
, struct iovec
*dst
,
89 void *src
, size_t transferred
, unsigned count
,
93 struct fuse_ioctl_iovec
*fiov
= src
;
96 return fuse_copy_ioctl_iovec_old(dst
, src
, transferred
,
100 if (count
* sizeof(struct fuse_ioctl_iovec
) != transferred
)
103 for (i
= 0; i
< count
; i
++) {
104 /* Did the server supply an inappropriate value? */
105 if (fiov
[i
].base
!= (unsigned long) fiov
[i
].base
||
106 fiov
[i
].len
!= (unsigned long) fiov
[i
].len
)
109 dst
[i
].iov_base
= (void __user
*) (unsigned long) fiov
[i
].base
;
110 dst
[i
].iov_len
= (size_t) fiov
[i
].len
;
114 (ptr_to_compat(dst
[i
].iov_base
) != fiov
[i
].base
||
115 (compat_size_t
) dst
[i
].iov_len
!= fiov
[i
].len
))
123 /* For fs-verity, determine iov lengths from input */
124 static int fuse_setup_measure_verity(unsigned long arg
, struct iovec
*iov
)
127 struct fsverity_digest __user
*uarg
= (void __user
*)arg
;
129 if (copy_from_user(&digest_size
, &uarg
->digest_size
, sizeof(digest_size
)))
132 if (digest_size
> SIZE_MAX
- sizeof(struct fsverity_digest
))
135 iov
->iov_len
= sizeof(struct fsverity_digest
) + digest_size
;
140 static int fuse_setup_enable_verity(unsigned long arg
, struct iovec
*iov
,
141 unsigned int *in_iovs
)
143 struct fsverity_enable_arg enable
;
144 struct fsverity_enable_arg __user
*uarg
= (void __user
*)arg
;
145 const __u32 max_buffer_len
= FUSE_VERITY_ENABLE_ARG_MAX_PAGES
* PAGE_SIZE
;
147 if (copy_from_user(&enable
, uarg
, sizeof(enable
)))
150 if (enable
.salt_size
> max_buffer_len
|| enable
.sig_size
> max_buffer_len
)
153 if (enable
.salt_size
> 0) {
157 iov
->iov_base
= u64_to_user_ptr(enable
.salt_ptr
);
158 iov
->iov_len
= enable
.salt_size
;
161 if (enable
.sig_size
> 0) {
165 iov
->iov_base
= u64_to_user_ptr(enable
.sig_ptr
);
166 iov
->iov_len
= enable
.sig_size
;
172 * For ioctls, there is no generic way to determine how much memory
173 * needs to be read and/or written. Furthermore, ioctls are allowed
174 * to dereference the passed pointer, so the parameter requires deep
175 * copying but FUSE has no idea whatsoever about what to copy in or
178 * This is solved by allowing FUSE server to retry ioctl with
179 * necessary in/out iovecs. Let's assume the ioctl implementation
180 * needs to read in the following structure.
187 * On the first callout to FUSE server, inarg->in_size and
188 * inarg->out_size will be NULL; then, the server completes the ioctl
189 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
190 * the actual iov array to
192 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
194 * which tells FUSE to copy in the requested area and retry the ioctl.
195 * On the second round, the server has access to the structure and
196 * from that it can tell what to look for next, so on the invocation,
197 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
199 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
200 * { .iov_base = a.buf, .iov_len = a.buflen } }
202 * FUSE will copy both struct a and the pointed buffer from the
203 * process doing the ioctl and retry ioctl with both struct a and the
206 * This time, FUSE server has everything it needs and completes ioctl
207 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
209 * Copying data out works the same way.
211 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
212 * automatically initializes in and out iovs by decoding @cmd with
213 * _IOC_* macros and the server is not allowed to request RETRY. This
214 * limits ioctl data transfers to well-formed ioctls and is the forced
215 * behavior for all FUSE servers.
217 long fuse_do_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
,
220 struct fuse_file
*ff
= file
->private_data
;
221 struct fuse_mount
*fm
= ff
->fm
;
222 struct fuse_ioctl_in inarg
= {
228 struct fuse_ioctl_out outarg
;
229 struct iovec
*iov_page
= NULL
;
230 struct iovec
*in_iov
= NULL
, *out_iov
= NULL
;
231 unsigned int in_iovs
= 0, out_iovs
= 0, max_pages
;
232 size_t in_size
, out_size
, c
;
236 struct fuse_args_pages ap
= {};
238 #if BITS_PER_LONG == 32
239 inarg
.flags
|= FUSE_IOCTL_32BIT
;
241 if (flags
& FUSE_IOCTL_COMPAT
) {
242 inarg
.flags
|= FUSE_IOCTL_32BIT
;
243 #ifdef CONFIG_X86_X32_ABI
244 if (in_x32_syscall())
245 inarg
.flags
|= FUSE_IOCTL_COMPAT_X32
;
250 /* assume all the iovs returned by client always fits in a page */
251 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec
) * FUSE_IOCTL_MAX_IOV
> PAGE_SIZE
);
254 ap
.folios
= fuse_folios_alloc(fm
->fc
->max_pages
, GFP_KERNEL
, &ap
.descs
);
255 iov_page
= (struct iovec
*) __get_free_page(GFP_KERNEL
);
256 if (!ap
.folios
|| !iov_page
)
259 fuse_folio_descs_length_init(ap
.descs
, 0, fm
->fc
->max_pages
);
262 * If restricted, initialize IO parameters as encoded in @cmd.
263 * RETRY from server is not allowed.
265 if (!(flags
& FUSE_IOCTL_UNRESTRICTED
)) {
266 struct iovec
*iov
= iov_page
;
268 iov
->iov_base
= (void __user
*)arg
;
269 iov
->iov_len
= _IOC_SIZE(cmd
);
271 if (_IOC_DIR(cmd
) & _IOC_WRITE
) {
276 if (_IOC_DIR(cmd
) & _IOC_READ
) {
283 case FS_IOC_MEASURE_VERITY
:
284 err
= fuse_setup_measure_verity(arg
, iov
);
286 case FS_IOC_ENABLE_VERITY
:
287 err
= fuse_setup_enable_verity(arg
, iov
, &in_iovs
);
295 inarg
.in_size
= in_size
= iov_length(in_iov
, in_iovs
);
296 inarg
.out_size
= out_size
= iov_length(out_iov
, out_iovs
);
299 * Out data can be used either for actual out data or iovs,
300 * make sure there always is at least one page.
302 out_size
= max_t(size_t, out_size
, PAGE_SIZE
);
303 max_pages
= DIV_ROUND_UP(max(in_size
, out_size
), PAGE_SIZE
);
305 /* make sure there are enough buffer pages and init request with them */
307 if (max_pages
> fm
->fc
->max_pages
)
309 while (ap
.num_folios
< max_pages
) {
310 ap
.folios
[ap
.num_folios
] = folio_alloc(GFP_KERNEL
| __GFP_HIGHMEM
, 0);
311 if (!ap
.folios
[ap
.num_folios
])
316 /* okay, let's send it to the client */
317 ap
.args
.opcode
= FUSE_IOCTL
;
318 ap
.args
.nodeid
= ff
->nodeid
;
319 ap
.args
.in_numargs
= 1;
320 ap
.args
.in_args
[0].size
= sizeof(inarg
);
321 ap
.args
.in_args
[0].value
= &inarg
;
323 ap
.args
.in_numargs
++;
324 ap
.args
.in_args
[1].size
= in_size
;
325 ap
.args
.in_pages
= true;
328 iov_iter_init(&ii
, ITER_SOURCE
, in_iov
, in_iovs
, in_size
);
329 for (i
= 0; iov_iter_count(&ii
) && !WARN_ON(i
>= ap
.num_folios
); i
++) {
330 c
= copy_folio_from_iter(ap
.folios
[i
], 0, PAGE_SIZE
, &ii
);
331 if (c
!= PAGE_SIZE
&& iov_iter_count(&ii
))
336 ap
.args
.out_numargs
= 2;
337 ap
.args
.out_args
[1].size
= out_size
;
338 ap
.args
.out_pages
= true;
339 ap
.args
.out_argvar
= true;
341 transferred
= fuse_send_ioctl(fm
, &ap
.args
, &outarg
);
346 /* did it ask for retry? */
347 if (outarg
.flags
& FUSE_IOCTL_RETRY
) {
350 /* no retry if in restricted mode */
352 if (!(flags
& FUSE_IOCTL_UNRESTRICTED
))
355 in_iovs
= outarg
.in_iovs
;
356 out_iovs
= outarg
.out_iovs
;
359 * Make sure things are in boundary, separate checks
360 * are to protect against overflow.
363 if (in_iovs
> FUSE_IOCTL_MAX_IOV
||
364 out_iovs
> FUSE_IOCTL_MAX_IOV
||
365 in_iovs
+ out_iovs
> FUSE_IOCTL_MAX_IOV
)
368 vaddr
= kmap_local_folio(ap
.folios
[0], 0);
369 err
= fuse_copy_ioctl_iovec(fm
->fc
, iov_page
, vaddr
,
370 transferred
, in_iovs
+ out_iovs
,
371 (flags
& FUSE_IOCTL_COMPAT
) != 0);
377 out_iov
= in_iov
+ in_iovs
;
379 err
= fuse_verify_ioctl_iov(fm
->fc
, in_iov
, in_iovs
);
383 err
= fuse_verify_ioctl_iov(fm
->fc
, out_iov
, out_iovs
);
391 if (transferred
> inarg
.out_size
)
395 iov_iter_init(&ii
, ITER_DEST
, out_iov
, out_iovs
, transferred
);
396 for (i
= 0; iov_iter_count(&ii
) && !WARN_ON(i
>= ap
.num_folios
); i
++) {
397 c
= copy_folio_to_iter(ap
.folios
[i
], 0, PAGE_SIZE
, &ii
);
398 if (c
!= PAGE_SIZE
&& iov_iter_count(&ii
))
403 free_page((unsigned long) iov_page
);
404 while (ap
.num_folios
)
405 folio_put(ap
.folios
[--ap
.num_folios
]);
408 return err
? err
: outarg
.result
;
410 EXPORT_SYMBOL_GPL(fuse_do_ioctl
);
412 long fuse_ioctl_common(struct file
*file
, unsigned int cmd
,
413 unsigned long arg
, unsigned int flags
)
415 struct inode
*inode
= file_inode(file
);
416 struct fuse_conn
*fc
= get_fuse_conn(inode
);
418 if (!fuse_allow_current_process(fc
))
421 if (fuse_is_bad(inode
))
424 return fuse_do_ioctl(file
, cmd
, arg
, flags
);
427 long fuse_file_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
429 return fuse_ioctl_common(file
, cmd
, arg
, 0);
432 long fuse_file_compat_ioctl(struct file
*file
, unsigned int cmd
,
435 return fuse_ioctl_common(file
, cmd
, arg
, FUSE_IOCTL_COMPAT
);
438 static int fuse_priv_ioctl(struct inode
*inode
, struct fuse_file
*ff
,
439 unsigned int cmd
, void *ptr
, size_t size
)
441 struct fuse_mount
*fm
= ff
->fm
;
442 struct fuse_ioctl_in inarg
;
443 struct fuse_ioctl_out outarg
;
447 memset(&inarg
, 0, sizeof(inarg
));
451 #if BITS_PER_LONG == 32
452 inarg
.flags
|= FUSE_IOCTL_32BIT
;
454 if (S_ISDIR(inode
->i_mode
))
455 inarg
.flags
|= FUSE_IOCTL_DIR
;
457 if (_IOC_DIR(cmd
) & _IOC_READ
)
458 inarg
.out_size
= size
;
459 if (_IOC_DIR(cmd
) & _IOC_WRITE
)
460 inarg
.in_size
= size
;
462 args
.opcode
= FUSE_IOCTL
;
463 args
.nodeid
= ff
->nodeid
;
465 args
.in_args
[0].size
= sizeof(inarg
);
466 args
.in_args
[0].value
= &inarg
;
467 args
.in_args
[1].size
= inarg
.in_size
;
468 args
.in_args
[1].value
= ptr
;
469 args
.out_numargs
= 2;
470 args
.out_args
[1].size
= inarg
.out_size
;
471 args
.out_args
[1].value
= ptr
;
473 err
= fuse_send_ioctl(fm
, &args
, &outarg
);
475 if (outarg
.result
< 0)
477 else if (outarg
.flags
& FUSE_IOCTL_RETRY
)
483 static struct fuse_file
*fuse_priv_ioctl_prepare(struct inode
*inode
)
485 struct fuse_mount
*fm
= get_fuse_mount(inode
);
486 bool isdir
= S_ISDIR(inode
->i_mode
);
488 if (!fuse_allow_current_process(fm
->fc
))
489 return ERR_PTR(-EACCES
);
491 if (fuse_is_bad(inode
))
492 return ERR_PTR(-EIO
);
494 if (!S_ISREG(inode
->i_mode
) && !isdir
)
495 return ERR_PTR(-ENOTTY
);
497 return fuse_file_open(fm
, get_node_id(inode
), O_RDONLY
, isdir
);
500 static void fuse_priv_ioctl_cleanup(struct inode
*inode
, struct fuse_file
*ff
)
502 fuse_file_release(inode
, ff
, O_RDONLY
, NULL
, S_ISDIR(inode
->i_mode
));
505 int fuse_fileattr_get(struct dentry
*dentry
, struct fileattr
*fa
)
507 struct inode
*inode
= d_inode(dentry
);
508 struct fuse_file
*ff
;
513 ff
= fuse_priv_ioctl_prepare(inode
);
517 if (fa
->flags_valid
) {
518 err
= fuse_priv_ioctl(inode
, ff
, FS_IOC_GETFLAGS
,
519 &flags
, sizeof(flags
));
523 fileattr_fill_flags(fa
, flags
);
525 err
= fuse_priv_ioctl(inode
, ff
, FS_IOC_FSGETXATTR
,
530 fileattr_fill_xflags(fa
, xfa
.fsx_xflags
);
531 fa
->fsx_extsize
= xfa
.fsx_extsize
;
532 fa
->fsx_nextents
= xfa
.fsx_nextents
;
533 fa
->fsx_projid
= xfa
.fsx_projid
;
534 fa
->fsx_cowextsize
= xfa
.fsx_cowextsize
;
537 fuse_priv_ioctl_cleanup(inode
, ff
);
542 int fuse_fileattr_set(struct mnt_idmap
*idmap
,
543 struct dentry
*dentry
, struct fileattr
*fa
)
545 struct inode
*inode
= d_inode(dentry
);
546 struct fuse_file
*ff
;
547 unsigned int flags
= fa
->flags
;
551 ff
= fuse_priv_ioctl_prepare(inode
);
555 if (fa
->flags_valid
) {
556 err
= fuse_priv_ioctl(inode
, ff
, FS_IOC_SETFLAGS
,
557 &flags
, sizeof(flags
));
561 memset(&xfa
, 0, sizeof(xfa
));
562 xfa
.fsx_xflags
= fa
->fsx_xflags
;
563 xfa
.fsx_extsize
= fa
->fsx_extsize
;
564 xfa
.fsx_nextents
= fa
->fsx_nextents
;
565 xfa
.fsx_projid
= fa
->fsx_projid
;
566 xfa
.fsx_cowextsize
= fa
->fsx_cowextsize
;
568 err
= fuse_priv_ioctl(inode
, ff
, FS_IOC_FSSETXATTR
,
573 fuse_priv_ioctl_cleanup(inode
, ff
);