CVE-2022-1974: Time-of-check Time-of-use (TOCTOU) Race Condition (CWE-367)
value: A use-after-free flaw was found in the Linux kernel's NFC core functionality due to a race condition between kobject creation and delete. This vulnerability allows a local attacker with CAP_NET_ADMIN privilege to leak kernel information.
CWE description: The product checks the state of a resource before using that resource, but the resource's state can change between the check and the use in a way that invalidates the results of the check. This can cause the product to perform invalid actions when the resource is in an unexpected state.
CVSS v3 base score: 4.1
Published: 2022-08-31T16:15Z
Repository: https://github.com/torvalds/linux
Language: C
Files changed: net/nfc/core.c
Commit date: 2022-05-01 13:26:05 +0100
Commit message: nfc: replace improper check device_is_registered() in netlink related functions

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 <duoming@zju.edu.cn>
Signed-off-by: David S. Miller <davem@davemloft.net>

Diff: diff --git a/net/nfc/core.c b/net/nfc/core.c
index dc7a2404efdf..5b286e1e0a6f 100644
--- a/net/nfc/core.c
+++ b/net/nfc/core.c
@@ -36,11 +36,11 @@ int nfc_fw_download(struct nfc_dev *dev, const char *firmware_name)
 
 	pr_debug("%s do firmware %s\n", dev_name(&dev->dev), firmware_name);
 
 	device_lock(&dev->dev);
 
-	if (!device_is_registered(&dev->dev)) {
+	if (dev->shutting_down) {
 		rc = -ENODEV;
 		goto error;
 	}
 
 	if (dev->dev_up) {
@@ -92,11 +92,11 @@ int nfc_dev_up(struct nfc_dev *dev)
 
 	pr_debug("dev_name=%s\n", dev_name(&dev->dev));
 
 	device_lock(&dev->dev);
 
-	if (!device_is_registered(&dev->dev)) {
+	if (dev->shutting_down) {
 		rc = -ENODEV;
 		goto error;
 	}
 
 	if (dev->rfkill && rfkill_blocked(dev->rfkill)) {
@@ -140,11 +140,11 @@ int nfc_dev_down(struct nfc_dev *dev)
 
 	pr_debug("dev_name=%s\n", dev_name(&dev->dev));
 
 	device_lock(&dev->dev);
 
-	if (!device_is_registered(&dev->dev)) {
+	if (dev->shutting_down) {
 		rc = -ENODEV;
 		goto error;
 	}
 
 	if (!dev->dev_up) {
@@ -205,11 +205,11 @@ int nfc_start_poll(struct nfc_dev *dev, u32 im_protocols, u32 tm_protocols)
 	if (!im_protocols && !tm_protocols)
 		return -EINVAL;
 
 	device_lock(&dev->dev);
 
-	if (!device_is_registered(&dev->dev)) {
+	if (dev->shutting_down) {
 		rc = -ENODEV;
 		goto error;
 	}
 
 	if (!dev->dev_up) {
@@ -244,11 +244,11 @@ int nfc_stop_poll(struct nfc_dev *dev)
 
 	pr_debug("dev_name=%s\n", dev_name(&dev->dev));
 
 	device_lock(&dev->dev);
 
-	if (!device_is_registered(&dev->dev)) {
+	if (dev->shutting_down) {
 		rc = -ENODEV;
 		goto error;
 	}
 
 	if (!dev->polling) {
@@ -289,11 +289,11 @@ int nfc_dep_link_up(struct nfc_dev *dev, int target_index, u8 comm_mode)
 	if (!dev->ops->dep_link_up)
 		return -EOPNOTSUPP;
 
 	device_lock(&dev->dev);
 
-	if (!device_is_registered(&dev->dev)) {
+	if (dev->shutting_down) {
 		rc = -ENODEV;
 		goto error;
 	}
 
 	if (dev->dep_link_up == true) {
@@ -333,11 +333,11 @@ int nfc_dep_link_down(struct nfc_dev *dev)
 	if (!dev->ops->dep_link_down)
 		return -EOPNOTSUPP;
 
 	device_lock(&dev->dev);
 
-	if (!device_is_registered(&dev->dev)) {
+	if (dev->shutting_down) {
 		rc = -ENODEV;
 		goto error;
 	}
 
 	if (dev->dep_link_up == false) {
@@ -399,11 +399,11 @@ int nfc_activate_target(struct nfc_dev *dev, u32 target_idx, u32 protocol)
 	pr_debug("dev_name=%s target_idx=%u protocol=%u\n",
 		 dev_name(&dev->dev), target_idx, protocol);
 
 	device_lock(&dev->dev);
 
-	if (!device_is_registered(&dev->dev)) {
+	if (dev->shutting_down) {
 		rc = -ENODEV;
 		goto error;
 	}
 
 	if (dev->active_target) {
@@ -446,11 +446,11 @@ int nfc_deactivate_target(struct nfc_dev *dev, u32 target_idx, u8 mode)
 	pr_debug("dev_name=%s target_idx=%u\n",
 		 dev_name(&dev->dev), target_idx);
 
 	device_lock(&dev->dev);
 
-	if (!device_is_registered(&dev->dev)) {
+	if (dev->shutting_down) {
 		rc = -ENODEV;
 		goto error;
 	}
 
 	if (dev->active_target == NULL) {
@@ -493,11 +493,11 @@ int nfc_data_exchange(struct nfc_dev *dev, u32 target_idx, struct sk_buff *skb,
 	pr_debug("dev_name=%s target_idx=%u skb->len=%u\n",
 		 dev_name(&dev->dev), target_idx, skb->len);
 
 	device_lock(&dev->dev);
 
-	if (!device_is_registered(&dev->dev)) {
+	if (dev->shutting_down) {
 		rc = -ENODEV;
 		kfree_skb(skb);
 		goto error;
 	}
 
@@ -550,11 +550,11 @@ int nfc_enable_se(struct nfc_dev *dev, u32 se_idx)
 
 	pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
 
 	device_lock(&dev->dev);
 
-	if (!device_is_registered(&dev->dev)) {
+	if (dev->shutting_down) {
 		rc = -ENODEV;
 		goto error;
 	}
 
 	if (!dev->dev_up) {
@@ -599,11 +599,11 @@ int nfc_disable_se(struct nfc_dev *dev, u32 se_idx)
 
 	pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
 
 	device_lock(&dev->dev);
 
-	if (!device_is_registered(&dev->dev)) {
+	if (dev->shutting_down) {
 		rc = -ENODEV;
 		goto error;
 	}
 
 	if (!dev->dev_up) {
@@ -1132,10 +1132,11 @@ int nfc_register_device(struct nfc_dev *dev)
 		if (rfkill_register(dev->rfkill) < 0) {
 			rfkill_destroy(dev->rfkill);
 			dev->rfkill = NULL;
 		}
 	}
+	dev->shutting_down = false;
 	device_unlock(&dev->dev);
 
 	rc = nfc_genl_device_added(dev);
 	if (rc)
 		pr_debug("The userspace won't be notified that the device %s was added\n",
@@ -1164,16 +1165,14 @@ void nfc_unregister_device(struct nfc_dev *dev)
 	device_lock(&dev->dev);
 	if (dev->rfkill) {
 		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);
 	}
 
 	nfc_llcp_unregister_device(dev);

Vulnerable code: 	if (!device_is_registered(&dev->dev)) {
	if (!device_is_registered(&dev->dev)) {
	if (!device_is_registered(&dev->dev)) {
	if (!device_is_registered(&dev->dev)) {
	if (!device_is_registered(&dev->dev)) {
	if (!device_is_registered(&dev->dev)) {
	if (!device_is_registered(&dev->dev)) {
	if (!device_is_registered(&dev->dev)) {
	if (!device_is_registered(&dev->dev)) {
	if (!device_is_registered(&dev->dev)) {
	if (!device_is_registered(&dev->dev)) {
	if (!device_is_registered(&dev->dev)) {
		device_lock(&dev->dev);
		dev->shutting_down = true;
		device_unlock(&dev->dev);
Fixed code: 	if (dev->shutting_down) {
	if (dev->shutting_down) {
	if (dev->shutting_down) {
	if (dev->shutting_down) {
	if (dev->shutting_down) {
	if (dev->shutting_down) {
	if (dev->shutting_down) {
	if (dev->shutting_down) {
	if (dev->shutting_down) {
	if (dev->shutting_down) {
	if (dev->shutting_down) {
	if (dev->shutting_down) {
	dev->shutting_down = false;
	dev->shutting_down = true;