// SPDX-License-Identifier: GPL-2.0 /* * synth_test.c - userspace driver for the emux race verification harness. * * Plans: * load [secs] - open /dev/snd/hwC0D0 and load a soundfont through the * REAL hwdep SNDRV_EMUX_IOCTL_LOAD_PATCH path * (OPEN_PATCH + LOAD_DATA + LOAD_INFO), then keep the * fd open. Writes /tmp/synth_loaded when done. * reload [secs] - same, but keeps re-issuing LOAD_DATA+LOAD_INFO in a * loop (font gets eaten by the kernel-side removal * hammer in plan mr01nat). * mr03 [iters] - open hwdep and hammer ioctls (VERSION / MEM_AVAIL / * MISC_MODE) - the in-flight side of SYNTH-MR-03. * mr04 [iters] - hammer open+read+close on * /proc/asound/card0/wavetableD0 - the reader side of * SYNTH-MR-04. * mr04hold - open the proc file, print "HOLD", sleep 3s (the host * script frees emux in between), then read again - * delayed-UAF variant of SYNTH-MR-04. * mr05 - poll for /dev/snd/hwC0D0 (probe-window race), open it * and issue SNDRV_EMUX_IOCTL_MISC_MODE - the NULL-deref * side of SYNTH-MR-05. */ #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #include "include/sound/sfnt_info.h" /* ---- soundfont binary construction (wire format of * sound/synth/emux/soundfont.c) ---- */ static void patch_hdr(unsigned char *p, int type, int len, int optarg) { struct soundfont_patch_info h; memset(&h, 0, sizeof(h)); h.key = SNDRV_OSS_SOUNDFONT_PATCH; h.device_no = 0; h.sf_id = 0; h.optarg = optarg; h.len = len; h.type = type; memcpy(p, &h, sizeof(h)); } static int sf_load(int fd) { unsigned char buf[512]; struct soundfont_open_parm parm; struct soundfont_sample_info si; struct soundfont_voice_rec_hdr rh; struct soundfont_voice_info vi; int rc; /* OPEN_PATCH */ memset(&parm, 0, sizeof(parm)); parm.type = SNDRV_SFNT_PAT_TYPE_MISC | SNDRV_SFNT_PAT_SHARED; strcpy(parm.name, "synthrace"); patch_hdr(buf, SNDRV_SFNT_OPEN_PATCH, sizeof(parm), 0); memcpy(buf + sizeof(struct soundfont_patch_info), &parm, sizeof(parm)); rc = ioctl(fd, SNDRV_EMUX_IOCTL_LOAD_PATCH, buf); if (rc < 0) { /* font already open from an earlier round */ if (errno == EBUSY) return 0; fprintf(stderr, "LOAD_PATCH OPEN: %s\n", strerror(errno)); return -1; } /* LOAD_DATA: 1 sample, 64 s16le frames */ memset(&si, 0, sizeof(si)); si.sample = 1; si.start = 0; si.end = 64; si.loopstart = 8; si.loopend = 56; si.size = 64; si.mode_flags = SNDRV_SFNT_SAMPLE_NO_BLANK; patch_hdr(buf, SNDRV_SFNT_LOAD_DATA, sizeof(si) + si.size * 2, 0); memcpy(buf + sizeof(struct soundfont_patch_info), &si, sizeof(si)); memset(buf + sizeof(struct soundfont_patch_info) + sizeof(si), 0x7f, si.size * 2); rc = ioctl(fd, SNDRV_EMUX_IOCTL_LOAD_PATCH, buf); if (rc < 0) { fprintf(stderr, "LOAD_PATCH DATA: %s\n", strerror(errno)); return -1; } /* LOAD_INFO: one zone, bank 0 / instr 0, note 0..127, vel 0..127, * exclusiveClass = 1, tied to sample 1 */ memset(&vi, 0, sizeof(vi)); vi.sf_id = 0; vi.sample = 1; vi.root = 60; vi.high = 127; vi.velhigh = 127; vi.fixkey = -1; vi.fixvel = -1; vi.fixpan = -1; vi.pan = -1; vi.amplitude = 127; vi.scaleTuning = 100; vi.exclusiveClass = 1; memset(&rh, 0, sizeof(rh)); rh.bank = 0; rh.instr = 0; rh.nvoices = 1; rh.write_mode = SNDRV_SFNT_WR_APPEND; patch_hdr(buf, SNDRV_SFNT_LOAD_INFO, sizeof(rh) + sizeof(vi), 0); memcpy(buf + sizeof(struct soundfont_patch_info), &rh, sizeof(rh)); memcpy(buf + sizeof(struct soundfont_patch_info) + sizeof(rh), &vi, sizeof(vi)); rc = ioctl(fd, SNDRV_EMUX_IOCTL_LOAD_PATCH, buf); if (rc < 0) { fprintf(stderr, "LOAD_PATCH INFO: %s\n", strerror(errno)); return -1; } /* CLOSE_PATCH -> close_patch() -> rebuild_presets(): zones loaded via * LOAD_INFO only enter the preset table at close time (this is the * real sfxload sequence), so note_on's search_zones() can find them */ patch_hdr(buf, SNDRV_SFNT_CLOSE_PATCH, 0, 0); rc = ioctl(fd, SNDRV_EMUX_IOCTL_LOAD_PATCH, buf); if (rc < 0) { fprintf(stderr, "LOAD_PATCH CLOSE: %s\n", strerror(errno)); return -1; } return 0; } static int plan_load(int secs, int reload) { int fd, n = 0; unsigned long until = (unsigned long)time(NULL) + (secs > 0 ? secs : 30); fd = open("/dev/snd/hwC0D0", O_RDWR); if (fd < 0) { fprintf(stderr, "open hwC0D0: %s\n", strerror(errno)); return 1; } if (sf_load(fd) < 0) { close(fd); return 1; } printf("LOAD: font ready\n"); fflush(stdout); int tf = open("/tmp/synth_loaded", O_WRONLY | O_CREAT | O_TRUNC, 0644); if (tf >= 0) close(tf); while (reload && (unsigned long)time(NULL) < until) { /* re-load data+info; kernel-side hammer eats the font */ if (sf_load(fd) < 0) break; n++; usleep(2000); } printf("LOAD: done (%d reloads)\n", n); /* keep the fd open until the plan ends (exclusive hwdep) */ sleep(secs > 0 ? secs : 30); close(fd); return 0; } static int plan_mr03(int iters) { int fd, i; struct snd_emux_misc_mode mm; unsigned int ver = 0; int avail = 0; fd = open("/dev/snd/hwC0D0", O_RDWR); if (fd < 0) { fprintf(stderr, "open hwC0D0: %s\n", strerror(errno)); return 1; } printf("MR03: hammering %d ioctl iterations\n", iters); fflush(stdout); for (i = 0; i < iters; i++) { ioctl(fd, SNDRV_EMUX_IOCTL_VERSION, &ver); ioctl(fd, SNDRV_EMUX_IOCTL_MEM_AVAIL, &avail); memset(&mm, 0, sizeof(mm)); mm.port = -1; mm.mode = 0; mm.value = 0; ioctl(fd, SNDRV_EMUX_IOCTL_MISC_MODE, &mm); if ((i % 128) == 0) { printf("MR03: iter %d\n", i); fflush(stdout); } usleep(100); } printf("MR03: closing\n"); fflush(stdout); close(fd); sleep(2); close(open("/dev/snd/hwC0D0", O_RDWR)); /* delayed variant: reopen */ printf("MR03: done\n"); return 0; } static int plan_mr04(int iters) { int i; printf("MR04: hammering %d proc-read iterations\n", iters); fflush(stdout); for (i = 0; i < iters; i++) { char buf[4096]; int fd = open("/proc/asound/card0/wavetableD0", O_RDONLY); if (fd >= 0) { ssize_t r = read(fd, buf, sizeof(buf)); (void)r; close(fd); } if ((i % 128) == 0) { printf("MR04: iter %d\n", i); fflush(stdout); } usleep(500); } printf("MR04: done\n"); return 0; } static int plan_mr04hold(void) { char buf[4096]; int fd; fd = open("/proc/asound/card0/wavetableD0", O_RDONLY); if (fd < 0) { fprintf(stderr, "open proc: %s\n", strerror(errno)); return 1; } printf("MR04HOLD: fd open, reading once\n"); fflush(stdout); read(fd, buf, sizeof(buf)); printf("MR04HOLD: holding fd for 4s (free happens now)\n"); fflush(stdout); sleep(4); off_t ls = lseek(fd, 0, SEEK_SET); printf("MR04HOLD: delayed read on stale fd (lseek=%ld)\n", (long)ls); fflush(stdout); ssize_t rr = read(fd, buf, sizeof(buf)); printf("MR04HOLD: stale read rc=%zd errno=%d\n", rr, rr < 0 ? errno : 0); fflush(stdout); printf("MR04HOLD: done\n"); close(fd); return 0; } static int plan_mr05(void) { int fd, tries; struct snd_emux_misc_mode mm; for (tries = 0; tries < 5000; tries++) { struct stat st; if (stat("/dev/snd/hwC0D0", &st) == 0) break; usleep(1000); } if (tries >= 5000) { fprintf(stderr, "MR05: hwC0D0 never appeared\n"); return 1; } fd = open("/dev/snd/hwC0D0", O_RDWR); if (fd < 0) { fprintf(stderr, "MR05: open: %s\n", strerror(errno)); return 1; } printf("MR05: opened inside probe window, issuing MISC_MODE\n"); fflush(stdout); memset(&mm, 0, sizeof(mm)); mm.port = -1; mm.mode = 0; mm.value = 0; ioctl(fd, SNDRV_EMUX_IOCTL_MISC_MODE, &mm); printf("MR05: ioctl returned rc=%d (%s)\n", fd, strerror(errno)); fflush(stdout); close(fd); return 0; } int main(int argc, char **argv) { if (argc < 2) { fprintf(stderr, "usage: synth_test [arg]\n"); return 2; } if (!strcmp(argv[1], "load")) return plan_load(argc > 2 ? atoi(argv[2]) : 30, 0); if (!strcmp(argv[1], "reload")) return plan_load(argc > 2 ? atoi(argv[2]) : 30, 1); if (!strcmp(argv[1], "mr03")) return plan_mr03(argc > 2 ? atoi(argv[2]) : 3000); if (!strcmp(argv[1], "mr04")) return plan_mr04(argc > 2 ? atoi(argv[2]) : 2000); if (!strcmp(argv[1], "mr04hold")) return plan_mr04hold(); if (!strcmp(argv[1], "mr05")) return plan_mr05(); fprintf(stderr, "unknown plan %s\n", argv[1]); return 2; }