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>
12 static ssize_t fuse_send_ioctl(struct fuse_mount *fm, struct fuse_args *args,
13 struct fuse_ioctl_out *outarg)
17 args->out_args[0].size = sizeof(*outarg);
18 args->out_args[0].value = outarg;
20 ret = fuse_simple_request(fm, args);
22 /* Translate ENOSYS, which shouldn't be returned from fs */
26 if (ret >= 0 && outarg->result == -ENOSYS)
27 outarg->result = -ENOTTY;
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.
38 static int fuse_copy_ioctl_iovec_old(struct iovec *dst, void *src,
39 size_t transferred, unsigned count,
43 if (count * sizeof(struct compat_iovec) == transferred) {
44 struct compat_iovec *ciov = src;
48 * With this interface a 32bit server cannot support
49 * non-compat (i.e. ones coming from 64bit apps) ioctl
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;
63 if (count * sizeof(struct iovec) != transferred)
66 memcpy(dst, src, transferred);
70 /* Make sure iov_length() won't overflow */
71 static int fuse_verify_ioctl_iov(struct fuse_conn *fc, struct iovec *iov,
75 u32 max = fc->max_pages << PAGE_SHIFT;
77 for (n = 0; n < count; n++, iov++) {
78 if (iov->iov_len > (size_t) max)
85 static int fuse_copy_ioctl_iovec(struct fuse_conn *fc, struct iovec *dst,
86 void *src, size_t transferred, unsigned count,
90 struct fuse_ioctl_iovec *fiov = src;
93 return fuse_copy_ioctl_iovec_old(dst, src, transferred,
97 if (count * sizeof(struct fuse_ioctl_iovec) != transferred)
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)
106 dst[i].iov_base = (void __user *) (unsigned long) fiov[i].base;
107 dst[i].iov_len = (size_t) fiov[i].len;
111 (ptr_to_compat(dst[i].iov_base) != fiov[i].base ||
112 (compat_size_t) dst[i].iov_len != fiov[i].len))
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
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.
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
142 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
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
149 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
150 * { .iov_base = a.buf, .iov_len = a.buflen } }
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
156 * This time, FUSE server has everything it needs and completes ioctl
157 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
159 * Copying data out works the same way.
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.
167 long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
170 struct fuse_file *ff = file->private_data;
171 struct fuse_mount *fm = ff->fm;
172 struct fuse_ioctl_in inarg = {
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;
186 struct fuse_args_pages ap = {};
188 #if BITS_PER_LONG == 32
189 inarg.flags |= FUSE_IOCTL_32BIT;
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;
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);
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)
209 fuse_page_descs_length_init(ap.descs, 0, fm->fc->max_pages);
212 * If restricted, initialize IO parameters as encoded in @cmd.
213 * RETRY from server is not allowed.
215 if (!(flags & FUSE_IOCTL_UNRESTRICTED)) {
216 struct iovec *iov = iov_page;
218 iov->iov_base = (void __user *)arg;
219 iov->iov_len = _IOC_SIZE(cmd);
221 if (_IOC_DIR(cmd) & _IOC_WRITE) {
226 if (_IOC_DIR(cmd) & _IOC_READ) {
233 inarg.in_size = in_size = iov_length(in_iov, in_iovs);
234 inarg.out_size = out_size = iov_length(out_iov, out_iovs);
237 * Out data can be used either for actual out data or iovs,
238 * make sure there always is at least one page.
240 out_size = max_t(size_t, out_size, PAGE_SIZE);
241 max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE);
243 /* make sure there are enough buffer pages and init request with them */
245 if (max_pages > fm->fc->max_pages)
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])
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;
262 ap.args.in_numargs++;
263 ap.args.in_args[1].size = in_size;
264 ap.args.in_pages = true;
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))
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;
280 transferred = fuse_send_ioctl(fm, &ap.args, &outarg);
285 /* did it ask for retry? */
286 if (outarg.flags & FUSE_IOCTL_RETRY) {
289 /* no retry if in restricted mode */
291 if (!(flags & FUSE_IOCTL_UNRESTRICTED))
294 in_iovs = outarg.in_iovs;
295 out_iovs = outarg.out_iovs;
298 * Make sure things are in boundary, separate checks
299 * are to protect against overflow.
302 if (in_iovs > FUSE_IOCTL_MAX_IOV ||
303 out_iovs > FUSE_IOCTL_MAX_IOV ||
304 in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV)
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);
316 out_iov = in_iov + in_iovs;
318 err = fuse_verify_ioctl_iov(fm->fc, in_iov, in_iovs);
322 err = fuse_verify_ioctl_iov(fm->fc, out_iov, out_iovs);
330 if (transferred > inarg.out_size)
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))
342 free_page((unsigned long) iov_page);
344 __free_page(ap.pages[--ap.num_pages]);
347 return err ? err : outarg.result;
349 EXPORT_SYMBOL_GPL(fuse_do_ioctl);
351 long fuse_ioctl_common(struct file *file, unsigned int cmd,
352 unsigned long arg, unsigned int flags)
354 struct inode *inode = file_inode(file);
355 struct fuse_conn *fc = get_fuse_conn(inode);
357 if (!fuse_allow_current_process(fc))
360 if (fuse_is_bad(inode))
363 return fuse_do_ioctl(file, cmd, arg, flags);
366 long fuse_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
368 return fuse_ioctl_common(file, cmd, arg, 0);
371 long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
374 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT);
377 static int fuse_priv_ioctl(struct inode *inode, struct fuse_file *ff,
378 unsigned int cmd, void *ptr, size_t size)
380 struct fuse_mount *fm = ff->fm;
381 struct fuse_ioctl_in inarg;
382 struct fuse_ioctl_out outarg;
386 memset(&inarg, 0, sizeof(inarg));
390 #if BITS_PER_LONG == 32
391 inarg.flags |= FUSE_IOCTL_32BIT;
393 if (S_ISDIR(inode->i_mode))
394 inarg.flags |= FUSE_IOCTL_DIR;
396 if (_IOC_DIR(cmd) & _IOC_READ)
397 inarg.out_size = size;
398 if (_IOC_DIR(cmd) & _IOC_WRITE)
399 inarg.in_size = size;
401 args.opcode = FUSE_IOCTL;
402 args.nodeid = ff->nodeid;
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;
412 err = fuse_send_ioctl(fm, &args, &outarg);
414 if (outarg.result < 0)
416 else if (outarg.flags & FUSE_IOCTL_RETRY)
422 static struct fuse_file *fuse_priv_ioctl_prepare(struct inode *inode)
424 struct fuse_mount *fm = get_fuse_mount(inode);
425 bool isdir = S_ISDIR(inode->i_mode);
427 if (!fuse_allow_current_process(fm->fc))
428 return ERR_PTR(-EACCES);
430 if (fuse_is_bad(inode))
431 return ERR_PTR(-EIO);
433 if (!S_ISREG(inode->i_mode) && !isdir)
434 return ERR_PTR(-ENOTTY);
436 return fuse_file_open(fm, get_node_id(inode), O_RDONLY, isdir);
439 static void fuse_priv_ioctl_cleanup(struct inode *inode, struct fuse_file *ff)
441 fuse_file_release(inode, ff, O_RDONLY, NULL, S_ISDIR(inode->i_mode));
444 int fuse_fileattr_get(struct dentry *dentry, struct fileattr *fa)
446 struct inode *inode = d_inode(dentry);
447 struct fuse_file *ff;
452 ff = fuse_priv_ioctl_prepare(inode);
456 if (fa->flags_valid) {
457 err = fuse_priv_ioctl(inode, ff, FS_IOC_GETFLAGS,
458 &flags, sizeof(flags));
462 fileattr_fill_flags(fa, flags);
464 err = fuse_priv_ioctl(inode, ff, FS_IOC_FSGETXATTR,
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;
476 fuse_priv_ioctl_cleanup(inode, ff);
481 int fuse_fileattr_set(struct mnt_idmap *idmap,
482 struct dentry *dentry, struct fileattr *fa)
484 struct inode *inode = d_inode(dentry);
485 struct fuse_file *ff;
486 unsigned int flags = fa->flags;
490 ff = fuse_priv_ioctl_prepare(inode);
494 if (fa->flags_valid) {
495 err = fuse_priv_ioctl(inode, ff, FS_IOC_SETFLAGS,
496 &flags, sizeof(flags));
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;
507 err = fuse_priv_ioctl(inode, ff, FS_IOC_FSSETXATTR,
512 fuse_priv_ioctl_cleanup(inode, ff);