Linux bluetooth development
 help / color / mirror / Atom feed
From: Luiz Augusto von Dentz <luiz.dentz@gmail.com>
To: linux-bluetooth@vger.kernel.org
Subject: [PATCH v1 14/16] Bluetooth: hci_debugfs: Fix racy toggling of debugfs controlled flags
Date: Tue, 17 Nov 2026 13:57:35 -0500	[thread overview]
Message-ID: <20261117185737.3758255-15-luiz.dentz@gmail.com> (raw)
In-Reply-To: <20261117185737.3758255-1-luiz.dentz@gmail.com>

From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

The debugfs writers checked the current state of the flag, then blindly
inverted it with hci_dev_change_flag(). Two concurrent writers both pass
the check and the second inversion undoes the first one, leaving the flag
out of sync with the actual controller/SMP channel state (e.g. DUT mode
flag vs controller, or the BR/EDR SMP channel being leaked or removed
twice).

Set or clear the flag explicitly instead, redo the dut_mode check under
hci_req_sync_lock() and serialize smp_force_bredr() with hdev->lock.

Reported-by: sashiko.dev (linux-442108d5-76ce-44a1-a84d-16b44d6a26a6)
Fixes: 4b4148e9acc1 ("Bluetooth: Add support for setting DUT mode")
Assisted-by: OpenCode:claude-fable-5.2
Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
---
 net/bluetooth/hci_debugfs.c | 30 ++++++++++++++++++++++++------
 net/bluetooth/smp.c         | 25 +++++++++++++++++++------
 2 files changed, 43 insertions(+), 12 deletions(-)

diff --git a/net/bluetooth/hci_debugfs.c b/net/bluetooth/hci_debugfs.c
index cd5ef398f238..06bb399c86d1 100644
--- a/net/bluetooth/hci_debugfs.c
+++ b/net/bluetooth/hci_debugfs.c
@@ -777,7 +777,10 @@ static ssize_t force_static_address_write(struct file *file,
 	if (enable == hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR))
 		return -EALREADY;
 
-	hci_dev_change_flag(hdev, HCI_FORCE_STATIC_ADDR);
+	if (enable)
+		hci_dev_set_flag(hdev, HCI_FORCE_STATIC_ADDR);
+	else
+		hci_dev_clear_flag(hdev, HCI_FORCE_STATIC_ADDR);
 
 	return count;
 }
@@ -1177,7 +1180,10 @@ static ssize_t force_no_mitm_write(struct file *file,
 	if (enable == hci_dev_test_flag(hdev, HCI_FORCE_NO_MITM))
 		return -EALREADY;
 
-	hci_dev_change_flag(hdev, HCI_FORCE_NO_MITM);
+	if (enable)
+		hci_dev_set_flag(hdev, HCI_FORCE_NO_MITM);
+	else
+		hci_dev_clear_flag(hdev, HCI_FORCE_NO_MITM);
 
 	return count;
 }
@@ -1303,20 +1309,32 @@ static ssize_t dut_mode_write(struct file *file, const char __user *user_buf,
 		return -EALREADY;
 
 	hci_req_sync_lock(hdev);
+
+	/* Check again now that concurrent writers are excluded */
+	if (enable == hci_dev_test_flag(hdev, HCI_DUT_MODE)) {
+		hci_req_sync_unlock(hdev);
+		return -EALREADY;
+	}
+
 	if (enable)
 		skb = __hci_cmd_sync(hdev, HCI_OP_ENABLE_DUT_MODE, 0, NULL,
 				     HCI_CMD_TIMEOUT);
 	else
 		skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL,
 				     HCI_CMD_TIMEOUT);
-	hci_req_sync_unlock(hdev);
-
-	if (IS_ERR(skb))
+	if (IS_ERR(skb)) {
+		hci_req_sync_unlock(hdev);
 		return PTR_ERR(skb);
+	}
 
 	kfree_skb(skb);
 
-	hci_dev_change_flag(hdev, HCI_DUT_MODE);
+	if (enable)
+		hci_dev_set_flag(hdev, HCI_DUT_MODE);
+	else
+		hci_dev_clear_flag(hdev, HCI_DUT_MODE);
+
+	hci_req_sync_unlock(hdev);
 
 	return count;
 }
diff --git a/net/bluetooth/smp.c b/net/bluetooth/smp.c
index 1c315c9ee7dd..ba3573964803 100644
--- a/net/bluetooth/smp.c
+++ b/net/bluetooth/smp.c
@@ -3438,8 +3438,14 @@ static void smp_del_chan(struct l2cap_chan *chan)
 
 int smp_force_bredr(struct hci_dev *hdev, bool enable)
 {
-	if (enable == hci_dev_test_flag(hdev, HCI_FORCE_BREDR_SMP))
-		return -EALREADY;
+	int err = 0;
+
+	hci_dev_lock(hdev);
+
+	if (enable == hci_dev_test_flag(hdev, HCI_FORCE_BREDR_SMP)) {
+		err = -EALREADY;
+		goto unlock;
+	}
 
 	/* The channels are only registered while the controller is powered
 	 * and LE capable. Otherwise just record the flag, smp_register() will
@@ -3450,8 +3456,10 @@ int smp_force_bredr(struct hci_dev *hdev, bool enable)
 
 		if (hdev->smp_data && !hdev->smp_bredr_data) {
 			chan = smp_add_cid(hdev, L2CAP_CID_SMP_BREDR);
-			if (IS_ERR(chan))
-				return PTR_ERR(chan);
+			if (IS_ERR(chan)) {
+				err = PTR_ERR(chan);
+				goto unlock;
+			}
 
 			hdev->smp_bredr_data = chan;
 		}
@@ -3465,9 +3473,14 @@ int smp_force_bredr(struct hci_dev *hdev, bool enable)
 		}
 	}
 
-	hci_dev_change_flag(hdev, HCI_FORCE_BREDR_SMP);
+	if (enable)
+		hci_dev_set_flag(hdev, HCI_FORCE_BREDR_SMP);
+	else
+		hci_dev_clear_flag(hdev, HCI_FORCE_BREDR_SMP);
 
-	return 0;
+unlock:
+	hci_dev_unlock(hdev);
+	return err;
 }
 
 int smp_register(struct hci_dev *hdev)
-- 
2.56.0


  parent reply	other threads:[~2026-11-17 18:57 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-17 18:57 [PATCH v1 00/16] Bluetooth: Fixes for issues reported by sashiko.dev Luiz Augusto von Dentz
2026-11-17 18:57 ` [PATCH v1 01/16] Bluetooth: mgmt: Fix read_index_list() buffer overflow Luiz Augusto von Dentz
2026-11-17 20:13   ` Bluetooth: Fixes for issues reported by sashiko.dev bluez.test.bot
2026-11-17 18:57 ` [PATCH v1 02/16] Bluetooth: hci_core: Take req_sync lock in hci_power_on() Luiz Augusto von Dentz
2026-11-17 18:57 ` [PATCH v1 03/16] Bluetooth: hci_sync: Lock the lookup in hci_disable_ext_adv_instance_sync() Luiz Augusto von Dentz
2026-11-17 18:57 ` [PATCH v1 04/16] Bluetooth: hci_sync: Hold hdev->lock in hci_schedule_adv_instance_sync() Luiz Augusto von Dentz
2026-11-17 18:57 ` [PATCH v1 05/16] Bluetooth: SMP: Serialize link key updates from smp_notify_keys() Luiz Augusto von Dentz
2026-11-17 18:57 ` [PATCH v1 06/16] Bluetooth: SMP: Check smp_data before using SMP_DEV() Luiz Augusto von Dentz
2026-11-17 18:57 ` [PATCH v1 07/16] Bluetooth: SMP: Don't create the BR/EDR channel while unregistered Luiz Augusto von Dentz
2026-11-17 18:57 ` [PATCH v1 08/16] Bluetooth: hci_event: Validate sent command length in LE Set Ext Adv Enable Luiz Augusto von Dentz
2026-11-17 18:57 ` [PATCH v1 09/16] Bluetooth: mgmt: Reject zero multi advertising rotation duration Luiz Augusto von Dentz
2026-11-17 18:57 ` [PATCH v1 10/16] Bluetooth: L2CAP: Don't use uninitialized EFS in l2cap_parse_conf_rsp() Luiz Augusto von Dentz
2026-11-17 18:57 ` [PATCH v1 11/16] Bluetooth: mgmt: Fix mesh transmission locking and lifetime Luiz Augusto von Dentz
2026-11-17 18:57 ` [PATCH v1 12/16] Bluetooth: MGMT: Fix use-after-free in mgmt_powering_down() Luiz Augusto von Dentz
2026-11-17 18:57 ` [PATCH v1 13/16] Bluetooth: MSFT: Protect the event prefix with hdev->lock Luiz Augusto von Dentz
2026-11-17 18:57 ` Luiz Augusto von Dentz [this message]
2026-11-17 18:57 ` [PATCH v1 15/16] Bluetooth: hci_core: Hold hdev->lock when removing advertisement monitors Luiz Augusto von Dentz
2026-11-17 18:57 ` [PATCH v1 16/16] Bluetooth: RFCOMM: Serialize tty device reparenting Luiz Augusto von Dentz

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261117185737.3758255-15-luiz.dentz@gmail.com \
    --to=luiz.dentz@gmail.com \
    --cc=linux-bluetooth@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox