Skip to content

[tree] In TBasket/TBranch, error out if reading oob in the streamer#22165

Open
silverweed wants to merge 2 commits into
root-project:masterfrom
silverweed:tbasket_read_oob
Open

[tree] In TBasket/TBranch, error out if reading oob in the streamer#22165
silverweed wants to merge 2 commits into
root-project:masterfrom
silverweed:tbasket_read_oob

Conversation

@silverweed
Copy link
Copy Markdown
Contributor

@silverweed silverweed commented May 6, 2026

TODO

  • add tests
  • verify if we can just ignore fNevBufSize and pass nullptr to ReadArray or if we should rather check that they match

Checklist:

  • tested changes locally
  • updated the docs (if necessary)

This PR fixes #22164

@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 6, 2026

Test Results

    22 files      22 suites   3d 9h 23m 44s ⏱️
 3 851 tests  3 800 ✅ 0 💤 51 ❌
76 052 runs  76 000 ✅ 0 💤 52 ❌

For more details on these failures, see this check.

Results for commit d34cef1.

♻️ This comment has been updated with latest results.

@silverweed silverweed marked this pull request as ready for review May 7, 2026 11:40
Comment thread tree/tree/src/TBranch.cxx Outdated
Comment thread tree/tree/src/TBasket.cxx Outdated
Comment thread tree/tree/src/TBasket.cxx
Comment on lines +1021 to +1022
// the buffer for us. This way we are sure that it will be of the correct size even if the file contains
// corrupted data.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I find that we can do better. With this change we swap the potential problem that the actual number of elements read is larger than the allocated memory (leading to out of bound writes) for the potential problem that the actual number of elements read is smaller than the expected number of elements (leading to out of bounds reads).

Maybe a better way is to explicitly read the size and to use ReadFastArray so that we can do the proper check.

Comment thread tree/tree/src/TBasket.cxx
if (fNevBuf > fNevBufSize) {
Error(
"Streamer",
"Inconsistent length of the entry buffer (%d events for a buffer size of %d). Refusing to deserialize.",
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
"Inconsistent length of the entry buffer (%d events for a buffer size of %d). Refusing to deserialize.",
"Inconsistent length for the entry offset buffer (%d events for a buffer size of %d). Refusing to deserialize.",

or

Suggested change
"Inconsistent length of the entry buffer (%d events for a buffer size of %d). Refusing to deserialize.",
"Entry offset buffer length is smaller than needed (%d events for a buffer size of %d). The basket is not readable.",

Comment thread tree/tree/src/TBasket.cxx
flag -= 80;
}
if (!mustGenerateOffsets && flag && (flag % 10 != 2)) {
if (fNevBuf > fNevBufSize) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Side note: fNevBuf is the number entries stored in the baskets while fNevBufSize is (in this context) the capacity of the fEntryOffset (and fDisplacement) arrays, so this check is correct.

We may want to add this information explicitly as code comments here.

Comment thread tree/tree/src/TBranch.cxx
b >> n;
if (n > fMaxBaskets) {
Error("Streamer",
"Inconsistent number of baskets: refusing to deserialize. Read %d for an expected maximum of %d.", n,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
"Inconsistent number of baskets: refusing to deserialize. Read %d for an expected maximum of %d.", n,
"Inconsistent number of baskets. This basket can not be read. Read %d for the actual number of baskets while we read %d as the value of fMaxBaskets.", n,

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.

[tree] Heap buffer overflow in TBasket::Streamer when reading TTree baskets with mismatched fNevBufSize and on-disk array length

2 participants