mirror of
https://github.com/caddyserver/caddy.git
synced 2026-11-20 14:59:36 +00:00
caddyhttp: fix min_rate credit overflow on transfers over 9.2 GB (#8396)
Some checks are pending
Tests / test (./cmd/caddy/caddy, ~1.27.0, macos-latest, 0, 1.27, mac) (push) Waiting to run
Tests / test (./cmd/caddy/caddy, ~1.27.0, ubuntu-latest, 0, 1.27, linux) (push) Waiting to run
Tests / test (./cmd/caddy/caddy.exe, ~1.27.0, windows-latest, True, 1.27, windows) (push) Waiting to run
Tests / test (s390x on IBM Z) (push) Waiting to run
Tests / goreleaser-check (push) Waiting to run
Cross-Build / build (~1.26.0, 1.26, aix) (push) Waiting to run
Cross-Build / build (~1.26.0, 1.26, darwin) (push) Waiting to run
Cross-Build / build (~1.26.0, 1.26, dragonfly) (push) Waiting to run
Cross-Build / build (~1.26.0, 1.26, freebsd) (push) Waiting to run
Cross-Build / build (~1.26.0, 1.26, illumos) (push) Waiting to run
Cross-Build / build (~1.26.0, 1.26, linux) (push) Waiting to run
Cross-Build / build (~1.26.0, 1.26, netbsd) (push) Waiting to run
Cross-Build / build (~1.26.0, 1.26, openbsd) (push) Waiting to run
Cross-Build / build (~1.26.0, 1.26, solaris) (push) Waiting to run
Cross-Build / build (~1.26.0, 1.26, windows) (push) Waiting to run
Lint / lint (push) Waiting to run
Lint / lint-1 (push) Waiting to run
Lint / lint-2 (push) Waiting to run
Lint / govulncheck (push) Waiting to run
Lint / dependency-review (push) Waiting to run
OpenSSF Scorecard supply-chain security / Scorecard analysis (push) Waiting to run
Some checks are pending
Tests / test (./cmd/caddy/caddy, ~1.27.0, macos-latest, 0, 1.27, mac) (push) Waiting to run
Tests / test (./cmd/caddy/caddy, ~1.27.0, ubuntu-latest, 0, 1.27, linux) (push) Waiting to run
Tests / test (./cmd/caddy/caddy.exe, ~1.27.0, windows-latest, True, 1.27, windows) (push) Waiting to run
Tests / test (s390x on IBM Z) (push) Waiting to run
Tests / goreleaser-check (push) Waiting to run
Cross-Build / build (~1.26.0, 1.26, aix) (push) Waiting to run
Cross-Build / build (~1.26.0, 1.26, darwin) (push) Waiting to run
Cross-Build / build (~1.26.0, 1.26, dragonfly) (push) Waiting to run
Cross-Build / build (~1.26.0, 1.26, freebsd) (push) Waiting to run
Cross-Build / build (~1.26.0, 1.26, illumos) (push) Waiting to run
Cross-Build / build (~1.26.0, 1.26, linux) (push) Waiting to run
Cross-Build / build (~1.26.0, 1.26, netbsd) (push) Waiting to run
Cross-Build / build (~1.26.0, 1.26, openbsd) (push) Waiting to run
Cross-Build / build (~1.26.0, 1.26, solaris) (push) Waiting to run
Cross-Build / build (~1.26.0, 1.26, windows) (push) Waiting to run
Lint / lint (push) Waiting to run
Lint / lint-1 (push) Waiting to run
Lint / lint-2 (push) Waiting to run
Lint / govulncheck (push) Waiting to run
Lint / dependency-review (push) Waiting to run
OpenSSF Scorecard supply-chain security / Scorecard analysis (push) Waiting to run
IdleDeadline.next computed the min_rate allowance as transferred * time.Second / MinRate, which overflows int64 once a request body or response passes about 9.2 GB. The credit goes negative, the deadline lands in the past, and the connection is cut even though the client is far above the configured rate. Compute the credit with a 128-bit intermediate and saturate instead.
This commit is contained in:
parent
a97bc460cf
commit
f458aa0081
2 changed files with 59 additions and 2 deletions
|
|
@ -18,6 +18,8 @@ import (
|
|||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"math"
|
||||
"math/bits"
|
||||
"net/http"
|
||||
"os"
|
||||
"sync"
|
||||
|
|
@ -64,8 +66,11 @@ type IdleDeadline struct {
|
|||
|
||||
func (d *IdleDeadline) next() (deadline time.Time) {
|
||||
if d.MinRate > 0 {
|
||||
credit := time.Duration(d.transferred) * time.Second / time.Duration(d.MinRate)
|
||||
deadline = d.Start.Add(d.Timeout + credit)
|
||||
allowance := d.Timeout + rateCredit(d.transferred, d.MinRate)
|
||||
if allowance < d.Timeout { // the sum overflowed
|
||||
allowance = math.MaxInt64
|
||||
}
|
||||
deadline = d.Start.Add(allowance)
|
||||
} else {
|
||||
deadline = time.Now().Add(d.Timeout)
|
||||
}
|
||||
|
|
@ -76,6 +81,25 @@ func (d *IdleDeadline) next() (deadline time.Time) {
|
|||
return
|
||||
}
|
||||
|
||||
// rateCredit returns how long transferring n bytes may take at rate
|
||||
// bytes/second, saturating instead of overflowing: n * time.Second exceeds
|
||||
// int64 once n passes about 9.2 GB, which would turn the credit negative
|
||||
// and abort large transfers that easily sustain the rate.
|
||||
func rateCredit(n, rate int64) time.Duration {
|
||||
if n <= 0 {
|
||||
return 0
|
||||
}
|
||||
hi, lo := bits.Mul64(uint64(n), uint64(time.Second))
|
||||
if hi >= uint64(rate) {
|
||||
return math.MaxInt64
|
||||
}
|
||||
q, _ := bits.Div64(hi, lo, uint64(rate))
|
||||
if q > math.MaxInt64 {
|
||||
return math.MaxInt64
|
||||
}
|
||||
return time.Duration(q)
|
||||
}
|
||||
|
||||
// idleOverride holds what restore needs to undo an override.
|
||||
type idleOverride struct {
|
||||
prev IdleDeadline
|
||||
|
|
|
|||
|
|
@ -20,6 +20,7 @@ import (
|
|||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"math"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
|
|
@ -1605,3 +1606,35 @@ func TestIdleTimeoutWriterRestoresHardDeadline(t *testing.T) {
|
|||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestIdleDeadlineMinRateLargeTransfer(t *testing.T) {
|
||||
start := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)
|
||||
const gb = 1_000_000_000
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
transferred int64
|
||||
minRate int64
|
||||
want time.Duration // allowance past start, excluding Timeout
|
||||
}{
|
||||
{"small", 5 * 1024 * 1024, 1024 * 1024, 5 * time.Second},
|
||||
{"just below overflow", 9 * gb, gb, 9 * time.Second},
|
||||
{"10 GB at 1 MB/s", 10 * gb, 1_000_000, 10_000 * time.Second},
|
||||
{"100 GB at 1 GB/s", 100 * gb, gb, 100 * time.Second},
|
||||
{"1 TB at 1 B/s", 1000 * gb, 1, math.MaxInt64},
|
||||
{"max int64 bytes", math.MaxInt64, 1, math.MaxInt64},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
d := IdleDeadline{Start: start, Timeout: time.Minute, MinRate: tc.minRate, transferred: tc.transferred}
|
||||
got := d.next()
|
||||
if tc.want == math.MaxInt64 {
|
||||
if !got.After(start.Add(100 * 365 * 24 * time.Hour)) {
|
||||
t.Fatalf("deadline = %v, want far in the future", got)
|
||||
}
|
||||
return
|
||||
}
|
||||
if want := start.Add(time.Minute + tc.want); !got.Equal(want) {
|
||||
t.Fatalf("deadline = %v, want %v", got, want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue