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 #include <linux/fsverity.h> 12 13 #include <linux/slab.h> 14 #define FUSE_VERITY_ENABLE_ARG_MAX_PAGES 256 15 16 static ssize_t fuse_send_ioctl(struct fuse_mount *fm, struct fuse_args *args, 17 struct fuse_ioctl_out *outarg) 18 { 19 ssize_t ret; 20 21 args->out_args[0].size = sizeof(*outarg); 22 args->out_args[0].value = outarg; 23 24 ret = fuse_simple_request(fm, args); 25 26 /* Translate ENOSYS, which shouldn't be returned from fs */ 27 if (ret == -ENOSYS) 28 ret = -ENOTTY; 29 30 if (ret >= 0 && outarg->result == -ENOSYS) 31 outarg->result = -ENOTTY; 32 33 return ret; 34 } 35 36 /* 37 * CUSE servers compiled on 32bit broke on 64bit kernels because the 38 * ABI was defined to be 'struct iovec' which is different on 32bit 39 * and 64bit. Fortunately we can determine which structure the server 40 * used from the size of the reply. 41 */ 42 static int fuse_copy_ioctl_iovec_old(struct iovec *dst, void *src, 43 size_t transferred, unsigned count, 44 bool is_compat) 45 { 46 #ifdef CONFIG_COMPAT 47 if (count * sizeof(struct compat_iovec) == transferred) { 48 struct compat_iovec *ciov = src; 49 unsigned i; 50 51 /* 52 * With this interface a 32bit server cannot support 53 * non-compat (i.e. ones coming from 64bit apps) ioctl 54 * requests 55 */ 56 if (!is_compat) 57 return -EINVAL; 58 59 for (i = 0; i < count; i++) { 60 dst[i].iov_base = compat_ptr(ciov[i].iov_base); 61 dst[i].iov_len = ciov[i].iov_len; 62 } 63 return 0; 64 } 65 #endif 66 67 if (count * sizeof(struct iovec) != transferred) 68 return -EIO; 69 70 memcpy(dst, src, transferred); 71 return 0; 72 } 73 74 /* Make sure iov_length() won't overflow */ 75 static int fuse_verify_ioctl_iov(struct fuse_conn *fc, struct iovec *iov, 76 size_t count) 77 { 78 size_t n; 79 u32 max = fc->max_pages << PAGE_SHIFT; 80 81 for (n = 0; n < count; n++, iov++) { 82 if (iov->iov_len > (size_t) max) 83 return -ENOMEM; 84 max -= iov->iov_len; 85 } 86 return 0; 87 } 88 89 static int fuse_copy_ioctl_iovec(struct fuse_conn *fc, struct iovec *dst, 90 void *src, size_t transferred, unsigned count, 91 bool is_compat) 92 { 93 unsigned i; 94 struct fuse_ioctl_iovec *fiov = src; 95 96 if (fc->minor < 16) { 97 return fuse_copy_ioctl_iovec_old(dst, src, transferred, 98 count, is_compat); 99 } 100 101 if (count * sizeof(struct fuse_ioctl_iovec) != transferred) 102 return -EIO; 103 104 for (i = 0; i < count; i++) { 105 /* Did the server supply an inappropriate value? */ 106 if (fiov[i].base != (unsigned long) fiov[i].base || 107 fiov[i].len != (unsigned long) fiov[i].len) 108 return -EIO; 109 110 dst[i].iov_base = (void __user *) (unsigned long) fiov[i].base; 111 dst[i].iov_len = (size_t) fiov[i].len; 112 113 #ifdef CONFIG_COMPAT 114 if (is_compat && 115 (ptr_to_compat(dst[i].iov_base) != fiov[i].base || 116 (compat_size_t) dst[i].iov_len != fiov[i].len)) 117 return -EIO; 118 #endif 119 } 120 121 return 0; 122 } 123 124 /* For fs-verity, determine iov lengths from input */ 125 static int fuse_setup_measure_verity(unsigned long arg, struct iovec *iov) 126 { 127 __u16 digest_size; 128 struct fsverity_digest __user *uarg = (void __user *)arg; 129 130 if (copy_from_user(&digest_size, &uarg->digest_size, sizeof(digest_size))) 131 return -EFAULT; 132 133 iov->iov_len = sizeof(struct fsverity_digest) + digest_size; 134 135 return 0; 136 } 137 138 static int fuse_setup_enable_verity(unsigned long arg, struct iovec *iov, 139 unsigned int *in_iovs) 140 { 141 struct fsverity_enable_arg enable; 142 struct fsverity_enable_arg __user *uarg = (void __user *)arg; 143 const __u32 max_buffer_len = FUSE_VERITY_ENABLE_ARG_MAX_PAGES * PAGE_SIZE; 144 145 if (copy_from_user(&enable, uarg, sizeof(enable))) 146 return -EFAULT; 147 148 if (enable.salt_size > max_buffer_len || enable.sig_size > max_buffer_len) 149 return -ENOMEM; 150 151 if (enable.salt_size > 0) { 152 iov++; 153 (*in_iovs)++; 154 155 iov->iov_base = u64_to_user_ptr(enable.salt_ptr); 156 iov->iov_len = enable.salt_size; 157 } 158 159 if (enable.sig_size > 0) { 160 iov++; 161 (*in_iovs)++; 162 163 iov->iov_base = u64_to_user_ptr(enable.sig_ptr); 164 iov->iov_len = enable.sig_size; 165 } 166 return 0; 167 } 168 169 /* 170 * For ioctls, there is no generic way to determine how much memory 171 * needs to be read and/or written. Furthermore, ioctls are allowed 172 * to dereference the passed pointer, so the parameter requires deep 173 * copying but FUSE has no idea whatsoever about what to copy in or 174 * out. 175 * 176 * This is solved by allowing FUSE server to retry ioctl with 177 * necessary in/out iovecs. Let's assume the ioctl implementation 178 * needs to read in the following structure. 179 * 180 * struct a { 181 * char *buf; 182 * size_t buflen; 183 * } 184 * 185 * On the first callout to FUSE server, inarg->in_size and 186 * inarg->out_size will be NULL; then, the server completes the ioctl 187 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and 188 * the actual iov array to 189 * 190 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } } 191 * 192 * which tells FUSE to copy in the requested area and retry the ioctl. 193 * On the second round, the server has access to the structure and 194 * from that it can tell what to look for next, so on the invocation, 195 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to 196 * 197 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) }, 198 * { .iov_base = a.buf, .iov_len = a.buflen } } 199 * 200 * FUSE will copy both struct a and the pointed buffer from the 201 * process doing the ioctl and retry ioctl with both struct a and the 202 * buffer. 203 * 204 * This time, FUSE server has everything it needs and completes ioctl 205 * without FUSE_IOCTL_RETRY which finishes the ioctl call. 206 * 207 * Copying data out works the same way. 208 * 209 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel 210 * automatically initializes in and out iovs by decoding @cmd with 211 * _IOC_* macros and the server is not allowed to request RETRY. This 212 * limits ioctl data transfers to well-formed ioctls and is the forced 213 * behavior for all FUSE servers. 214 */ 215 long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg, 216 unsigned int flags) 217 { 218 struct fuse_file *ff = file->private_data; 219 struct fuse_mount *fm = ff->fm; 220 struct fuse_ioctl_in inarg = { 221 .fh = ff->fh, 222 .cmd = cmd, 223 .arg = arg, 224 .flags = flags 225 }; 226 struct fuse_ioctl_out outarg; 227 struct iovec *iov_page = NULL; 228 struct iovec *in_iov = NULL, *out_iov = NULL; 229 unsigned int in_iovs = 0, out_iovs = 0, max_pages; 230 size_t in_size, out_size, c; 231 ssize_t transferred; 232 int err, i; 233 struct iov_iter ii; 234 struct fuse_args_pages ap = {}; 235 236 #if BITS_PER_LONG == 32 237 inarg.flags |= FUSE_IOCTL_32BIT; 238 #else 239 if (flags & FUSE_IOCTL_COMPAT) { 240 inarg.flags |= FUSE_IOCTL_32BIT; 241 #ifdef CONFIG_X86_X32_ABI 242 if (in_x32_syscall()) 243 inarg.flags |= FUSE_IOCTL_COMPAT_X32; 244 #endif 245 } 246 #endif 247 248 /* assume all the iovs returned by client always fits in a page */ 249 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE); 250 251 err = -ENOMEM; 252 ap.folios = fuse_folios_alloc(fm->fc->max_pages, GFP_KERNEL, &ap.descs); 253 iov_page = kmalloc(PAGE_SIZE, GFP_KERNEL); 254 if (!ap.folios || !iov_page) 255 goto out; 256 257 fuse_folio_descs_length_init(ap.descs, 0, fm->fc->max_pages); 258 259 /* 260 * If restricted, initialize IO parameters as encoded in @cmd. 261 * RETRY from server is not allowed. 262 */ 263 if (!(flags & FUSE_IOCTL_UNRESTRICTED)) { 264 struct iovec *iov = iov_page; 265 266 iov->iov_base = (void __user *)arg; 267 iov->iov_len = _IOC_SIZE(cmd); 268 269 if (_IOC_DIR(cmd) & _IOC_WRITE) { 270 in_iov = iov; 271 in_iovs = 1; 272 } 273 274 if (_IOC_DIR(cmd) & _IOC_READ) { 275 out_iov = iov; 276 out_iovs = 1; 277 } 278 279 err = 0; 280 switch (cmd) { 281 case FS_IOC_MEASURE_VERITY: 282 err = fuse_setup_measure_verity(arg, iov); 283 break; 284 case FS_IOC_ENABLE_VERITY: 285 err = fuse_setup_enable_verity(arg, iov, &in_iovs); 286 break; 287 } 288 if (err) 289 goto out; 290 } 291 292 retry: 293 inarg.in_size = in_size = iov_length(in_iov, in_iovs); 294 inarg.out_size = out_size = iov_length(out_iov, out_iovs); 295 296 /* 297 * Out data can be used either for actual out data or iovs, 298 * make sure there always is at least one page. 299 */ 300 out_size = max_t(size_t, out_size, PAGE_SIZE); 301 max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE); 302 303 /* make sure there are enough buffer pages and init request with them */ 304 err = -ENOMEM; 305 if (max_pages > fm->fc->max_pages) 306 goto out; 307 while (ap.num_folios < max_pages) { 308 ap.folios[ap.num_folios] = folio_alloc(GFP_KERNEL | __GFP_HIGHMEM, 0); 309 if (!ap.folios[ap.num_folios]) 310 goto out; 311 ap.num_folios++; 312 } 313 314 /* okay, let's send it to the client */ 315 ap.args.opcode = FUSE_IOCTL; 316 ap.args.nodeid = ff->nodeid; 317 ap.args.in_numargs = 1; 318 ap.args.in_args[0].size = sizeof(inarg); 319 ap.args.in_args[0].value = &inarg; 320 if (in_size) { 321 ap.args.in_numargs++; 322 ap.args.in_args[1].size = in_size; 323 ap.args.in_pages = true; 324 325 err = -EFAULT; 326 iov_iter_init(&ii, ITER_SOURCE, in_iov, in_iovs, in_size); 327 for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= ap.num_folios); i++) { 328 c = copy_folio_from_iter(ap.folios[i], 0, PAGE_SIZE, &ii); 329 if (c != PAGE_SIZE && iov_iter_count(&ii)) 330 goto out; 331 } 332 } 333 334 ap.args.out_numargs = 2; 335 ap.args.out_args[1].size = out_size; 336 ap.args.out_pages = true; 337 ap.args.out_argvar = true; 338 339 transferred = fuse_send_ioctl(fm, &ap.args, &outarg); 340 err = transferred; 341 if (transferred < 0) 342 goto out; 343 344 /* did it ask for retry? */ 345 if (outarg.flags & FUSE_IOCTL_RETRY) { 346 void *vaddr; 347 348 /* no retry if in restricted mode */ 349 err = -EIO; 350 if (!(flags & FUSE_IOCTL_UNRESTRICTED)) 351 goto out; 352 353 in_iovs = outarg.in_iovs; 354 out_iovs = outarg.out_iovs; 355 356 /* 357 * Make sure things are in boundary, separate checks 358 * are to protect against overflow. 359 */ 360 err = -ENOMEM; 361 if (in_iovs > FUSE_IOCTL_MAX_IOV || 362 out_iovs > FUSE_IOCTL_MAX_IOV || 363 in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV) 364 goto out; 365 366 vaddr = kmap_local_folio(ap.folios[0], 0); 367 err = fuse_copy_ioctl_iovec(fm->fc, iov_page, vaddr, 368 transferred, in_iovs + out_iovs, 369 (flags & FUSE_IOCTL_COMPAT) != 0); 370 kunmap_local(vaddr); 371 if (err) 372 goto out; 373 374 in_iov = iov_page; 375 out_iov = in_iov + in_iovs; 376 377 err = fuse_verify_ioctl_iov(fm->fc, in_iov, in_iovs); 378 if (err) 379 goto out; 380 381 err = fuse_verify_ioctl_iov(fm->fc, out_iov, out_iovs); 382 if (err) 383 goto out; 384 385 goto retry; 386 } 387 388 err = -EIO; 389 if (transferred > inarg.out_size) 390 goto out; 391 392 err = -EFAULT; 393 iov_iter_init(&ii, ITER_DEST, out_iov, out_iovs, transferred); 394 for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= ap.num_folios); i++) { 395 c = copy_folio_to_iter(ap.folios[i], 0, PAGE_SIZE, &ii); 396 if (c != PAGE_SIZE && iov_iter_count(&ii)) 397 goto out; 398 } 399 err = 0; 400 out: 401 kfree(iov_page); 402 while (ap.num_folios) 403 folio_put(ap.folios[--ap.num_folios]); 404 kfree(ap.folios); 405 406 return err ? err : outarg.result; 407 } 408 EXPORT_SYMBOL_GPL(fuse_do_ioctl); 409 410 long fuse_ioctl_common(struct file *file, unsigned int cmd, 411 unsigned long arg, unsigned int flags) 412 { 413 struct inode *inode = file_inode(file); 414 struct fuse_conn *fc = get_fuse_conn(inode); 415 416 if (!fuse_allow_current_process(fc)) 417 return -EACCES; 418 419 if (fuse_is_bad(inode)) 420 return -EIO; 421 422 return fuse_do_ioctl(file, cmd, arg, flags); 423 } 424 425 long fuse_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 426 { 427 return fuse_ioctl_common(file, cmd, arg, 0); 428 } 429 430 long fuse_file_compat_ioctl(struct file *file, unsigned int cmd, 431 unsigned long arg) 432 { 433 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT); 434 } 435 436 static int fuse_priv_ioctl(struct inode *inode, struct fuse_file *ff, 437 unsigned int cmd, void *ptr, size_t size) 438 { 439 struct fuse_mount *fm = ff->fm; 440 struct fuse_ioctl_in inarg; 441 struct fuse_ioctl_out outarg; 442 FUSE_ARGS(args); 443 int err; 444 445 memset(&inarg, 0, sizeof(inarg)); 446 inarg.fh = ff->fh; 447 inarg.cmd = cmd; 448 449 #if BITS_PER_LONG == 32 450 inarg.flags |= FUSE_IOCTL_32BIT; 451 #endif 452 if (S_ISDIR(inode->i_mode)) 453 inarg.flags |= FUSE_IOCTL_DIR; 454 455 if (_IOC_DIR(cmd) & _IOC_READ) 456 inarg.out_size = size; 457 if (_IOC_DIR(cmd) & _IOC_WRITE) 458 inarg.in_size = size; 459 460 args.opcode = FUSE_IOCTL; 461 args.nodeid = ff->nodeid; 462 args.in_numargs = 2; 463 args.in_args[0].size = sizeof(inarg); 464 args.in_args[0].value = &inarg; 465 args.in_args[1].size = inarg.in_size; 466 args.in_args[1].value = ptr; 467 args.out_numargs = 2; 468 args.out_args[1].size = inarg.out_size; 469 args.out_args[1].value = ptr; 470 471 err = fuse_send_ioctl(fm, &args, &outarg); 472 if (!err) { 473 if (outarg.result < 0) 474 err = outarg.result; 475 else if (outarg.flags & FUSE_IOCTL_RETRY) 476 err = -EIO; 477 } 478 return err; 479 } 480 481 static struct fuse_file *fuse_priv_ioctl_prepare(struct inode *inode) 482 { 483 struct fuse_mount *fm = get_fuse_mount(inode); 484 bool isdir = S_ISDIR(inode->i_mode); 485 486 if (!fuse_allow_current_process(fm->fc)) 487 return ERR_PTR(-EACCES); 488 489 if (fuse_is_bad(inode)) 490 return ERR_PTR(-EIO); 491 492 if (!S_ISREG(inode->i_mode) && !isdir) 493 return ERR_PTR(-ENOTTY); 494 495 return fuse_file_open(fm, get_node_id(inode), O_RDONLY, isdir); 496 } 497 498 static void fuse_priv_ioctl_cleanup(struct inode *inode, struct fuse_file *ff) 499 { 500 fuse_file_release(inode, ff, O_RDONLY, NULL, S_ISDIR(inode->i_mode)); 501 } 502 503 int fuse_fileattr_get(struct dentry *dentry, struct file_kattr *fa) 504 { 505 struct inode *inode = d_inode(dentry); 506 struct fuse_file *ff; 507 unsigned int flags; 508 struct fsxattr xfa; 509 int err; 510 511 ff = fuse_priv_ioctl_prepare(inode); 512 if (IS_ERR(ff)) 513 return PTR_ERR(ff); 514 515 if (fa->flags_valid) { 516 err = fuse_priv_ioctl(inode, ff, FS_IOC_GETFLAGS, 517 &flags, sizeof(flags)); 518 if (err) 519 goto cleanup; 520 521 fileattr_fill_flags(fa, flags); 522 } else { 523 err = fuse_priv_ioctl(inode, ff, FS_IOC_FSGETXATTR, 524 &xfa, sizeof(xfa)); 525 if (err) 526 goto cleanup; 527 528 fileattr_fill_xflags(fa, xfa.fsx_xflags); 529 fa->fsx_extsize = xfa.fsx_extsize; 530 fa->fsx_nextents = xfa.fsx_nextents; 531 fa->fsx_projid = xfa.fsx_projid; 532 fa->fsx_cowextsize = xfa.fsx_cowextsize; 533 } 534 cleanup: 535 fuse_priv_ioctl_cleanup(inode, ff); 536 537 return err; 538 } 539 540 int fuse_fileattr_set(const struct mnt_idmap *idmap, 541 struct dentry *dentry, struct file_kattr *fa) 542 { 543 struct inode *inode = d_inode(dentry); 544 struct fuse_file *ff; 545 unsigned int flags = fa->flags; 546 struct fsxattr xfa; 547 int err; 548 549 ff = fuse_priv_ioctl_prepare(inode); 550 if (IS_ERR(ff)) 551 return PTR_ERR(ff); 552 553 if (fa->flags_valid) { 554 err = fuse_priv_ioctl(inode, ff, FS_IOC_SETFLAGS, 555 &flags, sizeof(flags)); 556 if (err) 557 goto cleanup; 558 } else { 559 memset(&xfa, 0, sizeof(xfa)); 560 xfa.fsx_xflags = fa->fsx_xflags; 561 xfa.fsx_extsize = fa->fsx_extsize; 562 xfa.fsx_nextents = fa->fsx_nextents; 563 xfa.fsx_projid = fa->fsx_projid; 564 xfa.fsx_cowextsize = fa->fsx_cowextsize; 565 566 err = fuse_priv_ioctl(inode, ff, FS_IOC_FSSETXATTR, 567 &xfa, sizeof(xfa)); 568 } 569 570 cleanup: 571 fuse_priv_ioctl_cleanup(inode, ff); 572 573 return err; 574 } 575