Skip to content

Commit fdfe483

Browse files
author
Antoine Riard
committed
Implement ErrorMessage msg and ErrorAction::SendErrorMessage + fuzz test
1 parent 12583b6 commit fdfe483

File tree

4 files changed

+94
-1
lines changed

4 files changed

+94
-1
lines changed

fuzz/fuzz_targets/msg_targets/gen_target.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
for target in CommitmentSigned FundingCreated FundingLocked FundingSigned OpenChannel RevokeAndACK Shutdown UpdateAddHTLC UpdateFailHTLC UpdateFailMalformedHTLC UpdateFee UpdateFulfillHTLC AcceptChannel ClosingSigned; do
1+
for target in CommitmentSigned FundingCreated FundingLocked FundingSigned OpenChannel RevokeAndACK Shutdown UpdateAddHTLC UpdateFailHTLC UpdateFailMalformedHTLC UpdateFee UpdateFulfillHTLC AcceptChannel ClosingSigned ErrorMessage; do
22
tn=$(echo $target | sed 's/\([a-z0-9]\)\([A-Z]\)/\1_\L\2/g')
33
fn=msg_$(echo $tn | tr '[:upper:]' '[:lower:]')_target.rs
44
cat msg_target_template.txt | sed s/MSG_TARGET/$target/ > $fn
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// This file is auto-generated by gen_target.sh based on msg_target_template.txt
2+
// To modify it, modify msg_target_template.txt and run gen_target.sh instead.
3+
4+
extern crate lightning;
5+
6+
use lightning::ln::msgs;
7+
use lightning::util::reset_rng_state;
8+
9+
use lightning::ln::msgs::{MsgEncodable, MsgDecodable};
10+
11+
mod utils;
12+
13+
#[inline]
14+
pub fn do_test(data: &[u8]) {
15+
reset_rng_state();
16+
test_msg!(msgs::ErrorMessage, data);
17+
}
18+
19+
#[cfg(feature = "afl")]
20+
extern crate afl;
21+
#[cfg(feature = "afl")]
22+
fn main() {
23+
afl::read_stdio_bytes(|data| {
24+
do_test(&data);
25+
});
26+
}
27+
28+
#[cfg(feature = "honggfuzz")]
29+
#[macro_use] extern crate honggfuzz;
30+
#[cfg(feature = "honggfuzz")]
31+
fn main() {
32+
loop {
33+
fuzz!(|data| {
34+
do_test(data);
35+
});
36+
}
37+
}
38+
39+
#[cfg(test)]
40+
mod tests {
41+
use utils::extend_vec_from_hex;
42+
#[test]
43+
fn duplicate_crash() {
44+
let mut a = Vec::new();
45+
extend_vec_from_hex("00", &mut a);
46+
super::do_test(&a);
47+
}
48+
}

src/ln/msgs.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,11 @@ pub struct Init {
138138
pub local_features: LocalFeatures,
139139
}
140140

141+
pub struct ErrorMessage {
142+
pub channel_id: [u8; 32],
143+
pub data: String,
144+
}
145+
141146
pub struct Ping {
142147
pub ponglen: u16,
143148
pub byteslen: u16,
@@ -375,6 +380,10 @@ pub enum ErrorAction {
375380
DisconnectPeer,
376381
/// The peer did something harmless that we weren't able to process, just log and ignore
377382
IgnoreError,
383+
/// The peer did something incorrect. Tell them.
384+
SendErrorMessage {
385+
msg: ErrorMessage
386+
},
378387
}
379388

380389
pub struct HandleError { //TODO: rename me
@@ -1562,3 +1571,35 @@ impl MsgEncodable for OnionErrorPacket {
15621571
res
15631572
}
15641573
}
1574+
1575+
impl MsgEncodable for ErrorMessage {
1576+
fn encode(&self) -> Vec<u8> {
1577+
let mut res = Vec::with_capacity(34 + self.data.len());
1578+
res.extend_from_slice(&self.channel_id);
1579+
res.extend_from_slice(&byte_utils::be16_to_array(self.data.len() as u16));
1580+
res.extend_from_slice(&self.data.as_bytes());
1581+
res
1582+
}
1583+
}
1584+
1585+
impl MsgDecodable for ErrorMessage {
1586+
fn decode(v: &[u8]) -> Result<Self,DecodeError> {
1587+
if v.len() < 34 {
1588+
return Err(DecodeError::WrongLength);
1589+
}
1590+
let len = byte_utils::slice_to_be16(&v[33..34]);
1591+
let mut data = String::new();
1592+
if len > 0 {
1593+
data = String::from_utf8(v[35..len as usize].to_vec()).unwrap();
1594+
if len != data.len() as u16 {
1595+
return Err(DecodeError::WrongLength);
1596+
}
1597+
}
1598+
let mut channel_id = [0; 32];
1599+
channel_id[..].copy_from_slice(&v[0..32]);
1600+
Ok(Self {
1601+
channel_id,
1602+
data: data,
1603+
})
1604+
}
1605+
}

src/ln/peer_handler.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,10 @@ impl<Descriptor: SocketDescriptor> PeerManager<Descriptor> {
302302
msgs::ErrorAction::IgnoreError => {
303303
continue;
304304
},
305+
msgs::ErrorAction::SendErrorMessage { msg } => {
306+
encode_and_send_msg!(msg, 17);
307+
continue;
308+
},
305309
}
306310
} else {
307311
return Err(PeerHandleError{ no_connection_possible: false });

0 commit comments

Comments
 (0)