Skip to content

Commit 3bea9b5

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 9995ad7 commit 3bea9b5

File tree

1 file changed

+52
-4
lines changed

1 file changed

+52
-4
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2528,7 +2528,8 @@ where
25282528

25292529
fn construct_recv_pending_htlc_info(
25302530
&self, hop_data: msgs::OnionHopData, shared_secret: [u8; 32], payment_hash: PaymentHash,
2531-
amt_msat: u64, cltv_expiry: u32, phantom_shared_secret: Option<[u8; 32]>, allow_underpay: bool
2531+
amt_msat: u64, cltv_expiry: u32, phantom_shared_secret: Option<[u8; 32]>, allow_underpay: bool,
2532+
counterparty_skimmed_fee_msat: Option<u64>,
25322533
) -> Result<PendingHTLCInfo, ReceiveError> {
25332534
// final_incorrect_cltv_expiry
25342535
if hop_data.outgoing_cltv_value > cltv_expiry {
@@ -2555,7 +2556,10 @@ where
25552556
msg: "The final CLTV expiry is too soon to handle",
25562557
});
25572558
}
2558-
if !allow_underpay && hop_data.amt_to_forward > amt_msat {
2559+
if (!allow_underpay && hop_data.amt_to_forward > amt_msat) ||
2560+
(allow_underpay && hop_data.amt_to_forward >
2561+
amt_msat.saturating_add(counterparty_skimmed_fee_msat.unwrap_or(0)))
2562+
{
25592563
return Err(ReceiveError {
25602564
err_code: 19,
25612565
err_data: amt_msat.to_be_bytes().to_vec(),
@@ -2855,7 +2859,7 @@ where
28552859
onion_utils::Hop::Receive(next_hop_data) => {
28562860
// OUR PAYMENT!
28572861
match self.construct_recv_pending_htlc_info(next_hop_data, shared_secret, msg.payment_hash,
2858-
msg.amount_msat, msg.cltv_expiry, None, allow_underpay)
2862+
msg.amount_msat, msg.cltv_expiry, None, allow_underpay, msg.skimmed_fee_msat)
28592863
{
28602864
Ok(info) => {
28612865
// Note that we could obviously respond immediately with an update_fulfill_htlc
@@ -3671,7 +3675,7 @@ where
36713675
onion_utils::Hop::Receive(hop_data) => {
36723676
match self.construct_recv_pending_htlc_info(hop_data,
36733677
incoming_shared_secret, payment_hash, outgoing_amt_msat,
3674-
outgoing_cltv_value, Some(phantom_shared_secret), false)
3678+
outgoing_cltv_value, Some(phantom_shared_secret), false, None)
36753679
{
36763680
Ok(info) => phantom_receives.push((prev_short_channel_id, prev_funding_outpoint, prev_user_channel_id, vec![(info, prev_htlc_id)])),
36773681
Err(ReceiveError { err_code, err_data, msg }) => failed_payment!(msg, err_code, err_data, Some(phantom_shared_secret))
@@ -9734,6 +9738,50 @@ mod tests {
97349738
get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, last_random_pk);
97359739
}
97369740

9741+
#[test]
9742+
fn reject_excessively_underpaying_htlcs() {
9743+
let chanmon_cfg = create_chanmon_cfgs(1);
9744+
let node_cfg = create_node_cfgs(1, &chanmon_cfg);
9745+
let node_chanmgr = create_node_chanmgrs(1, &node_cfg, &[None]);
9746+
let node = create_network(1, &node_cfg, &node_chanmgr);
9747+
let sender_intended_amt_msat = 100;
9748+
let extra_fee_msat = 10;
9749+
let hop_data = msgs::OnionHopData {
9750+
amt_to_forward: 100,
9751+
outgoing_cltv_value: 42,
9752+
format: msgs::OnionHopDataFormat::FinalNode {
9753+
keysend_preimage: None,
9754+
payment_metadata: None,
9755+
payment_data: Some(msgs::FinalOnionHopData {
9756+
payment_secret: PaymentSecret([0; 32]), total_msat: sender_intended_amt_msat,
9757+
}),
9758+
}
9759+
};
9760+
// Check that if the amount we received + the penultimate hop extra fee is less than the sender
9761+
// intended amount, we fail the payment.
9762+
if let Err(crate::ln::channelmanager::ReceiveError { err_code, .. }) =
9763+
node[0].node.construct_recv_pending_htlc_info(hop_data, [0; 32], PaymentHash([0; 32]),
9764+
sender_intended_amt_msat - extra_fee_msat - 1, 42, None, true, Some(extra_fee_msat))
9765+
{
9766+
assert_eq!(err_code, 19);
9767+
} else { panic!(); }
9768+
9769+
// If amt_received + extra_fee is equal to the sender intended amount, we're fine.
9770+
let hop_data = msgs::OnionHopData { // This is the same hop_data as above, OnionHopData doesn't implement Clone
9771+
amt_to_forward: 100,
9772+
outgoing_cltv_value: 42,
9773+
format: msgs::OnionHopDataFormat::FinalNode {
9774+
keysend_preimage: None,
9775+
payment_metadata: None,
9776+
payment_data: Some(msgs::FinalOnionHopData {
9777+
payment_secret: PaymentSecret([0; 32]), total_msat: sender_intended_amt_msat,
9778+
}),
9779+
}
9780+
};
9781+
assert!(node[0].node.construct_recv_pending_htlc_info(hop_data, [0; 32], PaymentHash([0; 32]),
9782+
sender_intended_amt_msat - extra_fee_msat, 42, None, true, Some(extra_fee_msat)).is_ok());
9783+
}
9784+
97379785
#[cfg(anchors)]
97389786
#[test]
97399787
fn test_anchors_zero_fee_htlc_tx_fallback() {

0 commit comments

Comments
 (0)