|
| 1 | +/* This is a fuzz test for `peer_init_received()`, which is responsible for |
| 2 | + * handling BOLT #1 `init` messages. All this test essentially does in encode |
| 3 | + * the fuzzer's input as the payload for a cryptographically valid 'init' |
| 4 | + * message and pass it to this handler. |
| 5 | + */ |
| 6 | +#include "config.h" |
| 7 | +#include <ccan/ccan/io/io.h> |
| 8 | +#include <ccan/ccan/str/hex/hex.h> |
| 9 | +#include <common/setup.h> |
| 10 | +#include <common/utils.h> |
| 11 | +#include <fcntl.h> |
| 12 | +#include <tests/fuzz/libfuzz.h> |
| 13 | + |
| 14 | +static struct io_plan *test_write(struct io_conn *conn, const void *data, |
| 15 | + size_t len, struct io_plan *(*next)(struct io_conn *, void *), void *arg); |
| 16 | + |
| 17 | +static struct io_plan *test_read(struct io_conn *conn, void *data, size_t len, |
| 18 | + struct io_plan *(*next)(struct io_conn *, void *), void *arg); |
| 19 | + |
| 20 | +#undef io_write |
| 21 | +#define io_write(conn, data, len, cb, cb_arg) \ |
| 22 | + test_write((conn), \ |
| 23 | + (data), \ |
| 24 | + (len), \ |
| 25 | + (struct io_plan *(*)(struct io_conn *, void *))(cb), \ |
| 26 | + (void *)(cb_arg)) |
| 27 | + |
| 28 | +#undef io_read |
| 29 | +#define io_read(conn, data, len, cb, cb_arg) \ |
| 30 | + test_read((conn), \ |
| 31 | + (data), \ |
| 32 | + (len), \ |
| 33 | + (struct io_plan *(*)(struct io_conn *, void *))(cb), \ |
| 34 | + (void *)(cb_arg)) |
| 35 | + |
| 36 | + #include "../../connectd/peer_exchange_initmsg.c" |
| 37 | + |
| 38 | +/* MOCKS START */ |
| 39 | +bool address_routable(const struct wireaddr *wireaddr UNNEEDED, |
| 40 | + bool allow_localhost UNNEEDED) |
| 41 | +{ return false; } |
| 42 | +struct io_plan *peer_connected(struct io_conn *conn UNNEEDED, |
| 43 | + struct daemon *daemon UNNEEDED, |
| 44 | + const struct node_id *id UNNEEDED, |
| 45 | + const struct wireaddr_internal *addr UNNEEDED, |
| 46 | + const struct wireaddr *remote_addr UNNEEDED, |
| 47 | + struct crypto_state *cs UNNEEDED, |
| 48 | + const u8 *their_features TAKES UNNEEDED, |
| 49 | + enum is_websocket is_websocket UNNEEDED, |
| 50 | + struct timemono starttime, |
| 51 | + bool incoming UNNEEDED) |
| 52 | +{ if (taken(their_features)) |
| 53 | + tal_steal(tmpctx, their_features); |
| 54 | + return io_close(conn); |
| 55 | +} |
| 56 | +/* MOCKS END */ |
| 57 | + |
| 58 | +/* These functions are only called when `peer_init_received()` |
| 59 | + * enounters an error or an unknown message. Simply return |
| 60 | + * when that happens. |
| 61 | + */ |
| 62 | +static struct io_plan * |
| 63 | +test_write(struct io_conn *conn, const void *data, size_t len, |
| 64 | + struct io_plan *(*next)(struct io_conn *, void *), void *arg) |
| 65 | +{ |
| 66 | + return io_close(conn); |
| 67 | +} |
| 68 | + |
| 69 | +static struct io_plan * |
| 70 | +test_read(struct io_conn *conn, void *data, size_t len, |
| 71 | + struct io_plan *(*next)(struct io_conn *, void *), void *arg) |
| 72 | +{ |
| 73 | + return io_close(conn); |
| 74 | +} |
| 75 | + |
| 76 | +static struct secret secret_from_hex(const char *hex) |
| 77 | +{ |
| 78 | + struct secret secret; |
| 79 | + hex += 2; |
| 80 | + if (!hex_decode(hex, strlen(hex), &secret, sizeof(secret))) |
| 81 | + abort(); |
| 82 | + return secret; |
| 83 | +} |
| 84 | + |
| 85 | +/* Encodes the given message. Stores the decrypting crypto_state in `cs`. */ |
| 86 | +static u8 *encode_msg(const tal_t *ctx, const void *message, size_t size, struct crypto_state *cs) |
| 87 | +{ |
| 88 | + struct crypto_state cs_out, cs_in; |
| 89 | + struct secret sk, rk, ck; |
| 90 | + u16 len; |
| 91 | + |
| 92 | + void *msg = tal_dup_arr(ctx, char, message, size, 0); |
| 93 | + |
| 94 | + ck = secret_from_hex("0x919219dbb2920afa8db80f9a51787a840bcf111ed8d588caf9ab4be716e42b01"); |
| 95 | + sk = secret_from_hex("0x969ab31b4d288cedf6218839b27a3e2140827047f2c0f01bf5c04435d43511a9"); |
| 96 | + rk = secret_from_hex("0xbb9020b8965f4df047e07f955f3c4b88418984aadc5cdb35096b9ea8fa5c3442"); |
| 97 | + |
| 98 | + cs_out.sn = cs_out.rn = cs_in.sn = cs_in.rn = 0; |
| 99 | + cs_out.sk = cs_in.rk = sk; |
| 100 | + cs_out.rk = cs_in.sk = rk; |
| 101 | + cs_out.s_ck = cs_out.r_ck = cs_in.s_ck = cs_in.r_ck = ck; |
| 102 | + |
| 103 | + u8 *encoded_msg = cryptomsg_encrypt_msg(ctx, &cs_out, msg); |
| 104 | + |
| 105 | + if (!cryptomsg_decrypt_header(&cs_in, encoded_msg, &len)) |
| 106 | + abort(); |
| 107 | + |
| 108 | + /* Trim header */ |
| 109 | + memmove(encoded_msg, encoded_msg + CRYPTOMSG_HDR_SIZE, |
| 110 | + tal_bytelen(encoded_msg) - CRYPTOMSG_HDR_SIZE); |
| 111 | + tal_resize(&encoded_msg, tal_bytelen(encoded_msg) - CRYPTOMSG_HDR_SIZE); |
| 112 | + |
| 113 | + *cs = cs_in; |
| 114 | + return encoded_msg; |
| 115 | +} |
| 116 | + |
| 117 | +static struct io_plan *do_nothing(struct io_conn *conn, void *side) |
| 118 | +{ |
| 119 | + return io_close(conn); |
| 120 | +} |
| 121 | + |
| 122 | +void init(int *argc, char ***argv) |
| 123 | +{ |
| 124 | + chainparams = chainparams_for_network("bitcoin"); |
| 125 | + /* Don't call this if we're in unit-test mode, as libfuzz.c does it */ |
| 126 | + if (!tmpctx) |
| 127 | + common_setup("fuzzer"); |
| 128 | + int devnull = open("/dev/null", O_WRONLY); |
| 129 | + status_setup_sync(devnull); |
| 130 | + dev_towire_allow_invalid_node_id = true; |
| 131 | +} |
| 132 | + |
| 133 | +void run(const uint8_t *data, size_t size) |
| 134 | +{ |
| 135 | + struct io_conn *conn; |
| 136 | + struct crypto_state cs; |
| 137 | + struct early_peer *peer; |
| 138 | + u8 *encoded_msg; |
| 139 | + const struct node_id id = {{ |
| 140 | + 02, |
| 141 | + 00, 00, 00, 00, |
| 142 | + 00, 00, 00, 00, |
| 143 | + 00, 00, 00, 00, |
| 144 | + 00, 00, 00, 00, |
| 145 | + 00, 00, 00, 00, |
| 146 | + 00, 00, 00, 00, |
| 147 | + 00, 00, 00, 00, |
| 148 | + 00, 00, 00, 00 }}; |
| 149 | + |
| 150 | + encoded_msg = encode_msg(tmpctx, data, size, &cs); |
| 151 | + peer = tal(tmpctx, struct early_peer); |
| 152 | + peer->cs = cs; |
| 153 | + peer->msg = encoded_msg; |
| 154 | + peer->incoming = true; |
| 155 | + peer->daemon = talz(tmpctx, struct daemon); |
| 156 | + peer->timeout = NULL; |
| 157 | + peer->id = id; |
| 158 | + |
| 159 | + conn = io_new_conn(tmpctx, -1, do_nothing, NULL); |
| 160 | + peer_init_received(conn, peer); |
| 161 | + |
| 162 | + clean_tmpctx(); |
| 163 | +} |
0 commit comments