diff mbox series

Bluetooth: Fix atomicity violation in {conn,adv}_{min,max}_interval_set

Message ID 20231222105526.9208-1-2045gemini@gmail.com
State Superseded
Headers show
Series Bluetooth: Fix atomicity violation in {conn,adv}_{min,max}_interval_set | expand

Commit Message

Gui-Dong Han Dec. 22, 2023, 10:55 a.m. UTC
In {conn,adv}_min_interval_set():
	if (val < ... || val > ... || val > hdev->le_{conn,adv}_max_interval)
		return -EINVAL;
	hci_dev_lock(hdev);
	hdev->le_{conn,adv}_min_interval = val;
	hci_dev_unlock(hdev);

In {conn,adv}_max_interval_set():
	if (val < ... || val > ... || val < hdev->le_{conn,adv}_min_interval)
		return -EINVAL;
	hci_dev_lock(hdev);
	hdev->le_{conn,adv}_max_interval
	hci_dev_unlock(hdev);

The atomicity violation occurs due to concurrent execution of set_min and
set_max funcs which may lead to inconsistent reads and writes of the min
value and the max value. The checks for value validity are ineffective as
the min/max values could change immediately after being checked, raising
the risk of the min value being greater than the max value and causing
invalid settings.

This possible bug is found by an experimental static analysis tool
developed by our team, BassCheck[1]. This tool analyzes the locking APIs
to extract function pairs that can be concurrently executed, and then
analyzes the instructions in the paired functions to identify possible
concurrency bugs including data races and atomicity violations. The above
possible bug is reported when our tool analyzes the source code of
Linux 5.17.

To resolve this issue, it is suggested to encompass the validity checks
within the locked sections in both set_min and set_max funcs. The
modification ensures that the validation of 'val' against the
current min/max values is atomic, thus maintaining the integrity of the
settings. With this patch applied, our tool no longer reports the bug,
with the kernel configuration allyesconfig for x86_64. Due to the lack of
associated hardware, we cannot test the patch in runtime testing, and just
verify it according to the code logic.

[1] https://sites.google.com/view/basscheck/

Fixes: 3a5c82b78fd28 ("Bluetooth: Move LE debugfs file creation into ...")
Cc: stable@vger.kernel.org
Reported-by: BassCheck <bass@buaa.edu.cn>
Signed-off-by: Gui-Dong Han <2045gemini@gmail.com>
---
 net/bluetooth/hci_debugfs.c | 30 +++++++++++++++++++-----------
 1 file changed, 19 insertions(+), 11 deletions(-)

Comments

Gui-Dong Han Dec. 22, 2023, 12:03 p.m. UTC | #1
Hi,

Thanks for your feedback. Let me clarify the potential issue with 
concurrent execution of setmax and setmin functions. Consider a scenario 
where setmin writes a new, valid 'min' value, and concurrently, setmax 
writes a value that is greater than the old 'min' but smaller than the 
new 'min'. In this case, setmax might check against the old 'min' value 
(before acquiring the lock) but write its value after the 'min' has been 
updated by setmin. This leads to a situation where the 'max' value ends 
up being smaller than the 'min' value, which is an inconsistency.

Regarding the lock sequence you mentioned, it's indeed from the original 
code. My patch aims to include the validity checks within the 
lock/unlock sequence to prevent the described race condition.

Thanks,
Han

On 22/12/2023 下午7:41, David Laight wrote:
> From: Gui-Dong Han
>> Sent: 22 December 2023 10:55
>>
>> In {conn,adv}_min_interval_set():
>> 	if (val < ... || val > ... || val > hdev->le_{conn,adv}_max_interval)
>> 		return -EINVAL;
>> 	hci_dev_lock(hdev);
>> 	hdev->le_{conn,adv}_min_interval = val;
>> 	hci_dev_unlock(hdev);
>>
>> In {conn,adv}_max_interval_set():
>> 	if (val < ... || val > ... || val < hdev->le_{conn,adv}_min_interval)
>> 		return -EINVAL;
>> 	hci_dev_lock(hdev);
>> 	hdev->le_{conn,adv}_max_interval
>> 	hci_dev_unlock(hdev);
>>
>> The atomicity violation occurs due to concurrent execution of set_min and
>> set_max funcs which may lead to inconsistent reads and writes of the min
>> value and the max value. The checks for value validity are ineffective as
>> the min/max values could change immediately after being checked, raising
>> the risk of the min value being greater than the max value and causing
>> invalid settings.
>>
>> This possible bug is found by an experimental static analysis tool
>> developed by our team, BassCheck[1]. This tool analyzes the locking APIs
>> to extract function pairs that can be concurrently executed, and then
>> analyzes the instructions in the paired functions to identify possible
>> concurrency bugs including data races and atomicity violations. The above
>> possible bug is reported when our tool analyzes the source code of
>> Linux 5.17.
> Your static analysis tool is basically broken.
>
> The only possible issues are if the accesses aren't atomic.
> In practise they always will be but using READ_ONCE() and
> WRITE_ONCE() would make that certain.
>
> The lock sequence:
>> 	hci_dev_lock(hdev);
>>   	hdev->le_conn_min_interval = val;
>>   	hci_dev_unlock(hdev);
> is pretty pointless - is doesn't 'lock' two+ things together.
>
> 	David
>
> -
> Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
> Registration No: 1397386 (Wales)
>
diff mbox series

Patch

diff --git a/net/bluetooth/hci_debugfs.c b/net/bluetooth/hci_debugfs.c
index 6b7741f6e95b..6fdda807f2cf 100644
--- a/net/bluetooth/hci_debugfs.c
+++ b/net/bluetooth/hci_debugfs.c
@@ -849,11 +849,13 @@  DEFINE_SHOW_ATTRIBUTE(long_term_keys);
 static int conn_min_interval_set(void *data, u64 val)
 {
 	struct hci_dev *hdev = data;
-
-	if (val < 0x0006 || val > 0x0c80 || val > hdev->le_conn_max_interval)
+	
+	hci_dev_lock(hdev);
+	if (val < 0x0006 || val > 0x0c80 || val > hdev->le_conn_max_interval) {
+		hci_dev_unlock(hdev);	
 		return -EINVAL;
+	}
 
-	hci_dev_lock(hdev);
 	hdev->le_conn_min_interval = val;
 	hci_dev_unlock(hdev);
 
@@ -877,11 +879,13 @@  DEFINE_DEBUGFS_ATTRIBUTE(conn_min_interval_fops, conn_min_interval_get,
 static int conn_max_interval_set(void *data, u64 val)
 {
 	struct hci_dev *hdev = data;
-
-	if (val < 0x0006 || val > 0x0c80 || val < hdev->le_conn_min_interval)
+	
+	hci_dev_lock(hdev);
+	if (val < 0x0006 || val > 0x0c80 || val < hdev->le_conn_min_interval) {
+		hci_dev_unlock(hdev);
 		return -EINVAL;
+	}
 
-	hci_dev_lock(hdev);
 	hdev->le_conn_max_interval = val;
 	hci_dev_unlock(hdev);
 
@@ -989,11 +993,13 @@  DEFINE_DEBUGFS_ATTRIBUTE(adv_channel_map_fops, adv_channel_map_get,
 static int adv_min_interval_set(void *data, u64 val)
 {
 	struct hci_dev *hdev = data;
-
-	if (val < 0x0020 || val > 0x4000 || val > hdev->le_adv_max_interval)
+	
+	hci_dev_lock(hdev);
+	if (val < 0x0020 || val > 0x4000 || val > hdev->le_adv_max_interval) {
+		hci_dev_unlock(hdev);	
 		return -EINVAL;
+	}
 
-	hci_dev_lock(hdev);
 	hdev->le_adv_min_interval = val;
 	hci_dev_unlock(hdev);
 
@@ -1018,10 +1024,12 @@  static int adv_max_interval_set(void *data, u64 val)
 {
 	struct hci_dev *hdev = data;
 
-	if (val < 0x0020 || val > 0x4000 || val < hdev->le_adv_min_interval)
+	hci_dev_lock(hdev);
+	if (val < 0x0020 || val > 0x4000 || val < hdev->le_adv_min_interval) {
+		hci_dev_unlock(hdev);
 		return -EINVAL;
+	}
 
-	hci_dev_lock(hdev);
 	hdev->le_adv_max_interval = val;
 	hci_dev_unlock(hdev);