Replace allocations of LSM specific mount data with the
shared mnt_opts blob.

Signed-off-by: Casey Schaufler <ca...@schaufler-ca.com>
---
 include/linux/lsm_hooks.h  |  1 +
 security/security.c        | 12 ++++++++++++
 security/selinux/hooks.c   | 10 +++++++---
 security/smack/smack_lsm.c |  4 ++--
 4 files changed, 22 insertions(+), 5 deletions(-)

diff --git a/include/linux/lsm_hooks.h b/include/linux/lsm_hooks.h
index 2e3b1559714c..38f89762c0df 100644
--- a/include/linux/lsm_hooks.h
+++ b/include/linux/lsm_hooks.h
@@ -220,4 +220,5 @@ static inline struct xattr *lsm_get_xattr_slot(struct xattr 
*xattrs,
        return &xattrs[(*xattr_count)++];
 }
 
+extern void *lsm_mnt_opts_alloc(gfp_t priority);
 #endif /* ! __LINUX_LSM_HOOKS_H */
diff --git a/security/security.c b/security/security.c
index 93d4ac39fe9f..98a80078b2df 100644
--- a/security/security.c
+++ b/security/security.c
@@ -901,6 +901,18 @@ void security_sb_free(struct super_block *sb)
        sb->s_security = NULL;
 }
 
+/**
+ * lsm_mnt_opts_alloc - allocate a mnt_opts blob
+ * @priority: memory allocation priority
+ *
+ * Returns a newly allocated mnt_opts blob or NULL if
+ * memory isn't available.
+ */
+void *lsm_mnt_opts_alloc(gfp_t priority)
+{
+       return kzalloc(blob_sizes.lbs_mnt_opts, priority);
+}
+
 /**
  * security_free_mnt_opts() - Free memory associated with mount options
  * @mnt_opts: LSM processed mount options
diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
index c86b430f34c3..8e0671920e3a 100644
--- a/security/selinux/hooks.c
+++ b/security/selinux/hooks.c
@@ -2809,7 +2809,7 @@ static int selinux_fs_context_submount(struct fs_context 
*fc,
        if (!(sbsec->flags & (FSCONTEXT_MNT|CONTEXT_MNT|DEFCONTEXT_MNT)))
                return 0;
 
-       opts = kzalloc(sizeof(*opts), GFP_KERNEL);
+       opts = lsm_mnt_opts_alloc(GFP_KERNEL);
        if (!opts)
                return -ENOMEM;
 
@@ -2831,8 +2831,12 @@ static int selinux_fs_context_dup(struct fs_context *fc,
        if (!src)
                return 0;
 
-       fc->security = kmemdup(src, sizeof(*src), GFP_KERNEL);
-       return fc->security ? 0 : -ENOMEM;
+       fc->security = lsm_mnt_opts_alloc(GFP_KERNEL);
+       if (!fc->security)
+               return -ENOMEM;
+
+       memcpy(fc->security, src, sizeof(*src));
+       return 0;
 }
 
 static const struct fs_parameter_spec selinux_fs_parameters[] = {
diff --git a/security/smack/smack_lsm.c b/security/smack/smack_lsm.c
index 956dce6b1e97..0cc24b57bb52 100644
--- a/security/smack/smack_lsm.c
+++ b/security/smack/smack_lsm.c
@@ -622,7 +622,7 @@ static int smack_fs_context_submount(struct fs_context *fc,
        struct smack_mnt_opts *ctx;
        struct inode_smack *isp;
 
-       ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
+       ctx = lsm_mnt_opts_alloc(GFP_KERNEL);
        if (!ctx)
                return -ENOMEM;
        fc->security = ctx;
@@ -673,7 +673,7 @@ static int smack_fs_context_dup(struct fs_context *fc,
        if (!src)
                return 0;
 
-       fc->security = kzalloc(sizeof(struct smack_mnt_opts), GFP_KERNEL);
+       fc->security = lsm_mnt_opts_alloc(GFP_KERNEL);
        if (!fc->security)
                return -ENOMEM;
 
-- 
2.47.0


Reply via email to