Skip to content

Fix out-of-bounds reads in DHT bencode parser - #4

Open
defnax wants to merge 2 commits into
RetroShare:masterfrom
defnax:bencode-fix
Open

defnax wants to merge 2 commits into
RetroShare:masterfrom
defnax:bencode-fix

Conversation

@defnax

@defnax defnax commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

Replace unbounded strtoll calls with length-bounded integer parsing and reject overflow and missing terminators.

Check remaining packet length before reading list and dictionary terminators, reject malformed strings, and safely clean up partial containers.

Add guard-page regression tests for truncated packets and integer limits.

code by: GPT 6 Astra light

Replace unbounded strtoll calls with length-bounded integer parsing
and reject overflow and missing terminators.

Check remaining packet length before reading list and dictionary
terminators, reject malformed strings, and safely clean up partial
containers.

Add guard-page regression tests for truncated packets and integer
limits.

@csoler csoler left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this fix is inappropriate: the proposed code re-implements parts of strtoll, ignoring some documented rules (arbitrary number of spaces before the digits, possible "+" sign, etc) which makes it misleading. Besides it should test the terminating 'e' character which when missing is a clue that something's wrong.

Instead of re-implementing strtoll with these additional checks, I suggest you still rely on strtoll with additional checks since the bug report is right in saying that it's possible to craft a packet without a proper terminating character (missing 'e' basically):

0 - call ret=strtoll() as originally done
1 - if endp==*data return false (detects empty numbers) See "man strtol"
2 - check errno (detects overflow/underflow)
3 - if ok, check that *endp == 'e' (this is specific to the encoding of ints in BitDHT). This has to be true no matter what. See bencode.c:458
4 - return ret.

Copy numeric fields into NUL-terminated buffers before calling strtoll.
Reject missing digits, overflow, trailing junk, and missing delimiters
while preserving whitespace and optional sign handling.

Extend regression tests for parsing compatibility and packet bounds.
@csoler

csoler commented Oct 11, 2026

Copy link
Copy Markdown
Contributor

Please don't copy-paste what I'm saying to AI and copy-back the result. It's insulting for the person who's reviewing.

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.

2 participants