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)
14 ssize_t ret = fuse_simple_request(fm, args);
16 /* Translate ENOSYS, which shouldn't be returned from fs */
24 * CUSE servers compiled on 32bit broke on 64bit kernels because the
25 * ABI was defined to be 'struct iovec' which is different on 32bit
26 * and 64bit. Fortunately we can determine which structure the server
27 * used from the size of the reply.
29 static int fuse_copy_ioctl_iovec_old(struct iovec *dst, void *src,
30 size_t transferred, unsigned count,
34 if (count * sizeof(struct compat_iovec) == transferred) {
35 struct compat_iovec *ciov = src;
39 * With this interface a 32bit server cannot support
40 * non-compat (i.e. ones coming from 64bit apps) ioctl
46 for (i = 0; i < count; i++) {
47 dst[i].iov_base = compat_ptr(ciov[i].iov_base);
48 dst[i].iov_len = ciov[i].iov_len;
54 if (count * sizeof(struct iovec) != transferred)
57 memcpy(dst, src, transferred);
61 /* Make sure iov_length() won't overflow */
62 static int fuse_verify_ioctl_iov(struct fuse_conn *fc, struct iovec *iov,
66 u32 max = fc->max_pages << PAGE_SHIFT;
68 for (n = 0; n < count; n++, iov++) {
69 if (iov->iov_len > (size_t) max)
76 static int fuse_copy_ioctl_iovec(struct fuse_conn *fc, struct iovec *dst,
77 void *src, size_t transferred, unsigned count,
81 struct fuse_ioctl_iovec *fiov = src;
84 return fuse_copy_ioctl_iovec_old(dst, src, transferred,
88 if (count * sizeof(struct fuse_ioctl_iovec) != transferred)
91 for (i = 0; i < count; i++) {
92 /* Did the server supply an inappropriate value? */
93 if (fiov[i].base != (unsigned long) fiov[i].base ||
94 fiov[i].len != (unsigned long) fiov[i].len)
97 dst[i].iov_base = (void __user *) (unsigned long) fiov[i].base;
98 dst[i].iov_len = (size_t) fiov[i].len;
102 (ptr_to_compat(dst[i].iov_base) != fiov[i].base ||
103 (compat_size_t) dst[i].iov_len != fiov[i].len))
113 * For ioctls, there is no generic way to determine how much memory
114 * needs to be read and/or written. Furthermore, ioctls are allowed
115 * to dereference the passed pointer, so the parameter requires deep
116 * copying but FUSE has no idea whatsoever about what to copy in or
119 * This is solved by allowing FUSE server to retry ioctl with
120 * necessary in/out iovecs. Let's assume the ioctl implementation
121 * needs to read in the following structure.
128 * On the first callout to FUSE server, inarg->in_size and
129 * inarg->out_size will be NULL; then, the server completes the ioctl
130 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
131 * the actual iov array to
133 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
135 * which tells FUSE to copy in the requested area and retry the ioctl.
136 * On the second round, the server has access to the structure and
137 * from that it can tell what to look for next, so on the invocation,
138 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
140 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
141 * { .iov_base = a.buf, .iov_len = a.buflen } }
143 * FUSE will copy both struct a and the pointed buffer from the
144 * process doing the ioctl and retry ioctl with both struct a and the
147 * This time, FUSE server has everything it needs and completes ioctl
148 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
150 * Copying data out works the same way.
152 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
153 * automatically initializes in and out iovs by decoding @cmd with
154 * _IOC_* macros and the server is not allowed to request RETRY. This
155 * limits ioctl data transfers to well-formed ioctls and is the forced
156 * behavior for all FUSE servers.
158 long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
161 struct fuse_file *ff = file->private_data;
162 struct fuse_mount *fm = ff->fm;
163 struct fuse_ioctl_in inarg = {
169 struct fuse_ioctl_out outarg;
170 struct iovec *iov_page = NULL;
171 struct iovec *in_iov = NULL, *out_iov = NULL;
172 unsigned int in_iovs = 0, out_iovs = 0, max_pages;
173 size_t in_size, out_size, c;
177 struct fuse_args_pages ap = {};
179 #if BITS_PER_LONG == 32
180 inarg.flags |= FUSE_IOCTL_32BIT;
182 if (flags & FUSE_IOCTL_COMPAT) {
183 inarg.flags |= FUSE_IOCTL_32BIT;
184 #ifdef CONFIG_X86_X32_ABI
185 if (in_x32_syscall())
186 inarg.flags |= FUSE_IOCTL_COMPAT_X32;
191 /* assume all the iovs returned by client always fits in a page */
192 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE);
195 ap.pages = fuse_pages_alloc(fm->fc->max_pages, GFP_KERNEL, &ap.descs);
196 iov_page = (struct iovec *) __get_free_page(GFP_KERNEL);
197 if (!ap.pages || !iov_page)
200 fuse_page_descs_length_init(ap.descs, 0, fm->fc->max_pages);
203 * If restricted, initialize IO parameters as encoded in @cmd.
204 * RETRY from server is not allowed.
206 if (!(flags & FUSE_IOCTL_UNRESTRICTED)) {
207 struct iovec *iov = iov_page;
209 iov->iov_base = (void __user *)arg;
210 iov->iov_len = _IOC_SIZE(cmd);
212 if (_IOC_DIR(cmd) & _IOC_WRITE) {
217 if (_IOC_DIR(cmd) & _IOC_READ) {
224 inarg.in_size = in_size = iov_length(in_iov, in_iovs);
225 inarg.out_size = out_size = iov_length(out_iov, out_iovs);
228 * Out data can be used either for actual out data or iovs,
229 * make sure there always is at least one page.
231 out_size = max_t(size_t, out_size, PAGE_SIZE);
232 max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE);
234 /* make sure there are enough buffer pages and init request with them */
236 if (max_pages > fm->fc->max_pages)
238 while (ap.num_pages < max_pages) {
239 ap.pages[ap.num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
240 if (!ap.pages[ap.num_pages])
246 /* okay, let's send it to the client */
247 ap.args.opcode = FUSE_IOCTL;
248 ap.args.nodeid = ff->nodeid;
249 ap.args.in_numargs = 1;
250 ap.args.in_args[0].size = sizeof(inarg);
251 ap.args.in_args[0].value = &inarg;
253 ap.args.in_numargs++;
254 ap.args.in_args[1].size = in_size;
255 ap.args.in_pages = true;
258 iov_iter_init(&ii, WRITE, in_iov, in_iovs, in_size);
259 for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= ap.num_pages); i++) {
260 c = copy_page_from_iter(ap.pages[i], 0, PAGE_SIZE, &ii);
261 if (c != PAGE_SIZE && iov_iter_count(&ii))
266 ap.args.out_numargs = 2;
267 ap.args.out_args[0].size = sizeof(outarg);
268 ap.args.out_args[0].value = &outarg;
269 ap.args.out_args[1].size = out_size;
270 ap.args.out_pages = true;
271 ap.args.out_argvar = true;
273 transferred = fuse_send_ioctl(fm, &ap.args);
278 /* did it ask for retry? */
279 if (outarg.flags & FUSE_IOCTL_RETRY) {
282 /* no retry if in restricted mode */
284 if (!(flags & FUSE_IOCTL_UNRESTRICTED))
287 in_iovs = outarg.in_iovs;
288 out_iovs = outarg.out_iovs;
291 * Make sure things are in boundary, separate checks
292 * are to protect against overflow.
295 if (in_iovs > FUSE_IOCTL_MAX_IOV ||
296 out_iovs > FUSE_IOCTL_MAX_IOV ||
297 in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV)
300 vaddr = kmap_local_page(ap.pages[0]);
301 err = fuse_copy_ioctl_iovec(fm->fc, iov_page, vaddr,
302 transferred, in_iovs + out_iovs,
303 (flags & FUSE_IOCTL_COMPAT) != 0);
309 out_iov = in_iov + in_iovs;
311 err = fuse_verify_ioctl_iov(fm->fc, in_iov, in_iovs);
315 err = fuse_verify_ioctl_iov(fm->fc, out_iov, out_iovs);
323 if (transferred > inarg.out_size)
327 iov_iter_init(&ii, READ, out_iov, out_iovs, transferred);
328 for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= ap.num_pages); i++) {
329 c = copy_page_to_iter(ap.pages[i], 0, PAGE_SIZE, &ii);
330 if (c != PAGE_SIZE && iov_iter_count(&ii))
335 free_page((unsigned long) iov_page);
337 __free_page(ap.pages[--ap.num_pages]);
340 return err ? err : outarg.result;
342 EXPORT_SYMBOL_GPL(fuse_do_ioctl);
344 long fuse_ioctl_common(struct file *file, unsigned int cmd,
345 unsigned long arg, unsigned int flags)
347 struct inode *inode = file_inode(file);
348 struct fuse_conn *fc = get_fuse_conn(inode);
350 if (!fuse_allow_current_process(fc))
353 if (fuse_is_bad(inode))
356 return fuse_do_ioctl(file, cmd, arg, flags);
359 long fuse_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
361 return fuse_ioctl_common(file, cmd, arg, 0);
364 long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
367 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT);
370 static int fuse_priv_ioctl(struct inode *inode, struct fuse_file *ff,
371 unsigned int cmd, void *ptr, size_t size)
373 struct fuse_mount *fm = ff->fm;
374 struct fuse_ioctl_in inarg;
375 struct fuse_ioctl_out outarg;
379 memset(&inarg, 0, sizeof(inarg));
383 #if BITS_PER_LONG == 32
384 inarg.flags |= FUSE_IOCTL_32BIT;
386 if (S_ISDIR(inode->i_mode))
387 inarg.flags |= FUSE_IOCTL_DIR;
389 if (_IOC_DIR(cmd) & _IOC_READ)
390 inarg.out_size = size;
391 if (_IOC_DIR(cmd) & _IOC_WRITE)
392 inarg.in_size = size;
394 args.opcode = FUSE_IOCTL;
395 args.nodeid = ff->nodeid;
397 args.in_args[0].size = sizeof(inarg);
398 args.in_args[0].value = &inarg;
399 args.in_args[1].size = inarg.in_size;
400 args.in_args[1].value = ptr;
401 args.out_numargs = 2;
402 args.out_args[0].size = sizeof(outarg);
403 args.out_args[0].value = &outarg;
404 args.out_args[1].size = inarg.out_size;
405 args.out_args[1].value = ptr;
407 err = fuse_send_ioctl(fm, &args);
409 if (outarg.result < 0)
411 else if (outarg.flags & FUSE_IOCTL_RETRY)
417 static struct fuse_file *fuse_priv_ioctl_prepare(struct inode *inode)
419 struct fuse_mount *fm = get_fuse_mount(inode);
420 bool isdir = S_ISDIR(inode->i_mode);
422 if (!S_ISREG(inode->i_mode) && !isdir)
423 return ERR_PTR(-ENOTTY);
425 return fuse_file_open(fm, get_node_id(inode), O_RDONLY, isdir);
428 static void fuse_priv_ioctl_cleanup(struct inode *inode, struct fuse_file *ff)
430 fuse_file_release(inode, ff, O_RDONLY, NULL, S_ISDIR(inode->i_mode));
433 int fuse_fileattr_get(struct dentry *dentry, struct fileattr *fa)
435 struct inode *inode = d_inode(dentry);
436 struct fuse_file *ff;
441 ff = fuse_priv_ioctl_prepare(inode);
445 if (fa->flags_valid) {
446 err = fuse_priv_ioctl(inode, ff, FS_IOC_GETFLAGS,
447 &flags, sizeof(flags));
451 fileattr_fill_flags(fa, flags);
453 err = fuse_priv_ioctl(inode, ff, FS_IOC_FSGETXATTR,
458 fileattr_fill_xflags(fa, xfa.fsx_xflags);
459 fa->fsx_extsize = xfa.fsx_extsize;
460 fa->fsx_nextents = xfa.fsx_nextents;
461 fa->fsx_projid = xfa.fsx_projid;
462 fa->fsx_cowextsize = xfa.fsx_cowextsize;
465 fuse_priv_ioctl_cleanup(inode, ff);
470 int fuse_fileattr_set(struct user_namespace *mnt_userns,
471 struct dentry *dentry, struct fileattr *fa)
473 struct inode *inode = d_inode(dentry);
474 struct fuse_file *ff;
475 unsigned int flags = fa->flags;
479 ff = fuse_priv_ioctl_prepare(inode);
483 if (fa->flags_valid) {
484 err = fuse_priv_ioctl(inode, ff, FS_IOC_SETFLAGS,
485 &flags, sizeof(flags));
489 memset(&xfa, 0, sizeof(xfa));
490 xfa.fsx_xflags = fa->fsx_xflags;
491 xfa.fsx_extsize = fa->fsx_extsize;
492 xfa.fsx_nextents = fa->fsx_nextents;
493 xfa.fsx_projid = fa->fsx_projid;
494 xfa.fsx_cowextsize = fa->fsx_cowextsize;
496 err = fuse_priv_ioctl(inode, ff, FS_IOC_FSSETXATTR,
501 fuse_priv_ioctl_cleanup(inode, ff);