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
239 changes: 239 additions & 0 deletions scripts/fuzz/fuzz_rtp_flow.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,239 @@
/*
* Fuzzer for rtpproxy RTP relay flow machinery with the resizer engaged.
*
* Threat model: rtpproxy relays attacker-supplied RTP between endpoints; each
* relayed packet's header (sequence, timestamp, SSRC, payload type, padding,
* CSRC) and UDP source address are fully attacker-controlled. When a session
* is created with the "z" command modifier (repacketization ptime), relayed
* packets additionally flow through the resizer (rtp_resizer.c), a heap-based
* jitter buffer that re-times packets -- a state machine driven entirely by
* the attacker's sequence/timestamp/payload-size choices.
*
* No existing fuzz target ever enables the resizer (the z modifier is absent
* from all harness setup scripts), so rtp_resizer.c and the surrounding
* stream/proc paths are dark in production coverage (2026-11-26 report:
* rtp_resizer.c 238/268 lines dark, rtp.c 99/184, rtp_analyze.c 89/384,
* rtpp_stream.c 493/863 best-dark, rtpp_proc.c 83/103, rtpp_sessinfo.c 92
* best-dark). This target creates sessions with Uz/Lz modifiers (ptime 10,
* 20 and 40ms, including asymmetric 10 -> 40) plus one plain session, then
* injects attacker-controlled RTP packets with attacker-controlled source
* addresses into both streams of every session, exercising:
* - src/rtp_resizer.c: enqueue, repacketization and drain state machine;
* - src/rtpp_stream.c: relay path, resizer hooks, per-stream stats;
* - src/rtp.c / src/rtp_analyze.c: packet parse validation, sequence and
* timestamp tracking (dups, loss, reorder, probation);
* - src/rtpp_proc.c and src/rtpp_sessinfo.c: server-side per-tick
* processing and session statistics;
* - src/rtpp_hash_table.c: session hash table walk under relay load.
*
* Input format is identical to fuzz_rtp_session: a leading op byte (bit0:
* reseed RNGs) followed by rfz_get_chunk()-delimited chunks, each holding a
* 16-byte sockaddr_in source address followed by the raw RTP packet bytes.
* The seed corpus generator (gen_rtp_flow_seeds.py) constructs valid RTP
* streams with real-world header variants; libFuzzer mutates from there.
*/

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <assert.h>
#include <semaphore.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <errno.h>

#include "fuzz_standalone.h"

#define HAVE_CONFIG_H 1
#include "config_pp.h"

#include "rtpp_types.h"
#include "rtpp_cfg.h"
#include "rtp.h"
#include "rtpp_time.h"
#include "rtpp_codeptr.h"
#include "rtp_packet.h"
#include "rtpp_session.h"
#include "rtpp_pipe.h"
#include "rtpp_proc.h"
#include "rtpp_network.h"
#include "rtpp_stream.h"
#include "rtpp_ttl.h"
#include "rtpp_codeptr.h"
#include "rtpp_refcnt.h"
#include "rtpp_command.h"
#include "rtpp_weakref.h"
#include "rtpp_hash_table.h"
#include "advanced/packet_processor.h"
#include "advanced/pproc_manager.h"

#include "rfz_chunk.h"
#include "rfz_utils.h"
#include "rfz_command.h"

static struct {
sem_t wi_proc_done;
} fuzz_ctx;

static void
fuzz_ctx_dtor(void)
{
ExecuteRTPPCommand(&gconf, "X", 1, 0);
sem_destroy(&fuzz_ctx.wi_proc_done);
}

static const char * const setup_script[] = {
#include "fuzz_rtp_flow.setup"
NULL
};

static int
ExecuteScript(void)
{
char line[RTPP_CMD_BUFLEN];

for (int i = 0; setup_script[i] != NULL; i++) {
const char *cp = setup_script[i];
int size = strlen(cp);
memcpy(line, cp, size + 1);
int r = ExecuteRTPPCommand(&gconf, line, size, 0);
if (r != 0)
return (-1);
}
return (0);
}

int
LLVMFuzzerInitialize(int *_argc, char ***_argv)
{
RTPPInitializeParams.ttl = "60";
int r = RTPPInitialize();

if (r != 0)
goto e0;
if (sem_init(&fuzz_ctx.wi_proc_done, 0, 0) != 0)
goto e0;
if (ExecuteScript() != 0)
goto e1;
if (ExecuteRTPPCommand(&gconf, "X", 1, 0) != 0)
goto e1;
atexit(fuzz_ctx_dtor);
return (0);
e1:
sem_destroy(&fuzz_ctx.wi_proc_done);
e0:
return (-1);
}

struct foreach_args {
const char *data;
size_t size;
struct rtpp_proc_rstats *rsp;
int nwait;
};

static void
wi_proc_complete(void *arg)
{
sem_post(&fuzz_ctx.wi_proc_done);
}

static int
proc_foreach(void *dp, void *ap)
{
struct foreach_args *fap;
const struct rtpp_session *sp;
rtpp_refcnt_dtor_t wpd_f = (rtpp_refcnt_dtor_t)&wi_proc_complete;

fap = (struct foreach_args *)ap;
/*
* This method does not need us to bump ref, since we are in the
* locked context of the rtpp_hash_table, which holds its own ref.
*/
sp = (const struct rtpp_session *)dp;

for (int i=0; i < 2; i++) {
struct sockaddr *rap;
struct rtp_packet *pktp = rtp_packet_alloc();
assert (pktp != NULL);
void *olddata = CALL_SMETHOD(pktp->rcnt, getdata);
RTPP_OBJ_DTOR_ATTACH_s(pktp, wpd_f, olddata);
rap = sstosa(&pktp->raddr);
memcpy(rap, fap->data, sizeof(struct sockaddr_in));
rap->sa_family = AF_INET;
pktp->size = fap->size - sizeof(struct sockaddr_in);
memcpy(pktp->data.buf, fap->data + sizeof(struct sockaddr_in), pktp->size);
struct rtpp_stream *istp = sp->rtp->stream[i],
*ostp = sp->rtp->stream[i ^ 1];
struct pkt_proc_ctx pktx = {.strmp_in = istp,
.strmp_out = ostp,
.rsp = fap->rsp,
.pktp = pktp};
CALL_SMETHOD(istp->pproc_manager, handleat, &pktx, _PPROC_ORD_EMPTY);
fap->nwait += 1;
}
return (RTPP_HT_MATCH_CONT);
}

int
LLVMFuzzerTestOneInput(const char *data, size_t size)
{
static struct rtpp_proc_rstats rs = {0};
sem_t *wpdp = &fuzz_ctx.wi_proc_done;
struct {
union {
struct {
uint8_t reseed:1;
};
uint8_t value;
};
} op_flags;

if (size < 2)
return (0);

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

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

assert(ExecuteScript() == 0);

struct rfz_chunk chunk = {.rem_size = size, .rem_data = data};
struct foreach_args fa = {.rsp = &rs};

do {
chunk = rfz_get_chunk(chunk.rem_data, chunk.rem_size);
if (chunk.size < sizeof(struct sockaddr_in))
break;
if (chunk.size > sizeof(struct sockaddr_in) + MAX_RPKT_LEN)
break;
fa.data = chunk.data;
fa.size = chunk.size;
CALL_SMETHOD(gconf.cfsp->sessions_ht, foreach, proc_foreach, (void *)&fa, NULL);
} while (chunk.rem_size > 1);

/* Resizer-held packets only return to the free queue at session
* teardown, so tear the sessions down BEFORE waiting and bound each
* wait: unbounded sem_wait() would hang the target on packets the
* resizer keeps queued past the 25s libFuzzer timeout. */
assert(ExecuteRTPPCommand(&gconf, "X", 1, 0) == 0);
for (int i = 0; i < fa.nwait; i++) {
struct timespec dl;
clock_gettime(CLOCK_REALTIME, &dl);
dl.tv_nsec += 100 * 1000 * 1000;
if (dl.tv_nsec >= 1000 * 1000 * 1000) {
dl.tv_sec += 1;
dl.tv_nsec -= 1000 * 1000 * 1000;
}
if (sem_timedwait(wpdp, &dl) == -1 && errno == ETIMEDOUT)
break;
}
return (0);
}
117 changes: 117 additions & 0 deletions scripts/fuzz/fuzz_rtp_flow.dict
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
addr1="\x01\x02\x03\x04"
# port1=1234
port1="\x04\xd2"

addr2="\x05\x06\x07\x08"
# port2=5678
port2="\x2e\x16"

# AFL dictionary for RTP/RTCP packets
# -----------------------------------
#
# Basic sections derived from standard RTP/RTCP headers and payloads.
# Does not include vendor-specific extensions.
#
# Created by [Your Name]
#

# RTP header fields
header_vpxt="10000000"
header_vpxc="10010000"
header_vpxt_mark="10000001"
header_vpxc_mark="10010001"

# RTP payload types
payload_pcmu="\x00"
payload_pcma="\x08"
payload_gsm="\x03"
payload_g723="\x04"
payload_dvi4_8k="\x05"
payload_dvi4_16k="\x06"
payload_lpc="\x07"
payload_g722="\x09"
payload_l16_stereo="\x0a"
payload_l16_mono="\x0b"
payload_qcelp="\x0c"
payload_cn="\x13"
payload_mpeg="\x0e"
payload_g729="\x12"

# RTCP packet types
# Sender Report
rtcp_sr="\xc8"
# Receiver Report
rtcp_rr="\xc9"
# Source Description
rtcp_sdes="\xca"
# Goodbye
rtcp_bye="\xcb"
# Application-defined
rtcp_app="\xcc"

# RTCP SDES item types
sdes_cname="\x01"
sdes_name="\x02"
sdes_email="\x03"
sdes_phone="\x04"
sdes_loc="\x05"
sdes_tool="\x06"
sdes_note="\x07"
sdes_priv="\x08"

# Common RTP/RTCP extensions
# Extended Sequence Number
ext_trans_seq="\xbe\xde"
# Media Identifier
ext_mid="\xbe\xde\x02"
# RTP Stream Identifier
ext_rtp_stream_id="\xbe\xde\x0d"

# AFL dictionary for STUN/ICE packets
# -----------------------------------
#
# Basic sections derived from standard STUN/ICE headers and attributes.
# Does not include vendor-specific extensions.
#
# Created by [Your Name]
#

# STUN message types
stun_binding_request="\x00\x01"
stun_binding_response="\x01\x01"
stun_binding_error_response="\x01\x11"
stun_binding_indication="\x00\x11"

# STUN message headers
stun_magic_cookie="\x21\x12\xa4\x42"

# STUN attributes
attr_mapped_address="\x00\x01"
attr_response_address="\x00\x02"
attr_change_request="\x00\x03"
attr_source_address="\x00\x04"
attr_changed_address="\x00\x05"
attr_username="\x00\x06"
attr_password="\x00\x07"
attr_message_integrity="\x00\x08"
attr_error_code="\x00\x09"
attr_unknown_attributes="\x00\x0a"
attr_reflected_from="\x00\x0b"
attr_realm="\x00\x14"
attr_nonce="\x00\x15"
attr_xor_mapped_address="\x00\x20"
attr_software="\x80\x22"
attr_alternate_server="\x80\x23"
attr_fingerprint="\x80\x28"

# ICE-specific attributes
attr_priority="\x00\x24"
attr_use_candidate="\x00\x25"
attr_ice_controlled="\x80\x29"
attr_ice_controlling="\x80\x2a"

# Common STUN message header extensions
# Example length field (32 bytes)
header_length="\x00\x20"
# Example transaction ID
header_transaction_id="\x63\x1a\x1f\x80\x39\x4d\x41\x50\x4c\x65\x75"
5 changes: 5 additions & 0 deletions scripts/fuzz/fuzz_rtp_flow.options
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[libfuzzer]
detect_leaks=0
max_len=8192
print_final_stats=1
use_value_profile=1
6 changes: 6 additions & 0 deletions scripts/fuzz/fuzz_rtp_flow.setup
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"Uz20 callid_1 1.2.3.4 1234 from_tag_1",
"Lz20 callid_1 5.6.7.8 5678 from_tag_1 to_tag_1",
"Uz10 callid_2 1.2.3.4 1234 from_tag_2 && " \
"Lz40 callid_2 5.6.7.8 5678 from_tag_2 to_tag_2",
"U callid_3 1.2.3.4 1234 from_tag_3",
"L callid_3 5.6.7.8 5678 from_tag_3 to_tag_3",
Loading
Loading