mirror of
https://github.com/bleackstorm/SteamFrame-FPV-Control-Center.git
synced 2026-12-03 07:31:57 +01:00
fix(safety): harden dual-core atomics, UDP peer filtering, TOCTOU, and CRC-8 UART frames
This commit is contained in:
1 parent
426ab7c7d6
commit
c0bad51d3b
9 files changed
+232
-72
No files matched your search
+44
-2
@@ -155,11 +155,16 @@ impl G29BridgeReceiver {
|
||||
port: u16,
|
||||
control: EngineControlHandle,
|
||||
telemetry: Option<Arc<Mutex<TelemetryState>>>,
|
||||
allowed_peers: Vec<std::net::IpAddr>,
|
||||
) -> Result<Self> {
|
||||
let addr = format!("{}:{}", bind_ip, port);
|
||||
let socket = UdpSocket::bind(&addr)?;
|
||||
socket.set_read_timeout(Some(Duration::from_millis(100)))?;
|
||||
log::info!("[G29 Bridge] En écoute UDP unifiée sur {}", addr);
|
||||
log::info!(
|
||||
"[G29 Bridge] En écoute UDP unifiée sur {} (pairs autorisés: {:?})",
|
||||
addr,
|
||||
allowed_peers
|
||||
);
|
||||
|
||||
let state = Arc::new(Mutex::new(G29State::default()));
|
||||
let state_clone = Arc::clone(&state);
|
||||
@@ -170,8 +175,19 @@ impl G29BridgeReceiver {
|
||||
.name("g29-udp-rx".into())
|
||||
.spawn(move || {
|
||||
let mut buf = [0u8; 512];
|
||||
let mut last_reject_log = Instant::now() - Duration::from_secs(10);
|
||||
while !stop_clone.load(Ordering::Acquire) {
|
||||
if let Ok((len, _src)) = socket.recv_from(&mut buf) {
|
||||
if let Ok((len, src)) = socket.recv_from(&mut buf) {
|
||||
if !Self::is_peer_allowed(src.ip(), &allowed_peers) {
|
||||
if last_reject_log.elapsed() >= Duration::from_secs(5) {
|
||||
log::warn!(
|
||||
"[G29 Bridge] Paquet UDP rejeté depuis IP non autorisée : {}",
|
||||
src
|
||||
);
|
||||
last_reject_log = Instant::now();
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if let Ok(text) = std::str::from_utf8(&buf[..len]) {
|
||||
for line in text.lines() {
|
||||
Self::process_line(
|
||||
@@ -213,6 +229,10 @@ impl G29BridgeReceiver {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn is_peer_allowed(src_ip: std::net::IpAddr, allowed_peers: &[std::net::IpAddr]) -> bool {
|
||||
src_ip.is_loopback() || allowed_peers.is_empty() || allowed_peers.contains(&src_ip)
|
||||
}
|
||||
|
||||
pub fn state(&self) -> Arc<Mutex<G29State>> {
|
||||
Arc::clone(&self.state)
|
||||
}
|
||||
@@ -553,4 +573,26 @@ mod tests {
|
||||
assert!((ts.battery_current_a - 12.5).abs() < 0.01);
|
||||
assert_eq!(ts.battery_remaining_pct, 75);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_udp_peer_allowlist() {
|
||||
use std::net::{IpAddr, Ipv4Addr};
|
||||
let allowed = vec![IpAddr::V4(Ipv4Addr::new(192, 168, 1, 1))];
|
||||
assert!(G29BridgeReceiver::is_peer_allowed(
|
||||
IpAddr::V4(Ipv4Addr::new(192, 168, 1, 1)),
|
||||
&allowed
|
||||
));
|
||||
assert!(G29BridgeReceiver::is_peer_allowed(
|
||||
IpAddr::V4(Ipv4Addr::LOCALHOST),
|
||||
&allowed
|
||||
));
|
||||
assert!(!G29BridgeReceiver::is_peer_allowed(
|
||||
IpAddr::V4(Ipv4Addr::new(10, 0, 0, 99)),
|
||||
&allowed
|
||||
));
|
||||
assert!(G29BridgeReceiver::is_peer_allowed(
|
||||
IpAddr::V4(Ipv4Addr::new(10, 0, 0, 99)),
|
||||
&[]
|
||||
));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user