Skip to contentSkip to conversation

qcow2: Fix alloc_clusters_noref() overflow detection

PATCH Applied Max Reitz 3 participants 4 messages May 4, 2014 – May 5, 2014 Block layer

lore
Share & tools
lore

Max Reitz

author
#0 lore raw

PATCH 1/1 qcow2: Fix alloc_clusters_noref() overflow detection

If the very first allocation has a length of 0, the free_cluster_index is still 0 after the for loop, which means that subtracting one from it will underflow and signal an invalid range of clusters by returning -EFBIG. However, there is no such range, as its length is 0.

Fix this by preventing underflows on free_cluster_index during the check.

---
 block/qcow2-refcount.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
block/qcow2-refcount.c +3 -1
index e79895d..9507aef 100644
@@ -656,7 +656,9 @@ retry:
 
     /* Make sure that all offsets in the "allocated" range are representable
      * in an int64_t */
-    if (s->free_cluster_index - 1 > (INT64_MAX >> s->cluster_bits)) {
+    if (s->free_cluster_index > 0 &&
+        s->free_cluster_index - 1 > (INT64_MAX >> s->cluster_bits))
+    {
         return -EFBIG;
     }
 
Signature · 1 line
1.9.2
Reviewed-by on v1 1/1
#1 lore raw

Am 04.05.2014 um 05:31 hat Max Reitz geschrieben:

Quoted 9 lines

If the very first allocation has a length of 0, the free_cluster_index is still 0 after the for loop, which means that subtracting one from it will underflow and signal an invalid range of clusters by returning -EFBIG. However, there is no such range, as its length is 0.

Fix this by preventing underflows on free_cluster_index during the check.

Heh, I wondered about this when I reviewed that other patch, and came to the conclusion that it probably doesn't happen. Did you find a case where it does happen in fact?

Anyway, this can't hurt:

author
#2 lore raw
Quoted 6 lines · Kevin Wolf

Am 04.05.2014 um 05:31 hat Max Reitz geschrieben:

Quoted 5 lines

If the very first allocation has a length of 0, the free_cluster_index is still 0 after the for loop, which means that subtracting one from it will underflow and signal an invalid range of clusters by returning -EFBIG. However, there is no such range, as its length is 0.

Quoted 7 lines
Quoted 4 lines

Fix this by preventing underflows on free_cluster_index during the check.

Heh, I wondered about this when I reviewed that other patch, and came to the conclusion that it probably doesn't happen. Did you find a case where it does happen in fact?

Yes, deleting the last internal snapshot results in a 0-byte allocation for the new (empty) snapshot table. If this is the first allocation after an image has been opened (which is the case for qemu-img snapshot -d), you'll receive the “File too big” error message (which confused me quite a bit at first, as I wasn't specifically testing this series).

Max

Quoted 3 lines

Anyway, this can't hurt:

#3 lore raw
In reply to Max Reitz
Quoted 5 lines · Max Reitz

If the very first allocation has a length of 0, the free_cluster_index is still 0 after the for loop, which means that subtracting one from it will underflow and signal an invalid range of clusters by returning -EFBIG. However, there is no such range, as its length is 0.

Quoted 7 lines

Fix this by preventing underflows on free_cluster_index during the check.

---
 block/qcow2-refcount.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

Thanks, applied to my block tree:
https://github.com/stefanha/qemu/commits/block

Stefan