Skip to content

Commit 69c4a42

Browse files
olgakorn1pcmoore
authored andcommitted
lsm,selinux: add new hook to compare new mount to an existing mount
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>
1 parent 431c3be commit 69c4a42

5 files changed

Lines changed: 78 additions & 0 deletions

File tree

include/linux/lsm_hook_defs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ LSM_HOOK(int, 0, sb_alloc_security, struct super_block *sb)
6262
LSM_HOOK(void, LSM_RET_VOID, sb_free_security, struct super_block *sb)
6363
LSM_HOOK(void, LSM_RET_VOID, sb_free_mnt_opts, void *mnt_opts)
6464
LSM_HOOK(int, 0, sb_eat_lsm_opts, char *orig, void **mnt_opts)
65+
LSM_HOOK(int, 0, sb_mnt_opts_compat, struct super_block *sb, void *mnt_opts)
6566
LSM_HOOK(int, 0, sb_remount, struct super_block *sb, void *mnt_opts)
6667
LSM_HOOK(int, 0, sb_kern_mount, struct super_block *sb)
6768
LSM_HOOK(int, 0, sb_show_options, struct seq_file *m, struct super_block *sb)

include/linux/lsm_hooks.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,12 @@
142142
* @orig the original mount data copied from userspace.
143143
* @copy copied data which will be passed to the security module.
144144
* Returns 0 if the copy was successful.
145+
* @sb_mnt_opts_compat:
146+
* Determine if the new mount options in @mnt_opts are allowed given
147+
* the existing mounted filesystem at @sb.
148+
* @sb superblock being compared
149+
* @mnt_opts new mount options
150+
* Return 0 if options are compatible.
145151
* @sb_remount:
146152
* Extracts security system specific mount options and verifies no changes
147153
* are being made to those options.

include/linux/security.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ int security_sb_alloc(struct super_block *sb);
294294
void security_sb_free(struct super_block *sb);
295295
void security_free_mnt_opts(void **mnt_opts);
296296
int security_sb_eat_lsm_opts(char *options, void **mnt_opts);
297+
int security_sb_mnt_opts_compat(struct super_block *sb, void *mnt_opts);
297298
int security_sb_remount(struct super_block *sb, void *mnt_opts);
298299
int security_sb_kern_mount(struct super_block *sb);
299300
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,
646647
return 0;
647648
}
648649

650+
static inline int security_sb_mnt_opts_compat(struct super_block *sb,
651+
void *mnt_opts)
652+
{
653+
return 0;
654+
}
655+
656+
649657
static inline int security_sb_kern_mount(struct super_block *sb)
650658
{
651659
return 0;

security/security.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -890,6 +890,13 @@ int security_sb_eat_lsm_opts(char *options, void **mnt_opts)
890890
}
891891
EXPORT_SYMBOL(security_sb_eat_lsm_opts);
892892

893+
int security_sb_mnt_opts_compat(struct super_block *sb,
894+
void *mnt_opts)
895+
{
896+
return call_int_hook(sb_mnt_opts_compat, 0, sb, mnt_opts);
897+
}
898+
EXPORT_SYMBOL(security_sb_mnt_opts_compat);
899+
893900
int security_sb_remount(struct super_block *sb,
894901
void *mnt_opts)
895902
{

security/selinux/hooks.c

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2685,6 +2685,61 @@ static int selinux_sb_eat_lsm_opts(char *options, void **mnt_opts)
26852685
return rc;
26862686
}
26872687

2688+
static int selinux_sb_mnt_opts_compat(struct super_block *sb, void *mnt_opts)
2689+
{
2690+
struct selinux_mnt_opts *opts = mnt_opts;
2691+
struct superblock_security_struct *sbsec = sb->s_security;
2692+
u32 sid;
2693+
int rc;
2694+
2695+
/*
2696+
* Superblock not initialized (i.e. no options) - reject if any
2697+
* options specified, otherwise accept.
2698+
*/
2699+
if (!(sbsec->flags & SE_SBINITIALIZED))
2700+
return opts ? 1 : 0;
2701+
2702+
/*
2703+
* Superblock initialized and no options specified - reject if
2704+
* superblock has any options set, otherwise accept.
2705+
*/
2706+
if (!opts)
2707+
return (sbsec->flags & SE_MNTMASK) ? 1 : 0;
2708+
2709+
if (opts->fscontext) {
2710+
rc = parse_sid(sb, opts->fscontext, &sid);
2711+
if (rc)
2712+
return 1;
2713+
if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, sid))
2714+
return 1;
2715+
}
2716+
if (opts->context) {
2717+
rc = parse_sid(sb, opts->context, &sid);
2718+
if (rc)
2719+
return 1;
2720+
if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, sid))
2721+
return 1;
2722+
}
2723+
if (opts->rootcontext) {
2724+
struct inode_security_struct *root_isec;
2725+
2726+
root_isec = backing_inode_security(sb->s_root);
2727+
rc = parse_sid(sb, opts->rootcontext, &sid);
2728+
if (rc)
2729+
return 1;
2730+
if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, sid))
2731+
return 1;
2732+
}
2733+
if (opts->defcontext) {
2734+
rc = parse_sid(sb, opts->defcontext, &sid);
2735+
if (rc)
2736+
return 1;
2737+
if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, sid))
2738+
return 1;
2739+
}
2740+
return 0;
2741+
}
2742+
26882743
static int selinux_sb_remount(struct super_block *sb, void *mnt_opts)
26892744
{
26902745
struct selinux_mnt_opts *opts = mnt_opts;
@@ -7078,6 +7133,7 @@ static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = {
70787133

70797134
LSM_HOOK_INIT(sb_free_security, selinux_sb_free_security),
70807135
LSM_HOOK_INIT(sb_free_mnt_opts, selinux_free_mnt_opts),
7136+
LSM_HOOK_INIT(sb_mnt_opts_compat, selinux_sb_mnt_opts_compat),
70817137
LSM_HOOK_INIT(sb_remount, selinux_sb_remount),
70827138
LSM_HOOK_INIT(sb_kern_mount, selinux_sb_kern_mount),
70837139
LSM_HOOK_INIT(sb_show_options, selinux_sb_show_options),

0 commit comments

Comments
 (0)