Skip to content

Commit

Permalink
fix: crash in postject_find_resource() on Linux (#77)
Browse files Browse the repository at this point in the history
* fix: crash in `postject_find_resource()` on Linux

The program headers base address values returned by `getauxval(AT_PHDR)`
and `dl_iterate_phdr()` are identical only on
`g++ (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0`. However, the values are
totally different on `clang version 10.0.0-4ubuntu1` and
`g++ (GCC) 8.5.0 20210514 (Red Hat 8.5.0-16)`.
Since the `dl_iterate_phdr()` approach seems to work for all the 3
compilers, I think we should proceed with that.

Fixes: #70
Refs: #76
Signed-off-by: Darshan Sen <[email protected]>

* chore: remove unnecessary if block

Signed-off-by: Darshan Sen <[email protected]>

* fix: only iterate the main executable program headers

The resource gets injected in the main executable, so there is no need
to iterate the other shared libraries that are loaded by the program.
This also resolves a security concern.

Refs: #77 (review)
Signed-off-by: Darshan Sen <[email protected]>

* chore: shorten change

Signed-off-by: Darshan Sen <[email protected]>

* test: add comment about _GNU_SOURCE

Signed-off-by: Darshan Sen <[email protected]>

---------

Signed-off-by: Darshan Sen <[email protected]>
  • Loading branch information
RaisinTen authored Mar 3, 2023
1 parent 2b88c33 commit e6f79ac
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
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

0 comments on commit e6f79ac

Please sign in to comment.