From dfaa2abea1108c49862725af956033478f06d478 Mon Sep 17 00:00:00 2001
Message-Id: <dfaa2abea1108c49862725af956033478f06d478.1418787305.git.jen@redhat.com>
In-Reply-To: <c0f9fa126e925a00235d9077b474dabb4dbdfdc1.1418787305.git.jen@redhat.com>
References: <c0f9fa126e925a00235d9077b474dabb4dbdfdc1.1418787305.git.jen@redhat.com>
From: Amos Kong <akong@redhat.com>
Date: Mon, 8 Dec 2014 14:48:35 -0600
Subject: [CHANGE 09/12] migration: fix parameter validation on ram load
 (CVE-2014-7840)
To: rhvirt-patches@redhat.com,
    jen@redhat.com

RH-Author: Amos Kong <akong@redhat.com>
Message-id: <1418050115-24916-1-git-send-email-akong@redhat.com>
Patchwork-id: 62761
O-Subject: [RHEV-7.1 qemu-kvm-rhev PATCH] migration: fix parameter validation on ram load (CVE-2014-7840)
Bugzilla: 1163079
RH-Acked-by: Juan Quintela <quintela@redhat.com>
RH-Acked-by: Dr. David Alan Gilbert (git) <dgilbert@redhat.com>
RH-Acked-by: Michael S. Tsirkin <mst@redhat.com>

From: Michael S. Tsirkin <mst@redhat.com>

During migration, the values read from migration stream during ram load
are not validated. Especially offset in host_from_stream_offset() and
also the length of the writes in the callers of said function.

To fix this, we need to make sure that the [offset, offset + length]
range fits into one of the allocated memory regions.

Validating addr < len should be sufficient since data seems to always be
managed in TARGET_PAGE_SIZE chunks.

Fixes: CVE-2014-7840

Note: follow-up patches add extra checks on each block->host access.

Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
Reviewed-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
Signed-off-by: Amit Shah <amit.shah@redhat.com>
(cherry picked from commit 0be839a2701369f669532ea5884c15bead1c6e08)

Signed-off-by: Amos Kong <akong@redhat.com>
Signed-off-by: Jeff E. Nelson <jen@redhat.com>
---
 arch_init.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/arch_init.c b/arch_init.c
index b8427bc..ef246be 100644
--- a/arch_init.c
+++ b/arch_init.c
@@ -1013,7 +1013,7 @@ static inline void *host_from_stream_offset(QEMUFile *f,
     uint8_t len;
 
     if (flags & RAM_SAVE_FLAG_CONTINUE) {
-        if (!block) {
+        if (!block || block->length <= offset) {
             error_report("Ack, bad migration stream!");
             return NULL;
         }
@@ -1026,8 +1026,9 @@ static inline void *host_from_stream_offset(QEMUFile *f,
     id[len] = 0;
 
     QTAILQ_FOREACH(block, &ram_list.blocks, next) {
-        if (!strncmp(id, block->idstr, sizeof(id)))
+        if (!strncmp(id, block->idstr, sizeof(id)) && block->length > offset) {
             return memory_region_get_ram_ptr(block->mr) + offset;
+        }
     }
 
     error_report("Can't find block %s!", id);
-- 
2.1.0

