hw/audio/hda-codec: vmstate_hda_audio_stream restores unclamped stream and channel indices, causing out-of-bounds access on running_real[]
Host environment
-
Operating system: Debian GNU/Linux 12 (bookworm)
-
OS/kernel version:
Linux 6.1.0-51-cloud-amd64 x86_64 -
Architecture:
x86_64 -
QEMU flavor:
qemu-system-x86_64 -
QEMU version: Verified on QEMU
9.0.2,11.1.1, and current upstreammaster(73294d096cc591e122fde9e63db4739de6d84f2b, 2026-10-28) -
QEMU command line:
./qemu-system-x86_64 -machine q35,accel=kvm -device intel-hda -device hda-duplex -incoming tcp:127.0.0.1:4444
Emulated/Virtualized environment
- Operating system: Guest-agnostic (device state deserialization / post-migration access)
- OS/kernel version: N/A
- Architecture: x86_64 (
q35/pcwithintel-hdaandhda-duplex/hda-output/hda-micro)
Description of problem
Tooling Disclosure (per QEMU bug reporting policy): This issue was identified with the assistance of a static analysis screener checking VMStateDescription deserialization invariants across QEMU device models, and was manually triaged, audited against upstream master (73294d096cc591e122fde9e63db4739de6d84f2b), and paired with a compiler-verified .post_load patch by the reporter prior to submission.
In hw/audio/hda-codec.c, HDAAudioState maintains bool running_real[2 * 16] (32 entries: 16 input stream tags and 16 output stream tags).
During normal guest verb execution (hda_audio_command, AC_VERB_SET_CHANNEL_STREAMID), the 4-bit stream tag and channel number are masked to 0..15 (st->stream = (payload >> 4) & 0x0f; st->channel = payload & 0x0f;). Consequently, hda_audio_Post_load (hw/audio/hda-codec.c:729) and runtime callbacks index a->running_real[st->output * 16 + st->stream] directly without re-masking st->stream:
st->running = a->running_real[st->output * 16 + st->stream];However, vmstate_hda_audio_stream (hw/audio/hda-codec.c:764-779) deserializes st->stream and st->channel as full 32-bit unsigned integers (VMSTATE_UINT32(stream, HDAAudioStream)) without a .post_load callback to clamp or mask them to 0x0f. When the parent vmstate_hda_audio subsequently runs hda_audio_post_load(), a->running_real[st->output * 16 + st->stream] immediately reads past running_real[32] using the unmasked 32-bit st->stream.
Steps to reproduce
- Start QEMU with
-device intel-hda -device hda-duplex. - Restore a migration state where
HDAAudioStream.stream >= 16. - During migration completion,
hda_audio_post_load()indexesa->running_real[st->output * 16 + st->stream]out of bounds.
Additional information
Proposed Fix Patch (03-hw-audio-hda-codec-vmstate-postload.patch, verified against master 73294d096cc591e122fde9e63db4739de6d84f2b)
--- a/hw/audio/hda-codec.c
+++ b/hw/audio/hda-codec.c
@@ -761,9 +761,25 @@
}
};
+static int hda_audio_stream_post_load(void *opaque, int version_id)
+{
+ HDAAudioStream *st = opaque;
+
+ /*
+ * The stream tag is a 4-bit value on the wire: the guest can only set it
+ * through AC_VERB_SET_CHANNEL_STREAMID, which masks it with 0x0f. Clamp
+ * values coming from a (possibly malformed) migration stream before they
+ * are used as indices into running_real[].
+ */
+ st->stream &= 0x0f;
+ st->channel &= 0x0f;
+ return 0;
+}
+
static const VMStateDescription vmstate_hda_audio_stream = {
.name = "hda-audio-stream",
.version_id = 1,
+ .post_load = hda_audio_stream_post_load,
.fields = (const VMStateField[]) {
VMSTATE_UINT32(stream, HDAAudioStream),
VMSTATE_UINT32(channel, HDAAudioStream),