libselinux: Add overflow checks
PATCH v5 1/6 libselinux: Add overflow checks
Using "*= 2" is a common idiom to grow an array or buffer, but it is possible for this multiplication to overflow.
Use the __builtin_smul_overflow() function that will do the multiplication while checking for overflow.
---
v5: No changes
libselinux/src/matchpathcon.c | 6 +++++- libselinux/src/seusers.c | 4 ++-- 2 files changed, 7 insertions(+), 3 deletions(-)
libselinux/src/matchpathcon.c +5 -1
@@ -117,7 +117,11 @@ static int add_array_elt(char *con)char **tmp;if (con_array_size) {while (con_array_used >= con_array_size) { - con_array_size *=2 ; +if (__builtin_smul_overflow (con_array_size,2 , + &con_array_size)) { +free_array_elts (); +return -1 ; + } tmp = (char **)reallocarray (con_array, con_array_size,sizeof (char *));if (!tmp) {
libselinux/src/seusers.c +2 -2
@@ -114,9 +114,9 @@ static gid_t get_default_gid(const char *name)break ; rc =getpwnam_r (name, &pwstorage, rbuf, rbuflen, &pwent); -if (rc ==ERANGE && rbuflen <LONG_MAX /2 ) { +if (rc ==ERANGE && + !__builtin_smull_overflow (rbuflen,2 , &rbuflen)) {free (rbuf); - rbuflen *=2 ;continue ; }if (rc ==0 && pwent)
Signature · 1 line
2.54.0
PATCH v5 2/6 libsemanage: Add overflow checks
Using "*= 2" is a common idiom to grow a buffer, but it is possible for this multiplication to overflow.
Use the __builtin_mul_overflow() family of functions that will do the multiplication while checking for overflow.
---
v5: No changes
libsemanage/src/direct_api.c | 7 +++++-- libsemanage/src/genhomedircon.c | 11 +++++------ 2 files changed, 10 insertions(+), 8 deletions(-)
libsemanage/src/direct_api.c +5 -2
@@ -588,8 +588,11 @@ static int read_from_pipe_to_data(semanage_handle_t *sh, size_t initial_len, data_read_len += read_len;if (data_read_len == max_len) {char *tmp; - - max_len *=2 ; +if (__builtin_mul_overflow (max_len,2 , &max_len)) { +ERR (sh,"Overflow" ); +free (data_read); +return -1 ; + } tmp =realloc (data_read, max_len);if (tmp ==NULL ) {ERR (sh,"Failed to realloc, out of memory." );
libsemanage/src/genhomedircon.c +5 -6
@@ -1090,9 +1090,9 @@ retry: goto cleanup; retval =getpwnam_r (name, &pwstorage, rbuf, rbuflen, &pwent); -if (retval ==ERANGE && rbuflen <LONG_MAX /2 ) { +if (retval ==ERANGE && + !__builtin_smull_overflow (rbuflen,2 , &rbuflen)) {free (rbuf); - rbuflen *=2 ; goto retry; }if (retval !=0 || pwent ==NULL ) {
@@ -1171,11 +1171,10 @@ static int get_group_users(genhomedircon_settings_t *s, &group)) != 0 && errno == ERANGE) {char *new_grbuf; - grbuflen *=2 ; -if (grbuflen <0 ) -/* the member list could exceed 2Gb on a system with a 32-bit CPU (where -* sizeof(long) = 4) - if this ever happened, the loop would become infinite. */ +if (__builtin_smull_overflow (grbuflen,2 , &grbuflen)) { +ERR (s->h_semanage,"Overflow" ); goto cleanup; + } new_grbuf =realloc (grbuf, grbuflen);if (new_grbuf ==NULL ) goto cleanup;
Signature · 1 line
2.54.0
PATCH v5 3/6 libsepol/cil: Add overflow checks for circular reference checks
In reply to James CarterWhen resolving aliases a check is made for circular references. Similar checks for circular references are made for user, role, and type bounds and for class permissions (class mappings and class permission sets make circular references possible).
These checks use the common idiom of using "limit *= 2", but there is no check for overflow.
Use the __builtin_mul_overflow() family of functions that will do the multiplication while checking for overflow.
---
v5: No changes
libsepol/cil/src/cil_resolve_ast.c | 5 ++++- libsepol/cil/src/cil_verify.c | 20 ++++++++++++++++---- 2 files changed, 20 insertions(+), 5 deletions(-)
libsepol/cil/src/cil_resolve_ast.c +4 -1
@@ -652,8 +652,11 @@ static int cil_resolve_alias_to_actual(struct cil_tree_node *current, }if (steps == limit) { +if (__builtin_smul_overflow (limit,2 , &limit)) { +cil_tree_log (current,CIL_ERR ,"Overflow" ); +return SEPOL_ERR ; + } steps =0 ; - limit *=2 ; a2 = a1; } }
libsepol/cil/src/cil_verify.c +16 -4
@@ -975,8 +975,11 @@ static int __cil_verify_user_pre_eval(struct cil_tree_node *node) }if (steps == limit) { +if (__builtin_smul_overflow (limit,2 , &limit)) { +cil_log (CIL_ERR ,"Overflow\n" ); + goto exit; + } steps =0 ; - limit *=2 ; u1 = u2; }
@@ -1028,8 +1031,11 @@ static int __cil_verify_role(struct cil_tree_node *node) }if (steps == limit) { +if (__builtin_smul_overflow (limit,2 , &limit)) { +cil_log (CIL_ERR ,"Overflow\n" ); + goto exit; + } steps =0 ; - limit *=2 ; r1 = r2; }
@@ -1060,8 +1066,11 @@ static int __cil_verify_type(struct cil_tree_node *node) }if (steps == limit) { +if (__builtin_smul_overflow (limit,2 , &limit)) { +cil_log (CIL_ERR ,"Overflow\n" ); + goto exit; + } steps =0 ; - limit *=2 ; t1 = t2; }
@@ -2112,8 +2121,11 @@ static int __cil_verify_classperms(struct cil_list *classperms, }else { steps++;if (steps > limit) { +if (__builtin_umul_overflow (limit,2 , &limit)) { +cil_log (CIL_ERR ,"Overflow\n" ); + goto exit; + } steps =1 ; - limit *=2 ; orig = cur; } }
Signature · 1 line
2.54.0
PATCH v5 4/6 libsepol/cil: Add overflow checks and cil_reallocarray
In reply to James CarterThe CIL stack implementation uses "stack->size *= 2" to grow the size of the stack without checking for overflow and calls cil_realloc() using a multiplcation that could overflow.
Use __builtin_smul_overflow() to grow the stack size and exit with an error if there is an overflow. Add the function cil_reallocarray() that checks for overflow of the multiplication of the array member size with the number of members and exits with an error if there is an overflow.
---
v5: No changes
libsepol/cil/src/cil_mem.c | 12 ++++++++++++ libsepol/cil/src/cil_mem.h | 1 + libsepol/cil/src/cil_stack.c | 9 ++++++--- 3 files changed, 19 insertions(+), 3 deletions(-)
libsepol/cil/src/cil_mem.c +12 -0
@@ -74,6 +74,18 @@ void *cil_realloc(void *ptr, size_t size)return mem; } +void *cil_reallocarray (void *ptr,size_t nmemb,size_t size) +{ +size_t array_size; + +if (__builtin_mul_overflow (nmemb, size, &array_size)) { +cil_log (CIL_ERR ,"Overflow\n" ); +exit (1 ); + } + +return cil_realloc (ptr, array_size); +} +char *cil_strdup (const char *str) {char *mem =NULL ;
libsepol/cil/src/cil_mem.h +1 -0
@@ -34,6 +34,7 @@void *cil_malloc (size_t size);void *cil_calloc (size_t num_elements,size_t element_size);void *cil_realloc (void *ptr,size_t size); +void *cil_reallocarray (void *ptr,size_t nmemb,size_t size);char *cil_strdup (const char *str);int cil_asprintf (char **strp,const char *fmt, ...);
libsepol/cil/src/cil_stack.c +6 -3
@@ -77,9 +77,12 @@ void cil_stack_push(struct cil_stack *stack, enum cil_flavor flavor, void *data) stack->pos++;if (stack->pos == stack->size) { - stack->size *=2 ; - stack->stack =cil_realloc (stack->stack, -sizeof (*stack->stack) * stack->size); +if (__builtin_smul_overflow (stack->size,2 , &stack->size)) { +cil_log (CIL_ERR ,"Overflow\n" ); +exit (1 ); + } + stack->stack =cil_reallocarray (stack->stack, stack->size, +sizeof (*stack->stack)); } stack->stack[stack->pos].flavor = flavor;
Signature · 1 line
2.54.0
PATCH v5 5/6 libsepol: Add overflow checks to strs_* family of functions
In reply to James CarterThe strs_* family of functions provide convenient functions for handling an array of strings which is used in kernel_to_conf and kernel_to_cil. It uses the common idiom of "*= 2" to grow the array without any checks for overflow. There is also the function strs_add_at_index() which can cause multiple iterations of the "*= 2" idiom.
Use the __builtin_mul_overflow() function that will do the multiplication while checking for overflow.
In addition, the function strs_len_items() adds up the lengths of all the strings in the array which can overflow.
Use the __builtin_add_overflow() function that will do the addition while checking for overflow.
---
v5: Use new_size in the while loop conditional for strs_add_at_index()
libsepol/src/kernel_to_common.c | 29 ++++++++++++++++++++++------- libsepol/src/kernel_to_conf.c | 10 +++++++++- 2 files changed, 31 insertions(+), 8 deletions(-)
libsepol/src/kernel_to_common.c +22 -7
@@ -117,13 +117,18 @@ int strs_add(struct strs *strs, char *s)if (strs->num +1 > strs->size) {char **new;size_t i = strs->size; - strs->size *=2 ; - new =reallocarray (strs->list, strs->size,sizeof (char *)); +size_t new_size; +if (__builtin_mul_overflow (strs->size,2 , &new_size)) { +ERR (NULL ,"Overflow" ); +return -1 ; + } + new =reallocarray (strs->list, new_size,sizeof (char *));if (!new) {ERR (NULL ,"Out of memory" );return -1 ; } strs->list = new; + strs->size = new_size;memset (&strs->list[i],0 ,sizeof (char *) * (strs->size - i)); }
@@ -172,15 +177,20 @@ int strs_add_at_index(struct strs *strs, char *s, size_t index)if (index >= strs->size) {char **new;size_t i = strs->size; -while (index >= strs->size) { - strs->size *=2 ; +size_t new_size = strs->size; +while (index >= new_size) { +if (__builtin_mul_overflow (strs->size,2 , &new_size)) { +ERR (NULL ,"Overflow" ); +return -1 ; + } } - new =reallocarray (strs->list, strs->size,sizeof (char *)); + new =reallocarray (strs->list, new_size,sizeof (char *));if (!new) {ERR (NULL ,"Out of memory" );return -1 ; } strs->list = new; + strs->size = new_size;memset (&strs->list[i],0 ,sizeof (char *) * (strs->size - i)); }
@@ -229,7 +239,9 @@ size_t strs_len_items(const struct strs *strs)for (i =0 ; i < strs->num; i++) {if (!strs->list[i])continue ; - len +=strlen (strs->list[i]); +if (__builtin_add_overflow (len,strlen (strs->list[i]), &len)) { +return SIZE_MAX ; + } }return len;
@@ -248,7 +260,10 @@ char *strs_to_str(const struct strs *strs) }/* strs->num added because either ' ' or '\0' follows each item */ - len =strs_len_items (strs) + strs->num; +if (__builtin_add_overflow (strs_len_items (strs), strs->num, &len)) { +ERR (NULL ,"Overflow" ); + goto exit; + } str =malloc (len);if (!str) {ERR (NULL ,"Out of memory" );
libsepol/src/kernel_to_conf.c +9 -1
@@ -1663,7 +1663,15 @@ static char *attr_strs_to_str(struct strs *strs) }/* 2*strs->num - 1 because ", " follows all but last attr (followed by '\0') */ - len =strs_len_items (strs) +2 * strs->num -1 ; +if (__builtin_mul_overflow (strs->num,2 , &len)) { +ERR (NULL ,"Overflow" ); + goto exit; + } + len -=1 ; +if (__builtin_add_overflow (len,strs_len_items (strs), &len)) { +ERR (NULL ,"Overflow" ); + goto exit; + } str =malloc (len);if (!str) {ERR (NULL ,"Out of memory" );
Signature · 1 line
2.54.0
Stephen Smalley
maintainerRe: [PATCH 5/6 v5] libsepol: Add overflow checks to strs_* family of functions
Quoted 51 lines · James Carter
The strs_* family of functions provide convenient functions for handling an array of strings which is used in
kernel_to_confandkernel_to_cil. It uses the common idiom of "*= 2" to grow the array without any checks for overflow. There is also the functionstrs_add_at_index()which can cause multiple iterations of the "*= 2" idiom.Use the
__builtin_mul_overflow()function that will do the multiplication while checking for overflow.In addition, the function
strs_len_items()adds up the lengths of all the strings in the array which can overflow.Use the
__builtin_add_overflow()function that will do the addition while checking for overflow.Signed-off-by James Carter <[email protected]>---
v5: Usenew_sizein the while loop conditional forstrs_add_at_index()libsepol/src/kernel_to_common.c | 29 ++++++++++++++++++++++------- libsepol/src/kernel_to_conf.c | 10 +++++++++- 2 files changed, 31 insertions(+), 8 deletions(-)libsepol/src/kernel_to_common.c +7
-2@@ -117,13 +117,18 @@ int strs_add(struct strs *strs, char *s)if (strs->num +1 > strs->size) {char **new;size_t i = strs->size; - strs->size *=2 ; - new =reallocarray (strs->list, strs->size,sizeof (char *)); +size_t new_size; +if (__builtin_mul_overflow (strs->size,2 , &new_size)) { +ERR (NULL ,"Overflow" ); +return -1 ; + } + new =reallocarray (strs->list, new_size,sizeof (char *));if (!new) {ERR (NULL ,"Out of memory" );return -1 ; } strs->list = new; + strs->size = new_size;memset (&strs->list[i],0 ,sizeof (char *) * (strs->size - i)); }
libsepol/src/kernel_to_common.c int strs_add_at_index(struct strs *strs, char *s, size_t index) · line 182
Quoted 8 lines
libsepol/src/kernel_to_common.c +3
-2@@ -172,15 +177,20 @@ int strs_add_at_index(struct strs *strs, char *s, size_t index)if (index >= strs->size) {char **new;size_t i = strs->size; -while (index >= strs->size) { - strs->size *=2 ; +size_t new_size = strs->size; +while (index >= new_size) { +if (__builtin_mul_overflow (strs->size,2 , &new_size)) {
This needs to be:
if (__builtin_mul_overflow(new_size, 2, &new_size)) {Quoted 62 lines
libsepol/src/kernel_to_common.c +12
-3+ERR (NULL ,"Overflow" ); +return -1 ; + } } - new =reallocarray (strs->list, strs->size,sizeof (char *)); + new =reallocarray (strs->list, new_size,sizeof (char *));if (!new) {ERR (NULL ,"Out of memory" );return -1 ; } strs->list = new; + strs->size = new_size;memset (&strs->list[i],0 ,sizeof (char *) * (strs->size - i)); }@@ -229,7 +239,9 @@ size_t strs_len_items(const struct strs *strs)for (i =0 ; i < strs->num; i++) {if (!strs->list[i])continue ; - len +=strlen (strs->list[i]); +if (__builtin_add_overflow (len,strlen (strs->list[i]), &len)) { +return SIZE_MAX ; + } }return len;@@ -248,7 +260,10 @@ char *strs_to_str(const struct strs *strs) }/* strs->num added because either ' ' or '\0' follows each item */ - len =strs_len_items (strs) + strs->num; +if (__builtin_add_overflow (strs_len_items (strs), strs->num, &len)) { +ERR (NULL ,"Overflow" ); + goto exit; + } str =malloc (len);if (!str) {ERR (NULL ,"Out of memory" );libsepol/src/kernel_to_conf.c +9
-2@@ -1663,7 +1663,15 @@ static char *attr_strs_to_str(struct strs *strs) }/* 2*strs->num - 1 because ", " follows all but last attr (followed by '\0') */ - len =strs_len_items (strs) +2 * strs->num -1 ; +if (__builtin_mul_overflow (strs->num,2 , &len)) { +ERR (NULL ,"Overflow" ); + goto exit; + } + len -=1 ; +if (__builtin_add_overflow (len,strs_len_items (strs), &len)) { +ERR (NULL ,"Overflow" ); + goto exit; + } str =malloc (len);if (!str) {ERR (NULL ,"Out of memory" ); --2.54.0
PATCH v5 6/6 libsepol: Add overflow checks
In reply to James CarterThe function stack_push() uses the common idiom of "*= 2" to grow the size of the stack, but there is not check for overflow. Use the __builtin_smul_overflow() function to report an overflow and return an error.
There are several calls of stack_push() that does not check for an error. Add checks for an error for these calls.
---
v5: No changes
libsepol/src/module_to_cil.c | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-)
libsepol/src/module_to_cil.c +25 -8
@@ -445,15 +445,21 @@ static int stack_push(struct stack *stack, void *ptr) {int rc =-1 ;void *new_stack; +int new_size;if (stack->pos +1 == stack->size) { - new_stack =reallocarray (stack->stack, stack->size *2 , +if (__builtin_smul_overflow (stack->size,2 , &new_size)) { +ERR (NULL ,"Overflow" ); + goto exit; + } + new_stack =reallocarray (stack->stack, new_size,sizeof (*stack->stack));if (new_stack ==NULL ) { +ERR (NULL ,"Out of memory" ); goto exit; } stack->stack = new_stack; - stack->size *=2 ; + stack->size = new_size; } stack->pos++;
@@ -1482,7 +1488,6 @@ static int cond_expr_to_cil(int indent, struct policydb *pdb, rc =stack_push (stack, new_val);if (rc !=0 ) { -ERR (NULL ,"Out of memory" ); goto exit; } new_val =NULL ;
@@ -2163,7 +2168,6 @@ static int constraint_expr_to_string(struct policydb *pdb, rc =stack_push (stack, new_val);if (rc !=0 ) { -ERR (NULL ,"Out of memory" ); goto exit; }
@@ -4183,7 +4187,10 @@ static int module_block_to_cil(struct policydb *pdb, struct avrule_block *block, (*indent)++; } -stack_push (stack, decl); + rc =stack_push (stack, decl); +if (rc !=0 ) { + goto exit; + } rc =block_to_cil (pdb, block, stack, *indent);if (rc !=0 ) {
@@ -4210,7 +4217,10 @@ static int global_block_to_cil(struct policydb *pdb, struct avrule_block *block, "Warning: 'else' not allowed in global block. Dropping from output."); } -stack_push (stack, decl); + rc =stack_push (stack, decl); +if (rc !=0 ) { + goto exit; + }// type aliases and commons are only stored in the global symtab. // However, to get scoping correct, we assume they are in the
@@ -4284,7 +4294,10 @@ static int linked_block_to_cil(struct policydb *pdb, struct avrule_block *block, } } -stack_push (stack, decl); + rc =stack_push (stack, decl); +if (rc !=0 ) { + goto exit; + } rc =block_to_cil (pdb, block, stack,0 );if (rc !=0 ) {
@@ -4595,7 +4608,11 @@ static int fp_to_buffer(FILE *fp, char **data, size_t *data_len)while ((read_len =fread (d + d_len,1 , max_len - d_len, fp)) >0 ) { d_len += read_len;if (d_len == max_len) { - max_len *=2 ; +if (__builtin_mul_overflow (max_len,2 , &max_len)) { +ERR (NULL ,"Overflow" ); + rc =-1 ; + goto exit; + } d_tmp =realloc (d, max_len);if (d_tmp ==NULL ) {ERR (NULL ,"Out of memory" );
Signature · 1 line
2.54.0