Skip to content

Commit 24c2468

Browse files
Ignore channel updates in onion errors.
Per <lightning/bolts#1163>.
1 parent 1d421d3 commit 24c2468

File tree

3 files changed

+46
-122
lines changed

3 files changed

+46
-122
lines changed

lightning/src/ln/functional_test_utils.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2460,11 +2460,11 @@ pub fn expect_payment_failed_conditions_event<'a, 'b, 'c, 'd, 'e>(
24602460
const CHAN_DISABLED_FLAG: u8 = 2;
24612461
assert_eq!(msg.contents.flags & CHAN_DISABLED_FLAG, 0);
24622462
},
2463-
NetworkUpdate::ChannelFailure { short_channel_id, is_permanent } if chan_closed => {
2463+
NetworkUpdate::ChannelFailure { short_channel_id, is_permanent } => {
24642464
if let Some(scid) = conditions.expected_blamed_scid {
24652465
assert_eq!(*short_channel_id, scid);
24662466
}
2467-
assert!(is_permanent);
2467+
assert_eq!(*is_permanent, chan_closed);
24682468
},
24692469
_ => panic!("Unexpected update type"),
24702470
}

lightning/src/ln/onion_route_tests.rs

+37-22
Original file line numberDiff line numberDiff line change
@@ -300,12 +300,13 @@ fn test_fee_failures() {
300300
claim_payment(&nodes[0], &[&nodes[1], &nodes[2]], payment_preimage_success);
301301

302302
// If the hop gives fee_insufficient but enough fees were provided, then the previous hop
303-
// malleated the payment before forwarding, taking funds when they shouldn't have.
303+
// malleated the payment before forwarding, taking funds when they shouldn't have. However,
304+
// because we ignore channel update contents, we will still blame the 2nd channel.
304305
let (_, payment_hash, payment_secret) = get_payment_preimage_hash!(nodes[2]);
305-
let short_channel_id = channels[0].0.contents.short_channel_id;
306+
let short_channel_id = channels[1].0.contents.short_channel_id;
306307
run_onion_failure_test("fee_insufficient", 0, &nodes, &route, &payment_hash, &payment_secret, |msg| {
307308
msg.amount_msat -= 1;
308-
}, || {}, true, Some(UPDATE|12), Some(NetworkUpdate::ChannelFailure { short_channel_id, is_permanent: true}), Some(short_channel_id));
309+
}, || {}, true, Some(UPDATE|12), Some(NetworkUpdate::ChannelFailure { short_channel_id, is_permanent: false}), Some(short_channel_id));
309310

310311
// In an earlier version, we spuriously failed to forward payments if the expected feerate
311312
// changed between the channel open and the payment.
@@ -478,7 +479,9 @@ fn test_onion_failure() {
478479
let session_priv = SecretKey::from_slice(&[3; 32]).unwrap();
479480
let onion_keys = onion_utils::construct_onion_keys(&Secp256k1::new(), &route.paths[0], &session_priv).unwrap();
480481
msg.reason = onion_utils::build_first_hop_failure_packet(onion_keys[0].shared_secret.as_ref(), UPDATE|7, &err_data);
481-
}, ||{}, true, Some(UPDATE|7), Some(NetworkUpdate::ChannelUpdateMessage{msg: chan_update.clone()}), Some(short_channel_id));
482+
}, ||{}, true, Some(UPDATE|7),
483+
Some(NetworkUpdate::ChannelFailure { short_channel_id, is_permanent: false }),
484+
Some(short_channel_id));
482485

483486
// Check we can still handle onion failures that include channel updates without a type prefix
484487
let err_data_without_type = chan_update.encode_with_len();
@@ -488,7 +491,9 @@ fn test_onion_failure() {
488491
let session_priv = SecretKey::from_slice(&[3; 32]).unwrap();
489492
let onion_keys = onion_utils::construct_onion_keys(&Secp256k1::new(), &route.paths[0], &session_priv).unwrap();
490493
msg.reason = onion_utils::build_first_hop_failure_packet(onion_keys[0].shared_secret.as_ref(), UPDATE|7, &err_data_without_type);
491-
}, ||{}, true, Some(UPDATE|7), Some(NetworkUpdate::ChannelUpdateMessage{msg: chan_update}), Some(short_channel_id));
494+
}, ||{}, true, Some(UPDATE|7),
495+
Some(NetworkUpdate::ChannelFailure { short_channel_id, is_permanent: false }),
496+
Some(short_channel_id));
492497

493498
let short_channel_id = channels[1].0.contents.short_channel_id;
494499
run_onion_failure_test_with_fail_intercept("permanent_channel_failure", 100, &nodes, &route, &payment_hash, &payment_secret, |msg| {
@@ -523,7 +528,9 @@ fn test_onion_failure() {
523528
let mut bogus_route = route.clone();
524529
let route_len = bogus_route.paths[0].hops.len();
525530
bogus_route.paths[0].hops[route_len-1].fee_msat = amt_to_forward;
526-
run_onion_failure_test("amount_below_minimum", 0, &nodes, &bogus_route, &payment_hash, &payment_secret, |_| {}, ||{}, true, Some(UPDATE|11), Some(NetworkUpdate::ChannelUpdateMessage{msg: ChannelUpdate::dummy(short_channel_id)}), Some(short_channel_id));
531+
run_onion_failure_test("amount_below_minimum", 0, &nodes, &bogus_route, &payment_hash, &payment_secret, |_| {}, ||{}, true, Some(UPDATE|11),
532+
Some(NetworkUpdate::ChannelFailure { short_channel_id, is_permanent: false }),
533+
Some(short_channel_id));
527534

528535
// Clear pending payments so that the following positive test has the correct payment hash.
529536
for node in nodes.iter() {
@@ -535,24 +542,28 @@ fn test_onion_failure() {
535542
let preimage = send_along_route(&nodes[0], bogus_route, &[&nodes[1], &nodes[2]], amt_to_forward+1).0;
536543
claim_payment(&nodes[0], &[&nodes[1], &nodes[2]], preimage);
537544

538-
let short_channel_id = channels[0].0.contents.short_channel_id;
545+
// We ignore channel update contents in onion errors, so will blame the 2nd channel even though
546+
// the first node is the one that messed up.
547+
let short_channel_id = channels[1].0.contents.short_channel_id;
539548
run_onion_failure_test("fee_insufficient", 0, &nodes, &route, &payment_hash, &payment_secret, |msg| {
540549
msg.amount_msat -= 1;
541-
}, || {}, true, Some(UPDATE|12), Some(NetworkUpdate::ChannelFailure { short_channel_id, is_permanent: true}), Some(short_channel_id));
550+
}, || {}, true, Some(UPDATE|12), Some(NetworkUpdate::ChannelFailure { short_channel_id, is_permanent: false}), Some(short_channel_id));
542551

543-
let short_channel_id = channels[0].0.contents.short_channel_id;
552+
let short_channel_id = channels[1].0.contents.short_channel_id;
544553
run_onion_failure_test("incorrect_cltv_expiry", 0, &nodes, &route, &payment_hash, &payment_secret, |msg| {
545554
// need to violate: cltv_expiry - cltv_expiry_delta >= outgoing_cltv_value
546555
msg.cltv_expiry -= 1;
547-
}, || {}, true, Some(UPDATE|13), Some(NetworkUpdate::ChannelFailure { short_channel_id, is_permanent: true}), Some(short_channel_id));
556+
}, || {}, true, Some(UPDATE|13), Some(NetworkUpdate::ChannelFailure { short_channel_id, is_permanent: false}), Some(short_channel_id));
548557

549558
let short_channel_id = channels[1].0.contents.short_channel_id;
550559
run_onion_failure_test("expiry_too_soon", 0, &nodes, &route, &payment_hash, &payment_secret, |msg| {
551560
let height = msg.cltv_expiry - CLTV_CLAIM_BUFFER - LATENCY_GRACE_PERIOD_BLOCKS + 1;
552561
connect_blocks(&nodes[0], height - nodes[0].best_block_info().1);
553562
connect_blocks(&nodes[1], height - nodes[1].best_block_info().1);
554563
connect_blocks(&nodes[2], height - nodes[2].best_block_info().1);
555-
}, ||{}, true, Some(UPDATE|14), Some(NetworkUpdate::ChannelUpdateMessage{msg: ChannelUpdate::dummy(short_channel_id)}), Some(short_channel_id));
564+
}, ||{}, true, Some(UPDATE|14),
565+
Some(NetworkUpdate::ChannelFailure { short_channel_id, is_permanent: false }),
566+
Some(short_channel_id));
556567

557568
run_onion_failure_test("unknown_payment_hash", 2, &nodes, &route, &payment_hash, &payment_secret, |_| {}, || {
558569
nodes[2].node.fail_htlc_backwards(&payment_hash);
@@ -596,7 +607,9 @@ fn test_onion_failure() {
596607
// disconnect event to the channel between nodes[1] ~ nodes[2]
597608
nodes[1].node.peer_disconnected(&nodes[2].node.get_our_node_id());
598609
nodes[2].node.peer_disconnected(&nodes[1].node.get_our_node_id());
599-
}, true, Some(UPDATE|7), Some(NetworkUpdate::ChannelUpdateMessage{msg: ChannelUpdate::dummy(short_channel_id)}), Some(short_channel_id));
610+
}, true, Some(UPDATE|7),
611+
Some(NetworkUpdate::ChannelFailure { short_channel_id, is_permanent: false }),
612+
Some(short_channel_id));
600613
run_onion_failure_test("channel_disabled", 0, &nodes, &route, &payment_hash, &payment_secret, |_| {}, || {
601614
// disconnect event to the channel between nodes[1] ~ nodes[2]
602615
for _ in 0..DISABLE_GOSSIP_TICKS + 1 {
@@ -605,7 +618,9 @@ fn test_onion_failure() {
605618
}
606619
nodes[1].node.get_and_clear_pending_msg_events();
607620
nodes[2].node.get_and_clear_pending_msg_events();
608-
}, true, Some(UPDATE|20), Some(NetworkUpdate::ChannelUpdateMessage{msg: ChannelUpdate::dummy(short_channel_id)}), Some(short_channel_id));
621+
}, true, Some(UPDATE|20),
622+
Some(NetworkUpdate::ChannelFailure { short_channel_id, is_permanent: false }),
623+
Some(short_channel_id));
609624
reconnect_nodes(ReconnectArgs::new(&nodes[1], &nodes[2]));
610625

611626
run_onion_failure_test("expiry_too_far", 0, &nodes, &route, &payment_hash, &payment_secret, |msg| {
@@ -844,9 +859,9 @@ fn do_test_onion_failure_stale_channel_update(announced_channel: bool) {
844859
// We'll be attempting to route payments using the default ChannelUpdate for channels. This will
845860
// lead to onion failures at the first hop once we update the ChannelConfig for the
846861
// second hop.
847-
let expect_onion_failure = |name: &str, error_code: u16, channel_update: &msgs::ChannelUpdate| {
862+
let expect_onion_failure = |name: &str, error_code: u16| {
848863
let short_channel_id = channel_to_update.1;
849-
let network_update = NetworkUpdate::ChannelUpdateMessage { msg: channel_update.clone() };
864+
let network_update = NetworkUpdate::ChannelFailure { short_channel_id, is_permanent: false };
850865
run_onion_failure_test(
851866
name, 0, &nodes, &route, &payment_hash, &payment_secret, |_| {}, || {}, true,
852867
Some(error_code), Some(network_update), Some(short_channel_id),
@@ -878,7 +893,7 @@ fn do_test_onion_failure_stale_channel_update(announced_channel: bool) {
878893
// Connect a block, which should expire the previous config, leading to a failure when
879894
// forwarding the HTLC.
880895
expire_prev_config();
881-
expect_onion_failure("fee_insufficient", UPDATE|12, &msg);
896+
expect_onion_failure("fee_insufficient", UPDATE|12);
882897

883898
// Redundant updates should not trigger a new ChannelUpdate.
884899
assert!(update_and_get_channel_update(&config, false, None, false).is_none());
@@ -891,15 +906,15 @@ fn do_test_onion_failure_stale_channel_update(announced_channel: bool) {
891906
// new ChannelUpdate.
892907
config.forwarding_fee_base_msat = default_config.forwarding_fee_base_msat;
893908
config.cltv_expiry_delta = u16::max_value();
894-
let msg = update_and_get_channel_update(&config, true, Some(&msg), true).unwrap();
895-
expect_onion_failure("incorrect_cltv_expiry", UPDATE|13, &msg);
909+
assert!(update_and_get_channel_update(&config, true, Some(&msg), true).is_some());
910+
expect_onion_failure("incorrect_cltv_expiry", UPDATE|13);
896911

897912
// Reset the proportional fee and increase the CLTV expiry delta which should trigger a new
898913
// ChannelUpdate.
899914
config.cltv_expiry_delta = default_config.cltv_expiry_delta;
900915
config.forwarding_fee_proportional_millionths = u32::max_value();
901-
let msg = update_and_get_channel_update(&config, true, Some(&msg), true).unwrap();
902-
expect_onion_failure("fee_insufficient", UPDATE|12, &msg);
916+
assert!(update_and_get_channel_update(&config, true, Some(&msg), true).is_some());
917+
expect_onion_failure("fee_insufficient", UPDATE|12);
903918

904919
// To test persistence of the updated config, we'll re-initialize the ChannelManager.
905920
let config_after_restart = {
@@ -1530,10 +1545,10 @@ fn do_test_phantom_dust_exposure_failure(multiplier_dust_limit: bool) {
15301545
err_data.extend_from_slice(&channel.1.encode());
15311546

15321547
let mut fail_conditions = PaymentFailedConditions::new()
1533-
.blamed_scid(channel.0.contents.short_channel_id)
1548+
.blamed_scid(route.paths[0].hops.last().as_ref().unwrap().short_channel_id)
15341549
.blamed_chan_closed(false)
15351550
.expected_htlc_error_data(0x1000 | 7, &err_data);
1536-
expect_payment_failed_conditions(&nodes[0], payment_hash, false, fail_conditions);
1551+
expect_payment_failed_conditions(&nodes[0], payment_hash, false, fail_conditions);
15371552
}
15381553

15391554
#[test]

lightning/src/ln/onion_utils.rs

+7-98
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use crate::ln::channelmanager::{HTLCSource, RecipientOnionFields};
1515
use crate::ln::features::{ChannelFeatures, NodeFeatures};
1616
use crate::ln::msgs;
1717
use crate::ln::types::{PaymentHash, PaymentPreimage};
18-
use crate::ln::wire::Encode;
1918
use crate::routing::gossip::NetworkUpdate;
2019
use crate::routing::router::{Path, RouteHop, RouteParameters};
2120
use crate::sign::NodeSigner;
@@ -806,106 +805,16 @@ where
806805
{
807806
let update_len =
808807
u16::from_be_bytes(update_len_slice.try_into().expect("len is 2")) as usize;
809-
if let Some(mut update_slice) = err_packet
808+
if err_packet
810809
.failuremsg
811810
.get(debug_field_size + 4..debug_field_size + 4 + update_len)
811+
.is_some()
812812
{
813-
// Historically, the BOLTs were unclear if the message type
814-
// bytes should be included here or not. The BOLTs have now
815-
// been updated to indicate that they *are* included, but many
816-
// nodes still send messages without the type bytes, so we
817-
// support both here.
818-
// TODO: Switch to hard require the type prefix, as the current
819-
// permissiveness introduces the (although small) possibility
820-
// that we fail to decode legitimate channel updates that
821-
// happen to start with ChannelUpdate::TYPE, i.e., [0x01, 0x02].
822-
if update_slice.len() > 2
823-
&& update_slice[0..2] == msgs::ChannelUpdate::TYPE.to_be_bytes()
824-
{
825-
update_slice = &update_slice[2..];
826-
} else {
827-
log_trace!(logger, "Failure provided features a channel update without type prefix. Deprecated, but allowing for now.");
828-
}
829-
let update_opt = msgs::ChannelUpdate::read(&mut Cursor::new(&update_slice));
830-
if update_opt.is_ok() || update_slice.is_empty() {
831-
// if channel_update should NOT have caused the failure:
832-
// MAY treat the channel_update as invalid.
833-
let is_chan_update_invalid = match error_code & 0xff {
834-
7 => false,
835-
11 => {
836-
update_opt.is_ok()
837-
&& amt_to_forward
838-
> update_opt.as_ref().unwrap().contents.htlc_minimum_msat
839-
},
840-
12 => {
841-
update_opt.is_ok()
842-
&& amt_to_forward
843-
.checked_mul(
844-
update_opt
845-
.as_ref()
846-
.unwrap()
847-
.contents
848-
.fee_proportional_millionths as u64,
849-
)
850-
.map(|prop_fee| prop_fee / 1_000_000)
851-
.and_then(|prop_fee| {
852-
prop_fee.checked_add(
853-
update_opt.as_ref().unwrap().contents.fee_base_msat
854-
as u64,
855-
)
856-
})
857-
.map(|fee_msats| route_hop.fee_msat >= fee_msats)
858-
.unwrap_or(false)
859-
},
860-
13 => {
861-
update_opt.is_ok()
862-
&& route_hop.cltv_expiry_delta as u16
863-
>= update_opt.as_ref().unwrap().contents.cltv_expiry_delta
864-
},
865-
14 => false, // expiry_too_soon; always valid?
866-
20 => update_opt.as_ref().unwrap().contents.flags & 2 == 0,
867-
_ => false, // unknown error code; take channel_update as valid
868-
};
869-
if is_chan_update_invalid {
870-
// This probably indicates the node which forwarded
871-
// to the node in question corrupted something.
872-
network_update = Some(NetworkUpdate::ChannelFailure {
873-
short_channel_id: route_hop.short_channel_id,
874-
is_permanent: true,
875-
});
876-
} else {
877-
if let Ok(chan_update) = update_opt {
878-
// Make sure the ChannelUpdate contains the expected
879-
// short channel id.
880-
if failing_route_hop.short_channel_id
881-
== chan_update.contents.short_channel_id
882-
{
883-
short_channel_id = Some(failing_route_hop.short_channel_id);
884-
} else {
885-
log_info!(logger, "Node provided a channel_update for which it was not authoritative, ignoring.");
886-
}
887-
network_update =
888-
Some(NetworkUpdate::ChannelUpdateMessage { msg: chan_update })
889-
} else {
890-
// The node in question intentionally encoded a 0-length channel update. This is
891-
// likely due to https://github.com/ElementsProject/lightning/issues/6200.
892-
short_channel_id = Some(failing_route_hop.short_channel_id);
893-
network_update = Some(NetworkUpdate::ChannelFailure {
894-
short_channel_id: failing_route_hop.short_channel_id,
895-
is_permanent: false,
896-
});
897-
}
898-
};
899-
} else {
900-
// If the channel_update had a non-zero length (i.e. was
901-
// present) but we couldn't read it, treat it as a total
902-
// node failure.
903-
log_info!(
904-
logger,
905-
"Failed to read a channel_update of len {} in an onion",
906-
update_slice.len()
907-
);
908-
}
813+
network_update = Some(NetworkUpdate::ChannelFailure {
814+
short_channel_id: failing_route_hop.short_channel_id,
815+
is_permanent: false,
816+
});
817+
short_channel_id = Some(failing_route_hop.short_channel_id);
909818
}
910819
}
911820
if network_update.is_none() {

0 commit comments

Comments
 (0)