Skip to content

Commit 193f51a

Browse files
committed
uefi: Fix unaligned memcpy in ConfigurationString parser
1 parent b1b0461 commit 193f51a

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

uefi/src/proto/hii/config_str.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use core::slice;
99
use core::str::{self, FromStr};
1010
use uguid::Guid;
1111

12+
use crate::mem::AlignedBuffer;
1213
use crate::proto::device_path::DevicePath;
1314
use crate::{CStr16, Char16};
1415

@@ -25,7 +26,7 @@ pub struct ConfigurationStringIter<'a> {
2526
}
2627

2728
impl<'a> ConfigurationStringIter<'a> {
28-
/// Creates a new splitter instance for a given configuration string buffer.
29+
/// Creates a new splitter instance for a given configuration string.
2930
#[must_use]
3031
pub const fn new(bfr: &'a str) -> Self {
3132
Self { bfr }
@@ -169,11 +170,15 @@ impl ConfigurationString {
169170
if data.len() % 2 != 0 {
170171
return None;
171172
}
172-
let mut data: Vec<_> = Self::parse_bytes_from_hex(data).collect();
173-
data.chunks_exact_mut(2).for_each(|c| c.swap(0, 1));
174-
data.extend_from_slice(&[0, 0]);
173+
let size_bytes = data.len() / 2 + 2; // includes \0 terminator
174+
let size_chars = size_bytes / 2;
175+
let mut bfr = AlignedBuffer::from_size_align(size_bytes, 2).ok()?;
176+
bfr.copy_from_iter(Self::parse_bytes_from_hex(data).chain([0, 0]));
177+
bfr.as_slice_mut()
178+
.chunks_exact_mut(2)
179+
.for_each(|c| c.swap(0, 1));
175180
let data: &[Char16] =
176-
unsafe { slice::from_raw_parts(data.as_slice().as_ptr().cast(), data.len() / 2) };
181+
unsafe { slice::from_raw_parts(bfr.as_slice().as_ptr().cast(), size_chars) };
177182
Some(CStr16::from_char16_with_nul(data).ok()?.to_string())
178183
}
179184

0 commit comments

Comments
 (0)