Skip to contentSkip to conversation

libselinux: Add overflow checks

PATCH Superseded James Carter 2 participants 10 messages Jun 23 SELinux security module

v4 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

PATCH v4 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.

---
v4: 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
index 2623e5dc..80de61a0 100644
@@ -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
index e4a844df..310b1e7c 100644
@@ -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
#1 lore raw

PATCH v4 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.

---
v4: 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
index 9572dbf0..b87b87e7 100644
@@ -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
index ce7ea167..f04fc127 100644
@@ -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
#2 lore raw

PATCH v4 3/6 libsepol/cil: Add overflow checks for circular reference checks

In reply to James Carter

When 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.

---
v4: Add '\n" for cil_log() messages

 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
index bdbb871a..a805ca17 100644
@@ -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
index eb732e44..699cdccf 100644
@@ -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
maintainer Acked-by on v4 3/6
#3 lore raw

Re: [PATCH 3/6 v4] libsepol/cil: Add overflow checks for circular reference checks

Quoted 6 lines · James Carter

When 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).

Quoted 7 lines

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.

Quoted 83 lines

---
v4: Add '\n" for cil_log() messages

 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
index bdbb871a..a805ca17 100644
@@ -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 -5
index eb732e44..699cdccf 100644
@@ -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;
                }
        }
--

2.54.0

#4 lore raw

PATCH v4 4/6 libsepol/cil: Add overflow checks and cil_reallocarray

In reply to James Carter

The 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.

---
v4: Switch args for call to cil_reallocarray() to the correct order

 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
index aeffdae2..4f75122a 100644
@@ -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
index 7ec316af..f3f40c46 100644
@@ -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
index d9c7b0c0..da2efa5f 100644
@@ -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
maintainer Acked-by on v4 4/6
#5 lore raw

Re: [PATCH 4/6 v4] libsepol/cil: Add overflow checks and cil_reallocarray

Quoted 5 lines · James Carter

The 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.

Quoted 7 lines

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.

Quoted 66 lines

---
v4: Switch args for call to cil_reallocarray() to the correct order

 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
index aeffdae2..4f75122a 100644
@@ -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
index 7ec316af..f3f40c46 100644
@@ -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 -4
index d9c7b0c0..da2efa5f 100644
@@ -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;
--

2.54.0

#6 lore raw

PATCH v4 5/6 libsepol: Add overflow checks to strs_* family of functions

In reply to James Carter

The 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.

---
v4: Fix spelling in commit message
Add temporary variables for the new size for strs_add() and
strs_add_at_index()

 libsepol/src/kernel_to_common.c | 27 +++++++++++++++++++++------
 libsepol/src/kernel_to_conf.c   | 10 +++++++++-
 2 files changed, 30 insertions(+), 7 deletions(-)
libsepol/src/kernel_to_common.c +21 -6
index adb7b661..9fc29cb9 100644
@@ -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;
+		size_t new_size;
 		while (index >= strs->size) {
-			strs->size *= 2;
+			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
index 3e6bcb33..9bfe5b38 100644
@@ -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
maintainer
#7 lore raw

Re: [PATCH 5/6 v4] libsepol: Add overflow checks to strs_* family of functions

Quoted 53 lines · James Carter

The 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.

---
v4: Fix spelling in commit message
Add temporary variables for the new size for strs_add() and
strs_add_at_index()

 libsepol/src/kernel_to_common.c | 27 +++++++++++++++++++++------
 libsepol/src/kernel_to_conf.c   | 10 +++++++++-
 2 files changed, 30 insertions(+), 7 deletions(-)
libsepol/src/kernel_to_common.c +7 -2
index adb7b661..9fc29cb9 100644
@@ -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 7 lines
libsepol/src/kernel_to_common.c +2 -1
@@ -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;
+               size_t new_size;
                while (index >= strs->size) {
-                       strs->size *= 2;
+                       if (__builtin_mul_overflow(strs->size, 2, &new_size)) {

Since you aren't updating strs->size here anymore, the loop condition won't use the new size nor
will the next multiply.

Maybe:

    size_t new_size = strs->size;
   while (index >= new_size) {
           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
index 3e6bcb33..9bfe5b38 100644
@@ -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

#8 lore raw

PATCH v4 6/6 libsepol: Add overflow checks

In reply to James Carter

The 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.

---
v4: Add temporary variable for the new size for stack_push()

 libsepol/src/module_to_cil.c | 33 +++++++++++++++++++++++++--------
 1 file changed, 25 insertions(+), 8 deletions(-)
libsepol/src/module_to_cil.c +25 -8
index 00db3d7f..2aaf4feb 100644
@@ -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
maintainer Acked-by on v4 6/6
#9 lore raw

Re: [PATCH 6/6 v4] libsepol: Add overflow checks

Quoted 10 lines · James Carter

The 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.

Quoted 102 lines

---
v4: Add temporary variable for the new size for stack_push()

 libsepol/src/module_to_cil.c | 33 +++++++++++++++++++++++++--------
 1 file changed, 25 insertions(+), 8 deletions(-)
libsepol/src/module_to_cil.c +25 -9
index 00db3d7f..2aaf4feb 100644
@@ -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");
--

2.54.0