Skip to contentSkip to conversation

scripts/oss-fuzz: build PCRE2 from source with sanitizer instrumentation

PATCH In review Petr Matyas 2 participants 5 messages Aug 5 – Aug 10 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 scripts/oss-fuzz: build PCRE2 from source with sanitizer instrumentation

The selabel_file fuzzers link against the system libpcre2, which is not instrumented when building with -fsanitize=memory. MSan requires all libraries to be instrumented; uninstrumented libraries can produce false positives because their writes do not update the sanitizer's shadow memory.

In practice this causes a spurious use-of-uninitialized-value report:
pcre2_compile() writes literal character bytes via direct assignment (not through memset/memcpy), so MSan's shadow memory does not reflect those writes. pcre2_jit_compile() then calls memcmp() in detect_repeat() over compiled bytecode containing those character bytes, and MSan flags the comparison as reading uninitialized memory.

The PCRE2 maintainers confirmed this is a false positive per MSan documentation and declined to change the library [1].

Fix by building PCRE2 from source with the same -fsanitize= flags as the rest of the fuzzer build. With instrumentation, all pcre2_compile() writes are tracked, detect_repeat()'s memcmp() sees properly initialized bytes, and the false positive disappears. Both selabel_file fuzzers are now linked against the resulting static libpcre2-8.a from DESTDIR instead of the system shared library.

[1] https://github.com/KwisatzHaderach/pcre2/pull/1

---
 scripts/oss-fuzz.sh | 28 ++++++++++++++++++++++++++--
 1 file changed, 26 insertions(+), 2 deletions(-)
scripts/oss-fuzz.sh +26 -2
index 08e06975..57ac5c2d 100755
@@ -43,6 +43,30 @@ mkdir -p "$OUT"
 export LIB_FUZZING_ENGINE=${LIB_FUZZING_ENGINE:--fsanitize=fuzzer}
 
 rm -rf "$DESTDIR"
+
+## Build PCRE2 from source so it is instrumented with the same sanitizer
+## as the fuzzer targets.  Using the system libpcre2 (uninstrumented) causes
+## MSan to report use-of-uninitialized-value false positives: pcre2_compile()
+## writes character bytes via direct assignment that the sanitizer cannot
+## track in uninstrumented code, so pcre2_jit_compile()'s memcmp()-based
+## repeat-detection reads them as "uninitialized".  Building from source
+## with the same -fsanitize= flags ensures all writes are properly tracked.
+PCRE2_SRC=/tmp/pcre2-src
+if [ ! -d "$PCRE2_SRC" ]; then
+    git clone --depth 1 https://github.com/PCRE2Project/pcre2.git "$PCRE2_SRC"
+    git -C "$PCRE2_SRC" submodule update --init
+fi
+cmake -S "$PCRE2_SRC" -B "$PCRE2_SRC/build" \
+    -DCMAKE_C_COMPILER="$CC" \
+    -DCMAKE_C_FLAGS="$flags" \
+    -DPCRE2_SUPPORT_JIT=ON \
+    -DPCRE2_BUILD_TESTS=OFF \
+    -DBUILD_SHARED_LIBS=OFF \
+    -DCMAKE_INSTALL_PREFIX=/usr \
+    -DCMAKE_INSTALL_LIBDIR=lib
+cmake --build "$PCRE2_SRC/build" -j"$(nproc)"
+cmake --install "$PCRE2_SRC/build"
+
 make -C libsepol clean
 make -C libselinux clean
 # LIBSO and LIBMAP shouldn't be expanded here because their values are unknown until Makefile
@@ -106,7 +130,7 @@ cp checkpolicy/fuzz/checkpolicy-fuzzer.dict "$OUT/"
 # shellcheck disable=SC2086
 $CC $CFLAGS -DUSE_PCRE2 -DPCRE2_CODE_UNIT_WIDTH=8 -c -o selabel_file_text-fuzzer.o libselinux/fuzz/selabel_file_text-fuzzer.c
 # shellcheck disable=SC2086
-$CXX $CXXFLAGS $LIB_FUZZING_ENGINE selabel_file_text-fuzzer.o "$DESTDIR/usr/lib/libselinux.a" -lpcre2-8 -o "$OUT/selabel_file_text-fuzzer"
+$CXX $CXXFLAGS $LIB_FUZZING_ENGINE selabel_file_text-fuzzer.o "$DESTDIR/usr/lib/libselinux.a" "$DESTDIR/usr/lib/libpcre2-8.a" -o "$OUT/selabel_file_text-fuzzer"
 
 zip -j "$OUT/selabel_file_text-fuzzer_seed_corpus.zip" libselinux/fuzz/input
 
@@ -117,6 +141,6 @@ zip -j "$OUT/selabel_file_text-fuzzer_seed_corpus.zip" libselinux/fuzz/input
 # shellcheck disable=SC2086
 $CC $CFLAGS -DUSE_PCRE2 -DPCRE2_CODE_UNIT_WIDTH=8 -c -o selabel_file_compiled-fuzzer.o libselinux/fuzz/selabel_file_compiled-fuzzer.c
 # shellcheck disable=SC2086
-$CXX $CXXFLAGS $LIB_FUZZING_ENGINE selabel_file_compiled-fuzzer.o "$DESTDIR/usr/lib/libselinux.a" -lpcre2-8 -o "$OUT/selabel_file_compiled-fuzzer"
+$CXX $CXXFLAGS $LIB_FUZZING_ENGINE selabel_file_compiled-fuzzer.o "$DESTDIR/usr/lib/libselinux.a" "$DESTDIR/usr/lib/libpcre2-8.a" -o "$OUT/selabel_file_compiled-fuzzer"
 
 zip -j "$OUT/selabel_file_compiled-fuzzer_seed_corpus.zip" libselinux/fuzz/input
Signature · 1 line
2.55.0
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 23 lines · Petr Matyas

The selabel_file fuzzers link against the system libpcre2, which is not instrumented when building with -fsanitize=memory. MSan requires all libraries to be instrumented; uninstrumented libraries can produce false positives because their writes do not update the sanitizer's shadow memory.

In practice this causes a spurious use-of-uninitialized-value report:
pcre2_compile() writes literal character bytes via direct assignment (not through memset/memcpy), so MSan's shadow memory does not reflect those writes. pcre2_jit_compile() then calls memcmp() in detect_repeat() over compiled bytecode containing those character bytes, and MSan flags the comparison as reading uninitialized memory.

The PCRE2 maintainers confirmed this is a false positive per MSan documentation and declined to change the library [1].

Fix by building PCRE2 from source with the same -fsanitize= flags as the rest of the fuzzer build. With instrumentation, all pcre2_compile() writes are tracked, detect_repeat()'s memcmp() sees properly initialized bytes, and the false positive disappears. Both selabel_file fuzzers are now linked against the resulting static libpcre2-8.a from DESTDIR instead of the system shared library.

Quoted 3 lines

[1] https://github.com/KwisatzHaderach/pcre2/pull/1

maintainer
#2 lore raw
Quoted 25 lines · Stephen Smalley
Quoted 23 lines · Petr Matyas

The selabel_file fuzzers link against the system libpcre2, which is not instrumented when building with -fsanitize=memory. MSan requires all libraries to be instrumented; uninstrumented libraries can produce false positives because their writes do not update the sanitizer's shadow memory.

In practice this causes a spurious use-of-uninitialized-value report:
pcre2_compile() writes literal character bytes via direct assignment (not through memset/memcpy), so MSan's shadow memory does not reflect those writes. pcre2_jit_compile() then calls memcmp() in detect_repeat() over compiled bytecode containing those character bytes, and MSan flags the comparison as reading uninitialized memory.

The PCRE2 maintainers confirmed this is a false positive per MSan documentation and declined to change the library [1].

Fix by building PCRE2 from source with the same -fsanitize= flags as the rest of the fuzzer build. With instrumentation, all pcre2_compile() writes are tracked, detect_repeat()'s memcmp() sees properly initialized bytes, and the false positive disappears. Both selabel_file fuzzers are now linked against the resulting static libpcre2-8.a from DESTDIR instead of the system shared library.

Quoted 5 lines
Quoted 3 lines

[1] https://github.com/KwisatzHaderach/pcre2/pull/1

Merged.

maintainer
#3 lore raw
Quoted 28 lines · Stephen Smalley
Quoted 25 lines · Stephen Smalley
Quoted 23 lines · Petr Matyas

The selabel_file fuzzers link against the system libpcre2, which is not instrumented when building with -fsanitize=memory. MSan requires all libraries to be instrumented; uninstrumented libraries can produce false positives because their writes do not update the sanitizer's shadow memory.

In practice this causes a spurious use-of-uninitialized-value report:
pcre2_compile() writes literal character bytes via direct assignment (not through memset/memcpy), so MSan's shadow memory does not reflect those writes. pcre2_jit_compile() then calls memcmp() in detect_repeat() over compiled bytecode containing those character bytes, and MSan flags the comparison as reading uninitialized memory.

The PCRE2 maintainers confirmed this is a false positive per MSan documentation and declined to change the library [1].

Fix by building PCRE2 from source with the same -fsanitize= flags as the rest of the fuzzer build. With instrumentation, all pcre2_compile() writes are tracked, detect_repeat()'s memcmp() sees properly initialized bytes, and the false positive disappears. Both selabel_file fuzzers are now linked against the resulting static libpcre2-8.a from DESTDIR instead of the system shared library.

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

[1] https://github.com/KwisatzHaderach/pcre2/pull/1

Merged.

This resolved the issue with oss-fuzz. Thank you for investigating and fixing it!

maintainer
#4 lore raw
Quoted 33 lines · Stephen Smalley
Quoted 30 lines · Stephen Smalley
Quoted 27 lines · Stephen Smalley
Quoted 25 lines · Petr Matyas

The selabel_file fuzzers link against the system libpcre2, which is not instrumented when building with -fsanitize=memory. MSan requires all libraries to be instrumented; uninstrumented libraries can produce false positives because their writes do not update the sanitizer's shadow memory.

In practice this causes a spurious use-of-uninitialized-value report:
pcre2_compile() writes literal character bytes via direct assignment (not through memset/memcpy), so MSan's shadow memory does not reflect those writes. pcre2_jit_compile() then calls memcmp() in detect_repeat() over compiled bytecode containing those character bytes, and MSan flags the comparison as reading uninitialized memory.

The PCRE2 maintainers confirmed this is a false positive per MSan documentation and declined to change the library [1].

Fix by building PCRE2 from source with the same -fsanitize= flags as the rest of the fuzzer build. With instrumentation, all pcre2_compile() writes are tracked, detect_repeat()'s memcmp() sees properly initialized bytes, and the false positive disappears. Both selabel_file fuzzers are now linked against the resulting static libpcre2-8.a from DESTDIR instead of the system shared library.

[1] https://github.com/KwisatzHaderach/pcre2/pull/1

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

Merged.

This resolved the issue with oss-fuzz. Thank you for investigating and fixing it!

Got a new oss-fuzz report that doesn't reproduce if I revert your earlier prevent ReDOS patch,
so it seems to be tied to the use of the pcre2 JIT even though the stack trace isn't helpful.
Output below and reproducer attached.

+----------------------------------------Release Build
Stacktrace----------------------------------------+
Command: /mnt/scratch0/clusterfuzz/bot/builds/clusterfuzz-builds-honggfuzz_selinux_32ddf78d9f476617df7a8f4cf22b4d9340aef874/revisions/selabel_file_compiled-fuzzer Time ran: 0.02901911735534668
Accepting input from '[STDIN]'
Usage for fuzzing: honggfuzz -P [flags] -- /mnt/scratch0/clusterfuzz/bot/builds/clusterfuzz-builds-honggfuzz_selinux_32ddf78d9f476617df7a8f4cf22b4d9340aef874/revisions/selabel_file_compiled-fuzzer AddressSanitizer:DEADLYSIGNAL
=================================================================
==250==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000000
(pc 0x79a295c29113 bp 0x75a2940e2e20 sp 0x7ffe28467260 T0)
==250==The signal is caused by a READ memory access.
==250==Hint: address points to the zero page.

    #0 0x79a295c29113  (<unknown module>)

==250==Register values:
rax = 0x000075c2951e002f rbx = 0x000075c2951e0031 rcx =
0x000075c2951e002f rdx = 0x0000000000000000
rdi = 0x000075c2951e002f rsi = 0x0000000000000001 rbp =
0x000075a2940e2e20 rsp = 0x00007ffe28467260
r8 = 0x000079a295c290b2 r9 = 0x00007fffffffff01 r10 =
0x00007fffffffff01 r11 = 0x00000eced2a34001
r12 = 0x000075a2940e2e20 r13 = 0x0000000000989680 r14 =
0x000075a2948d0020 r15 = 0x000075a2948d8020
AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV (<unknown module>)
==250==ABORTING
Original Stacktrace on revision
64c49eb31cdfa4336b1c71e09b13b8b08435cc59 (26 lines)
[Environment] ASAN_OPTIONS=exitcode=77
+----------------------------------------Release Build
Stacktrace----------------------------------------+
Command: /mnt/scratch0/clusterfuzz/resources/platform/linux/unshare -c
-n /mnt/scratch0/clusterfuzz/bot/builds/clusterfuzz-builds_selinux_32ddf78d9f476617df7a8f4cf22b4d9340aef874/revisions/selabel_file_compiled-fuzzer -runs=100 '/mnt/scratch0/clusterfuzz/bot/inputs/fuzzer-testcases/cec16e5ee8803994da27df80ea97364cE.-6.ADDR.0.INSTR.[UNKNOWN].fuzz' Time ran: 0.040236473083496094
INFO: Running with entropic power schedule (0xFF, 100).
INFO: Seed: 3046068775
INFO: Loaded 1 modules (32680 inline 8-bit counters): 32680
[0x5ae1f2ea5fe0, 0x5ae1f2eadf88),
INFO: Loaded 1 PC tables (32680 PCs): 32680 [0x5ae1f2eadf88,0x5ae1f2f2da08), /mnt/scratch0/clusterfuzz/bot/builds/clusterfuzz-builds_selinux_32ddf78d9f476617df7a8f4cf22b4d9340aef874/revisions/selabel_file_compiled-fuzzer:
Running 1 inputs 100 time(s) each.
Running: /mnt/scratch0/clusterfuzz/bot/inputs/fuzzer-testcases/cec16e5ee8803994da27df80ea97364cE.-6.ADDR.0.INSTR.[UNKNOWN].fuzz AddressSanitizer:DEADLYSIGNAL
=================================================================
==1036==ERROR: AddressSanitizer: SEGV on unknown address
0x000000000000 (pc 0x7e85a7a65680 bp 0x7a85a5cf1220 sp 0x7ffcef4417a0
T0)
==1036==The signal is caused by a READ memory access.
==1036==Hint: address points to the zero page.

    #0 0x7e85a7a65680  (<unknown module>)

==1036==Register values:
rax = 0x0000000000000000 rbx = 0x00007aa5a6de0091 rcx = 0x0000000000000000 rdx = 0x00007e85a7a66108
rdi = 0x00007aa5a6de008f rsi = 0x0000000000000001 rbp = 0x00007a85a5cf1220 rsp = 0x00007ffcef4417a0
r8 = 0x00007e85a7a65456 r9 = 0x00005ae1f2e4f7e0 r10 =
0x00007fffffffff01 r11 = 0x00000f6b34db4001
r12 = 0x00007a85a5cf1220 r13 = 0x000000000098967d r14 = 0x00007a85a64d0020 r15 = 0x00007a85a64d7fe0 AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV (<unknown module>) ==1036==ABORTING