[fuzz] RTSP server: rtsp_server (session fed arbitrary requests after a valid prefix) and announce targets
This commit is contained in:
@@ -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<Vec<u8>> {
|
||||
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);
|
||||
}
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user