From 5cefc50d497d347c2352d785657b04db99e06ec8 Mon Sep 17 00:00:00 2001 From: Max Reitz Date: Sat, 13 Jun 2015 16:22:07 +0200 Subject: [PATCH 13/42] qcow2: Do not overflow when writing an L1 sector Message-id: <1434212556-3927-14-git-send-email-mreitz@redhat.com> Patchwork-id: 66032 O-Subject: [RHEL-7.2 qemu-kvm PATCH 13/42] qcow2: Do not overflow when writing an L1 sector Bugzilla: 1129893 RH-Acked-by: Jeffrey Cody RH-Acked-by: Fam Zheng RH-Acked-by: Stefan Hajnoczi BZ: 1129893 While writing an L1 table sector, qcow2_write_l1_entry() copies the respective range from s->l1_table to the local "buf" array. The size of s->l1_table does not have to be a multiple of L1_ENTRIES_PER_SECTOR; thus, limit the index which is used for copying all entries to the L1 size. Cc: qemu-stable@nongnu.org Signed-off-by: Max Reitz Reviewed-by: Peter Lieven Reviewed-by: Eric Blake Signed-off-by: Kevin Wolf (cherry picked from commit a1391444fe1cfef14976458f3293a2c6945e725c) Signed-off-by: Max Reitz Signed-off-by: Miroslav Rezanina --- block/qcow2-cluster.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/block/qcow2-cluster.c b/block/qcow2-cluster.c index 0e3b8d7..053e9fe 100644 --- a/block/qcow2-cluster.c +++ b/block/qcow2-cluster.c @@ -151,12 +151,14 @@ static int l2_load(BlockDriverState *bs, uint64_t l2_offset, int qcow2_write_l1_entry(BlockDriverState *bs, int l1_index) { BDRVQcowState *s = bs->opaque; - uint64_t buf[L1_ENTRIES_PER_SECTOR]; + uint64_t buf[L1_ENTRIES_PER_SECTOR] = { 0 }; int l1_start_index; int i, ret; l1_start_index = l1_index & ~(L1_ENTRIES_PER_SECTOR - 1); - for (i = 0; i < L1_ENTRIES_PER_SECTOR; i++) { + for (i = 0; i < L1_ENTRIES_PER_SECTOR && l1_start_index + i < s->l1_size; + i++) + { buf[i] = cpu_to_be64(s->l1_table[l1_start_index + i]); } -- 1.8.3.1