Merge PR 'Reject overflowing legacy PaymentForwarded amounts' (#5316)
What changed, and why it matters
This change fixes a spot where two payment amounts could be added together without checking for integer overflow. In Rust, adding two large u64 numbers can wrap around to a tiny value, which could make a forwarded payment look incorrectly small. The patch now rejects the data if the addition overflows. This is a defensive fix in event deserialization/serialization logic.
Review whether other legacy event fields or amount combinations in the same file use unchecked arithmetic, and consider adding regression tests for overflow deserialization.
Security signals we found
Integer overflow protection added to legacy event deserialization
u64 amount arithmetic changed from wrapping add to checked_add
DecodeError::InvalidValue returned on overflow
Legacy PaymentForwarded event handling
Evidence from the diff
In lightning/src/events/mod.rs, the legacy PaymentForwarded event deserialization computed prev_htlc amount_msat as outbound_amount_forwarded_msat + fee using plain u64 addition. The patch explicitly types outbound_amount_forwarded_msat as u64 and replaces the addition with checked_add(…).ok_or(DecodeError::InvalidValue)? via transpose(). This prevents silent u64 wraparound when reading stored events and instead returns InvalidValue.
Changed components
lightning/src/events/mod.rsEvent::MaybeReadable deserializationlegacy PaymentForwarded eventInspect captured patch +4 / −2
### lightning/src/events/mod.rs
@@ -3074,7 +3074,7 @@ impl MaybeReadable for Event {
let mut total_fee_earned_msat = None;
let mut skimmed_fee_msat = None;
let mut claim_from_onchain_tx = false;
- let mut outbound_amount_forwarded_msat = 0;
+ let mut outbound_amount_forwarded_msat: u64 = 0;
let mut prev_htlcs = vec![];
let mut next_htlcs = vec![];
read_tlv_fields!(reader, {
@@ -3095,7 +3095,9 @@ impl MaybeReadable for Event {
channel_id: prev_channel_id_legacy.ok_or(DecodeError::InvalidValue)?,
htlc_id: None,
amount_msat: total_fee_earned_msat
- .map(|fee| outbound_amount_forwarded_msat + fee),
+ .map(|fee| outbound_amount_forwarded_msat.checked_add(fee)
+ .ok_or(DecodeError::InvalidValue))
+ .transpose()?,
user_channel_id: prev_user_channel_id_legacy,
node_id: prev_node_id_legacy,
}])),Why this scored 44/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.