Skip to content

Commit 054530c

Browse files
committed
Fix potential overflow bug introduced in channel reserve check fix
Found by chanmon_fail_consistency fuzz test.
1 parent c86657e commit 054530c

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

src/ln/channel.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -881,9 +881,14 @@ impl Channel {
881881
}
882882
}
883883

884-
885884
let value_to_self_msat: i64 = (self.value_to_self_msat - local_htlc_total_msat) as i64 + value_to_self_msat_offset;
886-
let value_to_remote_msat: i64 = (self.channel_value_satoshis * 1000 - self.value_to_self_msat - remote_htlc_total_msat) as i64 - value_to_self_msat_offset;
885+
assert!(value_to_self_msat >= 0);
886+
// Note that in case they have several just-awaiting-last-RAA fulfills in-progress (ie
887+
// AwaitingRemoteRevokeToRemove or AwaitingRemovedRemoteRevoke) we may have allowed them to
888+
// "violate" their reserve value by couting those against it. Thus, we have to convert
889+
// everything to i64 before subtracting as otherwise we can overflow.
890+
let value_to_remote_msat: i64 = (self.channel_value_satoshis * 1000) as i64 - (self.value_to_self_msat as i64) - (remote_htlc_total_msat as i64) - value_to_self_msat_offset;
891+
assert!(value_to_remote_msat >= 0);
887892

888893
#[cfg(debug_assertions)]
889894
{

0 commit comments

Comments
 (0)