Merge bitcoin/bitcoin#28207: mempool: Persist with XOR

fa6b053b5c mempool: persist with XOR (MarcoFalke)

Pull request description:

  Currently the `mempool.dat` file stores data received from remote peers as-is. This may be problematic when a program other than Bitcoin Core tries to interpret them by accident. For example, an anti-virus program or other program may scan the file and move it into quarantine, or delete it, or corrupt it.

  While the local wallet is expected to re-submit any pending transactions, unrelated transactions may be missing from the mempool after a restart. This may cause fee estimates to be off, or may cause block relay to be slower.

  Fix this, similar to https://github.com/bitcoin/bitcoin/pull/6650, by rolling a random XOR pattern over the dat file when writing or reading it.

  Obviously this can only protect against programs that accidentally and unintentionally are trying to mess with the dat file. Any program that intentionally wants to mess with the dat file can still trivially do so.

ACKs for top commit:
  achow101:
    re-ACK fa6b053b5c
  glozow:
    reACK fa6b053b5c
  ismaelsadeeq:
    ACK fa6b053b5c

Tree-SHA512: ded2ce3d81bc944b828263534e3178a1e45a914fe8e024f4a14c6561a73e301820944ecc75dd704b3d4221a7a3a5c0597ccab79546250c1197609ee981fe324e
This commit is contained in:
Andrew Chow
2023-11-13 11:17:02 -05:00
9 changed files with 49 additions and 15 deletions

View File

@@ -23,6 +23,8 @@ static constexpr unsigned int DEFAULT_BLOCKSONLY_MAX_MEMPOOL_SIZE_MB{5};
static constexpr unsigned int DEFAULT_MEMPOOL_EXPIRY_HOURS{336};
/** Default for -mempoolfullrbf, if the transaction replaceability signaling is ignored */
static constexpr bool DEFAULT_MEMPOOL_FULL_RBF{false};
/** Whether to fall back to legacy V1 serialization when writing mempool.dat */
static constexpr bool DEFAULT_PERSIST_V1_DAT{false};
/** Default for -acceptnonstdtxn */
static constexpr bool DEFAULT_ACCEPT_NON_STD_TXN{false};
@@ -56,6 +58,7 @@ struct MemPoolOptions {
bool permit_bare_multisig{DEFAULT_PERMIT_BAREMULTISIG};
bool require_standard{true};
bool full_rbf{DEFAULT_MEMPOOL_FULL_RBF};
bool persist_v1_dat{DEFAULT_PERSIST_V1_DAT};
MemPoolLimits limits{};
};
} // namespace kernel

View File

@@ -8,6 +8,7 @@
#include <consensus/amount.h>
#include <logging.h>
#include <primitives/transaction.h>
#include <random.h>
#include <serialize.h>
#include <streams.h>
#include <sync.h>
@@ -34,14 +35,14 @@ using fsbridge::FopenFn;
namespace kernel {
static const uint64_t MEMPOOL_DUMP_VERSION = 1;
static const uint64_t MEMPOOL_DUMP_VERSION_NO_XOR_KEY{1};
static const uint64_t MEMPOOL_DUMP_VERSION{2};
bool LoadMempool(CTxMemPool& pool, const fs::path& load_path, Chainstate& active_chainstate, ImportMempoolOptions&& opts)
{
if (load_path.empty()) return false;
FILE* filestr{opts.mockable_fopen_function(load_path, "rb")};
CAutoFile file{filestr, CLIENT_VERSION};
CAutoFile file{opts.mockable_fopen_function(load_path, "rb"), CLIENT_VERSION};
if (file.IsNull()) {
LogPrintf("Failed to open mempool file from disk. Continuing anyway.\n");
return false;
@@ -57,9 +58,15 @@ bool LoadMempool(CTxMemPool& pool, const fs::path& load_path, Chainstate& active
try {
uint64_t version;
file >> version;
if (version != MEMPOOL_DUMP_VERSION) {
std::vector<std::byte> xor_key;
if (version == MEMPOOL_DUMP_VERSION_NO_XOR_KEY) {
// Leave XOR-key empty
} else if (version == MEMPOOL_DUMP_VERSION) {
file >> xor_key;
} else {
return false;
}
file.SetXor(xor_key);
uint64_t num;
file >> num;
while (num) {
@@ -151,17 +158,22 @@ bool DumpMempool(const CTxMemPool& pool, const fs::path& dump_path, FopenFn mock
auto mid = SteadyClock::now();
CAutoFile file{mockable_fopen_function(dump_path + ".new", "wb"), CLIENT_VERSION};
if (file.IsNull()) {
return false;
}
try {
FILE* filestr{mockable_fopen_function(dump_path + ".new", "wb")};
if (!filestr) {
return false;
}
CAutoFile file{filestr, CLIENT_VERSION};
uint64_t version = MEMPOOL_DUMP_VERSION;
const uint64_t version{pool.m_persist_v1_dat ? MEMPOOL_DUMP_VERSION_NO_XOR_KEY : MEMPOOL_DUMP_VERSION};
file << version;
std::vector<std::byte> xor_key(8);
if (!pool.m_persist_v1_dat) {
FastRandomContext{}.fillrand(xor_key);
file << xor_key;
}
file.SetXor(xor_key);
file << (uint64_t)vinfo.size();
for (const auto& i : vinfo) {
file << *(i.tx);