Skip to content

Commit fd0a564

Browse files
JBYoshiJonathanWoollett-Light
authored andcommitted
ARM64: Add bounds checks on device reads/writes
Errors here appear to just emit warnings, so I added a check here to ensure the offset is in bounds. I've done this in a separate commit because my dev setup doesn't use ARM. In case something ARM-specific breaks in CI, I'd like to be able to keep those changes in their own commits so I can debug more easily. Signed-off-by: Jonathan Browne <[email protected]>
1 parent a38c2a4 commit fd0a564

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

src/vmm/src/devices/legacy/rtc_pl031.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,27 +26,29 @@ impl std::ops::DerefMut for RTCDevice {
2626
// Implements Bus functions for AMBA PL031 RTC device
2727
impl RTCDevice {
2828
pub fn bus_read(&mut self, offset: u64, data: &mut [u8]) {
29-
if data.len() == 4 {
29+
if let (Ok(offset), 4) = (u16::try_from(offset), data.len()) {
3030
// read() function from RTC implementation expects a slice of
3131
// len 4, and we just validated that this is the data lengt
32-
self.read(offset as u16, data.try_into().unwrap())
32+
self.read(offset, data.try_into().unwrap())
3333
} else {
3434
warn!(
35-
"Found invalid data length while trying to read from the RTC: {}",
35+
"Found invalid data offset/length while trying to read from the RTC: {}, {}",
36+
offset,
3637
data.len()
3738
);
3839
METRICS.rtc.error_count.inc();
3940
}
4041
}
4142

4243
pub fn bus_write(&mut self, offset: u64, data: &[u8]) {
43-
if data.len() == 4 {
44+
if let (Ok(offset), 4) = (u16::try_from(offset), data.len()) {
4445
// write() function from RTC implementation expects a slice of
4546
// len 4, and we just validated that this is the data length
46-
self.write(offset as u16, data.try_into().unwrap())
47+
self.write(offset, data.try_into().unwrap())
4748
} else {
4849
warn!(
49-
"Found invalid data length while trying to write to the RTC: {}",
50+
"Found invalid data offset/length while trying to write to the RTC: {}, {}",
51+
offset,
5052
data.len()
5153
);
5254
METRICS.rtc.error_count.inc();

0 commit comments

Comments
 (0)