Skip to content

Commit bd2ee62

Browse files
authored
libmicrohttpd2: Add new targets (#13851)
This PR adds new fuzzing targets from mhd_str and mhd_libinfo from libmicrohttpd2 project. Signed-off-by: Arthur Chan <[email protected]>
1 parent 8a2481a commit bd2ee62

File tree

3 files changed

+65
-2
lines changed

3 files changed

+65
-2
lines changed

projects/libmicrohttpd2/build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ make -j$(nproc)
3030
make install
3131

3232
# Compile fuzzer
33-
FUZZERS="fuzz_response fuzz_daemon fuzz_mhd2 fuzz_str fuzz_crypto_int"
33+
FUZZERS="fuzz_response fuzz_daemon fuzz_mhd2 fuzz_str fuzz_crypto_int fuzz_libinfo"
3434

3535
for fuzzer in $FUZZERS; do
3636
extra_src=""
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright 2025 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+
#include <stdint.h>
17+
#include <stddef.h>
18+
#include <vector>
19+
#include <cstdlib>
20+
#include <algorithm>
21+
22+
#include "microhttpd2.h"
23+
#include "fuzzer/FuzzedDataProvider.h"
24+
25+
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
26+
FuzzedDataProvider fdp(data, size);
27+
28+
// Generate random ids
29+
int fixed_id = fdp.ConsumeIntegral<int>();
30+
int dynamic_id = fdp.ConsumeIntegral<int>();
31+
32+
// Generate random raw data
33+
std::vector<uint8_t> raw_data = fdp.ConsumeRemainingBytes<uint8_t>();
34+
35+
// Fuzz MHD_lib_get_info_fixed_sz
36+
MHD_lib_get_info_fixed_sz(
37+
static_cast<MHD_LibInfoFixed>(fixed_id),
38+
raw_data.size() > 0 ? reinterpret_cast<MHD_LibInfoFixedData*>(raw_data.data()) : nullptr,
39+
raw_data.size());
40+
41+
// Fuzz MHD_lib_get_info_dynamic_sz
42+
MHD_lib_get_info_dynamic_sz(
43+
static_cast<MHD_LibInfoDynamic>(dynamic_id),
44+
raw_data.size() > 0 ? reinterpret_cast<MHD_LibInfoDynamicData*>(raw_data.data()) : nullptr,
45+
raw_data.size());
46+
47+
return 0;
48+
}

projects/libmicrohttpd2/fuzz_str.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <fuzzer/FuzzedDataProvider.h>
2525
extern "C" {
2626
#include "mhd_str.h"
27+
#include "microhttpd2.h"
2728
}
2829

2930
static void fuzz_tokens(FuzzedDataProvider& fdp) {
@@ -180,16 +181,30 @@ static void fuzz_base64(FuzzedDataProvider& fdp) {
180181
}
181182
}
182183

184+
static void fuzz_transformation(FuzzedDataProvider& fdp) {
185+
// Fuzz targets in multiple rounds
186+
for (int i = 0; i < fdp.ConsumeIntegralInRange<unsigned>(1, 8); i++) {
187+
// Generate random integer
188+
int value = fdp.ConsumeIntegral<int>();
189+
190+
// Fuzz conversion functions
191+
MHD_http_method_to_string(static_cast<MHD_HTTP_Method>(value));
192+
MHD_predef_header_to_string(static_cast<MHD_PredefinedHeader>(value));
193+
MHD_protocol_version_to_string(static_cast<MHD_HTTP_ProtocolVersion>(value));
194+
}
195+
}
196+
183197
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
184198
FuzzedDataProvider fdp(data, size);
185199

186200
for (int i = 0; i < fdp.ConsumeIntegralInRange<unsigned>(1, 6); i++) {
187-
switch (fdp.ConsumeIntegralInRange<int>(0, 5)) {
201+
switch (fdp.ConsumeIntegralInRange<int>(0, 6)) {
188202
case 0: fuzz_tokens(fdp); break;
189203
case 1: fuzz_conversion(fdp); break;
190204
case 2: fuzz_decode(fdp); break;
191205
case 3: fuzz_quoted(fdp); break;
192206
case 4: fuzz_base64(fdp); break;
207+
case 5: fuzz_transformation(fdp); break;
193208
}
194209
}
195210
return 0;

0 commit comments

Comments
 (0)