Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp6149087iob; Tue, 10 May 2022 11:23:39 -0700 (PDT) X-Google-Smtp-Source: ABdhPJylz/iR6cyjjYSfCmIR7Vs9/4w8g2TEYLXUtnxMcsz2+SVl51LNDCGcJrJpbzqQVua52OO7 X-Received: by 2002:a05:6402:4313:b0:427:ca44:5d5e with SMTP id m19-20020a056402431300b00427ca445d5emr25304503edc.67.1652207019172; Tue, 10 May 2022 11:23:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652207019; cv=none; d=google.com; s=arc-20160816; b=j7adP3bycfjjV5iKHaQWzU7tcv6B5SgjUs7uNTrRkgALr+GOuwtEofSi3W+7dsPiy3 E4ecHuEXisZ5XYbY7ajIhRc33Uv6kR8S8B8iUkY0OAfTe1EyrBEkoDkvEzyAU2ty0oO3 KV/y235Ijt0o79e4oee2jp+Hw44drMGDzvNWYC13oq+8hM5v9JWbWm6I3UpQQtAbbU5Z qlk54FFkLWst4ophsSvfmaVJRmCgvYmD7kss8N4/Y0aGtnFLT5P4wHOiu5QhtL+Ac+rv jj2JtzsjH+hPmh+rcWAm+0GjOgqKaNuGArS1p+NdSeaDO5S4g5FKRtun91thz1MDCzhB 2KPA== 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=19e9u0UGXBpnRvX2pzbN3asd2jlypeWw3VNGtZHmGVw=; b=mJWySKOWetxzDG4Y2MkzkBfLnK8yj9VIexvq70wBwkc83DoClSPM9jIdJ2rprGiaVV pnIB7OdlgfTJC7APxATwN0+PRCDu3KU7flIWmSNIZnwmougYIlvkgzFDT/D0Xj7zWoCl EUD2tQ+ROZnopGOo/dMSVj21UYASXMTgze+/T96Stia7lNXmuviYmLtlYtx58aEXN8q6 2v/T/d1JBAjrNp1xAUu7QS64cX6KAPfseWiwMN3ZV1yYbTIt/xOcFrWIn3YxELHv3iRZ cWzzP9bjALrlLlcA2jogr/+HypLmhD3i8LxmqMVQaoV4zY0lP+7Pcq3LQgxOb2udDQ1B LJEg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=G4lbRvHM; 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 g1-20020a056402424100b0042808b06340si19817666edb.278.2022.05.10.11.23.13; Tue, 10 May 2022 11:23:39 -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=G4lbRvHM; 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 S1343668AbiEJNsU (ORCPT + 99 others); Tue, 10 May 2022 09:48:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56848 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243867AbiEJNcT (ORCPT ); Tue, 10 May 2022 09:32:19 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 002A313C4F8; Tue, 10 May 2022 06:22:48 -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 9150E6170D; Tue, 10 May 2022 13:22:48 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A2E49C385C2; Tue, 10 May 2022 13:22:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1652188968; bh=+1BH1aE1fXinu2vbVmetEVK9BjFvU/bpTvJPV1vZ2aE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=G4lbRvHM7xcfWCtF1oaoo5XT0pSnegbSwcbpYed+NXTPp4KaiQ0P/dk4ChXJH3EoB gthVDhFCFhkPpyH/b/DsWQ9ZQv1HObdjzhQFn9hRuQcTVTPbgD5zF9qzeTIWJcaGQz zIsTzmIz4SuegPTx03KoBiEF5nbgTsT/hFaNY/WM= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Duoming Zhou , "David S. Miller" Subject: [PATCH 5.4 20/52] nfc: replace improper check device_is_registered() in netlink related functions Date: Tue, 10 May 2022 15:07:49 +0200 Message-Id: <20220510130730.446023203@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220510130729.852544477@linuxfoundation.org> References: <20220510130729.852544477@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.7 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,URIBL_BLOCKED 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: Duoming Zhou commit da5c0f119203ad9728920456a0f52a6d850c01cd upstream. The device_is_registered() in nfc core is used to check whether nfc device is registered in netlink related functions such as nfc_fw_download(), nfc_dev_up() and so on. Although device_is_registered() is protected by device_lock, there is still a race condition between device_del() and device_is_registered(). The root cause is that kobject_del() in device_del() is not protected by device_lock. (cleanup task) | (netlink task) | nfc_unregister_device | nfc_fw_download device_del | device_lock ... | if (!device_is_registered)//(1) kobject_del//(2) | ... ... | device_unlock The device_is_registered() returns the value of state_in_sysfs and the state_in_sysfs is set to zero in kobject_del(). If we pass check in position (1), then set zero in position (2). As a result, the check in position (1) is useless. This patch uses bool variable instead of device_is_registered() to judge whether the nfc device is registered, which is well synchronized. Fixes: 3e256b8f8dfa ("NFC: add nfc subsystem core") Signed-off-by: Duoming Zhou Signed-off-by: David S. Miller Signed-off-by: Greg Kroah-Hartman --- net/nfc/core.c | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) --- a/net/nfc/core.c +++ b/net/nfc/core.c @@ -38,7 +38,7 @@ int nfc_fw_download(struct nfc_dev *dev, device_lock(&dev->dev); - if (!device_is_registered(&dev->dev)) { + if (dev->shutting_down) { rc = -ENODEV; goto error; } @@ -94,7 +94,7 @@ int nfc_dev_up(struct nfc_dev *dev) device_lock(&dev->dev); - if (!device_is_registered(&dev->dev)) { + if (dev->shutting_down) { rc = -ENODEV; goto error; } @@ -142,7 +142,7 @@ int nfc_dev_down(struct nfc_dev *dev) device_lock(&dev->dev); - if (!device_is_registered(&dev->dev)) { + if (dev->shutting_down) { rc = -ENODEV; goto error; } @@ -206,7 +206,7 @@ int nfc_start_poll(struct nfc_dev *dev, device_lock(&dev->dev); - if (!device_is_registered(&dev->dev)) { + if (dev->shutting_down) { rc = -ENODEV; goto error; } @@ -245,7 +245,7 @@ int nfc_stop_poll(struct nfc_dev *dev) device_lock(&dev->dev); - if (!device_is_registered(&dev->dev)) { + if (dev->shutting_down) { rc = -ENODEV; goto error; } @@ -290,7 +290,7 @@ int nfc_dep_link_up(struct nfc_dev *dev, device_lock(&dev->dev); - if (!device_is_registered(&dev->dev)) { + if (dev->shutting_down) { rc = -ENODEV; goto error; } @@ -334,7 +334,7 @@ int nfc_dep_link_down(struct nfc_dev *de device_lock(&dev->dev); - if (!device_is_registered(&dev->dev)) { + if (dev->shutting_down) { rc = -ENODEV; goto error; } @@ -400,7 +400,7 @@ int nfc_activate_target(struct nfc_dev * device_lock(&dev->dev); - if (!device_is_registered(&dev->dev)) { + if (dev->shutting_down) { rc = -ENODEV; goto error; } @@ -446,7 +446,7 @@ int nfc_deactivate_target(struct nfc_dev device_lock(&dev->dev); - if (!device_is_registered(&dev->dev)) { + if (dev->shutting_down) { rc = -ENODEV; goto error; } @@ -493,7 +493,7 @@ int nfc_data_exchange(struct nfc_dev *de device_lock(&dev->dev); - if (!device_is_registered(&dev->dev)) { + if (dev->shutting_down) { rc = -ENODEV; kfree_skb(skb); goto error; @@ -550,7 +550,7 @@ int nfc_enable_se(struct nfc_dev *dev, u device_lock(&dev->dev); - if (!device_is_registered(&dev->dev)) { + if (dev->shutting_down) { rc = -ENODEV; goto error; } @@ -599,7 +599,7 @@ int nfc_disable_se(struct nfc_dev *dev, device_lock(&dev->dev); - if (!device_is_registered(&dev->dev)) { + if (dev->shutting_down) { rc = -ENODEV; goto error; } @@ -1127,6 +1127,7 @@ int nfc_register_device(struct nfc_dev * dev->rfkill = NULL; } } + dev->shutting_down = false; device_unlock(&dev->dev); rc = nfc_genl_device_added(dev); @@ -1159,12 +1160,10 @@ void nfc_unregister_device(struct nfc_de rfkill_unregister(dev->rfkill); rfkill_destroy(dev->rfkill); } + dev->shutting_down = true; device_unlock(&dev->dev); if (dev->ops->check_presence) { - device_lock(&dev->dev); - dev->shutting_down = true; - device_unlock(&dev->dev); del_timer_sync(&dev->check_pres_timer); cancel_work_sync(&dev->check_pres_work); }