Skip to content

Commit 34990aa

Browse files
committed
multiboot2: fix possible panics for elf sections
1 parent 9e54f63 commit 34990aa

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

multiboot2/Changelog.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
# CHANGELOG for crate `multiboot2`
22

33
## 0.15.1 (2023-03-18)
4-
- **BREAKING** `MemoryMapTag::all_memory_areas` was renamed to `memory_areas`
4+
- **BREAKING** `MemoryMapTag::all_memory_areas()` was renamed to `memory_areas`
55
and now returns `MemoryAreaIter` instead of
66
`impl Iterator<Item = &MemoryArea>`. Experience showed that its better to
77
return the specific iterator whenever possible.
8-
- **BREAKING** `MemoryMapTag::memory_areas` was renamed to
8+
- **BREAKING** `MemoryMapTag::memory_areas()` was renamed to
99
`available_memory_areas`
1010
(_Sorry for the breaking changes in a minor release, but I just stumbled upon
1111
this und since the last breaking release was just yesterday, users have to
1212
deal with changes anyway._)
13+
- **BREAKING** `ElfSection::name()` now returns a Result instead of just the
14+
value. This prevents possible panics.
15+
- fix: prevent a possible panic in `ElfSection::section_type()`
1316

1417
## 0.15.0 (2023-03-17)
1518
- **BREAKING** MSRV is 1.56.1

multiboot2/src/elf_sections.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use crate::tag_type::Tag;
22
use crate::TagType;
33
use core::fmt::{Debug, Formatter};
4+
use std::str::Utf8Error;
5+
use derive_more::Display;
46

57
/// This tag contains section header table from an ELF kernel.
68
///
@@ -178,7 +180,10 @@ impl ElfSection {
178180
11 => ElfSectionType::DynamicLoaderSymbolTable,
179181
0x6000_0000..=0x6FFF_FFFF => ElfSectionType::EnvironmentSpecific,
180182
0x7000_0000..=0x7FFF_FFFF => ElfSectionType::ProcessorSpecific,
181-
_ => panic!(),
183+
e => {
184+
log::error!("Unknown section type {e:x}. Treating as ElfSectionType::Unused");
185+
ElfSectionType::Unused
186+
}
182187
}
183188
}
184189

@@ -188,7 +193,7 @@ impl ElfSection {
188193
}
189194

190195
/// Read the name of the section.
191-
pub fn name(&self) -> &str {
196+
pub fn name(&self) -> Result<&str, Utf8Error> {
192197
use core::{slice, str};
193198

194199
let name_ptr = unsafe { self.string_table().offset(self.get().name_index() as isize) };
@@ -202,7 +207,7 @@ impl ElfSection {
202207
len as usize
203208
};
204209

205-
str::from_utf8(unsafe { slice::from_raw_parts(name_ptr, strlen) }).unwrap()
210+
str::from_utf8(unsafe { slice::from_raw_parts(name_ptr, strlen) })
206211
}
207212

208213
/// Get the physical start address of the section.
@@ -246,15 +251,15 @@ impl ElfSection {
246251
match self.entry_size {
247252
40 => unsafe { &*(self.inner as *const ElfSectionInner32) },
248253
64 => unsafe { &*(self.inner as *const ElfSectionInner64) },
249-
_ => panic!(),
254+
s => panic!("Unexpected entry size: {s}"),
250255
}
251256
}
252257

253258
unsafe fn string_table(&self) -> *const u8 {
254259
let addr = match self.entry_size {
255260
40 => (*(self.string_section as *const ElfSectionInner32)).addr as usize,
256261
64 => (*(self.string_section as *const ElfSectionInner64)).addr as usize,
257-
_ => panic!(),
262+
s => panic!("Unexpected entry size: {s}"),
258263
};
259264
(addr + self.offset) as *const _
260265
}

0 commit comments

Comments
 (0)