History

introduce cloning of fs_context

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]

Al Viro committed 7 years ago

Commit 0b520751 parent cb50b347 files · +175 −0Browse files

Unified · Split

7 files
  1. fs/fs_context.c +67 −0
  2. include/linux/fs_context.h +2 −0
  3. include/linux/lsm_hooks.h +7 −0
  4. include/linux/security.h +6 −0
  5. security/security.c +5 −0
  6. security/selinux/hooks.c +39 −0
  7. security/smack/smack_lsm.c +49 −0
fs/fs_context.c+67−0
@@ -337,6 +337,47 @@ void fc_drop_locked(struct fs_context *fc) · 336 unmodified lines
337 337
338static void legacy_fs_context_free(struct fs_context *fc);338static 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 */
344struct 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
375err_fc:
376 put_fs_context(fc);
377 return ERR_PTR(ret);
378}
379EXPORT_SYMBOL(vfs_dup_fs_context);
380
340/**381/**
341 * put_fs_context - Dispose of a superblock configuration context.382 * put_fs_context - Dispose of a superblock configuration context.
342 * @fc: The context to dispose of.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 */
427static 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 * Add a parameter to a legacy config. We build up a comma-separated list of450 * Add a parameter to a legacy config. We build up a comma-separated list of
385 * options.451 * options.
@@ -514,6 +580,7 @@ static int legacy_reconfigure(struct fs_context *fc) · 128 unmodified lines
514 580
515const struct fs_context_operations legacy_fs_context_ops = {581const struct fs_context_operations legacy_fs_context_ops = {
516 .free = legacy_fs_context_free,582 .free = legacy_fs_context_free,
583 .dup = legacy_fs_context_dup,
517 .parse_param = legacy_parse_param,584 .parse_param = legacy_parse_param,
518 .parse_monolithic = legacy_parse_monolithic,585 .parse_monolithic = legacy_parse_monolithic,
519 .get_tree = legacy_get_tree,586 .get_tree = legacy_get_tree,
include/linux/fs_context.h+2−0
@@ -94,6 +94,7 @@ struct fs_context { · 93 unmodified lines
94 94
95struct fs_context_operations {95struct fs_context_operations {
96 void (*free)(struct fs_context *fc);96 void (*free)(struct fs_context *fc);
97 int (*dup)(struct fs_context *fc, struct fs_context *src_fc);
97 int (*parse_param)(struct fs_context *fc, struct fs_parameter *param);98 int (*parse_param)(struct fs_context *fc, struct fs_parameter *param);
98 int (*parse_monolithic)(struct fs_context *fc, void *data);99 int (*parse_monolithic)(struct fs_context *fc, void *data);
99 int (*get_tree)(struct fs_context *fc);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
111extern struct fs_context *fs_context_for_submount(struct file_system_type *fs_type,112extern struct fs_context *fs_context_for_submount(struct file_system_type *fs_type,
112 struct dentry *reference);113 struct dentry *reference);
113 114
115extern struct fs_context *vfs_dup_fs_context(struct fs_context *fc);
114extern int vfs_parse_fs_param(struct fs_context *fc, struct fs_parameter *param);116extern int vfs_parse_fs_param(struct fs_context *fc, struct fs_parameter *param);
115extern int vfs_parse_fs_string(struct fs_context *fc, const char *key,117extern int vfs_parse_fs_string(struct fs_context *fc, const char *key,
116 const char *value, size_t v_size);118 const char *value, size_t v_size);
include/linux/lsm_hooks.h+7−0
@@ -79,6 +79,11 @@ · 78 unmodified lines
79 * Security hooks for mount using fs_context.79 * Security hooks for mount using fs_context.
80 * [See also Documentation/filesystems/mounting.txt]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 * @fs_context_parse_param:87 * @fs_context_parse_param:
83 * Userspace provided a parameter to configure a superblock. The LSM may88 * Userspace provided a parameter to configure a superblock. The LSM may
84 * reject it with an error and may use it for itself, in which case it89 * 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 void (*bprm_committing_creds)(struct linux_binprm *bprm);1475 void (*bprm_committing_creds)(struct linux_binprm *bprm);
1471 void (*bprm_committed_creds)(struct linux_binprm *bprm);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 int (*fs_context_parse_param)(struct fs_context *fc, struct fs_parameter *param);1479 int (*fs_context_parse_param)(struct fs_context *fc, struct fs_parameter *param);
1474 1480
1475 int (*sb_alloc_security)(struct super_block *sb);1481 int (*sb_alloc_security)(struct super_block *sb);
@@ -1813,6 +1819,7 @@ struct security_hook_heads { · 337 unmodified lines
1813 struct hlist_head bprm_check_security;1819 struct hlist_head bprm_check_security;
1814 struct hlist_head bprm_committing_creds;1820 struct hlist_head bprm_committing_creds;
1815 struct hlist_head bprm_committed_creds;1821 struct hlist_head bprm_committed_creds;
1822 struct hlist_head fs_context_dup;
1816 struct hlist_head fs_context_parse_param;1823 struct hlist_head fs_context_parse_param;
1817 struct hlist_head sb_alloc_security;1824 struct hlist_head sb_alloc_security;
1818 struct hlist_head sb_free_security;1825 struct hlist_head sb_free_security;
include/linux/security.h+6−0
@@ -223,6 +223,7 @@ int security_bprm_set_creds(struct linux_binprm *bprm); · 222 unmodified lines
223int security_bprm_check(struct linux_binprm *bprm);223int security_bprm_check(struct linux_binprm *bprm);
224void security_bprm_committing_creds(struct linux_binprm *bprm);224void security_bprm_committing_creds(struct linux_binprm *bprm);
225void security_bprm_committed_creds(struct linux_binprm *bprm);225void security_bprm_committed_creds(struct linux_binprm *bprm);
226int security_fs_context_dup(struct fs_context *fc, struct fs_context *src_fc);
226int security_fs_context_parse_param(struct fs_context *fc, struct fs_parameter *param);227int security_fs_context_parse_param(struct fs_context *fc, struct fs_parameter *param);
227int security_sb_alloc(struct super_block *sb);228int security_sb_alloc(struct super_block *sb);
228void security_sb_free(struct super_block *sb);229void 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
525static inline int security_fs_context_dup(struct fs_context *fc,
526 struct fs_context *src_fc)
527{
528 return 0;
529}
524static inline int security_fs_context_parse_param(struct fs_context *fc,530static inline int security_fs_context_parse_param(struct fs_context *fc,
525 struct fs_parameter *param)531 struct fs_parameter *param)
526{532{
security/security.c+5−0
@@ -374,6 +374,11 @@ void security_bprm_committed_creds(struct linux_binprm *bprm) · 373 unmodified lines
374 call_void_hook(bprm_committed_creds, bprm);374 call_void_hook(bprm_committed_creds, bprm);
375}375}
376 376
377int 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
377int security_fs_context_parse_param(struct fs_context *fc, struct fs_parameter *param)382int security_fs_context_parse_param(struct fs_context *fc, struct fs_parameter *param)
378{383{
379 return call_int_hook(fs_context_parse_param, -ENOPARAM, fc, param);384 return call_int_hook(fs_context_parse_param, -ENOPARAM, fc, param);
security/selinux/hooks.c+39−0
@@ -2764,6 +2764,44 @@ static int selinux_umount(struct vfsmount *mnt, int flags) · 2763 unmodified lines
2764 FILESYSTEM__UNMOUNT, NULL);2764 FILESYSTEM__UNMOUNT, NULL);
2765}2765}
2766 2766
2767static 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
2767static const struct fs_parameter_spec selinux_param_specs[] = {2805static const struct fs_parameter_spec selinux_param_specs[] = {
2768 fsparam_string(CONTEXT_STR, Opt_context),2806 fsparam_string(CONTEXT_STR, Opt_context),
2769 fsparam_string(DEFCONTEXT_STR, Opt_defcontext),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 LSM_HOOK_INIT(bprm_committing_creds, selinux_bprm_committing_creds),6783 LSM_HOOK_INIT(bprm_committing_creds, selinux_bprm_committing_creds),
6746 LSM_HOOK_INIT(bprm_committed_creds, selinux_bprm_committed_creds),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 LSM_HOOK_INIT(fs_context_parse_param, selinux_fs_context_parse_param),6787 LSM_HOOK_INIT(fs_context_parse_param, selinux_fs_context_parse_param),
6749 6788
6750 LSM_HOOK_INIT(sb_alloc_security, selinux_sb_alloc_security),6789 LSM_HOOK_INIT(sb_alloc_security, selinux_sb_alloc_security),
security/smack/smack_lsm.c+49−0
@@ -647,6 +647,54 @@ out_opt_err: · 646 unmodified lines
647 return -EINVAL;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 */
657static 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
650static const struct fs_parameter_spec smack_param_specs[] = {698static const struct fs_parameter_spec smack_param_specs[] = {
651 fsparam_string("fsdefault", Opt_fsdefault),699 fsparam_string("fsdefault", Opt_fsdefault),
652 fsparam_string("fsfloor", Opt_fsfloor),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 LSM_HOOK_INIT(ptrace_traceme, smack_ptrace_traceme),4674 LSM_HOOK_INIT(ptrace_traceme, smack_ptrace_traceme),
4627 LSM_HOOK_INIT(syslog, smack_syslog),4675 LSM_HOOK_INIT(syslog, smack_syslog),
4628 4676
4677 LSM_HOOK_INIT(fs_context_dup, smack_fs_context_dup),
4629 LSM_HOOK_INIT(fs_context_parse_param, smack_fs_context_parse_param),4678 LSM_HOOK_INIT(fs_context_parse_param, smack_fs_context_parse_param),
4630 4679
4631 LSM_HOOK_INIT(sb_alloc_security, smack_sb_alloc_security),4680 LSM_HOOK_INIT(sb_alloc_security, smack_sb_alloc_security),

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t