Skip to contentSkip to conversation

contrib/elf2dmp: Windows Server 2022 support

PATCH Superseded Viktor Prutyanov 4 participants 10 messages Nov 30, 2022 – Feb 22, 2023

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#1 lore raw

PATCH 1/3 contrib/elf2dmp: fix code style

In reply to Viktor Prutyanov

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 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 | 66 +++++++++++++++++++++++-------------------
 1 file changed, 37 insertions(+), 29 deletions(-)
contrib/elf2dmp/main.c +37 -29
index 9224764239..f3052b3c64 100644
@@ -333,6 +333,40 @@ 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;
+
+    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;
+    }
+
+    return 0;
+}
+
 static int write_dump(struct pa_space *ps,
         WinDumpHeader64 *hdr, const char *name)
 {
@@ -369,42 +403,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
#0 lore raw

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

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      | 103 +++++++++++++++++++++-----------
 contrib/elf2dmp/pe.h        | 115 ++++++++++++++++++++----------------
 3 files changed, 135 insertions(+), 84 deletions(-)
Signature · 1 line
2.35.1
#5 lore raw

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

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 f3052b3c64..f7de82a03e 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
 
@@ -400,6 +401,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)
 {
@@ -484,6 +504,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]);
@@ -531,11 +552,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
#8 lore raw
In reply to Viktor Prutyanov
Quoted 12 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

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 5 lines
  contrib/elf2dmp/addrspace.c |   1 +
  contrib/elf2dmp/main.c      | 103 +++++++++++++++++++++-----------
  contrib/elf2dmp/pe.h        | 115 ++++++++++++++++++++----------------
  3 files changed, 135 insertions(+), 84 deletions(-)

Hi Paolo,

Could you please add the series to your branch?
I've sent it from my other email. I can add

Thanks,
Viktor Prutyanov

#9 lore raw

Ping.

Quoted 29 lines · Viktor Prutyanov
Quoted 12 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

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 5 lines
  contrib/elf2dmp/addrspace.c |   1 +
  contrib/elf2dmp/main.c      | 103 +++++++++++++++++++++-----------
  contrib/elf2dmp/pe.h        | 115 ++++++++++++++++++++----------------
  3 files changed, 135 insertions(+), 84 deletions(-)

Hi Paolo,

Could you please add the series to your branch?
I've sent it from my other email. I can add

Thanks,
Viktor Prutyanov

#3 lore raw

Re: [PATCH v1 2/3] contrib/elf2dmp: move PE dir search to pe_get_data_dir_entry

In reply to Viktor Prutyanov

Hello Viktor,

See my following comments inline,

Quoted 62 lines · 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 | 66 +++++++++++++++++++++++-------------------
  1 file changed, 37 insertions(+), 29 deletions(-)
contrib/elf2dmp/main.c +34 -4
index 9224764239..f3052b3c64 100644
@@ -333,6 +333,40 @@ 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;
+
+    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;
+    }
+
+    return 0;
+}
+
  static int write_dump(struct pa_space *ps,
          WinDumpHeader64 *hdr, const char *name)
  {
@@ -369,42 +403,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;

contrib/elf2dmp/main.c static int write_dump(struct pa_space *ps, · line 386

Quoted 10 lines
contrib/elf2dmp/main.c +0 -4
-    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);

This BUG_ON gets removed due to encapsulating the code into function pe_get_data_dir_entry.

Any reason of not keeping this check in pe_get_data_dir_entry?

Quoted 7 lines
contrib/elf2dmp/main.c +0 -7
-
-    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)) {

contrib/elf2dmp/main.c static int write_dump(struct pa_space *ps, · line 403

Quoted 10 lines
contrib/elf2dmp/main.c +0 -10
-        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);

Or add common log for both Debug and PE directory instead of removing it?

Thanks

Annie

Quoted 10 lines
contrib/elf2dmp/main.c +3 -4
-
-    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;
      }
 
#6 lore raw

Re: [PATCH v1 3/3] contrib/elf2dmp: add PE name check and Windows Server 2022 support

In reply to Viktor Prutyanov
Quoted 21 lines · 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 +0 -0
index f3052b3c64..f7de82a03e 100644

contrib/elf2dmp/main.c line 20

Quoted 4 lines
contrib/elf2dmp/main.c +1 -0
@@ -17,6 +17,7 @@
 
  #define SYM_URL_BASE    "https://msdl.microsoft.com/download/symbols/"
  #define PDB_NAME    "ntkrnlmp.pdb"
+#define PE_NAME     "ntoskrnl.exe"

As what has been clarified earlier in the meeting, this elf2dmp is only for 64bits systems, so the name "ntoskrnl.exe" suffices here. Otherwise, it won't work for 32bits PAE systems.

A question about elf2dmp on ARM platform, has it been validated there?

Thanks

Annie

Quoted 87 lines
contrib/elf2dmp/main.c +25 -2
  #define INITIAL_MXCSR   0x1f80
 
@@ -400,6 +401,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)
  {
@@ -484,6 +504,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]);
@@ -531,11 +552,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 {
#7 lore raw

Re: [PATCH v1 3/3] contrib/elf2dmp: add PE name check and Windows Server 2022 support

Quoted 123 lines · Annie.li
Quoted 21 lines · 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 +0 -0
index f3052b3c64..f7de82a03e 100644

contrib/elf2dmp/main.c line 20

Quoted 4 lines
contrib/elf2dmp/main.c +1 -0
@@ -17,6 +17,7 @@
 
  #define SYM_URL_BASE    "https://msdl.microsoft.com/download/symbols/"
  #define PDB_NAME    "ntkrnlmp.pdb"
+#define PE_NAME     "ntoskrnl.exe"

As what has been clarified earlier in the meeting, this elf2dmp is only for 64bits systems, so the name "ntoskrnl.exe" suffices here. Otherwise, it won't work
for 32bits PAE systems.

A question about elf2dmp on ARM platform, has it been validated there?

Thanks

Annie

Quoted 83 lines
contrib/elf2dmp/main.c +25 -2
  #define INITIAL_MXCSR   0x1f80
 
@@ -400,6 +401,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)
  {
@@ -484,6 +504,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]);
@@ -531,11 +552,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 +14 -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
 
Quoted 4 lines

contrib/elf2dmp/pe.h typedef struct IMAGE_DEBUG_DIRECTORY { · line 122

Quoted 4 lines
contrib/elf2dmp/pe.h +1 -0
+#define IMAGE_FILE_EXPORT_DIRECTORY 0
  #define IMAGE_FILE_DEBUG_DIRECTORY  6
 
  typedef struct guid_t {

Hi Annie,

Thank you for the review!
At the moment, elf2dmp only addresses the x86_64 platform.

Best regards,
Viktor Prutyanov

#4 lore raw

Re: [PATCH v1 2/3] contrib/elf2dmp: move PE dir search to pe_get_data_dir_entry

In reply to Annie.li

Hello,

Quoted 94 lines · Annie.li

Hello Viktor,

See my following comments inline,

Quoted 62 lines · 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 | 66 +++++++++++++++++++++++-------------------
  1 file changed, 37 insertions(+), 29 deletions(-)
contrib/elf2dmp/main.c +34 -4
index 9224764239..f3052b3c64 100644
@@ -333,6 +333,40 @@ 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;
+
+    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;
+    }
+
+    return 0;
+}
+
  static int write_dump(struct pa_space *ps,
          WinDumpHeader64 *hdr, const char *name)
  {
@@ -369,42 +403,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;

contrib/elf2dmp/main.c static int write_dump(struct pa_space *ps, · line 386

Quoted 10 lines
contrib/elf2dmp/main.c +0 -4
-    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);

This BUG_ON gets removed due to encapsulating the code into function pe_get_data_dir_entry.

Any reason of not keeping this check in pe_get_data_dir_entry?

Quoted 11 lines
contrib/elf2dmp/main.c +0 -11
-
-    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)) ||
Quoted 8 lines

contrib/elf2dmp/main.c static int write_dump(struct pa_space *ps, · line 403

Quoted 6 lines
contrib/elf2dmp/main.c +0 -6
-            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);

Or add common log for both Debug and PE directory instead of removing it?

Sounds reasonable, I will send a new version.

Best regards,
Viktor Prutyanov

Quoted 15 lines

Thanks

Annie

Quoted 10 lines
contrib/elf2dmp/main.c +3 -4
-
-    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;
      }