1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2011 Novell Inc. 4 * Copyright (C) 2016 Red Hat, Inc. 5 */ 6 7 #include <linux/fs.h> 8 #include <linux/mount.h> 9 #include <linux/slab.h> 10 #include <linux/cred.h> 11 #include <linux/xattr.h> 12 #include <linux/exportfs.h> 13 #include <linux/file.h> 14 #include <linux/fileattr.h> 15 #include <linux/uuid.h> 16 #include <linux/namei.h> 17 #include <linux/ratelimit.h> 18 #include <linux/overflow.h> 19 #include "overlayfs.h" 20 21 /* Get write access to upper mnt - may fail if upper sb was remounted ro */ 22 int ovl_get_write_access(struct dentry *dentry) 23 { 24 struct ovl_fs *ofs = OVL_FS(dentry->d_sb); 25 return mnt_get_write_access(ovl_upper_mnt(ofs)); 26 } 27 28 /* Get write access to upper sb - may block if upper sb is frozen */ 29 void ovl_start_write(struct dentry *dentry) 30 { 31 struct ovl_fs *ofs = OVL_FS(dentry->d_sb); 32 sb_start_write(ovl_upper_mnt(ofs)->mnt_sb); 33 } 34 35 int ovl_want_write(struct dentry *dentry) 36 { 37 struct ovl_fs *ofs = OVL_FS(dentry->d_sb); 38 return mnt_want_write(ovl_upper_mnt(ofs)); 39 } 40 41 void ovl_put_write_access(struct dentry *dentry) 42 { 43 struct ovl_fs *ofs = OVL_FS(dentry->d_sb); 44 mnt_put_write_access(ovl_upper_mnt(ofs)); 45 } 46 47 void ovl_end_write(struct dentry *dentry) 48 { 49 struct ovl_fs *ofs = OVL_FS(dentry->d_sb); 50 sb_end_write(ovl_upper_mnt(ofs)->mnt_sb); 51 } 52 53 void ovl_drop_write(struct dentry *dentry) 54 { 55 struct ovl_fs *ofs = OVL_FS(dentry->d_sb); 56 mnt_drop_write(ovl_upper_mnt(ofs)); 57 } 58 59 struct dentry *ovl_workdir(struct dentry *dentry) 60 { 61 struct ovl_fs *ofs = OVL_FS(dentry->d_sb); 62 return ofs->workdir; 63 } 64 65 const struct cred *ovl_override_creds(struct super_block *sb) 66 { 67 struct ovl_fs *ofs = OVL_FS(sb); 68 69 return override_creds(ofs->creator_cred); 70 } 71 72 /* 73 * Check if underlying fs supports file handles and try to determine encoding 74 * type, in order to deduce maximum inode number used by fs. 75 * 76 * Return 0 if file handles are not supported. 77 * Return 1 (FILEID_INO32_GEN) if fs uses the default 32bit inode encoding. 78 * Return -1 if fs uses a non default encoding with unknown inode size. 79 */ 80 int ovl_can_decode_fh(struct super_block *sb) 81 { 82 if (!capable(CAP_DAC_READ_SEARCH)) 83 return 0; 84 85 if (!exportfs_can_decode_fh(sb->s_export_op)) 86 return 0; 87 88 return sb->s_export_op->encode_fh ? -1 : FILEID_INO32_GEN; 89 } 90 91 struct dentry *ovl_indexdir(struct super_block *sb) 92 { 93 struct ovl_fs *ofs = OVL_FS(sb); 94 95 return ofs->config.index ? ofs->workdir : NULL; 96 } 97 98 /* Index all files on copy up. For now only enabled for NFS export */ 99 bool ovl_index_all(struct super_block *sb) 100 { 101 struct ovl_fs *ofs = OVL_FS(sb); 102 103 return ofs->config.nfs_export && ofs->config.index; 104 } 105 106 /* Verify lower origin on lookup. For now only enabled for NFS export */ 107 bool ovl_verify_lower(struct super_block *sb) 108 { 109 struct ovl_fs *ofs = OVL_FS(sb); 110 111 return ofs->config.nfs_export && ofs->config.index; 112 } 113 114 struct ovl_path *ovl_stack_alloc(unsigned int n) 115 { 116 return kcalloc(n, sizeof(struct ovl_path), GFP_KERNEL); 117 } 118 119 void ovl_stack_cpy(struct ovl_path *dst, struct ovl_path *src, unsigned int n) 120 { 121 unsigned int i; 122 123 memcpy(dst, src, sizeof(struct ovl_path) * n); 124 for (i = 0; i < n; i++) 125 dget(src[i].dentry); 126 } 127 128 void ovl_stack_put(struct ovl_path *stack, unsigned int n) 129 { 130 unsigned int i; 131 132 for (i = 0; stack && i < n; i++) 133 dput(stack[i].dentry); 134 } 135 136 void ovl_stack_free(struct ovl_path *stack, unsigned int n) 137 { 138 ovl_stack_put(stack, n); 139 kfree(stack); 140 } 141 142 struct ovl_entry *ovl_alloc_entry(unsigned int numlower) 143 { 144 struct ovl_entry *oe; 145 146 oe = kzalloc(struct_size(oe, __lowerstack, numlower), GFP_KERNEL); 147 if (oe) 148 oe->__numlower = numlower; 149 150 return oe; 151 } 152 153 void ovl_free_entry(struct ovl_entry *oe) 154 { 155 ovl_stack_put(ovl_lowerstack(oe), ovl_numlower(oe)); 156 kfree(oe); 157 } 158 159 #define OVL_D_REVALIDATE (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE) 160 161 bool ovl_dentry_remote(struct dentry *dentry) 162 { 163 return dentry->d_flags & OVL_D_REVALIDATE; 164 } 165 166 void ovl_dentry_update_reval(struct dentry *dentry, struct dentry *realdentry) 167 { 168 if (!ovl_dentry_remote(realdentry)) 169 return; 170 171 spin_lock(&dentry->d_lock); 172 dentry->d_flags |= realdentry->d_flags & OVL_D_REVALIDATE; 173 spin_unlock(&dentry->d_lock); 174 } 175 176 void ovl_dentry_init_reval(struct dentry *dentry, struct dentry *upperdentry, 177 struct ovl_entry *oe) 178 { 179 return ovl_dentry_init_flags(dentry, upperdentry, oe, OVL_D_REVALIDATE); 180 } 181 182 void ovl_dentry_init_flags(struct dentry *dentry, struct dentry *upperdentry, 183 struct ovl_entry *oe, unsigned int mask) 184 { 185 struct ovl_path *lowerstack = ovl_lowerstack(oe); 186 unsigned int i, flags = 0; 187 188 if (upperdentry) 189 flags |= upperdentry->d_flags; 190 for (i = 0; i < ovl_numlower(oe) && lowerstack[i].dentry; i++) 191 flags |= lowerstack[i].dentry->d_flags; 192 193 spin_lock(&dentry->d_lock); 194 dentry->d_flags &= ~mask; 195 dentry->d_flags |= flags & mask; 196 spin_unlock(&dentry->d_lock); 197 } 198 199 bool ovl_dentry_weird(struct dentry *dentry) 200 { 201 if (!d_can_lookup(dentry) && !d_is_file(dentry) && !d_is_symlink(dentry)) 202 return true; 203 204 if (dentry->d_flags & (DCACHE_NEED_AUTOMOUNT | DCACHE_MANAGE_TRANSIT)) 205 return true; 206 207 /* 208 * Exceptionally for layers with casefold, we accept that they have 209 * their own hash and compare operations 210 */ 211 if (sb_has_encoding(dentry->d_sb)) 212 return false; 213 214 return dentry->d_flags & (DCACHE_OP_HASH | DCACHE_OP_COMPARE); 215 } 216 217 enum ovl_path_type ovl_path_type(struct dentry *dentry) 218 { 219 struct ovl_entry *oe = OVL_E(dentry); 220 enum ovl_path_type type = 0; 221 222 if (ovl_dentry_upper(dentry)) { 223 type = __OVL_PATH_UPPER; 224 225 /* 226 * Non-dir dentry can hold lower dentry of its copy up origin. 227 */ 228 if (ovl_numlower(oe)) { 229 if (ovl_test_flag(OVL_CONST_INO, d_inode(dentry))) 230 type |= __OVL_PATH_ORIGIN; 231 if (d_is_dir(dentry) || 232 !ovl_has_upperdata(d_inode(dentry))) 233 type |= __OVL_PATH_MERGE; 234 } 235 } else { 236 if (ovl_numlower(oe) > 1) 237 type |= __OVL_PATH_MERGE; 238 } 239 return type; 240 } 241 242 void ovl_path_upper(struct dentry *dentry, struct path *path) 243 { 244 struct ovl_fs *ofs = OVL_FS(dentry->d_sb); 245 246 path->mnt = ovl_upper_mnt(ofs); 247 path->dentry = ovl_dentry_upper(dentry); 248 } 249 250 void ovl_path_lower(struct dentry *dentry, struct path *path) 251 { 252 struct ovl_entry *oe = OVL_E(dentry); 253 struct ovl_path *lowerpath = ovl_lowerstack(oe); 254 255 if (ovl_numlower(oe)) { 256 path->mnt = lowerpath->layer->mnt; 257 path->dentry = lowerpath->dentry; 258 } else { 259 *path = (struct path) { }; 260 } 261 } 262 263 void ovl_path_lowerdata(struct dentry *dentry, struct path *path) 264 { 265 struct ovl_entry *oe = OVL_E(dentry); 266 struct ovl_path *lowerdata = ovl_lowerdata(oe); 267 struct dentry *lowerdata_dentry = ovl_lowerdata_dentry(oe); 268 269 if (lowerdata_dentry) { 270 path->dentry = lowerdata_dentry; 271 /* 272 * Pairs with smp_wmb() in ovl_dentry_set_lowerdata(). 273 * Make sure that if lowerdata->dentry is visible, then 274 * datapath->layer is visible as well. 275 */ 276 smp_rmb(); 277 path->mnt = READ_ONCE(lowerdata->layer)->mnt; 278 } else { 279 *path = (struct path) { }; 280 } 281 } 282 283 enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path) 284 { 285 enum ovl_path_type type = ovl_path_type(dentry); 286 287 if (!OVL_TYPE_UPPER(type)) 288 ovl_path_lower(dentry, path); 289 else 290 ovl_path_upper(dentry, path); 291 292 return type; 293 } 294 295 enum ovl_path_type ovl_path_realdata(struct dentry *dentry, struct path *path) 296 { 297 enum ovl_path_type type = ovl_path_type(dentry); 298 299 WARN_ON_ONCE(d_is_dir(dentry)); 300 301 if (!OVL_TYPE_UPPER(type) || OVL_TYPE_MERGE(type)) 302 ovl_path_lowerdata(dentry, path); 303 else 304 ovl_path_upper(dentry, path); 305 306 return type; 307 } 308 309 struct dentry *ovl_dentry_upper(struct dentry *dentry) 310 { 311 struct inode *inode = d_inode(dentry); 312 313 return inode ? ovl_upperdentry_dereference(OVL_I(inode)) : NULL; 314 } 315 316 struct dentry *ovl_dentry_lower(struct dentry *dentry) 317 { 318 struct ovl_entry *oe = OVL_E(dentry); 319 320 return ovl_numlower(oe) ? ovl_lowerstack(oe)->dentry : NULL; 321 } 322 323 const struct ovl_layer *ovl_layer_lower(struct dentry *dentry) 324 { 325 struct ovl_entry *oe = OVL_E(dentry); 326 327 return ovl_numlower(oe) ? ovl_lowerstack(oe)->layer : NULL; 328 } 329 330 /* 331 * ovl_dentry_lower() could return either a data dentry or metacopy dentry 332 * depending on what is stored in lowerstack[0]. At times we need to find 333 * lower dentry which has data (and not metacopy dentry). This helper 334 * returns the lower data dentry. 335 */ 336 struct dentry *ovl_dentry_lowerdata(struct dentry *dentry) 337 { 338 return ovl_lowerdata_dentry(OVL_E(dentry)); 339 } 340 341 int ovl_dentry_set_lowerdata(struct dentry *dentry, struct ovl_path *datapath) 342 { 343 struct ovl_entry *oe = OVL_E(dentry); 344 struct ovl_path *lowerdata = ovl_lowerdata(oe); 345 struct dentry *datadentry = datapath->dentry; 346 347 if (WARN_ON_ONCE(ovl_numlower(oe) <= 1)) 348 return -EIO; 349 350 WRITE_ONCE(lowerdata->layer, datapath->layer); 351 /* 352 * Pairs with smp_rmb() in ovl_path_lowerdata(). 353 * Make sure that if lowerdata->dentry is visible, then 354 * lowerdata->layer is visible as well. 355 */ 356 smp_wmb(); 357 WRITE_ONCE(lowerdata->dentry, dget(datadentry)); 358 359 ovl_dentry_update_reval(dentry, datadentry); 360 361 return 0; 362 } 363 364 struct dentry *ovl_dentry_real(struct dentry *dentry) 365 { 366 return ovl_dentry_upper(dentry) ?: ovl_dentry_lower(dentry); 367 } 368 369 struct dentry *ovl_i_dentry_upper(struct inode *inode) 370 { 371 return ovl_upperdentry_dereference(OVL_I(inode)); 372 } 373 374 struct inode *ovl_i_path_real(struct inode *inode, struct path *path) 375 { 376 struct ovl_path *lowerpath = ovl_lowerpath(OVL_I_E(inode)); 377 378 path->dentry = ovl_i_dentry_upper(inode); 379 if (!path->dentry) { 380 path->dentry = lowerpath->dentry; 381 path->mnt = lowerpath->layer->mnt; 382 } else { 383 path->mnt = ovl_upper_mnt(OVL_FS(inode->i_sb)); 384 } 385 386 return path->dentry ? d_inode_rcu(path->dentry) : NULL; 387 } 388 389 struct inode *ovl_inode_upper(struct inode *inode) 390 { 391 struct dentry *upperdentry = ovl_i_dentry_upper(inode); 392 393 return upperdentry ? d_inode(upperdentry) : NULL; 394 } 395 396 struct inode *ovl_inode_lower(struct inode *inode) 397 { 398 struct ovl_path *lowerpath = ovl_lowerpath(OVL_I_E(inode)); 399 400 return lowerpath ? d_inode(lowerpath->dentry) : NULL; 401 } 402 403 struct inode *ovl_inode_real(struct inode *inode) 404 { 405 return ovl_inode_upper(inode) ?: ovl_inode_lower(inode); 406 } 407 408 /* Return inode which contains lower data. Do not return metacopy */ 409 struct inode *ovl_inode_lowerdata(struct inode *inode) 410 { 411 struct dentry *lowerdata = ovl_lowerdata_dentry(OVL_I_E(inode)); 412 413 if (WARN_ON(!S_ISREG(inode->i_mode))) 414 return NULL; 415 416 return lowerdata ? d_inode(lowerdata) : NULL; 417 } 418 419 /* Return real inode which contains data. Does not return metacopy inode */ 420 struct inode *ovl_inode_realdata(struct inode *inode) 421 { 422 struct inode *upperinode; 423 424 upperinode = ovl_inode_upper(inode); 425 if (upperinode && ovl_has_upperdata(inode)) 426 return upperinode; 427 428 return ovl_inode_lowerdata(inode); 429 } 430 431 const char *ovl_lowerdata_redirect(struct inode *inode) 432 { 433 return inode && S_ISREG(inode->i_mode) ? 434 OVL_I(inode)->lowerdata_redirect : NULL; 435 } 436 437 struct ovl_dir_cache *ovl_dir_cache(struct inode *inode) 438 { 439 return inode && S_ISDIR(inode->i_mode) ? OVL_I(inode)->cache : NULL; 440 } 441 442 void ovl_set_dir_cache(struct inode *inode, struct ovl_dir_cache *cache) 443 { 444 OVL_I(inode)->cache = cache; 445 } 446 447 void ovl_dentry_set_flag(unsigned long flag, struct dentry *dentry) 448 { 449 set_bit(flag, OVL_E_FLAGS(dentry)); 450 } 451 452 void ovl_dentry_clear_flag(unsigned long flag, struct dentry *dentry) 453 { 454 clear_bit(flag, OVL_E_FLAGS(dentry)); 455 } 456 457 bool ovl_dentry_test_flag(unsigned long flag, struct dentry *dentry) 458 { 459 return test_bit(flag, OVL_E_FLAGS(dentry)); 460 } 461 462 bool ovl_dentry_is_opaque(struct dentry *dentry) 463 { 464 return ovl_dentry_test_flag(OVL_E_OPAQUE, dentry); 465 } 466 467 bool ovl_dentry_is_whiteout(struct dentry *dentry) 468 { 469 return !dentry->d_inode && ovl_dentry_is_opaque(dentry); 470 } 471 472 void ovl_dentry_set_opaque(struct dentry *dentry) 473 { 474 ovl_dentry_set_flag(OVL_E_OPAQUE, dentry); 475 } 476 477 bool ovl_dentry_has_xwhiteouts(struct dentry *dentry) 478 { 479 return ovl_dentry_test_flag(OVL_E_XWHITEOUTS, dentry); 480 } 481 482 void ovl_dentry_set_xwhiteouts(struct dentry *dentry) 483 { 484 ovl_dentry_set_flag(OVL_E_XWHITEOUTS, dentry); 485 } 486 487 /* 488 * ovl_layer_set_xwhiteouts() is called before adding the overlay dir 489 * dentry to dcache, while readdir of that same directory happens after 490 * the overlay dir dentry is in dcache, so if some cpu observes that 491 * ovl_dentry_is_xwhiteouts(), it will also observe layer->has_xwhiteouts 492 * for the layers where xwhiteouts marker was found in that merge dir. 493 */ 494 void ovl_layer_set_xwhiteouts(struct ovl_fs *ofs, 495 const struct ovl_layer *layer) 496 { 497 if (layer->has_xwhiteouts) 498 return; 499 500 /* Write once to read-mostly layer properties */ 501 ofs->layers[layer->idx].has_xwhiteouts = true; 502 } 503 504 /* 505 * For hard links and decoded file handles, it's possible for ovl_dentry_upper() 506 * to return positive, while there's no actual upper alias for the inode. 507 * Copy up code needs to know about the existence of the upper alias, so it 508 * can't use ovl_dentry_upper(). 509 */ 510 bool ovl_dentry_has_upper_alias(struct dentry *dentry) 511 { 512 return ovl_dentry_test_flag(OVL_E_UPPER_ALIAS, dentry); 513 } 514 515 void ovl_dentry_set_upper_alias(struct dentry *dentry) 516 { 517 ovl_dentry_set_flag(OVL_E_UPPER_ALIAS, dentry); 518 } 519 520 static bool ovl_should_check_upperdata(struct inode *inode) 521 { 522 if (!S_ISREG(inode->i_mode)) 523 return false; 524 525 if (!ovl_inode_lower(inode)) 526 return false; 527 528 return true; 529 } 530 531 bool ovl_has_upperdata(struct inode *inode) 532 { 533 if (!ovl_should_check_upperdata(inode)) 534 return true; 535 536 if (!ovl_test_flag(OVL_UPPERDATA, inode)) 537 return false; 538 /* 539 * Pairs with smp_wmb() in ovl_set_upperdata(). Main user of 540 * ovl_has_upperdata() is ovl_copy_up_meta_inode_data(). Make sure 541 * if setting of OVL_UPPERDATA is visible, then effects of writes 542 * before that are visible too. 543 */ 544 smp_rmb(); 545 return true; 546 } 547 548 void ovl_set_upperdata(struct inode *inode) 549 { 550 /* 551 * Pairs with smp_rmb() in ovl_has_upperdata(). Make sure 552 * if OVL_UPPERDATA flag is visible, then effects of write operations 553 * before it are visible as well. 554 */ 555 smp_wmb(); 556 ovl_set_flag(OVL_UPPERDATA, inode); 557 } 558 559 /* Caller should hold ovl_inode->lock */ 560 bool ovl_dentry_needs_data_copy_up_locked(struct dentry *dentry, int flags) 561 { 562 if (!ovl_open_flags_need_copy_up(flags)) 563 return false; 564 565 return !ovl_test_flag(OVL_UPPERDATA, d_inode(dentry)); 566 } 567 568 bool ovl_dentry_needs_data_copy_up(struct dentry *dentry, int flags) 569 { 570 if (!ovl_open_flags_need_copy_up(flags)) 571 return false; 572 573 return !ovl_has_upperdata(d_inode(dentry)); 574 } 575 576 const char *ovl_dentry_get_redirect(struct dentry *dentry) 577 { 578 return OVL_I(d_inode(dentry))->redirect; 579 } 580 581 void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect) 582 { 583 struct ovl_inode *oi = OVL_I(d_inode(dentry)); 584 585 kfree(oi->redirect); 586 oi->redirect = redirect; 587 } 588 589 void ovl_inode_update(struct inode *inode, struct dentry *upperdentry) 590 { 591 struct inode *upperinode = d_inode(upperdentry); 592 593 WARN_ON(OVL_I(inode)->__upperdentry); 594 595 /* 596 * Make sure upperdentry is consistent before making it visible 597 */ 598 smp_wmb(); 599 OVL_I(inode)->__upperdentry = upperdentry; 600 if (inode_unhashed(inode)) { 601 inode->i_private = upperinode; 602 __insert_inode_hash(inode, (unsigned long) upperinode); 603 } 604 } 605 606 static void ovl_dir_version_inc(struct dentry *dentry, bool impurity) 607 { 608 struct inode *inode = d_inode(dentry); 609 610 WARN_ON(!inode_is_locked(inode)); 611 WARN_ON(!d_is_dir(dentry)); 612 /* 613 * Version is used by readdir code to keep cache consistent. 614 * For merge dirs (or dirs with origin) all changes need to be noted. 615 * For non-merge dirs, cache contains only impure entries (i.e. ones 616 * which have been copied up and have origins), so only need to note 617 * changes to impure entries. 618 */ 619 if (!ovl_dir_is_real(inode) || impurity) 620 OVL_I(inode)->version++; 621 } 622 623 void ovl_dir_modified(struct dentry *dentry, bool impurity) 624 { 625 /* Copy mtime/ctime */ 626 ovl_copyattr(d_inode(dentry)); 627 628 ovl_dir_version_inc(dentry, impurity); 629 } 630 631 u64 ovl_inode_version_get(struct inode *inode) 632 { 633 WARN_ON(!inode_is_locked(inode)); 634 return OVL_I(inode)->version; 635 } 636 637 bool ovl_is_whiteout(struct dentry *dentry) 638 { 639 struct inode *inode = dentry->d_inode; 640 641 return inode && IS_WHITEOUT(inode); 642 } 643 644 /* 645 * Use this over ovl_is_whiteout for upper and lower files, as it also 646 * handles overlay.whiteout xattr whiteout files. 647 */ 648 bool ovl_path_is_whiteout(struct ovl_fs *ofs, const struct path *path) 649 { 650 return ovl_is_whiteout(path->dentry) || 651 ovl_path_check_xwhiteout_xattr(ofs, path); 652 } 653 654 struct file *ovl_path_open(const struct path *path, int flags) 655 { 656 struct inode *inode = d_inode(path->dentry); 657 struct mnt_idmap *real_idmap = mnt_idmap(path->mnt); 658 int err, acc_mode; 659 660 if (flags & ~(O_ACCMODE | O_LARGEFILE)) 661 BUG(); 662 663 switch (flags & O_ACCMODE) { 664 case O_RDONLY: 665 acc_mode = MAY_READ; 666 break; 667 case O_WRONLY: 668 acc_mode = MAY_WRITE; 669 break; 670 default: 671 BUG(); 672 } 673 674 err = inode_permission(real_idmap, inode, acc_mode | MAY_OPEN); 675 if (err) 676 return ERR_PTR(err); 677 678 /* O_NOATIME is an optimization, don't fail if not permitted */ 679 if (inode_owner_or_capable(real_idmap, inode)) 680 flags |= O_NOATIME; 681 682 return dentry_open(path, flags, current_cred()); 683 } 684 685 /* Caller should hold ovl_inode->lock */ 686 static bool ovl_already_copied_up_locked(struct dentry *dentry, int flags) 687 { 688 bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED; 689 690 if (ovl_dentry_upper(dentry) && 691 (ovl_dentry_has_upper_alias(dentry) || disconnected) && 692 !ovl_dentry_needs_data_copy_up_locked(dentry, flags)) 693 return true; 694 695 return false; 696 } 697 698 bool ovl_already_copied_up(struct dentry *dentry, int flags) 699 { 700 bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED; 701 702 /* 703 * Check if copy-up has happened as well as for upper alias (in 704 * case of hard links) is there. 705 * 706 * Both checks are lockless: 707 * - false negatives: will recheck under oi->lock 708 * - false positives: 709 * + ovl_dentry_upper() uses memory barriers to ensure the 710 * upper dentry is up-to-date 711 * + ovl_dentry_has_upper_alias() relies on locking of 712 * upper parent i_rwsem to prevent reordering copy-up 713 * with rename. 714 */ 715 if (ovl_dentry_upper(dentry) && 716 (ovl_dentry_has_upper_alias(dentry) || disconnected) && 717 !ovl_dentry_needs_data_copy_up(dentry, flags)) 718 return true; 719 720 return false; 721 } 722 723 /* 724 * The copy up "transaction" keeps an elevated mnt write count on upper mnt, 725 * but leaves taking freeze protection on upper sb to lower level helpers. 726 */ 727 int ovl_copy_up_start(struct dentry *dentry, int flags) 728 { 729 struct inode *inode = d_inode(dentry); 730 int err; 731 732 err = ovl_inode_lock_interruptible(inode); 733 if (err) 734 return err; 735 736 if (ovl_already_copied_up_locked(dentry, flags)) 737 err = 1; /* Already copied up */ 738 else 739 err = ovl_get_write_access(dentry); 740 if (err) 741 goto out_unlock; 742 743 return 0; 744 745 out_unlock: 746 ovl_inode_unlock(inode); 747 return err; 748 } 749 750 void ovl_copy_up_end(struct dentry *dentry) 751 { 752 ovl_put_write_access(dentry); 753 ovl_inode_unlock(d_inode(dentry)); 754 } 755 756 bool ovl_path_check_origin_xattr(struct ovl_fs *ofs, const struct path *path) 757 { 758 int res; 759 760 res = ovl_path_getxattr(ofs, path, OVL_XATTR_ORIGIN, NULL, 0); 761 762 /* Zero size value means "copied up but origin unknown" */ 763 if (res >= 0) 764 return true; 765 766 return false; 767 } 768 769 bool ovl_path_check_xwhiteout_xattr(struct ovl_fs *ofs, const struct path *path) 770 { 771 struct dentry *dentry = path->dentry; 772 int res; 773 774 /* xattr.whiteout must be a zero size regular file */ 775 if (!d_is_reg(dentry) || i_size_read(d_inode(dentry)) != 0) 776 return false; 777 778 res = ovl_path_getxattr(ofs, path, OVL_XATTR_XWHITEOUT, NULL, 0); 779 return res >= 0; 780 } 781 782 /* 783 * Load persistent uuid from xattr into s_uuid if found, or store a new 784 * random generated value in s_uuid and in xattr. 785 */ 786 bool ovl_init_uuid_xattr(struct super_block *sb, struct ovl_fs *ofs, 787 const struct path *upperpath) 788 { 789 bool set = false; 790 uuid_t uuid; 791 int res; 792 793 /* Try to load existing persistent uuid */ 794 res = ovl_path_getxattr(ofs, upperpath, OVL_XATTR_UUID, uuid.b, 795 UUID_SIZE); 796 if (res == UUID_SIZE) 797 goto set_uuid; 798 799 if (res != -ENODATA) 800 goto fail; 801 802 /* 803 * With uuid=auto, if uuid xattr is found, it will be used. 804 * If uuid xattrs is not found, generate a persistent uuid only on mount 805 * of new overlays where upper root dir is not yet marked as impure. 806 * An upper dir is marked as impure on copy up or lookup of its subdirs. 807 */ 808 if (ofs->config.uuid == OVL_UUID_AUTO) { 809 res = ovl_path_getxattr(ofs, upperpath, OVL_XATTR_IMPURE, NULL, 810 0); 811 if (res > 0) { 812 /* Any mount of old overlay - downgrade to uuid=null */ 813 ofs->config.uuid = OVL_UUID_NULL; 814 return true; 815 } else if (res == -ENODATA) { 816 /* First mount of new overlay - upgrade to uuid=on */ 817 ofs->config.uuid = OVL_UUID_ON; 818 } else if (res < 0) { 819 goto fail; 820 } 821 822 } 823 824 /* Generate overlay instance uuid */ 825 uuid_gen(&uuid); 826 827 /* Try to store persistent uuid */ 828 set = true; 829 res = ovl_setxattr(ofs, upperpath->dentry, OVL_XATTR_UUID, uuid.b, 830 UUID_SIZE); 831 if (res) 832 goto fail; 833 834 set_uuid: 835 super_set_uuid(sb, uuid.b, sizeof(uuid)); 836 return true; 837 838 fail: 839 ofs->config.uuid = OVL_UUID_NULL; 840 pr_warn("failed to %s uuid (%pd2, err=%i); falling back to uuid=null.\n", 841 set ? "set" : "get", upperpath->dentry, res); 842 return false; 843 } 844 845 char ovl_get_dir_xattr_val(struct ovl_fs *ofs, const struct path *path, 846 enum ovl_xattr ox) 847 { 848 int res; 849 char val; 850 851 if (!d_is_dir(path->dentry)) 852 return 0; 853 854 res = ovl_path_getxattr(ofs, path, ox, &val, 1); 855 return res == 1 ? val : 0; 856 } 857 858 #define OVL_XATTR_OPAQUE_POSTFIX "opaque" 859 #define OVL_XATTR_REDIRECT_POSTFIX "redirect" 860 #define OVL_XATTR_ORIGIN_POSTFIX "origin" 861 #define OVL_XATTR_IMPURE_POSTFIX "impure" 862 #define OVL_XATTR_NLINK_POSTFIX "nlink" 863 #define OVL_XATTR_UPPER_POSTFIX "upper" 864 #define OVL_XATTR_UUID_POSTFIX "uuid" 865 #define OVL_XATTR_METACOPY_POSTFIX "metacopy" 866 #define OVL_XATTR_PROTATTR_POSTFIX "protattr" 867 #define OVL_XATTR_XWHITEOUT_POSTFIX "whiteout" 868 869 #define OVL_XATTR_TAB_ENTRY(x) \ 870 [x] = { [false] = OVL_XATTR_TRUSTED_PREFIX x ## _POSTFIX, \ 871 [true] = OVL_XATTR_USER_PREFIX x ## _POSTFIX } 872 873 const char *const ovl_xattr_table[][2] = { 874 OVL_XATTR_TAB_ENTRY(OVL_XATTR_OPAQUE), 875 OVL_XATTR_TAB_ENTRY(OVL_XATTR_REDIRECT), 876 OVL_XATTR_TAB_ENTRY(OVL_XATTR_ORIGIN), 877 OVL_XATTR_TAB_ENTRY(OVL_XATTR_IMPURE), 878 OVL_XATTR_TAB_ENTRY(OVL_XATTR_NLINK), 879 OVL_XATTR_TAB_ENTRY(OVL_XATTR_UPPER), 880 OVL_XATTR_TAB_ENTRY(OVL_XATTR_UUID), 881 OVL_XATTR_TAB_ENTRY(OVL_XATTR_METACOPY), 882 OVL_XATTR_TAB_ENTRY(OVL_XATTR_PROTATTR), 883 OVL_XATTR_TAB_ENTRY(OVL_XATTR_XWHITEOUT), 884 }; 885 886 int ovl_check_setxattr(struct ovl_fs *ofs, struct dentry *upperdentry, 887 enum ovl_xattr ox, const void *value, size_t size, 888 int xerr) 889 { 890 int err; 891 892 if (ofs->noxattr) 893 return xerr; 894 895 err = ovl_setxattr(ofs, upperdentry, ox, value, size); 896 897 if (err == -EOPNOTSUPP) { 898 pr_warn("cannot set %s xattr on upper\n", ovl_xattr(ofs, ox)); 899 ofs->noxattr = true; 900 return xerr; 901 } 902 903 return err; 904 } 905 906 int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry) 907 { 908 struct ovl_fs *ofs = OVL_FS(dentry->d_sb); 909 int err; 910 911 if (ovl_test_flag(OVL_IMPURE, d_inode(dentry))) 912 return 0; 913 914 /* 915 * Do not fail when upper doesn't support xattrs. 916 * Upper inodes won't have origin nor redirect xattr anyway. 917 */ 918 err = ovl_check_setxattr(ofs, upperdentry, OVL_XATTR_IMPURE, "y", 1, 0); 919 if (!err) 920 ovl_set_flag(OVL_IMPURE, d_inode(dentry)); 921 922 return err; 923 } 924 925 926 #define OVL_PROTATTR_MAX 32 /* Reserved for future flags */ 927 928 void ovl_check_protattr(struct inode *inode, struct dentry *upper) 929 { 930 struct ovl_fs *ofs = OVL_FS(inode->i_sb); 931 u32 iflags = inode->i_flags & OVL_PROT_I_FLAGS_MASK; 932 char buf[OVL_PROTATTR_MAX+1]; 933 int res, n; 934 935 res = ovl_getxattr_upper(ofs, upper, OVL_XATTR_PROTATTR, buf, 936 OVL_PROTATTR_MAX); 937 if (res < 0) 938 return; 939 940 /* 941 * Initialize inode flags from overlay.protattr xattr and upper inode 942 * flags. If upper inode has those fileattr flags set (i.e. from old 943 * kernel), we do not clear them on ovl_get_inode(), but we will clear 944 * them on next fileattr_set(). 945 */ 946 for (n = 0; n < res; n++) { 947 if (buf[n] == 'a') 948 iflags |= S_APPEND; 949 else if (buf[n] == 'i') 950 iflags |= S_IMMUTABLE; 951 else 952 break; 953 } 954 955 if (!res || n < res) { 956 pr_warn_ratelimited("incompatible overlay.protattr format (%pd2, len=%d)\n", 957 upper, res); 958 } else { 959 inode_set_flags(inode, iflags, OVL_PROT_I_FLAGS_MASK); 960 } 961 } 962 963 int ovl_set_protattr(struct inode *inode, struct dentry *upper, 964 struct file_kattr *fa) 965 { 966 struct ovl_fs *ofs = OVL_FS(inode->i_sb); 967 char buf[OVL_PROTATTR_MAX]; 968 int len = 0, err = 0; 969 u32 iflags = 0; 970 971 BUILD_BUG_ON(HWEIGHT32(OVL_PROT_FS_FLAGS_MASK) > OVL_PROTATTR_MAX); 972 973 if (fa->flags & FS_APPEND_FL) { 974 buf[len++] = 'a'; 975 iflags |= S_APPEND; 976 } 977 if (fa->flags & FS_IMMUTABLE_FL) { 978 buf[len++] = 'i'; 979 iflags |= S_IMMUTABLE; 980 } 981 982 /* 983 * Do not allow to set protection flags when upper doesn't support 984 * xattrs, because we do not set those fileattr flags on upper inode. 985 * Remove xattr if it exist and all protection flags are cleared. 986 */ 987 if (len) { 988 err = ovl_check_setxattr(ofs, upper, OVL_XATTR_PROTATTR, 989 buf, len, -EPERM); 990 } else if (inode->i_flags & OVL_PROT_I_FLAGS_MASK) { 991 err = ovl_removexattr(ofs, upper, OVL_XATTR_PROTATTR); 992 if (err == -EOPNOTSUPP || err == -ENODATA) 993 err = 0; 994 } 995 if (err) 996 return err; 997 998 inode_set_flags(inode, iflags, OVL_PROT_I_FLAGS_MASK); 999 1000 /* Mask out the fileattr flags that should not be set in upper inode */ 1001 fa->flags &= ~OVL_PROT_FS_FLAGS_MASK; 1002 fa->fsx_xflags &= ~OVL_PROT_FSX_FLAGS_MASK; 1003 1004 return 0; 1005 } 1006 1007 /* 1008 * Caller must hold a reference to inode to prevent it from being freed while 1009 * it is marked inuse. 1010 */ 1011 bool ovl_inuse_trylock(struct dentry *dentry) 1012 { 1013 struct inode *inode = d_inode(dentry); 1014 bool locked = false; 1015 1016 spin_lock(&inode->i_lock); 1017 if (!(inode_state_read(inode) & I_OVL_INUSE)) { 1018 inode_state_set(inode, I_OVL_INUSE); 1019 locked = true; 1020 } 1021 spin_unlock(&inode->i_lock); 1022 1023 return locked; 1024 } 1025 1026 void ovl_inuse_unlock(struct dentry *dentry) 1027 { 1028 if (dentry) { 1029 struct inode *inode = d_inode(dentry); 1030 1031 spin_lock(&inode->i_lock); 1032 WARN_ON(!(inode_state_read(inode) & I_OVL_INUSE)); 1033 inode_state_clear(inode, I_OVL_INUSE); 1034 spin_unlock(&inode->i_lock); 1035 } 1036 } 1037 1038 bool ovl_is_inuse(struct dentry *dentry) 1039 { 1040 struct inode *inode = d_inode(dentry); 1041 bool inuse; 1042 1043 spin_lock(&inode->i_lock); 1044 inuse = (inode_state_read(inode) & I_OVL_INUSE); 1045 spin_unlock(&inode->i_lock); 1046 1047 return inuse; 1048 } 1049 1050 /* 1051 * Does this overlay dentry need to be indexed on copy up? 1052 */ 1053 bool ovl_need_index(struct dentry *dentry) 1054 { 1055 struct dentry *lower = ovl_dentry_lower(dentry); 1056 1057 if (!lower || !ovl_indexdir(dentry->d_sb)) 1058 return false; 1059 1060 /* Index all files for NFS export and consistency verification */ 1061 if (ovl_index_all(dentry->d_sb)) 1062 return true; 1063 1064 /* Index only lower hardlinks on copy up */ 1065 if (!d_is_dir(lower) && d_inode(lower)->i_nlink > 1) 1066 return true; 1067 1068 return false; 1069 } 1070 1071 /* Caller must hold OVL_I(inode)->lock */ 1072 static void ovl_cleanup_index(struct dentry *dentry) 1073 { 1074 struct ovl_fs *ofs = OVL_FS(dentry->d_sb); 1075 struct dentry *indexdir = ovl_indexdir(dentry->d_sb); 1076 struct dentry *lowerdentry = ovl_dentry_lower(dentry); 1077 struct dentry *upperdentry = ovl_dentry_upper(dentry); 1078 struct dentry *index = NULL; 1079 struct inode *inode; 1080 struct qstr name = { }; 1081 bool got_write = false; 1082 int err; 1083 1084 err = ovl_get_index_name(ofs, lowerdentry, &name); 1085 if (err) 1086 goto fail; 1087 1088 err = ovl_want_write(dentry); 1089 if (err) 1090 goto fail; 1091 1092 got_write = true; 1093 inode = d_inode(upperdentry); 1094 if (!S_ISDIR(inode->i_mode) && inode->i_nlink != 1) { 1095 pr_warn_ratelimited("cleanup linked index (%pd2, ino=%lu, nlink=%u)\n", 1096 upperdentry, inode->i_ino, inode->i_nlink); 1097 /* 1098 * We either have a bug with persistent union nlink or a lower 1099 * hardlink was added while overlay is mounted. Adding a lower 1100 * hardlink and then unlinking all overlay hardlinks would drop 1101 * overlay nlink to zero before all upper inodes are unlinked. 1102 * As a safety measure, when that situation is detected, set 1103 * the overlay nlink to the index inode nlink minus one for the 1104 * index entry itself. 1105 */ 1106 set_nlink(d_inode(dentry), inode->i_nlink - 1); 1107 ovl_set_nlink_upper(dentry); 1108 goto out; 1109 } 1110 1111 index = ovl_lookup_upper_unlocked(ofs, name.name, indexdir, name.len); 1112 err = PTR_ERR(index); 1113 if (IS_ERR(index)) { 1114 index = NULL; 1115 } else if (ovl_index_all(dentry->d_sb)) { 1116 /* Whiteout orphan index to block future open by handle */ 1117 err = ovl_cleanup_and_whiteout(OVL_FS(dentry->d_sb), 1118 indexdir, index); 1119 } else { 1120 /* Cleanup orphan index entries */ 1121 err = ovl_cleanup(ofs, indexdir, index); 1122 } 1123 if (err) 1124 goto fail; 1125 1126 out: 1127 if (got_write) 1128 ovl_drop_write(dentry); 1129 kfree(name.name); 1130 dput(index); 1131 return; 1132 1133 fail: 1134 pr_err("cleanup index of '%pd2' failed (%i)\n", dentry, err); 1135 goto out; 1136 } 1137 1138 /* 1139 * Operations that change overlay inode and upper inode nlink need to be 1140 * synchronized with copy up for persistent nlink accounting. 1141 */ 1142 int ovl_nlink_start(struct dentry *dentry) 1143 { 1144 struct inode *inode = d_inode(dentry); 1145 int err; 1146 1147 if (WARN_ON(!inode)) 1148 return -ENOENT; 1149 1150 /* 1151 * With inodes index is enabled, we store the union overlay nlink 1152 * in an xattr on the index inode. When whiting out an indexed lower, 1153 * we need to decrement the overlay persistent nlink, but before the 1154 * first copy up, we have no upper index inode to store the xattr. 1155 * 1156 * As a workaround, before whiteout/rename over an indexed lower, 1157 * copy up to create the upper index. Creating the upper index will 1158 * initialize the overlay nlink, so it could be dropped if unlink 1159 * or rename succeeds. 1160 * 1161 * TODO: implement metadata only index copy up when called with 1162 * ovl_copy_up_flags(dentry, O_PATH). 1163 */ 1164 if (ovl_need_index(dentry) && !ovl_dentry_has_upper_alias(dentry)) { 1165 err = ovl_copy_up(dentry); 1166 if (err) 1167 return err; 1168 } 1169 1170 err = ovl_inode_lock_interruptible(inode); 1171 if (err) 1172 return err; 1173 1174 err = ovl_want_write(dentry); 1175 if (err) 1176 goto out_unlock; 1177 1178 if (d_is_dir(dentry) || !ovl_test_flag(OVL_INDEX, inode)) 1179 return 0; 1180 1181 /* 1182 * The overlay inode nlink should be incremented/decremented IFF the 1183 * upper operation succeeds, along with nlink change of upper inode. 1184 * Therefore, before link/unlink/rename, we store the union nlink 1185 * value relative to the upper inode nlink in an upper inode xattr. 1186 */ 1187 with_ovl_creds(dentry->d_sb) 1188 err = ovl_set_nlink_upper(dentry); 1189 if (err) 1190 goto out_drop_write; 1191 1192 return 0; 1193 1194 out_drop_write: 1195 ovl_drop_write(dentry); 1196 out_unlock: 1197 ovl_inode_unlock(inode); 1198 1199 return err; 1200 } 1201 1202 void ovl_nlink_end(struct dentry *dentry) 1203 { 1204 struct inode *inode = d_inode(dentry); 1205 1206 ovl_drop_write(dentry); 1207 1208 if (ovl_test_flag(OVL_INDEX, inode) && inode->i_nlink == 0) { 1209 with_ovl_creds(dentry->d_sb) 1210 ovl_cleanup_index(dentry); 1211 } 1212 1213 ovl_inode_unlock(inode); 1214 } 1215 1216 int ovl_lock_rename_workdir(struct dentry *workdir, struct dentry *work, 1217 struct dentry *upperdir, struct dentry *upper) 1218 { 1219 struct dentry *trap; 1220 1221 /* Workdir should not be subdir of upperdir and vice versa */ 1222 trap = lock_rename(workdir, upperdir); 1223 if (IS_ERR(trap)) 1224 goto err; 1225 if (trap) 1226 goto err_unlock; 1227 if (work && (work->d_parent != workdir || d_unhashed(work))) 1228 goto err_unlock; 1229 if (upper && (upper->d_parent != upperdir || d_unhashed(upper))) 1230 goto err_unlock; 1231 1232 return 0; 1233 1234 err_unlock: 1235 unlock_rename(workdir, upperdir); 1236 err: 1237 pr_err("failed to lock workdir+upperdir\n"); 1238 return -EIO; 1239 } 1240 1241 /* 1242 * err < 0, 0 if no metacopy xattr, metacopy data size if xattr found. 1243 * an empty xattr returns OVL_METACOPY_MIN_SIZE to distinguish from no xattr value. 1244 */ 1245 int ovl_check_metacopy_xattr(struct ovl_fs *ofs, const struct path *path, 1246 struct ovl_metacopy *data) 1247 { 1248 int res; 1249 1250 /* Only regular files can have metacopy xattr */ 1251 if (!S_ISREG(d_inode(path->dentry)->i_mode)) 1252 return 0; 1253 1254 res = ovl_path_getxattr(ofs, path, OVL_XATTR_METACOPY, 1255 data, data ? OVL_METACOPY_MAX_SIZE : 0); 1256 if (res < 0) { 1257 if (res == -ENODATA || res == -EOPNOTSUPP) 1258 return 0; 1259 /* 1260 * getxattr on user.* may fail with EACCES in case there's no 1261 * read permission on the inode. Not much we can do, other than 1262 * tell the caller that this is not a metacopy inode. 1263 */ 1264 if (ofs->config.userxattr && res == -EACCES) 1265 return 0; 1266 goto out; 1267 } 1268 1269 if (res == 0) { 1270 /* Emulate empty data for zero size metacopy xattr */ 1271 res = OVL_METACOPY_MIN_SIZE; 1272 if (data) { 1273 memset(data, 0, res); 1274 data->len = res; 1275 } 1276 } else if (res < OVL_METACOPY_MIN_SIZE) { 1277 pr_warn_ratelimited("metacopy file '%pd' has too small xattr\n", 1278 path->dentry); 1279 return -EIO; 1280 } else if (data) { 1281 if (data->version != 0) { 1282 pr_warn_ratelimited("metacopy file '%pd' has unsupported version\n", 1283 path->dentry); 1284 return -EIO; 1285 } 1286 if (res != data->len) { 1287 pr_warn_ratelimited("metacopy file '%pd' has invalid xattr size\n", 1288 path->dentry); 1289 return -EIO; 1290 } 1291 } 1292 1293 return res; 1294 out: 1295 pr_warn_ratelimited("failed to get metacopy (%i)\n", res); 1296 return res; 1297 } 1298 1299 int ovl_set_metacopy_xattr(struct ovl_fs *ofs, struct dentry *d, struct ovl_metacopy *metacopy) 1300 { 1301 size_t len = metacopy->len; 1302 1303 /* If no flags or digest fall back to empty metacopy file */ 1304 if (metacopy->version == 0 && metacopy->flags == 0 && metacopy->digest_algo == 0) 1305 len = 0; 1306 1307 return ovl_check_setxattr(ofs, d, OVL_XATTR_METACOPY, 1308 metacopy, len, -EOPNOTSUPP); 1309 } 1310 1311 bool ovl_is_metacopy_dentry(struct dentry *dentry) 1312 { 1313 struct ovl_entry *oe = OVL_E(dentry); 1314 1315 if (!d_is_reg(dentry)) 1316 return false; 1317 1318 if (ovl_dentry_upper(dentry)) { 1319 if (!ovl_has_upperdata(d_inode(dentry))) 1320 return true; 1321 return false; 1322 } 1323 1324 return (ovl_numlower(oe) > 1); 1325 } 1326 1327 char *ovl_get_redirect_xattr(struct ovl_fs *ofs, const struct path *path, int padding) 1328 { 1329 int res; 1330 char *s, *next, *buf = NULL; 1331 1332 res = ovl_path_getxattr(ofs, path, OVL_XATTR_REDIRECT, NULL, 0); 1333 if (res == -ENODATA || res == -EOPNOTSUPP) 1334 return NULL; 1335 if (res < 0) 1336 goto fail; 1337 if (res == 0) 1338 goto invalid; 1339 1340 buf = kzalloc(res + padding + 1, GFP_KERNEL); 1341 if (!buf) 1342 return ERR_PTR(-ENOMEM); 1343 1344 res = ovl_path_getxattr(ofs, path, OVL_XATTR_REDIRECT, buf, res); 1345 if (res < 0) 1346 goto fail; 1347 if (res == 0) 1348 goto invalid; 1349 1350 if (buf[0] == '/') { 1351 for (s = buf; *s++ == '/'; s = next) { 1352 next = strchrnul(s, '/'); 1353 if (s == next) 1354 goto invalid; 1355 } 1356 } else { 1357 if (strchr(buf, '/') != NULL) 1358 goto invalid; 1359 } 1360 1361 return buf; 1362 invalid: 1363 pr_warn_ratelimited("invalid redirect (%s)\n", buf); 1364 res = -EINVAL; 1365 goto err_free; 1366 fail: 1367 pr_warn_ratelimited("failed to get redirect (%i)\n", res); 1368 err_free: 1369 kfree(buf); 1370 return ERR_PTR(res); 1371 } 1372 1373 /* Call with mounter creds as it may open the file */ 1374 int ovl_ensure_verity_loaded(const struct path *datapath) 1375 { 1376 struct inode *inode = d_inode(datapath->dentry); 1377 struct file *filp; 1378 1379 if (!fsverity_active(inode) && IS_VERITY(inode)) { 1380 /* 1381 * If this inode was not yet opened, the verity info hasn't been 1382 * loaded yet, so we need to do that here to force it into memory. 1383 */ 1384 filp = kernel_file_open(datapath, O_RDONLY, current_cred()); 1385 if (IS_ERR(filp)) 1386 return PTR_ERR(filp); 1387 fput(filp); 1388 } 1389 1390 return 0; 1391 } 1392 1393 int ovl_validate_verity(struct ovl_fs *ofs, 1394 const struct path *metapath, 1395 const struct path *datapath) 1396 { 1397 struct ovl_metacopy metacopy_data; 1398 u8 actual_digest[FS_VERITY_MAX_DIGEST_SIZE]; 1399 int xattr_digest_size, digest_size; 1400 int xattr_size, err; 1401 u8 verity_algo; 1402 1403 if (!ofs->config.verity_mode || 1404 /* Verity only works on regular files */ 1405 !S_ISREG(d_inode(metapath->dentry)->i_mode)) 1406 return 0; 1407 1408 xattr_size = ovl_check_metacopy_xattr(ofs, metapath, &metacopy_data); 1409 if (xattr_size < 0) 1410 return xattr_size; 1411 1412 if (!xattr_size || !metacopy_data.digest_algo) { 1413 if (ofs->config.verity_mode == OVL_VERITY_REQUIRE) { 1414 pr_warn_ratelimited("metacopy file '%pd' has no digest specified\n", 1415 metapath->dentry); 1416 return -EIO; 1417 } 1418 return 0; 1419 } 1420 1421 xattr_digest_size = ovl_metadata_digest_size(&metacopy_data); 1422 1423 err = ovl_ensure_verity_loaded(datapath); 1424 if (err < 0) { 1425 pr_warn_ratelimited("lower file '%pd' failed to load fs-verity info\n", 1426 datapath->dentry); 1427 return -EIO; 1428 } 1429 1430 digest_size = fsverity_get_digest(d_inode(datapath->dentry), actual_digest, 1431 &verity_algo, NULL); 1432 if (digest_size == 0) { 1433 pr_warn_ratelimited("lower file '%pd' has no fs-verity digest\n", datapath->dentry); 1434 return -EIO; 1435 } 1436 1437 if (xattr_digest_size != digest_size || 1438 metacopy_data.digest_algo != verity_algo || 1439 memcmp(metacopy_data.digest, actual_digest, xattr_digest_size) != 0) { 1440 pr_warn_ratelimited("lower file '%pd' has the wrong fs-verity digest\n", 1441 datapath->dentry); 1442 return -EIO; 1443 } 1444 1445 return 0; 1446 } 1447 1448 int ovl_get_verity_digest(struct ovl_fs *ofs, const struct path *src, 1449 struct ovl_metacopy *metacopy) 1450 { 1451 int err, digest_size; 1452 1453 if (!ofs->config.verity_mode || !S_ISREG(d_inode(src->dentry)->i_mode)) 1454 return 0; 1455 1456 err = ovl_ensure_verity_loaded(src); 1457 if (err < 0) { 1458 pr_warn_ratelimited("lower file '%pd' failed to load fs-verity info\n", 1459 src->dentry); 1460 return -EIO; 1461 } 1462 1463 digest_size = fsverity_get_digest(d_inode(src->dentry), 1464 metacopy->digest, &metacopy->digest_algo, NULL); 1465 if (digest_size == 0 || 1466 WARN_ON_ONCE(digest_size > FS_VERITY_MAX_DIGEST_SIZE)) { 1467 if (ofs->config.verity_mode == OVL_VERITY_REQUIRE) { 1468 pr_warn_ratelimited("lower file '%pd' has no fs-verity digest\n", 1469 src->dentry); 1470 return -EIO; 1471 } 1472 return 0; 1473 } 1474 1475 metacopy->len += digest_size; 1476 return 0; 1477 } 1478 1479 /* 1480 * ovl_sync_status() - Check fs sync status for volatile mounts 1481 * 1482 * Returns 1 if this is not a volatile mount and a real sync is required. 1483 * 1484 * Returns 0 if syncing can be skipped because mount is volatile, and no errors 1485 * have occurred on the upperdir since the mount. 1486 * 1487 * Returns -errno if it is a volatile mount, and the error that occurred since 1488 * the last mount. If the error code changes, it'll return the latest error 1489 * code. 1490 */ 1491 1492 int ovl_sync_status(struct ovl_fs *ofs) 1493 { 1494 struct vfsmount *mnt; 1495 1496 if (ovl_should_sync(ofs)) 1497 return 1; 1498 1499 mnt = ovl_upper_mnt(ofs); 1500 if (!mnt) 1501 return 0; 1502 1503 return errseq_check(&mnt->mnt_sb->s_wb_err, ofs->errseq); 1504 } 1505 1506 /* 1507 * ovl_copyattr() - copy inode attributes from layer to ovl inode 1508 * 1509 * When overlay copies inode information from an upper or lower layer to the 1510 * relevant overlay inode it will apply the idmapping of the upper or lower 1511 * layer when doing so ensuring that the ovl inode ownership will correctly 1512 * reflect the ownership of the idmapped upper or lower layer. For example, an 1513 * idmapped upper or lower layer mapping id 1001 to id 1000 will take care to 1514 * map any lower or upper inode owned by id 1001 to id 1000. These mapping 1515 * helpers are nops when the relevant layer isn't idmapped. 1516 */ 1517 void ovl_copyattr(struct inode *inode) 1518 { 1519 struct path realpath; 1520 struct inode *realinode; 1521 struct mnt_idmap *real_idmap; 1522 vfsuid_t vfsuid; 1523 vfsgid_t vfsgid; 1524 1525 realinode = ovl_i_path_real(inode, &realpath); 1526 real_idmap = mnt_idmap(realpath.mnt); 1527 1528 spin_lock(&inode->i_lock); 1529 vfsuid = i_uid_into_vfsuid(real_idmap, realinode); 1530 vfsgid = i_gid_into_vfsgid(real_idmap, realinode); 1531 1532 inode->i_uid = vfsuid_into_kuid(vfsuid); 1533 inode->i_gid = vfsgid_into_kgid(vfsgid); 1534 inode->i_mode = realinode->i_mode; 1535 inode_set_atime_to_ts(inode, inode_get_atime(realinode)); 1536 inode_set_mtime_to_ts(inode, inode_get_mtime(realinode)); 1537 inode_set_ctime_to_ts(inode, inode_get_ctime(realinode)); 1538 i_size_write(inode, i_size_read(realinode)); 1539 spin_unlock(&inode->i_lock); 1540 } 1541