LSPS2: Release pruned pending requests from the total limit
What changed, and why it matters
This commit fixes a denial-of-service bug in the LSPS2 (Lightning Service Provider Specification 2) service code. The service tracks how many unanswered client requests it has globally and stops accepting new ones after 1,000. When a client disconnected before being answered, the per-client request list was cleaned up, but the global counter was not reduced. That meant every such request permanently consumed one of the 1,000 slots until the program restarted. An attacker could repeatedly connect, send a request, and disconnect to exhaust the slot limit, blocking legitimate clients from using the service. The fix makes the cleanup routine report how many requests it removed and subtracts that from the global counter when a peer disconnects. A regression test is included.
Review and merge the patch, then backport to maintained release branches that include the LSPS2 service code. Monitor for any other sites where pending_requests entries are removed without updating total_pending_requests. Run the new regression test in CI.
Security signals we found
Denial-of-service via resource exhaustion of a global request quota
Counter desynchronization between per-peer state and global atomic counter
Missing decrement on peer disconnect leading to permanent slot consumption
Fix includes regression test demonstrating exploitability
Reported by external security scanner and independent researcher
Evidence from the diff
In lightning-liquidity/src/lsps2/service.rs, PeerState::prune_pending_requests now returns the number of entries it removed. In LiquidityManager::peer_disconnected, that count is subtracted from the AtomicUsize total_pending_requests using fetch_update with saturating_sub, mirroring the existing remove_pending_request logic. A debug_assert verifies the previous counter value was at least the number pruned. The change ensures total_pending_requests stays synchronized with the per-peer pending_requests maps. The added test pending_requests_pruned_on_disconnect_release_total_limit verifies that after MAX_TOTAL_PENDING_REQUESTS disconnect-and-prune cycles, a different peer’s get_info request is still accepted, which would fail before the fix because the counter would remain pinned at the limit.
Changed components
lightning-liquidity/src/lsps2/service.rslightning-liquidity/tests/lsps2_integration_tests.rsLSPS2 service handlertotal_pending_requests counterpeer_disconnected cleanup pathInspect captured patch +81 / −2
### lightning-liquidity/src/lsps2/service.rs
@@ -619,7 +619,8 @@ impl PeerState {
self.needs_persist |= true;
}
- fn prune_pending_requests(&mut self) {
+ fn prune_pending_requests(&mut self) -> usize {
+ let num_requests_before_prune = self.pending_requests.len();
self.pending_requests.retain(|_, entry| {
match entry {
LSPS2Request::GetInfo(_) => false,
@@ -629,6 +630,7 @@ impl PeerState {
},
}
});
+ num_requests_before_prune - self.pending_requests.len()
}
fn prune_expired_request_state(&mut self) {
@@ -1982,7 +1984,18 @@ where
let mut peer_state_lock = inner_state_lock.lock().unwrap();
// We clean up the peer state, but leave removing the peer entry to the prune logic in
// `persist` which removes it from the store.
- peer_state_lock.prune_pending_requests();
+ let num_pruned = peer_state_lock.prune_pending_requests();
+ // The pruned requests will never be answered, so we need to release their slots in
+ // the global counter, too.
+ let res = self.total_pending_requests.fetch_update(
+ Ordering::Relaxed,
+ Ordering::Relaxed,
+ |x| Some(x.saturating_sub(num_pruned)),
+ );
+ debug_assert!(
+ res.is_ok_and(|previous_value| previous_value >= num_pruned),
+ "total_pending_requests counter out-of-sync! This should never happen!"
+ );
peer_state_lock.prune_expired_request_state();
}
}
### lightning-liquidity/tests/lsps2_integration_tests.rs
@@ -1260,6 +1260,72 @@ fn duplicate_request_id_rejected() {
assert!(get_info_response.payload.contains("result"));
}
+#[test]
+fn pending_requests_pruned_on_disconnect_release_total_limit() {
+ let chanmon_cfgs = create_chanmon_cfgs(2);
+ let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
+ let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
+ let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
+ let (lsps_nodes, _) = setup_test_lsps2_nodes(nodes);
+ let LSPSNodes { service_node, client_node } = lsps_nodes;
+
+ let service_node_id = service_node.inner.node.get_our_node_id();
+ let client_node_id = client_node.inner.node.get_our_node_id();
+
+ let client_handler = client_node.liquidity_manager.lsps2_client_handler().unwrap();
+ let service_handler = service_node.liquidity_manager.lsps2_service_handler().unwrap();
+
+ // Repeatedly send a `get_info` request and disconnect before it is answered, so that the
+ // pending request is pruned from the peer state.
+ let mut last_request_id = None;
+ for _ in 0..MAX_TOTAL_PENDING_REQUESTS {
+ let _ = client_handler.request_opening_params(service_node_id, None);
+ let req_msg = get_lsps_message!(client_node, service_node_id);
+ service_node.liquidity_manager.handle_custom_message(req_msg, client_node_id).unwrap();
+
+ match service_node.liquidity_manager.next_event().unwrap() {
+ LiquidityEvent::LSPS2Service(LSPS2ServiceEvent::GetInfo { request_id, .. }) => {
+ last_request_id = Some(request_id);
+ },
+ _ => panic!("Unexpected event"),
+ }
+
+ service_node.liquidity_manager.peer_disconnected(client_node_id);
+ }
+
+ // The pruned request can't be answered anymore ...
+ let raw_opening_params = LSPS2RawOpeningFeeParams {
+ min_fee_msat: 100,
+ proportional: 21,
+ valid_until: LSPSDateTime::from_str("2035-05-20T08:30:45Z").unwrap(),
+ min_lifetime: 144,
+ max_client_to_self_delay: 128,
+ min_payment_size_msat: 1,
+ max_payment_size_msat: 100_000_000,
+ };
+ let result = service_handler.opening_fee_params_generated(
+ &client_node_id,
+ last_request_id.unwrap(),
+ vec![raw_opening_params],
+ );
+ assert!(matches!(result, Err(APIError::APIMisuseError { .. })));
+
+ // ... but the pruned requests must not count towards the total number of pending requests,
+ // i.e., a different peer's `get_info` request is still accepted.
+ let secp = Secp256k1::new();
+ let other_sk = SecretKey::from_slice(&[42u8; 32]).unwrap();
+ let other_node_id = PublicKey::from_secret_key(&secp, &other_sk);
+
+ let _ = client_handler.request_opening_params(service_node_id, None);
+ let other_request_msg = get_lsps_message!(client_node, service_node_id);
+ service_node.liquidity_manager.handle_custom_message(other_request_msg, other_node_id).unwrap();
+
+ match service_node.liquidity_manager.next_event().unwrap() {
+ LiquidityEvent::LSPS2Service(LSPS2ServiceEvent::GetInfo { .. }) => {},
+ _ => panic!("Unexpected event"),
+ }
+}
+
#[test]
fn invalid_token_flow() {
let chanmon_cfgs = create_chanmon_cfgs(2);Why this scored 76/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.