Skip to content

Commit 5dbe1eb

Browse files
pcmoorexjamesmorris
authored andcommitted
SELinux: Allow NetLabel to directly cache SIDs
Now that the SELinux NetLabel "base SID" is always the netmsg initial SID we can do a big optimization - caching the SID and not just the MLS attributes. This not only saves a lot of per-packet memory allocations and copies but it has a nice side effect of removing a chunk of code. Signed-off-by: Paul Moore <paul.moore@hp.com> Signed-off-by: James Morris <jmorris@namei.org>
1 parent d621d35 commit 5dbe1eb

5 files changed

Lines changed: 55 additions & 134 deletions

File tree

‎security/selinux/hooks.c‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3468,11 +3468,7 @@ static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid)
34683468
u32 nlbl_type;
34693469

34703470
selinux_skb_xfrm_sid(skb, &xfrm_sid);
3471-
selinux_netlbl_skbuff_getsid(skb,
3472-
family,
3473-
SECINITSID_NETMSG,
3474-
&nlbl_type,
3475-
&nlbl_sid);
3471+
selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid);
34763472

34773473
if (security_net_peersid_resolve(nlbl_sid, nlbl_type,
34783474
xfrm_sid,

‎security/selinux/include/netlabel.h‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ void selinux_netlbl_sk_security_clone(struct sk_security_struct *ssec,
4848

4949
int selinux_netlbl_skbuff_getsid(struct sk_buff *skb,
5050
u16 family,
51-
u32 base_sid,
5251
u32 *type,
5352
u32 *sid);
5453

@@ -89,7 +88,6 @@ static inline void selinux_netlbl_sk_security_clone(
8988

9089
static inline int selinux_netlbl_skbuff_getsid(struct sk_buff *skb,
9190
u16 family,
92-
u32 base_sid,
9391
u32 *type,
9492
u32 *sid)
9593
{

‎security/selinux/include/security.h‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,15 +124,13 @@ int security_genfs_sid(const char *fstype, char *name, u16 sclass,
124124

125125
#ifdef CONFIG_NETLABEL
126126
int security_netlbl_secattr_to_sid(struct netlbl_lsm_secattr *secattr,
127-
u32 base_sid,
128127
u32 *sid);
129128

130129
int security_netlbl_sid_to_secattr(u32 sid,
131130
struct netlbl_lsm_secattr *secattr);
132131
#else
133132
static inline int security_netlbl_secattr_to_sid(
134133
struct netlbl_lsm_secattr *secattr,
135-
u32 base_sid,
136134
u32 *sid)
137135
{
138136
return -EIDRM;

‎security/selinux/netlabel.c‎

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,33 @@
3535
#include "objsec.h"
3636
#include "security.h"
3737

38+
/**
39+
* selinux_netlbl_sidlookup_cached - Cache a SID lookup
40+
* @skb: the packet
41+
* @secattr: the NetLabel security attributes
42+
* @sid: the SID
43+
*
44+
* Description:
45+
* Query the SELinux security server to lookup the correct SID for the given
46+
* security attributes. If the query is successful, cache the result to speed
47+
* up future lookups. Returns zero on success, negative values on failure.
48+
*
49+
*/
50+
static int selinux_netlbl_sidlookup_cached(struct sk_buff *skb,
51+
struct netlbl_lsm_secattr *secattr,
52+
u32 *sid)
53+
{
54+
int rc;
55+
56+
rc = security_netlbl_secattr_to_sid(secattr, sid);
57+
if (rc == 0 &&
58+
(secattr->flags & NETLBL_SECATTR_CACHEABLE) &&
59+
(secattr->flags & NETLBL_SECATTR_CACHE))
60+
netlbl_cache_add(skb, secattr);
61+
62+
return rc;
63+
}
64+
3865
/**
3966
* selinux_netlbl_sock_setsid - Label a socket using the NetLabel mechanism
4067
* @sk: the socket to label
@@ -144,7 +171,6 @@ void selinux_netlbl_sk_security_clone(struct sk_security_struct *ssec,
144171
* selinux_netlbl_skbuff_getsid - Get the sid of a packet using NetLabel
145172
* @skb: the packet
146173
* @family: protocol family
147-
* @base_sid: the SELinux SID to use as a context for MLS only attributes
148174
* @type: NetLabel labeling protocol type
149175
* @sid: the SID
150176
*
@@ -156,7 +182,6 @@ void selinux_netlbl_sk_security_clone(struct sk_security_struct *ssec,
156182
*/
157183
int selinux_netlbl_skbuff_getsid(struct sk_buff *skb,
158184
u16 family,
159-
u32 base_sid,
160185
u32 *type,
161186
u32 *sid)
162187
{
@@ -170,13 +195,9 @@ int selinux_netlbl_skbuff_getsid(struct sk_buff *skb,
170195

171196
netlbl_secattr_init(&secattr);
172197
rc = netlbl_skbuff_getattr(skb, family, &secattr);
173-
if (rc == 0 && secattr.flags != NETLBL_SECATTR_NONE) {
174-
rc = security_netlbl_secattr_to_sid(&secattr, base_sid, sid);
175-
if (rc == 0 &&
176-
(secattr.flags & NETLBL_SECATTR_CACHEABLE) &&
177-
(secattr.flags & NETLBL_SECATTR_CACHE))
178-
netlbl_cache_add(skb, &secattr);
179-
} else
198+
if (rc == 0 && secattr.flags != NETLBL_SECATTR_NONE)
199+
rc = selinux_netlbl_sidlookup_cached(skb, &secattr, sid);
200+
else
180201
*sid = SECSID_NULL;
181202
*type = secattr.type;
182203
netlbl_secattr_destroy(&secattr);
@@ -210,9 +231,7 @@ void selinux_netlbl_sock_graft(struct sock *sk, struct socket *sock)
210231
netlbl_secattr_init(&secattr);
211232
if (netlbl_sock_getattr(sk, &secattr) == 0 &&
212233
secattr.flags != NETLBL_SECATTR_NONE &&
213-
security_netlbl_secattr_to_sid(&secattr,
214-
SECINITSID_NETMSG,
215-
&nlbl_peer_sid) == 0)
234+
security_netlbl_secattr_to_sid(&secattr, &nlbl_peer_sid) == 0)
216235
sksec->peer_sid = nlbl_peer_sid;
217236
netlbl_secattr_destroy(&secattr);
218237

@@ -316,15 +335,9 @@ int selinux_netlbl_sock_rcv_skb(struct sk_security_struct *sksec,
316335

317336
netlbl_secattr_init(&secattr);
318337
rc = netlbl_skbuff_getattr(skb, family, &secattr);
319-
if (rc == 0 && secattr.flags != NETLBL_SECATTR_NONE) {
320-
rc = security_netlbl_secattr_to_sid(&secattr,
321-
SECINITSID_NETMSG,
322-
&nlbl_sid);
323-
if (rc == 0 &&
324-
(secattr.flags & NETLBL_SECATTR_CACHEABLE) &&
325-
(secattr.flags & NETLBL_SECATTR_CACHE))
326-
netlbl_cache_add(skb, &secattr);
327-
} else
338+
if (rc == 0 && secattr.flags != NETLBL_SECATTR_NONE)
339+
rc = selinux_netlbl_sidlookup_cached(skb, &secattr, &nlbl_sid);
340+
else
328341
nlbl_sid = SECINITSID_UNLABELED;
329342
netlbl_secattr_destroy(&secattr);
330343
if (rc != 0)

‎security/selinux/ss/services.c‎

Lines changed: 20 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -2547,50 +2547,10 @@ void selinux_audit_set_callback(int (*callback)(void))
25472547
}
25482548

25492549
#ifdef CONFIG_NETLABEL
2550-
/*
2551-
* NetLabel cache structure
2552-
*/
2553-
#define NETLBL_CACHE(x) ((struct selinux_netlbl_cache *)(x))
2554-
#define NETLBL_CACHE_T_NONE 0
2555-
#define NETLBL_CACHE_T_SID 1
2556-
#define NETLBL_CACHE_T_MLS 2
2557-
struct selinux_netlbl_cache {
2558-
u32 type;
2559-
union {
2560-
u32 sid;
2561-
struct mls_range mls_label;
2562-
} data;
2563-
};
2564-
2565-
/**
2566-
* security_netlbl_cache_free - Free the NetLabel cached data
2567-
* @data: the data to free
2568-
*
2569-
* Description:
2570-
* This function is intended to be used as the free() callback inside the
2571-
* netlbl_lsm_cache structure.
2572-
*
2573-
*/
2574-
static void security_netlbl_cache_free(const void *data)
2575-
{
2576-
struct selinux_netlbl_cache *cache;
2577-
2578-
if (data == NULL)
2579-
return;
2580-
2581-
cache = NETLBL_CACHE(data);
2582-
switch (cache->type) {
2583-
case NETLBL_CACHE_T_MLS:
2584-
ebitmap_destroy(&cache->data.mls_label.level[0].cat);
2585-
break;
2586-
}
2587-
kfree(data);
2588-
}
2589-
25902550
/**
25912551
* security_netlbl_cache_add - Add an entry to the NetLabel cache
25922552
* @secattr: the NetLabel packet security attributes
2593-
* @ctx: the SELinux context
2553+
* @sid: the SELinux SID
25942554
*
25952555
* Description:
25962556
* Attempt to cache the context in @ctx, which was derived from the packet in
@@ -2599,60 +2559,46 @@ static void security_netlbl_cache_free(const void *data)
25992559
*
26002560
*/
26012561
static void security_netlbl_cache_add(struct netlbl_lsm_secattr *secattr,
2602-
struct context *ctx)
2562+
u32 sid)
26032563
{
2604-
struct selinux_netlbl_cache *cache = NULL;
2564+
u32 *sid_cache;
26052565

2606-
secattr->cache = netlbl_secattr_cache_alloc(GFP_ATOMIC);
2607-
if (secattr->cache == NULL)
2608-
return;
2609-
2610-
cache = kzalloc(sizeof(*cache), GFP_ATOMIC);
2611-
if (cache == NULL)
2566+
sid_cache = kmalloc(sizeof(*sid_cache), GFP_ATOMIC);
2567+
if (sid_cache == NULL)
26122568
return;
2613-
2614-
cache->type = NETLBL_CACHE_T_MLS;
2615-
if (ebitmap_cpy(&cache->data.mls_label.level[0].cat,
2616-
&ctx->range.level[0].cat) != 0) {
2617-
kfree(cache);
2569+
secattr->cache = netlbl_secattr_cache_alloc(GFP_ATOMIC);
2570+
if (secattr->cache == NULL) {
2571+
kfree(sid_cache);
26182572
return;
26192573
}
2620-
cache->data.mls_label.level[1].cat.highbit =
2621-
cache->data.mls_label.level[0].cat.highbit;
2622-
cache->data.mls_label.level[1].cat.node =
2623-
cache->data.mls_label.level[0].cat.node;
2624-
cache->data.mls_label.level[0].sens = ctx->range.level[0].sens;
2625-
cache->data.mls_label.level[1].sens = ctx->range.level[0].sens;
26262574

2627-
secattr->cache->free = security_netlbl_cache_free;
2628-
secattr->cache->data = (void *)cache;
2575+
*sid_cache = sid;
2576+
secattr->cache->free = kfree;
2577+
secattr->cache->data = sid_cache;
26292578
secattr->flags |= NETLBL_SECATTR_CACHE;
26302579
}
26312580

26322581
/**
26332582
* security_netlbl_secattr_to_sid - Convert a NetLabel secattr to a SELinux SID
26342583
* @secattr: the NetLabel packet security attributes
2635-
* @base_sid: the SELinux SID to use as a context for MLS only attributes
26362584
* @sid: the SELinux SID
26372585
*
26382586
* Description:
26392587
* Convert the given NetLabel security attributes in @secattr into a
26402588
* SELinux SID. If the @secattr field does not contain a full SELinux
2641-
* SID/context then use the context in @base_sid as the foundation. If
2642-
* possibile the 'cache' field of @secattr is set and the CACHE flag is set;
2643-
* this is to allow the @secattr to be used by NetLabel to cache the secattr to
2644-
* SID conversion for future lookups. Returns zero on success, negative
2645-
* values on failure.
2589+
* SID/context then use SECINITSID_NETMSG as the foundation. If possibile the
2590+
* 'cache' field of @secattr is set and the CACHE flag is set; this is to
2591+
* allow the @secattr to be used by NetLabel to cache the secattr to SID
2592+
* conversion for future lookups. Returns zero on success, negative values on
2593+
* failure.
26462594
*
26472595
*/
26482596
int security_netlbl_secattr_to_sid(struct netlbl_lsm_secattr *secattr,
2649-
u32 base_sid,
26502597
u32 *sid)
26512598
{
26522599
int rc = -EIDRM;
26532600
struct context *ctx;
26542601
struct context ctx_new;
2655-
struct selinux_netlbl_cache *cache;
26562602

26572603
if (!ss_initialized) {
26582604
*sid = SECSID_NULL;
@@ -2662,43 +2608,13 @@ int security_netlbl_secattr_to_sid(struct netlbl_lsm_secattr *secattr,
26622608
POLICY_RDLOCK;
26632609

26642610
if (secattr->flags & NETLBL_SECATTR_CACHE) {
2665-
cache = NETLBL_CACHE(secattr->cache->data);
2666-
switch (cache->type) {
2667-
case NETLBL_CACHE_T_SID:
2668-
*sid = cache->data.sid;
2669-
rc = 0;
2670-
break;
2671-
case NETLBL_CACHE_T_MLS:
2672-
ctx = sidtab_search(&sidtab, base_sid);
2673-
if (ctx == NULL)
2674-
goto netlbl_secattr_to_sid_return;
2675-
2676-
ctx_new.user = ctx->user;
2677-
ctx_new.role = ctx->role;
2678-
ctx_new.type = ctx->type;
2679-
ctx_new.range.level[0].sens =
2680-
cache->data.mls_label.level[0].sens;
2681-
ctx_new.range.level[0].cat.highbit =
2682-
cache->data.mls_label.level[0].cat.highbit;
2683-
ctx_new.range.level[0].cat.node =
2684-
cache->data.mls_label.level[0].cat.node;
2685-
ctx_new.range.level[1].sens =
2686-
cache->data.mls_label.level[1].sens;
2687-
ctx_new.range.level[1].cat.highbit =
2688-
cache->data.mls_label.level[1].cat.highbit;
2689-
ctx_new.range.level[1].cat.node =
2690-
cache->data.mls_label.level[1].cat.node;
2691-
2692-
rc = sidtab_context_to_sid(&sidtab, &ctx_new, sid);
2693-
break;
2694-
default:
2695-
goto netlbl_secattr_to_sid_return;
2696-
}
2611+
*sid = *(u32 *)secattr->cache->data;
2612+
rc = 0;
26972613
} else if (secattr->flags & NETLBL_SECATTR_SECID) {
26982614
*sid = secattr->attr.secid;
26992615
rc = 0;
27002616
} else if (secattr->flags & NETLBL_SECATTR_MLS_LVL) {
2701-
ctx = sidtab_search(&sidtab, base_sid);
2617+
ctx = sidtab_search(&sidtab, SECINITSID_NETMSG);
27022618
if (ctx == NULL)
27032619
goto netlbl_secattr_to_sid_return;
27042620

@@ -2725,7 +2641,7 @@ int security_netlbl_secattr_to_sid(struct netlbl_lsm_secattr *secattr,
27252641
if (rc != 0)
27262642
goto netlbl_secattr_to_sid_return_cleanup;
27272643

2728-
security_netlbl_cache_add(secattr, &ctx_new);
2644+
security_netlbl_cache_add(secattr, *sid);
27292645

27302646
ebitmap_destroy(&ctx_new.range.level[0].cat);
27312647
} else {

0 commit comments

Comments
 (0)