Skip to contentSkip to conversation

semodule_link: avoid NULL dereference on OOM

PATCH In review Christian Göttsche 2 participants 7 messages Nov 9, 2023 – Nov 16, 2023 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/5 semodule_link: avoid NULL dereference on OOM

In case the initial calloc(3) call fails the variable mods is still NULL while its size hint num_mods is set.

Reported by Clang Analyzer:

semodule_link.c:182:29: warning: Array access (from variable 'mods') results in a null pointer dereference [core.NullDereference]
182 | sepol_module_package_free(mods[i]);
| ^~~~~~~

Fixes 63e798a2034a ("semodule_link: update")
---
 semodule-utils/semodule_link/semodule_link.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
semodule-utils/semodule_link/semodule_link.c +5 -3
index 0f157bd9..58fca34d 100644
@@ -178,9 +178,11 @@ failure:
 	ret = EXIT_FAILURE;
 
 cleanup:
-	for (i = 0; i < num_mods; i++)
-		sepol_module_package_free(mods[i]);
-	free(mods);
+	if (mods) {
+		for (i = 0; i < num_mods; i++)
+			sepol_module_package_free(mods[i]);
+		free(mods);
+	}
 	sepol_module_package_free(base);
 
 	return ret;
Signature · 1 line
2.42.0
#1 lore raw

PATCH 2/5 libsepol: set number of target names

Set the number of target names to 0 instead of leaving it uninitialized. The number is always 0 since CIL does not support non-trivial not-self neverallow rules yet.

Reported by Clang Analyzer:

module_to_cil.c:1211:18: warning: The right operand of '<' is a garbage value [core.UndefinedBinaryOperatorResult]
1211 | for (t = 0; t < num_tnames; t++) {
| ^ ~~~~~~~~~~

---
 libsepol/src/module_to_cil.c | 2 ++
 1 file changed, 2 insertions(+)
libsepol/src/module_to_cil.c +2 -0
index d2868019..2d77e22a 100644
@@ -1199,6 +1199,8 @@ static int avrule_list_to_cil(int indent, struct policydb *pdb, struct avrule *a
 				rc = -1;
 				goto exit;
 			}
+
+			num_tnames = 0;
 		} else {
 			ts = &avrule->ttypes;
 			rc = process_typeset(pdb, ts, attr_list, &tnames, &num_tnames);
Signature · 1 line
2.42.0
#2 lore raw

PATCH 3/5 libselinux: fix memory leak in customizable_init()

In reply to Christian Göttsche

Reported by Clang Analyzer:

    is_customizable_type.c:36:3: warning: Potential leak of memory pointed to by 'buf' [unix.Malloc]
       36 |                 fclose(fp);
          |                 ^~~~~~
Fixes 9911f2ac6f77 ("libselinux: check for stream rewind failures")
---
 libselinux/src/is_customizable_type.c | 1 +
 1 file changed, 1 insertion(+)
libselinux/src/is_customizable_type.c +1 -0
index 9be50174..da301c60 100644
@@ -33,6 +33,7 @@ static void customizable_init(void)
 	}
 
 	if (fseek(fp, 0L, SEEK_SET) == -1) {
+		free(buf);
 		fclose(fp);
 		return;
 	}
Signature · 1 line
2.42.0
#3 lore raw

PATCH 4/5 libsepol: avoid leak in OOM branch

In reply to Christian Göttsche

In case the member sid_key failed to allocate, free the parent struct.

Reported by Clang Analyzer:

module_to_cil.c:2607:9: warning: Potential leak of memory pointed to by 'item' [unix.Malloc]
2607 | return rc;
| ^~

---
 libsepol/src/module_to_cil.c | 1 +
 1 file changed, 1 insertion(+)
libsepol/src/module_to_cil.c +1 -0
index 2d77e22a..ee22dbbd 100644
@@ -2582,6 +2582,7 @@ static int ocontext_isid_to_cil(struct policydb *pdb, const char *const *sid_to_
 		item->sid_key = strdup(sid);
 		if (!item->sid_key) {
 			ERR(NULL, "Out of memory");
+			free(item);
 			rc = -1;
 			goto exit;
 		}
Signature · 1 line
2.42.0
#4 lore raw

PATCH 5/5 libsepol: avoid memory corruption on realloc failure

In reply to Christian Göttsche

Use a single pointer variable for the realloc(3) result to not immediately override the source pointer.

Also don't unnecessarily copy the first character.

Reported by Clang Analyzer:

services.c:810:14: warning: Assigned value is garbage or undefined [core.uninitialized.Assign]
810 | **r_buf = **new_buf;
| ^ ~~~~~~~~~

---
 libsepol/src/services.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
libsepol/src/services.c +6 -6
index 51bd56a0..738a3404 100644
@@ -787,8 +787,8 @@ mls_ops:
 
 	if (r_buf && ((s[0] == 0) || ((s[0] == 1 &&
 				(flags & SHOW_GRANTED) == SHOW_GRANTED)))) {
-		int len, new_buf_len;
-		char *p, **new_buf = r_buf;
+		int len;
+		char *p;
 		/*
 		* These contain the constraint components that are added to the
 		* callers reason buffer.
@@ -801,13 +801,13 @@ mls_ops:
 				len = snprintf(p, reason_buf_len - reason_buf_used,
 						"%s", buffers[x]);
 				if (len < 0 || len >= reason_buf_len - reason_buf_used) {
-					new_buf_len = reason_buf_len + REASON_BUF_SIZE;
-					*new_buf = realloc(*r_buf, new_buf_len);
-					if (!*new_buf) {
+					int new_buf_len = reason_buf_len + REASON_BUF_SIZE;
+					char *new_buf = realloc(*r_buf, new_buf_len);
+					if (!new_buf) {
 						ERR(NULL, "failed to realloc reason buffer");
 						goto out1;
 					}
-					**r_buf = **new_buf;
+					*r_buf = new_buf;
 					reason_buf_len = new_buf_len;
 					continue;
 				} else {
Signature · 1 line
2.42.0
Acked-by on v1 1/5
#5 lore raw
In reply to Christian Göttsche
Quoted 6 lines · Christian Göttsche

In case the initial calloc(3) call fails the variable mods is still NULL while its size hint num_mods is set.

Reported by Clang Analyzer:

Quoted 7 lines

semodule_link.c:182:29: warning: Array access (from variable 'mods') results in a null pointer dereference [core.NullDereference]
182 | sepol_module_package_free(mods[i]);
| ^~~~~~~

Fixes 63e798a2034a ("semodule_link: update")

For these five patches:

Quoted 26 lines
---
 semodule-utils/semodule_link/semodule_link.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
semodule-utils/semodule_link/semodule_link.c +5 -4
index 0f157bd9..58fca34d 100644
@@ -178,9 +178,11 @@ failure:
        ret = EXIT_FAILURE;
 
 cleanup:
-       for (i = 0; i < num_mods; i++)
-               sepol_module_package_free(mods[i]);
-       free(mods);
+       if (mods) {
+               for (i = 0; i < num_mods; i++)
+                       sepol_module_package_free(mods[i]);
+               free(mods);
+       }
        sepol_module_package_free(base);
 
        return ret;
--

2.42.0

#6 lore raw
Quoted 13 lines · James Carter
Quoted 10 lines · Christian Göttsche

In case the initial calloc(3) call fails the variable mods is still NULL while its size hint num_mods is set.

Reported by Clang Analyzer:

semodule_link.c:182:29: warning: Array access (from variable 'mods') results in a null pointer dereference [core.NullDereference]
182 | sepol_module_package_free(mods[i]);
| ^~~~~~~

Quoted 7 lines
Quoted 3 lines
Fixes 63e798a2034a ("semodule_link: update")

For these five patches:

These five patches have been merged.
Thanks,
Jim

Quoted 26 lines
Quoted 26 lines
---
 semodule-utils/semodule_link/semodule_link.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
semodule-utils/semodule_link/semodule_link.c +5 -4
index 0f157bd9..58fca34d 100644
@@ -178,9 +178,11 @@ failure:
        ret = EXIT_FAILURE;
 
 cleanup:
-       for (i = 0; i < num_mods; i++)
-               sepol_module_package_free(mods[i]);
-       free(mods);
+       if (mods) {
+               for (i = 0; i < num_mods; i++)
+                       sepol_module_package_free(mods[i]);
+               free(mods);
+       }
        sepol_module_package_free(base);
 
        return ret;
--

2.42.0