Skip to content

Commit b3882b4

Browse files
committed
Log cases where an onion failure cannot be attributed or interpreted
Create more visibility into these edge cases. The non-attributable failure in particular can be used to disrupt sender operation and it is therefore good to at least log these cases clearly.
1 parent 447148d commit b3882b4

File tree

1 file changed

+66
-6
lines changed

1 file changed

+66
-6
lines changed

lightning/src/ln/onion_utils.rs

Lines changed: 66 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,22 +1044,28 @@ where
10441044
let amt_to_forward = htlc_msat - route_hop.fee_msat;
10451045
htlc_msat = amt_to_forward;
10461046

1047-
let err_packet = match decrypt_onion_error_packet(&mut encrypted_packet, shared_secret) {
1048-
Ok(p) => p,
1049-
Err(_) => return,
1050-
};
1047+
let decrypt_result = decrypt_onion_error_packet(&mut encrypted_packet, shared_secret);
1048+
10511049
let um = gen_um_from_shared_secret(shared_secret.as_ref());
10521050
let mut hmac = HmacEngine::<Sha256>::new(&um);
1053-
hmac.input(&err_packet.encode()[32..]);
1051+
hmac.input(&encrypted_packet[32..]);
10541052

1055-
if !fixed_time_eq(&Hmac::from_engine(hmac).to_byte_array(), &err_packet.hmac) {
1053+
if !fixed_time_eq(&Hmac::from_engine(hmac).to_byte_array(), &encrypted_packet[..32]) {
10561054
return;
10571055
}
1056+
1057+
let err_packet = match decrypt_result {
1058+
Ok(p) => p,
1059+
Err(_) => return,
1060+
};
1061+
10581062
let error_code_slice = match err_packet.failuremsg.get(0..2) {
10591063
Some(s) => s,
10601064
None => {
10611065
// Useless packet that we can't use but it passed HMAC, so it definitely came from the peer
10621066
// in question
1067+
log_warn!(logger, "Missing error code in failure from {}", route_hop.pubkey);
1068+
10631069
let network_update = Some(NetworkUpdate::NodeFailure {
10641070
node_id: route_hop.pubkey,
10651071
is_permanent: true,
@@ -1219,6 +1225,12 @@ where
12191225
} else {
12201226
// only not set either packet unparseable or hmac does not match with any
12211227
// payment not retryable only when garbage is from the final node
1228+
log_warn!(
1229+
logger,
1230+
"Non-attributable failure encountered on route {}",
1231+
path.hops.iter().map(|h| h.pubkey.to_string()).collect::<Vec<_>>().join("->")
1232+
);
1233+
12221234
DecodedOnionFailure {
12231235
network_update: None,
12241236
short_channel_id: None,
@@ -2155,6 +2167,54 @@ mod tests {
21552167
assert_eq!(decrypted_failure.onion_error_code, Some(0x2002));
21562168
}
21572169

2170+
#[test]
2171+
fn test_non_attributable_failure_packet_onion() {
2172+
// Create a failure packet with bogus data.
2173+
let packet = vec![1u8; 292];
2174+
2175+
// In the current protocol, it is unfortunately not possible to identify the failure source.
2176+
let decrypted_failure = test_failure_attribution(&packet);
2177+
assert_eq!(decrypted_failure.short_channel_id, None);
2178+
}
2179+
2180+
#[test]
2181+
fn test_missing_error_code() {
2182+
// Create a failure packet with a valid hmac and structure, but no error code.
2183+
let onion_keys: Vec<OnionKeys> = build_test_onion_keys();
2184+
let shared_secret = onion_keys[0].shared_secret.as_ref();
2185+
let um = gen_um_from_shared_secret(&shared_secret);
2186+
2187+
let failuremsg = vec![1];
2188+
let pad = Vec::new();
2189+
let mut packet = msgs::DecodedOnionErrorPacket { hmac: [0; 32], failuremsg, pad };
2190+
2191+
let mut hmac = HmacEngine::<Sha256>::new(&um);
2192+
hmac.input(&packet.encode()[32..]);
2193+
packet.hmac = Hmac::from_engine(hmac).to_byte_array();
2194+
2195+
let packet = encrypt_failure_packet(shared_secret, &packet.encode()[..]);
2196+
2197+
let decrypted_failure = test_failure_attribution(&packet.data);
2198+
assert_eq!(decrypted_failure.short_channel_id, Some(0));
2199+
}
2200+
2201+
fn test_failure_attribution(packet: &[u8]) -> DecodedOnionFailure {
2202+
let logger: Arc<TestLogger> = Arc::new(TestLogger::new());
2203+
let ctx_full = Secp256k1::new();
2204+
let path = build_test_path();
2205+
let htlc_source = HTLCSource::OutboundRoute {
2206+
path,
2207+
session_priv: get_test_session_key(),
2208+
first_hop_htlc_msat: 0,
2209+
payment_id: PaymentId([1; 32]),
2210+
};
2211+
2212+
let decrypted_failure =
2213+
process_onion_failure(&ctx_full, &logger, &htlc_source, packet.into());
2214+
2215+
decrypted_failure
2216+
}
2217+
21582218
struct RawOnionHopData {
21592219
data: Vec<u8>,
21602220
}

0 commit comments

Comments
 (0)