Skip to content

Commit a0045af

Browse files
committed
common/htlc_wire: add towire/fromwire helpers for wrapped tlv streams.
And make sure we check the length properly in fromwire! Signed-off-by: Rusty Russell <[email protected]>
1 parent 8269d8a commit a0045af

File tree

1 file changed

+38
-34
lines changed

1 file changed

+38
-34
lines changed

common/htlc_wire.c

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,18 @@ struct existing_htlc *new_existing_htlc(const tal_t *ctx,
7878
return existing;
7979
}
8080

81+
static void towire_len_and_tlvstream(u8 **pptr, struct tlv_field *extra_tlvs)
82+
{
83+
/* Making a copy is a bit awful, but it's the easiest way to
84+
* get the length */
85+
u8 *tmp_pptr = tal_arr(tmpctx, u8, 0);
86+
towire_tlvstream_raw(&tmp_pptr, extra_tlvs);
87+
88+
assert(tal_bytelen(tmp_pptr) == (u16)tal_bytelen(tmp_pptr));
89+
towire_u16(pptr, tal_bytelen(tmp_pptr));
90+
towire_u8_array(pptr, tmp_pptr, tal_bytelen(tmp_pptr));
91+
}
92+
8193
/* FIXME: We could adapt tools/generate-wire.py to generate structures
8294
* and code like this. */
8395
void towire_added_htlc(u8 **pptr, const struct added_htlc *added)
@@ -94,13 +106,8 @@ void towire_added_htlc(u8 **pptr, const struct added_htlc *added)
94106
} else
95107
towire_bool(pptr, false);
96108
if (added->extra_tlvs) {
97-
u8 *tmp_pptr = tal_arr(tmpctx, u8, 0);
98-
towire_tlvstream_raw(&tmp_pptr, added->extra_tlvs);
99-
100109
towire_bool(pptr, true);
101-
towire_u16(pptr, tal_bytelen(tmp_pptr));
102-
towire_u8_array(pptr, tmp_pptr,
103-
tal_bytelen(tmp_pptr));
110+
towire_len_and_tlvstream(pptr, added->extra_tlvs);
104111
} else
105112
towire_bool(pptr, false);
106113
towire_bool(pptr, added->fail_immediate);
@@ -131,13 +138,8 @@ void towire_existing_htlc(u8 **pptr, const struct existing_htlc *existing)
131138
} else
132139
towire_bool(pptr, false);
133140
if (existing->extra_tlvs) {
134-
u8 *tmp_pptr = tal_arr(tmpctx, u8, 0);
135-
towire_tlvstream_raw(&tmp_pptr, existing->extra_tlvs);
136-
137141
towire_bool(pptr, true);
138-
towire_u16(pptr, tal_bytelen(tmp_pptr));
139-
towire_u8_array(pptr, tmp_pptr,
140-
tal_bytelen(tmp_pptr));
142+
towire_len_and_tlvstream(pptr, existing->extra_tlvs);
141143
} else
142144
towire_bool(pptr, false);
143145
}
@@ -192,6 +194,28 @@ void towire_shachain(u8 **pptr, const struct shachain *shachain)
192194
}
193195
}
194196

197+
static struct tlv_field *fromwire_len_and_tlvstream(const tal_t *ctx,
198+
const u8 **cursor, size_t *max)
199+
{
200+
struct tlv_field *tlvs = tal_arr(ctx, struct tlv_field, 0);
201+
size_t len = fromwire_u16(cursor, max);
202+
203+
/* Subtle: we are not using fromwire_tal_arrn here, which
204+
* would do this. */
205+
if (len > *max) {
206+
fromwire_fail(cursor, max);
207+
return NULL;
208+
}
209+
210+
/* NOTE: We might consider to be more strict and only allow for
211+
* known tlv types from the tlvs_tlv_update_add_htlc_tlvs
212+
* record. */
213+
if (!fromwire_tlv(cursor, &len, NULL, 0, cast_const(void *, ctx),
214+
&tlvs, FROMWIRE_TLV_ANY_TYPE, NULL, NULL))
215+
return tal_free(tlvs);
216+
return tlvs;
217+
}
218+
195219
void fromwire_added_htlc(const u8 **cursor, size_t *max,
196220
struct added_htlc *added)
197221
{
@@ -207,17 +231,7 @@ void fromwire_added_htlc(const u8 **cursor, size_t *max,
207231
} else
208232
added->path_key = NULL;
209233
if (fromwire_bool(cursor, max)) {
210-
size_t tlv_len = fromwire_u16(cursor, max);
211-
/* NOTE: We might consider to be more strict and only allow for
212-
* known tlv types from the tlvs_tlv_update_add_htlc_tlvs
213-
* record. */
214-
const u64 *allowed = cast_const(u64 *, FROMWIRE_TLV_ANY_TYPE);
215-
added->extra_tlvs = tal_arr(added, struct tlv_field, 0);
216-
if (!fromwire_tlv(cursor, &tlv_len, NULL, 0, added,
217-
&added->extra_tlvs, allowed, NULL, NULL)) {
218-
tal_free(added->extra_tlvs);
219-
added->extra_tlvs = NULL;
220-
}
234+
added->extra_tlvs = fromwire_len_and_tlvstream(added, cursor, max);
221235
} else
222236
added->extra_tlvs = NULL;
223237
added->fail_immediate = fromwire_bool(cursor, max);
@@ -250,17 +264,7 @@ struct existing_htlc *fromwire_existing_htlc(const tal_t *ctx,
250264
} else
251265
existing->path_key = NULL;
252266
if (fromwire_bool(cursor, max)) {
253-
size_t tlv_len = fromwire_u16(cursor, max);
254-
/* NOTE: We might consider to be more strict and only allow for
255-
* known tlv types from the tlvs_tlv_update_add_htlc_tlvs
256-
* record. */
257-
const u64 *allowed = cast_const(u64 *, FROMWIRE_TLV_ANY_TYPE);
258-
existing->extra_tlvs = tal_arr(existing, struct tlv_field, 0);
259-
if (!fromwire_tlv(cursor, &tlv_len, NULL, 0, existing,
260-
&existing->extra_tlvs, allowed, NULL, NULL)) {
261-
tal_free(existing->extra_tlvs);
262-
existing->extra_tlvs = NULL;
263-
}
267+
existing->extra_tlvs = fromwire_len_and_tlvstream(existing, cursor, max);
264268
} else
265269
existing->extra_tlvs = NULL;
266270
return existing;

0 commit comments

Comments
 (0)