1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* Daemon interface 3 * 4 * Copyright (C) 2007, 2021 Red Hat, Inc. All Rights Reserved. 5 * Written by David Howells (dhowells@redhat.com) 6 */ 7 8 #include <linux/module.h> 9 #include <linux/init.h> 10 #include <linux/sched.h> 11 #include <linux/completion.h> 12 #include <linux/slab.h> 13 #include <linux/fs.h> 14 #include <linux/file.h> 15 #include <linux/namei.h> 16 #include <linux/poll.h> 17 #include <linux/mount.h> 18 #include <linux/security.h> 19 #include <linux/statfs.h> 20 #include <linux/ctype.h> 21 #include <linux/string.h> 22 #include <linux/fs_struct.h> 23 #include "internal.h" 24 25 static int cachefiles_daemon_open(struct inode *, struct file *); 26 static int cachefiles_daemon_release(struct inode *, struct file *); 27 static ssize_t cachefiles_daemon_read(struct file *, char __user *, size_t, 28 loff_t *); 29 static ssize_t cachefiles_daemon_write(struct file *, const char __user *, 30 size_t, loff_t *); 31 static __poll_t cachefiles_daemon_poll(struct file *, 32 struct poll_table_struct *); 33 static int cachefiles_daemon_frun(struct cachefiles_cache *, char *); 34 static int cachefiles_daemon_fcull(struct cachefiles_cache *, char *); 35 static int cachefiles_daemon_fstop(struct cachefiles_cache *, char *); 36 static int cachefiles_daemon_brun(struct cachefiles_cache *, char *); 37 static int cachefiles_daemon_bcull(struct cachefiles_cache *, char *); 38 static int cachefiles_daemon_bstop(struct cachefiles_cache *, char *); 39 static int cachefiles_daemon_cull(struct cachefiles_cache *, char *); 40 static int cachefiles_daemon_debug(struct cachefiles_cache *, char *); 41 static int cachefiles_daemon_dir(struct cachefiles_cache *, char *); 42 static int cachefiles_daemon_inuse(struct cachefiles_cache *, char *); 43 static int cachefiles_daemon_secctx(struct cachefiles_cache *, char *); 44 static int cachefiles_daemon_tag(struct cachefiles_cache *, char *); 45 static int cachefiles_daemon_bind(struct cachefiles_cache *, char *); 46 static void cachefiles_daemon_unbind(struct cachefiles_cache *); 47 48 static unsigned long cachefiles_open; 49 50 const struct file_operations cachefiles_daemon_fops = { 51 .owner = THIS_MODULE, 52 .open = cachefiles_daemon_open, 53 .release = cachefiles_daemon_release, 54 .read = cachefiles_daemon_read, 55 .write = cachefiles_daemon_write, 56 .poll = cachefiles_daemon_poll, 57 .llseek = noop_llseek, 58 }; 59 60 struct cachefiles_daemon_cmd { 61 char name[8]; 62 int (*handler)(struct cachefiles_cache *cache, char *args); 63 }; 64 65 static const struct cachefiles_daemon_cmd cachefiles_daemon_cmds[] = { 66 { "bind", cachefiles_daemon_bind }, 67 { "brun", cachefiles_daemon_brun }, 68 { "bcull", cachefiles_daemon_bcull }, 69 { "bstop", cachefiles_daemon_bstop }, 70 { "cull", cachefiles_daemon_cull }, 71 { "debug", cachefiles_daemon_debug }, 72 { "dir", cachefiles_daemon_dir }, 73 { "frun", cachefiles_daemon_frun }, 74 { "fcull", cachefiles_daemon_fcull }, 75 { "fstop", cachefiles_daemon_fstop }, 76 { "inuse", cachefiles_daemon_inuse }, 77 { "secctx", cachefiles_daemon_secctx }, 78 { "tag", cachefiles_daemon_tag }, 79 { "", NULL } 80 }; 81 82 83 /* 84 * Prepare a cache for caching. 85 */ 86 static int cachefiles_daemon_open(struct inode *inode, struct file *file) 87 { 88 struct cachefiles_cache *cache; 89 90 _enter(""); 91 92 /* only the superuser may do this */ 93 if (!capable(CAP_SYS_ADMIN)) 94 return -EPERM; 95 96 /* the cachefiles device may only be open once at a time */ 97 if (xchg(&cachefiles_open, 1) == 1) 98 return -EBUSY; 99 100 /* allocate a cache record */ 101 cache = kzalloc_obj(struct cachefiles_cache); 102 if (!cache) { 103 cachefiles_open = 0; 104 return -ENOMEM; 105 } 106 107 mutex_init(&cache->daemon_mutex); 108 init_waitqueue_head(&cache->daemon_pollwq); 109 INIT_LIST_HEAD(&cache->volumes); 110 INIT_LIST_HEAD(&cache->object_list); 111 spin_lock_init(&cache->object_list_lock); 112 113 /* set default caching limits 114 * - limit at 1% free space and/or free files 115 * - cull below 5% free space and/or free files 116 * - cease culling above 7% free space and/or free files 117 */ 118 cache->frun_percent = 7; 119 cache->fcull_percent = 5; 120 cache->fstop_percent = 1; 121 cache->brun_percent = 7; 122 cache->bcull_percent = 5; 123 cache->bstop_percent = 1; 124 125 file->private_data = cache; 126 cache->cachefilesd = file; 127 return 0; 128 } 129 130 /* 131 * Release a cache. 132 */ 133 static int cachefiles_daemon_release(struct inode *inode, struct file *file) 134 { 135 struct cachefiles_cache *cache = file->private_data; 136 137 _enter(""); 138 139 ASSERT(cache); 140 141 set_bit(CACHEFILES_DEAD, &cache->flags); 142 cachefiles_daemon_unbind(cache); 143 144 /* clean up the control file interface */ 145 cache->cachefilesd = NULL; 146 file->private_data = NULL; 147 cachefiles_open = 0; 148 149 kfree(cache); 150 _leave(""); 151 return 0; 152 } 153 154 static ssize_t cachefiles_do_daemon_read(struct cachefiles_cache *cache, 155 char __user *_buffer, size_t buflen) 156 { 157 unsigned long long b_released; 158 unsigned f_released; 159 char buffer[256]; 160 int n; 161 162 /* check how much space the cache has */ 163 cachefiles_has_space(cache, 0, 0, cachefiles_has_space_check); 164 165 /* summarise */ 166 f_released = atomic_xchg(&cache->f_released, 0); 167 b_released = atomic_long_xchg(&cache->b_released, 0); 168 clear_bit(CACHEFILES_STATE_CHANGED, &cache->flags); 169 170 n = snprintf(buffer, sizeof(buffer), 171 "cull=%c" 172 " frun=%llx" 173 " fcull=%llx" 174 " fstop=%llx" 175 " brun=%llx" 176 " bcull=%llx" 177 " bstop=%llx" 178 " freleased=%x" 179 " breleased=%llx", 180 test_bit(CACHEFILES_CULLING, &cache->flags) ? '1' : '0', 181 (unsigned long long) cache->frun, 182 (unsigned long long) cache->fcull, 183 (unsigned long long) cache->fstop, 184 (unsigned long long) cache->brun, 185 (unsigned long long) cache->bcull, 186 (unsigned long long) cache->bstop, 187 f_released, 188 b_released); 189 190 if (n > buflen) 191 return -EMSGSIZE; 192 193 if (copy_to_user(_buffer, buffer, n) != 0) 194 return -EFAULT; 195 196 return n; 197 } 198 199 /* 200 * Read the cache state. 201 */ 202 static ssize_t cachefiles_daemon_read(struct file *file, char __user *_buffer, 203 size_t buflen, loff_t *pos) 204 { 205 struct cachefiles_cache *cache = file->private_data; 206 207 //_enter(",,%zu,", buflen); 208 209 if (!test_bit(CACHEFILES_READY, &cache->flags)) 210 return 0; 211 212 return cachefiles_do_daemon_read(cache, _buffer, buflen); 213 } 214 215 /* 216 * Take a command from cachefilesd, parse it and act on it. 217 */ 218 static ssize_t cachefiles_daemon_write(struct file *file, 219 const char __user *_data, 220 size_t datalen, 221 loff_t *pos) 222 { 223 const struct cachefiles_daemon_cmd *cmd; 224 struct cachefiles_cache *cache = file->private_data; 225 ssize_t ret; 226 char *data, *args, *cp; 227 228 //_enter(",,%zu,", datalen); 229 230 ASSERT(cache); 231 232 if (test_bit(CACHEFILES_DEAD, &cache->flags)) 233 return -EIO; 234 235 if (datalen > PAGE_SIZE - 1) 236 return -EOPNOTSUPP; 237 238 /* drag the command string into the kernel so we can parse it */ 239 data = memdup_user_nul(_data, datalen); 240 if (IS_ERR(data)) 241 return PTR_ERR(data); 242 243 ret = -EINVAL; 244 if (memchr(data, '\0', datalen)) 245 goto error; 246 247 /* strip any newline */ 248 cp = memchr(data, '\n', datalen); 249 if (cp) { 250 if (cp == data) 251 goto error; 252 253 *cp = '\0'; 254 } 255 256 /* parse the command */ 257 ret = -EOPNOTSUPP; 258 259 for (args = data; *args; args++) 260 if (isspace(*args)) 261 break; 262 if (*args) { 263 if (args == data) 264 goto error; 265 *args = '\0'; 266 args = skip_spaces(++args); 267 } 268 269 /* run the appropriate command handler */ 270 for (cmd = cachefiles_daemon_cmds; cmd->name[0]; cmd++) 271 if (strcmp(cmd->name, data) == 0) 272 goto found_command; 273 274 error: 275 kfree(data); 276 //_leave(" = %zd", ret); 277 return ret; 278 279 found_command: 280 mutex_lock(&cache->daemon_mutex); 281 282 ret = -EIO; 283 if (!test_bit(CACHEFILES_DEAD, &cache->flags)) 284 ret = cmd->handler(cache, args); 285 286 mutex_unlock(&cache->daemon_mutex); 287 288 if (ret == 0) 289 ret = datalen; 290 goto error; 291 } 292 293 /* 294 * Poll for culling state 295 * - use EPOLLOUT to indicate culling state 296 */ 297 static __poll_t cachefiles_daemon_poll(struct file *file, 298 struct poll_table_struct *poll) 299 { 300 struct cachefiles_cache *cache = file->private_data; 301 __poll_t mask; 302 303 poll_wait(file, &cache->daemon_pollwq, poll); 304 mask = 0; 305 306 if (test_bit(CACHEFILES_STATE_CHANGED, &cache->flags)) 307 mask |= EPOLLIN; 308 309 if (test_bit(CACHEFILES_CULLING, &cache->flags)) 310 mask |= EPOLLOUT; 311 312 return mask; 313 } 314 315 /* 316 * Give a range error for cache space constraints 317 * - can be tail-called 318 */ 319 static int cachefiles_daemon_range_error(struct cachefiles_cache *cache, 320 char *args) 321 { 322 pr_err("Free space limits must be in range 0%%<=stop<cull<run<100%%\n"); 323 324 return -EINVAL; 325 } 326 327 /* 328 * Set the percentage of files at which to stop culling 329 * - command: "frun <N>%" 330 */ 331 static int cachefiles_daemon_frun(struct cachefiles_cache *cache, char *args) 332 { 333 unsigned long frun; 334 335 _enter(",%s", args); 336 337 if (!*args) 338 return -EINVAL; 339 340 frun = simple_strtoul(args, &args, 10); 341 if (args[0] != '%' || args[1] != '\0') 342 return -EINVAL; 343 344 if (frun <= cache->fcull_percent || frun >= 100) 345 return cachefiles_daemon_range_error(cache, args); 346 347 cache->frun_percent = frun; 348 return 0; 349 } 350 351 /* 352 * Set the percentage of files at which to start culling 353 * - command: "fcull <N>%" 354 */ 355 static int cachefiles_daemon_fcull(struct cachefiles_cache *cache, char *args) 356 { 357 unsigned long fcull; 358 359 _enter(",%s", args); 360 361 if (!*args) 362 return -EINVAL; 363 364 fcull = simple_strtoul(args, &args, 10); 365 if (args[0] != '%' || args[1] != '\0') 366 return -EINVAL; 367 368 if (fcull <= cache->fstop_percent || fcull >= cache->frun_percent) 369 return cachefiles_daemon_range_error(cache, args); 370 371 cache->fcull_percent = fcull; 372 return 0; 373 } 374 375 /* 376 * Set the percentage of files at which to stop allocating 377 * - command: "fstop <N>%" 378 */ 379 static int cachefiles_daemon_fstop(struct cachefiles_cache *cache, char *args) 380 { 381 unsigned long fstop; 382 383 _enter(",%s", args); 384 385 if (!*args) 386 return -EINVAL; 387 388 fstop = simple_strtoul(args, &args, 10); 389 if (args[0] != '%' || args[1] != '\0') 390 return -EINVAL; 391 392 if (fstop >= cache->fcull_percent) 393 return cachefiles_daemon_range_error(cache, args); 394 395 cache->fstop_percent = fstop; 396 return 0; 397 } 398 399 /* 400 * Set the percentage of blocks at which to stop culling 401 * - command: "brun <N>%" 402 */ 403 static int cachefiles_daemon_brun(struct cachefiles_cache *cache, char *args) 404 { 405 unsigned long brun; 406 407 _enter(",%s", args); 408 409 if (!*args) 410 return -EINVAL; 411 412 brun = simple_strtoul(args, &args, 10); 413 if (args[0] != '%' || args[1] != '\0') 414 return -EINVAL; 415 416 if (brun <= cache->bcull_percent || brun >= 100) 417 return cachefiles_daemon_range_error(cache, args); 418 419 cache->brun_percent = brun; 420 return 0; 421 } 422 423 /* 424 * Set the percentage of blocks at which to start culling 425 * - command: "bcull <N>%" 426 */ 427 static int cachefiles_daemon_bcull(struct cachefiles_cache *cache, char *args) 428 { 429 unsigned long bcull; 430 431 _enter(",%s", args); 432 433 if (!*args) 434 return -EINVAL; 435 436 bcull = simple_strtoul(args, &args, 10); 437 if (args[0] != '%' || args[1] != '\0') 438 return -EINVAL; 439 440 if (bcull <= cache->bstop_percent || bcull >= cache->brun_percent) 441 return cachefiles_daemon_range_error(cache, args); 442 443 cache->bcull_percent = bcull; 444 return 0; 445 } 446 447 /* 448 * Set the percentage of blocks at which to stop allocating 449 * - command: "bstop <N>%" 450 */ 451 static int cachefiles_daemon_bstop(struct cachefiles_cache *cache, char *args) 452 { 453 unsigned long bstop; 454 455 _enter(",%s", args); 456 457 if (!*args) 458 return -EINVAL; 459 460 bstop = simple_strtoul(args, &args, 10); 461 if (args[0] != '%' || args[1] != '\0') 462 return -EINVAL; 463 464 if (bstop >= cache->bcull_percent) 465 return cachefiles_daemon_range_error(cache, args); 466 467 cache->bstop_percent = bstop; 468 return 0; 469 } 470 471 /* 472 * Set the cache directory 473 * - command: "dir <name>" 474 */ 475 static int cachefiles_daemon_dir(struct cachefiles_cache *cache, char *args) 476 { 477 char *dir; 478 479 _enter(",%s", args); 480 481 if (!*args) { 482 pr_err("Empty directory specified\n"); 483 return -EINVAL; 484 } 485 486 if (cache->rootdirname) { 487 pr_err("Second cache directory specified\n"); 488 return -EEXIST; 489 } 490 491 dir = kstrdup(args, GFP_KERNEL); 492 if (!dir) 493 return -ENOMEM; 494 495 cache->rootdirname = dir; 496 return 0; 497 } 498 499 /* 500 * Set the cache security context 501 * - command: "secctx <ctx>" 502 */ 503 static int cachefiles_daemon_secctx(struct cachefiles_cache *cache, char *args) 504 { 505 int err; 506 507 _enter(",%s", args); 508 509 if (!*args) { 510 pr_err("Empty security context specified\n"); 511 return -EINVAL; 512 } 513 514 if (cache->have_secid) { 515 pr_err("Second security context specified\n"); 516 return -EINVAL; 517 } 518 519 err = security_secctx_to_secid(args, strlen(args), &cache->secid); 520 if (err) 521 return err; 522 523 cache->have_secid = true; 524 return 0; 525 } 526 527 /* 528 * Set the cache tag 529 * - command: "tag <name>" 530 */ 531 static int cachefiles_daemon_tag(struct cachefiles_cache *cache, char *args) 532 { 533 char *tag; 534 535 _enter(",%s", args); 536 537 if (!*args) { 538 pr_err("Empty tag specified\n"); 539 return -EINVAL; 540 } 541 542 if (cache->tag) 543 return -EEXIST; 544 545 tag = kstrdup(args, GFP_KERNEL); 546 if (!tag) 547 return -ENOMEM; 548 549 cache->tag = tag; 550 return 0; 551 } 552 553 /* 554 * Request a node in the cache be culled from the current working directory 555 * - command: "cull <name>" 556 */ 557 static int cachefiles_daemon_cull(struct cachefiles_cache *cache, char *args) 558 { 559 struct path path; 560 const struct cred *saved_cred; 561 int ret; 562 563 _enter(",%s", args); 564 565 if (strchr(args, '/')) 566 goto inval; 567 568 if (!test_bit(CACHEFILES_READY, &cache->flags)) { 569 pr_err("cull applied to unready cache\n"); 570 return -EIO; 571 } 572 573 if (test_bit(CACHEFILES_DEAD, &cache->flags)) { 574 pr_err("cull applied to dead cache\n"); 575 return -EIO; 576 } 577 578 get_fs_pwd(current->fs, &path); 579 580 if (!d_can_lookup(path.dentry)) 581 goto notdir; 582 583 cachefiles_begin_secure(cache, &saved_cred); 584 ret = cachefiles_cull(cache, path.dentry, args); 585 cachefiles_end_secure(cache, saved_cred); 586 587 path_put(&path); 588 _leave(" = %d", ret); 589 return ret; 590 591 notdir: 592 path_put(&path); 593 pr_err("cull command requires dirfd to be a directory\n"); 594 return -ENOTDIR; 595 596 inval: 597 pr_err("cull command requires dirfd and filename\n"); 598 return -EINVAL; 599 } 600 601 /* 602 * Set debugging mode 603 * - command: "debug <mask>" 604 */ 605 static int cachefiles_daemon_debug(struct cachefiles_cache *cache, char *args) 606 { 607 unsigned long mask; 608 609 _enter(",%s", args); 610 611 mask = simple_strtoul(args, &args, 0); 612 if (args[0] != '\0') 613 goto inval; 614 615 cachefiles_debug = mask; 616 _leave(" = 0"); 617 return 0; 618 619 inval: 620 pr_err("debug command requires mask\n"); 621 return -EINVAL; 622 } 623 624 /* 625 * Find out whether an object in the current working directory is in use or not 626 * - command: "inuse <name>" 627 */ 628 static int cachefiles_daemon_inuse(struct cachefiles_cache *cache, char *args) 629 { 630 struct path path; 631 const struct cred *saved_cred; 632 int ret; 633 634 //_enter(",%s", args); 635 636 if (strchr(args, '/')) 637 goto inval; 638 639 if (!test_bit(CACHEFILES_READY, &cache->flags)) { 640 pr_err("inuse applied to unready cache\n"); 641 return -EIO; 642 } 643 644 if (test_bit(CACHEFILES_DEAD, &cache->flags)) { 645 pr_err("inuse applied to dead cache\n"); 646 return -EIO; 647 } 648 649 get_fs_pwd(current->fs, &path); 650 651 if (!d_can_lookup(path.dentry)) 652 goto notdir; 653 654 cachefiles_begin_secure(cache, &saved_cred); 655 ret = cachefiles_check_in_use(cache, path.dentry, args); 656 cachefiles_end_secure(cache, saved_cred); 657 658 path_put(&path); 659 //_leave(" = %d", ret); 660 return ret; 661 662 notdir: 663 path_put(&path); 664 pr_err("inuse command requires dirfd to be a directory\n"); 665 return -ENOTDIR; 666 667 inval: 668 pr_err("inuse command requires dirfd and filename\n"); 669 return -EINVAL; 670 } 671 672 /* 673 * Bind a directory as a cache 674 */ 675 static int cachefiles_daemon_bind(struct cachefiles_cache *cache, char *args) 676 { 677 _enter("{%u,%u,%u,%u,%u,%u},%s", 678 cache->frun_percent, 679 cache->fcull_percent, 680 cache->fstop_percent, 681 cache->brun_percent, 682 cache->bcull_percent, 683 cache->bstop_percent, 684 args); 685 686 if (cache->fstop_percent >= cache->fcull_percent || 687 cache->fcull_percent >= cache->frun_percent || 688 cache->frun_percent >= 100) 689 return -ERANGE; 690 691 if (cache->bstop_percent >= cache->bcull_percent || 692 cache->bcull_percent >= cache->brun_percent || 693 cache->brun_percent >= 100) 694 return -ERANGE; 695 696 if (!cache->rootdirname) { 697 pr_err("No cache directory specified\n"); 698 return -EINVAL; 699 } 700 701 /* Don't permit already bound caches to be re-bound */ 702 if (test_bit(CACHEFILES_READY, &cache->flags)) { 703 pr_err("Cache already bound\n"); 704 return -EBUSY; 705 } 706 707 if (*args) { 708 pr_err("'bind' command doesn't take an argument\n"); 709 return -EINVAL; 710 } 711 712 /* Make sure we have copies of the tag string */ 713 if (!cache->tag) { 714 /* 715 * The tag string is released by the fops->release() 716 * function, so we don't release it on error here 717 */ 718 cache->tag = kstrdup("CacheFiles", GFP_KERNEL); 719 if (!cache->tag) 720 return -ENOMEM; 721 } 722 723 return cachefiles_add_cache(cache); 724 } 725 726 /* 727 * Unbind a cache. 728 */ 729 static void cachefiles_daemon_unbind(struct cachefiles_cache *cache) 730 { 731 _enter(""); 732 733 if (test_bit(CACHEFILES_READY, &cache->flags)) 734 cachefiles_withdraw_cache(cache); 735 736 cachefiles_put_directory(cache->graveyard); 737 cachefiles_put_directory(cache->store); 738 mntput(cache->mnt); 739 put_cred(cache->cache_cred); 740 741 kfree(cache->rootdirname); 742 kfree(cache->tag); 743 744 _leave(""); 745 } 746