|
| 1 | +From 04646b47d5f03ab96a681931a1b93cd12209e6bf Mon Sep 17 00:00:00 2001 |
| 2 | +From: Cameron Baird <cameronbaird@microsoft.com> |
| 3 | +Date: Fri, 12 Jul 2024 20:49:14 +0000 |
| 4 | +Subject: [PATCH 3/3] CVE-2022-41723 |
| 5 | + |
| 6 | +Upstream fix: I58a743df450a4a4923dddd5cf6bb0592b0a7bdf3 |
| 7 | +http2/hpack: avoid quadratic complexity in hpack decoding |
| 8 | + |
| 9 | +When parsing a field literal containing two Huffman-encoded strings, |
| 10 | +don't decode the first string until verifying all data is present. |
| 11 | +Avoids forced quadratic complexity when repeatedly parsing a partial |
| 12 | +field, repeating the Huffman decoding of the string on each iteration. |
| 13 | +--- |
| 14 | + vendor/golang.org/x/net/http2/hpack/hpack.go | 79 ++++++++++++-------- |
| 15 | + 1 file changed, 49 insertions(+), 30 deletions(-) |
| 16 | + |
| 17 | +diff --git a/vendor/golang.org/x/net/http2/hpack/hpack.go b/vendor/golang.org/x/net/http2/hpack/hpack.go |
| 18 | +index 85f18a2..02e80e3 100644 |
| 19 | +--- a/vendor/golang.org/x/net/http2/hpack/hpack.go |
| 20 | ++++ b/vendor/golang.org/x/net/http2/hpack/hpack.go |
| 21 | +@@ -359,6 +359,7 @@ func (d *Decoder) parseFieldLiteral(n uint8, it indexType) error { |
| 22 | + |
| 23 | + var hf HeaderField |
| 24 | + wantStr := d.emitEnabled || it.indexed() |
| 25 | ++ var undecodedName undecodedString |
| 26 | + if nameIdx > 0 { |
| 27 | + ihf, ok := d.at(nameIdx) |
| 28 | + if !ok { |
| 29 | +@@ -366,15 +367,27 @@ func (d *Decoder) parseFieldLiteral(n uint8, it indexType) error { |
| 30 | + } |
| 31 | + hf.Name = ihf.Name |
| 32 | + } else { |
| 33 | +- hf.Name, buf, err = d.readString(buf, wantStr) |
| 34 | ++ undecodedName, buf, err = d.readString(buf) |
| 35 | + if err != nil { |
| 36 | + return err |
| 37 | + } |
| 38 | + } |
| 39 | +- hf.Value, buf, err = d.readString(buf, wantStr) |
| 40 | ++ undecodedValue, buf, err := d.readString(buf) |
| 41 | + if err != nil { |
| 42 | + return err |
| 43 | + } |
| 44 | ++ if wantStr { |
| 45 | ++ if nameIdx <= 0 { |
| 46 | ++ hf.Name, err = d.decodeString(undecodedName) |
| 47 | ++ if err != nil { |
| 48 | ++ return err |
| 49 | ++ } |
| 50 | ++ } |
| 51 | ++ hf.Value, err = d.decodeString(undecodedValue) |
| 52 | ++ if err != nil { |
| 53 | ++ return err |
| 54 | ++ } |
| 55 | ++ } |
| 56 | + d.buf = buf |
| 57 | + if it.indexed() { |
| 58 | + d.dynTab.add(hf) |
| 59 | +@@ -459,46 +472,52 @@ func readVarInt(n byte, p []byte) (i uint64, remain []byte, err error) { |
| 60 | + return 0, origP, errNeedMore |
| 61 | + } |
| 62 | + |
| 63 | +-// readString decodes an hpack string from p. |
| 64 | ++// readString reads an hpack string from p. |
| 65 | + // |
| 66 | +-// wantStr is whether s will be used. If false, decompression and |
| 67 | +-// []byte->string garbage are skipped if s will be ignored |
| 68 | +-// anyway. This does mean that huffman decoding errors for non-indexed |
| 69 | +-// strings past the MAX_HEADER_LIST_SIZE are ignored, but the server |
| 70 | +-// is returning an error anyway, and because they're not indexed, the error |
| 71 | +-// won't affect the decoding state. |
| 72 | +-func (d *Decoder) readString(p []byte, wantStr bool) (s string, remain []byte, err error) { |
| 73 | ++// It returns a reference to the encoded string data to permit deferring decode costs |
| 74 | ++// until after the caller verifies all data is present. |
| 75 | ++func (d *Decoder) readString(p []byte) (u undecodedString, remain []byte, err error) { |
| 76 | + if len(p) == 0 { |
| 77 | +- return "", p, errNeedMore |
| 78 | ++ return u, p, errNeedMore |
| 79 | + } |
| 80 | + isHuff := p[0]&128 != 0 |
| 81 | + strLen, p, err := readVarInt(7, p) |
| 82 | + if err != nil { |
| 83 | +- return "", p, err |
| 84 | ++ return u, p, err |
| 85 | + } |
| 86 | + if d.maxStrLen != 0 && strLen > uint64(d.maxStrLen) { |
| 87 | +- return "", nil, ErrStringLength |
| 88 | ++ // Returning an error here means Huffman decoding errors |
| 89 | ++ // for non-indexed strings past the maximum string length |
| 90 | ++ // are ignored, but the server is returning an error anyway |
| 91 | ++ // and because the string is not indexed the error will not |
| 92 | ++ // affect the decoding state. |
| 93 | ++ return u, nil, ErrStringLength |
| 94 | + } |
| 95 | + if uint64(len(p)) < strLen { |
| 96 | +- return "", p, errNeedMore |
| 97 | +- } |
| 98 | +- if !isHuff { |
| 99 | +- if wantStr { |
| 100 | +- s = string(p[:strLen]) |
| 101 | +- } |
| 102 | +- return s, p[strLen:], nil |
| 103 | ++ return u, p, errNeedMore |
| 104 | + } |
| 105 | ++ u.isHuff = isHuff |
| 106 | ++ u.b = p[:strLen] |
| 107 | ++ return u, p[strLen:], nil |
| 108 | ++} |
| 109 | + |
| 110 | +- if wantStr { |
| 111 | +- buf := bufPool.Get().(*bytes.Buffer) |
| 112 | +- buf.Reset() // don't trust others |
| 113 | +- defer bufPool.Put(buf) |
| 114 | +- if err := huffmanDecode(buf, d.maxStrLen, p[:strLen]); err != nil { |
| 115 | +- buf.Reset() |
| 116 | +- return "", nil, err |
| 117 | +- } |
| 118 | ++type undecodedString struct { |
| 119 | ++ isHuff bool |
| 120 | ++ b []byte |
| 121 | ++} |
| 122 | ++ |
| 123 | ++func (d *Decoder) decodeString(u undecodedString) (string, error) { |
| 124 | ++ if !u.isHuff { |
| 125 | ++ return string(u.b), nil |
| 126 | ++ } |
| 127 | ++ buf := bufPool.Get().(*bytes.Buffer) |
| 128 | ++ buf.Reset() // don't trust others |
| 129 | ++ var s string |
| 130 | ++ err := huffmanDecode(buf, d.maxStrLen, u.b) |
| 131 | ++ if err == nil { |
| 132 | + s = buf.String() |
| 133 | +- buf.Reset() // be nice to GC |
| 134 | + } |
| 135 | +- return s, p[strLen:], nil |
| 136 | ++ buf.Reset() // be nice to GC |
| 137 | ++ bufPool.Put(buf) |
| 138 | ++ return s, err |
| 139 | + } |
| 140 | +-- |
| 141 | +2.34.1 |
| 142 | + |
0 commit comments