Repository navigation
Validate k in DiskProver and input lengths in Verifier.validate_proof - #587
Closed
zohuyhieuzo03 wants to merge 1 commit into
Closed
zohuyhieuzo03 wants to merge 1 commit into
zohuyhieuzo03 wants to merge 1 commit into
Conversation
DiskProver read k from the plot file header and used it as a divisor (ByteAlign(k) / 8) without any range check, so a plot with k = 0 caused an integer division by zero (SIGFPE), which kills the process and cannot be caught as a C++ exception. Reject k outside [kMinPlotSize, kMaxPlotSize] with std::invalid_argument, the same range the plotter and the verifier already enforce. The Python Verifier.validate_proof binding did not check the length of seed or challenge, while the underlying code reads 32 bytes from each (F1Calculator memcpy of 31 bytes of the id). Raise ValueError unless both are exactly 32 bytes, matching get_qualities_for_challenge. Adds Python tests for both cases.
Coverage Report for CI Build 43140154616Coverage decreased (-0.01%) to 79.701%Details
Uncovered Changes
Coverage RegressionsNo coverage regressions found. Coverage Stats
💛 - Coveralls |
Member
|
Thanks for this hardening but this library is about to be archived and replaced in production with Chia Proof of Space 2.0 so we're unlikely to add this. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Two input-validation gaps in the Python-facing code, both ending in undefined behaviour or a process kill instead of a normal exception.
DiskProver(filename)andk.kis read from the plot file header (prover_disk.hpp,this->k = header.k) and then used as a divisor:c2_size = ByteAlign(k) / 8; c2_entries = (...) / c2_size. Withk = 0this is an integer division by zero. That raises SIGFPE, which kills the process and cannot be caught as a C++ exception, sotry/exceptin Python does not help. The constructor now rejectskoutside[kMinPlotSize, kMaxPlotSize]withstd::invalid_argument(surfaced asValueError), the same range the plotter (plotter_disk.hpp) andVerifier::ValidateProofalready enforce.Verifier.validate_proof(seed, k, challenge, proof). The binding does not check the length ofseedorchallenge, butF1Calculatorreads 31 bytes from the id (memcpy(enc_key + 1, orig_key, 31)) and the challenge is read as 32 bytes. A shorterbytesobject leads to an out-of-bounds heap read. It now raisesValueErrorunless both are exactly 32 bytes, matchingget_qualities_for_challenge, which already checks the challenge length. The Rust binding is unaffected (it takes&[u8; 32]).Behaviour change
validate_proofraisesValueErrorfor aseedorchallengethat is not 32 bytes (previously undefined behaviour). Callers that already pass 32-byte hashes are unaffected.DiskProverraisesValueErrorfor a plot file whose headerkis out of range (previously a crash fork = 0; other out-of-range values were not validated).Tests
Added to
tests/test_python_bindings.py:test_validate_proof_rejects_wrong_length_inputstest_disk_prover_rejects_k_out_of_range_in_header(creates a k=21 plot, patches the headerkbyte to 0, 1, 17, 51 and 255, expectsValueError)Run in Docker (python 3.12, built from this tree with
pip install ., run with--network none): without the source changes the newktest kills the interpreter withFatal Python error: Floating-point exception; with the changes3 passed(the two new tests and the existingtest_disk_prover_byte_conversions). I did not run the full existing suite, the C++ test binary or the long stress test.Disclosure
The issue was found with AI-assisted static analysis. I reviewed the code, wrote the change with AI assistance (Claude Code) and ran the tests above myself.
Note
Low Risk
Defensive input validation at Python/C++ boundaries; legitimate 32-byte seeds/challenges and valid plots behave unchanged.
Overview
Hardens Python bindings and plot loading against malformed inputs that previously caused undefined behavior or uncatchable crashes instead of
ValueError.Verifier.validate_proofnow rejectsseedorchallengeunless each is exactly 32 bytes, matching the length checks already used inget_qualities_for_challenge. Short buffers no longer provoke out-of-bounds reads in the native verifier path.DiskProverconstructor now validates headerkagainst[kMinPlotSize, kMaxPlotSize](18–50) before usingkas a divisor when parsing C2 table metadata—so corrupt or malicious plot headers (e.g.k == 0) raiseValueErrorinstead of triggering SIGFPE.Tests in
test_python_bindings.pycover wrong-length proof inputs and out-of-range headerkvalues.Reviewed by Cursor Bugbot for commit 8551716. Bugbot is set up for automated code reviews on this repo. Configure here.