davea42 / libdwarf-code

Contains source for libdwarf, a library for reading DWARF2 and later DWARF. Contains source to create dwarfdump, a program which prints DWARF2 and later DWARF in readable format. Has a very limited DWARF writer set of functions in libdwarfp (producer library). Builds using GNU configure, meson, or cmake.
Other
173 stars 70 forks source link

DW_DLE_TOO_FEW_SECTIONS Error for ELF files with no SHDRs #219

Closed rvijayc closed 10 months ago

rvijayc commented 10 months ago

It is quite valid to have an ELF file with program headers only and no section headers (for example, core dumps). But, the following code raises a DW_DLV_TOO_FEW_SECTIONS error for such files.

https://github.com/davea42/libdwarf-code/blob/c0cfba34ec80996426b5be2523f6447a2c9b7b39/src/lib/libdwarf/dwarf_elf_load_headers.c#L213-L216

Wouldn't it be better to handle this gracefully by returning DW_DLV_NO_ENTRY instead of DW_DLV_ERROR? I believe this is what the older versions of libdwarf used to return when we called dwarf_elf_init. I don't believe this should be an error. Please correct me if I missed anything.

Appreciate your help! Thank you.

davea42 commented 10 months ago

You are correct, this was a mistake on my part.

According to the Elf generic ABI e_shoff of zero means there are no sections.

the following patch is untested, but I will test now and push this soonish.

-- a/src/lib/libdwarf/dwarf_elf_load_headers.c
+++ b/src/lib/libdwarf/dwarf_elf_load_headers.c
@@ -210,8 +210,11 @@ generic_ehdr_from_32(dwarf_elf_object_access_internals_t *ep,
     ASNAR(ep->f_copy_word,ehdr->ge_shentsize,e->e_shentsize);
     ASNAR(ep->f_copy_word,ehdr->ge_shnum,e->e_shnum);
     ASNAR(ep->f_copy_word,ehdr->ge_shstrndx,e->e_shstrndx);
+    if (!ehdr->ge_shoff) {
+        return DW_DLV_NO_ENTRY;
+    }
     if (ehdr->ge_shoff < sizeof(dw_elf32_ehdr)) {
-        /* zero or offset is inside the header! */
+        /* offset is inside the header! */

I will also patch one of my small test objects to put in a zero e_shoff to ensure the right thing happens.

Thanks for pointing this out. Lets leave this open until it is actually fixed...

rvijayc commented 10 months ago

Thanks for your response. I assume this will apply to other similar checks as well? For example:

https://github.com/davea42/libdwarf-code/blob/c0cfba34ec80996426b5be2523f6447a2c9b7b39/src/lib/libdwarf/dwarf_elf_load_headers.c#L222-L225

https://github.com/davea42/libdwarf-code/blob/c0cfba34ec80996426b5be2523f6447a2c9b7b39/src/lib/libdwarf/dwarf_elf_load_headers.c#L233-L236

Although, we may never hit this for ELF files with no sections header (we'll bail out earlier on the shoff check).

davea42 commented 10 months ago

What is at line 222 is detecting corrupt dwarf, and is an error.

I created an object with zeroed e_shoff in regressiontests so we have an ongoing test of that specific issue.

Changes pushed to github.

davea42 commented 10 months ago

The changes are in today's v0.9.1 release.

davea42 commented 10 months ago

Since (I believe) this is fixed, I am closing. I have two test objects now for regression testing, each with e_shoff zero. One Elf32 the other Elf64.