-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathhoneypot.cpp
More file actions
359 lines (321 loc) · 13.6 KB
/
honeypot.cpp
File metadata and controls
359 lines (321 loc) · 13.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
// Copyright Cartesi and individual authors (see AUTHORS)
// SPDX-License-Identifier: Apache-2.0 (see LICENSE)
#include <cstdint>
#include <cerrno> // errno
#include <cstdio> // std::fprintf/stderr
#include <cstring> // std::strerror
#include <algorithm> // std::equal/std::copy_n
#include <array> // std::array
#include <tuple> // std::ignore
extern "C" {
#include <fcntl.h> // open
#include <sys/mman.h> // mmap/msync
#include <unistd.h> // close/lseek
#include <libcmt/abi.h>
#include <libcmt/io.h>
#include <libcmt/rollup.h>
}
#include <honeypot-config.hpp>
#define STATE_BLOCK_DEVICE "/dev/pmem1"
namespace {
////////////////////////////////////////////////////////////////////////////////
// ERC-20 address type.
using erc20_address = cmt_abi_address_t;
// Compare two ERC-20 addresses.
bool operator==(const erc20_address &a, const erc20_address &b) {
return std::equal(std::begin(a.data), std::end(a.data), std::begin(b.data));
}
////////////////////////////////////////////////////////////////////////////////
// Big Endian 256 type.
using be256 = std::array<uint8_t, 32>;
// Adds `a` and `b` and store in `res`.
// Returns true when there is no arithmetic overflow, false otherwise.
[[nodiscard]]
bool be256_checked_add(be256 &res, const be256 &a, const be256 &b) {
uint16_t carry = 0;
for (size_t i = 0; i < res.size(); ++i) {
const size_t j = res.size() - i - 1;
const uint16_t aj = a[j];
const uint16_t bj = b[j];
const uint16_t tmp = carry + aj + bj;
res[j] = static_cast<uint8_t>(tmp);
carry = tmp >> 8U;
}
return carry == 0;
}
////////////////////////////////////////////////////////////////////////////////
// Rollup utilities.
template <typename T>
[[nodiscard]]
constexpr cmt_abi_bytes_t payload_to_bytes(const T &payload) {
cmt_abi_bytes_t payload_bytes = {
.length = sizeof(T),
.data = const_cast<T *>(&payload) // NOLINT(cppcoreguidelines-pro-type-const-cast)
};
return payload_bytes;
}
// Emit a report POD into rollup device.
template <typename T>
[[nodiscard]]
bool rollup_emit_report(cmt_rollup_t *rollup, const T &payload) {
const cmt_abi_bytes_t payload_bytes = payload_to_bytes(payload);
const int err = cmt_rollup_emit_report(rollup, &payload_bytes);
if (err < 0) {
std::ignore = std::fprintf(stderr, "[dapp] unable to emit report: %s\n", std::strerror(-err));
return false;
}
return true;
}
// Emit a voucher POD into rollup device.
template <typename T>
[[nodiscard]]
bool rollup_emit_voucher(cmt_rollup_t *rollup, const erc20_address &address, const T &payload) {
const cmt_abi_bytes_t payload_bytes = payload_to_bytes(payload);
const cmt_abi_u256_t wei{}; // Transfer 0 Wei
const int err = cmt_rollup_emit_voucher(rollup, &address, &wei, &payload_bytes, nullptr);
if (err < 0) {
std::ignore = std::fprintf(stderr, "[dapp] unable to emit voucher: %s\n", std::strerror(-err));
return false;
}
return true;
}
// Finish last rollup request, wait for next rollup request and process it.
// For every new request, reads an input POD and call backs its respective advance or inspect state handler.
template <typename STATE, typename ADVANCE_STATE, typename INSPECT_STATE>
[[nodiscard]]
bool rollup_process_next_request(cmt_rollup_t *rollup, STATE *state, ADVANCE_STATE advance_state,
INSPECT_STATE inspect_state, bool accept_previous_request) {
// Finish previous request and wait for the next request.
cmt_rollup_finish_t finish{.accept_previous_request = accept_previous_request};
const int err = cmt_rollup_finish(rollup, &finish);
if (err < 0) {
std::ignore = std::fprintf(stderr, "[dapp] unable to perform rollup finish: %s\n", std::strerror(-err));
return false;
}
// Handle request
switch (finish.next_request_type) {
case HTIF_YIELD_REASON_ADVANCE: { // Advance state.
return advance_state(rollup, state);
}
case HTIF_YIELD_REASON_INSPECT: { // Inspect state.
// Call inspect state handler.
return inspect_state(rollup, state);
}
default: { // Invalid request.
std::ignore = std::fprintf(stderr, "[dapp] invalid request type\n");
return false;
}
}
}
////////////////////////////////////////////////////////////////////////////////
// ERC-20 encoding utilities.
// Bytecode for solidity 'transfer(address,uint256)' in solidity.
#define TRANSFER_FUNCTION_SELECTOR_BYTES \
{0xa9, 0x05, 0x9c, 0xbb, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
// Payload encoding for ERC-20 deposits.
struct [[gnu::packed]] erc20_deposit {
erc20_address token_address;
erc20_address sender_address;
be256 amount;
};
// Payload encoding for ERC-20 transfers.
struct [[gnu::packed]] erc20_transfer {
std::array<uint8_t, 16> bytecode;
erc20_address destination;
be256 amount;
};
// Encodes a ERC-20 transfer of amount to destination address.
erc20_transfer encode_erc20_transfer(erc20_address destination, be256 amount) {
erc20_transfer payload{
.bytecode = TRANSFER_FUNCTION_SELECTOR_BYTES,
.destination = destination,
.amount = amount,
};
return payload;
}
////////////////////////////////////////////////////////////////////////////////
// DApp state utilities.
// Load dapp state from disk.
template <typename STATE>
[[nodiscard]]
STATE *dapp_load_state(const char *block_device) {
// Open the dapp state block device.
const int state_fd = open(block_device, O_RDWR);
if (state_fd < 0) {
std::ignore = std::fprintf(stderr, "[dapp] unable to open state block device: %s\n", std::strerror(errno));
return nullptr;
}
// Check if the block device size is big enough.
const auto size = lseek(state_fd, 0, SEEK_END);
if (size < 0) {
std::ignore = std::fprintf(stderr, "[dapp] unable to seek state block device: %s\n", std::strerror(errno));
std::ignore = close(state_fd);
return nullptr;
}
if (static_cast<size_t>(size) < sizeof(STATE)) {
std::ignore = std::fprintf(stderr, "[dapp] state block device size is too small\n");
std::ignore = close(state_fd);
return nullptr;
}
// Map the state block device to memory.
// Note that we call mmap() but never call munmap(), we intentionally let the OS automatically do this on exit.
void *mem = mmap(nullptr, sizeof(STATE), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, state_fd, 0);
if (mem == MAP_FAILED) {
std::ignore =
std::fprintf(stderr, "[dapp] unable to map state block device to memory: %s\n", std::strerror(errno));
std::ignore = close(state_fd);
return nullptr;
}
// After the mmap() call, the file descriptor can be closed immediately without invalidating the mapping.
if (close(state_fd) < 0) {
std::ignore = std::fprintf(stderr, "[dapp] unable to close state block device: %s\n", std::strerror(errno));
return nullptr;
}
return reinterpret_cast<STATE *>(mem); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast)
}
// Flush dapp state to disk.
template <typename STATE>
void dapp_flush_state(STATE *state) {
// Flushes state changes made into memory using mmap(2) back to the filesystem.
if (msync(state, sizeof(STATE), MS_SYNC) < 0) {
// Cannot recover from failure here, but report the error if any.
std::ignore =
std::fprintf(stderr, "[dapp] unable to flush state from memory to disk: %s\n", std::strerror(errno));
}
}
////////////////////////////////////////////////////////////////////////////////
// Honeypot application.
constexpr erc20_address ERC20_PORTAL_ADDRESS = {CONFIG_ERC20_PORTAL_ADDRESS};
constexpr erc20_address ERC20_WITHDRAWAL_ADDRESS = {CONFIG_ERC20_WITHDRAWAL_ADDRESS};
constexpr erc20_address ERC20_TOKEN_ADDRESS = {CONFIG_ERC20_TOKEN_ADDRESS};
// Status code sent in as reports for well formed advance requests.
enum class advance_status : uint8_t {
SUCCESS = 0,
INVALID_REQUEST,
DEPOSIT_INVALID_TOKEN,
DEPOSIT_BALANCE_OVERFLOW,
WITHDRAW_NO_FUNDS,
WITHDRAW_VOUCHER_FAILED,
};
// POD for advance reports.
struct [[gnu::packed]] advance_report {
advance_status status{};
};
// POD for inspect reports.
struct [[gnu::packed]] inspect_report {
be256 balance{};
};
// POD for dapp state.
struct [[gnu::packed]] dapp_state {
be256 balance{};
};
// Process an ERC-20 deposit request.
bool process_deposit(cmt_rollup_t *rollup, dapp_state *state, const erc20_deposit &deposit) {
// Check token address.
if (deposit.token_address != ERC20_TOKEN_ADDRESS) {
std::ignore = std::fprintf(stderr, "[dapp] invalid deposit token address\n");
std::ignore = rollup_emit_report(rollup, advance_report{advance_status::DEPOSIT_INVALID_TOKEN});
return false;
}
// Add deposit amount to balance.
be256 new_balance{};
if (!be256_checked_add(new_balance, state->balance, deposit.amount)) {
std::ignore = std::fprintf(stderr, "[dapp] deposit balance overflow\n");
std::ignore = rollup_emit_report(rollup, advance_report{advance_status::DEPOSIT_BALANCE_OVERFLOW});
return false;
}
state->balance = new_balance;
// Flush dapp state to disk, so we can inspect its state from outside.
dapp_flush_state(state);
// Report that operation succeed.
std::ignore = std::fprintf(stderr, "[dapp] successful deposit\n");
std::ignore = rollup_emit_report(rollup, advance_report{advance_status::SUCCESS});
return true;
}
// Process a ERC-20 withdraw request.
bool process_withdraw(cmt_rollup_t *rollup, dapp_state *state) {
// Report an error if the balance is empty.
if (state->balance == be256{}) {
std::ignore = std::fprintf(stderr, "[dapp] no funds to withdraw\n");
std::ignore = rollup_emit_report(rollup, advance_report{advance_status::WITHDRAW_NO_FUNDS});
return false;
}
// Issue a voucher with the entire balance.
const erc20_transfer transfer_payload = encode_erc20_transfer(ERC20_WITHDRAWAL_ADDRESS, state->balance);
if (!rollup_emit_voucher(rollup, ERC20_TOKEN_ADDRESS, transfer_payload)) {
std::ignore = std::fprintf(stderr, "[dapp] unable to issue withdraw voucher\n");
std::ignore = rollup_emit_report(rollup, advance_report{advance_status::WITHDRAW_VOUCHER_FAILED});
return false;
}
// Only zero balance after successful voucher emission.
state->balance = be256{};
// Flush dapp state to disk, so we can inspect its state from outside.
dapp_flush_state(state);
// Report that operation succeed.
std::ignore = std::fprintf(stderr, "[dapp] successful withdrawal\n");
std::ignore = rollup_emit_report(rollup, advance_report{advance_status::SUCCESS});
return true;
}
// Process advance state requests.
bool advance_state(cmt_rollup_t *rollup, dapp_state *state) {
// Read the input.
cmt_rollup_advance_t input{};
const int err = cmt_rollup_read_advance_state(rollup, &input);
if (err < 0) {
std::ignore = std::fprintf(stderr, "[dapp] unable to read advance state: %s\n", std::strerror(-err));
std::ignore = rollup_emit_report(rollup, advance_report{advance_status::INVALID_REQUEST});
return false;
}
// Deposit?
if (input.msg_sender == ERC20_PORTAL_ADDRESS && input.payload.length == sizeof(erc20_deposit)) {
erc20_deposit deposit{};
std::copy_n(static_cast<const uint8_t *>(input.payload.data), sizeof(erc20_deposit),
reinterpret_cast<uint8_t *>(&deposit)); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast)
return process_deposit(rollup, state, deposit);
}
// Withdraw?
if (input.msg_sender == ERC20_WITHDRAWAL_ADDRESS && input.payload.length == 0) {
return process_withdraw(rollup, state);
}
// Invalid request.
std::ignore = std::fprintf(stderr, "[dapp] invalid advance state request\n");
std::ignore = rollup_emit_report(rollup, advance_report{advance_status::INVALID_REQUEST});
return false;
}
// Process inspect state queries.
bool inspect_state(cmt_rollup_t *rollup, dapp_state *state) {
// Inspect balance.
std::ignore = std::fprintf(stderr, "[dapp] inspect balance request\n");
return rollup_emit_report(rollup, inspect_report{state->balance});
}
}; // anonymous namespace
// Application main.
int main() {
cmt_rollup_t rollup{};
// Disable buffering of stderr to avoid dynamic allocations behind the scenes
if (std::setvbuf(stderr, nullptr, _IONBF, 0) != 0) {
std::ignore = std::fprintf(stderr, "[dapp] unable to disable stderr buffering: %s\n", std::strerror(errno));
return -1;
}
// Load dapp state from disk.
auto *state = dapp_load_state<dapp_state>(STATE_BLOCK_DEVICE);
if (state == nullptr) {
std::ignore = std::fprintf(stderr, "[dapp] unable to load dapp state\n");
return -1;
}
// Initialize rollup device.
const int err = cmt_rollup_init(&rollup);
if (err != 0) {
std::ignore = std::fprintf(stderr, "[dapp] unable to initialize rollup device: %s\n", std::strerror(-err));
return -1;
}
// Process requests forever.
std::ignore = std::fprintf(stderr, "[dapp] processing rollup requests...\n");
bool accept_previous_request = true;
while (true) {
// Always continue, despite request failing or not.
accept_previous_request =
rollup_process_next_request(&rollup, state, advance_state, inspect_state, accept_previous_request);
}
// Unreachable code, return is intentionally omitted.
}