Skip to content

Commit a82fb62

Browse files
Merge pull request #1703 from TheBlueMatt/2022-09-badonion-first-check
Correctly handle BADONION onion errors
2 parents 4ae65e8 + e6a3c23 commit a82fb62

File tree

2 files changed

+97
-9
lines changed

2 files changed

+97
-9
lines changed

lightning/src/ln/functional_tests.rs

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use ln::channelmanager::{ChannelManager, ChannelManagerReadArgs, PaymentId, RAAC
2424
use ln::channel::{Channel, ChannelError};
2525
use ln::{chan_utils, onion_utils};
2626
use ln::chan_utils::{htlc_success_tx_weight, htlc_timeout_tx_weight, HTLCOutputInCommitment};
27-
use routing::gossip::NetworkGraph;
27+
use routing::gossip::{NetworkGraph, NetworkUpdate};
2828
use routing::router::{PaymentParameters, Route, RouteHop, RouteParameters, find_route, get_route};
2929
use ln::features::{ChannelFeatures, InitFeatures, InvoiceFeatures, NodeFeatures};
3030
use ln::msgs;
@@ -7166,6 +7166,85 @@ fn test_update_fulfill_htlc_bolt2_after_malformed_htlc_message_must_forward_upda
71667166
check_added_monitors!(nodes[1], 1);
71677167
}
71687168

7169+
#[test]
7170+
fn test_channel_failed_after_message_with_badonion_node_perm_bits_set() {
7171+
let chanmon_cfgs = create_chanmon_cfgs(3);
7172+
let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
7173+
let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
7174+
let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
7175+
create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
7176+
let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2, InitFeatures::known(), InitFeatures::known());
7177+
7178+
let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], 100_000);
7179+
7180+
// First hop
7181+
let mut payment_event = {
7182+
nodes[0].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret)).unwrap();
7183+
check_added_monitors!(nodes[0], 1);
7184+
SendEvent::from_node(&nodes[0])
7185+
};
7186+
7187+
nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
7188+
commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
7189+
expect_pending_htlcs_forwardable!(nodes[1]);
7190+
check_added_monitors!(nodes[1], 1);
7191+
payment_event = SendEvent::from_node(&nodes[1]);
7192+
assert_eq!(payment_event.msgs.len(), 1);
7193+
7194+
// Second Hop
7195+
payment_event.msgs[0].onion_routing_packet.version = 1; // Trigger an invalid_onion_version error
7196+
nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event.msgs[0]);
7197+
check_added_monitors!(nodes[2], 0);
7198+
commitment_signed_dance!(nodes[2], nodes[1], payment_event.commitment_msg, false, true);
7199+
7200+
let events_3 = nodes[2].node.get_and_clear_pending_msg_events();
7201+
assert_eq!(events_3.len(), 1);
7202+
match events_3[0] {
7203+
MessageSendEvent::UpdateHTLCs { ref updates, .. } => {
7204+
let mut update_msg = updates.update_fail_malformed_htlcs[0].clone();
7205+
// Set the NODE bit (BADONION and PERM already set in invalid_onion_version error)
7206+
update_msg.failure_code |= 0x2000;
7207+
7208+
nodes[1].node.handle_update_fail_malformed_htlc(&nodes[2].node.get_our_node_id(), &update_msg);
7209+
commitment_signed_dance!(nodes[1], nodes[2], updates.commitment_signed, false, true);
7210+
},
7211+
_ => panic!("Unexpected event"),
7212+
}
7213+
7214+
expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[1],
7215+
vec![HTLCDestination::NextHopChannel {
7216+
node_id: Some(nodes[2].node.get_our_node_id()), channel_id: chan_2.2 }]);
7217+
let events_4 = nodes[1].node.get_and_clear_pending_msg_events();
7218+
assert_eq!(events_4.len(), 1);
7219+
check_added_monitors!(nodes[1], 1);
7220+
7221+
match events_4[0] {
7222+
MessageSendEvent::UpdateHTLCs { ref updates, .. } => {
7223+
nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &updates.update_fail_htlcs[0]);
7224+
commitment_signed_dance!(nodes[0], nodes[1], updates.commitment_signed, false, true);
7225+
},
7226+
_ => panic!("Unexpected event"),
7227+
}
7228+
7229+
let events_5 = nodes[0].node.get_and_clear_pending_events();
7230+
assert_eq!(events_5.len(), 1);
7231+
7232+
// Expect a PaymentPathFailed event with a ChannelFailure network update for the channel between
7233+
// the node originating the error to its next hop.
7234+
match events_5[0] {
7235+
Event::PaymentPathFailed { network_update:
7236+
Some(NetworkUpdate::ChannelFailure { short_channel_id, is_permanent }), error_code, ..
7237+
} => {
7238+
assert_eq!(short_channel_id, chan_2.0.contents.short_channel_id);
7239+
assert!(is_permanent);
7240+
assert_eq!(error_code, Some(0x8000|0x4000|0x2000|4));
7241+
},
7242+
_ => panic!("Unexpected event"),
7243+
}
7244+
7245+
// TODO: Test actual removal of channel from NetworkGraph when it's implemented.
7246+
}
7247+
71697248
fn do_test_failure_delay_dust_htlc_local_commitment(announce_latest: bool) {
71707249
// Dust-HTLC failure updates must be delayed until failure-trigger tx (in this case local commitment) reach ANTI_REORG_DELAY
71717250
// We can have at most two valid local commitment tx, so both cases must be covered, and both txs must be checked to get them all as

lightning/src/ln/onion_utils.rs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,7 @@ pub(super) fn process_onion_failure<T: secp256k1::Signing, L: Deref>(secp_ctx: &
425425

426426
if fixed_time_eq(&Hmac::from_engine(hmac).into_inner(), &err_packet.hmac) {
427427
if let Some(error_code_slice) = err_packet.failuremsg.get(0..2) {
428+
const BADONION: u16 = 0x8000;
428429
const PERM: u16 = 0x4000;
429430
const NODE: u16 = 0x2000;
430431
const UPDATE: u16 = 0x1000;
@@ -445,21 +446,32 @@ pub(super) fn process_onion_failure<T: secp256k1::Signing, L: Deref>(secp_ctx: &
445446
let mut network_update = None;
446447
let mut short_channel_id = None;
447448

448-
if error_code & NODE == NODE {
449+
if error_code & BADONION == BADONION {
450+
// If the error code has the BADONION bit set, always blame the channel
451+
// from the node "originating" the error to its next hop. The
452+
// "originator" is ultimately actually claiming that its counterparty
453+
// is the one who is failing the HTLC.
454+
// If the "originator" here isn't lying we should really mark the
455+
// next-hop node as failed entirely, but we can't be confident in that,
456+
// as it would allow any node to get us to completely ban one of its
457+
// counterparties. Instead, we simply remove the channel in question.
458+
network_update = Some(NetworkUpdate::ChannelFailure {
459+
short_channel_id: failing_route_hop.short_channel_id,
460+
is_permanent: true,
461+
});
462+
} else if error_code & NODE == NODE {
449463
let is_permanent = error_code & PERM == PERM;
450464
network_update = Some(NetworkUpdate::NodeFailure { node_id: route_hop.pubkey, is_permanent });
451465
short_channel_id = Some(route_hop.short_channel_id);
452-
}
453-
else if error_code & PERM == PERM {
466+
} else if error_code & PERM == PERM {
454467
if !payment_failed {
455468
network_update = Some(NetworkUpdate::ChannelFailure {
456469
short_channel_id: failing_route_hop.short_channel_id,
457470
is_permanent: true,
458471
});
459472
short_channel_id = Some(failing_route_hop.short_channel_id);
460473
}
461-
}
462-
else if error_code & UPDATE == UPDATE {
474+
} else if error_code & UPDATE == UPDATE {
463475
if let Some(update_len_slice) = err_packet.failuremsg.get(debug_field_size+2..debug_field_size+4) {
464476
let update_len = u16::from_be_bytes(update_len_slice.try_into().expect("len is 2")) as usize;
465477
if let Some(mut update_slice) = err_packet.failuremsg.get(debug_field_size + 4..debug_field_size + 4 + update_len) {
@@ -545,9 +557,6 @@ pub(super) fn process_onion_failure<T: secp256k1::Signing, L: Deref>(secp_ctx: &
545557
short_channel_id = Some(route_hop.short_channel_id);
546558
}
547559

548-
// TODO: Here (and a few other places) we assume that BADONION errors
549-
// are always "sourced" from the node previous to the one which failed
550-
// to decode the onion.
551560
res = Some((network_update, short_channel_id, !(error_code & PERM == PERM && is_from_final_node)));
552561

553562
let (description, title) = errors::get_onion_error_description(error_code);

0 commit comments

Comments
 (0)