Skip to content

Validate k in DiskProver and input lengths in Verifier.validate_proof - #587

Closed
zohuyhieuzo03 wants to merge 1 commit into
Chia-Network:mainfrom
zohuyhieuzo03:validate-plot-k-and-verifier-input-lengths
Closed

zohuyhieuzo03 wants to merge 1 commit into
Chia-Network:mainfrom
zohuyhieuzo03:validate-plot-k-and-verifier-input-lengths

Conversation

@zohuyhieuzo03

@zohuyhieuzo03 zohuyhieuzo03 commented Nov 12, 2026 •

Copy link
Copy Markdown

Summary

Two input-validation gaps in the Python-facing code, both ending in undefined behaviour or a process kill instead of a normal exception.

  1. DiskProver(filename) and k. k is 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. With k = 0 this is an integer division by zero. That raises SIGFPE, which kills the process and cannot be caught as a C++ exception, so try/except in Python does not help. The constructor now rejects k outside [kMinPlotSize, kMaxPlotSize] with std::invalid_argument (surfaced as ValueError), the same range the plotter (plotter_disk.hpp) and Verifier::ValidateProof already enforce.
  2. Verifier.validate_proof(seed, k, challenge, proof). The binding does not check the length of seed or challenge, but F1Calculator reads 31 bytes from the id (memcpy(enc_key + 1, orig_key, 31)) and the challenge is read as 32 bytes. A shorter bytes object leads to an out-of-bounds heap read. It now raises ValueError unless both are exactly 32 bytes, matching get_qualities_for_challenge, which already checks the challenge length. The Rust binding is unaffected (it takes &[u8; 32]).

Behaviour change

validate_proof raises ValueError for a seed or challenge that is not 32 bytes (previously undefined behaviour). Callers that already pass 32-byte hashes are unaffected. DiskProver raises ValueError for a plot file whose header k is out of range (previously a crash for k = 0; other out-of-range values were not validated).

Tests

Added to tests/test_python_bindings.py:

  • test_validate_proof_rejects_wrong_length_inputs
  • test_disk_prover_rejects_k_out_of_range_in_header (creates a k=21 plot, patches the header k byte to 0, 1, 17, 51 and 255, expects ValueError)

Run in Docker (python 3.12, built from this tree with pip install ., run with --network none): without the source changes the new k test kills the interpreter with Fatal Python error: Floating-point exception; with the changes 3 passed (the two new tests and the existing test_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_proof now rejects seed or challenge unless each is exactly 32 bytes, matching the length checks already used in get_qualities_for_challenge. Short buffers no longer provoke out-of-bounds reads in the native verifier path.

DiskProver constructor now validates header k against [kMinPlotSize, kMaxPlotSize] (18–50) before using k as a divisor when parsing C2 table metadata—so corrupt or malicious plot headers (e.g. k == 0) raise ValueError instead of triggering SIGFPE.

Tests in test_python_bindings.py cover wrong-length proof inputs and out-of-range header k values.

Reviewed by Cursor Bugbot for commit 8551716. Bugbot is set up for automated code reviews on this repo. Configure here.

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.
@coveralls-official

Copy link
Copy Markdown

Coverage Report for CI Build 43140154616

Coverage decreased (-0.01%) to 79.701%

Details

  • Coverage decreased (-0.01%) from the base build.
  • Patch coverage: 1 uncovered change across 1 file (1 of 2 lines covered, 50.0%).
  • No coverage regressions found.

Uncovered Changes

File Changed Covered %
src/prover_disk.hpp 2 1 50.0%

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 4488
Covered Lines: 3577
Line Coverage: 79.7%
Coverage Strength: 14660754.12 hits per line

💛 - Coveralls

@hoffmang9

Copy link
Copy Markdown
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.

@hoffmang9 hoffmang9 closed this Nov 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants