Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 38E33C4321E for ; Mon, 5 Dec 2022 14:24:15 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1p2CN3-00018T-0G; Mon, 05 Dec 2022 09:23:01 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1p26Xs-0005nU-50 for qemu-devel@nongnu.org; Mon, 05 Dec 2022 03:09:52 -0500 Received: from szxga02-in.huawei.com ([45.249.212.188]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1p26Xp-0007Sj-Kx for qemu-devel@nongnu.org; Mon, 05 Dec 2022 03:09:47 -0500 Received: from dggpemm500010.china.huawei.com (unknown [172.30.72.54]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4NQblv4CTvzFqfQ; Mon, 5 Dec 2022 16:08:43 +0800 (CST) Received: from huawei.com (10.174.185.41) by dggpemm500010.china.huawei.com (7.185.36.134) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Mon, 5 Dec 2022 16:09:30 +0800 To: , CC: , , , Subject: [PATCH] net/net.c: Fix qemu crash when hot-pluging a vhost-net failed. Date: Mon, 5 Dec 2022 16:08:59 +0800 Message-ID: <20221205080859.2216-1-yangming73@huawei.com> X-Mailer: git-send-email 2.32.0.windows.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-Originating-IP: [10.174.185.41] X-ClientProxiedBy: dggems702-chm.china.huawei.com (10.3.19.179) To dggpemm500010.china.huawei.com (7.185.36.134) X-CFilter-Loop: Reflected Received-SPF: pass client-ip=45.249.212.188; envelope-from=yangming73@huawei.com; helo=szxga02-in.huawei.com X-Spam_score_int: -41 X-Spam_score: -4.2 X-Spam_bar: ---- X-Spam_report: (-4.2 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-Mailman-Approved-At: Mon, 05 Dec 2022 09:22:55 -0500 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: Ming Yang From: Ming Yang via Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Hot-pluging a vhost-net may cause virtual machine crash in following steps: 1. Starting a vm without net devices. 2. Hot-pluging 70 memory devices. 3. Hot-pluging a vhost-net device. The reason is : if hotplug a vhost-net failed, the nc cannot be found via function qemu_find_netdev, as it has been cleaned up through function qemu_cleanup_net_client. Which leads to the result that assert(nc) failed, then qemu crashed. While, the root reason is that, in commit 46d4d36d0bf2 if not both has_vhostforce and vhostforce flags are true, the errp would not be set. Then net_init_tap would not return a negative value, fallowed by founding nc and assert nc. In this patch, asserting nc is replaced with setting an error message. Fixes: 46d4d36d0bf2("tap: setting error appropriately when calling net_init_tap_one()") Signed-off-by: Ming Yang Signed-off-by: Liang Zhang --- net/net.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/net/net.c b/net/net.c index 840ad9dca5..1d1d7e54c4 100644 --- a/net/net.c +++ b/net/net.c @@ -1103,7 +1103,16 @@ static int net_client_init1(const Netdev *netdev, bool is_netdev, Error **errp) if (is_netdev) { nc = qemu_find_netdev(netdev->id); - assert(nc); + /* + * If the tap of hotpluged net device do not has both has_vhostforce flag and vhostforce flags, + * when error occurs, the error messags will be report but not set to errp. Thus net_client_init_fun + * will not return a negatave value. Therefore the value of nc might be NULL. To make qemu robust, + * it is better to judge if nc is NULL. + */ + if (!nc) { + error_setg(errp, "Device '%s' could not be initialized", netdev->id); + return -1; + } nc->is_netdev = true; } -- 2.33.0