Skip to content

Implement DisconnectPeer event + test #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion fuzz/fuzz_targets/msg_targets/gen_target.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
for target in CommitmentSigned FundingCreated FundingLocked FundingSigned OpenChannel RevokeAndACK Shutdown UpdateAddHTLC UpdateFailHTLC UpdateFailMalformedHTLC UpdateFee UpdateFulfillHTLC AcceptChannel ClosingSigned; do
for target in CommitmentSigned FundingCreated FundingLocked FundingSigned OpenChannel RevokeAndACK Shutdown UpdateAddHTLC UpdateFailHTLC UpdateFailMalformedHTLC UpdateFee UpdateFulfillHTLC AcceptChannel ClosingSigned ErrorMessage; do
tn=$(echo $target | sed 's/\([a-z0-9]\)\([A-Z]\)/\1_\L\2/g')
fn=msg_$(echo $tn | tr '[:upper:]' '[:lower:]')_target.rs
cat msg_target_template.txt | sed s/MSG_TARGET/$target/ > $fn
Expand Down
48 changes: 48 additions & 0 deletions fuzz/fuzz_targets/msg_targets/msg_error_message_target.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// This file is auto-generated by gen_target.sh based on msg_target_template.txt
// To modify it, modify msg_target_template.txt and run gen_target.sh instead.

extern crate lightning;

use lightning::ln::msgs;
use lightning::util::reset_rng_state;

use lightning::ln::msgs::{MsgEncodable, MsgDecodable};

mod utils;

#[inline]
pub fn do_test(data: &[u8]) {
reset_rng_state();
test_msg!(msgs::ErrorMessage, data);
}

#[cfg(feature = "afl")]
extern crate afl;
#[cfg(feature = "afl")]
fn main() {
afl::read_stdio_bytes(|data| {
do_test(&data);
});
}

#[cfg(feature = "honggfuzz")]
#[macro_use] extern crate honggfuzz;
#[cfg(feature = "honggfuzz")]
fn main() {
loop {
fuzz!(|data| {
do_test(data);
});
}
}

#[cfg(test)]
mod tests {
use utils::extend_vec_from_hex;
#[test]
fn duplicate_crash() {
let mut a = Vec::new();
extend_vec_from_hex("00", &mut a);
super::do_test(&a);
}
}
41 changes: 41 additions & 0 deletions src/ln/msgs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ pub struct Init {
pub local_features: LocalFeatures,
}

pub struct ErrorMessage {
pub channel_id: [u8; 32],
pub data: String,
}

pub struct Ping {
pub ponglen: u16,
pub byteslen: u16,
Expand Down Expand Up @@ -375,6 +380,10 @@ pub enum ErrorAction {
DisconnectPeer,
/// The peer did something harmless that we weren't able to process, just log and ignore
IgnoreError,
/// The peer did something incorrect. Tell them.
SendErrorMessage {
msg: ErrorMessage
},
}

pub struct HandleError { //TODO: rename me
Expand Down Expand Up @@ -1562,3 +1571,35 @@ impl MsgEncodable for OnionErrorPacket {
res
}
}

impl MsgEncodable for ErrorMessage {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a TODO for MsgDecodable for ErrorMessage? We should obviously also support receiving them.

fn encode(&self) -> Vec<u8> {
let mut res = Vec::with_capacity(34 + self.data.len());
res.extend_from_slice(&self.channel_id);
res.extend_from_slice(&byte_utils::be16_to_array(self.data.len() as u16));
res.extend_from_slice(&self.data.as_bytes());
res
}
}

impl MsgDecodable for ErrorMessage {
fn decode(v: &[u8]) -> Result<Self,DecodeError> {
if v.len() < 34 {
return Err(DecodeError::WrongLength);
}
let len = byte_utils::slice_to_be16(&v[33..34]);
let mut data = String::new();
if len > 0 {
data = String::from_utf8(v[35..len as usize].to_vec()).unwrap();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unwrap() here can panic if the message contains non-utf-8 crap.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, some of the indexing is off in the decoder here.

if len != data.len() as u16 {
return Err(DecodeError::WrongLength);
}
}
let mut channel_id = [0; 32];
channel_id[..].copy_from_slice(&v[0..32]);
Ok(Self {
channel_id,
data: data,
})
}
}
4 changes: 4 additions & 0 deletions src/ln/peer_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,10 @@ impl<Descriptor: SocketDescriptor> PeerManager<Descriptor> {
msgs::ErrorAction::IgnoreError => {
continue;
},
msgs::ErrorAction::SendErrorMessage { msg } => {
encode_and_send_msg!(msg, 17);
continue;
},
}
} else {
return Err(PeerHandleError{ no_connection_possible: false });
Expand Down