]> git.proxmox.com Git - pve-qemu.git/blob - debian/patches/extra/0005-pci-fix-overflow-in-snprintf-string-formatting.patch
bump version to 7.0.0-4
[pve-qemu.git] / debian / patches / extra / 0005-pci-fix-overflow-in-snprintf-string-formatting.patch
1 From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2 From: Claudio Fontana <cfontana@suse.de>
3 Date: Tue, 31 May 2022 13:47:07 +0200
4 Subject: [PATCH] pci: fix overflow in snprintf string formatting
5 MIME-Version: 1.0
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
8
9 the code in pcibus_get_fw_dev_path contained the potential for a
10 stack buffer overflow of 1 byte, potentially writing to the stack an
11 extra NUL byte.
12
13 This overflow could happen if the PCI slot is >= 0x10000000,
14 and the PCI function is >= 0x10000000, due to the size parameter
15 of snprintf being incorrectly calculated in the call:
16
17 if (PCI_FUNC(d->devfn))
18 snprintf(path + off, sizeof(path) + off, ",%x", PCI_FUNC(d->devfn));
19
20 since the off obtained from a previous call to snprintf is added
21 instead of subtracted from the total available size of the buffer.
22
23 Without the accurate size guard from snprintf, we end up writing in the
24 worst case:
25
26 name (32) + "@" (1) + SLOT (8) + "," (1) + FUNC (8) + term NUL (1) = 51 bytes
27
28 In order to provide something more robust, replace all of the code in
29 pcibus_get_fw_dev_path with a single call to g_strdup_printf,
30 so there is no need to rely on manual calculations.
31
32 Found by compiling QEMU with FORTIFY_SOURCE=3 as the error:
33
34 *** buffer overflow detected ***: terminated
35
36 Thread 1 "qemu-system-x86" received signal SIGABRT, Aborted.
37 [Switching to Thread 0x7ffff642c380 (LWP 121307)]
38 0x00007ffff71ff55c in __pthread_kill_implementation () from /lib64/libc.so.6
39 (gdb) bt
40 #0 0x00007ffff71ff55c in __pthread_kill_implementation () at /lib64/libc.so.6
41 #1 0x00007ffff71ac6f6 in raise () at /lib64/libc.so.6
42 #2 0x00007ffff7195814 in abort () at /lib64/libc.so.6
43 #3 0x00007ffff71f279e in __libc_message () at /lib64/libc.so.6
44 #4 0x00007ffff729767a in __fortify_fail () at /lib64/libc.so.6
45 #5 0x00007ffff7295c36 in () at /lib64/libc.so.6
46 #6 0x00007ffff72957f5 in __snprintf_chk () at /lib64/libc.so.6
47 #7 0x0000555555b1c1fd in pcibus_get_fw_dev_path ()
48 #8 0x0000555555f2bde4 in qdev_get_fw_dev_path_helper.constprop ()
49 #9 0x0000555555f2bd86 in qdev_get_fw_dev_path_helper.constprop ()
50 #10 0x00005555559a6e5d in get_boot_device_path ()
51 #11 0x00005555559a712c in get_boot_devices_list ()
52 #12 0x0000555555b1a3d0 in fw_cfg_machine_reset ()
53 #13 0x0000555555bf4c2d in pc_machine_reset ()
54 #14 0x0000555555c66988 in qemu_system_reset ()
55 #15 0x0000555555a6dff6 in qdev_machine_creation_done ()
56 #16 0x0000555555c79186 in qmp_x_exit_preconfig.part ()
57 #17 0x0000555555c7b459 in qemu_init ()
58 #18 0x0000555555960a29 in main ()
59
60 Found-by: Dario Faggioli <Dario Faggioli <dfaggioli@suse.com>
61 Found-by: Martin Liška <martin.liska@suse.com>
62 Cc: qemu-stable@nongnu.org
63 Signed-off-by: Claudio Fontana <cfontana@suse.de>
64 Message-Id: <20220531114707.18830-1-cfontana@suse.de>
65 Reviewed-by: Ani Sinha <ani@anisinha.ca>
66 (cherry-picked from commit 36f18c6989a3d1ff1d7a0e50b0868ef3958299b4)
67 Signed-off-by: Fabian Ebner <f.ebner@proxmox.com>
68 ---
69 hw/pci/pci.c | 18 +++++++++---------
70 1 file changed, 9 insertions(+), 9 deletions(-)
71
72 diff --git a/hw/pci/pci.c b/hw/pci/pci.c
73 index dae9119bfe..c69b412434 100644
74 --- a/hw/pci/pci.c
75 +++ b/hw/pci/pci.c
76 @@ -2625,15 +2625,15 @@ static char *pci_dev_fw_name(DeviceState *dev, char *buf, int len)
77 static char *pcibus_get_fw_dev_path(DeviceState *dev)
78 {
79 PCIDevice *d = (PCIDevice *)dev;
80 - char path[50], name[33];
81 - int off;
82 -
83 - off = snprintf(path, sizeof(path), "%s@%x",
84 - pci_dev_fw_name(dev, name, sizeof name),
85 - PCI_SLOT(d->devfn));
86 - if (PCI_FUNC(d->devfn))
87 - snprintf(path + off, sizeof(path) + off, ",%x", PCI_FUNC(d->devfn));
88 - return g_strdup(path);
89 + char name[33];
90 + int has_func = !!PCI_FUNC(d->devfn);
91 +
92 + return g_strdup_printf("%s@%x%s%.*x",
93 + pci_dev_fw_name(dev, name, sizeof(name)),
94 + PCI_SLOT(d->devfn),
95 + has_func ? "," : "",
96 + has_func,
97 + PCI_FUNC(d->devfn));
98 }
99
100 static char *pcibus_get_dev_path(DeviceState *dev)