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