libsepol: use correct error type to please UBSAN
PATCH 1/3 libsepol: use correct error type to please UBSAN
The callback function apply in hashtap_map has a return type of int and can return -1 on error. Use int as type to save the return value to avoid implicit conversions:
hashtab.c:236:10: runtime error: implicit conversion from type 'int' of value -1 (32-bit, signed) to type 'unsigned int' changed the value to 4294967295 (32-bit, unsigned)
--- libsepol/src/hashtab.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
libsepol/src/hashtab.c +2 -1
@@ -224,8 +224,9 @@ int hashtab_map(hashtab_t h,int (*apply) (hashtab_key_t k, hashtab_datum_t d, void *args), void *args) { - unsigned int i, ret; + unsigned int i;hashtab_ptr_t cur; +int ret;if (!h)return SEPOL_OK ;
Signature · 1 line
2.34.1
PATCH 2/3 libsepol: more strict constraint validation
Check the permission bitset in normal constraints is not empty and has no invalid bits set.
Check the names and type_names members are empty in case they are not used.
Check the operator and attribute type are not set for simple expression types.
--- libsepol/src/policydb_validate.c | 43 +++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 4 deletions(-)
libsepol/src/policydb_validate.c +39 -4
@@ -130,6 +130,21 @@ bad:return -1 ; } +static int validate_empty_type_set (type_set_t *type_set) +{ +if (!ebitmap_is_empty (&type_set->types)) + goto bad; +if (!ebitmap_is_empty (&type_set->negset)) + goto bad; +if (type_set->flags !=0 ) + goto bad; + +return 0 ; + +bad: +return -1 ; +} +static int validate_role_set (role_set_t *role_set,validate_t *role) {if (validate_ebitmap (&role_set->roles, role))
@@ -176,23 +191,37 @@ bad:return -1 ; } -static int validate_constraint_nodes (sepol_handle_t *handle,constraint_node_t *cons,validate_t flavors[]) +static int validate_constraint_nodes (sepol_handle_t *handle,unsigned int nperms,constraint_node_t *cons,validate_t flavors[]) {constraint_expr_t *cexp;for (; cons; cons = cons->next) { +if (nperms >0 && cons->permissions ==0 ) + goto bad; +if (nperms >0 && nperms !=PERM_SYMTAB_SIZE && cons->permissions >= (UINT32_C (1 ) << nperms)) + goto bad; +for (cexp = cons->expr; cexp; cexp = cexp->next) {if (cexp->attr &CEXPR_USER ) {if (validate_ebitmap (&cexp->names, &flavors[SYM_USERS ])) goto bad; +if (validate_empty_type_set (cexp->type_names)) + goto bad; }else if (cexp->attr &CEXPR_ROLE ) {if (validate_ebitmap (&cexp->names, &flavors[SYM_ROLES ])) goto bad; +if (validate_empty_type_set (cexp->type_names)) + goto bad; }else if (cexp->attr &CEXPR_TYPE ) {if (validate_ebitmap (&cexp->names, &flavors[SYM_TYPES ])) goto bad;if (validate_type_set (cexp->type_names, &flavors[SYM_TYPES ])) goto bad; + }else { +if (!ebitmap_is_empty (&cexp->names)) + goto bad; +if (validate_empty_type_set (cexp->type_names)) + goto bad; }if (cexp->expr_type ==CEXPR_ATTR || cexp->expr_type ==CEXPR_NAMES ) {
@@ -236,6 +265,12 @@ static int validate_constraint_nodes(sepol_handle_t *handle, constraint_node_t *default : goto bad; } + +if (cexp->op !=0 ) + goto bad; + +if (cexp->attr !=0 ) + goto bad; } } }
@@ -251,11 +286,11 @@ static int validate_class_datum(sepol_handle_t *handle, class_datum_t *class, va {if (validate_value (class->s.value, &flavors[SYM_CLASSES ])) goto bad; -if (validate_constraint_nodes (handle, class->constraints, flavors)) +if (class->permissions.nprim >PERM_SYMTAB_SIZE ) goto bad; -if (validate_constraint_nodes (handle, class->validatetrans, flavors)) +if (validate_constraint_nodes (handle, class->permissions.nprim, class->constraints, flavors)) goto bad; -if (class->permissions.nprim >PERM_SYMTAB_SIZE ) +if (validate_constraint_nodes (handle,0 , class->validatetrans, flavors)) goto bad;switch (class->default_user) {
Signature · 1 line
2.34.1
PATCH 3/3 libsepol: validate several flags
In reply to Christian GöttscheCheck the type for type and role sets is valid.
Check the scope of a scope datum is valid.
Check the flavor and flags of a type datum are valid.
Check xperms are set if and only if it is an extended permission avrule.
Check xperms has a valid specified field.
Check the flag of avrule blocks is valid.
--- libsepol/src/policydb_validate.c | 86 +++++++++++++++++++++++++++++++- 1 file changed, 84 insertions(+), 2 deletions(-)
libsepol/src/policydb_validate.c +84 -2
@@ -124,6 +124,15 @@ static int validate_type_set(type_set_t *type_set, validate_t *type)if (validate_ebitmap (&type_set->negset, type)) goto bad; +switch (type_set->flags) { +case 0 : +case TYPE_STAR : +case TYPE_COMP : +break ; +default : + goto bad; + } +return 0 ; bad:
@@ -148,9 +157,21 @@ bad:static int validate_role_set (role_set_t *role_set,validate_t *role) {if (validate_ebitmap (&role_set->roles, role)) -return -1 ; + goto bad; + +switch (role_set->flags) { +case 0 : +case ROLE_STAR : +case ROLE_COMP : +break ; +default : + goto bad; + }return 0 ; + +bad: +return -1 ; }static int validate_scope (__attribute__ ((unused))hashtab_key_t k,hashtab_datum_t d,void *args)
@@ -159,12 +180,23 @@ static int validate_scope(__attribute__ ((unused)) hashtab_key_t k, hashtab_datuuint32_t *nprim = (uint32_t *)args;unsigned int i; +switch (scope_datum->scope) { +case SCOPE_REQ : +case SCOPE_DECL : +break ; +default : + goto bad; + } +for (i =0 ; i < scope_datum->decl_ids_len; i++) {if (!value_isvalid (scope_datum->decl_ids[i], *nprim)) -return -1 ; + goto bad; }return 0 ; + +bad: +return -1 ; }static int validate_scopes (sepol_handle_t *handle,symtab_t scopes[],avrule_block_t *block)
@@ -403,6 +435,26 @@ static int validate_type_datum(sepol_handle_t *handle, type_datum_t *type, validif (type->bounds &&validate_value (type->bounds, &flavors[SYM_TYPES ])) goto bad; +switch (type->flavor) { +case TYPE_TYPE : +case TYPE_ATTRIB : +case TYPE_ALIAS : +break ; +default : + goto bad; + } + +switch (type->flags) { +case 0 : +case TYPE_FLAGS_PERMISSIVE : +case TYPE_FLAGS_EXPAND_ATTR_TRUE : +case TYPE_FLAGS_EXPAND_ATTR_FALSE : +case TYPE_FLAGS_EXPAND_ATTR : +break ; +default : + goto bad; + } +return 0 ; bad:
@@ -688,6 +740,7 @@ static int validate_avrules(sepol_handle_t *handle, avrule_t *avrule, validate_tif (validate_value (class->tclass, &flavors[SYM_CLASSES ])) goto bad; } +switch (avrule->specified) {case AVRULE_ALLOWED :case AVRULE_AUDITALLOW :
@@ -705,6 +758,27 @@ static int validate_avrules(sepol_handle_t *handle, avrule_t *avrule, validate_tdefault : goto bad; } + +if (avrule->specified &AVRULE_XPERMS ) { +if (!avrule->xperms) + goto bad; +switch (avrule->xperms->specified) { +case AVRULE_XPERMS_IOCTLFUNCTION : +case AVRULE_XPERMS_IOCTLDRIVER : +break ; +default : + goto bad; + } + }else if (avrule->xperms) + goto bad; + +switch (avrule->flags) { +case 0 : +case RULE_SELF : +break ; +default : + goto bad; + } }return 0 ;
@@ -1041,6 +1115,14 @@ static int validate_avrule_blocks(sepol_handle_t *handle, avrule_block_t *avruleif (validate_symtabs (handle, decl->symtab, flavors)) goto bad; } + +switch (avrule_block->flags) { +case 0 : +case AVRULE_OPTIONAL : +break ; +default : + goto bad; + } }return 0 ;
Signature · 1 line
2.34.1
Quoted 8 lines · Christian Göttsche
The callback function apply in
hashtap_maphas a return type of int and can return -1 on error. Use int as type to save the return value to avoid implicit conversions:hashtab.c:236:10: runtime error: implicit conversion from type 'int' of value -1 (32-bit, signed) to type 'unsigned int' changed the value to 4294967295 (32-bit, unsigned)
Signed-off-by Christian Göttsche <[email protected]>
For all three patches:
Quoted 22 lines
--- libsepol/src/hashtab.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)libsepol/src/hashtab.c +2
-2@@ -224,8 +224,9 @@ int hashtab_map(hashtab_t h,int (*apply) (hashtab_key_t k, hashtab_datum_t d, void *args), void *args) { - unsigned int i, ret; + unsigned int i;hashtab_ptr_t cur; +int ret;if (!h)return SEPOL_OK ; --2.34.1
Quoted 8 lines · James Carter
Quoted 5 lines · Christian Göttsche
The callback function apply in
hashtap_maphas a return type of int and can return -1 on error. Use int as type to save the return value to avoid implicit conversions:
Quoted 7 lines
Quoted 3 lines
hashtab.c:236:10: runtime error: implicit conversion from type 'int' of value -1 (32-bit, signed) to type 'unsigned int' changed the value to 4294967295 (32-bit, unsigned)
Signed-off-by Christian Göttsche <[email protected]>For all three patches:
Acked-by James Carter <[email protected]>
These three patches have been merged.
Thanks,
Jim
Quoted 22 lines
Quoted 22 lines
--- libsepol/src/hashtab.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)libsepol/src/hashtab.c +2
-2@@ -224,8 +224,9 @@ int hashtab_map(hashtab_t h,int (*apply) (hashtab_key_t k, hashtab_datum_t d, void *args), void *args) { - unsigned int i, ret; + unsigned int i;hashtab_ptr_t cur; +int ret;if (!h)return SEPOL_OK ; --2.34.1