-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHotpatching.c
More file actions
64 lines (55 loc) · 1.32 KB
/
Copy pathHotpatching.c
File metadata and controls
64 lines (55 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <stdio.h>
#include <pthread.h>
#include <stdint.h>
#include <sys/mman.h>
#include <assert.h>
#include <unistd.h>
__attribute__ ((ms_hook_prologue))
__attribute__ ((aligned(8)))
__attribute__ ((noinline))
__attribute__ ((noclone))
void hello(void)
{
__asm("");
puts("I've entered the function.");
}
__attribute__ ((ms_hook_prologue))
__attribute__ ((aligned(8)))
__attribute__ ((noinline))
__attribute__ ((noclone))
void new_hello(void)
{
static int x;
printf("Hotpatched!, Updation of function on the run. %d\n",x);
}
void hotpatch(void *target, void *replacement)
{
assert(((uintptr_t)target & 0x07) == 0); // 8-byte aligned?
void *page = (void *)((uintptr_t)target & ~0xfff);
mprotect(page, 4096, PROT_WRITE | PROT_EXEC);
uint32_t rel = (char *)replacement - (char *)target - 5;
union {
uint8_t bytes[8];
uint64_t value;
} instruction = { {0xe9, rel >> 0, rel >> 8, rel >> 16, rel >> 24} };
*(uint64_t *)target = instruction.value;
mprotect(page, 4096, PROT_EXEC);
}
void *worker(void *arg)
{
(void)arg;
for (;;) {
hello();
usleep(100000);
}
return NULL;
}
int main(void)
{
pthread_t thread;
pthread_create(&thread, NULL, worker, NULL);
getchar();
hotpatch(hello, new_hello);
pthread_join(thread, NULL);
return 0;
}