|
| 1 | +// Copyright (c) 2009-2010 Satoshi Nakamoto |
| 2 | +// Copyright (c) 2009-2014 The Bitcoin developers |
| 3 | +// Distributed under the MIT software license, see the accompanying |
| 4 | +// file COPYING or http://www.opensource.org/licenses/mit-license.php. |
| 5 | + |
| 6 | +// NOTE: This file is intended to be customised by the end user, and includes only local node policy logic |
| 7 | + |
| 8 | +#include "policy/policy.h" |
| 9 | + |
| 10 | +#include "main.h" |
| 11 | +#include "tinyformat.h" |
| 12 | +#include "util.h" |
| 13 | +#include "utilstrencodings.h" |
| 14 | + |
| 15 | +#include <boost/foreach.hpp> |
| 16 | + |
| 17 | + /** |
| 18 | + * Check transaction inputs to mitigate two |
| 19 | + * potential denial-of-service attacks: |
| 20 | + * |
| 21 | + * 1. scriptSigs with extra data stuffed into them, |
| 22 | + * not consumed by scriptPubKey (or P2SH script) |
| 23 | + * 2. P2SH scripts with a crazy number of expensive |
| 24 | + * CHECKSIG/CHECKMULTISIG operations |
| 25 | + * |
| 26 | + * Check transaction inputs, and make sure any |
| 27 | + * pay-to-script-hash transactions are evaluating IsStandard scripts |
| 28 | + * |
| 29 | + * Why bother? To avoid denial-of-service attacks; an attacker |
| 30 | + * can submit a standard HASH... OP_EQUAL transaction, |
| 31 | + * which will get accepted into blocks. The redemption |
| 32 | + * script can be anything; an attacker could use a very |
| 33 | + * expensive-to-check-upon-redemption script like: |
| 34 | + * DUP CHECKSIG DROP ... repeated 100 times... OP_1 |
| 35 | + */ |
| 36 | + |
| 37 | +bool IsStandard(const CScript& scriptPubKey, txnouttype& whichType) |
| 38 | +{ |
| 39 | + std::vector<std::vector<unsigned char> > vSolutions; |
| 40 | + if (!Solver(scriptPubKey, whichType, vSolutions)) |
| 41 | + return false; |
| 42 | + |
| 43 | + if (whichType == TX_MULTISIG) |
| 44 | + { |
| 45 | + unsigned char m = vSolutions.front()[0]; |
| 46 | + unsigned char n = vSolutions.back()[0]; |
| 47 | + // Support up to x-of-3 multisig txns as standard |
| 48 | + if (n < 1 || n > 3) |
| 49 | + return false; |
| 50 | + if (m < 1 || m > n) |
| 51 | + return false; |
| 52 | + } |
| 53 | + |
| 54 | + return whichType != TX_NONSTANDARD; |
| 55 | +} |
| 56 | + |
| 57 | +bool IsStandardTx(const CTransaction& tx, std::string& reason) |
| 58 | +{ |
| 59 | + if (tx.nVersion > CTransaction::CURRENT_VERSION || tx.nVersion < 1) { |
| 60 | + reason = "version"; |
| 61 | + return false; |
| 62 | + } |
| 63 | + |
| 64 | + // Extremely large transactions with lots of inputs can cost the network |
| 65 | + // almost as much to process as they cost the sender in fees, because |
| 66 | + // computing signature hashes is O(ninputs*txsize). Limiting transactions |
| 67 | + // to MAX_STANDARD_TX_SIZE mitigates CPU exhaustion attacks. |
| 68 | + unsigned int sz = tx.GetSerializeSize(SER_NETWORK, CTransaction::CURRENT_VERSION); |
| 69 | + if (sz >= MAX_STANDARD_TX_SIZE) { |
| 70 | + reason = "tx-size"; |
| 71 | + return false; |
| 72 | + } |
| 73 | + |
| 74 | + BOOST_FOREACH(const CTxIn& txin, tx.vin) |
| 75 | + { |
| 76 | + // Biggest 'standard' txin is a 15-of-15 P2SH multisig with compressed |
| 77 | + // keys. (remember the 520 byte limit on redeemScript size) That works |
| 78 | + // out to a (15*(33+1))+3=513 byte redeemScript, 513+1+15*(73+1)+3=1627 |
| 79 | + // bytes of scriptSig, which we round off to 1650 bytes for some minor |
| 80 | + // future-proofing. That's also enough to spend a 20-of-20 |
| 81 | + // CHECKMULTISIG scriptPubKey, though such a scriptPubKey is not |
| 82 | + // considered standard) |
| 83 | + if (txin.scriptSig.size() > 1650) { |
| 84 | + reason = "scriptsig-size"; |
| 85 | + return false; |
| 86 | + } |
| 87 | + if (!txin.scriptSig.IsPushOnly()) { |
| 88 | + reason = "scriptsig-not-pushonly"; |
| 89 | + return false; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + unsigned int nDataOut = 0; |
| 94 | + txnouttype whichType; |
| 95 | + BOOST_FOREACH(const CTxOut& txout, tx.vout) { |
| 96 | + if (!::IsStandard(txout.scriptPubKey, whichType)) { |
| 97 | + reason = "scriptpubkey"; |
| 98 | + return false; |
| 99 | + } |
| 100 | + |
| 101 | + if (whichType == TX_NULL_DATA) |
| 102 | + nDataOut++; |
| 103 | + else if ((whichType == TX_MULTISIG) && (!fIsBareMultisigStd)) { |
| 104 | + reason = "bare-multisig"; |
| 105 | + return false; |
| 106 | + } else if (txout.IsDust(::minRelayTxFee)) { |
| 107 | + reason = "dust"; |
| 108 | + return false; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + // only one OP_RETURN txout is permitted |
| 113 | + if (nDataOut > 1) { |
| 114 | + reason = "multi-op-return"; |
| 115 | + return false; |
| 116 | + } |
| 117 | + |
| 118 | + return true; |
| 119 | +} |
| 120 | + |
| 121 | +bool AreInputsStandard(const CTransaction& tx, const CCoinsViewCache& mapInputs) |
| 122 | +{ |
| 123 | + if (tx.IsCoinBase()) |
| 124 | + return true; // Coinbases don't use vin normally |
| 125 | + |
| 126 | + for (unsigned int i = 0; i < tx.vin.size(); i++) |
| 127 | + { |
| 128 | + const CTxOut& prev = mapInputs.GetOutputFor(tx.vin[i]); |
| 129 | + |
| 130 | + std::vector<std::vector<unsigned char> > vSolutions; |
| 131 | + txnouttype whichType; |
| 132 | + // get the scriptPubKey corresponding to this input: |
| 133 | + const CScript& prevScript = prev.scriptPubKey; |
| 134 | + if (!Solver(prevScript, whichType, vSolutions)) |
| 135 | + return false; |
| 136 | + int nArgsExpected = ScriptSigArgsExpected(whichType, vSolutions); |
| 137 | + if (nArgsExpected < 0) |
| 138 | + return false; |
| 139 | + |
| 140 | + // Transactions with extra stuff in their scriptSigs are |
| 141 | + // non-standard. Note that this EvalScript() call will |
| 142 | + // be quick, because if there are any operations |
| 143 | + // beside "push data" in the scriptSig |
| 144 | + // IsStandardTx() will have already returned false |
| 145 | + // and this method isn't called. |
| 146 | + std::vector<std::vector<unsigned char> > stack; |
| 147 | + if (!EvalScript(stack, tx.vin[i].scriptSig, SCRIPT_VERIFY_NONE, BaseSignatureChecker())) |
| 148 | + return false; |
| 149 | + |
| 150 | + if (whichType == TX_SCRIPTHASH) |
| 151 | + { |
| 152 | + if (stack.empty()) |
| 153 | + return false; |
| 154 | + CScript subscript(stack.back().begin(), stack.back().end()); |
| 155 | + std::vector<std::vector<unsigned char> > vSolutions2; |
| 156 | + txnouttype whichType2; |
| 157 | + if (Solver(subscript, whichType2, vSolutions2)) |
| 158 | + { |
| 159 | + int tmpExpected = ScriptSigArgsExpected(whichType2, vSolutions2); |
| 160 | + if (tmpExpected < 0) |
| 161 | + return false; |
| 162 | + nArgsExpected += tmpExpected; |
| 163 | + } |
| 164 | + else |
| 165 | + { |
| 166 | + // Any other Script with less than 15 sigops OK: |
| 167 | + unsigned int sigops = subscript.GetSigOpCount(true); |
| 168 | + // ... extra data left on the stack after execution is OK, too: |
| 169 | + return (sigops <= MAX_P2SH_SIGOPS); |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + if (stack.size() != (unsigned int)nArgsExpected) |
| 174 | + return false; |
| 175 | + } |
| 176 | + |
| 177 | + return true; |
| 178 | +} |
0 commit comments