selinux: introduce neveraudit types
Stephen Smalley
maintainerRFC PATCH 1/1 selinux: introduce neveraudit types
Introduce neveraudit types i.e. types that should never trigger audit messages. This allows the AVC to skip all audit-related processing for such types. Note that neveraudit differs from dontaudit not only wrt being applied for all checks with a given source type but also in that it disables all auditing, not just permission denials.
When a type is both a permissive type and a neveraudit type, the security server can short-circuit the security_compute_av() logic, rendering the type equivalent to an unconfined type.
This change just introduces the basic support but does not yet further optimize the AVC or hook function logic when a type is both a permissive type and a dontaudit type.
---
If this seems viable/preferable to the unconfined types patch, then I will update the userspace patches, but it would just be a global renaming from unconfined to neveraudit.
security/selinux/include/avc.h | 4 ++++ security/selinux/include/security.h | 4 +++- security/selinux/ss/policydb.c | 19 +++++++++++++++++++ security/selinux/ss/policydb.h | 2 ++ security/selinux/ss/services.c | 20 ++++++++++++++++++++ 5 files changed, 48 insertions(+), 1 deletion(-)
security/selinux/include/avc.h +4 -0
@@ -65,6 +65,10 @@ static inline u32 avc_audit_required(u32 requested, struct av_decision *avd, int result, u32 auditdeny, u32 *deniedp) { u32 denied, audited; + +if (avd->flags &AVD_FLAGS_NEVERAUDIT ) +return 0 ; + denied = requested & ~avd->allowed;if (unlikely (denied)) { audited = denied & avd->auditdeny;
security/selinux/include/security.h +3 -1
@@ -47,10 +47,11 @@#define POLICYDB_VERSION_GLBLUB 32#define POLICYDB_VERSION_COMP_FTRANS 33/* compressed filename transitions */ #define POLICYDB_VERSION_COND_XPERMS 34/* extended permissions in conditional policies */ +#define POLICYDB_VERSION_NEVERAUDIT 35/* neveraudit types */ /* Range of policy versions we understand*/ #define POLICYDB_VERSION_MIN POLICYDB_VERSION_BASE -#define POLICYDB_VERSION_MAX POLICYDB_VERSION_COND_XPERMS +#define POLICYDB_VERSION_MAX POLICYDB_VERSION_NEVERAUDIT/* Mask for just the mount related flags */ #define SE_MNTMASK 0x0f
@@ -260,6 +261,7 @@ struct extended_perms {/* definitions of av_decision.flags */ #define AVD_FLAGS_PERMISSIVE 0x0001 +#define AVD_FLAGS_NEVERAUDIT 0x0002 void security_compute_av(u32 ssid, u32 tsid, u16 tclass, struct av_decision *avd,
security/selinux/ss/policydb.c +19 -0
@@ -160,6 +160,11 @@ static const struct policydb_compat_info policydb_compat[] = { .sym_num = SYM_NUM, .ocon_num = OCON_NUM, }, + { + .version = POLICYDB_VERSION_NEVERAUDIT, + .sym_num = SYM_NUM, + .ocon_num = OCON_NUM, + }, }; static const struct policydb_compat_info *
@@ -531,6 +536,7 @@ static void policydb_init(struct policydb *p)ebitmap_init (&p->filename_trans_ttypes);ebitmap_init (&p->policycaps);ebitmap_init (&p->permissive_map); +ebitmap_init (&p->neveraudit_map); }/*
@@ -852,6 +858,7 @@ void policydb_destroy(struct policydb *p)ebitmap_destroy (&p->filename_trans_ttypes);ebitmap_destroy (&p->policycaps);ebitmap_destroy (&p->permissive_map); +ebitmap_destroy (&p->neveraudit_map); }/*
@@ -2538,6 +2545,12 @@ int policydb_read(struct policydb *p, struct policy_file *fp) goto bad; } +if (p->policyvers >=POLICYDB_VERSION_NEVERAUDIT ) { + rc =ebitmap_read (&p->neveraudit_map, fp); +if (rc) + goto bad; + } + rc = -EINVAL ; info =policydb_lookup_compat (p->policyvers);if (!info) {
@@ -3723,6 +3736,12 @@ int policydb_write(struct policydb *p, struct policy_file *fp)return rc; } +if (p->policyvers >=POLICYDB_VERSION_NEVERAUDIT ) { + rc =ebitmap_write (&p->neveraudit_map, fp); +if (rc) +return rc; + } + num_syms = info->sym_num;for (i =0 ; i < num_syms; i++) {struct policy_data pd;
security/selinux/ss/policydb.h +2 -0
@@ -300,6 +300,8 @@ struct policydb {struct ebitmap permissive_map; +struct ebitmap neveraudit_map; +/* length of this policy when it was loaded */ size_t len;
security/selinux/ss/services.c +20 -0
@@ -1153,6 +1153,14 @@ void security_compute_av(u32 ssid,if (ebitmap_get_bit (&policydb->permissive_map, scontext->type)) avd->flags |=AVD_FLAGS_PERMISSIVE ; +/* neveraudit domain? */ +if (ebitmap_get_bit (&policydb->neveraudit_map, scontext->type)) + avd->flags |=AVD_FLAGS_NEVERAUDIT ; + +/* both permissive and neveraudit => unconfined */ +if (avd->flags == (AVD_FLAGS_PERMISSIVE |AVD_FLAGS_NEVERAUDIT )) + goto allow; + tcontext =sidtab_search (sidtab, tsid);if (!tcontext) { pr_err("SELinux: %s: unrecognized SID %d\n",
@@ -1172,6 +1180,8 @@ void security_compute_av(u32 ssid, policydb->allow_unknown); out:rcu_read_unlock (); +if (avd->flags &AVD_FLAGS_NEVERAUDIT ) + avd->auditallow = avd->auditdeny =0 ;return ; allow: avd->allowed =0xffffffff ;
@@ -1208,6 +1218,14 @@ void security_compute_av_user(u32 ssid,if (ebitmap_get_bit (&policydb->permissive_map, scontext->type)) avd->flags |=AVD_FLAGS_PERMISSIVE ; +/* neveraudit domain? */ +if (ebitmap_get_bit (&policydb->neveraudit_map, scontext->type)) + avd->flags |=AVD_FLAGS_NEVERAUDIT ; + +/* both permissive and neveraudit => unconfined */ +if (avd->flags == (AVD_FLAGS_PERMISSIVE |AVD_FLAGS_NEVERAUDIT )) + goto allow; + tcontext =sidtab_search (sidtab, tsid);if (!tcontext) { pr_err("SELinux: %s: unrecognized SID %d\n",
@@ -1225,6 +1243,8 @@ void security_compute_av_user(u32 ssid, NULL); out:rcu_read_unlock (); +if (avd->flags &AVD_FLAGS_NEVERAUDIT ) + avd->auditallow = avd->auditdeny =0 ;return ; allow: avd->allowed =0xffffffff ;
Signature · 1 line
2.49.0
Stephen Smalley
maintainerRFC PATCH 2/2 selinux: optimize selinux_inode_permission() for unconfined tasks
Extend the task avdcache to also cache whether the task SID unconfined aka permissive|neveraudit and if so, optimize selinux_inode_permission(). The same approach could be applied to all of the hook functions.
---
This change demonstrates how the previous patch could be leveraged to optimize selinux_inode_permission().
security/selinux/hooks.c | 7 ++++++- security/selinux/include/objsec.h | 1 + 2 files changed, 7 insertions(+), 1 deletion(-)
security/selinux/hooks.c +6 -1
@@ -3184,6 +3184,7 @@ static inline void task_avdcache_update(struct task_security_struct *tsec, tsec->avdcache.dir[spot].audited = audited; tsec->avdcache.dir[spot].allowed = avd->allowed; tsec->avdcache.dir[spot].permissive = avd->flags &AVD_FLAGS_PERMISSIVE ; + tsec->avdcache.unconfined = (avd->flags == (AVD_FLAGS_PERMISSIVE |AVD_FLAGS_NEVERAUDIT )); }/**
@@ -3210,10 +3211,14 @@ static int selinux_inode_permission(struct inode *inode, int requested)if (!mask)return 0 ; + tsec =selinux_cred (current_cred ()); +if (tsec->avdcache.unconfined && tsec->sid == tsec->avdcache.sid && + tsec->avdcache.seqno ==avc_policy_seqno ()) +return 0 ; + isec =inode_security_rcu (inode, requested &MAY_NOT_BLOCK );if (IS_ERR (isec))return PTR_ERR (isec); - tsec =selinux_cred (current_cred ()); perms =file_mask_to_av (inode->i_mode, mask); rc =task_avdcache_search (tsec, isec, &avdc);
security/selinux/include/objsec.h +1 -0
@@ -49,6 +49,7 @@ struct task_security_struct {u32 seqno;/* AVC sequence number */ unsigned int dir_spot;/* dir cache index to check first */ struct avdc_entry dir[TSEC_AVDC_DIR_SIZE ];/* dir entries */ +bool unconfined;/* unconfined */ } avdcache; } __randomize_layout;
Signature · 1 line
2.49.0
Paul Moore
maintainerRe: [RFC PATCH 2/2] selinux: optimize selinux_inode_permission() for unconfined tasks
Quoted 48 lines · Stephen Smalley
Extend the task avdcache to also cache whether the task SID unconfined aka permissive|neveraudit and if so, optimize
selinux_inode_permission(). The same approach could be applied to all of the hook functions.Signed-off-by Stephen Smalley <[email protected]>---
This change demonstrates how the previous patch could be leveraged to optimizeselinux_inode_permission().security/selinux/hooks.c | 7 ++++++- security/selinux/include/objsec.h | 1 + 2 files changed, 7 insertions(+), 1 deletion(-)security/selinux/hooks.c +6
-1@@ -3184,6 +3184,7 @@ static inline void task_avdcache_update(struct task_security_struct *tsec, tsec->avdcache.dir[spot].audited = audited; tsec->avdcache.dir[spot].allowed = avd->allowed; tsec->avdcache.dir[spot].permissive = avd->flags &AVD_FLAGS_PERMISSIVE ; + tsec->avdcache.unconfined = (avd->flags == (AVD_FLAGS_PERMISSIVE |AVD_FLAGS_NEVERAUDIT )); }/** @@ -3210,10 +3211,14 @@ static int selinux_inode_permission(struct inode *inode, int requested)if (!mask)return 0 ; + tsec =selinux_cred (current_cred ()); +if (tsec->avdcache.unconfined && tsec->sid == tsec->avdcache.sid && + tsec->avdcache.seqno ==avc_policy_seqno ()) +return 0 ; + isec =inode_security_rcu (inode, requested &MAY_NOT_BLOCK );if (IS_ERR (isec))return PTR_ERR (isec); - tsec =selinux_cred (current_cred ()); perms =file_mask_to_av (inode->i_mode, mask); rc =task_avdcache_search (tsec, isec, &avdc);security/selinux/include/objsec.h +0
-0
security/selinux/include/objsec.h struct task_security_struct { · line 52
Quoted 4 lines
security/selinux/include/objsec.h +1
-0@@ -49,6 +49,7 @@ struct task_security_struct {u32 seqno;/* AVC sequence number */ unsigned int dir_spot;/* dir cache index to check first */ struct avdc_entry dir[TSEC_AVDC_DIR_SIZE ];/* dir entries */ +bool unconfined;/* unconfined */
Once again, I understand the end result is effectively the same, but please don't call this "unconfined" as I worry about confusion with the unconfined_t type. I would suggest "permissive_noaudit" or something similar; if you come up with something shorter, that's even better.
Quoted 5 lines
security/selinux/include/objsec.h +0
-1} avdcache; } __randomize_layout; --2.49.0
Signature · 1 line
paul-moore.com
Paul Moore
maintainerQuoted 16 lines · Stephen Smalley
Introduce neveraudit types i.e. types that should never trigger audit messages. This allows the AVC to skip all audit-related processing for such types. Note that neveraudit differs from dontaudit not only wrt being applied for all checks with a given source type but also in that it disables all auditing, not just permission denials.
When a type is both a permissive type and a neveraudit type, the security server can short-circuit the
security_compute_av()logic, rendering the type equivalent to an unconfined type.This change just introduces the basic support but does not yet further optimize the AVC or hook function logic when a type is both a permissive type and a dontaudit type.
Quoted 8 lines
Suggested-by Paul Moore <[email protected]>Signed-off-by Stephen Smalley <[email protected]>---
If this seems viable/preferable to the unconfined types patch, then I will update the userspace patches, but it would just be a global renaming from unconfined to neveraudit.
Without going over this patch very closely, generally speaking yes, I believe the permissive|neveraudit approach is more desirable than an unconfined approach for the reasons mentioned in the other thread.
Quoted 6 lines
security/selinux/include/avc.h | 4 ++++ security/selinux/include/security.h | 4 +++- security/selinux/ss/policydb.c | 19 +++++++++++++++++++ security/selinux/ss/policydb.h | 2 ++ security/selinux/ss/services.c | 20 ++++++++++++++++++++ 5 files changed, 48 insertions(+), 1 deletion(-)
Signature · 1 line
paul-moore.com