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
218 changes: 218 additions & 0 deletions scripts/fuzz/fuzz_rtp_record.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
/*
* Fuzzer for rtpproxy session recording machinery (rtpp_record.c).
*
* Threat model: rtpproxy's control protocol lets a client start recording of
* any relayed session ("R"/COPY commands). The recording channel derives
* file names from attacker-influenced strings (call-id, from-tag, and for
* COPY an explicit recording name supplied over the control socket), writes
* attacker-controlled RTP/RTCP packet bytes into those files, and moves them
* between the spool and permanent directories on close. All of that is
* untrusted input: path construction, buffer batching (4KB rbuf with
* flush/synchronous-writev thresholds), pcap vs ad-hoc header preparation,
* and the close/rename/unlink lifecycle.
*
* No existing fuzz target ever creates a recording, so rtpp_record.c stays
* 353/444 lines dark in production coverage (2026-11-26 report; only the
* command_parser reaches 91 lines of it). This target drives:
* - src/rtpp_record.c: ctor (RTP/RTCP/BOTH modes, spool/permanent path
* building, PATH_MAX handling, '/' refusal for call-id/tag names), the
* write path (rbuf batching, oversized synchronous writev, ad-hoc and
* pcap packet headers, epoch handling), and close (flush, spool rename);
* - src/commands/rpcpv1_record.c: "R[s]" command option parsing via the
* real control protocol (ExecuteRTPPCommand);
* - session/stream plumbing feeding the recorded packets.
*
* Input layout: one op byte (bit0 reseed, bit1 pcap mode, bit2 orig suffix,
* bits3-4 record type 0=RTP/1=RTCP/2=BOTH, bit5 also run "R" command), one
* rname-length byte (0 = NULL name, i.e. derive from call-id/from-tag; else
* up to 63 bytes of fuzzed recording name), then rfz_get_chunk() chunks of
* [16-byte sockaddr_in][RTP/RTCP packet bytes], each written into the record
* with fresh arrival timestamps (alternating the session's two streams).
*
* The recording names, addresses and packet bytes are fully attacker
* controlled; path traversal attempts through the recording name are in
* scope (they can only land inside the fuzzer's spool/permanent dirs, which
* point at the OSS-Fuzz sandbox).
*/

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <assert.h>
#include <netdb.h>
#include <semaphore.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.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_pipe.h"
#include "rtpp_network.h"
#include "rtpp_codeptr.h"
#include "rtp_packet.h"
#include "rtpp_session.h"
#include "rtpp_stream.h"
#include "rtpp_bindaddr.h"
#include "rtpp_refcnt.h"
#include "rtpp_command.h"
#include "rtpp_hash_table.h"
#include "rtpp_record.h"
#include "advanced/packet_processor.h"
#include "advanced/pproc_manager.h"

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

#define RUN_CMD(s) ExecuteRTPPCommand(&gconf, (s), strlen(s), 0)

struct record_fuzz_args {
const char *rname;
int orig;
int record_type;
const char *data;
size_t size;
};

static int
record_foreach(void *dp, void *ap)
{
struct record_fuzz_args *rfa = (struct record_fuzz_args *)ap;
struct rtpp_session *sp = (struct rtpp_session *)dp;
struct rtpp_record *rrp;
struct rfz_chunk chunk = {.rem_size = rfa->size, .rem_data = rfa->data};
int i = 0;

rrp = rtpp_record_ctor(gconf.cfsp, NULL, sp, rfa->rname, rfa->orig,
rfa->record_type);
if (rrp == NULL)
return (RTPP_HT_MATCH_CONT);

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;
struct rtp_packet *pktp = rtp_packet_alloc();
assert(pktp != NULL);
struct sockaddr *rap = sstosa(&pktp->raddr);
memcpy(rap, chunk.data, sizeof(struct sockaddr_in));
rap->sa_family = AF_INET;
pktp->size = chunk.size - sizeof(struct sockaddr_in);
memcpy(pktp->data.buf, chunk.data + sizeof(struct sockaddr_in), pktp->size);
pktp->rtime.wall = 100.0 + i * 0.02;
pktp->rtime.mono = 1.0 + i * 0.02;
struct rtpp_stream *stp = sp->rtp->stream[i & 1];
/* Production packets carry their local address and port, set by
* the network layer on receipt; prepare_pkt_hdr_pcap() dereferences
* packet->laddr unconditionally, so mirror the production
* initialisation here (borrowed from the stream bindaddr). */
pktp->laddr = stp->laddr->addr;
pktp->lport = stp->port;
struct pkt_proc_ctx pktx = {
.strmp_in = stp,
.strmp_out = stp,
.pktp = pktp,
};
CALL_SMETHOD(rrp, pktwrite, &pktx);
RTPP_OBJ_DECREF(pktp);
i++;
} while (chunk.rem_size > 1);

RTPP_OBJ_DECREF(rrp);
return (RTPP_HT_MATCH_CONT);
}

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

if (r != 0)
return (-1);
return (0);
}

int
LLVMFuzzerTestOneInput(const char *data, size_t size)
{
struct {
union {
struct {
uint8_t reseed:1;
uint8_t pcap:1;
uint8_t orig:1;
uint8_t rtype0:1;
uint8_t rtype1:1;
uint8_t run_rcmd:1;
};
uint8_t value;
};
} op_flags;
char rnamebuf[64];

if (size < 2)
return (0);

op_flags.value = data[0];
int rname_len = (unsigned char)data[1];
if (rname_len > (int)sizeof(rnamebuf) - 1)
rname_len = sizeof(rnamebuf) - 1;
if (2 + rname_len > (int)size)
return (0);
data += 2;
size -= 2;

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

/* Fresh session for this iteration; "X" at the end tears it down. */
if (RUN_CMD("U callid_1 1.2.3.4 1234 from_tag_1") != 0)
return (0);
RUN_CMD("L callid_1 5.6.7.8 5678 from_tag_1 to_tag_1");

gconf.cfsp->record_pcap = op_flags.pcap;

if (op_flags.run_rcmd) {
/* Real control-protocol path: record the whole session. */
RUN_CMD("R callid_1 from_tag_1");
}

struct record_fuzz_args rfa;
if (rname_len > 0) {
memcpy(rnamebuf, data, rname_len);
rnamebuf[rname_len] = '\0';
rfa.rname = rnamebuf;
} else {
rfa.rname = NULL;
}
if (rfa.rname != NULL && strncmp(rfa.rname, "udp:", 4) == 0) {
/* Remote recording requires a fully provisioned remote_copy_args
* (an rtpp_socket pair); the production caller always supplies one,
* so passing NULL here would be an API-contract violation (it
* caused a NULL-deref SEGV in ropen_remote_ctor_pa). This harness
* covers the local recording modes: fall back to the session-
* derived local name for remote-style names. */
rfa.rname = NULL;
}
rfa.orig = op_flags.orig;
rfa.record_type = ((int)op_flags.rtype1 << 1) | op_flags.rtype0;
rfa.data = data + rname_len;
rfa.size = size - rname_len;

CALL_SMETHOD(gconf.cfsp->sessions_ht, foreach, record_foreach, &rfa, NULL);

assert(ExecuteRTPPCommand(&gconf, "X", 1, 0) == 0);
return (0);
}
16 changes: 16 additions & 0 deletions scripts/fuzz/fuzz_rtp_record.dict
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"udp:"
"127.0.0.1"
"../"
"/"
"rec"
".rtp"
".rtcp"
".pcap"
"callid_1"
"from_tag_1"
header_vpxt="\\x80"
header_m="\\x81"
pt_pcmu="\\x00"
rtcp_sr="\\x80\\xc8"
"\\x04\\xd2"
"\\x01\\x02\\x03\\x04"
5 changes: 5 additions & 0 deletions scripts/fuzz/fuzz_rtp_record.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
83 changes: 83 additions & 0 deletions scripts/fuzz/gen_rtp_record_seeds.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#!/usr/bin/env python3
"""
Seed corpus generator for fuzz_rtp_record.

Input layout (see fuzz_rtp_record.c): [op byte][rname_len byte][rname][chunks...],
chunks = rfz_get_chunk() framing of [16-byte sockaddr_in][RTP packet bytes].

Seeds cover the dark rtpp_record.c dimensions: NULL vs explicit recording
names (including path-shaped names), RTP/RTCP/BOTH record types, pcap vs
ad-hoc header modes, rbuf batching vs oversized synchronous writev, and the
control-protocol "R" command path.
"""
import os
import struct
import sys


def chunk(payload: bytes) -> bytes:
n = len(payload) - 1
parts = []
while n >= 255:
parts.append(255)
n -= 255
parts.append(n)
return bytes(parts) + payload


def addr(port: int, ip: bytes = b"\x01\x02\x03\x04") -> bytes:
return ip + struct.pack(">H", port) + b"\x00" * 10


def rtp(seq: int, ts: int, ssrc: int, pt: int = 0, marker: int = 0,
payload: bytes = b"\x00" * 160) -> bytes:
return struct.pack(">BBHII", 0x80 | 0, (marker << 7) | (pt & 0x7F),
seq & 0xFFFF, ts & 0xFFFFFFFF, ssrc) + payload


def rtcp_sr(seq: int) -> bytes:
# RTCP SR: v=2, p=0, count=0, PT=200, length=6 (bogus-but-parseable)
hdr = struct.pack(">BBH", 0x80, 200, 6)
return hdr + struct.pack(">II", 0xDEADBEEF, 1234) + b"\x00" * 20


def seed(op: int, rname: bytes, packets) -> bytes:
data = bytes([op, len(rname)]) + rname
for i, pkt in enumerate(packets):
data += chunk(addr(1234) + pkt)
return data


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

def put(name, blob):
with open(os.path.join(outdir, name), "wb") as f:
f.write(blob)

stream20 = [rtp(i, i * 160, 0xABCD0001) for i in range(20)]
bigpkts = [rtp(i, i * 160, 0xABCD0002, payload=b"\x41" * 900) for i in range(6)]
tiny = [rtp(i, i * 160, 0xABCD0003, payload=b"\x00" * 4) for i in range(30)]
mixed = [rtp(0, 0, 0xABCD0004), rtcp_sr(0), rtp(1, 160, 0xABCD0004), rtcp_sr(1)]

# 1. NULL name (session-derived), RTP mode, ad-hoc headers.
put("nullname_rtp", seed(0x00, b"", stream20))
# 2. Explicit short name, BOTH mode (single-file), ad-hoc.
put("name_both", seed(0x14, b"rec1", stream20))
# 3. Path-shaped name, pcap mode.
put("pcap_pathname", seed(0x22, b"sub/dir/rec", stream20))
# 4. Traversal attempt + oversized packets (sync writev + flush paths).
put("trav_big", seed(0x05, b"../rec", bigpkts))
# 5. RTCP type, tiny packets (rbuf batching).
put("rtcp_tiny", seed(0x08, b"r1", tiny))
# 6. Control-protocol "R" command bit + mixed RTP/RTCP content.
put("rcmd_mixed", seed(0x30, b"", mixed))
# 7. orig suffix variant.
put("orig_rtp", seed(0x04, b"origrec", stream20[:8]))

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 rtp_record"
OBJS0="${OBJS}"
for fz in ${ALL}
do
Expand Down Expand Up @@ -168,6 +168,17 @@ then
rm -rf "${SEEDTMP}"
fi

# fuzz_rtp_record support: pack a seed corpus covering recording-name
# variants (session-derived, explicit, path-shaped), RTP/RTCP/BOTH record
# types, pcap/ad-hoc modes and the "R" control-protocol command path.
if [ -e scripts/fuzz/gen_rtp_record_seeds.py ]
then
SEEDTMP="`mktemp -d ${OUT}/rec_seed_corpus.XXXXXXXX`"
python3 scripts/fuzz/gen_rtp_record_seeds.py "${SEEDTMP}"
(cd "${SEEDTMP}" && zip -q -r "${OUT}/fuzz_rtp_record_seed_corpus.zip" .)
rm -rf "${SEEDTMP}"
fi

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