selinux: implement namespace_init and namespace_install hooks
RFC PATCH v3 1/1 selinux: implement namespace_init and namespace_install hooks
From: Daniel Durning <[email protected]>
Add implementations of the namespace_init and namespace_install hooks for SELinux. Corresponding permissions are defined for each hook (create and setns, respectively). Additionally, mount namespaces have a create_anon permission to control anonymous mount namespace creation separately. New security classes are defined for each individual namespace type to allow granularity.
In namespace_init we check the caller's SID against itself. When a namespace is created the SID of the caller is saved in the namespace security blob. This SID is checked against the caller in namespace_install. We fall back to SECINITSID_KERNEL if ns_common is pointing to an init namespace, since these are created before policy load and will be lacking a security blob.
Requires Christian Brauner's revised namespace LSM blob patch and Mickaël Salaün's v2 LSM namespace audit data patch, which are the first 2 patches in the series linked below.
---
Changes in v2:
- Added the create_anon permission to separately control creation of anonymous mount namespaces
Changes in v3:
- Rebased onto v2 of the LSM namespace blob/audit data patches
--- security/selinux/hooks.c | 69 +++++++++++++++++++++++++++++ security/selinux/include/classmap.h | 12 ++++- security/selinux/include/objsec.h | 10 +++++ 3 files changed, 90 insertions(+), 1 deletion(-)
security/selinux/hooks.c +69 -0
@@ -94,6 +94,8 @@#include <linux/io_uring/cmd.h> #include <uapi/linux/lsm.h> #include <linux/memfd.h> +#include <linux/ns_common.h> +#include <linux/nsfs.h> #include "initcalls.h" #include "avc.h"
@@ -1327,6 +1329,31 @@ static inline u16 socket_type_to_security_class(int family, int type, int protocreturn SECCLASS_SOCKET ; } +static inline u16 ns_type_to_security_class (struct ns_common *ns) +{ +switch (ns->ns_type) { +case CLONE_NEWCGROUP : +return SECCLASS_CGROUP_NAMESPACE ; +case CLONE_NEWIPC : +return SECCLASS_IPC_NAMESPACE ; +case CLONE_NEWNS : +return SECCLASS_MNT_NAMESPACE ; +case CLONE_NEWNET : +return SECCLASS_NET_NAMESPACE ; +case CLONE_NEWPID : +return SECCLASS_PID_NAMESPACE ; +case CLONE_NEWTIME : +return SECCLASS_TIME_NAMESPACE ; +case CLONE_NEWUSER : +return SECCLASS_USER_NAMESPACE ; +case CLONE_NEWUTS : +return SECCLASS_UTS_NAMESPACE ; +default : +WARN_ON (1 ); +return SECCLASS_NAMESPACE ; + } +} + static int selinux_genfs_get_sid(struct dentry *dentry, u16 tclass, u16 flags,
@@ -7010,6 +7037,45 @@ static int selinux_inode_getsecctx(struct inode *inode, struct lsm_context *cp) cp->id =LSM_ID_SELINUX ;return 0 ; } + +static int selinux_namespace_init (struct ns_common *ns) +{ +struct common_audit_data ad; +struct ns_security_struct *nssec =selinux_ns (ns); +u16 sclass =ns_type_to_security_class (ns); +u32 sid =current_sid (); + + ad.type =LSM_AUDIT_DATA_NS ; + ad.u.ns.ns_type = ns->ns_type; + ad.u.ns.ns_id = ns->ns_id; + + nssec->sid = sid; +if (ns->inum ==MNT_NS_ANON_INO ) +return avc_has_perm (sid, sid, sclass, +MNT_NAMESPACE__CREATE_ANON , &ad); + +return avc_has_perm (sid, sid, sclass,NAMESPACE__CREATE , &ad); +} + +static int selinux_namespace_install (const struct nsset *nsset, +struct ns_common *ns) +{ +struct common_audit_data ad; +struct ns_security_struct *nssec =selinux_ns (ns); +u16 sclass =ns_type_to_security_class (ns); +u32 sid =current_sid (); + + ad.type =LSM_AUDIT_DATA_NS ; + ad.u.ns.ns_type = ns->ns_type; + ad.u.ns.ns_id = ns->ns_id; + +if (!nssec) +return avc_has_perm (sid,SECINITSID_KERNEL , sclass, +NAMESPACE__SETNS , &ad); + +return avc_has_perm (sid, nssec->sid, sclass,NAMESPACE__SETNS , &ad); +} +#ifdef CONFIG_KEYS static int selinux_key_alloc(struct key *k, const struct cred *cred,
@@ -7521,6 +7587,7 @@ struct lsm_blob_sizes selinux_blob_sizes __ro_after_init = { .lbs_bpf_map = sizeof(struct bpf_security_struct), .lbs_bpf_prog = sizeof(struct bpf_security_struct), .lbs_bpf_token = sizeof(struct bpf_security_struct), + .lbs_ns = sizeof(struct ns_security_struct), };/*
@@ -7724,6 +7791,7 @@ static struct security_hook_list selinux_hooks[] __ro_after_init = {LSM_HOOK_INIT (tun_dev_attach_queue, selinux_tun_dev_attach_queue),LSM_HOOK_INIT (tun_dev_attach, selinux_tun_dev_attach),LSM_HOOK_INIT (tun_dev_open, selinux_tun_dev_open), +LSM_HOOK_INIT (namespace_install, selinux_namespace_install), #ifdef CONFIG_SECURITY_INFINIBANDLSM_HOOK_INIT (ib_pkey_access, selinux_ib_pkey_access), LSM_HOOK_INIT(ib_endport_manage_subnet,
@@ -7799,6 +7867,7 @@ static struct security_hook_list selinux_hooks[] __ro_after_init = {LSM_HOOK_INIT (inode_getsecctx, selinux_inode_getsecctx),LSM_HOOK_INIT (sk_alloc_security, selinux_sk_alloc_security),LSM_HOOK_INIT (tun_dev_alloc_security, selinux_tun_dev_alloc_security), +LSM_HOOK_INIT (namespace_init, selinux_namespace_init), #ifdef CONFIG_SECURITY_INFINIBAND LSM_HOOK_INIT(ib_alloc_security, selinux_ib_alloc_security), #endif
security/selinux/include/classmap.h +11 -1
@@ -33,6 +33,8 @@"mac_override" ,"mac_admin" ,"syslog" ,"wake_alarm" ,"block_suspend" , \"audit_read" ,"perfmon" ,"bpf" ,"checkpoint_restore" +#define COMMON_NAMESPACE_PERMS "create", "setns" + #ifdef __KERNEL__ /* avoid this check when building host programs */ #include <linux/capability.h>
@@ -178,9 +180,17 @@ const struct security_class_mapping secclass_map[] = { { "open", "cpu", "kernel", "tracepoint", "read", "write", NULL } }, { "anon_inode", { COMMON_FILE_PERMS, NULL } }, { "io_uring", { "override_creds", "sqpoll", "cmd", "allowed", NULL } }, - { "user_namespace", { "create", NULL } }, {"memfd_file" , { COMMON_FILE_PERMS, "execute_no_trans", "entrypoint", NULL } }, + { "cgroup_namespace", { COMMON_NAMESPACE_PERMS, NULL } }, + { "ipc_namespace", { COMMON_NAMESPACE_PERMS, NULL } }, + { "mnt_namespace", { COMMON_NAMESPACE_PERMS, "create_anon", NULL } }, + { "net_namespace", { COMMON_NAMESPACE_PERMS, NULL } }, + { "pid_namespace", { COMMON_NAMESPACE_PERMS, NULL } }, + { "time_namespace", { COMMON_NAMESPACE_PERMS, NULL } }, + { "user_namespace", { COMMON_NAMESPACE_PERMS, NULL } }, + { "uts_namespace", { COMMON_NAMESPACE_PERMS, NULL } }, + { "namespace", { COMMON_NAMESPACE_PERMS, NULL } }, /* last one */ { NULL, {} } };
security/selinux/include/objsec.h +10 -0
@@ -27,6 +27,7 @@#include <linux/msg.h> #include <net/net_namespace.h> #include <linux/bpf.h> +#include <linux/ns_common.h> #include "flask.h" #include "avc.h"
@@ -182,6 +183,10 @@ struct perf_event_security_struct {u32 sid;/* SID of perf_event obj creator */ }; +struct ns_security_struct { +u32 sid;/* SID of ns obj creator */ +}; +extern struct lsm_blob_sizes selinux_blob_sizes;static inline struct cred_security_struct *selinux_cred (const struct cred *cred) {
@@ -270,6 +275,11 @@ selinux_perf_event(void *perf_event)return perf_event + selinux_blob_sizes.lbs_perf_event; } +static inline struct ns_security_struct *selinux_ns (struct ns_common *ns) +{ +return ns->ns_security + selinux_blob_sizes.lbs_ns; +} +#ifdef CONFIG_BPF_SYSCALL static inline struct bpf_security_struct *selinux_bpf_map_security (struct bpf_map *map)
Signature · 1 line
2.53.0
Paul Moore
maintainerQuoted 26 lines · On May 28, 2026 [email protected] wrote:
Add implementations of the
namespace_initandnamespace_installhooks for SELinux. Corresponding permissions are defined for each hook (create and setns, respectively). Additionally, mount namespaces have acreate_anonpermission to control anonymous mount namespace creation separately. New security classes are defined for each individual namespace type to allow granularity.In
namespace_initwe check the caller's SID against itself. When a namespace is created the SID of the caller is saved in the namespace security blob. This SID is checked against the caller innamespace_install. We fall back toSECINITSID_KERNELifns_commonis pointing to an init namespace, since these are created before policy load and will be lacking a security blob.Requires Christian Brauner's revised namespace LSM blob patch and Mickaël Salaün's v2 LSM namespace audit data patch, which are the first 2 patches in the series linked below.
Signed-off-by Daniel Durning <[email protected]>---
Changes in v2:
- Added thecreate_anonpermission to separately control creation of anonymous mount namespaces
Quoted 7 lines
Changes in v3:
- Rebased onto v2 of the LSM namespace blob/audit data patches--- security/selinux/hooks.c | 69 +++++++++++++++++++++++++++++ security/selinux/include/classmap.h | 12 ++++- security/selinux/include/objsec.h | 10 +++++ 3 files changed, 90 insertions(+), 1 deletion(-)
This looks good to me with the exception of one thing (below).
Quoted 33 lines
security/selinux/hooks.c +25
-0@@ -7010,6 +7037,45 @@ static int selinux_inode_getsecctx(struct inode *inode, struct lsm_context *cp) cp->id =LSM_ID_SELINUX ;return 0 ; } + +static int selinux_namespace_init (struct ns_common *ns) +{ +struct common_audit_data ad; +struct ns_security_struct *nssec =selinux_ns (ns); +u16 sclass =ns_type_to_security_class (ns); +u32 sid =current_sid (); + + ad.type =LSM_AUDIT_DATA_NS ; + ad.u.ns.ns_type = ns->ns_type; + ad.u.ns.ns_id = ns->ns_id; + + nssec->sid = sid; +if (ns->inum ==MNT_NS_ANON_INO ) +return avc_has_perm (sid, sid, sclass, +MNT_NAMESPACE__CREATE_ANON , &ad); + +return avc_has_perm (sid, sid, sclass,NAMESPACE__CREATE , &ad); +} + +static int selinux_namespace_install (const struct nsset *nsset, +struct ns_common *ns) +{ +struct common_audit_data ad; +struct ns_security_struct *nssec =selinux_ns (ns);
security/selinux/hooks.c static int selinux_inode_getsecctx(struct inode *inode, struct lsm_context *cp) · line 7074
Quoted 10 lines
security/selinux/hooks.c +10
-0+u16 sclass =ns_type_to_security_class (ns); +u32 sid =current_sid (); + + ad.type =LSM_AUDIT_DATA_NS ; + ad.u.ns.ns_type = ns->ns_type; + ad.u.ns.ns_id = ns->ns_id; + +if (!nssec) +return avc_has_perm (sid,SECINITSID_KERNEL , sclass, +NAMESPACE__SETNS , &ad);
In which cases do we expect to see "nssec" equal to NULL? If this is a possibility, shouldn't we be checking for the "ns" parameter to be NULL and not "nssec"? I'm asking because selinux_ns() shouldn't ever return NULL, and if the "ns" parameter has a chance of being NULL then we risk a crash from the dereference in selinux_ns().
Quoted 3 lines
security/selinux/hooks.c +3
-0+return avc_has_perm (sid, nssec->sid, sclass,NAMESPACE__SETNS , &ad); +} +
...
Quoted 37 lines
security/selinux/include/objsec.h +10
-0@@ -27,6 +27,7 @@#include <linux/msg.h> #include <net/net_namespace.h> #include <linux/bpf.h> +#include <linux/ns_common.h> #include "flask.h" #include "avc.h" @@ -182,6 +183,10 @@ struct perf_event_security_struct {u32 sid;/* SID of perf_event obj creator */ }; +struct ns_security_struct { +u32 sid;/* SID of ns obj creator */ +}; +extern struct lsm_blob_sizes selinux_blob_sizes;static inline struct cred_security_struct *selinux_cred (const struct cred *cred) {@@ -270,6 +275,11 @@ selinux_perf_event(void *perf_event)return perf_event + selinux_blob_sizes.lbs_perf_event; } +static inline struct ns_security_struct *selinux_ns (struct ns_common *ns) +{ +return ns->ns_security + selinux_blob_sizes.lbs_ns; +} +#ifdef CONFIG_BPF_SYSCALL static inline struct bpf_security_struct *selinux_bpf_map_security (struct bpf_map *map)Signature · 1 line
2.53.0
--
paul-moore.com
Stephen Smalley
maintainerQuoted 79 lines · Paul Moore
Quoted 26 lines · On May 28, 2026 [email protected] wrote:
Add implementations of the
namespace_initandnamespace_installhooks for SELinux. Corresponding permissions are defined for each hook (create and setns, respectively). Additionally, mount namespaces have acreate_anonpermission to control anonymous mount namespace creation separately. New security classes are defined for each individual namespace type to allow granularity.In
namespace_initwe check the caller's SID against itself. When a namespace is created the SID of the caller is saved in the namespace security blob. This SID is checked against the caller innamespace_install. We fall back toSECINITSID_KERNELifns_commonis pointing to an init namespace, since these are created before policy load and will be lacking a security blob.Requires Christian Brauner's revised namespace LSM blob patch and Mickaël Salaün's v2 LSM namespace audit data patch, which are the first 2 patches in the series linked below.
Signed-off-by Daniel Durning <[email protected]>---
Changes in v2:
- Added thecreate_anonpermission to separately control creation of anonymous mount namespacesQuoted 7 lines
Changes in v3:
- Rebased onto v2 of the LSM namespace blob/audit data patches--- security/selinux/hooks.c | 69 +++++++++++++++++++++++++++++ security/selinux/include/classmap.h | 12 ++++- security/selinux/include/objsec.h | 10 +++++ 3 files changed, 90 insertions(+), 1 deletion(-)This looks good to me with the exception of one thing (below).
Quoted 41 lines
security/selinux/hooks.c +33
-0@@ -7010,6 +7037,45 @@ static int selinux_inode_getsecctx(struct inode *inode, struct lsm_context *cp) cp->id =LSM_ID_SELINUX ;return 0 ; } + +static int selinux_namespace_init (struct ns_common *ns) +{ +struct common_audit_data ad; +struct ns_security_struct *nssec =selinux_ns (ns); +u16 sclass =ns_type_to_security_class (ns); +u32 sid =current_sid (); + + ad.type =LSM_AUDIT_DATA_NS ; + ad.u.ns.ns_type = ns->ns_type; + ad.u.ns.ns_id = ns->ns_id; + + nssec->sid = sid; +if (ns->inum ==MNT_NS_ANON_INO ) +return avc_has_perm (sid, sid, sclass, +MNT_NAMESPACE__CREATE_ANON , &ad); + +return avc_has_perm (sid, sid, sclass,NAMESPACE__CREATE , &ad); +} + +static int selinux_namespace_install (const struct nsset *nsset, +struct ns_common *ns) +{ +struct common_audit_data ad; +struct ns_security_struct *nssec =selinux_ns (ns); +u16 sclass =ns_type_to_security_class (ns); +u32 sid =current_sid (); + + ad.type =LSM_AUDIT_DATA_NS ; + ad.u.ns.ns_type = ns->ns_type; + ad.u.ns.ns_id = ns->ns_id; + +if (!nssec)
Quoted 8 lines
security/selinux/hooks.c static int selinux_inode_getsecctx(struct inode *inode, struct lsm_context *cp) · line 7074
Quoted 2 lines
security/selinux/hooks.c +2
-0+return avc_has_perm (sid,SECINITSID_KERNEL , sclass, +NAMESPACE__SETNS , &ad);In which cases do we expect to see "nssec" equal to NULL? If this is a possibility, shouldn't we be checking for the "ns" parameter to be NULL and not "nssec"? I'm asking because
selinux_ns()shouldn't ever return NULL, and if the "ns" parameter has a chance of being NULL then we risk a crash from the dereference inselinux_ns().
From the patch description:
"We fall back to SECINITSID_KERNEL if ns_common is pointing to an init namespace, since these are created before policy load and will be lacking a security blob."
To be more precise, those init namespaces are created before LSM/SELinux has initialized IIUC.
Stephen Smalley
maintainerQuoted 90 lines · Stephen Smalley
Quoted 79 lines · Paul Moore
Quoted 26 lines · On May 28, 2026 [email protected] wrote:
Add implementations of the
namespace_initandnamespace_installhooks for SELinux. Corresponding permissions are defined for each hook (create and setns, respectively). Additionally, mount namespaces have acreate_anonpermission to control anonymous mount namespace creation separately. New security classes are defined for each individual namespace type to allow granularity.In
namespace_initwe check the caller's SID against itself. When a namespace is created the SID of the caller is saved in the namespace security blob. This SID is checked against the caller innamespace_install. We fall back toSECINITSID_KERNELifns_commonis pointing to an init namespace, since these are created before policy load and will be lacking a security blob.Requires Christian Brauner's revised namespace LSM blob patch and Mickaël Salaün's v2 LSM namespace audit data patch, which are the first 2 patches in the series linked below.
Signed-off-by Daniel Durning <[email protected]>---
Changes in v2:
- Added thecreate_anonpermission to separately control creation of anonymous mount namespacesQuoted 7 lines
Changes in v3:
- Rebased onto v2 of the LSM namespace blob/audit data patches--- security/selinux/hooks.c | 69 +++++++++++++++++++++++++++++ security/selinux/include/classmap.h | 12 ++++- security/selinux/include/objsec.h | 10 +++++ 3 files changed, 90 insertions(+), 1 deletion(-)This looks good to me with the exception of one thing (below).
Quoted 41 lines
security/selinux/hooks.c +33
-0@@ -7010,6 +7037,45 @@ static int selinux_inode_getsecctx(struct inode *inode, struct lsm_context *cp) cp->id =LSM_ID_SELINUX ;return 0 ; } + +static int selinux_namespace_init (struct ns_common *ns) +{ +struct common_audit_data ad; +struct ns_security_struct *nssec =selinux_ns (ns); +u16 sclass =ns_type_to_security_class (ns); +u32 sid =current_sid (); + + ad.type =LSM_AUDIT_DATA_NS ; + ad.u.ns.ns_type = ns->ns_type; + ad.u.ns.ns_id = ns->ns_id; + + nssec->sid = sid; +if (ns->inum ==MNT_NS_ANON_INO ) +return avc_has_perm (sid, sid, sclass, +MNT_NAMESPACE__CREATE_ANON , &ad); + +return avc_has_perm (sid, sid, sclass,NAMESPACE__CREATE , &ad); +} + +static int selinux_namespace_install (const struct nsset *nsset, +struct ns_common *ns) +{ +struct common_audit_data ad; +struct ns_security_struct *nssec =selinux_ns (ns); +u16 sclass =ns_type_to_security_class (ns); +u32 sid =current_sid (); + + ad.type =LSM_AUDIT_DATA_NS ; + ad.u.ns.ns_type = ns->ns_type; + ad.u.ns.ns_id = ns->ns_id; + +if (!nssec)Quoted 8 lines
security/selinux/hooks.c static int selinux_inode_getsecctx(struct inode *inode, struct lsm_context *cp) · line 7074
Quoted 2 lines
security/selinux/hooks.c +2
-0+return avc_has_perm (sid,SECINITSID_KERNEL , sclass, +NAMESPACE__SETNS , &ad);In which cases do we expect to see "nssec" equal to NULL? If this is a possibility, shouldn't we be checking for the "ns" parameter to be NULL and not "nssec"? I'm asking because
selinux_ns()shouldn't ever return NULL, and if the "ns" parameter has a chance of being NULL then we risk a crash from the dereference inselinux_ns().
Quoted 7 lines
From the patch description:
"We fall back toSECINITSID_KERNELifns_commonis pointing to an init namespace, since these are created before policy load and will be lacking a security blob."To be more precise, those init namespaces are created before LSM/SELinux has initialized IIUC.
If we wanted to fix that, we'd need the patch that introduces the LSM hooks for namespaces to also update security_init() to alloc the nssec for the initial namespaces, maybe?
Paul Moore
maintainerQuoted 91 lines · Stephen Smalley
Quoted 89 lines · Stephen Smalley
Quoted 87 lines · Paul Moore
Quoted 26 lines · On May 28, 2026 [email protected] wrote:
Add implementations of the
namespace_initandnamespace_installhooks for SELinux. Corresponding permissions are defined for each hook (create and setns, respectively). Additionally, mount namespaces have acreate_anonpermission to control anonymous mount namespace creation separately. New security classes are defined for each individual namespace type to allow granularity.In
namespace_initwe check the caller's SID against itself. When a namespace is created the SID of the caller is saved in the namespace security blob. This SID is checked against the caller innamespace_install. We fall back toSECINITSID_KERNELifns_commonis pointing to an init namespace, since these are created before policy load and will be lacking a security blob.Requires Christian Brauner's revised namespace LSM blob patch and Mickaël Salaün's v2 LSM namespace audit data patch, which are the first 2 patches in the series linked below.
Signed-off-by Daniel Durning <[email protected]>---
Changes in v2:
- Added thecreate_anonpermission to separately control creation of anonymous mount namespacesQuoted 7 lines
Changes in v3:
- Rebased onto v2 of the LSM namespace blob/audit data patches--- security/selinux/hooks.c | 69 +++++++++++++++++++++++++++++ security/selinux/include/classmap.h | 12 ++++- security/selinux/include/objsec.h | 10 +++++ 3 files changed, 90 insertions(+), 1 deletion(-)This looks good to me with the exception of one thing (below).
Quoted 33 lines
security/selinux/hooks.c +25
-0@@ -7010,6 +7037,45 @@ static int selinux_inode_getsecctx(struct inode *inode, struct lsm_context *cp) cp->id =LSM_ID_SELINUX ;return 0 ; } + +static int selinux_namespace_init (struct ns_common *ns) +{ +struct common_audit_data ad; +struct ns_security_struct *nssec =selinux_ns (ns); +u16 sclass =ns_type_to_security_class (ns); +u32 sid =current_sid (); + + ad.type =LSM_AUDIT_DATA_NS ; + ad.u.ns.ns_type = ns->ns_type; + ad.u.ns.ns_id = ns->ns_id; + + nssec->sid = sid; +if (ns->inum ==MNT_NS_ANON_INO ) +return avc_has_perm (sid, sid, sclass, +MNT_NAMESPACE__CREATE_ANON , &ad); + +return avc_has_perm (sid, sid, sclass,NAMESPACE__CREATE , &ad); +} + +static int selinux_namespace_install (const struct nsset *nsset, +struct ns_common *ns) +{ +struct common_audit_data ad; +struct ns_security_struct *nssec =selinux_ns (ns);security/selinux/hooks.c static int selinux_inode_getsecctx(struct inode *inode, struct lsm_context *cp) · line 7074
Quoted 10 lines
security/selinux/hooks.c +10
-0+u16 sclass =ns_type_to_security_class (ns); +u32 sid =current_sid (); + + ad.type =LSM_AUDIT_DATA_NS ; + ad.u.ns.ns_type = ns->ns_type; + ad.u.ns.ns_id = ns->ns_id; + +if (!nssec) +return avc_has_perm (sid,SECINITSID_KERNEL , sclass, +NAMESPACE__SETNS , &ad);In which cases do we expect to see "nssec" equal to NULL? If this is a possibility, shouldn't we be checking for the "ns" parameter to be NULL and not "nssec"? I'm asking because
selinux_ns()shouldn't ever return NULL, and if the "ns" parameter has a chance of being NULL then we risk a crash from the dereference inselinux_ns().
Quoted 7 lines
Quoted 7 lines
From the patch description:
"We fall back toSECINITSID_KERNELifns_commonis pointing to an init namespace, since these are created before policy load and will be lacking a security blob."To be more precise, those init namespaces are created before LSM/SELinux has initialized IIUC.
Forget the description for a moment and look at the code. Perhaps I'm just burned out and missing something obvious, but I'm not seeing a path where "nssec" is ever NULL (without some dumb luck).
With "nssec" being defined by the following line:
nssec = selinux_ns(ns);
... and selinux_ns() being a fairly simple function:
ns_security_struct *selinux_ns(ns_common *ns)
{
return ns->ns_security + selinux_blob_sizes.lbs_ns;
}... if the "ns" parameter passed to selinux_namespace_install() is NULL, the kernel will crash out in selinux_ns() due to the bogus dereference. If "ns->ns_security" is NULL then the return/"nssec" value is going to be non-NULL but very wrong. The only way I can see nssec being NULL is the dumb luck case where "ns->ns_security" is NULL and the "selinux_blob_sizes.lbs_ns" offset happens to also be zero; here "nssec" will be NULL, but we can't rely on that.
Quoted 3 lines
If we wanted to fix that, we'd need the patch that introduces the LSM hooks for namespaces to also update
security_init()to alloc the nssec for the initial namespaces, maybe?
Somewhat independent of my concern above, but yes, that's probably a good idea regardless. Assuming that the namespaces are properly setup before the call to security_init(), call lsm_ns_alloc() for each namespace in security_init() and then each LSM can initialize init's namespace blobs as necessary in their own init function, e.g. selinux_init().
Signature · 1 line
paul-moore.com
Stephen Smalley
maintainerQuoted 117 lines · Paul Moore
Quoted 91 lines · Stephen Smalley
Quoted 89 lines · Stephen Smalley
Quoted 87 lines · Paul Moore
Quoted 26 lines · On May 28, 2026 [email protected] wrote:
Add implementations of the
namespace_initandnamespace_installhooks for SELinux. Corresponding permissions are defined for each hook (create and setns, respectively). Additionally, mount namespaces have acreate_anonpermission to control anonymous mount namespace creation separately. New security classes are defined for each individual namespace type to allow granularity.In
namespace_initwe check the caller's SID against itself. When a namespace is created the SID of the caller is saved in the namespace security blob. This SID is checked against the caller innamespace_install. We fall back toSECINITSID_KERNELifns_commonis pointing to an init namespace, since these are created before policy load and will be lacking a security blob.Requires Christian Brauner's revised namespace LSM blob patch and Mickaël Salaün's v2 LSM namespace audit data patch, which are the first 2 patches in the series linked below.
Signed-off-by Daniel Durning <[email protected]>---
Changes in v2:
- Added thecreate_anonpermission to separately control creation of anonymous mount namespacesQuoted 7 lines
Changes in v3:
- Rebased onto v2 of the LSM namespace blob/audit data patches--- security/selinux/hooks.c | 69 +++++++++++++++++++++++++++++ security/selinux/include/classmap.h | 12 ++++- security/selinux/include/objsec.h | 10 +++++ 3 files changed, 90 insertions(+), 1 deletion(-)This looks good to me with the exception of one thing (below).
Quoted 33 lines
security/selinux/hooks.c +25
-0@@ -7010,6 +7037,45 @@ static int selinux_inode_getsecctx(struct inode *inode, struct lsm_context *cp) cp->id =LSM_ID_SELINUX ;return 0 ; } + +static int selinux_namespace_init (struct ns_common *ns) +{ +struct common_audit_data ad; +struct ns_security_struct *nssec =selinux_ns (ns); +u16 sclass =ns_type_to_security_class (ns); +u32 sid =current_sid (); + + ad.type =LSM_AUDIT_DATA_NS ; + ad.u.ns.ns_type = ns->ns_type; + ad.u.ns.ns_id = ns->ns_id; + + nssec->sid = sid; +if (ns->inum ==MNT_NS_ANON_INO ) +return avc_has_perm (sid, sid, sclass, +MNT_NAMESPACE__CREATE_ANON , &ad); + +return avc_has_perm (sid, sid, sclass,NAMESPACE__CREATE , &ad); +} + +static int selinux_namespace_install (const struct nsset *nsset, +struct ns_common *ns) +{ +struct common_audit_data ad; +struct ns_security_struct *nssec =selinux_ns (ns);security/selinux/hooks.c static int selinux_inode_getsecctx(struct inode *inode, struct lsm_context *cp) · line 7074
Quoted 10 lines
security/selinux/hooks.c +10
-0+u16 sclass =ns_type_to_security_class (ns); +u32 sid =current_sid (); + + ad.type =LSM_AUDIT_DATA_NS ; + ad.u.ns.ns_type = ns->ns_type; + ad.u.ns.ns_id = ns->ns_id; + +if (!nssec) +return avc_has_perm (sid,SECINITSID_KERNEL , sclass, +NAMESPACE__SETNS , &ad);In which cases do we expect to see "nssec" equal to NULL? If this is a possibility, shouldn't we be checking for the "ns" parameter to be NULL and not "nssec"? I'm asking because
selinux_ns()shouldn't ever return NULL, and if the "ns" parameter has a chance of being NULL then we risk a crash from the dereference inselinux_ns().Quoted 7 lines
Quoted 7 lines
From the patch description:
"We fall back toSECINITSID_KERNELifns_commonis pointing to an init namespace, since these are created before policy load and will be lacking a security blob."To be more precise, those init namespaces are created before LSM/SELinux has initialized IIUC.
Forget the description for a moment and look at the code. Perhaps I'm just burned out and missing something obvious, but I'm not seeing a path where "nssec" is ever NULL (without some dumb luck).
With "nssec" being defined by the following line:
nssec = selinux_ns(ns);... and
selinux_ns()being a fairly simple function:ns_security_struct *selinux_ns(ns_common *ns) { return ns->ns_security + selinux_blob_sizes.lbs_ns; }
Quoted 7 lines
... if the "ns" parameter passed to
selinux_namespace_install()is NULL, the kernel will crash out inselinux_ns()due to the bogus dereference. If "ns->ns_security" is NULL then the return/"nssec" value is going to be non-NULL but very wrong. The only way I can see nssec being NULL is the dumb luck case where "ns->ns_security" is NULL and the "selinux_blob_sizes.lbs_ns" offset happens to also be zero; here "nssec" will be NULL, but we can't rely on that.
I think that would be the default at the moment so likely what he was testing. But yes, agreed - we can't rely on that.
Quoted 10 lines
Quoted 3 lines
If we wanted to fix that, we'd need the patch that introduces the LSM hooks for namespaces to also update
security_init()to alloc the nssec for the initial namespaces, maybe?Somewhat independent of my concern above, but yes, that's probably a good idea regardless. Assuming that the namespaces are properly setup before the call to
security_init(), calllsm_ns_alloc()for each namespace insecurity_init()and then each LSM can initialize init's namespace blobs as necessary in their own init function, e.g.selinux_init().
Ok, I've lost the plot on that patch - where is it at this stage?
Paul Moore
maintainerQuoted 132 lines · Stephen Smalley
Quoted 116 lines · Paul Moore
Quoted 91 lines · Stephen Smalley
Quoted 89 lines · Stephen Smalley
Quoted 87 lines · Paul Moore
Quoted 26 lines · On May 28, 2026 [email protected] wrote:
Add implementations of the
namespace_initandnamespace_installhooks for SELinux. Corresponding permissions are defined for each hook (create and setns, respectively). Additionally, mount namespaces have acreate_anonpermission to control anonymous mount namespace creation separately. New security classes are defined for each individual namespace type to allow granularity.In
namespace_initwe check the caller's SID against itself. When a namespace is created the SID of the caller is saved in the namespace security blob. This SID is checked against the caller innamespace_install. We fall back toSECINITSID_KERNELifns_commonis pointing to an init namespace, since these are created before policy load and will be lacking a security blob.Requires Christian Brauner's revised namespace LSM blob patch and Mickaël Salaün's v2 LSM namespace audit data patch, which are the first 2 patches in the series linked below.
Signed-off-by Daniel Durning <[email protected]>---
Changes in v2:
- Added thecreate_anonpermission to separately control creation of anonymous mount namespacesQuoted 7 lines
Changes in v3:
- Rebased onto v2 of the LSM namespace blob/audit data patches--- security/selinux/hooks.c | 69 +++++++++++++++++++++++++++++ security/selinux/include/classmap.h | 12 ++++- security/selinux/include/objsec.h | 10 +++++ 3 files changed, 90 insertions(+), 1 deletion(-)This looks good to me with the exception of one thing (below).
Quoted 33 lines
security/selinux/hooks.c +25
-0@@ -7010,6 +7037,45 @@ static int selinux_inode_getsecctx(struct inode *inode, struct lsm_context *cp) cp->id =LSM_ID_SELINUX ;return 0 ; } + +static int selinux_namespace_init (struct ns_common *ns) +{ +struct common_audit_data ad; +struct ns_security_struct *nssec =selinux_ns (ns); +u16 sclass =ns_type_to_security_class (ns); +u32 sid =current_sid (); + + ad.type =LSM_AUDIT_DATA_NS ; + ad.u.ns.ns_type = ns->ns_type; + ad.u.ns.ns_id = ns->ns_id; + + nssec->sid = sid; +if (ns->inum ==MNT_NS_ANON_INO ) +return avc_has_perm (sid, sid, sclass, +MNT_NAMESPACE__CREATE_ANON , &ad); + +return avc_has_perm (sid, sid, sclass,NAMESPACE__CREATE , &ad); +} + +static int selinux_namespace_install (const struct nsset *nsset, +struct ns_common *ns) +{ +struct common_audit_data ad; +struct ns_security_struct *nssec =selinux_ns (ns);security/selinux/hooks.c static int selinux_inode_getsecctx(struct inode *inode, struct lsm_context *cp) · line 7074
Quoted 10 lines
security/selinux/hooks.c +10
-0+u16 sclass =ns_type_to_security_class (ns); +u32 sid =current_sid (); + + ad.type =LSM_AUDIT_DATA_NS ; + ad.u.ns.ns_type = ns->ns_type; + ad.u.ns.ns_id = ns->ns_id; + +if (!nssec) +return avc_has_perm (sid,SECINITSID_KERNEL , sclass, +NAMESPACE__SETNS , &ad);In which cases do we expect to see "nssec" equal to NULL? If this is a possibility, shouldn't we be checking for the "ns" parameter to be NULL and not "nssec"? I'm asking because
selinux_ns()shouldn't ever return NULL, and if the "ns" parameter has a chance of being NULL then we risk a crash from the dereference inselinux_ns().Quoted 7 lines
Quoted 7 lines
From the patch description:
"We fall back toSECINITSID_KERNELifns_commonis pointing to an init namespace, since these are created before policy load and will be lacking a security blob."To be more precise, those init namespaces are created before LSM/SELinux has initialized IIUC.
Forget the description for a moment and look at the code. Perhaps I'm just burned out and missing something obvious, but I'm not seeing a path where "nssec" is ever NULL (without some dumb luck).
With "nssec" being defined by the following line:
nssec = selinux_ns(ns);... and
selinux_ns()being a fairly simple function:ns_security_struct *selinux_ns(ns_common *ns) { return ns->ns_security + selinux_blob_sizes.lbs_ns; }Quoted 7 lines
... if the "ns" parameter passed to
selinux_namespace_install()is NULL, the kernel will crash out inselinux_ns()due to the bogus dereference. If "ns->ns_security" is NULL then the return/"nssec" value is going to be non-NULL but very wrong. The only way I can see nssec being NULL is the dumb luck case where "ns->ns_security" is NULL and the "selinux_blob_sizes.lbs_ns" offset happens to also be zero; here "nssec" will be NULL, but we can't rely on that.I think that would be the default at the moment so likely what he was testing. But yes, agreed - we can't rely on that.
Quoted 4 lines
Quoted 3 lines
If we wanted to fix that, we'd need the patch that introduces the LSM hooks for namespaces to also update
security_init()to alloc the nssec for the initial namespaces, maybe?
Quoted 8 lines
Quoted 6 lines
Somewhat independent of my concern above, but yes, that's probably a good idea regardless. Assuming that the namespaces are properly setup before the call to
security_init(), calllsm_ns_alloc()for each namespace insecurity_init()and then each LSM can initialize init's namespace blobs as necessary in their own init function, e.g.selinux_init().Ok, I've lost the plot on that patch - where is it at this stage?
Merged in lsm/dev-staging with the expectation that it will move to lsm/dev next week after the merge window closes on Sunday (it's in the LSM tree as both Landlock and SELinux have pending patches that depend on those hooks). If you want to submit a patch to allocate init's namespace blobs in security_init() (which would be very welcome) go ahead and base it off that branch.
Signature · 1 line
paul-moore.com
Stephen Smalley
maintainerQuoted 14 lines · Paul Moore
Quoted 11 lines · Stephen Smalley
Quoted 10 lines
Quoted 3 lines
If we wanted to fix that, we'd need the patch that introduces the LSM hooks for namespaces to also update
security_init()to alloc the nssec for the initial namespaces, maybe?Somewhat independent of my concern above, but yes, that's probably a good idea regardless. Assuming that the namespaces are properly setup before the call to
security_init(), calllsm_ns_alloc()for each namespace insecurity_init()and then each LSM can initialize init's namespace blobs as necessary in their own init function, e.g.selinux_init().
Quoted 8 lines
Quoted 1 line
Ok, I've lost the plot on that patch - where is it at this stage?
Merged in lsm/dev-staging with the expectation that it will move to lsm/dev next week after the merge window closes on Sunday (it's in the LSM tree as both Landlock and SELinux have pending patches that depend on those hooks). If you want to submit a patch to allocate init's namespace blobs in
security_init()(which would be very welcome) go ahead and base it off that branch.
A patch that does this can be found at:
https://lore.kernel.org/linux-security-module/[email protected]/
Stephen Smalley
maintainerQuoted 25 lines · Stephen Smalley
Quoted 14 lines · Paul Moore
Quoted 11 lines · Stephen Smalley
Quoted 10 lines
Quoted 3 lines
If we wanted to fix that, we'd need the patch that introduces the LSM hooks for namespaces to also update
security_init()to alloc the nssec for the initial namespaces, maybe?Somewhat independent of my concern above, but yes, that's probably a good idea regardless. Assuming that the namespaces are properly setup before the call to
security_init(), calllsm_ns_alloc()for each namespace insecurity_init()and then each LSM can initialize init's namespace blobs as necessary in their own init function, e.g.selinux_init().Quoted 8 lines
Quoted 1 line
Ok, I've lost the plot on that patch - where is it at this stage?
Merged in lsm/dev-staging with the expectation that it will move to lsm/dev next week after the merge window closes on Sunday (it's in the LSM tree as both Landlock and SELinux have pending patches that depend on those hooks). If you want to submit a patch to allocate init's namespace blobs in
security_init()(which would be very welcome) go ahead and base it off that branch.
Quoted 2 lines
A patch that does this can be found at:
https://lore.kernel.org/linux-security-module/[email protected]/
A revised version of this patch can be found at:
https://lore.kernel.org/linux-security-module/[email protected]/