Repository navigation
fix!: make verify_hash_tree check the entire hash tree - #410
Closed
diegonehab wants to merge 1 commit into
Closed
diegonehab wants to merge 1 commit into
diegonehab wants to merge 1 commit into
Conversation
verify_hash_tree only compared page hashes with memory, so corrupted inner nodes, sparse nodes, padding pages, dirty page trees, or page hash-tree cache entries went undetected while the root hash was wrong. It now updates the hash tree, checks every dense, sparse, and cached node against memory, checks that dirty nodes have dirty parents, raises an error naming the first inconsistent node, and returns the root hash. The C API now takes a cm_hash output instead of a bool.
diegonehab
force-pushed
the
feature/verify-merkle-tree
branch
from
October 16, 2026 13:07
aa359c6 to
5d07821
Compare
edubart
reviewed
Oct 16, 2026
| CM_API cm_error cm_verify_hash_tree(cm_machine *m, bool *result); | ||
| /// \details Fails if any node in the sparse tree, the dense trees, or the page hash-tree cache does not match the | ||
| /// machine state, or if any dirty page tree has a dirty node with a clean parent. | ||
| /// On success, every hash the machine reports matches the machine state. |
Member
There was a problem hiding this comment.
I would add a comment mentioning this operation should be used sporadically because unlikely other hash tree operations it is does not have multi-threaded acceleration.
Member
Author
|
I'm starting to think that, if you don't trust the machine, you should just clear the hash-tree. What do you think, Eduardo? Then we can leave this "verify" function to do what it was meant to do. Which is to see if we screwed up ourselves, not if somebody else screwed with us. |
Member
Author
|
I think this is not worth it. It's best to clear and start from scratch. |
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
verify_hash_treecompared each page hash with memory and nothing else. A machine could pass it with a corrupted dense inner node, sparse node, padding page, dirty page tree, or page hash-tree cache entry, andget_root_hashwould then return a wrong hash. Loading a stored machine from damaged.dht,.dpt,hash_tree.sht, orhash_tree.phtcfiles is one way to get there.verify_hash_treenow updates the hash tree and then checks all of it against memory. If it passes, every hash the machine reports matches the machine state.On the first mismatch, it raises an error naming the node, for example
dense hash tree node at address 0x80002000 with log2_size 13 (in RAM) does not match its children. On success, it returns the root hash, just likeget_root_hash.The error messages use a new
to_hex_stringhelper inalgorithm.hpp.Breaking changes
cm_verify_hash_tree(cm_machine *m, cm_hash *hash)returns the root hash instead of abool, and fails instead of reportingfalsemachine:verify_hash_tree()in Lua andmachine.verify_hash_treein JSON-RPC return the root hash and raise an error on mismatchTests
spec-hash-tree.luastores a machine, corrupts one structure on disk in a copy, loads the copy, and checks the error message. The cases cover the dense root, a dense inner node, a dense page node, a padding page node, a dirty node under a clean parent, the sparse root, and the page data, root hash, word hash, and borrowed flag of a cache entry. Two more cases check that an uncorrupted copy returns the stored root hash, and that a corrupted sparse leaf is repaired from its dense root. Existing callers in the Lua and C API tests now compare the returned hash withget_root_hash.