1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 FUSE: Filesystem in Userspace 4 Copyright (C) 2001-2008 Miklos Szeredi <miklos@szeredi.hu> 5 */ 6 7 #include "dev.h" 8 #include "fuse_i.h" 9 10 #include <linux/pagemap.h> 11 #include <linux/file.h> 12 #include <linux/fs_context.h> 13 #include <linux/moduleparam.h> 14 #include <linux/sched.h> 15 #include <linux/namei.h> 16 #include <linux/slab.h> 17 #include <linux/xattr.h> 18 #include <linux/iversion.h> 19 #include <linux/posix_acl.h> 20 #include <linux/security.h> 21 #include <linux/types.h> 22 #include <linux/kernel.h> 23 24 static bool __read_mostly allow_sys_admin_access; 25 module_param(allow_sys_admin_access, bool, 0644); 26 MODULE_PARM_DESC(allow_sys_admin_access, 27 "Allow users with CAP_SYS_ADMIN in initial userns to bypass allow_other access check"); 28 29 struct dentry_bucket { 30 struct rb_root tree; 31 spinlock_t lock; 32 }; 33 34 #define FUSE_HASH_BITS 5 35 #define FUSE_HASH_SIZE (1 << FUSE_HASH_BITS) 36 static struct dentry_bucket dentry_hash[FUSE_HASH_SIZE]; 37 static struct delayed_work dentry_tree_work; 38 39 /* Minimum invalidation work queue frequency */ 40 #define FUSE_DENTRY_INVAL_FREQ_MIN 5 41 42 unsigned __read_mostly inval_wq; 43 static int inval_wq_set(const char *val, const struct kernel_param *kp) 44 { 45 unsigned int num; 46 unsigned int old = inval_wq; 47 int ret; 48 49 if (!val) 50 return -EINVAL; 51 52 ret = kstrtouint(val, 0, &num); 53 if (ret) 54 return ret; 55 56 if ((num < FUSE_DENTRY_INVAL_FREQ_MIN) && (num != 0)) 57 return -EINVAL; 58 59 /* This should prevent overflow in secs_to_jiffies() */ 60 if (num > USHRT_MAX) 61 return -EINVAL; 62 63 *((unsigned int *)kp->arg) = num; 64 65 if (num && !old) 66 schedule_delayed_work(&dentry_tree_work, 67 secs_to_jiffies(num)); 68 else if (!num && old) 69 cancel_delayed_work_sync(&dentry_tree_work); 70 71 return 0; 72 } 73 static const struct kernel_param_ops inval_wq_ops = { 74 .set = inval_wq_set, 75 .get = param_get_uint, 76 }; 77 module_param_cb(inval_wq, &inval_wq_ops, &inval_wq, 0644); 78 __MODULE_PARM_TYPE(inval_wq, "uint"); 79 MODULE_PARM_DESC(inval_wq, 80 "Dentries invalidation work queue period in secs (>= " 81 __stringify(FUSE_DENTRY_INVAL_FREQ_MIN) ")."); 82 83 static inline struct dentry_bucket *get_dentry_bucket(struct dentry *dentry) 84 { 85 int i = hash_ptr(dentry, FUSE_HASH_BITS); 86 87 return &dentry_hash[i]; 88 } 89 90 static void fuse_advise_use_readdirplus(struct inode *dir) 91 { 92 struct fuse_inode *fi = get_fuse_inode(dir); 93 94 set_bit(FUSE_I_ADVISE_RDPLUS, &fi->state); 95 } 96 97 struct fuse_dentry { 98 u64 time; 99 u64 epoch; 100 union { 101 struct rcu_head rcu; 102 struct rb_node node; 103 }; 104 struct dentry *dentry; 105 }; 106 107 static void __fuse_dentry_tree_del_node(struct fuse_dentry *fd, 108 struct dentry_bucket *bucket) 109 { 110 if (!RB_EMPTY_NODE(&fd->node)) { 111 rb_erase(&fd->node, &bucket->tree); 112 RB_CLEAR_NODE(&fd->node); 113 } 114 } 115 116 static void fuse_dentry_tree_del_node(struct dentry *dentry) 117 { 118 struct fuse_dentry *fd = dentry->d_fsdata; 119 struct dentry_bucket *bucket = get_dentry_bucket(dentry); 120 121 spin_lock(&bucket->lock); 122 __fuse_dentry_tree_del_node(fd, bucket); 123 spin_unlock(&bucket->lock); 124 } 125 126 static void fuse_dentry_tree_add_node(struct dentry *dentry) 127 { 128 struct fuse_dentry *fd = dentry->d_fsdata; 129 struct dentry_bucket *bucket; 130 struct fuse_dentry *cur; 131 struct rb_node **p, *parent = NULL; 132 133 if (!inval_wq) 134 return; 135 136 bucket = get_dentry_bucket(dentry); 137 138 spin_lock(&bucket->lock); 139 140 __fuse_dentry_tree_del_node(fd, bucket); 141 142 p = &bucket->tree.rb_node; 143 while (*p) { 144 parent = *p; 145 cur = rb_entry(*p, struct fuse_dentry, node); 146 if (fd->time < cur->time) 147 p = &(*p)->rb_left; 148 else 149 p = &(*p)->rb_right; 150 } 151 rb_link_node(&fd->node, parent, p); 152 rb_insert_color(&fd->node, &bucket->tree); 153 spin_unlock(&bucket->lock); 154 } 155 156 /* 157 * work queue which, when enabled, will periodically check for expired dentries 158 * in the dentries tree. 159 */ 160 static void fuse_dentry_tree_work(struct work_struct *work) 161 { 162 LIST_HEAD(dispose); 163 struct fuse_dentry *fd; 164 struct rb_node *node; 165 int i; 166 167 for (i = 0; i < FUSE_HASH_SIZE; i++) { 168 spin_lock(&dentry_hash[i].lock); 169 node = rb_first(&dentry_hash[i].tree); 170 while (node) { 171 fd = rb_entry(node, struct fuse_dentry, node); 172 if (!time_before64(fd->time, get_jiffies_64())) 173 break; 174 175 rb_erase(&fd->node, &dentry_hash[i].tree); 176 RB_CLEAR_NODE(&fd->node); 177 spin_lock(&fd->dentry->d_lock); 178 /* If dentry is still referenced, let next dput release it */ 179 fd->dentry->d_flags |= DCACHE_OP_DELETE; 180 __move_to_shrink_list(fd->dentry, &dispose); 181 spin_unlock(&fd->dentry->d_lock); 182 if (need_resched()) { 183 spin_unlock(&dentry_hash[i].lock); 184 cond_resched(); 185 spin_lock(&dentry_hash[i].lock); 186 } 187 node = rb_first(&dentry_hash[i].tree); 188 } 189 spin_unlock(&dentry_hash[i].lock); 190 } 191 shrink_dentry_list(&dispose); 192 193 if (inval_wq) 194 schedule_delayed_work(&dentry_tree_work, 195 secs_to_jiffies(inval_wq)); 196 } 197 198 void fuse_epoch_work(struct work_struct *work) 199 { 200 struct fuse_conn *fc = container_of(work, struct fuse_conn, 201 epoch_work); 202 struct fuse_mount *fm; 203 struct inode *inode; 204 205 down_read(&fc->killsb); 206 207 inode = fuse_ilookup(fc, FUSE_ROOT_ID, &fm); 208 if (inode) { 209 iput(inode); 210 /* Remove all possible active references to cached inodes */ 211 shrink_dcache_sb(fm->sb); 212 } else 213 pr_warn("Failed to get root inode"); 214 215 up_read(&fc->killsb); 216 } 217 218 void fuse_dentry_tree_init(void) 219 { 220 int i; 221 222 for (i = 0; i < FUSE_HASH_SIZE; i++) { 223 spin_lock_init(&dentry_hash[i].lock); 224 dentry_hash[i].tree = RB_ROOT; 225 } 226 INIT_DELAYED_WORK(&dentry_tree_work, fuse_dentry_tree_work); 227 } 228 229 void fuse_dentry_tree_cleanup(void) 230 { 231 int i; 232 233 inval_wq = 0; 234 cancel_delayed_work_sync(&dentry_tree_work); 235 236 for (i = 0; i < FUSE_HASH_SIZE; i++) 237 WARN_ON_ONCE(!RB_EMPTY_ROOT(&dentry_hash[i].tree)); 238 } 239 240 void fuse_dentry_set_epoch(struct dentry *dentry, u64 epoch) 241 { 242 struct fuse_dentry *fd = dentry->d_fsdata; 243 244 fd->epoch = epoch; 245 } 246 247 static inline void __fuse_dentry_settime(struct dentry *dentry, u64 time) 248 { 249 ((struct fuse_dentry *) dentry->d_fsdata)->time = time; 250 } 251 252 static inline u64 fuse_dentry_time(const struct dentry *entry) 253 { 254 return ((struct fuse_dentry *) entry->d_fsdata)->time; 255 } 256 257 static void fuse_dentry_settime(struct dentry *dentry, u64 time) 258 { 259 struct fuse_conn *fc = get_fuse_conn_super(dentry->d_sb); 260 bool delete = !time && fc->delete_stale; 261 /* 262 * Mess with DCACHE_OP_DELETE because dput() will be faster without it. 263 * Don't care about races, either way it's just an optimization 264 */ 265 if ((!delete && (dentry->d_flags & DCACHE_OP_DELETE)) || 266 (delete && !(dentry->d_flags & DCACHE_OP_DELETE))) { 267 spin_lock(&dentry->d_lock); 268 if (!delete) 269 dentry->d_flags &= ~DCACHE_OP_DELETE; 270 else 271 dentry->d_flags |= DCACHE_OP_DELETE; 272 spin_unlock(&dentry->d_lock); 273 } 274 275 __fuse_dentry_settime(dentry, time); 276 fuse_dentry_tree_add_node(dentry); 277 } 278 279 /* 280 * FUSE caches dentries and attributes with separate timeout. The 281 * time in jiffies until the dentry/attributes are valid is stored in 282 * dentry->d_fsdata and fuse_inode->i_time respectively. 283 */ 284 285 /* 286 * Calculate the time in jiffies until a dentry/attributes are valid 287 */ 288 u64 fuse_time_to_jiffies(u64 sec, u32 nsec) 289 { 290 if (sec || nsec) { 291 struct timespec64 ts = { 292 sec, 293 min_t(u32, nsec, NSEC_PER_SEC - 1) 294 }; 295 296 return get_jiffies_64() + timespec64_to_jiffies(&ts); 297 } else 298 return 0; 299 } 300 301 /* 302 * Set dentry and possibly attribute timeouts from the lookup/mk* 303 * replies 304 */ 305 void fuse_change_entry_timeout(struct dentry *entry, struct fuse_entry_out *o) 306 { 307 fuse_dentry_settime(entry, 308 fuse_time_to_jiffies(o->entry_valid, o->entry_valid_nsec)); 309 } 310 311 void fuse_invalidate_attr_mask(struct inode *inode, u32 mask) 312 { 313 set_mask_bits(&get_fuse_inode(inode)->inval_mask, 0, mask); 314 } 315 316 /* 317 * Mark the attributes as stale, so that at the next call to 318 * ->getattr() they will be fetched from userspace 319 */ 320 void fuse_invalidate_attr(struct inode *inode) 321 { 322 fuse_invalidate_attr_mask(inode, STATX_BASIC_STATS); 323 } 324 325 static void fuse_dir_changed(struct inode *dir) 326 { 327 fuse_invalidate_attr_mask(dir, FUSE_STATX_MODDIR); 328 inode_maybe_inc_iversion(dir, false); 329 } 330 331 /* 332 * Mark the attributes as stale due to an atime change. Avoid the invalidate if 333 * atime is not used. 334 */ 335 void fuse_invalidate_atime(struct inode *inode) 336 { 337 if (!IS_RDONLY(inode)) 338 fuse_invalidate_attr_mask(inode, STATX_ATIME); 339 } 340 341 /* 342 * Just mark the entry as stale, so that a next attempt to look it up 343 * will result in a new lookup call to userspace 344 * 345 * This is called when a dentry is about to become negative and the 346 * timeout is unknown (unlink, rmdir, rename and in some cases 347 * lookup) 348 */ 349 void fuse_invalidate_entry_cache(struct dentry *entry) 350 { 351 fuse_dentry_settime(entry, 0); 352 } 353 354 /* 355 * Same as fuse_invalidate_entry_cache(), but also try to remove the 356 * dentry from the hash 357 */ 358 static void fuse_invalidate_entry(struct dentry *entry) 359 { 360 d_invalidate(entry); 361 fuse_invalidate_entry_cache(entry); 362 } 363 364 static void fuse_lookup_init(struct fuse_args *args, u64 nodeid, 365 const struct qstr *name, 366 struct fuse_entry_out *outarg) 367 { 368 memset(outarg, 0, sizeof(struct fuse_entry_out)); 369 args->opcode = FUSE_LOOKUP; 370 args->nodeid = nodeid; 371 args->in_numargs = 3; 372 fuse_set_zero_arg0(args); 373 args->in_args[1].size = name->len; 374 args->in_args[1].value = name->name; 375 args->in_args[2].size = 1; 376 args->in_args[2].value = ""; 377 args->out_numargs = 1; 378 args->out_args[0].size = sizeof(struct fuse_entry_out); 379 args->out_args[0].value = outarg; 380 } 381 382 /* 383 * Check whether the dentry is still valid 384 * 385 * If the entry validity timeout has expired and the dentry is 386 * positive, try to redo the lookup. If the lookup results in a 387 * different inode, then let the VFS invalidate the dentry and redo 388 * the lookup once more. If the lookup results in the same inode, 389 * then refresh the attributes, timeouts and mark the dentry valid. 390 */ 391 static int fuse_dentry_revalidate(struct inode *dir, const struct qstr *name, 392 struct dentry *entry, unsigned int flags) 393 { 394 struct inode *inode; 395 struct fuse_mount *fm; 396 struct fuse_conn *fc; 397 struct fuse_inode *fi; 398 struct fuse_dentry *fd = entry->d_fsdata; 399 int ret; 400 401 fc = get_fuse_conn_super(dir->i_sb); 402 if (fd->epoch < atomic_read(&fc->epoch)) 403 goto invalid; 404 405 inode = d_inode_rcu(entry); 406 if (inode && fuse_is_bad(inode)) 407 goto invalid; 408 else if (time_before64(fuse_dentry_time(entry), get_jiffies_64()) || 409 (flags & (LOOKUP_EXCL | LOOKUP_REVAL | LOOKUP_RENAME_TARGET))) { 410 struct fuse_entry_out outarg; 411 FUSE_ARGS(args); 412 struct fuse_forget_link *forget; 413 u64 attr_version; 414 415 /* For negative dentries, always do a fresh lookup */ 416 if (!inode) 417 goto invalid; 418 419 ret = -ECHILD; 420 if (flags & LOOKUP_RCU) 421 goto out; 422 423 fm = get_fuse_mount(inode); 424 425 forget = fuse_alloc_forget(); 426 ret = -ENOMEM; 427 if (!forget) 428 goto out; 429 430 attr_version = fuse_get_attr_version(fm->fc); 431 432 fuse_lookup_init(&args, get_node_id(dir), name, &outarg); 433 ret = fuse_simple_request(fm, &args); 434 /* Zero nodeid is same as -ENOENT */ 435 if (!ret && !outarg.nodeid) 436 ret = -ENOENT; 437 if (!ret) { 438 fi = get_fuse_inode(inode); 439 if (outarg.nodeid != get_node_id(inode) || 440 (bool) IS_AUTOMOUNT(inode) != (bool) (outarg.attr.flags & FUSE_ATTR_SUBMOUNT)) { 441 fuse_chan_queue_forget(fm->fc->chan, forget, 442 outarg.nodeid, 1); 443 goto invalid; 444 } 445 spin_lock(&fi->lock); 446 fi->nlookup++; 447 spin_unlock(&fi->lock); 448 } 449 kfree(forget); 450 if (ret == -ENOMEM || ret == -EINTR) 451 goto out; 452 if (ret || fuse_invalid_attr(&outarg.attr) || 453 fuse_stale_inode(inode, outarg.generation, &outarg.attr)) 454 goto invalid; 455 456 forget_all_cached_acls(inode); 457 fuse_change_attributes(inode, &outarg.attr, NULL, 458 ATTR_TIMEOUT(&outarg), 459 attr_version); 460 fuse_change_entry_timeout(entry, &outarg); 461 } else if (inode) { 462 fi = get_fuse_inode(inode); 463 if (flags & LOOKUP_RCU) { 464 if (test_bit(FUSE_I_INIT_RDPLUS, &fi->state)) 465 return -ECHILD; 466 } else if (test_and_clear_bit(FUSE_I_INIT_RDPLUS, &fi->state)) { 467 fuse_advise_use_readdirplus(dir); 468 } 469 } 470 ret = 1; 471 out: 472 return ret; 473 474 invalid: 475 ret = 0; 476 goto out; 477 } 478 479 static int fuse_dentry_init(struct dentry *dentry) 480 { 481 struct fuse_dentry *fd; 482 483 fd = kzalloc_obj(struct fuse_dentry, 484 GFP_KERNEL_ACCOUNT | __GFP_RECLAIMABLE); 485 if (!fd) 486 return -ENOMEM; 487 488 fd->dentry = dentry; 489 RB_CLEAR_NODE(&fd->node); 490 dentry->d_fsdata = fd; 491 /* 492 * Initialising epoch to '0' ensures the dentry is invalid 493 * if compared to fc->epoch, which is initialized to '1'. 494 */ 495 fuse_dentry_set_epoch(dentry, 0); 496 497 return 0; 498 } 499 500 static void fuse_dentry_release(struct dentry *dentry) 501 { 502 struct fuse_dentry *fd = dentry->d_fsdata; 503 504 if (!RB_EMPTY_NODE(&fd->node)) 505 fuse_dentry_tree_del_node(dentry); 506 kfree_rcu(fd, rcu); 507 } 508 509 static int fuse_dentry_delete(const struct dentry *dentry) 510 { 511 return time_before64(fuse_dentry_time(dentry), get_jiffies_64()); 512 } 513 514 /* 515 * Create a fuse_mount object with a new superblock (with path->dentry 516 * as the root), and return that mount so it can be auto-mounted on 517 * @path. 518 */ 519 static struct vfsmount *fuse_dentry_automount(struct path *path) 520 { 521 struct fs_context *fsc; 522 struct vfsmount *mnt; 523 struct fuse_inode *mp_fi = get_fuse_inode(d_inode(path->dentry)); 524 525 fsc = fs_context_for_submount(path->mnt->mnt_sb->s_type, path->dentry); 526 if (IS_ERR(fsc)) 527 return ERR_CAST(fsc); 528 529 /* Pass the FUSE inode of the mount for fuse_get_tree_submount() */ 530 fsc->fs_private = mp_fi; 531 532 /* Create the submount */ 533 mnt = fc_mount(fsc); 534 put_fs_context(fsc); 535 return mnt; 536 } 537 538 const struct dentry_operations fuse_dentry_operations = { 539 .d_revalidate = fuse_dentry_revalidate, 540 .d_delete = fuse_dentry_delete, 541 .d_init = fuse_dentry_init, 542 .d_release = fuse_dentry_release, 543 .d_automount = fuse_dentry_automount, 544 }; 545 546 int fuse_valid_type(int m) 547 { 548 return S_ISREG(m) || S_ISDIR(m) || S_ISLNK(m) || S_ISCHR(m) || 549 S_ISBLK(m) || S_ISFIFO(m) || S_ISSOCK(m); 550 } 551 552 static bool fuse_valid_size(u64 size) 553 { 554 return size <= LLONG_MAX; 555 } 556 557 bool fuse_invalid_attr(struct fuse_attr *attr) 558 { 559 return !fuse_valid_type(attr->mode) || !fuse_valid_size(attr->size); 560 } 561 562 int fuse_lookup_name(struct super_block *sb, u64 nodeid, const struct qstr *name, 563 struct fuse_entry_out *outarg, struct inode **inode) 564 { 565 struct fuse_mount *fm = get_fuse_mount_super(sb); 566 FUSE_ARGS(args); 567 struct fuse_forget_link *forget; 568 u64 attr_version, evict_ctr; 569 int err; 570 571 *inode = NULL; 572 err = -ENAMETOOLONG; 573 if (name->len > fm->fc->name_max) 574 goto out; 575 576 577 forget = fuse_alloc_forget(); 578 err = -ENOMEM; 579 if (!forget) 580 goto out; 581 582 attr_version = fuse_get_attr_version(fm->fc); 583 evict_ctr = fuse_get_evict_ctr(fm->fc); 584 585 fuse_lookup_init(&args, nodeid, name, outarg); 586 err = fuse_simple_request(fm, &args); 587 /* Zero nodeid is same as -ENOENT, but with valid timeout */ 588 if (err || !outarg->nodeid) 589 goto out_put_forget; 590 591 err = -EIO; 592 if (fuse_invalid_attr(&outarg->attr)) 593 goto out_put_forget; 594 if (outarg->nodeid == FUSE_ROOT_ID && outarg->generation != 0) { 595 pr_warn_once("root generation should be zero\n"); 596 outarg->generation = 0; 597 } 598 599 *inode = fuse_iget(sb, outarg->nodeid, outarg->generation, 600 &outarg->attr, ATTR_TIMEOUT(outarg), 601 attr_version, evict_ctr); 602 err = -ENOMEM; 603 if (!*inode) { 604 fuse_chan_queue_forget(fm->fc->chan, forget, outarg->nodeid, 1); 605 goto out; 606 } 607 err = 0; 608 609 out_put_forget: 610 kfree(forget); 611 out: 612 return err; 613 } 614 615 static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry, 616 unsigned int flags) 617 { 618 struct fuse_entry_out outarg; 619 struct fuse_conn *fc; 620 struct inode *inode; 621 struct dentry *newent; 622 int err, epoch; 623 bool outarg_valid = true; 624 bool locked; 625 626 if (fuse_is_bad(dir)) 627 return ERR_PTR(-EIO); 628 629 fc = get_fuse_conn_super(dir->i_sb); 630 epoch = atomic_read(&fc->epoch); 631 632 locked = fuse_lock_inode(dir); 633 err = fuse_lookup_name(dir->i_sb, get_node_id(dir), &entry->d_name, 634 &outarg, &inode); 635 fuse_unlock_inode(dir, locked); 636 if (err == -ENOENT) { 637 outarg_valid = false; 638 err = 0; 639 } 640 if (err) 641 goto out_err; 642 643 err = -EIO; 644 if (inode && get_node_id(inode) == FUSE_ROOT_ID) 645 goto out_iput; 646 647 /* Set before the dentry is visible, or a racing lookup invalidates it */ 648 fuse_dentry_set_epoch(entry, epoch); 649 newent = d_splice_alias(inode, entry); 650 err = PTR_ERR(newent); 651 if (IS_ERR(newent)) 652 goto out_err; 653 654 if (newent) { 655 entry = newent; 656 fuse_dentry_set_epoch(entry, epoch); 657 } 658 if (outarg_valid) 659 fuse_change_entry_timeout(entry, &outarg); 660 else 661 fuse_invalidate_entry_cache(entry); 662 663 if (inode) 664 fuse_advise_use_readdirplus(dir); 665 return newent; 666 667 out_iput: 668 iput(inode); 669 out_err: 670 return ERR_PTR(err); 671 } 672 673 static int get_security_context(struct dentry *entry, umode_t mode, 674 struct fuse_in_arg *ext) 675 { 676 struct fuse_secctx *fctx; 677 struct fuse_secctx_header *header; 678 struct lsm_context lsmctx = { }; 679 void *ptr; 680 u32 total_len = sizeof(*header); 681 int err, nr_ctx = 0; 682 const char *name = NULL; 683 size_t namesize; 684 685 err = security_dentry_init_security(entry, mode, &entry->d_name, 686 &name, &lsmctx); 687 688 /* If no LSM is supporting this security hook ignore error */ 689 if (err && err != -EOPNOTSUPP) 690 goto out_err; 691 692 if (lsmctx.len) { 693 nr_ctx = 1; 694 namesize = strlen(name) + 1; 695 err = -EIO; 696 if (WARN_ON(namesize > XATTR_NAME_MAX + 1 || 697 lsmctx.len > S32_MAX)) 698 goto out_err; 699 total_len += FUSE_REC_ALIGN(sizeof(*fctx) + namesize + 700 lsmctx.len); 701 } 702 703 err = -ENOMEM; 704 header = ptr = kzalloc(total_len, GFP_KERNEL); 705 if (!ptr) 706 goto out_err; 707 708 header->nr_secctx = nr_ctx; 709 header->size = total_len; 710 ptr += sizeof(*header); 711 if (nr_ctx) { 712 fctx = ptr; 713 fctx->size = lsmctx.len; 714 ptr += sizeof(*fctx); 715 716 strscpy(ptr, name, namesize); 717 ptr += namesize; 718 719 memcpy(ptr, lsmctx.context, lsmctx.len); 720 } 721 ext->size = total_len; 722 ext->value = header; 723 err = 0; 724 out_err: 725 if (nr_ctx) 726 security_release_secctx(&lsmctx); 727 return err; 728 } 729 730 static void *extend_arg(struct fuse_in_arg *buf, u32 bytes) 731 { 732 void *p; 733 u32 newlen = buf->size + bytes; 734 735 p = krealloc(buf->value, newlen, GFP_KERNEL); 736 if (!p) { 737 kfree(buf->value); 738 buf->size = 0; 739 buf->value = NULL; 740 return NULL; 741 } 742 743 memset(p + buf->size, 0, bytes); 744 buf->value = p; 745 buf->size = newlen; 746 747 return p + newlen - bytes; 748 } 749 750 static u32 fuse_ext_size(size_t size) 751 { 752 return FUSE_REC_ALIGN(sizeof(struct fuse_ext_header) + size); 753 } 754 755 /* 756 * This adds just a single supplementary group that matches the parent's group. 757 */ 758 static int get_create_supp_group(const struct mnt_idmap *idmap, 759 struct inode *dir, 760 struct fuse_in_arg *ext) 761 { 762 struct fuse_conn *fc = get_fuse_conn(dir); 763 struct fuse_ext_header *xh; 764 struct fuse_supp_groups *sg; 765 kgid_t kgid = dir->i_gid; 766 vfsgid_t vfsgid = make_vfsgid(idmap, fc->user_ns, kgid); 767 gid_t parent_gid = from_kgid(fc->user_ns, kgid); 768 769 u32 sg_len = fuse_ext_size(sizeof(*sg) + sizeof(sg->groups[0])); 770 771 if (parent_gid == (gid_t) -1 || vfsgid_eq_kgid(vfsgid, current_fsgid()) || 772 !vfsgid_in_group_p(vfsgid)) 773 return 0; 774 775 xh = extend_arg(ext, sg_len); 776 if (!xh) 777 return -ENOMEM; 778 779 xh->size = sg_len; 780 xh->type = FUSE_EXT_GROUPS; 781 782 sg = (struct fuse_supp_groups *) &xh[1]; 783 sg->nr_groups = 1; 784 sg->groups[0] = parent_gid; 785 786 return 0; 787 } 788 789 static int get_create_ext(const struct mnt_idmap *idmap, 790 struct fuse_args *args, 791 struct inode *dir, struct dentry *dentry, 792 umode_t mode) 793 { 794 struct fuse_conn *fc = get_fuse_conn_super(dentry->d_sb); 795 struct fuse_in_arg ext = { .size = 0, .value = NULL }; 796 int err = 0; 797 798 if (fc->init_security) 799 err = get_security_context(dentry, mode, &ext); 800 if (!err && fc->create_supp_group) 801 err = get_create_supp_group(idmap, dir, &ext); 802 803 if (!err && ext.size) { 804 WARN_ON(args->in_numargs >= ARRAY_SIZE(args->in_args)); 805 args->is_ext = true; 806 args->ext_idx = args->in_numargs++; 807 args->in_args[args->ext_idx] = ext; 808 } else { 809 kfree(ext.value); 810 } 811 812 return err; 813 } 814 815 static void free_ext_value(struct fuse_args *args) 816 { 817 if (args->is_ext) 818 kfree(args->in_args[args->ext_idx].value); 819 } 820 821 /* 822 * Atomic create+open operation 823 * 824 * If the filesystem doesn't support this, then fall back to separate 825 * 'mknod' + 'open' requests. 826 */ 827 static int fuse_create_open(const struct mnt_idmap *idmap, struct inode *dir, 828 struct dentry *entry, struct file *file, 829 unsigned int flags, umode_t mode, u32 opcode) 830 { 831 struct inode *inode; 832 struct fuse_mount *fm = get_fuse_mount(dir); 833 FUSE_ARGS(args); 834 struct fuse_forget_link *forget; 835 struct fuse_create_in inarg; 836 struct fuse_open_out *outopenp; 837 struct fuse_entry_out outentry; 838 struct fuse_inode *fi; 839 struct fuse_file *ff; 840 int epoch, err; 841 bool trunc = flags & O_TRUNC; 842 843 /* Userspace expects S_IFREG in create mode */ 844 BUG_ON((mode & S_IFMT) != S_IFREG); 845 846 epoch = atomic_read(&fm->fc->epoch); 847 forget = fuse_alloc_forget(); 848 err = -ENOMEM; 849 if (!forget) 850 goto out_err; 851 852 ff = fuse_file_alloc(fm, true); 853 if (!ff) 854 goto out_put_forget_req; 855 856 if (!fm->fc->dont_mask) 857 mode &= ~current_umask(); 858 859 flags &= ~O_NOCTTY; 860 memset(&inarg, 0, sizeof(inarg)); 861 memset(&outentry, 0, sizeof(outentry)); 862 inarg.flags = flags; 863 inarg.mode = mode; 864 inarg.umask = current_umask(); 865 866 if (fm->fc->handle_killpriv_v2 && trunc && 867 !(flags & O_EXCL) && !capable(CAP_FSETID)) { 868 inarg.open_flags |= FUSE_OPEN_KILL_SUIDGID; 869 } 870 871 args.opcode = opcode; 872 args.nodeid = get_node_id(dir); 873 args.in_numargs = 2; 874 args.in_args[0].size = sizeof(inarg); 875 args.in_args[0].value = &inarg; 876 args.in_args[1].size = entry->d_name.len + 1; 877 args.in_args[1].value = entry->d_name.name; 878 args.out_numargs = 2; 879 args.out_args[0].size = sizeof(outentry); 880 args.out_args[0].value = &outentry; 881 /* Store outarg for fuse_finish_open() */ 882 outopenp = &ff->args->open_outarg; 883 args.out_argvar = true; /* compat */ 884 args.out_args[1].size = sizeof(*outopenp); 885 args.out_args[1].value = outopenp; 886 887 err = get_create_ext(idmap, &args, dir, entry, mode); 888 if (err) 889 goto out_free_ff; 890 891 err = fuse_simple_idmap_request(idmap, fm, &args); 892 free_ext_value(&args); 893 if (err < 0) 894 goto out_free_ff; 895 896 err = -EIO; 897 if (!S_ISREG(outentry.attr.mode) || invalid_nodeid(outentry.nodeid) || 898 fuse_invalid_attr(&outentry.attr)) 899 goto out_free_ff; 900 901 ff->fh = outopenp->fh; 902 ff->nodeid = outentry.nodeid; 903 ff->open_flags = outopenp->open_flags; 904 inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation, 905 &outentry.attr, ATTR_TIMEOUT(&outentry), 0, 0); 906 if (!inode) { 907 flags &= ~(O_CREAT | O_EXCL | O_TRUNC); 908 fuse_sync_release(NULL, ff, flags); 909 fuse_chan_queue_forget(fm->fc->chan, forget, outentry.nodeid, 1); 910 err = -ENOMEM; 911 goto out_err; 912 } 913 kfree(forget); 914 fuse_dentry_set_epoch(entry, epoch); 915 d_instantiate(entry, inode); 916 fuse_change_entry_timeout(entry, &outentry); 917 fuse_dir_changed(dir); 918 err = generic_file_open(inode, file); 919 if (!err) { 920 file->private_data = ff; 921 err = finish_open(file, entry, fuse_finish_open); 922 } 923 if (err) { 924 fi = get_fuse_inode(inode); 925 fuse_sync_release(fi, ff, flags); 926 } else { 927 if (fm->fc->atomic_o_trunc && trunc) 928 truncate_pagecache(inode, 0); 929 else if (!(ff->open_flags & FOPEN_KEEP_CACHE)) 930 invalidate_inode_pages2(inode->i_mapping); 931 } 932 return err; 933 934 out_free_ff: 935 fuse_file_free(ff); 936 out_put_forget_req: 937 kfree(forget); 938 out_err: 939 return err; 940 } 941 942 static int fuse_mknod(const struct mnt_idmap *, struct inode *, struct dentry *, 943 umode_t, dev_t); 944 static int fuse_atomic_open(struct inode *dir, struct dentry *entry, 945 struct file *file, unsigned flags, 946 umode_t mode) 947 { 948 int err; 949 const struct mnt_idmap *idmap = file_mnt_idmap(file); 950 struct fuse_conn *fc = get_fuse_conn(dir); 951 952 if (fuse_is_bad(dir)) 953 return -EIO; 954 955 if (d_in_lookup(entry)) { 956 struct dentry *res = fuse_lookup(dir, entry, 0); 957 if (res || d_really_is_positive(entry)) 958 return finish_no_open(file, res); 959 } 960 961 if (!(flags & O_CREAT)) 962 return finish_no_open(file, NULL); 963 964 /* Only creates */ 965 file->f_mode |= FMODE_CREATED; 966 967 if (fc->no_create) 968 goto mknod; 969 970 err = fuse_create_open(idmap, dir, entry, file, flags, mode, FUSE_CREATE); 971 if (err == -ENOSYS) { 972 fc->no_create = 1; 973 goto mknod; 974 } else if (err == -EEXIST) 975 fuse_invalidate_entry(entry); 976 return err; 977 978 mknod: 979 err = fuse_mknod(idmap, dir, entry, mode, 0); 980 if (err) 981 return err; 982 return finish_no_open(file, NULL); 983 } 984 985 /* 986 * Code shared between mknod, mkdir, symlink and link 987 */ 988 static struct dentry *create_new_entry(const struct mnt_idmap *idmap, struct fuse_mount *fm, 989 struct fuse_args *args, struct inode *dir, 990 struct dentry *entry, umode_t mode) 991 { 992 struct fuse_entry_out outarg; 993 struct inode *inode; 994 struct dentry *d; 995 struct fuse_forget_link *forget; 996 int epoch, err; 997 998 if (fuse_is_bad(dir)) 999 return ERR_PTR(-EIO); 1000 1001 epoch = atomic_read(&fm->fc->epoch); 1002 1003 forget = fuse_alloc_forget(); 1004 if (!forget) 1005 return ERR_PTR(-ENOMEM); 1006 1007 memset(&outarg, 0, sizeof(outarg)); 1008 args->nodeid = get_node_id(dir); 1009 args->out_numargs = 1; 1010 args->out_args[0].size = sizeof(outarg); 1011 args->out_args[0].value = &outarg; 1012 1013 if (args->opcode != FUSE_LINK) { 1014 err = get_create_ext(idmap, args, dir, entry, mode); 1015 if (err) 1016 goto out_put_forget_req; 1017 } 1018 1019 err = fuse_simple_idmap_request(idmap, fm, args); 1020 free_ext_value(args); 1021 if (err) 1022 goto out_put_forget_req; 1023 1024 err = -EIO; 1025 if (invalid_nodeid(outarg.nodeid) || fuse_invalid_attr(&outarg.attr)) 1026 goto out_put_forget_req; 1027 1028 if ((outarg.attr.mode ^ mode) & S_IFMT) 1029 goto out_put_forget_req; 1030 1031 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation, 1032 &outarg.attr, ATTR_TIMEOUT(&outarg), 0, 0); 1033 if (!inode) { 1034 fuse_chan_queue_forget(fm->fc->chan, forget, outarg.nodeid, 1); 1035 return ERR_PTR(-ENOMEM); 1036 } 1037 fuse_dentry_set_epoch(entry, epoch); 1038 kfree(forget); 1039 1040 d = d_splice_alias(inode, entry); 1041 if (IS_ERR(d)) 1042 return d; 1043 1044 if (d) { 1045 fuse_dentry_set_epoch(d, epoch); 1046 fuse_change_entry_timeout(d, &outarg); 1047 } else { 1048 fuse_change_entry_timeout(entry, &outarg); 1049 } 1050 fuse_dir_changed(dir); 1051 return d; 1052 1053 out_put_forget_req: 1054 if (err == -EEXIST) 1055 fuse_invalidate_entry(entry); 1056 kfree(forget); 1057 return ERR_PTR(err); 1058 } 1059 1060 static int create_new_nondir(const struct mnt_idmap *idmap, struct fuse_mount *fm, 1061 struct fuse_args *args, struct inode *dir, 1062 struct dentry *entry, umode_t mode) 1063 { 1064 /* 1065 * Note that when creating anything other than a directory we 1066 * can be sure create_new_entry() will NOT return an alternate 1067 * dentry as d_splice_alias() only returns an alternate dentry 1068 * for directories. So we don't need to check for that case 1069 * when passing back the result. 1070 */ 1071 WARN_ON_ONCE(S_ISDIR(mode)); 1072 1073 return PTR_ERR(create_new_entry(idmap, fm, args, dir, entry, mode)); 1074 } 1075 1076 static int fuse_mknod(const struct mnt_idmap *idmap, struct inode *dir, 1077 struct dentry *entry, umode_t mode, dev_t rdev) 1078 { 1079 struct fuse_mknod_in inarg; 1080 struct fuse_mount *fm = get_fuse_mount(dir); 1081 FUSE_ARGS(args); 1082 1083 if (!fm->fc->dont_mask) 1084 mode &= ~current_umask(); 1085 1086 memset(&inarg, 0, sizeof(inarg)); 1087 inarg.mode = mode; 1088 inarg.rdev = new_encode_dev(rdev); 1089 inarg.umask = current_umask(); 1090 args.opcode = FUSE_MKNOD; 1091 args.in_numargs = 2; 1092 args.in_args[0].size = sizeof(inarg); 1093 args.in_args[0].value = &inarg; 1094 args.in_args[1].size = entry->d_name.len + 1; 1095 args.in_args[1].value = entry->d_name.name; 1096 return create_new_nondir(idmap, fm, &args, dir, entry, mode); 1097 } 1098 1099 static int fuse_create(const struct mnt_idmap *idmap, struct inode *dir, 1100 struct dentry *entry, umode_t mode) 1101 { 1102 return fuse_mknod(idmap, dir, entry, mode, 0); 1103 } 1104 1105 static int fuse_tmpfile(const struct mnt_idmap *idmap, struct inode *dir, 1106 struct file *file, umode_t mode) 1107 { 1108 struct fuse_conn *fc = get_fuse_conn(dir); 1109 int err; 1110 1111 if (fc->no_tmpfile) 1112 return -EOPNOTSUPP; 1113 1114 err = fuse_create_open(idmap, dir, file->f_path.dentry, file, 1115 file->f_flags, mode, FUSE_TMPFILE); 1116 if (err == -ENOSYS) { 1117 fc->no_tmpfile = 1; 1118 err = -EOPNOTSUPP; 1119 } 1120 return err; 1121 } 1122 1123 static struct dentry *fuse_mkdir(const struct mnt_idmap *idmap, struct inode *dir, 1124 struct dentry *entry, umode_t mode) 1125 { 1126 struct fuse_mkdir_in inarg; 1127 struct fuse_mount *fm = get_fuse_mount(dir); 1128 FUSE_ARGS(args); 1129 1130 if (!fm->fc->dont_mask) 1131 mode &= ~current_umask(); 1132 1133 /* 1134 * vfs_mkdir() now passes S_IFDIR in @mode, but @mode is forwarded 1135 * verbatim to the userspace server which has only ever been given the 1136 * permission bits. Strip the type bit until the protocol is known to 1137 * cope with it. 1138 */ 1139 mode &= ~S_IFDIR; 1140 1141 memset(&inarg, 0, sizeof(inarg)); 1142 inarg.mode = mode; 1143 inarg.umask = current_umask(); 1144 args.opcode = FUSE_MKDIR; 1145 args.in_numargs = 2; 1146 args.in_args[0].size = sizeof(inarg); 1147 args.in_args[0].value = &inarg; 1148 args.in_args[1].size = entry->d_name.len + 1; 1149 args.in_args[1].value = entry->d_name.name; 1150 return create_new_entry(idmap, fm, &args, dir, entry, S_IFDIR); 1151 } 1152 1153 static int fuse_symlink(const struct mnt_idmap *idmap, struct inode *dir, 1154 struct dentry *entry, const char *link) 1155 { 1156 struct fuse_mount *fm = get_fuse_mount(dir); 1157 unsigned len = strlen(link) + 1; 1158 FUSE_ARGS(args); 1159 1160 args.opcode = FUSE_SYMLINK; 1161 args.in_numargs = 3; 1162 fuse_set_zero_arg0(&args); 1163 args.in_args[1].size = entry->d_name.len + 1; 1164 args.in_args[1].value = entry->d_name.name; 1165 args.in_args[2].size = len; 1166 args.in_args[2].value = link; 1167 return create_new_nondir(idmap, fm, &args, dir, entry, S_IFLNK); 1168 } 1169 1170 void fuse_flush_time_update(struct inode *inode) 1171 { 1172 int err = sync_inode_metadata(inode, 1); 1173 1174 mapping_set_error(inode->i_mapping, err); 1175 } 1176 1177 static void fuse_update_ctime_in_cache(struct inode *inode) 1178 { 1179 if (!IS_NOCMTIME(inode)) { 1180 inode_set_ctime_current(inode); 1181 mark_inode_dirty_sync(inode); 1182 fuse_flush_time_update(inode); 1183 } 1184 } 1185 1186 void fuse_update_ctime(struct inode *inode) 1187 { 1188 fuse_invalidate_attr_mask(inode, STATX_CTIME); 1189 fuse_update_ctime_in_cache(inode); 1190 } 1191 1192 static void fuse_entry_unlinked(struct dentry *entry) 1193 { 1194 struct inode *inode = d_inode(entry); 1195 struct fuse_conn *fc = get_fuse_conn(inode); 1196 struct fuse_inode *fi = get_fuse_inode(inode); 1197 1198 spin_lock(&fi->lock); 1199 fi->attr_version = atomic64_inc_return(&fc->attr_version); 1200 /* 1201 * If i_nlink == 0 then unlink doesn't make sense, yet this can 1202 * happen if userspace filesystem is careless. It would be 1203 * difficult to enforce correct nlink usage so just ignore this 1204 * condition here 1205 */ 1206 if (S_ISDIR(inode->i_mode)) 1207 clear_nlink(inode); 1208 else if (inode->i_nlink > 0) 1209 drop_nlink(inode); 1210 spin_unlock(&fi->lock); 1211 fuse_invalidate_entry_cache(entry); 1212 fuse_update_ctime(inode); 1213 } 1214 1215 static int fuse_unlink(struct inode *dir, struct dentry *entry) 1216 { 1217 int err; 1218 struct fuse_mount *fm = get_fuse_mount(dir); 1219 FUSE_ARGS(args); 1220 1221 if (fuse_is_bad(dir)) 1222 return -EIO; 1223 1224 args.opcode = FUSE_UNLINK; 1225 args.nodeid = get_node_id(dir); 1226 args.in_numargs = 2; 1227 fuse_set_zero_arg0(&args); 1228 args.in_args[1].size = entry->d_name.len + 1; 1229 args.in_args[1].value = entry->d_name.name; 1230 err = fuse_simple_request(fm, &args); 1231 if (!err) { 1232 fuse_dir_changed(dir); 1233 fuse_entry_unlinked(entry); 1234 } else if (err == -EINTR || err == -ENOENT) 1235 fuse_invalidate_entry(entry); 1236 return err; 1237 } 1238 1239 static int fuse_rmdir(struct inode *dir, struct dentry *entry) 1240 { 1241 int err; 1242 struct fuse_mount *fm = get_fuse_mount(dir); 1243 FUSE_ARGS(args); 1244 1245 if (fuse_is_bad(dir)) 1246 return -EIO; 1247 1248 args.opcode = FUSE_RMDIR; 1249 args.nodeid = get_node_id(dir); 1250 args.in_numargs = 2; 1251 fuse_set_zero_arg0(&args); 1252 args.in_args[1].size = entry->d_name.len + 1; 1253 args.in_args[1].value = entry->d_name.name; 1254 err = fuse_simple_request(fm, &args); 1255 if (!err) { 1256 fuse_dir_changed(dir); 1257 fuse_entry_unlinked(entry); 1258 } else if (err == -EINTR || err == -ENOENT) 1259 fuse_invalidate_entry(entry); 1260 return err; 1261 } 1262 1263 static int fuse_rename_common(const struct mnt_idmap *idmap, struct inode *olddir, 1264 struct dentry *oldent, struct inode *newdir, 1265 struct dentry *newent, unsigned int flags, 1266 int opcode, size_t argsize) 1267 { 1268 int err; 1269 struct fuse_rename2_in inarg; 1270 struct fuse_mount *fm = get_fuse_mount(olddir); 1271 FUSE_ARGS(args); 1272 1273 memset(&inarg, 0, argsize); 1274 inarg.newdir = get_node_id(newdir); 1275 inarg.flags = flags; 1276 args.opcode = opcode; 1277 args.nodeid = get_node_id(olddir); 1278 args.in_numargs = 3; 1279 args.in_args[0].size = argsize; 1280 args.in_args[0].value = &inarg; 1281 args.in_args[1].size = oldent->d_name.len + 1; 1282 args.in_args[1].value = oldent->d_name.name; 1283 args.in_args[2].size = newent->d_name.len + 1; 1284 args.in_args[2].value = newent->d_name.name; 1285 err = fuse_simple_idmap_request(idmap, fm, &args); 1286 if (!err) { 1287 /* ctime changes */ 1288 fuse_update_ctime(d_inode(oldent)); 1289 1290 if (flags & RENAME_EXCHANGE) 1291 fuse_update_ctime(d_inode(newent)); 1292 1293 fuse_dir_changed(olddir); 1294 if (olddir != newdir) 1295 fuse_dir_changed(newdir); 1296 1297 /* newent will end up negative */ 1298 if (!(flags & RENAME_EXCHANGE) && d_really_is_positive(newent)) 1299 fuse_entry_unlinked(newent); 1300 } else if (err == -EINTR || err == -ENOENT) { 1301 /* If request was interrupted, DEITY only knows if the 1302 rename actually took place. If the invalidation 1303 fails (e.g. some process has CWD under the renamed 1304 directory), then there can be inconsistency between 1305 the dcache and the real filesystem. Tough luck. */ 1306 fuse_invalidate_entry(oldent); 1307 if (d_really_is_positive(newent)) 1308 fuse_invalidate_entry(newent); 1309 } 1310 1311 return err; 1312 } 1313 1314 static int fuse_rename2(const struct mnt_idmap *idmap, struct inode *olddir, 1315 struct dentry *oldent, struct inode *newdir, 1316 struct dentry *newent, unsigned int flags) 1317 { 1318 struct fuse_conn *fc = get_fuse_conn(olddir); 1319 int err; 1320 1321 if (fuse_is_bad(olddir)) 1322 return -EIO; 1323 1324 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) 1325 return -EINVAL; 1326 1327 if (flags) { 1328 if (fc->no_rename2 || fc->minor < 23) 1329 return -EINVAL; 1330 1331 err = fuse_rename_common((flags & RENAME_WHITEOUT) ? idmap : &invalid_mnt_idmap, 1332 olddir, oldent, newdir, newent, flags, 1333 FUSE_RENAME2, 1334 sizeof(struct fuse_rename2_in)); 1335 if (err == -ENOSYS) { 1336 fc->no_rename2 = 1; 1337 err = -EINVAL; 1338 } 1339 } else { 1340 err = fuse_rename_common(&invalid_mnt_idmap, olddir, oldent, newdir, newent, 0, 1341 FUSE_RENAME, 1342 sizeof(struct fuse_rename_in)); 1343 } 1344 1345 return err; 1346 } 1347 1348 static int fuse_link(struct dentry *entry, struct inode *newdir, 1349 struct dentry *newent) 1350 { 1351 int err; 1352 struct fuse_link_in inarg; 1353 struct inode *inode = d_inode(entry); 1354 struct fuse_mount *fm = get_fuse_mount(inode); 1355 FUSE_ARGS(args); 1356 1357 if (fm->fc->no_link) 1358 goto out; 1359 1360 memset(&inarg, 0, sizeof(inarg)); 1361 inarg.oldnodeid = get_node_id(inode); 1362 args.opcode = FUSE_LINK; 1363 args.in_numargs = 2; 1364 args.in_args[0].size = sizeof(inarg); 1365 args.in_args[0].value = &inarg; 1366 args.in_args[1].size = newent->d_name.len + 1; 1367 args.in_args[1].value = newent->d_name.name; 1368 err = create_new_nondir(&invalid_mnt_idmap, fm, &args, newdir, newent, inode->i_mode); 1369 if (!err) 1370 fuse_update_ctime_in_cache(inode); 1371 else if (err == -EINTR) 1372 fuse_invalidate_attr(inode); 1373 1374 if (err == -ENOSYS) 1375 fm->fc->no_link = 1; 1376 out: 1377 if (fm->fc->no_link) 1378 return -EPERM; 1379 1380 return err; 1381 } 1382 1383 static void fuse_fillattr(const struct mnt_idmap *idmap, struct inode *inode, 1384 struct fuse_attr *attr, struct kstat *stat) 1385 { 1386 unsigned int blkbits; 1387 struct fuse_conn *fc = get_fuse_conn(inode); 1388 vfsuid_t vfsuid = make_vfsuid(idmap, fc->user_ns, 1389 make_kuid(fc->user_ns, attr->uid)); 1390 vfsgid_t vfsgid = make_vfsgid(idmap, fc->user_ns, 1391 make_kgid(fc->user_ns, attr->gid)); 1392 1393 stat->dev = inode->i_sb->s_dev; 1394 stat->ino = attr->ino; 1395 stat->mode = (inode->i_mode & S_IFMT) | (attr->mode & 07777); 1396 stat->nlink = attr->nlink; 1397 stat->uid = vfsuid_into_kuid(vfsuid); 1398 stat->gid = vfsgid_into_kgid(vfsgid); 1399 stat->rdev = inode->i_rdev; 1400 stat->atime.tv_sec = attr->atime; 1401 stat->atime.tv_nsec = attr->atimensec; 1402 stat->mtime.tv_sec = attr->mtime; 1403 stat->mtime.tv_nsec = attr->mtimensec; 1404 stat->ctime.tv_sec = attr->ctime; 1405 stat->ctime.tv_nsec = attr->ctimensec; 1406 stat->size = attr->size; 1407 stat->blocks = attr->blocks; 1408 1409 if (attr->blksize != 0) 1410 blkbits = ilog2(attr->blksize); 1411 else 1412 blkbits = inode->i_sb->s_blocksize_bits; 1413 1414 stat->blksize = 1 << blkbits; 1415 } 1416 1417 static void fuse_statx_to_attr(struct fuse_statx *sx, struct fuse_attr *attr) 1418 { 1419 memset(attr, 0, sizeof(*attr)); 1420 attr->ino = sx->ino; 1421 attr->size = sx->size; 1422 attr->blocks = sx->blocks; 1423 attr->atime = sx->atime.tv_sec; 1424 attr->mtime = sx->mtime.tv_sec; 1425 attr->ctime = sx->ctime.tv_sec; 1426 attr->atimensec = sx->atime.tv_nsec; 1427 attr->mtimensec = sx->mtime.tv_nsec; 1428 attr->ctimensec = sx->ctime.tv_nsec; 1429 attr->mode = sx->mode; 1430 attr->nlink = sx->nlink; 1431 attr->uid = sx->uid; 1432 attr->gid = sx->gid; 1433 attr->rdev = new_encode_dev(MKDEV(sx->rdev_major, sx->rdev_minor)); 1434 attr->blksize = sx->blksize; 1435 } 1436 1437 static int fuse_do_statx(const struct mnt_idmap *idmap, struct inode *inode, 1438 struct file *file, struct kstat *stat) 1439 { 1440 int err; 1441 struct fuse_attr attr; 1442 struct fuse_statx *sx; 1443 struct fuse_statx_in inarg; 1444 struct fuse_statx_out outarg; 1445 struct fuse_mount *fm = get_fuse_mount(inode); 1446 u64 attr_version = fuse_get_attr_version(fm->fc); 1447 FUSE_ARGS(args); 1448 1449 memset(&inarg, 0, sizeof(inarg)); 1450 memset(&outarg, 0, sizeof(outarg)); 1451 /* Directories have separate file-handle space */ 1452 if (file && S_ISREG(inode->i_mode)) { 1453 struct fuse_file *ff = file->private_data; 1454 1455 inarg.getattr_flags |= FUSE_GETATTR_FH; 1456 inarg.fh = ff->fh; 1457 } 1458 /* For now leave sync hints as the default, request all stats. */ 1459 inarg.sx_flags = 0; 1460 inarg.sx_mask = STATX_BASIC_STATS | STATX_BTIME; 1461 args.opcode = FUSE_STATX; 1462 args.nodeid = get_node_id(inode); 1463 args.in_numargs = 1; 1464 args.in_args[0].size = sizeof(inarg); 1465 args.in_args[0].value = &inarg; 1466 args.out_numargs = 1; 1467 args.out_args[0].size = sizeof(outarg); 1468 args.out_args[0].value = &outarg; 1469 err = fuse_simple_request(fm, &args); 1470 if (err) 1471 return err; 1472 1473 sx = &outarg.stat; 1474 if (((sx->mask & STATX_SIZE) && !fuse_valid_size(sx->size)) || 1475 ((sx->mask & STATX_TYPE) && (!fuse_valid_type(sx->mode) || 1476 inode_wrong_type(inode, sx->mode)))) { 1477 fuse_make_bad(inode); 1478 return -EIO; 1479 } 1480 1481 fuse_statx_to_attr(&outarg.stat, &attr); 1482 if ((sx->mask & STATX_BASIC_STATS) == STATX_BASIC_STATS) { 1483 fuse_change_attributes(inode, &attr, &outarg.stat, 1484 ATTR_TIMEOUT(&outarg), attr_version); 1485 } 1486 1487 if (stat) { 1488 stat->result_mask = sx->mask & (STATX_BASIC_STATS | STATX_BTIME); 1489 stat->btime.tv_sec = sx->btime.tv_sec; 1490 stat->btime.tv_nsec = min_t(u32, sx->btime.tv_nsec, NSEC_PER_SEC - 1); 1491 fuse_fillattr(idmap, inode, &attr, stat); 1492 stat->result_mask |= STATX_TYPE; 1493 } 1494 1495 return 0; 1496 } 1497 1498 static int fuse_do_getattr(const struct mnt_idmap *idmap, struct inode *inode, 1499 struct kstat *stat, struct file *file) 1500 { 1501 int err; 1502 struct fuse_getattr_in inarg; 1503 struct fuse_attr_out outarg; 1504 struct fuse_mount *fm = get_fuse_mount(inode); 1505 FUSE_ARGS(args); 1506 u64 attr_version; 1507 1508 attr_version = fuse_get_attr_version(fm->fc); 1509 1510 memset(&inarg, 0, sizeof(inarg)); 1511 memset(&outarg, 0, sizeof(outarg)); 1512 /* Directories have separate file-handle space */ 1513 if (file && S_ISREG(inode->i_mode)) { 1514 struct fuse_file *ff = file->private_data; 1515 1516 inarg.getattr_flags |= FUSE_GETATTR_FH; 1517 inarg.fh = ff->fh; 1518 } 1519 args.opcode = FUSE_GETATTR; 1520 args.nodeid = get_node_id(inode); 1521 args.in_numargs = 1; 1522 args.in_args[0].size = sizeof(inarg); 1523 args.in_args[0].value = &inarg; 1524 args.out_numargs = 1; 1525 args.out_args[0].size = sizeof(outarg); 1526 args.out_args[0].value = &outarg; 1527 err = fuse_simple_request(fm, &args); 1528 if (!err) { 1529 if (fuse_invalid_attr(&outarg.attr) || 1530 inode_wrong_type(inode, outarg.attr.mode)) { 1531 fuse_make_bad(inode); 1532 err = -EIO; 1533 } else { 1534 fuse_change_attributes(inode, &outarg.attr, NULL, 1535 ATTR_TIMEOUT(&outarg), 1536 attr_version); 1537 if (stat) 1538 fuse_fillattr(idmap, inode, &outarg.attr, stat); 1539 } 1540 } 1541 return err; 1542 } 1543 1544 static int fuse_update_get_attr(const struct mnt_idmap *idmap, struct inode *inode, 1545 struct file *file, struct kstat *stat, 1546 u32 request_mask, unsigned int flags) 1547 { 1548 struct fuse_inode *fi = get_fuse_inode(inode); 1549 struct fuse_conn *fc = get_fuse_conn(inode); 1550 int err = 0; 1551 bool sync; 1552 u32 inval_mask = READ_ONCE(fi->inval_mask); 1553 u32 cache_mask = fuse_get_cache_mask(inode); 1554 1555 1556 /* FUSE only supports basic stats and possibly btime */ 1557 request_mask &= STATX_BASIC_STATS | STATX_BTIME; 1558 retry: 1559 if (fc->no_statx) 1560 request_mask &= STATX_BASIC_STATS; 1561 1562 if (!request_mask) 1563 sync = false; 1564 else if (flags & AT_STATX_FORCE_SYNC) 1565 sync = true; 1566 else if (flags & AT_STATX_DONT_SYNC) 1567 sync = false; 1568 else if (request_mask & inval_mask & ~cache_mask) 1569 sync = true; 1570 else 1571 sync = time_before64(fi->i_time, get_jiffies_64()); 1572 1573 if (sync) { 1574 forget_all_cached_acls(inode); 1575 /* Try statx if BTIME is requested */ 1576 if (!fc->no_statx && (request_mask & ~STATX_BASIC_STATS)) { 1577 err = fuse_do_statx(idmap, inode, file, stat); 1578 if (err == -ENOSYS) { 1579 fc->no_statx = 1; 1580 err = 0; 1581 goto retry; 1582 } 1583 } else { 1584 err = fuse_do_getattr(idmap, inode, stat, file); 1585 } 1586 } else if (stat) { 1587 generic_fillattr(idmap, request_mask, inode, stat); 1588 stat->mode = fi->orig_i_mode; 1589 stat->ino = fi->orig_ino; 1590 stat->blksize = 1 << fi->cached_i_blkbits; 1591 if (test_bit(FUSE_I_BTIME, &fi->state)) { 1592 stat->btime = fi->i_btime; 1593 stat->result_mask |= STATX_BTIME; 1594 } 1595 } 1596 1597 return err; 1598 } 1599 1600 int fuse_update_attributes(struct inode *inode, struct file *file, u32 mask) 1601 { 1602 return fuse_update_get_attr(&nop_mnt_idmap, inode, file, NULL, mask, 0); 1603 } 1604 1605 int fuse_reverse_inval_entry(struct fuse_conn *fc, u64 parent_nodeid, 1606 u64 child_nodeid, struct qstr *name, u32 flags) 1607 { 1608 int err = -ENOTDIR; 1609 struct inode *parent; 1610 struct dentry *dir; 1611 struct dentry *entry; 1612 1613 parent = fuse_ilookup(fc, parent_nodeid, NULL); 1614 if (!parent) 1615 return -ENOENT; 1616 1617 inode_lock_nested(parent, I_MUTEX_PARENT); 1618 if (!S_ISDIR(parent->i_mode)) 1619 goto unlock; 1620 1621 err = -ENOENT; 1622 dir = d_find_alias(parent); 1623 if (!dir) 1624 goto unlock; 1625 1626 name->hash = full_name_hash(dir, name->name, name->len); 1627 entry = d_lookup(dir, name); 1628 dput(dir); 1629 if (!entry) 1630 goto unlock; 1631 1632 fuse_dir_changed(parent); 1633 if (!(flags & FUSE_EXPIRE_ONLY)) 1634 d_invalidate(entry); 1635 fuse_invalidate_entry_cache(entry); 1636 1637 if (child_nodeid != 0 && d_really_is_positive(entry)) { 1638 inode_lock(d_inode(entry)); 1639 if (get_node_id(d_inode(entry)) != child_nodeid) { 1640 err = -ENOENT; 1641 goto badentry; 1642 } 1643 if (d_mountpoint(entry)) { 1644 err = -EBUSY; 1645 goto badentry; 1646 } 1647 if (d_is_dir(entry)) { 1648 shrink_dcache_parent(entry); 1649 if (!simple_empty(entry)) { 1650 err = -ENOTEMPTY; 1651 goto badentry; 1652 } 1653 d_inode(entry)->i_flags |= S_DEAD; 1654 } 1655 dont_mount(entry); 1656 clear_nlink(d_inode(entry)); 1657 err = 0; 1658 badentry: 1659 inode_unlock(d_inode(entry)); 1660 if (!err) 1661 d_delete(entry); 1662 } else { 1663 err = 0; 1664 } 1665 dput(entry); 1666 1667 unlock: 1668 inode_unlock(parent); 1669 iput(parent); 1670 return err; 1671 } 1672 1673 static inline bool fuse_permissible_uidgid(struct fuse_conn *fc) 1674 { 1675 const struct cred *cred = current_cred(); 1676 1677 return (uid_eq(cred->euid, fc->user_id) && 1678 uid_eq(cred->suid, fc->user_id) && 1679 uid_eq(cred->uid, fc->user_id) && 1680 gid_eq(cred->egid, fc->group_id) && 1681 gid_eq(cred->sgid, fc->group_id) && 1682 gid_eq(cred->gid, fc->group_id)); 1683 } 1684 1685 /* 1686 * Calling into a user-controlled filesystem gives the filesystem 1687 * daemon ptrace-like capabilities over the current process. This 1688 * means, that the filesystem daemon is able to record the exact 1689 * filesystem operations performed, and can also control the behavior 1690 * of the requester process in otherwise impossible ways. For example 1691 * it can delay the operation for arbitrary length of time allowing 1692 * DoS against the requester. 1693 * 1694 * For this reason only those processes can call into the filesystem, 1695 * for which the owner of the mount has ptrace privilege. This 1696 * excludes processes started by other users, suid or sgid processes. 1697 */ 1698 bool fuse_allow_current_process(struct fuse_conn *fc) 1699 { 1700 bool allow; 1701 1702 if (fc->allow_other) 1703 allow = current_in_userns(fc->user_ns); 1704 else 1705 allow = fuse_permissible_uidgid(fc); 1706 1707 if (!allow && allow_sys_admin_access && capable(CAP_SYS_ADMIN)) 1708 allow = true; 1709 1710 return allow; 1711 } 1712 1713 static int fuse_access(struct inode *inode, int mask) 1714 { 1715 struct fuse_mount *fm = get_fuse_mount(inode); 1716 FUSE_ARGS(args); 1717 struct fuse_access_in inarg; 1718 int err; 1719 1720 BUG_ON(mask & MAY_NOT_BLOCK); 1721 1722 /* 1723 * We should not send FUSE_ACCESS to the userspace 1724 * when idmapped mounts are enabled as for this case 1725 * we have fc->default_permissions = 1 and access 1726 * permission checks are done on the kernel side. 1727 */ 1728 WARN_ON_ONCE(!(fm->sb->s_iflags & SB_I_NOIDMAP)); 1729 1730 if (fm->fc->no_access) 1731 return 0; 1732 1733 memset(&inarg, 0, sizeof(inarg)); 1734 inarg.mask = mask & (MAY_READ | MAY_WRITE | MAY_EXEC); 1735 args.opcode = FUSE_ACCESS; 1736 args.nodeid = get_node_id(inode); 1737 args.in_numargs = 1; 1738 args.in_args[0].size = sizeof(inarg); 1739 args.in_args[0].value = &inarg; 1740 err = fuse_simple_request(fm, &args); 1741 if (err == -ENOSYS) { 1742 fm->fc->no_access = 1; 1743 err = 0; 1744 } 1745 return err; 1746 } 1747 1748 static int fuse_perm_getattr(struct inode *inode, int mask) 1749 { 1750 if (mask & MAY_NOT_BLOCK) 1751 return -ECHILD; 1752 1753 forget_all_cached_acls(inode); 1754 return fuse_do_getattr(&nop_mnt_idmap, inode, NULL, NULL); 1755 } 1756 1757 /* 1758 * Check permission. The two basic access models of FUSE are: 1759 * 1760 * 1) Local access checking ('default_permissions' mount option) based 1761 * on file mode. This is the plain old disk filesystem permission 1762 * model. 1763 * 1764 * 2) "Remote" access checking, where server is responsible for 1765 * checking permission in each inode operation. An exception to this 1766 * is if ->permission() was invoked from sys_access() in which case an 1767 * access request is sent. Execute permission is still checked 1768 * locally based on file mode. 1769 */ 1770 static int fuse_permission(const struct mnt_idmap *idmap, 1771 struct inode *inode, int mask) 1772 { 1773 struct fuse_conn *fc = get_fuse_conn(inode); 1774 bool refreshed = false; 1775 int err = 0; 1776 1777 if (fuse_is_bad(inode)) 1778 return -EIO; 1779 1780 if (!fuse_allow_current_process(fc)) 1781 return -EACCES; 1782 1783 /* 1784 * If attributes are needed, refresh them before proceeding 1785 */ 1786 if (fc->default_permissions || 1787 ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))) { 1788 struct fuse_inode *fi = get_fuse_inode(inode); 1789 u32 perm_mask = STATX_MODE | STATX_UID | STATX_GID; 1790 1791 if (perm_mask & READ_ONCE(fi->inval_mask) || 1792 time_before64(fi->i_time, get_jiffies_64())) { 1793 refreshed = true; 1794 1795 err = fuse_perm_getattr(inode, mask); 1796 if (err) 1797 return err; 1798 } 1799 } 1800 1801 if (fc->default_permissions) { 1802 err = generic_permission(idmap, inode, mask); 1803 1804 /* If permission is denied, try to refresh file 1805 attributes. This is also needed, because the root 1806 node will at first have no permissions */ 1807 if (err == -EACCES && !refreshed) { 1808 err = fuse_perm_getattr(inode, mask); 1809 if (!err) 1810 err = generic_permission(idmap, 1811 inode, mask); 1812 } 1813 1814 /* Note: the opposite of the above test does not 1815 exist. So if permissions are revoked this won't be 1816 noticed immediately, only after the attribute 1817 timeout has expired */ 1818 } else if (mask & (MAY_ACCESS | MAY_CHDIR)) { 1819 err = fuse_access(inode, mask); 1820 } else if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) { 1821 if (!(inode->i_mode & S_IXUGO)) { 1822 if (refreshed) 1823 return -EACCES; 1824 1825 err = fuse_perm_getattr(inode, mask); 1826 if (!err && !(inode->i_mode & S_IXUGO)) 1827 return -EACCES; 1828 } 1829 } 1830 return err; 1831 } 1832 1833 static int fuse_readlink_folio(struct inode *inode, struct folio *folio) 1834 { 1835 struct fuse_mount *fm = get_fuse_mount(inode); 1836 struct fuse_folio_desc desc = { .length = folio_size(folio) - 1 }; 1837 struct fuse_args_pages ap = { 1838 .num_folios = 1, 1839 .folios = &folio, 1840 .descs = &desc, 1841 }; 1842 char *link; 1843 ssize_t res; 1844 1845 ap.args.opcode = FUSE_READLINK; 1846 ap.args.nodeid = get_node_id(inode); 1847 ap.args.out_pages = true; 1848 ap.args.out_argvar = true; 1849 ap.args.page_zeroing = true; 1850 ap.args.out_numargs = 1; 1851 ap.args.out_args[0].size = desc.length; 1852 res = fuse_simple_request(fm, &ap.args); 1853 1854 fuse_invalidate_atime(inode); 1855 1856 if (res < 0) 1857 return res; 1858 1859 if (WARN_ON(res >= PAGE_SIZE)) 1860 return -EIO; 1861 1862 link = folio_address(folio); 1863 link[res] = '\0'; 1864 1865 return 0; 1866 } 1867 1868 static const char *fuse_get_link(struct dentry *dentry, struct inode *inode, 1869 struct delayed_call *callback) 1870 { 1871 struct fuse_conn *fc = get_fuse_conn(inode); 1872 struct folio *folio; 1873 int err; 1874 1875 err = -EIO; 1876 if (fuse_is_bad(inode)) 1877 goto out_err; 1878 1879 if (fc->cache_symlinks) 1880 return page_get_link_raw(dentry, inode, callback); 1881 1882 err = -ECHILD; 1883 if (!dentry) 1884 goto out_err; 1885 1886 folio = folio_alloc(GFP_KERNEL, 0); 1887 err = -ENOMEM; 1888 if (!folio) 1889 goto out_err; 1890 1891 err = fuse_readlink_folio(inode, folio); 1892 if (err) { 1893 folio_put(folio); 1894 goto out_err; 1895 } 1896 1897 set_delayed_call(callback, page_put_link, folio); 1898 1899 return folio_address(folio); 1900 1901 out_err: 1902 return ERR_PTR(err); 1903 } 1904 1905 static int fuse_dir_open(struct inode *inode, struct file *file) 1906 { 1907 struct fuse_mount *fm = get_fuse_mount(inode); 1908 int err; 1909 1910 if (fuse_is_bad(inode)) 1911 return -EIO; 1912 1913 err = generic_file_open(inode, file); 1914 if (err) 1915 return err; 1916 1917 err = fuse_do_open(fm, get_node_id(inode), file, true); 1918 if (!err) { 1919 struct fuse_file *ff = file->private_data; 1920 1921 /* 1922 * Keep handling FOPEN_STREAM and FOPEN_NONSEEKABLE for 1923 * directories for backward compatibility, though it's unlikely 1924 * to be useful. 1925 */ 1926 if (ff->open_flags & (FOPEN_STREAM | FOPEN_NONSEEKABLE)) 1927 nonseekable_open(inode, file); 1928 if (!(ff->open_flags & FOPEN_KEEP_CACHE)) 1929 invalidate_inode_pages2(inode->i_mapping); 1930 } 1931 1932 return err; 1933 } 1934 1935 static int fuse_dir_release(struct inode *inode, struct file *file) 1936 { 1937 fuse_release_common(file, true); 1938 1939 return 0; 1940 } 1941 1942 static int fuse_dir_fsync(struct file *file, loff_t start, loff_t end, 1943 int datasync) 1944 { 1945 struct inode *inode = file->f_mapping->host; 1946 struct fuse_conn *fc = get_fuse_conn(inode); 1947 int err; 1948 1949 if (fuse_is_bad(inode)) 1950 return -EIO; 1951 1952 if (fc->no_fsyncdir) 1953 return 0; 1954 1955 inode_lock(inode); 1956 err = fuse_fsync_common(file, start, end, datasync, FUSE_FSYNCDIR); 1957 if (err == -ENOSYS) { 1958 fc->no_fsyncdir = 1; 1959 err = 0; 1960 } 1961 inode_unlock(inode); 1962 1963 return err; 1964 } 1965 1966 static long fuse_dir_ioctl(struct file *file, unsigned int cmd, 1967 unsigned long arg) 1968 { 1969 struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host); 1970 1971 /* FUSE_IOCTL_DIR only supported for API version >= 7.18 */ 1972 if (fc->minor < 18) 1973 return -ENOTTY; 1974 1975 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_DIR); 1976 } 1977 1978 static long fuse_dir_compat_ioctl(struct file *file, unsigned int cmd, 1979 unsigned long arg) 1980 { 1981 struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host); 1982 1983 if (fc->minor < 18) 1984 return -ENOTTY; 1985 1986 return fuse_ioctl_common(file, cmd, arg, 1987 FUSE_IOCTL_COMPAT | FUSE_IOCTL_DIR); 1988 } 1989 1990 static bool update_mtime(unsigned ivalid, bool trust_local_mtime) 1991 { 1992 /* Always update if mtime is explicitly set */ 1993 if (ivalid & ATTR_MTIME_SET) 1994 return true; 1995 1996 /* Or if kernel i_mtime is the official one */ 1997 if (trust_local_mtime) 1998 return true; 1999 2000 /* If it's an open(O_TRUNC) or an ftruncate(), don't update */ 2001 if ((ivalid & ATTR_SIZE) && (ivalid & (ATTR_OPEN | ATTR_FILE))) 2002 return false; 2003 2004 /* In all other cases update */ 2005 return true; 2006 } 2007 2008 static void iattr_to_fattr(const struct mnt_idmap *idmap, struct fuse_conn *fc, 2009 struct iattr *iattr, struct fuse_setattr_in *arg, 2010 bool trust_local_cmtime) 2011 { 2012 unsigned ivalid = iattr->ia_valid; 2013 2014 if (ivalid & ATTR_MODE) 2015 arg->valid |= FATTR_MODE, arg->mode = iattr->ia_mode; 2016 2017 if (ivalid & ATTR_UID) { 2018 kuid_t fsuid = from_vfsuid(idmap, fc->user_ns, iattr->ia_vfsuid); 2019 2020 arg->valid |= FATTR_UID; 2021 arg->uid = from_kuid(fc->user_ns, fsuid); 2022 } 2023 2024 if (ivalid & ATTR_GID) { 2025 kgid_t fsgid = from_vfsgid(idmap, fc->user_ns, iattr->ia_vfsgid); 2026 2027 arg->valid |= FATTR_GID; 2028 arg->gid = from_kgid(fc->user_ns, fsgid); 2029 } 2030 2031 if (ivalid & ATTR_SIZE) 2032 arg->valid |= FATTR_SIZE, arg->size = iattr->ia_size; 2033 if (ivalid & ATTR_ATIME) { 2034 arg->valid |= FATTR_ATIME; 2035 arg->atime = iattr->ia_atime.tv_sec; 2036 arg->atimensec = iattr->ia_atime.tv_nsec; 2037 if (!(ivalid & ATTR_ATIME_SET)) 2038 arg->valid |= FATTR_ATIME_NOW; 2039 } 2040 if ((ivalid & ATTR_MTIME) && update_mtime(ivalid, trust_local_cmtime)) { 2041 arg->valid |= FATTR_MTIME; 2042 arg->mtime = iattr->ia_mtime.tv_sec; 2043 arg->mtimensec = iattr->ia_mtime.tv_nsec; 2044 if (!(ivalid & ATTR_MTIME_SET) && !trust_local_cmtime) 2045 arg->valid |= FATTR_MTIME_NOW; 2046 } 2047 if ((ivalid & ATTR_CTIME) && trust_local_cmtime) { 2048 arg->valid |= FATTR_CTIME; 2049 arg->ctime = iattr->ia_ctime.tv_sec; 2050 arg->ctimensec = iattr->ia_ctime.tv_nsec; 2051 } 2052 } 2053 2054 /* 2055 * Prevent concurrent writepages on inode 2056 * 2057 * This is done by adding a negative bias to the inode write counter 2058 * and waiting for all pending writes to finish. 2059 */ 2060 void fuse_set_nowrite(struct inode *inode) 2061 { 2062 struct fuse_inode *fi = get_fuse_inode(inode); 2063 2064 BUG_ON(!inode_is_locked(inode)); 2065 2066 spin_lock(&fi->lock); 2067 BUG_ON(fi->writectr < 0); 2068 fi->writectr += FUSE_NOWRITE; 2069 spin_unlock(&fi->lock); 2070 wait_event(fi->page_waitq, fi->writectr == FUSE_NOWRITE); 2071 } 2072 2073 /* 2074 * Allow writepages on inode 2075 * 2076 * Remove the bias from the writecounter and send any queued 2077 * writepages. 2078 */ 2079 static void __fuse_release_nowrite(struct inode *inode) 2080 { 2081 struct fuse_inode *fi = get_fuse_inode(inode); 2082 2083 BUG_ON(fi->writectr != FUSE_NOWRITE); 2084 fi->writectr = 0; 2085 fuse_flush_writepages(inode); 2086 } 2087 2088 void fuse_release_nowrite(struct inode *inode) 2089 { 2090 struct fuse_inode *fi = get_fuse_inode(inode); 2091 2092 spin_lock(&fi->lock); 2093 __fuse_release_nowrite(inode); 2094 spin_unlock(&fi->lock); 2095 } 2096 2097 static void fuse_setattr_fill(struct fuse_conn *fc, struct fuse_args *args, 2098 struct inode *inode, 2099 struct fuse_setattr_in *inarg_p, 2100 struct fuse_attr_out *outarg_p) 2101 { 2102 args->opcode = FUSE_SETATTR; 2103 args->nodeid = get_node_id(inode); 2104 args->in_numargs = 1; 2105 args->in_args[0].size = sizeof(*inarg_p); 2106 args->in_args[0].value = inarg_p; 2107 args->out_numargs = 1; 2108 args->out_args[0].size = sizeof(*outarg_p); 2109 args->out_args[0].value = outarg_p; 2110 } 2111 2112 /* 2113 * Flush inode->i_mtime to the server 2114 */ 2115 int fuse_flush_times(struct inode *inode, struct fuse_file *ff) 2116 { 2117 struct fuse_mount *fm = get_fuse_mount(inode); 2118 FUSE_ARGS(args); 2119 struct fuse_setattr_in inarg; 2120 struct fuse_attr_out outarg; 2121 2122 memset(&inarg, 0, sizeof(inarg)); 2123 memset(&outarg, 0, sizeof(outarg)); 2124 2125 inarg.valid = FATTR_MTIME; 2126 inarg.mtime = inode_get_mtime_sec(inode); 2127 inarg.mtimensec = inode_get_mtime_nsec(inode); 2128 if (fm->fc->minor >= 23) { 2129 inarg.valid |= FATTR_CTIME; 2130 inarg.ctime = inode_get_ctime_sec(inode); 2131 inarg.ctimensec = inode_get_ctime_nsec(inode); 2132 } 2133 if (ff) { 2134 inarg.valid |= FATTR_FH; 2135 inarg.fh = ff->fh; 2136 } 2137 fuse_setattr_fill(fm->fc, &args, inode, &inarg, &outarg); 2138 2139 return fuse_simple_request(fm, &args); 2140 } 2141 2142 /* 2143 * Set attributes, and at the same time refresh them. 2144 * 2145 * Truncation is slightly complicated, because the 'truncate' request 2146 * may fail, in which case we don't want to touch the mapping. 2147 * vmtruncate() doesn't allow for this case, so do the rlimit checking 2148 * and the actual truncation by hand. 2149 */ 2150 int fuse_do_setattr(const struct mnt_idmap *idmap, struct dentry *dentry, 2151 struct iattr *attr, struct file *file) 2152 { 2153 struct inode *inode = d_inode(dentry); 2154 struct fuse_mount *fm = get_fuse_mount(inode); 2155 struct fuse_conn *fc = fm->fc; 2156 struct fuse_inode *fi = get_fuse_inode(inode); 2157 struct address_space *mapping = inode->i_mapping; 2158 FUSE_ARGS(args); 2159 struct fuse_setattr_in inarg; 2160 struct fuse_attr_out outarg; 2161 bool is_truncate = false; 2162 bool is_wb = fc->writeback_cache && S_ISREG(inode->i_mode); 2163 loff_t oldsize; 2164 int err; 2165 bool trust_local_cmtime = is_wb; 2166 bool fault_blocked = false; 2167 u64 attr_version; 2168 2169 if (!fc->default_permissions) 2170 attr->ia_valid |= ATTR_FORCE; 2171 2172 err = setattr_prepare(idmap, dentry, attr); 2173 if (err) 2174 return err; 2175 2176 if (attr->ia_valid & ATTR_SIZE) { 2177 if (WARN_ON(!S_ISREG(inode->i_mode))) 2178 return -EIO; 2179 is_truncate = true; 2180 } 2181 2182 if (FUSE_IS_VDAX(inode) && is_truncate) { 2183 filemap_invalidate_lock(mapping); 2184 fault_blocked = true; 2185 err = fuse_vdax_break_layouts(inode, 0, -1); 2186 if (err) 2187 goto unlock; 2188 } else if (IS_DAX(inode) && is_truncate) { 2189 /* No truncate for DAX yet */ 2190 return -EINVAL; 2191 } 2192 2193 if (attr->ia_valid & ATTR_OPEN) { 2194 /* This is coming from open(..., ... | O_TRUNC); */ 2195 WARN_ON(!(attr->ia_valid & ATTR_SIZE)); 2196 WARN_ON(attr->ia_size != 0); 2197 if (fc->atomic_o_trunc) { 2198 /* 2199 * No need to send request to userspace, since actual 2200 * truncation has already been done by OPEN. But still 2201 * need to truncate page cache. 2202 */ 2203 i_size_write(inode, 0); 2204 truncate_pagecache(inode, 0); 2205 goto out; 2206 } 2207 file = NULL; 2208 } 2209 2210 /* Flush dirty data/metadata before non-truncate SETATTR */ 2211 if (is_wb && 2212 attr->ia_valid & 2213 (ATTR_MODE | ATTR_UID | ATTR_GID | ATTR_MTIME_SET | 2214 ATTR_TIMES_SET)) { 2215 err = write_inode_now(inode, true); 2216 if (err) 2217 goto unlock; 2218 2219 fuse_set_nowrite(inode); 2220 fuse_release_nowrite(inode); 2221 } 2222 2223 if (is_truncate) { 2224 fuse_set_nowrite(inode); 2225 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state); 2226 if (trust_local_cmtime && attr->ia_size != inode->i_size) 2227 attr->ia_valid |= ATTR_MTIME | ATTR_CTIME; 2228 } 2229 2230 memset(&inarg, 0, sizeof(inarg)); 2231 memset(&outarg, 0, sizeof(outarg)); 2232 iattr_to_fattr(idmap, fc, attr, &inarg, trust_local_cmtime); 2233 if (file) { 2234 struct fuse_file *ff = file->private_data; 2235 inarg.valid |= FATTR_FH; 2236 inarg.fh = ff->fh; 2237 } 2238 2239 /* Kill suid/sgid for non-directory chown unconditionally */ 2240 if (fc->handle_killpriv_v2 && !S_ISDIR(inode->i_mode) && 2241 attr->ia_valid & (ATTR_UID | ATTR_GID)) 2242 inarg.valid |= FATTR_KILL_SUIDGID; 2243 2244 if (attr->ia_valid & ATTR_SIZE) { 2245 /* For mandatory locking in truncate */ 2246 inarg.valid |= FATTR_LOCKOWNER; 2247 inarg.lock_owner = fuse_lock_owner_id(fc, current->files); 2248 2249 /* Kill suid/sgid for truncate only if no CAP_FSETID */ 2250 if (fc->handle_killpriv_v2 && !capable(CAP_FSETID)) 2251 inarg.valid |= FATTR_KILL_SUIDGID; 2252 } 2253 2254 attr_version = fuse_get_attr_version(fm->fc); 2255 fuse_setattr_fill(fc, &args, inode, &inarg, &outarg); 2256 err = fuse_simple_request(fm, &args); 2257 if (err) { 2258 if (err == -EINTR) 2259 fuse_invalidate_attr(inode); 2260 goto error; 2261 } 2262 2263 if (fuse_invalid_attr(&outarg.attr) || 2264 inode_wrong_type(inode, outarg.attr.mode)) { 2265 fuse_make_bad(inode); 2266 err = -EIO; 2267 goto error; 2268 } 2269 2270 spin_lock(&fi->lock); 2271 /* the kernel maintains i_mtime locally */ 2272 if (trust_local_cmtime) { 2273 if (attr->ia_valid & ATTR_MTIME) 2274 inode_set_mtime_to_ts(inode, attr->ia_mtime); 2275 if (attr->ia_valid & ATTR_CTIME) 2276 inode_set_ctime_to_ts(inode, attr->ia_ctime); 2277 /* FIXME: clear I_DIRTY_SYNC? */ 2278 } 2279 2280 if (fi->attr_version > attr_version) { 2281 /* 2282 * Apply attributes, for example for fsnotify_change(), but set 2283 * attribute timeout to zero. 2284 */ 2285 outarg.attr_valid = outarg.attr_valid_nsec = 0; 2286 } 2287 2288 fuse_change_attributes_common(inode, &outarg.attr, NULL, 2289 ATTR_TIMEOUT(&outarg), 2290 fuse_get_cache_mask(inode), 0); 2291 oldsize = inode->i_size; 2292 /* see the comment in fuse_change_attributes() */ 2293 if (!is_wb || is_truncate) 2294 i_size_write(inode, outarg.attr.size); 2295 2296 if (is_truncate) { 2297 /* NOTE: this may release/reacquire fi->lock */ 2298 __fuse_release_nowrite(inode); 2299 } 2300 spin_unlock(&fi->lock); 2301 2302 /* 2303 * Only call invalidate_inode_pages2() after removing 2304 * FUSE_NOWRITE, otherwise fuse_launder_folio() would deadlock. 2305 */ 2306 if ((is_truncate || !is_wb) && 2307 S_ISREG(inode->i_mode) && oldsize != outarg.attr.size) { 2308 if (outarg.attr.size > oldsize) 2309 truncate_pagecache_range(inode, oldsize, 2310 outarg.attr.size - 1); 2311 truncate_pagecache(inode, outarg.attr.size); 2312 invalidate_inode_pages2(mapping); 2313 } 2314 2315 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state); 2316 out: 2317 if (fault_blocked) 2318 filemap_invalidate_unlock(mapping); 2319 2320 return 0; 2321 2322 error: 2323 if (is_truncate) 2324 fuse_release_nowrite(inode); 2325 2326 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state); 2327 2328 unlock: 2329 if (fault_blocked) 2330 filemap_invalidate_unlock(mapping); 2331 return err; 2332 } 2333 2334 static int fuse_setattr(const struct mnt_idmap *idmap, struct dentry *entry, 2335 struct iattr *attr) 2336 { 2337 struct inode *inode = d_inode(entry); 2338 struct fuse_conn *fc = get_fuse_conn(inode); 2339 struct file *file = (attr->ia_valid & ATTR_FILE) ? attr->ia_file : NULL; 2340 int ret; 2341 2342 if (fuse_is_bad(inode)) 2343 return -EIO; 2344 2345 if (!fuse_allow_current_process(get_fuse_conn(inode))) 2346 return -EACCES; 2347 2348 if (attr->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID)) { 2349 attr->ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | 2350 ATTR_MODE); 2351 2352 /* 2353 * The only sane way to reliably kill suid/sgid is to do it in 2354 * the userspace filesystem 2355 * 2356 * This should be done on write(), truncate() and chown(). 2357 */ 2358 if (!fc->handle_killpriv && !fc->handle_killpriv_v2) { 2359 /* 2360 * ia_mode calculation may have used stale i_mode. 2361 * Refresh and recalculate. 2362 */ 2363 ret = fuse_do_getattr(idmap, inode, NULL, file); 2364 if (ret) 2365 return ret; 2366 2367 attr->ia_mode = inode->i_mode; 2368 if (inode->i_mode & S_ISUID) { 2369 attr->ia_valid |= ATTR_MODE; 2370 attr->ia_mode &= ~S_ISUID; 2371 } 2372 if ((inode->i_mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) { 2373 attr->ia_valid |= ATTR_MODE; 2374 attr->ia_mode &= ~S_ISGID; 2375 } 2376 } 2377 } 2378 if (!attr->ia_valid) 2379 return 0; 2380 2381 ret = fuse_do_setattr(idmap, entry, attr, file); 2382 if (!ret) { 2383 /* 2384 * If filesystem supports acls it may have updated acl xattrs in 2385 * the filesystem, so forget cached acls for the inode. 2386 */ 2387 if (fc->posix_acl) 2388 forget_all_cached_acls(inode); 2389 2390 /* Directory mode changed, may need to revalidate access */ 2391 if (d_is_dir(entry) && (attr->ia_valid & ATTR_MODE)) 2392 fuse_invalidate_entry_cache(entry); 2393 } 2394 return ret; 2395 } 2396 2397 static int fuse_getattr(const struct mnt_idmap *idmap, 2398 const struct path *path, struct kstat *stat, 2399 u32 request_mask, unsigned int flags) 2400 { 2401 struct inode *inode = d_inode(path->dentry); 2402 struct fuse_conn *fc = get_fuse_conn(inode); 2403 2404 if (fuse_is_bad(inode)) 2405 return -EIO; 2406 2407 if (!fuse_allow_current_process(fc)) { 2408 if (!request_mask) { 2409 /* 2410 * If user explicitly requested *nothing* then don't 2411 * error out, but return st_dev only. 2412 */ 2413 stat->result_mask = 0; 2414 stat->dev = inode->i_sb->s_dev; 2415 return 0; 2416 } 2417 return -EACCES; 2418 } 2419 2420 return fuse_update_get_attr(idmap, inode, NULL, stat, request_mask, flags); 2421 } 2422 2423 static const struct inode_operations fuse_dir_inode_operations = { 2424 .lookup = fuse_lookup, 2425 .mkdir = fuse_mkdir, 2426 .symlink = fuse_symlink, 2427 .unlink = fuse_unlink, 2428 .rmdir = fuse_rmdir, 2429 .rename = fuse_rename2, 2430 .link = fuse_link, 2431 .setattr = fuse_setattr, 2432 .create = fuse_create, 2433 .atomic_open = fuse_atomic_open, 2434 .tmpfile = fuse_tmpfile, 2435 .mknod = fuse_mknod, 2436 .permission = fuse_permission, 2437 .getattr = fuse_getattr, 2438 .listxattr = fuse_listxattr, 2439 .get_inode_acl = fuse_get_inode_acl, 2440 .get_acl = fuse_get_acl, 2441 .set_acl = fuse_set_acl, 2442 .fileattr_get = fuse_fileattr_get, 2443 .fileattr_set = fuse_fileattr_set, 2444 }; 2445 2446 static const struct file_operations fuse_dir_operations = { 2447 .llseek = generic_file_llseek, 2448 .read = generic_read_dir, 2449 .iterate_shared = fuse_readdir, 2450 .open = fuse_dir_open, 2451 .release = fuse_dir_release, 2452 .fsync = fuse_dir_fsync, 2453 .unlocked_ioctl = fuse_dir_ioctl, 2454 .compat_ioctl = fuse_dir_compat_ioctl, 2455 }; 2456 2457 static const struct inode_operations fuse_common_inode_operations = { 2458 .setattr = fuse_setattr, 2459 .permission = fuse_permission, 2460 .getattr = fuse_getattr, 2461 .listxattr = fuse_listxattr, 2462 .get_inode_acl = fuse_get_inode_acl, 2463 .get_acl = fuse_get_acl, 2464 .set_acl = fuse_set_acl, 2465 .fileattr_get = fuse_fileattr_get, 2466 .fileattr_set = fuse_fileattr_set, 2467 }; 2468 2469 static const struct inode_operations fuse_symlink_inode_operations = { 2470 .setattr = fuse_setattr, 2471 .get_link = fuse_get_link, 2472 .getattr = fuse_getattr, 2473 .listxattr = fuse_listxattr, 2474 }; 2475 2476 void fuse_init_common(struct inode *inode) 2477 { 2478 inode->i_op = &fuse_common_inode_operations; 2479 } 2480 2481 void fuse_init_dir(struct inode *inode) 2482 { 2483 struct fuse_inode *fi = get_fuse_inode(inode); 2484 2485 inode->i_op = &fuse_dir_inode_operations; 2486 inode->i_fop = &fuse_dir_operations; 2487 2488 spin_lock_init(&fi->rdc.lock); 2489 fi->rdc.cached = false; 2490 fi->rdc.size = 0; 2491 fi->rdc.pos = 0; 2492 fi->rdc.version = 0; 2493 } 2494 2495 static int fuse_symlink_read_folio(struct file *null, struct folio *folio) 2496 { 2497 int err = fuse_readlink_folio(folio->mapping->host, folio); 2498 2499 if (!err) 2500 folio_mark_uptodate(folio); 2501 2502 folio_unlock(folio); 2503 2504 return err; 2505 } 2506 2507 static const struct address_space_operations fuse_symlink_aops = { 2508 .read_folio = fuse_symlink_read_folio, 2509 }; 2510 2511 void fuse_init_symlink(struct inode *inode) 2512 { 2513 inode->i_op = &fuse_symlink_inode_operations; 2514 inode->i_data.a_ops = &fuse_symlink_aops; 2515 inode_nohighmem(inode); 2516 } 2517