librz/bin: add more ELF header warning detail (#6301)

This commit is contained in:
Khairul Azhar Kasmiran 2026-05-02 16:43:28 +08:00 committed by GitHub
parent 056bb141a9
commit c1e26fee02
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 99 additions and 50 deletions

View file

@ -33,28 +33,40 @@ static bool verify_phdr_entry(ELFOBJ *bin, RzBinObjectLoadOptions *options, Elf_
return true;
}
Elf_(Off) end_off;
if (!Elf_(rz_bin_elf_add_off)(&end_off, entry->p_offset, entry->p_filesz) || end_off > bin->size) {
RZ_LOG_WARN("phdr entry: p_offset is invalid for p_filesz.\n");
return false;
bool ret = true;
if (entry->p_filesz > bin->size) {
RZ_LOG_WARN("phdr entry: p_filesz (%" PFMTnd " bytes) > binary size (%" PFMT64d " bytes).\n",
entry->p_filesz, bin->size);
ret = false;
} else {
Elf_(Off) end_off;
if (!Elf_(rz_bin_elf_add_off)(&end_off, entry->p_offset, entry->p_filesz) || end_off > bin->size) {
RZ_LOG_WARN("phdr entry: p_offset (0x%" PFMTnx ") is invalid for "
"p_filesz (%" PFMTnd " bytes) and binary size (%" PFMT64d " bytes).\n",
entry->p_offset, entry->p_filesz, bin->size);
ret = false;
}
}
if (!Elf_(rz_bin_elf_add_addr)(NULL, entry->p_vaddr, entry->p_memsz)) {
RZ_LOG_WARN("phdr entry: p_vaddr is invalid for p_memsz.\n");
return false;
ret = false;
}
if (entry->p_flags == PT_LOAD && (!entry->p_memsz || entry->p_filesz > entry->p_memsz)) {
RZ_LOG_WARN("phdr entry: p_memsz and/or p_filesz are invalid for a PT_LOAD segment.\n");
return false;
ret = false;
}
if (entry->p_align && entry->p_offset % entry->p_align != entry->p_vaddr % entry->p_align) {
RZ_LOG_WARN("phdr entry: p_offset and p_vaddr are not congruent for p_align.\n");
return false;
RZ_LOG_WARN("phdr entry: p_offset (0x%" PFMTnx ") and p_vaddr (0x%" PFMTnx ") "
"are not congruent for p_align (0x%" PFMTnx ").\n",
entry->p_offset, entry->p_vaddr, entry->p_align);
ret = false;
}
return true;
return ret;
}
static bool get_elf_segment(ELFOBJ *bin, RzBinObjectLoadOptions *options, RzBinElfSegment *segment, ut64 offset, size_t pos) {

View file

@ -28,6 +28,12 @@
#define ELF_M_SYM ELF64_M_SYM
#define ELF_M_SIZE ELF64_M_SIZE
#define ELF_M_INFO ELF64_M_INFO
#define PFMTnx PFMT64x
#define PFMTnX PFMT64X
#define PFMTnd PFMT64d
#define PFMTnu PFMT64u
#define PFMTno PFMT64o
#else
#define Elf_(name) Elf32_##name
#define ELF_ST_BIND ELF32_ST_BIND
@ -40,6 +46,12 @@
#define ELF_M_SYM ELF32_M_SYM
#define ELF_M_SIZE ELF32_M_SIZE
#define ELF_M_INFO ELF32_M_INFO
#define PFMTnx PFMT32x
#define PFMTnX PFMT32X
#define PFMTnd PFMT32d
#define PFMTnu PFMT32u
#define PFMTno PFMT32o
#endif
/**

View file

@ -549,6 +549,7 @@ static inline void *rz_new_copy(int size, const void *data) {
#define PFMTDPTR "td"
#define PFMT32x "x"
#define PFMT32X "X"
#define PFMT32d "d"
#define PFMT32u "u"
#define PFMT32o "o"

View file

@ -392,87 +392,87 @@ fsr = 0x0000000000000000
EOF
EXPECT_ERR=<<EOF
WARNING: Number of sections is 0.
WARNING: phdr entry: p_offset and p_vaddr are not congruent for p_align.
WARNING: phdr entry: p_offset (0xd34) and p_vaddr (0xa06ed00000) are not congruent for p_align (0x2000).
WARNING: The segment 0 at 0x40 seems to be invalid.
WARNING: phdr entry: p_offset and p_vaddr are not congruent for p_align.
WARNING: phdr entry: p_offset (0x2d34) and p_vaddr (0xa06ef02000) are not congruent for p_align (0x2000).
WARNING: The segment 1 at 0x78 seems to be invalid.
WARNING: phdr entry: p_offset and p_vaddr are not congruent for p_align.
WARNING: phdr entry: p_offset (0x4d34) and p_vaddr (0xa06f002000) are not congruent for p_align (0x2000).
WARNING: The segment 2 at 0xb0 seems to be invalid.
WARNING: phdr entry: p_offset and p_vaddr are not congruent for p_align.
WARNING: phdr entry: p_offset (0x6d34) and p_vaddr (0xa06f004000) are not congruent for p_align (0x2000).
WARNING: The segment 3 at 0xe8 seems to be invalid.
WARNING: phdr entry: p_offset and p_vaddr are not congruent for p_align.
WARNING: phdr entry: p_offset (0x8d34) and p_vaddr (0xa27aca4000) are not congruent for p_align (0x2000).
WARNING: The segment 4 at 0x120 seems to be invalid.
WARNING: phdr entry: p_offset and p_vaddr are not congruent for p_align.
WARNING: phdr entry: p_offset (0xad34) and p_vaddr (0xa27e872000) are not congruent for p_align (0x2000).
WARNING: The segment 5 at 0x158 seems to be invalid.
WARNING: phdr entry: p_offset and p_vaddr are not congruent for p_align.
WARNING: phdr entry: p_offset (0xcd34) and p_vaddr (0xa281006000) are not congruent for p_align (0x2000).
WARNING: The segment 6 at 0x190 seems to be invalid.
WARNING: phdr entry: p_offset and p_vaddr are not congruent for p_align.
WARNING: phdr entry: p_offset (0xed34) and p_vaddr (0xa2837c0000) are not congruent for p_align (0x2000).
WARNING: The segment 7 at 0x1c8 seems to be invalid.
WARNING: phdr entry: p_offset and p_vaddr are not congruent for p_align.
WARNING: phdr entry: p_offset (0x10d34) and p_vaddr (0xa28d9f4000) are not congruent for p_align (0x2000).
WARNING: The segment 8 at 0x200 seems to be invalid.
WARNING: phdr entry: p_offset and p_vaddr are not congruent for p_align.
WARNING: phdr entry: p_offset (0x12d34) and p_vaddr (0xa297b68000) are not congruent for p_align (0x2000).
WARNING: The segment 9 at 0x238 seems to be invalid.
WARNING: phdr entry: p_offset and p_vaddr are not congruent for p_align.
WARNING: phdr entry: p_offset (0x14d34) and p_vaddr (0xa2aefe2000) are not congruent for p_align (0x2000).
WARNING: The segment 10 at 0x270 seems to be invalid.
WARNING: phdr entry: p_offset and p_vaddr are not congruent for p_align.
WARNING: phdr entry: p_offset (0x16d34) and p_vaddr (0xa2be486000) are not congruent for p_align (0x2000).
WARNING: The segment 11 at 0x2a8 seems to be invalid.
WARNING: phdr entry: p_offset and p_vaddr are not congruent for p_align.
WARNING: phdr entry: p_offset (0x18d34) and p_vaddr (0xa2d24f6000) are not congruent for p_align (0x2000).
WARNING: The segment 12 at 0x2e0 seems to be invalid.
WARNING: phdr entry: p_offset and p_vaddr are not congruent for p_align.
WARNING: phdr entry: p_offset (0x1ad34) and p_vaddr (0xa2d776e000) are not congruent for p_align (0x2000).
WARNING: The segment 13 at 0x318 seems to be invalid.
WARNING: phdr entry: p_offset and p_vaddr are not congruent for p_align.
WARNING: phdr entry: p_offset (0x1cd34) and p_vaddr (0xa2e984e000) are not congruent for p_align (0x2000).
WARNING: The segment 14 at 0x350 seems to be invalid.
WARNING: phdr entry: p_offset and p_vaddr are not congruent for p_align.
WARNING: phdr entry: p_offset (0x1ed34) and p_vaddr (0xa2eb6d0000) are not congruent for p_align (0x2000).
WARNING: The segment 15 at 0x388 seems to be invalid.
WARNING: phdr entry: p_offset and p_vaddr are not congruent for p_align.
WARNING: phdr entry: p_offset (0x20d34) and p_vaddr (0xa2ec398000) are not congruent for p_align (0x2000).
WARNING: The segment 16 at 0x3c0 seems to be invalid.
WARNING: phdr entry: p_offset and p_vaddr are not congruent for p_align.
WARNING: phdr entry: p_offset (0x22d34) and p_vaddr (0xa2f0d2c000) are not congruent for p_align (0x2000).
WARNING: The segment 17 at 0x3f8 seems to be invalid.
WARNING: phdr entry: p_offset and p_vaddr are not congruent for p_align.
WARNING: phdr entry: p_offset (0x24d34) and p_vaddr (0xa305c66000) are not congruent for p_align (0x2000).
WARNING: The segment 18 at 0x430 seems to be invalid.
WARNING: phdr entry: p_offset and p_vaddr are not congruent for p_align.
WARNING: phdr entry: p_offset (0x26d34) and p_vaddr (0xa312baa000) are not congruent for p_align (0x2000).
WARNING: The segment 19 at 0x468 seems to be invalid.
WARNING: phdr entry: p_offset and p_vaddr are not congruent for p_align.
WARNING: phdr entry: p_offset (0x28d34) and p_vaddr (0xa313776000) are not congruent for p_align (0x2000).
WARNING: The segment 20 at 0x4a0 seems to be invalid.
WARNING: phdr entry: p_offset and p_vaddr are not congruent for p_align.
WARNING: phdr entry: p_offset (0x2ad34) and p_vaddr (0xa3141ae000) are not congruent for p_align (0x2000).
WARNING: The segment 21 at 0x4d8 seems to be invalid.
WARNING: phdr entry: p_offset and p_vaddr are not congruent for p_align.
WARNING: phdr entry: p_offset (0x2cd34) and p_vaddr (0xa3272d8000) are not congruent for p_align (0x2000).
WARNING: The segment 22 at 0x510 seems to be invalid.
WARNING: phdr entry: p_offset and p_vaddr are not congruent for p_align.
WARNING: phdr entry: p_offset (0x2ed34) and p_vaddr (0xa32880c000) are not congruent for p_align (0x2000).
WARNING: The segment 23 at 0x548 seems to be invalid.
WARNING: phdr entry: p_offset and p_vaddr are not congruent for p_align.
WARNING: phdr entry: p_offset (0x30d34) and p_vaddr (0xa32880e000) are not congruent for p_align (0x2000).
WARNING: The segment 24 at 0x580 seems to be invalid.
WARNING: phdr entry: p_offset and p_vaddr are not congruent for p_align.
WARNING: phdr entry: p_offset (0x222d34) and p_vaddr (0xa328a00000) are not congruent for p_align (0x2000).
WARNING: The segment 25 at 0x5b8 seems to be invalid.
WARNING: phdr entry: p_offset and p_vaddr are not congruent for p_align.
WARNING: phdr entry: p_offset (0x224d34) and p_vaddr (0xa34a0ec000) are not congruent for p_align (0x2000).
WARNING: The segment 26 at 0x5f0 seems to be invalid.
WARNING: phdr entry: p_offset and p_vaddr are not congruent for p_align.
WARNING: phdr entry: p_offset (0x226d34) and p_vaddr (0xa352702000) are not congruent for p_align (0x2000).
WARNING: The segment 27 at 0x628 seems to be invalid.
WARNING: phdr entry: p_offset and p_vaddr are not congruent for p_align.
WARNING: phdr entry: p_offset (0x228d34) and p_vaddr (0xa352802000) are not congruent for p_align (0x2000).
WARNING: The segment 28 at 0x660 seems to be invalid.
WARNING: phdr entry: p_offset and p_vaddr are not congruent for p_align.
WARNING: phdr entry: p_offset (0x22ad34) and p_vaddr (0xa352804000) are not congruent for p_align (0x2000).
WARNING: The segment 29 at 0x698 seems to be invalid.
WARNING: phdr entry: p_offset and p_vaddr are not congruent for p_align.
WARNING: phdr entry: p_offset (0x230d34) and p_vaddr (0xa35280a000) are not congruent for p_align (0x2000).
WARNING: The segment 30 at 0x6d0 seems to be invalid.
WARNING: phdr entry: p_offset and p_vaddr are not congruent for p_align.
WARNING: phdr entry: p_offset (0x232d34) and p_vaddr (0xa35280c000) are not congruent for p_align (0x2000).
WARNING: The segment 31 at 0x708 seems to be invalid.
WARNING: phdr entry: p_offset and p_vaddr are not congruent for p_align.
WARNING: phdr entry: p_offset (0x234d34) and p_vaddr (0xa35280e000) are not congruent for p_align (0x2000).
WARNING: The segment 32 at 0x740 seems to be invalid.
WARNING: phdr entry: p_offset and p_vaddr are not congruent for p_align.
WARNING: phdr entry: p_offset (0x234d34) and p_vaddr (0xa352810000) are not congruent for p_align (0x2000).
WARNING: The segment 33 at 0x778 seems to be invalid.
WARNING: phdr entry: p_offset and p_vaddr are not congruent for p_align.
WARNING: phdr entry: p_offset (0x236d34) and p_vaddr (0xa35281a000) are not congruent for p_align (0x2000).
WARNING: The segment 34 at 0x7b0 seems to be invalid.
WARNING: phdr entry: p_offset and p_vaddr are not congruent for p_align.
WARNING: phdr entry: p_offset (0x238d34) and p_vaddr (0xa353e36000) are not congruent for p_align (0x2000).
WARNING: The segment 35 at 0x7e8 seems to be invalid.
WARNING: phdr entry: p_offset and p_vaddr are not congruent for p_align.
WARNING: phdr entry: p_offset (0x23ad34) and p_vaddr (0xa357a0c000) are not congruent for p_align (0x2000).
WARNING: The segment 36 at 0x820 seems to be invalid.
WARNING: phdr entry: p_offset and p_vaddr are not congruent for p_align.
WARNING: phdr entry: p_offset (0x23cd34) and p_vaddr (0xa358216000) are not congruent for p_align (0x2000).
WARNING: The segment 37 at 0x858 seems to be invalid.
WARNING: phdr entry: p_offset and p_vaddr are not congruent for p_align.
WARNING: phdr entry: p_offset (0x23ed34) and p_vaddr (0xa35b7c0000) are not congruent for p_align (0x2000).
WARNING: The segment 38 at 0x890 seems to be invalid.
WARNING: phdr entry: p_offset and p_vaddr are not congruent for p_align.
WARNING: phdr entry: p_offset (0x240d34) and p_vaddr (0xfffffe849aaaa000) are not congruent for p_align (0x2000).
WARNING: The segment 39 at 0x8c8 seems to be invalid.
WARNING: phdr entry: p_offset and p_vaddr are not congruent for p_align.
WARNING: phdr entry: p_offset (0x240d34) and p_vaddr (0xfffffe849aea4000) are not congruent for p_align (0x2000).
WARNING: The segment 40 at 0x900 seems to be invalid.
EOF
RUN

View file

@ -150,6 +150,30 @@ WARNING: Failed to initialize section string table.
EOF
RUN
NAME=elf warnings
FILE=bins/elf/analysis/tiny.elf
CMDS=iSS
EXPECT=<<EOF
paddr size vaddr vsize align perm name
------------------------------------------------------------
0x00000000 0x10020 0x00010000 0x10020 0xc0312ab3 -r-- LOAD0
0x00000000 0x2d 0x00010000 0x2d 0x0 -rw- ehdr
EOF
EXPECT_ERR=<<EOF
WARNING: Failed to read ELF header (e_phnum).
WARNING: Failed to read ELF header (e_shentsize).
WARNING: Failed to read ELF header (e_shnum).
WARNING: Failed to read ELF header (e_shstrndx).
WARNING: The binary seems to be a tiny elf (45 bytes). Reload e_phnum value.
WARNING: Number of sections is 0.
WARNING: phdr entry: p_filesz (65568 bytes) > binary size (45 bytes).
WARNING: phdr entry: p_offset (0x0) and p_vaddr (0x10000) are not congruent for p_align (0xc0312ab3).
WARNING: The segment 0 at 0x4 seems to be invalid.
WARNING: ELF header e_shstrndx value (0) >= number of sections (0).
WARNING: Failed to initialize section string table.
EOF
RUN
NAME=disable segments checks
FILE=bins/elf/analysis/tiny.elf
ARGS=-e elf.checks.segments=false