Skip to content

Commit ca293bd

Browse files
Check UpdateAddHTLC::skimmed_fee_msat on receive
Make sure the penultimate hop took the amount of fee that they claimed to take. Without checking this TLV, we're heavily relying on the receiving wallet code to correctly implement logic to calculate that that the fee is as expected.
1 parent 7a5597a commit ca293bd

File tree

1 file changed

+52
-4
lines changed

1 file changed

+52
-4
lines changed

lightning/src/ln/channelmanager.rs

+52-4
Original file line numberDiff line numberDiff line change
@@ -2419,7 +2419,8 @@ where
24192419

24202420
fn construct_recv_pending_htlc_info(
24212421
&self, hop_data: msgs::OnionHopData, shared_secret: [u8; 32], payment_hash: PaymentHash,
2422-
amt_msat: u64, cltv_expiry: u32, phantom_shared_secret: Option<[u8; 32]>, allow_underpay: bool
2422+
amt_msat: u64, cltv_expiry: u32, phantom_shared_secret: Option<[u8; 32]>, allow_underpay: bool,
2423+
counterparty_skimmed_fee_msat: Option<u64>,
24232424
) -> Result<PendingHTLCInfo, ReceiveError> {
24242425
// final_incorrect_cltv_expiry
24252426
if hop_data.outgoing_cltv_value > cltv_expiry {
@@ -2446,7 +2447,10 @@ where
24462447
msg: "The final CLTV expiry is too soon to handle",
24472448
});
24482449
}
2449-
if !allow_underpay && hop_data.amt_to_forward > amt_msat {
2450+
if (!allow_underpay && hop_data.amt_to_forward > amt_msat) ||
2451+
(allow_underpay && hop_data.amt_to_forward >
2452+
amt_msat.saturating_add(counterparty_skimmed_fee_msat.unwrap_or(0)))
2453+
{
24502454
return Err(ReceiveError {
24512455
err_code: 19,
24522456
err_data: amt_msat.to_be_bytes().to_vec(),
@@ -2745,7 +2749,7 @@ where
27452749
onion_utils::Hop::Receive(next_hop_data) => {
27462750
// OUR PAYMENT!
27472751
match self.construct_recv_pending_htlc_info(next_hop_data, shared_secret, msg.payment_hash,
2748-
msg.amount_msat, msg.cltv_expiry, None, allow_underpay)
2752+
msg.amount_msat, msg.cltv_expiry, None, allow_underpay, msg.skimmed_fee_msat)
27492753
{
27502754
Ok(info) => {
27512755
// Note that we could obviously respond immediately with an update_fulfill_htlc
@@ -3561,7 +3565,7 @@ where
35613565
onion_utils::Hop::Receive(hop_data) => {
35623566
match self.construct_recv_pending_htlc_info(hop_data,
35633567
incoming_shared_secret, payment_hash, outgoing_amt_msat,
3564-
outgoing_cltv_value, Some(phantom_shared_secret), false)
3568+
outgoing_cltv_value, Some(phantom_shared_secret), false, None)
35653569
{
35663570
Ok(info) => phantom_receives.push((prev_short_channel_id, prev_funding_outpoint, prev_user_channel_id, vec![(info, prev_htlc_id)])),
35673571
Err(ReceiveError { err_code, err_data, msg }) => failed_payment!(msg, err_code, err_data, Some(phantom_shared_secret))
@@ -9590,6 +9594,50 @@ mod tests {
95909594
get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, last_random_pk);
95919595
}
95929596

9597+
#[test]
9598+
fn reject_excessively_underpaying_htlcs() {
9599+
let chanmon_cfg = create_chanmon_cfgs(1);
9600+
let node_cfg = create_node_cfgs(1, &chanmon_cfg);
9601+
let node_chanmgr = create_node_chanmgrs(1, &node_cfg, &[None]);
9602+
let node = create_network(1, &node_cfg, &node_chanmgr);
9603+
let sender_intended_amt_msat = 100;
9604+
let extra_fee_msat = 10;
9605+
let hop_data = msgs::OnionHopData {
9606+
amt_to_forward: 100,
9607+
outgoing_cltv_value: 42,
9608+
format: msgs::OnionHopDataFormat::FinalNode {
9609+
keysend_preimage: None,
9610+
payment_metadata: None,
9611+
payment_data: Some(msgs::FinalOnionHopData {
9612+
payment_secret: PaymentSecret([0; 32]), total_msat: sender_intended_amt_msat,
9613+
}),
9614+
}
9615+
};
9616+
// Check that if the amount we received + the penultimate hop extra fee is less than the sender
9617+
// intended amount, we fail the payment.
9618+
if let Err(crate::ln::channelmanager::ReceiveError { err_code, .. }) =
9619+
node[0].node.construct_recv_pending_htlc_info(hop_data, [0; 32], PaymentHash([0; 32]),
9620+
sender_intended_amt_msat - extra_fee_msat - 1, 42, None, true, Some(extra_fee_msat))
9621+
{
9622+
assert_eq!(err_code, 19);
9623+
} else { panic!(); }
9624+
9625+
// If amt_received + extra_fee is equal to the sender intended amount, we're fine.
9626+
let hop_data = msgs::OnionHopData { // This is the same hop_data as above, OnionHopData doesn't implement Clone
9627+
amt_to_forward: 100,
9628+
outgoing_cltv_value: 42,
9629+
format: msgs::OnionHopDataFormat::FinalNode {
9630+
keysend_preimage: None,
9631+
payment_metadata: None,
9632+
payment_data: Some(msgs::FinalOnionHopData {
9633+
payment_secret: PaymentSecret([0; 32]), total_msat: sender_intended_amt_msat,
9634+
}),
9635+
}
9636+
};
9637+
assert!(node[0].node.construct_recv_pending_htlc_info(hop_data, [0; 32], PaymentHash([0; 32]),
9638+
sender_intended_amt_msat - extra_fee_msat, 42, None, true, Some(extra_fee_msat)).is_ok());
9639+
}
9640+
95939641
#[cfg(anchors)]
95949642
#[test]
95959643
fn test_anchors_zero_fee_htlc_tx_fallback() {

0 commit comments

Comments
 (0)