qemu-e2k/hw/virtio
Wentao Liang 4bf58c7213 virtio-iommu: use-after-free fix
A potential Use-after-free was reported in virtio_iommu_handle_command
when using virtio-iommu:

> I find a potential Use-after-free in QEMU 6.2.0, which is in
> virtio_iommu_handle_command() (./hw/virtio/virtio-iommu.c).
>
>
> Specifically, in the loop body, the variable 'buf' allocated at line 639 can be
> freed by g_free() at line 659. However, if the execution path enters the loop
> body again and the if branch takes true at line 616, the control will directly
> jump to 'out' at line 651. At this time, 'buf' is a freed pointer, which is not
> assigned with an allocated memory but used at line 653. As a result, a UAF bug
> is triggered.
>
>
>
> 599     for (;;) {
> ...
> 615         sz = iov_to_buf(iov, iov_cnt, 0, &head, sizeof(head));
> 616         if (unlikely(sz != sizeof(head))) {
> 617             tail.status = VIRTIO_IOMMU_S_DEVERR;
> 618             goto out;
> 619         }
> ...
> 639             buf = g_malloc0(output_size);
> ...
> 651 out:
> 652         sz = iov_from_buf(elem->in_sg, elem->in_num, 0,
> 653                           buf ? buf : &tail, output_size);
> ...
> 659         g_free(buf);
>
> We can fix it by set ‘buf‘ to NULL after freeing it:
>
>
> 651 out:
> 652         sz = iov_from_buf(elem->in_sg, elem->in_num, 0,
> 653                           buf ? buf : &tail, output_size);
> ...
> 659         g_free(buf);
> +++ buf = NULL;
> 660     }

Fix as suggested by the reporter.

Signed-off-by: Wentao Liang <Wentao_Liang_g@163.com>
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
Message-id: 20220407095047.50371-1-mst@redhat.com
Message-ID: <20220406040445-mutt-send-email-mst@kernel.org>
Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
2022-04-08 15:02:09 +01:00
..
Kconfig vhost-user-rng: Add vhost-user-rng implementation 2021-10-20 04:37:55 -04:00
meson.build virtio: fix --enable-vhost-user build on non-Linux 2022-03-29 15:58:06 +02:00
trace-events virtio-iommu: Default to bypass during boot 2022-03-06 05:08:23 -05:00
trace.h trace: switch position of headers to what Meson requires 2020-08-21 06:18:24 -04:00
vhost-backend.c vhost-backend: stick to -errno error return convention 2022-01-07 05:19:55 -05:00
vhost-iova-tree.c vhost: Add VhostIOVATree 2022-03-15 13:57:44 +08:00
vhost-iova-tree.h vhost: Add VhostIOVATree 2022-03-15 13:57:44 +08:00
vhost-scsi-pci.c Use DECLARE_*CHECKER* macros 2020-09-09 09:27:09 -04:00
vhost-shadow-virtqueue.c vdpa: Add custom IOTLB translations to SVQ 2022-03-15 13:57:44 +08:00
vhost-shadow-virtqueue.h vdpa: Add custom IOTLB translations to SVQ 2022-03-15 13:57:44 +08:00
vhost-stub.c vhost-user: simplify vhost_user_init/vhost_user_cleanup 2019-03-12 21:22:31 -04:00
vhost-user-blk-pci.c Use DECLARE_*CHECKER* macros 2020-09-09 09:27:09 -04:00
vhost-user-fs-pci.c vhost-user-fs: add the "bootindex" property 2021-01-13 09:06:37 -05:00
vhost-user-fs.c Revert "virtio: introduce macro IRTIO_CONFIG_IRQ_IDX" 2022-01-10 16:02:54 -05:00
vhost-user-i2c-pci.c hw/virtio: add vhost-user-i2c-pci boilerplate 2021-07-16 11:10:45 -04:00
vhost-user-i2c.c hw/vhost-user-i2c: Add support for VIRTIO_I2C_F_ZERO_LENGTH_REQUEST 2022-03-04 08:30:52 -05:00
vhost-user-input-pci.c Use DECLARE_*CHECKER* macros 2020-09-09 09:27:09 -04:00
vhost-user-rng-pci.c vhost-user-rng-pci: Add vhost-user-rng-pci implementation 2021-10-20 04:37:55 -04:00
vhost-user-rng.c vhost-user-rng: Add vhost-user-rng implementation 2021-10-20 04:37:55 -04:00
vhost-user-scsi-pci.c Use DECLARE_*CHECKER* macros 2020-09-09 09:27:09 -04:00
vhost-user-vsock-pci.c vhost-user-vsock-pci: force virtio version 1 2020-09-29 02:15:24 -04:00
vhost-user-vsock.c vhost-vsock: handle common features in vhost-vsock-common 2021-10-05 17:30:57 -04:00
vhost-user.c vhost-user: fix VirtQ notifier cleanup 2022-03-04 08:30:52 -05:00
vhost-vdpa.c vhost-vdpa: fix typo in a comment 2022-03-31 21:30:56 +02:00
vhost-vsock-common.c vhost-vsock: detach the virqueue element in case of error 2022-03-06 05:08:23 -05:00
vhost-vsock-pci.c vhost-vsock-pci: force virtio version 1 2020-09-29 02:15:24 -04:00
vhost-vsock.c virtio/vhost-vsock: don't double close vhostfd, remove redundant cleanup 2022-01-07 19:30:13 -05:00
vhost.c vhost: use wfd on functions setting vring call fd 2022-03-06 06:19:47 -05:00
virtio-9p-pci.c Use DECLARE_*CHECKER* macros 2020-09-09 09:27:09 -04:00
virtio-balloon-pci.c hw/virtio/virtio-balloon: Remove the "class" property 2021-02-05 08:52:59 -05:00
virtio-balloon.c virtio/virtio-balloon: Prefer Object* over void* parameter 2022-03-18 13:57:50 +01:00
virtio-blk-pci.c Use DECLARE_*CHECKER* macros 2020-09-09 09:27:09 -04:00
virtio-bus.c virtio: fix the condition for iommu_platform not supported 2022-03-04 08:30:52 -05:00
virtio-crypto-pci.c Use DECLARE_*CHECKER* macros 2020-09-09 09:27:09 -04:00
virtio-crypto.c Use g_new() & friends where that makes obvious sense 2022-03-21 15:44:44 +01:00
virtio-input-host-pci.c Use DECLARE_*CHECKER* macros 2020-09-09 09:27:09 -04:00
virtio-input-pci.c Use OBJECT_DECLARE_SIMPLE_TYPE when possible 2020-09-18 14:12:32 -04:00
virtio-iommu-pci.c hw/arm/virt: Remove device tree restriction for virtio-iommu 2021-12-15 10:35:26 +00:00
virtio-iommu.c virtio-iommu: use-after-free fix 2022-04-08 15:02:09 +01:00
virtio-mem-pci.c qapi: Include qom-path in MEMORY_DEVICE_SIZE_CHANGE qapi events 2021-10-02 08:43:21 +02:00
virtio-mem-pci.h Use DECLARE_*CHECKER* macros 2020-09-09 09:27:09 -04:00
virtio-mem.c hw/arm/virt: Support for virtio-mem-pci 2022-01-20 11:47:52 +00:00
virtio-mmio.c Remove unnecessary minimum_version_id_old fields 2022-01-28 15:38:23 +01:00
virtio-net-pci.c virtio-net: calculating proper msix vectors on init 2021-03-15 16:41:22 +08:00
virtio-pci.c kvm/msi: do explicit commit when adding msi routes 2022-03-15 11:26:20 +01:00
virtio-pci.h Revert "virtio-pci: add support for configure interrupt" 2022-01-10 16:00:02 -05:00
virtio-pmem-pci.c virtio-pmem-pci: force virtio version 1 2020-09-29 02:14:30 -04:00
virtio-pmem-pci.h Use DECLARE_*CHECKER* macros 2020-09-09 09:27:09 -04:00
virtio-pmem.c Mark remaining global TypeInfo instances as const 2022-02-21 13:30:20 +00:00
virtio-rng-pci.c Use DECLARE_*CHECKER* macros 2020-09-09 09:27:09 -04:00
virtio-rng.c sysemu: Let VMChangeStateHandler take boolean 'running' argument 2021-03-09 23:13:57 +01:00
virtio-scsi-pci.c Use DECLARE_*CHECKER* macros 2020-09-09 09:27:09 -04:00
virtio-serial-pci.c Use DECLARE_*CHECKER* macros 2020-09-09 09:27:09 -04:00
virtio.c Replace GCC_FMT_ATTR with G_GNUC_PRINTF 2022-03-22 14:40:51 +04:00