Skip to content

Commit dce0736

Browse files
committed
Fix serialization expected lengths and check them in test/fuzzing
1 parent 8a8c75a commit dce0736

File tree

4 files changed

+37
-12
lines changed

4 files changed

+37
-12
lines changed

lightning/src/ln/chan_utils.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ pub struct TxCreationKeys {
320320
/// Broadcaster's Payment Key (which isn't allowed to be spent from for some delay)
321321
pub broadcaster_delayed_payment_key: PublicKey,
322322
}
323-
impl_writeable!(TxCreationKeys, 33*6,
323+
impl_writeable!(TxCreationKeys, 33*5,
324324
{ per_commitment_point, revocation_key, broadcaster_htlc_key, countersignatory_htlc_key, broadcaster_delayed_payment_key });
325325

326326
/// One counterparty's public keys which do not change over the life of a channel.
@@ -427,7 +427,10 @@ pub struct HTLCOutputInCommitment {
427427
pub transaction_output_index: Option<u32>,
428428
}
429429

430-
impl_writeable!(HTLCOutputInCommitment, HTLC_OUTPUT_IN_COMMITMENT_SIZE, {
430+
impl_writeable_len_match!(HTLCOutputInCommitment, {
431+
{ HTLCOutputInCommitment { transaction_output_index: None, .. }, 1 + 8 + 4 + 32 + 1},
432+
{ _, HTLC_OUTPUT_IN_COMMITMENT_SIZE }
433+
}, {
431434
offered,
432435
amount_msat,
433436
cltv_expiry,

lightning/src/ln/msgs.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1396,7 +1396,7 @@ impl Readable for Pong {
13961396

13971397
impl Writeable for UnsignedChannelAnnouncement {
13981398
fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
1399-
w.size_hint(2 + 2*32 + 4*33 + self.features.byte_count() + self.excess_data.len());
1399+
w.size_hint(2 + 32 + 8 + 4*33 + self.features.byte_count() + self.excess_data.len());
14001400
self.features.write(w)?;
14011401
self.chain_hash.write(w)?;
14021402
self.short_channel_id.write(w)?;
@@ -1430,7 +1430,7 @@ impl Readable for UnsignedChannelAnnouncement {
14301430

14311431
impl_writeable_len_match!(ChannelAnnouncement, {
14321432
{ ChannelAnnouncement { contents: UnsignedChannelAnnouncement {ref features, ref excess_data, ..}, .. },
1433-
2 + 2*32 + 4*33 + features.byte_count() + excess_data.len() + 4*64 }
1433+
2 + 32 + 8 + 4*33 + features.byte_count() + excess_data.len() + 4*64 }
14341434
}, {
14351435
node_signature_1,
14361436
node_signature_2,
@@ -1491,8 +1491,8 @@ impl Readable for UnsignedChannelUpdate {
14911491
}
14921492

14931493
impl_writeable_len_match!(ChannelUpdate, {
1494-
{ ChannelUpdate { contents: UnsignedChannelUpdate {ref excess_data, ..}, .. },
1495-
64 + excess_data.len() + 64 }
1494+
{ ChannelUpdate { contents: UnsignedChannelUpdate {ref excess_data, ref htlc_maximum_msat, ..}, .. },
1495+
64 + 64 + excess_data.len() + if let OptionalField::Present(_) = htlc_maximum_msat { 8 } else { 0 } }
14961496
}, {
14971497
signature,
14981498
contents
@@ -1528,7 +1528,7 @@ impl Readable for ErrorMessage {
15281528

15291529
impl Writeable for UnsignedNodeAnnouncement {
15301530
fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
1531-
w.size_hint(64 + 76 + self.features.byte_count() + self.addresses.len()*38 + self.excess_address_data.len() + self.excess_data.len());
1531+
w.size_hint(76 + self.features.byte_count() + self.addresses.len()*38 + self.excess_address_data.len() + self.excess_data.len());
15321532
self.features.write(w)?;
15331533
self.timestamp.write(w)?;
15341534
self.node_id.write(w)?;
@@ -1611,7 +1611,7 @@ impl Readable for UnsignedNodeAnnouncement {
16111611
}
16121612
}
16131613

1614-
impl_writeable_len_match!(NodeAnnouncement, {
1614+
impl_writeable_len_match!(NodeAnnouncement, <=, {
16151615
{ NodeAnnouncement { contents: UnsignedNodeAnnouncement { ref features, ref addresses, ref excess_address_data, ref excess_data, ..}, .. },
16161616
64 + 76 + features.byte_count() + addresses.len()*(NetAddress::MAX_LEN as usize + 1) + excess_address_data.len() + excess_data.len() }
16171617
}, {

lightning/src/util/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ impl Default for ChannelConfig {
223223
}
224224

225225
//Add write and readable traits to channelconfig
226-
impl_writeable!(ChannelConfig, 8+2+1+1, {
226+
impl_writeable!(ChannelConfig, 4+2+1+1, {
227227
fee_proportional_millionths,
228228
cltv_expiry_delta,
229229
announced_channel,

lightning/src/util/ser_macros.rs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,16 @@ macro_rules! impl_writeable {
120120
if $len != 0 {
121121
w.size_hint($len);
122122
}
123+
#[cfg(any(test, feature = "fuzztarget"))]
124+
{
125+
// In tests, assert that the hard-coded length matches the actual one
126+
if $len != 0 {
127+
use util::ser::LengthCalculatingWriter;
128+
let mut len_calc = LengthCalculatingWriter(0);
129+
$( self.$field.write(&mut len_calc)?; )*
130+
assert_eq!(len_calc.0, $len);
131+
}
132+
}
123133
$( self.$field.write(w)?; )*
124134
Ok(())
125135
}
@@ -135,12 +145,21 @@ macro_rules! impl_writeable {
135145
}
136146
}
137147
macro_rules! impl_writeable_len_match {
138-
($st:ident, {$({$m: pat, $l: expr}),*}, {$($field:ident),*}) => {
148+
($st:ident, $cmp: tt, {$({$m: pat, $l: expr}),*}, {$($field:ident),*}) => {
139149
impl Writeable for $st {
140150
fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
141-
w.size_hint(match *self {
151+
let len = match *self {
142152
$($m => $l,)*
143-
});
153+
};
154+
w.size_hint(len);
155+
#[cfg(any(test, feature = "fuzztarget"))]
156+
{
157+
// In tests, assert that the hard-coded length matches the actual one
158+
use util::ser::LengthCalculatingWriter;
159+
let mut len_calc = LengthCalculatingWriter(0);
160+
$( self.$field.write(&mut len_calc)?; )*
161+
assert!(len_calc.0 $cmp len);
162+
}
144163
$( self.$field.write(w)?; )*
145164
Ok(())
146165
}
@@ -153,6 +172,9 @@ macro_rules! impl_writeable_len_match {
153172
})
154173
}
155174
}
175+
};
176+
($st:ident, {$({$m: pat, $l: expr}),*}, {$($field:ident),*}) => {
177+
impl_writeable_len_match!($st, ==, { $({ $m, $l }),* }, { $($field),* });
156178
}
157179
}
158180

0 commit comments

Comments
 (0)