Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional fixes for array access panics in esp-ieee802154 #1923

Merged
merged 1 commit into from
Aug 12, 2024
Merged
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
1 change: 1 addition & 0 deletions esp-ieee802154/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- Added additional checks to prevent various array access panics while processing frames
- Added range check to avoid panic when indexing into RX_BUFFER slice

### Changed
Expand Down
6 changes: 6 additions & 0 deletions esp-ieee802154/src/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,15 @@ pub struct ReceivedFrame {
}

pub(crate) fn frame_is_ack_required(frame: &[u8]) -> bool {
if frame.len() <= FRAME_AR_OFFSET {
return false;
}
(frame[FRAME_AR_OFFSET] & FRAME_AR_BIT) != 0
}

pub(crate) fn frame_get_version(frame: &[u8]) -> u8 {
if frame.len() <= FRAME_VERSION_OFFSET {
return 0;
}
frame[FRAME_VERSION_OFFSET] & FRAME_VERSION_MASK
}
15 changes: 12 additions & 3 deletions esp-ieee802154/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,21 @@ impl<'a> Ieee802154<'a> {
/// Get a received frame, if available
pub fn get_received(&mut self) -> Option<Result<ReceivedFrame, Error>> {
if let Some(raw) = ieee802154_poll() {
let maybe_decoded =
mac::Frame::try_read(&raw.data[1..][..raw.data[0] as usize], FooterMode::Explicit);
let maybe_decoded = if raw.data[0] as usize > raw.data.len() {
// try to decode up to data.len()
mac::Frame::try_read(&raw.data[1..][..raw.data.len()], FooterMode::Explicit)
} else {
mac::Frame::try_read(&raw.data[1..][..raw.data[0] as usize], FooterMode::Explicit)
};

let result = match maybe_decoded {
Ok((decoded, _)) => {
let rssi = raw.data[raw.data[0] as usize - 1] as i8; // crc is not written to rx buffer
// crc is not written to rx buffer
let rssi = if raw.data[0] as usize > raw.data.len() {
raw.data[raw.data.len() - 1] as i8
} else {
raw.data[raw.data[0] as usize - 1] as i8
};

Ok(ReceivedFrame {
frame: Frame {
Expand Down
14 changes: 9 additions & 5 deletions esp-ieee802154/src/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,17 @@ use esp_wifi_sys::include::{
use heapless::spsc::Queue;

use crate::{
frame::{frame_get_version, frame_is_ack_required, FRAME_VERSION_1, FRAME_VERSION_2},
frame::{
frame_get_version,
frame_is_ack_required,
FRAME_SIZE,
FRAME_VERSION_1,
FRAME_VERSION_2,
},
hal::*,
pib::*,
};

pub(crate) const FRAME_SIZE: usize = 129;

const PHY_ENABLE_VERSION_PRINT: u32 = 1;

static mut RX_BUFFER: [u8; FRAME_SIZE] = [0u8; FRAME_SIZE];
Expand Down Expand Up @@ -391,9 +395,9 @@ fn ZB_MAC() {
log::warn!("Receive queue full");
}

let frm = if RX_BUFFER[0] > FRAME_SIZE as u8 {
let frm = if RX_BUFFER[0] >= FRAME_SIZE as u8 {
log::warn!("RX_BUFFER[0] {:} is larger than frame size", RX_BUFFER[0]);
&RX_BUFFER[1..][..FRAME_SIZE]
&RX_BUFFER[1..][..FRAME_SIZE - 1]
} else {
&RX_BUFFER[1..][..RX_BUFFER[0] as usize]
};
Expand Down