Skip to content

Commit 6bdbb2b

Browse files
Alexandra IordacheSamuel Ortiz
Alexandra Iordache
authored and
Samuel Ortiz
committed
fix unchecked arithmetic in BzImage::load
Fixed unchecked arithmetic operations (addition, substraction and multiplication) that could cause the sizes of the setup sectors and kernel image to be incorrectly computed. Signed-off-by: Alexandra Iordache <[email protected]> Signed-off-by: Andreea Florescu <[email protected]>
1 parent f947af0 commit 6bdbb2b

File tree

1 file changed

+13
-2
lines changed
  • src/loader/x86_64/bzimage

1 file changed

+13
-2
lines changed

src/loader/x86_64/bzimage/mod.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ use super::super::{
2626
pub enum Error {
2727
/// Invalid bzImage binary.
2828
InvalidBzImage,
29+
/// Overflow occurred during an arithmetic operation.
30+
Overflow,
2931
/// Unable to read bzImage header.
3032
ReadBzImageHeader,
3133
/// Unable to read bzImage compressed image.
@@ -36,17 +38,21 @@ pub enum Error {
3638
SeekBzImageHeader,
3739
/// Unable to seek to bzImage compressed kernel.
3840
SeekBzImageCompressedKernel,
41+
/// Underflow occurred during an arithmetic operation.
42+
Underflow,
3943
}
4044

4145
impl fmt::Display for Error {
4246
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4347
let desc = match self {
4448
Error::InvalidBzImage => "Invalid bzImage",
49+
Error::Overflow => "Overflow occurred during an arithmetic operation",
4550
Error::ReadBzImageHeader => "Unable to read bzImage header",
4651
Error::ReadBzImageCompressedKernel => "Unable to read bzImage compressed kernel",
4752
Error::SeekBzImageEnd => "Unable to seek bzImage end",
4853
Error::SeekBzImageHeader => "Unable to seek bzImage header",
4954
Error::SeekBzImageCompressedKernel => "Unable to seek bzImage compressed kernel",
55+
Error::Underflow => "Underflow occurred during an arithmetic operation",
5056
};
5157

5258
write!(f, "Kernel Loader: {}", desc)
@@ -130,8 +136,13 @@ impl KernelLoader for BzImage {
130136
if setup_size == 0 {
131137
setup_size = 4;
132138
}
133-
setup_size = (setup_size + 1) * 512;
134-
kernel_size -= setup_size;
139+
setup_size = setup_size
140+
.checked_add(1)
141+
.and_then(|setup_size| setup_size.checked_mul(512))
142+
.ok_or(Error::Overflow)?;
143+
kernel_size = kernel_size
144+
.checked_sub(setup_size)
145+
.ok_or(Error::Underflow)?;
135146

136147
// Check that `code32_start`, the default address of the kernel, is not lower than high
137148
// memory.

0 commit comments

Comments
 (0)