Skip to contentSkip to conversation

contrib/elf2dmp: Windows Server 2022 support

PATCH In review Viktor Prutyanov 4 participants 8 messages Feb 22, 2023 – Mar 17, 2023

lore
Share & tools
lore
#0 lore raw

v2 cover contrib/elf2dmp: Windows Server 2022 support

Hi,

For now, elf2dmp is unable to convert ELF-dump to DMP-dump made of Windows Server 2022 guest. This patch series fixes it.

v1: improve code-style fix
v2: don't remove data directory entry RVA print and DOS header size check

Viktor Prutyanov (3):
contrib/elf2dmp: fix code style
contrib/elf2dmp: move PE dir search to pe_get_data_dir_entry contrib/elf2dmp: add PE name check and Windows Server 2022 support

 contrib/elf2dmp/addrspace.c |   1 +
 contrib/elf2dmp/main.c      | 108 ++++++++++++++++++++++-----------
 contrib/elf2dmp/pe.h        | 115 ++++++++++++++++++++----------------
 3 files changed, 140 insertions(+), 84 deletions(-)
Signature · 1 line
2.35.1
#1 lore raw

PATCH v2 1/3 contrib/elf2dmp: fix code style

Originally elf2dmp were added with some code style issues, especially in pe.h header, and some were introduced by 2d0fc797faaa73fbc1d30f5f9e90407bf3dd93f0. Fix them now.

---
 contrib/elf2dmp/addrspace.c |   1 +
 contrib/elf2dmp/main.c      |   9 ++--
 contrib/elf2dmp/pe.h        | 100 ++++++++++++++++++------------------
 3 files changed, 57 insertions(+), 53 deletions(-)
contrib/elf2dmp/addrspace.c +1 -0
index 53ded17061..0b04cba00e 100644
@@ -11,6 +11,7 @@
 static struct pa_block *pa_space_find_block(struct pa_space *ps, uint64_t pa)
 {
     size_t i;
+
     for (i = 0; i < ps->block_nr; i++) {
         if (ps->block[i].paddr <= pa &&
                 pa <= ps->block[i].paddr + ps->block[i].size) {
contrib/elf2dmp/main.c +6 -3
index d77b8f98f7..9224764239 100644
@@ -282,14 +282,16 @@ static int fill_header(WinDumpHeader64 *hdr, struct pa_space *ps,
     };
 
     for (i = 0; i < ps->block_nr; i++) {
-        h.PhysicalMemoryBlock.NumberOfPages += ps->block[i].size / ELF2DMP_PAGE_SIZE;
+        h.PhysicalMemoryBlock.NumberOfPages +=
+                ps->block[i].size / ELF2DMP_PAGE_SIZE;
         h.PhysicalMemoryBlock.Run[i] = (WinDumpPhyMemRun64) {
             .BasePage = ps->block[i].paddr / ELF2DMP_PAGE_SIZE,
             .PageCount = ps->block[i].size / ELF2DMP_PAGE_SIZE,
         };
     }
 
-    h.RequiredDumpSpace += h.PhysicalMemoryBlock.NumberOfPages << ELF2DMP_PAGE_BITS;
+    h.RequiredDumpSpace +=
+            h.PhysicalMemoryBlock.NumberOfPages << ELF2DMP_PAGE_BITS;
 
     *hdr = h;
 
@@ -299,7 +301,8 @@ static int fill_header(WinDumpHeader64 *hdr, struct pa_space *ps,
 static int fill_context(KDDEBUGGER_DATA64 *kdbg,
         struct va_space *vs, QEMU_Elf *qe)
 {
-        int i;
+    int i;
+
     for (i = 0; i < qe->state_nr; i++) {
         uint64_t Prcb;
         uint64_t Context;
contrib/elf2dmp/pe.h +50 -50

120 lines not on this page · raw message

Signature · 1 line
2.35.1
#2 lore raw

PATCH v2 2/3 contrib/elf2dmp: move PE dir search to pe_get_data_dir_entry

In reply to Viktor Prutyanov

Move out PE directory search functionality to be reused not only for Debug Directory processing but for arbitrary PE directory.

---
 contrib/elf2dmp/main.c | 71 +++++++++++++++++++++++++-----------------
 1 file changed, 42 insertions(+), 29 deletions(-)
contrib/elf2dmp/main.c +42 -29
index 9224764239..2f6028d8eb 100644
@@ -333,6 +333,45 @@ static int fill_context(KDDEBUGGER_DATA64 *kdbg,
     return 0;
 }
 
+static int pe_get_data_dir_entry(uint64_t base, void *start_addr, int idx,
+        void *entry, size_t size, struct va_space *vs)
+{
+    const char e_magic[2] = "MZ";
+    const char Signature[4] = "PE\0\0";
+    IMAGE_DOS_HEADER *dos_hdr = start_addr;
+    IMAGE_NT_HEADERS64 nt_hdrs;
+    IMAGE_FILE_HEADER *file_hdr = &nt_hdrs.FileHeader;
+    IMAGE_OPTIONAL_HEADER64 *opt_hdr = &nt_hdrs.OptionalHeader;
+    IMAGE_DATA_DIRECTORY *data_dir = nt_hdrs.OptionalHeader.DataDirectory;
+
+    QEMU_BUILD_BUG_ON(sizeof(*dos_hdr) >= ELF2DMP_PAGE_SIZE);
+
+    if (memcmp(&dos_hdr->e_magic, e_magic, sizeof(e_magic))) {
+        return 1;
+    }
+
+    if (va_space_rw(vs, base + dos_hdr->e_lfanew,
+                &nt_hdrs, sizeof(nt_hdrs), 0)) {
+        return 1;
+    }
+
+    if (memcmp(&nt_hdrs.Signature, Signature, sizeof(Signature)) ||
+            file_hdr->Machine != 0x8664 || opt_hdr->Magic != 0x020b) {
+        return 1;
+    }
+
+    if (va_space_rw(vs,
+                base + data_dir[idx].VirtualAddress,
+                entry, size, 0)) {
+        return 1;
+    }
+
+    printf("Data directory entry #%d: RVA = 0x%08"PRIx32"\n", idx,
+            (uint32_t)data_dir[idx].VirtualAddress);
+
+    return 0;
+}
+
 static int write_dump(struct pa_space *ps,
         WinDumpHeader64 *hdr, const char *name)
 {
@@ -369,42 +408,16 @@ static int write_dump(struct pa_space *ps,
 static int pe_get_pdb_symstore_hash(uint64_t base, void *start_addr,
         char *hash, struct va_space *vs)
 {
-    const char e_magic[2] = "MZ";
-    const char Signature[4] = "PE\0\0";
     const char sign_rsds[4] = "RSDS";
-    IMAGE_DOS_HEADER *dos_hdr = start_addr;
-    IMAGE_NT_HEADERS64 nt_hdrs;
-    IMAGE_FILE_HEADER *file_hdr = &nt_hdrs.FileHeader;
-    IMAGE_OPTIONAL_HEADER64 *opt_hdr = &nt_hdrs.OptionalHeader;
-    IMAGE_DATA_DIRECTORY *data_dir = nt_hdrs.OptionalHeader.DataDirectory;
     IMAGE_DEBUG_DIRECTORY debug_dir;
     OMFSignatureRSDS rsds;
     char *pdb_name;
     size_t pdb_name_sz;
     size_t i;
 
-    QEMU_BUILD_BUG_ON(sizeof(*dos_hdr) >= ELF2DMP_PAGE_SIZE);
-
-    if (memcmp(&dos_hdr->e_magic, e_magic, sizeof(e_magic))) {
-        return 1;
-    }
-
-    if (va_space_rw(vs, base + dos_hdr->e_lfanew,
-                &nt_hdrs, sizeof(nt_hdrs), 0)) {
-        return 1;
-    }
-
-    if (memcmp(&nt_hdrs.Signature, Signature, sizeof(Signature)) ||
-            file_hdr->Machine != 0x8664 || opt_hdr->Magic != 0x020b) {
-        return 1;
-    }
-
-    printf("Debug Directory RVA = 0x%08"PRIx32"\n",
-            (uint32_t)data_dir[IMAGE_FILE_DEBUG_DIRECTORY].VirtualAddress);
-
-    if (va_space_rw(vs,
-                base + data_dir[IMAGE_FILE_DEBUG_DIRECTORY].VirtualAddress,
-                &debug_dir, sizeof(debug_dir), 0)) {
+    if (pe_get_data_dir_entry(base, start_addr, IMAGE_FILE_DEBUG_DIRECTORY,
+                &debug_dir, sizeof(debug_dir), vs)) {
+        eprintf("Failed to get Debug Directory\n");
         return 1;
     }
 
Signature · 1 line
2.35.1
#3 lore raw

PATCH v2 3/3 contrib/elf2dmp: add PE name check and Windows Server 2022 support

In reply to Viktor Prutyanov

Since its inception elf2dmp has checked MZ signatures within an address space above IDT[0] interrupt vector and took first PE image found as Windows Kernel.
But in Windows Server 2022 memory dump this address space range is full of invalid PE fragments and the tool must check that PE image is 'ntoskrnl.exe' actually.
So, introduce additional validation by checking image name from Export Directory against 'ntoskrnl.exe'.

---
 contrib/elf2dmp/main.c | 28 ++++++++++++++++++++++++++--
 contrib/elf2dmp/pe.h   | 15 +++++++++++++++
 2 files changed, 41 insertions(+), 2 deletions(-)
contrib/elf2dmp/main.c +26 -2
index 2f6028d8eb..89f0c69ab0 100644
@@ -17,6 +17,7 @@
 
 #define SYM_URL_BASE    "https://msdl.microsoft.com/download/symbols/"
 #define PDB_NAME    "ntkrnlmp.pdb"
+#define PE_NAME     "ntoskrnl.exe"
 
 #define INITIAL_MXCSR   0x1f80
 
@@ -405,6 +406,25 @@ static int write_dump(struct pa_space *ps,
     return fclose(dmp_file);
 }
 
+static bool pe_check_export_name(uint64_t base, void *start_addr,
+        struct va_space *vs)
+{
+    IMAGE_EXPORT_DIRECTORY export_dir;
+    const char *pe_name;
+
+    if (pe_get_data_dir_entry(base, start_addr, IMAGE_FILE_EXPORT_DIRECTORY,
+                &export_dir, sizeof(export_dir), vs)) {
+        return false;
+    }
+
+    pe_name = va_space_resolve(vs, base + export_dir.Name);
+    if (!pe_name) {
+        return false;
+    }
+
+    return !strcmp(pe_name, PE_NAME);
+}
+
 static int pe_get_pdb_symstore_hash(uint64_t base, void *start_addr,
         char *hash, struct va_space *vs)
 {
@@ -489,6 +509,7 @@ int main(int argc, char *argv[])
     uint64_t KdDebuggerDataBlock;
     KDDEBUGGER_DATA64 *kdbg;
     uint64_t KdVersionBlock;
+    bool kernel_found = false;
 
     if (argc != 3) {
         eprintf("usage:\n\t%s elf_file dmp_file\n", argv[0]);
@@ -536,11 +557,14 @@ int main(int argc, char *argv[])
         }
 
         if (*(uint16_t *)nt_start_addr == 0x5a4d) { /* MZ */
-            break;
+            if (pe_check_export_name(KernBase, nt_start_addr, &vs)) {
+                kernel_found = true;
+                break;
+            }
         }
     }
 
-    if (!nt_start_addr) {
+    if (!kernel_found) {
         eprintf("Failed to find NT kernel image\n");
         err = 1;
         goto out_ps;
contrib/elf2dmp/pe.h +15 -0
index 807d006364..71126af1ac 100644
@@ -88,6 +88,20 @@ typedef struct IMAGE_NT_HEADERS64 {
     IMAGE_OPTIONAL_HEADER64 OptionalHeader;
 } __attribute__ ((packed)) IMAGE_NT_HEADERS64;
 
+typedef struct IMAGE_EXPORT_DIRECTORY {
+    uint32_t    Characteristics;
+    uint32_t    TimeDateStamp;
+    uint16_t    MajorVersion;
+    uint16_t    MinorVersion;
+    uint32_t    Name;
+    uint32_t    Base;
+    uint32_t    NumberOfFunctions;
+    uint32_t    NumberOfNames;
+    uint32_t    AddressOfFunctions;
+    uint32_t    AddressOfNames;
+    uint32_t    AddressOfNameOrdinals;
+} __attribute__ ((packed)) IMAGE_EXPORT_DIRECTORY;
+
 typedef struct IMAGE_DEBUG_DIRECTORY {
     uint32_t Characteristics;
     uint32_t TimeDateStamp;
@@ -102,6 +116,7 @@ typedef struct IMAGE_DEBUG_DIRECTORY {
 #define IMAGE_DEBUG_TYPE_CODEVIEW   2
 #endif
 
+#define IMAGE_FILE_EXPORT_DIRECTORY 0
 #define IMAGE_FILE_DEBUG_DIRECTORY  6
 
 typedef struct guid_t {
Signature · 1 line
2.35.1
Reviewed-by on the v2 series
#4 lore raw
In reply to Viktor Prutyanov

Hello Viktor,

This patch set looks good to me, thank you!

Quoted 7 lines · Viktor Prutyanov

Hi,

For now, elf2dmp is unable to convert ELF-dump to DMP-dump made of Windows Server 2022 guest. This patch series fixes it.

v1: improve code-style fix
v2: don't remove data directory entry RVA print and DOS header size check

Thanks

Annie

Quoted 11 lines

Viktor Prutyanov (3):
contrib/elf2dmp: fix code style
contrib/elf2dmp: move PE dir search to pe_get_data_dir_entry contrib/elf2dmp: add PE name check and Windows Server 2022 support

  contrib/elf2dmp/addrspace.c |   1 +
  contrib/elf2dmp/main.c      | 108 ++++++++++++++++++++++-----------
  contrib/elf2dmp/pe.h        | 115 ++++++++++++++++++++----------------
  3 files changed, 140 insertions(+), 84 deletions(-)
#5 lore raw
In reply to Viktor Prutyanov
Quoted 15 lines

Hi,

For now, elf2dmp is unable to convert ELF-dump to DMP-dump made of Windows Server 2022 guest. This patch series fixes it.

v1: improve code-style fix
v2: don't remove data directory entry RVA print and DOS header size check

Viktor Prutyanov (3):
contrib/elf2dmp: fix code style
contrib/elf2dmp: move PE dir search to pe_get_data_dir_entry contrib/elf2dmp: add PE name check and Windows Server 2022 support

Quoted 7 lines

contrib/elf2dmp/addrspace.c | 1 + contrib/elf2dmp/main.c | 108 ++++++++++++++++++++++----------- contrib/elf2dmp/pe.h | 115 ++++++++++++++++++++----------------

3 files changed, 140 insertions(+), 84 deletions(-)

--
2.35.1

Hi Peter,

As we discussed, I would like to ask you to pick up this series.

Thanks,
Viktor Prutyanov

#6 lore raw
Quoted 21 lines · Viktor Prutyanov
Quoted 21 lines

Hi,

For now, elf2dmp is unable to convert ELF-dump to DMP-dump made of Windows Server 2022 guest. This patch series fixes it.

v1: improve code-style fix
v2: don't remove data directory entry RVA print and DOS header size check

Viktor Prutyanov (3):
contrib/elf2dmp: fix code style
contrib/elf2dmp: move PE dir search to pe_get_data_dir_entry contrib/elf2dmp: add PE name check and Windows Server 2022 support

contrib/elf2dmp/addrspace.c | 1 + contrib/elf2dmp/main.c | 108 ++++++++++++++++++++++----------- contrib/elf2dmp/pe.h | 115 ++++++++++++++++++++----------------

3 files changed, 140 insertions(+), 84 deletions(-)
Quoted 6 lines
Quoted 2 lines

--
2.35.1

Hi Peter,

As we discussed, I would like to ask you to pick up this series.

Sure, I can put this into the target-arm pullreq I'm doing, unless anybody prefers them to go into the tree via a different route.

thanks
-- PMM

#7 lore raw

Thank you! Personally, I agree with any way to get the elf2dmp patch series into the tree.

Best regards,
Viktor Prutyanov