Fix AFL Found Bugs in libsepol
With the patches mentioned below, I have been able to run the AFL fuzzer for 3 days without issue. I didn't get much feedback in v2 of the patchset, but these should address the issues in that series.
[PATCH v3 1/7] libsepol: fix invalid access of NULL on [PATCH v3 2/7] libsepol: ensure key is valid before doing search [PATCH v3 3/7] ebitmap: detect invalid bitmap
[PATCH v3 4/7] genfs_read: fix use heap-use-after-free [PATCH v3 5/7] libsepol: fix overflow and 0 length allocations [PATCH v3 6/7] libsepol: bound attr_type_map access by nprim [PATCH v3 7/7] libsepol: fix unitialized jmp and invalid dereference
PATCH v3 1/7 libsepol: fix invalid access of NULL on type_val_to_struct
From: William Roberts <[email protected]>
In type_set_expand:
When nprim, the table index counter, is greater than the value of initizalized entries in the type_val_to_struct[] array, detect this as invalid and return an error.
--- libsepol/src/expand.c | 4 ++++ 1 file changed, 4 insertions(+)
libsepol/src/expand.c +4 -0
@@ -2514,6 +2514,10 @@ int type_set_expand(type_set_t * set, ebitmap_t * t, policydb_t * p,if (i > p->p_types.nprim -1 ) goto err_types; +if (!p->type_val_to_struct[i]) { + goto err_types; + } +if (p->type_val_to_struct[i]->flavor ==TYPE_ATTRIB ) { if (ebitmap_union
Signature · 1 line
1.9.1
PATCH v3 2/7 libsepol: ensure key is valid before doing search
In reply to [email protected]From: William Roberts <[email protected]>
--- libsepol/src/mls.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-)
libsepol/src/mls.c +7 -5
@@ -262,6 +262,7 @@ int mls_context_isvalid(const policydb_t * p, const context_struct_t * c)user_datum_t *usrdatum;unsigned int i, l;ebitmap_node_t *cnode; +hashtab_key_t key;if (!p->mls)return 1 ;
@@ -279,11 +280,12 @@ int mls_context_isvalid(const policydb_t * p, const context_struct_t * c)if (!c->range.level[l].sens || c->range.level[l].sens > p->p_levels.nprim)return 0 ; - levdatum = (level_datum_t *)hashtab_search (p->p_levels.table, - p-> - p_sens_val_to_name - [c->range.level[l]. - sens -1 ]); + + key = p->p_sens_val_to_name[c->range.level[l].sens -1 ]; +if (!key) +return 0 ; + + levdatum = (level_datum_t *)hashtab_search (p->p_levels.table, key);if (!levdatum)return 0 ;
Signature · 1 line
1.9.1
PATCH v3 3/7 ebitmap: detect invalid bitmap
In reply to [email protected]From: William Roberts <[email protected]>
When count is 0 and the highbit is not zero, the ebitmap is not valid and the internal node is not allocated. This causes issues when routines, like mls_context_isvalid() attempt to use the ebitmap_for_each_bit() and ebitmap_node_get_bit() as they assume a highbit > 0 will have a node allocated.
--- libsepol/src/ebitmap.c | 4 ++++ 1 file changed, 4 insertions(+)
libsepol/src/ebitmap.c +4 -0
@@ -394,6 +394,10 @@ int ebitmap_read(ebitmap_t * e, void *fp) e->highbit, MAPSIZE); goto bad; } + +if (e->highbit && !count) + goto bad; + l =NULL ;for (i =0 ; i < count; i++) { rc =next_entry (buf, fp,sizeof (uint32_t ));
Signature · 1 line
1.9.1
PATCH v3 4/7 genfs_read: fix use heap-use-after-free
In reply to [email protected]From: William Roberts <[email protected]>
The newc variable is calloc'd and assigned to a new owner during a loop. After the first assignment of newc to newgenfs->head, the subsequent iteration could fail before the newc is reseated with a new heap allocation pointer. When the subsequent iteration fails, the newc variable is freed. Later, an attempt it made to free the same pointer assigned to newgenfs->head.
To correct this, clear newc after every loop iteration.
--- libsepol/src/policydb.c | 2 ++ 1 file changed, 2 insertions(+)
libsepol/src/policydb.c +2 -0
@@ -2812,6 +2812,8 @@ static int genfs_read(policydb_t * p, struct policy_file *fp) l->next = newc; else newgenfs->head = newc; +/* clear newc after a new owner has the pointer */ + newc =NULL ; } }
Signature · 1 line
1.9.1
PATCH v3 5/7 libsepol: fix overflow and 0 length allocations
In reply to [email protected]From: William Roberts <[email protected]>
Throughout libsepol, values taken from sepolicy are used in places where length == 0 or length == <saturated> matter, find and fix these.
Also, correct any type mismatches noticed along the way.
--- libsepol/src/conditional.c | 3 ++- libsepol/src/context.c | 16 ++++++++----- libsepol/src/context_record.c | 52 ++++++++++++++++++++++++------------------- libsepol/src/module.c | 13 +++++++++++ libsepol/src/module_to_cil.c | 4 +++- libsepol/src/policydb.c | 52 ++++++++++++++++++++++++++++++++++++++++--- libsepol/src/private.h | 3 +++ 7 files changed, 110 insertions(+), 33 deletions(-)
libsepol/src/conditional.c +2 -1
@@ -589,7 +589,8 @@ int cond_read_bool(policydb_t * p, goto err; len =le32_to_cpu (buf[2 ]); - +if (zero_or_saturated (len)) + goto err; key =malloc (len +1 );if (!key) goto err;
libsepol/src/context.c +11 -5
@@ -10,6 +10,7 @@#include "context.h" #include "handle.h" #include "mls.h" +#include "private.h" /* ----- Compatibility ---- */ int policydb_context_isvalid (const policydb_t * p,const context_struct_t * c)
@@ -297,10 +298,18 @@ int context_from_string(sepol_handle_t * handle,char *con_cpy =NULL ;sepol_context_t *ctx_record =NULL ; +if (zero_or_saturated (con_str_len)) { +ERR (handle,"Invalid context length" ); + goto err; + } +/* sepol_context_from_string expects a NULL-terminated string */ con_cpy =malloc (con_str_len +1 ); -if (!con_cpy) - goto omem; +if (!con_cpy) { +ERR (handle,"out of memory" ); + goto err; + } +memcpy (con_cpy, con_str, con_str_len); con_cpy[con_str_len] ='\0' ;
@@ -315,9 +324,6 @@ int context_from_string(sepol_handle_t * handle,sepol_context_free (ctx_record);return STATUS_SUCCESS ; - omem: -ERR (handle,"out of memory" ); - err:ERR (handle,"could not create context structure" );free (con_cpy);
libsepol/src/context_record.c +29 -23
74 lines not on this page · raw message
libsepol/src/module.c +13 -0
@@ -30,6 +30,7 @@#include <stdio.h> #include <stdlib.h> #include <limits.h> +#include <inttypes.h> #define SEPOL_PACKAGE_SECTION_FC 0xf97cff90#define SEPOL_PACKAGE_SECTION_SEUSER 0x97cff91
@@ -793,6 +794,12 @@ int sepol_module_package_info(struct sepol_policy_file *spf, int *type, goto cleanup; } len =le32_to_cpu (buf[0 ]); +if (zero_or_saturated (len)) { +ERR (file->handle, +"invalid module name length: 0x%" PRIx32, + len); + goto cleanup; + } *name =malloc (len +1 );if (!*name) {ERR (file->handle,"out of memory" );
@@ -814,6 +821,12 @@ int sepol_module_package_info(struct sepol_policy_file *spf, int *type, goto cleanup; } len =le32_to_cpu (buf[0 ]); +if (zero_or_saturated (len)) { +ERR (file->handle, +"invalid module version length: 0x%" PRIx32, + len); + goto cleanup; + } *version =malloc (len +1 );if (!*version) {ERR (file->handle,"out of memory" );
libsepol/src/module_to_cil.c +3 -1
@@ -47,6 +47,8 @@#include <sepol/policydb/services.h> #include <sepol/policydb/util.h> +#include "private.h" +#ifdef __GNUC__# define UNUSED (x) UNUSED_ ## x __attribute__((__unused__))#else
@@ -124,7 +126,7 @@ static int get_line(char **start, char *end, char **line)for (len =0 ; p < end && *p !='\n' && *p !='\0' ; p++, len++); -if (len ==0 ) { +if (zero_or_saturated (len)) { rc =0 ; goto exit; }
libsepol/src/policydb.c +49 -3
173 lines not on this page · raw message
libsepol/src/private.h +3 -0
@@ -45,6 +45,9 @@#define ARRAY_SIZE (x) (sizeof(x)/sizeof((x)[0])) +#define is_saturated (x) (x == (typeof(x))-1) +#define zero_or_saturated (x) ((x == 0) || is_saturated(x)) +/* Policy compatibility information. */ struct policydb_compat_info {unsigned int type;
Signature · 1 line
1.9.1
Re: [PATCH v3 5/7] libsepol: fix overflow and 0 length allocations
Quoted 51 lines · On 08/15/2016 11:59 AM, [email protected] wrote:
From: William Roberts <[email protected]>
Throughout libsepol, values taken from sepolicy are used in places where length == 0 or length == <saturated> matter, find and fix these.
Also, correct any type mismatches noticed along the way.
Signed-off-by William Roberts <[email protected]>--- libsepol/src/conditional.c | 3 ++- libsepol/src/context.c | 16 ++++++++----- libsepol/src/context_record.c | 52 ++++++++++++++++++++++++------------------- libsepol/src/module.c | 13 +++++++++++ libsepol/src/module_to_cil.c | 4 +++- libsepol/src/policydb.c | 52 ++++++++++++++++++++++++++++++++++++++++--- libsepol/src/private.h | 3 +++ 7 files changed, 110 insertions(+), 33 deletions(-)libsepol/src/context_record.c +5
-10@@ -5,6 +5,7 @@#include "context_internal.h" #include "debug.h" +#include "private.h" struct sepol_context {@@ -284,39 +285,44 @@ int sepol_context_to_string(sepol_handle_t * handle, {int rc; -const int user_sz =strlen (con->user); -const int role_sz =strlen (con->role); -const int type_sz =strlen (con->type); -const int mls_sz = (con->mls) ?strlen (con->mls) :0 ; -const int total_sz = user_sz + role_sz + type_sz + - mls_sz + ((con->mls) ?3 :2 ); - -char *str = (char *)malloc (total_sz +1 ); -if (!str) - goto omem; +char *str =NULL ; +const size_t user_sz =strlen (con->user); +const size_t role_sz =strlen (con->role); +const size_t type_sz =strlen (con->type);
libsepol/src/context_record.c int sepol_context_to_string(sepol_handle_t * handle, · line 301
Quoted 10 lines
libsepol/src/context_record.c +9
-0+const size_t mls_sz = (con->mls) ?strlen (con->mls) :0 ; +const size_t total_sz = user_sz + role_sz + type_sz + + mls_sz + ((con->mls) ?3 :2 ) +1 ; + +if (zero_or_saturated (total_sz)) { +ERR (handle,"invalid size" ); + goto err; + } + str = (char *)malloc (total_sz);
Before, total_sz + 1 was malloc'd, so it made sense to check for saturation, but you add the 1 to total_sz before checking for saturation.
This case is also different in that multiple string lengths are being added, so overflow would be the real concern here, but I don't think that it is possible to overflow because of limits in checkpolicy (8K) and xattrs (64K).
Jim
Quoted 312 lines
libsepol/src/context_record.c +15
-13+if (!str) { +ERR (handle,"out of memory" ); + goto err; + }if (con->mls) { - rc =snprintf (str, total_sz +1 ,"%s:%s:%s:%s" , + rc =snprintf (str, total_sz,"%s:%s:%s:%s" , con->user, con->role, con->type, con->mls); -if (rc <0 || (rc >= total_sz +1 )) { -ERR (handle,"print error" ); - goto err; - } }else { - rc =snprintf (str, total_sz +1 ,"%s:%s:%s" , + rc =snprintf (str, total_sz,"%s:%s:%s" , con->user, con->role, con->type); -if (rc <0 || (rc >= total_sz +1 )) { -ERR (handle,"print error" ); - goto err; - } + } + +/* +* rc is >= 0 on the size_t cast and is safe to promote +* to an unsigned value. +*/ +if (rc <0 || (size_t )rc >= total_sz) { +ERR (handle,"print error" ); + goto err; } *str_ptr = str;return STATUS_SUCCESS ; - omem: -ERR (handle,"out of memory" ); - err:ERR (handle,"could not convert context to string" );free (str);libsepol/src/module.c +13
-0@@ -30,6 +30,7 @@#include <stdio.h> #include <stdlib.h> #include <limits.h> +#include <inttypes.h> #define SEPOL_PACKAGE_SECTION_FC 0xf97cff90#define SEPOL_PACKAGE_SECTION_SEUSER 0x97cff91@@ -793,6 +794,12 @@ int sepol_module_package_info(struct sepol_policy_file *spf, int *type, goto cleanup; } len =le32_to_cpu (buf[0 ]); +if (zero_or_saturated (len)) { +ERR (file->handle, +"invalid module name length: 0x%" PRIx32, + len); + goto cleanup; + } *name =malloc (len +1 );if (!*name) {ERR (file->handle,"out of memory" );@@ -814,6 +821,12 @@ int sepol_module_package_info(struct sepol_policy_file *spf, int *type, goto cleanup; } len =le32_to_cpu (buf[0 ]); +if (zero_or_saturated (len)) { +ERR (file->handle, +"invalid module version length: 0x%" PRIx32, + len); + goto cleanup; + } *version =malloc (len +1 );if (!*version) {ERR (file->handle,"out of memory" );libsepol/src/module_to_cil.c +3
-1@@ -47,6 +47,8 @@#include <sepol/policydb/services.h> #include <sepol/policydb/util.h> +#include "private.h" +#ifdef __GNUC__# define UNUSED (x) UNUSED_ ## x __attribute__((__unused__))#else @@ -124,7 +126,7 @@ static int get_line(char **start, char *end, char **line)for (len =0 ; p < end && *p !='\n' && *p !='\0' ; p++, len++); -if (len ==0 ) { +if (zero_or_saturated (len)) { rc =0 ; goto exit; }libsepol/src/policydb.c +49
-3@@ -1911,6 +1911,9 @@ static int perm_read(policydb_t * p goto bad; len =le32_to_cpu (buf[0 ]); +if (zero_or_saturated (len)) + goto bad; + perdatum->s.value =le32_to_cpu (buf[1 ]); key =malloc (len +1 );@@ -1949,6 +1952,9 @@ static int common_read(policydb_t * p, hashtab_t h, struct policy_file *fp) goto bad; len =le32_to_cpu (buf[0 ]); +if (zero_or_saturated (len)) + goto bad; + comdatum->s.value =le32_to_cpu (buf[1 ]);if (symtab_init (&comdatum->permissions,PERM_SYMTAB_SIZE ))@@ -2092,7 +2098,11 @@ static int class_read(policydb_t * p, hashtab_t h, struct policy_file *fp) goto bad; len =le32_to_cpu (buf[0 ]); +if (zero_or_saturated (len)) + goto bad; len2 =le32_to_cpu (buf[1 ]); +if (is_saturated (len2)) + goto bad; cladatum->s.value =le32_to_cpu (buf[2 ]);if (symtab_init (&cladatum->permissions,PERM_SYMTAB_SIZE ))@@ -2199,6 +2209,9 @@ static int role_read(policydb_t * p, hashtab_t h, struct policy_file *fp) goto bad; len =le32_to_cpu (buf[0 ]); +if (zero_or_saturated (len)) + goto bad; + role->s.value =le32_to_cpu (buf[1 ]);if (policydb_has_boundary_feature (p)) role->bounds =le32_to_cpu (buf[2 ]);@@ -2287,6 +2300,9 @@ static int type_read(policydb_t * p, hashtab_t h, struct policy_file *fp) goto bad; len =le32_to_cpu (buf[pos]); +if (zero_or_saturated (len)) + goto bad; + typdatum->s.value =le32_to_cpu (buf[++pos]);if (policydb_has_boundary_feature (p)) {uint32_t properties;@@ -2447,6 +2463,8 @@ int filename_trans_read(filename_trans_t **t, struct policy_file *fp)if (rc <0 )return -1 ; len =le32_to_cpu (buf[0 ]); +if (zero_or_saturated (len)) +return -1 ; name =calloc (len +1 ,sizeof (*name));if (!name)@@ -2556,6 +2574,9 @@ static int ocontext_read_xen(struct policydb_compat_info *info,if (rc <0 )return -1 ; len =le32_to_cpu (buf[0 ]); +if (zero_or_saturated (len)) +return -1 ; + c->u.name =malloc (len +1 );if (!c->u.name)return -1 ;@@ -2618,6 +2639,8 @@ static int ocontext_read_selinux(struct policydb_compat_info *info,if (rc <0 )return -1 ; len =le32_to_cpu (buf[0 ]); +if (zero_or_saturated (len)) +return -1 ; c->u.name =malloc (len +1 );if (!c->u.name)return -1 ;@@ -2659,6 +2682,8 @@ static int ocontext_read_selinux(struct policydb_compat_info *info,return -1 ; c->v.behavior =le32_to_cpu (buf[0 ]); len =le32_to_cpu (buf[1 ]); +if (zero_or_saturated (len)) +return -1 ; c->u.name =malloc (len +1 );if (!c->u.name)return -1 ;@@ -2719,7 +2744,7 @@ static int genfs_read(policydb_t * p, struct policy_file *fp)uint32_t buf[1 ];size_t nel, nel2, len, len2;genfs_t *genfs_p, *newgenfs, *genfs; -unsigned int i, j; +size_t i, j;ocontext_t *l, *c, *newc =NULL ;int rc;@@ -2733,6 +2758,8 @@ static int genfs_read(policydb_t * p, struct policy_file *fp)if (rc <0 ) goto bad; len =le32_to_cpu (buf[0 ]); +if (zero_or_saturated (len)) + goto bad; newgenfs =calloc (1 ,sizeof (genfs_t));if (!newgenfs) goto bad;@@ -2778,6 +2805,8 @@ static int genfs_read(policydb_t * p, struct policy_file *fp)if (rc <0 ) goto bad; len =le32_to_cpu (buf[0 ]); +if (zero_or_saturated (len)) + goto bad; newc->u.name =malloc (len +1 );if (!newc->u.name) { goto bad;@@ -2877,6 +2906,9 @@ static int user_read(policydb_t * p, hashtab_t h, struct policy_file *fp) goto bad; len =le32_to_cpu (buf[0 ]); +if (zero_or_saturated (len)) + goto bad; + usrdatum->s.value =le32_to_cpu (buf[1 ]);if (policydb_has_boundary_feature (p)) usrdatum->bounds =le32_to_cpu (buf[2 ]);@@ -2960,6 +2992,9 @@ static int sens_read(policydb_t * p goto bad; len =le32_to_cpu (buf[0 ]); +if (zero_or_saturated (len)) + goto bad; + levdatum->isalias =le32_to_cpu (buf[1 ]); key =malloc (len +1 );@@ -3003,6 +3038,9 @@ static int cat_read(policydb_t * p goto bad; len =le32_to_cpu (buf[0 ]); +if (zero_or_saturated (len)) + goto bad; + catdatum->s.value =le32_to_cpu (buf[1 ]); catdatum->isalias =le32_to_cpu (buf[2 ]);@@ -3339,6 +3377,8 @@ static int filename_trans_rule_read(filename_trans_rule_t ** r, struct policy_fireturn -1 ; len =le32_to_cpu (buf[0 ]); +if (zero_or_saturated (len)) +return -1 ; ftr->name =malloc (len +1 );if (!ftr->name)@@ -3580,6 +3620,8 @@ static int scope_read(policydb_t * p, int symnum, struct policy_file *fp)if (rc <0 ) goto cleanup; key_len =le32_to_cpu (buf[0 ]); +if (zero_or_saturated (key_len)) + goto cleanup; key =malloc (key_len +1 );if (!key) goto cleanup;@@ -3664,8 +3706,8 @@ int policydb_read(policydb_t * p, struct policy_file *fp, unsigned verbose) } len = buf[1 ]; -if (len >POLICYDB_STRING_MAX_LENGTH ) { -ERR (fp->handle,"policydb string length too long " ); +if (len ==0 || len >POLICYDB_STRING_MAX_LENGTH ) { +ERR (fp->handle,"policydb string length %s " , len ?"too long" :"zero" );return POLICYDB_ERROR ; }@@ -3798,6 +3840,8 @@ int policydb_read(policydb_t * p, struct policy_file *fp, unsigned verbose) goto bad; } len =le32_to_cpu (buf[0 ]); +if (zero_or_saturated (len)) + goto bad;if ((p->name =malloc (len +1 )) ==NULL ) { goto bad; }@@ -3809,6 +3853,8 @@ int policydb_read(policydb_t * p, struct policy_file *fp, unsigned verbose) goto bad; } len =le32_to_cpu (buf[0 ]); +if (zero_or_saturated (len)) + goto bad;if ((p->version =malloc (len +1 )) ==NULL ) { goto bad; }libsepol/src/private.h +3
-0@@ -45,6 +45,9 @@#define ARRAY_SIZE (x) (sizeof(x)/sizeof((x)[0])) +#define is_saturated (x) (x == (typeof(x))-1) +#define zero_or_saturated (x) ((x == 0) || is_saturated(x)) +/* Policy compatibility information. */ struct policydb_compat_info {unsigned int type;
Signature · 2 lines
James Carter <[email protected]> National Security Agency
Re: [PATCH v3 5/7] libsepol: fix overflow and 0 length allocations
Quoted 9 lines · James Carter
Quoted 7 lines · On 08/15/2016 11:59 AM, [email protected] wrote:
From: William Roberts <[email protected]>
Throughout libsepol, values taken from sepolicy are used in places where length == 0 or length == <saturated> matter, find and fix these.
Quoted 7 lines
Quoted 7 lines
Also, correct any type mismatches noticed along the way.
Signed-off-by William Roberts <[email protected]>--- libsepol/src/conditional.c | 3 ++- libsepol/src/context.c | 16 ++++++++----- libsepol/src/context_record.c | 52
++++++++++++++++++++++++-------------------
Quoted 3 lines
Quoted 3 lines
libsepol/src/module.c | 13 +++++++++++ libsepol/src/module_to_cil.c | 4 +++- libsepol/src/policydb.c | 52
++++++++++++++++++++++++++++++++++++++++---
Quoted 6 lines
Review
Quoted 6 lines
libsepol/src/private.h | 3 +++ 7 files changed, 110 insertions(+), 33 deletions(-)(quoted diff) +0
-0
b/libsepol/src/context_record.c
Quoted 5 lines
Quoted 5 lines
index ac2884a..fdf60a0 100644
libsepol/src/context_record.c +0
-0@@ -5,6 +5,7 @@
Quoted 7 lines
libsepol/src/context_record.c int sepol_context_to_string(sepol_handle_t *
Quoted 7 lines
libsepol/src/context_record.c +1
-0#include "context_internal.h" #include "debug.h" +#include "private.h" struct sepol_context {@@ -284,39 +285,44 @@ int sepol_context_to_string(sepol_handle_t *
handle,
Quoted 26 lines
Quoted 26 lines
libsepol/src/context_record.c +12
-10{int rc; -const int user_sz =strlen (con->user); -const int role_sz =strlen (con->role); -const int type_sz =strlen (con->type); -const int mls_sz = (con->mls) ?strlen (con->mls) :0 ; -const int total_sz = user_sz + role_sz + type_sz + - mls_sz + ((con->mls) ?3 :2 ); - -char *str = (char *)malloc (total_sz +1 ); -if (!str) - goto omem; +char *str =NULL ; +const size_t user_sz =strlen (con->user); +const size_t role_sz =strlen (con->role); +const size_t type_sz =strlen (con->type); +const size_t mls_sz = (con->mls) ?strlen (con->mls) :0 ; +const size_t total_sz = user_sz + role_sz + type_sz + + mls_sz + ((con->mls) ?3 :2 ) +1 ; + +if (zero_or_saturated (total_sz)) { +ERR (handle,"invalid size" ); + goto err; + }
Quoted 4 lines
libsepol/src/context_record.c int sepol_context_to_string(sepol_handle_t * handle, · line 301
Quoted 1 line
libsepol/src/context_record.c +1
-0+ str = (char *)malloc (total_sz);Before,
total_sz+ 1 was malloc'd, so it made sense to check for
saturation, but you add the 1 to total_sz before checking for saturation.
True I didn't want to get rid of the const so we should be checking for 0 or 1 length, or drop the const and check prior to the increment.
Quoted 2 lines
This case is also different in that multiple string lengths are being
added, so overflow would be the real concern here, but I don't think that it is possible to overflow because of limits in checkpolicy (8K) and xattrs (64K).
Check policy isn't generating any of this and the xattr limits are not being enforced here. In theory we could make the check on the xattr limits if that woukd be preferred.
Quoted 54 lines
Jim
Quoted 50 lines
libsepol/src/context_record.c +15
-13+if (!str) { +ERR (handle,"out of memory" ); + goto err; + }if (con->mls) { - rc =snprintf (str, total_sz +1 ,"%s:%s:%s:%s" , + rc =snprintf (str, total_sz,"%s:%s:%s:%s" , con->user, con->role, con->type, con->mls); -if (rc <0 || (rc >= total_sz +1 )) { -ERR (handle,"print error" ); - goto err; - } }else { - rc =snprintf (str, total_sz +1 ,"%s:%s:%s" , + rc =snprintf (str, total_sz,"%s:%s:%s" , con->user, con->role, con->type); -if (rc <0 || (rc >= total_sz +1 )) { -ERR (handle,"print error" ); - goto err; - } + } + +/* +* rc is >= 0 on the size_t cast and is safe to promote +* to an unsigned value. +*/ +if (rc <0 || (size_t )rc >= total_sz) { +ERR (handle,"print error" ); + goto err; } *str_ptr = str;return STATUS_SUCCESS ; - omem: -ERR (handle,"out of memory" ); - err:ERR (handle,"could not convert context to string" );free (str);libsepol/src/module.c +1
-0@@ -30,6 +30,7 @@#include <stdio.h> #include <stdlib.h> #include <limits.h> +#include <inttypes.h>
Quoted 3 lines
libsepol/src/module.c int sepol_module_package_info(struct
Quoted 3 lines
libsepol/src/module.c +0
-0#define SEPOL_PACKAGE_SECTION_FC 0xf97cff90#define SEPOL_PACKAGE_SECTION_SEUSER 0x97cff91@@ -793,6 +794,12 @@ int sepol_module_package_info(struct
sepol_policy_file *spf, int *type,
Quoted 6 lines
libsepol/src/module.c int sepol_module_package_info(struct sepol_policy_file *spf, int *type,
Quoted 6 lines
libsepol/src/module.c +3
-0goto cleanup; } len =le32_to_cpu (buf[0 ]); +if (zero_or_saturated (len)) { + ERR(file->handle, + "invalid module name length:
0x%"PRIx32,
Quoted 7 lines
libsepol/src/module.c int sepol_module_package_info(struct
Quoted 7 lines
libsepol/src/module.c +3
-0+ len); + goto cleanup; + } *name =malloc (len +1 );if (!*name) {ERR (file->handle,"out of memory" );@@ -814,6 +821,12 @@ int sepol_module_package_info(struct
sepol_policy_file *spf, int *type,
Quoted 6 lines
libsepol/src/module.c int sepol_module_package_info(struct sepol_policy_file *spf, int *type,
Quoted 6 lines
libsepol/src/module.c +3
-0goto cleanup; } len =le32_to_cpu (buf[0 ]); +if (zero_or_saturated (len)) { + ERR(file->handle, + "invalid module version length:
0x%"PRIx32,
Quoted 14 lines
Quoted 14 lines
libsepol/src/module.c +3
-0+ len); + goto cleanup; + } *version =malloc (len +1 );if (!*version) {ERR (file->handle,"out of memory" );libsepol/src/module_to_cil.c +0
-0@@ -47,6 +47,8 @@#include <sepol/policydb/services.h> #include <sepol/policydb/util.h>
Quoted 6 lines
libsepol/src/module_to_cil.c static int get_line(char **start, char *end, char
Quoted 6 lines
libsepol/src/module_to_cil.c +2
-0+#include "private.h" +#ifdef __GNUC__# define UNUSED (x) UNUSED_ ## x __attribute__((__unused__))#else @@ -124,7 +126,7 @@ static int get_line(char **start, char *end, char
**line)
Quoted 15 lines
Quoted 15 lines
libsepol/src/module_to_cil.c +1
-1for (len =0 ; p < end && *p !='\n' && *p !='\0' ; p++, len++); -if (len ==0 ) { +if (zero_or_saturated (len)) { rc =0 ; goto exit; }libsepol/src/policydb.c +0
-0@@ -1911,6 +1911,9 @@ static int perm_read(policydb_t * p goto bad;
Quoted 8 lines
libsepol/src/policydb.c static int common_read(policydb_t * p, hashtab_t
Quoted 8 lines
libsepol/src/policydb.c +3
-0len =le32_to_cpu (buf[0 ]); +if (zero_or_saturated (len)) + goto bad; + perdatum->s.value =le32_to_cpu (buf[1 ]); key =malloc (len +1 );@@ -1949,6 +1952,9 @@ static int common_read(policydb_t * p, hashtab_t
h, struct policy_file *fp)
Quoted 10 lines
libsepol/src/policydb.c static int class_read(policydb_t * p, hashtab_t
Quoted 10 lines
libsepol/src/policydb.c +3
-0goto bad; len =le32_to_cpu (buf[0 ]); +if (zero_or_saturated (len)) + goto bad; + comdatum->s.value =le32_to_cpu (buf[1 ]);if (symtab_init (&comdatum->permissions,PERM_SYMTAB_SIZE ))@@ -2092,7 +2098,11 @@ static int class_read(policydb_t * p, hashtab_t
h, struct policy_file *fp)
Quoted 10 lines
Quoted 10 lines
libsepol/src/policydb.c +4
-0goto bad; len =le32_to_cpu (buf[0 ]); +if (zero_or_saturated (len)) + goto bad; len2 =le32_to_cpu (buf[1 ]); +if (is_saturated (len2)) + goto bad; cladatum->s.value =le32_to_cpu (buf[2 ]);
Quoted 2 lines
libsepol/src/policydb.c static int role_read(policydb_t * p, hashtab_t h,
Quoted 2 lines
libsepol/src/policydb.c +0
-0if (symtab_init (&cladatum->permissions,PERM_SYMTAB_SIZE ))@@ -2199,6 +2209,9 @@ static int role_read(policydb_t * p, hashtab_t h,
struct policy_file *fp)
Quoted 10 lines
libsepol/src/policydb.c static int type_read(policydb_t * p, hashtab_t h,
Quoted 10 lines
libsepol/src/policydb.c +3
-0goto bad; len =le32_to_cpu (buf[0 ]); +if (zero_or_saturated (len)) + goto bad; + role->s.value =le32_to_cpu (buf[1 ]);if (policydb_has_boundary_feature (p)) role->bounds =le32_to_cpu (buf[2 ]);@@ -2287,6 +2300,9 @@ static int type_read(policydb_t * p, hashtab_t h,
struct policy_file *fp)
Quoted 10 lines
libsepol/src/policydb.c int filename_trans_read(filename_trans_t **t,
Quoted 10 lines
libsepol/src/policydb.c +3
-0goto bad; len =le32_to_cpu (buf[pos]); +if (zero_or_saturated (len)) + goto bad; + typdatum->s.value =le32_to_cpu (buf[++pos]);if (policydb_has_boundary_feature (p)) {uint32_t properties;@@ -2447,6 +2463,8 @@ int filename_trans_read(filename_trans_t **t,
struct policy_file *fp)
Quoted 9 lines
libsepol/src/policydb.c static int ocontext_read_xen(struct
Quoted 9 lines
libsepol/src/policydb.c +2
-0if (rc <0 )return -1 ; len =le32_to_cpu (buf[0 ]); +if (zero_or_saturated (len)) +return -1 ; name =calloc (len +1 ,sizeof (*name));if (!name)@@ -2556,6 +2574,9 @@ static int ocontext_read_xen(struct
policydb_compat_info *info,
Quoted 10 lines
libsepol/src/policydb.c static int ocontext_read_selinux(struct
Quoted 10 lines
libsepol/src/policydb.c +3
-0if (rc <0 )return -1 ; len =le32_to_cpu (buf[0 ]); +if (zero_or_saturated (len)) +return -1 ; + c->u.name =malloc (len +1 );if (!c->u.name)return -1 ;@@ -2618,6 +2639,8 @@ static int ocontext_read_selinux(struct
policydb_compat_info *info,
Quoted 9 lines
libsepol/src/policydb.c static int ocontext_read_selinux(struct
Quoted 9 lines
libsepol/src/policydb.c +2
-0if (rc <0 )return -1 ; len =le32_to_cpu (buf[0 ]); +if (zero_or_saturated (len)) +return -1 ; c->u.name =malloc (len +1 );if (!c->u.name)return -1 ;@@ -2659,6 +2682,8 @@ static int ocontext_read_selinux(struct
policydb_compat_info *info,
Quoted 9 lines
libsepol/src/policydb.c static int genfs_read(policydb_t * p, struct
Quoted 9 lines
libsepol/src/policydb.c +2
-0return -1 ; c->v.behavior =le32_to_cpu (buf[0 ]); len =le32_to_cpu (buf[1 ]); +if (zero_or_saturated (len)) +return -1 ; c->u.name =malloc (len +1 );if (!c->u.name)return -1 ;@@ -2719,7 +2744,7 @@ static int genfs_read(policydb_t * p, struct
policy_file *fp)
Quoted 9 lines
libsepol/src/policydb.c static int genfs_read(policydb_t * p, struct
Quoted 9 lines
libsepol/src/policydb.c +1
-1uint32_t buf[1 ];size_t nel, nel2, len, len2;genfs_t *genfs_p, *newgenfs, *genfs; -unsigned int i, j; +size_t i, j;ocontext_t *l, *c, *newc =NULL ;int rc;@@ -2733,6 +2758,8 @@ static int genfs_read(policydb_t * p, struct
policy_file *fp)
Quoted 9 lines
libsepol/src/policydb.c static int genfs_read(policydb_t * p, struct
Quoted 9 lines
libsepol/src/policydb.c +2
-0if (rc <0 ) goto bad; len =le32_to_cpu (buf[0 ]); +if (zero_or_saturated (len)) + goto bad; newgenfs =calloc (1 ,sizeof (genfs_t));if (!newgenfs) goto bad;@@ -2778,6 +2805,8 @@ static int genfs_read(policydb_t * p, struct
policy_file *fp)
Quoted 9 lines
libsepol/src/policydb.c static int user_read(policydb_t * p, hashtab_t h,
Quoted 9 lines
libsepol/src/policydb.c +2
-0if (rc <0 ) goto bad; len =le32_to_cpu (buf[0 ]); +if (zero_or_saturated (len)) + goto bad; newc->u.name =malloc (len +1 );if (!newc->u.name) { goto bad;@@ -2877,6 +2906,9 @@ static int user_read(policydb_t * p, hashtab_t h,
struct policy_file *fp)
Quoted 22 lines
Quoted 22 lines
libsepol/src/policydb.c +6
-0goto bad; len =le32_to_cpu (buf[0 ]); +if (zero_or_saturated (len)) + goto bad; + usrdatum->s.value =le32_to_cpu (buf[1 ]);if (policydb_has_boundary_feature (p)) usrdatum->bounds =le32_to_cpu (buf[2 ]);@@ -2960,6 +2992,9 @@ static int sens_read(policydb_t * p goto bad; len =le32_to_cpu (buf[0 ]); +if (zero_or_saturated (len)) + goto bad; + levdatum->isalias =le32_to_cpu (buf[1 ]); key =malloc (len +1 );@@ -3003,6 +3038,9 @@ static int cat_read(policydb_t * p goto bad;
Quoted 8 lines
libsepol/src/policydb.c static int
Quoted 8 lines
libsepol/src/policydb.c +3
-0len =le32_to_cpu (buf[0 ]); +if (zero_or_saturated (len)) + goto bad; + catdatum->s.value =le32_to_cpu (buf[1 ]); catdatum->isalias =le32_to_cpu (buf[2 ]);@@ -3339,6 +3377,8 @@ static int
filename_trans_rule_read(filename_trans_rule_t ** r, struct policy_fi
Quoted 9 lines
libsepol/src/policydb.c static int scope_read(policydb_t * p, int symnum,
Quoted 9 lines
libsepol/src/policydb.c +2
-0return -1 ; len =le32_to_cpu (buf[0 ]); +if (zero_or_saturated (len)) +return -1 ; ftr->name =malloc (len +1 );if (!ftr->name)@@ -3580,6 +3620,8 @@ static int scope_read(policydb_t * p, int symnum,
struct policy_file *fp)
Quoted 9 lines
libsepol/src/policydb.c int policydb_read(policydb_t * p, struct
Quoted 9 lines
libsepol/src/policydb.c +2
-0if (rc <0 ) goto cleanup; key_len =le32_to_cpu (buf[0 ]); +if (zero_or_saturated (key_len)) + goto cleanup; key =malloc (key_len +1 );if (!key) goto cleanup;@@ -3664,8 +3706,8 @@ int policydb_read(policydb_t * p, struct
policy_file *fp, unsigned verbose)
Quoted 7 lines
libsepol/src/policydb.c int policydb_read(policydb_t * p, struct policy_file *fp, unsigned verbose)
Quoted 7 lines
libsepol/src/policydb.c +2
-2} len = buf[1 ]; -if (len >POLICYDB_STRING_MAX_LENGTH ) { -ERR (fp->handle,"policydb string length too long " ); +if (len ==0 || len >POLICYDB_STRING_MAX_LENGTH ) { + ERR(fp->handle, "policydb string length %s ", len ? "too
long" : "zero");
Quoted 4 lines
libsepol/src/policydb.c int policydb_read(policydb_t * p, struct
Quoted 4 lines
libsepol/src/policydb.c +0
-0return POLICYDB_ERROR ; }@@ -3798,6 +3840,8 @@ int policydb_read(policydb_t * p, struct
policy_file *fp, unsigned verbose)
Quoted 9 lines
libsepol/src/policydb.c int policydb_read(policydb_t * p, struct
Quoted 9 lines
libsepol/src/policydb.c +2
-0goto bad; } len =le32_to_cpu (buf[0 ]); +if (zero_or_saturated (len)) + goto bad;if ((p->name =malloc (len +1 )) ==NULL ) { goto bad; }@@ -3809,6 +3853,8 @@ int policydb_read(policydb_t * p, struct
policy_file *fp, unsigned verbose)
Quoted 29 lines
Quoted 14 lines
libsepol/src/policydb.c +2
-0goto bad; } len =le32_to_cpu (buf[0 ]); +if (zero_or_saturated (len)) + goto bad;if ((p->version =malloc (len +1 )) ==NULL ) { goto bad; }libsepol/src/private.h +0
-0libsepol/src/private.h line 53
Quoted 9 lines
libsepol/src/private.h +3
-0@@ -45,6 +45,9 @@#define ARRAY_SIZE (x) (sizeof(x)/sizeof((x)[0])) +#define is_saturated (x) (x == (typeof(x))-1) +#define zero_or_saturated (x) ((x == 0) || is_saturated(x)) +/* Policy compatibility information. */ struct policydb_compat_info {unsigned int type;--
James Carter <[email protected]>
National Security Agency
Quoted 5 lines
_______________________________________________
Seandroid-list mailing list [email protected]
To unsubscribe, send email to [email protected]. To get help, send an email containing "help" to
Re: [PATCH v3 5/7] libsepol: fix overflow and 0 length allocations
Quoted 78 lines · William Roberts
Quoted 9 lines · James Carter
Quoted 7 lines · On 08/15/2016 11:59 AM, [email protected] wrote:
From: William Roberts <[email protected]>
Throughout libsepol, values taken from sepolicy are used in places where length == 0 or length == <saturated> matter, find and fix these.
Quoted 7 lines
Quoted 7 lines
Also, correct any type mismatches noticed along the way.
Signed-off-by William Roberts <[email protected]>--- libsepol/src/conditional.c | 3 ++- libsepol/src/context.c | 16 ++++++++----- libsepol/src/context_record.c | 52 ++++++++++++++++++++++++-------------------
Quoted 3 lines
Quoted 3 lines
libsepol/src/module.c | 13 +++++++++++ libsepol/src/module_to_cil.c | 4 +++- libsepol/src/policydb.c | 52 ++++++++++++++++++++++++++++++++++++++++---
Quoted 6 lines
Review
Quoted 6 lines
libsepol/src/private.h | 3 +++ 7 files changed, 110 insertions(+), 33 deletions(-)(quoted diff) +0
-0
b/libsepol/src/context_record.cQuoted 5 lines
Quoted 5 lines
index ac2884a..fdf60a0 100644
libsepol/src/context_record.c +0
-0@@ -5,6 +5,7 @@Quoted 7 lines
libsepol/src/context_record.c int sepol_context_to_string(sepol_handle_t *
Quoted 7 lines
libsepol/src/context_record.c +1
-0#include "context_internal.h" #include "debug.h" +#include "private.h" struct sepol_context {@@ -284,39 +285,44 @@ int sepol_context_to_string(sepol_handle_t *handle,
Quoted 26 lines
Quoted 26 lines
libsepol/src/context_record.c +12
-10{int rc; -const int user_sz =strlen (con->user); -const int role_sz =strlen (con->role); -const int type_sz =strlen (con->type); -const int mls_sz = (con->mls) ?strlen (con->mls) :0 ; -const int total_sz = user_sz + role_sz + type_sz + - mls_sz + ((con->mls) ?3 :2 ); - -char *str = (char *)malloc (total_sz +1 ); -if (!str) - goto omem; +char *str =NULL ; +const size_t user_sz =strlen (con->user); +const size_t role_sz =strlen (con->role); +const size_t type_sz =strlen (con->type); +const size_t mls_sz = (con->mls) ?strlen (con->mls) :0 ; +const size_t total_sz = user_sz + role_sz + type_sz + + mls_sz + ((con->mls) ?3 :2 ) +1 ; + +if (zero_or_saturated (total_sz)) { +ERR (handle,"invalid size" ); + goto err; + }Quoted 4 lines
libsepol/src/context_record.c int sepol_context_to_string(sepol_handle_t * handle, · line 301
Quoted 1 line
libsepol/src/context_record.c +1
-0+ str = (char *)malloc (total_sz);Before,
total_sz+ 1 was malloc'd, so it made sense to check forsaturation, but you add the 1 to
total_szbefore checking for saturation.True I didn't want to get rid of the const so we should be checking for 0 or 1 length, or drop the const and check prior to the increment.
Quoted 1 line
Quoted 9 lines
Quoted 1 line
This case is also different in that multiple string lengths are being
added, so overflow would be the real concern here, but I don't think that it is possible to overflow because of limits in checkpolicy (8K) and xattrs (64K).
Check policy isn't generating any of this and the xattr limits are not being enforced here. In theory we could make the check on the xattr limits if that woukd be preferred.
Currently, in file-systems like reiserFS that support scalable xattrs, only VFS is the one limiting the size to 64k. Since their is no constant, and maybe one day this arbitrary VFS limit
would be removed, I think we should check correctlly here that were allocating > 1 bytes, and we keep total_sz a const.
Quoted 355 lines
Quoted 13 lines
Jim
Quoted 8 lines
libsepol/src/context_record.c +5
-1+if (!str) { +ERR (handle,"out of memory" ); + goto err; + }if (con->mls) { - rc = snprintf(str, total_sz + 1, "%s:%s:%s:%s", + rc = snprintf(str, total_sz, "%s:%s:%s:%s", con->user, con->role, con->type,
con->mls);Quoted 42 lines
Quoted 42 lines
libsepol/src/context_record.c +10
-12-if (rc <0 || (rc >= total_sz +1 )) { -ERR (handle,"print error" ); - goto err; - } }else { - rc =snprintf (str, total_sz +1 ,"%s:%s:%s" , + rc =snprintf (str, total_sz,"%s:%s:%s" , con->user, con->role, con->type); -if (rc <0 || (rc >= total_sz +1 )) { -ERR (handle,"print error" ); - goto err; - } + } + +/* +* rc is >= 0 on the size_t cast and is safe to promote +* to an unsigned value. +*/ +if (rc <0 || (size_t )rc >= total_sz) { +ERR (handle,"print error" ); + goto err; } *str_ptr = str;return STATUS_SUCCESS ; - omem: -ERR (handle,"out of memory" ); - err:ERR (handle,"could not convert context to string" );free (str);libsepol/src/module.c +1
-0@@ -30,6 +30,7 @@#include <stdio.h> #include <stdlib.h> #include <limits.h> +#include <inttypes.h> Quoted 3 lines
libsepol/src/module.c int sepol_module_package_info(struct
Quoted 3 lines
libsepol/src/module.c +0
-0#define SEPOL_PACKAGE_SECTION_FC 0xf97cff90#define SEPOL_PACKAGE_SECTION_SEUSER 0x97cff91@@ -793,6 +794,12 @@ int sepol_module_package_info(struct
sepol_policy_file*spf, int *type,Quoted 6 lines
libsepol/src/module.c int sepol_module_package_info(struct sepol_policy_file *spf, int *type,
Quoted 6 lines
libsepol/src/module.c +3
-0goto cleanup; } len =le32_to_cpu (buf[0 ]); +if (zero_or_saturated (len)) { + ERR(file->handle, + "invalid module name length:0x%"PRIx32,
Quoted 7 lines
libsepol/src/module.c int sepol_module_package_info(struct
Quoted 7 lines
libsepol/src/module.c +3
-0+ len); + goto cleanup; + } *name =malloc (len +1 );if (!*name) {ERR (file->handle,"out of memory" );@@ -814,6 +821,12 @@ int sepol_module_package_info(struct
sepol_policy_file*spf, int *type,Quoted 6 lines
libsepol/src/module.c int sepol_module_package_info(struct sepol_policy_file *spf, int *type,
Quoted 6 lines
libsepol/src/module.c +3
-0goto cleanup; } len =le32_to_cpu (buf[0 ]); +if (zero_or_saturated (len)) { + ERR(file->handle, + "invalid module version length:0x%"PRIx32,
Quoted 14 lines
Quoted 14 lines
libsepol/src/module.c +3
-0+ len); + goto cleanup; + } *version =malloc (len +1 );if (!*version) {ERR (file->handle,"out of memory" );libsepol/src/module_to_cil.c +0
-0@@ -47,6 +47,8 @@#include <sepol/policydb/services.h> #include <sepol/policydb/util.h> Quoted 6 lines
libsepol/src/module_to_cil.c static int get_line(char **start, char *end, char
Quoted 6 lines
libsepol/src/module_to_cil.c +2
-0+#include "private.h" +#ifdef __GNUC__# define UNUSED (x) UNUSED_ ## x __attribute__((__unused__))#else @@ -124,7 +126,7 @@ static int get_line(char **start, char *end, char**line)
Quoted 15 lines
Quoted 15 lines
libsepol/src/module_to_cil.c +1
-1for (len =0 ; p < end && *p !='\n' && *p !='\0' ; p++, len++); -if (len ==0 ) { +if (zero_or_saturated (len)) { rc =0 ; goto exit; }libsepol/src/policydb.c +0
-0@@ -1911,6 +1911,9 @@ static int perm_read(policydb_t * p goto bad;Quoted 8 lines
libsepol/src/policydb.c static int common_read(policydb_t * p, hashtab_t
Quoted 8 lines
libsepol/src/policydb.c +3
-0len =le32_to_cpu (buf[0 ]); +if (zero_or_saturated (len)) + goto bad; + perdatum->s.value =le32_to_cpu (buf[1 ]); key =malloc (len +1 );@@ -1949,6 +1952,9 @@ static int common_read(policydb_t * p, hashtab_th, struct
policy_file*fp)Quoted 10 lines
libsepol/src/policydb.c static int class_read(policydb_t * p, hashtab_t
Quoted 10 lines
libsepol/src/policydb.c +3
-0goto bad; len =le32_to_cpu (buf[0 ]); +if (zero_or_saturated (len)) + goto bad; + comdatum->s.value =le32_to_cpu (buf[1 ]);if (symtab_init (&comdatum->permissions,PERM_SYMTAB_SIZE ))@@ -2092,7 +2098,11 @@ static int class_read(policydb_t * p, hashtab_th, struct
policy_file*fp)Quoted 10 lines
Quoted 10 lines
libsepol/src/policydb.c +4
-0goto bad; len =le32_to_cpu (buf[0 ]); +if (zero_or_saturated (len)) + goto bad; len2 =le32_to_cpu (buf[1 ]); +if (is_saturated (len2)) + goto bad; cladatum->s.value =le32_to_cpu (buf[2 ]);Quoted 2 lines
libsepol/src/policydb.c static int role_read(policydb_t * p, hashtab_t h,
Quoted 2 lines
libsepol/src/policydb.c +0
-0if (symtab_init (&cladatum->permissions,PERM_SYMTAB_SIZE ))@@ -2199,6 +2209,9 @@ static int role_read(policydb_t * p, hashtab_t h,struct
policy_file*fp)Quoted 10 lines
libsepol/src/policydb.c static int type_read(policydb_t * p, hashtab_t h,
Quoted 10 lines
libsepol/src/policydb.c +3
-0goto bad; len =le32_to_cpu (buf[0 ]); +if (zero_or_saturated (len)) + goto bad; + role->s.value =le32_to_cpu (buf[1 ]);if (policydb_has_boundary_feature (p)) role->bounds =le32_to_cpu (buf[2 ]);@@ -2287,6 +2300,9 @@ static int type_read(policydb_t * p, hashtab_t h,struct
policy_file*fp)Quoted 10 lines
libsepol/src/policydb.c int filename_trans_read(filename_trans_t **t,
Quoted 10 lines
libsepol/src/policydb.c +3
-0goto bad; len =le32_to_cpu (buf[pos]); +if (zero_or_saturated (len)) + goto bad; + typdatum->s.value =le32_to_cpu (buf[++pos]);if (policydb_has_boundary_feature (p)) {uint32_t properties;@@ -2447,6 +2463,8 @@ int filename_trans_read(filename_trans_t **t,struct
policy_file*fp)Quoted 9 lines
libsepol/src/policydb.c static int ocontext_read_xen(struct
Quoted 9 lines
libsepol/src/policydb.c +2
-0if (rc <0 )return -1 ; len =le32_to_cpu (buf[0 ]); +if (zero_or_saturated (len)) +return -1 ; name =calloc (len +1 ,sizeof (*name));if (!name)@@ -2556,6 +2574,9 @@ static int ocontext_read_xen(struct
policydb_compat_info*info,Quoted 10 lines
libsepol/src/policydb.c static int ocontext_read_selinux(struct
Quoted 10 lines
libsepol/src/policydb.c +3
-0if (rc <0 )return -1 ; len =le32_to_cpu (buf[0 ]); +if (zero_or_saturated (len)) +return -1 ; + c->u.name =malloc (len +1 );if (!c->u.name)return -1 ;@@ -2618,6 +2639,8 @@ static int ocontext_read_selinux(struct
policydb_compat_info*info,Quoted 9 lines
libsepol/src/policydb.c static int ocontext_read_selinux(struct
Quoted 9 lines
libsepol/src/policydb.c +2
-0if (rc <0 )return -1 ; len =le32_to_cpu (buf[0 ]); +if (zero_or_saturated (len)) +return -1 ; c->u.name =malloc (len +1 );if (!c->u.name)return -1 ;@@ -2659,6 +2682,8 @@ static int ocontext_read_selinux(struct
policydb_compat_info*info,Quoted 9 lines
libsepol/src/policydb.c static int genfs_read(policydb_t * p, struct
Quoted 9 lines
libsepol/src/policydb.c +2
-0return -1 ; c->v.behavior =le32_to_cpu (buf[0 ]); len =le32_to_cpu (buf[1 ]); +if (zero_or_saturated (len)) +return -1 ; c->u.name =malloc (len +1 );if (!c->u.name)return -1 ;@@ -2719,7 +2744,7 @@ static int genfs_read(policydb_t * p, struct
policy_file*fp)Quoted 9 lines
libsepol/src/policydb.c static int genfs_read(policydb_t * p, struct
Quoted 9 lines
libsepol/src/policydb.c +1
-1uint32_t buf[1 ];size_t nel, nel2, len, len2;genfs_t *genfs_p, *newgenfs, *genfs; -unsigned int i, j; +size_t i, j;ocontext_t *l, *c, *newc =NULL ;int rc;@@ -2733,6 +2758,8 @@ static int genfs_read(policydb_t * p, struct
policy_file*fp)Quoted 9 lines
libsepol/src/policydb.c static int genfs_read(policydb_t * p, struct
Quoted 9 lines
libsepol/src/policydb.c +2
-0if (rc <0 ) goto bad; len =le32_to_cpu (buf[0 ]); +if (zero_or_saturated (len)) + goto bad; newgenfs =calloc (1 ,sizeof (genfs_t));if (!newgenfs) goto bad;@@ -2778,6 +2805,8 @@ static int genfs_read(policydb_t * p, struct
policy_file*fp)Quoted 9 lines
libsepol/src/policydb.c static int user_read(policydb_t * p, hashtab_t h,
Quoted 9 lines
libsepol/src/policydb.c +2
-0if (rc <0 ) goto bad; len =le32_to_cpu (buf[0 ]); +if (zero_or_saturated (len)) + goto bad; newc->u.name =malloc (len +1 );if (!newc->u.name) { goto bad;@@ -2877,6 +2906,9 @@ static int user_read(policydb_t * p, hashtab_t h,struct
policy_file*fp)Quoted 22 lines
Quoted 22 lines
libsepol/src/policydb.c +6
-0goto bad; len =le32_to_cpu (buf[0 ]); +if (zero_or_saturated (len)) + goto bad; + usrdatum->s.value =le32_to_cpu (buf[1 ]);if (policydb_has_boundary_feature (p)) usrdatum->bounds =le32_to_cpu (buf[2 ]);@@ -2960,6 +2992,9 @@ static int sens_read(policydb_t * p goto bad; len =le32_to_cpu (buf[0 ]); +if (zero_or_saturated (len)) + goto bad; + levdatum->isalias =le32_to_cpu (buf[1 ]); key =malloc (len +1 );@@ -3003,6 +3038,9 @@ static int cat_read(policydb_t * p goto bad;Quoted 8 lines
libsepol/src/policydb.c static int filename_trans_rule_read(filename_trans_rule_t
Quoted 8 lines
libsepol/src/policydb.c +3
-0len =le32_to_cpu (buf[0 ]); +if (zero_or_saturated (len)) + goto bad; + catdatum->s.value =le32_to_cpu (buf[1 ]); catdatum->isalias =le32_to_cpu (buf[2 ]);@@ -3339,6 +3377,8 @@ static int filename_trans_rule_read(filename_trans_rule_t** r, struct
policy_fiQuoted 9 lines
libsepol/src/policydb.c static int scope_read(policydb_t * p, int symnum,
Quoted 9 lines
libsepol/src/policydb.c +2
-0return -1 ; len =le32_to_cpu (buf[0 ]); +if (zero_or_saturated (len)) +return -1 ; ftr->name =malloc (len +1 );if (!ftr->name)@@ -3580,6 +3620,8 @@ static int scope_read(policydb_t * p, int symnum,struct
policy_file*fp)Quoted 9 lines
libsepol/src/policydb.c int policydb_read(policydb_t * p, struct
Quoted 9 lines
libsepol/src/policydb.c +2
-0if (rc <0 ) goto cleanup; key_len =le32_to_cpu (buf[0 ]); +if (zero_or_saturated (key_len)) + goto cleanup; key =malloc (key_len +1 );if (!key) goto cleanup;@@ -3664,8 +3706,8 @@ int policydb_read(policydb_t * p, struct
policy_file*fp, unsigned verbose)Quoted 7 lines
libsepol/src/policydb.c int policydb_read(policydb_t * p, struct policy_file *fp, unsigned verbose)
Quoted 7 lines
libsepol/src/policydb.c +2
-2} len = buf[1 ]; -if (len >POLICYDB_STRING_MAX_LENGTH ) { -ERR (fp->handle,"policydb string length too long " ); +if (len ==0 || len >POLICYDB_STRING_MAX_LENGTH ) { + ERR(fp->handle, "policydb string length %s ", len ?"too long" : "zero");
Quoted 4 lines
libsepol/src/policydb.c int policydb_read(policydb_t * p, struct
Quoted 4 lines
libsepol/src/policydb.c +0
-0return POLICYDB_ERROR ; }@@ -3798,6 +3840,8 @@ int policydb_read(policydb_t * p, struct
policy_file*fp, unsigned verbose)Quoted 9 lines
libsepol/src/policydb.c int policydb_read(policydb_t * p, struct
Quoted 9 lines
libsepol/src/policydb.c +2
-0goto bad; } len =le32_to_cpu (buf[0 ]); +if (zero_or_saturated (len)) + goto bad;if ((p->name =malloc (len +1 )) ==NULL ) { goto bad; }@@ -3809,6 +3853,8 @@ int policydb_read(policydb_t * p, struct
policy_file*fp, unsigned verbose)Quoted 29 lines
Quoted 14 lines
libsepol/src/policydb.c +2
-0goto bad; } len =le32_to_cpu (buf[0 ]); +if (zero_or_saturated (len)) + goto bad;if ((p->version =malloc (len +1 )) ==NULL ) { goto bad; }libsepol/src/private.h +0
-0libsepol/src/private.h line 53
Quoted 9 lines
libsepol/src/private.h +3
-0@@ -45,6 +45,9 @@#define ARRAY_SIZE (x) (sizeof(x)/sizeof((x)[0])) +#define is_saturated (x) (x == (typeof(x))-1) +#define zero_or_saturated (x) ((x == 0) || is_saturated(x)) +/* Policy compatibility information. */ struct policydb_compat_info {unsigned int type;--
James Carter <[email protected]>
National Security AgencyQuoted 5 lines
_______________________________________________
Seandroid-list mailing list [email protected]
To unsubscribe, send email to [email protected]. To get help, send an email containing "help" to
Signature · 3 lines
Respectfully, William C Roberts
Re: [PATCH v3 5/7] libsepol: fix overflow and 0 length allocations
Quoted 92 lines · William Roberts
Quoted 79 lines · William Roberts
Quoted 67 lines · James Carter
Quoted 65 lines · On 08/15/2016 11:59 AM, [email protected] wrote:
From: William Roberts <[email protected]>
Throughout libsepol, values taken from sepolicy are used in places where length == 0 or length == <saturated> matter, find and fix these.
Also, correct any type mismatches noticed along the way.
Signed-off-by William Roberts <[email protected]>--- libsepol/src/conditional.c | 3 ++- libsepol/src/context.c | 16 ++++++++----- libsepol/src/context_record.c | 52 ++++++++++++++++++++++++------------------- libsepol/src/module.c | 13 +++++++++++ libsepol/src/module_to_cil.c | 4 +++- libsepol/src/policydb.c | 52 ++++++++++++++++++++++++++++++++++++++++--- libsepol/src/private.h | 3 +++ 7 files changed, 110 insertions(+), 33 deletions(-)libsepol/src/context_record.c +13
-10@@ -5,6 +5,7 @@#include "context_internal.h" #include "debug.h" +#include "private.h" struct sepol_context {@@ -284,39 +285,44 @@ int sepol_context_to_string(sepol_handle_t * handle, {int rc; -const int user_sz =strlen (con->user); -const int role_sz =strlen (con->role); -const int type_sz =strlen (con->type); -const int mls_sz = (con->mls) ?strlen (con->mls) :0 ; -const int total_sz = user_sz + role_sz + type_sz + - mls_sz + ((con->mls) ?3 :2 ); - -char *str = (char *)malloc (total_sz +1 ); -if (!str) - goto omem; +char *str =NULL ; +const size_t user_sz =strlen (con->user); +const size_t role_sz =strlen (con->role); +const size_t type_sz =strlen (con->type); +const size_t mls_sz = (con->mls) ?strlen (con->mls) :0 ; +const size_t total_sz = user_sz + role_sz + type_sz + + mls_sz + ((con->mls) ?3 :2 ) +1 ; + +if (zero_or_saturated (total_sz)) { +ERR (handle,"invalid size" ); + goto err; + }Quoted 5 lines
libsepol/src/context_record.c int sepol_context_to_string(sepol_handle_t * handle, · line 301
Quoted 1 line
libsepol/src/context_record.c +1
-0+ str = (char *)malloc (total_sz);Before,
total_sz+ 1 was malloc'd, so it made sense to check for saturation, but you add the 1 tototal_szbefore checking for saturation.True I didn't want to get rid of the const so we should be checking for 0 or 1 length, or drop the const and check prior to the increment.
Quoted 1 line
Quoted 8 lines
Quoted 4 lines
This case is also different in that multiple string lengths are being added, so overflow would be the real concern here, but I don't think that it is possible to overflow because of limits in checkpolicy (8K) and xattrs (64K).
Check policy isn't generating any of this and the xattr limits are not being enforced here. In theory we could make the check on the xattr limits if that woukd be preferred.
Quoted 5 lines
Currently, in file-systems like reiserFS that support scalable xattrs, only VFS is the one limiting the size to 64k. Since their is no constant, and maybe one day this arbitrary VFS limit
would be removed, I think we should check correctlly here that were allocating > 1 bytes, and we keeptotal_sza const.
BTW do you want to take everything up to this patchset and I can rebase on the repo
instead of sending out so many patches each time. This is me assuming the others are ok by lack of commentary.
Quoted 364 lines
Quoted 349 lines
Quoted 349 lines
Jim
Quoted 328 lines
libsepol/src/context_record.c +15
-13+if (!str) { +ERR (handle,"out of memory" ); + goto err; + }if (con->mls) { - rc =snprintf (str, total_sz +1 ,"%s:%s:%s:%s" , + rc =snprintf (str, total_sz,"%s:%s:%s:%s" , con->user, con->role, con->type, con->mls); -if (rc <0 || (rc >= total_sz +1 )) { -ERR (handle,"print error" ); - goto err; - } }else { - rc =snprintf (str, total_sz +1 ,"%s:%s:%s" , + rc =snprintf (str, total_sz,"%s:%s:%s" , con->user, con->role, con->type); -if (rc <0 || (rc >= total_sz +1 )) { -ERR (handle,"print error" ); - goto err; - } + } + +/* +* rc is >= 0 on the size_t cast and is safe to promote +* to an unsigned value. +*/ +if (rc <0 || (size_t )rc >= total_sz) { +ERR (handle,"print error" ); + goto err; } *str_ptr = str;return STATUS_SUCCESS ; - omem: -ERR (handle,"out of memory" ); - err:ERR(handle, "could not convert context to string"); free(str);libsepol/src/module.c +7
-0@@ -30,6 +30,7 @@#include <stdio.h> #include <stdlib.h> #include <limits.h> +#include <inttypes.h> #define SEPOL_PACKAGE_SECTION_FC 0xf97cff90#define SEPOL_PACKAGE_SECTION_SEUSER 0x97cff91@@ -793,6 +794,12 @@ int sepol_module_package_info(struct sepol_policy_file *spf, int *type, goto cleanup; } len =le32_to_cpu (buf[0 ]); +if (zero_or_saturated (len)) { +ERR (file->handle, +"invalid module name length: 0x%"PRIx32, + len); + goto cleanup; + } *name =malloc (len +1 );if (!*name) { ERR(file->handle, "out of memory");libsepol/src/module.c +6
-0@@ -814,6 +821,12 @@ int sepol_module_package_info(struct sepol_policy_file *spf, int *type, goto cleanup; } len =le32_to_cpu (buf[0 ]); +if (zero_or_saturated (len)) { +ERR (file->handle, +"invalid module version length: 0x%"PRIx32, + len); + goto cleanup; + } *version =malloc (len +1 );if (!*version) { ERR(file->handle, "out of memory");libsepol/src/module_to_cil.c +3
-1@@ -47,6 +47,8 @@#include <sepol/policydb/services.h> #include <sepol/policydb/util.h> +#include "private.h" +#ifdef __GNUC__# define UNUSED (x) UNUSED_ ## x __attribute__((__unused__))#else @@ -124,7 +126,7 @@ static int get_line(char **start, char *end, char **line)for (len =0 ; p < end && *p !='\n' && *p !='\0' ; p++, len++); -if (len ==0 ) { +if (zero_or_saturated (len)) { rc =0 ; goto exit;}libsepol/src/policydb.c +6
-0@@ -1911,6 +1911,9 @@ static int perm_read(policydb_t * p goto bad; len =le32_to_cpu (buf[0 ]); +if (zero_or_saturated (len)) + goto bad; + perdatum->s.value =le32_to_cpu (buf[1 ]); key =malloc (len +1 );@@ -1949,6 +1952,9 @@ static int common_read(policydb_t * p, hashtab_t h, struct policy_file *fp) goto bad; len =le32_to_cpu (buf[0 ]); +if (zero_or_saturated (len)) + goto bad; + comdatum->s.value =le32_to_cpu (buf[1 ]);if (symtab_init(&comdatum->permissions, PERM_SYMTAB_SIZE))libsepol/src/policydb.c +4
-0@@ -2092,7 +2098,11 @@ static int class_read(policydb_t * p, hashtab_t h, struct policy_file *fp) goto bad; len =le32_to_cpu (buf[0 ]); +if (zero_or_saturated (len)) + goto bad; len2 =le32_to_cpu (buf[1 ]); +if (is_saturated (len2)) + goto bad; cladatum->s.value =le32_to_cpu (buf[2 ]);if (symtab_init(&cladatum->permissions, PERM_SYMTAB_SIZE))libsepol/src/policydb.c +3
-0@@ -2199,6 +2209,9 @@ static int role_read(policydb_t * p, hashtab_t h, struct policy_file *fp) goto bad; len =le32_to_cpu (buf[0 ]); +if (zero_or_saturated (len)) + goto bad; + role->s.value =le32_to_cpu (buf[1 ]);if (policydb_has_boundary_feature (p))role->bounds = le32_to_cpu(buf[2]);libsepol/src/policydb.c +3
-0@@ -2287,6 +2300,9 @@ static int type_read(policydb_t * p, hashtab_t h, struct policy_file *fp) goto bad; len =le32_to_cpu (buf[pos]); +if (zero_or_saturated (len)) + goto bad; + typdatum->s.value =le32_to_cpu (buf[++pos]);if (policydb_has_boundary_feature (p)) {uint32_t properties;libsepol/src/policydb.c +2
-0@@ -2447,6 +2463,8 @@ int filename_trans_read(filename_trans_t **t, struct policy_file *fp)if (rc <0 )return -1 ; len =le32_to_cpu (buf[0 ]); +if (zero_or_saturated (len)) +return -1 ; name =calloc (len +1 ,sizeof (*name));if (!name)libsepol/src/policydb.c +3
-0@@ -2556,6 +2574,9 @@ static int ocontext_read_xen(struct policydb_compat_info *info, if (rc < 0)return -1 ; len =le32_to_cpu (buf[0 ]); +if (zero_or_saturated (len)) +return -1 ; + c->u.name =malloc (len +1 );if (!c->u.name)return -1;libsepol/src/policydb.c +2
-0@@ -2618,6 +2639,8 @@ static int ocontext_read_selinux(struct policydb_compat_info *info, if (rc < 0)return -1 ; len =le32_to_cpu (buf[0 ]); +if (zero_or_saturated (len)) +return -1 ; c->u.name =malloc (len +1 );if (!c->u.name)return -1;libsepol/src/policydb.c +2
-0@@ -2659,6 +2682,8 @@ static int ocontext_read_selinux(struct policydb_compat_info *info, return -1 ; c->v.behavior =le32_to_cpu (buf[0 ]); len =le32_to_cpu (buf[1 ]); +if (zero_or_saturated (len)) +return -1 ; c->u.name =malloc (len +1 );if (!c->u.name)return -1;libsepol/src/policydb.c +3
-1@@ -2719,7 +2744,7 @@ static int genfs_read(policydb_t * p, struct policy_file *fp)uint32_t buf[1 ];size_t nel, nel2, len, len2;genfs_t *genfs_p, *newgenfs, *genfs; -unsigned int i, j; +size_t i, j;ocontext_t *l, *c, *newc =NULL ;int rc;@@ -2733,6 +2758,8 @@ static int genfs_read(policydb_t * p, struct policy_file *fp)if (rc <0 ) goto bad; len =le32_to_cpu (buf[0 ]); +if (zero_or_saturated (len)) + goto bad; newgenfs =calloc (1 ,sizeof (genfs_t));if (!newgenfs)goto bad;libsepol/src/policydb.c +2
-0@@ -2778,6 +2805,8 @@ static int genfs_read(policydb_t * p, struct policy_file *fp)if (rc <0 ) goto bad; len =le32_to_cpu (buf[0 ]); +if (zero_or_saturated (len)) + goto bad; newc->u.name =malloc (len +1 );if (!newc->u.name) {goto bad;libsepol/src/policydb.c +3
-0@@ -2877,6 +2906,9 @@ static int user_read(policydb_t * p, hashtab_t h, struct policy_file *fp) goto bad; len =le32_to_cpu (buf[0 ]); +if (zero_or_saturated (len)) + goto bad; + usrdatum->s.value =le32_to_cpu (buf[1 ]);if (policydb_has_boundary_feature (p))usrdatum->bounds = le32_to_cpu(buf[2]);libsepol/src/policydb.c +6
-0@@ -2960,6 +2992,9 @@ static int sens_read(policydb_t * p goto bad; len =le32_to_cpu (buf[0 ]); +if (zero_or_saturated (len)) + goto bad; + levdatum->isalias =le32_to_cpu (buf[1 ]); key =malloc (len +1 );@@ -3003,6 +3038,9 @@ static int cat_read(policydb_t * p goto bad; len =le32_to_cpu (buf[0 ]); +if (zero_or_saturated (len)) + goto bad; + catdatum->s.value =le32_to_cpu (buf[1 ]); catdatum->isalias =le32_to_cpu (buf[2 ]);@@ -3339,6 +3377,8 @@ static intfilename_trans_rule_read(filename_trans_rule_t ** r, struct
policy_fireturn -1; len = le32_to_cpu(buf[0]);libsepol/src/policydb.c +4
-0+if (zero_or_saturated (len)) +return -1 ; ftr->name =malloc (len +1 );if (!ftr->name)@@ -3580,6 +3620,8 @@ static int scope_read(policydb_t * p, int symnum, struct policy_file *fp)if (rc <0 ) goto cleanup; key_len =le32_to_cpu (buf[0 ]); +if (zero_or_saturated (key_len)) + goto cleanup; key =malloc (key_len +1 );if (!key)goto cleanup;libsepol/src/policydb.c +2
-2@@ -3664,8 +3706,8 @@ int policydb_read(policydb_t * p, struct policy_file *fp, unsigned verbose) } len = buf[1 ]; -if (len >POLICYDB_STRING_MAX_LENGTH ) { -ERR (fp->handle,"policydb string length too long " ); +if (len ==0 || len >POLICYDB_STRING_MAX_LENGTH ) { +ERR (fp->handle,"policydb string length %s " , len ?"too long" :"zero" );return POLICYDB_ERROR ;}libsepol/src/policydb.c +2
-0@@ -3798,6 +3840,8 @@ int policydb_read(policydb_t * p, struct policy_file *fp, unsigned verbose) goto bad; } len =le32_to_cpu (buf[0 ]); +if (zero_or_saturated (len)) + goto bad;if ((p->name =malloc (len +1 )) ==NULL ) { goto bad;}libsepol/src/policydb.c +2
-0@@ -3809,6 +3853,8 @@ int policydb_read(policydb_t * p, struct policy_file *fp, unsigned verbose) goto bad; } len =le32_to_cpu (buf[0 ]); +if (zero_or_saturated (len)) + goto bad;if ((p->version =malloc (len +1 )) ==NULL ) { goto bad;}libsepol/src/private.h +0
-0libsepol/src/private.h line 53
Quoted 9 lines
libsepol/src/private.h +3
-0@@ -45,6 +45,9 @@#define ARRAY_SIZE (x) (sizeof(x)/sizeof((x)[0])) +#define is_saturated (x) (x == (typeof(x))-1) +#define zero_or_saturated (x) ((x == 0) || is_saturated(x)) +/* Policy compatibility information. */ struct policydb_compat_info {unsigned int type;--
James Carter <[email protected]>
National Security AgencyQuoted 6 lines
Quoted 6 lines
_______________________________________________
Seandroid-list mailing list [email protected]
To unsubscribe, send email to [email protected]. To get help, send an email containing "help" to [email protected].--
Respectfully,William C Roberts
Signature · 3 lines
Respectfully, William C Roberts
Re: [PATCH v3 5/7] libsepol: fix overflow and 0 length allocations
<snip>
Quoted 7 lines
Quoted 5 lines
Currently, in file-systems like reiserFS that support scalable xattrs, only VFS is the one limiting the size to 64k. Since their is no constant, and maybe one day this arbitrary VFS limit
would be removed, I think we should check correctlly here that were allocating > 1 bytes, and we keeptotal_sza const.
Quoted 5 lines
BTW do you want to take everything up to this patchset and I can rebase on the repo
instead of sending out so many patches each time. This is me assuming the others are ok by lack of commentary.
BTW Patchset v4 has this patch at the very end, so the order is different, if you
can apply the ones you like up to X and i'll rebase my local branch accordingly. Rather than blasting 7 patches on a daily basis.
PATCH v3 6/7 libsepol: bound attr_type_map access by nprim
In reply to [email protected]From: William Roberts <[email protected]>
Correct an invalid memory access when attr_type_map array indexing is outside of bounds.
--- libsepol/src/policydb.c | 4 ++++ 1 file changed, 4 insertions(+)
libsepol/src/policydb.c +4 -0
@@ -3972,6 +3972,10 @@ int policydb_read(policydb_t * p, struct policy_file *fp, unsigned verbose)if (!ebitmap_node_get_bit (tnode, j) || i == j)continue ; + +if (j >= p->p_types.nprim) + goto bad; +if (ebitmap_set_bit (&p->attr_type_map[j], i,1 )) goto bad;
Signature · 1 line
1.9.1
PATCH v3 7/7 libsepol: fix unitialized jmp and invalid dereference
In reply to [email protected]From: William Roberts <[email protected]>
When initializing role_datum_t array, initialize the array. This corrects this issue:
==25766== Conditional jump or move depends on uninitialised value(s)
==25766== at 0x40ABFE: context_is_valid (context.c:59) ==25766== by 0x40AAED: policydb_context_isvalid (context.c:19) ==25766== by 0x43CBF4: context_read_and_validate (policydb.c:1881) ==25766== by 0x43E7B3: ocontext_read_selinux (policydb.c:2631) ==25766== by 0x43EC4D: ocontext_read (policydb.c:2729) ==25766== by 0x442019: policydb_read (policydb.c:3937) ==25766== by 0x442F15: sepol_policydb_read (policydb_public.c:174) ==25766== by 0x407ED4: init (check_seapp.c:885) ==25766== by 0x408D83: main (check_seapp.c:1230)
Also, check for NULL when determining if a role can be associated with a type.
--- libsepol/src/context.c | 2 +- libsepol/src/policydb.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
libsepol/src/context.c +1 -1
@@ -56,7 +56,7 @@ int context_is_valid(const policydb_t * p, const context_struct_t * c)* Role must be authorized for the type. */ role = p->role_val_to_struct[c->role -1 ]; -if (!ebitmap_get_bit (&role->cache, c->type -1 )) +if (!role || !ebitmap_get_bit (&role->cache, c->type -1 ))/* role may not be associated with type */ return 0 ;
libsepol/src/policydb.c +1 -1
@@ -1068,7 +1068,7 @@ int policydb_index_others(sepol_handle_t * handle,free (p->role_val_to_struct); p->role_val_to_struct = (role_datum_t **) -malloc (p->p_roles.nprim *sizeof (role_datum_t *)); +calloc (p->p_roles.nprim,sizeof (role_datum_t *));if (!p->role_val_to_struct)return -1 ;
Signature · 1 line
1.9.1
Quoted 10 lines
-----Original Message-----
From: Selinux [mailto:[email protected]] On Behalf Of [email protected]
Sent: Monday, August 15, 2016 8:59 AM
To: [email protected]; [email protected]; seandroid- [email protected]; [email protected]
Subject: Fix AFL Found Bugs in libsepol
Quoted 3 lines
With the patches mentioned below, I have been able to run the AFL fuzzer for 3 days without issue. I didn't get much feedback in v2 of the patchset, but these should address the issues in that series.
As well as fix a few more issues found along the way.
Quoted 12 lines
[PATCH v3 1/7] libsepol: fix invalid access of NULL on [PATCH v3 2/7] libsepol:
ensure key is valid before doing search [PATCH v3 3/7] ebitmap: detect invalid bitmap [PATCH v3 4/7]genfs_read: fix use heap-use-after-free [PATCH v3 5/7]
libsepol: fix overflow and 0 length allocations [PATCH v3 6/7] libsepol: boundattr_type_mapaccess by nprim [PATCH v3 7/7] libsepol: fix unitialized jmp and invalid dereference
_______________________________________________
Selinux mailing list
[email protected]
To unsubscribe, send email to [email protected].
To get help, send an email containing "help" to [email protected].