Skip to contentSkip to conversation

libsepol: reject policies with no avtab entries in expand_module()

PATCH Superseded Stephen Smalley 2 participants 3 messages Aug 4 SELinux security module

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Stephen Smalley

author maintainer
#0 lore raw

PATCH 1/1 libsepol: reject policies with no avtab entries in expand_module()

avtab_read() already rejects policies that have no avtab entries in both libsepol and the kernel, but expand_module() can produce a policydb that has none. Check and reject such policies in expand_module() so that checkpolicy (and its fuzzer) will fail rather than writing out an invalid binary policy.

Reported-by oss-fuzz (issue 541553475)
---
 libsepol/src/expand.c | 5 +++++
 1 file changed, 5 insertions(+)
libsepol/src/expand.c +5 -0
index 8cb62d82..466d5bc1 100644
@@ -3494,6 +3494,11 @@ int expand_module(sepol_handle_t *handle, policydb_t *base, policydb_t *out,
 		goto cleanup;
 	}
 
+	if (out->te_avtab.nel == 0) {
+		ERR(handle, "avtab must contain at least one entry");
+		goto cleanup;
+	}
+
 	/* copy constraints */
 	if (hashtab_map(state.base->p_classes.table, constraint_copy_callback,
 			&state)) {
Signature · 1 line
2.55.0
author maintainer
#1 lore raw
Quoted 9 lines · Stephen Smalley

avtab_read() already rejects policies that have no avtab entries in both libsepol and the kernel, but expand_module() can produce a policydb that has none. Check and reject such policies in expand_module() so that checkpolicy (and its fuzzer) will fail rather than writing out an invalid binary policy.

Reported-by oss-fuzz (issue 541553475)

This breaks test-expander in libsepol; apparently some of the test policies there lack any avrules.
Should I fix the test policies, or is this change incorrect? Also seems to run afoul of sanitizers due to memory leak on failed expand_modules().

Quoted 23 lines
---
 libsepol/src/expand.c | 5 +++++
 1 file changed, 5 insertions(+)
libsepol/src/expand.c +5 -1
index 8cb62d82..466d5bc1 100644
@@ -3494,6 +3494,11 @@ int expand_module(sepol_handle_t *handle, policydb_t *base, policydb_t *out,
                goto cleanup;
        }
 
+       if (out->te_avtab.nel == 0) {
+               ERR(handle, "avtab must contain at least one entry");
+               goto cleanup;
+       }
+
        /* copy constraints */
        if (hashtab_map(state.base->p_classes.table, constraint_copy_callback,
                        &state)) {
--

2.55.0

#2 lore raw
Quoted 10 lines · Stephen Smalley
Quoted 7 lines · Stephen Smalley

avtab_read() already rejects policies that have no avtab entries in both libsepol and the kernel, but expand_module() can produce a policydb that has none. Check and reject such policies in expand_module() so that checkpolicy (and its fuzzer) will fail rather than writing out an invalid binary policy.

Quoted 6 lines
Quoted 2 lines
Reported-by oss-fuzz (issue 541553475)

This breaks test-expander in libsepol; apparently some of the test policies there lack any avrules.
Should I fix the test policies, or is this change incorrect?

In cil_binary.c there is a check that the te_avtab has at least one rule and an error is returned if there is not.
In libsepol, avtab_read() also gives an error for an empty table.

I think the test policies are wrong.
Jim

Quoted 26 lines

Also seems to run afoul of sanitizers due to memory leak on failed expand_modules().

Quoted 23 lines
---
 libsepol/src/expand.c | 5 +++++
 1 file changed, 5 insertions(+)
libsepol/src/expand.c +5 -1
index 8cb62d82..466d5bc1 100644
@@ -3494,6 +3494,11 @@ int expand_module(sepol_handle_t *handle, policydb_t *base, policydb_t *out,
                goto cleanup;
        }
 
+       if (out->te_avtab.nel == 0) {
+               ERR(handle, "avtab must contain at least one entry");
+               goto cleanup;
+       }
+
        /* copy constraints */
        if (hashtab_map(state.base->p_classes.table, constraint_copy_callback,
                        &state)) {
--

2.55.0