forked from kubernetes/kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 112
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[release-4.10] UPSTREAM: 121120: Prevent rapid reset http2 DOS on API server #1756
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -143,13 +143,13 @@ func (t *timeoutHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | |
}() | ||
|
||
postTimeoutFn() | ||
tw.timeout(err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cherry-pick conflict: in 4.11+ this is a little different. PTAL at the other patchsets and let me know if my resolution here looks appropriate. |
||
tw.timeout(r, err) | ||
} | ||
} | ||
|
||
type timeoutWriter interface { | ||
http.ResponseWriter | ||
timeout(*apierrors.StatusError) | ||
timeout(*http.Request, *apierrors.StatusError) | ||
} | ||
|
||
func newTimeoutWriter(w http.ResponseWriter) (timeoutWriter, http.ResponseWriter) { | ||
|
@@ -242,7 +242,7 @@ func copyHeaders(dst, src http.Header) { | |
} | ||
} | ||
|
||
func (tw *baseTimeoutWriter) timeout(err *apierrors.StatusError) { | ||
func (tw *baseTimeoutWriter) timeout(r *http.Request, err *apierrors.StatusError) { | ||
tw.mu.Lock() | ||
defer tw.mu.Unlock() | ||
|
||
|
@@ -252,6 +252,14 @@ func (tw *baseTimeoutWriter) timeout(err *apierrors.StatusError) { | |
// We can safely timeout the HTTP request by sending by a timeout | ||
// handler | ||
if !tw.wroteHeader && !tw.hijacked { | ||
// http2 is an expensive protocol that is prone to abuse, | ||
// see CVE-2023-44487 and CVE-2023-39325 for an example. | ||
// Do not allow clients to reset these connections | ||
// prematurely as that can trivially OOM the api server | ||
// (i.e. basically degrade them to http1). | ||
if isLikelyEarlyHTTP2Reset(r) { | ||
tw.w.Header().Set("Connection", "close") | ||
} | ||
tw.w.WriteHeader(http.StatusGatewayTimeout) | ||
enc := json.NewEncoder(tw.w) | ||
enc.Encode(&err.ErrStatus) | ||
|
@@ -274,6 +282,24 @@ func (tw *baseTimeoutWriter) timeout(err *apierrors.StatusError) { | |
} | ||
} | ||
|
||
// isLikelyEarlyHTTP2Reset returns true if an http2 stream was reset before the request deadline. | ||
// Note that this does not prevent a client from trying to create more streams than the configured | ||
// max, but https://github.com/golang/net/commit/b225e7ca6dde1ef5a5ae5ce922861bda011cfabd protects | ||
// us from abuse via that vector. | ||
func isLikelyEarlyHTTP2Reset(r *http.Request) bool { | ||
if r.ProtoMajor != 2 { | ||
return false | ||
} | ||
|
||
deadline, ok := r.Context().Deadline() | ||
if !ok { | ||
return true // this context had no deadline but was canceled meaning the client likely reset it early | ||
} | ||
|
||
// this context was canceled before its deadline meaning the client likely reset it early | ||
return time.Now().Before(deadline) | ||
} | ||
|
||
func (tw *baseTimeoutWriter) CloseNotify() <-chan bool { | ||
tw.mu.Lock() | ||
defer tw.mu.Unlock() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cherry-pick conflict: in 4.11+ there is some code here to clear some headers, but it's not in 4.10. PTAL at the other patchsets and let me know if my resolution here looks appropriate.