Out-of-bounds read when importing corrupt blockchain with monero-blockchain-import
When importing a corrupt blockchain file with verification disabled, an attacker-controlled block package can cause an out-of-bounds read in the database layer. The attacker could potentially trigger memory corruption, though the report does not confirm exploitability.
- Fixed in 6 days
- Disclosed after 6 months
It is possible to trigger an out-of-bounds read in monero-blockchain-import when importing a corrupt blockchain and not verifying blocks and transitions during import (--verify 0).
Using a corrupt import_file, the attacker has full control over buffer_block in import_from_file (blockchain_import.cpp). As shown in the following lines of code (blockchain_import.cpp:404-407), this also enables the attacker to craft a corrupt bootstrap::block_package bp at will:
str1.assign(buffer_block, chunk_size);
bootstrap::block_package bp;
if (! ::serialization::parse_binary(str1, bp))
throw std::runtime_error("Error in deserialization of chunk");
If verification is turned off (opt_verify = false), the following line of code (blockchain_import.cpp:484) is executed, where all arguments in the function call are extracted from the corrupt bp, hence, controlled by the attacker:
core.get_blockchain_storage().get_db().add_block(b, block_size, cumulative_difficulty, coins_generated, txs);
The executed function BlockchainLMDB::add_block then executes the following line of code (db_lmdb.cpp:2850), which passes the same (corrupt) arguments to the function BlockchainDB::add_block, where the actual memory corruption finally happens:
BlockchainDB::add_block(blk, block_size, cumulative_difficulty, coins_generated, txs);
In BlockchainDB::add_block, there is unfortunately no sanity check about the passed arguments and the following lines are executed (blockchain_db:210-217):
int tx_i = 0;
crypto::hash tx_hash = null_hash;
for (const transaction& tx : txs)
{
tx_hash = blk.tx_hashes[tx_i]; // here the out-of-bounds read happens
add_transaction(blk_hash, tx, &tx_hash);
++tx_i;
}
As txs as well as blk originate from the bootstrap::block_package bp generated in blockchain_import, they can be set to arbitrary values by the attacker. In particular, if bp is crafted such that bp.txs.size() > bp.block.tx_hashes.size(), then an out-of-bounds memory corruption happens in the for loop when accessing blk.tx_hashes.
I have not yet examined whether the bug can be exploited in any malicious way, but I think it needs to be fixed anyways. Further, as the bug happens in the database outside of blockchain_import.cpp, it may also affect other code in monero, not only monero-blockchain-import. I have also not checked that. The bug can be easily fixed by introducing additional sanity checks, such as, whether bp.txs.size() != bp.block.tx_hashes.size().
I'll be happy to answer any further questions regarding the bug. Thank you!
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 the lack of a sanity check ensuring that the number of transactions matches the number of transaction hashes before iterating over them.
Impact
The out-of-bounds read could lead to memory corruption, but the report does not confirm whether it can be exploited maliciously; it may also affect other code in monero.
What the attacker needed
Ability to supply a corrupt import file; verification disabled (--verify 0)
The fix
The bug should be fixed by adding sanity checks, such as verifying that the number of transactions equals the number of transaction hashes.
How it was found
- Craft a corrupt import file where the block package has more transactions than transaction hashes.
- Run monero-blockchain-import with verification disabled (--verify 0) on the corrupt file.
- The import code deserialises the block package and passes it to the database layer.
- The database layer iterates over transactions and reads transaction hashes out of bounds.
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 that imports blockchain data trusts the file's contents. If the file is malformed, it can read past the end of a list, like reading a page beyond the end of a book. This can cause memory corruption.
What is Memory corruption?
Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.