From 46b4fe7f055b9283a245d75a9ad4c0f04311122a Mon Sep 17 00:00:00 2001 From: KoHaRxnP Date: Wed, 5 Aug 2026 18:07:29 +0900 Subject: [PATCH 1/2] fix: ELF note section parsing alignment and name length comparison --- postject-api.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/postject-api.h b/postject-api.h index 875ddc3..ce75f0b 100644 --- a/postject-api.h +++ b/postject-api.h @@ -206,4 +206,4 @@ static const void* postject_find_resource( #endif } -#endif // POSTJECT_API_H_ +#endif // POSTJECT_API_H_ \ No newline at end of file From 76339f85552225c808b7fbddf361c2686fe74dcf Mon Sep 17 00:00:00 2001 From: KoHaRxnP Date: Wed, 5 Aug 2026 18:12:05 +0900 Subject: [PATCH 2/2] fix: ELF note section parsing alignment and name length comparison --- postject-api.h | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/postject-api.h b/postject-api.h index ce75f0b..4a5173b 100644 --- a/postject-api.h +++ b/postject-api.h @@ -130,6 +130,8 @@ static const void* postject_find_resource( size_t n = main_program_info.dlpi_phnum; uintptr_t base_addr = main_program_info.dlpi_addr; + size_t name_len = strlen(name); + // iterate program header for (; n > 0; n--, p += sizeof(ElfW(Phdr))) { ElfW(Phdr)* phdr = (ElfW(Phdr)*)p; @@ -139,6 +141,10 @@ static const void* postject_find_resource( continue; } + // Determine alignment for notes in this segment (default to ELF class word size if p_align is 0/1) + size_t align = phdr->p_align > 0 ? phdr->p_align : sizeof(ElfW(Addr)); + if (align < 4) align = 4; // ELF Note standard minimum alignment is usually 4 + // note segment starts at base address + segment virtual address uintptr_t pos = (base_addr + phdr->p_vaddr); uintptr_t end = (pos + phdr->p_memsz); @@ -150,18 +156,20 @@ static const void* postject_find_resource( } ElfW(Nhdr)* note = (ElfW(Nhdr)*)(uintptr_t)pos; + + // Calculate padded offsets dynamically based on segment alignment + size_t namesz_padded = roundup(note->n_namesz, align); + size_t descsz_padded = roundup(note->n_descsz, align); + if (note->n_namesz != 0 && note->n_descsz != 0 && - strncmp((char*)(pos + sizeof(ElfW(Nhdr))), (char*)name, - sizeof(name)) == 0) { + note->n_namesz >= name_len && + strncmp((char*)(pos + sizeof(ElfW(Nhdr))), (char*)name, name_len) == 0) { *size = note->n_descsz; - // advance past note header and aligned name - // to get to description data - return (void*)((uintptr_t)note + sizeof(ElfW(Nhdr)) + - roundup(note->n_namesz, 4)); + // advance past note header and aligned name to get to description data + return (void*)((uintptr_t)note + sizeof(ElfW(Nhdr)) + namesz_padded); } - pos += (sizeof(ElfW(Nhdr)) + roundup(note->n_namesz, 4) + - roundup(note->n_descsz, 4)); + pos += (sizeof(ElfW(Nhdr)) + namesz_padded + descsz_padded); } } return NULL;