mirror of
https://github.com/bitcoin/bitcoin
synced 2026-12-04 16:26:37 +00:00
script: prevent partial reinitialization of sighash data #35662
No reviewers
Labels
No labels
Android
Backport
Block storage
Brainstorming
Bug
Build system
CI failed
Consensus
Data corruption
Descriptors
Docs
DrahtBot Guix build requested
Feature
Fuzzing
GUI
Good First Review
IPC
Insufficient Review
Kernel
Linux/Unix
Mempool
Mining
Needs Backport (31.x)
Needs Backport (32.x)
Needs Benchmark
Needs Conceptual Review
Needs backport (30.x)
Needs rebase
Needs release note
Net
P2P
PSBT
Privacy
Private Broadcast
Questions and Help
RPC/REST/ZMQ
Refactoring
Resource usage
Review club
Scripts and tools
Settings
TX fees and policy
Tests
Tracking Issue
UTXO Db and Indexes
Up for grabs
Upstream
Utils/log/libs
Validation
Waiting for author
Wallet
Windows
good first issue
interfaces
macOS
No milestone
No project
No assignees
2 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
bitcoin/bitcoin!35662
Loading…
Reference in a new issue
No description provided.
Delete branch "l0rinc/script-reset-precomputed-txdata"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Problem:
PrecomputedTransactionDatacould be default-constructed and filled later withInit().That allowed an object to exist without transaction-specific data and left a public reuse path that was easy to get wrong.
The old
Init()guard only checkedm_spent_outputs_ready, which remained false after initialization without spent outputs even when the BIP143 prevout, sequence, and output hashes were already cached.No current caller reuses an object across transactions, so validation and signing results are unchanged.
btck_script_pubkey_verify()in the experimental kernel C API also copied caller-supplied txdata on each verification call.Fix: Remove default construction and
Init()so eachPrecomputedTransactionDataobject is built for one transaction.Callers that already have the transaction and optional spent outputs construct it directly.
Validation stores txdata in
std::optionaland emplaces it only after a script-cache miss, keeping precomputation lazy.ConnectBlock()also leaves txdata storage empty when assumevalid skips script checks, covering #35663.The kernel verification path binds directly to caller-supplied txdata and constructs a local fallback only when needed.
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.
If your review is incorrectly listed, please copy-paste
<!--meta-tag:bot-skip-->into the comment that the bot should ignore.Conflicts
Reviewers, this pull request conflicts with the following ones:
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.
LLM Linter (✨ experimental)
Possible places where named args for integral literals may be used (e.g.
func(x, /*named_arg=*/0)in C++, andfunc(x, named_arg=0)in Python):src/test/script_assets_tests.cpp:CachingTransactionSignatureChecker txcheck(&tx, idx, prevouts[idx].nValue, true, signature_cache, txdata)src/test/script_assets_tests.cpp:CachingTransactionSignatureChecker txcheck(&tx, idx, prevouts[idx].nValue, true, signature_cache, txdata)2026-11-26 19:34:58
I'm tending NACK here. Afaict this is just a hardening improvement, but I'm not sure if it really is one. We should never be hitting a code path where a re-initialization is possible, and this seems to relax that requirement. I think rather than messing with
Init, we should seriously reconsider whether this data can be initialized at construction.I agree that resetting readiness flags inside
Init()still leaves a reuse API around and makes the type responsible for defending against misuse after construction (I was hoping someone would object).I’ll rework this so that
PrecomputedTransactionDatais always built for one transaction and has no public reinitialization path:std::optional<PrecomputedTransactionData>andemplace()once all required data has been gathered.This also seems like a natural place to fold in #35663: validation already needs optional txdata storage, so the assumevalid path can leave the per-block txdata vector empty when script checks are skipped, while still resizing it before any queued checks can take pointers into it.
Pushed:
PrecomputedTransactionDatais no longer default-constructible, publicInit()is gone, and each object is built for one transaction.ConnectBlock()also leaves txdata storage empty when script checks are skipped during assumevalid, so that path avoids the unused vector allocation.This is indeed a lot nicer, thanks for the push. What do you think?
Rebased after recent merges, ready for review again. I understood @theuni has a similar attempt before, if it's published somewhere, please let me know and I'll add him as a coauthor.
ACK on the changes
I find the title a bit confusing. Which staleness are we exactly preventing with this change?
Why not remove it entirely? The diff from doing so is small.
Updated the stack to remove the public
Init()helper entirely and added a leading kernel API fix that avoids copying caller-supplied txdata on each verification call (had it in my backlog for a while, I could also push it separately if reviewers want).@theuni, this touches the txdata lifetime area involved in the script-check issue you found , would you mind reviewing the approach and letting me know if it has your blessing?
Nice, I thought that should be a follow-up, but it was actually trivial. I removed
Init()entirely and moved its body into the constructor - beautiful!Earlier concerns neither republished nor resolved: 1 suggestion. See the Run archive linked from the Review report for their assessments.
🔵 Suggestion · Cache-hit tests do not cover lazy
txdataconstructionThe migrated cache-hit test retains a pre-existing coverage gap: it would still pass if
CheckInputScriptsconstructedtxdatabefore checking the script cache.Location:
src/test/txvalidationcache_tests.cpp:119(head).Review coverage was incomplete.
Reviewed:
241f55dbecc8· Review report · Previous reportspermalink
tACK
35a8964f88On which staleness this prevents: on the base (
7988380da9),Init(A, {})for a transaction with a witness, thenInit(B, spent_outputs)for one without, passes the oldassert(!m_spent_outputs_ready), since A was given no spent outputs. The object keeps A's BIP143 hashes, so B's BIP143 sighash comes out different from a fresh object's. I found no caller that initializes twice, so this is not reachable today; with this PR such code no longer compiles.I compared 5961 BIP143 and BIP341 key path sighashes from a library I maintain with
SignatureHashandSignatureHashSchnorr, on base and head, with txdata built with and without spent outputs and with and withoutforce. All agree, and Taproot without spent outputs is refused on both. On head the unit tests pass (155 ctest entries,script_assets_testsskipped for lack of data), and so doestest_kernel.The kernel commit stops
btck_script_pubkey_verifyfrom copying the caller's txdata on every call. I measured base against head with a static Release libbitcoinkernel on an Apple M5, verifying a trivial script with precomputed txdata for a 10000-input transaction. One call takes about 100 µs on base and 0.17 µs on head, and the base cost grows with the input count. Verifying all 10000 inputs once drops from about 1 s to under 2 ms. I can share the harnesses in a gist.🐙 This pull request conflicts with the target branch and needs rebase.
Simple rebase after https://github.com/bitcoin/bitcoin/pull/29843
🐙 This pull request conflicts with the target branch and needs rebase.