Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test_coredump: fix unaligned pointer dereference #657

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions src/coredump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,26 +88,30 @@ impl CoreDump {
let mut status = Vec::new();
for note in notes.flatten() {
if note.n_type == goblin::elf::note::NT_PRPSINFO {
psinfo = Some(unsafe { *(note.desc.as_ptr() as *const elfcore::elf_prpsinfo) });
psinfo = Some(unsafe {
std::ptr::read_unaligned(note.desc.as_ptr() as *const elfcore::elf_prpsinfo)
});
} else if note.n_type == goblin::elf::note::NT_PRSTATUS {
let thread_status =
unsafe { *(note.desc.as_ptr() as *const elfcore::elf_prstatus) };
let thread_status: elfcore::elf_prstatus = unsafe {
std::ptr::read_unaligned(note.desc.as_ptr() as *const elfcore::elf_prstatus)
};
status.push(thread_status);
} else if note.n_type == goblin::elf::note::NT_FILE {
let data = note.desc;
let ptrs = data.as_ptr() as *const usize;

let count = unsafe { *ptrs };
let _page_size = unsafe { *ptrs.offset(1) };
let count = unsafe { std::ptr::read_unaligned(ptrs) };
let _page_size = unsafe { std::ptr::read_unaligned(ptrs.offset(1)) };

let string_table = &data[(std::mem::size_of::<usize>() * (2 + count * 3))..];

for (i, filename) in string_table.split(|chr| *chr == 0).enumerate() {
if i < count {
let i = i as isize;
let start = unsafe { *ptrs.offset(i * 3 + 2) };
let _end = unsafe { *ptrs.offset(i * 3 + 3) };
let _page_offset = unsafe { *ptrs.offset(i * 3 + 4) };
let start = unsafe { std::ptr::read_unaligned(ptrs.offset(i * 3 + 2)) };
let _end = unsafe { std::ptr::read_unaligned(ptrs.offset(i * 3 + 3)) };
let _page_offset =
unsafe { std::ptr::read_unaligned(ptrs.offset(i * 3 + 4)) };

let pathname = Path::new(&OsStr::from_bytes(filename)).to_path_buf();
filenames.insert(start, pathname);
Expand Down
Loading