libsepol: do not modify p->p_roles.nprim in role_set_expand
PATCH 1/5 libsepol: do not modify p->p_roles.nprim in role_set_expand
There is no reason to modify the number of roles defined in a policy when no role is being inserted.
--- libsepol/src/expand.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
libsepol/src/expand.c +1 -1
@@ -2424,7 +2424,7 @@ int role_set_expand(role_set_t * x, ebitmap_t * r, policydb_t * out, policydb_tebitmap_init (r);if (x->flags &ROLE_STAR ) { -for (i =0 ; i < p->p_roles.nprim++; i++) +for (i =0 ; i < p->p_roles.nprim; i++)if (ebitmap_set_bit (r, i,1 ))return -1 ;return 0 ;
Signature · 1 line
2.10.2
PATCH 2/5 libsepol: do not check decl->symtab[i].nprim
Commit 02a7d77ef2bf ("libsepol: make parsing symbol table headers more robust") broke refpolicy build, because checkmodule generates avrule decl blocks with "decl->symtab[i].nprim = 0" for all possible i, even when decl->symtab[SYM_ROLES] and decl->symtab[SYM_TYPES] are not empty.
More precisely, decl->symtab[i].nprim seems to be only updated in libsepol/src/link.c (in *_copy_callback() functions).
Revert the buggy part of commit 02a7d77ef2bf to fix this regression.
--- libsepol/src/policydb.c | 4 ---- 1 file changed, 4 deletions(-)
libsepol/src/policydb.c +0 -4
@@ -3510,10 +3510,6 @@ static int avrule_decl_read(policydb_t * p, avrule_decl_t * decl,return -1 ; nprim =le32_to_cpu (buf[0 ]); nel =le32_to_cpu (buf[1 ]); -if (nel && !nprim) { -ERR (fp->handle,"unexpected items in decl symbol table with no symbol" ); -return -1 ; - }for (j =0 ; j < nel; j++) {if (read_f[i] (p, decl->symtab[i].table, fp)) {return -1 ;
Signature · 1 line
2.10.2
Stephen Smalley
AppliedRe: [PATCH 2/5] libsepol: do not check decl->symtab[i].nprim
Quoted 10 lines · Nicolas Iooss
Commit
02a7d77ef2bf("libsepol: make parsing symbol table headers more robust") broke refpolicy build, because checkmodule generates avrule decl blocks with "decl->symtab[i].nprim = 0" for all possible i, even when decl->symtab[SYM_ROLES] and decl->symtab[SYM_TYPES] are not empty.More precisely, decl->symtab[i].nprim seems to be only updated in
libsepol/src/link.c(in*_copy_callback()functions).Revert the buggy part of commit
02a7d77ef2bfto fix this regression.
Thanks, applied.
Quoted 22 lines
Signed-off-by Nicolas Iooss <[email protected]>--- libsepol/src/policydb.c | 4 ---- 1 file changed, 4 deletions(-)libsepol/src/policydb.c +0
-4@@ -3510,10 +3510,6 @@ static int avrule_decl_read(policydb_t * p, avrule_decl_t * decl,return -1 ; nprim =le32_to_cpu (buf[0 ]); nel =le32_to_cpu (buf[1 ]); -if (nel && !nprim) { -ERR (fp->handle,"unexpected items in decl symbol table with no symbol" ); -return -1 ; - }for (j =0 ; j < nel; j++) {if (read_f[i] (p, decl->symtab[i].table, fp)) {return -1 ;
PATCH 3/5 libsepol: ebitmap: reject loading bitmaps with incorrect high bit
In reply to Nicolas IoossCurrently ebitmap_load() accepts loading a bitmap with highbit=192 and one node {startbit=0, map=0x2}. When iterating over the bitmap, ebitmap_for_each_bit() is expected to only yield "1" but it gives the following bits: 1, 65, 129.
This is due to two facts in ebitmap_for_each_bit() implementation:
* ebitmap_next() stays on the first (and only) node of the bitmap instead of stopping the iteration.
* the end condition of the for loop consists in comparing the bit with ebitmap_length() (ie. the bitmap highbit), which is above the limit of the last node here.
These are not bugs when the bitmap highbit is equals to l->startbit+MAPSIZE, where l is the last node (this is how ebitmap_set_bit() sets it). So a simple fix consists in making ebitmap_load() reject bitmaps which are loaded with an invalid highbit value.
This issue has been found while fuzzing semodule_package with the American Fuzzy Lop.
--- libsepol/src/ebitmap.c | 6 ++++++ 1 file changed, 6 insertions(+)
libsepol/src/ebitmap.c +6 -0
@@ -453,6 +453,12 @@ int ebitmap_read(ebitmap_t * e, void *fp) l = n; } +if (count && l->startbit +MAPSIZE != e->highbit) { +printf + ("security: ebitmap: hight bit %u has not the expected value %zu\n" , + e->highbit, l->startbit +MAPSIZE ); + goto bad; + } ok: rc =0 ;
Signature · 1 line
2.10.2
Stephen Smalley
AppliedRe: [PATCH 3/5] libsepol: ebitmap: reject loading bitmaps with incorrect high bit
Quoted 12 lines · Nicolas Iooss
Currently
ebitmap_load()accepts loading a bitmap with highbit=192 and one node {startbit=0, map=0x2}. When iterating over the bitmap,ebitmap_for_each_bit()is expected to only yield "1" but it gives the following bits: 1, 65, 129.This is due to two facts in
ebitmap_for_each_bit()implementation:
*ebitmap_next()stays on the first (and only) node of the bitmap instead of stopping the iteration.
* the end condition of the for loop consists in comparing the bit withebitmap_length()(ie. the bitmap highbit), which is above the limit of the last node here.
Quoted 8 lines
These are not bugs when the bitmap highbit is equals to l->startbit+MAPSIZE, where l is the last node (this is how
ebitmap_set_bit()sets it). So a simple fix consists in makingebitmap_load()reject bitmaps which are loaded with an invalid highbit value.This issue has been found while fuzzing
semodule_packagewith the American Fuzzy Lop.
Thanks, applied
Quoted 24 lines
Signed-off-by Nicolas Iooss <[email protected]>--- libsepol/src/ebitmap.c | 6 ++++++ 1 file changed, 6 insertions(+)libsepol/src/ebitmap.c +6
-0@@ -453,6 +453,12 @@ int ebitmap_read(ebitmap_t * e, void *fp) l = n; } +if (count && l->startbit +MAPSIZE != e->highbit) { +printf + ("security: ebitmap: hight bit %u has not the expected value %zu\n" , + e->highbit, l->startbit +MAPSIZE ); + goto bad; + } ok: rc =0 ;
PATCH 4/5 libsepol: check decl_id bounds before using it
In reply to Nicolas IoossWhen loading an invalid module which uses a declaration ID 0, semodule_package crashes in policydb_index_decls():
p->decl_val_to_struct[decl->decl_id - 1] = decl;
gdb shows the following stack trace:
#0 0x00007ffff7aa1bbd in policydb_index_decls (p=p@entry=0x605360)
at policydb.c:1034
#1 0x00007ffff7aaa9fc in policydb_read (p=<optimized out>,
fp=fp@entry=0x605090, verbose=verbose@entry=0) at policydb.c:3958
#2 0x00007ffff7ab4764 in sepol_policydb_read (p=<optimized out>,
pf=pf@entry=0x605090) at policydb_public.c:174
#3 0x0000000000401d33 in main (argc=<optimized out>,
argv=0x7fffffffdc88) at semodule_package.c:220Change policydb_index_decls() to report an error instead:
libsepol.policydb_index_decls: invalid decl ID 0
--- libsepol/src/policydb.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
libsepol/src/policydb.c +5 -1
@@ -1011,7 +1011,7 @@ int policydb_index_decls(policydb_t * p) {avrule_block_t *curblock;avrule_decl_t *decl; -int num_decls =0 ; +unsigned int num_decls =0 ;free (p->decl_val_to_struct);
@@ -1031,6 +1031,10 @@ int policydb_index_decls(policydb_t * p)for (curblock = p->global; curblock !=NULL ; curblock = curblock->next) {for (decl = curblock->branch_list; decl !=NULL ; decl = decl->next) { +if (decl->decl_id <1 || decl->decl_id > num_decls) { +ERR (NULL ,"invalid decl ID %u" , decl->decl_id); +return -1 ; + } p->decl_val_to_struct[decl->decl_id -1 ] = decl; } }
Signature · 1 line
2.10.2
Re: [PATCH 4/5] libsepol: check decl_id bounds before using it
Quoted 39 lines · Nicolas Iooss
When loading an invalid module which uses a declaration ID 0,
semodule_packagecrashes inpolicydb_index_decls():p->decl_val_to_struct[decl->decl_id - 1] = decl;gdb shows the following stack trace:
#0 0x00007ffff7aa1bbd in policydb_index_decls (p=p@entry=0x605360) at policydb.c:1034 #1 0x00007ffff7aaa9fc in policydb_read (p=<optimized out>, fp=fp@entry=0x605090, verbose=verbose@entry=0) at policydb.c:3958 #2 0x00007ffff7ab4764 in sepol_policydb_read (p=<optimized out>, pf=pf@entry=0x605090) at policydb_public.c:174 #3 0x0000000000401d33 in main (argc=<optimized out>, argv=0x7fffffffdc88) at semodule_package.c:220Change
policydb_index_decls()to report an error instead:libsepol.policydb_index_decls: invalid decl ID 0
Signed-off-by Nicolas Iooss <[email protected]>--- libsepol/src/policydb.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)libsepol/src/policydb.c +1
-1@@ -1011,7 +1011,7 @@ int policydb_index_decls(policydb_t * p) {avrule_block_t *curblock;avrule_decl_t *decl; -int num_decls =0 ; +unsigned int num_decls =0 ;free (p->decl_val_to_struct);
libsepol/src/policydb.c int policydb_index_decls(policydb_t * p) · line 1035
Quoted 5 lines
libsepol/src/policydb.c +2
-0@@ -1031,6 +1031,10 @@ int policydb_index_decls(policydb_t * p)for (curblock = p->global; curblock !=NULL ; curblock = curblock->next) {for (decl = curblock->branch_list; decl !=NULL ; decl = decl->next) { +if (decl->decl_id <1 || decl->decl_id > num_decls) { +ERR (NULL ,"invalid decl ID %u" , decl->decl_id);
If we can avoid passing NULL to ERR(), we should; it is only supported for legacy callers that did not have a handle in the interface. In this case, this just requires passing fp->handle from the caller to policydb_index_decls().
Quoted 6 lines
libsepol/src/policydb.c +2
-0+return -1 ; + } p->decl_val_to_struct[decl->decl_id -1 ] = decl; } }
PATCH 5/5 libsepol: detect duplicated symbol IDs
In reply to Nicolas IoossA valid policy would not have two symbols (classes, roles, users...) sharing the same unique identifier. Make policydb_read() rejects such policy files.
When ..._val_to_name translation tables were allocated with malloc(), change to calloc() in order to initialize the tables with NULLs.
--- libsepol/src/conditional.c | 5 +++++ libsepol/src/policydb.c | 45 ++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 45 insertions(+), 5 deletions(-)
libsepol/src/conditional.c +5 -0
@@ -25,6 +25,7 @@#include <sepol/policydb/conditional.h> #include "private.h" +#include "debug.h" /* move all type rules to top of t/f lists to help kernel on evaluation */ static void cond_optimize (cond_av_list_t ** l)
@@ -551,6 +552,10 @@ int cond_index_bool(hashtab_key_t key, hashtab_datum_t datum, void *datap)if (!booldatum->s.value || booldatum->s.value > p->p_bools.nprim)return -EINVAL ; +if (p->p_bool_val_to_name[booldatum->s.value -1 ] !=NULL ) { +ERR (NULL ,"duplicated boolean ID %u" , booldatum->s.value); +return -EINVAL ; + } p->p_bool_val_to_name[booldatum->s.value -1 ] = key; p->bool_val_to_struct[booldatum->s.value -1 ] = booldatum;
libsepol/src/policydb.c +40 -5
116 lines not on this page · raw message
Signature · 1 line
2.10.2
Re: [PATCH 5/5] libsepol: detect duplicated symbol IDs
Quoted 26 lines · Nicolas Iooss
A valid policy would not have two symbols (classes, roles, users...) sharing the same unique identifier. Make
policydb_read()rejects such policy files.When ..._val_to_name translation tables were allocated with
malloc(), change tocalloc()in order to initialize the tables with NULLs.Signed-off-by Nicolas Iooss <[email protected]>--- libsepol/src/conditional.c | 5 +++++ libsepol/src/policydb.c | 45 ++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 45 insertions(+), 5 deletions(-)libsepol/src/conditional.c +1
-0@@ -25,6 +25,7 @@#include <sepol/policydb/conditional.h> #include "private.h" +#include "debug.h" /* move all type rules to top of t/f lists to help kernel on evaluation */ static void cond_optimize (cond_av_list_t ** l)
libsepol/src/conditional.c int cond_index_bool(hashtab_key_t key, hashtab_datum_t datum, void *datap) · line 556
Quoted 5 lines
libsepol/src/conditional.c +2
-0@@ -551,6 +552,10 @@ int cond_index_bool(hashtab_key_t key, hashtab_datum_t datum, void *datap)if (!booldatum->s.value || booldatum->s.value > p->p_bools.nprim)return -EINVAL ; +if (p->p_bool_val_to_name[booldatum->s.value -1 ] !=NULL ) { +ERR (NULL ,"duplicated boolean ID %u" , booldatum->s.value);
Similarly, we could pass down the fp->handle from the caller by wrapping the policydb_t *p and the sepol_handle_t *handle in an args struct and passing that as the (void*) arg to the index functions. Or we could live without this level of error messages and just return failure silently.
Quoted 21 lines
libsepol/src/conditional.c +2
-0+return -EINVAL ; + } p->p_bool_val_to_name[booldatum->s.value -1 ] = key; p->bool_val_to_struct[booldatum->s.value -1 ] = booldatum;libsepol/src/policydb.c +4
-0@@ -849,6 +849,10 @@ static int common_index(hashtab_key_t key, hashtab_datum_t datum, void *datap) p = (policydb_t *) datap;if (!comdatum->s.value || comdatum->s.value > p->p_commons.nprim)return -EINVAL ; +if (p->p_common_val_to_name[comdatum->s.value -1 ] !=NULL ) { +ERR (NULL ,"duplicated common ID %u" , comdatum->s.value); +return -EINVAL ; + } p->p_common_val_to_name[comdatum->s.value -1 ] = (char *)key;return 0 ;
libsepol/src/policydb.c static int class_index(hashtab_key_t key, hashtab_datum_t datum, void *datap) · line 871
Quoted 5 lines
libsepol/src/policydb.c +2
-0@@ -863,6 +867,11 @@ static int class_index(hashtab_key_t key, hashtab_datum_t datum, void *datap) p = (policydb_t *) datap;if (!cladatum->s.value || cladatum->s.value > p->p_classes.nprim)return -EINVAL ; +if (p->p_class_val_to_name[cladatum->s.value -1 ] !=NULL || + p->class_val_to_struct[cladatum->s.value -1 ] !=NULL ) {
Do we really need to test both?
Quoted 111 lines
libsepol/src/policydb.c +34
-5+ERR (NULL ,"duplicated class ID %u" , cladatum->s.value); +return -EINVAL ; + } p->p_class_val_to_name[cladatum->s.value -1 ] = (char *)key; p->class_val_to_struct[cladatum->s.value -1 ] = cladatum;@@ -878,6 +887,11 @@ static int role_index(hashtab_key_t key, hashtab_datum_t datum, void *datap) p = (policydb_t *) datap;if (!role->s.value || role->s.value > p->p_roles.nprim)return -EINVAL ; +if (p->p_role_val_to_name[role->s.value -1 ] !=NULL || + p->role_val_to_struct[role->s.value -1 ] !=NULL ) { +ERR (NULL ,"duplicated role ID %u" , role->s.value); +return -EINVAL ; + } p->p_role_val_to_name[role->s.value -1 ] = (char *)key; p->role_val_to_struct[role->s.value -1 ] = role;@@ -895,6 +909,11 @@ static int type_index(hashtab_key_t key, hashtab_datum_t datum, void *datap)if (typdatum->primary) {if (!typdatum->s.value || typdatum->s.value > p->p_types.nprim)return -EINVAL ; +if (p->p_type_val_to_name[typdatum->s.value -1 ] !=NULL || + p->type_val_to_struct[typdatum->s.value -1 ] !=NULL ) { +ERR (NULL ,"duplicated type ID %u" , typdatum->s.value); +return -EINVAL ; + } p->p_type_val_to_name[typdatum->s.value -1 ] = (char *)key; p->type_val_to_struct[typdatum->s.value -1 ] = typdatum; }@@ -912,7 +931,11 @@ static int user_index(hashtab_key_t key, hashtab_datum_t datum, void *datap)if (!usrdatum->s.value || usrdatum->s.value > p->p_users.nprim)return -EINVAL ; - +if (p->p_user_val_to_name[usrdatum->s.value -1 ] !=NULL || + p->user_val_to_struct[usrdatum->s.value -1 ] !=NULL ) { +ERR (NULL ,"duplicated user ID %u" , usrdatum->s.value); +return -EINVAL ; + } p->p_user_val_to_name[usrdatum->s.value -1 ] = (char *)key; p->user_val_to_struct[usrdatum->s.value -1 ] = usrdatum;@@ -931,6 +954,10 @@ static int sens_index(hashtab_key_t key, hashtab_datum_t datum, void *datap)if (!levdatum->level->sens || levdatum->level->sens > p->p_levels.nprim)return -EINVAL ; +if (p->p_sens_val_to_name[levdatum->level->sens -1 ] !=NULL ) { +ERR (NULL ,"duplicated sensitivity level %u" , levdatum->level->sens); +return -EINVAL ; + } p->p_sens_val_to_name[levdatum->level->sens -1 ] = (char *)key; }@@ -948,6 +975,10 @@ static int cat_index(hashtab_key_t key, hashtab_datum_t datum, void *datap)if (!catdatum->isalias) {if (!catdatum->s.value || catdatum->s.value > p->p_cats.nprim)return -EINVAL ; +if (p->p_cat_val_to_name[catdatum->s.value -1 ] !=NULL ) { +ERR (NULL ,"duplicated cat ID %u" , catdatum->s.value); +return -EINVAL ; + } p->p_cat_val_to_name[catdatum->s.value -1 ] = (char *)key; }@@ -968,7 +999,7 @@ int policydb_index_classes(policydb_t * p) {free (p->p_common_val_to_name); p->p_common_val_to_name = (char **) -malloc (p->p_commons.nprim *sizeof (char *)); +calloc (p->p_commons.nprim,sizeof (char *));if (!p->p_common_val_to_name)return -1 ;@@ -977,13 +1008,13 @@ int policydb_index_classes(policydb_t * p)free (p->class_val_to_struct); p->class_val_to_struct = (class_datum_t **) -malloc (p->p_classes.nprim *sizeof (class_datum_t *)); +calloc (p->p_classes.nprim,sizeof (class_datum_t *));if (!p->class_val_to_struct)return -1 ;free (p->p_class_val_to_name); p->p_class_val_to_name = (char **) -malloc (p->p_classes.nprim *sizeof (char *)); +calloc (p->p_classes.nprim,sizeof (char *));if (!p->p_class_val_to_name)return -1 ;@@ -999,7 +1030,7 @@ int policydb_index_bools(policydb_t * p)if (cond_init_bool_indexes (p) ==-1 )return -1 ; p->p_bool_val_to_name = (char **) -malloc (p->p_bools.nprim *sizeof (char *)); +calloc (p->p_bools.nprim,sizeof (char *));if (!p->p_bool_val_to_name)return -1 ;if (hashtab_map (p->p_bools.table, cond_index_bool, p))@@ -1035,6 +1066,10 @@ int policydb_index_decls(policydb_t * p)ERR (NULL ,"invalid decl ID %u" , decl->decl_id);return -1 ; } +if (p->decl_val_to_struct[decl->decl_id -1 ] !=NULL ) { +ERR (NULL ,"duplicated decl ID %u" , decl->decl_id); +return -1 ; + } p->decl_val_to_struct[decl->decl_id -1 ] = decl; } }
Re: [PATCH 5/5] libsepol: detect duplicated symbol IDs
Quoted 30 lines · Stephen Smalley
Quoted 29 lines · Nicolas Iooss
A valid policy would not have two symbols (classes, roles, users...) sharing the same unique identifier. Make
policydb_read()rejects such policy files.When ..._val_to_name translation tables were allocated with
malloc(), change tocalloc()in order to initialize the tables with NULLs.Signed-off-by Nicolas Iooss <[email protected]>--- libsepol/src/conditional.c | 5 +++++ libsepol/src/policydb.c | 45 ++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 45 insertions(+), 5 deletions(-)libsepol/src/conditional.c +1
-0@@ -25,6 +25,7 @@#include <sepol/policydb/conditional.h> #include "private.h" +#include "debug.h" /* move all type rules to top of t/f lists to help kernel on evaluation */ static void cond_optimize (cond_av_list_t ** l)@@ -551,6 +552,10 @@ int cond_index_bool(hashtab_key_t key, hashtab_datum_t datum, void *datap)if (!booldatum->s.value || booldatum->s.value > p->p_bools.nprim)return -EINVAL ;
Quoted 7 lines
libsepol/src/conditional.c int cond_index_bool(hashtab_key_t key, hashtab_datum_t datum, void *datap) · line 556
Quoted 2 lines
libsepol/src/conditional.c +2
-0+if (p->p_bool_val_to_name[booldatum->s.value -1 ] !=NULL ) { +ERR (NULL ,"duplicated boolean ID %u" , booldatum->s.value);Similarly, we could pass down the
fp->handlefrom the caller by wrapping thepolicydb_t*p and thesepol_handle_t*handle in an args struct and passing that as the (void*) arg to the index functions. Or we could live without this level of error messages and just return failure silently.
With the first option, the prototype of indexing functions policydb_index_...() would need to be updated to take a handle argument. This change in the API would be propagated to checkpolicy, which uses these functions.
The second option (return -EINVAL without printing anything) is already what is done when the value is out of bounds and is much simpler to implement, so it is the one I prefer. I will send a v2 with this.
[...]
Quoted 8 lines
libsepol/src/conditional.c static int class_index(hashtab_key_t key, hashtab_datum_t datum, void *datap) · line 871
Quoted 6 lines
libsepol/src/conditional.c +2
-0@@ -863,6 +867,11 @@ static int class_index(hashtab_key_t key, hashtab_datum_t datum, void *datap) p = (policydb_t *) datap;if (!cladatum->s.value || cladatum->s.value > p->p_classes.nprim)return -EINVAL ; +if (p->p_class_val_to_name[cladatum->s.value -1 ] !=NULL || + p->class_val_to_struct[cladatum->s.value -1 ] !=NULL ) {Do we really need to test both?
As p->..._val_to_struct and p->p_..._val_to_name arrays are always updated together, I agree one of the test is redundant. I will drop the second one in the v2 (and I will also remove the extra whitespace which managed to slip in the condition).
Thanks for your review,
Nicolas
Stephen Smalley
AppliedQuoted 2 lines · Nicolas Iooss
There is no reason to modify the number of roles defined in a policy when no role is being inserted.
Thanks, applied.
Quoted 20 lines
Signed-off-by Nicolas Iooss <[email protected]>--- libsepol/src/expand.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)libsepol/src/expand.c +1
-1@@ -2424,7 +2424,7 @@ int role_set_expand(role_set_t * x, ebitmap_t * r, policydb_t * out, policydb_tebitmap_init (r);if (x->flags &ROLE_STAR ) { -for (i =0 ; i < p->p_roles.nprim++; i++) +for (i =0 ; i < p->p_roles.nprim; i++)if (ebitmap_set_bit (r, i,1 ))return -1 ;return 0 ;