new primitive: vfs_dup_fs_context(). Comes with fs_context method (->dup()) for copying the filesystem-specific parts of fs_context, along with LSM one (->fs_context_dup()) for doing the same to LSM parts. [needs better commit message, and change of Author:, anyway]
Unified · Split
| @@ -337,6 +337,47 @@ void fc_drop_locked(struct fs_context *fc) · 336 unmodified lines | ||
| 337 | 337 | |
| 338 | 338 | static void legacy_fs_context_free(struct fs_context *fc); |
| 339 | 339 | |
| 340 | +/** | |
| 341 | + * vfs_dup_fc_config: Duplicate a filesystem context. | |
| 342 | + * @src_fc: The context to copy. | |
| 343 | + */ | |
| 344 | +struct fs_context *vfs_dup_fs_context(struct fs_context *src_fc) | |
| 345 | +{ | |
| 346 | + struct fs_context *fc; | |
| 347 | + int ret; | |
| 348 | + | |
| 349 | + if (!src_fc->ops->dup) | |
| 350 | + return ERR_PTR(-EOPNOTSUPP); | |
| 351 | + | |
| 352 | + fc = kmemdup(src_fc, sizeof(struct fs_context), GFP_KERNEL); | |
| 353 | + if (!fc) | |
| 354 | + return ERR_PTR(-ENOMEM); | |
| 355 | + | |
| 356 | + fc->fs_private = NULL; | |
| 357 | + fc->s_fs_info = NULL; | |
| 358 | + fc->source = NULL; | |
| 359 | + fc->security = NULL; | |
| 360 | + get_filesystem(fc->fs_type); | |
| 361 | + get_net(fc->net_ns); | |
| 362 | + get_user_ns(fc->user_ns); | |
| 363 | + get_cred(fc->cred); | |
| 364 | + | |
| 365 | + /* Can't call put until we've called ->dup */ | |
| 366 | + ret = fc->ops->dup(fc, src_fc); | |
| 367 | + if (ret < 0) | |
| 368 | + goto err_fc; | |
| 369 | + | |
| 370 | + ret = security_fs_context_dup(fc, src_fc); | |
| 371 | + if (ret < 0) | |
| 372 | + goto err_fc; | |
| 373 | + return fc; | |
| 374 | + | |
| 375 | +err_fc: | |
| 376 | + put_fs_context(fc); | |
| 377 | + return ERR_PTR(ret); | |
| 378 | +} | |
| 379 | +EXPORT_SYMBOL(vfs_dup_fs_context); | |
| 380 | + | |
| 340 | 381 | /** |
| 341 | 382 | * put_fs_context - Dispose of a superblock configuration context. |
| 342 | 383 | * @fc: The context to dispose of. |
| @@ -380,6 +421,31 @@ static void legacy_fs_context_free(struct fs_context *fc) · 37 unmodified lines | ||
| 380 | 421 | } |
| 381 | 422 | } |
| 382 | 423 | |
| 424 | +/* | |
| 425 | + * Duplicate a legacy config. | |
| 426 | + */ | |
| 427 | +static int legacy_fs_context_dup(struct fs_context *fc, struct fs_context *src_fc) | |
| 428 | +{ | |
| 429 | + struct legacy_fs_context *ctx; | |
| 430 | + struct legacy_fs_context *src_ctx = src_fc->fs_private; | |
| 431 | + | |
| 432 | + ctx = kmemdup(src_ctx, sizeof(*src_ctx), GFP_KERNEL); | |
| 433 | + if (!ctx) | |
| 434 | + return -ENOMEM; | |
| 435 | + | |
| 436 | + if (ctx->param_type == LEGACY_FS_INDIVIDUAL_PARAMS) { | |
| 437 | + ctx->legacy_data = kmemdup(src_ctx->legacy_data, | |
| 438 | + src_ctx->data_size, GFP_KERNEL); | |
| 439 | + if (!ctx->legacy_data) { | |
| 440 | + kfree(ctx); | |
| 441 | + return -ENOMEM; | |
| 442 | + } | |
| 443 | + } | |
| 444 | + | |
| 445 | + fc->fs_private = ctx; | |
| 446 | + return 0; | |
| 447 | +} | |
| 448 | + | |
| 383 | 449 | /* |
| 384 | 450 | * Add a parameter to a legacy config. We build up a comma-separated list of |
| 385 | 451 | * options. |
| @@ -514,6 +580,7 @@ static int legacy_reconfigure(struct fs_context *fc) · 128 unmodified lines | ||
| 514 | 580 | |
| 515 | 581 | const struct fs_context_operations legacy_fs_context_ops = { |
| 516 | 582 | .free = legacy_fs_context_free, |
| 583 | + .dup = legacy_fs_context_dup, | |
| 517 | 584 | .parse_param = legacy_parse_param, |
| 518 | 585 | .parse_monolithic = legacy_parse_monolithic, |
| 519 | 586 | .get_tree = legacy_get_tree, |
| @@ -94,6 +94,7 @@ struct fs_context { · 93 unmodified lines | ||
| 94 | 94 | |
| 95 | 95 | struct fs_context_operations { |
| 96 | 96 | void (*free)(struct fs_context *fc); |
| 97 | + int (*dup)(struct fs_context *fc, struct fs_context *src_fc); | |
| 97 | 98 | int (*parse_param)(struct fs_context *fc, struct fs_parameter *param); |
| 98 | 99 | int (*parse_monolithic)(struct fs_context *fc, void *data); |
| 99 | 100 | int (*get_tree)(struct fs_context *fc); |
| @@ -111,6 +112,7 @@ extern struct fs_context *fs_context_for_reconfigure(struct dentry *dentry, · 11 unmodified lines | ||
| 111 | 112 | extern struct fs_context *fs_context_for_submount(struct file_system_type *fs_type, |
| 112 | 113 | struct dentry *reference); |
| 113 | 114 | |
| 115 | +extern struct fs_context *vfs_dup_fs_context(struct fs_context *fc); | |
| 114 | 116 | extern int vfs_parse_fs_param(struct fs_context *fc, struct fs_parameter *param); |
| 115 | 117 | extern int vfs_parse_fs_string(struct fs_context *fc, const char *key, |
| 116 | 118 | const char *value, size_t v_size); |
| @@ -79,6 +79,11 @@ · 78 unmodified lines | ||
| 79 | 79 | * Security hooks for mount using fs_context. |
| 80 | 80 | * [See also Documentation/filesystems/mounting.txt] |
| 81 | 81 | * |
| 82 | + * @fs_context_dup: | |
| 83 | + * Allocate and attach a security structure to sc->security. This pointer | |
| 84 | + * is initialised to NULL by the caller. | |
| 85 | + * @fc indicates the new filesystem context. | |
| 86 | + * @src_fc indicates the original filesystem context. | |
| 82 | 87 | * @fs_context_parse_param: |
| 83 | 88 | * Userspace provided a parameter to configure a superblock. The LSM may |
| 84 | 89 | * reject it with an error and may use it for itself, in which case it |
| @@ -1470,6 +1475,7 @@ union security_list_options { · 1385 unmodified lines | ||
| 1470 | 1475 | void (*bprm_committing_creds)(struct linux_binprm *bprm); |
| 1471 | 1476 | void (*bprm_committed_creds)(struct linux_binprm *bprm); |
| 1472 | 1477 | |
| 1478 | + int (*fs_context_dup)(struct fs_context *fc, struct fs_context *src_sc); | |
| 1473 | 1479 | int (*fs_context_parse_param)(struct fs_context *fc, struct fs_parameter *param); |
| 1474 | 1480 | |
| 1475 | 1481 | int (*sb_alloc_security)(struct super_block *sb); |
| @@ -1813,6 +1819,7 @@ struct security_hook_heads { · 337 unmodified lines | ||
| 1813 | 1819 | struct hlist_head bprm_check_security; |
| 1814 | 1820 | struct hlist_head bprm_committing_creds; |
| 1815 | 1821 | struct hlist_head bprm_committed_creds; |
| 1822 | + struct hlist_head fs_context_dup; | |
| 1816 | 1823 | struct hlist_head fs_context_parse_param; |
| 1817 | 1824 | struct hlist_head sb_alloc_security; |
| 1818 | 1825 | struct hlist_head sb_free_security; |
| @@ -223,6 +223,7 @@ int security_bprm_set_creds(struct linux_binprm *bprm); · 222 unmodified lines | ||
| 223 | 223 | int security_bprm_check(struct linux_binprm *bprm); |
| 224 | 224 | void security_bprm_committing_creds(struct linux_binprm *bprm); |
| 225 | 225 | void security_bprm_committed_creds(struct linux_binprm *bprm); |
| 226 | +int security_fs_context_dup(struct fs_context *fc, struct fs_context *src_fc); | |
| 226 | 227 | int security_fs_context_parse_param(struct fs_context *fc, struct fs_parameter *param); |
| 227 | 228 | int security_sb_alloc(struct super_block *sb); |
| 228 | 229 | void security_sb_free(struct super_block *sb); |
| @@ -521,6 +522,11 @@ static inline void security_bprm_committed_creds(struct linux_binprm *bprm) · 292 unmodified lines | ||
| 521 | 522 | { |
| 522 | 523 | } |
| 523 | 524 | |
| 525 | +static inline int security_fs_context_dup(struct fs_context *fc, | |
| 526 | + struct fs_context *src_fc) | |
| 527 | +{ | |
| 528 | + return 0; | |
| 529 | +} | |
| 524 | 530 | static inline int security_fs_context_parse_param(struct fs_context *fc, |
| 525 | 531 | struct fs_parameter *param) |
| 526 | 532 | { |
| @@ -374,6 +374,11 @@ void security_bprm_committed_creds(struct linux_binprm *bprm) · 373 unmodified lines | ||
| 374 | 374 | call_void_hook(bprm_committed_creds, bprm); |
| 375 | 375 | } |
| 376 | 376 | |
| 377 | +int security_fs_context_dup(struct fs_context *fc, struct fs_context *src_fc) | |
| 378 | +{ | |
| 379 | + return call_int_hook(fs_context_dup, 0, fc, src_fc); | |
| 380 | +} | |
| 381 | + | |
| 377 | 382 | int security_fs_context_parse_param(struct fs_context *fc, struct fs_parameter *param) |
| 378 | 383 | { |
| 379 | 384 | return call_int_hook(fs_context_parse_param, -ENOPARAM, fc, param); |
| @@ -2764,6 +2764,44 @@ static int selinux_umount(struct vfsmount *mnt, int flags) · 2763 unmodified lines | ||
| 2764 | 2764 | FILESYSTEM__UNMOUNT, NULL); |
| 2765 | 2765 | } |
| 2766 | 2766 | |
| 2767 | +static int selinux_fs_context_dup(struct fs_context *fc, | |
| 2768 | + struct fs_context *src_fc) | |
| 2769 | +{ | |
| 2770 | + const struct selinux_mnt_opts *src = src_fc->security; | |
| 2771 | + struct selinux_mnt_opts *opts; | |
| 2772 | + | |
| 2773 | + if (!src) | |
| 2774 | + return 0; | |
| 2775 | + | |
| 2776 | + fc->security = kzalloc(sizeof(struct selinux_mnt_opts), GFP_KERNEL); | |
| 2777 | + if (!fc->security) | |
| 2778 | + return -ENOMEM; | |
| 2779 | + | |
| 2780 | + opts = fc->security; | |
| 2781 | + | |
| 2782 | + if (src->fscontext) { | |
| 2783 | + opts->fscontext = kstrdup(src->fscontext, GFP_KERNEL); | |
| 2784 | + if (!opts->fscontext) | |
| 2785 | + return -ENOMEM; | |
| 2786 | + } | |
| 2787 | + if (src->context) { | |
| 2788 | + opts->context = kstrdup(src->context, GFP_KERNEL); | |
| 2789 | + if (!opts->context) | |
| 2790 | + return -ENOMEM; | |
| 2791 | + } | |
| 2792 | + if (src->rootcontext) { | |
| 2793 | + opts->rootcontext = kstrdup(src->rootcontext, GFP_KERNEL); | |
| 2794 | + if (!opts->rootcontext) | |
| 2795 | + return -ENOMEM; | |
| 2796 | + } | |
| 2797 | + if (src->defcontext) { | |
| 2798 | + opts->defcontext = kstrdup(src->defcontext, GFP_KERNEL); | |
| 2799 | + if (!opts->defcontext) | |
| 2800 | + return -ENOMEM; | |
| 2801 | + } | |
| 2802 | + return 0; | |
| 2803 | +} | |
| 2804 | + | |
| 2767 | 2805 | static const struct fs_parameter_spec selinux_param_specs[] = { |
| 2768 | 2806 | fsparam_string(CONTEXT_STR, Opt_context), |
| 2769 | 2807 | fsparam_string(DEFCONTEXT_STR, Opt_defcontext), |
| @@ -6745,6 +6783,7 @@ static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = { · 3975 unmodified lines | ||
| 6745 | 6783 | LSM_HOOK_INIT(bprm_committing_creds, selinux_bprm_committing_creds), |
| 6746 | 6784 | LSM_HOOK_INIT(bprm_committed_creds, selinux_bprm_committed_creds), |
| 6747 | 6785 | |
| 6786 | + LSM_HOOK_INIT(fs_context_dup, selinux_fs_context_dup), | |
| 6748 | 6787 | LSM_HOOK_INIT(fs_context_parse_param, selinux_fs_context_parse_param), |
| 6749 | 6788 | |
| 6750 | 6789 | LSM_HOOK_INIT(sb_alloc_security, selinux_sb_alloc_security), |
| @@ -647,6 +647,54 @@ out_opt_err: · 646 unmodified lines | ||
| 647 | 647 | return -EINVAL; |
| 648 | 648 | } |
| 649 | 649 | |
| 650 | +/** | |
| 651 | + * smack_fs_context_dup - Duplicate the security data on fs_context duplication | |
| 652 | + * @fc: The new filesystem context. | |
| 653 | + * @src_fc: The source filesystem context being duplicated. | |
| 654 | + * | |
| 655 | + * Returns 0 on success or -ENOMEM on error. | |
| 656 | + */ | |
| 657 | +static int smack_fs_context_dup(struct fs_context *fc, | |
| 658 | + struct fs_context *src_fc) | |
| 659 | +{ | |
| 660 | + struct smack_mnt_opts *dst, *src = src_fc->security; | |
| 661 | + | |
| 662 | + if (!src) | |
| 663 | + return 0; | |
| 664 | + | |
| 665 | + fc->security = kzalloc(sizeof(struct smack_mnt_opts), GFP_KERNEL); | |
| 666 | + if (!fc->security) | |
| 667 | + return -ENOMEM; | |
| 668 | + dst = fc->security; | |
| 669 | + | |
| 670 | + if (src->fsdefault) { | |
| 671 | + dst->fsdefault = kstrdup(src->fsdefault, GFP_KERNEL); | |
| 672 | + if (!dst->fsdefault) | |
| 673 | + return -ENOMEM; | |
| 674 | + } | |
| 675 | + if (src->fsfloor) { | |
| 676 | + dst->fsfloor = kstrdup(src->fsfloor, GFP_KERNEL); | |
| 677 | + if (!dst->fsfloor) | |
| 678 | + return -ENOMEM; | |
| 679 | + } | |
| 680 | + if (src->fshat) { | |
| 681 | + dst->fshat = kstrdup(src->fshat, GFP_KERNEL); | |
| 682 | + if (!dst->fshat) | |
| 683 | + return -ENOMEM; | |
| 684 | + } | |
| 685 | + if (src->fsroot) { | |
| 686 | + dst->fsroot = kstrdup(src->fsroot, GFP_KERNEL); | |
| 687 | + if (!dst->fsroot) | |
| 688 | + return -ENOMEM; | |
| 689 | + } | |
| 690 | + if (src->fstransmute) { | |
| 691 | + dst->fstransmute = kstrdup(src->fstransmute, GFP_KERNEL); | |
| 692 | + if (!dst->fstransmute) | |
| 693 | + return -ENOMEM; | |
| 694 | + } | |
| 695 | + return 0; | |
| 696 | +} | |
| 697 | + | |
| 650 | 698 | static const struct fs_parameter_spec smack_param_specs[] = { |
| 651 | 699 | fsparam_string("fsdefault", Opt_fsdefault), |
| 652 | 700 | fsparam_string("fsfloor", Opt_fsfloor), |
| @@ -4626,6 +4674,7 @@ static struct security_hook_list smack_hooks[] __lsm_ro_after_init = { · 3973 unmodified lines | ||
| 4626 | 4674 | LSM_HOOK_INIT(ptrace_traceme, smack_ptrace_traceme), |
| 4627 | 4675 | LSM_HOOK_INIT(syslog, smack_syslog), |
| 4628 | 4676 | |
| 4677 | + LSM_HOOK_INIT(fs_context_dup, smack_fs_context_dup), | |
| 4629 | 4678 | LSM_HOOK_INIT(fs_context_parse_param, smack_fs_context_parse_param), |
| 4630 | 4679 | |
| 4631 | 4680 | LSM_HOOK_INIT(sb_alloc_security, smack_sb_alloc_security), |