Skip to content

Commit 88ed08d

Browse files
committed
Fix elf64DynMode.
Since Ubuntu 22.04 Address Space Layout Randomization is active, so it's needed to be taken into account when you want to hook dynamic functions.
1 parent 7f57594 commit 88ed08d

File tree

4 files changed

+15
-6
lines changed

4 files changed

+15
-6
lines changed

cTools/libs/binDynMod/elfDynMod/elf32DynMod.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ void *elf32Hook(const Elf32File *elf32, const char *func, const void *hand)
5555
}
5656

5757
uint32_t relpltAmount = relplt->sh_size / sizeof(Elf32Rel);
58+
Elf32Sym *hook_sym = elf32GetSymByName(elf32, "elf32Hook");
59+
uint32_t func_original_addr = elf32GetSSymAddr(hook_sym);
60+
uint32_t func_addr_diff = (uint32_t)(size_t)(void*)&elf32Hook - func_original_addr;
5861

5962
/***
6063
* r_info - This member gives both the symbol table index,
@@ -71,9 +74,10 @@ void *elf32Hook(const Elf32File *elf32, const char *func, const void *hand)
7174
uint32_t i = 0;
7275
for (i = 0; i < relpltAmount; ++i)
7376
if (ELF32_R_SYM(elf32->relaplt[i].r_info) == symbolIndex){
74-
// !TODO: need refactor
75-
relAddr = (void*) (size_t)*(uint32_t*) (size_t)elf32->relaplt[i].r_offset;
76-
*(uint32_t*) (size_t)(elf32->relaplt[i].r_offset) = (uint32_t)(uint64_t) hand;
77+
uint32_t offset = elf32->relaplt[i].r_offset;
78+
uint32_t* addr = (uint32_t*)(size_t)(func_addr_diff + offset);
79+
relAddr = (void*)(size_t) *addr;
80+
*addr = (uint32_t)(size_t) hand;
7781

7882
return relAddr;
7983
}

cTools/libs/binDynMod/elfDynMod/elf64DynMod.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ void *elf64Hook(const Elf64File *elf64, const char *func, const void *hand)
5555
}
5656

5757
uint64_t relpltAmount = relplt->sh_size / sizeof(Elf64Rel);
58+
Elf64Sym *hook_sym = elf64GetSymByName(elf64, "elf64Hook");
59+
uint64_t func_original_addr = elf64GetSSymAddr(hook_sym);
60+
uint64_t func_addr_diff = (uint64_t)&elf64Hook - func_original_addr;
5861

5962
/***
6063
* r_info - This member gives both the symbol table index,
@@ -71,8 +74,10 @@ void *elf64Hook(const Elf64File *elf64, const char *func, const void *hand)
7174
uint64_t i = 0;
7275
for (i = 0; i < relpltAmount; ++i)
7376
if (ELF64_R_SYM(elf64->relaplt[i].r_info) == symbolIndex){
74-
relAddr = (void*) *(uint64_t*) elf64->relaplt[i].r_offset;
75-
*(uint64_t*) (elf64->relaplt[i].r_offset) = (uint64_t) hand;
77+
uint64_t offset = elf64->relaplt[i].r_offset;
78+
uint64_t* addr = (uint64_t*)(func_addr_diff + offset);
79+
relAddr = (void*) *addr;
80+
*addr = (uint64_t) hand;
7681

7782
return relAddr;
7883
}

shellTools/tools/vim/install.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
# SOFTWARE.
2424

2525
SCRIPT_DIR="$(realpath $(dirname "${BASH_SOURCE[0]}"))"
26-
source "${SCRIPT_DIR}/../../os/libos.sh"
26+
source "${SCRIPT_DIR}/../../os/libOs.sh"
2727

2828
ROOT="$(realpath $(dirname "${BASH_SOURCE[0]}"))"
2929
BUILD_DIR="${ROOT}/vim/src"

0 commit comments

Comments
 (0)