Received: by 2002:ad5:4acb:0:0:0:0:0 with SMTP id n11csp1253764imw; Tue, 5 Jul 2022 06:29:37 -0700 (PDT) X-Google-Smtp-Source: AGRyM1svXFK+wGArHSwdnP5nwYel9DhqbtSCIsg2K6R1WA1XT+Zqcg1U/nuydVzJfKLmKjzipRNo X-Received: by 2002:aa7:cc03:0:b0:435:5574:bf30 with SMTP id q3-20020aa7cc03000000b004355574bf30mr46113550edt.15.1657027777092; Tue, 05 Jul 2022 06:29:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1657027777; cv=none; d=google.com; s=arc-20160816; b=HhsyrjMrwhCHmDUSFoMlnuzyRBigVm4JMLyNBizxHK6UXRTh4NSk4faMSHLs5LLWCR bbm4qRCqz8Qi4LWCn+qavzCuku1vXlZrDTNHXy3tTrLKCD+QXDekjhZB4ow3qeAv5ubT rWGE7ndOBuWeHg6fnUK64uaAJUeNfIPXrQtZ4WooZfcOp4ZMTy51wkAUQgNKlyIQpSd5 8e96GlDvEy3MZuLJyp+ryFrx+RIztUjm9lg4AYH7GaWwnd03hiwtR6I4tFwhJf+Qoq7W W4/dFVQvKhBFbWpJ0xCV4To7KdliHcI3kuAlNRb5ZfqfixHKo9NURvZeRKvfDLyw7xAb jZHg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=S+Fkmnax4OfFR2dxtkMMwSx987SPlBeIkSmmHaUdt5k=; b=mZtXXQO9+cSVWnsbg0Z0yrzy2V3f9xPhc+L2nb0oiX2mDSHl9rtbrG37rYgQdRjhvU WJeVFbOkP8nCmg+OjDYrtMPzkrZYjc7xqDA/QBpCFw47aNFBEQr2k/HsLEfmfS7XhElX 7JrUqSVCTqGDqN88C/berI4pMqzc6ZTLeJE9B0sjb4hrPEHK9HIDKR1r8LGen2cDF/bl 8eb9jFgA0ZONVqFzMsS28uGIawuaaQfnywe8wRO4xlWxMAiz3YZGZ20lLuFHX79zhIZ5 JGDLodaacUJugpogYBkHZVY3kbBbmENPvLJ75YVu8Nn7GlAkQPXCdpPo0MiEfJPo6YnE b7pw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=mieNze8p; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x2-20020a05640225c200b0043999754363si15941536edb.516.2022.07.05.06.29.10; Tue, 05 Jul 2022 06:29:37 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=mieNze8p; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236276AbiGEMUl (ORCPT + 99 others); Tue, 5 Jul 2022 08:20:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54616 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235933AbiGEMNZ (ORCPT ); Tue, 5 Jul 2022 08:13:25 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C438E1A825; Tue, 5 Jul 2022 05:10:55 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 07169B817C7; Tue, 5 Jul 2022 12:10:54 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5A8F6C385A2; Tue, 5 Jul 2022 12:10:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1657023052; bh=YNQxg1jTelACrdlbznmES/RRjoWJRAX91Bt+PqCCJz0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=mieNze8paUN45OZuJcBzm1hV9c8dY4CtNIxECSY4ReJDewTJknRohMCUFAUnv6v4k gqwmh5RuB7X8swiPNb792truG1vJSaUmqQVOTi2CxizfLu8SxWXMAQh9oSdTp3O3rO 9tWP8kwZQLjtiLqm2Qj8/RMofQxqRZHOndOHeVG4= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Jason Wang , "Michael S. Tsirkin" Subject: [PATCH 5.15 20/98] virtio-net: fix race between ndo_open() and virtio_device_ready() Date: Tue, 5 Jul 2022 13:57:38 +0200 Message-Id: <20220705115618.161586506@linuxfoundation.org> X-Mailer: git-send-email 2.37.0 In-Reply-To: <20220705115617.568350164@linuxfoundation.org> References: <20220705115617.568350164@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.8 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Jason Wang commit 50c0ada627f56c92f5953a8bf9158b045ad026a1 upstream. We currently call virtio_device_ready() after netdev registration. Since ndo_open() can be called immediately after register_netdev, this means there exists a race between ndo_open() and virtio_device_ready(): the driver may start to use the device before DRIVER_OK which violates the spec. Fix this by switching to use register_netdevice() and protect the virtio_device_ready() with rtnl_lock() to make sure ndo_open() can only be called after virtio_device_ready(). Fixes: 4baf1e33d0842 ("virtio_net: enable VQs early") Signed-off-by: Jason Wang Message-Id: <20220617072949.30734-1-jasowang@redhat.com> Signed-off-by: Michael S. Tsirkin Signed-off-by: Greg Kroah-Hartman --- drivers/net/virtio_net.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) --- a/drivers/net/virtio_net.c +++ b/drivers/net/virtio_net.c @@ -3241,14 +3241,20 @@ static int virtnet_probe(struct virtio_d } } - err = register_netdev(dev); + /* serialize netdev register + virtio_device_ready() with ndo_open() */ + rtnl_lock(); + + err = register_netdevice(dev); if (err) { pr_debug("virtio_net: registering device failed\n"); + rtnl_unlock(); goto free_failover; } virtio_device_ready(vdev); + rtnl_unlock(); + err = virtnet_cpu_notif_add(vi); if (err) { pr_debug("virtio_net: registering cpu notifier failed\n");