mirror of
https://github.com/bitcoin/bitcoin
synced 2026-12-04 10:47:36 +00:00
validation: throw when a compressed script can't be decompressed #36280
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!36280
Loading…
Reference in a new issue
No description provided.
Delete branch "2026_compression_throw"
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?
ScriptCompression::Unserignores theDecompressScriptreturn value andleaves the script empty on failure, without notifying upper layers about the error.
In the worst-case scenario, which is currently only reachable through corruption,
an unspendable off-curve P2PK turns into an anyone-can-spend empty script.
Given that, properly throwing the error to prevent any future mistakes from
happening seems the sanest path to follow.
Aside from that, added coverage for a P2PK script with an uncompressed pubkey
that has a valid X but a wrong Y, which makes it unspendable. Checking we store
such scripts raw and never encode them as 0x04/0x05, as those encodings
serialize X with only Y's parity (not the full Y), which would make decompression
recompute Y from X and turn the unspendable script into a spendable one, which
would not be good.
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.
Approach ACK
Concept ACK
Looks already pretty solid but I left a few, mostly minor comments.
nit: sorting
Do we really need to check this 1000 times? If we do, then maybe this should rather be a fuzz test? For a unit test doing this once should suffice I think.
nit: maybe use
BOOST_REQUIREinstead ofassert?I guess this coverage wasn't that interesting but this is dropping the call of
DecompressScriptwith differentnSize.Would you mind covering each type here already? It doesn't seem to be that complex and otherwise the test is weaker than it needs to be.
nSizeistype(the compression format) and is still provided by the fuzzer and tested against the code. Diff is that we are providing it inside the data stream we feed toScriptCompressioninstead of a direct func arg.Happy to know if you are seeing something else.
Sorry, my comment wasn't that clear, what has changed is that the fuzzer doesn't cover
DecompressScriptwithnSize>5 anymore, but as I said, not that important coverage I guess.Base:
a773e1e33b349dc819b5a0edc1c7a4839dc73b48Head:
5500521f8bd93a42388bf8ef3c48d7c4c0ab75c6Findings
Minor
Abort rollback snapshot copying when a coin read fails (src/compressor.h:85, head)
During rollback snapshot creation, an undecodable compressed script makes
GetValuereturn false. The copy loop skips that coin and continues, then computes statistics and writes a snapshot from the incomplete temporary database without reporting the failed read. Abort the copy and report the read failure. No current production source of an undecodable compressed coin record was identified, so production impact remains unconfirmed and is limited to the corruption case described in the PR.Review coverage was incomplete.
Review limitations
Full review report
This does not sound like a valid use case for recoverable error handling to me. If this is a clear indication of corruption, it should be treated as such.
Edit: I see that all decoding errors are reported through exceptions, which is not great. But it means that the error handling in this PR is aligned. Hence, ACK.
ah, last one checked against is 6 (
nSpecialScripts), which check the raw script storage path (any number over 5 takes the same path).This was on purpose because once we go over the last encoding format, we start consuming bytes from the script, which could make<-- not true, I need caffeine.nSizeto underflow and end up with a huge allocation.Concept ACK, we have lots of similar findings that we should slowly plug, this seems like a good next step.
Could we also check how callers handle the new exception, especially where an unreadable coin could be silently skipped? Left a few suggestions for diagnostics and coverage too.
I’ll follow up separately on the
OP_RETURNfallback potentially leaving a spendable script.For diagnostics we could include the compression class and a hex dump of the payload as well.
Include the compression class and payload in decode errors
When
dumptxoutsetcreates a rollback snapshot, the copy loop skips coins on read failure. The cursor catches the new decompression exception and returns false, so this PRadds another trigger for that issue. Could we abort the copy on a failed read, either here or in a separate follow-up? Otherwise,
dumptxoutsetcan succeed with a coin missing.scantxoutsetuses the same cursor reader, but a failed read currently looks like cancellation (success: falsewith partial results). Could we return the UTXO read error there as well?The existing corruption test can cover both RPCs before triggering the validation abort.
Report unreadable coins in rollback exports and UTXO scans
Given the concern about exceptions being caught along the way (hinted at in the PR description and discussed in several related PRs and issues recently), could we add simple higher-level checks that
CoinandCTxUndodeserialization propagate this new failure?Check failure propagation through Coin and undo deserialization
For completeness we could round-trip 2/3 as well
Cover off-curve keys in compression classes 2 and 3
Seems to me the off-curve and wrong-Y raw-storage assertions are the same, so could we extract them into a helper?
Share P2PK raw-storage checks
nit: the script is the last field of a
Coin, so nothing follows it in a chainstate value.Undo records and snapshot files are where more data follows.
Correct where trailing data follows the script
nit: could we cap raw-payload consumption to leave more fuzz input for later checks? The fuzzer might already handle this, but I would expect it to have an easier time this
way...
Limit payload consumption while keeping varied bytes
nit: I think we can simplify this a bit
Use XOnlyPubKey as the decodability oracle
Indeed, special-scripts were already checked to be decodable and should re-serialize to the same bytes, so a round-trip comparison would check that the script hasn't changed.
Require special records to re-encode unchanged
tACK
5500521f8bDecompressScriptcan only fail for types 4 and 5, when libsecp256k1 rejects02/03 || x: x is not below p, or x³+7 is not a square. InsideUnserthe type is always below 6, so the finalreturn falseis unreachable and the new throw fires on exactly those inputs. I checked this against 4636 vectors from a library I maintain, cross-checked there with Euler's criterion: 2398 throw and 2238 decode to the expected script, as a bare script, inside aCoinand inside aCTxUndo, with the trailing byte left in the stream each time. The vectors include 200 x+p values, 101 of them reducing to a valid x; all throw, since libsecp256k1 requires x < p.On regtest, a snapshot whose first coin is rewritten as type 4 with x=5 (off the curve) or x=1+p fails
loadtxoutsetwith "Bad snapshot format or truncated snapshot after deserializing 0 coins", while x=1 still reaches the content hash check.compress_tests,coins_testsand thescriptfuzz target (3000 random inputs, non-libFuzzer build) pass. I measured on macOS at the PR head with a unit test reading the vectors and a functional test, which I can share as a gist.The throw also reaches the coins cursor, where a rollback
dumptxoutsetskips the unreadable coin andscantxoutsetreturns partial results as if cancelled. I did not test those paths, but l0rinc's diff for them would fit in this PR.