libselinux: Add checks when growing lists to prevent wrapping
PATCH v2 1/4 libselinux: Add checks when growing lists to prevent wrapping
A common idiom to grow a list or buffer is to use "len *= 2", but it is possible for the multiplication to wrap on overflow. This would result in the new len being smaller than the old len and could lead to problems.
Add checks to prevent doing the multiplication if it would wrap. The multiplication can always be done if len <= MAX / 2 and should not be done if len > MAX / 2.
--- libselinux/src/matchpathcon.c | 4 ++++ libselinux/src/seusers.c | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-)
libselinux/src/matchpathcon.c +4 -0
@@ -117,6 +117,10 @@ static int add_array_elt(char *con)char **tmp;if (con_array_size) {while (con_array_used >= con_array_size) { +if (con_array_size >INT_MAX /2 ) { +free_array_elts (); +return -1 ; + } con_array_size *=2 ; tmp = (char **)reallocarray (con_array, con_array_size,sizeof (char *));
libselinux/src/seusers.c +1 -1
@@ -114,7 +114,7 @@ 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 && rbuflen <=LONG_MAX /2 ) {free (rbuf); rbuflen *=2 ;continue ;
Signature · 1 line
2.54.0
PATCH v2 2/4 libsemanage: Add checks when growing lists to prevent wrapping
A common idiom to grow a list or buffer is to use "len *= 2", but it is possible for the multiplication to wrap on overflow. This would result in the new len being smaller than the old len and could lead to problems.
Add checks to prevent doing the multiplication if it would wrap. The multiplication can always be done if len <= MAX / 2 and should not be done if len > MAX / 2.
--- libsemanage/src/direct_api.c | 6 +++++- libsemanage/src/genhomedircon.c | 10 +++++----- 2 files changed, 10 insertions(+), 6 deletions(-)
libsemanage/src/direct_api.c +5 -1
@@ -588,7 +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; - +if (max_len >SIZE_MAX /2 ) { +ERR (sh,"Too much data" ); +free (data_read); +return -1 ; + } max_len *=2 ; tmp =realloc (data_read, max_len);if (tmp ==NULL ) {
libsemanage/src/genhomedircon.c +5 -5
@@ -1090,7 +1090,7 @@ retry: goto cleanup; retval =getpwnam_r (name, &pwstorage, rbuf, rbuflen, &pwent); -if (retval ==ERANGE && rbuflen <LONG_MAX /2 ) { +if (retval ==ERANGE && rbuflen <=LONG_MAX /2 ) {free (rbuf); rbuflen *=2 ; goto retry;
@@ -1171,11 +1171,11 @@ 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 (grbuflen >LONG_MAX /2 ) { +ERR (s->h_semanage,"Member list too long" ); goto cleanup; + } + grbuflen *=2 ; new_grbuf =realloc (grbuf, grbuflen);if (new_grbuf ==NULL ) goto cleanup;
Signature · 1 line
2.54.0
Re: [PATCH 2/4 v2] libsemanage: Add checks when growing lists to prevent wrapping
Quoted 11 lines · James Carter
A common idiom to grow a list or buffer is to use "len *= 2", but it is possible for the multiplication to wrap on overflow. This would result in the new len being smaller than the old len and could lead to problems.
Add checks to prevent doing the multiplication if it would wrap. The multiplication can always be done if len <= MAX / 2 and should not be done if len > MAX / 2.
Signed-off-by James Carter <[email protected]>
Could also use __builtin_mul_overflow() used elsewhere, but
Quoted 55 lines
--- libsemanage/src/direct_api.c | 6 +++++- libsemanage/src/genhomedircon.c | 10 +++++----- 2 files changed, 10 insertions(+), 6 deletions(-)libsemanage/src/direct_api.c +5
-1@@ -588,7 +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; - +if (max_len >SIZE_MAX /2 ) { +ERR (sh,"Too much data" ); +free (data_read); +return -1 ; + } max_len *=2 ; tmp =realloc (data_read, max_len);if (tmp ==NULL ) {libsemanage/src/genhomedircon.c +5
-6@@ -1090,7 +1090,7 @@ retry: goto cleanup; retval =getpwnam_r (name, &pwstorage, rbuf, rbuflen, &pwent); -if (retval ==ERANGE && rbuflen <LONG_MAX /2 ) { +if (retval ==ERANGE && rbuflen <=LONG_MAX /2 ) {free (rbuf); rbuflen *=2 ; goto retry;@@ -1171,11 +1171,11 @@ 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 (grbuflen >LONG_MAX /2 ) { +ERR (s->h_semanage,"Member list too long" ); goto cleanup; + } + grbuflen *=2 ; new_grbuf =realloc (grbuf, grbuflen);if (new_grbuf ==NULL ) goto cleanup; --2.54.0
Re: [PATCH 2/4 v2] libsemanage: Add checks when growing lists to prevent wrapping
Quoted 8 lines · Stephen Smalley
Quoted 6 lines · James Carter
A common idiom to grow a list or buffer is to use "len *= 2", but it is possible for the multiplication to wrap on overflow. This would result in the new len being smaller than the old len and could lead to problems.
Quoted 9 lines
Quoted 5 lines
Add checks to prevent doing the multiplication if it would wrap. The multiplication can always be done if len <= MAX / 2 and should not be done if len > MAX / 2.
Signed-off-by James Carter <[email protected]>Could also use
__builtin_mul_overflow()used elsewhere, butAcked-by Stephen Smalley <[email protected]>
That is a good suggestion. I am going to use the__builtin_mul_overflow family of functions.
Thanks,
Jim
Quoted 55 lines
Quoted 55 lines
--- libsemanage/src/direct_api.c | 6 +++++- libsemanage/src/genhomedircon.c | 10 +++++----- 2 files changed, 10 insertions(+), 6 deletions(-)libsemanage/src/direct_api.c +5
-1@@ -588,7 +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; - +if (max_len >SIZE_MAX /2 ) { +ERR (sh,"Too much data" ); +free (data_read); +return -1 ; + } max_len *=2 ; tmp =realloc (data_read, max_len);if (tmp ==NULL ) {libsemanage/src/genhomedircon.c +5
-6@@ -1090,7 +1090,7 @@ retry: goto cleanup; retval =getpwnam_r (name, &pwstorage, rbuf, rbuflen, &pwent); -if (retval ==ERANGE && rbuflen <LONG_MAX /2 ) { +if (retval ==ERANGE && rbuflen <=LONG_MAX /2 ) {free (rbuf); rbuflen *=2 ; goto retry;@@ -1171,11 +1171,11 @@ 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 (grbuflen >LONG_MAX /2 ) { +ERR (s->h_semanage,"Member list too long" ); goto cleanup; + } + grbuflen *=2 ; new_grbuf =realloc (grbuf, grbuflen);if (new_grbuf ==NULL ) goto cleanup; --2.54.0
PATCH v2 3/4 libsepol/cil: Add checks when growing lists to prevent wrapping
In reply to James CarterA common idiom to grow a list or buffer is to use "len *= 2", but it is possible for the multiplication to wrap on overflow. This would result in the new len being smaller than the old len and could lead to problems.
Add checks to prevent doing the multiplication if it would wrap. The multiplication can always be done if len <= MAX / 2 and should not be done if len > MAX / 2.
--- libsepol/cil/src/cil_resolve_ast.c | 7 +++++++ libsepol/cil/src/cil_stack.c | 4 ++++ libsepol/cil/src/cil_verify.c | 28 ++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+)
libsepol/cil/src/cil_resolve_ast.c +7 -0
@@ -652,6 +652,13 @@ static int cil_resolve_alias_to_actual(struct cil_tree_node *current, }if (steps == limit) { +if (limit >INT_MAX /2 ) { +cil_tree_log ( + current,CIL_ERR , +"Alias checking did not resolve for %s" , + alias->datum.name); +return SEPOL_ERR ; + } steps =0 ; limit *=2 ; a2 = a1;
libsepol/cil/src/cil_stack.c +4 -0
@@ -77,6 +77,10 @@ void cil_stack_push(struct cil_stack *stack, enum cil_flavor flavor, void *data) stack->pos++;if (stack->pos == stack->size) { +if (stack->size >INT_MAX /2 ) { +cil_log (CIL_ERR ,"Stack too large\n" ); +exit (1 ); + } stack->size *=2 ; stack->stack =cil_realloc (stack->stack,sizeof (*stack->stack) * stack->size);
libsepol/cil/src/cil_verify.c +28 -0
@@ -975,6 +975,13 @@ static int __cil_verify_user_pre_eval(struct cil_tree_node *node) }if (steps == limit) { +if (limit >INT_MAX /2 ) { +cil_log (CIL_ERR , +"User bounds checking failed " +"to finish for user %s\n" , + user->datum.name); + goto exit; + } steps =0 ; limit *=2 ; u1 = u2;
@@ -1028,6 +1035,13 @@ static int __cil_verify_role(struct cil_tree_node *node) }if (steps == limit) { +if (limit >INT_MAX /2 ) { +cil_log (CIL_ERR , +"Role bounds checking failed " +"to finish for role %s\n" , + role->datum.name); + goto exit; + } steps =0 ; limit *=2 ; r1 = r2;
@@ -1060,6 +1074,13 @@ static int __cil_verify_type(struct cil_tree_node *node) }if (steps == limit) { +if (limit >INT_MAX /2 ) { +cil_log (CIL_ERR , +"Type bounds checking failed " +"to finish for type %s\n" , + type->datum.name); + goto exit; + } steps =0 ; limit *=2 ; t1 = t2;
@@ -2112,6 +2133,13 @@ static int __cil_verify_classperms(struct cil_list *classperms, }else { steps++;if (steps > limit) { +if (limit >INT_MAX /2 ) { +cil_log (CIL_ERR , +"Class permission circular reference " +"checking failed to finish for %s" , + cur->name); + goto exit; + } steps =1 ; limit *=2 ; orig = cur;
Signature · 1 line
2.54.0
Stephen Smalley
maintainerRe: [PATCH 3/4 v2] libsepol/cil: Add checks when growing lists to prevent wrapping
Quoted 22 lines · James Carter
A common idiom to grow a list or buffer is to use "len *= 2", but it is possible for the multiplication to wrap on overflow. This would result in the new len being smaller than the old len and could lead to problems.
Add checks to prevent doing the multiplication if it would wrap. The multiplication can always be done if len <= MAX / 2 and should not be done if len > MAX / 2.
Signed-off-by James Carter <[email protected]>--- libsepol/cil/src/cil_resolve_ast.c | 7 +++++++ libsepol/cil/src/cil_stack.c | 4 ++++ libsepol/cil/src/cil_verify.c | 28 ++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+)libsepol/cil/src/cil_stack.c +0
-0
libsepol/cil/src/cil_stack.c void cil_stack_push(struct cil_stack *stack, enum cil_flavor flavor, void *data) · line 86
Quoted 10 lines
libsepol/cil/src/cil_stack.c +4
-0@@ -77,6 +77,10 @@ void cil_stack_push(struct cil_stack *stack, enum cil_flavor flavor, void *data) stack->pos++;if (stack->pos == stack->size) { +if (stack->size >INT_MAX /2 ) { +cil_log (CIL_ERR ,"Stack too large\n" ); +exit (1 ); + } stack->size *=2 ; stack->stack =cil_realloc (stack->stack,sizeof (*stack->stack) * stack->size);
Can't this still overflow on the sizeof(*strack->stack)*stack->size calculation? reallocarray() is good for this kind of thing.
Quoted 5 lines
libsepol/cil/src/cil_verify.c +0
-0
libsepol/cil/src/cil_verify.c static int __cil_verify_user_pre_eval(struct cil_tree_node *node) · line 984
Quoted 10 lines
libsepol/cil/src/cil_verify.c +7
-0@@ -975,6 +975,13 @@ static int __cil_verify_user_pre_eval(struct cil_tree_node *node) }if (steps == limit) { +if (limit >INT_MAX /2 ) { +cil_log (CIL_ERR , +"User bounds checking failed " +"to finish for user %s\n" , + user->datum.name); + goto exit; + }
Not sure about the error messages for all of these; they are overflow conditions unrelated to the bounds checking.
libsepol/cil/src/cil_verify.c static int __cil_verify_classperms(struct cil_list *classperms, · line 2136
Quoted 5 lines
libsepol/cil/src/cil_verify.c +1
-0@@ -2112,6 +2133,13 @@ static int __cil_verify_classperms(struct cil_list *classperms, }else { steps++;if (steps > limit) { +if (limit >INT_MAX /2 ) {
limit here is unsigned so UINT_MAX / 2 would match its type but no big deal either way.
Quoted 13 lines
libsepol/cil/src/cil_verify.c +6
-1+cil_log (CIL_ERR , +"Class permission circular reference " +"checking failed to finish for %s" , + cur->name); + goto exit; + } steps =1 ; limit *=2 ; orig = cur; --2.54.0
Stephen Smalley
maintainerRe: [PATCH 3/4 v2] libsepol/cil: Add checks when growing lists to prevent wrapping
Quoted 26 lines · Stephen Smalley
Quoted 24 lines · James Carter
A common idiom to grow a list or buffer is to use "len *= 2", but it is possible for the multiplication to wrap on overflow. This would result in the new len being smaller than the old len and could lead to problems.
Add checks to prevent doing the multiplication if it would wrap. The multiplication can always be done if len <= MAX / 2 and should not be done if len > MAX / 2.
Signed-off-by James Carter <[email protected]>--- libsepol/cil/src/cil_resolve_ast.c | 7 +++++++ libsepol/cil/src/cil_stack.c | 4 ++++ libsepol/cil/src/cil_verify.c | 28 ++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+)libsepol/cil/src/cil_stack.c +0
-0@@ -77,6 +77,10 @@ void cil_stack_push(struct cil_stack *stack, enum cil_flavor flavor, void *data) stack->pos++;
Quoted 11 lines
libsepol/cil/src/cil_stack.c void cil_stack_push(struct cil_stack *stack, enum cil_flavor flavor, void *data) · line 86
Quoted 8 lines
libsepol/cil/src/cil_stack.c +4
-0if (stack->pos == stack->size) { +if (stack->size >INT_MAX /2 ) { +cil_log (CIL_ERR ,"Stack too large\n" ); +exit (1 ); + } stack->size *=2 ; stack->stack =cil_realloc (stack->stack,sizeof (*stack->stack) * stack->size);Can't this still overflow on the sizeof(*strack->stack)*stack->size calculation?
reallocarray()is good for this kind of thing.
I guess this one is ok because stack->size is capped at INT_MAX and sizeof(*stack->stack) * INT_MAX can't exceed SIZE_MAX.
Quoted 42 lines
Quoted 6 lines
libsepol/cil/src/cil_verify.c +0
-0libsepol/cil/src/cil_verify.c static int __cil_verify_user_pre_eval(struct cil_tree_node *node) · line 984
Quoted 10 lines
libsepol/cil/src/cil_verify.c +7
-0@@ -975,6 +975,13 @@ static int __cil_verify_user_pre_eval(struct cil_tree_node *node) }if (steps == limit) { +if (limit >INT_MAX /2 ) { +cil_log (CIL_ERR , +"User bounds checking failed " +"to finish for user %s\n" , + user->datum.name); + goto exit; + }Not sure about the error messages for all of these; they are overflow conditions unrelated to the bounds checking.
libsepol/cil/src/cil_verify.c static int __cil_verify_classperms(struct cil_list *classperms, · line 2136
Quoted 5 lines
libsepol/cil/src/cil_verify.c +1
-0@@ -2112,6 +2133,13 @@ static int __cil_verify_classperms(struct cil_list *classperms, }else { steps++;if (steps > limit) { +if (limit >INT_MAX /2 ) {limit here is unsigned so
UINT_MAX/ 2 would match its type but no big deal either way.Quoted 13 lines
libsepol/cil/src/cil_verify.c +6
-1+cil_log (CIL_ERR , +"Class permission circular reference " +"checking failed to finish for %s" , + cur->name); + goto exit; + } steps =1 ; limit *=2 ; orig = cur; --2.54.0
Stephen Smalley
maintainerRe: [PATCH 3/4 v2] libsepol/cil: Add checks when growing lists to prevent wrapping
Quoted 41 lines · Stephen Smalley
Quoted 26 lines · Stephen Smalley
Quoted 24 lines · James Carter
A common idiom to grow a list or buffer is to use "len *= 2", but it is possible for the multiplication to wrap on overflow. This would result in the new len being smaller than the old len and could lead to problems.
Add checks to prevent doing the multiplication if it would wrap. The multiplication can always be done if len <= MAX / 2 and should not be done if len > MAX / 2.
Signed-off-by James Carter <[email protected]>--- libsepol/cil/src/cil_resolve_ast.c | 7 +++++++ libsepol/cil/src/cil_stack.c | 4 ++++ libsepol/cil/src/cil_verify.c | 28 ++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+)libsepol/cil/src/cil_stack.c +0
-0@@ -77,6 +77,10 @@ void cil_stack_push(struct cil_stack *stack, enum cil_flavor flavor, void *data) stack->pos++;Quoted 11 lines
libsepol/cil/src/cil_stack.c void cil_stack_push(struct cil_stack *stack, enum cil_flavor flavor, void *data) · line 86
Quoted 8 lines
libsepol/cil/src/cil_stack.c +4
-0if (stack->pos == stack->size) { +if (stack->size >INT_MAX /2 ) { +cil_log (CIL_ERR ,"Stack too large\n" ); +exit (1 ); + } stack->size *=2 ; stack->stack =cil_realloc (stack->stack,sizeof (*stack->stack) * stack->size);Can't this still overflow on the sizeof(*strack->stack)*stack->size calculation?
reallocarray()is good for this kind of thing.
Quoted 2 lines
I guess this one is ok because
stack->sizeis capped atINT_MAXandsizeof(*stack->stack)*INT_MAXcan't exceedSIZE_MAX.
Never mind, I was right the first time - on 32-bit it can still overflow.
Quoted 43 lines
Quoted 43 lines
Quoted 7 lines
libsepol/cil/src/cil_verify.c +0
-0libsepol/cil/src/cil_verify.c static int __cil_verify_user_pre_eval(struct cil_tree_node *node) · line 984
Quoted 10 lines
libsepol/cil/src/cil_verify.c +7
-0@@ -975,6 +975,13 @@ static int __cil_verify_user_pre_eval(struct cil_tree_node *node) }if (steps == limit) { +if (limit >INT_MAX /2 ) { +cil_log (CIL_ERR , +"User bounds checking failed " +"to finish for user %s\n" , + user->datum.name); + goto exit; + }Not sure about the error messages for all of these; they are overflow conditions unrelated to the bounds checking.
libsepol/cil/src/cil_verify.c static int __cil_verify_classperms(struct cil_list *classperms, · line 2136
Quoted 5 lines
libsepol/cil/src/cil_verify.c +1
-0@@ -2112,6 +2133,13 @@ static int __cil_verify_classperms(struct cil_list *classperms, }else { steps++;if (steps > limit) { +if (limit >INT_MAX /2 ) {limit here is unsigned so
UINT_MAX/ 2 would match its type but no big deal either way.Quoted 13 lines
libsepol/cil/src/cil_verify.c +6
-1+cil_log (CIL_ERR , +"Class permission circular reference " +"checking failed to finish for %s" , + cur->name); + goto exit; + } steps =1 ; limit *=2 ; orig = cur; --2.54.0
Re: [PATCH 3/4 v2] libsepol/cil: Add checks when growing lists to prevent wrapping
Quoted 44 lines · Stephen Smalley
Quoted 41 lines · Stephen Smalley
Quoted 37 lines · Stephen Smalley
Quoted 22 lines · James Carter
A common idiom to grow a list or buffer is to use "len *= 2", but it is possible for the multiplication to wrap on overflow. This would result in the new len being smaller than the old len and could lead to problems.
Add checks to prevent doing the multiplication if it would wrap. The multiplication can always be done if len <= MAX / 2 and should not be done if len > MAX / 2.
Signed-off-by James Carter <[email protected]>--- libsepol/cil/src/cil_resolve_ast.c | 7 +++++++ libsepol/cil/src/cil_stack.c | 4 ++++ libsepol/cil/src/cil_verify.c | 28 ++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+)libsepol/cil/src/cil_stack.c +0
-0libsepol/cil/src/cil_stack.c void cil_stack_push(struct cil_stack *stack, enum cil_flavor flavor, void *data) · line 86
Quoted 10 lines
libsepol/cil/src/cil_stack.c +4
-0@@ -77,6 +77,10 @@ void cil_stack_push(struct cil_stack *stack, enum cil_flavor flavor, void *data) stack->pos++;if (stack->pos == stack->size) { +if (stack->size >INT_MAX /2 ) { +cil_log (CIL_ERR ,"Stack too large\n" ); +exit (1 ); + } stack->size *=2 ; stack->stack =cil_realloc (stack->stack,sizeof (*stack->stack) * stack->size);Can't this still overflow on the sizeof(*strack->stack)*stack->size calculation?
reallocarray()is good for this kind of thing.
Quoted 5 lines
Quoted 2 lines
I guess this one is ok because
stack->sizeis capped atINT_MAXandsizeof(*stack->stack)*INT_MAXcan't exceedSIZE_MAX.Never mind, I was right the first time - on 32-bit it can still overflow.
I should add a cil_reallocarray.
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
libsepol/cil/src/cil_verify.c +0
-0@@ -975,6 +975,13 @@ static int __cil_verify_user_pre_eval(struct cil_tree_node *node) }
Quoted 12 lines
Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
libsepol/cil/src/cil_verify.c +7
-0if (steps == limit) { +if (limit >INT_MAX /2 ) { +cil_log (CIL_ERR , +"User bounds checking failed " +"to finish for user %s\n" , + user->datum.name); + goto exit; + }Quoted 3 lines
Quoted 3 lines
Not sure about the error messages for all of these; they are overflow conditions unrelated to the bounds checking.
Honestly, I don't know if an error message is needed at all. No real policy is going to overflow while checking for circular bounds.
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
libsepol/cil/src/cil_verify.c static int __cil_verify_classperms(struct cil_list *classperms, · line 2136
Quoted 5 lines
libsepol/cil/src/cil_verify.c +1
-0@@ -2112,6 +2133,13 @@ static int __cil_verify_classperms(struct cil_list *classperms, }else { steps++;if (steps > limit) { +if (limit >INT_MAX /2 ) {limit here is unsigned so
UINT_MAX/ 2 would match its type but no big deal either way.
I missed that this was using a different type than the others. Thanks,
Jim
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
libsepol/cil/src/cil_verify.c +6
-1+cil_log (CIL_ERR , +"Class permission circular reference " +"checking failed to finish for %s" , + cur->name); + goto exit; + } steps =1 ; limit *=2 ; orig = cur; --2.54.0
PATCH v2 4/4 libsepol: Add checks when growing lists to prevent wrapping
In reply to James CarterA common idiom to grow a list or buffer is to use "len *= 2", but it is possible for the multiplication to wrap on overflow. This would result in the new len being smaller than the old len and could lead to problems.
Add checks to prevent doing the multiplication if it would wrap. The multiplication can always be done if len <= MAX / 2 and should not be done if len > MAX / 2.
---
v2: Add check in strs_init
libsepol/src/kernel_to_common.c | 18 ++++++++++++++++++ libsepol/src/module_to_cil.c | 12 ++++++++++-- 2 files changed, 28 insertions(+), 2 deletions(-)
libsepol/src/kernel_to_common.c +18 -0
@@ -22,6 +22,8 @@#include "private.h" #include "kernel_to_common.h" +#define MAX_NUM_STRS 1UL << 24 +void sepol_indent (FILE *out,int indent) {if (fprintf (out,"%*s" , indent *4 ,"" ) <0 ) {
@@ -61,6 +63,8 @@ int strs_init(struct strs **strs, size_t size)if (size ==0 ) { size =1 ; + }else if (size >MAX_NUM_STRS ) { + size =MAX_NUM_STRS ; } *strs =NULL ;
@@ -117,7 +121,14 @@ int strs_add(struct strs *strs, char *s)if (strs->num +1 > strs->size) {char **new;size_t i = strs->size; +if (strs->size ==MAX_NUM_STRS ) { +ERR (NULL ,"Too many strings" ); +return -1 ; + } strs->size *=2 ; +if (strs->size >MAX_NUM_STRS ) { + strs->size =MAX_NUM_STRS ; + } new =reallocarray (strs->list, strs->size,sizeof (char *));if (!new) {ERR (NULL ,"Out of memory" );
@@ -169,12 +180,19 @@ char *strs_remove_last(struct strs *strs)int strs_add_at_index (struct strs *strs,char *s,size_t index) { +if (index >=MAX_NUM_STRS ) { +ERR (NULL ,"Index too large" ); +return -1 ; + }if (index >= strs->size) {char **new;size_t i = strs->size;while (index >= strs->size) { strs->size *=2 ; } +if (strs->size >MAX_NUM_STRS ) { + strs->size =MAX_NUM_STRS ; + } new =reallocarray (strs->list, strs->size,sizeof (char *));if (!new) {ERR (NULL ,"Out of memory" );
libsepol/src/module_to_cil.c +10 -2
@@ -447,13 +447,16 @@ static int stack_push(struct stack *stack, void *ptr)void *new_stack;if (stack->pos +1 == stack->size) { - new_stack =reallocarray (stack->stack, stack->size *2 , +if (stack->size >INT_MAX /2 ) { + goto exit; + } + stack->size *=2 ; + new_stack =reallocarray (stack->stack, stack->size,sizeof (*stack->stack));if (new_stack ==NULL ) { goto exit; } stack->stack = new_stack; - stack->size *=2 ; } stack->pos++;
@@ -4595,6 +4598,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) { +if (max_len >SIZE_MAX /2 ) { +ERR (NULL ,"Buffer too large" ); + rc =-1 ; + goto exit; + } max_len *=2 ; d_tmp =realloc (d, max_len);if (d_tmp ==NULL ) {
Signature · 1 line
2.54.0
Quoted 11 lines · James Carter
A common idiom to grow a list or buffer is to use "len *= 2", but it is possible for the multiplication to wrap on overflow. This would result in the new len being smaller than the old len and could lead to problems.
Add checks to prevent doing the multiplication if it would wrap. The multiplication can always be done if len <= MAX / 2 and should not be done if len > MAX / 2.
Signed-off-by James Carter <[email protected]>
Could also use __builtin_mul_overflow() used elsewhere, but
Quoted 37 lines
--- libselinux/src/matchpathcon.c | 4 ++++ libselinux/src/seusers.c | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-)libselinux/src/matchpathcon.c +4
-0@@ -117,6 +117,10 @@ static int add_array_elt(char *con)char **tmp;if (con_array_size) {while (con_array_used >= con_array_size) { +if (con_array_size >INT_MAX /2 ) { +free_array_elts (); +return -1 ; + } con_array_size *=2 ; tmp = (char **)reallocarray (con_array, con_array_size,sizeof (char *));libselinux/src/seusers.c +1
-2@@ -114,7 +114,7 @@ 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 && rbuflen <=LONG_MAX /2 ) {free (rbuf); rbuflen *=2 ;continue ; --2.54.0