Skip to content

Commit 88ef5e1

Browse files
committed
Add fuzzer for proxy v1
1 parent 23c918e commit 88ef5e1

4 files changed

Lines changed: 95 additions & 0 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ fuzzing/broker/broker_fuzz_handle_unsubscribe
6161
fuzzing/broker/broker_fuzz_initial_packet
6262
fuzzing/broker/broker_fuzz_initial_packet_with_init
6363
fuzzing/broker/broker_fuzz_password_file
64+
fuzzing/broker/broker_fuzz_proxy_v1
6465
fuzzing/broker/broker_fuzz_proxy_v2
6566
fuzzing/broker/broker_fuzz_psk_file
6667
fuzzing/broker/broker_fuzz_queue_msg

fuzzing/broker/Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ include ${R}/fuzzing/config.mk
66
FUZZERS:= \
77
broker_fuzz_acl_file \
88
broker_fuzz_password_file \
9+
broker_fuzz_proxy_v1 \
910
broker_fuzz_proxy_v2 \
1011
broker_fuzz_psk_file \
1112
broker_fuzz_queue_msg \
@@ -49,6 +50,11 @@ broker_fuzz_password_file : broker_fuzz_password_file.cpp ${R}/src/mosquitto_bro
4950
install $@ ${OUT}/$@
5051
cp ${R}/fuzzing/corpora/broker_password_file_seed_corpus.zip ${OUT}/$@_seed_corpus.zip
5152

53+
broker_fuzz_proxy_v1 : broker_fuzz_proxy_v1.cpp ${R}/src/proxy_v1.o
54+
$(CXX) $(LOCAL_CXXFLAGS) $(LOCAL_CPPFLAGS) $(LOCAL_LDFLAGS) -o $@ $< ${R}/src/proxy_v1.o $(LOCAL_LIBADD)
55+
cp ${R}/fuzzing/corpora/broker_fuzz_proxy_v1_seed_corpus.zip ${OUT}/$@_seed_corpus.zip
56+
install $@ ${OUT}/$@
57+
5258
broker_fuzz_proxy_v2 : broker_fuzz_proxy_v2.cpp ${R}/src/proxy_v2.o
5359
$(CXX) $(LOCAL_CXXFLAGS) $(LOCAL_CPPFLAGS) $(LOCAL_LDFLAGS) -o $@ $< ${R}/src/proxy_v2.o $(LOCAL_LIBADD)
5460
cp ${R}/fuzzing/corpora/broker_fuzz_proxy_v2_seed_corpus.zip ${OUT}/$@_seed_corpus.zip
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
Copyright (c) 2025 Roger Light <roger@atchoo.org>
3+
4+
All rights reserved. This program and the accompanying materials
5+
are made available under the terms of the Eclipse Public License 2.0
6+
and Eclipse Distribution License v1.0 which accompany this distribution.
7+
8+
The Eclipse Public License is available at
9+
https://www.eclipse.org/legal/epl-2.0/
10+
and the Eclipse Distribution License is available at
11+
http://www.eclipse.org/org/documents/edl-v10.php.
12+
13+
SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
14+
15+
Contributors:
16+
Roger Light - initial implementation and documentation.
17+
*/
18+
19+
#include <cstdio>
20+
#include <cstdint>
21+
#include <cstdlib>
22+
#include <cstring>
23+
#include <sys/stat.h>
24+
#include <unistd.h>
25+
26+
static const uint8_t *packet_data = NULL;
27+
static int packet_data_pos = 0;
28+
static int packet_data_remaining = 0;
29+
30+
extern "C" {
31+
#include "mosquitto_broker_internal.h"
32+
33+
ssize_t net__read(struct mosquitto *mosq, void *buf, size_t count)
34+
{
35+
int res = count < packet_data_remaining?count:packet_data_remaining;
36+
memcpy(buf, &packet_data[packet_data_pos], res);
37+
packet_data_remaining -= res;
38+
return res;
39+
}
40+
41+
int net__socket_get_address(mosq_sock_t sock, char *buf, size_t len, uint16_t *remote_port)
42+
{
43+
snprintf(buf, len, "localhost");
44+
*remote_port = 1883;
45+
return MOSQ_ERR_SUCCESS;
46+
}
47+
48+
int http__context_init(struct mosquitto *context)
49+
{
50+
context->transport = mosq_t_http;
51+
52+
return MOSQ_ERR_SUCCESS;
53+
}
54+
55+
int log__printf(struct mosquitto *mosq, unsigned int priority, const char *fmt, ...)
56+
{
57+
return MOSQ_ERR_SUCCESS;
58+
}
59+
60+
}
61+
62+
63+
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
64+
{
65+
struct mosquitto context{};
66+
struct mosquitto__listener listener{};
67+
68+
packet_data = data;
69+
packet_data_pos = 0;
70+
packet_data_remaining = size;
71+
72+
context.listener = &listener;
73+
context.proxy.cmd = -1;
74+
context.transport = mosq_t_proxy_v1;
75+
76+
while(packet_data_remaining > 0 && context.transport != mosq_t_tcp){
77+
int rc = proxy_v1__read(&context);
78+
if(rc){
79+
break;
80+
}
81+
}
82+
free(context.address);
83+
free(context.proxy.buf);
84+
free(context.proxy.tls_version);
85+
free(context.proxy.cipher);
86+
87+
return 0;
88+
}
9.13 KB
Binary file not shown.

0 commit comments

Comments
 (0)