-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkaslr.h
76 lines (55 loc) · 2.44 KB
/
kaslr.h
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
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <stdint.h>
#include <stdio.h>
// choose_random_location is the function set KASLR
// - find_random_phys_addr for phys_map
// - find_random_virt_addr for kernel
// There is less than 9 bits randomization for kernel image
// The following code comes from https://elixir.bootlin.com/linux/v6.11/source/arch/x86/boot/compressed/kaslr.c#L796
// which enables KASLR
// static unsigned long find_random_virt_addr(unsigned long minimum,
// unsigned long image_size)
// {
// unsigned long slots, random_addr;
// /*
// * There are how many CONFIG_PHYSICAL_ALIGN-sized slots
// * that can hold image_size within the range of minimum to
// * KERNEL_IMAGE_SIZE?
// */
// slots = 1 + (KERNEL_IMAGE_SIZE - minimum - image_size) / CONFIG_PHYSICAL_ALIGN;
// random_addr = kaslr_get_random_long("Virtual") % slots;
// return random_addr * CONFIG_PHYSICAL_ALIGN + minimum;
// }
// From the code, we have:
/*
- KERNEL_IMAGE_SIZE = 0x40000000 (CONFIG_RANDOMIZE_BASE=y)
- minimum = 0x1000000
- CONFIG_PHYSICAL_ALIGN = 0x0x200000
- img_size is depedned on the image loaded
------------------------------------------------------
slots <- 1+(0x40000000-0x1000000-img_size) / 0x200000
if
img_size > 0
then
slots < 505
There is not much randomazation in Kernel Text
*/
// Do the similar computing for Phys_map area and found it has 16-bit randomization
// 2M
#define STEP 0x200000ull // CONFIG_PHYSICAL_ALIGN=0x200000
#define KERNEL_LOWER_BOUND 0xffffffff80000000ull
#define KERNEL_UPPER_BOUND 0xffffffffc0000000ull
#define entry_SYSCALL_64_offset 0x1400000ull
#define SCAN_START KERNEL_LOWER_BOUND
#define SCAN_END KERNEL_UPPER_BOUND
#define ARR_SIZE (SCAN_END - SCAN_START) / STEP
// 0x40000000 == 1GB which is slot_areas's step
// 1GB
#define STEP_PHYS 0x40000000ull
#define PHYS_LOWER_BOUND 0xffff887000000000ull
// Assume the target has less than 1T RAM
#define PHYS_UPPER_BOUND 0xffffa45555555555ull
#define SCAN_START_PHYS PHYS_LOWER_BOUND
#define SCAN_END_PHYS PHYS_UPPER_BOUND
#define ARR_SIZE_PHYS (SCAN_END_PHYS - SCAN_START_PHYS) / STEP_PHYS
#define DUMMY_ITERATIONS 5ull
#define ITERATIONS 100ull