Skip to content

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

Open
rdmark wants to merge 3 commits intomainfrom
fix-bNetStr2Bstr
Open

fix bNetStr2Bstr: prevent OOB read on crafted netstring length#149
rdmark wants to merge 3 commits intomainfrom
fix-bNetStr2Bstr

Conversation

@rdmark
Copy link
Collaborator

@rdmark rdmark commented Mar 7, 2026

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.

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.
@rdmark rdmark requested a review from msteinert as a code owner March 7, 2026 13:37
@github-actions
Copy link

github-actions bot commented Mar 7, 2026

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 a7eab38

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 f348ec9 to a7eab38 Compare March 7, 2026 14:33
@sonarqubecloud
Copy link

sonarqubecloud bot commented Mar 7, 2026

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