Merge #8976: libconsensus: Add input validation of flags (#1891)

5ca8ef2 libconsensus: Add input validation of flags (Wladimir J. van der Laan)
This commit is contained in:
Alexander Block 2018-02-05 16:42:05 +01:00 committed by UdjinM6
parent 063bc55424
commit 61af31531a
3 changed files with 17 additions and 1 deletions

View File

@ -70,10 +70,19 @@ struct ECCryptoClosure
ECCryptoClosure instance_of_eccryptoclosure;
}
/** Check that all specified flags are part of the libconsensus interface. */
static bool verify_flags(unsigned int flags)
{
return (flags & ~(dashconsensus_SCRIPT_FLAGS_VERIFY_ALL)) == 0;
}
int dashconsensus_verify_script(const unsigned char *scriptPubKey, unsigned int scriptPubKeyLen,
const unsigned char *txTo , unsigned int txToLen,
unsigned int nIn, unsigned int flags, dashconsensus_error* err)
{
if (!verify_flags(flags)) {
return dashconsensus_ERR_INVALID_FLAGS;
}
try {
TxInputStream stream(SER_NETWORK, PROTOCOL_VERSION, txTo, txToLen);
CTransaction tx(deserialize, stream);

View File

@ -39,6 +39,7 @@ typedef enum dashconsensus_error_t
dashconsensus_ERR_TX_INDEX,
dashconsensus_ERR_TX_SIZE_MISMATCH,
dashconsensus_ERR_TX_DESERIALIZE,
dashconsensus_ERR_INVALID_FLAGS,
} dashconsensus_error;
/** Script verification flags */
@ -50,6 +51,9 @@ enum
dashconsensus_SCRIPT_FLAGS_VERIFY_NULLDUMMY = (1U << 4), // enforce NULLDUMMY (BIP147)
dashconsensus_SCRIPT_FLAGS_VERIFY_CHECKLOCKTIMEVERIFY = (1U << 9), // enable CHECKLOCKTIMEVERIFY (BIP65)
dashconsensus_SCRIPT_FLAGS_VERIFY_CHECKSEQUENCEVERIFY = (1U << 10), // enable CHECKSEQUENCEVERIFY (BIP112)
dashconsensus_SCRIPT_FLAGS_VERIFY_ALL = dashconsensus_SCRIPT_FLAGS_VERIFY_P2SH | dashconsensus_SCRIPT_FLAGS_VERIFY_DERSIG |
dashconsensus_SCRIPT_FLAGS_VERIFY_NULLDUMMY | dashconsensus_SCRIPT_FLAGS_VERIFY_CHECKLOCKTIMEVERIFY |
dashconsensus_SCRIPT_FLAGS_VERIFY_CHECKSEQUENCEVERIFY
};
/// Returns 1 if the input nIn of the serialized transaction pointed to by

View File

@ -154,7 +154,10 @@ void DoTest(const CScript& scriptPubKey, const CScript& scriptSig, int flags, co
#if defined(HAVE_CONSENSUS_LIB)
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
stream << tx2;
BOOST_CHECK_MESSAGE(dashconsensus_verify_script(scriptPubKey.data(), scriptPubKey.size(), (const unsigned char*)&stream[0], stream.size(), 0, flags, NULL) == expect,message);
int libconsensus_flags = flags & dashconsensus_SCRIPT_FLAGS_VERIFY_ALL;
if (libconsensus_flags == flags) {
BOOST_CHECK_MESSAGE(dashconsensus_verify_script(scriptPubKey.data(), scriptPubKey.size(), (const unsigned char*)&stream[0], stream.size(), 0, libconsensus_flags, NULL) == expect,message);
}
#endif
}