Skip to content

Commit 482ef26

Browse files
committed
works
1 parent 475dd45 commit 482ef26

File tree

2 files changed

+90
-70
lines changed

2 files changed

+90
-70
lines changed

fuzz/src/process_onion_failure.rs

Lines changed: 76 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,35 @@ fn do_test<Out: test_logger::Output>(data: &[u8], out: Out) {
3030
let logger: Arc<dyn Logger> =
3131
Arc::new(test_logger::TestLogger::new("".to_owned(), out));
3232

33-
let first_hop_htlc_msat_bytes = match get_slice!(8).try_into() {
34-
Ok(val) => val,
35-
Err(_) => return,
36-
};
33+
macro_rules! get_u64 {
34+
() => {
35+
match get_slice!(8).try_into() {
36+
Ok(val) => u64::from_be_bytes(val),
37+
Err(_) => return,
38+
}
39+
};
40+
}
41+
42+
macro_rules! get_u32 {
43+
() => {
44+
match get_slice!(4).try_into() {
45+
Ok(val) => u32::from_be_bytes(val),
46+
Err(_) => return,
47+
}
48+
};
49+
}
50+
51+
macro_rules! get_bool {
52+
() => {
53+
get_slice!(1)[0] != 0
54+
};
55+
}
56+
57+
macro_rules! get_u8 {
58+
() => {
59+
get_slice!(1)[0]
60+
};
61+
}
3762

3863
let session_priv = match SecretKey::from_slice(get_slice!(32)) {
3964
Ok(val) => val,
@@ -54,74 +79,60 @@ fn do_test<Out: test_logger::Output>(data: &[u8], out: Out) {
5479
};
5580
}
5681

57-
let path = Path {
58-
hops: vec![
59-
// Bob
60-
RouteHop {
61-
pubkey: get_pubkey!(),
62-
node_features: NodeFeatures::empty(),
63-
short_channel_id: 0,
64-
channel_features: ChannelFeatures::empty(),
65-
fee_msat: 3_000,
66-
cltv_expiry_delta: 24,
67-
maybe_announced_channel: false,
68-
},
69-
70-
// Carol
71-
RouteHop {
72-
pubkey: get_pubkey!(),
73-
node_features: NodeFeatures::empty(),
74-
short_channel_id: (572330 << 40) + (42 << 16) + 2821,
75-
channel_features: ChannelFeatures::empty(),
76-
fee_msat: 153_000,
77-
cltv_expiry_delta: 0,
78-
maybe_announced_channel: false,
79-
},
80-
],
81-
blinded_tail: Some(BlindedTail {
82-
trampoline_hops: vec![
83-
// Carol's pubkey
84-
TrampolineHop {
85-
pubkey: get_pubkey!(),
86-
node_features: Features::empty(),
87-
fee_msat: 2_500,
88-
cltv_expiry_delta: 24,
89-
},
90-
91-
// Dave's pubkey
92-
TrampolineHop {
93-
pubkey: get_pubkey!(),
94-
node_features: Features::empty(),
95-
fee_msat: 2_500,
96-
cltv_expiry_delta: 24,
97-
},
82+
let mut hops = Vec::<RouteHop>::new();
83+
let hop_count = get_slice!(1)[0] as usize;
84+
for i in 0..hop_count {
85+
hops.push(RouteHop {
86+
pubkey: get_pubkey!(),
87+
node_features: NodeFeatures::empty(),
88+
short_channel_id: get_u64!(),
89+
channel_features: ChannelFeatures::empty(),
90+
fee_msat: get_u64!(),
91+
cltv_expiry_delta: get_u32!(),
92+
maybe_announced_channel: get_bool!(),
93+
});
94+
}
9895

99-
// Emily's pubkey
100-
TrampolineHop {
96+
let blinded_tail = match get_bool!() {
97+
true => {
98+
let mut trampoline_hops = Vec::<TrampolineHop>::new();
99+
let trampoline_hop_count = get_slice!(1)[0] as usize;
100+
for i in 0..trampoline_hop_count {
101+
trampoline_hops.push(TrampolineHop {
101102
pubkey: get_pubkey!(),
102-
node_features: Features::empty(),
103-
fee_msat: 150_500,
104-
cltv_expiry_delta: 36,
105-
},
106-
],
107-
108-
// Dummy blinded hop (because LDK doesn't allow unblinded Trampoline receives)
109-
hops: vec![
110-
// Emily's dummy blinded node id
111-
BlindedHop {
103+
node_features: NodeFeatures::empty(),
104+
fee_msat: get_u64!(),
105+
cltv_expiry_delta: get_u32!(),
106+
});
107+
}
108+
let mut blinded_hops = Vec::<BlindedHop>::new();
109+
let blinded_hop_count = get_slice!(1)[0] as usize;
110+
for i in 0..blinded_hop_count {
111+
blinded_hops.push(BlindedHop {
112112
blinded_node_id: get_pubkey!(),
113-
encrypted_payload: vec![],
114-
}
115-
],
116-
blinding_point: get_pubkey!(),
117-
excess_final_cltv_expiry_delta: 0,
118-
final_value_msat: 150_000_000,
119-
})};
113+
encrypted_payload: get_slice!(get_u8!()).to_vec(),
114+
});
115+
}
116+
Some(BlindedTail{
117+
trampoline_hops,
118+
hops: blinded_hops,
119+
blinding_point: get_pubkey!(),
120+
excess_final_cltv_expiry_delta: get_u32!(),
121+
final_value_msat: get_u64!(),
122+
})
123+
},
124+
false => None,
125+
};
126+
127+
let path = Path {
128+
hops,
129+
blinded_tail,
130+
};
120131

121132
let htlc_source = HTLCSource::OutboundRoute {
122133
path,
123134
session_priv,
124-
first_hop_htlc_msat: u64::from_be_bytes(first_hop_htlc_msat_bytes),
135+
first_hop_htlc_msat: get_u64!(),
125136
payment_id,
126137
};
127138

lightning/src/ln/onion_utils.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -991,6 +991,19 @@ where
991991
_ => unreachable!(),
992992
};
993993

994+
if encrypted_packet.data.len() < 32 {
995+
return DecodedOnionFailure {
996+
network_update: None,
997+
short_channel_id: None,
998+
payment_failed_permanently: false,
999+
failed_within_blinded_path: false,
1000+
#[cfg(any(test, feature = "_test_utils"))]
1001+
onion_error_code: None,
1002+
#[cfg(any(test, feature = "_test_utils"))]
1003+
onion_error_data: None,
1004+
};
1005+
}
1006+
9941007
// Learnings from the HTLC failure to inform future payment retries and scoring.
9951008
struct FailureLearnings {
9961009
network_update: Option<NetworkUpdate>,
@@ -999,7 +1012,6 @@ where
9991012
failed_within_blinded_path: bool,
10001013
}
10011014
let mut res: Option<FailureLearnings> = None;
1002-
let mut htlc_msat = *first_hop_htlc_msat;
10031015
let mut _error_code_ret = None;
10041016
let mut _error_packet_ret = None;
10051017
let mut is_from_final_non_blinded_node = false;
@@ -1138,16 +1150,13 @@ where
11381150
}
11391151
};
11401152

1141-
let amt_to_forward = htlc_msat - route_hop.fee_msat();
1142-
htlc_msat = amt_to_forward;
1143-
11441153
crypt_failure_packet(shared_secret.as_ref(), &mut encrypted_packet);
11451154

11461155
let um = gen_um_from_shared_secret(shared_secret.as_ref());
11471156
let mut hmac = HmacEngine::<Sha256>::new(&um);
11481157
hmac.input(&encrypted_packet.data[32..]);
11491158

1150-
if !fixed_time_eq(&Hmac::from_engine(hmac).to_byte_array(), &encrypted_packet.data[..32]) {
1159+
if &Hmac::from_engine(hmac).to_byte_array() != &encrypted_packet.data[..32] {
11511160
continue;
11521161
}
11531162

0 commit comments

Comments
 (0)