Self-Audit: anti_double_mining.py (#7458) — Deep security audit with Claude#3191
Open
BossChaos wants to merge 1 commit intoScottcjn:mainfrom
Open
Self-Audit: anti_double_mining.py (#7458) — Deep security audit with Claude#3191BossChaos wants to merge 1 commit intoScottcjn:mainfrom
BossChaos wants to merge 1 commit intoScottcjn:mainfrom
Conversation
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.
Security Audit Report: RustChain Anti-Double-Mining
Repository: RustChain Blockchain Bounty Program
File:
node/anti_double_mining.py(1035 lines)Auditor: BossChaos
Wallet: RTC6d1f27d28961279f1034d9561c2403697eb55602
Executive Summary
Combined audit of 1035-line anti-double-mining protection implementation.
RustChain Anti-Double-Mining Security Audit
Critical Vulnerabilities Found: 7 (2 CRITICAL, 3 HIGH, 2 MEDIUM)
CRITICAL-1: Race Condition in Enrollment Fallback Allows Double-Reward Theft
Severity: CRITICAL
CVSS v3.1: 9.1 (CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:H/A:H)
Lines: 148-149, 310-311, 359-362
Function:
detect_duplicate_identities(),get_epoch_miner_groups(),calculate_anti_double_mining_rewards()Attack Vector
Concurrent miners exploit the fallback path in
miner_attest_recent(time-window query) while enrollment settlement races. An attacker mines two blocks in the same epoch from different identities, then during settlement, the enrollment record for one miner hasn't been committed yet, causing both to be detected as separate machines (via fallback) rather than the same identity.PoC Attack Flow
epoch_enrollfor Miner B is delayed due to fork raceminer_attest_recentmisses Miner B entirelyRemediation Code
CRITICAL-2: Fingerprint Hash Truncation Enables Identity Collision Attack
Severity: CRITICAL
CVSS v3.1: 8.6 (CVSS:3.1/AV:L/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H)
Lines: 47-67
Function:
compute_machine_identity_hash()Attack Vector
The identity hash uses only 16 hex characters from SHA-256 (
hexdigest()[:16]). This reduces entropy from 256 bits to 64 bits, making collision attacks computationally feasible. An attacker can craft fingerprint profiles that hash to the same 16-char prefix, causing legitimate miners to be incorrectly flagged as duplicates.Collision Calculation
Remediation Code
HIGH-1: TOCTOU Vulnerability in Reward Assignment Enables Reward Amplification
Severity: HIGH
CVSS v3.1: 7.5 (CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:H/A:N)
Lines: 400-438
Function:
calculate_anti_double_mining_rewards()Attack Vector
Time-of-check to time-of-use vulnerability: duplicate detection (line 403) and reward assignment (lines 400-438) are not atomic. An attacker can:
Remediation Code
HIGH-2: Silent JSON Parse Failure Enables Bypass via Malformed Fingerprint
Severity: HIGH
CVSS v3.1: 7.1 (CVSS:3.1/AV:N/AC:L/PR:L/UI:N/S:U/C:N/I:H/A:N)
Lines: 181-182, 342-344
Functions:
detect_duplicate_identities(),get_epoch_miner_groups()Attack Vector
JSON parsing errors are silently caught and ignored, causing
fingerprint_profileto remain empty. An attacker can submit malformedprofile_jsonto bypass identity grouping:Impact
profile_json = "INVALID_JSON{for Miner BRemediation Code
HIGH-3: No Attestation Freshness Check Enables Stale-Replay Attack
Severity: HIGH
CVSS v3.1: 6.8 (CVSS:3.1/AV:N/AC:L/PR:H/UI:N/S:U/C:N/I:H/A:N)
Lines: 152-182, 290-356
Functions:
detect_duplicate_identities(),get_epoch_miner_groups()Attack Vector
The code fetches the "most recent" fingerprint profile without validating it's within the current epoch. An attacker with a legitimate attestation from epoch N-1 can:
Remediation Code
MEDIUM-1: Epoch-Based Hash Collision Window Enables Cross-Epoch Replay
Severity: MEDIUM
CVSS v3.1: 5.3 (CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:L/A:N)
Lines: 66
Function:
compute_machine_identity_hash()Attack Vector
With the 16-char truncation, once an attacker finds a colliding fingerprint profile, it works across ALL epochs unless the hash function changes. The system has no mechanism to invalidate known-bad identity hashes.
Remediation Code
MEDIUM-2: Deterministic Tie-Breaker Predictability Enables Gaming
Severity: MEDIUM
CVSS v3.1: 4.6 (CVSS:3.1/AV:N/AC:L/PR:L/UI:N/S:U/C:N/I:L/A:N)
Lines: 244-282
Function:
select_representative_miner()Attack Vector
When multiple miners have identical entropy scores and timestamps, the alphabetical tie-breaker is predictable. An attacker can choose miner IDs strategically to win tie-breaking selection:
Impact
Remediation Code
Summary Table
Overall Security Assessment
System Status: NOT PRODUCTION-READY
The anti-double-mining protection has fundamental flaws enabling:
Immediate Actions Required:
Auditor: BossChaos | Wallet: RTC6d1f27d28961279f1034d9561c2403697eb55602
Security Audit Report:
node/anti_double_mining.py(Section 518-1035)Target: RustChain Blockchain Anti-Double-Mining Module
Auditor: BossChaos | Wallet: RTC6d1f27d28961279f1034d9561c2403697eb55602
CRITICAL Vulnerabilities
C-01: Race Condition - TOCTOU in Epoch Settlement
CVSS v3.1:
CVSS:3.1/AV:N/AC:H/PR:N/UI:N/S:U/C:N/I:H/A:H| Score: 7.5settle_epoch_with_anti_double_mining()Vulnerability:
Check and set of
settledflag are not atomic. Concurrent callers can both pass the check before either commits.Attack Vector: Two nodes simultaneously call
settle_epoch_with_anti_double_mining()for the same epoch. Both pass thealready_settledcheck and proceed to credit rewards—double payment.Remediation:
C-02: Unvalidated Warthog Bonus Multiplier
CVSS v3.1:
CVSS:3.1/AV:N/AC:L/PR:H/UI:N/S:C/C:H/I:H/A:H| Score: 9.1_calculate_anti_double_mining_rewards_conn(), reward calculation loopVulnerability:
The
warthog_bonusis read directly fromminer_attest_recentwithout validation, range checking, or upper bound.Attack Vector: Compromised/malicious node operator sets
warthog_bonus = 1000000.0for their miner, exponentially inflating their weight and capturing disproportionate rewards.Remediation:
C-03: Unvalidated Fingerprint Passed Flag
CVSS v3.1:
CVSS:3.1/AV:N/AC:L/PR:H/UI:N/S:C/C:H/I:H/A:H| Score: 8.8_calculate_anti_double_mining_rewards_conn()Vulnerability:
The
fingerprint_passedcolumn is trusted without verification against actual fingerprint history.Attack Vector: Attacker with compromised attestation system marks fake miners as
fingerprint_passed=1, bypassing fingerprint validation entirely.Remediation:
HIGH Vulnerabilities
H-01: Missing Authorization on Balance Updates
CVSS v3.1:
CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H| Score: 9.8settle_epoch_with_anti_double_mining()Vulnerability:
No cryptographic signature verification that the caller is authorized to distribute rewards. Any node can credit arbitrary balances.
Attack Vector: Off-chain attacker with network access calls the function to credit themselves unlimited tokens.
Remediation:
H-02: Integer Division Precision Loss in Reward Distribution
CVSS v3.1:
CVSS:3.1/AV:N/AC:H/PR:N/UI:N/S:U/C:N/I:H/A:L| Score: 5.9_calculate_anti_double_mining_rewards_conn()Vulnerability:
Truncation via
int()and cumulativeremaining -= sharecan cause:remainingbecoming negative if float arithmetic produces share > actual remainingAttack Vector: Malicious entity manipulates floating-point edge cases to siphon dust amounts from each epoch.
Remediation:
H-03: No Slot/Epoch Existence Verification
CVSS v3.1:
CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:N| **Score: 8.6`_calculate_anti_double_mining_rewards_conn()Vulnerability:
Epoch parameters are used without verifying they exist in the canonical chain.
Attack Vector: Attacker calls settlement for future or non-existent epochs, potentially front-running legitimate settlements.
Remediation:
H-04: SQLite BEGIN IMMEDIATE Insufficient Isolation
CVSS v3.1:
CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H| **Score: 9.1`settle_epoch_with_anti_double_mining()Vulnerability:
When using
existing_conn, the caller owns the transaction. If_calculate_anti_double_mining_rewards_connreads data that changes beforesettle_epoch_with_anti_double_miningwrites, rewards may be calculated on stale data.Attack Vector: Concurrent settlement of adjacent epochs can cause reward calculation on data from wrong epoch.
Remediation:
MEDIUM Vulnerabilities
M-01: JSON Fingerprint Hash Collision Potential
CVSS v3.1:
CVSS:3.1/AV:N/AC:H/PR:H/UI:N/S:U/C:N/I:H/A:L| Score: 5.3fingerprint_a,fingerprint_bVulnerability: JSON fingerprints compared as strings. Different JSON representations (whitespace, key ordering) of same fingerprint may bypass duplicate detection.
Attack Vector: Sophisticated attacker generates syntactically different but semantically identical fingerprints to evade detection.
Remediation:
M-02: Missing Input Validation on Parameters
CVSS v3.1:
CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:N/A:H| Score: 5.8settle_epoch_with_anti_double_mining()Vulnerability:
No validation that
epoch >= 0,per_epoch_urtc > 0,current_slot > 0.Attack Vector: Negative values or zero could cause division by zero in weight calculations.
Remediation:
M-03: Silent Exception Swallowing
CVSS v3.1:
CVSS:3.1/AV:N/AC:H/PR:N/UI:N/S:U/C:N/I:N/A:L| Score: 3.7_calculate_anti_double_mining_rewards_conn()Vulnerability:
Attack Vector: Anomalies in warthog bonus queries are hidden, potentially masking manipulation or data corruption.
Remediation:
M-04: Hardcoded Genesis Timestamp Dependency
CVSS v3.1:
CVSS:3.1/AV:N/AC:H/PR:N/UI:N/S:U/C:N/I:N/A:L| Score: 3.1_calculate_anti_double_mining_rewards_conn()Vulnerability: Relies on module-level
GENESIS_TIMESTAMPwithout verification against on-chain state.Attack Vector: Chain hard-fork could invalidate timestamp calculations, causing reward calculation failures.
LOW Vulnerabilities
L-01: Test Data Cleanup in Production Path
CVSS v3.1:
CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:L/A:L| Score: 2.5__main__Vulnerability:
os.remove(test_db)cleanup in production code path.Summary Table
Recommended Action: Prioritize C-01 through H-02 fixes before mainnet deployment.