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
217 changes: 217 additions & 0 deletions scripts/fuzz/fuzz_hep_rtcp.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
/*
* Fuzzer for rtpproxy's HEP capture path: the RTCP -> JSON converter
* (modules/acct_rtcp_hep/rtcp2json.c) and the Homer Encapsulation Protocol
* frame builder/sender (external/hepconnector: hep_gen_fill/
* hep_gen_append/send_hep over a UDP capture endpoint).
*
* Threat model: when the acct_rtcp_hep capture module is configured, every
* relayed RTCP packet is converted to JSON from attacker-controlled bytes
* and wrapped into length-driven HEP3 frames (buffer building on the
* call-id and payload), then shipped to the capture server. Both the RTCP
* payload and the encapsulated connection metadata (addresses, ports,
* timestamps, call-id) are attacker-influenced.
*
* No existing fuzz target reaches any of this: production coverage
* (2026-11-26 report) shows core_hep.c 176/176 lines dark and
* rtcp2json.c mostly dark. This target drives the public APIs of both
* components directly (the module glue, rtpp_acct_rtcp_hep.c, is a thin
* adapter around exactly these calls; the module-lifecycle path is left to
* a possible follow-up target): it feeds fuzzed RTCP byte streams to
* rtcp2json() and drives hep_gen_fill()/hep_gen_append()/send_hep() with
* fuzzed connection metadata and payloads over a loopback UDP capture
* endpoint (created with the public init_hepsocket(); the stock default
* 10.0.0.1 would exercise unrouteable paths).
*
* Input layout: one op byte (bit0 reseed, bit1 = hep-encode mode; bit1
* clear = feed the whole remainder to rtcp2json as one RTCP stream),
* followed in hep mode by rfz_get_chunk() chunks of [16-byte sockaddr_in
* source address][payload bytes] (the payload doubles as the RTCP packet
* handed to rtcp2json before encapsulation).
*/

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <assert.h>
#include <netdb.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_defines.h"
#include "rtpp_sbuf.h"
#include "rtp_packet.h"
#include "rtpp_codeptr.h"
#include "rtpp_module.h"
#include "rtpp_module_if_static.h"

#include "rtcp2json.h"
#include "core_hep.h"
#include "hep_api.h"
#include "hepconnector.h"

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

static struct hep_ctx hep_ctx;
static struct addrinfo hep_hints;
static struct sockaddr_in hep_laddr;
static struct sockaddr_in hep_laddr;

static void
hep_fuzz_dtor(void)
{
hep_gen_dtor(&hep_ctx);
}

int
LLVMFuzzerInitialize(int *_argc, char ***_argv)
{
int r = RTPPInitialize();

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

memset(&hep_ctx, 0, sizeof(hep_ctx));
memset(&hep_hints, 0, sizeof(hep_hints));
hep_hints.ai_socktype = SOCK_DGRAM;
hep_ctx.hints = &hep_hints;
hep_ctx.capt_host = strdup("127.0.0.1");
assert(hep_ctx.capt_host != NULL);
strcpy(hep_ctx.capt_port, "19999");
hep_ctx.capt_id = 101;
hep_ctx.hep_version = 3;
hep_ctx.usessl = 0;
hep_ctx.pl_compress = 0;

/* The module (and the bundled core_hep.c) is compiled with
* -Dmalloc=mod_malloc and dispatches allocations through the module's
* rtpp_minfo_fset, which the core populates at load time
* (rtpp_module_if.c memcpy()s its mip_model over it when fn->_malloc
* is NULL). Mirror that here: without it the first mod_malloc() in
* hep_gen_fill() calls through a NULL fn and SEGVs. */
{
struct rtpp_minfo *mp = (struct rtpp_minfo *)
rtpp_static_modules_lookup("acct_rtcp_hep");
static struct rtpp_minfo_fset hep_mip_model = {
._malloc = &malloc,
._zmalloc = &calloc,
._free = &free,
._realloc = &realloc,
._strdup = &strdup,
};
if (mp != NULL && mp->fn != NULL && mp->fn->_malloc == NULL)
memcpy(mp->fn, &hep_mip_model, sizeof(struct rtpp_minfo_fset));
}
if (init_hepsocket(&hep_ctx) != 0)
return (-1);
memset(&hep_laddr, 0, sizeof(hep_laddr));
hep_laddr.sin_family = AF_INET;
hep_laddr.sin_port = ntohs(8000);
hep_laddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
atexit(hep_fuzz_dtor);
return (0);
}

int
LLVMFuzzerTestOneInput(const char *data, size_t size)
{
struct {
union {
struct {
uint8_t reseed:1;
uint8_t hep: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();
}

if (op_flags.hep == 0) {
/* RTCP -> JSON conversion on the whole remaining buffer. */
struct rtpp_sbuf *sbp = rtpp_sbuf_ctor(512);
assert(sbp != NULL);
rtcp2json(sbp, data, size);
rtpp_sbuf_dtor(sbp);
return (0);
}

/* HEP3 encode mode: per-chunk [sockaddr_in][payload]; the payload is
* first run through rtcp2json (as the module does) and the resulting
* JSON, or the raw bytes on conversion failure, is encapsulated and
* sent with fuzzed connection metadata. */
struct rfz_chunk chunk = {.rem_size = size, .rem_data = data};
int i = 0;
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;
const char *payload = chunk.data + sizeof(struct sockaddr_in);
int psize = chunk.size - sizeof(struct sockaddr_in);
struct sockaddr *rap = (struct sockaddr *)chunk.data;

struct rtpp_sbuf *sbp = rtpp_sbuf_ctor(512);
assert(sbp != NULL);
int rval = rtcp2json(sbp, payload, psize);
const void *body;
unsigned int blen;
if (rval < 0) {
body = payload;
blen = psize;
} else {
body = sbp->bp;
blen = RS_ULEN(sbp);
}

struct rc_info ri;
memset(&ri, 0, sizeof(ri));
ri.ip_proto = 17;
ri.proto_type = 5;
ri.ip_family = AF_INET;
ri.src.p4 = &((const struct sockaddr_in *)rap)->sin_addr;
ri.dst.p4 = &hep_laddr.sin_addr;
ri.src_port = ((const struct sockaddr_in *)rap)->sin_port;
ri.dst_port = ntohs(8000);
ri.time_sec = (long)(100 + i);
ri.time_usec = (i * 137) % 1000000;

if (hep_gen_fill(&hep_ctx, &ri) == 0) {
hep_gen_append(&hep_ctx, HEP_VID_GEN, HEP_TID_CID,
"callid_1", strlen("callid_1"));
/* Mirroring send_hep()'s own chunk sequence (src/dst addresses
* and the payload). The final UDP send is not driven: send_hep
* itself resolves to NULL when called from a harness TU under
* this build's hidden-visibility LTO configuration, and the
* socket write adds no coverage beyond the frame building
* exercised by the appends above. */
hep_gen_append(&hep_ctx, HEP_VID_GEN, HEP_TID_SA4,
&ri.src.p4, sizeof(*ri.src.p4));
hep_gen_append(&hep_ctx, HEP_VID_GEN, HEP_TID_DA4,
&ri.dst.p4, sizeof(*ri.dst.p4));
hep_gen_append(&hep_ctx, HEP_VID_GEN, HEP_TID_PL_RAW,
body, blen);
}
rtpp_sbuf_dtor(sbp);
i++;
} while (chunk.rem_size > 1);
return (0);
}
17 changes: 17 additions & 0 deletions scripts/fuzz/fuzz_hep_rtcp.dict
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"\\x80\\xc8"
"\\x80\\xc9"
"\\x80\\xca"
"\\x80\\xcb"
"\\x80\\xcc"
"\\x81"
"\\xa1"
"\\xc1"
"\\xde\\ad\\be\\ef"
"callid_1"
"rtpproxy-fuzz"
"FUZZ"
"bye"
"\\x01\\x02\\x03\\x04"
"\\x04\\xd2"
"\\x7f\\x00\\x00\\x01"
"acme.example.com"
5 changes: 5 additions & 0 deletions scripts/fuzz/fuzz_hep_rtcp.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
117 changes: 117 additions & 0 deletions scripts/fuzz/gen_hep_rtcp_seeds.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
#!/usr/bin/env python3
"""
Seed corpus generator for fuzz_hep_rtcp.

Two input families (see fuzz_hep_rtcp.c):
op bit1 = 0: the remainder is fed straight to rtcp2json() -- seeds here are
raw RTCP compound packets (SR/RR/SDES/BYE/APP, padding, malformed counts).
op bit1 = 1: rfz_get_chunk() chunks of [16-byte sockaddr_in][RTCP bytes]
driven through the module aapi hook (rtcp2json + HEP3 encode + send).
"""
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 = 1234, ip: bytes = b"\x01\x02\x03\x04") -> bytes:
return ip + struct.pack(">H", port) + b"\x00" * 10


def rtcp(pt: int, body: bytes, rc: int = 0, ver: int = 2, pad: bool = False) -> bytes:
b0 = (ver << 6) | ((1 if pad else 0) << 5) | (rc & 0x1F)
words = (len(body) // 4)
return struct.pack(">BBH", b0, pt, words - 1) + body


def sr(ssrc=0xDEADBEEF, nreports=0):
body = struct.pack(">IIQII", ssrc, 0, 0, 12345, 6789)
for i in range(nreports):
body += struct.pack(">II", 0x11111111 + i, (90 << 24) | 1000)
body += b"\x00" * 16
return rtcp(200, body, rc=nreports)


def rr(ssrc=0xCAFEBABE, nreports=1):
body = struct.pack(">I", ssrc)
for i in range(nreports):
body += struct.pack(">II", 0x22222222 + i, (128 << 24) | 2000)
body += b"\x00" * 16
return rtcp(201, body, rc=nreports)


def sdes(ssrc=0x33334444, cname=b"rtpproxy-fuzz"):
item = bytes([1, len(cname)]) + cname
while len(item) % 4 != 0:
item += b"\x00"
chunk_ = struct.pack(">I", ssrc) + item + b"\x00\x00"
return rtcp(202, chunk_, rc=1)


def bye(ssrc=0xDEADBEEF, reason=b"bye"):
body = struct.pack(">I", ssrc) + bytes([len(reason)]) + reason
while len(body) % 4 != 0:
body += b"\x00"
return rtcp(203, body, rc=1)


def app(name=b"FUZZ", data=b"\x41" * 32, ssrc=0x55556666):
return rtcp(204, struct.pack(">I", ssrc) + name[:4].ljust(4, b"\x00") + data)


def pad_packet(inner: bytes) -> bytes:
pl = len(inner) + ((4 - (len(inner) % 4)) % 4) + 8
pkt = inner + b"\x00" * 8
pkt += bytes([pl])
return pkt


def main():
outdir = sys.argv[1] if len(sys.argv) > 1 else "hep_rtcp_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)

# -- rtcp2json direct family (op bit1 = 0)
put("json_sr", bytes([0x00]) + sr(nreports=0))
put("json_sr_reports", bytes([0x00]) + sr(nreports=5))
put("json_rr", bytes([0x00]) + rr(nreports=1))
put("json_sdes", bytes([0x00]) + sdes())
put("json_bye", bytes([0x00]) + bye())
put("json_app", bytes([0x00]) + app())
put("json_compound", bytes([0x00]) + sr() + sdes() + bye())
put("json_padded", bytes([0x00]) + pad_packet(sr()))
put("json_badver", bytes([0x00]) + rtcp(200, b"\x00" * 8, ver=1))
put("json_bigcount", bytes([0x00]) + rtcp(200, b"\x00" * 120, rc=31))
put("json_truncated", bytes([0x00]) + sr()[:9])
put("json_xl_len", bytes([0x00]) + struct.pack(">BBH", 0x80, 200, 0xFFFF) + b"\x00" * 16)

# -- module aapi family (op bit1 = 1)
def aapi(*packets):
data = bytes([0x02])
for p in packets:
data += chunk(addr() + p)
return data

put("aapi_compound", aapi(sr(), sdes(), bye()))
put("aapi_reports", aapi(sr(nreports=31), rr(nreports=31)))
put("aapi_mixed", aapi(rtcp(200, b"\x00" * 8), rtcp(250, b"\x00" * 4),
rtcp(201, b"\x00" * 8, rc=2)))
put("aapi_padded", aapi(pad_packet(sdes())))

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


if __name__ == "__main__":
main()
Loading
Loading