From 51c56d2825bf6a5c010bdb9106c0198886a45e1e Mon Sep 17 00:00:00 2001 From: ziggie Date: Wed, 20 Dec 2023 14:28:18 +0100 Subject: [PATCH] lnwallet: add underflow check when computing balance. --- lnwallet/channel.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lnwallet/channel.go b/lnwallet/channel.go index f668c618e..0aee6c331 100644 --- a/lnwallet/channel.go +++ b/lnwallet/channel.go @@ -4801,6 +4801,19 @@ func (lc *LightningChannel) computeView(view *htlcView, remoteChain bool, } feePerKw := filteredHTLCView.feePerKw + // We need to first check ourBalance and theirBalance to be negative + // because MilliSathoshi is a unsigned type and can underflow in + // `evaluateHTLCView`. This should never happen for views which do not + // include new updates (remote or local). + if int64(ourBalance) < 0 { + err := fmt.Errorf("%w: our balance", ErrBelowChanReserve) + return 0, 0, 0, nil, err + } + if int64(theirBalance) < 0 { + err := fmt.Errorf("%w: their balance", ErrBelowChanReserve) + return 0, 0, 0, nil, err + } + // Now go through all HTLCs at this stage, to calculate the total // weight, needed to calculate the transaction fee. var totalHtlcWeight int64