Skip to content

Commit 7fd294d

Browse files
authored
Merge pull request #285 from TheBlueMatt/2019-01-fuzz-cleanups
Trivial cleanups in full_stack_target/fuzztarget
2 parents 301f91e + d2ae344 commit 7fd294d

File tree

4 files changed

+25
-2
lines changed

4 files changed

+25
-2
lines changed

fuzz/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ cargo-fuzz = true
1313
[features]
1414
afl_fuzz = ["afl"]
1515
honggfuzz_fuzz = ["honggfuzz"]
16+
libfuzzer_fuzz = ["libfuzzer-sys"]
1617

1718
[dependencies]
1819
afl = { version = "0.4", optional = true }
@@ -22,6 +23,7 @@ bitcoin_hashes = { git = "https://github.com/TheBlueMatt/bitcoin_hashes", branch
2223
hex = "0.3"
2324
honggfuzz = { version = "0.5", optional = true }
2425
secp256k1 = { version = "0.11", features=["fuzztarget"] }
26+
libfuzzer-sys = { git = "https://github.com/rust-fuzz/libfuzzer-sys.git", optional = true }
2527

2628
[build-dependencies]
2729
cc = "1.0"

fuzz/fuzz_targets/full_stack_target.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
//! Test that no series of bytes received over the wire/connections created/payments sent can
2+
//! result in a crash. We do this by standing up a node and then reading bytes from input to denote
3+
//! actions such as creating new inbound/outbound connections, bytes to be read from a connection,
4+
//! or payments to send/ways to handle events generated.
5+
//! This test has been very useful, though due to its complexity good starting inputs are critical.
6+
7+
//Uncomment this for libfuzzer builds:
8+
//#![no_main]
9+
110
extern crate bitcoin;
211
extern crate bitcoin_hashes;
312
extern crate lightning;
@@ -9,11 +18,12 @@ use bitcoin::blockdata::script::{Builder, Script};
918
use bitcoin::blockdata::opcodes;
1019
use bitcoin::consensus::encode::deserialize;
1120
use bitcoin::network::constants::Network;
12-
use bitcoin::util::hash::{BitcoinHash, Sha256dHash, Hash160};
21+
use bitcoin::util::hash::{BitcoinHash, Sha256dHash};
1322

1423
use bitcoin_hashes::Hash as TraitImport;
1524
use bitcoin_hashes::HashEngine as TraitImportEngine;
1625
use bitcoin_hashes::sha256::Hash as Sha256;
26+
use bitcoin_hashes::hash160::Hash as Hash160;
1727

1828
use lightning::chain::chaininterface::{BroadcasterInterface,ConfirmationTarget,ChainListener,FeeEstimator,ChainWatchInterfaceUtil};
1929
use lightning::chain::transaction::OutPoint;
@@ -235,7 +245,7 @@ impl KeysInterface for KeyProvider {
235245
fn get_destination_script(&self) -> Script {
236246
let secp_ctx = Secp256k1::signing_only();
237247
let channel_monitor_claim_key = SecretKey::from_slice(&secp_ctx, &hex::decode("0fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").unwrap()[..]).unwrap();
238-
let our_channel_monitor_claim_key_hash = Hash160::from_data(&PublicKey::from_secret_key(&secp_ctx, &channel_monitor_claim_key).serialize());
248+
let our_channel_monitor_claim_key_hash = Hash160::hash(&PublicKey::from_secret_key(&secp_ctx, &channel_monitor_claim_key).serialize());
239249
Builder::new().push_opcode(opcodes::All::OP_PUSHBYTES_0).push_slice(&our_channel_monitor_claim_key_hash[..]).into_script()
240250
}
241251

@@ -540,6 +550,14 @@ fn main() {
540550
}
541551
}
542552

553+
#[cfg(feature = "libfuzzer_fuzz")]
554+
#[macro_use] extern crate libfuzzer_sys;
555+
#[cfg(feature = "libfuzzer_fuzz")]
556+
fuzz_target!(|data: &[u8]| {
557+
let logger: Arc<Logger> = Arc::new(test_logger::TestLogger{});
558+
do_test(data, &logger);
559+
});
560+
543561
extern crate hex;
544562
#[cfg(test)]
545563
mod tests {

src/util/byte_utils.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ pub fn slice_to_be32(v: &[u8]) -> u32 {
1010
((v[2] as u32) << 8*1) |
1111
((v[3] as u32) << 8*0)
1212
}
13+
#[cfg(not(feature = "fuzztarget"))] // Used only by poly1305
1314
#[inline]
1415
pub fn slice_to_le32(v: &[u8]) -> u32 {
1516
((v[0] as u32) << 8*0) |
@@ -54,6 +55,7 @@ pub fn be32_to_array(u: u32) -> [u8; 4] {
5455
v[3] = ((u >> 8*0) & 0xff) as u8;
5556
v
5657
}
58+
#[cfg(not(feature = "fuzztarget"))] // Used only by poly1305
5759
#[inline]
5860
pub fn le32_to_array(u: u32) -> [u8; 4] {
5961
let mut v = [0; 4];

src/util/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ pub mod ser;
66

77
pub(crate) mod byte_utils;
88
pub(crate) mod chacha20;
9+
#[cfg(not(feature = "fuzztarget"))]
910
pub(crate) mod poly1305;
1011
pub(crate) mod chacha20poly1305rfc;
1112
pub(crate) mod internal_traits;

0 commit comments

Comments
 (0)