Skip to content

univalue: respect token end pointer - #35281

Open
ferminquant wants to merge 1 commit into
bitcoin:masterfrom
ferminquant:fix-univalue-token-bounds
Open

ferminquant wants to merge 1 commit into
bitcoin:masterfrom
ferminquant:fix-univalue-token-bounds

Conversation

@ferminquant

@ferminquant ferminquant commented May 13, 2026 •

Copy link
Copy Markdown

Fixes #28260.

Avoid reading past the supplied end pointer in two places:

  • matching JSON keywords with strncmp;
  • validating number prefixes with firstDigit[1].

Also update the UniValue unit test and parse_univalue fuzz target to exercise bounded input.

@DrahtBot

DrahtBot commented May 13, 2026 •

Copy link
Copy Markdown
Contributor

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

External sites

Reviews

See the guideline and AI policy for information on the review process.

Type Reviewers
Concept ACK sedited
Stale ACK carloantinarella

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

Conflicts

No conflicts as of last run.

@ferminquant
ferminquant force-pushed the fix-univalue-token-bounds branch 2 times, most recently from 50c6a17 to e9a2bbe Compare May 13, 2026 15:29
Comment thread src/test/fuzz/parse_univalue.cpp Resolved
@DrahtBot

Copy link
Copy Markdown
Contributor

🚧 At least one of the CI tasks failed.
Task iwyu: https://github.com/bitcoin/bitcoin/actions/runs/25806359727/job/75815150297
LLM reason (✨ experimental): CI failed because the IWYU (include-what-you-use) check flagged missing/wrong includes and intentionally returned non-zero exit status (“Failure generated from IWYU”).

Hints

Try to run the tests locally, according to the documentation. However, a CI failure may still
happen due to a number of reasons, for example:

  • Possibly due to a silent merge conflict (the changes in this pull request being
    incompatible with the current code in the target branch). If so, make sure to rebase on the latest
    commit of the target branch.

  • A sanitizer issue, which can only be found by compiling with the sanitizer and running the
    affected test.

  • An intermittent issue.

Leave a comment here, if you need help tracking down a confusing failure.

@ferminquant
ferminquant force-pushed the fix-univalue-token-bounds branch from e9a2bbe to db49f97 Compare May 14, 2026 01:14
@sedited
sedited requested a review from maflcko June 8, 2026 12:33
Comment thread src/univalue/test/unitester.cpp Resolved
@carloantinarella

Copy link
Copy Markdown
Contributor

tACK 28f609c

Thanks for the added tests.
One comment: my suggestion is to squash the two commits into a single one, as they do not seem to represent different logical steps.

@sedited sedited 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.

Concept ACK

I think most of these unit tests are redundant however. Also not sure we should be introducing a new utility for this. I think the only cases actually adding a bit of coverage is the leading -01 case. Maybe that should just be covered by some more test fixture files?

EDIT:

It would also be good to mention that this is fixing two separate out of bounds reads.

Comment thread src/univalue/lib/univalue_read.cpp Resolved
Comment thread src/univalue/test/unitester.cpp Resolved
Comment thread src/univalue/test/unitester.cpp Resolved
Comment thread src/univalue/test/unitester.cpp Resolved
@sedited

sedited commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

@ferminquant can you respond to the review here? It's been a month.

@ferminquant

ferminquant commented Aug 6, 2026 •

Copy link
Copy Markdown
Author

Thanks for the review. Done in commit 7f633b3:

  • Reduced the unit coverage to the minimal bounded-end regressions and removed the extra helper.
  • Updated the std::string_view construction.
  • Updated the PR description to mention the two independent out-of-bounds reads.
  • Squashed the two commits.

The focused UniValue test builds and passes locally.

@ferminquant
ferminquant force-pushed the fix-univalue-token-bounds branch from 28f609c to 7f633b3 Compare August 6, 2026 21:36
Comment thread src/univalue/test/unitester.cpp Resolved
Comment thread src/univalue/test/unitester.cpp Resolved
Comment thread src/univalue/test/unitester.cpp Resolved
@ferminquant
ferminquant force-pushed the fix-univalue-token-bounds branch from 7f633b3 to d107f95 Compare August 7, 2026 15:08
Comment thread src/univalue/test/unitester.cpp Resolved
@ferminquant

Copy link
Copy Markdown
Author

Thanks, fixed in d107f95:

  • Kept fixture inputs as std::string_view without copying.
  • Removed no_nul_test().
  • Changed bounded_input_test() to use correctly sized, non-null-terminated buffers.

The focused UniValue test passes locally.

@ferminquant
ferminquant force-pushed the fix-univalue-token-bounds branch from d107f95 to e40cccf Compare August 7, 2026 15:43
Comment thread src/univalue/test/unitester.cpp Resolved
@sedited

sedited commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

@ferminquant this has been in need of a rebase for a while. Can you do so?

Avoid reading past the supplied end pointer when matching JSON keywords and validating number prefixes.

Also update the UniValue test and parse_univalue fuzz target to exercise bounded input.
@ferminquant
ferminquant force-pushed the fix-univalue-token-bounds branch from b9a54af to 045b0ef Compare October 2, 2026 17:30
@ferminquant

Copy link
Copy Markdown
Author

@sedited Done, sorry for the delay.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

getJsonToken assumes underlying string is null-terminated but requires end pointer

5 participants