Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
176 changes: 176 additions & 0 deletions scripts/fuzz/fuzz_command_stream.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
/*
* Fuzzer for rtpproxy control-protocol stream framing (rtpp_command_stream.c).
*
* Threat model: when rtpproxy's control socket is a stream socket (UNIX
* stream, TCP, stdio), client commands arrive as a byte stream framed by
* newlines: rtpp_command_stream_doio() reads bytes into a 8KB connection
* buffer and rtpp_command_stream_get() splits complete lines into commands,
* executes them and delivers replies back over the connection fd. A remote
* attacker controls every byte: framing, partial lines, buffer compaction
* across reads, oversized lines, embedded NULs, and arbitrary command
* payloads. This is the primary attacker-facing interface of rtpproxy.
*
* No existing fuzz target exercises the stream framing layer: production
* coverage (2026-11-26 report) shows rtpp_command_stream.c 86/86 lines dark,
* rtpp_command.c 129 lines best-dark, and the reply-delivery paths dark
* (command_parser feeds pre-split command strings straight into the command
* core). This target drives:
* - src/rtpp_command_stream.c: doio reads, buffer compaction, framing;
* - src/rtpp_command.c / reply delivery: command execution with replies
* written back over a socketpair, drained by the harness;
* - the full command surface through the real control path (sessions
* created/queried/deleted, records started, stats fetched).
*
* Layout: one op byte (bit0 reseed) followed by raw fuzzed bytes that are
* written into the client end of a socketpair. The harness then runs the
* same loop as rtpp_command_async.c's process_commands_stream(): doio,
* then get() + handle_command() until EAGAIN, draining replies. The fds are
* non-blocking so EAGAIN/EINTR handling in doio is exercised too.
*/

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <assert.h>
#include <fcntl.h>
#include <poll.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "fuzz_standalone.h"

#define HAVE_CONFIG_H 1
#include "config_pp.h"

#include "rtpp_types.h"
#include "rtpp_cfg.h"
#include "rtpp_time.h"
#include "rtpp_refcnt.h"
#include "rtpp_codeptr.h"
#include "rtpp_network.h"
#include "rtpp_command.h"
#include "rtpp_command_args.h"
#include "rtpp_command_sub.h"
#include "rtpp_command_private.h"
#include "rtpp_command_reply.h"
#include "rtpp_command_stats.h"
#include "rtpp_command_stream.h"
#include "rtpp_controlfd.h"

#include "rfz_utils.h"

static int sp_in[2];
static int sp_out[2];
static struct rtpp_ctrl_sock fcsock;
static struct rtpp_command_stats cstat;

int
LLVMFuzzerInitialize(int *_argc, char ***_argv)
{
struct sockaddr *sap;

RTPPInitializeParams.ttl = "60";
int r = RTPPInitialize();
if (r != 0)
return (-1);

if (socketpair(AF_UNIX, SOCK_STREAM, 0, sp_in) != 0)
return (-1);
if (socketpair(AF_UNIX, SOCK_STREAM, 0, sp_out) != 0)
return (-1);
assert(fcntl(sp_out[1], F_SETFL, O_NONBLOCK) == 0);

memset(&fcsock, 0, sizeof(fcsock));
fcsock.type = RTPC_FD;
fcsock.controlfd_in = sp_in[0];
fcsock.controlfd_out = sp_out[0];
struct sockaddr_in *sinp;
sap = sstosa(&fcsock.bindaddr);
sap->sa_family = AF_INET;
sinp = (struct sockaddr_in *)sap;
sinp->sin_port = 0;
sinp->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
return (0);
}

int
LLVMFuzzerTestOneInput(const char *data, size_t size)
{
struct {
union {
struct {
uint8_t reseed:1;
};
uint8_t value;
};
} op_flags;
struct rtpp_cmd_connection rcs;
struct rtpp_timestamp dtime;
char drainbuf[4096];
ssize_t n;

if (size < 2)
return (0);

op_flags.value = data[0];
data += 1;
size -= 1;

if (op_flags.reseed) {
SeedRNGs();
}

/* Feed the fuzzed byte stream into the client side of the socket. */
while (size > 0) {
n = write(sp_in[1], data, size);
if (n <= 0)
break;
data += n;
size -= n;
}

memset(&rcs, 0, sizeof(rcs));
rcs.controlfd_in = sp_in[0];
rcs.controlfd_out = sp_out[0];
rcs.csock = &fcsock;

for (int round = 0; round < 32; round++) {
/* Production polls for readability before calling doio();
* rtpp_command_stream_doio() busy-spins on EAGAIN when handed an
* unreadable fd, so mirror the poll-then-read contract with a
* short timeout and stop when no data is left. */
struct pollfd pfd = {.fd = sp_in[0], .events = POLLIN};
int pret = poll(&pfd, 1, 20);
if (pret <= 0)
break;
int rval = rtpp_command_stream_doio(gconf.cfsp, &rcs);
if (rval == 0)
break;
for (;;) {
rtpp_timestamp_get(&dtime);
struct rtpp_command *cmd = rtpp_command_stream_get(gconf.cfsp,
&rcs, &rval, &dtime, &cstat);
if (cmd == NULL) {
if (rval == GET_CMD_EAGAIN)
break;
if (rval == GET_CMD_OK || rval == GET_CMD_INVAL ||
rval == GET_CMD_ENOMEM)
continue;
break;
}
cmd->laddr = sstosa(&fcsock.bindaddr);
handle_command(gconf.cfsp, cmd);
RTPP_OBJ_DECREF(cmd);
do {
n = read(sp_out[1], drainbuf, sizeof(drainbuf));
} while (n > 0);
}
}
do {
n = read(sp_out[1], drainbuf, sizeof(drainbuf));
} while (n > 0);
return (0);
}
24 changes: 24 additions & 0 deletions scripts/fuzz/fuzz_command_stream.dict
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"\x0a"
"Vf"
"U "
"Uz20 "
"L "
"Lz40 "
"R "
"S "
"X"
"I "
"G"
"Q"
"callid_1"
"from_tag_1"
"to_tag_1"
"1.2.3.4"
"5.6.7.8"
"1234"
"5678"
"udp"
"unix"
"tcp"
"cunix:"
"\x0d"
5 changes: 5 additions & 0 deletions scripts/fuzz/fuzz_command_stream.options
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[libfuzzer]
detect_leaks=0
max_len=4096
print_final_stats=1
use_value_profile=1
52 changes: 52 additions & 0 deletions scripts/fuzz/gen_command_stream_seeds.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/env python3
"""
Seed corpus generator for fuzz_command_stream.

Each input is [op byte][raw stream bytes]. The stream bytes are the exact
bytes a control-protocol client would send over a stream (TCP/UNIX) socket:
newline-framed command lines. Seeds provide well-formed commands, partial
framing, oversized lines, and CRLF/NUl variants; libFuzzer mutates the
command payloads from there.
"""
import os
import sys


def seed(name, body, outdir, op=0x01):
with open(os.path.join(outdir, name), "wb") as f:
f.write(bytes([op]) + body.encode("latin-1"))


def main():
outdir = sys.argv[1] if len(sys.argv) > 1 else "command_stream_seed_corpus"
os.makedirs(outdir, exist_ok=True)

seed("ver", "Vf\n", outdir)
seed("ul_flow",
"U callid_1 1.2.3.4 1234 from_tag_1\n"
"L callid_1 5.6.7.8 5678 from_tag_1 to_tag_1\n"
"I callid_1 from_tag_1\n"
"X\n", outdir)
seed("resizer_flow",
"Uz20 callid_1 1.2.3.4 1234 from_tag_1\n"
"Lz40 callid_1 5.6.7.8 5678 from_tag_1 to_tag_1\n"
"X\n", outdir)
seed("record_flow",
"U callid_1 1.2.3.4 1234 from_tag_1\n"
"R callid_1 from_tag_1\n"
"S callid_1 from_tag_1\n"
"X\n", outdir)
seed("query_flow", "G\nGv\nI\nQ\n", outdir)
seed("partial_line", "U callid_1 1.2.3.4", outdir)
seed("split_two_reads",
"U cal\nlid_1 1.2.3.4 1234 from_tag_1\nX\n", outdir)
seed("empty_lines", "\n\n\nVf\n\n", outdir)
seed("crlf", "Vf\r\nU callid_1 1.2.3.4 1234 from_tag_1\r\n", outdir)
seed("long_line", "U " + "c" * 2000 + " 1.2.3.4 1234 t1\n", outdir)
seed("no_newline_tail", "U callid_1 1.2.3.4 1234 from_tag_1\nU callid_2", outdir)

print(f"wrote seeds to {outdir}")


if __name__ == "__main__":
main()
13 changes: 12 additions & 1 deletion scripts/fuzz/oss-fuzz-build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ do
OBJS="${OBJS} ${obj}"
done

ALL="command_parser rtp_parser rtcp_parser rtp_session config_file"
ALL="command_parser rtp_parser rtcp_parser rtp_session config_file command_stream"
OBJS0="${OBJS}"
for fz in ${ALL}
do
Expand Down Expand Up @@ -168,6 +168,17 @@ then
rm -rf "${SEEDTMP}"
fi

# fuzz_command_stream support: pack a seed corpus of newline-framed control
# protocol streams (well-formed commands, partial framing, split lines,
# CRLF, oversized lines) exercising the stream layer the other targets skip.
if [ -e scripts/fuzz/gen_command_stream_seeds.py ]
then
SEEDTMP="`mktemp -d ${OUT}/cmdstream_seed_corpus.XXXXXXXX`"
python3 scripts/fuzz/gen_command_stream_seeds.py "${SEEDTMP}"
(cd "${SEEDTMP}" && zip -q -r "${OUT}/fuzz_command_stream_seed_corpus.zip" .)
rm -rf "${SEEDTMP}"
fi

if [ "${OUT}" != "." ]
then
cp -Rp ${OUT} build-out
Expand Down
Loading