Skip to content

Commit e6f79ac

Browse files
authored
fix: crash in postject_find_resource() on Linux (#77)
* 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]>
1 parent 2b88c33 commit e6f79ac

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

postject-api.h

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#elif defined(__linux__)
1313
#include <elf.h>
1414
#include <link.h>
15-
#include <sys/auxv.h>
1615
#include <sys/param.h>
1716
#elif defined(_WIN32)
1817
#include <windows.h>
@@ -44,6 +43,16 @@ static inline bool postject_has_resource() {
4443
return sentinel[sizeof(POSTJECT_SENTINEL_FUSE)] == '1';
4544
}
4645

46+
#if defined(__linux__)
47+
static int postject__dl_iterate_phdr_callback(struct dl_phdr_info* info,
48+
size_t size,
49+
void* data) {
50+
// Snag the dl_phdr_info struct for the main program, then stop iterating
51+
*((struct dl_phdr_info*)data) = *info;
52+
return 1;
53+
}
54+
#endif
55+
4756
static const void* postject_find_resource(
4857
const char* name,
4958
size_t* size,
@@ -114,9 +123,12 @@ static const void* postject_find_resource(
114123
name = options->elf_section_name;
115124
}
116125

117-
uintptr_t p = getauxval(AT_PHDR);
118-
size_t n = getauxval(AT_PHNUM);
119-
uintptr_t base_addr = p - sizeof(ElfW(Ehdr));
126+
struct dl_phdr_info main_program_info;
127+
dl_iterate_phdr(postject__dl_iterate_phdr_callback, &main_program_info);
128+
129+
uintptr_t p = (uintptr_t)main_program_info.dlpi_phdr;
130+
size_t n = main_program_info.dlpi_phnum;
131+
uintptr_t base_addr = main_program_info.dlpi_addr;
120132

121133
// iterate program header
122134
for (; n > 0; n--, p += sizeof(ElfW(Phdr))) {

test/test.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
#define _GNU_SOURCE // This is needed because postject-api.h uses
2+
// dl_iterate_phdr and dl_phdr_info which are non-standard
3+
// GNU extensions.
4+
15
#include <stdio.h>
26
#include <string.h>
37

0 commit comments

Comments
 (0)