Skip to contentSkip to conversation

libselinux: Add overflow checks

PATCH Superseded James Carter 2 participants 15 messages Jun 22 – Jun 23 SELinux security module

v3 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

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

---
v3: Use __built_smul_overflow()

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

---
v3: Use __builtin_mul_overflow() family of functions

 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
maintainer Acked-by on v3 2/6
#2 lore raw

Re: [PATCH 2/6 v3] libsemanage: Add overflow checks

Quoted 8 lines · James Carter

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.

Quoted 59 lines

---
v3: Use __builtin_mul_overflow() family of functions

 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 -7
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;
--

2.54.0

#3 lore raw

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

---
v3: Use the __builtin_mul_overflow() family of functions
Split out cil_stack fixes to a new patch

 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..240f3b23 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");
+					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");
+				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");
+				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");
+				goto exit;
+			}
 			steps = 1;
-			limit *= 2;
 			orig = cur;
 		}
 	}
Signature · 1 line
2.54.0
maintainer
#4 lore raw

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

Quoted 43 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).

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.

---
v3: Use the __builtin_mul_overflow() family of functions
Split out cil_stack fixes to a new patch

 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 +0 -0
index eb732e44..240f3b23 100644

libsepol/cil/src/cil_verify.c static int __cil_verify_user_pre_eval(struct cil_tree_node *node) · line 979

Quoted 5 lines
libsepol/cil/src/cil_verify.c +2 -0
@@ -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");

cil_log() appears to require the caller to provide the terminating \n, unlike cil_tree_log().
This is repeated below.

Quoted 49 lines
libsepol/cil/src/cil_verify.c +14 -5
+                                       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");
+                               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");
+                               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");
+                               goto exit;
+                       }
                        steps = 1;
-                       limit *= 2;
                        orig = cur;
                }
        }
--

2.54.0

#5 lore raw

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

Quoted 47 lines · Stephen Smalley
Quoted 45 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).

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.

---
v3: Use the __builtin_mul_overflow() family of functions
Split out cil_stack fixes to a new patch

 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 +0 -0
index eb732e44..240f3b23 100644
@@ -975,8 +975,11 @@ static int __cil_verify_user_pre_eval(struct cil_tree_node *node)
                        }
 
Quoted 7 lines

libsepol/cil/src/cil_verify.c static int __cil_verify_user_pre_eval(struct cil_tree_node *node) · line 979

Quoted 3 lines
libsepol/cil/src/cil_verify.c +2 -0
                        if (steps == limit) {
+                               if (__builtin_smul_overflow(limit, 2, &limit)) {
+                                       cil_log(CIL_ERR, "Overflow");

cil_log() appears to require the caller to provide the terminating \n, unlike cil_tree_log().
This is repeated below.

Thanks, I will fix that.
Jim

Quoted 50 lines
Quoted 50 lines
libsepol/cil/src/cil_verify.c +14 -5
+                                       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");
+                               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");
+                               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");
+                               goto exit;
+                       }
                        steps = 1;
-                       limit *= 2;
                        orig = cur;
                }
        }
--

2.54.0

#6 lore raw

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

---
v3: Previously in patch 0003
Use the __builtin_mul_overflow() family of functions Add a cil_reallocarray() function

 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..6fd051f8 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, sizeof(*stack->stack), stack->size);
 	}
 
 	stack->stack[stack->pos].flavor = flavor;
Signature · 1 line
2.54.0
maintainer
#7 lore raw

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

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

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.

---
v3: Previously in patch 0003
Use the __builtin_mul_overflow() family of functions Add a cil_reallocarray() function

 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 +0 -0
index d9c7b0c0..6fd051f8 100644
@@ -77,9 +77,12 @@ void cil_stack_push(struct cil_stack *stack, enum cil_flavor flavor, void *data)
        stack->pos++;
 

libsepol/cil/src/cil_stack.c void cil_stack_push(struct cil_stack *stack, enum cil_flavor flavor, void *data) · line 85

Quoted 10 lines
libsepol/cil/src/cil_stack.c +6 -3
        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, sizeof(*stack->stack), stack->size);

No real effect but you switched the sizeof(*stack->stack), stack->size order relative to the cil_reallocarray() interface.

Quoted 6 lines
libsepol/cil/src/cil_stack.c +0 -1
        }
 
        stack->stack[stack->pos].flavor = flavor;
--

2.54.0

#8 lore raw

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

Quoted 66 lines · Stephen Smalley
Quoted 64 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.

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.

---
v3: Previously in patch 0003
Use the __builtin_mul_overflow() family of functions Add a cil_reallocarray() function

 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 +0 -0
index d9c7b0c0..6fd051f8 100644
@@ -77,9 +77,12 @@ void cil_stack_push(struct cil_stack *stack, enum cil_flavor flavor, void *data)
        stack->pos++;
 
Quoted 13 lines

libsepol/cil/src/cil_stack.c void cil_stack_push(struct cil_stack *stack, enum cil_flavor flavor, void *data) · line 85

Quoted 10 lines
libsepol/cil/src/cil_stack.c +6 -3
        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, sizeof(*stack->stack), stack->size);

No real effect but you switched the sizeof(*stack->stack), stack->size order relative to the cil_reallocarray() interface.

That was just sloppiness on my part. I just replace the '*' with a ',' and didn't think about the order.
I will fix it.
Jim

Quoted 7 lines
Quoted 7 lines
libsepol/cil/src/cil_stack.c +0 -1
        }
 
        stack->stack[stack->pos].flavor = flavor;
--

2.54.0

#9 lore raw

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

In reply to James Carter

The strs_* family of functions provide convient 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.

---
v3: Previously in patch 0004
Use __builtin_mul_overflow() family of functions
Just check for overflow and don't set a maximum number of strings All check for addition overflow using __builtin_add_overflow()

 libsepol/src/kernel_to_common.c | 20 ++++++++++++++++----
 libsepol/src/kernel_to_conf.c   | 10 +++++++++-
 2 files changed, 25 insertions(+), 5 deletions(-)
libsepol/src/kernel_to_common.c +16 -4
index adb7b661..bcd13c9a 100644
@@ -117,7 +117,10 @@ int strs_add(struct strs *strs, char *s)
 	if (strs->num + 1 > strs->size) {
 		char **new;
 		size_t i = strs->size;
-		strs->size *= 2;
+		if (__builtin_mul_overflow(strs->size, 2, &strs->size)) {
+			ERR(NULL, "Overflow");
+			return -1;
+		}
 		new = reallocarray(strs->list, strs->size, sizeof(char *));
 		if (!new) {
 			ERR(NULL, "Out of memory");
@@ -173,7 +176,11 @@ int strs_add_at_index(struct strs *strs, char *s, size_t index)
 		char **new;
 		size_t i = strs->size;
 		while (index >= strs->size) {
-			strs->size *= 2;
+			if (__builtin_mul_overflow(strs->size, 2,
+						   &strs->size)) {
+				ERR(NULL, "Overflow");
+				return -1;
+			}
 		}
 		new = reallocarray(strs->list, strs->size, sizeof(char *));
 		if (!new) {
@@ -229,7 +236,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 +257,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
#10 lore raw

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

Quoted 2 lines · James Carter

The strs_* family of functions provide convient functions for

spelling

Quoted 31 lines

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.

---
v3: Previously in patch 0004
Use __builtin_mul_overflow() family of functions
Just check for overflow and don't set a maximum number of strings All check for addition overflow using __builtin_add_overflow()

 libsepol/src/kernel_to_common.c | 20 ++++++++++++++++----
 libsepol/src/kernel_to_conf.c   | 10 +++++++++-
 2 files changed, 25 insertions(+), 5 deletions(-)
libsepol/src/kernel_to_common.c +0 -0
index adb7b661..bcd13c9a 100644

libsepol/src/kernel_to_common.c int strs_add(struct strs *strs, char *s) · line 123

Quoted 8 lines
libsepol/src/kernel_to_common.c +4 -1
@@ -117,7 +117,10 @@ int strs_add(struct strs *strs, char *s)
        if (strs->num + 1 > strs->size) {
                char **new;
                size_t i = strs->size;
-               strs->size *= 2;
+               if (__builtin_mul_overflow(strs->size, 2, &strs->size)) {
+                       ERR(NULL, "Overflow");
+                       return -1;
+               }

Don't think it matters currently but you update strs->size here before the reallocarray and therefore
later error paths can return with inconsistent (size, list) pairs. Would be safer to compute the new
size to a temporary and only update strs->size if the reallocarray() succeeded.

libsepol/src/kernel_to_common.c int strs_add_at_index(struct strs *strs, char *s, size_t index) · line 183

Quoted 13 lines
libsepol/src/kernel_to_common.c +5 -1
                new = reallocarray(strs->list, strs->size, sizeof(char *));
                if (!new) {
                        ERR(NULL, "Out of memory");
@@ -173,7 +176,11 @@ int strs_add_at_index(struct strs *strs, char *s, size_t index)
                char **new;
                size_t i = strs->size;
                while (index >= strs->size) {
-                       strs->size *= 2;
+                       if (__builtin_mul_overflow(strs->size, 2,
+                                                  &strs->size)) {
+                               ERR(NULL, "Overflow");
+                               return -1;
+                       }

Same pattern here.

Quoted 50 lines
libsepol/src/kernel_to_common.c +7 -2
                }
                new = reallocarray(strs->list, strs->size, sizeof(char *));
                if (!new) {
@@ -229,7 +236,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 +257,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

#11 lore raw

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

Quoted 44 lines · Stephen Smalley
Quoted 2 lines · James Carter

The strs_* family of functions provide convient functions for

spelling

Quoted 37 lines

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.

---
v3: Previously in patch 0004
Use __builtin_mul_overflow() family of functions
Just check for overflow and don't set a maximum number of strings All check for addition overflow using __builtin_add_overflow()

 libsepol/src/kernel_to_common.c | 20 ++++++++++++++++----
 libsepol/src/kernel_to_conf.c   | 10 +++++++++-
 2 files changed, 25 insertions(+), 5 deletions(-)
libsepol/src/kernel_to_common.c +2 -1
index adb7b661..bcd13c9a 100644
@@ -117,7 +117,10 @@ int strs_add(struct strs *strs, char *s)
        if (strs->num + 1 > strs->size) {
                char **new;
                size_t i = strs->size;
-               strs->size *= 2;
+               if (__builtin_mul_overflow(strs->size, 2, &strs->size)) {
+                       ERR(NULL, "Overflow");
Quoted 8 lines

libsepol/src/kernel_to_common.c int strs_add(struct strs *strs, char *s) · line 123

Quoted 2 lines
libsepol/src/kernel_to_common.c +2 -0
+                       return -1;
+               }

Don't think it matters currently but you update strs->size here before the reallocarray and therefore
later error paths can return with inconsistent (size, list) pairs. Would be safer to compute the new
size to a temporary and only update strs->size if the reallocarray() succeeded.

Obviously, the only thing that should be done at this point is to exit with an error, but I can't guarantee that all the callers are going to do that, so the safe thing should be done.
Thanks,
Jim

Quoted 67 lines
Quoted 5 lines
libsepol/src/kernel_to_common.c +0 -0
                new = reallocarray(strs->list, strs->size, sizeof(char *));
                if (!new) {
                        ERR(NULL, "Out of memory");

libsepol/src/kernel_to_common.c int strs_add_at_index(struct strs *strs, char *s, size_t index) · line 183

Quoted 9 lines
libsepol/src/kernel_to_common.c +5 -1
@@ -173,7 +176,11 @@ int strs_add_at_index(struct strs *strs, char *s, size_t index)
                char **new;
                size_t i = strs->size;
                while (index >= strs->size) {
-                       strs->size *= 2;
+                       if (__builtin_mul_overflow(strs->size, 2,
+                                                  &strs->size)) {
+                               ERR(NULL, "Overflow");
+                               return -1;
+                       }

Same pattern here.

Quoted 50 lines
libsepol/src/kernel_to_common.c +7 -2
                }
                new = reallocarray(strs->list, strs->size, sizeof(char *));
                if (!new) {
@@ -229,7 +236,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 +257,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

#12 lore raw

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

---
v3: Previously in patch 0004
Use __builtin_mul_overflow() family of functions Add checks for an error for all calls to stack_push()

 libsepol/src/module_to_cil.c | 31 +++++++++++++++++++++++--------
 1 file changed, 23 insertions(+), 8 deletions(-)
libsepol/src/module_to_cil.c +23 -8
index 00db3d7f..5b34186b 100644
@@ -447,13 +447,17 @@ 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 (__builtin_smul_overflow(stack->size, 2, &stack->size)) {
+			ERR(NULL, "Overflow");
+			goto exit;
+		}
+		new_stack = reallocarray(stack->stack, stack->size,
 					 sizeof(*stack->stack));
 		if (new_stack == NULL) {
+			ERR(NULL, "Out of memory");
 			goto exit;
 		}
 		stack->stack = new_stack;
-		stack->size *= 2;
 	}
 
 	stack->pos++;
@@ -1482,7 +1486,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 +2166,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 +4185,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 +4215,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 +4292,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 +4606,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 v3 6/6
#13 lore raw

Re: [PATCH 6/6 v3] 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 100 lines

---
v3: Previously in patch 0004
Use __builtin_mul_overflow() family of functions Add checks for an error for all calls to stack_push()

 libsepol/src/module_to_cil.c | 31 +++++++++++++++++++++++--------
 1 file changed, 23 insertions(+), 8 deletions(-)
libsepol/src/module_to_cil.c +23 -9
index 00db3d7f..5b34186b 100644
@@ -447,13 +447,17 @@ 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 (__builtin_smul_overflow(stack->size, 2, &stack->size)) {
+                       ERR(NULL, "Overflow");
+                       goto exit;
+               }
+               new_stack = reallocarray(stack->stack, stack->size,
                                         sizeof(*stack->stack));
                if (new_stack == NULL) {
+                       ERR(NULL, "Out of memory");
                        goto exit;
                }
                stack->stack = new_stack;
-               stack->size *= 2;
        }
 
        stack->pos++;
@@ -1482,7 +1486,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 +2166,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 +4185,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 +4215,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 +4292,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 +4606,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

maintainer Acked-by on v3 1/6
#14 lore raw
In reply to James Carter
Quoted 8 lines · James Carter

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.

Quoted 43 lines

---
v3: Use __built_smul_overflow()

 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 -3
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)
--

2.54.0