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>
13 * CUSE servers compiled on 32bit broke on 64bit kernels because the
14 * ABI was defined to be 'struct iovec' which is different on 32bit
15 * and 64bit. Fortunately we can determine which structure the server
16 * used from the size of the reply.
18 static int fuse_copy_ioctl_iovec_old(struct iovec *dst, void *src,
19 size_t transferred, unsigned count,
23 if (count * sizeof(struct compat_iovec) == transferred) {
24 struct compat_iovec *ciov = src;
28 * With this interface a 32bit server cannot support
29 * non-compat (i.e. ones coming from 64bit apps) ioctl
35 for (i = 0; i < count; i++) {
36 dst[i].iov_base = compat_ptr(ciov[i].iov_base);
37 dst[i].iov_len = ciov[i].iov_len;
43 if (count * sizeof(struct iovec) != transferred)
46 memcpy(dst, src, transferred);
50 /* Make sure iov_length() won't overflow */
51 static int fuse_verify_ioctl_iov(struct fuse_conn *fc, struct iovec *iov,
55 u32 max = fc->max_pages << PAGE_SHIFT;
57 for (n = 0; n < count; n++, iov++) {
58 if (iov->iov_len > (size_t) max)
65 static int fuse_copy_ioctl_iovec(struct fuse_conn *fc, struct iovec *dst,
66 void *src, size_t transferred, unsigned count,
70 struct fuse_ioctl_iovec *fiov = src;
73 return fuse_copy_ioctl_iovec_old(dst, src, transferred,
77 if (count * sizeof(struct fuse_ioctl_iovec) != transferred)
80 for (i = 0; i < count; i++) {
81 /* Did the server supply an inappropriate value? */
82 if (fiov[i].base != (unsigned long) fiov[i].base ||
83 fiov[i].len != (unsigned long) fiov[i].len)
86 dst[i].iov_base = (void __user *) (unsigned long) fiov[i].base;
87 dst[i].iov_len = (size_t) fiov[i].len;
91 (ptr_to_compat(dst[i].iov_base) != fiov[i].base ||
92 (compat_size_t) dst[i].iov_len != fiov[i].len))
102 * For ioctls, there is no generic way to determine how much memory
103 * needs to be read and/or written. Furthermore, ioctls are allowed
104 * to dereference the passed pointer, so the parameter requires deep
105 * copying but FUSE has no idea whatsoever about what to copy in or
108 * This is solved by allowing FUSE server to retry ioctl with
109 * necessary in/out iovecs. Let's assume the ioctl implementation
110 * needs to read in the following structure.
117 * On the first callout to FUSE server, inarg->in_size and
118 * inarg->out_size will be NULL; then, the server completes the ioctl
119 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
120 * the actual iov array to
122 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
124 * which tells FUSE to copy in the requested area and retry the ioctl.
125 * On the second round, the server has access to the structure and
126 * from that it can tell what to look for next, so on the invocation,
127 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
129 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
130 * { .iov_base = a.buf, .iov_len = a.buflen } }
132 * FUSE will copy both struct a and the pointed buffer from the
133 * process doing the ioctl and retry ioctl with both struct a and the
136 * This time, FUSE server has everything it needs and completes ioctl
137 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
139 * Copying data out works the same way.
141 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
142 * automatically initializes in and out iovs by decoding @cmd with
143 * _IOC_* macros and the server is not allowed to request RETRY. This
144 * limits ioctl data transfers to well-formed ioctls and is the forced
145 * behavior for all FUSE servers.
147 long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
150 struct fuse_file *ff = file->private_data;
151 struct fuse_mount *fm = ff->fm;
152 struct fuse_ioctl_in inarg = {
158 struct fuse_ioctl_out outarg;
159 struct iovec *iov_page = NULL;
160 struct iovec *in_iov = NULL, *out_iov = NULL;
161 unsigned int in_iovs = 0, out_iovs = 0, max_pages;
162 size_t in_size, out_size, c;
166 struct fuse_args_pages ap = {};
168 #if BITS_PER_LONG == 32
169 inarg.flags |= FUSE_IOCTL_32BIT;
171 if (flags & FUSE_IOCTL_COMPAT) {
172 inarg.flags |= FUSE_IOCTL_32BIT;
173 #ifdef CONFIG_X86_X32
174 if (in_x32_syscall())
175 inarg.flags |= FUSE_IOCTL_COMPAT_X32;
180 /* assume all the iovs returned by client always fits in a page */
181 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE);
184 ap.pages = fuse_pages_alloc(fm->fc->max_pages, GFP_KERNEL, &ap.descs);
185 iov_page = (struct iovec *) __get_free_page(GFP_KERNEL);
186 if (!ap.pages || !iov_page)
189 fuse_page_descs_length_init(ap.descs, 0, fm->fc->max_pages);
192 * If restricted, initialize IO parameters as encoded in @cmd.
193 * RETRY from server is not allowed.
195 if (!(flags & FUSE_IOCTL_UNRESTRICTED)) {
196 struct iovec *iov = iov_page;
198 iov->iov_base = (void __user *)arg;
199 iov->iov_len = _IOC_SIZE(cmd);
201 if (_IOC_DIR(cmd) & _IOC_WRITE) {
206 if (_IOC_DIR(cmd) & _IOC_READ) {
213 inarg.in_size = in_size = iov_length(in_iov, in_iovs);
214 inarg.out_size = out_size = iov_length(out_iov, out_iovs);
217 * Out data can be used either for actual out data or iovs,
218 * make sure there always is at least one page.
220 out_size = max_t(size_t, out_size, PAGE_SIZE);
221 max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE);
223 /* make sure there are enough buffer pages and init request with them */
225 if (max_pages > fm->fc->max_pages)
227 while (ap.num_pages < max_pages) {
228 ap.pages[ap.num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
229 if (!ap.pages[ap.num_pages])
235 /* okay, let's send it to the client */
236 ap.args.opcode = FUSE_IOCTL;
237 ap.args.nodeid = ff->nodeid;
238 ap.args.in_numargs = 1;
239 ap.args.in_args[0].size = sizeof(inarg);
240 ap.args.in_args[0].value = &inarg;
242 ap.args.in_numargs++;
243 ap.args.in_args[1].size = in_size;
244 ap.args.in_pages = true;
247 iov_iter_init(&ii, WRITE, in_iov, in_iovs, in_size);
248 for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= ap.num_pages); i++) {
249 c = copy_page_from_iter(ap.pages[i], 0, PAGE_SIZE, &ii);
250 if (c != PAGE_SIZE && iov_iter_count(&ii))
255 ap.args.out_numargs = 2;
256 ap.args.out_args[0].size = sizeof(outarg);
257 ap.args.out_args[0].value = &outarg;
258 ap.args.out_args[1].size = out_size;
259 ap.args.out_pages = true;
260 ap.args.out_argvar = true;
262 transferred = fuse_simple_request(fm, &ap.args);
267 /* did it ask for retry? */
268 if (outarg.flags & FUSE_IOCTL_RETRY) {
271 /* no retry if in restricted mode */
273 if (!(flags & FUSE_IOCTL_UNRESTRICTED))
276 in_iovs = outarg.in_iovs;
277 out_iovs = outarg.out_iovs;
280 * Make sure things are in boundary, separate checks
281 * are to protect against overflow.
284 if (in_iovs > FUSE_IOCTL_MAX_IOV ||
285 out_iovs > FUSE_IOCTL_MAX_IOV ||
286 in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV)
289 vaddr = kmap_local_page(ap.pages[0]);
290 err = fuse_copy_ioctl_iovec(fm->fc, iov_page, vaddr,
291 transferred, in_iovs + out_iovs,
292 (flags & FUSE_IOCTL_COMPAT) != 0);
298 out_iov = in_iov + in_iovs;
300 err = fuse_verify_ioctl_iov(fm->fc, in_iov, in_iovs);
304 err = fuse_verify_ioctl_iov(fm->fc, out_iov, out_iovs);
312 if (transferred > inarg.out_size)
316 iov_iter_init(&ii, READ, out_iov, out_iovs, transferred);
317 for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= ap.num_pages); i++) {
318 c = copy_page_to_iter(ap.pages[i], 0, PAGE_SIZE, &ii);
319 if (c != PAGE_SIZE && iov_iter_count(&ii))
324 free_page((unsigned long) iov_page);
326 __free_page(ap.pages[--ap.num_pages]);
329 return err ? err : outarg.result;
331 EXPORT_SYMBOL_GPL(fuse_do_ioctl);
333 long fuse_ioctl_common(struct file *file, unsigned int cmd,
334 unsigned long arg, unsigned int flags)
336 struct inode *inode = file_inode(file);
337 struct fuse_conn *fc = get_fuse_conn(inode);
339 if (!fuse_allow_current_process(fc))
342 if (fuse_is_bad(inode))
345 return fuse_do_ioctl(file, cmd, arg, flags);
348 long fuse_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
350 return fuse_ioctl_common(file, cmd, arg, 0);
353 long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
356 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT);
359 static int fuse_priv_ioctl(struct inode *inode, struct fuse_file *ff,
360 unsigned int cmd, void *ptr, size_t size)
362 struct fuse_mount *fm = ff->fm;
363 struct fuse_ioctl_in inarg;
364 struct fuse_ioctl_out outarg;
368 memset(&inarg, 0, sizeof(inarg));
372 #if BITS_PER_LONG == 32
373 inarg.flags |= FUSE_IOCTL_32BIT;
375 if (S_ISDIR(inode->i_mode))
376 inarg.flags |= FUSE_IOCTL_DIR;
378 if (_IOC_DIR(cmd) & _IOC_READ)
379 inarg.out_size = size;
380 if (_IOC_DIR(cmd) & _IOC_WRITE)
381 inarg.in_size = size;
383 args.opcode = FUSE_IOCTL;
384 args.nodeid = ff->nodeid;
386 args.in_args[0].size = sizeof(inarg);
387 args.in_args[0].value = &inarg;
388 args.in_args[1].size = inarg.in_size;
389 args.in_args[1].value = ptr;
390 args.out_numargs = 2;
391 args.out_args[0].size = sizeof(outarg);
392 args.out_args[0].value = &outarg;
393 args.out_args[1].size = inarg.out_size;
394 args.out_args[1].value = ptr;
396 err = fuse_simple_request(fm, &args);
398 if (outarg.result < 0)
400 else if (outarg.flags & FUSE_IOCTL_RETRY)
406 static struct fuse_file *fuse_priv_ioctl_prepare(struct inode *inode)
408 struct fuse_mount *fm = get_fuse_mount(inode);
409 bool isdir = S_ISDIR(inode->i_mode);
411 if (!S_ISREG(inode->i_mode) && !isdir)
412 return ERR_PTR(-ENOTTY);
414 return fuse_file_open(fm, get_node_id(inode), O_RDONLY, isdir);
417 static void fuse_priv_ioctl_cleanup(struct inode *inode, struct fuse_file *ff)
419 fuse_file_release(inode, ff, O_RDONLY, NULL, S_ISDIR(inode->i_mode));
422 int fuse_fileattr_get(struct dentry *dentry, struct fileattr *fa)
424 struct inode *inode = d_inode(dentry);
425 struct fuse_file *ff;
430 ff = fuse_priv_ioctl_prepare(inode);
434 if (fa->flags_valid) {
435 err = fuse_priv_ioctl(inode, ff, FS_IOC_GETFLAGS,
436 &flags, sizeof(flags));
440 fileattr_fill_flags(fa, flags);
442 err = fuse_priv_ioctl(inode, ff, FS_IOC_FSGETXATTR,
447 fileattr_fill_xflags(fa, xfa.fsx_xflags);
448 fa->fsx_extsize = xfa.fsx_extsize;
449 fa->fsx_nextents = xfa.fsx_nextents;
450 fa->fsx_projid = xfa.fsx_projid;
451 fa->fsx_cowextsize = xfa.fsx_cowextsize;
454 fuse_priv_ioctl_cleanup(inode, ff);
459 int fuse_fileattr_set(struct user_namespace *mnt_userns,
460 struct dentry *dentry, struct fileattr *fa)
462 struct inode *inode = d_inode(dentry);
463 struct fuse_file *ff;
464 unsigned int flags = fa->flags;
468 ff = fuse_priv_ioctl_prepare(inode);
472 if (fa->flags_valid) {
473 err = fuse_priv_ioctl(inode, ff, FS_IOC_SETFLAGS,
474 &flags, sizeof(flags));
478 memset(&xfa, 0, sizeof(xfa));
479 xfa.fsx_xflags = fa->fsx_xflags;
480 xfa.fsx_extsize = fa->fsx_extsize;
481 xfa.fsx_nextents = fa->fsx_nextents;
482 xfa.fsx_projid = fa->fsx_projid;
483 xfa.fsx_cowextsize = fa->fsx_cowextsize;
485 err = fuse_priv_ioctl(inode, ff, FS_IOC_FSSETXATTR,
490 fuse_priv_ioctl_cleanup(inode, ff);