Fix ENOMEM errors during policy reload
cover Fix ENOMEM errors during policy reload
This patchset is an alternative, hopefully better (but also more risky), solution of the ENOMEM problem ([1]) that I first tried to solve in [2].
In this version I encapsulate the initial SID table within sidtab and also switch back from converting the sidtab in-place to converting into a new sidtab and then just switching the pointer (keeping the code ready for switching to RCU locks).
The change is split into three patches for easier review. Some changes done in the first two patches are effectively undone by the last patch, so it might actually make more sense to send the final version as one squashed patch (please let me know which is better for you).
The first patch moves the sidtab conversion logic into sidtab.c. This allows hiding sidtab_insert() from sidtab.h in the second patch, where it becomes an internal function.
The second patch separates the handling of initial SIDs into a separate lookup table inside sidtab. After this change, the main table always contains N entries with keys from 0 to (N-1). This property is then leveraged in the last patch.
Finally, the third patch rewrites the main sidtab to a more efficient implementation that also gracefully handles context conversions during policy reloads, which no longer produces the ENOMEM errors.
After applying this patchset, the time it takes to insert new sidtab entries is drastically reduced. I measured the time to populate the table with N new entries by repeatedly writing to /sys/fs/selinux/context. A graph of the results is available at [3].
The SID -> context lookups are now also faster. With the old implementation, these are O(N) once N goes above 128. The new implementation can handle them theoretically in O(log N), but in practice the slope is almost flat, so they are practically almost constant-time.
Review and feedback welcome.
[1] https://github.com/SELinuxProject/selinux-kernel/issues/38
[2] https://lore.kernel.org/selinux/[email protected]/
[3] https://docs.google.com/spreadsheets/d/e/2PACX-1vRUArNJR6kckm2SEs4dRZlijNVdCTmsNuWRGe7X3fC01YkBHpxXHnmcssxEiMF3Z7ivtXN2L5MC0ry-/pubhtml
Ondrej Mosnacek (3):
selinux: refactor sidtab conversion
selinux: use separate table for initial SID lookup
selinux: overhaul sidtab to fix bug and improve performance
security/selinux/ss/mls.c | 23 +- security/selinux/ss/mls.h | 3 +- security/selinux/ss/policydb.c | 10 +- security/selinux/ss/services.c | 188 +++++------ security/selinux/ss/services.h | 2 +- security/selinux/ss/sidtab.c | 550 ++++++++++++++++++++------------- security/selinux/ss/sidtab.h | 90 ++++-- 7 files changed, 498 insertions(+), 368 deletions(-)
Signature · 1 line
2.17.2
RFC PATCH 1/3 selinux: refactor sidtab conversion
This is a purely cosmetic change that encapsulates the three-step sidtab conversion logic (shutdown -> clone -> map) into a single function defined in sidtab.c (as opposed to services.c).
--- security/selinux/ss/services.c | 22 +-------------- security/selinux/ss/sidtab.c | 50 ++++++++++++++++++++++++---------- security/selinux/ss/sidtab.h | 11 ++++---- 3 files changed, 42 insertions(+), 41 deletions(-)
security/selinux/ss/services.c +1 -21
@@ -1880,19 +1880,6 @@ int security_change_sid(struct selinux_state *state, out_sid, false); } -/* Clone the SID into the new SID table. */ -static int clone_sid (u32 sid, -struct context *context, -void *arg) -{ -struct sidtab *s = arg; - -if (sid >SECINITSID_NUM ) -return sidtab_insert (s, sid, context); -else -return 0 ; -} -static inline int convert_context_handle_invalid_context (struct selinux_state *state,struct context *context)
@@ -2186,13 +2173,6 @@ int security_load_policy(struct selinux_state *state, void *data, size_t len) goto err; } -/* Clone the SID table. */ -sidtab_shutdown (sidtab); - - rc =sidtab_map (sidtab, clone_sid, &newsidtab); -if (rc) - goto err; -/* * Convert the internal representations of contexts * in the new SID table.
@@ -2200,7 +2180,7 @@ int security_load_policy(struct selinux_state *state, void *data, size_t len) args.state = state; args.oldp = policydb; args.newp = newpolicydb; - rc =sidtab_map (&newsidtab, convert_context, &args); + rc =sidtab_convert (sidtab, &newsidtab, convert_context, &args);if (rc) {pr_err ("SELinux: unable to convert the internal" " representation of contexts in the new SID"
security/selinux/ss/sidtab.c +36 -14
65 lines not on this page · raw message
security/selinux/ss/sidtab.h +5 -6
@@ -37,11 +37,11 @@ int sidtab_insert(struct sidtab *s, u32 sid, struct context *context);struct context *sidtab_search (struct sidtab *s,u32 sid);struct context *sidtab_search_force (struct sidtab *s,u32 sid); -int sidtab_map (struct sidtab *s, -int (*apply) (u32 sid, -struct context *context, -void *args), -void *args); +int sidtab_convert (struct sidtab *s,struct sidtab *news, +int (*apply) (u32 sid, +struct context *context, +void *args), +void *args); int sidtab_context_to_sid(struct sidtab *s, struct context *context,
@@ -50,7 +50,6 @@ int sidtab_context_to_sid(struct sidtab *s,void sidtab_hash_eval (struct sidtab *h,char *tag);void sidtab_destroy (struct sidtab *s);void sidtab_set (struct sidtab *dst,struct sidtab *src); -void sidtab_shutdown (struct sidtab *s);#endif /* _SS_SIDTAB_H_ */
Signature · 1 line
2.17.2
Re: [RFC PATCH 1/3] selinux: refactor sidtab conversion
Quoted 5 lines · Ondrej Mosnacek
This is a purely cosmetic change that encapsulates the three-step sidtab conversion logic (shutdown -> clone -> map) into a single function defined in
sidtab.c(as opposed toservices.c).Signed-off-by Ondrej Mosnacek <[email protected]>
Quoted 155 lines
--- security/selinux/ss/services.c | 22 +-------------- security/selinux/ss/sidtab.c | 50 ++++++++++++++++++++++++---------- security/selinux/ss/sidtab.h | 11 ++++---- 3 files changed, 42 insertions(+), 41 deletions(-)security/selinux/ss/services.c +1
-21@@ -1880,19 +1880,6 @@ int security_change_sid(struct selinux_state *state, out_sid, false); } -/* Clone the SID into the new SID table. */ -static int clone_sid (u32 sid, -struct context *context, -void *arg) -{ -struct sidtab *s = arg; - -if (sid >SECINITSID_NUM ) -return sidtab_insert (s, sid, context); -else -return 0 ; -} -static inline int convert_context_handle_invalid_context (struct selinux_state *state,struct context *context)@@ -2186,13 +2173,6 @@ int security_load_policy(struct selinux_state *state, void *data, size_t len) goto err; } -/* Clone the SID table. */ -sidtab_shutdown (sidtab); - - rc =sidtab_map (sidtab, clone_sid, &newsidtab); -if (rc) - goto err; -/* * Convert the internal representations of contexts * in the new SID table. @@ -2200,7 +2180,7 @@ int security_load_policy(struct selinux_state *state, void *data, size_t len) args.state = state; args.oldp = policydb; args.newp = newpolicydb; - rc =sidtab_map (&newsidtab, convert_context, &args); + rc =sidtab_convert (sidtab, &newsidtab, convert_context, &args);if (rc) {pr_err ("SELinux: unable to convert the internal" " representation of contexts in the new SID"security/selinux/ss/sidtab.c +36
-14@@ -116,11 +116,11 @@ struct context *sidtab_search_force(struct sidtab *s, u32 sid)return sidtab_search_core (s, sid,1 ); } -int sidtab_map (struct sidtab *s, -int (*apply) (u32 sid, -struct context *context, -void *args), -void *args) +static int sidtab_map (struct sidtab *s, +int (*apply) (u32 sid, +struct context *context, +void *args), +void *args) {int i, rc =0 ;struct sidtab_node *cur;@@ -141,6 +141,37 @@ out:return rc; } +/* Clone the SID into the new SID table. */ +static int clone_sid (u32 sid,struct context *context,void *arg) +{ +struct sidtab *s = arg; + +if (sid >SECINITSID_NUM ) +return sidtab_insert (s, sid, context); +else +return 0 ; +} + +int sidtab_convert (struct sidtab *s,struct sidtab *news, +int (*convert) (u32 sid, +struct context *context, +void *args), +void *args) +{ +unsigned long flags; +int rc; + +spin_lock_irqsave (&s->lock, flags); + s->shutdown =1 ; +spin_unlock_irqrestore (&s->lock, flags); + + rc =sidtab_map (s, clone_sid, news); +if (rc) +return rc; + +return sidtab_map (news, convert, args); +} +static void sidtab_update_cache (struct sidtab *s,struct sidtab_node *n,int loc) {BUG_ON (loc >=SIDTAB_CACHE_LEN );@@ -295,12 +326,3 @@ void sidtab_set(struct sidtab *dst, struct sidtab *src) dst->cache[i] =NULL ;spin_unlock_irqrestore (&src->lock, flags); } - -void sidtab_shutdown (struct sidtab *s) -{ -unsigned long flags; - -spin_lock_irqsave (&s->lock, flags); - s->shutdown =1 ; -spin_unlock_irqrestore (&s->lock, flags); -}security/selinux/ss/sidtab.h +5
-6@@ -37,11 +37,11 @@ int sidtab_insert(struct sidtab *s, u32 sid, struct context *context);struct context *sidtab_search (struct sidtab *s,u32 sid);struct context *sidtab_search_force (struct sidtab *s,u32 sid); -int sidtab_map (struct sidtab *s, -int (*apply) (u32 sid, -struct context *context, -void *args), -void *args); +int sidtab_convert (struct sidtab *s,struct sidtab *news, +int (*apply) (u32 sid, +struct context *context, +void *args), +void *args); int sidtab_context_to_sid(struct sidtab *s, struct context *context,@@ -50,7 +50,6 @@ int sidtab_context_to_sid(struct sidtab *s,void sidtab_hash_eval (struct sidtab *h,char *tag);void sidtab_destroy (struct sidtab *s);void sidtab_set (struct sidtab *dst,struct sidtab *src); -void sidtab_shutdown (struct sidtab *s);#endif /* _SS_SIDTAB_H_ */
Re: [RFC PATCH 1/3] selinux: refactor sidtab conversion
In reply to Ondrej MosnacekQuoted 10 lines · Ondrej Mosnacek
This is a purely cosmetic change that encapsulates the three-step sidtab conversion logic (shutdown -> clone -> map) into a single function defined in
sidtab.c(as opposed toservices.c).Signed-off-by Ondrej Mosnacek <[email protected]>--- security/selinux/ss/services.c | 22 +-------------- security/selinux/ss/sidtab.c | 50 ++++++++++++++++++++++++---------- security/selinux/ss/sidtab.h | 11 ++++---- 3 files changed, 42 insertions(+), 41 deletions(-)
Merged into selinux/next with some whitespace fixes (inherited from code you cut n' pasted). Please remember to run your patches through scripts/checkpatch.pl before submission.
Signature · 2 lines
paul moore www.paul-moore.com
Re: [RFC PATCH 1/3] selinux: refactor sidtab conversion
Quoted 12 lines · Paul Moore
Quoted 10 lines · Ondrej Mosnacek
This is a purely cosmetic change that encapsulates the three-step sidtab conversion logic (shutdown -> clone -> map) into a single function defined in
sidtab.c(as opposed toservices.c).Signed-off-by Ondrej Mosnacek <[email protected]>--- security/selinux/ss/services.c | 22 +-------------- security/selinux/ss/sidtab.c | 50 ++++++++++++++++++++++++---------- security/selinux/ss/sidtab.h | 11 ++++---- 3 files changed, 42 insertions(+), 41 deletions(-)
Quoted 3 lines
Merged into selinux/next with some whitespace fixes (inherited from code you cut n' pasted). Please remember to run your patches through
scripts/checkpatch.plbefore submission.
Damn, I still haven't set up that commit hook... Done now, seems to work fine. Sorry for not having done that sooner and thanks for nagging me about it :)
Signature · 3 lines
Ondrej Mosnacek <omosnace at redhat dot com> Associate Software Engineer, Security Technologies Red Hat, Inc.
RFC PATCH 2/3 selinux: use separate table for initial SID lookup
In reply to Ondrej MosnacekThis patch is non-functional and moves handling of initial SIDs into a separate table. Note that the SIDs stored in the main table are now shifted by SECINITSID_NUM and converted to/from the actual SIDs transparently by helper functions.
This change doesn't make much sense on its own, but it simplifies further sidtab overhaul in a succeeding patch.
--- security/selinux/ss/policydb.c | 10 ++- security/selinux/ss/services.c | 88 ++++++++++-------- security/selinux/ss/services.h | 2 +- security/selinux/ss/sidtab.c | 158 +++++++++++++++++++-------------- security/selinux/ss/sidtab.h | 14 +-- 5 files changed, 162 insertions(+), 110 deletions(-)
security/selinux/ss/policydb.c +9 -1
@@ -909,13 +909,21 @@ int policydb_load_isids(struct policydb *p, struct sidtab *s)if (!c->context[0 ].user) {pr_err ("SELinux: SID %s was never defined.\n" , c->u.name); +sidtab_destroy (s); + goto out; + } +if (c->sid[0 ] >SECINITSID_NUM ) { +pr_err ("SELinux: Initial SID %s out of range.\n" , + c->u.name); +sidtab_destroy (s); goto out; } - rc =sidtab_insert (s, c->sid[0 ], &c->context[0 ]); + rc =sidtab_set_initial (s, c->sid[0 ], &c->context[0 ]);if (rc) {pr_err ("SELinux: unable to load initial SID %s.\n" , c->u.name); +sidtab_destroy (s); goto out; } }
security/selinux/ss/services.c +50 -38
299 lines not on this page · raw message
security/selinux/ss/services.h +1 -1
@@ -24,7 +24,7 @@ struct selinux_map { };struct selinux_ss { -struct sidtab sidtab; +struct sidtab *sidtab;struct policydb policydb;rwlock_t policy_rwlock;u32 latest_granting;
security/selinux/ss/sidtab.c +93 -65
255 lines not on this page · raw message
security/selinux/ss/sidtab.h +9 -5
@@ -22,6 +22,11 @@ struct sidtab_node {#define SIDTAB_SIZE SIDTAB_HASH_BUCKETS +struct sidtab_isid_entry { +int set; +struct context context; +}; +struct sidtab {struct sidtab_node **htable;unsigned int nel;/* number of elements */
@@ -30,10 +35,12 @@ struct sidtab {#define SIDTAB_CACHE_LEN 3struct sidtab_node *cache[SIDTAB_CACHE_LEN ];spinlock_t lock; + +struct sidtab_isid_entry isids[SECINITSID_NUM +1 ]; };int sidtab_init (struct sidtab *s); -int sidtab_insert (struct sidtab *s,u32 sid,struct context *context); +int sidtab_set_initial (struct sidtab *s,u32 sid,struct context *context);struct context *sidtab_search (struct sidtab *s,u32 sid);struct context *sidtab_search_force (struct sidtab *s,u32 sid);
@@ -43,13 +50,10 @@ int sidtab_convert(struct sidtab *s, struct sidtab *news, void *args), void *args); -int sidtab_context_to_sid (struct sidtab *s, -struct context *context, -u32 *sid); +int sidtab_context_to_sid (struct sidtab *s,struct context *context,u32 *sid);void sidtab_hash_eval (struct sidtab *h,char *tag);void sidtab_destroy (struct sidtab *s); -void sidtab_set (struct sidtab *dst,struct sidtab *src);#endif /* _SS_SIDTAB_H_ */
Signature · 1 line
2.17.2
Re: [RFC PATCH 2/3] selinux: use separate table for initial SID lookup
Quoted 4 lines · Ondrej Mosnacek
This patch is non-functional and moves handling of initial SIDs into a separate table. Note that the SIDs stored in the main table are now shifted by
SECINITSID_NUMand converted to/from the actual SIDs transparently by helper functions.
When you say "non-functional", you mean it doesn't make any functional changes, right? As opposed to leaving the kernel in a broken intermediate state until your 3rd patch?
I think you need to double check that all references to state->ss->sidtab are preceded by a check of state->initialized since it could be NULL before first policy load and a system could come up with SELinux enabled but never load a policy.
Aren't you still wasting a slot in the initial SIDs array, or did I miss something?
Quoted 699 lines
This change doesn't make much sense on its own, but it simplifies further sidtab overhaul in a succeeding patch.
Signed-off-by Ondrej Mosnacek <[email protected]>--- security/selinux/ss/policydb.c | 10 ++- security/selinux/ss/services.c | 88 ++++++++++-------- security/selinux/ss/services.h | 2 +- security/selinux/ss/sidtab.c | 158 +++++++++++++++++++-------------- security/selinux/ss/sidtab.h | 14 +-- 5 files changed, 162 insertions(+), 110 deletions(-)security/selinux/ss/policydb.c +9
-1@@ -909,13 +909,21 @@ int policydb_load_isids(struct policydb *p, struct sidtab *s)if (!c->context[0 ].user) {pr_err ("SELinux: SID %s was never defined.\n" , c->u.name); +sidtab_destroy (s); + goto out; + } +if (c->sid[0 ] >SECINITSID_NUM ) { +pr_err ("SELinux: Initial SID %s out of range.\n" , + c->u.name); +sidtab_destroy (s); goto out; } - rc =sidtab_insert (s, c->sid[0 ], &c->context[0 ]); + rc =sidtab_set_initial (s, c->sid[0 ], &c->context[0 ]);if (rc) {pr_err ("SELinux: unable to load initial SID %s.\n" , c->u.name); +sidtab_destroy (s); goto out; } }security/selinux/ss/services.c +50
-38@@ -776,7 +776,7 @@ static int security_compute_validatetrans(struct selinux_state *state,read_lock (&state->ss->policy_rwlock); policydb = &state->ss->policydb; - sidtab = &state->ss->sidtab; + sidtab = state->ss->sidtab;if (!user) tclass =unmap_class (&state->ss->map, orig_tclass);@@ -876,7 +876,7 @@ int security_bounded_transition(struct selinux_state *state,read_lock (&state->ss->policy_rwlock); policydb = &state->ss->policydb; - sidtab = &state->ss->sidtab; + sidtab = state->ss->sidtab; rc = -EINVAL ; old_context =sidtab_search (sidtab, old_sid);@@ -1034,7 +1034,7 @@ void security_compute_xperms_decision(struct selinux_state *state, goto allow; policydb = &state->ss->policydb; - sidtab = &state->ss->sidtab; + sidtab = state->ss->sidtab; scontext =sidtab_search (sidtab, ssid);if (!scontext) {@@ -1123,7 +1123,7 @@ void security_compute_av(struct selinux_state *state, goto allow; policydb = &state->ss->policydb; - sidtab = &state->ss->sidtab; + sidtab = state->ss->sidtab; scontext =sidtab_search (sidtab, ssid);if (!scontext) {@@ -1177,7 +1177,7 @@ void security_compute_av_user(struct selinux_state *state, goto allow; policydb = &state->ss->policydb; - sidtab = &state->ss->sidtab; + sidtab = state->ss->sidtab; scontext =sidtab_search (sidtab, ssid);if (!scontext) {@@ -1315,7 +1315,7 @@ static int security_sid_to_context_core(struct selinux_state *state, }read_lock (&state->ss->policy_rwlock); policydb = &state->ss->policydb; - sidtab = &state->ss->sidtab; + sidtab = state->ss->sidtab;if (force) context =sidtab_search_force (sidtab, sid);else @@ -1483,7 +1483,7 @@ static int security_context_to_sid_core(struct selinux_state *state, }read_lock (&state->ss->policy_rwlock); policydb = &state->ss->policydb; - sidtab = &state->ss->sidtab; + sidtab = state->ss->sidtab; rc =string_to_context_struct (policydb, sidtab, scontext2, &context, def_sid);if (rc == -EINVAL && force) {@@ -1668,7 +1668,7 @@ static int security_compute_sid(struct selinux_state *state, } policydb = &state->ss->policydb; - sidtab = &state->ss->sidtab; + sidtab = state->ss->sidtab; scontext =sidtab_search (sidtab, ssid);if (!scontext) {@@ -1925,10 +1925,7 @@ static int convert_context(u32 key,struct user_datum *usrdatum;char *s;u32 len; -int rc =0 ; - -if (key <=SECINITSID_NUM ) - goto out; +int rc; args = p;@@ -2090,9 +2087,8 @@ static int security_preserve_bools(struct selinux_state *state,int security_load_policy (struct selinux_state *state,void *data,size_t len) {struct policydb *policydb; -struct sidtab *sidtab; +struct sidtab *oldsidtab, *newsidtab;struct policydb *oldpolicydb, *newpolicydb; -struct sidtab oldsidtab, newsidtab;struct selinux_mapping *oldmapping;struct selinux_map newmap;struct convert_context_args args;@@ -2108,27 +2104,37 @@ int security_load_policy(struct selinux_state *state, void *data, size_t len) newpolicydb = oldpolicydb +1 ; policydb = &state->ss->policydb; - sidtab = &state->ss->sidtab; + + newsidtab =kmalloc (sizeof (*newsidtab),GFP_KERNEL ); +if (!newsidtab) { + rc = -ENOMEM ; + goto out; + }if (!state->initialized) { rc =policydb_read (policydb, fp); -if (rc) +if (rc) { +kfree (newsidtab); goto out; + } policydb->len = len; rc =selinux_set_mapping (policydb, secclass_map, &state->ss->map);if (rc) { +kfree (newsidtab);policydb_destroy (policydb); goto out; } - rc =policydb_load_isids (policydb, sidtab); + rc =policydb_load_isids (policydb, newsidtab);if (rc) { +kfree (newsidtab);policydb_destroy (policydb); goto out; } + state->ss->sidtab = newsidtab;security_load_policycaps (state); state->initialized =1 ; seqno = ++state->ss->latest_granting;@@ -2141,13 +2147,17 @@ int security_load_policy(struct selinux_state *state, void *data, size_t len) goto out; } + oldsidtab = state->ss->sidtab; +#if 0 -sidtab_hash_eval (sidtab,"sids" ); +sidtab_hash_eval (oldsidtab,"sids" );#endif rc =policydb_read (newpolicydb, fp); -if (rc) +if (rc) { +kfree (newsidtab); goto out; + } newpolicydb->len = len;/* If switching between different policy types, log MLS status */ @@ -2156,10 +2166,11 @@ int security_load_policy(struct selinux_state *state, void *data, size_t len)else if (!policydb->mls_enabled && newpolicydb->mls_enabled)pr_info ("SELinux: Enabling MLS support...\n" ); - rc =policydb_load_isids (newpolicydb, &newsidtab); + rc =policydb_load_isids (newpolicydb, newsidtab);if (rc) {pr_err ("SELinux: unable to load the initial SIDs\n" );policydb_destroy (newpolicydb); +kfree (newsidtab); goto out; }@@ -2180,7 +2191,7 @@ int security_load_policy(struct selinux_state *state, void *data, size_t len) args.state = state; args.oldp = policydb; args.newp = newpolicydb; - rc =sidtab_convert (sidtab, &newsidtab, convert_context, &args); + rc =sidtab_convert (oldsidtab, newsidtab, convert_context, &args);if (rc) {pr_err ("SELinux: unable to convert the internal" " representation of contexts in the new SID"@@ -2190,12 +2201,11 @@ int security_load_policy(struct selinux_state *state, void *data, size_t len)/* Save the old policydb and SID table to free later. */ memcpy (oldpolicydb, policydb,sizeof (*policydb)); -sidtab_set (&oldsidtab, sidtab);/* Install the new policydb and SID table. */ write_lock_irq (&state->ss->policy_rwlock);memcpy (policydb, newpolicydb,sizeof (*policydb)); -sidtab_set (sidtab, &newsidtab); + state->ss->sidtab = newsidtab;security_load_policycaps (state); oldmapping = state->ss->map.mapping; state->ss->map.mapping = newmap.mapping;@@ -2205,7 +2215,8 @@ int security_load_policy(struct selinux_state *state, void *data, size_t len)/* Free the old policydb and SID table. */ policydb_destroy (oldpolicydb); -sidtab_destroy (&oldsidtab); +sidtab_destroy (oldsidtab); +kfree (oldsidtab);kfree (oldmapping);avc_ss_reset (state->avc, seqno);@@ -2219,7 +2230,8 @@ int security_load_policy(struct selinux_state *state, void *data, size_t len) err:kfree (newmap.mapping); -sidtab_destroy (&newsidtab); +sidtab_destroy (newsidtab); +kfree (newsidtab);policydb_destroy (newpolicydb); out:@@ -2256,7 +2268,7 @@ int security_port_sid(struct selinux_state *state,read_lock (&state->ss->policy_rwlock); policydb = &state->ss->policydb; - sidtab = &state->ss->sidtab; + sidtab = state->ss->sidtab; c = policydb->ocontexts[OCON_PORT ];while (c) {@@ -2302,7 +2314,7 @@ int security_ib_pkey_sid(struct selinux_state *state,read_lock (&state->ss->policy_rwlock); policydb = &state->ss->policydb; - sidtab = &state->ss->sidtab; + sidtab = state->ss->sidtab; c = policydb->ocontexts[OCON_IBPKEY ];while (c) {@@ -2348,7 +2360,7 @@ int security_ib_endport_sid(struct selinux_state *state,read_lock (&state->ss->policy_rwlock); policydb = &state->ss->policydb; - sidtab = &state->ss->sidtab; + sidtab = state->ss->sidtab; c = policydb->ocontexts[OCON_IBENDPORT ];while (c) {@@ -2394,7 +2406,7 @@ int security_netif_sid(struct selinux_state *state,read_lock (&state->ss->policy_rwlock); policydb = &state->ss->policydb; - sidtab = &state->ss->sidtab; + sidtab = state->ss->sidtab; c = policydb->ocontexts[OCON_NETIF ];while (c) {@@ -2459,7 +2471,7 @@ int security_node_sid(struct selinux_state *state,read_lock (&state->ss->policy_rwlock); policydb = &state->ss->policydb; - sidtab = &state->ss->sidtab; + sidtab = state->ss->sidtab;switch (domain) {case AF_INET : {@@ -2559,7 +2571,7 @@ int security_get_user_sids(struct selinux_state *state,read_lock (&state->ss->policy_rwlock); policydb = &state->ss->policydb; - sidtab = &state->ss->sidtab; + sidtab = state->ss->sidtab;context_init (&usercon);@@ -2661,7 +2673,7 @@ static inline int __security_genfs_sid(struct selinux_state *state, u32 *sid) {struct policydb *policydb = &state->ss->policydb; -struct sidtab *sidtab = &state->ss->sidtab; +struct sidtab *sidtab = state->ss->sidtab;int len;u16 sclass;struct genfs *genfs;@@ -2747,7 +2759,7 @@ int security_fs_use(struct selinux_state *state, struct super_block *sb)read_lock (&state->ss->policy_rwlock); policydb = &state->ss->policydb; - sidtab = &state->ss->sidtab; + sidtab = state->ss->sidtab; c = policydb->ocontexts[OCON_FSUSE ];while (c) {@@ -2953,7 +2965,7 @@ int security_sid_mls_copy(struct selinux_state *state, u32 sid, u32 mls_sid, u32 *new_sid) { struct policydb *policydb = &state->ss->policydb; -struct sidtab *sidtab = &state->ss->sidtab; +struct sidtab *sidtab = state->ss->sidtab;struct context *context1;struct context *context2;struct context newcon;@@ -3044,7 +3056,7 @@ int security_net_peersid_resolve(struct selinux_state *state, u32 *peer_sid) {struct policydb *policydb = &state->ss->policydb; -struct sidtab *sidtab = &state->ss->sidtab; +struct sidtab *sidtab = state->ss->sidtab;int rc;struct context *nlbl_ctx;struct context *xfrm_ctx;@@ -3405,7 +3417,7 @@ int selinux_audit_rule_match(u32 sid, u32 field, u32 op, void *vrule, goto out; } - ctxt =sidtab_search (&state->ss->sidtab, sid); + ctxt =sidtab_search (state->ss->sidtab, sid);if (unlikely (!ctxt)) {WARN_ONCE (1 ,"selinux_audit_rule_match: unrecognized SID %d\n" , sid);@@ -3568,7 +3580,7 @@ int security_netlbl_secattr_to_sid(struct selinux_state *state, u32 *sid) {struct policydb *policydb = &state->ss->policydb; -struct sidtab *sidtab = &state->ss->sidtab; +struct sidtab *sidtab = state->ss->sidtab;int rc;struct context *ctx;struct context ctx_new;@@ -3646,7 +3658,7 @@ int security_netlbl_sid_to_secattr(struct selinux_state *state,read_lock (&state->ss->policy_rwlock); rc = -ENOENT ; - ctx =sidtab_search (&state->ss->sidtab, sid); + ctx =sidtab_search (state->ss->sidtab, sid);if (ctx ==NULL ) goto out;security/selinux/ss/services.h +1
-1@@ -24,7 +24,7 @@ struct selinux_map { };struct selinux_ss { -struct sidtab sidtab; +struct sidtab *sidtab;struct policydb policydb;rwlock_t policy_rwlock;u32 latest_granting;security/selinux/ss/sidtab.c +93
-65@@ -22,16 +22,24 @@ int sidtab_init(struct sidtab *s) s->htable =kmalloc_array (SIDTAB_SIZE ,sizeof (*s->htable),GFP_ATOMIC );if (!s->htable)return -ENOMEM ; + +for (i =0 ; i <=SECINITSID_NUM ; i++) + s->isids[i].set =0 ; +for (i =0 ; i <SIDTAB_SIZE ; i++) s->htable[i] =NULL ; + +for (i =0 ; i <SIDTAB_CACHE_LEN ; i++) + s->cache[i] =NULL ; + s->nel =0 ; - s->next_sid =1 ; + s->next_sid =0 ; s->shutdown =0 ;spin_lock_init (&s->lock);return 0 ; } -int sidtab_insert (struct sidtab *s,u32 sid,struct context *context) +static int sidtab_insert (struct sidtab *s,u32 sid,struct context *context) {int hvalue;struct sidtab_node *prev, *cur, *newnode;@@ -76,34 +84,53 @@ int sidtab_insert(struct sidtab *s, u32 sid, struct context *context)return 0 ; } -static struct context *sidtab_search_core (struct sidtab *s,u32 sid,int force) +int sidtab_set_initial (struct sidtab *s,u32 sid,struct context *context) +{ +struct sidtab_isid_entry *entry = &s->isids[sid]; +int rc =context_cpy (&entry->context, context); +if (rc) +return rc; + + entry->set =1 ; +return 0 ; +} + +static struct context *sidtab_lookup (struct sidtab *s,u32 sid) {int hvalue;struct sidtab_node *cur; -if (!s) -return NULL ; - hvalue =SIDTAB_HASH (sid); cur = s->htable[hvalue];while (cur && sid > cur->sid) cur = cur->next; -if (force && cur && sid == cur->sid && cur->context.len) -return &cur->context; +if (!cur || sid != cur->sid) +return NULL ; -if (!cur || sid != cur->sid || cur->context.len) { -/* Remap invalid SIDs to the unlabeled SID. */ - sid =SECINITSID_UNLABELED ; - hvalue =SIDTAB_HASH (sid); - cur = s->htable[hvalue]; -while (cur && sid > cur->sid) - cur = cur->next; -if (!cur || sid != cur->sid) -return NULL ; +return &cur->context; +} + +static struct context *sidtab_search_core (struct sidtab *s,u32 sid,int force) +{ +struct context *context; +struct sidtab_isid_entry *entry; + +if (!s) +return NULL ; + +if (sid >SECINITSID_NUM ) { +u32 index = sid - (SECINITSID_NUM +1 ); + context =sidtab_lookup (s, index); + }else { + entry = &s->isids[sid]; + context = entry->set ? &entry->context :NULL ; } +if (context && (!context->len || force)) +return context; -return &cur->context; + entry = &s->isids[SECINITSID_UNLABELED ]; +return entry->set ? &entry->context :NULL ; }struct context *sidtab_search (struct sidtab *s,u32 sid)@@ -145,11 +172,7 @@ out:static int clone_sid (u32 sid,struct context *context,void *arg) {struct sidtab *s = arg; - -if (sid >SECINITSID_NUM ) -return sidtab_insert (s, sid, context); -else -return 0 ; +return sidtab_insert (s, sid, context); } int sidtab_convert(struct sidtab *s, struct sidtab *news,@@ -183,8 +206,8 @@ static void sidtab_update_cache(struct sidtab *s, struct sidtab_node *n, int loc s->cache[0 ] = n; } -static inline u32 sidtab_search_context (struct sidtab *s, -struct context *context) +static inline int sidtab_search_context (struct sidtab *s, +struct context *context,u32 *sid) {int i;struct sidtab_node *cur;@@ -194,15 +217,17 @@ static inline u32 sidtab_search_context(struct sidtab *s,while (cur) {if (context_cmp (&cur->context, context)) {sidtab_update_cache (s, cur,SIDTAB_CACHE_LEN -1 ); -return cur->sid; + *sid = cur->sid; +return 0 ; } cur = cur->next; } } -return 0 ; +return -ENOENT ; } -static inline u32 sidtab_search_cache (struct sidtab *s,struct context *context) +static inline int sidtab_search_cache (struct sidtab *s,struct context *context, +u32 *sid) {int i;struct sidtab_node *node;@@ -210,54 +235,67 @@ static inline u32 sidtab_search_cache(struct sidtab *s, struct context *context)for (i =0 ; i <SIDTAB_CACHE_LEN ; i++) { node = s->cache[i];if (unlikely (!node)) -return 0 ; +return -ENOENT ;if (context_cmp (&node->context, context)) {sidtab_update_cache (s, node, i); -return node->sid; + *sid = node->sid; +return 0 ; } } -return 0 ; +return -ENOENT ; } -int sidtab_context_to_sid (struct sidtab *s, -struct context *context, -u32 *out_sid) +static int sidtab_reverse_lookup (struct sidtab *s,struct context *context, +u32 *sid) { -u32 sid; -int ret =0 ; +int ret;unsigned long flags; - *out_sid =SECSID_NULL ; - - sid =sidtab_search_cache (s, context); -if (!sid) - sid =sidtab_search_context (s, context); -if (!sid) { + ret =sidtab_search_cache (s, context, sid); +if (ret) + ret =sidtab_search_context (s, context, sid); +if (ret) {spin_lock_irqsave (&s->lock, flags);/* Rescan now that we hold the lock. */ - sid =sidtab_search_context (s, context); -if (sid) + ret =sidtab_search_context (s, context, sid); +if (!ret) goto unlock_out;/* No SID exists for the context. Allocate a new one. */ -if (s->next_sid ==UINT_MAX || s->shutdown) { +if (s->next_sid == (UINT_MAX -SECINITSID_NUM -1 ) || s->shutdown) { ret = -ENOMEM ; goto unlock_out; } - sid = s->next_sid++; + *sid = s->next_sid++;if (context->len)pr_info ("SELinux: Context %s is not valid (left unmapped).\n" , context->str); - ret =sidtab_insert (s, sid, context); + ret =sidtab_insert (s, *sid, context);if (ret) s->next_sid--; unlock_out:spin_unlock_irqrestore (&s->lock, flags); } -if (ret) -return ret; +return ret; +} + +int sidtab_context_to_sid (struct sidtab *s,struct context *context,u32 *sid) +{ +int rc; +u32 i; + +for (i =0 ; i <=SECINITSID_NUM ; i++) { +struct sidtab_isid_entry *entry = &s->isids[i]; +if (entry->set &&context_cmp (context, &entry->context)) { + *sid = i; +return 0 ; + } + } - *out_sid = sid; + rc =sidtab_reverse_lookup (s, context, sid); +if (rc) +return rc; + *sid +=SECINITSID_NUM +1 ;return 0 ; }@@ -296,6 +334,11 @@ void sidtab_destroy(struct sidtab *s)if (!s)return ; +for (i =0 ; i <=SECINITSID_NUM ; i++) +if (s->isids[i].set) +context_destroy (&s->isids[i].context); + +for (i =0 ; i <SIDTAB_SIZE ; i++) { cur = s->htable[i];while (cur) {@@ -311,18 +354,3 @@ void sidtab_destroy(struct sidtab *s) s->nel =0 ; s->next_sid =1 ; } - -void sidtab_set (struct sidtab *dst,struct sidtab *src) -{ -unsigned long flags; -int i; - -spin_lock_irqsave (&src->lock, flags); - dst->htable = src->htable; - dst->nel = src->nel; - dst->next_sid = src->next_sid; - dst->shutdown =0 ; -for (i =0 ; i <SIDTAB_CACHE_LEN ; i++) - dst->cache[i] =NULL ; -spin_unlock_irqrestore (&src->lock, flags); -}security/selinux/ss/sidtab.h +9
-5@@ -22,6 +22,11 @@ struct sidtab_node {#define SIDTAB_SIZE SIDTAB_HASH_BUCKETS +struct sidtab_isid_entry { +int set; +struct context context; +}; +struct sidtab {struct sidtab_node **htable;unsigned int nel;/* number of elements */ @@ -30,10 +35,12 @@ struct sidtab {#define SIDTAB_CACHE_LEN 3struct sidtab_node *cache[SIDTAB_CACHE_LEN ];spinlock_t lock; + +struct sidtab_isid_entry isids[SECINITSID_NUM +1 ]; };int sidtab_init (struct sidtab *s); -int sidtab_insert (struct sidtab *s,u32 sid,struct context *context); +int sidtab_set_initial (struct sidtab *s,u32 sid,struct context *context);struct context *sidtab_search (struct sidtab *s,u32 sid);struct context *sidtab_search_force (struct sidtab *s,u32 sid);@@ -43,13 +50,10 @@ int sidtab_convert(struct sidtab *s, struct sidtab *news, void *args), void *args); -int sidtab_context_to_sid (struct sidtab *s, -struct context *context, -u32 *sid); +int sidtab_context_to_sid (struct sidtab *s,struct context *context,u32 *sid);void sidtab_hash_eval (struct sidtab *h,char *tag);void sidtab_destroy (struct sidtab *s); -void sidtab_set (struct sidtab *dst,struct sidtab *src);#endif /* _SS_SIDTAB_H_ */
Re: [RFC PATCH 2/3] selinux: use separate table for initial SID lookup
Quoted 9 lines · Stephen Smalley
Quoted 4 lines · Ondrej Mosnacek
This patch is non-functional and moves handling of initial SIDs into a separate table. Note that the SIDs stored in the main table are now shifted by
SECINITSID_NUMand converted to/from the actual SIDs transparently by helper functions.When you say "non-functional", you mean it doesn't make any functional changes, right? As opposed to leaving the kernel in a broken intermediate state until your 3rd patch?
I mean it *should* be non-functional on its own, unless I made a mistake. I admit I didn't do very much testing on this patch, but I at least checked that I can boot the kernel, load a policy and that reported file contexts make sense.
Quoted 5 lines
I think you need to double check that all references to
state->ss->sidtabare preceded by a check ofstate->initializedsince it could be NULL before first policy load and a system could come up with SELinux enabled but never load a policy.
Well, the original code does not initialize the sidtab field with sidtab_init() either so state->ss->sidtab.htable would be NULL (or some garbage pointer) after boot and any use of sidtab would cause a GPF anyway.
Looking at the places that reference the sidtab (which are all highlighted in the patch because of the switch to pointer type in the struct selinux_ss definition), these don't seem to check for state->initialized:
- security_port_sid
- security_ib_pkey_sid
- security_ib_endport_sid
- security_netif_sid
- security_node_sid
- security_sid_mls_copy, called from:
- selinux_socket_unix_stream_connect (avc_has_perm is called before)
- selinux_conn_sid, called from:
- selinux_sctp_assoc_request (selinux_policycap_extsockclass is
called before)
- selinux_inet_conn_request
- selinux_ip_postroute (selinux_policycap_netpeer is called before)
- security_net_peersid_resolve, called from:
- selinux_skb_peerlbl_sid, called from:
- selinux_socket_sock_rcv_skb (selinux_policycap_netpeer is called before)
- selinux_socket_getpeersec_dgram
- selinux_sctp_assoc_request (again)
- selinux_inet_conn_request (again)
- selinux_inet_conn_established
- selinux_ip_forward (selinux_policycap_netpeer is called before)
- selinux_ip_postroute (again)
- selinux_audit_rule_match
- security_netlbl_secattr_to_sid, called from:
- selinux_netlbl_sidlookup_cached, called from:
- selinux_netlbl_skbuff_getsid (netlbl_enabled is called before)
- selinux_netlbl_sock_rcv_skb (netlbl_enabled is called before)
- security_netlbl_sid_to_secattr, called from:
- selinux_netlbl_sock_genattr, called from:
- selinux_netlbl_socket_post_create, called from:
- selinux_socket_post_create
- selinux_netlbl_skbuff_setsid, called from:
- selinux_ip_forward (again)
- selinux_netlbl_sctp_assoc_request, called from:
- selinux_sctp_assoc_request (again)
- selinux_netlbl_inet_conn_request, called from:
- selinux_inet_conn_request (again)
I suppose in some (or all?) of these cases state->initialized might be implicitly always 1, but at least in these it wasn't immediately obvious to me.
Either way, such cases would already be buggy before this patch, since they would happily access the policy structure (likely hitting some NULL/invalid pointers) and most likely also dereference the invalid htable pointer in sidtab.
Quoted 3 lines
Aren't you still wasting a slot in the initial SIDs array, or did I miss something?
Yes, I am, because the original code doesn't seem to guard against SECSID_NULL being loaded as initial SID into sidtab. I asked in the other thread whether this is considered a bug, but I didn't get a reply, so I left it sort of bug-to-bug compatible for now... Anyway, it doesn't seem to make much sense to keep that behavior so I will probably just go ahead and add the check to policydb_load_isids() and shrink the table. You can expect it to be resolved in the final patchset.
Quoted 693 lines
Quoted 693 lines
This change doesn't make much sense on its own, but it simplifies further sidtab overhaul in a succeeding patch.
Signed-off-by Ondrej Mosnacek <[email protected]>--- security/selinux/ss/policydb.c | 10 ++- security/selinux/ss/services.c | 88 ++++++++++-------- security/selinux/ss/services.h | 2 +- security/selinux/ss/sidtab.c | 158 +++++++++++++++++++-------------- security/selinux/ss/sidtab.h | 14 +-- 5 files changed, 162 insertions(+), 110 deletions(-)security/selinux/ss/policydb.c +9
-1@@ -909,13 +909,21 @@ int policydb_load_isids(struct policydb *p, struct sidtab *s)if (!c->context[0 ].user) {pr_err ("SELinux: SID %s was never defined.\n" , c->u.name); +sidtab_destroy (s); + goto out; + } +if (c->sid[0 ] >SECINITSID_NUM ) { +pr_err ("SELinux: Initial SID %s out of range.\n" , + c->u.name); +sidtab_destroy (s); goto out; } - rc =sidtab_insert (s, c->sid[0 ], &c->context[0 ]); + rc =sidtab_set_initial (s, c->sid[0 ], &c->context[0 ]);if (rc) {pr_err ("SELinux: unable to load initial SID %s.\n" , c->u.name); +sidtab_destroy (s); goto out; } }security/selinux/ss/services.c +50
-38@@ -776,7 +776,7 @@ static int security_compute_validatetrans(struct selinux_state *state,read_lock (&state->ss->policy_rwlock); policydb = &state->ss->policydb; - sidtab = &state->ss->sidtab; + sidtab = state->ss->sidtab;if (!user) tclass =unmap_class (&state->ss->map, orig_tclass);@@ -876,7 +876,7 @@ int security_bounded_transition(struct selinux_state *state,read_lock (&state->ss->policy_rwlock); policydb = &state->ss->policydb; - sidtab = &state->ss->sidtab; + sidtab = state->ss->sidtab; rc = -EINVAL ; old_context =sidtab_search (sidtab, old_sid);@@ -1034,7 +1034,7 @@ void security_compute_xperms_decision(struct selinux_state *state, goto allow; policydb = &state->ss->policydb; - sidtab = &state->ss->sidtab; + sidtab = state->ss->sidtab; scontext =sidtab_search (sidtab, ssid);if (!scontext) {@@ -1123,7 +1123,7 @@ void security_compute_av(struct selinux_state *state, goto allow; policydb = &state->ss->policydb; - sidtab = &state->ss->sidtab; + sidtab = state->ss->sidtab; scontext =sidtab_search (sidtab, ssid);if (!scontext) {@@ -1177,7 +1177,7 @@ void security_compute_av_user(struct selinux_state *state, goto allow; policydb = &state->ss->policydb; - sidtab = &state->ss->sidtab; + sidtab = state->ss->sidtab; scontext =sidtab_search (sidtab, ssid);if (!scontext) {@@ -1315,7 +1315,7 @@ static int security_sid_to_context_core(struct selinux_state *state, }read_lock (&state->ss->policy_rwlock); policydb = &state->ss->policydb; - sidtab = &state->ss->sidtab; + sidtab = state->ss->sidtab;if (force) context =sidtab_search_force (sidtab, sid);else @@ -1483,7 +1483,7 @@ static int security_context_to_sid_core(struct selinux_state *state, }read_lock (&state->ss->policy_rwlock); policydb = &state->ss->policydb; - sidtab = &state->ss->sidtab; + sidtab = state->ss->sidtab; rc =string_to_context_struct (policydb, sidtab, scontext2, &context, def_sid);if (rc == -EINVAL && force) {@@ -1668,7 +1668,7 @@ static int security_compute_sid(struct selinux_state *state, } policydb = &state->ss->policydb; - sidtab = &state->ss->sidtab; + sidtab = state->ss->sidtab; scontext =sidtab_search (sidtab, ssid);if (!scontext) {@@ -1925,10 +1925,7 @@ static int convert_context(u32 key,struct user_datum *usrdatum;char *s;u32 len; -int rc =0 ; - -if (key <=SECINITSID_NUM ) - goto out; +int rc; args = p;@@ -2090,9 +2087,8 @@ static int security_preserve_bools(struct selinux_state *state,int security_load_policy (struct selinux_state *state,void *data,size_t len) {struct policydb *policydb; -struct sidtab *sidtab; +struct sidtab *oldsidtab, *newsidtab;struct policydb *oldpolicydb, *newpolicydb; -struct sidtab oldsidtab, newsidtab;struct selinux_mapping *oldmapping;struct selinux_map newmap;struct convert_context_args args;@@ -2108,27 +2104,37 @@ int security_load_policy(struct selinux_state *state, void *data, size_t len) newpolicydb = oldpolicydb +1 ; policydb = &state->ss->policydb; - sidtab = &state->ss->sidtab; + + newsidtab =kmalloc (sizeof (*newsidtab),GFP_KERNEL ); +if (!newsidtab) { + rc = -ENOMEM ; + goto out; + }if (!state->initialized) { rc =policydb_read (policydb, fp); -if (rc) +if (rc) { +kfree (newsidtab); goto out; + } policydb->len = len; rc =selinux_set_mapping (policydb, secclass_map, &state->ss->map);if (rc) { +kfree (newsidtab);policydb_destroy (policydb); goto out; } - rc =policydb_load_isids (policydb, sidtab); + rc =policydb_load_isids (policydb, newsidtab);if (rc) { +kfree (newsidtab);policydb_destroy (policydb); goto out; } + state->ss->sidtab = newsidtab;security_load_policycaps (state); state->initialized =1 ; seqno = ++state->ss->latest_granting;@@ -2141,13 +2147,17 @@ int security_load_policy(struct selinux_state *state, void *data, size_t len) goto out; } + oldsidtab = state->ss->sidtab; +#if 0 -sidtab_hash_eval (sidtab,"sids" ); +sidtab_hash_eval (oldsidtab,"sids" );#endif rc =policydb_read (newpolicydb, fp); -if (rc) +if (rc) { +kfree (newsidtab); goto out; + } newpolicydb->len = len;/* If switching between different policy types, log MLS status */ @@ -2156,10 +2166,11 @@ int security_load_policy(struct selinux_state *state, void *data, size_t len)else if (!policydb->mls_enabled && newpolicydb->mls_enabled)pr_info ("SELinux: Enabling MLS support...\n" ); - rc =policydb_load_isids (newpolicydb, &newsidtab); + rc =policydb_load_isids (newpolicydb, newsidtab);if (rc) {pr_err ("SELinux: unable to load the initial SIDs\n" );policydb_destroy (newpolicydb); +kfree (newsidtab); goto out; }@@ -2180,7 +2191,7 @@ int security_load_policy(struct selinux_state *state, void *data, size_t len) args.state = state; args.oldp = policydb; args.newp = newpolicydb; - rc =sidtab_convert (sidtab, &newsidtab, convert_context, &args); + rc =sidtab_convert (oldsidtab, newsidtab, convert_context, &args);if (rc) {pr_err ("SELinux: unable to convert the internal" " representation of contexts in the new SID"@@ -2190,12 +2201,11 @@ int security_load_policy(struct selinux_state *state, void *data, size_t len)/* Save the old policydb and SID table to free later. */ memcpy (oldpolicydb, policydb,sizeof (*policydb)); -sidtab_set (&oldsidtab, sidtab);/* Install the new policydb and SID table. */ write_lock_irq (&state->ss->policy_rwlock);memcpy (policydb, newpolicydb,sizeof (*policydb)); -sidtab_set (sidtab, &newsidtab); + state->ss->sidtab = newsidtab;security_load_policycaps (state); oldmapping = state->ss->map.mapping; state->ss->map.mapping = newmap.mapping;@@ -2205,7 +2215,8 @@ int security_load_policy(struct selinux_state *state, void *data, size_t len)/* Free the old policydb and SID table. */ policydb_destroy (oldpolicydb); -sidtab_destroy (&oldsidtab); +sidtab_destroy (oldsidtab); +kfree (oldsidtab);kfree (oldmapping);avc_ss_reset (state->avc, seqno);@@ -2219,7 +2230,8 @@ int security_load_policy(struct selinux_state *state, void *data, size_t len) err:kfree (newmap.mapping); -sidtab_destroy (&newsidtab); +sidtab_destroy (newsidtab); +kfree (newsidtab);policydb_destroy (newpolicydb); out:@@ -2256,7 +2268,7 @@ int security_port_sid(struct selinux_state *state,read_lock (&state->ss->policy_rwlock); policydb = &state->ss->policydb; - sidtab = &state->ss->sidtab; + sidtab = state->ss->sidtab; c = policydb->ocontexts[OCON_PORT ];while (c) {@@ -2302,7 +2314,7 @@ int security_ib_pkey_sid(struct selinux_state *state,read_lock (&state->ss->policy_rwlock); policydb = &state->ss->policydb; - sidtab = &state->ss->sidtab; + sidtab = state->ss->sidtab; c = policydb->ocontexts[OCON_IBPKEY ];while (c) {@@ -2348,7 +2360,7 @@ int security_ib_endport_sid(struct selinux_state *state,read_lock (&state->ss->policy_rwlock); policydb = &state->ss->policydb; - sidtab = &state->ss->sidtab; + sidtab = state->ss->sidtab; c = policydb->ocontexts[OCON_IBENDPORT ];while (c) {@@ -2394,7 +2406,7 @@ int security_netif_sid(struct selinux_state *state,read_lock (&state->ss->policy_rwlock); policydb = &state->ss->policydb; - sidtab = &state->ss->sidtab; + sidtab = state->ss->sidtab; c = policydb->ocontexts[OCON_NETIF ];while (c) {@@ -2459,7 +2471,7 @@ int security_node_sid(struct selinux_state *state,read_lock (&state->ss->policy_rwlock); policydb = &state->ss->policydb; - sidtab = &state->ss->sidtab; + sidtab = state->ss->sidtab;switch (domain) {case AF_INET : {@@ -2559,7 +2571,7 @@ int security_get_user_sids(struct selinux_state *state,read_lock (&state->ss->policy_rwlock); policydb = &state->ss->policydb; - sidtab = &state->ss->sidtab; + sidtab = state->ss->sidtab;context_init (&usercon);@@ -2661,7 +2673,7 @@ static inline int __security_genfs_sid(struct selinux_state *state, u32 *sid) {struct policydb *policydb = &state->ss->policydb; -struct sidtab *sidtab = &state->ss->sidtab; +struct sidtab *sidtab = state->ss->sidtab;int len;u16 sclass;struct genfs *genfs;@@ -2747,7 +2759,7 @@ int security_fs_use(struct selinux_state *state, struct super_block *sb)read_lock (&state->ss->policy_rwlock); policydb = &state->ss->policydb; - sidtab = &state->ss->sidtab; + sidtab = state->ss->sidtab; c = policydb->ocontexts[OCON_FSUSE ];while (c) {@@ -2953,7 +2965,7 @@ int security_sid_mls_copy(struct selinux_state *state, u32 sid, u32 mls_sid, u32 *new_sid) { struct policydb *policydb = &state->ss->policydb; -struct sidtab *sidtab = &state->ss->sidtab; +struct sidtab *sidtab = state->ss->sidtab;struct context *context1;struct context *context2;struct context newcon;@@ -3044,7 +3056,7 @@ int security_net_peersid_resolve(struct selinux_state *state, u32 *peer_sid) {struct policydb *policydb = &state->ss->policydb; -struct sidtab *sidtab = &state->ss->sidtab; +struct sidtab *sidtab = state->ss->sidtab;int rc;struct context *nlbl_ctx;struct context *xfrm_ctx;@@ -3405,7 +3417,7 @@ int selinux_audit_rule_match(u32 sid, u32 field, u32 op, void *vrule, goto out; } - ctxt =sidtab_search (&state->ss->sidtab, sid); + ctxt =sidtab_search (state->ss->sidtab, sid);if (unlikely (!ctxt)) {WARN_ONCE (1 ,"selinux_audit_rule_match: unrecognized SID %d\n" , sid);@@ -3568,7 +3580,7 @@ int security_netlbl_secattr_to_sid(struct selinux_state *state, u32 *sid) {struct policydb *policydb = &state->ss->policydb; -struct sidtab *sidtab = &state->ss->sidtab; +struct sidtab *sidtab = state->ss->sidtab;int rc;struct context *ctx;struct context ctx_new;@@ -3646,7 +3658,7 @@ int security_netlbl_sid_to_secattr(struct selinux_state *state,read_lock (&state->ss->policy_rwlock); rc = -ENOENT ; - ctx =sidtab_search (&state->ss->sidtab, sid); + ctx =sidtab_search (state->ss->sidtab, sid);if (ctx ==NULL ) goto out;security/selinux/ss/services.h +1
-1@@ -24,7 +24,7 @@ struct selinux_map { };struct selinux_ss { -struct sidtab sidtab; +struct sidtab *sidtab;struct policydb policydb;rwlock_t policy_rwlock;u32 latest_granting;security/selinux/ss/sidtab.c +93
-65@@ -22,16 +22,24 @@ int sidtab_init(struct sidtab *s) s->htable =kmalloc_array (SIDTAB_SIZE ,sizeof (*s->htable),GFP_ATOMIC );if (!s->htable)return -ENOMEM ; + +for (i =0 ; i <=SECINITSID_NUM ; i++) + s->isids[i].set =0 ; +for (i =0 ; i <SIDTAB_SIZE ; i++) s->htable[i] =NULL ; + +for (i =0 ; i <SIDTAB_CACHE_LEN ; i++) + s->cache[i] =NULL ; + s->nel =0 ; - s->next_sid =1 ; + s->next_sid =0 ; s->shutdown =0 ;spin_lock_init (&s->lock);return 0 ; } -int sidtab_insert (struct sidtab *s,u32 sid,struct context *context) +static int sidtab_insert (struct sidtab *s,u32 sid,struct context *context) {int hvalue;struct sidtab_node *prev, *cur, *newnode;@@ -76,34 +84,53 @@ int sidtab_insert(struct sidtab *s, u32 sid, struct context *context)return 0 ; } -static struct context *sidtab_search_core (struct sidtab *s,u32 sid,int force) +int sidtab_set_initial (struct sidtab *s,u32 sid,struct context *context) +{ +struct sidtab_isid_entry *entry = &s->isids[sid]; +int rc =context_cpy (&entry->context, context); +if (rc) +return rc; + + entry->set =1 ; +return 0 ; +} + +static struct context *sidtab_lookup (struct sidtab *s,u32 sid) {int hvalue;struct sidtab_node *cur; -if (!s) -return NULL ; - hvalue =SIDTAB_HASH (sid); cur = s->htable[hvalue];while (cur && sid > cur->sid) cur = cur->next; -if (force && cur && sid == cur->sid && cur->context.len) -return &cur->context; +if (!cur || sid != cur->sid) +return NULL ; -if (!cur || sid != cur->sid || cur->context.len) { -/* Remap invalid SIDs to the unlabeled SID. */ - sid =SECINITSID_UNLABELED ; - hvalue =SIDTAB_HASH (sid); - cur = s->htable[hvalue]; -while (cur && sid > cur->sid) - cur = cur->next; -if (!cur || sid != cur->sid) -return NULL ; +return &cur->context; +} + +static struct context *sidtab_search_core (struct sidtab *s,u32 sid,int force) +{ +struct context *context; +struct sidtab_isid_entry *entry; + +if (!s) +return NULL ; + +if (sid >SECINITSID_NUM ) { +u32 index = sid - (SECINITSID_NUM +1 ); + context =sidtab_lookup (s, index); + }else { + entry = &s->isids[sid]; + context = entry->set ? &entry->context :NULL ; } +if (context && (!context->len || force)) +return context; -return &cur->context; + entry = &s->isids[SECINITSID_UNLABELED ]; +return entry->set ? &entry->context :NULL ; }struct context *sidtab_search (struct sidtab *s,u32 sid)@@ -145,11 +172,7 @@ out:static int clone_sid (u32 sid,struct context *context,void *arg) {struct sidtab *s = arg; - -if (sid >SECINITSID_NUM ) -return sidtab_insert (s, sid, context); -else -return 0 ; +return sidtab_insert (s, sid, context); } int sidtab_convert(struct sidtab *s, struct sidtab *news,@@ -183,8 +206,8 @@ static void sidtab_update_cache(struct sidtab *s, struct sidtab_node *n, int loc s->cache[0 ] = n; } -static inline u32 sidtab_search_context (struct sidtab *s, -struct context *context) +static inline int sidtab_search_context (struct sidtab *s, +struct context *context,u32 *sid) {int i;struct sidtab_node *cur;@@ -194,15 +217,17 @@ static inline u32 sidtab_search_context(struct sidtab *s,while (cur) {if (context_cmp (&cur->context, context)) {sidtab_update_cache (s, cur,SIDTAB_CACHE_LEN -1 ); -return cur->sid; + *sid = cur->sid; +return 0 ; } cur = cur->next; } } -return 0 ; +return -ENOENT ; } -static inline u32 sidtab_search_cache (struct sidtab *s,struct context *context) +static inline int sidtab_search_cache (struct sidtab *s,struct context *context, +u32 *sid) {int i;struct sidtab_node *node;@@ -210,54 +235,67 @@ static inline u32 sidtab_search_cache(struct sidtab *s, struct context *context)for (i =0 ; i <SIDTAB_CACHE_LEN ; i++) { node = s->cache[i];if (unlikely (!node)) -return 0 ; +return -ENOENT ;if (context_cmp (&node->context, context)) {sidtab_update_cache (s, node, i); -return node->sid; + *sid = node->sid; +return 0 ; } } -return 0 ; +return -ENOENT ; } -int sidtab_context_to_sid (struct sidtab *s, -struct context *context, -u32 *out_sid) +static int sidtab_reverse_lookup (struct sidtab *s,struct context *context, +u32 *sid) { -u32 sid; -int ret =0 ; +int ret;unsigned long flags; - *out_sid =SECSID_NULL ; - - sid =sidtab_search_cache (s, context); -if (!sid) - sid =sidtab_search_context (s, context); -if (!sid) { + ret =sidtab_search_cache (s, context, sid); +if (ret) + ret =sidtab_search_context (s, context, sid); +if (ret) {spin_lock_irqsave (&s->lock, flags);/* Rescan now that we hold the lock. */ - sid =sidtab_search_context (s, context); -if (sid) + ret =sidtab_search_context (s, context, sid); +if (!ret) goto unlock_out;/* No SID exists for the context. Allocate a new one. */ -if (s->next_sid ==UINT_MAX || s->shutdown) { +if (s->next_sid == (UINT_MAX -SECINITSID_NUM -1 ) || s->shutdown) { ret = -ENOMEM ; goto unlock_out; } - sid = s->next_sid++; + *sid = s->next_sid++;if (context->len)pr_info ("SELinux: Context %s is not valid (left unmapped).\n" , context->str); - ret =sidtab_insert (s, sid, context); + ret =sidtab_insert (s, *sid, context);if (ret) s->next_sid--; unlock_out:spin_unlock_irqrestore (&s->lock, flags); } -if (ret) -return ret; +return ret; +} + +int sidtab_context_to_sid (struct sidtab *s,struct context *context,u32 *sid) +{ +int rc; +u32 i; + +for (i =0 ; i <=SECINITSID_NUM ; i++) { +struct sidtab_isid_entry *entry = &s->isids[i]; +if (entry->set &&context_cmp (context, &entry->context)) { + *sid = i; +return 0 ; + } + } - *out_sid = sid; + rc =sidtab_reverse_lookup (s, context, sid); +if (rc) +return rc; + *sid +=SECINITSID_NUM +1 ;return 0 ; }@@ -296,6 +334,11 @@ void sidtab_destroy(struct sidtab *s)if (!s)return ; +for (i =0 ; i <=SECINITSID_NUM ; i++) +if (s->isids[i].set) +context_destroy (&s->isids[i].context); + +for (i =0 ; i <SIDTAB_SIZE ; i++) { cur = s->htable[i];while (cur) {@@ -311,18 +354,3 @@ void sidtab_destroy(struct sidtab *s) s->nel =0 ; s->next_sid =1 ; } - -void sidtab_set (struct sidtab *dst,struct sidtab *src) -{ -unsigned long flags; -int i; - -spin_lock_irqsave (&src->lock, flags); - dst->htable = src->htable; - dst->nel = src->nel; - dst->next_sid = src->next_sid; - dst->shutdown =0 ; -for (i =0 ; i <SIDTAB_CACHE_LEN ; i++) - dst->cache[i] =NULL ; -spin_unlock_irqrestore (&src->lock, flags); -}security/selinux/ss/sidtab.h +9
-4@@ -22,6 +22,11 @@ struct sidtab_node {#define SIDTAB_SIZE SIDTAB_HASH_BUCKETS +struct sidtab_isid_entry { +int set; +struct context context; +}; +struct sidtab {struct sidtab_node **htable;unsigned int nel;/* number of elements */ @@ -30,10 +35,12 @@ struct sidtab {#define SIDTAB_CACHE_LEN 3struct sidtab_node *cache[SIDTAB_CACHE_LEN ];spinlock_t lock; + +struct sidtab_isid_entry isids[SECINITSID_NUM +1 ]; };int sidtab_init (struct sidtab *s); -int sidtab_insert (struct sidtab *s,u32 sid,struct context *context); +int sidtab_set_initial (struct sidtab *s,u32 sid,struct context *context);struct context *sidtab_search (struct sidtab *s,u32 sid);struct context *sidtab_search_force (struct sidtab *s,u32 sid);@@ -43,13 +50,10 @@ int sidtab_convert(struct sidtab *s, struct sidtab *news, void *args), void *args); -int sidtab_context_to_sid (struct sidtab *s, -struct context *context, -u32 *sid); +int sidtab_context_to_sid (struct sidtab *s,struct context *context,u32 *sid);
Quoted 8 lines
security/selinux/ss/sidtab.h int sidtab_convert(struct sidtab *s, struct sidtab *news, · line 59
Quoted 7 lines
security/selinux/ss/sidtab.h +0
-1void sidtab_hash_eval (struct sidtab *h,char *tag);void sidtab_destroy (struct sidtab *s); -void sidtab_set (struct sidtab *dst,struct sidtab *src);#endif /* _SS_SIDTAB_H_ */
--
Ondrej Mosnacek <omosnace at redhat dot com> Associate Software Engineer, Security Technologies Red Hat, Inc.
Re: [RFC PATCH 2/3] selinux: use separate table for initial SID lookup
Quoted 7 lines · Ondrej Mosnacek
Quoted 6 lines · Stephen Smalley
Quoted 4 lines · Ondrej Mosnacek
This patch is non-functional and moves handling of initial SIDs into a separate table. Note that the SIDs stored in the main table are now shifted by
SECINITSID_NUMand converted to/from the actual SIDs transparently by helper functions.
Quoted 8 lines
Quoted 3 lines
When you say "non-functional", you mean it doesn't make any functional changes, right? As opposed to leaving the kernel in a broken intermediate state until your 3rd patch?
I mean it *should* be non-functional on its own, unless I made a mistake. I admit I didn't do very much testing on this patch, but I at least checked that I can boot the kernel, load a policy and that reported file contexts make sense.
Commonly non-functional means "not working". I think you mean "this patch makes no functional changes".
Quoted 11 lines
Quoted 6 lines
I think you need to double check that all references to
state->ss->sidtabare preceded by a check ofstate->initializedsince it could be NULL before first policy load and a system could come up with SELinux enabled but never load a policy.Well, the original code does not initialize the sidtab field with
sidtab_init()either sostate->ss->sidtab.htablewould be NULL (or some garbage pointer) after boot and any use of sidtab would cause a GPF anyway.
Prior to the patch, the sidtab is directly embedded in the selinux_ss, which is static and thus all fields are initialized to 0/NULL. But you could access the sidtab itself without any problem since it was not a pointer. Likewise for the policydb. When you change the sidtab to be a pointer, then you have to deal with the possibility that it will be NULL. So you might be introducing new situations where we would trigger a GPF.
Quoted 6 lines
Looking at the places that reference the sidtab (which are all highlighted in the patch because of the switch to pointer type in the struct
selinux_ssdefinition), these don't seem to check forstate->initialized:
-security_port_sid
In this case, policydb->ocontexts[OCON_PORT] will be NULL at initialization, so c will be NULL and we will just set *out_sid to SECINITSID_PORT and return without ever accessing the sidtab.
Quoted 4 lines
-
security_ib_pkey_sid
-security_ib_endport_sid
-security_netif_sid
-security_node_sid
These are all similar.
Quoted 1 line
-
security_sid_mls_copy, called from:
This one checks state->initialized and returns if not set.
Quoted 7 lines
-
selinux_socket_unix_stream_connect(avc_has_permis called before)
-selinux_conn_sid, called from:
-selinux_sctp_assoc_request(selinux_policycap_extsockclassis
called before)
-selinux_inet_conn_request
-selinux_ip_postroute(selinux_policycap_netpeeris called before)
-security_net_peersid_resolve, called from:
This one should always return before taking the policy rwlock when !state->initialized because you can't have configured network labels without a policy IIUC (so xfrm_sid and/or nlbl_sid should be NULL), or regardless, policydb->mls_enabled will be 0 at this point.
Quoted 9 lines
-
selinux_skb_peerlbl_sid, called from:
-selinux_socket_sock_rcv_skb(selinux_policycap_netpeeris called before)
-selinux_socket_getpeersec_dgram
-selinux_sctp_assoc_request(again)
-selinux_inet_conn_request(again)
-selinux_inet_conn_established
-selinux_ip_forward(selinux_policycap_netpeeris called before)
-selinux_ip_postroute(again)
-selinux_audit_rule_match
This one can't be called without a prior selinux_audit_rule_init, which checks state->initialized.
Quoted 1 line
-
security_netlbl_secattr_to_sid, called from:
This one checks state->initialized.
Quoted 4 lines
-
selinux_netlbl_sidlookup_cached, called from:
-selinux_netlbl_skbuff_getsid(netlbl_enabledis called before)
-selinux_netlbl_sock_rcv_skb(netlbl_enabledis called before)
-security_netlbl_sid_to_secattr, called from:
Ditto.
Quoted 23 lines
-
selinux_netlbl_sock_genattr, called from:
-selinux_netlbl_socket_post_create, called from:
-selinux_socket_post_create
-selinux_netlbl_skbuff_setsid, called from:
-selinux_ip_forward(again)
-selinux_netlbl_sctp_assoc_request, called from:
-selinux_sctp_assoc_request(again)
-selinux_netlbl_inet_conn_request, called from:
-selinux_inet_conn_request(again)I suppose in some (or all?) of these cases
state->initializedmight be implicitly always 1, but at least in these it wasn't immediately obvious to me.Either way, such cases would already be buggy before this patch, since they would happily access the policy structure (likely hitting some NULL/invalid pointers) and most likely also dereference the invalid htable pointer in sidtab.
Quoted 3 lines
Aren't you still wasting a slot in the initial SIDs array, or did I miss something?
Quoted 8 lines
Yes, I am, because the original code doesn't seem to guard against
SECSID_NULLbeing loaded as initial SID into sidtab. I asked in the other thread whether this is considered a bug, but I didn't get a reply, so I left it sort of bug-to-bug compatible for now... Anyway, it doesn't seem to make much sense to keep that behavior so I will probably just go ahead and add the check topolicydb_load_isids()and shrink the table. You can expect it to be resolved in the final patchset.
Yes, that would be a bug.
Quoted 708 lines
Quoted 694 lines
Quoted 694 lines
This change doesn't make much sense on its own, but it simplifies further sidtab overhaul in a succeeding patch.
Signed-off-by Ondrej Mosnacek <[email protected]>--- security/selinux/ss/policydb.c | 10 ++- security/selinux/ss/services.c | 88 ++++++++++-------- security/selinux/ss/services.h | 2 +- security/selinux/ss/sidtab.c | 158 +++++++++++++++++++-------------- security/selinux/ss/sidtab.h | 14 +-- 5 files changed, 162 insertions(+), 110 deletions(-)security/selinux/ss/policydb.c +9
-1@@ -909,13 +909,21 @@ int policydb_load_isids(struct policydb *p, struct sidtab *s)if (!c->context[0 ].user) {pr_err ("SELinux: SID %s was never defined.\n" , c->u.name); +sidtab_destroy (s); + goto out; + } +if (c->sid[0 ] >SECINITSID_NUM ) { +pr_err ("SELinux: Initial SID %s out of range.\n" , + c->u.name); +sidtab_destroy (s); goto out; } - rc =sidtab_insert (s, c->sid[0 ], &c->context[0 ]); + rc =sidtab_set_initial (s, c->sid[0 ], &c->context[0 ]);if (rc) {pr_err ("SELinux: unable to load initial SID %s.\n" , c->u.name); +sidtab_destroy (s); goto out; } }security/selinux/ss/services.c +50
-38@@ -776,7 +776,7 @@ static int security_compute_validatetrans(struct selinux_state *state,read_lock (&state->ss->policy_rwlock); policydb = &state->ss->policydb; - sidtab = &state->ss->sidtab; + sidtab = state->ss->sidtab;if (!user) tclass =unmap_class (&state->ss->map, orig_tclass);@@ -876,7 +876,7 @@ int security_bounded_transition(struct selinux_state *state,read_lock (&state->ss->policy_rwlock); policydb = &state->ss->policydb; - sidtab = &state->ss->sidtab; + sidtab = state->ss->sidtab; rc = -EINVAL ; old_context =sidtab_search (sidtab, old_sid);@@ -1034,7 +1034,7 @@ void security_compute_xperms_decision(struct selinux_state *state, goto allow; policydb = &state->ss->policydb; - sidtab = &state->ss->sidtab; + sidtab = state->ss->sidtab; scontext =sidtab_search (sidtab, ssid);if (!scontext) {@@ -1123,7 +1123,7 @@ void security_compute_av(struct selinux_state *state, goto allow; policydb = &state->ss->policydb; - sidtab = &state->ss->sidtab; + sidtab = state->ss->sidtab; scontext =sidtab_search (sidtab, ssid);if (!scontext) {@@ -1177,7 +1177,7 @@ void security_compute_av_user(struct selinux_state *state, goto allow; policydb = &state->ss->policydb; - sidtab = &state->ss->sidtab; + sidtab = state->ss->sidtab; scontext =sidtab_search (sidtab, ssid);if (!scontext) {@@ -1315,7 +1315,7 @@ static int security_sid_to_context_core(struct selinux_state *state, }read_lock (&state->ss->policy_rwlock); policydb = &state->ss->policydb; - sidtab = &state->ss->sidtab; + sidtab = state->ss->sidtab;if (force) context =sidtab_search_force (sidtab, sid);else @@ -1483,7 +1483,7 @@ static int security_context_to_sid_core(struct selinux_state *state, }read_lock (&state->ss->policy_rwlock); policydb = &state->ss->policydb; - sidtab = &state->ss->sidtab; + sidtab = state->ss->sidtab; rc =string_to_context_struct (policydb, sidtab, scontext2, &context, def_sid);if (rc == -EINVAL && force) {@@ -1668,7 +1668,7 @@ static int security_compute_sid(struct selinux_state *state, } policydb = &state->ss->policydb; - sidtab = &state->ss->sidtab; + sidtab = state->ss->sidtab; scontext =sidtab_search (sidtab, ssid);if (!scontext) {@@ -1925,10 +1925,7 @@ static int convert_context(u32 key,struct user_datum *usrdatum;char *s;u32 len; -int rc =0 ; - -if (key <=SECINITSID_NUM ) - goto out; +int rc; args = p;@@ -2090,9 +2087,8 @@ static int security_preserve_bools(struct selinux_state *state,int security_load_policy (struct selinux_state *state,void *data,size_t len) {struct policydb *policydb; -struct sidtab *sidtab; +struct sidtab *oldsidtab, *newsidtab;struct policydb *oldpolicydb, *newpolicydb; -struct sidtab oldsidtab, newsidtab;struct selinux_mapping *oldmapping;struct selinux_map newmap;struct convert_context_args args;@@ -2108,27 +2104,37 @@ int security_load_policy(struct selinux_state *state, void *data, size_t len) newpolicydb = oldpolicydb +1 ; policydb = &state->ss->policydb; - sidtab = &state->ss->sidtab; + + newsidtab =kmalloc (sizeof (*newsidtab),GFP_KERNEL ); +if (!newsidtab) { + rc = -ENOMEM ; + goto out; + }if (!state->initialized) { rc =policydb_read (policydb, fp); -if (rc) +if (rc) { +kfree (newsidtab); goto out; + } policydb->len = len; rc =selinux_set_mapping (policydb, secclass_map, &state->ss->map);if (rc) { +kfree (newsidtab);policydb_destroy (policydb); goto out; } - rc =policydb_load_isids (policydb, sidtab); + rc =policydb_load_isids (policydb, newsidtab);if (rc) { +kfree (newsidtab);policydb_destroy (policydb); goto out; } + state->ss->sidtab = newsidtab;security_load_policycaps (state); state->initialized =1 ; seqno = ++state->ss->latest_granting;@@ -2141,13 +2147,17 @@ int security_load_policy(struct selinux_state *state, void *data, size_t len) goto out; } + oldsidtab = state->ss->sidtab; +#if 0 -sidtab_hash_eval (sidtab,"sids" ); +sidtab_hash_eval (oldsidtab,"sids" );#endif rc =policydb_read (newpolicydb, fp); -if (rc) +if (rc) { +kfree (newsidtab); goto out; + } newpolicydb->len = len;/* If switching between different policy types, log MLS status */ @@ -2156,10 +2166,11 @@ int security_load_policy(struct selinux_state *state, void *data, size_t len)else if (!policydb->mls_enabled && newpolicydb->mls_enabled)pr_info ("SELinux: Enabling MLS support...\n" ); - rc =policydb_load_isids (newpolicydb, &newsidtab); + rc =policydb_load_isids (newpolicydb, newsidtab);if (rc) {pr_err ("SELinux: unable to load the initial SIDs\n" );policydb_destroy (newpolicydb); +kfree (newsidtab); goto out; }@@ -2180,7 +2191,7 @@ int security_load_policy(struct selinux_state *state, void *data, size_t len) args.state = state; args.oldp = policydb; args.newp = newpolicydb; - rc =sidtab_convert (sidtab, &newsidtab, convert_context, &args); + rc =sidtab_convert (oldsidtab, newsidtab, convert_context, &args);if (rc) {pr_err ("SELinux: unable to convert the internal" " representation of contexts in the new SID"@@ -2190,12 +2201,11 @@ int security_load_policy(struct selinux_state *state, void *data, size_t len)/* Save the old policydb and SID table to free later. */ memcpy (oldpolicydb, policydb,sizeof (*policydb)); -sidtab_set (&oldsidtab, sidtab);/* Install the new policydb and SID table. */ write_lock_irq (&state->ss->policy_rwlock);memcpy (policydb, newpolicydb,sizeof (*policydb)); -sidtab_set (sidtab, &newsidtab); + state->ss->sidtab = newsidtab;security_load_policycaps (state); oldmapping = state->ss->map.mapping; state->ss->map.mapping = newmap.mapping;@@ -2205,7 +2215,8 @@ int security_load_policy(struct selinux_state *state, void *data, size_t len)/* Free the old policydb and SID table. */ policydb_destroy (oldpolicydb); -sidtab_destroy (&oldsidtab); +sidtab_destroy (oldsidtab); +kfree (oldsidtab);kfree (oldmapping);avc_ss_reset (state->avc, seqno);@@ -2219,7 +2230,8 @@ int security_load_policy(struct selinux_state *state, void *data, size_t len) err:kfree (newmap.mapping); -sidtab_destroy (&newsidtab); +sidtab_destroy (newsidtab); +kfree (newsidtab);policydb_destroy (newpolicydb); out:@@ -2256,7 +2268,7 @@ int security_port_sid(struct selinux_state *state,read_lock (&state->ss->policy_rwlock); policydb = &state->ss->policydb; - sidtab = &state->ss->sidtab; + sidtab = state->ss->sidtab; c = policydb->ocontexts[OCON_PORT ];while (c) {@@ -2302,7 +2314,7 @@ int security_ib_pkey_sid(struct selinux_state *state,read_lock (&state->ss->policy_rwlock); policydb = &state->ss->policydb; - sidtab = &state->ss->sidtab; + sidtab = state->ss->sidtab; c = policydb->ocontexts[OCON_IBPKEY ];while (c) {@@ -2348,7 +2360,7 @@ int security_ib_endport_sid(struct selinux_state *state,read_lock (&state->ss->policy_rwlock); policydb = &state->ss->policydb; - sidtab = &state->ss->sidtab; + sidtab = state->ss->sidtab; c = policydb->ocontexts[OCON_IBENDPORT ];while (c) {@@ -2394,7 +2406,7 @@ int security_netif_sid(struct selinux_state *state,read_lock (&state->ss->policy_rwlock); policydb = &state->ss->policydb; - sidtab = &state->ss->sidtab; + sidtab = state->ss->sidtab; c = policydb->ocontexts[OCON_NETIF ];while (c) {@@ -2459,7 +2471,7 @@ int security_node_sid(struct selinux_state *state,read_lock (&state->ss->policy_rwlock); policydb = &state->ss->policydb; - sidtab = &state->ss->sidtab; + sidtab = state->ss->sidtab;switch (domain) {case AF_INET : {@@ -2559,7 +2571,7 @@ int security_get_user_sids(struct selinux_state *state,read_lock (&state->ss->policy_rwlock); policydb = &state->ss->policydb; - sidtab = &state->ss->sidtab; + sidtab = state->ss->sidtab;context_init (&usercon);@@ -2661,7 +2673,7 @@ static inline int __security_genfs_sid(struct selinux_state *state, u32 *sid) {struct policydb *policydb = &state->ss->policydb; -struct sidtab *sidtab = &state->ss->sidtab; +struct sidtab *sidtab = state->ss->sidtab;int len;u16 sclass;struct genfs *genfs;@@ -2747,7 +2759,7 @@ int security_fs_use(struct selinux_state *state, struct super_block *sb)read_lock (&state->ss->policy_rwlock); policydb = &state->ss->policydb; - sidtab = &state->ss->sidtab; + sidtab = state->ss->sidtab; c = policydb->ocontexts[OCON_FSUSE ];while (c) {@@ -2953,7 +2965,7 @@ int security_sid_mls_copy(struct selinux_state *state, u32 sid, u32 mls_sid, u32 *new_sid) { struct policydb *policydb = &state->ss->policydb; -struct sidtab *sidtab = &state->ss->sidtab; +struct sidtab *sidtab = state->ss->sidtab;struct context *context1;struct context *context2;struct context newcon;@@ -3044,7 +3056,7 @@ int security_net_peersid_resolve(struct selinux_state *state, u32 *peer_sid) {struct policydb *policydb = &state->ss->policydb; -struct sidtab *sidtab = &state->ss->sidtab; +struct sidtab *sidtab = state->ss->sidtab;int rc;struct context *nlbl_ctx;struct context *xfrm_ctx;@@ -3405,7 +3417,7 @@ int selinux_audit_rule_match(u32 sid, u32 field, u32 op, void *vrule, goto out; } - ctxt =sidtab_search (&state->ss->sidtab, sid); + ctxt =sidtab_search (state->ss->sidtab, sid);if (unlikely (!ctxt)) {WARN_ONCE (1 ,"selinux_audit_rule_match: unrecognized SID %d\n" , sid);@@ -3568,7 +3580,7 @@ int security_netlbl_secattr_to_sid(struct selinux_state *state, u32 *sid) {struct policydb *policydb = &state->ss->policydb; -struct sidtab *sidtab = &state->ss->sidtab; +struct sidtab *sidtab = state->ss->sidtab;int rc;struct context *ctx;struct context ctx_new;@@ -3646,7 +3658,7 @@ int security_netlbl_sid_to_secattr(struct selinux_state *state,read_lock (&state->ss->policy_rwlock); rc = -ENOENT ; - ctx =sidtab_search (&state->ss->sidtab, sid); + ctx =sidtab_search (state->ss->sidtab, sid);if (ctx ==NULL ) goto out;security/selinux/ss/services.h +1
-1@@ -24,7 +24,7 @@ struct selinux_map { };struct selinux_ss { -struct sidtab sidtab; +struct sidtab *sidtab;struct policydb policydb;rwlock_t policy_rwlock;u32 latest_granting;security/selinux/ss/sidtab.c +93
-65@@ -22,16 +22,24 @@ int sidtab_init(struct sidtab *s) s->htable =kmalloc_array (SIDTAB_SIZE ,sizeof (*s->htable),GFP_ATOMIC );if (!s->htable)return -ENOMEM ; + +for (i =0 ; i <=SECINITSID_NUM ; i++) + s->isids[i].set =0 ; +for (i =0 ; i <SIDTAB_SIZE ; i++) s->htable[i] =NULL ; + +for (i =0 ; i <SIDTAB_CACHE_LEN ; i++) + s->cache[i] =NULL ; + s->nel =0 ; - s->next_sid =1 ; + s->next_sid =0 ; s->shutdown =0 ;spin_lock_init (&s->lock);return 0 ; } -int sidtab_insert (struct sidtab *s,u32 sid,struct context *context) +static int sidtab_insert (struct sidtab *s,u32 sid,struct context *context) {int hvalue;struct sidtab_node *prev, *cur, *newnode;@@ -76,34 +84,53 @@ int sidtab_insert(struct sidtab *s, u32 sid, struct context *context)return 0 ; } -static struct context *sidtab_search_core (struct sidtab *s,u32 sid,int force) +int sidtab_set_initial (struct sidtab *s,u32 sid,struct context *context) +{ +struct sidtab_isid_entry *entry = &s->isids[sid]; +int rc =context_cpy (&entry->context, context); +if (rc) +return rc; + + entry->set =1 ; +return 0 ; +} + +static struct context *sidtab_lookup (struct sidtab *s,u32 sid) {int hvalue;struct sidtab_node *cur; -if (!s) -return NULL ; - hvalue =SIDTAB_HASH (sid); cur = s->htable[hvalue];while (cur && sid > cur->sid) cur = cur->next; -if (force && cur && sid == cur->sid && cur->context.len) -return &cur->context; +if (!cur || sid != cur->sid) +return NULL ; -if (!cur || sid != cur->sid || cur->context.len) { -/* Remap invalid SIDs to the unlabeled SID. */ - sid =SECINITSID_UNLABELED ; - hvalue =SIDTAB_HASH (sid); - cur = s->htable[hvalue]; -while (cur && sid > cur->sid) - cur = cur->next; -if (!cur || sid != cur->sid) -return NULL ; +return &cur->context; +} + +static struct context *sidtab_search_core (struct sidtab *s,u32 sid,int force) +{ +struct context *context; +struct sidtab_isid_entry *entry; + +if (!s) +return NULL ; + +if (sid >SECINITSID_NUM ) { +u32 index = sid - (SECINITSID_NUM +1 ); + context =sidtab_lookup (s, index); + }else { + entry = &s->isids[sid]; + context = entry->set ? &entry->context :NULL ; } +if (context && (!context->len || force)) +return context; -return &cur->context; + entry = &s->isids[SECINITSID_UNLABELED ]; +return entry->set ? &entry->context :NULL ; }struct context *sidtab_search (struct sidtab *s,u32 sid)@@ -145,11 +172,7 @@ out:static int clone_sid (u32 sid,struct context *context,void *arg) {struct sidtab *s = arg; - -if (sid >SECINITSID_NUM ) -return sidtab_insert (s, sid, context); -else -return 0 ; +return sidtab_insert (s, sid, context); } int sidtab_convert(struct sidtab *s, struct sidtab *news,@@ -183,8 +206,8 @@ static void sidtab_update_cache(struct sidtab *s, struct sidtab_node *n, int loc s->cache[0 ] = n; } -static inline u32 sidtab_search_context (struct sidtab *s, -struct context *context) +static inline int sidtab_search_context (struct sidtab *s, +struct context *context,u32 *sid) {int i;struct sidtab_node *cur;@@ -194,15 +217,17 @@ static inline u32 sidtab_search_context(struct sidtab *s,while (cur) {if (context_cmp (&cur->context, context)) {sidtab_update_cache (s, cur,SIDTAB_CACHE_LEN -1 ); -return cur->sid; + *sid = cur->sid; +return 0 ; } cur = cur->next; } } -return 0 ; +return -ENOENT ; } -static inline u32 sidtab_search_cache (struct sidtab *s,struct context *context) +static inline int sidtab_search_cache (struct sidtab *s,struct context *context, +u32 *sid) {int i;struct sidtab_node *node;@@ -210,54 +235,67 @@ static inline u32 sidtab_search_cache(struct sidtab *s, struct context *context)for (i =0 ; i <SIDTAB_CACHE_LEN ; i++) { node = s->cache[i];if (unlikely (!node)) -return 0 ; +return -ENOENT ;if (context_cmp (&node->context, context)) {sidtab_update_cache (s, node, i); -return node->sid; + *sid = node->sid; +return 0 ; } } -return 0 ; +return -ENOENT ; } -int sidtab_context_to_sid (struct sidtab *s, -struct context *context, -u32 *out_sid) +static int sidtab_reverse_lookup (struct sidtab *s,struct context *context, +u32 *sid) { -u32 sid; -int ret =0 ; +int ret;unsigned long flags; - *out_sid =SECSID_NULL ; - - sid =sidtab_search_cache (s, context); -if (!sid) - sid =sidtab_search_context (s, context); -if (!sid) { + ret =sidtab_search_cache (s, context, sid); +if (ret) + ret =sidtab_search_context (s, context, sid); +if (ret) {spin_lock_irqsave (&s->lock, flags);/* Rescan now that we hold the lock. */ - sid =sidtab_search_context (s, context); -if (sid) + ret =sidtab_search_context (s, context, sid); +if (!ret) goto unlock_out;/* No SID exists for the context. Allocate a new one. */ -if (s->next_sid ==UINT_MAX || s->shutdown) { +if (s->next_sid == (UINT_MAX -SECINITSID_NUM -1 ) || s->shutdown) { ret = -ENOMEM ; goto unlock_out; } - sid = s->next_sid++; + *sid = s->next_sid++;if (context->len)pr_info ("SELinux: Context %s is not valid (left unmapped).\n" , context->str); - ret =sidtab_insert (s, sid, context); + ret =sidtab_insert (s, *sid, context);if (ret) s->next_sid--; unlock_out:spin_unlock_irqrestore (&s->lock, flags); } -if (ret) -return ret; +return ret; +} + +int sidtab_context_to_sid (struct sidtab *s,struct context *context,u32 *sid) +{ +int rc; +u32 i; + +for (i =0 ; i <=SECINITSID_NUM ; i++) { +struct sidtab_isid_entry *entry = &s->isids[i]; +if (entry->set &&context_cmp (context, &entry->context)) { + *sid = i; +return 0 ; + } + } - *out_sid = sid; + rc =sidtab_reverse_lookup (s, context, sid); +if (rc) +return rc; + *sid +=SECINITSID_NUM +1 ;return 0 ; }@@ -296,6 +334,11 @@ void sidtab_destroy(struct sidtab *s)if (!s)return ; +for (i =0 ; i <=SECINITSID_NUM ; i++) +if (s->isids[i].set) +context_destroy (&s->isids[i].context); + +for (i =0 ; i <SIDTAB_SIZE ; i++) { cur = s->htable[i];while (cur) {@@ -311,18 +354,3 @@ void sidtab_destroy(struct sidtab *s) s->nel =0 ; s->next_sid =1 ; } - -void sidtab_set (struct sidtab *dst,struct sidtab *src) -{ -unsigned long flags; -int i; - -spin_lock_irqsave (&src->lock, flags); - dst->htable = src->htable; - dst->nel = src->nel; - dst->next_sid = src->next_sid; - dst->shutdown =0 ; -for (i =0 ; i <SIDTAB_CACHE_LEN ; i++) - dst->cache[i] =NULL ; -spin_unlock_irqrestore (&src->lock, flags); -}security/selinux/ss/sidtab.h +9
-4@@ -22,6 +22,11 @@ struct sidtab_node {#define SIDTAB_SIZE SIDTAB_HASH_BUCKETS +struct sidtab_isid_entry { +int set; +struct context context; +}; +struct sidtab {struct sidtab_node **htable;unsigned int nel;/* number of elements */ @@ -30,10 +35,12 @@ struct sidtab {#define SIDTAB_CACHE_LEN 3struct sidtab_node *cache[SIDTAB_CACHE_LEN ];spinlock_t lock; + +struct sidtab_isid_entry isids[SECINITSID_NUM +1 ]; };int sidtab_init (struct sidtab *s); -int sidtab_insert (struct sidtab *s,u32 sid,struct context *context); +int sidtab_set_initial (struct sidtab *s,u32 sid,struct context *context);struct context *sidtab_search (struct sidtab *s,u32 sid);struct context *sidtab_search_force (struct sidtab *s,u32 sid);@@ -43,13 +50,10 @@ int sidtab_convert(struct sidtab *s, struct sidtab *news, void *args), void *args); -int sidtab_context_to_sid (struct sidtab *s, -struct context *context, -u32 *sid); +int sidtab_context_to_sid (struct sidtab *s,struct context *context,u32 *sid);Quoted 8 lines
security/selinux/ss/sidtab.h int sidtab_convert(struct sidtab *s, struct sidtab *news, · line 59
Quoted 7 lines
security/selinux/ss/sidtab.h +0
-1void sidtab_hash_eval (struct sidtab *h,char *tag);void sidtab_destroy (struct sidtab *s); -void sidtab_set (struct sidtab *dst,struct sidtab *src);#endif /* _SS_SIDTAB_H_ */ --
Ondrej Mosnacek <omosnace at redhat dot com> Associate Software Engineer, Security Technologies Red Hat, Inc.
Re: [RFC PATCH 2/3] selinux: use separate table for initial SID lookup
Quoted 12 lines · Stephen Smalley
Quoted 11 lines · Ondrej Mosnacek
Quoted 9 lines · Stephen Smalley
Quoted 4 lines · Ondrej Mosnacek
This patch is non-functional and moves handling of initial SIDs into a separate table. Note that the SIDs stored in the main table are now shifted by
SECINITSID_NUMand converted to/from the actual SIDs transparently by helper functions.When you say "non-functional", you mean it doesn't make any functional changes, right? As opposed to leaving the kernel in a broken intermediate state until your 3rd patch?
Quoted 7 lines
Quoted 4 lines
I mean it *should* be non-functional on its own, unless I made a mistake. I admit I didn't do very much testing on this patch, but I at least checked that I can boot the kernel, load a policy and that reported file contexts make sense.
Commonly non-functional means "not working". I think you mean "this patch makes no functional changes".
Ah, I see, so it was just a language issue :) Indeed I meant the latter.
Quoted 13 lines
Quoted 8 lines
Quoted 7 lines
I think you need to double check that all references to
state->ss->sidtabare preceded by a check ofstate->initializedsince it could be NULL before first policy load and a system could come up with SELinux enabled but never load a policy.Quoted 4 lines
Well, the original code does not initialize the sidtab field with
sidtab_init()either sostate->ss->sidtab.htablewould be NULL (or some garbage pointer) after boot and any use of sidtab would cause a GPF anyway.
Quoted 7 lines
Prior to the patch, the sidtab is directly embedded in the
selinux_ss, which is static and thus all fields are initialized to 0/NULL. But you could access the sidtab itself without any problem since it was not a pointer. Likewise for the policydb. When you change the sidtab to be a pointer, then you have to deal with the possibility that it will be NULL. So you might be introducing new situations where we would trigger a GPF.
My point was that even when the sidtab is embedded in selinux_ss, its htable field is a pointer and any sidtab_search[_force]()/sidtab_context_to_sid() calls on an uninitialized sidtab would still trigger a NULL pointer dereference. And outside the policy load code, these are the only functions that are ever used to access the sidtab.
Quoted 92 lines
Quoted 7 lines
Looking at the places that reference the sidtab (which are all highlighted in the patch because of the switch to pointer type in the struct
selinux_ssdefinition), these don't seem to check forstate->initialized:
-security_port_sidIn this case, policydb->ocontexts[OCON_PORT] will be NULL at initialization, so c will be NULL and we will just set
*out_sidtoSECINITSID_PORTand return without ever accessing the sidtab.Quoted 4 lines
-
security_ib_pkey_sid
-security_ib_endport_sid
-security_netif_sid
-security_node_sidThese are all similar.
Quoted 1 line
-
security_sid_mls_copy, called from:This one checks
state->initializedand returns if not set.Quoted 7 lines
-
selinux_socket_unix_stream_connect(avc_has_permis called before)
-selinux_conn_sid, called from:
-selinux_sctp_assoc_request(selinux_policycap_extsockclassis
called before)
-selinux_inet_conn_request
-selinux_ip_postroute(selinux_policycap_netpeeris called before)
-security_net_peersid_resolve, called from:This one should always return before taking the policy rwlock when !state->initialized because you can't have configured network labels without a policy IIUC (so
xfrm_sidand/ornlbl_sidshould be NULL), or regardless,policydb->mls_enabledwill be 0 at this point.Quoted 9 lines
-
selinux_skb_peerlbl_sid, called from:
-selinux_socket_sock_rcv_skb(selinux_policycap_netpeeris called before)
-selinux_socket_getpeersec_dgram
-selinux_sctp_assoc_request(again)
-selinux_inet_conn_request(again)
-selinux_inet_conn_established
-selinux_ip_forward(selinux_policycap_netpeeris called before)
-selinux_ip_postroute(again)
-selinux_audit_rule_matchThis one can't be called without a prior
selinux_audit_rule_init, which checksstate->initialized.Quoted 1 line
-
security_netlbl_secattr_to_sid, called from:This one checks
state->initialized.Quoted 4 lines
-
selinux_netlbl_sidlookup_cached, called from:
-selinux_netlbl_skbuff_getsid(netlbl_enabledis called before)
-selinux_netlbl_sock_rcv_skb(netlbl_enabledis called before)
-security_netlbl_sid_to_secattr, called from:Ditto.
Quoted 23 lines
-
selinux_netlbl_sock_genattr, called from:
-selinux_netlbl_socket_post_create, called from:
-selinux_socket_post_create
-selinux_netlbl_skbuff_setsid, called from:
-selinux_ip_forward(again)
-selinux_netlbl_sctp_assoc_request, called from:
-selinux_sctp_assoc_request(again)
-selinux_netlbl_inet_conn_request, called from:
-selinux_inet_conn_request(again)I suppose in some (or all?) of these cases
state->initializedmight be implicitly always 1, but at least in these it wasn't immediately obvious to me.Either way, such cases would already be buggy before this patch, since they would happily access the policy structure (likely hitting some NULL/invalid pointers) and most likely also dereference the invalid htable pointer in sidtab.
Quoted 3 lines
Aren't you still wasting a slot in the initial SIDs array, or did I miss something?
Quoted 8 lines
Yes, I am, because the original code doesn't seem to guard against
SECSID_NULLbeing loaded as initial SID into sidtab. I asked in the other thread whether this is considered a bug, but I didn't get a reply, so I left it sort of bug-to-bug compatible for now... Anyway, it doesn't seem to make much sense to keep that behavior so I will probably just go ahead and add the check topolicydb_load_isids()and shrink the table. You can expect it to be resolved in the final patchset.
Quoted 711 lines
Yes, that would be a bug.
Quoted 708 lines
Quoted 694 lines
Quoted 694 lines
This change doesn't make much sense on its own, but it simplifies further sidtab overhaul in a succeeding patch.
Signed-off-by Ondrej Mosnacek <[email protected]>--- security/selinux/ss/policydb.c | 10 ++- security/selinux/ss/services.c | 88 ++++++++++-------- security/selinux/ss/services.h | 2 +- security/selinux/ss/sidtab.c | 158 +++++++++++++++++++-------------- security/selinux/ss/sidtab.h | 14 +-- 5 files changed, 162 insertions(+), 110 deletions(-)security/selinux/ss/policydb.c +9
-1@@ -909,13 +909,21 @@ int policydb_load_isids(struct policydb *p, struct sidtab *s)if (!c->context[0 ].user) {pr_err ("SELinux: SID %s was never defined.\n" , c->u.name); +sidtab_destroy (s); + goto out; + } +if (c->sid[0 ] >SECINITSID_NUM ) { +pr_err ("SELinux: Initial SID %s out of range.\n" , + c->u.name); +sidtab_destroy (s); goto out; } - rc =sidtab_insert (s, c->sid[0 ], &c->context[0 ]); + rc =sidtab_set_initial (s, c->sid[0 ], &c->context[0 ]);if (rc) {pr_err ("SELinux: unable to load initial SID %s.\n" , c->u.name); +sidtab_destroy (s); goto out; } }security/selinux/ss/services.c +50
-38@@ -776,7 +776,7 @@ static int security_compute_validatetrans(struct selinux_state *state,read_lock (&state->ss->policy_rwlock); policydb = &state->ss->policydb; - sidtab = &state->ss->sidtab; + sidtab = state->ss->sidtab;if (!user) tclass =unmap_class (&state->ss->map, orig_tclass);@@ -876,7 +876,7 @@ int security_bounded_transition(struct selinux_state *state,read_lock (&state->ss->policy_rwlock); policydb = &state->ss->policydb; - sidtab = &state->ss->sidtab; + sidtab = state->ss->sidtab; rc = -EINVAL ; old_context =sidtab_search (sidtab, old_sid);@@ -1034,7 +1034,7 @@ void security_compute_xperms_decision(struct selinux_state *state, goto allow; policydb = &state->ss->policydb; - sidtab = &state->ss->sidtab; + sidtab = state->ss->sidtab; scontext =sidtab_search (sidtab, ssid);if (!scontext) {@@ -1123,7 +1123,7 @@ void security_compute_av(struct selinux_state *state, goto allow; policydb = &state->ss->policydb; - sidtab = &state->ss->sidtab; + sidtab = state->ss->sidtab; scontext =sidtab_search (sidtab, ssid);if (!scontext) {@@ -1177,7 +1177,7 @@ void security_compute_av_user(struct selinux_state *state, goto allow; policydb = &state->ss->policydb; - sidtab = &state->ss->sidtab; + sidtab = state->ss->sidtab; scontext =sidtab_search (sidtab, ssid);if (!scontext) {@@ -1315,7 +1315,7 @@ static int security_sid_to_context_core(struct selinux_state *state, }read_lock (&state->ss->policy_rwlock); policydb = &state->ss->policydb; - sidtab = &state->ss->sidtab; + sidtab = state->ss->sidtab;if (force) context =sidtab_search_force (sidtab, sid);else @@ -1483,7 +1483,7 @@ static int security_context_to_sid_core(struct selinux_state *state, }read_lock (&state->ss->policy_rwlock); policydb = &state->ss->policydb; - sidtab = &state->ss->sidtab; + sidtab = state->ss->sidtab; rc =string_to_context_struct (policydb, sidtab, scontext2, &context, def_sid);if (rc == -EINVAL && force) {@@ -1668,7 +1668,7 @@ static int security_compute_sid(struct selinux_state *state, } policydb = &state->ss->policydb; - sidtab = &state->ss->sidtab; + sidtab = state->ss->sidtab; scontext =sidtab_search (sidtab, ssid);if (!scontext) {@@ -1925,10 +1925,7 @@ static int convert_context(u32 key,struct user_datum *usrdatum;char *s;u32 len; -int rc =0 ; - -if (key <=SECINITSID_NUM ) - goto out; +int rc; args = p;@@ -2090,9 +2087,8 @@ static int security_preserve_bools(struct selinux_state *state,int security_load_policy (struct selinux_state *state,void *data,size_t len) {struct policydb *policydb; -struct sidtab *sidtab; +struct sidtab *oldsidtab, *newsidtab;struct policydb *oldpolicydb, *newpolicydb; -struct sidtab oldsidtab, newsidtab;struct selinux_mapping *oldmapping;struct selinux_map newmap;struct convert_context_args args;@@ -2108,27 +2104,37 @@ int security_load_policy(struct selinux_state *state, void *data, size_t len) newpolicydb = oldpolicydb +1 ; policydb = &state->ss->policydb; - sidtab = &state->ss->sidtab; + + newsidtab =kmalloc (sizeof (*newsidtab),GFP_KERNEL ); +if (!newsidtab) { + rc = -ENOMEM ; + goto out; + }if (!state->initialized) { rc =policydb_read (policydb, fp); -if (rc) +if (rc) { +kfree (newsidtab); goto out; + } policydb->len = len; rc =selinux_set_mapping (policydb, secclass_map, &state->ss->map);if (rc) { +kfree (newsidtab);policydb_destroy (policydb); goto out; } - rc =policydb_load_isids (policydb, sidtab); + rc =policydb_load_isids (policydb, newsidtab);if (rc) { +kfree (newsidtab);policydb_destroy (policydb); goto out; } + state->ss->sidtab = newsidtab;security_load_policycaps (state); state->initialized =1 ; seqno = ++state->ss->latest_granting;@@ -2141,13 +2147,17 @@ int security_load_policy(struct selinux_state *state, void *data, size_t len) goto out; } + oldsidtab = state->ss->sidtab; +#if 0 -sidtab_hash_eval (sidtab,"sids" ); +sidtab_hash_eval (oldsidtab,"sids" );#endif rc =policydb_read (newpolicydb, fp); -if (rc) +if (rc) { +kfree (newsidtab); goto out; + } newpolicydb->len = len;/* If switching between different policy types, log MLS status */ @@ -2156,10 +2166,11 @@ int security_load_policy(struct selinux_state *state, void *data, size_t len)else if (!policydb->mls_enabled && newpolicydb->mls_enabled)pr_info ("SELinux: Enabling MLS support...\n" ); - rc =policydb_load_isids (newpolicydb, &newsidtab); + rc =policydb_load_isids (newpolicydb, newsidtab);if (rc) {pr_err ("SELinux: unable to load the initial SIDs\n" );policydb_destroy (newpolicydb); +kfree (newsidtab); goto out; }@@ -2180,7 +2191,7 @@ int security_load_policy(struct selinux_state *state, void *data, size_t len) args.state = state; args.oldp = policydb; args.newp = newpolicydb; - rc =sidtab_convert (sidtab, &newsidtab, convert_context, &args); + rc =sidtab_convert (oldsidtab, newsidtab, convert_context, &args);if (rc) {pr_err ("SELinux: unable to convert the internal" " representation of contexts in the new SID"@@ -2190,12 +2201,11 @@ int security_load_policy(struct selinux_state *state, void *data, size_t len)/* Save the old policydb and SID table to free later. */ memcpy (oldpolicydb, policydb,sizeof (*policydb)); -sidtab_set (&oldsidtab, sidtab);/* Install the new policydb and SID table. */ write_lock_irq (&state->ss->policy_rwlock);memcpy (policydb, newpolicydb,sizeof (*policydb)); -sidtab_set (sidtab, &newsidtab); + state->ss->sidtab = newsidtab;security_load_policycaps (state); oldmapping = state->ss->map.mapping; state->ss->map.mapping = newmap.mapping;@@ -2205,7 +2215,8 @@ int security_load_policy(struct selinux_state *state, void *data, size_t len)/* Free the old policydb and SID table. */ policydb_destroy (oldpolicydb); -sidtab_destroy (&oldsidtab); +sidtab_destroy (oldsidtab); +kfree (oldsidtab);kfree (oldmapping);avc_ss_reset (state->avc, seqno);@@ -2219,7 +2230,8 @@ int security_load_policy(struct selinux_state *state, void *data, size_t len) err:kfree (newmap.mapping); -sidtab_destroy (&newsidtab); +sidtab_destroy (newsidtab); +kfree (newsidtab);policydb_destroy (newpolicydb); out:@@ -2256,7 +2268,7 @@ int security_port_sid(struct selinux_state *state,read_lock (&state->ss->policy_rwlock); policydb = &state->ss->policydb; - sidtab = &state->ss->sidtab; + sidtab = state->ss->sidtab; c = policydb->ocontexts[OCON_PORT ];while (c) {@@ -2302,7 +2314,7 @@ int security_ib_pkey_sid(struct selinux_state *state,read_lock (&state->ss->policy_rwlock); policydb = &state->ss->policydb; - sidtab = &state->ss->sidtab; + sidtab = state->ss->sidtab; c = policydb->ocontexts[OCON_IBPKEY ];while (c) {@@ -2348,7 +2360,7 @@ int security_ib_endport_sid(struct selinux_state *state,read_lock (&state->ss->policy_rwlock); policydb = &state->ss->policydb; - sidtab = &state->ss->sidtab; + sidtab = state->ss->sidtab; c = policydb->ocontexts[OCON_IBENDPORT ];while (c) {@@ -2394,7 +2406,7 @@ int security_netif_sid(struct selinux_state *state,read_lock (&state->ss->policy_rwlock); policydb = &state->ss->policydb; - sidtab = &state->ss->sidtab; + sidtab = state->ss->sidtab; c = policydb->ocontexts[OCON_NETIF ];while (c) {@@ -2459,7 +2471,7 @@ int security_node_sid(struct selinux_state *state,read_lock (&state->ss->policy_rwlock); policydb = &state->ss->policydb; - sidtab = &state->ss->sidtab; + sidtab = state->ss->sidtab;switch (domain) {case AF_INET : {@@ -2559,7 +2571,7 @@ int security_get_user_sids(struct selinux_state *state,read_lock (&state->ss->policy_rwlock); policydb = &state->ss->policydb; - sidtab = &state->ss->sidtab; + sidtab = state->ss->sidtab;context_init (&usercon);@@ -2661,7 +2673,7 @@ static inline int __security_genfs_sid(struct selinux_state *state, u32 *sid) {struct policydb *policydb = &state->ss->policydb; -struct sidtab *sidtab = &state->ss->sidtab; +struct sidtab *sidtab = state->ss->sidtab;int len;u16 sclass;struct genfs *genfs;@@ -2747,7 +2759,7 @@ int security_fs_use(struct selinux_state *state, struct super_block *sb)read_lock (&state->ss->policy_rwlock); policydb = &state->ss->policydb; - sidtab = &state->ss->sidtab; + sidtab = state->ss->sidtab; c = policydb->ocontexts[OCON_FSUSE ];while (c) {@@ -2953,7 +2965,7 @@ int security_sid_mls_copy(struct selinux_state *state, u32 sid, u32 mls_sid, u32 *new_sid) { struct policydb *policydb = &state->ss->policydb; -struct sidtab *sidtab = &state->ss->sidtab; +struct sidtab *sidtab = state->ss->sidtab;struct context *context1;struct context *context2;struct context newcon;@@ -3044,7 +3056,7 @@ int security_net_peersid_resolve(struct selinux_state *state, u32 *peer_sid) {struct policydb *policydb = &state->ss->policydb; -struct sidtab *sidtab = &state->ss->sidtab; +struct sidtab *sidtab = state->ss->sidtab;int rc;struct context *nlbl_ctx;struct context *xfrm_ctx;@@ -3405,7 +3417,7 @@ int selinux_audit_rule_match(u32 sid, u32 field, u32 op, void *vrule, goto out; } - ctxt =sidtab_search (&state->ss->sidtab, sid); + ctxt =sidtab_search (state->ss->sidtab, sid);if (unlikely (!ctxt)) {WARN_ONCE (1 ,"selinux_audit_rule_match: unrecognized SID %d\n" , sid);@@ -3568,7 +3580,7 @@ int security_netlbl_secattr_to_sid(struct selinux_state *state, u32 *sid) {struct policydb *policydb = &state->ss->policydb; -struct sidtab *sidtab = &state->ss->sidtab; +struct sidtab *sidtab = state->ss->sidtab;int rc;struct context *ctx;struct context ctx_new;@@ -3646,7 +3658,7 @@ int security_netlbl_sid_to_secattr(struct selinux_state *state,read_lock (&state->ss->policy_rwlock); rc = -ENOENT ; - ctx =sidtab_search (&state->ss->sidtab, sid); + ctx =sidtab_search (state->ss->sidtab, sid);if (ctx ==NULL ) goto out;security/selinux/ss/services.h +1
-1@@ -24,7 +24,7 @@ struct selinux_map { };struct selinux_ss { -struct sidtab sidtab; +struct sidtab *sidtab;struct policydb policydb;rwlock_t policy_rwlock;u32 latest_granting;security/selinux/ss/sidtab.c +93
-65@@ -22,16 +22,24 @@ int sidtab_init(struct sidtab *s) s->htable =kmalloc_array (SIDTAB_SIZE ,sizeof (*s->htable),GFP_ATOMIC );if (!s->htable)return -ENOMEM ; + +for (i =0 ; i <=SECINITSID_NUM ; i++) + s->isids[i].set =0 ; +for (i =0 ; i <SIDTAB_SIZE ; i++) s->htable[i] =NULL ; + +for (i =0 ; i <SIDTAB_CACHE_LEN ; i++) + s->cache[i] =NULL ; + s->nel =0 ; - s->next_sid =1 ; + s->next_sid =0 ; s->shutdown =0 ;spin_lock_init (&s->lock);return 0 ; } -int sidtab_insert (struct sidtab *s,u32 sid,struct context *context) +static int sidtab_insert (struct sidtab *s,u32 sid,struct context *context) {int hvalue;struct sidtab_node *prev, *cur, *newnode;@@ -76,34 +84,53 @@ int sidtab_insert(struct sidtab *s, u32 sid, struct context *context)return 0 ; } -static struct context *sidtab_search_core (struct sidtab *s,u32 sid,int force) +int sidtab_set_initial (struct sidtab *s,u32 sid,struct context *context) +{ +struct sidtab_isid_entry *entry = &s->isids[sid]; +int rc =context_cpy (&entry->context, context); +if (rc) +return rc; + + entry->set =1 ; +return 0 ; +} + +static struct context *sidtab_lookup (struct sidtab *s,u32 sid) {int hvalue;struct sidtab_node *cur; -if (!s) -return NULL ; - hvalue =SIDTAB_HASH (sid); cur = s->htable[hvalue];while (cur && sid > cur->sid) cur = cur->next; -if (force && cur && sid == cur->sid && cur->context.len) -return &cur->context; +if (!cur || sid != cur->sid) +return NULL ; -if (!cur || sid != cur->sid || cur->context.len) { -/* Remap invalid SIDs to the unlabeled SID. */ - sid =SECINITSID_UNLABELED ; - hvalue =SIDTAB_HASH (sid); - cur = s->htable[hvalue]; -while (cur && sid > cur->sid) - cur = cur->next; -if (!cur || sid != cur->sid) -return NULL ; +return &cur->context; +} + +static struct context *sidtab_search_core (struct sidtab *s,u32 sid,int force) +{ +struct context *context; +struct sidtab_isid_entry *entry; + +if (!s) +return NULL ; + +if (sid >SECINITSID_NUM ) { +u32 index = sid - (SECINITSID_NUM +1 ); + context =sidtab_lookup (s, index); + }else { + entry = &s->isids[sid]; + context = entry->set ? &entry->context :NULL ; } +if (context && (!context->len || force)) +return context; -return &cur->context; + entry = &s->isids[SECINITSID_UNLABELED ]; +return entry->set ? &entry->context :NULL ; }struct context *sidtab_search (struct sidtab *s,u32 sid)@@ -145,11 +172,7 @@ out:static int clone_sid (u32 sid,struct context *context,void *arg) {struct sidtab *s = arg; - -if (sid >SECINITSID_NUM ) -return sidtab_insert (s, sid, context); -else -return 0 ; +return sidtab_insert (s, sid, context); } int sidtab_convert(struct sidtab *s, struct sidtab *news,@@ -183,8 +206,8 @@ static void sidtab_update_cache(struct sidtab *s, struct sidtab_node *n, int loc s->cache[0 ] = n; } -static inline u32 sidtab_search_context (struct sidtab *s, -struct context *context) +static inline int sidtab_search_context (struct sidtab *s, +struct context *context,u32 *sid) {int i;struct sidtab_node *cur;@@ -194,15 +217,17 @@ static inline u32 sidtab_search_context(struct sidtab *s,while (cur) {if (context_cmp (&cur->context, context)) {sidtab_update_cache (s, cur,SIDTAB_CACHE_LEN -1 ); -return cur->sid; + *sid = cur->sid; +return 0 ; } cur = cur->next; } } -return 0 ; +return -ENOENT ; } -static inline u32 sidtab_search_cache (struct sidtab *s,struct context *context) +static inline int sidtab_search_cache (struct sidtab *s,struct context *context, +u32 *sid) {int i;struct sidtab_node *node;@@ -210,54 +235,67 @@ static inline u32 sidtab_search_cache(struct sidtab *s, struct context *context)for (i =0 ; i <SIDTAB_CACHE_LEN ; i++) { node = s->cache[i];if (unlikely (!node)) -return 0 ; +return -ENOENT ;if (context_cmp (&node->context, context)) {sidtab_update_cache (s, node, i); -return node->sid; + *sid = node->sid; +return 0 ; } } -return 0 ; +return -ENOENT ; } -int sidtab_context_to_sid (struct sidtab *s, -struct context *context, -u32 *out_sid) +static int sidtab_reverse_lookup (struct sidtab *s,struct context *context, +u32 *sid) { -u32 sid; -int ret =0 ; +int ret;unsigned long flags; - *out_sid =SECSID_NULL ; - - sid =sidtab_search_cache (s, context); -if (!sid) - sid =sidtab_search_context (s, context); -if (!sid) { + ret =sidtab_search_cache (s, context, sid); +if (ret) + ret =sidtab_search_context (s, context, sid); +if (ret) {spin_lock_irqsave (&s->lock, flags);/* Rescan now that we hold the lock. */ - sid =sidtab_search_context (s, context); -if (sid) + ret =sidtab_search_context (s, context, sid); +if (!ret) goto unlock_out;/* No SID exists for the context. Allocate a new one. */ -if (s->next_sid ==UINT_MAX || s->shutdown) { +if (s->next_sid == (UINT_MAX -SECINITSID_NUM -1 ) || s->shutdown) { ret = -ENOMEM ; goto unlock_out; } - sid = s->next_sid++; + *sid = s->next_sid++;if (context->len)pr_info ("SELinux: Context %s is not valid (left unmapped).\n" , context->str); - ret =sidtab_insert (s, sid, context); + ret =sidtab_insert (s, *sid, context);if (ret) s->next_sid--; unlock_out:spin_unlock_irqrestore (&s->lock, flags); } -if (ret) -return ret; +return ret; +} + +int sidtab_context_to_sid (struct sidtab *s,struct context *context,u32 *sid) +{ +int rc; +u32 i; + +for (i =0 ; i <=SECINITSID_NUM ; i++) { +struct sidtab_isid_entry *entry = &s->isids[i]; +if (entry->set &&context_cmp (context, &entry->context)) { + *sid = i; +return 0 ; + } + } - *out_sid = sid; + rc =sidtab_reverse_lookup (s, context, sid); +if (rc) +return rc; + *sid +=SECINITSID_NUM +1 ;return 0 ; }@@ -296,6 +334,11 @@ void sidtab_destroy(struct sidtab *s)if (!s)return ; +for (i =0 ; i <=SECINITSID_NUM ; i++) +if (s->isids[i].set) +context_destroy (&s->isids[i].context); + +for (i =0 ; i <SIDTAB_SIZE ; i++) { cur = s->htable[i];while (cur) {@@ -311,18 +354,3 @@ void sidtab_destroy(struct sidtab *s) s->nel =0 ; s->next_sid =1 ; } - -void sidtab_set (struct sidtab *dst,struct sidtab *src) -{ -unsigned long flags; -int i; - -spin_lock_irqsave (&src->lock, flags); - dst->htable = src->htable; - dst->nel = src->nel; - dst->next_sid = src->next_sid; - dst->shutdown =0 ; -for (i =0 ; i <SIDTAB_CACHE_LEN ; i++) - dst->cache[i] =NULL ; -spin_unlock_irqrestore (&src->lock, flags); -}security/selinux/ss/sidtab.h +9
-4@@ -22,6 +22,11 @@ struct sidtab_node {#define SIDTAB_SIZE SIDTAB_HASH_BUCKETS +struct sidtab_isid_entry { +int set; +struct context context; +}; +struct sidtab {struct sidtab_node **htable;unsigned int nel;/* number of elements */ @@ -30,10 +35,12 @@ struct sidtab {#define SIDTAB_CACHE_LEN 3struct sidtab_node *cache[SIDTAB_CACHE_LEN ];spinlock_t lock; + +struct sidtab_isid_entry isids[SECINITSID_NUM +1 ]; };int sidtab_init (struct sidtab *s); -int sidtab_insert (struct sidtab *s,u32 sid,struct context *context); +int sidtab_set_initial (struct sidtab *s,u32 sid,struct context *context);struct context *sidtab_search (struct sidtab *s,u32 sid);struct context *sidtab_search_force (struct sidtab *s,u32 sid);@@ -43,13 +50,10 @@ int sidtab_convert(struct sidtab *s, struct sidtab *news, void *args), void *args); -int sidtab_context_to_sid (struct sidtab *s, -struct context *context, -u32 *sid); +int sidtab_context_to_sid (struct sidtab *s,struct context *context,u32 *sid);Quoted 8 lines
security/selinux/ss/sidtab.h int sidtab_convert(struct sidtab *s, struct sidtab *news, · line 59
Quoted 7 lines
security/selinux/ss/sidtab.h +0
-1void sidtab_hash_eval (struct sidtab *h,char *tag);void sidtab_destroy (struct sidtab *s); -void sidtab_set (struct sidtab *dst,struct sidtab *src);#endif /* _SS_SIDTAB_H_ */ --
Ondrej Mosnacek <omosnace at redhat dot com> Associate Software Engineer, Security Technologies Red Hat, Inc.
--
Ondrej Mosnacek <omosnace at redhat dot com> Associate Software Engineer, Security Technologies Red Hat, Inc.
RFC PATCH 3/3 selinux: overhaul sidtab to fix bug and improve performance
In reply to Ondrej MosnacekBefore this patch, during a policy reload the sidtab would become frozen and trying to map a new context to SID would be unable to add a new entry to sidtab and fail with -ENOMEM.
Such failures are usually propagated into userspace, which has no way of distignuishing them from actual allocation failures and thus doesn't handle them gracefully. Such situation can be triggered e.g. by the following reproducer:
while true; do load_policy; echo -n .; sleep 0.1; done &
for (( i = 0; i < 1024; i++ )); do
runcon -l s0:c$i echo -n x || break
# or:
# chcon -l s0:c$i <some_file> || break
doneThis patch overhauls the sidtab so it doesn't need to be frozen during policy reload, thus solving the above problem.
The new SID table leverages the fact that SIDs are allocated sequentially and are never invalidated and stores them in linear buckets indexed by a tree structure. This brings several advantages:
1. Fast SID -> context lookup - this lookup can now be done in logarithmic time complexity (usually in less than 4 array lookups) and can still be done safely without locking.
2. No need to re-search the whole table on reverse lookup miss - after acquiring the spinlock only the newly added entries need to be searched, which means that reverse lookups that end up inserting a new entry are now about twice as fast.
3. No need to freeze sidtab during policy reload - it is now possible to handle insertion of new entries even during sidtab conversion.
The tree structure of the new sidtab is able to grow automatically to up to about 2^31 entries (at which point it should not have more than about 4 tree levels). The old sidtab had a theoretical capacity of almost 2^32 entries, but half of that is still more than enough since by that point the reverse table lookups would become unusably slow anyway...
The number of entries per tree node is selected automatically so that each node fits into a single page, which should be the easiest size for kmalloc() to handle.
Note that currently the new implementation does not have a cache for recent reverse lookups as the old one had. Since repeated lookups are quite likely with sidtab, it is an important optimization and I will try to add it before submitting a final version.
Tested by selinux-testsuite and thoroughly tortured by this simple stress test:
```
function rand_cat() {
echo $(( $RANDOM % 1024 ))
}
function do_work() {
while true; do
echo -n "system_u:system_r:kernel_t:s0:c$(rand_cat),c$(rand_cat)" \
>/sys/fs/selinux/context 2>/dev/null || true
done
}
do_work >/dev/null &
do_work >/dev/null &
do_work >/dev/null &
while load_policy; do echo -n .; sleep 0.1; done
kill %1
kill %2
kill %3
```--- security/selinux/ss/mls.c | 23 +- security/selinux/ss/mls.h | 3 +- security/selinux/ss/services.c | 90 +++--- security/selinux/ss/sidtab.c | 514 +++++++++++++++++++-------------- security/selinux/ss/sidtab.h | 75 +++-- 5 files changed, 391 insertions(+), 314 deletions(-)
security/selinux/ss/mls.c +10 -13
@@ -436,16 +436,17 @@ int mls_setup_user_range(struct policydb *p,/* * Convert the MLS fields in the security context -* structure `c' from the values specified in the -* policy `oldp' to the values specified in the policy `newp'. +* structure `oldc' from the values specified in the +* policy `oldp' to the values specified in the policy `newp', +* storing the resulting context in `newc'. */ int mls_convert_context (struct policydb *oldp,struct policydb *newp, -struct context *c) +struct context *oldc, +struct context *newc) {struct level_datum *levdatum;struct cat_datum *catdatum; -struct ebitmap bitmap;struct ebitmap_node *node;int l, i;
@@ -455,28 +456,24 @@ int mls_convert_context(struct policydb *oldp,for (l =0 ; l <2 ; l++) { levdatum =hashtab_search (newp->p_levels.table,sym_name (oldp,SYM_LEVELS , - c->range.level[l].sens -1 )); + oldc->range.level[l].sens -1 ));if (!levdatum)return -EINVAL ; - c->range.level[l].sens = levdatum->level->sens; + newc->range.level[l].sens = levdatum->level->sens; -ebitmap_init (&bitmap); -ebitmap_for_each_positive_bit (&c->range.level[l].cat, node, i) { +ebitmap_for_each_positive_bit (&oldc->range.level[l].cat, node, i) {int rc; catdatum =hashtab_search (newp->p_cats.table,sym_name (oldp,SYM_CATS , i));if (!catdatum)return -EINVAL ; - rc =ebitmap_set_bit (&bitmap, catdatum->value -1 ,1 ); + rc =ebitmap_set_bit (&newc->range.level[l].cat, + catdatum->value -1 ,1 );if (rc)return rc; - -cond_resched (); } -ebitmap_destroy (&c->range.level[l].cat); - c->range.level[l].cat = bitmap; }return 0 ;
security/selinux/ss/mls.h +2 -1
@@ -46,7 +46,8 @@ int mls_range_set(struct context *context, struct mls_range *range);int mls_convert_context (struct policydb *oldp,struct policydb *newp, -struct context *context); +struct context *oldc, +struct context *newc); int mls_compute_sid(struct policydb *p, struct context *scontext,
security/selinux/ss/services.c +35 -55
195 lines not on this page · raw message
security/selinux/ss/sidtab.c +291 -223
611 lines not on this page · raw message
security/selinux/ss/sidtab.h +53 -22
107 lines not on this page · raw message
Signature · 1 line
2.17.2