Skip to contentSkip to conversation

libsepol: Add overflow checks when linking file and netfilter contexts

PATCH In review James Carter 2 participants 5 messages Jul 24 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libsepol: Add overflow checks when linking file and netfilter contexts

Use __builtin_add_overflow() when calculating the total length of all the file and netfilter contexts.

---
 libsepol/src/module.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
libsepol/src/module.c +4 -2
index 5fcf5870..8aaa693d 100644
@@ -249,7 +249,8 @@ static int link_file_contexts(sepol_module_package_t *base,
 
 	fc_len = base->file_contexts_len;
 	for (i = 0; i < num_modules; i++) {
-		fc_len += modules[i]->file_contexts_len;
+		if (__builtin_add_overflow(fc_len, modules[i]->file_contexts_len, &fc_len))
+			return -1;
 	}
 
 	if ((s = (char *)realloc(base->file_contexts, fc_len)) == NULL) {
@@ -281,7 +282,8 @@ static int link_netfilter_contexts(sepol_module_package_t *base,
 
 	base_nc_len = base->netfilter_contexts_len;
 	for (i = 0; i < num_modules; i++) {
-		base_nc_len += modules[i]->netfilter_contexts_len;
+		if (__builtin_add_overflow(base_nc_len, modules[i]->netfilter_contexts_len, &base_nc_len))
+			return -1;
 	}
 
 	if ((base_context = (char *)realloc(base->netfilter_contexts,
Signature · 1 line
2.55.0
#1 lore raw

PATCH 1/1 libsepol: Use size_t for counting bytes in module.c

Use size_t for variables used to count bytes in the functions read_helper() and write_helper().

---
 libsepol/src/module.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
libsepol/src/module.c +2 -2
index 5fcf5870..fdf05f12 100644
@@ -345,7 +345,7 @@ int sepol_link_packages(sepol_handle_t *handle, sepol_module_package_t *base,
 #define _read_helper_bufsize BUFSIZ
 static int read_helper(char *buf, struct policy_file *file, uint32_t bytes)
 {
-	uint32_t offset, nel, read_len;
+	size_t offset, nel, read_len;
 	int rc;
 
 	offset = 0;
@@ -842,7 +842,7 @@ cleanup:
 
 static int write_helper(char *data, size_t len, struct policy_file *file)
 {
-	int idx = 0;
+	size_t idx = 0;
 	size_t len2;
 
 	while (len) {
Signature · 1 line
2.55.0
maintainer Acked-by on v1 1/1
#2 lore raw

Re: [PATCH] libsepol: Use size_t for counting bytes in module.c

Quoted 5 lines · James Carter

Use size_t for variables used to count bytes in the functions read_helper() and write_helper().

Quoted 29 lines
---
 libsepol/src/module.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
libsepol/src/module.c +2 -3
index 5fcf5870..fdf05f12 100644
@@ -345,7 +345,7 @@ int sepol_link_packages(sepol_handle_t *handle, sepol_module_package_t *base,
 #define _read_helper_bufsize BUFSIZ
 static int read_helper(char *buf, struct policy_file *file, uint32_t bytes)
 {
-       uint32_t offset, nel, read_len;
+       size_t offset, nel, read_len;
        int rc;
 
        offset = 0;
@@ -842,7 +842,7 @@ cleanup:
 
 static int write_helper(char *data, size_t len, struct policy_file *file)
 {
-       int idx = 0;
+       size_t idx = 0;
        size_t len2;
 
        while (len) {
--

2.55.0

maintainer
#3 lore raw

Re: [PATCH] libsepol: Use size_t for counting bytes in module.c

Quoted 9 lines · Stephen Smalley
Quoted 5 lines · James Carter

Use size_t for variables used to count bytes in the functions read_helper() and write_helper().

Merged up through this one.

Quoted 30 lines
Quoted 30 lines
---
 libsepol/src/module.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
libsepol/src/module.c +2 -3
index 5fcf5870..fdf05f12 100644
@@ -345,7 +345,7 @@ int sepol_link_packages(sepol_handle_t *handle, sepol_module_package_t *base,
 #define _read_helper_bufsize BUFSIZ
 static int read_helper(char *buf, struct policy_file *file, uint32_t bytes)
 {
-       uint32_t offset, nel, read_len;
+       size_t offset, nel, read_len;
        int rc;
 
        offset = 0;
@@ -842,7 +842,7 @@ cleanup:
 
 static int write_helper(char *data, size_t len, struct policy_file *file)
 {
-       int idx = 0;
+       size_t idx = 0;
        size_t len2;
 
        while (len) {
--

2.55.0

maintainer Acked-by on v1 1/1
#4 lore raw
In reply to James Carter
Quoted 5 lines · James Carter

Use __builtin_add_overflow() when calculating the total length of all the file and netfilter contexts.

Quoted 31 lines
---
 libsepol/src/module.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
libsepol/src/module.c +4 -3
index 5fcf5870..8aaa693d 100644
@@ -249,7 +249,8 @@ static int link_file_contexts(sepol_module_package_t *base,
 
        fc_len = base->file_contexts_len;
        for (i = 0; i < num_modules; i++) {
-               fc_len += modules[i]->file_contexts_len;
+               if (__builtin_add_overflow(fc_len, modules[i]->file_contexts_len, &fc_len))
+                       return -1;
        }
 
        if ((s = (char *)realloc(base->file_contexts, fc_len)) == NULL) {
@@ -281,7 +282,8 @@ static int link_netfilter_contexts(sepol_module_package_t *base,
 
        base_nc_len = base->netfilter_contexts_len;
        for (i = 0; i < num_modules; i++) {
-               base_nc_len += modules[i]->netfilter_contexts_len;
+               if (__builtin_add_overflow(base_nc_len, modules[i]->netfilter_contexts_len, &base_nc_len))
+                       return -1;
        }
 
        if ((base_context = (char *)realloc(base->netfilter_contexts,
--

2.55.0