Skip to contentSkip to conversation

libsepol/fuzz: more strict fuzzing of binary policies

PATCH In review Christian Göttsche 2 participants 3 messages Jul 6, 2023 – Jul 12, 2023 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libsepol/fuzz: more strict fuzzing of binary policies

Validate policy after optimizing.

Run policy assertion check, ignoring any assertions.

Abort on failures writing the parsed policy, as writing should not fail on validated policies.

Set close-on-exec flag in case of any sibling thread.

---
 libsepol/fuzz/binpolicy-fuzzer.c | 21 ++++++++++++++++-----
 1 file changed, 16 insertions(+), 5 deletions(-)
libsepol/fuzz/binpolicy-fuzzer.c +16 -5
index 85c59645..79d42b0e 100644
@@ -3,6 +3,8 @@
 #include <sepol/kernel_to_conf.h>
 #include <sepol/policydb/policydb.h>
 
+extern int policydb_validate(sepol_handle_t *handle, const policydb_t *p);
+
 extern int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
 
 static int write_binary_policy(policydb_t *p, FILE *outfp)
@@ -38,18 +40,27 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
 	if (policydb_load_isids(&policydb, &sidtab))
 		goto exit;
 
-	if (policydb.policy_type == POLICY_KERN)
+	if (policydb.policy_type == POLICY_KERN) {
 		(void) policydb_optimize(&policydb);
 
-	devnull = fopen("/dev/null", "w");
+		if (policydb_validate(NULL, &policydb) == -1)
+			abort();
+	}
+
+	(void) check_assertions(NULL, &policydb, policydb.global->branch_list->avrules);
+
+	devnull = fopen("/dev/null", "we");
 	if (!devnull)
 		goto exit;
 
-	(void) write_binary_policy(&policydb, devnull);
+	if (write_binary_policy(&policydb, devnull))
+		abort();
 
-	(void) sepol_kernel_policydb_to_conf(devnull, &policydb);
+	if (sepol_kernel_policydb_to_conf(devnull, &policydb))
+		abort();
 
-	(void) sepol_kernel_policydb_to_cil(devnull, &policydb);
+	if (sepol_kernel_policydb_to_cil(devnull, &policydb))
+		abort();
 
 exit:
 	if (devnull != NULL)
Signature · 1 line
2.40.1
Acked-by on v1 1/1
#1 lore raw
Quoted 11 lines · Christian Göttsche

Validate policy after optimizing.

Run policy assertion check, ignoring any assertions.

Abort on failures writing the parsed policy, as writing should not fail on validated policies.

Set close-on-exec flag in case of any sibling thread.

Quoted 53 lines
---
 libsepol/fuzz/binpolicy-fuzzer.c | 21 ++++++++++++++++-----
 1 file changed, 16 insertions(+), 5 deletions(-)
libsepol/fuzz/binpolicy-fuzzer.c +16 -6
index 85c59645..79d42b0e 100644
@@ -3,6 +3,8 @@
 #include <sepol/kernel_to_conf.h>
 #include <sepol/policydb/policydb.h>
 
+extern int policydb_validate(sepol_handle_t *handle, const policydb_t *p);
+
 extern int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
 
 static int write_binary_policy(policydb_t *p, FILE *outfp)
@@ -38,18 +40,27 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
        if (policydb_load_isids(&policydb, &sidtab))
                goto exit;
 
-       if (policydb.policy_type == POLICY_KERN)
+       if (policydb.policy_type == POLICY_KERN) {
                (void) policydb_optimize(&policydb);
 
-       devnull = fopen("/dev/null", "w");
+               if (policydb_validate(NULL, &policydb) == -1)
+                       abort();
+       }
+
+       (void) check_assertions(NULL, &policydb, policydb.global->branch_list->avrules);
+
+       devnull = fopen("/dev/null", "we");
        if (!devnull)
                goto exit;
 
-       (void) write_binary_policy(&policydb, devnull);
+       if (write_binary_policy(&policydb, devnull))
+               abort();
 
-       (void) sepol_kernel_policydb_to_conf(devnull, &policydb);
+       if (sepol_kernel_policydb_to_conf(devnull, &policydb))
+               abort();
 
-       (void) sepol_kernel_policydb_to_cil(devnull, &policydb);
+       if (sepol_kernel_policydb_to_cil(devnull, &policydb))
+               abort();
 
 exit:
        if (devnull != NULL)
--

2.40.1

#2 lore raw
Quoted 8 lines · James Carter
Quoted 5 lines · Christian Göttsche

Validate policy after optimizing.

Run policy assertion check, ignoring any assertions.

Quoted 9 lines
Quoted 6 lines

Abort on failures writing the parsed policy, as writing should not fail on validated policies.

Set close-on-exec flag in case of any sibling thread.

Merged.
Thanks,
Jim

Quoted 53 lines
Quoted 53 lines
---
 libsepol/fuzz/binpolicy-fuzzer.c | 21 ++++++++++++++++-----
 1 file changed, 16 insertions(+), 5 deletions(-)
libsepol/fuzz/binpolicy-fuzzer.c +16 -6
index 85c59645..79d42b0e 100644
@@ -3,6 +3,8 @@
 #include <sepol/kernel_to_conf.h>
 #include <sepol/policydb/policydb.h>
 
+extern int policydb_validate(sepol_handle_t *handle, const policydb_t *p);
+
 extern int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
 
 static int write_binary_policy(policydb_t *p, FILE *outfp)
@@ -38,18 +40,27 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
        if (policydb_load_isids(&policydb, &sidtab))
                goto exit;
 
-       if (policydb.policy_type == POLICY_KERN)
+       if (policydb.policy_type == POLICY_KERN) {
                (void) policydb_optimize(&policydb);
 
-       devnull = fopen("/dev/null", "w");
+               if (policydb_validate(NULL, &policydb) == -1)
+                       abort();
+       }
+
+       (void) check_assertions(NULL, &policydb, policydb.global->branch_list->avrules);
+
+       devnull = fopen("/dev/null", "we");
        if (!devnull)
                goto exit;
 
-       (void) write_binary_policy(&policydb, devnull);
+       if (write_binary_policy(&policydb, devnull))
+               abort();
 
-       (void) sepol_kernel_policydb_to_conf(devnull, &policydb);
+       if (sepol_kernel_policydb_to_conf(devnull, &policydb))
+               abort();
 
-       (void) sepol_kernel_policydb_to_cil(devnull, &policydb);
+       if (sepol_kernel_policydb_to_cil(devnull, &policydb))
+               abort();
 
 exit:
        if (devnull != NULL)
--

2.40.1