03e484
From af9348b27b8bc0c7033527220e7840f4b3209832 Mon Sep 17 00:00:00 2001
03e484
From: Jon Maloy <jmaloy@redhat.com>
03e484
Date: Tue, 6 Oct 2020 18:00:53 -0400
03e484
Subject: [PATCH 2/2] hw/core/loader: Fix possible crash in rom_copy()
03e484
03e484
RH-Author: Jon Maloy <jmaloy@redhat.com>
03e484
Message-id: <20201006180053.484822-2-jmaloy@redhat.com>
03e484
Patchwork-id: 98552
03e484
O-Subject: [RHEL-7.9.z qemu-kvm PATCH 1/1] hw/core/loader: Fix possible crash in rom_copy()
03e484
Bugzilla: 1842923
03e484
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
03e484
RH-Acked-by: Markus Armbruster <armbru@redhat.com>
03e484
RH-Acked-by: Thomas Huth <thuth@redhat.com>
03e484
03e484
From: Thomas Huth <thuth@redhat.com>
03e484
03e484
Both, "rom->addr" and "addr" are derived from the binary image
03e484
that can be loaded with the "-kernel" paramer. The code in
03e484
rom_copy() then calculates:
03e484
03e484
    d = dest + (rom->addr - addr);
03e484
03e484
and uses "d" as destination in a memcpy() some lines later. Now with
03e484
bad kernel images, it is possible that rom->addr is smaller than addr,
03e484
thus "rom->addr - addr" gets negative and the memcpy() then tries to
03e484
copy contents from the image to a bad memory location. This could
03e484
maybe be used to inject code from a kernel image into the QEMU binary,
03e484
so we better fix it with an additional sanity check here.
03e484
03e484
Cc: qemu-stable@nongnu.org
03e484
Reported-by: Guangming Liu
03e484
Buglink: https://bugs.launchpad.net/qemu/+bug/1844635
03e484
Message-Id: <20190925130331.27825-1-thuth@redhat.com>
03e484
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
03e484
Signed-off-by: Thomas Huth <thuth@redhat.com>
03e484
03e484
(cherry picked from commit e423455c4f23a1a828901c78fe6d03b7dde79319)
03e484
Signed-off-by: Jon Maloy <jmaloy@redhat.com>
03e484
Signed-off-by: Jon Maloy <jmaloy.redhat.com>
03e484
---
03e484
 hw/core/loader.c | 2 +-
03e484
 1 file changed, 1 insertion(+), 1 deletion(-)
03e484
03e484
diff --git a/hw/core/loader.c b/hw/core/loader.c
03e484
index 5a15449407..939d0855cb 100644
03e484
--- a/hw/core/loader.c
03e484
+++ b/hw/core/loader.c
03e484
@@ -841,7 +841,7 @@ int rom_copy(uint8_t *dest, hwaddr addr, size_t size)
03e484
         if (rom->addr + rom->romsize < addr) {
03e484
             continue;
03e484
         }
03e484
-        if (rom->addr > end) {
03e484
+        if (rom->addr > end || rom->addr < addr) {
03e484
             break;
03e484
         }
03e484
 
03e484
-- 
03e484
2.18.2
03e484