|
| 1 | +From aa710e1624a59109fa963de2afc047e411f2c268 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Kanishk-Bansal <kbkanishk975@gmail.com> |
| 3 | +Date: Fri, 14 Feb 2025 12:37:21 +0000 |
| 4 | +Subject: [PATCH] CVE-2023-39325 |
| 5 | + |
| 6 | +--- |
| 7 | + vendor/golang.org/x/net/http2/server.go | 66 ++++++++++++++++++++++++- |
| 8 | + 1 file changed, 64 insertions(+), 2 deletions(-) |
| 9 | + |
| 10 | +diff --git a/vendor/golang.org/x/net/http2/server.go b/vendor/golang.org/x/net/http2/server.go |
| 11 | +index 8cb14f3..6000140 100644 |
| 12 | +--- a/vendor/golang.org/x/net/http2/server.go |
| 13 | ++++ b/vendor/golang.org/x/net/http2/server.go |
| 14 | +@@ -581,9 +581,11 @@ type serverConn struct { |
| 15 | + advMaxStreams uint32 // our SETTINGS_MAX_CONCURRENT_STREAMS advertised the client |
| 16 | + curClientStreams uint32 // number of open streams initiated by the client |
| 17 | + curPushedStreams uint32 // number of open streams initiated by server push |
| 18 | ++ curHandlers uint32 // number of running handler goroutines |
| 19 | + maxClientStreamID uint32 // max ever seen from client (odd), or 0 if there have been no client requests |
| 20 | + maxPushPromiseID uint32 // ID of the last push promise (even), or 0 if there have been no pushes |
| 21 | + streams map[uint32]*stream |
| 22 | ++ unstartedHandlers []unstartedHandler |
| 23 | + initialStreamSendWindowSize int32 |
| 24 | + maxFrameSize int32 |
| 25 | + peerMaxHeaderListSize uint32 // zero means unknown (default) |
| 26 | +@@ -981,6 +983,8 @@ func (sc *serverConn) serve() { |
| 27 | + return |
| 28 | + case gracefulShutdownMsg: |
| 29 | + sc.startGracefulShutdownInternal() |
| 30 | ++ case handlerDoneMsg: |
| 31 | ++ sc.handlerDone() |
| 32 | + default: |
| 33 | + panic("unknown timer") |
| 34 | + } |
| 35 | +@@ -1028,6 +1032,7 @@ var ( |
| 36 | + idleTimerMsg = new(serverMessage) |
| 37 | + shutdownTimerMsg = new(serverMessage) |
| 38 | + gracefulShutdownMsg = new(serverMessage) |
| 39 | ++ handlerDoneMsg = new(serverMessage) |
| 40 | + ) |
| 41 | + |
| 42 | + func (sc *serverConn) onSettingsTimer() { sc.sendServeMsg(settingsTimerMsg) } |
| 43 | +@@ -2022,8 +2027,7 @@ func (sc *serverConn) processHeaders(f *MetaHeadersFrame) error { |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | +- go sc.runHandler(rw, req, handler) |
| 48 | +- return nil |
| 49 | ++ return sc.scheduleHandler(id, rw, req, handler) |
| 50 | + } |
| 51 | + |
| 52 | + func (sc *serverConn) upgradeRequest(req *http.Request) { |
| 53 | +@@ -2043,6 +2047,10 @@ func (sc *serverConn) upgradeRequest(req *http.Request) { |
| 54 | + sc.conn.SetReadDeadline(time.Time{}) |
| 55 | + } |
| 56 | + |
| 57 | ++ // This is the first request on the connection, |
| 58 | ++ // so start the handler directly rather than going |
| 59 | ++ // through scheduleHandler. |
| 60 | ++ sc.curHandlers++ |
| 61 | + go sc.runHandler(rw, req, sc.handler.ServeHTTP) |
| 62 | + } |
| 63 | + |
| 64 | +@@ -2283,8 +2291,62 @@ func (sc *serverConn) newResponseWriter(st *stream, req *http.Request) *response |
| 65 | + return &responseWriter{rws: rws} |
| 66 | + } |
| 67 | + |
| 68 | ++type unstartedHandler struct { |
| 69 | ++ streamID uint32 |
| 70 | ++ rw *responseWriter |
| 71 | ++ req *http.Request |
| 72 | ++ handler func(http.ResponseWriter, *http.Request) |
| 73 | ++} |
| 74 | ++ |
| 75 | ++// scheduleHandler starts a handler goroutine, |
| 76 | ++// or schedules one to start as soon as an existing handler finishes. |
| 77 | ++func (sc *serverConn) scheduleHandler(streamID uint32, rw *responseWriter, req *http.Request, handler func(http.ResponseWriter, *http.Request)) error { |
| 78 | ++ sc.serveG.check() |
| 79 | ++ maxHandlers := sc.advMaxStreams |
| 80 | ++ if sc.curHandlers < maxHandlers { |
| 81 | ++ sc.curHandlers++ |
| 82 | ++ go sc.runHandler(rw, req, handler) |
| 83 | ++ return nil |
| 84 | ++ } |
| 85 | ++ if len(sc.unstartedHandlers) > int(4*sc.advMaxStreams) { |
| 86 | ++ return sc.countError("too_many_early_resets", ConnectionError(ErrCodeEnhanceYourCalm)) |
| 87 | ++ } |
| 88 | ++ sc.unstartedHandlers = append(sc.unstartedHandlers, unstartedHandler{ |
| 89 | ++ streamID: streamID, |
| 90 | ++ rw: rw, |
| 91 | ++ req: req, |
| 92 | ++ handler: handler, |
| 93 | ++ }) |
| 94 | ++ return nil |
| 95 | ++} |
| 96 | ++ |
| 97 | ++func (sc *serverConn) handlerDone() { |
| 98 | ++ sc.serveG.check() |
| 99 | ++ sc.curHandlers-- |
| 100 | ++ i := 0 |
| 101 | ++ maxHandlers := sc.advMaxStreams |
| 102 | ++ for ; i < len(sc.unstartedHandlers); i++ { |
| 103 | ++ u := sc.unstartedHandlers[i] |
| 104 | ++ if sc.streams[u.streamID] == nil { |
| 105 | ++ // This stream was reset before its goroutine had a chance to start. |
| 106 | ++ continue |
| 107 | ++ } |
| 108 | ++ if sc.curHandlers >= maxHandlers { |
| 109 | ++ break |
| 110 | ++ } |
| 111 | ++ sc.curHandlers++ |
| 112 | ++ go sc.runHandler(u.rw, u.req, u.handler) |
| 113 | ++ sc.unstartedHandlers[i] = unstartedHandler{} // don't retain references |
| 114 | ++ } |
| 115 | ++ sc.unstartedHandlers = sc.unstartedHandlers[i:] |
| 116 | ++ if len(sc.unstartedHandlers) == 0 { |
| 117 | ++ sc.unstartedHandlers = nil |
| 118 | ++ } |
| 119 | ++} |
| 120 | ++ |
| 121 | + // Run on its own goroutine. |
| 122 | + func (sc *serverConn) runHandler(rw *responseWriter, req *http.Request, handler func(http.ResponseWriter, *http.Request)) { |
| 123 | ++ defer sc.sendServeMsg(handlerDoneMsg) |
| 124 | + didPanic := true |
| 125 | + defer func() { |
| 126 | + rw.rws.stream.cancelCtx() |
| 127 | +-- |
| 128 | +2.45.2 |
| 129 | + |
0 commit comments