lsm,selinux: add new hook to compare new mount to an existing mount
authorOlga Kornievskaia <kolga@netapp.com>
Sat, 27 Feb 2021 03:37:55 +0000 (22:37 -0500)
committerPaul Moore <paul@paul-moore.com>
Mon, 22 Mar 2021 18:53:37 +0000 (14:53 -0400)
Add a new hook that takes an existing super block and a new mount
with new options and determines if new options confict with an
existing mount or not.

A filesystem can use this new hook to determine if it can share
the an existing superblock with a new superblock for the new mount.

Signed-off-by: Olga Kornievskaia <kolga@netapp.com>
Acked-by: Anna Schumaker <Anna.Schumaker@Netapp.com>
[PM: tweak the subject line, fix tab/space problems]
Signed-off-by: Paul Moore <paul@paul-moore.com>
include/linux/lsm_hook_defs.h
include/linux/lsm_hooks.h
include/linux/security.h
security/security.c
security/selinux/hooks.c

index 477a597db0130534b3b1c61b379e84d7e1f24c76..1b61bc5dc21518a23779c4760b1965f3b510aea5 100644 (file)
@@ -62,6 +62,7 @@ LSM_HOOK(int, 0, sb_alloc_security, struct super_block *sb)
 LSM_HOOK(void, LSM_RET_VOID, sb_free_security, struct super_block *sb)
 LSM_HOOK(void, LSM_RET_VOID, sb_free_mnt_opts, void *mnt_opts)
 LSM_HOOK(int, 0, sb_eat_lsm_opts, char *orig, void **mnt_opts)
+LSM_HOOK(int, 0, sb_mnt_opts_compat, struct super_block *sb, void *mnt_opts)
 LSM_HOOK(int, 0, sb_remount, struct super_block *sb, void *mnt_opts)
 LSM_HOOK(int, 0, sb_kern_mount, struct super_block *sb)
 LSM_HOOK(int, 0, sb_show_options, struct seq_file *m, struct super_block *sb)
index fb7f3193753d9486d77e1d5def769b3436cea7b1..97bb36d7e99463d5e89c907320aba5ae33de7aa0 100644 (file)
  *     @orig the original mount data copied from userspace.
  *     @copy copied data which will be passed to the security module.
  *     Returns 0 if the copy was successful.
+ * @sb_mnt_opts_compat:
+ *     Determine if the new mount options in @mnt_opts are allowed given
+ *     the existing mounted filesystem at @sb.
+ *     @sb superblock being compared
+ *     @mnt_opts new mount options
+ *     Return 0 if options are compatible.
  * @sb_remount:
  *     Extracts security system specific mount options and verifies no changes
  *     are being made to those options.
index 8aeebd6646dcfcde1270a59fb8da7e04e2d15379..f1e5833bfedcf9efd114a707676e9d0b86c402f3 100644 (file)
@@ -294,6 +294,7 @@ int security_sb_alloc(struct super_block *sb);
 void security_sb_free(struct super_block *sb);
 void security_free_mnt_opts(void **mnt_opts);
 int security_sb_eat_lsm_opts(char *options, void **mnt_opts);
+int security_sb_mnt_opts_compat(struct super_block *sb, void *mnt_opts);
 int security_sb_remount(struct super_block *sb, void *mnt_opts);
 int security_sb_kern_mount(struct super_block *sb);
 int security_sb_show_options(struct seq_file *m, struct super_block *sb);
@@ -646,6 +647,13 @@ static inline int security_sb_remount(struct super_block *sb,
        return 0;
 }
 
+static inline int security_sb_mnt_opts_compat(struct super_block *sb,
+                                             void *mnt_opts)
+{
+       return 0;
+}
+
+
 static inline int security_sb_kern_mount(struct super_block *sb)
 {
        return 0;
index 5ac96b16f8fab0a76666cec5efc1f1003116e549..a4e7d50c3e393d4767c65e912e4225251f82bfa8 100644 (file)
@@ -890,6 +890,13 @@ int security_sb_eat_lsm_opts(char *options, void **mnt_opts)
 }
 EXPORT_SYMBOL(security_sb_eat_lsm_opts);
 
+int security_sb_mnt_opts_compat(struct super_block *sb,
+                               void *mnt_opts)
+{
+       return call_int_hook(sb_mnt_opts_compat, 0, sb, mnt_opts);
+}
+EXPORT_SYMBOL(security_sb_mnt_opts_compat);
+
 int security_sb_remount(struct super_block *sb,
                        void *mnt_opts)
 {
index eca9fc0ba764607b596fb73247c60c7525a284a2..07ca2ebf979eccfcc0585169c6d53f18ffc94d2c 100644 (file)
@@ -2685,6 +2685,61 @@ free_opt:
        return rc;
 }
 
+static int selinux_sb_mnt_opts_compat(struct super_block *sb, void *mnt_opts)
+{
+       struct selinux_mnt_opts *opts = mnt_opts;
+       struct superblock_security_struct *sbsec = sb->s_security;
+       u32 sid;
+       int rc;
+
+       /*
+        * Superblock not initialized (i.e. no options) - reject if any
+        * options specified, otherwise accept.
+        */
+       if (!(sbsec->flags & SE_SBINITIALIZED))
+               return opts ? 1 : 0;
+
+       /*
+        * Superblock initialized and no options specified - reject if
+        * superblock has any options set, otherwise accept.
+        */
+       if (!opts)
+               return (sbsec->flags & SE_MNTMASK) ? 1 : 0;
+
+       if (opts->fscontext) {
+               rc = parse_sid(sb, opts->fscontext, &sid);
+               if (rc)
+                       return 1;
+               if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, sid))
+                       return 1;
+       }
+       if (opts->context) {
+               rc = parse_sid(sb, opts->context, &sid);
+               if (rc)
+                       return 1;
+               if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, sid))
+                       return 1;
+       }
+       if (opts->rootcontext) {
+               struct inode_security_struct *root_isec;
+
+               root_isec = backing_inode_security(sb->s_root);
+               rc = parse_sid(sb, opts->rootcontext, &sid);
+               if (rc)
+                       return 1;
+               if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, sid))
+                       return 1;
+       }
+       if (opts->defcontext) {
+               rc = parse_sid(sb, opts->defcontext, &sid);
+               if (rc)
+                       return 1;
+               if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, sid))
+                       return 1;
+       }
+       return 0;
+}
+
 static int selinux_sb_remount(struct super_block *sb, void *mnt_opts)
 {
        struct selinux_mnt_opts *opts = mnt_opts;
@@ -7078,6 +7133,7 @@ static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = {
 
        LSM_HOOK_INIT(sb_free_security, selinux_sb_free_security),
        LSM_HOOK_INIT(sb_free_mnt_opts, selinux_free_mnt_opts),
+       LSM_HOOK_INIT(sb_mnt_opts_compat, selinux_sb_mnt_opts_compat),
        LSM_HOOK_INIT(sb_remount, selinux_sb_remount),
        LSM_HOOK_INIT(sb_kern_mount, selinux_sb_kern_mount),
        LSM_HOOK_INIT(sb_show_options, selinux_sb_show_options),