Skip to contentSkip to conversation

FUZZ.md,{checkpolicy,libselinux}/fuzz: document selinux fuzzing

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

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Stephen Smalley

author maintainer
#0 lore raw

PATCH 1/1 FUZZ.md,{checkpolicy,libselinux}/fuzz: document selinux fuzzing

Document the use of OSS-Fuzz and CIFuzz by the selinux userspace project and provide some instructions and helpers for debugging fuzzing failures.

---
This is based on my experience thus far in handling OSS-Fuzz/CIFuzz; for those who may be more familiar with them, feel free to correct or suggest additions! The decode*.c programs are just copied from the fuzzers themselves and then tweaked to display additional info; I also found them useful for reproducing bugs in a manner more amenable to using gdb/valgrind than the oss-fuzz.sh-produced build.

 FUZZ.md                                       |  86 +++++
 checkpolicy/fuzz/decodecheckpolicyfuzz.c      |  41 +++
 .../fuzz/decodeselabelfilecompiledfuzz.c      | 335 ++++++++++++++++++
 libselinux/fuzz/decodeselabelfiletextfuzz.c   | 313 ++++++++++++++++
 4 files changed, 775 insertions(+)
 create mode 100644 FUZZ.md
 create mode 100644 checkpolicy/fuzz/decodecheckpolicyfuzz.c
 create mode 100644 libselinux/fuzz/decodeselabelfilecompiledfuzz.c
 create mode 100644 libselinux/fuzz/decodeselabelfiletextfuzz.c
FUZZ.md new file +86 -0
new file mode 100644
index 00000000..405234b9
@@ -0,0 +1,86 @@
+Fuzzing of the SELinux userspace
+================================
+
+OSS-Fuzz
+--------
+
+Portions of the SELinux userspace are regularly fuzzed by OSS-Fuzz based
+on the configuration at:
+https://github.com/google/oss-fuzz/blob/master/projects/selinux/project.yaml
+
+The fuzzers can be found under the fuzz subdirectories of various
+directories within the selinux repository, and can be built using the
+./scripts/oss-fuzz.sh script. OSS-Fuzz itself uses this script to
+build the fuzzers.
+
+See
+https://issues.oss-fuzz.com/issues?q=project:selinux
+for public OSS-Fuzz selinux issues. OSS-Fuzz automatically makes
+issues public once they are verified to be fixed or after a 90 day
+disclosure window. Issues are initially created private and
+sent via email to those listed in the configuration above.
+
+Not all issues will be fixed by the upstream SELinux project, e.g.
+small memory leaks on error paths for checkpolicy are not viewed as
+worth fixing since checkpolicy will exit shortly thereafter, and OSS
+Fuzz will create issues if it encounters a timeout during fuzzing even
+though the fuzzer would have completed if given more time.
+
+Items categorized as a vulnerability by OSS-Fuzz will not always be
+treated as security issues by the upstream SELinux project; in
+particular, issues that depend on attacker-controlled policy are not
+considered to be security issues and will just be handled as regular
+bugs. See
+[SECURITY.md](https://github.com/SELinuxProject/selinux/blob/main/SECURITY.md)
+for more information on the SELinux userspace security vulnerability
+handling process.
+
+CIFuzz
+------
+
+The selinux GitHub CI Actions include a CIFuzz workflow based on the
+configuration at:
+https://github.com/SELinuxProject/selinux/blob/main/.github/workflows/cifuzz.yml
+
+This action is invoked for pushes and pull requests targeting the main
+branch of the SELinuxProject/selinux repository to help detect
+regressions on new commits.
+
+The status of CIFuzz workflow runs can be viewed at:
+https://github.com/SELinuxProject/selinux/actions/workflows/cifuzz.yml
+as well as in the check status for each push or pull request and
+displayed in the banner at the top of the
+[README.md](https://github.com/SELinuxProject/selinux/blob/main/README.md)
+file.
+
+Debugging Fuzzing Failures
+--------------------------
+
+OSS-Fuzz provides a detailed report and, when possible, a reproducer
+testcase. If the cause cannot be determined from the report, the
+reproducer testcase can be downloaded locally, the fuzzer can be built
+using ./scripts/oss-fuzz.sh, and the fuzzer can be manually run on the
+reproducer testcase input.
+
+In some cases, it may be helpful to run one of the selinux userspace
+programs on the fuzzer input directly, potentially under gdb or
+valgrind, to better understand the issue and whether it is applicable
+to any production tool. This can be done when the fuzzer input is
+accepted directly as input to one of the selinux userspace programs,
+such as a CIL policy file to secilc or a binary policy file to
+checkpolicy -b. In other cases, the fuzzing input actually consists of
+a binary header and one or more concatenated files that must be
+decoded or split. Simple programs for decoding the fuzzer input can
+also be found under the fuzz/ subdirectories within the selinux
+repository, starting with the prefix "decode". These programs can be
+run on the reproducer testcase to extract and display information
+about the reproducer. To build these programs, one must compile the
+selinux userspace and the program with the
+-DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION flag to provide access to
+the necessary helper functions that would otherwise not be exposed in
+a production build, e.g.
+
+    make CFLAGS+='-DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION -g' clean all
+    gcc -g -o decodeselabelfiletextfuzz libselinux/fuzz/decodeselabelfiletextfuzz.c libselinux/src/libselinux.a /usr/lib64/libpcre2-8.a
+    valgrind ./decodeselabelfiletextfuzz clusterfuzz-testcase-minimized-selabel_file_text-fuzzer-5721256067137536
+
checkpolicy/fuzz/decodecheckpolicyfuzz.c new file +41 -0

41 lines not on this page · raw message

libselinux/fuzz/decodeselabelfilecompiledfuzz.c new file +335 -0

335 lines not on this page · raw message

libselinux/fuzz/decodeselabelfiletextfuzz.c new file +313 -0

313 lines not on this page · raw message

Signature · 1 line
2.55.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] Memory leak of the first `lookup_all()` result because the `result` pointer is overwritten…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] Memory leak of the first lookup_all() result because the result pointer is overwritten without being freed.
- [Medium] Missing argc validation before accessing argv[1] causes a segmentation fault when the utilities are invoked without arguments. --

commit 3407307096b8d3c23806a403bce9d2bd0c25cc80
Author: Stephen Smalley <[email protected]>

FUZZ.md,{checkpolicy,libselinux}/fuzz: document selinux fuzzing

This commit documents the use of OSS-Fuzz and CIFuzz for the selinux userspace project. It also provides helper programs adapted from the fuzzers to assist in debugging fuzzing failures using tools like gdb and valgrind.

checkpolicy/fuzz/decodecheckpolicyfuzz.c line 6

Quoted 12 lines
checkpolicy/fuzz/decodecheckpolicyfuzz.c new file +6 -0
new file mode 100644
index 0000000000000..99ab150c03833
@@ -0,0 +1,41 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+int main(int argc, char **argv)
+{
+	FILE *fp = fopen(argv[1], "rb");

[Severity: Medium]
Could this cause a segmentation fault if the utility is invoked without arguments? Since there is no check to ensure argc >= 2 before accessing argv[1], passing a NULL pointer to fopen() could crash the tool and degrade the developer experience.

Quoted 4 lines
checkpolicy/fuzz/decodecheckpolicyfuzz.c new file +4 -0
+	if (!fp) {
+		perror(argv[1]);
+		exit(1);
+	}

[ ... ]

Quoted 5 lines
libselinux/fuzz/decodeselabelfilecompiledfuzz.c new file +0 -0
new file mode 100644
index 0000000000000..b5abd83022377

[ ... ]

libselinux/fuzz/decodeselabelfilecompiledfuzz.c

Quoted 3 lines
libselinux/fuzz/decodeselabelfilecompiledfuzz.c new file +3 -0
+int main(int argc, char **argv)
+{
+	FILE *fp = fopen(argv[1], "rb");

[Severity: Medium]
Similar to the checkpolicy fuzz utility, could this access to argv[1] also crash if the user forgets to provide a file path argument?

Quoted 4 lines
libselinux/fuzz/decodeselabelfilecompiledfuzz.c new file +4 -0
+	if (!fp) {
+		perror(argv[1]);
+		exit(1);
+	}

[ ... ]

Quoted 5 lines
libselinux/fuzz/decodeselabelfiletextfuzz.c new file +0 -0
new file mode 100644
index 0000000000000..35f56761cf777

[ ... ]

Quoted 8 lines
libselinux/fuzz/decodeselabelfiletextfuzz.c new file +8 -0
+	errno = 0;
+	result = lookup_all(&rec, key, mode, partial, find_all, NULL);
+
+	if (!result)
+		assert(errno != 0);
+
+	for (const struct lookup_result *res = result; res; res = res->next) {
+		assert(res->regex_str);

[ ... ]

libselinux/fuzz/decodeselabelfiletextfuzz.c line 243

Quoted 10 lines
libselinux/fuzz/decodeselabelfiletextfuzz.c new file +10 -0
+		assert(res->lr->validated);
+		assert(res->prefix_len <= strlen(res->regex_str));
+	}
+
+	sort_specs(&sdata);
+
+	assert(cmp(&rec, &rec) == SELABEL_EQUAL);
+
+	errno = 0;
+	result = lookup_all(&rec, key, mode, partial, find_all, NULL);

[Severity: Medium]
Does this second assignment to the result pointer overwrite the previous list without freeing it first?

Since the commit message notes these tools are explicitly designed to be run with Valgrind for debugging, an internal memory leak like this might generate false positives in the Valgrind output, obscuring the actual fuzzing bugs being investigated. Should free_lookup_result() be called before this second lookup?

Quoted 3 lines
libselinux/fuzz/decodeselabelfiletextfuzz.c new file +3 -0
+
+	if (!result)
+		assert(errno != 0);

[ ... ]

libselinux/fuzz/decodeselabelfiletextfuzz.c

Quoted 3 lines
libselinux/fuzz/decodeselabelfiletextfuzz.c new file +3 -0
+int main(int argc, char **argv)
+{
+	FILE *fp = fopen(argv[1], "rb");

[Severity: Medium]
Is there a missing check for argc >= 2 here before accessing argv[1]? Invoking this utility without arguments will likely result in a crash when fopen() is called.

Quoted 4 lines
libselinux/fuzz/decodeselabelfiletextfuzz.c new file +4 -0
+	if (!fp) {
+		perror(argv[1]);
+		exit(1);
+	}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author maintainer
#2 lore raw
In reply to Stephen Smalley
Quoted 5 lines · Stephen Smalley

Document the use of OSS-Fuzz and CIFuzz by the selinux userspace project and provide some instructions and helpers for debugging fuzzing failures.

Quoted 8 lines

---
This is based on my experience thus far in handling OSS-Fuzz/CIFuzz; for those who may be more familiar with them, feel free to correct or suggest additions! The decode*.c programs are just copied from the fuzzers themselves and then tweaked to display additional info; I also found them useful for reproducing bugs in a manner more amenable to using gdb/valgrind than the oss-fuzz.sh-produced build.

I'm sure we can just fold the decode*.c contents into the existing fuzzer .c files with appropriate
conditional guards around the main() function or similar. Also should likely make them actually
split and write out the individual files rather than just the header info so you can have the .conf
file for the checkpolicy-fuzzer and the file_contexts file or file_contexts.bin file for the selabel
fuzzers.