LSM: Mount option data management
Subsystems (3)
A message that is not in the archive (sent off-list or not yet received): [email protected]
Casey Schaufler
maintainercover LSM: Mount option data management
Mount option processing does not allow for multiple LSMs to process options. It requires that the LSMs manage the data associated with their options and assumes that one LSM will do all the work. A second LSM using mount options clobbers the data of the first, resulting in mount failures.
In order for multiple LSMs to provide mount options the LSM management of the data needs to change. A mount option blob replaces the LSM specific data that is allocated. This still needs to be allocated within the individual LSMs due to mount options being strictly optional. Duplication and freeing, on the other hand, can be managed in the infrastructure.
SELinux provides a special challenge in that it defers mount option handling in cases where the policy is not loaded. This requires that additonal logic be added to detect if an allocated blob was created by SELinux or by another LSM.
This is a substantial change from previous approaches to the problem, which failed backward compatibility. Hence, it is not a verson 2.
https://github.com/cschaufler/lsm-stacking#mount-262221
Casey Schaufler (1):
LSM: infrastructure management of mount options
include/linux/lsm_hook_defs.h | 2 - include/linux/lsm_hooks.h | 3 ++ security/lsm_init.c | 2 + security/security.c | 29 ++++++++++- security/selinux/hooks.c | 85 +++++++++++++++++------------- security/selinux/include/objsec.h | 8 +++ security/smack/smack.h | 8 +++ security/smack/smack_lsm.c | 86 +++++++++++-------------------- 8 files changed, 129 insertions(+), 94 deletions(-)
Signature · 1 line
2.56.0
Casey Schaufler
maintainerPATCH 1/1 LSM: infrastructure management of mount options
Move the management of LSM specific mount option data out of the individual modules and into the infrastructure to the extent possible. Duplication and freeing of mount option data is handled directly in the infrastructure. Allocation is a bit tricker, as it is sometimes delayed in the SELinux case. A new LSM helper has been added for use within the modules to ensure allocation is done properly.
Tested with both the SELinux and Smack testsuites, Also tested on a Fedora 44 system with a modified systemd and both SELinux and Smack enabled.
--- include/linux/lsm_hook_defs.h | 2 - include/linux/lsm_hooks.h | 3 ++ security/lsm_init.c | 2 + security/security.c | 29 ++++++++++- security/selinux/hooks.c | 85 +++++++++++++++++------------- security/selinux/include/objsec.h | 8 +++ security/smack/smack.h | 8 +++ security/smack/smack_lsm.c | 86 +++++++++++-------------------- 8 files changed, 129 insertions(+), 94 deletions(-)
include/linux/lsm_hook_defs.h +0 -2
@@ -55,8 +55,6 @@ LSM_HOOK(int, 0, bprm_check_security, struct linux_binprm *bprm) LSM_HOOK(void, LSM_RET_VOID, bprm_committing_creds, const struct linux_binprm *bprm) LSM_HOOK(void, LSM_RET_VOID, bprm_committed_creds, const struct linux_binprm *bprm) LSM_HOOK(int, 0, fs_context_submount, struct fs_context *fc, struct super_block *reference) -LSM_HOOK(int, 0, fs_context_dup, struct fs_context *fc, - struct fs_context *src_sc) LSM_HOOK(int, -ENOPARAM, fs_context_parse_param, struct fs_context *fc, struct fs_parameter *param) LSM_HOOK(int, 0, sb_alloc_security, struct super_block *sb)
include/linux/lsm_hooks.h +3 -0
@@ -120,6 +120,7 @@ struct lsm_blob_sizes {unsigned int lbs_bpf_map;unsigned int lbs_bpf_prog;unsigned int lbs_bpf_token; +unsigned int lbs_mnt_opts; };/*
@@ -218,4 +219,6 @@ static inline struct xattr *lsm_get_xattr_slot(struct xattr *xattrs, return &xattrs[(*xattr_count)++]; } +int lsm_mnt_opts_alloc (void **opts); +#endif /* ! __LINUX_LSM_HOOKS_H */
security/lsm_init.c +2 -0
@@ -316,6 +316,7 @@ static void __init lsm_prepare(struct lsm_info *lsm)lsm_blob_size_update (&blobs->lbs_bpf_map, &blob_sizes.lbs_bpf_map);lsm_blob_size_update (&blobs->lbs_bpf_prog, &blob_sizes.lbs_bpf_prog);lsm_blob_size_update (&blobs->lbs_bpf_token, &blob_sizes.lbs_bpf_token); +lsm_blob_size_update (&blobs->lbs_mnt_opts, &blob_sizes.lbs_mnt_opts); }/**
@@ -460,6 +461,7 @@ int __init security_init(void)lsm_pr ("blob(bpf_map) size %d\n" , blob_sizes.lbs_bpf_map);lsm_pr ("blob(bpf_prog) size %d\n" , blob_sizes.lbs_bpf_prog);lsm_pr ("blob(bpf_token) size %d\n" , blob_sizes.lbs_bpf_token); +lsm_pr ("blob(mnt_opts) size %d\n" , blob_sizes.lbs_mnt_opts); }if (blob_sizes.lbs_file)
security/security.c +28 -1
@@ -29,6 +29,7 @@#include <linux/overflow.h> #include <linux/perf_event.h> #include <linux/fs.h> +#include <linux/fs_context.h> #include <net/flow.h> #include <net/sock.h>
@@ -876,11 +877,23 @@ int security_fs_context_submount(struct fs_context *fc, struct super_block *refe* initialised to NULL by the caller. @fc indicates the new filesystem context. * @src_fc indicates the original filesystem context. * +* Do the duplication here rather than leave it to the modules. +* The infrastructure owns the blob. +* * Return: Returns 0 on success or a negative error code on failure. */ int security_fs_context_dup (struct fs_context *fc,struct fs_context *src_fc) { -return call_int_hook (fs_context_dup, fc, src_fc); +const void *src = src_fc->security; + +if (!src) +return 0 ; + + fc->security =kmemdup (src, blob_sizes.lbs_mnt_opts,GFP_KERNEL ); +if (!fc->security) +return -ENOMEM ; + +return 0 ; }/**
@@ -960,6 +973,19 @@ void security_sb_free(struct super_block *sb) sb->s_security =NULL ; } +/** +* lsm_mnt_opts_alloc - allocate a mnt_opts blob +* @opts: pointer to options +* +* Allocate a mount options blob. +* +* Returns 0, or -ENOMEM if memory isn't available. +*/ +int lsm_mnt_opts_alloc (void **opts) +{ +return lsm_blob_alloc (opts, blob_sizes.lbs_mnt_opts,GFP_KERNEL ); +} +/** * security_free_mnt_opts() - Free memory associated with mount options * @mnt_opts: LSM processed mount options
@@ -971,6 +997,7 @@ void security_free_mnt_opts(void **mnt_opts)if (!*mnt_opts)return ;call_void_hook (sb_free_mnt_opts, *mnt_opts); +kfree (*mnt_opts); *mnt_opts =NULL ; }EXPORT_SYMBOL (security_free_mnt_opts);
security/selinux/hooks.c +49 -36
201 lines not on this page · raw message
security/selinux/include/objsec.h +8 -0
@@ -276,6 +276,14 @@ selinux_perf_event(void *perf_event)return perf_event + selinux_blob_sizes.lbs_perf_event; } +static inline struct selinux_mnt_opts *selinux_mnt_opts (void *opts) +{ +if (!opts) +return NULL ; + +return opts + selinux_blob_sizes.lbs_mnt_opts; +} +#ifdef CONFIG_BPF_SYSCALL static inline struct bpf_security_struct *selinux_bpf_map_security (struct bpf_map *map)
security/smack/smack.h +8 -0
@@ -386,6 +386,14 @@ static inline struct socket_smack *smack_sock(const struct sock *sock)return sock->sk_security + smack_blob_sizes.lbs_sock; } +static inline struct smack_mnt_opts *smack_mnt_opts (void *opts) +{ +if (!opts) +return NULL ; + +return opts + smack_blob_sizes.lbs_mnt_opts; +} +#ifdef CONFIG_KEYS static inline struct smack_known **smack_key (const struct key *key) {
security/smack/smack_lsm.c +31 -55
160 lines not on this page · raw message
Signature · 1 line
2.56.0
[email protected]
AI reviewRe: [PATCH 1/1] LSM: infrastructure management of mount options
Thank you for your contribution! Sashiko AI review found 5 potential issues to consider: - [High] selinux: uninitialized opts dereference in selinux_fs_context_submount() - [High] smack: false…
Thank you for your contribution! Sashiko AI review found 5 potential issues to consider:
- [High] selinux: uninitialized opts dereference in selinux_fs_context_submount()
- [High] smack: false -EPERM in smack_set_mnt_opts() when mnt_opts is shared
- [Medium] selinux: missing opts->initialized check in selinux_sb_mnt_opts_compat()
- [Medium] selinux: premature opts->initialized in selinux_add_opt() and stale SIDs
- [Low] smack: smack_add_opt() returns -EINVAL instead of -ENOMEM on NULL string
--
commit 594cf0fb23bd823b404b83bdc2ae2e3211731aa5
Author: Casey Schaufler <[email protected]>
LSM: infrastructure management of mount options
This commit moves LSM mount option blob allocation, duplication, and freeing into the LSM infrastructure using a shared lbs_mnt_opts blob size and a new lsm_mnt_opts_alloc() helper.
Quoted 24 lines
security/selinux/hooks.c +7
-1@@ -381,6 +381,7 @@ static void inode_free_security(struct inode *inode) }struct selinux_mnt_opts { +bool initialized;u32 fscontext_sid;u32 context_sid;u32 rootcontext_sid;@@ -389,7 +390,12 @@ struct selinux_mnt_opts {static void selinux_free_mnt_opts (void *mnt_opts) { -kfree (mnt_opts); +struct selinux_mnt_opts *opts; + +if (mnt_opts) { + opts =selinux_mnt_opts (mnt_opts); + opts->initialized = false; + } }
[ ... ]
Quoted 14 lines
security/selinux/hooks.c +1
-2@@ -1023,7 +1034,7 @@ static int selinux_sb_clone_mnt_opts(const struct super_block *oldsb,*/ static int selinux_add_opt (int token,const char *s,void **mnt_opts) { -struct selinux_mnt_opts *opts = *mnt_opts; +struct selinux_mnt_opts *opts;u32 *dst_sid;int rc;@@ -1038,12 +1049,13 @@ static int selinux_add_opt(int token, const char *s, void **mnt_opts)return -EINVAL ; } -if (!opts) {
security/selinux/hooks.c static int selinux_add_opt(int token, const char *s, void **mnt_opts) · line 1058
Quoted 10 lines
security/selinux/hooks.c +5
-3- opts =kzalloc_obj (*opts); -if (!opts) +if (!*mnt_opts) { + rc =lsm_mnt_opts_alloc (mnt_opts); +if (rc)return -ENOMEM ; - *mnt_opts = opts; } + opts =selinux_mnt_opts (*mnt_opts); + opts->initialized = true;
[Severity: Medium]
Can setting opts->initialized = true before validating the option leave fc->security marked initialized with all SID fields equal to zero when selinux_add_opt() fails?
If the first SELinux option passed via fsconfig(FSCONFIG_SET_STRING) fails in security_context_str_to_sid(), opts->initialized remains true while all four SIDs in *opts are 0. A subsequent mount or superblock comparison against an existing superblock mounted with SELinux options (sbsec->flags & SE_MNTMASK) then sees opts && opts->initialized as true in selinux_sb_mnt_opts_compat() and selinux_set_mnt_opts(), skips every per-SID bad_option() check, and bypasses the SE_MNTMASK double-mount check.
Also, when selinux_sb_eat_lsm_opts() fails after parsing a valid option followed by an invalid one, selinux_free_mnt_opts() clears opts->initialized without zeroing the SID fields in *opts, so a later selinux_add_opt() call on the same context will re-enable stale SIDs.
[ ... ]
Quoted 1 line
security/selinux/hooks.c +0
-0@@ -2703,18 +2715,20 @@ static int selinux_sb_eat_lsm_opts(char *options, void **mnt_opts)
[ ... ]
Quoted 14 lines
static int
selinux_sb_mnt_opts_compat(struct super_block *sb, void *mnt_opts){security/selinux/hooks.c +5
-1-struct selinux_mnt_opts *opts = mnt_opts; +struct selinux_mnt_opts *opts;struct superblock_security_struct *sbsec =selinux_superblock (sb); +if (mnt_opts) + opts =selinux_mnt_opts (mnt_opts); +else + opts =NULL ;/* * Superblock not initialized (i.e. no options) - reject if any * options specified, otherwise accept. */
security/selinux/hooks.c static int selinux_sb_mnt_opts_compat(struct super_block *sb, void *mnt_opts)
Quoted 10 lines
security/selinux/hooks.c +1
-1if (!(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) +if (!opts || !opts->initialized)return (sbsec->flags &SE_MNTMASK ) ?1 :0 ;
[Severity: Medium]
Should the !(sbsec->flags & SE_SBINITIALIZED) check in selinux_sb_mnt_opts_compat() also test opts->initialized?
When another LSM allocates the shared mnt_opts blob (or after selinux_free_mnt_opts() clears opts->initialized), selinux_mnt_opts() still returns a non-NULL offset into the blob even though no SELinux options were specified, causing selinux_sb_mnt_opts_compat() to return 1 for an uninitialized superblock.
[ ... ]
Quoted 20 lines
security/selinux/hooks.c +9
-3@@ -2861,9 +2880,15 @@ static int selinux_fs_context_submount(struct fs_context *fc,if (!(sbsec->flags & (FSCONTEXT_MNT |CONTEXT_MNT |DEFCONTEXT_MNT )))return 0 ; - opts =kzalloc_obj (*opts); -if (!opts) -return -ENOMEM ; +if (!fc->security) { +int rc; + + rc =lsm_mnt_opts_alloc (&fc->security); +if (rc) +return rc; + opts =selinux_mnt_opts (fc->security); + opts->initialized = true; + }if (sbsec->flags &FSCONTEXT_MNT ) opts->fscontext_sid = sbsec->sid;
security/selinux/hooks.c static int selinux_fs_context_submount(struct fs_context *fc, · line 2901
Quoted 7 lines
security/selinux/hooks.c +1
-1@@ -2871,22 +2896,10 @@ static int selinux_fs_context_submount(struct fs_context *fc, opts->context_sid = sbsec->mntpoint_sid;if (sbsec->flags &DEFCONTEXT_MNT ) opts->defcontext_sid = sbsec->def_sid; - fc->security = opts; + opts->initialized = true;return 0 ; }
[Severity: High]
Does selinux_fs_context_submount() dereference opts uninitialized when an earlier LSM has already allocated fc->security?
opts is declared without initialization and is only assigned inside the if (!fc->security) block. When Smack is ordered before SELinux, smack_fs_context_submount() unconditionally allocates fc->security via lsm_mnt_opts_alloc(), so selinux_fs_context_submount() skips the if (!fc->security) block and writes the SID fields and opts->initialized = true through the uninitialized stack pointer opts.
Quoted 26 lines
security/smack/smack_lsm.c +5
-9@@ -560,24 +561,22 @@ struct smack_mnt_opts {const char *fstransmute; }; -static void smack_free_mnt_opts (void *mnt_opts) -{ -kfree (mnt_opts); -} -static int smack_add_opt (int token,const char *s,void **mnt_opts) { -struct smack_mnt_opts *opts = *mnt_opts; +struct smack_mnt_opts *opts;struct smack_known *skp; -if (!opts) { - opts = kzalloc_obj(struct smack_mnt_opts); -if (!opts) +if (!s) +return -EINVAL ; + +if (!*mnt_opts) {
security/smack/smack_lsm.c struct smack_mnt_opts { · line 579
Quoted 10 lines
security/smack/smack_lsm.c +5
-3+int rc; + + rc =lsm_mnt_opts_alloc (mnt_opts); +if (rc)return -ENOMEM ; - *mnt_opts = opts; } -if (!s) -return -ENOMEM ; + opts =smack_mnt_opts (*mnt_opts);
[Severity: Low]
Does changing if (!s) in smack_add_opt() from -ENOMEM to -EINVAL return the wrong error code on allocation failure?
In smack_sb_eat_lsm_opts(), match_opt_prefix() only matches when an '=' is present, and the result of kmemdup_nul() is passed directly to smack_add_opt() without a NULL check, so s is NULL only when kmemdup_nul() runs out of memory.
[ ... ]
Quoted 16 lines
security/smack/smack_lsm.c +1
-5@@ -750,12 +726,8 @@ static int smack_sb_eat_lsm_opts(char *options, void **mnt_opts) arg =kmemdup_nul (arg, from + len - arg,GFP_KERNEL ); rc =smack_add_opt (token, arg, mnt_opts);kfree (arg); -if (unlikely (rc)) { -if (*mnt_opts) -smack_free_mnt_opts (*mnt_opts); - *mnt_opts =NULL ; +if (unlikely (rc))return rc; - } }else {if (!first) {// copy with preceding comma from--;@@ -802,6 +774,11 @@ static int smack_set_mnt_opts(struct super_block *sb,if (sp->smk_flags &SMK_SB_INITIALIZED )
security/smack/smack_lsm.c static int smack_set_mnt_opts(struct super_block *sb, · line 784
Quoted 10 lines
security/smack/smack_lsm.c +5
-0return 0 ; +if (mnt_opts) + opts =smack_mnt_opts (mnt_opts); +else + opts =NULL ; +if (!smack_privileged (CAP_MAC_ADMIN )) {/* * Unprivileged mounts don't get to specify Smack values.
[Severity: High]
Does smack_set_mnt_opts() reject unprivileged mounts with -EPERM whenever another LSM allocates the shared mnt_opts blob?
Immediately below this hunk, smack_set_mnt_opts() checks:
security/smack/smack_lsm.c:smack_set_mnt_opts() {
...
if (!smack_privileged(CAP_MAC_ADMIN)) {
/*
* Unprivileged mounts don't get to specify Smack values.
*/
if (opts)
return -EPERM;
...
}Because smack_mnt_opts(mnt_opts) returns mnt_opts + smack_blob_sizes.lbs_mnt_opts whenever mnt_opts is non-NULL, opts is non-NULL even when Smack has not set any mount options in the blob. Any mount or submount by a task without CAP_MAC_ADMIN where SELinux allocated fc->security (or where an earlier smack_add_opt() call failed after allocating *mnt_opts) will now fail with -EPERM. In addition, if smack_sb_eat_lsm_opts() fails after parsing one Smack option, the already populated fields in *opts are no longer cleared on error.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1