From 0bec9391584522b38c7e280f07b2d4af736c1a93 Mon Sep 17 00:00:00 2001
From: Kevin Wolf <kwolf@redhat.com>
Date: Tue, 14 Feb 2012 11:13:55 +0100
Subject: [PATCH 30/99] block/raw-posix: Abort on pread beyond end of
 non-growable file

RH-Author: Kevin Wolf <kwolf@redhat.com>
Message-id: <1329218101-24213-31-git-send-email-kwolf@redhat.com>
Patchwork-id: 37218
O-Subject: [RHEL-6.3 qemu-kvm PATCH v2 30/96] block/raw-posix: Abort on pread beyond end of non-growable file
Bugzilla: 783950
RH-Acked-by: Paolo Bonzini <pbonzini@redhat.com>
RH-Acked-by: Marcelo Tosatti <mtosatti@redhat.com>
RH-Acked-by: Laszlo Ersek <lersek@redhat.com>

Bugzilla: 783950

This shouldn't happen under any normal circumstances. However, it looks like
it's possible to achieve this with corrupted images. Without this patch
raw_pread is hanging in an endless loop in such cases.

The patch is not affecting growable files, for which such reads happen in
normal use cases. raw_pread_aligned already handles these cases and won't
return zero in the first place.

Signed-off-by: Kevin Wolf <kwolf@redhat.com>
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
(cherry picked from commit 053965c7ff5b260672719884e644ce4117d01995)
---
 block/raw-posix.c |    6 +++++-
 1 files changed, 5 insertions(+), 1 deletions(-)

Signed-off-by: Michal Novotny <minovotn@redhat.com>
---
 block/raw-posix.c |    6 +++++-
 1 files changed, 5 insertions(+), 1 deletions(-)

diff --git a/block/raw-posix.c b/block/raw-posix.c
index beef064..735fe49 100644
--- a/block/raw-posix.c
+++ b/block/raw-posix.c
@@ -406,8 +406,12 @@ static int raw_pread(BlockDriverState *bs, int64_t offset,
                     size = s->aligned_buf_size;
 
                 ret = raw_pread_aligned(bs, offset, s->aligned_buf, size);
-                if (ret < 0)
+                if (ret < 0) {
                     return ret;
+                } else if (ret == 0) {
+                    fprintf(stderr, "raw_pread: read beyond end of file\n");
+                    abort();
+                }
 
                 size = ret;
                 if (size > count)
-- 
1.7.7.5

