selinux: simplify mls_context_to_sid()
PATCH v3 1/1 selinux: simplify mls_context_to_sid()
This function has only two callers, but only one of them actually needs the special logic at the beginning. Factoring this logic out into string_to_context_struct() allows us to drop the arguments 'oldc', 's', and 'def_sid'.
---
Changes in v3:
- correct the comment about policy read lock
Changes in v2:
- also drop unneeded #include's from mls.c
security/selinux/ss/mls.c | 49 +++++----------------------------- security/selinux/ss/mls.h | 5 +--- security/selinux/ss/services.c | 32 +++++++++++++++++++--- 3 files changed, 36 insertions(+), 50 deletions(-)
security/selinux/ss/mls.c +6 -43
@@ -24,10 +24,7 @@#include <linux/string.h> #include <linux/errno.h> #include <net/netlabel.h> -#include "sidtab.h" #include "mls.h" -#include "policydb.h" -#include "services.h" /* * Return the length in bytes for the MLS fields of the
@@ -223,20 +220,12 @@ int mls_context_isvalid(struct policydb *p, struct context *c)* This function modifies the string in place, inserting * NULL characters to terminate the MLS fields. * -* If a def_sid is provided and no MLS field is present, -* copy the MLS field of the associated default context. -* Used for upgraded to MLS systems where objects may lack -* MLS fields. -* -* Policy read-lock must be held for sidtab lookup. +* Policy read-lock must be held for policy data lookup. * */ int mls_context_to_sid (struct policydb *pol, -char oldc,char *scontext, -struct context *context, -struct sidtab *s, -u32 def_sid) +struct context *context) {char *sensitivity, *cur_cat, *next_cat, *rngptr;struct level_datum *levdatum;
@@ -244,29 +233,6 @@ int mls_context_to_sid(struct policydb *pol,int l, rc, i;char *rangep[2 ]; -if (!pol->mls_enabled) { -if ((def_sid !=SECSID_NULL && oldc) || (*scontext) =='\0' ) -return 0 ; -return -EINVAL ; - } - -/* -* No MLS component to the security context, try and map to -* default if provided. -*/ -if (!oldc) { -struct context *defcon; - -if (def_sid ==SECSID_NULL ) -return -EINVAL ; - - defcon =sidtab_search (s, def_sid); -if (!defcon) -return -EINVAL ; - -return mls_context_cpy (context, defcon); - } -/* * If we're dealing with a range, figure out where the two parts * of the range begin.
@@ -364,14 +330,11 @@ int mls_from_string(struct policydb *p, char *str, struct context *context,return -EINVAL ; tmpstr =kstrdup (str, gfp_mask); -if (!tmpstr) { - rc = -ENOMEM ; - }else { - rc =mls_context_to_sid (p,':' , tmpstr, context, -NULL ,SECSID_NULL ); -kfree (tmpstr); - } +if (!tmpstr) +return -ENOMEM ; + rc =mls_context_to_sid (p, tmpstr, context); +kfree (tmpstr);return rc; }
security/selinux/ss/mls.h +1 -4
@@ -33,11 +33,8 @@ int mls_range_isvalid(struct policydb *p, struct mls_range *r);int mls_level_isvalid (struct policydb *p,struct mls_level *l);int mls_context_to_sid (struct policydb *p, -char oldc,char *scontext, -struct context *context, -struct sidtab *s, -u32 def_sid); +struct context *context);int mls_from_string (struct policydb *p,char *str,struct context *context,gfp_t gfp_mask);
security/selinux/ss/services.c +29 -3
@@ -1425,9 +1425,35 @@ static int string_to_context_struct(struct policydb *pol, ctx->type = typdatum->value; - rc =mls_context_to_sid (pol, oldc, p, ctx, sidtabp, def_sid); -if (rc) - goto out; +if (!pol->mls_enabled) { + rc = -EINVAL ; +if ((def_sid ==SECSID_NULL || !oldc) && (*p) !='\0' ) + goto out; + }else if (!oldc) { +/* +* If a def_sid is provided and no MLS field is present, +* copy the MLS field of the associated default context. +* Used for upgrading to MLS systems where objects may lack +* MLS fields. +*/ +struct context *defcon; + + rc = -EINVAL ; +if (def_sid ==SECSID_NULL ) + goto out; + + defcon =sidtab_search (sidtabp, def_sid); +if (!defcon) + goto out; + + rc =mls_context_cpy (ctx, defcon); +if (rc) + goto out; + }else { + rc =mls_context_to_sid (pol, p, ctx); +if (rc) + goto out; + }/* Check the validity of the new context. */ rc = -EINVAL ;
Signature · 1 line
2.17.2
Quoted 128 lines · Ondrej Mosnacek
This function has only two callers, but only one of them actually needs the special logic at the beginning. Factoring this logic out into
string_to_context_struct()allows us to drop the arguments 'oldc', 's', and 'def_sid'.Signed-off-by Ondrej Mosnacek <[email protected]>---
Changes in v3:
- correct the comment about policy read lockChanges in v2:
- also drop unneeded #include's frommls.csecurity/selinux/ss/mls.c | 49 +++++----------------------------- security/selinux/ss/mls.h | 5 +--- security/selinux/ss/services.c | 32 +++++++++++++++++++--- 3 files changed, 36 insertions(+), 50 deletions(-)security/selinux/ss/mls.c +6
-43@@ -24,10 +24,7 @@#include <linux/string.h> #include <linux/errno.h> #include <net/netlabel.h> -#include "sidtab.h" #include "mls.h" -#include "policydb.h" -#include "services.h" /* * Return the length in bytes for the MLS fields of the @@ -223,20 +220,12 @@ int mls_context_isvalid(struct policydb *p, struct context *c)* This function modifies the string in place, inserting * NULL characters to terminate the MLS fields. * -* If a def_sid is provided and no MLS field is present, -* copy the MLS field of the associated default context. -* Used for upgraded to MLS systems where objects may lack -* MLS fields. -* -* Policy read-lock must be held for sidtab lookup. +* Policy read-lock must be held for policy data lookup. * */ int mls_context_to_sid (struct policydb *pol, -char oldc,char *scontext, -struct context *context, -struct sidtab *s, -u32 def_sid) +struct context *context) {char *sensitivity, *cur_cat, *next_cat, *rngptr;struct level_datum *levdatum;@@ -244,29 +233,6 @@ int mls_context_to_sid(struct policydb *pol,int l, rc, i;char *rangep[2 ]; -if (!pol->mls_enabled) { -if ((def_sid !=SECSID_NULL && oldc) || (*scontext) =='\0' ) -return 0 ; -return -EINVAL ; - } - -/* -* No MLS component to the security context, try and map to -* default if provided. -*/ -if (!oldc) { -struct context *defcon; - -if (def_sid ==SECSID_NULL ) -return -EINVAL ; - - defcon =sidtab_search (s, def_sid); -if (!defcon) -return -EINVAL ; - -return mls_context_cpy (context, defcon); - } -/* * If we're dealing with a range, figure out where the two parts * of the range begin. @@ -364,14 +330,11 @@ int mls_from_string(struct policydb *p, char *str, struct context *context,return -EINVAL ; tmpstr =kstrdup (str, gfp_mask); -if (!tmpstr) { - rc = -ENOMEM ; - }else { - rc =mls_context_to_sid (p,':' , tmpstr, context, -NULL ,SECSID_NULL ); -kfree (tmpstr); - } +if (!tmpstr) +return -ENOMEM ; + rc =mls_context_to_sid (p, tmpstr, context); +kfree (tmpstr);return rc; }security/selinux/ss/mls.h +1
-4@@ -33,11 +33,8 @@ int mls_range_isvalid(struct policydb *p, struct mls_range *r);int mls_level_isvalid (struct policydb *p,struct mls_level *l);int mls_context_to_sid (struct policydb *p, -char oldc,char *scontext, -struct context *context, -struct sidtab *s, -u32 def_sid); +struct context *context);int mls_from_string (struct policydb *p,char *str,struct context *context,gfp_t gfp_mask);security/selinux/ss/services.c +0
-0
security/selinux/ss/services.c static int string_to_context_struct(struct policydb *pol, · line 1431
Quoted 10 lines
security/selinux/ss/services.c +4
-3@@ -1425,9 +1425,35 @@ static int string_to_context_struct(struct policydb *pol, ctx->type = typdatum->value; - rc =mls_context_to_sid (pol, oldc, p, ctx, sidtabp, def_sid); -if (rc) - goto out; +if (!pol->mls_enabled) { + rc = -EINVAL ; +if ((def_sid ==SECSID_NULL || !oldc) && (*p) !='\0' ) + goto out;
I don't think this is your bug, but unless I'm mistaken, p could be OOB and be dereferenced here. Seems to have been introduced by 95ffe194204ae3cef88d0b59be209204bbe9b3be. Likely not caught in testing since both Linux distributions and Android enable MLS to use the category sets for isolation.
Quoted 29 lines
security/selinux/ss/services.c +25
-0+ }else if (!oldc) { +/* +* If a def_sid is provided and no MLS field is present, +* copy the MLS field of the associated default context. +* Used for upgrading to MLS systems where objects may lack +* MLS fields. +*/ +struct context *defcon; + + rc = -EINVAL ; +if (def_sid ==SECSID_NULL ) + goto out; + + defcon =sidtab_search (sidtabp, def_sid); +if (!defcon) + goto out; + + rc =mls_context_cpy (ctx, defcon); +if (rc) + goto out; + }else { + rc =mls_context_to_sid (pol, p, ctx); +if (rc) + goto out; + }/* Check the validity of the new context. */ rc = -EINVAL ;
Paul Moore
maintainerQuoted 132 lines · Stephen Smalley
Quoted 131 lines · Ondrej Mosnacek
This function has only two callers, but only one of them actually needs the special logic at the beginning. Factoring this logic out into
string_to_context_struct()allows us to drop the arguments 'oldc', 's', and 'def_sid'.Signed-off-by Ondrej Mosnacek <[email protected]>---
Changes in v3:
- correct the comment about policy read lockChanges in v2:
- also drop unneeded #include's frommls.csecurity/selinux/ss/mls.c | 49 +++++----------------------------- security/selinux/ss/mls.h | 5 +--- security/selinux/ss/services.c | 32 +++++++++++++++++++--- 3 files changed, 36 insertions(+), 50 deletions(-)security/selinux/ss/mls.c +6
-43@@ -24,10 +24,7 @@#include <linux/string.h> #include <linux/errno.h> #include <net/netlabel.h> -#include "sidtab.h" #include "mls.h" -#include "policydb.h" -#include "services.h" /* * Return the length in bytes for the MLS fields of the @@ -223,20 +220,12 @@ int mls_context_isvalid(struct policydb *p, struct context *c)* This function modifies the string in place, inserting * NULL characters to terminate the MLS fields. * -* If a def_sid is provided and no MLS field is present, -* copy the MLS field of the associated default context. -* Used for upgraded to MLS systems where objects may lack -* MLS fields. -* -* Policy read-lock must be held for sidtab lookup. +* Policy read-lock must be held for policy data lookup. * */ int mls_context_to_sid (struct policydb *pol, -char oldc,char *scontext, -struct context *context, -struct sidtab *s, -u32 def_sid) +struct context *context) {char *sensitivity, *cur_cat, *next_cat, *rngptr;struct level_datum *levdatum;@@ -244,29 +233,6 @@ int mls_context_to_sid(struct policydb *pol,int l, rc, i;char *rangep[2 ]; -if (!pol->mls_enabled) { -if ((def_sid !=SECSID_NULL && oldc) || (*scontext) =='\0' ) -return 0 ; -return -EINVAL ; - } - -/* -* No MLS component to the security context, try and map to -* default if provided. -*/ -if (!oldc) { -struct context *defcon; - -if (def_sid ==SECSID_NULL ) -return -EINVAL ; - - defcon =sidtab_search (s, def_sid); -if (!defcon) -return -EINVAL ; - -return mls_context_cpy (context, defcon); - } -/* * If we're dealing with a range, figure out where the two parts * of the range begin. @@ -364,14 +330,11 @@ int mls_from_string(struct policydb *p, char *str, struct context *context,return -EINVAL ; tmpstr =kstrdup (str, gfp_mask); -if (!tmpstr) { - rc = -ENOMEM ; - }else { - rc =mls_context_to_sid (p,':' , tmpstr, context, -NULL ,SECSID_NULL ); -kfree (tmpstr); - } +if (!tmpstr) +return -ENOMEM ; + rc =mls_context_to_sid (p, tmpstr, context); +kfree (tmpstr);return rc; }security/selinux/ss/mls.h +1
-4@@ -33,11 +33,8 @@ int mls_range_isvalid(struct policydb *p, struct mls_range *r);int mls_level_isvalid (struct policydb *p,struct mls_level *l);int mls_context_to_sid (struct policydb *p, -char oldc,char *scontext, -struct context *context, -struct sidtab *s, -u32 def_sid); +struct context *context);int mls_from_string (struct policydb *p,char *str,struct context *context,gfp_t gfp_mask);security/selinux/ss/services.c +0
-0@@ -1425,9 +1425,35 @@ static int string_to_context_struct(struct policydb *pol, ctx->type = typdatum->value;
Quoted 13 lines
security/selinux/ss/services.c static int string_to_context_struct(struct policydb *pol, · line 1431
Quoted 7 lines
security/selinux/ss/services.c +4
-3- rc =mls_context_to_sid (pol, oldc, p, ctx, sidtabp, def_sid); -if (rc) - goto out; +if (!pol->mls_enabled) { + rc = -EINVAL ; +if ((def_sid ==SECSID_NULL || !oldc) && (*p) !='\0' ) + goto out;I don't think this is your bug, but unless I'm mistaken, p could be OOB and be dereferenced here. Seems to have been introduced by
95ffe194204ae3cef88d0b59be209204bbe9b3be. Likely not caught in testing since both Linux distributions and Android enable MLS to use the category sets for isolation.
Yep, and we should fix that in v4.20-rcX independent of this patch. I think if we simply remove the "(*scontext) == '\0'" from the check we should be okay; I believe the only time we would want to return 0 when not running a MLS policy would be when there is something in the MLS portion of the label.
--
paul moore
www.paul-moore.com
Quoted 147 lines · Paul Moore
Quoted 132 lines · Stephen Smalley
Quoted 131 lines · Ondrej Mosnacek
This function has only two callers, but only one of them actually needs the special logic at the beginning. Factoring this logic out into
string_to_context_struct()allows us to drop the arguments 'oldc', 's', and 'def_sid'.Signed-off-by Ondrej Mosnacek <[email protected]>---
Changes in v3:
- correct the comment about policy read lockChanges in v2:
- also drop unneeded #include's frommls.csecurity/selinux/ss/mls.c | 49 +++++----------------------------- security/selinux/ss/mls.h | 5 +--- security/selinux/ss/services.c | 32 +++++++++++++++++++--- 3 files changed, 36 insertions(+), 50 deletions(-)security/selinux/ss/mls.c +6
-43@@ -24,10 +24,7 @@#include <linux/string.h> #include <linux/errno.h> #include <net/netlabel.h> -#include "sidtab.h" #include "mls.h" -#include "policydb.h" -#include "services.h" /* * Return the length in bytes for the MLS fields of the @@ -223,20 +220,12 @@ int mls_context_isvalid(struct policydb *p, struct context *c)* This function modifies the string in place, inserting * NULL characters to terminate the MLS fields. * -* If a def_sid is provided and no MLS field is present, -* copy the MLS field of the associated default context. -* Used for upgraded to MLS systems where objects may lack -* MLS fields. -* -* Policy read-lock must be held for sidtab lookup. +* Policy read-lock must be held for policy data lookup. * */ int mls_context_to_sid (struct policydb *pol, -char oldc,char *scontext, -struct context *context, -struct sidtab *s, -u32 def_sid) +struct context *context) {char *sensitivity, *cur_cat, *next_cat, *rngptr;struct level_datum *levdatum;@@ -244,29 +233,6 @@ int mls_context_to_sid(struct policydb *pol,int l, rc, i;char *rangep[2 ]; -if (!pol->mls_enabled) { -if ((def_sid !=SECSID_NULL && oldc) || (*scontext) =='\0' ) -return 0 ; -return -EINVAL ; - } - -/* -* No MLS component to the security context, try and map to -* default if provided. -*/ -if (!oldc) { -struct context *defcon; - -if (def_sid ==SECSID_NULL ) -return -EINVAL ; - - defcon =sidtab_search (s, def_sid); -if (!defcon) -return -EINVAL ; - -return mls_context_cpy (context, defcon); - } -/* * If we're dealing with a range, figure out where the two parts * of the range begin. @@ -364,14 +330,11 @@ int mls_from_string(struct policydb *p, char *str, struct context *context,return -EINVAL ; tmpstr =kstrdup (str, gfp_mask); -if (!tmpstr) { - rc = -ENOMEM ; - }else { - rc =mls_context_to_sid (p,':' , tmpstr, context, -NULL ,SECSID_NULL ); -kfree (tmpstr); - } +if (!tmpstr) +return -ENOMEM ; + rc =mls_context_to_sid (p, tmpstr, context); +kfree (tmpstr);return rc; }security/selinux/ss/mls.h +1
-4@@ -33,11 +33,8 @@ int mls_range_isvalid(struct policydb *p, struct mls_range *r);int mls_level_isvalid (struct policydb *p,struct mls_level *l);int mls_context_to_sid (struct policydb *p, -char oldc,char *scontext, -struct context *context, -struct sidtab *s, -u32 def_sid); +struct context *context);int mls_from_string (struct policydb *p,char *str,struct context *context,gfp_t gfp_mask);security/selinux/ss/services.c +0
-0@@ -1425,9 +1425,35 @@ static int string_to_context_struct(struct policydb *pol, ctx->type = typdatum->value;Quoted 13 lines
security/selinux/ss/services.c static int string_to_context_struct(struct policydb *pol, · line 1431
Quoted 7 lines
security/selinux/ss/services.c +4
-3- rc =mls_context_to_sid (pol, oldc, p, ctx, sidtabp, def_sid); -if (rc) - goto out; +if (!pol->mls_enabled) { + rc = -EINVAL ; +if ((def_sid ==SECSID_NULL || !oldc) && (*p) !='\0' ) + goto out;I don't think this is your bug, but unless I'm mistaken, p could be OOB and be dereferenced here. Seems to have been introduced by
95ffe194204ae3cef88d0b59be209204bbe9b3be. Likely not caught in testing since both Linux distributions and Android enable MLS to use the category sets for isolation.
Quoted 5 lines
Yep, and we should fix that in v4.20-rcX independent of this patch. I think if we simply remove the "(*scontext) == '\0'" from the check we should be okay; I believe the only time we would want to return 0 when not running a MLS policy would be when there is something in the MLS portion of the label.
I think I'll leave this part to you guys. I don't feel confident about changing the logic in this part of the code. I can't even parse the meaning of that scary if statement... Bug for bug compatibility was the best I could do here :)
Signature · 3 lines
Ondrej Mosnacek <omosnace at redhat dot com> Associate Software Engineer, Security Technologies Red Hat, Inc.
Quoted 147 lines · Paul Moore
Quoted 132 lines · Stephen Smalley
Quoted 131 lines · Ondrej Mosnacek
This function has only two callers, but only one of them actually needs the special logic at the beginning. Factoring this logic out into
string_to_context_struct()allows us to drop the arguments 'oldc', 's', and 'def_sid'.Signed-off-by Ondrej Mosnacek <[email protected]>---
Changes in v3:
- correct the comment about policy read lockChanges in v2:
- also drop unneeded #include's from mls.c security/selinux/ss/mls.c | 49 +++++----------------------------- security/selinux/ss/mls.h | 5 +--- security/selinux/ss/services.c | 32 +++++++++++++++++++--- 3 files changed, 36 insertions(+), 50 deletions(-)security/selinux/ss/mls.c +6
-43@@ -24,10 +24,7 @@#include <linux/string.h> #include <linux/errno.h> #include <net/netlabel.h> -#include "sidtab.h" #include "mls.h" -#include "policydb.h" -#include "services.h" /* * Return the length in bytes for the MLS fields of the @@ -223,20 +220,12 @@ int mls_context_isvalid(struct policydb *p, struct context *c)* This function modifies the string in place, inserting * NULL characters to terminate the MLS fields. * -* If a def_sid is provided and no MLS field is present, -* copy the MLS field of the associated default context. -* Used for upgraded to MLS systems where objects may lack -* MLS fields. -* -* Policy read-lock must be held for sidtab lookup. +* Policy read-lock must be held for policy data lookup. * */ int mls_context_to_sid (struct policydb *pol, -char oldc,char *scontext, -struct context *context, -struct sidtab *s, -u32 def_sid) +struct context *context) {char *sensitivity, *cur_cat, *next_cat, *rngptr;struct level_datum *levdatum;@@ -244,29 +233,6 @@ int mls_context_to_sid(struct policydb *pol,int l, rc, i;char *rangep[2 ]; -if (!pol->mls_enabled) { -if ((def_sid !=SECSID_NULL && oldc) || (*scontext) =='\0' ) -return 0 ; -return -EINVAL ; - } - -/* -* No MLS component to the security context, try and map to -* default if provided. -*/ -if (!oldc) { -struct context *defcon; - -if (def_sid ==SECSID_NULL ) -return -EINVAL ; - - defcon =sidtab_search (s, def_sid); -if (!defcon) -return -EINVAL ; - -return mls_context_cpy (context, defcon); - } -/* * If we're dealing with a range, figure out where the two parts * of the range begin. @@ -364,14 +330,11 @@ int mls_from_string(struct policydb *p, char *str, struct context *context,return -EINVAL ; tmpstr =kstrdup (str, gfp_mask); -if (!tmpstr) { - rc = -ENOMEM ; - }else { - rc =mls_context_to_sid (p,':' , tmpstr, context, -NULL ,SECSID_NULL ); -kfree (tmpstr); - } +if (!tmpstr) +return -ENOMEM ; + rc =mls_context_to_sid (p, tmpstr, context); +kfree (tmpstr);return rc; }security/selinux/ss/mls.h +1
-4@@ -33,11 +33,8 @@ int mls_range_isvalid(struct policydb *p, struct mls_range *r);int mls_level_isvalid (struct policydb *p,struct mls_level *l);int mls_context_to_sid (struct policydb *p, -char oldc,char *scontext, -struct context *context, -struct sidtab *s, -u32 def_sid); +struct context *context);int mls_from_string (struct policydb *p,char *str,struct context *context,gfp_t gfp_mask);security/selinux/ss/services.c +0
-0@@ -1425,9 +1425,35 @@ static int string_to_context_struct(struct policydb *pol, ctx->type = typdatum->value;Quoted 13 lines
security/selinux/ss/services.c static int string_to_context_struct(struct policydb *pol, · line 1431
Quoted 7 lines
security/selinux/ss/services.c +4
-3- rc =mls_context_to_sid (pol, oldc, p, ctx, sidtabp, def_sid); -if (rc) - goto out; +if (!pol->mls_enabled) { + rc = -EINVAL ; +if ((def_sid ==SECSID_NULL || !oldc) && (*p) !='\0' ) + goto out;I don't think this is your bug, but unless I'm mistaken, p could be OOB and be dereferenced here. Seems to have been introduced by
95ffe194204ae3cef88d0b59be209204bbe9b3be. Likely not caught in testing since both Linux distributions and Android enable MLS to use the category sets for isolation.
Quoted 5 lines
Yep, and we should fix that in v4.20-rcX independent of this patch. I think if we simply remove the "(*scontext) == '\0'" from the check we should be okay; I believe the only time we would want to return 0 when not running a MLS policy would be when there is something in the MLS portion of the label.
Yes, that should restore the previous behavior. The desired behavior is that if MLS is disabled, we reject the context as invalid if it has a MLS field unless the context was fetched from an xattr, which we only permit to provide filesystem compatibility between MLS and non-MLS systems.
Quoted 154 lines · Stephen Smalley
Quoted 153 lines · Paul Moore
Quoted 151 lines · Stephen Smalley
Quoted 134 lines · Ondrej Mosnacek
This function has only two callers, but only one of them actually needs the special logic at the beginning. Factoring this logic out into
string_to_context_struct()allows us to drop the arguments 'oldc', 's', and 'def_sid'.Signed-off-by Ondrej Mosnacek <[email protected]>---
Changes in v3:
- correct the comment about policy read lockChanges in v2:
- also drop unneeded #include's frommls.csecurity/selinux/ss/mls.c | 49
+++++-----------------------------
security/selinux/ss/mls.h | 5 +---security/selinux/ss/services.c| 32 +++++++++++++++++++---3 files changed, 36 insertions(+), 50 deletions(-)security/selinux/ss/mls.c +2
-13@@ -24,10 +24,7 @@ #include <linux/string.h> #include <linux/errno.h> #include <net/netlabel.h> -#include "sidtab.h" #include "mls.h" -#include "policydb.h" -#include "services.h" /** Return the length in bytes for the MLS fields of the @@ -223,20 +220,12 @@ int mls_context_isvalid(struct policydb *p,struct context *c) * This function modifies the string in place, inserting * NULL characters to terminate the MLS fields. * -* If a def_sid is provided and no MLS field is present, -* copy the MLS field of the associated default context. -* Used for upgraded to MLS systems where objects may lack -* MLS fields. -* -* Policy read-lock must be held for sidtab lookup. +* Policy read-lock must be held for policy data lookup. * */ int mls_context_to_sid(struct policydb *pol, - char oldc, char *scontext, - struct context *context, - struct sidtab *s, - u32 def_sid) + struct context *context) { char *sensitivity, *cur_cat, *next_cat, *rngptr;struct
level_datum*levdatum;security/selinux/ss/mls.c +0
-23@@ -244,29 +233,6 @@ int mls_context_to_sid(struct policydb *pol, int l, rc, i; char *rangep[2]; - if (!pol->mls_enabled) { - if ((def_sid != SECSID_NULL && oldc) || (*scontext) == '\0') - return 0; - return -EINVAL; - } - - /* - * No MLS component to the security context, try and map to - * default if provided. - */ - if (!oldc) { - struct context *defcon; - - if (def_sid == SECSID_NULL) - return -EINVAL; - - defcon = sidtab_search(s, def_sid); - if (!defcon) - return -EINVAL; - - return mls_context_cpy(context, defcon); - } - /** If we're dealing with a range, figure out where the two parts * of the range begin.
security/selinux/ss/mls.c +4
-7@@ -364,14 +330,11 @@ int mls_from_string(struct policydb *p, char *str, struct context *context, return -EINVAL; tmpstr = kstrdup(str, gfp_mask); - if (!tmpstr) { - rc = -ENOMEM; - } else { - rc = mls_context_to_sid(p, ':', tmpstr, context, - NULL, SECSID_NULL); - kfree(tmpstr); - } + if (!tmpstr) + return -ENOMEM; + rc = mls_context_to_sid(p, tmpstr, context); + kfree(tmpstr); return rc; }security/selinux/ss/mls.h +1
-4@@ -33,11 +33,8 @@ int mls_range_isvalid(struct policydb *p, struct mls_range *r); int mls_level_isvalid(struct policydb *p, struct mls_level *l); int mls_context_to_sid(struct policydb *p, - char oldc, char *scontext, - struct context *context, - struct sidtab *s, - u32 def_sid); + struct context *context); int mls_from_string(struct policydb *p, char *str, struct context *context,
gfp_tgfp_mask);security/selinux/ss/services.c +0
-0@@ -1425,9 +1425,35 @@ static int string_to_context_struct(struct policydb *pol,security/selinux/ss/services.c static int string_to_context_struct(struct · line 1432
Quoted 10 lines
security/selinux/ss/services.c +4
-3ctx->type = typdatum->value; - rc = mls_context_to_sid(pol, oldc, p, ctx, sidtabp, def_sid); - if (rc) - goto out; + if (!pol->mls_enabled) { + rc = -EINVAL; + if ((def_sid == SECSID_NULL || !oldc) && (*p) != '\0') + goto out;I don't think this is your bug, but unless I'm mistaken, p could be OOB and be dereferenced here. Seems to have been introduced by 95ffe194204ae3cef88d0b59be209204bbe9b3be. Likely not caught in testing since both Linux distributions and Android enable MLS to use the category sets for isolation.
Quoted 7 lines
Quoted 5 lines
Yep, and we should fix that in v4.20-rcX independent of this patch. I think if we simply remove the "(*scontext) == '\0'" from the check we should be okay; I believe the only time we would want to return 0 when not running a MLS policy would be when there is something in the MLS portion of the label.
Yes, that should restore the previous behavior. The desired behavior is that if MLS is disabled, we reject the context as invalid if it has a MLS field unless the context was fetched from an xattr, which we only permit to provide filesystem compatibility between MLS and non-MLS systems.
Sorry, that isn't correct. If MLS is disabled, then we want to return 0 if either there is no MLS field (!oldc) or the context came from an xattr (def_sid != SECSID_NULL). We should only return -EINVAL if there is a MLS field (oldc) and the context did not come from an xattr (def_sid == SECSID_NULL).
Paul Moore
maintainerQuoted 11 lines · Ondrej Mosnacek
This function has only two callers, but only one of them actually needs the special logic at the beginning. Factoring this logic out into
string_to_context_struct()allows us to drop the arguments 'oldc', 's', and 'def_sid'.Signed-off-by Ondrej Mosnacek <[email protected]>---
Changes in v3:
- correct the comment about policy read lock
Quoted 7 lines
Changes in v2:
- also drop unneeded #include's frommls.csecurity/selinux/ss/mls.c | 49 +++++----------------------------- security/selinux/ss/mls.h | 5 +--- security/selinux/ss/services.c | 32 +++++++++++++++++++--- 3 files changed, 36 insertions(+), 50 deletions(-)
What was the original motivation for this patch? Is there a performance issue?
I'm asking because I'm not really convinced this is an improvement. While I agree the number of function arguments is a bordering on "too many", I think I like having the logic in mls_context_to_sid() for right now.
Quoted 150 lines
security/selinux/ss/mls.c +6
-43@@ -24,10 +24,7 @@#include <linux/string.h> #include <linux/errno.h> #include <net/netlabel.h> -#include "sidtab.h" #include "mls.h" -#include "policydb.h" -#include "services.h" /* * Return the length in bytes for the MLS fields of the @@ -223,20 +220,12 @@ int mls_context_isvalid(struct policydb *p, struct context *c)* This function modifies the string in place, inserting * NULL characters to terminate the MLS fields. * -* If a def_sid is provided and no MLS field is present, -* copy the MLS field of the associated default context. -* Used for upgraded to MLS systems where objects may lack -* MLS fields. -* -* Policy read-lock must be held for sidtab lookup. +* Policy read-lock must be held for policy data lookup. * */ int mls_context_to_sid (struct policydb *pol, -char oldc,char *scontext, -struct context *context, -struct sidtab *s, -u32 def_sid) +struct context *context) {char *sensitivity, *cur_cat, *next_cat, *rngptr;struct level_datum *levdatum;@@ -244,29 +233,6 @@ int mls_context_to_sid(struct policydb *pol,int l, rc, i;char *rangep[2 ]; -if (!pol->mls_enabled) { -if ((def_sid !=SECSID_NULL && oldc) || (*scontext) =='\0' ) -return 0 ; -return -EINVAL ; - } - -/* -* No MLS component to the security context, try and map to -* default if provided. -*/ -if (!oldc) { -struct context *defcon; - -if (def_sid ==SECSID_NULL ) -return -EINVAL ; - - defcon =sidtab_search (s, def_sid); -if (!defcon) -return -EINVAL ; - -return mls_context_cpy (context, defcon); - } -/* * If we're dealing with a range, figure out where the two parts * of the range begin. @@ -364,14 +330,11 @@ int mls_from_string(struct policydb *p, char *str, struct context *context,return -EINVAL ; tmpstr =kstrdup (str, gfp_mask); -if (!tmpstr) { - rc = -ENOMEM ; - }else { - rc =mls_context_to_sid (p,':' , tmpstr, context, -NULL ,SECSID_NULL ); -kfree (tmpstr); - } +if (!tmpstr) +return -ENOMEM ; + rc =mls_context_to_sid (p, tmpstr, context); +kfree (tmpstr);return rc; }security/selinux/ss/mls.h +1
-4@@ -33,11 +33,8 @@ int mls_range_isvalid(struct policydb *p, struct mls_range *r);int mls_level_isvalid (struct policydb *p,struct mls_level *l);int mls_context_to_sid (struct policydb *p, -char oldc,char *scontext, -struct context *context, -struct sidtab *s, -u32 def_sid); +struct context *context);int mls_from_string (struct policydb *p,char *str,struct context *context,gfp_t gfp_mask);security/selinux/ss/services.c +29
-4@@ -1425,9 +1425,35 @@ static int string_to_context_struct(struct policydb *pol, ctx->type = typdatum->value; - rc =mls_context_to_sid (pol, oldc, p, ctx, sidtabp, def_sid); -if (rc) - goto out; +if (!pol->mls_enabled) { + rc = -EINVAL ; +if ((def_sid ==SECSID_NULL || !oldc) && (*p) !='\0' ) + goto out; + }else if (!oldc) { +/* +* If a def_sid is provided and no MLS field is present, +* copy the MLS field of the associated default context. +* Used for upgrading to MLS systems where objects may lack +* MLS fields. +*/ +struct context *defcon; + + rc = -EINVAL ; +if (def_sid ==SECSID_NULL ) + goto out; + + defcon =sidtab_search (sidtabp, def_sid); +if (!defcon) + goto out; + + rc =mls_context_cpy (ctx, defcon); +if (rc) + goto out; + }else { + rc =mls_context_to_sid (pol, p, ctx); +if (rc) + goto out; + }/* Check the validity of the new context. */ rc = -EINVAL ; --2.17.2
Signature · 2 lines
paul moore www.paul-moore.com
Quoted 15 lines · Paul Moore
Quoted 14 lines · Ondrej Mosnacek
This function has only two callers, but only one of them actually needs the special logic at the beginning. Factoring this logic out into
string_to_context_struct()allows us to drop the arguments 'oldc', 's', and 'def_sid'.Signed-off-by Ondrej Mosnacek <[email protected]>---
Changes in v3:
- correct the comment about policy read lockChanges in v2:
- also drop unneeded #include's frommls.c
Quoted 6 lines
Quoted 4 lines
security/selinux/ss/mls.c | 49 +++++----------------------------- security/selinux/ss/mls.h | 5 +--- security/selinux/ss/services.c | 32 +++++++++++++++++++--- 3 files changed, 36 insertions(+), 50 deletions(-)What was the original motivation for this patch? Is there a performance issue?
No, there is no performance issue that I know of. I simply wanted to move the sidtab_search() reference out of mls.c when I was adding the sid_to_context/content_to_sid wrappers to services.c. I have now dropped the wrappers in favor of just rewriting the sidtab functions, but the mls_context_to_sid() interface looked really awkward to me, especially considering that the 'ugly' part of the function is really used by only one caller, so I decided to post the patch anyway.
Quoted 5 lines
I'm asking because I'm not really convinced this is an improvement. While I agree the number of function arguments is a bordering on "too many", I think I like having the logic in
mls_context_to_sid()for right now.
I disagree, but I don't mind leaving it as it is if that's what you prefer.
Quoted 156 lines
Quoted 149 lines
security/selinux/ss/mls.c +6
-43@@ -24,10 +24,7 @@#include <linux/string.h> #include <linux/errno.h> #include <net/netlabel.h> -#include "sidtab.h" #include "mls.h" -#include "policydb.h" -#include "services.h" /* * Return the length in bytes for the MLS fields of the @@ -223,20 +220,12 @@ int mls_context_isvalid(struct policydb *p, struct context *c)* This function modifies the string in place, inserting * NULL characters to terminate the MLS fields. * -* If a def_sid is provided and no MLS field is present, -* copy the MLS field of the associated default context. -* Used for upgraded to MLS systems where objects may lack -* MLS fields. -* -* Policy read-lock must be held for sidtab lookup. +* Policy read-lock must be held for policy data lookup. * */ int mls_context_to_sid (struct policydb *pol, -char oldc,char *scontext, -struct context *context, -struct sidtab *s, -u32 def_sid) +struct context *context) {char *sensitivity, *cur_cat, *next_cat, *rngptr;struct level_datum *levdatum;@@ -244,29 +233,6 @@ int mls_context_to_sid(struct policydb *pol,int l, rc, i;char *rangep[2 ]; -if (!pol->mls_enabled) { -if ((def_sid !=SECSID_NULL && oldc) || (*scontext) =='\0' ) -return 0 ; -return -EINVAL ; - } - -/* -* No MLS component to the security context, try and map to -* default if provided. -*/ -if (!oldc) { -struct context *defcon; - -if (def_sid ==SECSID_NULL ) -return -EINVAL ; - - defcon =sidtab_search (s, def_sid); -if (!defcon) -return -EINVAL ; - -return mls_context_cpy (context, defcon); - } -/* * If we're dealing with a range, figure out where the two parts * of the range begin. @@ -364,14 +330,11 @@ int mls_from_string(struct policydb *p, char *str, struct context *context,return -EINVAL ; tmpstr =kstrdup (str, gfp_mask); -if (!tmpstr) { - rc = -ENOMEM ; - }else { - rc =mls_context_to_sid (p,':' , tmpstr, context, -NULL ,SECSID_NULL ); -kfree (tmpstr); - } +if (!tmpstr) +return -ENOMEM ; + rc =mls_context_to_sid (p, tmpstr, context); +kfree (tmpstr);return rc; }security/selinux/ss/mls.h +1
-4@@ -33,11 +33,8 @@ int mls_range_isvalid(struct policydb *p, struct mls_range *r);int mls_level_isvalid (struct policydb *p,struct mls_level *l);int mls_context_to_sid (struct policydb *p, -char oldc,char *scontext, -struct context *context, -struct sidtab *s, -u32 def_sid); +struct context *context);int mls_from_string (struct policydb *p,char *str,struct context *context,gfp_t gfp_mask);security/selinux/ss/services.c +29
-4@@ -1425,9 +1425,35 @@ static int string_to_context_struct(struct policydb *pol, ctx->type = typdatum->value; - rc =mls_context_to_sid (pol, oldc, p, ctx, sidtabp, def_sid); -if (rc) - goto out; +if (!pol->mls_enabled) { + rc = -EINVAL ; +if ((def_sid ==SECSID_NULL || !oldc) && (*p) !='\0' ) + goto out; + }else if (!oldc) { +/* +* If a def_sid is provided and no MLS field is present, +* copy the MLS field of the associated default context. +* Used for upgrading to MLS systems where objects may lack +* MLS fields. +*/ +struct context *defcon; + + rc = -EINVAL ; +if (def_sid ==SECSID_NULL ) + goto out; + + defcon =sidtab_search (sidtabp, def_sid); +if (!defcon) + goto out; + + rc =mls_context_cpy (ctx, defcon); +if (rc) + goto out; + }else { + rc =mls_context_to_sid (pol, p, ctx); +if (rc) + goto out; + }/* Check the validity of the new context. */ rc = -EINVAL ; --Quoted 2 lines
2.17.2
--
paul moore
www.paul-moore.com
Signature · 3 lines
Ondrej Mosnacek <omosnace at redhat dot com> Associate Software Engineer, Security Technologies Red Hat, Inc.