Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 | // SPDX-License-Identifier: GPL-2.0-or-later #include <linux/fdtable.h> #include <linux/anon_inodes.h> #include <linux/uio.h> #include "internal.h" struct ondemand_anon_file { struct file *file; int fd; }; static inline void cachefiles_req_put(struct cachefiles_req *req) { if (refcount_dec_and_test(&req->ref)) kfree(req); } static int cachefiles_ondemand_fd_release(struct inode *inode, struct file *file) { struct cachefiles_object *object = file->private_data; struct cachefiles_cache *cache; struct cachefiles_ondemand_info *info; int object_id; struct cachefiles_req *req; XA_STATE(xas, NULL, 0); if (!object) return 0; info = object->ondemand; cache = object->volume->cache; xas.xa = &cache->reqs; xa_lock(&cache->reqs); spin_lock(&info->lock); object_id = info->ondemand_id; info->ondemand_id = CACHEFILES_ONDEMAND_ID_CLOSED; cachefiles_ondemand_set_object_close(object); spin_unlock(&info->lock); /* Only flush CACHEFILES_REQ_NEW marked req to avoid race with daemon_read */ xas_for_each_marked(&xas, req, ULONG_MAX, CACHEFILES_REQ_NEW) { if (req->msg.object_id == object_id && req->msg.opcode == CACHEFILES_OP_CLOSE) { complete(&req->done); xas_store(&xas, NULL); } } xa_unlock(&cache->reqs); xa_erase(&cache->ondemand_ids, object_id); trace_cachefiles_ondemand_fd_release(object, object_id); cachefiles_put_object(object, cachefiles_obj_put_ondemand_fd); cachefiles_put_unbind_pincount(cache); return 0; } static ssize_t cachefiles_ondemand_fd_write_iter(struct kiocb *kiocb, struct iov_iter *iter) { struct cachefiles_object *object = kiocb->ki_filp->private_data; struct cachefiles_cache *cache = object->volume->cache; struct file *file = object->file; size_t len = iter->count; loff_t pos = kiocb->ki_pos; const struct cred *saved_cred; int ret; if (!file) return -ENOBUFS; cachefiles_begin_secure(cache, &saved_cred); ret = __cachefiles_prepare_write(object, file, &pos, &len, true); cachefiles_end_secure(cache, saved_cred); if (ret < 0) return ret; trace_cachefiles_ondemand_fd_write(object, file_inode(file), pos, len); ret = __cachefiles_write(object, file, pos, iter, NULL, NULL); if (!ret) ret = len; return ret; } static loff_t cachefiles_ondemand_fd_llseek(struct file *filp, loff_t pos, int whence) { struct cachefiles_object *object = filp->private_data; struct file *file = object->file; if (!file) return -ENOBUFS; return vfs_llseek(file, pos, whence); } static long cachefiles_ondemand_fd_ioctl(struct file *filp, unsigned int ioctl, unsigned long id) { struct cachefiles_object *object = filp->private_data; struct cachefiles_cache *cache = object->volume->cache; struct cachefiles_req *req; XA_STATE(xas, &cache->reqs, id); if (ioctl != CACHEFILES_IOC_READ_COMPLETE) return -EINVAL; if (!test_bit(CACHEFILES_ONDEMAND_MODE, &cache->flags)) return -EOPNOTSUPP; xa_lock(&cache->reqs); req = xas_load(&xas); if (!req || req->msg.opcode != CACHEFILES_OP_READ || req->object != object) { xa_unlock(&cache->reqs); return -EINVAL; } xas_store(&xas, NULL); xa_unlock(&cache->reqs); trace_cachefiles_ondemand_cread(object, id); complete(&req->done); return 0; } static const struct file_operations cachefiles_ondemand_fd_fops = { .owner = THIS_MODULE, .release = cachefiles_ondemand_fd_release, .write_iter = cachefiles_ondemand_fd_write_iter, .llseek = cachefiles_ondemand_fd_llseek, .unlocked_ioctl = cachefiles_ondemand_fd_ioctl, }; /* * OPEN request Completion (copen) * - command: "copen <id>,<cache_size>" * <cache_size> indicates the object size if >=0, error code if negative */ int cachefiles_ondemand_copen(struct cachefiles_cache *cache, char *args) { struct cachefiles_req *req; struct fscache_cookie *cookie; struct cachefiles_ondemand_info *info; char *pid, *psize; unsigned long id; long size; int ret; XA_STATE(xas, &cache->reqs, 0); if (!test_bit(CACHEFILES_ONDEMAND_MODE, &cache->flags)) return -EOPNOTSUPP; if (!*args) { pr_err("Empty id specified\n"); return -EINVAL; } pid = args; psize = strchr(args, ','); if (!psize) { pr_err("Cache size is not specified\n"); return -EINVAL; } *psize = 0; psize++; ret = kstrtoul(pid, 0, &id); if (ret) return ret; xa_lock(&cache->reqs); xas.xa_index = id; req = xas_load(&xas); if (!req || req->msg.opcode != CACHEFILES_OP_OPEN || !req->object->ondemand->ondemand_id) { xa_unlock(&cache->reqs); return -EINVAL; } xas_store(&xas, NULL); xa_unlock(&cache->reqs); info = req->object->ondemand; /* fail OPEN request if copen format is invalid */ ret = kstrtol(psize, 0, &size); if (ret) { req->error = ret; goto out; } /* fail OPEN request if daemon reports an error */ if (size < 0) { if (!IS_ERR_VALUE(size)) { req->error = -EINVAL; ret = -EINVAL; } else { req->error = size; ret = 0; } goto out; } spin_lock(&info->lock); /* * The anonymous fd was closed before copen ? Fail the request. * * t1 | t2 * --------------------------------------------------------- * cachefiles_ondemand_copen * req = xa_erase(&cache->reqs, id) * // Anon fd is maliciously closed. * cachefiles_ondemand_fd_release * xa_lock(&cache->reqs) * cachefiles_ondemand_set_object_close(object) * xa_unlock(&cache->reqs) * cachefiles_ondemand_set_object_open * // No one will ever close it again. * cachefiles_ondemand_daemon_read * cachefiles_ondemand_select_req * * Get a read req but its fd is already closed. The daemon can't * issue a cread ioctl with an closed fd, then hung. */ if (info->ondemand_id == CACHEFILES_ONDEMAND_ID_CLOSED) { spin_unlock(&info->lock); req->error = -EBADFD; goto out; } cookie = req->object->cookie; cookie->object_size = size; if (size) clear_bit(FSCACHE_COOKIE_NO_DATA_TO_READ, &cookie->flags); else set_bit(FSCACHE_COOKIE_NO_DATA_TO_READ, &cookie->flags); trace_cachefiles_ondemand_copen(req->object, id, size); cachefiles_ondemand_set_object_open(req->object); spin_unlock(&info->lock); wake_up_all(&cache->daemon_pollwq); out: spin_lock(&info->lock); /* Need to set object close to avoid reopen status continuing */ if (info->ondemand_id == CACHEFILES_ONDEMAND_ID_CLOSED) cachefiles_ondemand_set_object_close(req->object); spin_unlock(&info->lock); complete(&req->done); return ret; } int cachefiles_ondemand_restore(struct cachefiles_cache *cache, char *args) { struct cachefiles_req *req; XA_STATE(xas, &cache->reqs, 0); if (!test_bit(CACHEFILES_ONDEMAND_MODE, &cache->flags)) return -EOPNOTSUPP; /* * Reset the requests to CACHEFILES_REQ_NEW state, so that the * requests have been processed halfway before the crash of the * user daemon could be reprocessed after the recovery. */ xas_lock(&xas); xas_for_each(&xas, req, ULONG_MAX) xas_set_mark(&xas, CACHEFILES_REQ_NEW); xas_unlock(&xas); wake_up_all(&cache->daemon_pollwq); return 0; } static int cachefiles_ondemand_get_fd(struct cachefiles_req *req, struct ondemand_anon_file *anon_file) { struct cachefiles_object *object; struct cachefiles_cache *cache; struct cachefiles_open *load; u32 object_id; int ret; object = cachefiles_grab_object(req->object, cachefiles_obj_get_ondemand_fd); cache = object->volume->cache; ret = xa_alloc_cyclic(&cache->ondemand_ids, &object_id, NULL, XA_LIMIT(1, INT_MAX), &cache->ondemand_id_next, GFP_KERNEL); if (ret < 0) goto err; anon_file->fd = get_unused_fd_flags(O_WRONLY); if (anon_file->fd < 0) { ret = anon_file->fd; goto err_free_id; } anon_file->file = anon_inode_getfile("[cachefiles]", &cachefiles_ondemand_fd_fops, object, O_WRONLY); if (IS_ERR(anon_file->file)) { ret = PTR_ERR(anon_file->file); goto err_put_fd; } spin_lock(&object->ondemand->lock); if (object->ondemand->ondemand_id > 0) { spin_unlock(&object->ondemand->lock); /* Pair with check in cachefiles_ondemand_fd_release(). */ anon_file->file->private_data = NULL; ret = -EEXIST; goto err_put_file; } anon_file->file->f_mode |= FMODE_PWRITE | FMODE_LSEEK; load = (void *)req->msg.data; load->fd = anon_file->fd; object->ondemand->ondemand_id = object_id; spin_unlock(&object->ondemand->lock); cachefiles_get_unbind_pincount(cache); trace_cachefiles_ondemand_open(object, &req->msg, load); return 0; err_put_file: fput(anon_file->file); anon_file->file = NULL; err_put_fd: put_unused_fd(anon_file->fd); anon_file->fd = ret; err_free_id: xa_erase(&cache->ondemand_ids, object_id); err: spin_lock(&object->ondemand->lock); /* Avoid marking an opened object as closed. */ if (object->ondemand->ondemand_id <= 0) cachefiles_ondemand_set_object_close(object); spin_unlock(&object->ondemand->lock); cachefiles_put_object(object, cachefiles_obj_put_ondemand_fd); return ret; } static void ondemand_object_worker(struct work_struct *work) { struct cachefiles_ondemand_info *info = container_of(work, struct cachefiles_ondemand_info, ondemand_work); cachefiles_ondemand_init_object(info->object); } /* * If there are any inflight or subsequent READ requests on the * closed object, reopen it. * Skip read requests whose related object is reopening. */ static struct cachefiles_req *cachefiles_ondemand_select_req(struct xa_state *xas, unsigned long xa_max) { struct cachefiles_req *req; struct cachefiles_object *object; struct cachefiles_ondemand_info *info; xas_for_each_marked(xas, req, xa_max, CACHEFILES_REQ_NEW) { if (req->msg.opcode != CACHEFILES_OP_READ) return req; object = req->object; info = object->ondemand; if (cachefiles_ondemand_object_is_close(object)) { cachefiles_ondemand_set_object_reopening(object); queue_work(fscache_wq, &info->ondemand_work); continue; } if (cachefiles_ondemand_object_is_reopening(object)) continue; return req; } return NULL; } static inline bool cachefiles_ondemand_finish_req(struct cachefiles_req *req, struct xa_state *xas, int err) { if (unlikely(!xas || !req)) return false; if (xa_cmpxchg(xas->xa, xas->xa_index, req, NULL, 0) != req) return false; req->error = err; complete(&req->done); return true; } ssize_t cachefiles_ondemand_daemon_read(struct cachefiles_cache *cache, char __user *_buffer, size_t buflen) { struct cachefiles_req *req; struct cachefiles_msg *msg; size_t n; int ret = 0; struct ondemand_anon_file anon_file; XA_STATE(xas, &cache->reqs, cache->req_id_next); xa_lock(&cache->reqs); /* * Cyclically search for a request that has not ever been processed, * to prevent requests from being processed repeatedly, and make * request distribution fair. */ req = cachefiles_ondemand_select_req(&xas, ULONG_MAX); if (!req && cache->req_id_next > 0) { xas_set(&xas, 0); req = cachefiles_ondemand_select_req(&xas, cache->req_id_next - 1); } if (!req) { xa_unlock(&cache->reqs); return 0; } msg = &req->msg; n = msg->len; if (n > buflen) { xa_unlock(&cache->reqs); return -EMSGSIZE; } xas_clear_mark(&xas, CACHEFILES_REQ_NEW); cache->req_id_next = xas.xa_index + 1; refcount_inc(&req->ref); cachefiles_grab_object(req->object, cachefiles_obj_get_read_req); xa_unlock(&cache->reqs); if (msg->opcode == CACHEFILES_OP_OPEN) { ret = cachefiles_ondemand_get_fd(req, &anon_file); if (ret) goto out; } msg->msg_id = xas.xa_index; msg->object_id = req->object->ondemand->ondemand_id; if (copy_to_user(_buffer, msg, n) != 0) ret = -EFAULT; if (msg->opcode == CACHEFILES_OP_OPEN) { if (ret < 0) { fput(anon_file.file); put_unused_fd(anon_file.fd); goto out; } fd_install(anon_file.fd, anon_file.file); } out: cachefiles_put_object(req->object, cachefiles_obj_put_read_req); /* Remove error request and CLOSE request has no reply */ if (ret || msg->opcode == CACHEFILES_OP_CLOSE) cachefiles_ondemand_finish_req(req, &xas, ret); cachefiles_req_put(req); return ret ? ret : n; } typedef int (*init_req_fn)(struct cachefiles_req *req, void *private); static int cachefiles_ondemand_send_req(struct cachefiles_object *object, enum cachefiles_opcode opcode, size_t data_len, init_req_fn init_req, void *private) { struct cachefiles_cache *cache = object->volume->cache; struct cachefiles_req *req = NULL; XA_STATE(xas, &cache->reqs, 0); int ret; if (!test_bit(CACHEFILES_ONDEMAND_MODE, &cache->flags)) return 0; if (test_bit(CACHEFILES_DEAD, &cache->flags)) { ret = -EIO; goto out; } req = kzalloc(sizeof(*req) + data_len, GFP_KERNEL); if (!req) { ret = -ENOMEM; goto out; } refcount_set(&req->ref, 1); req->object = object; init_completion(&req->done); req->msg.opcode = opcode; req->msg.len = sizeof(struct cachefiles_msg) + data_len; ret = init_req(req, private); if (ret) goto out; do { /* * Stop enqueuing the request when daemon is dying. The * following two operations need to be atomic as a whole. * 1) check cache state, and * 2) enqueue request if cache is alive. * Otherwise the request may be enqueued after xarray has been * flushed, leaving the orphan request never being completed. * * CPU 1 CPU 2 * ===== ===== * test CACHEFILES_DEAD bit * set CACHEFILES_DEAD bit * flush requests in the xarray * enqueue the request */ xas_lock(&xas); if (test_bit(CACHEFILES_DEAD, &cache->flags) || cachefiles_ondemand_object_is_dropping(object)) { xas_unlock(&xas); ret = -EIO; goto out; } /* coupled with the barrier in cachefiles_flush_reqs() */ smp_mb(); if (opcode == CACHEFILES_OP_CLOSE && !cachefiles_ondemand_object_is_open(object)) { WARN_ON_ONCE(object->ondemand->ondemand_id == 0); xas_unlock(&xas); ret = -EIO; goto out; } /* * Cyclically find a free xas to avoid msg_id reuse that would * cause the daemon to successfully copen a stale msg_id. */ xas.xa_index = cache->msg_id_next; xas_find_marked(&xas, UINT_MAX, XA_FREE_MARK); if (xas.xa_node == XAS_RESTART) { xas.xa_index = 0; xas_find_marked(&xas, cache->msg_id_next - 1, XA_FREE_MARK); } if (xas.xa_node == XAS_RESTART) xas_set_err(&xas, -EBUSY); xas_store(&xas, req); if (xas_valid(&xas)) { cache->msg_id_next = xas.xa_index + 1; xas_clear_mark(&xas, XA_FREE_MARK); xas_set_mark(&xas, CACHEFILES_REQ_NEW); } xas_unlock(&xas); } while (xas_nomem(&xas, GFP_KERNEL)); ret = xas_error(&xas); if (ret) goto out; wake_up_all(&cache->daemon_pollwq); wait: ret = wait_for_completion_killable(&req->done); if (!ret) { ret = req->error; } else { ret = -EINTR; if (!cachefiles_ondemand_finish_req(req, &xas, ret)) { /* Someone will complete it soon. */ cpu_relax(); goto wait; } } cachefiles_req_put(req); return ret; out: /* Reset the object to close state in error handling path. * If error occurs after creating the anonymous fd, * cachefiles_ondemand_fd_release() will set object to close. */ if (opcode == CACHEFILES_OP_OPEN && !cachefiles_ondemand_object_is_dropping(object)) cachefiles_ondemand_set_object_close(object); kfree(req); return ret; } static int cachefiles_ondemand_init_open_req(struct cachefiles_req *req, void *private) { struct cachefiles_object *object = req->object; struct fscache_cookie *cookie = object->cookie; struct fscache_volume *volume = object->volume->vcookie; struct cachefiles_open *load = (void *)req->msg.data; size_t volume_key_size, cookie_key_size; void *volume_key, *cookie_key; /* * Volume key is a NUL-terminated string. key[0] stores strlen() of the * string, followed by the content of the string (excluding '\0'). */ volume_key_size = volume->key[0] + 1; volume_key = volume->key + 1; /* Cookie key is binary data, which is netfs specific. */ cookie_key_size = cookie->key_len; cookie_key = fscache_get_key(cookie); if (!(object->cookie->advice & FSCACHE_ADV_WANT_CACHE_SIZE)) { pr_err("WANT_CACHE_SIZE is needed for on-demand mode\n"); return -EINVAL; } load->volume_key_size = volume_key_size; load->cookie_key_size = cookie_key_size; memcpy(load->data, volume_key, volume_key_size); memcpy(load->data + volume_key_size, cookie_key, cookie_key_size); return 0; } static int cachefiles_ondemand_init_close_req(struct cachefiles_req *req, void *private) { struct cachefiles_object *object = req->object; if (!cachefiles_ondemand_object_is_open(object)) return -ENOENT; trace_cachefiles_ondemand_close(object, &req->msg); return 0; } struct cachefiles_read_ctx { loff_t off; size_t len; }; static int cachefiles_ondemand_init_read_req(struct cachefiles_req *req, void *private) { struct cachefiles_object *object = req->object; struct cachefiles_read *load = (void *)req->msg.data; struct cachefiles_read_ctx *read_ctx = private; load->off = read_ctx->off; load->len = read_ctx->len; trace_cachefiles_ondemand_read(object, &req->msg, load); return 0; } int cachefiles_ondemand_init_object(struct cachefiles_object *object) { struct fscache_cookie *cookie = object->cookie; struct fscache_volume *volume = object->volume->vcookie; size_t volume_key_size, cookie_key_size, data_len; if (!object->ondemand) return 0; /* * CacheFiles will firstly check the cache file under the root cache * directory. If the coherency check failed, it will fallback to * creating a new tmpfile as the cache file. Reuse the previously * allocated object ID if any. */ if (cachefiles_ondemand_object_is_open(object)) return 0; volume_key_size = volume->key[0] + 1; cookie_key_size = cookie->key_len; data_len = sizeof(struct cachefiles_open) + volume_key_size + cookie_key_size; return cachefiles_ondemand_send_req(object, CACHEFILES_OP_OPEN, data_len, cachefiles_ondemand_init_open_req, NULL); } void cachefiles_ondemand_clean_object(struct cachefiles_object *object) { unsigned long index; struct cachefiles_req *req; struct cachefiles_cache *cache; if (!object->ondemand) return; cachefiles_ondemand_send_req(object, CACHEFILES_OP_CLOSE, 0, cachefiles_ondemand_init_close_req, NULL); if (!object->ondemand->ondemand_id) return; /* Cancel all requests for the object that is being dropped. */ cache = object->volume->cache; xa_lock(&cache->reqs); cachefiles_ondemand_set_object_dropping(object); xa_for_each(&cache->reqs, index, req) { if (req->object == object) { req->error = -EIO; complete(&req->done); __xa_erase(&cache->reqs, index); } } xa_unlock(&cache->reqs); /* Wait for ondemand_object_worker() to finish to avoid UAF. */ cancel_work_sync(&object->ondemand->ondemand_work); } int cachefiles_ondemand_init_obj_info(struct cachefiles_object *object, struct cachefiles_volume *volume) { if (!cachefiles_in_ondemand_mode(volume->cache)) return 0; object->ondemand = kzalloc(sizeof(struct cachefiles_ondemand_info), GFP_KERNEL); if (!object->ondemand) return -ENOMEM; object->ondemand->object = object; spin_lock_init(&object->ondemand->lock); INIT_WORK(&object->ondemand->ondemand_work, ondemand_object_worker); return 0; } void cachefiles_ondemand_deinit_obj_info(struct cachefiles_object *object) { kfree(object->ondemand); object->ondemand = NULL; } int cachefiles_ondemand_read(struct cachefiles_object *object, loff_t pos, size_t len) { struct cachefiles_read_ctx read_ctx = {pos, len}; return cachefiles_ondemand_send_req(object, CACHEFILES_OP_READ, sizeof(struct cachefiles_read), cachefiles_ondemand_init_read_req, &read_ctx); } |