From e04551dc6eefe0ef0959abf562fe6a522213abff Mon Sep 17 00:00:00 2001 From: Arthur Chan Date: Mon, 30 Nov 2026 06:33:37 +0000 Subject: [PATCH] OSS-Fuzz: Add new fuzzer/corpus/dict/options targets RTP record handling Signed-off-by: Arthur Chan --- scripts/fuzz/fuzz_rtp_record.c | 218 +++++++++++++++++++++++++++ scripts/fuzz/fuzz_rtp_record.dict | 16 ++ scripts/fuzz/fuzz_rtp_record.options | 5 + scripts/fuzz/gen_rtp_record_seeds.py | 83 ++++++++++ scripts/fuzz/oss-fuzz-build.sh | 13 +- 5 files changed, 334 insertions(+), 1 deletion(-) create mode 100644 scripts/fuzz/fuzz_rtp_record.c create mode 100644 scripts/fuzz/fuzz_rtp_record.dict create mode 100644 scripts/fuzz/fuzz_rtp_record.options create mode 100644 scripts/fuzz/gen_rtp_record_seeds.py diff --git a/scripts/fuzz/fuzz_rtp_record.c b/scripts/fuzz/fuzz_rtp_record.c new file mode 100644 index 000000000..ad5d73753 --- /dev/null +++ b/scripts/fuzz/fuzz_rtp_record.c @@ -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 +#include +#include +#include +#include +#include +#include +#include +#include + +#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); +} diff --git a/scripts/fuzz/fuzz_rtp_record.dict b/scripts/fuzz/fuzz_rtp_record.dict new file mode 100644 index 000000000..97efe1dcb --- /dev/null +++ b/scripts/fuzz/fuzz_rtp_record.dict @@ -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" diff --git a/scripts/fuzz/fuzz_rtp_record.options b/scripts/fuzz/fuzz_rtp_record.options new file mode 100644 index 000000000..ab1f3fb12 --- /dev/null +++ b/scripts/fuzz/fuzz_rtp_record.options @@ -0,0 +1,5 @@ +[libfuzzer] +detect_leaks=0 +max_len=8192 +print_final_stats=1 +use_value_profile=1 diff --git a/scripts/fuzz/gen_rtp_record_seeds.py b/scripts/fuzz/gen_rtp_record_seeds.py new file mode 100644 index 000000000..472d16aa9 --- /dev/null +++ b/scripts/fuzz/gen_rtp_record_seeds.py @@ -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() diff --git a/scripts/fuzz/oss-fuzz-build.sh b/scripts/fuzz/oss-fuzz-build.sh index 347b760c6..7dc0e0c67 100755 --- a/scripts/fuzz/oss-fuzz-build.sh +++ b/scripts/fuzz/oss-fuzz-build.sh @@ -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 @@ -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