Computing hash of crafted block leads to crash in tree_hash()
A crafted block containing a very large number of transaction hashes causes tree_hash() to reserve a variable-length array on the stack and then zero it, overflowing the stack. An attacker could crash a node that parses and hashes such a block.
- Triaged in 2 days
- Fixed in 31 days
- Disclosed after 3 months
I'm not sure how to test this against against an actual Monero instance, so I'm instead showing an isolated PoC:
#include <cryptonote_basic/cryptonote_format_utils.h>
int main(void)
{
cryptonote::block b = AUTO_VAL_INIT(b);
for (size_t i = 0; i < 300000; i++) {
b.tx_hashes.push_back({});
}
std::ostringstream oss;
binary_archive<true> ba(oss);
std::string s;
if ( ::serialization::serialize(ba, b) == true ) {
s = oss.str();
} else {
return 0;
}
/* Uncomment to crash */
cryptonote::block b2 = AUTO_VAL_INIT(b2);
if ( parse_and_validate_block_from_blob(s, b2) == true ) {
/* Crash */
get_tx_tree_hash(b2);
}
return 0;
}
The reason this crashes is because of this code in tree_hash:
char ints[cnt][HASH_SIZE];
memset(ints, 0 , sizeof(ints)); // zero out as extra protection for using uninitialized mem
ints is allocated on the stack, not on the heap. Its size is dynamic; cnt (derived from the number of tx_hashes in this example) multiplied by 32 (HASH_SIZE) is the amount of bytes reserved on the stack.
On a typical, modern 64 bit OS, the stack is usually 8MB in size. Hence, a sufficient amount of tx_hashes will cause more stack to be reserved than is available.
Technically, the reservation of the stack space doesn't cause the crash (this only alters the stack pointer), but the subsequent memset does.
Note that the serialized size of a block with 300000 tx_hashes is about 9 MB (see s.size()), which is well within the limits of CRYPTONOTE_MAX_BLOCK_SIZE (500MB).
The best remediation to this issue is to use allocate memory on the heap, not the stack.
Impact
Crash nodes
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely a variable-length array sized from the number of transaction hashes is allocated on the stack and then zeroed, so a large count overflows the stack.
What the attacker needed
Ability to submit a crafted block; block size within the 500MB limit
The fix
It should be fixed by allocating the buffer on the heap instead of the stack.
How it was found
- Build a block with a very large number of transaction hashes.
- Serialise the block to a blob.
- Parse and validate the block from the blob.
- Compute the transaction tree hash of the parsed block, which crashes.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
A program keeps a small, fixed area of memory called the stack for short-lived data. Here the code puts a buffer whose size depends on attacker-controlled input on that stack, so a big enough input runs past the end of the stack and the program dies. The fix is to put that buffer in the heap, the larger general-purpose memory area.
What is DoS?
A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.