migration: add missing post-load bounds checks across devices
cover migration: add missing post-load bounds checks across devices
Several VMState descriptions restore array indices, ring heads or FIFO lengths directly from the migration stream without re-checking the bounds that runtime register writes enforce. If a migration stream or snapshot carries an out-of-range value, subsequent device callbacks can index past the corresponding array or buffer.
This series adds the missing post-load checks for Fifo8, fw_cfg, hda-codec, Hyper-V VMBus (GPADL and rx_queue), pcie_sriov, sifive_uart, ipoctal232, and vmmouse.
lbyxiaolizi (9):
util/fifo8: check head and num bounds in post_load hw/nvram/fw_cfg: reset out-of-range cur_entry in post_load hw/audio/hda-codec: mask stream and channel IDs in post_load hw/hyperv/vmbus: check seen_gfns <= num_gfns in gpadl post_load hw/hyperv/vmbus: check rx_queue head and size in post_load hw/pci/pcie_sriov: clamp NumVFs to TotalVFs in register_vfs hw/char/sifive_uart: check rx_fifo_len in post_load hw/char/ipoctal232: check channel FIFO indices in post_load hw/i386/vmmouse: check nb_queue bound in post_load
hw/audio/hda-codec.c | 10 ++++++++++ hw/char/ipoctal232.c | 13 +++++++++++++ hw/char/sifive_uart.c | 12 ++++++++++++ hw/hyperv/vmbus.c | 23 +++++++++++++++++++++++ hw/i386/vmmouse.c | 4 ++++ hw/nvram/fw_cfg.c | 18 ++++++++++++++++++ hw/pci/pcie_sriov.c | 7 +++++++ util/fifo8.c | 15 +++++++++++++++ 8 files changed, 102 insertions(+)
base-commit: 61436f5ff1c4e5ee8f60fa1a37962a60f73ea7a4
PATCH 1/9 util/fifo8: check head and num bounds in post_load
fifo8_pop() and fifo8_peek() index fifo->data[fifo->head] before wrapping modulo capacity, and fifo8_push() asserts that fifo->num < fifo->capacity.
vmstate_fifo8 restores head and num as raw uint32_t values without checking them against fifo->capacity. Add a post_load hook to reject streams where head >= capacity or num > capacity.
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/4772
--- util/fifo8.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+)
util/fifo8.c +15 -0
@@ -15,6 +15,7 @@#include "qemu/osdep.h" #include "migration/vmstate.h" #include "qemu/fifo8.h" +#include "qemu/error-report.h" void fifo8_reset (Fifo8 *fifo) {
@@ -177,10 +178,24 @@ uint32_t fifo8_num_used(const Fifo8 *fifo)return fifo->num; } +static int fifo8_post_load (void *opaque,int version_id) +{ +Fifo8 *fifo = opaque; + +if (fifo->head >= fifo->capacity || fifo->num > fifo->capacity) { +error_report ("fifo8: invalid state from migration stream " +"(head=%u num=%u capacity=%u)" , + fifo->head, fifo->num, fifo->capacity); +return -EINVAL ; + } +return 0 ; +} +const VMStateDescription vmstate_fifo8 = { .name ="Fifo8" , .version_id =1 , .minimum_version_id =1 , + .post_load = fifo8_post_load, .fields = (const VMStateField []) {VMSTATE_VBUFFER_UINT32 (data, Fifo8,1 ,NULL , capacity),VMSTATE_UINT32 (head, Fifo8),
PATCH 2/9 hw/nvram/fw_cfg: reset out-of-range cur_entry in post_load
In reply to lbyxiaolizifw_cfg_select() sets s->cur_entry to FW_CFG_INVALID when the requested index is >= fw_cfg_max_entry(s), so fw_cfg_data_read() and fw_cfg_dma_transfer() only check for FW_CFG_INVALID before indexing s->entries[arch][s->cur_entry & FW_CFG_ENTRY_MASK].
vmstate_fw_cfg restores cur_entry directly from the stream. Add a post_load hook that resets out-of-range selectors to FW_CFG_INVALID and clears cur_offset, matching fw_cfg_select().
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/4773
--- hw/nvram/fw_cfg.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)
hw/nvram/fw_cfg.c +18 -0
@@ -36,6 +36,7 @@#include "migration/vmstate.h" #include "trace.h" #include "qemu/error-report.h" +#include "qemu/log.h" #include "qemu/option.h" #include "qemu/config-file.h" #include "qemu/cutils.h"
@@ -676,10 +677,27 @@ static const VMStateDescription vmstate_fw_cfg_acpi_mr = { }, }; +static int fw_cfg_post_load (void *opaque,int version_id) +{ +FWCfgState *s = opaque; + +if (s->cur_entry !=FW_CFG_INVALID && + (s->cur_entry &FW_CFG_ENTRY_MASK ) >=fw_cfg_max_entry (s)) { +qemu_log_mask (LOG_GUEST_ERROR , +"fw_cfg: vmstate transfer received bad entry value 0x%x\n" , + s->cur_entry); + s->cur_entry =FW_CFG_INVALID ; + s->cur_offset =0 ; + } + +return 0 ; +} +static const VMStateDescription vmstate_fw_cfg = { .name ="fw_cfg" , .version_id =2 , .minimum_version_id =1 , + .post_load = fw_cfg_post_load, .fields = (const VMStateField []) {VMSTATE_UINT16 (cur_entry, FWCfgState),VMSTATE_UINT16_HACK (cur_offset, FWCfgState, is_version_1),
PATCH 3/9 hw/audio/hda-codec: mask stream and channel IDs in post_load
In reply to lbyxiaoliziAC_VERB_SET_CHANNEL_STREAMID masks st->stream and st->channel to 4 bits (& 0x0f), and hda_audio_post_load() uses st->stream directly to index a->running_real[st->output * 16 + st->stream].
vmstate_hda_audio_stream loads both fields as uint32_t. Mask them with 0x0f in a post_load callback on vmstate_hda_audio_stream before the parent hda_audio_post_load() runs.
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/4774
--- hw/audio/hda-codec.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
hw/audio/hda-codec.c +10 -0
@@ -761,9 +761,19 @@ static const VMStateDescription vmstate_hda_audio_stream_buf = { } }; +static int hda_audio_stream_post_load (void *opaque,int version_id) +{ +HDAAudioStream *st = opaque; + + 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),
PATCH 4/9 hw/hyperv/vmbus: check seen_gfns <= num_gfns in gpadl post_load
In reply to lbyxiaolizivmstate_gpadl allocates gpadl->gfns with num_gfns elements and loads seen_gfns as a raw uint32_t. If seen_gfns > num_gfns, the unsigned subtraction num_gfns - seen_gfns in handle_gpadl_body() wraps around and writes past gpadl->gfns[].
Reject seen_gfns > num_gfns in a post_load hook on vmstate_gpadl.
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/4776
--- hw/hyperv/vmbus.c | 11 +++++++++++ 1 file changed, 11 insertions(+)
hw/hyperv/vmbus.c +11 -0
@@ -531,10 +531,21 @@ void vmbus_unmap_sgl(VMBusChanReq *req, DMADirection dir, struct iovec *iov, } } +static int vmbus_gpadl_post_load (void *opaque,int version_id) +{ +VMBusGpadl *gpadl = opaque; + +if (gpadl->seen_gfns > gpadl->num_gfns) { +return -EINVAL ; + } +return 0 ; +} +static const VMStateDescription vmstate_gpadl = { .name ="vmbus/gpadl" , .version_id =0 , .minimum_version_id =0 , + .post_load = vmbus_gpadl_post_load, .fields = (const VMStateField []) {VMSTATE_UINT32 (id, VMBusGpadl),VMSTATE_UINT32 (child_relid, VMBusGpadl),
PATCH 5/9 hw/hyperv/vmbus: check rx_queue head and size in post_load
In reply to lbyxiaoliziVMBus.rx_queue has HV_MSG_QUEUE_LEN (16) entries, and vmbus_post_load() schedules process_message(), which indexes vmbus->rx_queue[vmbus->rx_queue_head] directly.
vmstate_rx_queue loads rx_queue_head and rx_queue_size as uint8_t values without bounds checking. Add a post_load hook to reject rx_queue_head >= HV_MSG_QUEUE_LEN or rx_queue_size > HV_MSG_QUEUE_LEN.
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/4776
--- hw/hyperv/vmbus.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
hw/hyperv/vmbus.c +12 -0
@@ -2607,11 +2607,23 @@ static bool vmbus_rx_queue_needed(void *opaque)return vmbus->rx_queue_size; } +static int vmbus_rx_queue_post_load (void *opaque,int version_id) +{ +VMBus *vmbus =VMBUS (opaque); + +if (vmbus->rx_queue_head >=HV_MSG_QUEUE_LEN || + vmbus->rx_queue_size >HV_MSG_QUEUE_LEN ) { +return -EINVAL ; + } +return 0 ; +} +static const VMStateDescription vmstate_rx_queue = { .name ="vmbus/rx_queue" , .version_id =0 , .minimum_version_id =0 , .needed = vmbus_rx_queue_needed, + .post_load = vmbus_rx_queue_post_load, .fields = (const VMStateField []) {VMSTATE_UINT8 (rx_queue_head, VMBus),VMSTATE_UINT8 (rx_queue_size, VMBus),
PATCH 6/9 hw/pci/pcie_sriov: clamp NumVFs to TotalVFs in register_vfs
In reply to lbyxiaoliziAt runtime, consume_config() clears PCI_SRIOV_CTRL_VFE from wmask when NumVFs > TotalVFs, preventing the guest from enabling more VFs than the total_vfs-sized dev->exp.sriov_pf.vf[] array holds.
On migration load, get_pci_config_device() validates the incoming config space against the post-reset wmask where both VFE and NumVFs are writable, and pcie_sriov_pf_post_load() -> consume_config() -> register_vfs() then loops up to NumVFs without checking TotalVFs. Clamp num_vfs to total_vfs in register_vfs() and write the clamped value back to config space.
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/4777
--- hw/pci/pcie_sriov.c | 7 +++++++ 1 file changed, 7 insertions(+)
hw/pci/pcie_sriov.c +7 -0
@@ -35,11 +35,18 @@ static void unparent_vfs(PCIDevice *dev, uint16_t total_vfs)static void register_vfs (PCIDevice *dev) {uint16_t num_vfs; +uint16_t total_vfs;uint16_t i;uint16_t sriov_cap = dev->exp.sriov_cap;assert (sriov_cap >0 ); num_vfs =pci_get_word (dev->config + sriov_cap +PCI_SRIOV_NUM_VF ); + total_vfs =pci_get_word (dev->config + sriov_cap +PCI_SRIOV_TOTAL_VF ); + +if (num_vfs > total_vfs) { + num_vfs = total_vfs; +pci_set_word (dev->config + sriov_cap +PCI_SRIOV_NUM_VF , num_vfs); + }trace_sriov_register_vfs (dev->name,PCI_SLOT (dev->devfn),PCI_FUNC (dev->devfn), num_vfs);
PATCH 7/9 hw/char/sifive_uart: check rx_fifo_len in post_load
In reply to lbyxiaoliziSiFiveUARTState.rx_fifo has SIFIVE_UART_RX_FIFO_SIZE (8) bytes, and sifive_uart_read() passes s->rx_fifo_len - 1 to memmove() when reading SIFIVE_UART_RXFIFO.
vmstate_sifive_uart loads rx_fifo_len as a raw uint8_t without bounds checking. Reject rx_fifo_len > SIFIVE_UART_RX_FIFO_SIZE in post_load.
--- hw/char/sifive_uart.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
hw/char/sifive_uart.c +12 -0
@@ -341,10 +341,22 @@ static void sifive_uart_reset_hold(Object *obj, ResetType type)qemu_irq_lower (s->irq); } +static int sifive_uart_post_load (void *opaque,int version_id) +{ +SiFiveUARTState *s = opaque; + +if (s->rx_fifo_len >SIFIVE_UART_RX_FIFO_SIZE ) { +return -EINVAL ; + } + +return 0 ; +} +static const VMStateDescription vmstate_sifive_uart = { .name =TYPE_SIFIVE_UART , .version_id =3 , .minimum_version_id =3 , + .post_load = sifive_uart_post_load, .fields = (const VMStateField []) {VMSTATE_UINT8_ARRAY (rx_fifo, SiFiveUARTState,SIFIVE_UART_RX_FIFO_SIZE ),
PATCH 8/9 hw/char/ipoctal232: check channel FIFO indices in post_load
In reply to lbyxiaoliziSCC2698Channel has mr[2] and rhr[RX_FIFO_SIZE] (3 bytes), indexed by mr_idx and rhr_idx during register reads and writes.
vmstate_scc2698_channel restores mr_idx, rhr_idx and rx_pending as raw uint8_t values. Add a post_load check to ensure mr_idx < ARRAY_SIZE(mr), rhr_idx < RX_FIFO_SIZE, and rx_pending <= RX_FIFO_SIZE.
--- hw/char/ipoctal232.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)
hw/char/ipoctal232.c +13 -0
@@ -125,10 +125,23 @@ struct IPOctalState {uint8_t irq_vector; }; +static int scc2698_channel_post_load (void *opaque,int version_id) +{ +SCC2698Channel *ch = opaque; + +if (ch->mr_idx >=ARRAY_SIZE (ch->mr) || + ch->rhr_idx >=RX_FIFO_SIZE || + ch->rx_pending >RX_FIFO_SIZE ) { +return -EINVAL ; + } +return 0 ; +} +static const VMStateDescription vmstate_scc2698_channel = { .name ="scc2698_channel" , .version_id =1 , .minimum_version_id =1 , + .post_load = scc2698_channel_post_load, .fields = (const VMStateField []) {VMSTATE_BOOL (rx_enabled, SCC2698Channel),VMSTATE_UINT8_ARRAY (mr, SCC2698Channel,2 ),
PATCH 9/9 hw/i386/vmmouse: check nb_queue bound in post_load
In reply to lbyxiaoliziVMMouseState.queue holds VMMOUSE_QUEUE_SIZE (1024) entries. vmmouse_read_id() only checks s->nb_queue == VMMOUSE_QUEUE_SIZE before appending, and vmmouse_data() moves s->nb_queue elements with memmove().
vmstate_vmmouse loads nb_queue as a raw uint16_t without checking it in vmmouse_post_load(). Reject s->nb_queue > VMMOUSE_QUEUE_SIZE in vmmouse_post_load().
--- hw/i386/vmmouse.c | 4 ++++ 1 file changed, 4 insertions(+)
hw/i386/vmmouse.c +4 -0
@@ -320,6 +320,10 @@ static int vmmouse_post_load(void *opaque, int version_id) {VMMouseState *s = opaque; +if (s->nb_queue >VMMOUSE_QUEUE_SIZE ) { +return -EINVAL ; + } +vmmouse_remove_handler (s);vmmouse_update_handler (s, s->absolute);return 0 ;