Skip to content

wallet: detect cycles in BDB page traversal during migration - #34946

Closed
instagibbs wants to merge 1 commit into
bitcoin:masterfrom
instagibbs:2026-03-infinite_migrate
Closed

instagibbs wants to merge 1 commit into
bitcoin:masterfrom
instagibbs:2026-03-infinite_migrate

Conversation

@instagibbs

Copy link
Copy Markdown
Member

The read-only BDB parser follows page chains without tracking visited pages. A crafted BDB file with circular references causes an infinite loop with unbounded memory growth, crashing the process.

Add visited-page sets to both the BTree DFS traversal and overflow page chain traversal, throwing on the first revisited page.

Script to craft a .dat file which demonstrated the issue (should run and suck up memory until OOM): https://gist.github.com/instagibbs/f92c17c34bfefa4aa391cb2b9c6306de

Issue discovered/demo'd/fixed by Claude.

The read-only BDB parser follows page chains without tracking
visited pages. A crafted BDB file with circular references causes
an infinite loop with unbounded memory growth, crashing the process.

Add visited-page sets to both the BTree DFS traversal and overflow
page chain traversal, throwing on the first revisited page.
@DrahtBot

DrahtBot commented Mar 28, 2026 •

Copy link
Copy Markdown
Contributor

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

Reviews

See the guideline for information on the review process.
A summary of reviews will appear here.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #34959 (wallet: Enforce BDB btree levels and overflow item sizes by achow101)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

@fanquake fanquake closed this Apr 1, 2026
sedited added a commit that referenced this pull request Jul 4, 2026
b2de59d wallet, bdbro: Validate btree page levels (Ava Chow)
dc3a2b9 wallet, bdbro: Enforce overflow data lengths (Ava Chow)

Pull request description:

  Alternative to #34946

  BDB's overflow records include the total length of the data to be read from the overflow pages. If this length is impossible (larger than max page * page size), or if the data that we are reading exceeds the stated length, then throw an exception as this is an invalid BDB file. This prevents infinite looping if an overflow page makes a circular reference.

  BDB BTrees also include the level in the tree that the page is supposed to be at. Leaf pages are always at level 1. Starting from the root page, we can validate that each child has a level one less than the parent, until we reach a leaf page with a level of 1. This also ensures that we cannot have circular internal page references.

ACKs for top commit:
  sedited:
    ACK b2de59d
  rkrux:
    code review ACK b2de59d

Tree-SHA512: b740c349f68c78150ee57b77e8e81605bb45a7db4fbca3c3a0d87318178dfa197f37faf0cbfd8bef791ab20f4a9afc3d2571842960055a70b6c081d605f59f94
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants