| 1 | // Copyright (c) 2014-2022, The Monero Project |
| 2 | // |
| 3 | // All rights reserved. |
| 4 | // |
| 5 | // Redistribution and use in source and binary forms, with or without modification, are |
| 6 | // permitted provided that the following conditions are met: |
| 7 | // |
| 8 | // 1. Redistributions of source code must retain the above copyright notice, this list of |
| 9 | // conditions and the following disclaimer. |
| 10 | // |
| 11 | // 2. Redistributions in binary form must reproduce the above copyright notice, this list |
| 12 | // of conditions and the following disclaimer in the documentation and/or other |
| 13 | // materials provided with the distribution. |
| 14 | // |
| 15 | // 3. Neither the name of the copyright holder nor the names of its contributors may be |
| 16 | // used to endorse or promote products derived from this software without specific |
| 17 | // prior written permission. |
| 18 | // |
| 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY |
| 20 | // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 21 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
| 22 | // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 24 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 26 | // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF |
| 27 | // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | // |
| 29 | // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers |
| 30 | |
| 31 | #include "chaingen.h" |
| 32 | #include "integer_overflow.h" |
| 33 | |
| 34 | using namespace epee; |
| 35 | using namespace cryptonote; |
| 36 | |
| 37 | namespace |
| 38 | { |
| 39 | void split_miner_tx_outs(transaction& miner_tx, uint64_t amount_1) |
| 40 | { |
| 41 | uint64_t total_amount = get_outs_money_amount(miner_tx); |
| 42 | uint64_t amount_2 = total_amount - amount_1; |
| 43 | txout_target_v target = miner_tx.vout[0].target; |
| 44 | |
| 45 | miner_tx.vout.clear(); |
| 46 | |
| 47 | tx_out out1; |
| 48 | out1.amount = amount_1; |
| 49 | out1.target = target; |
| 50 | miner_tx.vout.push_back(out1); |
| 51 | |
| 52 | tx_out out2; |
| 53 | out2.amount = amount_2; |
| 54 | out2.target = target; |
| 55 | miner_tx.vout.push_back(out2); |
| 56 | } |
| 57 | |
| 58 | void append_tx_source_entry(std::vector<cryptonote::tx_source_entry>& sources, const transaction& tx, size_t out_idx) |
| 59 | { |
| 60 | cryptonote::tx_source_entry se; |
| 61 | se.amount = tx.vout[out_idx].amount; |
| 62 | se.push_output(0, boost::get<cryptonote::txout_to_key>(tx.vout[out_idx].target).key, se.amount); |
| 63 | se.real_output = 0; |
| 64 | se.rct = false; |
| 65 | se.real_out_tx_key = get_tx_pub_key_from_extra(tx); |
| 66 | se.real_out_additional_tx_keys = get_additional_tx_pub_keys_from_extra(tx); |
| 67 | se.real_output_in_tx_index = out_idx; |
| 68 | |
| 69 | sources.push_back(se); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | //====================================================================================================================== |
| 74 | |
| 75 | gen_uint_overflow_base::gen_uint_overflow_base() |
| 76 | : m_last_valid_block_event_idx(static_cast<size_t>(-1)) |
| 77 | { |
| 78 | REGISTER_CALLBACK_METHOD(gen_uint_overflow_1, mark_last_valid_block); |
| 79 | } |
| 80 | |
| 81 | bool gen_uint_overflow_base::check_tx_verification_context(const cryptonote::tx_verification_context& tvc, bool tx_added, size_t event_idx, const cryptonote::transaction& /*tx*/) |
| 82 | { |
| 83 | return m_last_valid_block_event_idx < event_idx ? !tx_added && tvc.m_verifivation_failed : tx_added && !tvc.m_verifivation_failed; |
| 84 | } |
| 85 | |
| 86 | bool gen_uint_overflow_base::check_block_verification_context(const cryptonote::block_verification_context& bvc, size_t event_idx, const cryptonote::block& /*block*/) |
| 87 | { |
| 88 | return m_last_valid_block_event_idx < event_idx ? bvc.m_verifivation_failed | bvc.m_marked_as_orphaned : !bvc.m_verifivation_failed; |
| 89 | } |
| 90 | |
| 91 | bool gen_uint_overflow_base::mark_last_valid_block(cryptonote::core& c, size_t ev_index, const std::vector<test_event_entry>& events) |
| 92 | { |
| 93 | m_last_valid_block_event_idx = ev_index - 1; |
| 94 | return true; |
| 95 | } |
| 96 | |
| 97 | //====================================================================================================================== |
| 98 | |
| 99 | bool gen_uint_overflow_1::generate(std::vector<test_event_entry>& events) const |
| 100 | { |
| 101 | uint64_t ts_start = 1338224400; |
| 102 | |
| 103 | GENERATE_ACCOUNT(miner_account); |
| 104 | MAKE_GENESIS_BLOCK(events, blk_0, miner_account, ts_start); |
| 105 | DO_CALLBACK(events, "mark_last_valid_block"); |
| 106 | MAKE_ACCOUNT(events, bob_account); |
| 107 | MAKE_ACCOUNT(events, alice_account); |
| 108 | |
| 109 | // Problem 1. Miner tx output overflow |
| 110 | MAKE_MINER_TX_MANUALLY(miner_tx_0, blk_0); |
| 111 | split_miner_tx_outs(miner_tx_0, MONEY_SUPPLY); |
| 112 | block blk_1; |
| 113 | if (!generator.construct_block_manually(blk_1, blk_0, miner_account, test_generator::bf_miner_tx, 0, 0, 0, crypto::hash(), 0, miner_tx_0)) |
| 114 | return false; |
| 115 | events.push_back(blk_1); |
| 116 | |
| 117 | // Problem 1. Miner tx outputs overflow |
| 118 | MAKE_MINER_TX_MANUALLY(miner_tx_1, blk_1); |
| 119 | split_miner_tx_outs(miner_tx_1, MONEY_SUPPLY); |
| 120 | block blk_2; |
| 121 | if (!generator.construct_block_manually(blk_2, blk_1, miner_account, test_generator::bf_miner_tx, 0, 0, 0, crypto::hash(), 0, miner_tx_1)) |
| 122 | return false; |
| 123 | events.push_back(blk_2); |
| 124 | |
| 125 | REWIND_BLOCKS(events, blk_2r, blk_2, miner_account); |
| 126 | MAKE_TX_LIST_START(events, txs_0, miner_account, bob_account, MONEY_SUPPLY, blk_2); |
| 127 | MAKE_TX_LIST(events, txs_0, miner_account, bob_account, MONEY_SUPPLY, blk_2); |
| 128 | MAKE_NEXT_BLOCK_TX_LIST(events, blk_3, blk_2r, miner_account, txs_0); |
| 129 | REWIND_BLOCKS(events, blk_3r, blk_3, miner_account); |
| 130 | |
| 131 | // Problem 2. total_fee overflow, block_reward overflow |
| 132 | std::list<cryptonote::transaction> txs_1; |
| 133 | // Create txs with huge fee |
| 134 | txs_1.push_back(construct_tx_with_fee(events, blk_3, bob_account, alice_account, MK_COINS(1), MONEY_SUPPLY - MK_COINS(1))); |
| 135 | txs_1.push_back(construct_tx_with_fee(events, blk_3, bob_account, alice_account, MK_COINS(1), MONEY_SUPPLY - MK_COINS(1))); |
| 136 | MAKE_NEXT_BLOCK_TX_LIST(events, blk_4, blk_3r, miner_account, txs_1); |
| 137 | |
| 138 | return true; |
| 139 | } |
| 140 | |
| 141 | //====================================================================================================================== |
| 142 | |
| 143 | bool gen_uint_overflow_2::generate(std::vector<test_event_entry>& events) const |
| 144 | { |
| 145 | uint64_t ts_start = 1338224400; |
| 146 | |
| 147 | GENERATE_ACCOUNT(miner_account); |
| 148 | MAKE_GENESIS_BLOCK(events, blk_0, miner_account, ts_start); |
| 149 | MAKE_ACCOUNT(events, bob_account); |
| 150 | MAKE_ACCOUNT(events, alice_account); |
| 151 | REWIND_BLOCKS(events, blk_0r, blk_0, miner_account); |
| 152 | DO_CALLBACK(events, "mark_last_valid_block"); |
| 153 | |
| 154 | // Problem 1. Regular tx outputs overflow |
| 155 | std::vector<cryptonote::tx_source_entry> sources; |
| 156 | for (size_t i = 0; i < blk_0.miner_tx.vout.size(); ++i) |
| 157 | { |
| 158 | if (TESTS_DEFAULT_FEE < blk_0.miner_tx.vout[i].amount) |
| 159 | { |
| 160 | append_tx_source_entry(sources, blk_0.miner_tx, i); |
| 161 | break; |
| 162 | } |
| 163 | } |
| 164 | if (sources.empty()) |
| 165 | { |
| 166 | return false; |
| 167 | } |
| 168 | |
| 169 | std::vector<cryptonote::tx_destination_entry> destinations; |
| 170 | const account_public_address& bob_addr = bob_account.get_keys().m_account_address; |
| 171 | destinations.push_back(tx_destination_entry(MONEY_SUPPLY, bob_addr, false)); |
| 172 | destinations.push_back(tx_destination_entry(MONEY_SUPPLY - 1, bob_addr, false)); |
| 173 | // sources.front().amount = destinations[0].amount + destinations[2].amount + destinations[3].amount + TESTS_DEFAULT_FEE |
| 174 | destinations.push_back(tx_destination_entry(sources.front().amount - MONEY_SUPPLY - MONEY_SUPPLY + 1 - TESTS_DEFAULT_FEE, bob_addr, false)); |
| 175 | |
| 176 | cryptonote::transaction tx_1; |
| 177 | if (!construct_tx(miner_account.get_keys(), sources, destinations, boost::none, std::vector<uint8_t>(), tx_1)) |
| 178 | return false; |
| 179 | events.push_back(tx_1); |
| 180 | |
| 181 | MAKE_NEXT_BLOCK_TX1(events, blk_1, blk_0r, miner_account, tx_1); |
| 182 | REWIND_BLOCKS(events, blk_1r, blk_1, miner_account); |
| 183 | |
| 184 | // Problem 2. Regular tx inputs overflow |
| 185 | sources.clear(); |
| 186 | for (size_t i = 0; i < tx_1.vout.size(); ++i) |
| 187 | { |
| 188 | auto& tx_1_out = tx_1.vout[i]; |
| 189 | if (tx_1_out.amount < MONEY_SUPPLY - 1) |
| 190 | continue; |
| 191 | |
| 192 | append_tx_source_entry(sources, tx_1, i); |
| 193 | } |
| 194 | |
| 195 | destinations.clear(); |
| 196 | cryptonote::tx_destination_entry de; |
| 197 | de.addr = alice_account.get_keys().m_account_address; |
| 198 | de.amount = MONEY_SUPPLY - TESTS_DEFAULT_FEE; |
| 199 | destinations.push_back(de); |
| 200 | destinations.push_back(de); |
| 201 | |
| 202 | cryptonote::transaction tx_2; |
| 203 | if (!construct_tx(bob_account.get_keys(), sources, destinations, boost::none, std::vector<uint8_t>(), tx_2)) |
| 204 | return false; |
| 205 | events.push_back(tx_2); |
| 206 | |
| 207 | MAKE_NEXT_BLOCK_TX1(events, blk_2, blk_1r, miner_account, tx_2); |
| 208 | |
| 209 | return true; |
| 210 | } |