Skip to contentSkip to conversation

libselinux: Add checks when growing lists to prevent wrapping

PATCH Superseded James Carter 2 participants 6 messages Jun 17 SELinux security module

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 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
index 2623e5dc..dc276b7b 100644
@@ -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
index e4a844df..08514bd6 100644
@@ -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
#1 lore raw

PATCH 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
index 9572dbf0..54803515 100644
@@ -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
index ce7ea167..8dc2cb56 100644
@@ -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
#2 lore raw

PATCH 3/4 libsepol/cil: Add checks when growing lists to prevent wrapping

In reply to 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.

---
 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
index bdbb871a..23faf33f 100644
@@ -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
index d9c7b0c0..c80ed7ad 100644
@@ -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
index eb732e44..ee90b0ce 100644
@@ -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
#3 lore raw

PATCH 4/4 libsepol: Add checks when growing lists to prevent wrapping

In reply to 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.

---
 libsepol/src/kernel_to_common.c | 16 ++++++++++++++++
 libsepol/src/module_to_cil.c    | 12 ++++++++++--
 2 files changed, 26 insertions(+), 2 deletions(-)
libsepol/src/kernel_to_common.c +16 -0
index adb7b661..9ffd452b 100644
@@ -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) {
@@ -117,7 +119,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 +178,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
index 00db3d7f..ae7d700e 100644
@@ -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
#4 lore raw

Re: [PATCH 4/4] libsepol: Add checks when growing lists to prevent wrapping

Quoted 54 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.

---
 libsepol/src/kernel_to_common.c | 16 ++++++++++++++++
 libsepol/src/module_to_cil.c    | 12 ++++++++++--
 2 files changed, 26 insertions(+), 2 deletions(-)
libsepol/src/kernel_to_common.c +13 -0
index adb7b661..9ffd452b 100644
@@ -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) {
@@ -117,7 +119,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 +178,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;

libsepol/src/kernel_to_common.c char *strs_remove_last(struct strs *strs) · line 196

Quoted 10 lines
libsepol/src/kernel_to_common.c +3 -0
                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");

I just realized that I need to add a check in strs_init() as well. Standby for v2.
Jim

Quoted 38 lines
libsepol/src/module_to_cil.c +10 -3
index 00db3d7f..ae7d700e 100644
@@ -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) {
--

2.54.0

maintainer
#5 lore raw

Re: [PATCH 4/4] libsepol: Add checks when growing lists to prevent wrapping

In reply to James Carter
Quoted 21 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.

---
 libsepol/src/kernel_to_common.c | 16 ++++++++++++++++
 libsepol/src/module_to_cil.c    | 12 ++++++++++--
 2 files changed, 26 insertions(+), 2 deletions(-)
libsepol/src/kernel_to_common.c +0 -0
index adb7b661..9ffd452b 100644

libsepol/src/kernel_to_common.c line 25

Quoted 4 lines
libsepol/src/kernel_to_common.c +1 -0
@@ -22,6 +22,8 @@
 #include "private.h"
 #include "kernel_to_common.h"
 
+#define MAX_NUM_STRS 1UL << 24

Should parenthesize the expression (1UL << 24) so that if anyone ever does:
MAX_NUM_STRS - 1
it won't break.

Quoted 10 lines
libsepol/src/module_to_cil.c +1 -1
index 00db3d7f..ae7d700e 100644
@@ -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) {

libsepol/src/module_to_cil.c static int stack_push(struct stack *stack, void *ptr) · line 456

Quoted 10 lines
libsepol/src/module_to_cil.c +4 -1
+                       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;

Old code kept stack->size in sync with stack->stack; new code introduces a case where you could goto exit
with stack->size double what stack->stack permits so subsequent stack_push() could write OOB. Not a problem at the moment I think but probably worth fixing.