Bluetooth: Fix atomicity violation in {conn,adv}_{min,max}_interval_set
Subsystems (3)
PATCH 1/1 Bluetooth: Fix atomicity violation in {conn,adv}_{min,max}_interval_set
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/
--- net/bluetooth/hci_debugfs.c | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-)
net/bluetooth/hci_debugfs.c +19 -11
@@ -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);
Signature · 1 line
2.34.1
This is automated email and please do not reply to this email!
Dear submitter,
Thank you for submitting the patches to the linux bluetooth mailing list. This is a CI test results with your patch series:
PW Link:https://patchwork.kernel.org/project/bluetooth/list/?series=812452
---Test result---
Test Summary:
CheckPatch FAIL 0.79 seconds GitLint FAIL 4.01 seconds SubjectPrefix PASS 0.35 seconds BuildKernel PASS 28.00 seconds CheckAllWarning PASS 30.53 seconds CheckSparse PASS 35.88 seconds CheckSmatch PASS 104.39 seconds BuildKernel32 PASS 26.97 seconds TestRunnerSetup PASS 434.35 seconds TestRunner_l2cap-tester PASS 23.45 seconds TestRunner_iso-tester PASS 46.45 seconds TestRunner_bnep-tester PASS 7.35 seconds TestRunner_mgmt-tester FAIL 168.91 seconds TestRunner_rfcomm-tester PASS 11.21 seconds TestRunner_sco-tester PASS 14.81 seconds TestRunner_ioctl-tester PASS 12.41 seconds TestRunner_mesh-tester PASS 9.02 seconds TestRunner_smp-tester PASS 9.93 seconds TestRunner_userchan-tester PASS 7.40 seconds IncrementalBuild PASS 26.56 seconds
Details
##############################
Test: CheckPatch - FAIL
Desc: Run checkpatch.pl script
Output:
Bluetooth: Fix atomicity violation in {conn,adv}_{min,max}_interval_set
WARNING: Please use correct Fixes: style 'Fixes: <12 chars of sha1> ("<title line>")' - ie: 'Fixes: ("Bluetooth: Move LE debugfs file creation into ...")' #121:
WARNING: Reported-by: should be immediately followed by Closes: with a URL to the report #123:
ERROR: trailing whitespace
#139: FILE: net/bluetooth/hci_debugfs.c:852:
+^I$
ERROR: trailing whitespace
#142: FILE: net/bluetooth/hci_debugfs.c:855:
+^I^Ihci_dev_unlock(hdev);^I$
ERROR: trailing whitespace
#156: FILE: net/bluetooth/hci_debugfs.c:882:
+^I$
ERROR: trailing whitespace
#173: FILE: net/bluetooth/hci_debugfs.c:996:
+^I$
ERROR: trailing whitespace
#176: FILE: net/bluetooth/hci_debugfs.c:999:
+^I^Ihci_dev_unlock(hdev);^I$
total: 5 errors, 2 warnings, 0 checks, 62 lines checked
NOTE: For some of the reported defects, checkpatch may be able to
mechanically convert to the typical style using --fix or --fix-inplace.
NOTE: Whitespace errors detected.
You may wish to use scripts/cleanpatch or scripts/cleanfile
/github/workspace/src/src/13503199.patch has style problems, please review.
NOTE: Ignored message types: UNKNOWN_COMMIT_ID
NOTE: If any of the errors are false positives, please report
them to the maintainer, see CHECKPATCH in MAINTAINERS.
Use of uninitialized value $cid in concatenation (.) or string at /github/workspace/src/src/scripts/checkpatch.pl line 3228.
##############################
Test: GitLint - FAIL
Desc: Run gitlint
Output:
Bluetooth: Fix atomicity violation in {conn,adv}_{min,max}_interval_set
WARNING: I3 - ignore-body-lines: gitlint will be switching from using Python regex 'match' (match beginning) to 'search' (match anywhere) semantics. Please review your ignore-body-lines.regex option accordingly. To remove this warning, set general.regex-style-search=True. More details: https://jorisroovers.github.io/gitlint/configuration/#regex-style-search
4: B3 Line contains hard tab characters (\t): " if (val < ... || val > ... || val > hdev->le_{conn,adv}_max_interval)"
5: B3 Line contains hard tab characters (\t): " return -EINVAL;"
6: B3 Line contains hard tab characters (\t): " hci_dev_lock(hdev);"
7: B3 Line contains hard tab characters (\t): " hdev->le_{conn,adv}_min_interval = val;"
8: B3 Line contains hard tab characters (\t): " hci_dev_unlock(hdev);"
11: B3 Line contains hard tab characters (\t): " if (val < ... || val > ... || val < hdev->le_{conn,adv}_min_interval)"
12: B3 Line contains hard tab characters (\t): " return -EINVAL;"
13: B3 Line contains hard tab characters (\t): " hci_dev_lock(hdev);"
14: B3 Line contains hard tab characters (\t): " hdev->le_{conn,adv}_max_interval"
15: B3 Line contains hard tab characters (\t): " hci_dev_unlock(hdev);"
##############################
Test: TestRunner_mgmt-tester - FAIL
Desc: Run mgmt-tester with test-runner
Output:
Total: 497, Passed: 495 (99.6%), Failed: 1, Not Run: 1
Failed Test Cases
LL Privacy - Start Discovery 2 (Disable RL) Failed 0.336 seconds
---
Regards,
Linux Bluetooth
From: Gui-Dong Han
Quoted 23 lines
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_minandset_maxfuncs 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.
Quoted 7 lines
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:
net/bluetooth/hci_debugfs.c DEFINE_SHOW_ATTRIBUTE(long_term_keys); · line 860
Quoted 3 lines
hci_dev_lock(hdev);net/bluetooth/hci_debugfs.c +0
-0hdev->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)
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
Quoted 49 lines · David Laight
From: Gui-Dong Han
Quoted 23 lines
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_minandset_maxfuncs 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.Quoted 7 lines
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()andWRITE_ONCE()would make that certain.The lock sequence:
Quoted 3 lines
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)