ALSA: vx: vx_free_pipe() kfrees pipes with no lock common to the threaded IRQ pipe walker (UAF)

From: 徐晗翔

Date: Tue Dec 01 2026 - 07:50:07 EST


The VX driver family (sound/drivers/vx: VXPocket PCMCIA and vx222 PCI,
shared vx_pcm.c) manages its audio pipes (struct vx_pipe) in
chip->playback_pipes[] / chip->capture_pipes[] arrays with no lock
shared between the open/close paths and the interrupt delivery path:

- close: vx_pcm_playback_close() (vx_pcm.c:557-574) /
vx_pcm_capture_close() (vx_pcm.c:948-975) clear their array slot
(L568 / L957) and call vx_free_pipe(), which sends the FREE_PIPE
hardware message and then kfrees the pipe (vx_pcm.c:443-454,
kfree at L451) -- all unlocked.

- interrupt: snd_vx_threaded_irq_handler() (vx_core.c:489-523, a
request_threaded_irq handler -- process context, vxpocket.c:197 /
vx222.c:132) -> vx_pcm_update_intr() (vx_pcm.c:1082-1134) reads
chip->playback_pipes[p] / chip->capture_pipes[i] unlocked (L1120 /
L1130), dereferences pipe->substream (L1121/L1131), and drives the
pipes: vx_pcm_playback_transfer() (L676-691) re-reads
pipe->prepared on every chunk of its multi-chunk DMA loop and
vx_pcm_playback/capture_update() read pipe->running and the
runtime. The capture loop walks EVERY capture pipe on EVERY
interrupt.

The only lock in the driver, chip->lock, guards hardware message
transactions (vx_send_msg(), vx_core.c:345-349; the DMA chunk
transfer, vx_pcm.c:633) -- it does not cover the array reads, the
pipe dereferences, the ->prepared/->running checks, the unlocked
pipe->references refcount (vx_pcm.c:920/966), or the kfree. Because
pipe->prepared is set in vx_pcm_prepare() (L844) and NEVER cleared
anywhere, a stream that was merely opened and prepared keeps the IRQ
thread inside the multi-chunk transfer loop even after the stream has
been stopped: pcm close first stops the stream and then unpublishes
and kfrees the pipe, and the close side's vx_send_msg() serializes
against the IRQ thread's chunk transaction on chip->lock -- so the
kfree lands exactly at a chunk boundary, and the IRQ thread's next
loop iteration reads the freed pipe (UAF at vx_pcm.c:683 and inside
vx_pseudo_dma_write()). The capture side has the narrower
load-pointer-vs-free window (vx_pcm_capture_update() early-returns on
pipe->running, but that read itself is against freed memory once the
free wins the race). No privileges are needed for the close side;
VXPocket/vx222 hardware is required for any of this to be reachable.

This is a report-first submission: no reproducer is attached, because
the hardware cannot be emulated; the analysis below is complete and
the patch at the end is provided for direct testing by anyone with the
hardware.

The detailed description:

* Test environment / affected version (still present as of 7.4-rc5)

Static analysis on vanilla mainline 7.3; sound/drivers/vx/vx_pcm.c
and vx_core.c are byte-identical between the audited tree and the
vanilla 7.3 verification tree. Not dynamically verified: QEMU x86
has no VXPocket (PCMCIA) or vx222 (PCI) emulation, and no other
in-tree user of the vx core exists.

* Mechanism

- free side (process context): pcm close ->
snd_pcm_release() -> snd_pcm_release_substream() ->
snd_pcm_drop() (stops the stream; note vx's trigger(STOP) clears
pipe->running but nothing clears pipe->prepared) ->
ops->close: vx_pcm_playback_close(): L568
chip->playback_pipes[pipe->number] = NULL (unlocked), L569
vx_free_pipe(): L345 vx_send_msg() = guard(mutex)(&chip->lock)
+ hardware transaction, then L451 kfree(pipe) -- outside any lock
that the IRQ side respects. vx_pcm_capture_close() L957/L973 is
the capture-side equivalent.

- IRQ side (threaded handler, process context on another CPU):
vx_pcm_update_intr(): L1120/L1130 load pipe from the array
unlocked; L1121/L1131 dereference pipe->substream;
vx_pcm_playback_transfer() passes the pipe->prepared check at
L683 (prepared is never cleared) and runs the L685-690 chunk
loop, each iteration taking chip->lock for the DMA transaction
(vx_pcm.c:633). The close side's vx_send_msg() waits on
chip->lock at exactly a chunk boundary, completes FREE_PIPE and
kfree(pipe), and the IRQ thread's next iteration dereferences the
freed pipe (L683 -> vx_pseudo_dma_write(chip, runtime, pipe, ...)
inside the still-held chip->lock).

- the same unlocked pattern also leaves the shared monitoring
pipe's pipe->references refcount non-atomic (L920 increment,
L966 decrement).

What chip->lock does NOT cover is decisive here: the array publish/
unpublish, the pipe field dereferences, and the kfree are all
outside it on both sides; VX_STAT_IS_STALE (checked at handler
entry) only gates device removal, not close.

* Trigger model

VXPocket/vx222 present and bound; a playback stream opened and
prepared (DSP interrupts keep the threaded handler inside the
transfer loop); close(fd) (or drop+close) while the handler is
processing -- the chunk-boundary serialization makes the free land
between two IRQ iterations. Capture side: any interrupt arriving
while close frees the pipe. Consequence: use-after-free
read/write on struct vx_pipe and, via the stale pipe, on the
runtime buffers (the pseudo-DMA path reads/writes runtime->dma_area
through the freed pipe's fields) -- memory corruption and crash,
triggerable by an unprivileged local user.

* Patch

vx_pcm_playback/capture_close() unpublish and kfree pipes while
vx_pcm_update_intr() (threaded IRQ) walks the pipe arrays and
dereferences the pipes with no lock common to the two sides; the
multi-chunk DMA loop keeps using pipe->prepared (never cleared) even
for stopped streams, and the close side's vx_send_msg() serializes on
chip->lock exactly at a chunk boundary, so the kfree lands between
two IRQ iterations -- use-after-free on struct vx_pipe and, through
it, on the runtime buffers.

Add pcm_lock to struct vx_core; take it in the pipe publish/unpublish/
free paths, around the pipe walks in vx_pcm_update_intr(), and around
the pipe->references updates. Clear pipe->prepared on trigger STOP.
Ordering is uniformly pcm_lock -> chip->lock.

---
include/sound/vx_core.h | 1 +
sound/drivers/vx/vx_core.c | 1 +
sound/drivers/vx/vx_pcm.c | 46 ++++++++++++++++++++++++++-------
3 files changed, 39 insertions(+), 7 deletions(-)

diff --git a/include/sound/vx_core.h b/include/sound/vx_core.h
--- a/include/sound/vx_core.h
+++ b/include/sound/vx_core.h
@@ -150,6 +150,7 @@ struct vx_core {
const struct snd_vx_hardware *hw;
const struct snd_vx_ops *ops;

struct mutex lock;
+ struct mutex pcm_lock; /* pipe arrays + pipe lifetime vs the
+ * threaded IRQ walker (vx_pcm) */

unsigned int chip_status;
unsigned int pcm_running;
diff --git a/sound/drivers/vx/vx_core.c b/sound/drivers/vx/vx_core.c
--- a/sound/drivers/vx/vx_core.c
+++ b/sound/drivers/vx/vx_core.c
@@ -789,6 +789,7 @@ int snd_vx_create(struct snd_card *card, struct snd_vx_hardware *hw,
}
strcpy(card->driver, hw->name);
mutex_init(&chip->lock);
+ mutex_init(&chip->pcm_lock);
mutex_init(&chip->mixer_mutex);
...
diff --git a/sound/drivers/vx/vx_pcm.c b/sound/drivers/vx/vx_pcm.c
--- a/sound/drivers/vx/vx_pcm.c
+++ b/sound/drivers/vx/vx_pcm.c
@@ -529,6 +529,7 @@ static int vx_pcm_playback_open(struct snd_pcm_substream *subs)
if (snd_BUG_ON(audio >= chip->audio_outs))
return -EINVAL;

+ guard(mutex)(&chip->pcm_lock);
/* playback pipe may have been already allocated for monitoring */
pipe = chip->playback_pipes[audio];
if (! pipe) {
@@ -566,12 +567,16 @@ static int vx_pcm_playback_close(struct snd_pcm_substream *subs)
if (! subs->runtime->private_data)
return -EINVAL;

pipe = subs->runtime->private_data;
+ guard(mutex)(&chip->pcm_lock);

if (--pipe->references == 0) {
chip->playback_pipes[pipe->number] = NULL;
vx_free_pipe(chip, pipe);
@@ -749,6 +752,7 @@ static int vx_pcm_trigger(struct snd_pcm_substream *subs, int cmd)
vx_toggle_pipe(chip, pipe, 0);
vx_stop_pipe(chip, pipe);
vx_stop_stream(chip, pipe);
+ pipe->prepared = 0;
chip->pcm_running--;
pipe->running = 0;
break;
@@ -906,6 +909,7 @@ static int vx_pcm_capture_open(struct snd_pcm_substream *subs)
if (chip->chip_status & VX_STAT_IS_STALE)
return -EBUSY;

+ guard(mutex)(&chip->pcm_lock);
audio = subs->pcm->device * 2;
if (snd_BUG_ON(audio >= chip->audio_ins))
return -EINVAL;
@@ -955,6 +959,7 @@ static int vx_pcm_capture_close(struct snd_pcm_substream *subs)
if (! subs->runtime->private_data)
return -EINVAL;
pipe = subs->runtime->private_data;
+ guard(mutex)(&chip->pcm_lock);
chip->capture_pipes[pipe->number] = NULL;
@@
void vx_pcm_update_intr(struct vx_core *chip, unsigned int events)
{
unsigned int i;
struct vx_pipe *pipe;

+ guard(mutex)(&chip->pcm_lock);
#define EVENT_MASK (END_OF_BUFFER_EVENTS_PENDING|ASYNC_EVENTS_PENDING)
@@
/* read the last pending 6 bytes */ ... (capture close also
covered by the guard above)
--
2.43.0

Please keep me in Cc; I am not subscribed to the lists.

Regards,
Hanxiang Xu
xuhx@xxxxxxxxxxx