Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: crash in postject_find_resource() on Linux #77

Merged
merged 5 commits into from
Mar 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions postject-api.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#elif defined(__linux__)
#include <elf.h>
#include <link.h>
#include <sys/auxv.h>
#include <sys/param.h>
#elif defined(_WIN32)
#include <windows.h>
Expand Down Expand Up @@ -44,6 +43,16 @@ static inline bool postject_has_resource() {
return sentinel[sizeof(POSTJECT_SENTINEL_FUSE)] == '1';
}

#if defined(__linux__)
static int postject__dl_iterate_phdr_callback(struct dl_phdr_info* info,
size_t size,
void* data) {
// Snag the dl_phdr_info struct for the main program, then stop iterating
*((struct dl_phdr_info*)data) = *info;
return 1;
}
#endif

static const void* postject_find_resource(
const char* name,
size_t* size,
Expand Down Expand Up @@ -114,9 +123,12 @@ static const void* postject_find_resource(
name = options->elf_section_name;
}

uintptr_t p = getauxval(AT_PHDR);
size_t n = getauxval(AT_PHNUM);
uintptr_t base_addr = p - sizeof(ElfW(Ehdr));
struct dl_phdr_info main_program_info;
dl_iterate_phdr(postject__dl_iterate_phdr_callback, &main_program_info);

uintptr_t p = (uintptr_t)main_program_info.dlpi_phdr;
size_t n = main_program_info.dlpi_phnum;
uintptr_t base_addr = main_program_info.dlpi_addr;

// iterate program header
for (; n > 0; n--, p += sizeof(ElfW(Phdr))) {
Expand Down
4 changes: 4 additions & 0 deletions test/test.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#define _GNU_SOURCE // This is needed because postject-api.h uses
// dl_iterate_phdr and dl_phdr_info which are non-standard
// GNU extensions.

#include <stdio.h>
#include <string.h>

Expand Down