Skip to content

Commit 9c248e3

Browse files
committed
Merge #10481: Decodehextx scripts sanity check
ac4e438 Sanity check transaction scripts in DecodeHexTx (Andrew Chow) 5b75c47 Add a valid opcode sanity check to CScript (Andrew Chow) Tree-SHA512: a516e95c274c9d131123150c798cae8bed75925e8a4d59469967dd7f1d49f7f8161e26afb61024b821bd8dcdffdfd0d19b8dcad20b39b1106820326d8d56904d
2 parents 35e7f13 + ac4e438 commit 9c248e3

File tree

4 files changed

+59
-3
lines changed

4 files changed

+59
-3
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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,3 +267,16 @@ std::string CScriptWitness::ToString() const
267267
}
268268
return ret + ")";
269269
}
270+
271+
bool CScript::HasValidOps() const
272+
{
273+
CScript::const_iterator it = begin();
274+
while (it < end()) {
275+
opcodetype opcode;
276+
std::vector<unsigned char> item;
277+
if (!GetOp(it, opcode, item) || opcode > MAX_OPCODE || item.size() > MAX_SCRIPT_ELEMENT_SIZE) {
278+
return false;
279+
}
280+
}
281+
return true;
282+
}

src/script/script.h

Lines changed: 6 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
@@ -630,6 +633,9 @@ class CScript : public CScriptBase
630633
bool IsPushOnly(const_iterator pc) const;
631634
bool IsPushOnly() const;
632635

636+
/** Check if the script contains valid OP_CODES */
637+
bool HasValidOps() const;
638+
633639
/**
634640
* Returns whether the script is guaranteed to fail at execution,
635641
* regardless of the initial stack. This allows outputs to be pruned

src/test/script_tests.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1438,4 +1438,18 @@ BOOST_AUTO_TEST_CASE(script_FindAndDelete)
14381438
BOOST_CHECK(s == expect);
14391439
}
14401440

1441+
BOOST_AUTO_TEST_CASE(script_HasValidOps)
1442+
{
1443+
// Exercise the HasValidOps functionality
1444+
CScript script;
1445+
script = ScriptFromHex("76a9141234567890abcdefa1a2a3a4a5a6a7a8a9a0aaab88ac"); // Normal script
1446+
BOOST_CHECK(script.HasValidOps());
1447+
script = ScriptFromHex("76a914ff34567890abcdefa1a2a3a4a5a6a7a8a9a0aaab88ac");
1448+
BOOST_CHECK(script.HasValidOps());
1449+
script = ScriptFromHex("ff88ac"); // Script with OP_INVALIDOPCODE explicit
1450+
BOOST_CHECK(!script.HasValidOps());
1451+
script = ScriptFromHex("88acc0"); // Script with undefined opcode
1452+
BOOST_CHECK(!script.HasValidOps());
1453+
}
1454+
14411455
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)