Skip to content

Commit b315a29

Browse files
rustyrussellcdecker
authored andcommitted
wire: perform corruption test on init_msg.
Fixes: #3301 Signed-off-by: Rusty Russell <[email protected]>
1 parent eb6a768 commit b315a29

File tree

1 file changed

+38
-7
lines changed

1 file changed

+38
-7
lines changed

wire/test/run-peer-wire.c

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -833,11 +833,31 @@ static bool error_eq(const struct msg_error *a,
833833
static bool init_eq(const struct msg_init *a,
834834
const struct msg_init *b)
835835
{
836+
if (!eq_var(a, b, globalfeatures) || !eq_var(a, b, localfeatures))
837+
return false;
838+
839+
/* Both or neither */
840+
if (!a->tlvs != !b->tlvs)
841+
return false;
842+
843+
if (!a->tlvs)
844+
return true;
845+
846+
/* Both or neither */
847+
if (!a->tlvs->networks != !b->tlvs->networks)
848+
return false;
849+
850+
if (!a->tlvs->networks)
851+
return true;
852+
853+
if (tal_count(a->tlvs->networks->chains)
854+
!= tal_count(b->tlvs->networks->chains))
855+
return false;
836856
for (size_t i = 0; i < tal_count(a->tlvs->networks->chains); i++)
837-
assert(bitcoin_blkid_eq(&a->tlvs->networks->chains[i],
838-
&b->tlvs->networks->chains[i]));
839-
return eq_var(a, b, globalfeatures)
840-
&& eq_var(a, b, localfeatures);
857+
if (!bitcoin_blkid_eq(&a->tlvs->networks->chains[i],
858+
&b->tlvs->networks->chains[i]))
859+
return false;
860+
return true;
841861
}
842862

843863
static bool update_fee_eq(const struct msg_update_fee *a,
@@ -911,19 +931,29 @@ static bool node_announcement_eq(const struct msg_node_announcement *a,
911931
}
912932

913933
/* Try flipping each bit, try running short. */
914-
#define test_corruption(a, b, type) \
934+
#define test_bitflip_and_short(a, b, type, short_decodefail) \
915935
for (i = 0; i < tal_count(msg) * 8; i++) { \
916936
msg[i / 8] ^= (1 << (i%8)); \
917-
b = fromwire_struct_##type(ctx, msg); \
937+
b = fromwire_struct_##type(ctx, msg); \
918938
assert(!b || !type##_eq(a, b)); \
919939
msg[i / 8] ^= (1 << (i%8)); \
920940
} \
921941
for (i = 0; i < tal_count(msg); i++) { \
922942
u8 *trunc = tal_dup_arr(ctx, u8, msg, i, 0); \
923943
b = fromwire_struct_##type(ctx, trunc); \
924-
assert(!b); \
944+
if (short_decodefail) \
945+
assert(!b); \
946+
else \
947+
assert(!b || !type##_eq(a, b)); \
925948
}
926949

950+
#define test_corruption(a, b, type) \
951+
test_bitflip_and_short(a, b, type, true)
952+
953+
/* If it has a tlv at the end, truncated may still parse! */
954+
#define test_corruption_tlv(a, b, type) \
955+
test_bitflip_and_short(a, b, type, false)
956+
927957
int main(void)
928958
{
929959
setup_locale();
@@ -1046,6 +1076,7 @@ int main(void)
10461076
msg = towire_struct_init(ctx, &init);
10471077
init2 = fromwire_struct_init(ctx, msg);
10481078
assert(init_eq(&init, init2));
1079+
test_corruption_tlv(&init, init2, init);
10491080
}
10501081

10511082
memset(&uf, 2, sizeof(uf));

0 commit comments

Comments
 (0)