From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:59739) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WsXr2-0001B8-B3 for qemu-devel@nongnu.org; Thu, 05 Jun 2014 09:37:22 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WsXqw-0004AP-BQ for qemu-devel@nongnu.org; Thu, 05 Jun 2014 09:37:16 -0400 Received: from mx1.redhat.com ([209.132.183.28]:61783) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WsXqw-0004A9-1y for qemu-devel@nongnu.org; Thu, 05 Jun 2014 09:37:10 -0400 From: Kevin Wolf Date: Thu, 5 Jun 2014 15:36:29 +0200 Message-Id: <1401975393-7255-18-git-send-email-kwolf@redhat.com> In-Reply-To: <1401975393-7255-1-git-send-email-kwolf@redhat.com> References: <1401975393-7255-1-git-send-email-kwolf@redhat.com> Subject: [Qemu-devel] [PATCH v4 17/21] vhdx: Handle failure for potentially large allocations List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: kwolf@redhat.com, benoit.canet@irqsave.net, stefanha@redhat.com Some code in the block layer makes potentially huge allocations. Failure is not completely unexpected there, so avoid aborting qemu and handle out-of-memory situations gracefully. This patch addresses the allocations in the vhdx block driver. Signed-off-by: Kevin Wolf Reviewed-by: Stefan Hajnoczi Reviewed-by: Benoit Canet --- block/vhdx-log.c | 6 +++++- block/vhdx.c | 12 ++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/block/vhdx-log.c b/block/vhdx-log.c index a77c040..3eb7e68 100644 --- a/block/vhdx-log.c +++ b/block/vhdx-log.c @@ -349,7 +349,11 @@ static int vhdx_log_read_desc(BlockDriverState *bs, BDRVVHDXState *s, } desc_sectors = vhdx_compute_desc_sectors(hdr.descriptor_count); - desc_entries = qemu_blockalign(bs, desc_sectors * VHDX_LOG_SECTOR_SIZE); + desc_entries = qemu_try_blockalign(bs, desc_sectors * VHDX_LOG_SECTOR_SIZE); + if (desc_entries == NULL) { + ret = -ENOMEM; + goto exit; + } ret = vhdx_log_read_sectors(bs, log, §ors_read, desc_entries, desc_sectors, false); diff --git a/block/vhdx.c b/block/vhdx.c index 353c74d..0922f55 100644 --- a/block/vhdx.c +++ b/block/vhdx.c @@ -950,7 +950,11 @@ static int vhdx_open(BlockDriverState *bs, QDict *options, int flags, } /* s->bat is freed in vhdx_close() */ - s->bat = qemu_blockalign(bs, s->bat_rt.length); + s->bat = qemu_try_blockalign(bs, s->bat_rt.length); + if (s->bat == NULL) { + ret = -ENOMEM; + goto fail; + } ret = bdrv_pread(bs->file, s->bat_offset, s->bat, s->bat_rt.length); if (ret < 0) { @@ -1579,7 +1583,11 @@ static int vhdx_create_bat(BlockDriverState *bs, BDRVVHDXState *s, use_zero_blocks || bdrv_has_zero_init(bs) == 0) { /* for a fixed file, the default BAT entry is not zero */ - s->bat = g_malloc0(rt_bat->length); + s->bat = g_try_malloc0(rt_bat->length); + if (rt_bat->length && s->bat != NULL) { + ret = -ENOMEM; + goto exit; + } block_state = type == VHDX_TYPE_FIXED ? PAYLOAD_BLOCK_FULLY_PRESENT : PAYLOAD_BLOCK_NOT_PRESENT; block_state = use_zero_blocks ? PAYLOAD_BLOCK_ZERO : block_state; -- 1.8.3.1