Skip to content

Commit ad57fb7

Browse files
committed
wallet: Add BerkeleyDB version sanity check at init time
Detect version conflicts between the run-time BerkeleyDB library and the one used during compilation. This is very unsafe (can result in anything from crashes to corruption) so shut down when one is detected.
1 parent 30e664d commit ad57fb7

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

src/wallet/bdb.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,23 @@ bool BerkeleyBatch::TxnAbort()
723723
return (ret == 0);
724724
}
725725

726+
bool BerkeleyDatabaseSanityCheck()
727+
{
728+
int major, minor;
729+
DbEnv::version(&major, &minor, nullptr);
730+
731+
/* If the major version differs, or the minor version of library is *older*
732+
* than the header that was compiled against, flag an error.
733+
*/
734+
if (major != DB_VERSION_MAJOR || minor < DB_VERSION_MINOR) {
735+
LogPrintf("BerkeleyDB database version conflict: header version is %d.%d, library version is %d.%d\n",
736+
DB_VERSION_MAJOR, DB_VERSION_MINOR, major, minor);
737+
return false;
738+
}
739+
740+
return true;
741+
}
742+
726743
std::string BerkeleyDatabaseVersion()
727744
{
728745
return DbEnv::version(nullptr, nullptr, nullptr);

src/wallet/bdb.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,10 @@ class BerkeleyBatch : public DatabaseBatch
223223

224224
std::string BerkeleyDatabaseVersion();
225225

226+
/** Perform sanity check of runtime BDB version versus linked BDB version.
227+
*/
228+
bool BerkeleyDatabaseSanityCheck();
229+
226230
//! Return object giving access to Berkeley database at specified path.
227231
std::unique_ptr<BerkeleyDatabase> MakeBerkeleyDatabase(const fs::path& path, const DatabaseOptions& options, DatabaseStatus& status, bilingual_str& error);
228232

src/wallet/init.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ void WalletInit::AddWalletOptions(ArgsManager& argsman) const
8686

8787
bool WalletInit::ParameterInteraction() const
8888
{
89+
#ifdef USE_BDB
90+
if (!BerkeleyDatabaseSanityCheck()) {
91+
return InitError(Untranslated("A version conflict was detected between the run-time BerkeleyDB library and the one used during compilation."));
92+
}
93+
#endif
8994
if (gArgs.GetBoolArg("-disablewallet", DEFAULT_DISABLE_WALLET)) {
9095
for (const std::string& wallet : gArgs.GetArgs("-wallet")) {
9196
LogPrintf("%s: parameter interaction: -disablewallet -> ignoring -wallet=%s\n", __func__, wallet);

0 commit comments

Comments
 (0)