Skip to content

Commit ac4e438

Browse files
committed
Sanity check transaction scripts in DecodeHexTx
Make sure that the scripts of decoded transactions are valid scripts.
1 parent 5b75c47 commit ac4e438

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

src/core_read.cpp

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,18 +88,40 @@ CScript ParseScript(const std::string& s)
8888
return result;
8989
}
9090

91+
// Check that all of the input and output scripts of a transaction contains valid opcodes
92+
bool CheckTxScriptsSanity(const CMutableTransaction& tx)
93+
{
94+
// Check input scripts for non-coinbase txs
95+
if (!CTransaction(tx).IsCoinBase()) {
96+
for (unsigned int i = 0; i < tx.vin.size(); i++) {
97+
if (!tx.vin[i].scriptSig.HasValidOps() || tx.vin[i].scriptSig.size() > MAX_SCRIPT_SIZE) {
98+
return false;
99+
}
100+
}
101+
}
102+
// Check output scripts
103+
for (unsigned int i = 0; i < tx.vout.size(); i++) {
104+
if (!tx.vout[i].scriptPubKey.HasValidOps() || tx.vout[i].scriptPubKey.size() > MAX_SCRIPT_SIZE) {
105+
return false;
106+
}
107+
}
108+
109+
return true;
110+
}
111+
91112
bool DecodeHexTx(CMutableTransaction& tx, const std::string& strHexTx, bool fTryNoWitness)
92113
{
93-
if (!IsHex(strHexTx))
114+
if (!IsHex(strHexTx)) {
94115
return false;
116+
}
95117

96118
std::vector<unsigned char> txData(ParseHex(strHexTx));
97119

98120
if (fTryNoWitness) {
99121
CDataStream ssData(txData, SER_NETWORK, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS);
100122
try {
101123
ssData >> tx;
102-
if (ssData.eof()) {
124+
if (ssData.eof() && CheckTxScriptsSanity(tx)) {
103125
return true;
104126
}
105127
}
@@ -111,8 +133,9 @@ bool DecodeHexTx(CMutableTransaction& tx, const std::string& strHexTx, bool fTry
111133
CDataStream ssData(txData, SER_NETWORK, PROTOCOL_VERSION);
112134
try {
113135
ssData >> tx;
114-
if (!ssData.empty())
136+
if (!ssData.empty()) {
115137
return false;
138+
}
116139
}
117140
catch (const std::exception&) {
118141
return false;

src/script/script.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,8 @@ bool CScript::HasValidOps() const
273273
CScript::const_iterator it = begin();
274274
while (it < end()) {
275275
opcodetype opcode;
276-
if (!GetOp(it, opcode) || opcode > 0xb9) {
276+
std::vector<unsigned char> item;
277+
if (!GetOp(it, opcode, item) || opcode > MAX_OPCODE || item.size() > MAX_SCRIPT_ELEMENT_SIZE) {
277278
return false;
278279
}
279280
}

src/script/script.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,9 @@ enum opcodetype
190190
OP_INVALIDOPCODE = 0xff,
191191
};
192192

193+
// Maximum value that an opcode can be
194+
static const unsigned int MAX_OPCODE = OP_NOP10;
195+
193196
const char* GetOpName(opcodetype opcode);
194197

195198
class scriptnum_error : public std::runtime_error

0 commit comments

Comments
 (0)