From: Stephen Smalley <stephen.smalley.work@gmail.com>
To: selinux@vger.kernel.org
Cc: paul@paul-moore.com, omosnace@redhat.com, netdev@vger.kernel.org,
horms@kernel.org,
Stephen Smalley <stephen.smalley.work@gmail.com>
Subject: [PATCH v7 20/42] selinux: maintain a small cache in the global SID table
Date: Thu, 14 Aug 2025 09:26:11 -0400 [thread overview]
Message-ID: <20250814132637.1659-21-stephen.smalley.work@gmail.com> (raw)
In-Reply-To: <20250814132637.1659-1-stephen.smalley.work@gmail.com>
Maintain a small cache in the global SID table to avoid needing
to map the context string each time we use a given SID in a different
namespace than the original one.
Signed-off-by: Stephen Smalley <stephen.smalley.work@gmail.com>
---
security/selinux/Kconfig | 11 +++
security/selinux/global_sidtab.c | 58 ++++++++++++++++
security/selinux/hooks.c | 1 +
security/selinux/include/global_sidtab.h | 7 ++
security/selinux/include/sidtab.h | 29 +++++++-
security/selinux/ss/sidtab.c | 85 ++++++++++++++++++++++++
6 files changed, 189 insertions(+), 2 deletions(-)
diff --git a/security/selinux/Kconfig b/security/selinux/Kconfig
index aa25da389c46..f7bd54aa136c 100644
--- a/security/selinux/Kconfig
+++ b/security/selinux/Kconfig
@@ -141,3 +141,14 @@ config SECURITY_SELINUX_MAXNSDEPTH
help
This option sets the default maximum depth of SELinux namespaces.
The value may be viewed or modified via /sys/fs/selinux/maxnsdepth.
+
+config SECURITY_SELINUX_SS_SID_CACHE_SIZE
+ int "Global SID to security server SID translation cache size"
+ depends on SECURITY_SELINUX_NS
+ default 4
+ help
+ This option defines the size of the global SID -> security server
+ SID cache, which improves the performance of mapping global SIDs.
+ Setting this option to 0 disables the cache completely.
+
+ If unsure, keep the default value.
diff --git a/security/selinux/global_sidtab.c b/security/selinux/global_sidtab.c
index 48934b429c8c..e1acf6607788 100644
--- a/security/selinux/global_sidtab.c
+++ b/security/selinux/global_sidtab.c
@@ -119,6 +119,11 @@ static int map_global_sid_to_ss(struct selinux_state *state, u32 sid,
int rc;
char *scontext;
u32 scontext_len;
+#if CONFIG_SECURITY_SELINUX_SS_SID_CACHE_SIZE > 0
+ struct sidtab_ss_sid_cache *cache;
+ unsigned long flags;
+ int i, first, last;
+#endif
if (sid <= SECINITSID_NUM) {
*ss_sid = sid;
@@ -136,6 +141,16 @@ static int map_global_sid_to_ss(struct selinux_state *state, u32 sid,
rcu_read_unlock();
return 0;
}
+#if CONFIG_SECURITY_SELINUX_SS_SID_CACHE_SIZE > 0
+ cache = &entry->ss_sid_cache;
+ for (i = cache->first; i >= 0 && i <= cache->last; i++) {
+ if (cache->state[i] == state && cache->ss_sid[i]) {
+ *ss_sid = cache->ss_sid[i];
+ rcu_read_unlock();
+ return 0;
+ }
+ }
+#endif
rcu_read_unlock();
rc = global_sid_to_context(sid, &scontext, &scontext_len);
@@ -144,10 +159,53 @@ static int map_global_sid_to_ss(struct selinux_state *state, u32 sid,
rc = selinux_ss_context_to_sid_force(state, scontext,
scontext_len, ss_sid, gfp);
+#if CONFIG_SECURITY_SELINUX_SS_SID_CACHE_SIZE > 0
+ if (rc == 0) {
+ spin_lock_irqsave(&global_sidtab.lock, flags);
+ entry = sidtab_search_entry_force(&global_sidtab, sid);
+ if (!entry) {
+ spin_unlock_irqrestore(&global_sidtab.lock, flags);
+ return -EINVAL;
+ }
+ cache = &entry->ss_sid_cache;
+ first = cache->first;
+ last = cache->last;
+ for (i = 0; i < ARRAY_SIZE(cache->ss_sid) && cache->ss_sid[i];
+ i++)
+ ;
+ if (i < ARRAY_SIZE(cache->ss_sid)) {
+ WRITE_ONCE(cache->ss_sid[i], *ss_sid);
+ /* ensure that the SID is written before the state */
+ smp_wmb();
+ WRITE_ONCE(cache->state[i], state);
+ } else {
+ if (first == -1)
+ i = 0;
+ else
+ i = first;
+ WRITE_ONCE(cache->ss_sid[i], *ss_sid);
+ /* ensure that the SID is written before the state */
+ smp_wmb();
+ WRITE_ONCE(cache->state[i], state);
+ }
+ /* ensure that state is updated before indices */
+ smp_wmb();
+ if (first == -1 || i < first)
+ WRITE_ONCE(cache->first, i);
+ if (last == -1 || i > last)
+ WRITE_ONCE(cache->last, i);
+ spin_unlock_irqrestore(&global_sidtab.lock, flags);
+ }
+#endif
kfree(scontext);
return rc;
}
+void global_sidtab_invalidate_state(struct selinux_state *state)
+{
+ sidtab_invalidate_state(&global_sidtab, state);
+}
+
static int map_ss_sid_to_global(struct selinux_state *state, u32 ss_sid,
u32 *out_sid, gfp_t gfp)
{
diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
index 98347ddae2e9..e8cfd18a1cb1 100644
--- a/security/selinux/hooks.c
+++ b/security/selinux/hooks.c
@@ -7874,6 +7874,7 @@ static void selinux_state_free(struct work_struct *work)
do {
parent = state->parent;
+ global_sidtab_invalidate_state(state);
if (state->status_page)
__free_page(state->status_page);
selinux_state_policy_free(state);
diff --git a/security/selinux/include/global_sidtab.h b/security/selinux/include/global_sidtab.h
index 2e06bb865326..bf450d775b66 100644
--- a/security/selinux/include/global_sidtab.h
+++ b/security/selinux/include/global_sidtab.h
@@ -9,11 +9,18 @@
#ifdef CONFIG_SECURITY_SELINUX_NS
extern int global_sidtab_init(void);
+
+struct selinux_state;
+void global_sidtab_invalidate_state(struct selinux_state *state);
#else
static inline int global_sidtab_init(void)
{
return 0;
}
+
+static inline void global_sidtab_invalidate_state(struct selinux_state *state)
+{
+}
#endif /* CONFIG_SECURITY_SELINUX_NS */
#endif /* _GLOBAL_SIDTAB_H_ */
diff --git a/security/selinux/include/sidtab.h b/security/selinux/include/sidtab.h
index 61389c588775..2df3ac0df935 100644
--- a/security/selinux/include/sidtab.h
+++ b/security/selinux/include/sidtab.h
@@ -18,6 +18,16 @@
#include "context.h"
+#ifdef CONFIG_SECURITY_SELINUX_NS
+#if CONFIG_SECURITY_SELINUX_SS_SID_CACHE_SIZE > 0
+struct sidtab_ss_sid_cache {
+ u32 ss_sid[CONFIG_SECURITY_SELINUX_SS_SID_CACHE_SIZE];
+ struct selinux_state *state[CONFIG_SECURITY_SELINUX_SS_SID_CACHE_SIZE];
+ int first, last;
+};
+#endif
+#endif
+
struct sidtab_entry {
u32 sid;
u32 hash;
@@ -27,8 +37,11 @@ struct sidtab_entry {
#endif
struct hlist_node list;
#ifdef CONFIG_SECURITY_SELINUX_NS
- u32 ss_sid; // global SID table only
- struct selinux_state *state; // global SID table only
+ u32 ss_sid;
+ struct selinux_state *state;
+#if CONFIG_SECURITY_SELINUX_SS_SID_CACHE_SIZE > 0
+ struct sidtab_ss_sid_cache ss_sid_cache;
+#endif
#endif
};
@@ -166,4 +179,16 @@ static inline int sidtab_sid2str_get(struct sidtab *s,
}
#endif /* CONFIG_SECURITY_SELINUX_SID2STR_CACHE_SIZE > 0 */
+#ifdef CONFIG_SECURITY_SELINUX_NS
+#if CONFIG_SECURITY_SELINUX_SS_SID_CACHE_SIZE > 0
+extern void sidtab_invalidate_state(struct sidtab *s,
+ struct selinux_state *state);
+#else
+static inline void sidtab_invalidate_state(struct sidtab *s,
+ struct selinux_state *state)
+{
+}
+#endif /* CONFIG_SECURITY_SELINUX_SS_SID_CACHE_SIZE > 0 */
+#endif /* CONFIG_SECURITY_SELINUX_NS */
+
#endif /* _SS_SIDTAB_H_ */
diff --git a/security/selinux/ss/sidtab.c b/security/selinux/ss/sidtab.c
index 19991f01cd20..eea37f78bec9 100644
--- a/security/selinux/ss/sidtab.c
+++ b/security/selinux/ss/sidtab.c
@@ -648,3 +648,88 @@ int sidtab_sid2str_get(struct sidtab *s, struct sidtab_entry *entry, char **out,
}
#endif /* CONFIG_SECURITY_SELINUX_SID2STR_CACHE_SIZE > 0 */
+
+#ifdef CONFIG_SECURITY_SELINUX_NS
+#if CONFIG_SECURITY_SELINUX_SS_SID_CACHE_SIZE > 0
+static void sidtab_invalidate_state_entry(struct sidtab_entry *entry,
+ struct selinux_state *state)
+{
+ struct sidtab_ss_sid_cache *cache;
+ int i, first, last;
+
+ cache = &entry->ss_sid_cache;
+ first = cache->first;
+ last = cache->last;
+ for (i = first; i >= 0 && i <= last; i++) {
+ if (cache->state[i] == state) {
+ WRITE_ONCE(cache->ss_sid[i], 0);
+ WRITE_ONCE(cache->state[i], NULL);
+ if (first == i) {
+ for (first = i + 1; first <= last &&
+ !cache->ss_sid[first]; first++)
+ ;
+ if (first == (i+1))
+ first = -1;
+ WRITE_ONCE(cache->first, first);
+ }
+ if (last == i) {
+ for (last = i - 1; last >= first &&
+ last >= 0 &&
+ !cache->ss_sid[last]; last--)
+ ;
+ if (last == (i-1))
+ last = -1;
+ WRITE_ONCE(cache->last, last);
+ }
+ return;
+ }
+ }
+}
+
+static void sidtab_invalidate_state_tree(union sidtab_entry_inner entry,
+ u32 level,
+ struct selinux_state *state)
+{
+ u32 i;
+
+ if (level != 0) {
+ struct sidtab_node_inner *node = entry.ptr_inner;
+
+ if (!node)
+ return;
+
+ for (i = 0; i < SIDTAB_INNER_ENTRIES; i++)
+ sidtab_invalidate_state_tree(node->entries[i],
+ level - 1, state);
+ } else {
+ struct sidtab_node_leaf *node = entry.ptr_leaf;
+
+ if (!node)
+ return;
+
+ for (i = 0; i < SIDTAB_LEAF_ENTRIES; i++)
+ sidtab_invalidate_state_entry(&node->entries[i], state);
+ }
+}
+
+void sidtab_invalidate_state(struct sidtab *s, struct selinux_state *state)
+{
+ u32 i, level;
+ unsigned long flags;
+
+ spin_lock_irqsave(&s->lock, flags);
+
+ for (i = 0; i < SECINITSID_NUM; i++)
+ if (s->isids[i].set)
+ sidtab_invalidate_state_entry(&s->isids[i].entry, state);
+
+ level = SIDTAB_MAX_LEVEL;
+ while (level && !s->roots[level].ptr_inner)
+ --level;
+
+ sidtab_invalidate_state_tree(s->roots[level], level, state);
+
+ spin_unlock_irqrestore(&s->lock, flags);
+}
+#endif /* CONFIG_SECURITY_SELINUX_SS_SID_CACHE_SIZE > 0 */
+#endif /* CONFIG_SECURITY_SELINUX_NS */
--
2.50.1
next prev parent reply other threads:[~2025-08-14 13:27 UTC|newest]
Thread overview: 43+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-08-14 13:25 [PATCH v7 00/42] SELinux namespace support Stephen Smalley
2025-08-14 13:25 ` [PATCH v7 01/42] selinux: restore passing of selinux_state Stephen Smalley
2025-08-14 13:25 ` [PATCH v7 02/42] selinux: introduce current_selinux_state Stephen Smalley
2025-08-14 13:25 ` [PATCH v7 03/42] selinux: support multiple selinuxfs instances Stephen Smalley
2025-08-14 13:25 ` [PATCH v7 04/42] selinux: dynamically allocate selinux namespace Stephen Smalley
2025-08-14 13:25 ` [PATCH v7 05/42] netstate,selinux: create the selinux netlink socket per network namespace Stephen Smalley
2025-08-14 13:25 ` [PATCH v7 06/42] selinux: limit selinux netlink notifications to init namespace Stephen Smalley
2025-08-14 13:25 ` [PATCH v7 07/42] selinux: support per-task/cred selinux namespace Stephen Smalley
2025-08-14 13:25 ` [PATCH v7 08/42] selinux: introduce cred_selinux_state() and use it Stephen Smalley
2025-08-14 13:26 ` [PATCH v7 09/42] selinux: init inode from nearest initialized namespace Stephen Smalley
2025-08-14 13:26 ` [PATCH v7 10/42] selinux: add a selinuxfs interface to unshare selinux namespace Stephen Smalley
2025-08-14 13:26 ` [PATCH v7 11/42] selinux: add limits for SELinux namespaces Stephen Smalley
2025-08-14 13:26 ` [PATCH v7 12/42] selinux: exempt creation of init SELinux namespace from limits Stephen Smalley
2025-08-14 13:26 ` [PATCH v7 13/42] selinux: refactor selinux_state_create() Stephen Smalley
2025-08-14 13:26 ` [PATCH v7 14/42] selinux: allow userspace to detect non-init SELinux namespace Stephen Smalley
2025-08-14 13:26 ` [PATCH v7 15/42] selinuxfs: restrict write operations to the same selinux namespace Stephen Smalley
2025-08-14 13:26 ` [PATCH v7 16/42] selinux: introduce a global SID table Stephen Smalley
2025-08-14 13:26 ` [PATCH v7 17/42] selinux: wrap security server interfaces to use the " Stephen Smalley
2025-08-14 13:26 ` [PATCH v7 18/42] selinux: introduce a Kconfig option for SELinux namespaces Stephen Smalley
2025-08-14 13:26 ` [PATCH v7 19/42] selinux: eliminate global SID table if !CONFIG_SECURITY_SELINUX_NS Stephen Smalley
2025-08-14 13:26 ` Stephen Smalley [this message]
2025-08-14 13:26 ` [PATCH v7 21/42] selinux: update hook functions to use correct selinux namespace Stephen Smalley
2025-08-14 13:26 ` [PATCH v7 22/42] selinux: introduce cred_task_has_perm() Stephen Smalley
2025-08-14 13:26 ` [PATCH v7 23/42] selinux: introduce cred_has_extended_perms() Stephen Smalley
2025-08-14 13:26 ` [PATCH v7 24/42] selinux: introduce cred_self_has_perm() Stephen Smalley
2025-08-14 13:26 ` [PATCH v7 25/42] selinux: introduce cred_has_perm() Stephen Smalley
2025-08-14 13:26 ` [PATCH v7 26/42] selinux: introduce cred_ssid_has_perm() and cred_other_has_perm() Stephen Smalley
2025-08-14 13:26 ` [PATCH v7 27/42] selinux: introduce task_obj_has_perm() Stephen Smalley
2025-08-14 13:26 ` [PATCH v7 28/42] selinux: update bprm hooks for selinux namespaces Stephen Smalley
2025-08-14 13:26 ` [PATCH v7 29/42] selinux: add kerneldoc to new permission checking functions Stephen Smalley
2025-08-14 13:26 ` [PATCH v7 30/42] selinux: convert selinux_file_send_sigiotask() to namespace-aware helper Stephen Smalley
2025-08-14 13:26 ` [PATCH v7 31/42] selinux: rename cred_has_perm*() to cred_tsid_has_perm*() Stephen Smalley
2025-08-14 13:26 ` [PATCH v7 32/42] selinux: update cred_tsid_has_perm_noaudit() to return the combined avd Stephen Smalley
2025-08-14 13:26 ` [PATCH v7 33/42] selinux: convert additional checks to cred_ssid_has_perm() Stephen Smalley
2025-08-14 13:26 ` [PATCH v7 34/42] selinux: introduce selinux_state_has_perm() Stephen Smalley
2025-08-14 13:26 ` [PATCH v7 35/42] selinux: annotate selinuxfs permission checks Stephen Smalley
2025-08-14 13:26 ` [PATCH v7 36/42] selinux: annotate process transition " Stephen Smalley
2025-08-14 13:26 ` [PATCH v7 37/42] selinux: convert xfrm and netlabel " Stephen Smalley
2025-08-14 13:26 ` [PATCH v7 38/42] selinux: switch selinux_lsm_setattr() checks to current namespace Stephen Smalley
2025-08-14 13:26 ` [PATCH v7 39/42] selinux: make open_perms namespace-aware Stephen Smalley
2025-08-14 13:26 ` [PATCH v7 40/42] selinux: split cred_ssid_has_perm() into two cases Stephen Smalley
2025-08-14 13:26 ` [PATCH v7 41/42] selinux: convert nlmsg_sock_has_extended_perms() to namespace-aware Stephen Smalley
2025-08-14 13:26 ` [PATCH v7 42/42] selinux: disallow writes to /sys/fs/selinux/user in non-init namespaces Stephen Smalley
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250814132637.1659-21-stephen.smalley.work@gmail.com \
--to=stephen.smalley.work@gmail.com \
--cc=horms@kernel.org \
--cc=netdev@vger.kernel.org \
--cc=omosnace@redhat.com \
--cc=paul@paul-moore.com \
--cc=selinux@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).