Skip to content

Add unit tests for jsonw_pretty and fix buffer overflow vulnerabilities - #284

Open
gotaku3737-sketch wants to merge 13 commits into
libbpf:mainfrom
gotaku3737-sketch:main
Open

Add unit tests for jsonw_pretty and fix buffer overflow vulnerabilities#284
gotaku3737-sketch wants to merge 13 commits into
libbpf:mainfrom
gotaku3737-sketch:main

Conversation

@gotaku3737-sketch

Copy link
Copy Markdown

No description provided.

google-labs-jules Bot and others added 13 commits May 8, 2026 18:28
Implemented a new unit test suite in src/jsonw_test.c to verify the
functionality of the jsonw_pretty function in json_writer.c. The tests
cover:
- Default compact output (pretty-printing off)
- Formatted output (pretty-printing on)
- Toggling pretty-printing mid-stream

Updated src/Makefile to include a 'test' target for compiling and running
the unit tests, and updated the 'clean' target to remove the test binary.
The tests use open_memstream to capture and verify the JSON output.

Co-authored-by: gotaku3737-sketch <244136235+gotaku3737-sketch@users.noreply.github.com>
…-writer-6286810798765254243

🧪 [testing improvement] Add unit tests for jsonw_pretty
This commit replaces the manual memory allocation and unsafe use of
strcat with asprintf in do_prog.

Previously, a buffer was allocated with malloc(strlen(*argv) + 2) and
then filled using strcat. While the size was technically correct, using
strcat is generally discouraged for security and maintainability.

The new implementation uses asprintf, which is safer as it handles
allocation and formatting in a single step, reducing the risk of
buffer overflows or off-by-one errors during manual size calculation.

🎯 What: Potential buffer overflow in do_prog when appending a slash to
the program type.
⚠️ Risk: Manual buffer management and strcat can lead to vulnerabilities
if size calculations are incorrect or modified.
🛡️ Solution: Use asprintf to safely allocate and format the string.

Co-authored-by: gotaku3737-sketch <244136235+gotaku3737-sketch@users.noreply.github.com>
…rog-strcat-4091783086360426996

🔒 [security fix] Fix Buffer Overflow in do_prog via strcat
…g ops

Replaced unbounded string operations (`sprintf`, `strcpy`, `strcat`) with their safer, bounded equivalents (`snprintf`) across core source files (`src/prog.c`, `src/gen.c`, `src/feature.c`, `src/tracelog.c`, `src/kernel/bpf/disasm.c`, `src/main.c`). This implements a defense-in-depth approach to mitigate potential buffer overflows.

Co-authored-by: gotaku3737-sketch <244136235+gotaku3737-sketch@users.noreply.github.com>
…-fix-14531536299354862931

🛡️ Sentinel: [HIGH] Fix potential buffer overflows using bounded string operations
…parsing kallsyms

🚨 Severity: HIGH
💡 Vulnerability: The `sscanf` call in `src/xlated_dumper.c` used unbounded `%s` format specifiers to parse `/proc/kallsyms`.
🎯 Impact: An unusually long kernel symbol or module name could overflow the stack-allocated buffers `sym->name` and `sym->module`, leading to a buffer overflow and potential memory corruption.
🔧 Fix: Added explicit length limits to the `sscanf` format string (`%255s` and `%63[^]]`) matching the struct sizes defined in `xlated_dumper.h`.
✅ Verification: Ran `make -C src bpftool` and verified tests pass.

Co-authored-by: gotaku3737-sketch <244136235+gotaku3737-sketch@users.noreply.github.com>
…er-overflow-11300022512747804490

🛡️ Sentinel: [HIGH] Fix buffer overflow vulnerability in sscanf
This commit addresses a critical vulnerability in `src/jit_disasm.c` where
an out-of-bounds memory read and write could occur during string
manipulation.

🚨 Severity: CRITICAL
💡 Vulnerability: The loop responsible for stripping trailing spaces from
the formatted JSON string decremented the string index without checking
if it was within valid bounds (e.g., `< 0`).
🎯 Impact: If the string was empty or contained only spaces, the index
could reach `-1`, leading to memory corruption and undefined behavior.
🔧 Fix: Added a boundary check `i >= 0` to the while loop condition to
ensure the index does not underflow.
✅ Verification: Tested against empty strings and strings with only spaces,
and verified the build and json_writer tests pass.

Co-authored-by: gotaku3737-sketch <244136235+gotaku3737-sketch@users.noreply.github.com>
…603583010440029

🛡️ Sentinel: [CRITICAL] Fix out-of-bounds memory access in jit_disasm.c
…_dumper.c

Co-authored-by: gotaku3737-sketch <244136235+gotaku3737-sketch@users.noreply.github.com>
…ed-format-string-5652547563540882573

🛡️ Sentinel: [CRITICAL] Fix uninitialized VLA format string
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant