|
| 1 | +From 18bc0c1f8e741738490aa0a8415c372db4b20d62 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Muhammad Falak R Wani < [email protected]> |
| 3 | +Date: Tue, 23 Apr 2024 10:07:19 +0530 |
| 4 | +Subject: [PATCH] http2: close connections when receiving too many headers |
| 5 | + |
| 6 | +Adapted by @mfrw to apply on vendor directory for v0.17 to drop test |
| 7 | +files |
| 8 | + |
| 9 | +Maintaining HPACK state requires that we parse and process |
| 10 | +all HEADERS and CONTINUATION frames on a connection. |
| 11 | +When a request's headers exceed MaxHeaderBytes, we don't |
| 12 | +allocate memory to store the excess headers but we do |
| 13 | +parse them. This permits an attacker to cause an HTTP/2 |
| 14 | +endpoint to read arbitrary amounts of data, all associated |
| 15 | +with a request which is going to be rejected. |
| 16 | +Set a limit on the amount of excess header frames we |
| 17 | +will process before closing a connection. |
| 18 | + |
| 19 | +Thanks to Bartek Nowotarski for reporting this issue. |
| 20 | + |
| 21 | +Fixes CVE-2023-45288 |
| 22 | +Fixes golang/go#65051 |
| 23 | + |
| 24 | +Change-Id: I15df097268df13bb5a9e9d3a5c04a8a141d850f6 |
| 25 | +Reviewed-on: https://team-review.git.corp.google.com/c/golang/go-private/+/2130527 |
| 26 | +Reviewed-by: Roland Shoemaker < [email protected]> |
| 27 | +Reviewed-by: Tatiana Bradley < [email protected]> |
| 28 | +Reviewed-on: https://go-review.googlesource.com/c/net/+/576155 |
| 29 | +Reviewed-by: Dmitri Shuralyov < [email protected]> |
| 30 | +Auto-Submit: Dmitri Shuralyov < [email protected]> |
| 31 | +Reviewed-by: Than McIntosh < [email protected]> |
| 32 | +LUCI-TryBot-Result: Go LUCI < [email protected]> |
| 33 | +Signed-off-by: Muhammad Falak R Wani < [email protected]> |
| 34 | +--- |
| 35 | + vendor/golang.org/x/net/http2/frame.go | 31 ++++++++++++++++++++++++++ |
| 36 | + 1 file changed, 31 insertions(+) |
| 37 | + |
| 38 | +diff --git a/vendor/golang.org/x/net/http2/frame.go b/vendor/golang.org/x/net/http2/frame.go |
| 39 | +index c1f6b90..175c154 100644 |
| 40 | +--- a/vendor/golang.org/x/net/http2/frame.go |
| 41 | ++++ b/vendor/golang.org/x/net/http2/frame.go |
| 42 | +@@ -1565,6 +1565,7 @@ func (fr *Framer) readMetaFrame(hf *HeadersFrame) (*MetaHeadersFrame, error) { |
| 43 | + if size > remainSize { |
| 44 | + hdec.SetEmitEnabled(false) |
| 45 | + mh.Truncated = true |
| 46 | ++ remainSize = 0 |
| 47 | + return |
| 48 | + } |
| 49 | + remainSize -= size |
| 50 | +@@ -1577,6 +1578,36 @@ func (fr *Framer) readMetaFrame(hf *HeadersFrame) (*MetaHeadersFrame, error) { |
| 51 | + var hc headersOrContinuation = hf |
| 52 | + for { |
| 53 | + frag := hc.HeaderBlockFragment() |
| 54 | ++ |
| 55 | ++ // Avoid parsing large amounts of headers that we will then discard. |
| 56 | ++ // If the sender exceeds the max header list size by too much, |
| 57 | ++ // skip parsing the fragment and close the connection. |
| 58 | ++ // |
| 59 | ++ // "Too much" is either any CONTINUATION frame after we've already |
| 60 | ++ // exceeded the max header list size (in which case remainSize is 0), |
| 61 | ++ // or a frame whose encoded size is more than twice the remaining |
| 62 | ++ // header list bytes we're willing to accept. |
| 63 | ++ if int64(len(frag)) > int64(2*remainSize) { |
| 64 | ++ if VerboseLogs { |
| 65 | ++ log.Printf("http2: header list too large") |
| 66 | ++ } |
| 67 | ++ // It would be nice to send a RST_STREAM before sending the GOAWAY, |
| 68 | ++ // but the struture of the server's frame writer makes this difficult. |
| 69 | ++ return nil, ConnectionError(ErrCodeProtocol) |
| 70 | ++ } |
| 71 | ++ |
| 72 | ++ // Also close the connection after any CONTINUATION frame following an |
| 73 | ++ // invalid header, since we stop tracking the size of the headers after |
| 74 | ++ // an invalid one. |
| 75 | ++ if invalid != nil { |
| 76 | ++ if VerboseLogs { |
| 77 | ++ log.Printf("http2: invalid header: %v", invalid) |
| 78 | ++ } |
| 79 | ++ // It would be nice to send a RST_STREAM before sending the GOAWAY, |
| 80 | ++ // but the struture of the server's frame writer makes this difficult. |
| 81 | ++ return nil, ConnectionError(ErrCodeProtocol) |
| 82 | ++ } |
| 83 | ++ |
| 84 | + if _, err := hdec.Write(frag); err != nil { |
| 85 | + return nil, ConnectionError(ErrCodeCompression) |
| 86 | + } |
| 87 | +-- |
| 88 | +2.40.1 |
| 89 | + |
0 commit comments