libsepol: Validate policydb values when reading binary
cover libsepol: Validate policydb values when reading binary
Nicolas Iooss reports that fuzzing /usr/libexec/hll/pp with the American Fuzzy Lop revealed that inconsistent policy modules could be created that caused NULL dereferences and other problems.
This patch validates the policydb when reading in the binary policy. See the description of the second patch for more details.
The validation requires a negligible amount of time to complete.
James Carter (2):
libsepol: Create function ebitmap_highest_set_bit()
libsepol: Validate policydb values when reading binary policy
libsepol/include/sepol/policydb/ebitmap.h | 1 + libsepol/src/ebitmap.c | 20 + libsepol/src/policydb.c | 35 +- libsepol/src/policydb_validate.c | 764 ++++++++++++++++++++++ libsepol/src/policydb_validate.h | 7 + 5 files changed, 815 insertions(+), 12 deletions(-) create mode 100644 libsepol/src/policydb_validate.c create mode 100644 libsepol/src/policydb_validate.h
Signature · 1 line
2.26.2
PATCH 1/2 libsepol: Create function ebitmap_highest_set_bit()
Create the function ebitmap_highest_set_bit() which returns the position of the highest bit set in the ebitmap.
The return value is valid only if the ebitmap is not empty. An empty ebitmap will return 0.
--- libsepol/include/sepol/policydb/ebitmap.h | 1 + libsepol/src/ebitmap.c | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+)
libsepol/include/sepol/policydb/ebitmap.h +1 -0
@@ -94,6 +94,7 @@ extern int ebitmap_contains(const ebitmap_t * e1, const ebitmap_t * e2);extern int ebitmap_match_any (const ebitmap_t *e1,const ebitmap_t *e2);extern int ebitmap_get_bit (const ebitmap_t * e,unsigned int bit);extern int ebitmap_set_bit (ebitmap_t * e,unsigned int bit,int value); +extern unsigned int ebitmap_highest_set_bit (ebitmap_t * e);extern void ebitmap_destroy (ebitmap_t * e);extern int ebitmap_read (ebitmap_t * e,void *fp);
libsepol/src/ebitmap.c +20 -0
@@ -344,6 +344,26 @@ int ebitmap_set_bit(ebitmap_t * e, unsigned int bit, int value)return 0 ; } +unsigned int ebitmap_highest_set_bit (ebitmap_t * e) +{ +ebitmap_node_t *n; +MAPTYPE map; +unsigned int pos =0 ; + + n = e->node; +if (!n) +return 0 ; + +while (n->next) + n = n->next; + + map = n->map; +while (map >>=1 ) + pos++; + +return n->startbit + pos; +} +void ebitmap_destroy (ebitmap_t * e) {ebitmap_node_t *n, *temp;
Signature · 1 line
2.26.2
PATCH 2/2 libsepol: Validate policydb values when reading binary policy
In reply to James CarterNicolas Iooss reports that fuzzing /usr/libexec/hll/pp with the American Fuzzy Lop revealed that inconsistent policy modules could be created that caused NULL dereferences and other problems.
When reading in a binary modular or kernel policy, check values in the policydb to verify consistency. When reading in the data for commons, classes, roles, types, users, booleans, sensitivities, and categories verify that their value is between 1 and the number of primary identifiers (value-1 is used to index the sym_val_to_name array for all of these and the val_to_struct array for classes, roles, users, and types.) Next all references in policy rules are checked to ensure that they refer to a valid value.
It is possible for the type and role struct and name arrays to have gaps in them. For roles, there will be gaps in the case of a kernel policy created from a policy with role attributes, but nothing in the policy will refer to any of the gaps. For types, there will be gaps for any kernel policy with a version from 20 to 23, but, unfortunately, there will be references to the gaps. This is because, while attributes exist in these policies, they only exist in the type_attr_map. For policies with versions between 20 and 23, it must be assumed that all of the gaps and any references to them are valid. To check for references to gaps, bitmaps are created to map where the gaps are and all values are verified to be within the proper range and not within a gap.
--- libsepol/src/policydb.c | 35 +- libsepol/src/policydb_validate.c | 764 +++++++++++++++++++++++++++++++ libsepol/src/policydb_validate.h | 7 + 3 files changed, 794 insertions(+), 12 deletions(-) create mode 100644 libsepol/src/policydb_validate.c create mode 100644 libsepol/src/policydb_validate.h
libsepol/src/policydb.c +23 -12
@@ -54,6 +54,7 @@#include "private.h" #include "debug.h" #include "mls.h" +#include "policydb_validate.h" #define POLICYDB_TARGET_SZ ARRAY_SIZE(policydb_target_strings)const char *policydb_target_strings[] = {POLICYDB_STRING ,POLICYDB_XEN_STRING };
@@ -993,7 +994,7 @@ static int common_index(hashtab_key_t key, hashtab_datum_t datum, void *datap) comdatum = (common_datum_t *) datum; p = (policydb_t *) datap; -if (!comdatum->s.value || comdatum->s.value > p->p_commons.nprim) +if (!value_isvalid (comdatum->s.value, p->p_commons.nprim))return -EINVAL ;if (p->p_common_val_to_name[comdatum->s.value -1 ] !=NULL )return -EINVAL ;
@@ -1009,7 +1010,7 @@ static int class_index(hashtab_key_t key, hashtab_datum_t datum, void *datap) cladatum = (class_datum_t *) datum; p = (policydb_t *) datap; -if (!cladatum->s.value || cladatum->s.value > p->p_classes.nprim) +if (!value_isvalid (cladatum->s.value, p->p_classes.nprim))return -EINVAL ;if (p->p_class_val_to_name[cladatum->s.value -1 ] !=NULL )return -EINVAL ;
@@ -1026,7 +1027,7 @@ static int role_index(hashtab_key_t key, hashtab_datum_t datum, void *datap) role = (role_datum_t *) datum; p = (policydb_t *) datap; -if (!role->s.value || role->s.value > p->p_roles.nprim) +if (!value_isvalid (role->s.value, p->p_roles.nprim))return -EINVAL ;if (p->p_role_val_to_name[role->s.value -1 ] !=NULL )return -EINVAL ;
@@ -1045,7 +1046,7 @@ static int type_index(hashtab_key_t key, hashtab_datum_t datum, void *datap) p = (policydb_t *) datap;if (typdatum->primary) { -if (!typdatum->s.value || typdatum->s.value > p->p_types.nprim) +if (!value_isvalid (typdatum->s.value, p->p_types.nprim))return -EINVAL ;if (p->p_type_val_to_name[typdatum->s.value -1 ] !=NULL )return -EINVAL ;
@@ -1064,7 +1065,7 @@ static int user_index(hashtab_key_t key, hashtab_datum_t datum, void *datap) usrdatum = (user_datum_t *) datum; p = (policydb_t *) datap; -if (!usrdatum->s.value || usrdatum->s.value > p->p_users.nprim) +if (!value_isvalid (usrdatum->s.value, p->p_users.nprim))return -EINVAL ;if (p->p_user_val_to_name[usrdatum->s.value -1 ] !=NULL )return -EINVAL ;
@@ -1083,8 +1084,7 @@ static int sens_index(hashtab_key_t key, hashtab_datum_t datum, void *datap) p = (policydb_t *) datap;if (!levdatum->isalias) { -if (!levdatum->level->sens || - levdatum->level->sens > p->p_levels.nprim) +if (!value_isvalid (levdatum->level->sens, p->p_levels.nprim))return -EINVAL ;if (p->p_sens_val_to_name[levdatum->level->sens -1 ] !=NULL )return -EINVAL ;
@@ -1103,7 +1103,7 @@ static int cat_index(hashtab_key_t key, hashtab_datum_t datum, void *datap) p = (policydb_t *) datap;if (!catdatum->isalias) { -if (!catdatum->s.value || catdatum->s.value > p->p_cats.nprim) +if (!value_isvalid (catdatum->s.value, p->p_cats.nprim))return -EINVAL ;if (p->p_cat_val_to_name[catdatum->s.value -1 ] !=NULL )return -EINVAL ;
@@ -1190,7 +1190,7 @@ int policydb_index_decls(sepol_handle_t * handle, 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) { +if (!value_isvalid (decl->decl_id, num_decls)) {ERR (handle,"invalid decl ID %u" , decl->decl_id);return -1 ; }
@@ -2060,7 +2060,7 @@ static int context_read_and_validate(context_struct_t * c,static int perm_read (policydb_t * p __attribute__ ((unused)),hashtab_t h, -struct policy_file *fp) +struct policy_file *fp,uint32_t nprim) {char *key =0 ;perm_datum_t *perdatum;
@@ -2081,6 +2081,8 @@ static int perm_read(policydb_t * p goto bad; perdatum->s.value =le32_to_cpu (buf[1 ]); +if (!value_isvalid (perdatum->s.value, nprim)) + goto bad;if (hashtab_insert (h, key, perdatum)) goto bad;
@@ -2129,7 +2131,7 @@ static int common_read(policydb_t * p, hashtab_t h, struct policy_file *fp) key[len] =0 ;for (i =0 ; i < nel; i++) { -if (perm_read (p, comdatum->permissions.table, fp)) +if (perm_read (p, comdatum->permissions.table, fp, comdatum->permissions.nprim)) goto bad; }
@@ -2295,7 +2297,7 @@ static int class_read(policydb_t * p, hashtab_t h, struct policy_file *fp) } }for (i =0 ; i < nel; i++) { -if (perm_read (p, cladatum->permissions.table, fp)) +if (perm_read (p, cladatum->permissions.table, fp, cladatum->permissions.nprim)) goto bad; }
@@ -3631,12 +3633,18 @@ static int range_read(policydb_t * p, struct policy_file *fp)if (rc <0 ) goto err; rt->source_type =le32_to_cpu (buf[0 ]); +if (!value_isvalid (rt->source_type, p->p_types.nprim)) + goto err; rt->target_type =le32_to_cpu (buf[1 ]); +if (!value_isvalid (rt->target_type, p->p_types.nprim)) + goto err;if (new_rangetr) { rc =next_entry (buf, fp, (sizeof (uint32_t )));if (rc <0 ) goto err; rt->target_class =le32_to_cpu (buf[0 ]); +if (!value_isvalid (rt->target_class, p->p_classes.nprim)) + goto err; }else rt->target_class = p->process_class; r =calloc (1 ,sizeof (*r));
@@ -4544,6 +4552,9 @@ int policydb_read(policydb_t * p, struct policy_file *fp, unsigned verbose) } } +if (validate_policydb (fp->handle, p)) + goto bad; +return POLICYDB_SUCCESS ; bad:return POLICYDB_ERROR ;
libsepol/src/policydb_validate.c new file +764 -0
764 lines not on this page · raw message
libsepol/src/policydb_validate.h new file +7 -0
@@ -0,0 +1,7 @@ +#include <stdint.h> + +#include <sepol/handle.h> +#include <sepol/policydb/policydb.h> + +int value_isvalid (uint32_t value,uint32_t nprim); +int validate_policydb (sepol_handle_t *handle,policydb_t *p);
Signature · 1 line
2.26.2
Quoted 23 lines · James Carter
Nicolas Iooss reports that fuzzing
/usr/libexec/hll/ppwith the American Fuzzy Lop revealed that inconsistent policy modules could be created that caused NULL dereferences and other problems.This patch validates the policydb when reading in the binary policy. See the description of the second patch for more details.
The validation requires a negligible amount of time to complete.
James Carter (2):
libsepol: Create functionebitmap_highest_set_bit()
libsepol: Validate policydb values when reading binary policylibsepol/include/sepol/policydb/ebitmap.h | 1 + libsepol/src/ebitmap.c | 20 + libsepol/src/policydb.c | 35 +- libsepol/src/policydb_validate.c | 764 ++++++++++++++++++++++ libsepol/src/policydb_validate.h | 7 + 5 files changed, 815 insertions(+), 12 deletions(-) create mode 100644 libsepol/src/policydb_validate.c create mode 100644 libsepol/src/policydb_validate.h
Quoted 3 lines
--
2.26.2
Hello,
Thanks for these patches! I tested them and the fuzzer I am using (which consists in running AFL on "pp") no longer crashed :) So I confirm they fixed the issues I was experiencing, and the code looks good.
Nicolas
Quoted 34 lines · Nicolas Iooss
Quoted 23 lines · James Carter
Nicolas Iooss reports that fuzzing
/usr/libexec/hll/ppwith the American Fuzzy Lop revealed that inconsistent policy modules could be created that caused NULL dereferences and other problems.This patch validates the policydb when reading in the binary policy. See the description of the second patch for more details.
The validation requires a negligible amount of time to complete.
James Carter (2):
libsepol: Create functionebitmap_highest_set_bit()
libsepol: Validate policydb values when reading binary policylibsepol/include/sepol/policydb/ebitmap.h | 1 + libsepol/src/ebitmap.c | 20 + libsepol/src/policydb.c | 35 +- libsepol/src/policydb_validate.c | 764 ++++++++++++++++++++++ libsepol/src/policydb_validate.h | 7 + 5 files changed, 815 insertions(+), 12 deletions(-) create mode 100644 libsepol/src/policydb_validate.c create mode 100644 libsepol/src/policydb_validate.hQuoted 3 lines
--
2.26.2Hello,
Thanks for these patches! I tested them and the fuzzer I am using (which consists in running AFL on "pp") no longer crashed :) So I confirm they fixed the issues I was experiencing, and the code looks good.
Quoted 3 lines
Acked-by Nicolas Iooss <[email protected]>Nicolas
Merged using --whitespace=fix
Thanks!
^&^ git am --whitespace=fix 2-2-libsepol-Validate-policydb-values-when-reading-binary-policy.patch
Applying: libsepol: Validate policydb values when reading binary policy .git/rebase-apply/patch:331: trailing whitespace.
return -1;
.git/rebase-apply/patch:590: trailing whitespace.
return 0;
.git/rebase-apply/patch:747: trailing whitespace.
return -1;
.git/rebase-apply/patch:763: trailing whitespace.
return -1;
.git/rebase-apply/patch:886: trailing whitespace.
return -1;