Skip to content

Commit 28a612f

Browse files
yuntaiTheBlueMatt
authored andcommitted
Migrate fuzz router/channel target to Readable
and be more specific about DecodeError::InvalidValue
1 parent 3e89106 commit 28a612f

File tree

4 files changed

+20
-30
lines changed

4 files changed

+20
-30
lines changed

fuzz/fuzz_targets/channel_target.rs

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ use bitcoin::network::serialize::{serialize, BitcoinHash};
1010
use lightning::ln::channel::{Channel, ChannelKeys};
1111
use lightning::ln::channelmanager::{HTLCFailReason, PendingHTLCStatus};
1212
use lightning::ln::msgs;
13-
use lightning::ln::msgs::{MsgDecodable, ErrorAction};
13+
use lightning::ln::msgs::{ErrorAction};
1414
use lightning::chain::chaininterface::{FeeEstimator, ConfirmationTarget};
1515
use lightning::chain::transaction::OutPoint;
1616
use lightning::util::reset_rng_state;
1717
use lightning::util::logger::Logger;
18+
use lightning::util::ser::{Readable, Reader};
1819

1920
mod utils;
2021

@@ -119,8 +120,9 @@ pub fn do_test(data: &[u8]) {
119120
}
120121

121122
macro_rules! decode_msg {
122-
($MsgType: path, $len: expr) => {
123-
match <($MsgType)>::decode(get_slice!($len)) {
123+
($MsgType: path, $len: expr) => {{
124+
let mut reader = Reader::new(::std::io::Cursor::new(get_slice!($len)));
125+
match <($MsgType)>::read(&mut reader) {
124126
Ok(msg) => msg,
125127
Err(e) => match e {
126128
msgs::DecodeError::UnknownRealmByte => return,
@@ -131,11 +133,11 @@ pub fn do_test(data: &[u8]) {
131133
msgs::DecodeError::ExtraAddressesPerType => return,
132134
msgs::DecodeError::BadLengthDescriptor => return,
133135
msgs::DecodeError::ShortRead => panic!("We picked the length..."),
134-
msgs::DecodeError::InvalidValue => panic!("Writeable not used yet..."),
135-
msgs::DecodeError::Io(_) => panic!("Writeable not used yet..."),
136+
msgs::DecodeError::InvalidValue => panic!("Should not happen with p2p message decoding"),
137+
msgs::DecodeError::Io(e) => panic!(format!("{}", e)),
136138
}
137139
}
138-
}
140+
}}
139141
}
140142

141143
macro_rules! decode_msg_with_len16 {
@@ -145,21 +147,7 @@ pub fn do_test(data: &[u8]) {
145147
Some(slice) => slice,
146148
None => return,
147149
}[$begin_len..$begin_len + 2]);
148-
match <($MsgType)>::decode(get_slice!($begin_len as usize + 2 + (extra_len as usize)*$factor)) {
149-
Ok(msg) => msg,
150-
Err(e) => match e {
151-
msgs::DecodeError::UnknownRealmByte => return,
152-
msgs::DecodeError::UnknownRequiredFeature => return,
153-
msgs::DecodeError::BadPublicKey => return,
154-
msgs::DecodeError::BadSignature => return,
155-
msgs::DecodeError::BadText => return,
156-
msgs::DecodeError::ExtraAddressesPerType => return,
157-
msgs::DecodeError::BadLengthDescriptor => return,
158-
msgs::DecodeError::ShortRead => panic!("We picked the length..."),
159-
msgs::DecodeError::InvalidValue => panic!("Writeable not used yet..."),
160-
msgs::DecodeError::Io(_) => panic!("Writeable not used yet..."),
161-
}
162-
}
150+
decode_msg!($MsgType, $begin_len as usize + 2 + (extra_len as usize)*$factor)
163151
}
164152
}
165153
}

fuzz/fuzz_targets/router_target.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ use bitcoin::blockdata::script::{Script, Builder};
88
use lightning::chain::chaininterface::{ChainError,ChainWatchInterface, ChainListener};
99
use lightning::ln::channelmanager::ChannelDetails;
1010
use lightning::ln::msgs;
11-
use lightning::ln::msgs::{MsgDecodable, RoutingMessageHandler};
11+
use lightning::ln::msgs::{RoutingMessageHandler};
1212
use lightning::ln::router::{Router, RouteHint};
1313
use lightning::util::reset_rng_state;
1414
use lightning::util::logger::Logger;
15+
use lightning::util::ser::{Reader, Readable};
1516

1617
use secp256k1::key::PublicKey;
1718
use secp256k1::Secp256k1;
@@ -119,8 +120,9 @@ pub fn do_test(data: &[u8]) {
119120
}
120121

121122
macro_rules! decode_msg {
122-
($MsgType: path, $len: expr) => {
123-
match <($MsgType)>::decode(get_slice!($len)) {
123+
($MsgType: path, $len: expr) => {{
124+
let mut reader = Reader::new(::std::io::Cursor::new(get_slice!($len)));
125+
match <($MsgType)>::read(&mut reader) {
124126
Ok(msg) => msg,
125127
Err(e) => match e {
126128
msgs::DecodeError::UnknownRealmByte => return,
@@ -131,11 +133,11 @@ pub fn do_test(data: &[u8]) {
131133
msgs::DecodeError::ExtraAddressesPerType => return,
132134
msgs::DecodeError::BadLengthDescriptor => return,
133135
msgs::DecodeError::ShortRead => panic!("We picked the length..."),
134-
msgs::DecodeError::InvalidValue => panic!("Writeable not used yet..."),
135-
msgs::DecodeError::Io(_) => panic!("Writeable not used yet..."),
136+
msgs::DecodeError::InvalidValue => panic!("Should not happen with p2p message decoding"),
137+
msgs::DecodeError::Io(e) => panic!(format!("{}", e)),
136138
}
137139
}
138-
}
140+
}}
139141
}
140142

141143
macro_rules! decode_msg_with_len16 {

src/ln/msgs.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ pub enum DecodeError {
4646
BadLengthDescriptor,
4747
/// Error from std::io
4848
Io(::std::io::Error),
49-
/// Invalid value found when decoding
49+
/// 1 or 0 is not found for boolean value
5050
InvalidValue,
5151
}
5252
pub trait MsgDecodable: Sized {
@@ -525,7 +525,7 @@ impl Error for DecodeError {
525525
DecodeError::ExtraAddressesPerType => "More than one address of a single type",
526526
DecodeError::BadLengthDescriptor => "A length descriptor in the packet didn't describe the later data correctly",
527527
DecodeError::Io(ref e) => e.description(),
528-
DecodeError::InvalidValue => "Invalid value in the bytes",
528+
DecodeError::InvalidValue => "0 or 1 is not found for boolean",
529529
}
530530
}
531531
}

src/ln/peer_handler.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ impl<Descriptor: SocketDescriptor> PeerManager<Descriptor> {
364364
},
365365
msgs::DecodeError::BadLengthDescriptor => return Err(PeerHandleError{ no_connection_possible: false }),
366366
msgs::DecodeError::Io(_) => return Err(PeerHandleError{ no_connection_possible: false }),
367-
msgs::DecodeError::InvalidValue => return Err(PeerHandleError{ no_connection_possible: false }),
367+
msgs::DecodeError::InvalidValue => panic!("should not happen with message decoding"),
368368
}
369369
}
370370
};

0 commit comments

Comments
 (0)