Received: by 2002:ad5:4acb:0:0:0:0:0 with SMTP id n11csp1255558imw; Tue, 5 Jul 2022 06:31:08 -0700 (PDT) X-Google-Smtp-Source: AGRyM1sHRYmkWDYjqWlJ+jQCR6MxZaZKfuRqp/zkWTYydvF9ikwvktSHxHrkkbA8UMijfWLNDmKa X-Received: by 2002:a17:907:628c:b0:6ee:70cf:d59 with SMTP id nd12-20020a170907628c00b006ee70cf0d59mr33972497ejc.402.1657027868616; Tue, 05 Jul 2022 06:31:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1657027868; cv=none; d=google.com; s=arc-20160816; b=aGMLE/GOOrWeZUbgqQpluhrt0mlSNg8slwCORSWV1+A7lQ1cFbfwo/iAb9WOTdSLGK PJX0AW4FKaQWThw5UHpH19N1snJBlYh1eFCdfAeWxZLgbYEQnUXDR4Spbpi7mEjPN0Yy +j0BDV1zlsSYSLZpZ/Cze/HVgspSwt6JG0t3h8OkFCiUmJ/wWptH8d62eLdDDxEH6DUK QbgHjtN3hLjC3rNz4ZSCTSy6AHfzo7J/RPNYNrkJsmWbHNQnCSi80KBSV/9+kRhg+M2B VdPnvELU1xv4huBahOcCG7FQN8qJsLA/5DeElj1Qdb79drAin1aihoBKK4tOCaYQo0nL 87Ug== 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=84jOAw4TGT0dlKtGRhKNqCfUKI1u31PyA42YXGjWUl8=; b=wMemDcmhJJxymKajZjKs/n/1VhDwIBEkkKCHFK4ZWHx1E/evLmnsaE1LYGeDnxF3Mg Q56eEQy/IPdNX0TDnCWQYdz7btfBm/CQ9XzZK9oVaH5m3NikigS95FIXRJb/YfxRVg5T lVAwbs5t1M8yFonIyi1QCrSBN7Am49Ijfr2j6v4Jmi4UAN5oHtk8lxFRKTReyfWLAX6I p6qCmz+JFnzlbmMGy8AGsZRByysRU7lsJEaakfouNOrq3Hl7GaLNInGREBrO3Twdy2dj Sz3MCX6vYDzHDcbPOrIjQInKAPkt0y0bezmFFp1b6ikbspGa8xkT5RAwVU8nR6KVHa2h tNiA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="p6T/qzp0"; 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 y15-20020a056402270f00b0043770635d0asi4241334edd.576.2022.07.05.06.30.43; Tue, 05 Jul 2022 06:31:08 -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="p6T/qzp0"; 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 S234259AbiGEMHV (ORCPT + 99 others); Tue, 5 Jul 2022 08:07:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46802 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233802AbiGEMEc (ORCPT ); Tue, 5 Jul 2022 08:04:32 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6BBEB18393; Tue, 5 Jul 2022 05:04:09 -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 0818F618C1; Tue, 5 Jul 2022 12:04:09 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 17E39C341C7; Tue, 5 Jul 2022 12:04:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1657022648; bh=o9wFnSjris8ibElMDNV0VbhJ19NMBcbZ8ctLxZIfb5U=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=p6T/qzp08TYN8Z4TmW9z2cMLm2IUzN7vmpckvF6YwzTn4JbBdPTOWQGprwkXlhbUS f07vTN8FUNJAvFdiZE85zlUe3MdeqkxN+WHf8Q3hn5RAFuWQ1jEaqWfIE7djnmNkyN UKDQT845BvRpNm4j+nEE+ys9prcelqtHQB6huT6E= 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.4 19/58] caif_virtio: fix race between virtio_device_ready() and ndo_open() Date: Tue, 5 Jul 2022 13:57:55 +0200 Message-Id: <20220705115610.816744997@linuxfoundation.org> X-Mailer: git-send-email 2.37.0 In-Reply-To: <20220705115610.236040773@linuxfoundation.org> References: <20220705115610.236040773@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 11a37eb66812ce6a06b79223ad530eb0e1d7294d upstream. We currently depend on probe() calling virtio_device_ready() - which happens 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 (e.g. TX) 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: 0d2e1a2926b18 ("caif_virtio: Introduce caif over virtio") Signed-off-by: Jason Wang Message-Id: <20220620051115.3142-3-jasowang@redhat.com> Signed-off-by: Michael S. Tsirkin Signed-off-by: Greg Kroah-Hartman --- drivers/net/caif/caif_virtio.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) --- a/drivers/net/caif/caif_virtio.c +++ b/drivers/net/caif/caif_virtio.c @@ -723,13 +723,21 @@ static int cfv_probe(struct virtio_devic /* Carrier is off until netdevice is opened */ netif_carrier_off(netdev); + /* serialize netdev register + virtio_device_ready() with ndo_open() */ + rtnl_lock(); + /* register Netdev */ - err = register_netdev(netdev); + err = register_netdevice(netdev); if (err) { + rtnl_unlock(); dev_err(&vdev->dev, "Unable to register netdev (%d)\n", err); goto err; } + virtio_device_ready(vdev); + + rtnl_unlock(); + debugfs_init(cfv); return 0;