Received: from eggs.gnu.org ([2001:4830:134:3::10]:39243) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1X4Z4A-0003Ax-5a for qemu-devel@nongnu.org; Tue, 08 Jul 2014 13:20:48 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1X4Z3v-0007R4-8X for qemu-devel@nongnu.org; Tue, 08 Jul 2014 13:20:30 -0400 Received: from e34.co.us.ibm.com ([32.97.110.152]:53997) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1X4Z3v-0007Qn-1n for qemu-devel@nongnu.org; Tue, 08 Jul 2014 13:20:15 -0400 Received: from /spool/local by e34.co.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Tue, 8 Jul 2014 11:20:14 -0600 From: Michael Roth Date: Tue, 8 Jul 2014 12:16:53 -0500 Message-Id: <1404839947-1086-23-git-send-email-mdroth@linux.vnet.ibm.com> In-Reply-To: <1404839947-1086-1-git-send-email-mdroth@linux.vnet.ibm.com> References: <1404839947-1086-1-git-send-email-mdroth@linux.vnet.ibm.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Subject: [Qemu-devel] [PATCH 022/156] block: Prevent coroutine stack overflow when recursing in bdrv_open_backing_file. List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: qemu-stable@nongnu.org From: BenoƮt Canet In 1.7.1 qcow2_create2 reopen the file for flushing without the BDRV_O_NO_BACKING flags. As a consequence the code would recursively open the whole backing chain. These three stack arrays would pile up through the recursion and lead to a coroutine stack overflow. Convert these array to malloced buffers in order to streamline the coroutine footprint. Symptoms where freezes or segfaults on production machines while taking QMP externals snapshots. The overflow disturbed coroutine switching. Signed-off-by: Benoit Canet *note: backport of upstream's 1ba4b6a Signed-off-by: Michael Roth --- block.c | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/block.c b/block.c index 382ea71..8f84dbc 100644 --- a/block.c +++ b/block.c @@ -966,14 +966,14 @@ fail: */ int bdrv_open_backing_file(BlockDriverState *bs, QDict *options, Error **errp) { - char backing_filename[PATH_MAX]; - int back_flags, ret; + char *backing_filename = g_malloc0(PATH_MAX); + int back_flags, ret = 0; BlockDriver *back_drv = NULL; Error *local_err = NULL; if (bs->backing_hd != NULL) { QDECREF(options); - return 0; + goto free_exit; } /* NULL means an empty set of options */ @@ -986,10 +986,9 @@ int bdrv_open_backing_file(BlockDriverState *bs, QDict *options, Error **errp) backing_filename[0] = '\0'; } else if (bs->backing_file[0] == '\0' && qdict_size(options) == 0) { QDECREF(options); - return 0; + goto free_exit; } else { - bdrv_get_full_backing_filename(bs, backing_filename, - sizeof(backing_filename)); + bdrv_get_full_backing_filename(bs, backing_filename, PATH_MAX); } bs->backing_hd = bdrv_new(""); @@ -1012,11 +1011,14 @@ int bdrv_open_backing_file(BlockDriverState *bs, QDict *options, Error **errp) error_setg(errp, "Could not open backing file: %s", error_get_pretty(local_err)); error_free(local_err); - return ret; + goto free_exit; } pstrcpy(bs->backing_file, sizeof(bs->backing_file), bs->backing_hd->file->filename); - return 0; + ret = 0; +free_exit: + g_free(backing_filename); + return ret; } /* @@ -1032,7 +1034,8 @@ int bdrv_open(BlockDriverState *bs, const char *filename, QDict *options, { int ret; /* TODO: extra byte is a hack to ensure MAX_PATH space on Windows. */ - char tmp_filename[PATH_MAX + 1]; + char *backing_filename = NULL; + char *tmp_filename = g_malloc0(PATH_MAX + 1); BlockDriverState *file = NULL; QDict *file_options = NULL; const char *drvname; @@ -1052,7 +1055,7 @@ int bdrv_open(BlockDriverState *bs, const char *filename, QDict *options, int64_t total_size; BlockDriver *bdrv_qcow2; QEMUOptionParameter *create_options; - char backing_filename[PATH_MAX]; + backing_filename = g_malloc0(PATH_MAX); if (qdict_size(options) != 0) { error_setg(errp, "Can't use snapshot=on with driver-specific options"); @@ -1075,7 +1078,7 @@ int bdrv_open(BlockDriverState *bs, const char *filename, QDict *options, bdrv_unref(bs1); - ret = get_tmp_filename(tmp_filename, sizeof(tmp_filename)); + ret = get_tmp_filename(tmp_filename, PATH_MAX + 1); if (ret < 0) { error_setg_errno(errp, -ret, "Could not get temporary filename"); goto fail; @@ -1083,8 +1086,7 @@ int bdrv_open(BlockDriverState *bs, const char *filename, QDict *options, /* Real path is meaningless for protocols */ if (path_has_protocol(filename)) { - snprintf(backing_filename, sizeof(backing_filename), - "%s", filename); + snprintf(backing_filename, PATH_MAX, "%s", filename); } else if (!realpath(filename, backing_filename)) { ret = -errno; error_setg_errno(errp, errno, "Could not resolve path '%s'", filename); @@ -1206,6 +1208,8 @@ fail: if (error_is_set(&local_err)) { error_propagate(errp, local_err); } + g_free(tmp_filename); + g_free(backing_filename); return ret; close_and_fail: @@ -1214,6 +1218,8 @@ close_and_fail: if (error_is_set(&local_err)) { error_propagate(errp, local_err); } + g_free(tmp_filename); + g_free(backing_filename); return ret; } -- 1.9.1