Skip to contentSkip to conversation

block/raw-posix: Abort on pread beyond end of non-growable file

PATCH Applied Kevin Wolf 2 participants 2 messages Jan 22, 2010 – Jan 27, 2010 Block layer

lore
Share & tools
lore

Kevin Wolf

author
#0 lore raw

PATCH 1/1 block/raw-posix: Abort on pread beyond end of non-growable file

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.

---
 block/raw-posix.c |    6 +++++-
 1 files changed, 5 insertions(+), 1 deletions(-)
block/raw-posix.c +5 -1
index 4d79881..6ef1cff 100644
@@ -403,8 +403,12 @@ static int raw_pread(BlockDriverState *bs, int64_t offset,
                     size = ALIGNED_BUFFER_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)
Signature · 1 line
1.6.5.2
#1 lore raw
Quoted 10 lines · Kevin Wolf

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.

Applied. Thanks.

Regards,

Anthony Liguori

Quoted 23 lines
---
  block/raw-posix.c |    6 +++++-
  1 files changed, 5 insertions(+), 1 deletions(-)
block/raw-posix.c +5 -1
index 4d79881..6ef1cff 100644
@@ -403,8 +403,12 @@ static int raw_pread(BlockDriverState *bs, int64_t offset,
                      size = ALIGNED_BUFFER_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)