What changed, and why it matters
This commit fixes a math overflow bug in the database retry logic of LND, a Bitcoin Lightning Network node. When retrying failed database operations, the code could calculate a wait time so large that it wrapped around to a negative number, potentially causing immediate retries, unexpected behavior, or node instability. The fix caps each step of the calculation and uses safer integer math. It is a defensive hardening change rather than a directly exploitable remote vulnerability.
Treat as a hardening fix. Review whether any production configuration paths allow user-controlled or unusually large retry-delay parameters, and backport the fix to release branches. No immediate emergency response is indicated unless an attacker can control retry parameters or trigger persistent DB errors to exploit timing side effects.
Security signals we found
Integer overflow in retry delay calculation
Use of floating-point math.Pow for integer exponential backoff
Multiplication before bounds check enabling overflow
Negative or truncated time.Duration possible from overflow
Database retry loop behavior change from overflowed delay
Defensive bounds checking added at each arithmetic step
Evidence from the diff
The randRetryDelay function in sqldb/interfaces.go and sqldb/v2/interfaces.go computes a jittered exponential backoff for database transaction retries. The old implementation used math.Pow(2, attempt) as a time.Duration multiplier and multiplied before capping against maxRetryDelay. With large initialRetryDelay, maxRetryDelay, or attempt values, intermediate values could overflow time.Duration (a signed 64-bit integer on 64-bit platforms), producing negative or unexpectedly small delays. The new code checks bounds before each arithmetic operation (halfDelay, jitter addition, and factor multiplication), replaces the floating-point power with a bounded bit shift on int64, and returns maxRetryDelay whenever a cap would be exceeded. Tests are added to verify capping and non-overflow for extreme inputs.
Changed components
sqldb/interfaces.go: randRetryDelaysqldb/v2/interfaces.go: randRetryDelayDatabase transaction retry executorLND node database layerInspect captured patch +175 / −23
### sqldb/interfaces.go
@@ -4,7 +4,6 @@ import (
"context"
"database/sql"
"fmt"
- "math"
"math/rand"
prand "math/rand"
"time"
@@ -197,13 +196,26 @@ func randRetryDelay(initialRetryDelay, maxRetryDelay time.Duration,
attempt int) time.Duration {
halfDelay := initialRetryDelay / 2
- randDelay := rand.Int63n(int64(initialRetryDelay)) //nolint:gosec
+
+ // Cap every attempt, including the first, at maxRetryDelay. Check the
+ // operands before adding them so calculating the jittered delay cannot
+ // overflow time.Duration.
+ if halfDelay >= maxRetryDelay {
+ return maxRetryDelay
+ }
+
+ randDelay := time.Duration(
+ rand.Int63n(int64(initialRetryDelay)), //nolint:gosec
+ )
+ if randDelay >= maxRetryDelay-halfDelay {
+ return maxRetryDelay
+ }
// 50% plus 0%-100% gives us the range of 50%-150%.
- initialDelay := halfDelay + time.Duration(randDelay)
+ initialDelay := halfDelay + randDelay
// If this is the first attempt, we just return the initial delay.
- if attempt == 0 {
+ if attempt <= 0 {
return initialDelay
}
@@ -212,15 +224,15 @@ func randRetryDelay(initialRetryDelay, maxRetryDelay time.Duration,
// attempt. If we double something n times, that's the same as
// multiplying the value with 2^n. We limit the power to 32 to avoid
// overflows.
- factor := time.Duration(math.Pow(2, min(float64(attempt), 32)))
- actualDelay := initialDelay * factor
+ factor := int64(1) << min(attempt, 32)
- // Cap the delay at the maximum configured value.
- if actualDelay > maxRetryDelay {
+ // Compare against the divided maximum because multiplying first could
+ // overflow before we get a chance to cap the result.
+ if int64(initialDelay) > int64(maxRetryDelay)/factor {
return maxRetryDelay
}
- return actualDelay
+ return time.Duration(int64(initialDelay) * factor)
}
// MakeTx is a function that creates a new transaction. It returns a Tx and an
### sqldb/interfaces_test.go
@@ -0,0 +1,69 @@
+package sqldb
+
+import (
+ "math"
+ "testing"
+ "time"
+
+ "github.com/stretchr/testify/require"
+)
+
+// TestRandRetryDelay verifies that retry delays stay within their jitter range
+// and are capped without overflowing for large retry settings.
+func TestRandRetryDelay(t *testing.T) {
+ t.Parallel()
+
+ t.Run("initial jitter", func(t *testing.T) {
+ delay := randRetryDelay(
+ DefaultRetryDelay, DefaultMaxRetryDelay, 0,
+ )
+
+ require.GreaterOrEqual(
+ t, delay, DefaultRetryDelay/2,
+ )
+ require.LessOrEqual(
+ t, delay, DefaultRetryDelay*3/2,
+ )
+ })
+
+ t.Run("uncapped exponential backoff", func(t *testing.T) {
+ delay := randRetryDelay(
+ DefaultRetryDelay, DefaultMaxRetryDelay, 2,
+ )
+
+ require.GreaterOrEqual(t, delay, DefaultRetryDelay*2)
+ require.Less(t, delay, DefaultRetryDelay*6)
+ })
+
+ t.Run("exponential backoff reaches maximum", func(t *testing.T) {
+ delay := randRetryDelay(
+ DefaultRetryDelay, DefaultMaxRetryDelay, 6,
+ )
+
+ require.Equal(t, DefaultMaxRetryDelay, delay)
+ })
+
+ t.Run("large initial delay is capped", func(t *testing.T) {
+ delay := randRetryDelay(
+ DefaultMaxRetryDelay*4, DefaultMaxRetryDelay, 0,
+ )
+
+ require.Equal(t, DefaultMaxRetryDelay, delay)
+ })
+
+ t.Run("large retry count does not overflow", func(t *testing.T) {
+ delay := randRetryDelay(
+ DefaultMaxRetryDelay, DefaultMaxRetryDelay, 32,
+ )
+
+ require.Equal(t, DefaultMaxRetryDelay, delay)
+ })
+
+ t.Run("jitter addition does not overflow", func(t *testing.T) {
+ delay := randRetryDelay(
+ time.Duration(math.MaxInt64), DefaultMaxRetryDelay, 0,
+ )
+
+ require.Equal(t, DefaultMaxRetryDelay, delay)
+ })
+}
### sqldb/v2/interfaces.go
@@ -4,7 +4,6 @@ import (
"context"
"database/sql"
"fmt"
- "math"
"math/rand"
"time"
)
@@ -219,13 +218,26 @@ func randRetryDelay(initialRetryDelay, maxRetryDelay time.Duration,
attempt int) time.Duration {
halfDelay := initialRetryDelay / 2
- randDelay := rand.Int63n(int64(initialRetryDelay)) //nolint:gosec
+
+ // Cap every attempt, including the first, at maxRetryDelay. Check the
+ // operands before adding them so calculating the jittered delay cannot
+ // overflow time.Duration.
+ if halfDelay >= maxRetryDelay {
+ return maxRetryDelay
+ }
+
+ randDelay := time.Duration(
+ rand.Int63n(int64(initialRetryDelay)), //nolint:gosec
+ )
+ if randDelay >= maxRetryDelay-halfDelay {
+ return maxRetryDelay
+ }
// 50% plus 0%-100% gives us the range of 50%-150%.
- initialDelay := halfDelay + time.Duration(randDelay)
+ initialDelay := halfDelay + randDelay
// If this is the first attempt, we just return the initial delay.
- if attempt == 0 {
+ if attempt <= 0 {
return initialDelay
}
@@ -234,20 +246,15 @@ func randRetryDelay(initialRetryDelay, maxRetryDelay time.Duration,
// attempt. If we double something n times, that's the same as
// multiplying the value with 2^n. We limit the power to 32 to avoid
// overflows.
- factor := time.Duration(math.Pow(2, min(float64(attempt), 32)))
-
- // The factor is a plain multiplier and not a duration, so multiplying
- // it with the initial delay is intended.
- //
- //nolint:durationcheck
- actualDelay := initialDelay * factor
+ factor := int64(1) << min(attempt, 32)
- // Cap the delay at the maximum configured value.
- if actualDelay > maxRetryDelay {
+ // Compare against the divided maximum because multiplying first could
+ // overflow before we get a chance to cap the result.
+ if int64(initialDelay) > int64(maxRetryDelay)/factor {
return maxRetryDelay
}
- return actualDelay
+ return time.Duration(int64(initialDelay) * factor)
}
// MakeTx is a function that creates a new transaction. It returns a Tx and an
### sqldb/v2/interfaces_test.go
@@ -3,7 +3,9 @@ package sqldb
import (
"context"
"database/sql"
+ "math"
"testing"
+ "time"
"github.com/stretchr/testify/require"
)
@@ -46,3 +48,65 @@ func TestTransactionExecutorBackend(t *testing.T) {
require.Equal(t, BackendTypePostgres, executor.Backend())
}
+
+// TestRandRetryDelay verifies that retry delays stay within their jitter range
+// and are capped without overflowing for large retry settings.
+func TestRandRetryDelay(t *testing.T) {
+ t.Parallel()
+
+ t.Run("initial jitter", func(t *testing.T) {
+ delay := randRetryDelay(
+ DefaultInitialRetryDelay, DefaultMaxRetryDelay, 0,
+ )
+
+ require.GreaterOrEqual(
+ t, delay, DefaultInitialRetryDelay/2,
+ )
+ require.LessOrEqual(
+ t, delay, DefaultInitialRetryDelay*3/2,
+ )
+ })
+
+ t.Run("uncapped exponential backoff", func(t *testing.T) {
+ delay := randRetryDelay(
+ DefaultInitialRetryDelay, DefaultMaxRetryDelay, 2,
+ )
+
+ require.GreaterOrEqual(
+ t, delay, DefaultInitialRetryDelay*2,
+ )
+ require.Less(t, delay, DefaultInitialRetryDelay*6)
+ })
+
+ t.Run("exponential backoff reaches maximum", func(t *testing.T) {
+ delay := randRetryDelay(
+ DefaultInitialRetryDelay, DefaultMaxRetryDelay, 8,
+ )
+
+ require.Equal(t, DefaultMaxRetryDelay, delay)
+ })
+
+ t.Run("large initial delay is capped", func(t *testing.T) {
+ delay := randRetryDelay(
+ DefaultMaxRetryDelay*4, DefaultMaxRetryDelay, 0,
+ )
+
+ require.Equal(t, DefaultMaxRetryDelay, delay)
+ })
+
+ t.Run("large retry count does not overflow", func(t *testing.T) {
+ delay := randRetryDelay(
+ DefaultMaxRetryDelay, DefaultMaxRetryDelay, 32,
+ )
+
+ require.Equal(t, DefaultMaxRetryDelay, delay)
+ })
+
+ t.Run("jitter addition does not overflow", func(t *testing.T) {
+ delay := randRetryDelay(
+ time.Duration(math.MaxInt64), DefaultMaxRetryDelay, 0,
+ )
+
+ require.Equal(t, DefaultMaxRetryDelay, delay)
+ })
+}Why this scored 36/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.