Line | Count | Source |
1 | | // Copyright 2026 Google LLC |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | // |
15 | | //////////////////////////////////////////////////////////////////////////////// |
16 | | // |
17 | | // Fuzzer for the HTTP/2 HPACK header-compression codec |
18 | | // (src/mhd2/h2/hpack/mhd_hpack_codec.c and h2_huffman_codec.c). |
19 | | // |
20 | | // Threat model: in HTTP/2 the HEADERS / CONTINUATION frame payload (the |
21 | | // "header block fragment") is attacker controlled and is passed to |
22 | | // mhd_hpack_dec_data() representation by representation, exactly as done in |
23 | | // h2_req_fields.c. The decoder maintains a dynamic table that is mutated by |
24 | | // the attacker (literal-with-indexing, dynamic table size updates), so the |
25 | | // whole byte stream of a header block is fed through ONE decoder context. |
26 | | // |
27 | | // Input layout (FuzzedDataProvider): |
28 | | // 1. output buffer size for decoded fields (server-side buffer, 2..8192) |
29 | | // 2. maximum allowed decoder dynamic table size (server SETTINGS value) |
30 | | // 3. encoder dynamic table size + encoding policy seed (round-trip only) |
31 | | // 4. remaining bytes: the HPACK header block (attacker data) |
32 | | // |
33 | | // Every successfully decoded regular field is also re-encoded with the MHD |
34 | | // HPACK encoder and decoded again by an independent decoder, which exercises |
35 | | // the encoder, Huffman encoding and the dynamic table of both sides. On any |
36 | | // unexpected result the harness returns so that fuzzing continues. |
37 | | |
38 | | #include <stdint.h> |
39 | | #include <stddef.h> |
40 | | #include <stdlib.h> |
41 | | #include <string.h> |
42 | | #include <vector> |
43 | | |
44 | | #include "fuzzer/FuzzedDataProvider.h" |
45 | | |
46 | | #include "mhd_sys_options.h" |
47 | | #include "mhd_buffer.h" |
48 | | #include "mhd_lib_init.h" |
49 | | #include "h2/hpack/mhd_hpack_codec.h" |
50 | | #include "h2/hpack/mhd_hpack_dec_types.h" |
51 | | #include "h2/hpack/mhd_hpack_enc_types.h" |
52 | | |
53 | | namespace { |
54 | | |
55 | | constexpr size_t kMaxOutBuff = 8192; |
56 | | |
57 | | // Re-encode a decoded field and decode it with a peer decoder. Returns early |
58 | | // on any unexpected result. |
59 | | void RoundTrip(struct mhd_HpackEncContext *enc, |
60 | | struct mhd_HpackDecContext *peer, |
61 | | enum mhd_HpackEncPolicy pol, |
62 | | const char *name, size_t name_len, |
63 | 997k | const char *val, size_t val_len) { |
64 | 997k | struct mhd_BufferConst n; |
65 | 997k | struct mhd_BufferConst v; |
66 | 997k | n.size = name_len; |
67 | 997k | n.data = name; |
68 | 997k | v.size = val_len; |
69 | 997k | v.data = val; |
70 | | |
71 | | // Generous output buffer: the encoder may emit a dynamic table size update |
72 | | // plus a literal. Huffman never expands by more than 30/8 per byte. |
73 | 997k | const size_t enc_cap = 64 + (name_len + val_len) * 4; |
74 | 997k | std::vector<uint8_t> enc_buf(enc_cap); |
75 | 997k | size_t enc_len = 0; |
76 | 997k | if (mhd_HPACK_ENC_RES_OK != |
77 | 997k | mhd_hpack_enc_field(enc, &n, &v, pol, enc_buf.size(), enc_buf.data(), |
78 | 997k | &enc_len)) |
79 | 0 | return; |
80 | 997k | if (enc_len > enc_buf.size()) |
81 | 0 | return; |
82 | | |
83 | 997k | std::vector<char> out(name_len + val_len + 2 + 16); |
84 | 997k | size_t pos = 0; |
85 | 997k | bool got_field = false; |
86 | 1.99M | while (pos < enc_len) { |
87 | 999k | size_t nl = 0, vl = 0, used = 0; |
88 | 999k | enum mhd_HpackDecResult r = |
89 | 999k | mhd_hpack_dec_data(peer, enc_len - pos, enc_buf.data() + pos, |
90 | 999k | out.size(), out.data(), &nl, &vl, &used); |
91 | 999k | if (mhd_HPACK_DEC_RES_IS_ERR(r) || 0 == used) |
92 | 0 | return; |
93 | 999k | pos += used; |
94 | 999k | if (mhd_HPACK_DEC_RES_NEW_FIELD == r) { |
95 | 997k | if (got_field || pos != enc_len) |
96 | 0 | return; |
97 | 997k | if (nl != name_len || vl != val_len || |
98 | 997k | 0 != memcmp(out.data(), name, name_len) || |
99 | 997k | 0 != memcmp(out.data() + nl + 1, val, val_len)) |
100 | 0 | return; |
101 | 997k | got_field = true; |
102 | 997k | } |
103 | 999k | } |
104 | 997k | } |
105 | | |
106 | | // False if global library initialisation failed; every input is then skipped. |
107 | | bool g_lib_init_ok = false; |
108 | | |
109 | | } // namespace |
110 | | |
111 | 6 | extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv) { |
112 | | // Global library initialisation, as done by MHD on daemon start. Among |
113 | | // other things this builds the Huffman decoding tables |
114 | | // (mhd_h2_huffman_init()), without which every Huffman string is rejected. |
115 | 6 | g_lib_init_ok = mhd_lib_init_global_if_needed(); |
116 | 6 | return 0; |
117 | 6 | } |
118 | | |
119 | 4.51k | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
120 | 4.51k | if (!g_lib_init_ok) |
121 | 0 | return 0; |
122 | | |
123 | 4.51k | FuzzedDataProvider fdp(data, size); |
124 | | |
125 | 4.51k | const size_t out_size = fdp.ConsumeIntegralInRange<size_t>(2, kMaxOutBuff); |
126 | 4.51k | static const size_t kAllowedDyn[] = {4096, 0, 64, 256, 1024, 16384, 65536}; |
127 | 4.51k | const size_t allowed_dyn = fdp.PickValueInArray(kAllowedDyn); |
128 | 4.51k | static const size_t kEncDyn[] = {4096, 0, 128, 1024}; |
129 | 4.51k | const size_t enc_dyn = fdp.PickValueInArray(kEncDyn); |
130 | 4.51k | const uint8_t pol_seed = fdp.ConsumeIntegral<uint8_t>(); |
131 | | |
132 | 4.51k | std::vector<uint8_t> block = fdp.ConsumeRemainingBytes<uint8_t>(); |
133 | 4.51k | if (block.empty()) |
134 | 5 | return 0; |
135 | | |
136 | 4.51k | struct mhd_HpackDecContext dec; |
137 | 4.51k | struct mhd_HpackEncContext enc; |
138 | 4.51k | struct mhd_HpackDecContext peer; |
139 | 4.51k | if (!mhd_hpack_dec_init(&dec)) |
140 | 0 | return 0; |
141 | 4.51k | if (!mhd_hpack_enc_init(&enc)) { |
142 | 0 | mhd_hpack_dec_deinit(&dec); |
143 | 0 | return 0; |
144 | 0 | } |
145 | 4.51k | if (!mhd_hpack_dec_init(&peer)) { |
146 | 0 | mhd_hpack_enc_deinit(&enc); |
147 | 0 | mhd_hpack_dec_deinit(&dec); |
148 | 0 | return 0; |
149 | 0 | } |
150 | 4.51k | if (4096 != allowed_dyn) |
151 | 2.31k | mhd_hpack_dec_set_allowed_dyn_size(&dec, allowed_dyn); |
152 | 4.51k | if (4096 != enc_dyn) { |
153 | | // The peer decoder keeps the default (4096) limit, so any smaller encoder |
154 | | // table is always acceptable to it. |
155 | 2.90k | mhd_hpack_enc_set_dyn_size(&enc, enc_dyn); |
156 | 2.90k | (void)mhd_hpack_enc_dyn_resize(&enc); |
157 | 2.90k | } |
158 | | |
159 | | // Exactly-sized heap buffer so that ASan catches any out-of-bounds write. |
160 | 4.51k | char *out = static_cast<char *>(malloc(out_size)); |
161 | 4.51k | if (out == nullptr) { |
162 | 0 | mhd_hpack_dec_deinit(&peer); |
163 | 0 | mhd_hpack_enc_deinit(&enc); |
164 | 0 | mhd_hpack_dec_deinit(&dec); |
165 | 0 | return 0; |
166 | 0 | } |
167 | | |
168 | 4.51k | size_t pos = 0; |
169 | 4.51k | unsigned int field_no = 0; |
170 | 1.48M | while (pos < block.size()) { |
171 | 1.48M | size_t name_len = 0, val_len = 0, used = 0; |
172 | 1.48M | enum mhd_HpackDecResult r = |
173 | 1.48M | mhd_hpack_dec_data(&dec, block.size() - pos, block.data() + pos, |
174 | 1.48M | out_size, out, &name_len, &val_len, &used); |
175 | 1.48M | if (mhd_HPACK_DEC_RES_IS_ERR(r)) |
176 | 1.76k | break; // Connection error in a real server (COMPRESSION_ERROR). |
177 | 1.48M | if (used == 0 || used > block.size() - pos) |
178 | 0 | break; // No progress within the input: stop, avoiding an endless loop. |
179 | 1.48M | pos += used; |
180 | 1.48M | if (mhd_HPACK_DEC_RES_NEW_FIELD != r) |
181 | 39.7k | continue; |
182 | | |
183 | | // API contract: "name\0value\0" written into out_buff. |
184 | 1.44M | if (name_len + val_len + 2 > out_size) |
185 | 0 | break; |
186 | 1.44M | if (out[name_len] != '\0' || out[name_len + 1 + val_len] != '\0') |
187 | 0 | break; |
188 | | |
189 | 1.44M | const char *name = out; |
190 | 1.44M | const char *val = out + name_len + 1; |
191 | 1.44M | if (name_len != 0 && name[0] != ':') { |
192 | 997k | const enum mhd_HpackEncPolicy pol = static_cast<enum mhd_HpackEncPolicy>( |
193 | 997k | (pol_seed + field_no) % |
194 | 997k | (mhd_HPACK_ENC_POL_NEVER_W_NAME_LIT_NO_HUFFMAN + 1)); |
195 | 997k | RoundTrip(&enc, &peer, pol, name, name_len, val, val_len); |
196 | 997k | } |
197 | 1.44M | ++field_no; |
198 | 1.44M | } |
199 | | |
200 | 4.51k | free(out); |
201 | 4.51k | mhd_hpack_dec_deinit(&peer); |
202 | 4.51k | mhd_hpack_enc_deinit(&enc); |
203 | 4.51k | mhd_hpack_dec_deinit(&dec); |
204 | 4.51k | return 0; |
205 | 4.51k | } |