Skip to content

Commit 68b0397

Browse files
enjk8s-publishing-bot
authored andcommitted
Prevent rapid reset http2 DOS on API server
This change fully addresses CVE-2023-44487 and CVE-2023-39325 for the API server when the client is unauthenticated. The changes to util/runtime are required because otherwise a large number of requests can get blocked on the time.Sleep calls. For unauthenticated clients (either via 401 or the anonymous user), we simply no longer allow such clients to hold open http2 connections. They can use http2, but with the performance of http1 (with keep-alive disabled). Since this change has the potential to cause issues, the UnauthenticatedHTTP2DOSMitigation feature gate can be disabled to remove this protection (it is enabled by default). For example, when the API server is fronted by an L7 load balancer that is set up to mitigate http2 attacks, unauthenticated clients could force disable connection reuse between the load balancer and the API server (many incoming connections could share the same backend connection). An API server that is on a private network may opt to disable this protection to prevent performance regressions for unauthenticated clients. For all other clients, we rely on the golang.org/x/net fix in golang/net@b225e7c That change is not sufficient to adequately protect against a motivated client - future changes to Kube and/or golang.org/x/net will be explored to address this gap. The Kube API server now uses a max stream of 100 instead of 250 (this matches the Go http2 client default). This lowers the abuse limit from 1000 to 400. Signed-off-by: Monis Khan <[email protected]> Kubernetes-commit: 238d89c9a068dcd7ab994be1b3e646ce8d296ef8
1 parent 1f3a244 commit 68b0397

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

pkg/util/runtime/runtime.go

+9-6
Original file line numberDiff line numberDiff line change
@@ -126,14 +126,17 @@ type rudimentaryErrorBackoff struct {
126126
// OnError will block if it is called more often than the embedded period time.
127127
// This will prevent overly tight hot error loops.
128128
func (r *rudimentaryErrorBackoff) OnError(error) {
129+
now := time.Now() // start the timer before acquiring the lock
129130
r.lastErrorTimeLock.Lock()
130-
defer r.lastErrorTimeLock.Unlock()
131-
d := time.Since(r.lastErrorTime)
132-
if d < r.minPeriod {
133-
// If the time moves backwards for any reason, do nothing
134-
time.Sleep(r.minPeriod - d)
135-
}
131+
d := now.Sub(r.lastErrorTime)
136132
r.lastErrorTime = time.Now()
133+
r.lastErrorTimeLock.Unlock()
134+
135+
// Do not sleep with the lock held because that causes all callers of HandleError to block.
136+
// We only want the current goroutine to block.
137+
// A negative or zero duration causes time.Sleep to return immediately.
138+
// If the time moves backwards for any reason, do nothing.
139+
time.Sleep(r.minPeriod - d)
137140
}
138141

139142
// GetCaller returns the caller of the function that calls it.

pkg/util/runtime/runtime_test.go

+26
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ import (
2424
"os"
2525
"regexp"
2626
"strings"
27+
"sync"
2728
"testing"
29+
"time"
2830
)
2931

3032
func TestHandleCrash(t *testing.T) {
@@ -156,3 +158,27 @@ func captureStderr(f func()) (string, error) {
156158

157159
return <-resultCh, nil
158160
}
161+
162+
func Test_rudimentaryErrorBackoff_OnError_ParallelSleep(t *testing.T) {
163+
r := &rudimentaryErrorBackoff{
164+
minPeriod: time.Second,
165+
}
166+
167+
start := make(chan struct{})
168+
var wg sync.WaitGroup
169+
for i := 0; i < 30; i++ {
170+
wg.Add(1)
171+
go func() {
172+
<-start
173+
r.OnError(nil) // input error is ignored
174+
wg.Done()
175+
}()
176+
}
177+
st := time.Now()
178+
close(start)
179+
wg.Wait()
180+
181+
if since := time.Since(st); since > 5*time.Second {
182+
t.Errorf("OnError slept for too long: %s", since)
183+
}
184+
}

0 commit comments

Comments
 (0)