Skip to content

Commit 0c62e3a

Browse files
committed
New regression testing for CVE-2018-17144, CVE-2012-2459, and CVE-2010-5137.
CVE-2018-17144 and CVE-2012-2459 are only partially tested for regression. - CVE-2018-17144 is not tested for the inflation bug. - CVE-2012-2459 is only tested for the mutated block being rejected, not for the original block being accepted afterwards. This commit fixes that limitation. Also added functional test for CVE-2010-5137.
1 parent 38bfca6 commit 0c62e3a

File tree

2 files changed

+88
-7
lines changed

2 files changed

+88
-7
lines changed

test/functional/data/invalid_txs.py

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,24 @@
2424
from test_framework.messages import CTransaction, CTxIn, CTxOut, COutPoint
2525
from test_framework import script as sc
2626
from test_framework.blocktools import create_tx_with_script, MAX_BLOCK_SIGOPS
27-
27+
from test_framework.script import (
28+
CScript,
29+
OP_CAT,
30+
OP_SUBSTR,
31+
OP_LEFT,
32+
OP_RIGHT,
33+
OP_INVERT,
34+
OP_AND,
35+
OP_OR,
36+
OP_XOR,
37+
OP_2MUL,
38+
OP_2DIV,
39+
OP_MUL,
40+
OP_DIV,
41+
OP_MOD,
42+
OP_LSHIFT,
43+
OP_RSHIFT
44+
)
2845
basic_p2sh = sc.CScript([sc.OP_HASH160, sc.hash160(sc.CScript([sc.OP_0])), sc.OP_EQUAL])
2946

3047

@@ -178,7 +195,44 @@ def get_tx(self):
178195
script_pub_key=lotsa_checksigs,
179196
amount=1)
180197

198+
def getDisabledOpcodeTemplate(opcode):
199+
""" Creates disabled opcode tx template class"""
200+
def get_tx(self):
201+
tx = CTransaction()
202+
vin = self.valid_txin
203+
vin.scriptSig = CScript([opcode])
204+
tx.vin.append(vin)
205+
tx.vout.append(CTxOut(1, basic_p2sh))
206+
tx.calc_sha256()
207+
return tx
208+
209+
return type('DisabledOpcode_' + str(opcode), (BadTxTemplate,), {
210+
'reject_reason': "disabled opcode",
211+
'expect_disconnect': True,
212+
'get_tx': get_tx,
213+
'valid_in_block' : True
214+
})
215+
216+
# Disabled opcode tx templates (CVE-2010-5137)
217+
DisabledOpcodeTemplates = [getDisabledOpcodeTemplate(opcode) for opcode in [
218+
OP_CAT,
219+
OP_SUBSTR,
220+
OP_LEFT,
221+
OP_RIGHT,
222+
OP_INVERT,
223+
OP_AND,
224+
OP_OR,
225+
OP_XOR,
226+
OP_2MUL,
227+
OP_2DIV,
228+
OP_MUL,
229+
OP_DIV,
230+
OP_MOD,
231+
OP_LSHIFT,
232+
OP_RSHIFT]]
233+
181234

182235
def iter_all_templates():
183236
"""Iterate through all bad transaction template types."""
184237
return BadTxTemplate.__subclasses__()
238+

test/functional/p2p_invalid_block.py

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,13 @@ def run_test(self):
8585
# Check transactions for duplicate inputs (CVE-2018-17144)
8686
self.log.info("Test duplicate input block.")
8787

88-
block2_orig.vtx[2].vin.append(block2_orig.vtx[2].vin[0])
89-
block2_orig.vtx[2].rehash()
90-
block2_orig.hashMerkleRoot = block2_orig.calc_merkle_root()
91-
block2_orig.rehash()
92-
block2_orig.solve()
93-
node.p2p.send_blocks_and_test([block2_orig], node, success=False, reject_reason='bad-txns-inputs-duplicate')
88+
block2_dup = copy.deepcopy(block2_orig)
89+
block2_dup.vtx[2].vin.append(block2_dup.vtx[2].vin[0])
90+
block2_dup.vtx[2].rehash()
91+
block2_dup.hashMerkleRoot = block2_dup.calc_merkle_root()
92+
block2_dup.rehash()
93+
block2_dup.solve()
94+
node.p2p.send_blocks_and_test([block2_dup], node, success=False, reject_reason='bad-txns-inputs-duplicate')
9495

9596
self.log.info("Test very broken block.")
9697

@@ -106,5 +107,31 @@ def run_test(self):
106107
node.p2p.send_blocks_and_test([block3], node, success=False, reject_reason='bad-cb-amount')
107108

108109

110+
# Complete testing of CVE-2012-2459 by sending the original block.
111+
# It should be accepted even though it has the same hash as the mutated one.
112+
113+
self.log.info("Test accepting original block after rejecting its mutated version.")
114+
node.p2p.send_blocks_and_test([block2_orig], node, success=True, timeout=5)
115+
116+
# Update tip info
117+
height += 1
118+
block_time += 1
119+
tip = int(block2_orig.hash, 16)
120+
121+
# Complete testing of CVE-2018-17144, by checking for the inflation bug.
122+
# Create a block that spends the output of a tx in a previous block.
123+
block4 = create_block(tip, create_coinbase(height), block_time)
124+
tx3 = create_tx_with_script(tx2, 0, script_sig=b'\x51', amount=50 * COIN)
125+
126+
# Duplicates input
127+
tx3.vin.append(tx3.vin[0])
128+
tx3.rehash()
129+
block4.vtx.append(tx3)
130+
block4.hashMerkleRoot = block4.calc_merkle_root()
131+
block4.rehash()
132+
block4.solve()
133+
self.log.info("Test inflation by duplicating input")
134+
node.p2p.send_blocks_and_test([block4], node, success=False, reject_reason='bad-txns-inputs-duplicate')
135+
109136
if __name__ == '__main__':
110137
InvalidBlockRequestTest().main()

0 commit comments

Comments
 (0)