]> git.proxmox.com Git - pve-qemu.git/blob - debian/patches/extra/0018-hw-virtio-Replace-g_memdup-by-g_memdup2.patch
12ea0ad4a457f0f421e916a7b11d00db70c5c1c6
[pve-qemu.git] / debian / patches / extra / 0018-hw-virtio-Replace-g_memdup-by-g_memdup2.patch
1 From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2 From: =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= <philmd@redhat.com>
3 Date: Thu, 12 May 2022 19:57:46 +0200
4 Subject: [PATCH] hw/virtio: Replace g_memdup() by g_memdup2()
5 MIME-Version: 1.0
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
8
9 Per https://discourse.gnome.org/t/port-your-module-from-g-memdup-to-g-memdup2-now/5538
10
11 The old API took the size of the memory to duplicate as a guint,
12 whereas most memory functions take memory sizes as a gsize. This
13 made it easy to accidentally pass a gsize to g_memdup(). For large
14 values, that would lead to a silent truncation of the size from 64
15 to 32 bits, and result in a heap area being returned which is
16 significantly smaller than what the caller expects. This can likely
17 be exploited in various modules to cause a heap buffer overflow.
18
19 Replace g_memdup() by the safer g_memdup2() wrapper.
20
21 Acked-by: Jason Wang <jasowang@redhat.com>
22 Acked-by: Eugenio Pérez <eperezma@redhat.com>
23 Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com>
24 Message-Id: <20220512175747.142058-6-eperezma@redhat.com>
25 Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
26 Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
27 (cherry-picked from commit d792199de55ca5cb5334016884039c740290b5c7)
28 Signed-off-by: Fabian Ebner <f.ebner@proxmox.com>
29 ---
30 hw/net/virtio-net.c | 3 ++-
31 hw/virtio/virtio-crypto.c | 6 +++---
32 2 files changed, 5 insertions(+), 4 deletions(-)
33
34 diff --git a/hw/net/virtio-net.c b/hw/net/virtio-net.c
35 index 1067e72b39..e4748a7e6c 100644
36 --- a/hw/net/virtio-net.c
37 +++ b/hw/net/virtio-net.c
38 @@ -1443,7 +1443,8 @@ static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
39 }
40
41 iov_cnt = elem->out_num;
42 - iov2 = iov = g_memdup(elem->out_sg, sizeof(struct iovec) * elem->out_num);
43 + iov2 = iov = g_memdup2(elem->out_sg,
44 + sizeof(struct iovec) * elem->out_num);
45 s = iov_to_buf(iov, iov_cnt, 0, &ctrl, sizeof(ctrl));
46 iov_discard_front(&iov, &iov_cnt, sizeof(ctrl));
47 if (s != sizeof(ctrl)) {
48 diff --git a/hw/virtio/virtio-crypto.c b/hw/virtio/virtio-crypto.c
49 index dcd80b904d..0e31e3cc04 100644
50 --- a/hw/virtio/virtio-crypto.c
51 +++ b/hw/virtio/virtio-crypto.c
52 @@ -242,7 +242,7 @@ static void virtio_crypto_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
53 }
54
55 out_num = elem->out_num;
56 - out_iov_copy = g_memdup(elem->out_sg, sizeof(out_iov[0]) * out_num);
57 + out_iov_copy = g_memdup2(elem->out_sg, sizeof(out_iov[0]) * out_num);
58 out_iov = out_iov_copy;
59
60 in_num = elem->in_num;
61 @@ -605,11 +605,11 @@ virtio_crypto_handle_request(VirtIOCryptoReq *request)
62 }
63
64 out_num = elem->out_num;
65 - out_iov_copy = g_memdup(elem->out_sg, sizeof(out_iov[0]) * out_num);
66 + out_iov_copy = g_memdup2(elem->out_sg, sizeof(out_iov[0]) * out_num);
67 out_iov = out_iov_copy;
68
69 in_num = elem->in_num;
70 - in_iov_copy = g_memdup(elem->in_sg, sizeof(in_iov[0]) * in_num);
71 + in_iov_copy = g_memdup2(elem->in_sg, sizeof(in_iov[0]) * in_num);
72 in_iov = in_iov_copy;
73
74 if (unlikely(iov_to_buf(out_iov, out_num, 0, &req, sizeof(req))