From f329fe5cb04121ae45278dd45dfd202169073b2d Mon Sep 17 00:00:00 2001 From: Karoline Pauls Date: Sat, 9 Mar 2024 12:44:13 +0000 Subject: [PATCH] test_coredump: fix unaligned pointer dereference --- src/coredump.rs | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/coredump.rs b/src/coredump.rs index 53940bdb..5289666e 100644 --- a/src/coredump.rs +++ b/src/coredump.rs @@ -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::() * (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);