From 27b7f2bf1db844dba4258ad4e8639d23c1661055 Mon Sep 17 00:00:00 2001 From: mnovak Date: Fri, 4 Dec 2026 12:23:48 +0100 Subject: [PATCH] [fuzz] RTSP server: rtsp_server (session fed arbitrary requests after a valid prefix) and announce targets --- fuzz/Cargo.toml | 14 +++ fuzz/fuzz_targets/announce.rs | 26 +++++ fuzz/fuzz_targets/rtsp_server.rs | 172 +++++++++++++++++++++++++++++++ 3 files changed, 212 insertions(+) create mode 100644 fuzz/fuzz_targets/announce.rs create mode 100644 fuzz/fuzz_targets/rtsp_server.rs diff --git a/fuzz/Cargo.toml b/fuzz/Cargo.toml index 8c61c26..0509116 100644 --- a/fuzz/Cargo.toml +++ b/fuzz/Cargo.toml @@ -218,3 +218,17 @@ path = "fuzz_targets/video_packetizer_roundtrip.rs" test = false doc = false bench = false + +[[bin]] +name = "rtsp_server" +path = "fuzz_targets/rtsp_server.rs" +test = false +doc = false +bench = false + +[[bin]] +name = "announce" +path = "fuzz_targets/announce.rs" +test = false +doc = false +bench = false diff --git a/fuzz/fuzz_targets/announce.rs b/fuzz/fuzz_targets/announce.rs new file mode 100644 index 0000000..d15cb8f --- /dev/null +++ b/fuzz/fuzz_targets/announce.rs @@ -0,0 +1,26 @@ +//! Fuzzes the host's ANNOUNCE parser (hostile client SDP). +//! Asserts invariants, not only "no panic": an accepted payload is within every range and cap, and writing it back +//! gives the same config. +//! How to run: see the comment at the top of `fuzz/Cargo.toml`. + +#![no_main] + +use libfuzzer_sys::fuzz_target; + +use domistream::rtsp::server::{ + MAX_ANNOUNCE_ATTRIBUTES, MAX_AUDIO_PACKET_DURATION_MS, MAX_BITRATE_KBPS, MAX_DIMENSION, MAX_FPS, MAX_PACKET_SIZE, + MIN_PACKET_SIZE, StreamConfig, parse_announce, write_announce, +}; + +fuzz_target!(|data: &[u8]| { + let Ok(c) = parse_announce(data) else { return }; + assert!((1..=MAX_DIMENSION).contains(&c.width) && (1..=MAX_DIMENSION).contains(&c.height)); + assert!((1..=MAX_FPS).contains(&c.max_fps)); + assert!((MIN_PACKET_SIZE..=MAX_PACKET_SIZE).contains(&c.packet_size)); + assert!((1..=MAX_BITRATE_KBPS).contains(&c.bitrate_kbps)); + assert!((1..=MAX_AUDIO_PACKET_DURATION_MS).contains(&c.audio_packet_duration_ms)); + assert!((1..=8).contains(&c.surround.channels)); + assert!(c.unknown_attributes as usize <= MAX_ANNOUNCE_ATTRIBUTES); + let again = parse_announce(&write_announce(&c)).expect("what was accepted writes back"); + assert_eq!(again, StreamConfig { unknown_attributes: 0, ..c }); +}); diff --git a/fuzz/fuzz_targets/rtsp_server.rs b/fuzz/fuzz_targets/rtsp_server.rs new file mode 100644 index 0000000..c31a18a --- /dev/null +++ b/fuzz/fuzz_targets/rtsp_server.rs @@ -0,0 +1,172 @@ +//! Fuzzes the host's RTSP session with arbitrary requests, optionally after a valid prefix of the negotiation so the +//! later states are reached too. +//! Asserts invariants, not only "no panic": every answer parses with a documented status, 200 exactly when nothing +//! was refused, the state only moves forward and never on a refusal, and the events come once each, in order. +//! How to run: see the comment at the top of `fuzz/Cargo.toml`. + +#![no_main] + +use libfuzzer_sys::fuzz_target; + +use domistream::RandomSource; +use domistream::rtsp::client::VideoCodec; +use domistream::rtsp::sdp::FeatureFlags; +use domistream::rtsp::server::{ChromaSampling, StreamConfig, SurroundSettings, parse_announce, write_announce}; +use domistream::rtsp::{ + EncryptionFlags, HostOffer, OpusConfig, RtspFramer, RtspServer, RtspServerState, ServerConfig, ServerEvent, + StreamPorts, parse_response, +}; + +struct Fixed(u8); + +impl RandomSource for Fixed { + fn fill(&mut self, dest: &mut [u8]) { + dest.fill(self.0); + } +} + +fn rank(s: RtspServerState) -> u8 { + match s { + RtspServerState::Options => 0, + RtspServerState::Describe => 1, + RtspServerState::Setup => 2, + RtspServerState::Announce => 3, + RtspServerState::Play => 4, + RtspServerState::Playing => 5, + } +} + +fn announce() -> StreamConfig { + StreamConfig { + width: 1920, + height: 1080, + max_fps: 60, + packet_size: 1024, + bitrate_kbps: 20_000, + codec: VideoCodec::Hevc, + min_fec_packets: 2, + video_qos_traffic_type: 5, + audio_packet_duration_ms: 5, + audio_qos_traffic_type: 4, + max_reference_frames: 0, + reference_frame_invalidation: true, + dynamic_range_mode: 0, + encoder_csc_mode: 0, + chroma: ChromaSampling::Yuv420, + encryption: EncryptionFlags(7), + surround: SurroundSettings::default(), + unknown_attributes: 0, + } +} + +/// The valid negotiation, one request each. +fn prelude(session: &str) -> Vec> { + let body = write_announce(&announce()); + let mut v = vec![ + b"OPTIONS rtsp://192.0.2.10:48010 RTSP/1.0\r\nCSeq: 1\r\n\r\n".to_vec(), + b"DESCRIBE rtsp://192.0.2.10:48010 RTSP/1.0\r\nCSeq: 2\r\n\r\n".to_vec(), + ]; + for (i, s) in ["audio", "video", "control"].iter().enumerate() { + v.push( + format!("SETUP streamid={s}/0/0 RTSP/1.0\r\nCSeq: {}\r\nSession: {session}\r\nTransport: x\r\n\r\n", i + 3) + .into_bytes(), + ); + } + let mut a = format!("ANNOUNCE streamid=control/13/0 RTSP/1.0\r\nCSeq: 6\r\nContent-Length: {}\r\n\r\n", body.len()) + .into_bytes(); + a.extend_from_slice(&body); + v.push(a); + v.push(b"PLAY rtsp://192.0.2.10:48010 RTSP/1.0\r\nCSeq: 7\r\n\r\n".to_vec()); + // The first SETUP carries no session: none was issued yet. + if let Some(first) = v.get_mut(2) { + *first = b"SETUP streamid=audio/0/0 RTSP/1.0\r\nCSeq: 3\r\nTransport: x\r\n\r\n".to_vec(); + } + v +} + +struct Checker { + announced: u32, + played: u32, +} + +impl Checker { + fn feed(&mut self, s: &mut RtspServer, message: &[u8]) { + let before = s.state(); + let reply = s.handle_request(message); + let r = parse_response(&reply.bytes).expect("every answer parses"); + assert_eq!(r.status, reply.status); + assert!([200, 400, 451, 454, 455, 501].contains(&reply.status), "status {}", reply.status); + assert_eq!(reply.status == 200, reply.error.is_none()); + assert!(rank(s.state()) >= rank(before), "state went back"); + if reply.status != 200 { + assert_eq!(s.state(), before, "a refusal changed the state"); + } + while let Some(e) = s.poll_event() { + match e { + ServerEvent::Announced(c) => { + assert_eq!(self.announced, 0); + self.announced += 1; + let again = parse_announce(&write_announce(&c)).expect("an accepted ANNOUNCE writes back"); + assert_eq!(again, StreamConfig { unknown_attributes: 0, ..c }); + } + ServerEvent::PlayRequested => { + assert_eq!((self.announced, self.played), (1, 0)); + self.played += 1; + } + } + } + assert_eq!(self.played == 1, s.state() == RtspServerState::Playing); + } +} + +fuzz_target!(|data: &[u8]| { + let Some((&mode, rest)) = data.split_first() else { return }; + let config = ServerConfig { + ports: StreamPorts { audio: 48000, video: 47998, control: 47999 }, + offer: HostOffer { + feature_flags: FeatureFlags(3), + encryption_supported: EncryptionFlags(7), + reference_frame_invalidation: true, + h264: true, + hevc: true, + av1: mode & 0x40 != 0, + hdr: mode & 0x80 != 0, + yuv444: false, + opus_configs: vec![OpusConfig::stereo()], + }, + required_encryption: EncryptionFlags(1), + require_session_header: mode & 0x20 != 0, + session_timeout_secs: 90, + }; + let mut s = RtspServer::new(config, &mut Fixed(mode)).expect("a valid config"); + let mut check = Checker { announced: 0, played: 0 }; + let session = s.session_id().to_string(); + // Bits 2..=4: how many valid requests come first (0..=7). + for m in prelude(&session).iter().take(usize::from((mode >> 2) & 0x07)) { + check.feed(&mut s, m); + } + if mode & 0x01 == 0 { + // Messages separated by NUL bytes, each fed whole. + for m in rest.split(|b| *b == 0).take(32) { + check.feed(&mut s, m); + } + } else { + // A byte stream through the request framer. + let mut f = RtspFramer::for_requests(); + for chunk in rest.chunks(if mode & 0x02 == 0 { 7 } else { 512 }) { + if f.push(chunk).is_err() { + return; + } + loop { + match f.next_message() { + Ok(Some(m)) => check.feed(&mut s, &m), + Ok(None) => break, + Err(_) => return, + } + } + } + if let Ok(Some(m)) = f.finish() { + check.feed(&mut s, &m); + } + } +});