Received: by 2002:ad5:4acb:0:0:0:0:0 with SMTP id n11csp1248203imw; Tue, 5 Jul 2022 06:24:00 -0700 (PDT) X-Google-Smtp-Source: AGRyM1sL+lDfWS4h8im0qCZYuS2Ut2OcOl3qTVIWW7ku8+61B294mMiCgBxEw18vTTSWZJ9kt3kd X-Received: by 2002:a17:902:cf03:b0:16b:e692:c104 with SMTP id i3-20020a170902cf0300b0016be692c104mr9185118plg.134.1657027440584; Tue, 05 Jul 2022 06:24:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1657027440; cv=none; d=google.com; s=arc-20160816; b=ShHm189W94iu0jVMSWsPQXJB4jPHwhXk4lsd9rAeSHa1MvE9seYU1Ynjlo/3xR/+Pc 7YSVt2GYkoQPEwp66XX1gsNm7PKdzWMPKuhgw6iitd0r/TBtB3LqeNUCqaBHKiqWgsNI LPKOcAWgTLwJyyIyn3sUjTFNViVUXVsms5QlFQfFbNzbRzfP+yvZ27QkiiACThpYwrD2 hhyruBeLbe4jM6fHI7qBekdVd7FDHM7rjglYz+ODl6ubQ2GPEkWJ7qKLdVdOximMDOPx pbFUa/ekqelb66TF2Q1loDDgk9r2uZvYAZXO9RopzfRnaJTNDMrc4MsQ+Kx18LVw+nJx Ixcg== 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=sgzHx7FKeg/7VRNOaYlXzVWo7oFbHXp3FcT09jZiaNo=; b=H+VHCd3RfqJpfsKwy+KSYE3L0oetJ/jiaiG25C6Ii+SOgloktiWIS52wCjonQpvwhI 60tdfx2O/Fw0RprQbpbGGQi2TH/DUMUHrkmUOTqjiayhoUVvwQTfQN7PmX7r+V98TiTX OjBYIC9uh4z0xLVYfWCx+Db/z+r6H0XsI+kS9j4ri5qYXk0lEH53Xqde/fSayRqXsP4C YBLDNwuO6tns7Ep52g/92joyBzNaEe06R1p4U0Yl67FVts7PssYGbViSI1qLSedYi1M/ ebBAzIH2xKtsq1xr24SKFTpXzDQKp7xtJvpjC3Y0aWTZNxJTCjGEmXoqBCpgtvQfhpTu bSJA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=PS5IgRnm; 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 p16-20020a056a000b5000b00525324509f6si13635654pfo.118.2022.07.05.06.23.48; Tue, 05 Jul 2022 06:24:00 -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=PS5IgRnm; 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 S233914AbiGEMFN (ORCPT + 99 others); Tue, 5 Jul 2022 08:05:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45846 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233290AbiGEMDE (ORCPT ); Tue, 5 Jul 2022 08:03:04 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3053218342; Tue, 5 Jul 2022 05:03:03 -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 dfw.source.kernel.org (Postfix) with ESMTPS id C13C061830; Tue, 5 Jul 2022 12:03:02 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id D158FC341CB; Tue, 5 Jul 2022 12:03:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1657022582; bh=54bYYCW892a10Zmxkkbs4/RYo2ers1ZfpoR42t/8PJY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=PS5IgRnmmIwfAf4zZVgaJg4Ckz9PyW5cb7WS5Ig20/wAIVaJrxfvmruz3yuAThs1S 2TvZs4sfRI+jWcX0lZJlpsGfh3JC91BMZtCMNtou3xXe2OVFONcKVYv75JHu/r+/P/ 1ltKPH8F2ArtlN6dhP0gggqgfz2/M6Lfp8IZtvSQ= 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 4.19 08/33] virtio-net: fix race between ndo_open() and virtio_device_ready() Date: Tue, 5 Jul 2022 13:58:00 +0200 Message-Id: <20220705115606.955752032@linuxfoundation.org> X-Mailer: git-send-email 2.37.0 In-Reply-To: <20220705115606.709817198@linuxfoundation.org> References: <20220705115606.709817198@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 @@ -3105,14 +3105,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");