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
337337
338338 static void legacy_fs_context_free(struct fs_context *fc);
339339
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+
340381 /**
341382 * put_fs_context - Dispose of a superblock configuration context.
342383 * @fc: The context to dispose of.
@@ -380,6 +421,31 @@ static void legacy_fs_context_free(struct fs_context *fc) · 37 unmodified lines
380421 }
381422 }
382423
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+
383449 /*
384450 * Add a parameter to a legacy config. We build up a comma-separated list of
385451 * options.
@@ -514,6 +580,7 @@ static int legacy_reconfigure(struct fs_context *fc) · 128 unmodified lines
514580
515581 const struct fs_context_operations legacy_fs_context_ops = {
516582 .free = legacy_fs_context_free,
583+ .dup = legacy_fs_context_dup,
517584 .parse_param = legacy_parse_param,
518585 .parse_monolithic = legacy_parse_monolithic,
519586 .get_tree = legacy_get_tree,
include/linux/fs_context.h+2−0
@@ -94,6 +94,7 @@ struct fs_context { · 93 unmodified lines
9494
9595 struct fs_context_operations {
9696 void (*free)(struct fs_context *fc);
97+ int (*dup)(struct fs_context *fc, struct fs_context *src_fc);
9798 int (*parse_param)(struct fs_context *fc, struct fs_parameter *param);
9899 int (*parse_monolithic)(struct fs_context *fc, void *data);
99100 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
111112 extern struct fs_context *fs_context_for_submount(struct file_system_type *fs_type,
112113 struct dentry *reference);
113114
115+extern struct fs_context *vfs_dup_fs_context(struct fs_context *fc);
114116 extern int vfs_parse_fs_param(struct fs_context *fc, struct fs_parameter *param);
115117 extern int vfs_parse_fs_string(struct fs_context *fc, const char *key,
116118 const char *value, size_t v_size);
include/linux/lsm_hooks.h+7−0
@@ -79,6 +79,11 @@ · 78 unmodified lines
7979 * Security hooks for mount using fs_context.
8080 * [See also Documentation/filesystems/mounting.txt]
8181 *
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.
8287 * @fs_context_parse_param:
8388 * Userspace provided a parameter to configure a superblock. The LSM may
8489 * 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
14701475 void (*bprm_committing_creds)(struct linux_binprm *bprm);
14711476 void (*bprm_committed_creds)(struct linux_binprm *bprm);
14721477
1478+ int (*fs_context_dup)(struct fs_context *fc, struct fs_context *src_sc);
14731479 int (*fs_context_parse_param)(struct fs_context *fc, struct fs_parameter *param);
14741480
14751481 int (*sb_alloc_security)(struct super_block *sb);
@@ -1813,6 +1819,7 @@ struct security_hook_heads { · 337 unmodified lines
18131819 struct hlist_head bprm_check_security;
18141820 struct hlist_head bprm_committing_creds;
18151821 struct hlist_head bprm_committed_creds;
1822+ struct hlist_head fs_context_dup;
18161823 struct hlist_head fs_context_parse_param;
18171824 struct hlist_head sb_alloc_security;
18181825 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
223223 int security_bprm_check(struct linux_binprm *bprm);
224224 void security_bprm_committing_creds(struct linux_binprm *bprm);
225225 void security_bprm_committed_creds(struct linux_binprm *bprm);
226+int security_fs_context_dup(struct fs_context *fc, struct fs_context *src_fc);
226227 int security_fs_context_parse_param(struct fs_context *fc, struct fs_parameter *param);
227228 int security_sb_alloc(struct super_block *sb);
228229 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
521522 {
522523 }
523524
525+static inline int security_fs_context_dup(struct fs_context *fc,
526+ struct fs_context *src_fc)
527+{
528+ return 0;
529+}
524530 static inline int security_fs_context_parse_param(struct fs_context *fc,
525531 struct fs_parameter *param)
526532 {
security/security.c+5−0
@@ -374,6 +374,11 @@ void security_bprm_committed_creds(struct linux_binprm *bprm) · 373 unmodified lines
374374 call_void_hook(bprm_committed_creds, bprm);
375375 }
376376
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+
377382 int security_fs_context_parse_param(struct fs_context *fc, struct fs_parameter *param)
378383 {
379384 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
27642764 FILESYSTEM__UNMOUNT, NULL);
27652765 }
27662766
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+
27672805 static const struct fs_parameter_spec selinux_param_specs[] = {
27682806 fsparam_string(CONTEXT_STR, Opt_context),
27692807 fsparam_string(DEFCONTEXT_STR, Opt_defcontext),
@@ -6745,6 +6783,7 @@ static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = { · 3975 unmodified lines
67456783 LSM_HOOK_INIT(bprm_committing_creds, selinux_bprm_committing_creds),
67466784 LSM_HOOK_INIT(bprm_committed_creds, selinux_bprm_committed_creds),
67476785
6786+ LSM_HOOK_INIT(fs_context_dup, selinux_fs_context_dup),
67486787 LSM_HOOK_INIT(fs_context_parse_param, selinux_fs_context_parse_param),
67496788
67506789 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
647647 return -EINVAL;
648648 }
649649
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+
650698 static const struct fs_parameter_spec smack_param_specs[] = {
651699 fsparam_string("fsdefault", Opt_fsdefault),
652700 fsparam_string("fsfloor", Opt_fsfloor),
@@ -4626,6 +4674,7 @@ static struct security_hook_list smack_hooks[] __lsm_ro_after_init = { · 3973 unmodified lines
46264674 LSM_HOOK_INIT(ptrace_traceme, smack_ptrace_traceme),
46274675 LSM_HOOK_INIT(syslog, smack_syslog),
46284676
4677+ LSM_HOOK_INIT(fs_context_dup, smack_fs_context_dup),
46294678 LSM_HOOK_INIT(fs_context_parse_param, smack_fs_context_parse_param),
46304679
46314680 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