1 // SPDX-License-Identifier: GPL-2.0-only 2 3 #include <linux/fs.h> 4 #include <linux/module.h> 5 #include <linux/namei.h> 6 #include <linux/fs_context.h> 7 #include <linux/fs_parser.h> 8 #include <linux/posix_acl_xattr.h> 9 #include <linux/seq_file.h> 10 #include <linux/xattr.h> 11 #include "overlayfs.h" 12 #include "params.h" 13 14 static bool ovl_redirect_dir_def = IS_ENABLED(CONFIG_OVERLAY_FS_REDIRECT_DIR); 15 module_param_named(redirect_dir, ovl_redirect_dir_def, bool, 0644); 16 MODULE_PARM_DESC(redirect_dir, 17 "Default to on or off for the redirect_dir feature"); 18 19 static bool ovl_redirect_always_follow = 20 IS_ENABLED(CONFIG_OVERLAY_FS_REDIRECT_ALWAYS_FOLLOW); 21 module_param_named(redirect_always_follow, ovl_redirect_always_follow, 22 bool, 0644); 23 MODULE_PARM_DESC(redirect_always_follow, 24 "Follow redirects even if redirect_dir feature is turned off"); 25 26 static bool ovl_xino_auto_def = IS_ENABLED(CONFIG_OVERLAY_FS_XINO_AUTO); 27 module_param_named(xino_auto, ovl_xino_auto_def, bool, 0644); 28 MODULE_PARM_DESC(xino_auto, 29 "Auto enable xino feature"); 30 31 static bool ovl_index_def = IS_ENABLED(CONFIG_OVERLAY_FS_INDEX); 32 module_param_named(index, ovl_index_def, bool, 0644); 33 MODULE_PARM_DESC(index, 34 "Default to on or off for the inodes index feature"); 35 36 static bool ovl_nfs_export_def = IS_ENABLED(CONFIG_OVERLAY_FS_NFS_EXPORT); 37 module_param_named(nfs_export, ovl_nfs_export_def, bool, 0644); 38 MODULE_PARM_DESC(nfs_export, 39 "Default to on or off for the NFS export feature"); 40 41 static bool ovl_metacopy_def = IS_ENABLED(CONFIG_OVERLAY_FS_METACOPY); 42 module_param_named(metacopy, ovl_metacopy_def, bool, 0644); 43 MODULE_PARM_DESC(metacopy, 44 "Default to on or off for the metadata only copy up feature"); 45 46 enum ovl_opt { 47 Opt_lowerdir, 48 Opt_lowerdir_add, 49 Opt_datadir_add, 50 Opt_upperdir, 51 Opt_workdir, 52 Opt_default_permissions, 53 Opt_redirect_dir, 54 Opt_index, 55 Opt_uuid, 56 Opt_nfs_export, 57 Opt_userxattr, 58 Opt_xino, 59 Opt_metacopy, 60 Opt_verity, 61 Opt_fsync, 62 Opt_volatile, 63 Opt_override_creds, 64 }; 65 66 static const struct constant_table ovl_parameter_bool[] = { 67 { "on", true }, 68 { "off", false }, 69 {} 70 }; 71 72 static const struct constant_table ovl_parameter_uuid[] = { 73 { "off", OVL_UUID_OFF }, 74 { "null", OVL_UUID_NULL }, 75 { "auto", OVL_UUID_AUTO }, 76 { "on", OVL_UUID_ON }, 77 {} 78 }; 79 80 static const char *ovl_uuid_mode(struct ovl_config *config) 81 { 82 return ovl_parameter_uuid[config->uuid].name; 83 } 84 85 static int ovl_uuid_def(void) 86 { 87 return OVL_UUID_AUTO; 88 } 89 90 static const struct constant_table ovl_parameter_xino[] = { 91 { "off", OVL_XINO_OFF }, 92 { "auto", OVL_XINO_AUTO }, 93 { "on", OVL_XINO_ON }, 94 {} 95 }; 96 97 const char *ovl_xino_mode(struct ovl_config *config) 98 { 99 return ovl_parameter_xino[config->xino].name; 100 } 101 102 static int ovl_xino_def(void) 103 { 104 return ovl_xino_auto_def ? OVL_XINO_AUTO : OVL_XINO_OFF; 105 } 106 107 const struct constant_table ovl_parameter_redirect_dir[] = { 108 { "off", OVL_REDIRECT_OFF }, 109 { "follow", OVL_REDIRECT_FOLLOW }, 110 { "nofollow", OVL_REDIRECT_NOFOLLOW }, 111 { "on", OVL_REDIRECT_ON }, 112 {} 113 }; 114 115 static const char *ovl_redirect_mode(struct ovl_config *config) 116 { 117 return ovl_parameter_redirect_dir[config->redirect_mode].name; 118 } 119 120 static int ovl_redirect_mode_def(void) 121 { 122 return ovl_redirect_dir_def ? OVL_REDIRECT_ON : 123 ovl_redirect_always_follow ? OVL_REDIRECT_FOLLOW : 124 OVL_REDIRECT_NOFOLLOW; 125 } 126 127 static const struct constant_table ovl_parameter_verity[] = { 128 { "off", OVL_VERITY_OFF }, 129 { "on", OVL_VERITY_ON }, 130 { "require", OVL_VERITY_REQUIRE }, 131 {} 132 }; 133 134 static const char *ovl_verity_mode(struct ovl_config *config) 135 { 136 return ovl_parameter_verity[config->verity_mode].name; 137 } 138 139 static int ovl_verity_mode_def(void) 140 { 141 return OVL_VERITY_OFF; 142 } 143 144 static const struct constant_table ovl_parameter_fsync[] = { 145 { "volatile", OVL_FSYNC_VOLATILE }, 146 { "auto", OVL_FSYNC_AUTO }, 147 { "strict", OVL_FSYNC_STRICT }, 148 {} 149 }; 150 151 static const char *ovl_fsync_mode(struct ovl_config *config) 152 { 153 return ovl_parameter_fsync[config->fsync_mode].name; 154 } 155 156 static int ovl_fsync_mode_def(void) 157 { 158 return OVL_FSYNC_AUTO; 159 } 160 161 const struct fs_parameter_spec ovl_parameter_spec[] = { 162 fsparam_string_empty("lowerdir", Opt_lowerdir), 163 fsparam_file_or_string("lowerdir+", Opt_lowerdir_add), 164 fsparam_file_or_string("datadir+", Opt_datadir_add), 165 fsparam_file_or_string("upperdir", Opt_upperdir), 166 fsparam_file_or_string("workdir", Opt_workdir), 167 fsparam_flag("default_permissions", Opt_default_permissions), 168 fsparam_enum("redirect_dir", Opt_redirect_dir, ovl_parameter_redirect_dir), 169 fsparam_enum("index", Opt_index, ovl_parameter_bool), 170 fsparam_enum("uuid", Opt_uuid, ovl_parameter_uuid), 171 fsparam_enum("nfs_export", Opt_nfs_export, ovl_parameter_bool), 172 fsparam_flag("userxattr", Opt_userxattr), 173 fsparam_enum("xino", Opt_xino, ovl_parameter_xino), 174 fsparam_enum("metacopy", Opt_metacopy, ovl_parameter_bool), 175 fsparam_enum("verity", Opt_verity, ovl_parameter_verity), 176 fsparam_enum("fsync", Opt_fsync, ovl_parameter_fsync), 177 fsparam_flag("volatile", Opt_volatile), 178 fsparam_flag_no("override_creds", Opt_override_creds), 179 {} 180 }; 181 182 static char *ovl_next_opt(char **s) 183 { 184 char *sbegin = *s; 185 char *p; 186 187 if (sbegin == NULL) 188 return NULL; 189 190 for (p = sbegin; *p; p++) { 191 if (*p == '\\') { 192 p++; 193 if (!*p) 194 break; 195 } else if (*p == ',') { 196 *p = '\0'; 197 *s = p + 1; 198 return sbegin; 199 } 200 } 201 *s = NULL; 202 return sbegin; 203 } 204 205 static int ovl_parse_monolithic(struct fs_context *fc, void *data) 206 { 207 return vfs_parse_monolithic_sep(fc, data, ovl_next_opt); 208 } 209 210 static ssize_t ovl_parse_param_split_lowerdirs(char *str) 211 { 212 ssize_t nr_layers = 1, nr_colons = 0; 213 char *s, *d; 214 215 for (s = d = str;; s++, d++) { 216 if (*s == '\\') { 217 /* keep esc chars in split lowerdir */ 218 *d++ = *s++; 219 /* A trailing backslash escapes nothing. */ 220 if (!*s) 221 return -EINVAL; 222 } else if (*s == ':') { 223 bool next_colon = (*(s + 1) == ':'); 224 225 nr_colons++; 226 if (nr_colons == 2 && next_colon) { 227 pr_err("only single ':' or double '::' sequences of unescaped colons in lowerdir mount option allowed.\n"); 228 return -EINVAL; 229 } 230 /* count layers, not colons */ 231 if (!next_colon) 232 nr_layers++; 233 234 *d = '\0'; 235 continue; 236 } 237 238 *d = *s; 239 if (!*s) { 240 /* trailing colons */ 241 if (nr_colons) { 242 pr_err("unescaped trailing colons in lowerdir mount option.\n"); 243 return -EINVAL; 244 } 245 break; 246 } 247 nr_colons = 0; 248 } 249 250 return nr_layers; 251 } 252 253 static int ovl_mount_dir_noesc(const char *name, struct path *path) 254 { 255 int err = -EINVAL; 256 257 if (!*name) { 258 pr_err("empty lowerdir\n"); 259 goto out; 260 } 261 err = kern_path(name, LOOKUP_FOLLOW, path); 262 if (err) { 263 pr_err("failed to resolve '%s': %i\n", name, err); 264 goto out; 265 } 266 return 0; 267 268 out: 269 return err; 270 } 271 272 static void ovl_unescape(char *s) 273 { 274 char *d = s; 275 276 for (;; s++, d++) { 277 if (*s == '\\') 278 s++; 279 *d = *s; 280 if (!*s) 281 break; 282 } 283 } 284 285 static int ovl_mount_dir(const char *name, struct path *path) 286 { 287 int err = -ENOMEM; 288 char *tmp = kstrdup(name, GFP_KERNEL); 289 290 if (tmp) { 291 ovl_unescape(tmp); 292 err = ovl_mount_dir_noesc(tmp, path); 293 kfree(tmp); 294 } 295 return err; 296 } 297 298 static int ovl_mount_dir_check(struct fs_context *fc, const struct path *path, 299 enum ovl_opt layer, const char *name, bool upper) 300 { 301 bool is_casefolded = ovl_dentry_casefolded(path->dentry); 302 struct ovl_fs_context *ctx = fc->fs_private; 303 struct ovl_fs *ofs = fc->s_fs_info; 304 305 if (!d_is_dir(path->dentry)) 306 return invalfc(fc, "%s is not a directory", name); 307 308 /* 309 * Allow filesystems that are case-folding capable but deny composing 310 * ovl stack from inconsistent case-folded directories. 311 */ 312 if (!ctx->casefold_set) { 313 ofs->casefold = is_casefolded; 314 ctx->casefold_set = true; 315 } 316 317 if (ofs->casefold != is_casefolded) { 318 return invalfc(fc, "case-%ssensitive directory on %s is inconsistent", 319 is_casefolded ? "in" : "", name); 320 } 321 322 if (ovl_dentry_weird(path->dentry)) 323 return invalfc(fc, "filesystem on %s not supported", name); 324 325 /* 326 * Check whether upper path is read-only here to report failures 327 * early. Don't forget to recheck when the superblock is created 328 * as the mount attributes could change. 329 */ 330 if (upper) { 331 if (path->dentry->d_flags & DCACHE_OP_REAL) 332 return invalfc(fc, "filesystem on %s not supported as upperdir", name); 333 if (__mnt_is_readonly(path->mnt)) 334 return invalfc(fc, "filesystem on %s is read-only", name); 335 } else { 336 if (ctx->lowerdir_all && layer != Opt_lowerdir) 337 return invalfc(fc, "lowerdir+ and datadir+ cannot follow lowerdir"); 338 if (ctx->nr_data && layer == Opt_lowerdir_add) 339 return invalfc(fc, "regular lower layers cannot follow data layers"); 340 if (ctx->nr == OVL_MAX_STACK) 341 return invalfc(fc, "too many lower directories, limit is %d", 342 OVL_MAX_STACK); 343 } 344 return 0; 345 } 346 347 static int ovl_ctx_realloc_lower(struct fs_context *fc) 348 { 349 struct ovl_fs_context *ctx = fc->fs_private; 350 struct ovl_fs_context_layer *l; 351 size_t nr; 352 353 if (ctx->nr < ctx->capacity) 354 return 0; 355 356 nr = min_t(size_t, max(4096 / sizeof(*l), ctx->capacity * 2), 357 OVL_MAX_STACK); 358 l = krealloc_array(ctx->lower, nr, sizeof(*l), GFP_KERNEL_ACCOUNT); 359 if (!l) 360 return -ENOMEM; 361 362 ctx->lower = l; 363 ctx->capacity = nr; 364 return 0; 365 } 366 367 static void ovl_add_layer(struct fs_context *fc, enum ovl_opt layer, 368 struct path *path, char **pname) 369 { 370 struct ovl_fs *ofs = fc->s_fs_info; 371 struct ovl_config *config = &ofs->config; 372 struct ovl_fs_context *ctx = fc->fs_private; 373 struct ovl_fs_context_layer *l; 374 375 switch (layer) { 376 case Opt_workdir: 377 swap(config->workdir, *pname); 378 swap(ctx->work, *path); 379 break; 380 case Opt_upperdir: 381 swap(config->upperdir, *pname); 382 swap(ctx->upper, *path); 383 break; 384 case Opt_datadir_add: 385 ctx->nr_data++; 386 fallthrough; 387 case Opt_lowerdir: 388 fallthrough; 389 case Opt_lowerdir_add: 390 WARN_ON(ctx->nr >= ctx->capacity); 391 l = &ctx->lower[ctx->nr++]; 392 memset(l, 0, sizeof(*l)); 393 swap(l->name, *pname); 394 swap(l->path, *path); 395 break; 396 default: 397 WARN_ON(1); 398 } 399 } 400 401 static inline bool is_upper_layer(enum ovl_opt layer) 402 { 403 return layer == Opt_upperdir || layer == Opt_workdir; 404 } 405 406 /* Handle non-file descriptor-based layer options that require path lookup. */ 407 static inline int ovl_kern_path(const char *layer_name, struct path *layer_path, 408 enum ovl_opt layer) 409 { 410 int err; 411 412 switch (layer) { 413 case Opt_upperdir: 414 fallthrough; 415 case Opt_workdir: 416 fallthrough; 417 case Opt_lowerdir: 418 err = ovl_mount_dir(layer_name, layer_path); 419 break; 420 case Opt_lowerdir_add: 421 fallthrough; 422 case Opt_datadir_add: 423 err = ovl_mount_dir_noesc(layer_name, layer_path); 424 break; 425 default: 426 WARN_ON_ONCE(true); 427 err = -EINVAL; 428 } 429 430 return err; 431 } 432 433 static int ovl_do_parse_layer(struct fs_context *fc, const char *layer_name, 434 struct path *layer_path, enum ovl_opt layer) 435 { 436 char *name __free(kfree) = kstrdup(layer_name, GFP_KERNEL); 437 bool upper; 438 int err = 0; 439 440 if (!name) 441 return -ENOMEM; 442 443 upper = is_upper_layer(layer); 444 err = ovl_mount_dir_check(fc, layer_path, layer, name, upper); 445 if (err) 446 return err; 447 448 if (!upper) { 449 err = ovl_ctx_realloc_lower(fc); 450 if (err) 451 return err; 452 } 453 454 /* Store the user provided path string in ctx to show in mountinfo */ 455 ovl_add_layer(fc, layer, layer_path, &name); 456 return err; 457 } 458 459 static int ovl_parse_layer(struct fs_context *fc, struct fs_parameter *param, 460 enum ovl_opt layer) 461 { 462 struct path layer_path __free(path_put) = {}; 463 int err = 0; 464 465 switch (param->type) { 466 case fs_value_is_string: 467 err = ovl_kern_path(param->string, &layer_path, layer); 468 if (err) 469 return err; 470 err = ovl_do_parse_layer(fc, param->string, &layer_path, layer); 471 break; 472 case fs_value_is_file: { 473 char *buf __free(kfree); 474 char *layer_name; 475 476 buf = kmalloc(PATH_MAX, GFP_KERNEL_ACCOUNT); 477 if (!buf) 478 return -ENOMEM; 479 480 layer_path = param->file->f_path; 481 path_get(&layer_path); 482 483 layer_name = d_path(&layer_path, buf, PATH_MAX); 484 if (IS_ERR(layer_name)) 485 return PTR_ERR(layer_name); 486 487 err = ovl_do_parse_layer(fc, layer_name, &layer_path, layer); 488 break; 489 } 490 default: 491 WARN_ON_ONCE(true); 492 err = -EINVAL; 493 } 494 495 return err; 496 } 497 498 static void ovl_reset_lowerdirs(struct ovl_fs_context *ctx) 499 { 500 struct ovl_fs_context_layer *l = ctx->lower; 501 502 // Reset old user provided lowerdir string 503 kfree(ctx->lowerdir_all); 504 ctx->lowerdir_all = NULL; 505 506 for (size_t nr = 0; nr < ctx->nr; nr++, l++) { 507 path_put(&l->path); 508 kfree(l->name); 509 l->name = NULL; 510 } 511 ctx->nr = 0; 512 ctx->nr_data = 0; 513 } 514 515 /* 516 * Parse lowerdir= mount option: 517 * 518 * e.g.: lowerdir=/lower1:/lower2:/lower3::/data1::/data2 519 * Set "/lower1", "/lower2", and "/lower3" as lower layers and 520 * "/data1" and "/data2" as data lower layers. Any existing lower 521 * layers are replaced. 522 */ 523 static int ovl_parse_param_lowerdir(const char *name, struct fs_context *fc) 524 { 525 int err; 526 struct ovl_fs_context *ctx = fc->fs_private; 527 char *dup = NULL, *iter; 528 ssize_t nr_lower, nr; 529 bool data_layer = false; 530 531 /* 532 * Ensure we're backwards compatible with mount(2) 533 * by allowing relative paths. 534 */ 535 536 /* drop all existing lower layers */ 537 ovl_reset_lowerdirs(ctx); 538 539 if (!*name) 540 return 0; 541 542 if (*name == ':') { 543 pr_err("cannot append lower layer\n"); 544 return -EINVAL; 545 } 546 547 // Store user provided lowerdir string to show in mount options 548 ctx->lowerdir_all = kstrdup(name, GFP_KERNEL); 549 if (!ctx->lowerdir_all) 550 return -ENOMEM; 551 552 dup = kstrdup(name, GFP_KERNEL); 553 if (!dup) 554 return -ENOMEM; 555 556 err = -EINVAL; 557 nr_lower = ovl_parse_param_split_lowerdirs(dup); 558 if (nr_lower < 0) 559 goto out_err; 560 561 if (nr_lower > OVL_MAX_STACK) { 562 pr_err("too many lower directories, limit is %d\n", OVL_MAX_STACK); 563 goto out_err; 564 } 565 566 iter = dup; 567 for (nr = 0; nr < nr_lower; nr++) { 568 struct path path __free(path_put) = {}; 569 570 err = ovl_kern_path(iter, &path, Opt_lowerdir); 571 if (err) 572 goto out_err; 573 574 err = ovl_do_parse_layer(fc, iter, &path, Opt_lowerdir); 575 if (err) 576 goto out_err; 577 578 if (data_layer) 579 ctx->nr_data++; 580 581 /* Calling strchr() again would overrun. */ 582 if (ctx->nr == nr_lower) 583 break; 584 585 err = -EINVAL; 586 iter = strchr(iter, '\0') + 1; 587 if (*iter) { 588 /* 589 * This is a regular layer so we require that 590 * there are no data layers. 591 */ 592 if (ctx->nr_data > 0) { 593 pr_err("regular lower layers cannot follow data lower layers\n"); 594 goto out_err; 595 } 596 597 data_layer = false; 598 continue; 599 } 600 601 /* This is a data lower layer. */ 602 data_layer = true; 603 iter++; 604 } 605 kfree(dup); 606 return 0; 607 608 out_err: 609 kfree(dup); 610 611 /* Intentionally don't realloc to a smaller size. */ 612 return err; 613 } 614 615 static int ovl_parse_param(struct fs_context *fc, struct fs_parameter *param) 616 { 617 int err = 0; 618 struct fs_parse_result result; 619 struct ovl_fs *ofs = fc->s_fs_info; 620 struct ovl_config *config = &ofs->config; 621 struct ovl_fs_context *ctx = fc->fs_private; 622 int opt; 623 624 if (fc->purpose == FS_CONTEXT_FOR_RECONFIGURE) { 625 /* 626 * On remount overlayfs has always ignored all mount 627 * options no matter if malformed or not so for 628 * backwards compatibility we do the same here. 629 */ 630 if (fc->oldapi) 631 return 0; 632 633 /* 634 * Give us the freedom to allow changing mount options 635 * with the new mount api in the future. So instead of 636 * silently ignoring everything we report a proper 637 * error. This is only visible for users of the new 638 * mount api. 639 */ 640 return invalfc(fc, "No changes allowed in reconfigure"); 641 } 642 643 opt = fs_parse(fc, ovl_parameter_spec, param, &result); 644 if (opt < 0) 645 return opt; 646 647 switch (opt) { 648 case Opt_lowerdir: 649 err = ovl_parse_param_lowerdir(param->string, fc); 650 break; 651 case Opt_lowerdir_add: 652 case Opt_datadir_add: 653 case Opt_upperdir: 654 case Opt_workdir: 655 err = ovl_parse_layer(fc, param, opt); 656 break; 657 case Opt_default_permissions: 658 config->default_permissions = true; 659 break; 660 case Opt_redirect_dir: 661 config->redirect_mode = result.uint_32; 662 if (config->redirect_mode == OVL_REDIRECT_OFF) { 663 config->redirect_mode = ovl_redirect_always_follow ? 664 OVL_REDIRECT_FOLLOW : 665 OVL_REDIRECT_NOFOLLOW; 666 } 667 ctx->set.redirect = true; 668 break; 669 case Opt_index: 670 config->index = result.uint_32; 671 ctx->set.index = true; 672 break; 673 case Opt_uuid: 674 config->uuid = result.uint_32; 675 break; 676 case Opt_nfs_export: 677 config->nfs_export = result.uint_32; 678 ctx->set.nfs_export = true; 679 break; 680 case Opt_xino: 681 config->xino = result.uint_32; 682 break; 683 case Opt_metacopy: 684 config->metacopy = result.uint_32; 685 ctx->set.metacopy = true; 686 break; 687 case Opt_verity: 688 config->verity_mode = result.uint_32; 689 break; 690 case Opt_fsync: 691 config->fsync_mode = result.uint_32; 692 break; 693 case Opt_volatile: 694 config->fsync_mode = OVL_FSYNC_VOLATILE; 695 break; 696 case Opt_userxattr: 697 config->userxattr = true; 698 break; 699 case Opt_override_creds: { 700 const struct cred *cred = NULL; 701 702 if (result.negated) { 703 swap(cred, ofs->creator_cred); 704 put_cred(cred); 705 break; 706 } 707 708 if (!current_in_userns(fc->user_ns)) { 709 err = -EINVAL; 710 break; 711 } 712 713 cred = prepare_creds(); 714 if (cred) 715 swap(cred, ofs->creator_cred); 716 else 717 err = -ENOMEM; 718 719 put_cred(cred); 720 break; 721 } 722 default: 723 pr_err("unrecognized mount option \"%s\" or missing value\n", 724 param->key); 725 return -EINVAL; 726 } 727 728 return err; 729 } 730 731 static int ovl_get_tree(struct fs_context *fc) 732 { 733 return get_tree_nodev(fc, ovl_fill_super); 734 } 735 736 static inline void ovl_fs_context_free(struct ovl_fs_context *ctx) 737 { 738 ovl_reset_lowerdirs(ctx); 739 path_put(&ctx->upper); 740 path_put(&ctx->work); 741 kfree(ctx->lower); 742 kfree(ctx); 743 } 744 745 static void ovl_free(struct fs_context *fc) 746 { 747 struct ovl_fs *ofs = fc->s_fs_info; 748 struct ovl_fs_context *ctx = fc->fs_private; 749 750 /* 751 * ofs is stored in the fs_context when it is initialized. 752 * ofs is transferred to the superblock on a successful mount, 753 * but if an error occurs before the transfer we have to free 754 * it here. 755 */ 756 if (ofs) 757 ovl_free_fs(ofs); 758 759 if (ctx) 760 ovl_fs_context_free(ctx); 761 } 762 763 static int ovl_reconfigure(struct fs_context *fc) 764 { 765 struct super_block *sb = fc->root->d_sb; 766 struct ovl_fs *ofs = OVL_FS(sb); 767 struct super_block *upper_sb; 768 int ret = 0; 769 770 if (!(fc->sb_flags & SB_RDONLY) && ovl_force_readonly(ofs)) 771 return -EROFS; 772 773 if (fc->sb_flags & SB_RDONLY && !sb_rdonly(sb)) { 774 upper_sb = ovl_upper_mnt(ofs)->mnt_sb; 775 if (ovl_should_sync(ofs)) { 776 down_read(&upper_sb->s_umount); 777 ret = sync_filesystem(upper_sb); 778 up_read(&upper_sb->s_umount); 779 } 780 } 781 782 return ret; 783 } 784 785 static const struct fs_context_operations ovl_context_ops = { 786 .parse_monolithic = ovl_parse_monolithic, 787 .parse_param = ovl_parse_param, 788 .get_tree = ovl_get_tree, 789 .reconfigure = ovl_reconfigure, 790 .free = ovl_free, 791 }; 792 793 /* 794 * This is called during fsopen() and will record the user namespace of 795 * the caller in fc->user_ns since we've raised FS_USERNS_MOUNT. We'll 796 * need it when we actually create the superblock to verify that the 797 * process creating the superblock is in the same user namespace as 798 * process that called fsopen(). 799 */ 800 int ovl_init_fs_context(struct fs_context *fc) 801 { 802 struct ovl_fs_context *ctx; 803 struct ovl_fs *ofs; 804 805 ctx = kzalloc_obj(*ctx, GFP_KERNEL_ACCOUNT); 806 if (!ctx) 807 return -ENOMEM; 808 809 /* 810 * By default we allocate for three lower layers. It's likely 811 * that it'll cover most users. 812 */ 813 ctx->lower = kmalloc_objs(*ctx->lower, 3, GFP_KERNEL_ACCOUNT); 814 if (!ctx->lower) 815 goto out_err; 816 ctx->capacity = 3; 817 818 ofs = kzalloc_obj(struct ovl_fs); 819 if (!ofs) 820 goto out_err; 821 822 ofs->config.redirect_mode = ovl_redirect_mode_def(); 823 ofs->config.index = ovl_index_def; 824 ofs->config.uuid = ovl_uuid_def(); 825 ofs->config.nfs_export = ovl_nfs_export_def; 826 ofs->config.xino = ovl_xino_def(); 827 ofs->config.metacopy = ovl_metacopy_def; 828 ofs->config.fsync_mode = ovl_fsync_mode_def(); 829 830 fc->s_fs_info = ofs; 831 fc->fs_private = ctx; 832 fc->ops = &ovl_context_ops; 833 834 mutex_init(&ofs->whiteout_lock); 835 return 0; 836 837 out_err: 838 ovl_fs_context_free(ctx); 839 return -ENOMEM; 840 841 } 842 843 void ovl_free_fs(struct ovl_fs *ofs) 844 { 845 struct vfsmount **mounts; 846 unsigned i; 847 848 iput(ofs->workbasedir_trap); 849 iput(ofs->workdir_trap); 850 dput(ofs->whiteout); 851 dput(ofs->workdir); 852 if (ofs->workdir_locked) 853 ovl_inuse_unlock(ofs->workbasedir); 854 dput(ofs->workbasedir); 855 if (ofs->upperdir_locked) 856 ovl_inuse_unlock(ovl_upper_mnt(ofs)->mnt_root); 857 858 /* Reuse ofs->config.lowerdirs as a vfsmount array before freeing it */ 859 mounts = (struct vfsmount **) ofs->config.lowerdirs; 860 for (i = 0; i < ofs->numlayer; i++) { 861 iput(ofs->layers[i].trap); 862 kfree(ofs->config.lowerdirs[i]); 863 mounts[i] = ofs->layers[i].mnt; 864 } 865 kern_unmount_array(mounts, ofs->numlayer); 866 kfree(ofs->layers); 867 for (i = 0; i < ofs->numfs; i++) 868 free_anon_bdev(ofs->fs[i].pseudo_dev); 869 kfree(ofs->fs); 870 871 kfree(ofs->config.lowerdirs); 872 kfree(ofs->config.upperdir); 873 kfree(ofs->config.workdir); 874 if (ofs->creator_cred) 875 put_cred(ofs->creator_cred); 876 kfree(ofs); 877 } 878 879 int ovl_fs_params_verify(const struct ovl_fs_context *ctx, 880 struct ovl_config *config) 881 { 882 struct ovl_opt_set set = ctx->set; 883 884 /* Workdir/index are useless in non-upper mount */ 885 if (!config->upperdir) { 886 if (config->workdir) { 887 pr_info("option \"workdir=%s\" is useless in a non-upper mount, ignore\n", 888 config->workdir); 889 kfree(config->workdir); 890 config->workdir = NULL; 891 } 892 if (config->index && set.index) { 893 pr_info("option \"index=on\" is useless in a non-upper mount, ignore\n"); 894 set.index = false; 895 } 896 config->index = false; 897 } 898 899 if (!config->upperdir && ovl_is_volatile(config)) { 900 pr_info("option \"volatile\" is meaningless in a non-upper mount, ignoring it.\n"); 901 config->fsync_mode = ovl_fsync_mode_def(); 902 } 903 904 if (!config->upperdir && config->uuid == OVL_UUID_ON) { 905 pr_info("option \"uuid=on\" requires an upper fs, falling back to uuid=null.\n"); 906 config->uuid = OVL_UUID_NULL; 907 } 908 909 /* 910 * This is to make the logic below simpler. It doesn't make any other 911 * difference, since redirect_dir=on is only used for upper. 912 */ 913 if (!config->upperdir && config->redirect_mode == OVL_REDIRECT_FOLLOW) 914 config->redirect_mode = OVL_REDIRECT_ON; 915 916 /* metacopy -> redirect_dir dependency */ 917 if (config->metacopy && config->redirect_mode != OVL_REDIRECT_ON) { 918 if (set.metacopy && set.redirect) { 919 pr_err("conflicting options: metacopy=on,redirect_dir=%s\n", 920 ovl_redirect_mode(config)); 921 return -EINVAL; 922 } 923 if (set.redirect) { 924 /* 925 * There was an explicit redirect_dir=... that resulted 926 * in this conflict. 927 */ 928 pr_info("disabling metacopy due to redirect_dir=%s\n", 929 ovl_redirect_mode(config)); 930 config->metacopy = false; 931 } else { 932 /* Automatically enable redirect otherwise. */ 933 config->redirect_mode = OVL_REDIRECT_ON; 934 } 935 } 936 937 /* Resolve nfs_export -> index dependency */ 938 if (config->nfs_export && !config->index) { 939 if (!config->upperdir && 940 config->redirect_mode != OVL_REDIRECT_NOFOLLOW) { 941 pr_info("NFS export requires \"redirect_dir=nofollow\" on non-upper mount, falling back to nfs_export=off.\n"); 942 config->nfs_export = false; 943 } else if (set.nfs_export && set.index) { 944 pr_err("conflicting options: nfs_export=on,index=off\n"); 945 return -EINVAL; 946 } else if (set.index) { 947 /* 948 * There was an explicit index=off that resulted 949 * in this conflict. 950 */ 951 pr_info("disabling nfs_export due to index=off\n"); 952 config->nfs_export = false; 953 } else { 954 /* Automatically enable index otherwise. */ 955 config->index = true; 956 } 957 } 958 959 /* Resolve nfs_export -> !metacopy && !verity dependency */ 960 if (config->nfs_export && config->metacopy) { 961 if (set.nfs_export && set.metacopy) { 962 pr_err("conflicting options: nfs_export=on,metacopy=on\n"); 963 return -EINVAL; 964 } 965 if (set.metacopy) { 966 /* 967 * There was an explicit metacopy=on that resulted 968 * in this conflict. 969 */ 970 pr_info("disabling nfs_export due to metacopy=on\n"); 971 config->nfs_export = false; 972 } else if (config->verity_mode) { 973 /* 974 * There was an explicit verity=.. that resulted 975 * in this conflict. 976 */ 977 pr_info("disabling nfs_export due to verity=%s\n", 978 ovl_verity_mode(config)); 979 config->nfs_export = false; 980 } else { 981 /* 982 * There was an explicit nfs_export=on that resulted 983 * in this conflict. 984 */ 985 pr_info("disabling metacopy due to nfs_export=on\n"); 986 config->metacopy = false; 987 } 988 } 989 990 991 /* Resolve userxattr -> !redirect && !metacopy dependency */ 992 if (config->userxattr) { 993 if (set.redirect && 994 config->redirect_mode != OVL_REDIRECT_NOFOLLOW) { 995 pr_err("conflicting options: userxattr,redirect_dir=%s\n", 996 ovl_redirect_mode(config)); 997 return -EINVAL; 998 } 999 if (config->metacopy && set.metacopy) { 1000 pr_err("conflicting options: userxattr,metacopy=on\n"); 1001 return -EINVAL; 1002 } 1003 /* 1004 * Silently disable default setting of redirect and metacopy. 1005 * This shall be the default in the future as well: these 1006 * options must be explicitly enabled if used together with 1007 * userxattr. 1008 */ 1009 config->redirect_mode = OVL_REDIRECT_NOFOLLOW; 1010 config->metacopy = false; 1011 } 1012 1013 /* 1014 * Fail if we don't have trusted xattr capability and a feature was 1015 * explicitly requested that requires them. 1016 */ 1017 if (!config->userxattr && !capable(CAP_SYS_ADMIN)) { 1018 if (set.redirect && 1019 config->redirect_mode != OVL_REDIRECT_NOFOLLOW) { 1020 pr_err("redirect_dir requires permission to access trusted xattrs\n"); 1021 return -EPERM; 1022 } 1023 if (config->metacopy && set.metacopy) { 1024 pr_err("metacopy requires permission to access trusted xattrs\n"); 1025 return -EPERM; 1026 } 1027 if (config->verity_mode) { 1028 pr_err("verity requires permission to access trusted xattrs\n"); 1029 return -EPERM; 1030 } 1031 if (ctx->nr_data > 0) { 1032 pr_err("lower data-only dirs require permission to access trusted xattrs\n"); 1033 return -EPERM; 1034 } 1035 /* 1036 * Other xattr-dependent features should be disabled without 1037 * great disturbance to the user in ovl_make_workdir(). 1038 */ 1039 } 1040 1041 return 0; 1042 } 1043 1044 /** 1045 * ovl_show_options 1046 * @m: the seq_file handle 1047 * @dentry: The dentry to query 1048 * 1049 * Prints the mount options for a given superblock. 1050 * Returns zero; does not fail. 1051 */ 1052 int ovl_show_options(struct seq_file *m, struct dentry *dentry) 1053 { 1054 struct super_block *sb = dentry->d_sb; 1055 struct ovl_fs *ofs = OVL_FS(sb); 1056 size_t nr, nr_merged_lower, nr_lower = 0; 1057 char **lowerdirs = ofs->config.lowerdirs; 1058 1059 /* 1060 * lowerdirs[0] holds the colon separated list that user provided 1061 * with lowerdir mount option. 1062 * lowerdirs[1..numlayer] hold the lowerdir paths that were added 1063 * using the lowerdir+ and datadir+ mount options. 1064 * For now, we do not allow mixing the legacy lowerdir mount option 1065 * with the new lowerdir+ and datadir+ mount options. 1066 */ 1067 if (lowerdirs[0]) { 1068 seq_show_option(m, "lowerdir", lowerdirs[0]); 1069 } else { 1070 nr_lower = ofs->numlayer; 1071 nr_merged_lower = nr_lower - ofs->numdatalayer; 1072 } 1073 for (nr = 1; nr < nr_lower; nr++) { 1074 if (nr < nr_merged_lower) 1075 seq_show_option(m, "lowerdir+", lowerdirs[nr]); 1076 else 1077 seq_show_option(m, "datadir+", lowerdirs[nr]); 1078 } 1079 if (ofs->config.upperdir) { 1080 seq_show_option(m, "upperdir", ofs->config.upperdir); 1081 seq_show_option(m, "workdir", ofs->config.workdir); 1082 } 1083 if (ofs->config.default_permissions) 1084 seq_puts(m, ",default_permissions"); 1085 if (ofs->config.redirect_mode != ovl_redirect_mode_def()) 1086 seq_printf(m, ",redirect_dir=%s", 1087 ovl_redirect_mode(&ofs->config)); 1088 if (ofs->config.index != ovl_index_def) 1089 seq_printf(m, ",index=%s", str_on_off(ofs->config.index)); 1090 if (ofs->config.uuid != ovl_uuid_def()) 1091 seq_printf(m, ",uuid=%s", ovl_uuid_mode(&ofs->config)); 1092 if (ofs->config.nfs_export != ovl_nfs_export_def) 1093 seq_printf(m, ",nfs_export=%s", 1094 str_on_off(ofs->config.nfs_export)); 1095 if (ofs->config.xino != ovl_xino_def() && !ovl_same_fs(ofs)) 1096 seq_printf(m, ",xino=%s", ovl_xino_mode(&ofs->config)); 1097 if (ofs->config.metacopy != ovl_metacopy_def) 1098 seq_printf(m, ",metacopy=%s", str_on_off(ofs->config.metacopy)); 1099 if (ofs->config.fsync_mode != ovl_fsync_mode_def()) 1100 seq_printf(m, ",fsync=%s", ovl_fsync_mode(&ofs->config)); 1101 if (ofs->config.userxattr) 1102 seq_puts(m, ",userxattr"); 1103 if (ofs->config.verity_mode != ovl_verity_mode_def()) 1104 seq_printf(m, ",verity=%s", 1105 ovl_verity_mode(&ofs->config)); 1106 return 0; 1107 } 1108