Skip to content

Commit 22ec0a1

Browse files
Merge pull request #1758 from rust-osdev/bugfix-mem-gop
uefi: memory safety fixes (UB!)
2 parents 90c5ba4 + db43627 commit 22ec0a1

File tree

4 files changed

+21
-13
lines changed

4 files changed

+21
-13
lines changed

uefi-raw/src/protocol/console.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,8 @@ pub struct GraphicsOutputProtocol {
161161
pub set_mode: unsafe extern "efiapi" fn(*mut Self, mode_number: u32) -> Status,
162162
pub blt: unsafe extern "efiapi" fn(
163163
*mut Self,
164+
// Depending on `blt_operation`, this is an IN parameter (readable)
165+
// or an OUT parameter (writeable).
164166
blt_buffer: *mut GraphicsOutputBltPixel,
165167
blt_operation: GraphicsOutputBltOperation,
166168
source_x: usize,

uefi/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
image in QEMU or Cloud Hypervisor, when the debugcon/debug-console device is
2424
available.
2525
- The documentation for UEFI protocols has been streamlined and improved.
26+
- Fixed memory safety bug in `SimpleNetwork::read_nv_data`. The `buffer`
27+
parameter is now mutable.
2628

2729
# uefi - 0.35.0 (2025-05-04)
2830

uefi/src/proto/console/gop.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ use core::fmt::{Debug, Formatter};
5959
use core::marker::PhantomData;
6060
use core::ptr::{self, NonNull};
6161
use uefi_raw::protocol::console::{
62-
GraphicsOutputBltOperation, GraphicsOutputModeInformation, GraphicsOutputProtocol,
63-
GraphicsOutputProtocolMode,
62+
GraphicsOutputBltOperation, GraphicsOutputBltPixel, GraphicsOutputModeInformation,
63+
GraphicsOutputProtocol, GraphicsOutputProtocolMode,
6464
};
6565

6666
pub use uefi_raw::protocol::console::PixelBitmask;
@@ -201,7 +201,8 @@ impl GraphicsOutput {
201201
match src_region {
202202
BltRegion::Full => (self.0.blt)(
203203
&mut self.0,
204-
buffer.as_ptr() as *mut _,
204+
// SAFETY: The buffer is only used for reading.
205+
buffer.as_ptr().cast::<GraphicsOutputBltPixel>().cast_mut(),
205206
GraphicsOutputBltOperation::BLT_BUFFER_TO_VIDEO,
206207
0,
207208
0,
@@ -217,7 +218,8 @@ impl GraphicsOutput {
217218
px_stride,
218219
} => (self.0.blt)(
219220
&mut self.0,
220-
buffer.as_ptr() as *mut _,
221+
// SAFETY: The buffer is only used for reading.
222+
buffer.as_ptr().cast::<GraphicsOutputBltPixel>().cast_mut(),
221223
GraphicsOutputBltOperation::BLT_BUFFER_TO_VIDEO,
222224
src_x,
223225
src_y,

uefi/src/proto/network/snp.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -142,30 +142,32 @@ impl SimpleNetwork {
142142
status.to_result_with_val(|| mac_address)
143143
}
144144

145-
/// Perform read operations on the NVRAM device attached to
146-
/// a network interface.
147-
pub fn read_nv_data(&self, offset: usize, buffer: &[u8]) -> Result {
145+
/// Reads data from the NVRAM device attached to the network interface into
146+
/// the provided `dst_buffer`.
147+
pub fn read_nv_data(&self, offset: usize, dst_buffer: &mut [u8]) -> Result {
148148
unsafe {
149149
(self.0.non_volatile_data)(
150150
&self.0,
151151
Boolean::from(true),
152152
offset,
153-
buffer.len(),
154-
buffer.as_ptr() as *mut c_void,
153+
dst_buffer.len(),
154+
dst_buffer.as_mut_ptr().cast(),
155155
)
156156
}
157157
.to_result()
158158
}
159159

160-
/// Perform write operations on the NVRAM device attached to a network interface.
161-
pub fn write_nv_data(&self, offset: usize, buffer: &mut [u8]) -> Result {
160+
/// Writes data into the NVRAM device attached to the network interface from
161+
/// the provided `src_buffer`.
162+
pub fn write_nv_data(&self, offset: usize, src_buffer: &[u8]) -> Result {
162163
unsafe {
163164
(self.0.non_volatile_data)(
164165
&self.0,
165166
Boolean::from(false),
166167
offset,
167-
buffer.len(),
168-
buffer.as_mut_ptr().cast(),
168+
src_buffer.len(),
169+
// SAFETY: The buffer is only used for reading.
170+
src_buffer.as_ptr().cast::<c_void>().cast_mut(),
169171
)
170172
}
171173
.to_result()

0 commit comments

Comments
 (0)