diff --git a/crates/kernel/src/lib.rs b/crates/kernel/src/lib.rs index ee493ab..af9d19c 100644 --- a/crates/kernel/src/lib.rs +++ b/crates/kernel/src/lib.rs @@ -118,6 +118,7 @@ pub mod sched; #[allow(dead_code, clippy::all)] pub mod sched_syscalls; pub mod sctp; +pub(crate) mod sctp_packet; pub mod seccomp; pub mod seccomp_filter; pub mod seccomp_notifier; diff --git a/crates/kernel/src/net.rs b/crates/kernel/src/net.rs index ba6e7dc..d4ae954 100644 --- a/crates/kernel/src/net.rs +++ b/crates/kernel/src/net.rs @@ -350,6 +350,9 @@ pub const PROTO_UDP: u8 = 17; /// IP protocol number for GRE (RFC 2784). pub const PROTO_GRE: u8 = 47; +/// IP protocol number for SCTP (RFC 9260). +pub const PROTO_SCTP: u8 = 132; + /// Parsed IPv4 header. /// /// Stored in host byte order; the raw on-wire format uses @@ -642,6 +645,10 @@ pub struct NetworkStack { gre_admitted: u64, /// Count of GRE frames rejected by [`crate::gre::validate_gre_packet`]. gre_dropped: u64, + /// Count of SCTP packets admitted by the protocol-132 gate. + sctp_admitted: u64, + /// Count of SCTP packets rejected by the protocol-132 gate. + sctp_dropped: u64, } impl NetworkStack { @@ -661,6 +668,8 @@ impl NetworkStack { ipv6: Ipv6Stack::new(Ipv6Addr::link_local_from_mac(&mac), mac), gre_admitted: 0, gre_dropped: 0, + sctp_admitted: 0, + sctp_dropped: 0, } } @@ -674,6 +683,16 @@ impl NetworkStack { self.gre_dropped } + /// Number of SCTP packets admitted by the protocol-132 gate. + pub const fn sctp_admitted(&self) -> u64 { + self.sctp_admitted + } + + /// Number of SCTP packets dropped by the protocol-132 gate. + pub const fn sctp_dropped(&self) -> u64 { + self.sctp_dropped + } + /// Process an incoming Ethernet frame. /// /// Parses the Ethernet header and dispatches to the appropriate @@ -826,11 +845,11 @@ impl NetworkStack { /// Dispatches by IP protocol after verifying the IPv4 header checksum and /// that the datagram is addressed to us: ICMP echo requests are answered /// (returning the reply frame length), UDP datagrams pass a checksum and - /// per-port framing admission gate, and GRE (protocol 47) is validated - /// structurally with C-bit checksum verification. Only ICMP produces a - /// reply; UDP and GRE are consumed and return `Ok(0)`, which also covers - /// every silently dropped malformed frame. Any other protocol returns - /// [`Error::NotImplemented`]. + /// per-port framing admission gate, GRE (protocol 47) is validated with + /// C-bit checksum verification, and SCTP (protocol 132) passes a stateless + /// CRC32c and chunk-chain gate. Only ICMP produces a reply; UDP, GRE, and + /// SCTP are consumed and return `Ok(0)`, including silently dropped + /// malformed frames. Any other protocol returns [`Error::NotImplemented`]. fn handle_ipv4( &mut self, eth: &EtherHeader, @@ -872,6 +891,13 @@ impl NetworkStack { } Ok(0) } + PROTO_SCTP => { + match self.handle_sctp_packet(&ip_hdr, ip_payload) { + Ok(()) => self.sctp_admitted = self.sctp_admitted.saturating_add(1), + Err(_) => self.sctp_dropped = self.sctp_dropped.saturating_add(1), + } + Ok(0) + } _ => Err(Error::NotImplemented), } } @@ -933,6 +959,21 @@ impl NetworkStack { Ok(()) } + /// Validate an SCTP packet before protocol-132 admission. + /// + /// This stack has no IPv4 reassembly, so MF or a nonzero fragment offset + /// is rejected locally. DF is outside the mask and remains admissible. + /// + /// # Errors + /// + /// Returns [`Error::InvalidArgument`] for fragments or malformed SCTP. + fn handle_sctp_packet(&self, ip_hdr: &Ipv4Header, sctp_payload: &[u8]) -> Result<()> { + if ip_hdr.flags_frag & 0x3FFF != 0 { + return Err(Error::InvalidArgument); + } + crate::sctp_packet::validate_sctp_packet(sctp_payload) + } + /// Validate an incoming UDP datagram before delivery. /// /// Re-parses the datagram so the on-wire UDP `length` field — not @@ -1047,6 +1088,10 @@ impl NetworkStack { } } +#[cfg(test)] +#[path = "net_sctp_tests.rs"] +mod sctp_tests; + // ========================================================================= // Tests // ========================================================================= diff --git a/crates/kernel/src/net_sctp_tests.rs b/crates/kernel/src/net_sctp_tests.rs new file mode 100644 index 0000000..7d485f7 --- /dev/null +++ b/crates/kernel/src/net_sctp_tests.rs @@ -0,0 +1,170 @@ +// Copyright 2026 ONCRIX Contributors +// SPDX-License-Identifier: Apache-2.0 + +use super::*; + +const LOCAL_MAC: [u8; 6] = [0xAA; 6]; +const SOURCE_MAC: [u8; 6] = [0xBB; 6]; +const LOCAL_IP: [u8; 4] = [10, 0, 0, 2]; +const SOURCE_IP: [u8; 4] = [10, 0, 0, 1]; +const SCTP_INIT_PACKET: [u8; 32] = [ + 0x13, 0x88, 0x00, 0x50, 0x00, 0x00, 0x00, 0x00, 0x12, 0xF6, 0xF6, 0xE9, 0x01, 0x00, 0x00, 0x14, + 0xDE, 0xAD, 0xBE, 0xEF, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x02, 0x71, 0x5B, 0x5A, 0x94, +]; + +fn sctp_stack() -> NetworkStack { + NetworkStack::new(LOCAL_MAC, LOCAL_IP, SOURCE_IP, [255, 255, 255, 0]) +} + +fn ip_header(flags_frag: u16, protocol: u8) -> Ipv4Header { + Ipv4Header { + version_ihl: 0x45, + tos: 0, + total_len: (IPV4_HEADER_MIN_LEN + SCTP_INIT_PACKET.len()) as u16, + id: 0, + flags_frag, + ttl: 64, + protocol, + checksum: 0, + src_addr: SOURCE_IP, + dst_addr: LOCAL_IP, + } +} + +fn ip_frame(protocol: u8, flags_frag: u16, payload: &[u8]) -> ([u8; 128], usize) { + let mut frame = [0u8; 128]; + assert_eq!( + write_ether(&mut frame, &LOCAL_MAC, &SOURCE_MAC, ETHER_TYPE_IPV4), + Ok(ETHER_HEADER_LEN) + ); + let mut ip = ip_header(flags_frag, protocol); + ip.total_len = (IPV4_HEADER_MIN_LEN + payload.len()) as u16; + assert_eq!( + write_ipv4(&mut frame[ETHER_HEADER_LEN..], &ip), + Ok(IPV4_HEADER_MIN_LEN) + ); + let payload_offset = ETHER_HEADER_LEN + IPV4_HEADER_MIN_LEN; + frame[payload_offset..payload_offset + payload.len()].copy_from_slice(payload); + (frame, payload_offset + payload.len()) +} + +#[test] +fn sctp_direct_valid_packet_is_admitted() { + let stack = sctp_stack(); + assert_eq!( + stack.handle_sctp_packet(&ip_header(0, PROTO_SCTP), &SCTP_INIT_PACKET), + Ok(()) + ); +} + +#[test] +fn sctp_direct_corrupt_packet_is_rejected() { + let stack = sctp_stack(); + let mut packet = SCTP_INIT_PACKET; + packet[8] ^= 1; + assert_eq!( + stack.handle_sctp_packet(&ip_header(0, PROTO_SCTP), &packet), + Err(Error::InvalidArgument) + ); +} + +#[test] +fn sctp_direct_more_fragments_and_offset_are_rejected() { + let stack = sctp_stack(); + assert_eq!( + stack.handle_sctp_packet(&ip_header(0x2000, PROTO_SCTP), &SCTP_INIT_PACKET), + Err(Error::InvalidArgument) + ); + assert_eq!( + stack.handle_sctp_packet(&ip_header(1, PROTO_SCTP), &SCTP_INIT_PACKET), + Err(Error::InvalidArgument) + ); +} + +#[test] +fn sctp_direct_do_not_fragment_is_admitted() { + let stack = sctp_stack(); + assert_eq!( + stack.handle_sctp_packet(&ip_header(0x4000, PROTO_SCTP), &SCTP_INIT_PACKET), + Ok(()) + ); +} + +#[test] +fn sctp_protocol_132_is_distinct_from_unsupported_protocol() { + let mut stack = sctp_stack(); + let mut reply = [0u8; 128]; + let (sctp_frame, sctp_len) = ip_frame(PROTO_SCTP, 0, &SCTP_INIT_PACKET); + assert_eq!( + stack.process_packet(&sctp_frame[..sctp_len], &mut reply), + Ok(0) + ); + let (unknown_frame, unknown_len) = ip_frame(99, 0, &SCTP_INIT_PACKET); + assert_eq!( + stack.process_packet(&unknown_frame[..unknown_len], &mut reply), + Err(Error::NotImplemented) + ); +} + +#[test] +fn sctp_corrupt_boundary_packet_is_silently_dropped() { + let mut stack = sctp_stack(); + let mut packet = SCTP_INIT_PACKET; + packet[8] ^= 1; + let (frame, len) = ip_frame(PROTO_SCTP, 0, &packet); + let mut reply = [0u8; 128]; + assert_eq!(stack.process_packet(&frame[..len], &mut reply), Ok(0)); +} + +#[test] +fn sctp_valid_boundary_packet_increments_admitted_only() { + let mut stack = sctp_stack(); + let (frame, len) = ip_frame(PROTO_SCTP, 0, &SCTP_INIT_PACKET); + let mut reply = [0u8; 128]; + assert_eq!(stack.process_packet(&frame[..len], &mut reply), Ok(0)); + assert_eq!(stack.sctp_admitted(), 1); + assert_eq!(stack.sctp_dropped(), 0); +} + +#[test] +fn sctp_corrupt_boundary_packet_increments_dropped_only() { + let mut stack = sctp_stack(); + let mut packet = SCTP_INIT_PACKET; + packet[8] ^= 1; + let (frame, len) = ip_frame(PROTO_SCTP, 0, &packet); + let mut reply = [0u8; 128]; + assert_eq!(stack.process_packet(&frame[..len], &mut reply), Ok(0)); + assert_eq!(stack.sctp_admitted(), 0); + assert_eq!(stack.sctp_dropped(), 1); +} + +#[test] +fn sctp_fragment_boundary_packet_increments_dropped_only() { + let mut stack = sctp_stack(); + let (frame, len) = ip_frame(PROTO_SCTP, 0x2000, &SCTP_INIT_PACKET); + let mut reply = [0u8; 128]; + assert_eq!(stack.process_packet(&frame[..len], &mut reply), Ok(0)); + assert_eq!(stack.sctp_admitted(), 0); + assert_eq!(stack.sctp_dropped(), 1); +} + +#[test] +fn sctp_fragment_gate_does_not_change_gre_fragment_admission() { + let mut stack = sctp_stack(); + let mut reply = [0u8; 128]; + let (sctp_frame, sctp_len) = ip_frame(PROTO_SCTP, 0x2000, &SCTP_INIT_PACKET); + assert_eq!( + stack.process_packet(&sctp_frame[..sctp_len], &mut reply), + Ok(0) + ); + + let gre_packet = [0x00, 0x00, 0x08, 0x00]; + let (gre_frame, gre_len) = ip_frame(PROTO_GRE, 0x2000, &gre_packet); + assert_eq!( + stack.process_packet(&gre_frame[..gre_len], &mut reply), + Ok(0) + ); + assert_eq!(stack.sctp_dropped(), 1); + assert_eq!(stack.gre_admitted(), 1); + assert_eq!(stack.gre_dropped(), 0); +} diff --git a/crates/kernel/src/sctp_packet.rs b/crates/kernel/src/sctp_packet.rs new file mode 100644 index 0000000..1b9eba1 --- /dev/null +++ b/crates/kernel/src/sctp_packet.rs @@ -0,0 +1,200 @@ +// Copyright 2026 ONCRIX Contributors +// SPDX-License-Identifier: Apache-2.0 + +//! SCTP packet CRC32c checksum and stateless structural validator. +//! +//! This tableless `no_std` module validates the RFC 9260 packet checksum and +//! chunk chain without association state, allocation, or response generation. +//! +//! SCTP serializes its CRC32c field as little-endian (`__le32` in Linux), +//! while ports, verification tags, chunk lengths, and other integer fields +//! remain in network byte order. CRC reflection and field serialization are +//! separate concerns. + +use crate::sctp::{SctpChunkType, parse_sctp_header}; +use oncrix_lib::{Error, Result}; + +#[path = "sctp_packet_shape.rs"] +mod shape; + +#[path = "sctp_packet_params.rs"] +mod params; + +const CRC32C_POLY: u32 = 0x82F6_3B78; +const SCTP_HEADER_LEN: usize = 12; +const CHUNK_HEADER_LEN: usize = 4; +const CHUNK_DATA: u8 = 0; +const CHUNK_INIT: u8 = 1; +const CHUNK_INIT_ACK: u8 = 2; +const CHUNK_ABORT: u8 = 6; +const CHUNK_SHUTDOWN: u8 = 7; +const CHUNK_SHUTDOWN_ACK: u8 = 8; +const CHUNK_COOKIE_ECHO: u8 = 10; +const CHUNK_SHUTDOWN_COMPLETE: u8 = 14; +const SCTP_CHECKSUM_RANGE: core::ops::Range = 8..12; + +/// Round `len` up to a four-byte boundary, rejecting arithmetic overflow. +const fn pad4(len: usize) -> Option { + match len.checked_add(3) { + Some(rounded) => Some(rounded & !3), + None => None, + } +} + +#[inline] +fn crc32c_step(crc: u32, byte: u8) -> u32 { + let mut crc = crc ^ u32::from(byte); + let mut bit = 0; + while bit < 8 { + crc = if crc & 1 != 0 { + (crc >> 1) ^ CRC32C_POLY + } else { + crc >> 1 + }; + bit += 1; + } + crc +} + +/// Compute a tableless reflected CRC32c (Castagnoli) checksum. +/// +/// Uses polynomial `0x82F6_3B78`, initial register `!0`, and final complement. +/// The returned value is host order; protocol serialization is separate. +#[cfg(test)] +fn crc32c(data: &[u8]) -> u32 { + let mut crc = !0; + for &byte in data { + crc = crc32c_step(crc, byte); + } + !crc +} + +fn sctp_packet_crc32c(packet: &[u8]) -> u32 { + let mut crc = !0; + for (index, &byte) in packet.iter().enumerate() { + let input = if SCTP_CHECKSUM_RANGE.contains(&index) { + 0 + } else { + byte + }; + crc = crc32c_step(crc, input); + } + !crc +} + +/// Validate an SCTP packet without association state or response generation. +/// +/// The gate verifies the little-endian CRC32c field, nonzero ports, complete +/// chunk framing and padding, unknown-chunk action bits, INIT invariants, and +/// the RFC 9260 no-bundling rule for INIT-family chunks. +/// +/// # Errors +/// +/// Returns [`Error::InvalidArgument`] for any malformed or inadmissible packet. +pub(crate) fn validate_sctp_packet(packet: &[u8]) -> Result<()> { + let (header, _chunks) = parse_sctp_header(packet)?; + + let wire_checksum = u32::from_le_bytes([packet[8], packet[9], packet[10], packet[11]]); + if wire_checksum != sctp_packet_crc32c(packet) { + return Err(Error::InvalidArgument); + } + if header.src_port == 0 || header.dst_port == 0 { + return Err(Error::InvalidArgument); + } + + validate_chunk_chain(packet, header.verification_tag) +} + +fn validate_chunk_chain(packet: &[u8], verification_tag: u32) -> Result<()> { + let mut offset = SCTP_HEADER_LEN; + let mut chunk_count = 0usize; + let mut has_singleton = false; + let mut has_data = false; + let mut has_abort = false; + let mut has_shutdown_family = false; + + while offset < packet.len() { + if packet.len() - offset < CHUNK_HEADER_LEN { + return Err(Error::InvalidArgument); + } + + let raw_type = packet[offset]; + let declared_len = + usize::from(u16::from_be_bytes([packet[offset + 2], packet[offset + 3]])); + if declared_len < CHUNK_HEADER_LEN { + return Err(Error::InvalidArgument); + } + + let declared_end = offset + .checked_add(declared_len) + .ok_or(Error::InvalidArgument)?; + if declared_end > packet.len() { + return Err(Error::InvalidArgument); + } + + let padded_len = pad4(declared_len).ok_or(Error::InvalidArgument)?; + let padded_end = offset + .checked_add(padded_len) + .ok_or(Error::InvalidArgument)?; + if padded_end > packet.len() { + return Err(Error::InvalidArgument); + } + + match SctpChunkType::from_raw(raw_type) { + Ok(chunk_type) => { + if raw_type == CHUNK_COOKIE_ECHO && chunk_count != 0 { + return Err(Error::InvalidArgument); + } + shape::validate(chunk_type, &packet[offset..declared_end], verification_tag)?; + has_singleton |= is_singleton_chunk(raw_type); + has_data |= raw_type == CHUNK_DATA; + has_abort |= raw_type == CHUNK_ABORT; + has_shutdown_family |= matches!(raw_type, CHUNK_SHUTDOWN | CHUNK_SHUTDOWN_ACK); + } + Err(_) if raw_type & 0x80 == 0 => return Err(Error::InvalidArgument), + Err(_) => {} + } + + chunk_count = chunk_count.saturating_add(1); + offset = padded_end; + } + + if chunk_count == 0 + || (has_singleton && chunk_count != 1) + || (has_data && (has_abort || has_shutdown_family)) + { + return Err(Error::InvalidArgument); + } + Ok(()) +} + +const fn is_singleton_chunk(raw_type: u8) -> bool { + matches!( + raw_type, + CHUNK_INIT | CHUNK_INIT_ACK | CHUNK_SHUTDOWN_COMPLETE + ) +} + +#[cfg(test)] +#[path = "sctp_packet_test_support.rs"] +mod test_support; + +#[cfg(test)] +#[path = "sctp_packet_crc_tests.rs"] +mod crc_tests; + +#[cfg(test)] +#[path = "sctp_packet_framing_tests.rs"] +mod framing_tests; + +#[cfg(test)] +#[path = "sctp_packet_init_tests.rs"] +mod init_tests; + +#[cfg(test)] +#[path = "sctp_packet_shape_tests.rs"] +mod shape_tests; + +#[cfg(test)] +#[path = "sctp_packet_parameter_tests.rs"] +mod parameter_tests; diff --git a/crates/kernel/src/sctp_packet_crc_tests.rs b/crates/kernel/src/sctp_packet_crc_tests.rs new file mode 100644 index 0000000..63e20bf --- /dev/null +++ b/crates/kernel/src/sctp_packet_crc_tests.rs @@ -0,0 +1,53 @@ +// Copyright 2026 ONCRIX Contributors +// SPDX-License-Identifier: Apache-2.0 + +//! Known-answer tests for SCTP CRC32c computation. + +use super::test_support::SCTP_INIT_PACKET; +use super::{crc32c, sctp_packet_crc32c}; + +#[test] +fn crc32c_empty_input_is_zero() { + assert_eq!(crc32c(&[]), 0x0000_0000); +} + +#[test] +fn crc32c_check_string_matches_kat() { + assert_eq!(crc32c(b"123456789"), 0xE306_9283); +} + +#[test] +fn crc32c_thirty_two_zero_bytes() { + assert_eq!(crc32c(&[0x00u8; 32]), 0x8A91_36AA); +} + +#[test] +fn crc32c_thirty_two_ff_bytes() { + assert_eq!(crc32c(&[0xFFu8; 32]), 0x62A8_AB43); +} + +#[test] +fn crc32c_ascending_and_descending_bytes() { + let mut ascending = [0u8; 32]; + for (index, byte) in ascending.iter_mut().enumerate() { + *byte = index as u8; + } + assert_eq!(crc32c(&ascending), 0x46DD_794E); + + let mut descending = [0u8; 32]; + for (index, byte) in descending.iter_mut().enumerate() { + *byte = (31 - index) as u8; + } + assert_eq!(crc32c(&descending), 0x113F_DB5C); +} + +#[test] +fn sctp_packet_crc32c_zeros_checksum_field() { + assert_eq!(sctp_packet_crc32c(&SCTP_INIT_PACKET), 0xE9F6_F612); + assert_eq!(&SCTP_INIT_PACKET[8..12], &[0x12, 0xF6, 0xF6, 0xE9]); + assert_eq!(0xE9F6_F612u32.to_le_bytes(), [0x12, 0xF6, 0xF6, 0xE9]); + + let mut garbled = SCTP_INIT_PACKET; + garbled[8..12].copy_from_slice(&[0xAA, 0xBB, 0xCC, 0xDD]); + assert_eq!(sctp_packet_crc32c(&garbled), 0xE9F6_F612); +} diff --git a/crates/kernel/src/sctp_packet_framing_tests.rs b/crates/kernel/src/sctp_packet_framing_tests.rs new file mode 100644 index 0000000..a288488 --- /dev/null +++ b/crates/kernel/src/sctp_packet_framing_tests.rs @@ -0,0 +1,231 @@ +// Copyright 2026 ONCRIX Contributors +// SPDX-License-Identifier: Apache-2.0 + +//! SCTP packet checksum, framing, length, and padding tests. + +use super::test_support::{ + SCTP_INIT_PACKET, SKIPPABLE_UNKNOWN_CHUNK_8, VALID_DATA_CHUNK, chunk_packet, seal, +}; +use super::{sctp_packet_crc32c, validate_sctp_packet}; +use oncrix_lib::Error; + +fn declared_length_packet(declared: u16) -> [u8; 20] { + let mut packet = [0u8; 20]; + packet[0..2].copy_from_slice(&0x1388u16.to_be_bytes()); + packet[2..4].copy_from_slice(&0x0050u16.to_be_bytes()); + packet[4..8].copy_from_slice(&0x1122_3344u32.to_be_bytes()); + packet[12] = 0x80; + packet[14..16].copy_from_slice(&declared.to_be_bytes()); + seal(packet) +} + +fn two_chunk_packet(first: &[u8; 8], second: &[u8; 8]) -> [u8; 28] { + let mut packet = [0u8; 28]; + packet[0..2].copy_from_slice(&0x1388u16.to_be_bytes()); + packet[2..4].copy_from_slice(&0x0050u16.to_be_bytes()); + packet[4..8].copy_from_slice(&0x1122_3344u32.to_be_bytes()); + packet[12..20].copy_from_slice(first); + packet[20..28].copy_from_slice(second); + seal(packet) +} + +fn data_control_packet(first: &[u8], second: &[u8]) -> [u8; N] { + assert_eq!(N, 12 + first.len() + second.len()); + let mut packet = [0u8; N]; + packet[0..2].copy_from_slice(&0x1388u16.to_be_bytes()); + packet[2..4].copy_from_slice(&0x0050u16.to_be_bytes()); + packet[4..8].copy_from_slice(&0x1122_3344u32.to_be_bytes()); + packet[12..12 + first.len()].copy_from_slice(first); + let second_start = 12 + first.len(); + packet[second_start..second_start + second.len()].copy_from_slice(second); + seal(packet) +} + +#[test] +fn accepts_literal_le_init_packet() { + assert!(validate_sctp_packet(&SCTP_INIT_PACKET).is_ok()); +} + +#[test] +fn rejects_corrupt_checksum() { + let mut packet = SCTP_INIT_PACKET; + packet[8] ^= 0xFF; + assert_eq!(validate_sctp_packet(&packet), Err(Error::InvalidArgument)); +} + +#[test] +fn rejects_big_endian_serialized_checksum() { + let mut packet = SCTP_INIT_PACKET; + packet[8..12].copy_from_slice(&0xE9F6_F612u32.to_be_bytes()); + assert_eq!(validate_sctp_packet(&packet), Err(Error::InvalidArgument)); +} + +#[test] +fn rejects_zero_source_port() { + let mut packet = chunk_packet::<20>(&SKIPPABLE_UNKNOWN_CHUNK_8, 0x1122_3344); + packet[0..2].copy_from_slice(&0u16.to_be_bytes()); + let packet = seal(packet); + assert_eq!(validate_sctp_packet(&packet), Err(Error::InvalidArgument)); +} + +#[test] +fn rejects_zero_destination_port() { + let mut packet = chunk_packet::<20>(&SKIPPABLE_UNKNOWN_CHUNK_8, 0x1122_3344); + packet[2..4].copy_from_slice(&0u16.to_be_bytes()); + let packet = seal(packet); + assert_eq!(validate_sctp_packet(&packet), Err(Error::InvalidArgument)); +} + +#[test] +fn rejects_header_only_packet() { + let mut packet = [0u8; 12]; + packet[0..2].copy_from_slice(&0x1388u16.to_be_bytes()); + packet[2..4].copy_from_slice(&0x0050u16.to_be_bytes()); + let crc = sctp_packet_crc32c(&packet); + packet[8..12].copy_from_slice(&crc.to_le_bytes()); + assert_eq!(validate_sctp_packet(&packet), Err(Error::InvalidArgument)); +} + +#[test] +fn rejects_truncated_header() { + assert_eq!(validate_sctp_packet(&[0u8; 8]), Err(Error::InvalidArgument)); +} + +#[test] +fn rejects_chunk_length_zero() { + assert_eq!( + validate_sctp_packet(&declared_length_packet(0)), + Err(Error::InvalidArgument), + ); +} + +#[test] +fn rejects_chunk_lengths_one_through_three() { + for declared in 1u16..=3 { + assert_eq!( + validate_sctp_packet(&declared_length_packet(declared)), + Err(Error::InvalidArgument), + "declared length {declared} must reject", + ); + } +} + +#[test] +fn rejects_declared_length_overrun() { + let mut chunk = SKIPPABLE_UNKNOWN_CHUNK_8; + chunk[3] = 0x0C; + assert_eq!( + validate_sctp_packet(&chunk_packet::<20>(&chunk, 0x1122_3344)), + Err(Error::InvalidArgument), + ); +} + +#[test] +fn rejects_final_omitted_padding() { + let mut packet = [0u8; 18]; + packet[0..2].copy_from_slice(&0x1388u16.to_be_bytes()); + packet[2..4].copy_from_slice(&0x0050u16.to_be_bytes()); + packet[4..8].copy_from_slice(&0x1122_3344u32.to_be_bytes()); + packet[12] = 0x80; + packet[14..16].copy_from_slice(&6u16.to_be_bytes()); + let packet = seal(packet); + assert_eq!(validate_sctp_packet(&packet), Err(Error::InvalidArgument)); +} + +#[test] +fn rejects_final_partial_padding() { + let mut packet = [0u8; 19]; + packet[0..2].copy_from_slice(&0x1388u16.to_be_bytes()); + packet[2..4].copy_from_slice(&0x0050u16.to_be_bytes()); + packet[4..8].copy_from_slice(&0x1122_3344u32.to_be_bytes()); + packet[12] = 0x80; + packet[14..16].copy_from_slice(&6u16.to_be_bytes()); + let packet = seal(packet); + assert_eq!(validate_sctp_packet(&packet), Err(Error::InvalidArgument)); +} + +#[test] +fn accepts_full_nonzero_padding() { + let mut packet = [0u8; 20]; + packet[0..2].copy_from_slice(&0x1388u16.to_be_bytes()); + packet[2..4].copy_from_slice(&0x0050u16.to_be_bytes()); + packet[4..8].copy_from_slice(&0x1122_3344u32.to_be_bytes()); + packet[12] = 0x80; + packet[14..16].copy_from_slice(&6u16.to_be_bytes()); + packet[16..20].copy_from_slice(&[0x99, 0x88, 0xFF, 0xFF]); + assert!(validate_sctp_packet(&seal(packet)).is_ok()); +} + +#[test] +fn accepts_aligned_skippable_unknown_chunk() { + let chunk = [0x80, 0x00, 0x00, 0x08, 0x11, 0x22, 0x00, 0x00]; + assert!(validate_sctp_packet(&chunk_packet::<20>(&chunk, 0x1122_3344)).is_ok()); +} + +#[test] +fn accepts_between_chunk_padding_walk() { + let mut packet = [0u8; 28]; + packet[0..2].copy_from_slice(&0x1388u16.to_be_bytes()); + packet[2..4].copy_from_slice(&0x0050u16.to_be_bytes()); + packet[4..8].copy_from_slice(&0x1122_3344u32.to_be_bytes()); + packet[12..20].copy_from_slice(&[0x80, 0x00, 0x00, 0x06, 0xAA, 0xBB, 0, 0]); + packet[20..28].copy_from_slice(&[0x80, 0x00, 0x00, 0x08, 0, 0, 0, 0]); + assert!(validate_sctp_packet(&seal(packet)).is_ok()); +} + +#[test] +fn rejects_cookie_echo_after_another_chunk() { + let cookie_echo = [10, 0, 0, 5, 0xAA, 0, 0, 0]; + assert_eq!( + validate_sctp_packet(&two_chunk_packet(&SKIPPABLE_UNKNOWN_CHUNK_8, &cookie_echo,)), + Err(Error::InvalidArgument), + ); +} + +#[test] +fn accepts_cookie_echo_first_with_following_chunk() { + let cookie_echo = [10, 0, 0, 5, 0xAA, 0, 0, 0]; + assert!( + validate_sctp_packet(&two_chunk_packet(&cookie_echo, &SKIPPABLE_UNKNOWN_CHUNK_8,)).is_ok() + ); +} + +#[test] +fn rejects_data_followed_by_abort() { + assert_eq!( + validate_sctp_packet(&data_control_packet::<36>(&VALID_DATA_CHUNK, &[6, 0, 0, 4],)), + Err(Error::InvalidArgument), + ); +} + +#[test] +fn rejects_abort_followed_by_data() { + assert_eq!( + validate_sctp_packet(&data_control_packet::<36>(&[6, 0, 0, 4], &VALID_DATA_CHUNK,)), + Err(Error::InvalidArgument), + ); +} + +#[test] +fn rejects_data_bundled_with_shutdown_family_in_both_orders() { + let shutdown = [7, 0, 0, 8, 0, 0, 0, 1]; + let shutdown_ack = [8, 0, 0, 4]; + let rejected = [ + validate_sctp_packet(&data_control_packet::<40>(&VALID_DATA_CHUNK, &shutdown)).is_err(), + validate_sctp_packet(&data_control_packet::<40>(&shutdown, &VALID_DATA_CHUNK)).is_err(), + validate_sctp_packet(&data_control_packet::<36>(&VALID_DATA_CHUNK, &shutdown_ack)).is_err(), + validate_sctp_packet(&data_control_packet::<36>(&shutdown_ack, &VALID_DATA_CHUNK)).is_err(), + ]; + assert_eq!(rejected, [true; 4]); +} + +#[test] +fn accepts_chunk_larger_than_516_bytes() { + let mut packet = [0u8; 532]; + packet[0..2].copy_from_slice(&0x1388u16.to_be_bytes()); + packet[2..4].copy_from_slice(&0x0050u16.to_be_bytes()); + packet[4..8].copy_from_slice(&0x1122_3344u32.to_be_bytes()); + packet[12] = 0x80; + packet[14..16].copy_from_slice(&520u16.to_be_bytes()); + assert!(validate_sctp_packet(&seal(packet)).is_ok()); +} diff --git a/crates/kernel/src/sctp_packet_init_tests.rs b/crates/kernel/src/sctp_packet_init_tests.rs new file mode 100644 index 0000000..aae60e3 --- /dev/null +++ b/crates/kernel/src/sctp_packet_init_tests.rs @@ -0,0 +1,201 @@ +// Copyright 2026 ONCRIX Contributors +// SPDX-License-Identifier: Apache-2.0 + +//! SCTP unknown-chunk action and INIT-family validation tests. + +use super::test_support::{ + SKIPPABLE_UNKNOWN_CHUNK_8, VALID_INIT_ACK_CHUNK, VALID_INIT_CHUNK, chunk_packet, seal, +}; +use super::validate_sctp_packet; +use oncrix_lib::Error; + +fn unknown_type_packet(raw_type: u8) -> [u8; 20] { + let mut chunk = SKIPPABLE_UNKNOWN_CHUNK_8; + chunk[0] = raw_type; + chunk_packet(&chunk, 0x1122_3344) +} + +fn unknown_then_skippable_packet(raw_type: u8) -> [u8; 28] { + let mut packet = [0u8; 28]; + packet[0..2].copy_from_slice(&0x1388u16.to_be_bytes()); + packet[2..4].copy_from_slice(&0x0050u16.to_be_bytes()); + packet[4..8].copy_from_slice(&0x1122_3344u32.to_be_bytes()); + packet[12] = raw_type; + packet[14..16].copy_from_slice(&8u16.to_be_bytes()); + packet[20..28].copy_from_slice(&SKIPPABLE_UNKNOWN_CHUNK_8); + seal(packet) +} + +fn init_family_packet(chunk_type: u8, verification_tag: u32, initiate_tag: u32) -> [u8; 32] { + let mut chunk = VALID_INIT_CHUNK; + chunk[0] = chunk_type; + chunk[4..8].copy_from_slice(&initiate_tag.to_be_bytes()); + chunk_packet(&chunk, verification_tag) +} + +#[test] +fn rejects_unknown_chunk_action_00() { + assert_eq!( + validate_sctp_packet(&unknown_type_packet(0x0F)), + Err(Error::InvalidArgument), + ); +} + +#[test] +fn rejects_unknown_chunk_action_01() { + assert_eq!( + validate_sctp_packet(&unknown_type_packet(0x4F)), + Err(Error::InvalidArgument), + ); +} + +#[test] +fn skips_unknown_chunk_action_10() { + assert!(validate_sctp_packet(&unknown_then_skippable_packet(0x8F)).is_ok()); +} + +#[test] +fn skips_unknown_chunk_action_11() { + assert!(validate_sctp_packet(&unknown_then_skippable_packet(0xCF)).is_ok()); +} + +#[test] +fn rejects_init_with_nonzero_verification_tag() { + let packet = init_family_packet(0x01, 1, 0xDEAD_BEEF); + assert_eq!(validate_sctp_packet(&packet), Err(Error::InvalidArgument)); +} + +#[test] +fn rejects_init_with_short_length() { + let mut packet = [0u8; 32]; + packet[0..2].copy_from_slice(&0x1388u16.to_be_bytes()); + packet[2..4].copy_from_slice(&0x0050u16.to_be_bytes()); + packet[12] = 0x01; + packet[14..16].copy_from_slice(&16u16.to_be_bytes()); + packet[16..20].copy_from_slice(&0xDEAD_BEEFu32.to_be_bytes()); + let packet = seal(packet); + assert_eq!(validate_sctp_packet(&packet), Err(Error::InvalidArgument)); +} + +#[test] +fn rejects_init_with_zero_initiate_tag() { + let packet = init_family_packet(0x01, 0, 0); + assert_eq!(validate_sctp_packet(&packet), Err(Error::InvalidArgument)); +} + +#[test] +fn rejects_init_with_zero_outbound_streams() { + let mut chunk = VALID_INIT_CHUNK; + chunk[12..14].copy_from_slice(&0u16.to_be_bytes()); + assert_eq!( + validate_sctp_packet(&chunk_packet::<32>(&chunk, 0)), + Err(Error::InvalidArgument), + ); +} + +#[test] +fn rejects_init_with_zero_inbound_streams() { + let mut chunk = VALID_INIT_CHUNK; + chunk[14..16].copy_from_slice(&0u16.to_be_bytes()); + assert_eq!( + validate_sctp_packet(&chunk_packet::<32>(&chunk, 0)), + Err(Error::InvalidArgument), + ); +} + +#[test] +fn rejects_init_with_arwnd_below_1500() { + let mut chunk = VALID_INIT_CHUNK; + chunk[8..12].copy_from_slice(&1499u32.to_be_bytes()); + chunk[16..20].copy_from_slice(&0u32.to_be_bytes()); + assert_eq!( + validate_sctp_packet(&chunk_packet::<32>(&chunk, 0)), + Err(Error::InvalidArgument), + ); +} + +#[test] +fn accepts_init_with_arwnd_1500_and_initial_tsn_zero() { + let mut chunk = VALID_INIT_CHUNK; + chunk[8..12].copy_from_slice(&1500u32.to_be_bytes()); + chunk[16..20].copy_from_slice(&0u32.to_be_bytes()); + assert!(validate_sctp_packet(&chunk_packet::<32>(&chunk, 0)).is_ok()); +} + +#[test] +fn rejects_init_ack_with_arwnd_below_1500() { + let mut chunk = VALID_INIT_ACK_CHUNK; + chunk[8..12].copy_from_slice(&1499u32.to_be_bytes()); + assert_eq!( + validate_sctp_packet(&chunk_packet::<40>(&chunk, 1)), + Err(Error::InvalidArgument), + ); +} + +#[test] +fn accepts_init_ack_with_arwnd_1500_and_nonzero_verification_tag() { + let mut chunk = VALID_INIT_ACK_CHUNK; + chunk[8..12].copy_from_slice(&1500u32.to_be_bytes()); + assert!(validate_sctp_packet(&chunk_packet::<40>(&chunk, 1)).is_ok()); +} + +#[test] +fn rejects_init_ack_with_zero_verification_tag() { + assert_eq!( + validate_sctp_packet(&chunk_packet::<40>(&VALID_INIT_ACK_CHUNK, 0)), + Err(Error::InvalidArgument), + ); +} + +#[test] +fn rejects_init_with_malformed_optional_parameter() { + let mut chunk = [0u8; 24]; + chunk[..20].copy_from_slice(&VALID_INIT_CHUNK); + chunk[2..4].copy_from_slice(&24u16.to_be_bytes()); + chunk[20..24].copy_from_slice(&[0, 5, 0, 3]); + assert_eq!( + validate_sctp_packet(&chunk_packet::<36>(&chunk, 0)), + Err(Error::InvalidArgument), + ); +} + +#[test] +fn rejects_bundled_init() { + let mut packet = [0u8; 40]; + packet[0..2].copy_from_slice(&0x1388u16.to_be_bytes()); + packet[2..4].copy_from_slice(&0x0050u16.to_be_bytes()); + packet[12..32].copy_from_slice(&VALID_INIT_CHUNK); + packet[32..40].copy_from_slice(&SKIPPABLE_UNKNOWN_CHUNK_8); + let packet = seal(packet); + assert_eq!(validate_sctp_packet(&packet), Err(Error::InvalidArgument)); +} + +#[test] +fn rejects_bundled_init_ack() { + let mut packet = [0u8; 48]; + packet[0..2].copy_from_slice(&0x1388u16.to_be_bytes()); + packet[2..4].copy_from_slice(&0x0050u16.to_be_bytes()); + packet[4..8].copy_from_slice(&0x1122_3344u32.to_be_bytes()); + packet[12..40].copy_from_slice(&VALID_INIT_ACK_CHUNK); + packet[40..48].copy_from_slice(&SKIPPABLE_UNKNOWN_CHUNK_8); + let packet = seal(packet); + assert_eq!(validate_sctp_packet(&packet), Err(Error::InvalidArgument)); +} + +#[test] +fn rejects_bundled_shutdown_complete() { + let mut packet = [0u8; 24]; + packet[0..2].copy_from_slice(&0x1388u16.to_be_bytes()); + packet[2..4].copy_from_slice(&0x0050u16.to_be_bytes()); + packet[4..8].copy_from_slice(&0x1122_3344u32.to_be_bytes()); + packet[12] = 0x0E; + packet[14..16].copy_from_slice(&4u16.to_be_bytes()); + packet[16..24].copy_from_slice(&SKIPPABLE_UNKNOWN_CHUNK_8); + let packet = seal(packet); + assert_eq!(validate_sctp_packet(&packet), Err(Error::InvalidArgument)); +} + +#[test] +fn accepts_lone_init_ack() { + assert!(validate_sctp_packet(&chunk_packet::<40>(&VALID_INIT_ACK_CHUNK, 0x1122_3344)).is_ok()); +} diff --git a/crates/kernel/src/sctp_packet_parameter_tests.rs b/crates/kernel/src/sctp_packet_parameter_tests.rs new file mode 100644 index 0000000..443f4ea --- /dev/null +++ b/crates/kernel/src/sctp_packet_parameter_tests.rs @@ -0,0 +1,200 @@ +// Copyright 2026 ONCRIX Contributors +// SPDX-License-Identifier: Apache-2.0 + +//! RFC 9260 INIT-family parameter admission tests. + +use super::test_support::{VALID_INIT_ACK_CHUNK, VALID_INIT_CHUNK}; +use super::{sctp_packet_crc32c, validate_sctp_packet}; +use oncrix_lib::{Error, Result}; + +const COOKIE: [u8; 8] = [0, 7, 0, 5, 0xAA, 0, 0, 0]; +const IPV4: [u8; 8] = [0, 5, 0, 8, 192, 0, 2, 1]; +const IPV6: [u8; 20] = [0, 6, 0, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]; +const ECN: [u8; 4] = [0x80, 0, 0, 4]; + +fn validate_parameter_packet(base: &[u8], parameters: &[u8], tag: u32) -> Result<()> { + let chunk_len = 20 + parameters.len(); + let packet_len = 12 + ((chunk_len + 3) & !3); + let mut packet = [0u8; 128]; + packet[0..2].copy_from_slice(&0x1388u16.to_be_bytes()); + packet[2..4].copy_from_slice(&0x0050u16.to_be_bytes()); + packet[4..8].copy_from_slice(&tag.to_be_bytes()); + packet[12..32].copy_from_slice(&base[..20]); + let wire_chunk_len = u16::try_from(chunk_len).expect("test chunk length fits u16"); + packet[14..16].copy_from_slice(&wire_chunk_len.to_be_bytes()); + packet[32..32 + parameters.len()].copy_from_slice(parameters); + let crc = sctp_packet_crc32c(&packet[..packet_len]); + packet[8..12].copy_from_slice(&crc.to_le_bytes()); + validate_sctp_packet(&packet[..packet_len]) +} + +fn validate_init(parameters: &[u8]) -> Result<()> { + validate_parameter_packet(&VALID_INIT_CHUNK, parameters, 0) +} + +fn validate_init_ack(extra: &[u8]) -> Result<()> { + let mut parameters = [0u8; 96]; + parameters[..COOKIE.len()].copy_from_slice(&COOKIE); + parameters[COOKIE.len()..COOKIE.len() + extra.len()].copy_from_slice(extra); + validate_parameter_packet( + &VALID_INIT_ACK_CHUNK, + ¶meters[..COOKIE.len() + extra.len()], + 1, + ) +} + +#[test] +fn init_and_init_ack_accept_valid_repeatable_addresses() { + let mut addresses = [0u8; 56]; + addresses[..8].copy_from_slice(&IPV4); + addresses[8..16].copy_from_slice(&IPV4); + addresses[16..36].copy_from_slice(&IPV6); + addresses[36..56].copy_from_slice(&IPV6); + assert!(validate_init(&addresses).is_ok()); + assert!(validate_init_ack(&addresses).is_ok()); +} + +#[test] +fn init_and_init_ack_reject_malformed_address_widths() { + for parameter in [ + &[0, 5, 0, 7, 192, 0, 2][..], + &[0, 6, 0, 19, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0][..], + ] { + assert_eq!(validate_init(parameter), Err(Error::InvalidArgument)); + assert_eq!(validate_init_ack(parameter), Err(Error::InvalidArgument)); + } +} + +#[test] +fn init_validates_cookie_preservative_width_and_uniqueness() { + let valid = [0, 9, 0, 8, 0, 0, 0, 1]; + assert!(validate_init(&valid).is_ok()); + assert_eq!( + validate_init(&[0, 9, 0, 7, 0, 0, 1]), + Err(Error::InvalidArgument), + ); + let duplicate = [0, 9, 0, 8, 0, 0, 0, 1, 0, 9, 0, 8, 0, 0, 0, 2]; + assert_eq!(validate_init(&duplicate), Err(Error::InvalidArgument)); +} + +#[test] +fn init_validates_supported_address_types_shape_and_uniqueness() { + assert!(validate_init(&[0, 12, 0, 6, 0, 5]).is_ok()); + for malformed in [&[0, 12, 0, 5, 0][..], &[0, 12, 0, 7, 0, 5, 0][..]] { + assert_eq!(validate_init(malformed), Err(Error::InvalidArgument)); + } + let duplicate = [0, 12, 0, 6, 0, 5, 0, 0, 0, 12, 0, 6, 0, 6]; + assert_eq!(validate_init(&duplicate), Err(Error::InvalidArgument)); +} + +#[test] +fn init_and_init_ack_validate_ecn_width_and_uniqueness() { + assert!(validate_init(&ECN).is_ok()); + assert!(validate_init_ack(&ECN).is_ok()); + let malformed = [0x80, 0, 0, 5, 0]; + assert_eq!(validate_init(&malformed), Err(Error::InvalidArgument)); + assert_eq!(validate_init_ack(&malformed), Err(Error::InvalidArgument)); + let duplicate = [0x80, 0, 0, 4, 0x80, 0, 0, 4]; + assert_eq!(validate_init(&duplicate), Err(Error::InvalidArgument)); + assert_eq!(validate_init_ack(&duplicate), Err(Error::InvalidArgument)); +} + +#[test] +fn init_family_rejects_host_name() { + let host_name = [0, 11, 0, 5, b'x']; + assert_eq!(validate_init(&host_name), Err(Error::InvalidArgument)); + assert_eq!(validate_init_ack(&host_name), Err(Error::InvalidArgument)); +} + +#[test] +fn init_rejects_cookie_and_unrecognized_parameter() { + assert_eq!(validate_init(&COOKIE), Err(Error::InvalidArgument)); + assert_eq!(validate_init(&[0, 8, 0, 4]), Err(Error::InvalidArgument)); +} + +#[test] +fn init_ack_rejects_cookie_preservative_and_supported_address_types() { + assert_eq!( + validate_init_ack(&[0, 9, 0, 8, 0, 0, 0, 1]), + Err(Error::InvalidArgument), + ); + assert_eq!( + validate_init_ack(&[0, 12, 0, 6, 0, 5]), + Err(Error::InvalidArgument), + ); +} + +#[test] +fn init_ack_accepts_repeatable_unrecognized_parameter() { + let containers = [0, 8, 0, 8, 0x12, 0x34, 0, 4, 0, 8, 0, 8, 0x92, 0x34, 0, 4]; + assert!(validate_init_ack(&containers).is_ok()); +} + +#[test] +fn init_ack_rejects_empty_or_truncated_unrecognized_parameter() { + for parameter in [ + &[0, 8, 0, 4][..], + &[0, 8, 0, 5, 0][..], + &[0, 8, 0, 6, 0, 5][..], + &[0, 8, 0, 7, 0, 5, 0][..], + ] { + assert_eq!(validate_init_ack(parameter), Err(Error::InvalidArgument)); + } +} + +#[test] +fn init_ack_rejects_malformed_nested_unrecognized_parameter() { + for parameter in [ + &[0, 8, 0, 8, 0, 5, 0, 8][..], + &[0, 8, 0, 10, 0, 5, 0, 5, 0xAA, 0][..], + &[0, 8, 0, 9, 0, 5, 0, 4, 0][..], + &[0, 8, 0, 12, 0x12, 0x34, 0, 5, 0xAA, 0, 0, 0][..], + &[0, 8, 0, 12, 0, 5, 0, 4, 0, 6, 0, 4][..], + ] { + assert_eq!(validate_init_ack(parameter), Err(Error::InvalidArgument)); + } +} + +#[test] +fn init_family_stops_or_continues_for_unknown_parameter_actions() { + for parameter_type in [0x0010u16, 0x4010] { + let raw = parameter_type.to_be_bytes(); + let parameter = [raw[0], raw[1], 0, 4]; + assert!(validate_init(¶meter).is_ok()); + assert!(validate_init_ack(¶meter).is_ok()); + } + for parameter_type in [0x8010u16, 0xC010] { + let raw = parameter_type.to_be_bytes(); + let parameter = [raw[0], raw[1], 0, 4]; + assert!(validate_init(¶meter).is_ok()); + assert!(validate_init_ack(¶meter).is_ok()); + } +} + +#[test] +fn unknown_stop_ignores_trailing_malformed_parameter() { + for parameter_type in [0x0010u16, 0x4010] { + let raw = parameter_type.to_be_bytes(); + let parameters = [raw[0], raw[1], 0, 4, 0, 5, 0, 3]; + assert!(validate_init(¶meters).is_ok()); + assert!(validate_init_ack(¶meters).is_ok()); + } +} + +#[test] +fn unknown_continue_checks_trailing_malformed_parameter() { + for parameter_type in [0x8010u16, 0xC010] { + let raw = parameter_type.to_be_bytes(); + let parameters = [raw[0], raw[1], 0, 4, 0, 5, 0, 3]; + assert_eq!(validate_init(¶meters), Err(Error::InvalidArgument)); + assert_eq!(validate_init_ack(¶meters), Err(Error::InvalidArgument)); + } +} + +#[test] +fn init_ack_stop_requires_preceding_state_cookie() { + assert_eq!( + validate_parameter_packet(&VALID_INIT_ACK_CHUNK, &[0, 0x10, 0, 4], 1), + Err(Error::InvalidArgument), + ); +} diff --git a/crates/kernel/src/sctp_packet_params.rs b/crates/kernel/src/sctp_packet_params.rs new file mode 100644 index 0000000..74ecd95 --- /dev/null +++ b/crates/kernel/src/sctp_packet_params.rs @@ -0,0 +1,174 @@ +// Copyright 2026 ONCRIX Contributors +// SPDX-License-Identifier: Apache-2.0 + +//! Checked RFC 9260 TLV walking and INIT-family parameter policy. + +use oncrix_lib::{Error, Result}; + +const TLV_HEADER_LEN: usize = 4; +const INIT_FIXED_LEN: usize = 20; +const IPV4_ADDRESS: u16 = 5; +const IPV6_ADDRESS: u16 = 6; +const STATE_COOKIE: u16 = 7; +const UNRECOGNIZED_PARAMETER: u16 = 8; +const COOKIE_PRESERVATIVE: u16 = 9; +const HOST_NAME_ADDRESS: u16 = 11; +const SUPPORTED_ADDRESS_TYPES: u16 = 12; +const ECN_CAPABLE: u16 = 0x8000; +const SEEN_COOKIE: u8 = 1 << 0; +const SEEN_COOKIE_PRESERVATIVE: u8 = 1 << 1; +const SEEN_SUPPORTED_ADDRESS_TYPES: u8 = 1 << 2; +const SEEN_ECN: u8 = 1 << 3; + +pub(super) enum WalkControl { + Continue, + Stop, +} + +const fn pad4(len: usize) -> Option { + match len.checked_add(3) { + Some(rounded) => Some(rounded & !3), + None => None, + } +} + +fn tlv_header(input: &[u8]) -> Result<(u16, usize)> { + if input.len() < TLV_HEADER_LEN { + return Err(Error::InvalidArgument); + } + let parameter_type = u16::from_be_bytes([input[0], input[1]]); + let len = usize::from(u16::from_be_bytes([input[2], input[3]])); + if len < TLV_HEADER_LEN { + return Err(Error::InvalidArgument); + } + Ok((parameter_type, len)) +} + +pub(super) fn walk_tlvs(body: &[u8], mut validate_tlv: F) -> Result +where + F: FnMut(u16, &[u8]) -> Result, +{ + let mut offset = 0usize; + let mut count = 0usize; + while offset < body.len() { + let (parameter_type, tlv_len) = tlv_header(&body[offset..])?; + let end = offset.checked_add(tlv_len).ok_or(Error::InvalidArgument)?; + if end > body.len() { + return Err(Error::InvalidArgument); + } + count = count.checked_add(1).ok_or(Error::InvalidArgument)?; + if matches!( + validate_tlv(parameter_type, &body[offset..end])?, + WalkControl::Stop + ) { + return Ok(count); + } + if end == body.len() { + break; + } + let padded_len = pad4(tlv_len).ok_or(Error::InvalidArgument)?; + offset = offset + .checked_add(padded_len) + .ok_or(Error::InvalidArgument)?; + if offset > body.len() { + return Err(Error::InvalidArgument); + } + } + Ok(count) +} + +fn mark_once(seen: &mut u8, bit: u8) -> Result { + if *seen & bit != 0 { + return Err(Error::InvalidArgument); + } + *seen |= bit; + Ok(WalkControl::Continue) +} + +const fn unknown_control(parameter_type: u16) -> WalkControl { + if parameter_type & 0x8000 == 0 { + WalkControl::Stop + } else { + WalkControl::Continue + } +} + +fn validate_init_parameter(parameter_type: u16, tlv: &[u8], seen: &mut u8) -> Result { + let len = tlv.len(); + match parameter_type { + IPV4_ADDRESS if len == 8 => Ok(WalkControl::Continue), + IPV6_ADDRESS if len == 20 => Ok(WalkControl::Continue), + COOKIE_PRESERVATIVE if len == 8 => mark_once(seen, SEEN_COOKIE_PRESERVATIVE), + SUPPORTED_ADDRESS_TYPES if len >= 6 && len & 1 == 0 => { + mark_once(seen, SEEN_SUPPORTED_ADDRESS_TYPES) + } + ECN_CAPABLE if len == 4 => mark_once(seen, SEEN_ECN), + IPV4_ADDRESS + | IPV6_ADDRESS + | COOKIE_PRESERVATIVE + | SUPPORTED_ADDRESS_TYPES + | ECN_CAPABLE + | HOST_NAME_ADDRESS + | STATE_COOKIE + | UNRECOGNIZED_PARAMETER => Err(Error::InvalidArgument), + _ => Ok(unknown_control(parameter_type)), + } +} + +fn validate_single_nested_parameter(tlv: &[u8]) -> Result { + if tlv.len() < 8 { + return Err(Error::InvalidArgument); + } + let value = &tlv[TLV_HEADER_LEN..]; + let (_, nested_len) = tlv_header(value)?; + if nested_len != value.len() { + return Err(Error::InvalidArgument); + } + let count = walk_tlvs(value, |_, _| Ok(WalkControl::Continue))?; + if count != 1 { + return Err(Error::InvalidArgument); + } + Ok(WalkControl::Continue) +} + +fn validate_init_ack_parameter( + parameter_type: u16, + tlv: &[u8], + seen: &mut u8, +) -> Result { + let len = tlv.len(); + match parameter_type { + STATE_COOKIE if len >= 5 => mark_once(seen, SEEN_COOKIE), + IPV4_ADDRESS if len == 8 => Ok(WalkControl::Continue), + IPV6_ADDRESS if len == 20 => Ok(WalkControl::Continue), + UNRECOGNIZED_PARAMETER => validate_single_nested_parameter(tlv), + ECN_CAPABLE if len == 4 => mark_once(seen, SEEN_ECN), + STATE_COOKIE + | IPV4_ADDRESS + | IPV6_ADDRESS + | ECN_CAPABLE + | HOST_NAME_ADDRESS + | COOKIE_PRESERVATIVE + | SUPPORTED_ADDRESS_TYPES => Err(Error::InvalidArgument), + _ => Ok(unknown_control(parameter_type)), + } +} + +pub(super) fn validate_init_parameters(chunk: &[u8]) -> Result<()> { + let mut seen = 0u8; + walk_tlvs(&chunk[INIT_FIXED_LEN..], |parameter_type, tlv| { + validate_init_parameter(parameter_type, tlv, &mut seen) + })?; + Ok(()) +} + +pub(super) fn validate_init_ack_parameters(chunk: &[u8]) -> Result<()> { + let mut seen = 0u8; + walk_tlvs(&chunk[INIT_FIXED_LEN..], |parameter_type, tlv| { + validate_init_ack_parameter(parameter_type, tlv, &mut seen) + })?; + if seen & SEEN_COOKIE == 0 { + return Err(Error::InvalidArgument); + } + Ok(()) +} diff --git a/crates/kernel/src/sctp_packet_shape.rs b/crates/kernel/src/sctp_packet_shape.rs new file mode 100644 index 0000000..7a80053 --- /dev/null +++ b/crates/kernel/src/sctp_packet_shape.rs @@ -0,0 +1,118 @@ +// Copyright 2026 ONCRIX Contributors +// SPDX-License-Identifier: Apache-2.0 + +//! Allocation-free RFC 9260 known-chunk shape validation. + +use crate::sctp::SctpChunkType; +use oncrix_lib::{Error, Result}; + +use super::params::{ + WalkControl, validate_init_ack_parameters, validate_init_parameters, walk_tlvs, +}; + +const CHUNK_HEADER_LEN: usize = 4; +const INIT_FIXED_LEN: usize = 20; +const HEARTBEAT_INFO: u16 = 1; + +fn validate_init_fields(chunk: &[u8]) -> Result<()> { + let initiate_tag = u32::from_be_bytes([chunk[4], chunk[5], chunk[6], chunk[7]]); + let outbound_streams = u16::from_be_bytes([chunk[12], chunk[13]]); + let inbound_streams = u16::from_be_bytes([chunk[14], chunk[15]]); + if initiate_tag == 0 || outbound_streams == 0 || inbound_streams == 0 { + return Err(Error::InvalidArgument); + } + Ok(()) +} + +fn validate_sack(chunk: &[u8]) -> Result<()> { + if chunk.len() < 16 { + return Err(Error::InvalidArgument); + } + let gap_count = usize::from(u16::from_be_bytes([chunk[12], chunk[13]])); + let duplicate_count = usize::from(u16::from_be_bytes([chunk[14], chunk[15]])); + let variable_count = gap_count + .checked_add(duplicate_count) + .ok_or(Error::InvalidArgument)?; + let variable_len = variable_count + .checked_mul(4) + .ok_or(Error::InvalidArgument)?; + let expected_len = 16usize + .checked_add(variable_len) + .ok_or(Error::InvalidArgument)?; + if chunk.len() != expected_len { + return Err(Error::InvalidArgument); + } + Ok(()) +} + +fn validate_heartbeat(chunk: &[u8]) -> Result<()> { + if chunk.len() < 8 { + return Err(Error::InvalidArgument); + } + let count = walk_tlvs(&chunk[CHUNK_HEADER_LEN..], |parameter_type, _| { + if parameter_type != HEARTBEAT_INFO { + return Err(Error::InvalidArgument); + } + Ok(WalkControl::Continue) + })?; + if count != 1 { + return Err(Error::InvalidArgument); + } + Ok(()) +} + +pub(super) fn validate( + chunk_type: SctpChunkType, + chunk: &[u8], + verification_tag: u32, +) -> Result<()> { + match chunk_type { + SctpChunkType::Data if chunk.len() >= 17 => Ok(()), + SctpChunkType::Data => Err(Error::InvalidArgument), + SctpChunkType::Init if chunk.len() >= INIT_FIXED_LEN && verification_tag == 0 => { + validate_init_fields(chunk)?; + let advertised_receiver_window = + u32::from_be_bytes([chunk[8], chunk[9], chunk[10], chunk[11]]); + if advertised_receiver_window < 1500 { + return Err(Error::InvalidArgument); + } + validate_init_parameters(chunk)?; + Ok(()) + } + SctpChunkType::Init => Err(Error::InvalidArgument), + SctpChunkType::InitAck if chunk.len() >= 24 && verification_tag != 0 => { + validate_init_fields(chunk)?; + let advertised_receiver_window = + u32::from_be_bytes([chunk[8], chunk[9], chunk[10], chunk[11]]); + if advertised_receiver_window < 1500 { + return Err(Error::InvalidArgument); + } + validate_init_ack_parameters(chunk)?; + Ok(()) + } + SctpChunkType::InitAck => Err(Error::InvalidArgument), + SctpChunkType::Sack => validate_sack(chunk), + SctpChunkType::Heartbeat | SctpChunkType::HeartbeatAck => validate_heartbeat(chunk), + SctpChunkType::Abort => { + walk_tlvs(&chunk[CHUNK_HEADER_LEN..], |_, _| Ok(WalkControl::Continue))?; + Ok(()) + } + SctpChunkType::Error => { + let count = walk_tlvs(&chunk[CHUNK_HEADER_LEN..], |_, _| Ok(WalkControl::Continue))?; + if count == 0 { + return Err(Error::InvalidArgument); + } + Ok(()) + } + SctpChunkType::Shutdown if chunk.len() == 8 => Ok(()), + SctpChunkType::Shutdown => Err(Error::InvalidArgument), + SctpChunkType::ShutdownAck if chunk.len() == 4 => Ok(()), + SctpChunkType::ShutdownAck => Err(Error::InvalidArgument), + SctpChunkType::CookieEcho if chunk.len() >= 5 => Ok(()), + SctpChunkType::CookieEcho => Err(Error::InvalidArgument), + SctpChunkType::CookieAck if chunk.len() == 4 => Ok(()), + SctpChunkType::CookieAck => Err(Error::InvalidArgument), + SctpChunkType::ShutdownComplete if chunk.len() == 4 => Ok(()), + SctpChunkType::ShutdownComplete => Err(Error::InvalidArgument), + } +} diff --git a/crates/kernel/src/sctp_packet_shape_tests.rs b/crates/kernel/src/sctp_packet_shape_tests.rs new file mode 100644 index 0000000..469c14e --- /dev/null +++ b/crates/kernel/src/sctp_packet_shape_tests.rs @@ -0,0 +1,155 @@ +// Copyright 2026 ONCRIX Contributors +// SPDX-License-Identifier: Apache-2.0 + +//! RFC 9260 known-chunk shape validation tests. + +use super::test_support::{VALID_DATA_CHUNK, VALID_INIT_ACK_CHUNK, chunk_packet}; +use super::validate_sctp_packet; +use oncrix_lib::Error; + +fn rejects(chunk: &[u8], tag: u32) { + assert_eq!( + validate_sctp_packet(&chunk_packet::(chunk, tag)), + Err(Error::InvalidArgument), + ); +} + +#[test] +fn data_requires_fixed_fields_and_user_data() { + let mut empty_data = [0u8; 16]; + empty_data.copy_from_slice(&VALID_DATA_CHUNK[..16]); + empty_data[2..4].copy_from_slice(&16u16.to_be_bytes()); + rejects::<28>(&empty_data, 1); + assert!(validate_sctp_packet(&chunk_packet::<32>(&VALID_DATA_CHUNK, 1)).is_ok()); +} + +#[test] +fn fixed_length_chunks_require_exact_lengths() { + rejects::<20>(&[7, 0, 0, 5, 0, 0, 0, 0], 1); + assert!(validate_sctp_packet(&chunk_packet::<20>(&[7, 0, 0, 8, 0, 0, 0, 0], 1)).is_ok()); + for chunk_type in [8u8, 11, 14] { + rejects::<20>(&[chunk_type, 0, 0, 5, 0, 0, 0, 0], 1); + assert!(validate_sctp_packet(&chunk_packet::<16>(&[chunk_type, 0, 0, 4], 1)).is_ok()); + } +} + +#[test] +fn cookie_echo_requires_nonempty_cookie() { + rejects::<16>(&[10, 0, 0, 4], 1); + assert!(validate_sctp_packet(&chunk_packet::<20>(&[10, 0, 0, 5, 0xAA], 1)).is_ok()); +} + +#[test] +fn sack_length_matches_gap_and_duplicate_counts() { + let mut sack = [0u8; 24]; + sack[0] = 3; + sack[2..4].copy_from_slice(&16u16.to_be_bytes()); + assert!(validate_sctp_packet(&chunk_packet::<28>(&sack[..16], 1)).is_ok()); + sack[12..14].copy_from_slice(&1u16.to_be_bytes()); + rejects::<28>(&sack[..16], 1); + sack[2..4].copy_from_slice(&20u16.to_be_bytes()); + assert!(validate_sctp_packet(&chunk_packet::<32>(&sack[..20], 1)).is_ok()); + sack[14..16].copy_from_slice(&1u16.to_be_bytes()); + rejects::<32>(&sack[..20], 1); + sack[2..4].copy_from_slice(&24u16.to_be_bytes()); + assert!(validate_sctp_packet(&chunk_packet::<36>(&sack, 1)).is_ok()); +} + +#[test] +fn heartbeat_requires_one_heartbeat_info_parameter() { + for chunk_type in [4u8, 5] { + rejects::<16>(&[chunk_type, 0, 0, 4], 1); + rejects::<20>(&[chunk_type, 0, 0, 8, 0, 2, 0, 4], 1); + assert!( + validate_sctp_packet(&chunk_packet::<20>(&[chunk_type, 0, 0, 8, 0, 1, 0, 4], 1,)) + .is_ok() + ); + rejects::<24>(&[chunk_type, 0, 0, 12, 0, 1, 0, 4, 0, 1, 0, 4], 1); + } +} + +#[test] +fn abort_accepts_empty_or_complete_causes() { + assert!(validate_sctp_packet(&chunk_packet::<16>(&[6, 0, 0, 4], 1)).is_ok()); + assert!(validate_sctp_packet(&chunk_packet::<20>(&[6, 0, 0, 8, 0, 1, 0, 4], 1)).is_ok()); + rejects::<20>(&[6, 0, 0, 8, 0, 1, 0, 3], 1); +} + +#[test] +fn error_requires_at_least_one_complete_cause() { + rejects::<16>(&[9, 0, 0, 4], 1); + assert!(validate_sctp_packet(&chunk_packet::<20>(&[9, 0, 0, 8, 0, 1, 0, 4], 1)).is_ok()); + rejects::<20>(&[9, 0, 0, 8, 0, 1, 0, 3], 1); +} + +#[test] +fn init_ack_requires_exactly_one_state_cookie() { + let mut missing = VALID_INIT_ACK_CHUNK; + missing[2..4].copy_from_slice(&20u16.to_be_bytes()); + rejects::<32>(&missing[..20], 1); + + let mut wrong_type = VALID_INIT_ACK_CHUNK; + wrong_type[20..22].copy_from_slice(&16u16.to_be_bytes()); + rejects::<40>(&wrong_type, 1); + + let mut duplicate = [0u8; 36]; + duplicate[..28].copy_from_slice(&VALID_INIT_ACK_CHUNK); + duplicate[2..4].copy_from_slice(&36u16.to_be_bytes()); + duplicate[28..36].copy_from_slice(&[0, 7, 0, 8, 1, 2, 3, 4]); + rejects::<48>(&duplicate, 1); +} + +#[test] +fn init_ack_rejects_zero_outbound_streams() { + let mut chunk = VALID_INIT_ACK_CHUNK; + chunk[12..14].copy_from_slice(&0u16.to_be_bytes()); + rejects::<40>(&chunk, 1); +} + +#[test] +fn init_ack_rejects_zero_inbound_streams() { + let mut chunk = VALID_INIT_ACK_CHUNK; + chunk[14..16].copy_from_slice(&0u16.to_be_bytes()); + rejects::<40>(&chunk, 1); +} + +#[test] +fn init_ack_rejects_malformed_state_cookie() { + let mut malformed = VALID_INIT_ACK_CHUNK; + malformed[22..24].copy_from_slice(&3u16.to_be_bytes()); + rejects::<40>(&malformed, 1); +} + +#[test] +fn init_ack_rejects_empty_state_cookie_value() { + let mut empty = VALID_INIT_ACK_CHUNK; + empty[2..4].copy_from_slice(&24u16.to_be_bytes()); + empty[22..24].copy_from_slice(&4u16.to_be_bytes()); + rejects::<36>(&empty[..24], 1); +} + +#[test] +fn tlv_walker_rejects_short_overrun_partial_padding_and_trailing_bytes() { + let cases: [&[u8]; 4] = [ + &[6, 0, 0, 8, 0, 1, 0, 3], + &[6, 0, 0, 8, 0, 1, 0, 9], + &[6, 0, 0, 10, 0, 1, 0, 5, 0xAA, 0], + &[6, 0, 0, 9, 0, 1, 0, 4, 0xAA], + ]; + rejects::<20>(cases[0], 1); + rejects::<20>(cases[1], 1); + rejects::<24>(cases[2], 1); + rejects::<24>(cases[3], 1); +} + +#[test] +fn tlv_walker_accepts_exact_end_or_full_final_padding() { + assert!(validate_sctp_packet(&chunk_packet::<24>(&[6, 0, 0, 9, 0, 1, 0, 5, 0xAA], 1)).is_ok()); + assert!( + validate_sctp_packet(&chunk_packet::<24>( + &[6, 0, 0, 12, 0, 1, 0, 5, 0xAA, 0, 0, 0], + 1, + )) + .is_ok() + ); +} diff --git a/crates/kernel/src/sctp_packet_test_support.rs b/crates/kernel/src/sctp_packet_test_support.rs new file mode 100644 index 0000000..f557767 --- /dev/null +++ b/crates/kernel/src/sctp_packet_test_support.rs @@ -0,0 +1,46 @@ +// Copyright 2026 ONCRIX Contributors +// SPDX-License-Identifier: Apache-2.0 + +//! Shared fixtures for SCTP packet validation tests. + +use super::sctp_packet_crc32c; + +/// A 32-byte SCTP INIT packet with a little-endian CRC32c field. +pub(super) const SCTP_INIT_PACKET: [u8; 32] = [ + 0x13, 0x88, 0x00, 0x50, 0x00, 0x00, 0x00, 0x00, 0x12, 0xF6, 0xF6, 0xE9, 0x01, 0x00, 0x00, 0x14, + 0xDE, 0xAD, 0xBE, 0xEF, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x02, 0x71, 0x5B, 0x5A, 0x94, +]; + +pub(super) fn seal(mut packet: [u8; N]) -> [u8; N] { + let crc = sctp_packet_crc32c(&packet); + packet[8..12].copy_from_slice(&crc.to_le_bytes()); + packet +} + +pub(super) fn chunk_packet(chunk: &[u8], verification_tag: u32) -> [u8; N] { + assert!(chunk.len() <= N - 12, "chunk must fit packet"); + let mut packet = [0u8; N]; + packet[0..2].copy_from_slice(&0x1388u16.to_be_bytes()); + packet[2..4].copy_from_slice(&0x0050u16.to_be_bytes()); + packet[4..8].copy_from_slice(&verification_tag.to_be_bytes()); + packet[12..12 + chunk.len()].copy_from_slice(chunk); + seal(packet) +} + +pub(super) const SKIPPABLE_UNKNOWN_CHUNK_8: [u8; 8] = + [0x80, 0x00, 0x00, 0x08, 0xAA, 0xBB, 0xCC, 0xDD]; + +pub(super) const VALID_DATA_CHUNK: [u8; 20] = [ + 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, + 0xAA, 0x00, 0x00, 0x00, +]; + +pub(super) const VALID_INIT_CHUNK: [u8; 20] = [ + 0x01, 0x00, 0x00, 0x14, 0xDE, 0xAD, 0xBE, 0xEF, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x02, + 0x71, 0x5B, 0x5A, 0x94, +]; + +pub(super) const VALID_INIT_ACK_CHUNK: [u8; 28] = [ + 0x02, 0x00, 0x00, 0x1C, 0xDE, 0xAD, 0xBE, 0xEF, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x02, + 0x71, 0x5B, 0x5A, 0x94, 0x00, 0x07, 0x00, 0x08, 0xCA, 0xFE, 0xBA, 0xBE, +];