Skip to content

Use saturating math for reserve and LSPS amounts - #1185

Open
benthecarman wants to merge 2 commits into
lightningdevkit:mainfrom
benthecarman:saturate-anchor-reserve
Open

benthecarman wants to merge 2 commits into
lightningdevkit:mainfrom
benthecarman:saturate-anchor-reserve

Conversation

@benthecarman

Copy link
Copy Markdown
Contributor

Some reserve and channel size calculations used unchecked math. Large
inputs, such as per_channel_reserve_sats = u64::MAX, panic in debug
builds and can wrap in release builds.

Use saturating math so these hit the normal insufficient-funds checks
instead.

Found by Project Loupe.

Setting AnchorChannelsConfig::per_channel_reserve_sats to u64::MAX is a
natural way to say "never open or accept anchor channels with untrusted
peers", since no wallet can cover that reserve. However, the reserve
sums used unchecked arithmetic: multiplying it by the number of existing
anchor channels, or adding the reserve for a new channel, overflowed and
panicked in debug builds. Values close to a power of two could also
wrap to a tiny reserve in release builds.

Use saturating arithmetic so an oversized reserve is treated as
unsatisfiable instead.

This was found by Project Loupe.

This change was made with the help of Claude Code.
The LSPS2 service computed the JIT channel size by multiplying the
intercepted HTLC amount by the configured over-provisioning rate, and
the LSPS1 client summed the caller-provided channel balances, both with
unchecked arithmetic. Oversized inputs overflowed, panicking in debug
builds and wrapping to a bogus channel size in release builds.

Use saturating arithmetic so such inputs fall through to the existing
insufficient-funds and LSP-limit checks.

This was found by Project Loupe.

This change was made with the help of Claude Code.
@ldk-reviews-bot

ldk-reviews-bot commented Oct 20, 2026 •

Copy link
Copy Markdown

I've assigned @tnull as a reviewer!
I'll wait for their review and will help manage the review process.
Once they submit their review, I'll check if a second reviewer would be helpful.

@ldk-reviews-bot
ldk-reviews-bot requested a review from tnull October 20, 2026 19:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants