Skip to content

fix bNetStr2Bstr: prevent OOB read on crafted netstring length#149

Merged
rdmark merged 3 commits into
mainfrom
fix-bNetStr2Bstr
Mar 17, 2026
Merged

fix bNetStr2Bstr: prevent OOB read on crafted netstring length#149
rdmark merged 3 commits into
mainfrom
fix-bNetStr2Bstr

Conversation

@rdmark

@rdmark rdmark commented Mar 7, 2026

Copy link
Copy Markdown
Collaborator

The declared length field is parsed into x with an overflow guard against INT_MAX, but there was no check that i + 1 + x falls within the actual buffer before accessing buff[i + 1 + x] to verify the terminating comma.

A crafted input such as "99999999:." causes the parser to set x = 99999999 and then read buff[100000008], triggering a SEGV. Found by libFuzzer + ASan.
Fix: compute blen = strlen(buff) on entry and return NULL if (size_t)i + 1 + (size_t)x >= blen before the terminator check.

The bug was detected by libFuzzer fuzz testing.

@rdmark
rdmark requested a review from msteinert as a code owner March 7, 2026 13:37
@github-actions

github-actions Bot commented Mar 7, 2026

Copy link
Copy Markdown

File Coverage Lines Branches
All files 69% 74% 63%
bstring/bstraux.c 55% 64% 47%
bstring/bstrlib.c 74% 78% 71%
bstring/buniutil.c 80% 86% 73%
bstring/utf8util.c 60% 71% 50%

Minimum allowed coverage is 50%

Generated by 🐒 cobertura-action against 78cf64b

rdmark added 3 commits March 17, 2026 22:20
The declared length field is parsed into `x` with an overflow guard
against INT_MAX, but there was no check that `i + 1 + x` falls within
the actual buffer before accessing `buff[i + 1 + x]` to verify the
terminating comma.

A crafted input such as "99999999:.<data>" causes the parser to set
x = 99999999 and then read `buff[100000008]`, triggering a SEGV.
Found by libFuzzer + ASan.
Fix: compute `blen = strlen(buff)` on entry and return NULL if
`(size_t)i + 1 + (size_t)x >= blen` before the terminator check.

The bug was detected by libFuzzer fuzz testing.
Replace strlen with memchr(buff, '\0', i + 2 + x), limiting the scan
to exactly the bytes a valid netstring of the claimed length requires.
If a null byte appears before the expected comma position the buffer
is truncated and NULL is returned. memchr is C89 standard, replacing
the non-portable strnlen.
@rdmark
rdmark force-pushed the fix-bNetStr2Bstr branch from a7eab38 to 78cf64b Compare March 17, 2026 21:24
@sonarqubecloud

Copy link
Copy Markdown

@rdmark
rdmark merged commit 5274f72 into main Mar 17, 2026
19 checks passed
@rdmark
rdmark deleted the fix-bNetStr2Bstr branch March 17, 2026 21:32
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