* [PATCH v2] Bluetooth: Fix atomicity violation in {min,max}_key_size_set
@ 2023-12-22 15:12 Gui-Dong Han
2023-12-22 15:58 ` [v2] " bluez.test.bot
2024-01-11 8:45 ` [PATCH v2] " Gui-Dong Han
0 siblings, 2 replies; 3+ messages in thread
From: Gui-Dong Han @ 2023-12-22 15:12 UTC (permalink / raw)
To: marcel, johan.hedberg, luiz.dentz
Cc: linux-bluetooth, linux-kernel, baijiaju1990, Gui-Dong Han, stable
In min_key_size_set():
if (val > hdev->le_max_key_size || val < SMP_MIN_ENC_KEY_SIZE)
return -EINVAL;
hci_dev_lock(hdev);
hdev->le_min_key_size = val;
hci_dev_unlock(hdev);
In max_key_size_set():
if (val > SMP_MAX_ENC_KEY_SIZE || val < hdev->le_min_key_size)
return -EINVAL;
hci_dev_lock(hdev);
hdev->le_max_key_size = val;
hci_dev_unlock(hdev);
The atomicity violation occurs due to concurrent execution of set_min and
set_max funcs.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.
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: 18f81241b74f ("Bluetooth: Move {min,max}_key_size debugfs ...")
Cc: stable@vger.kernel.org
Signed-off-by: Gui-Dong Han <2045gemini@gmail.com>
---
v2:
* Adjust the format to pass the CI.
---
net/bluetooth/hci_debugfs.c | 16 ++++++++++------
1 file changed, 10 insertions(+), 6 deletions(-)
diff --git a/net/bluetooth/hci_debugfs.c b/net/bluetooth/hci_debugfs.c
index 6b7741f6e95b..3ffbf3f25363 100644
--- a/net/bluetooth/hci_debugfs.c
+++ b/net/bluetooth/hci_debugfs.c
@@ -1045,11 +1045,13 @@ DEFINE_DEBUGFS_ATTRIBUTE(adv_max_interval_fops, adv_max_interval_get,
static int min_key_size_set(void *data, u64 val)
{
struct hci_dev *hdev = data;
-
- if (val > hdev->le_max_key_size || val < SMP_MIN_ENC_KEY_SIZE)
+
+ hci_dev_lock(hdev);
+ if (val > hdev->le_max_key_size || val < SMP_MIN_ENC_KEY_SIZE) {
+ hci_dev_unlock(hdev);
return -EINVAL;
+ }
- hci_dev_lock(hdev);
hdev->le_min_key_size = val;
hci_dev_unlock(hdev);
@@ -1073,11 +1075,13 @@ DEFINE_DEBUGFS_ATTRIBUTE(min_key_size_fops, min_key_size_get,
static int max_key_size_set(void *data, u64 val)
{
struct hci_dev *hdev = data;
-
- if (val > SMP_MAX_ENC_KEY_SIZE || val < hdev->le_min_key_size)
+
+ hci_dev_lock(hdev);
+ if (val > SMP_MAX_ENC_KEY_SIZE || val < hdev->le_min_key_size) {
+ hci_dev_unlock(hdev);
return -EINVAL;
+ }
- hci_dev_lock(hdev);
hdev->le_max_key_size = val;
hci_dev_unlock(hdev);
--
2.34.1
^ permalink raw reply related [flat|nested] 3+ messages in thread
* RE: [v2] Bluetooth: Fix atomicity violation in {min,max}_key_size_set
2023-12-22 15:12 [PATCH v2] Bluetooth: Fix atomicity violation in {min,max}_key_size_set Gui-Dong Han
@ 2023-12-22 15:58 ` bluez.test.bot
2024-01-11 8:45 ` [PATCH v2] " Gui-Dong Han
1 sibling, 0 replies; 3+ messages in thread
From: bluez.test.bot @ 2023-12-22 15:58 UTC (permalink / raw)
To: linux-bluetooth, 2045gemini
[-- Attachment #1: Type: text/plain, Size: 1423 bytes --]
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=812520
---Test result---
Test Summary:
CheckPatch PASS 1.55 seconds
GitLint PASS 0.54 seconds
SubjectPrefix PASS 0.07 seconds
BuildKernel PASS 29.23 seconds
CheckAllWarning PASS 31.03 seconds
CheckSparse PASS 36.97 seconds
CheckSmatch PASS 101.71 seconds
BuildKernel32 PASS 27.72 seconds
TestRunnerSetup PASS 447.68 seconds
TestRunner_l2cap-tester PASS 23.94 seconds
TestRunner_iso-tester PASS 46.71 seconds
TestRunner_bnep-tester PASS 6.99 seconds
TestRunner_mgmt-tester PASS 161.13 seconds
TestRunner_rfcomm-tester PASS 10.85 seconds
TestRunner_sco-tester PASS 14.50 seconds
TestRunner_ioctl-tester PASS 12.27 seconds
TestRunner_mesh-tester PASS 8.83 seconds
TestRunner_smp-tester PASS 9.77 seconds
TestRunner_userchan-tester PASS 7.22 seconds
IncrementalBuild PASS 25.57 seconds
---
Regards,
Linux Bluetooth
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH v2] Bluetooth: Fix atomicity violation in {min,max}_key_size_set
2023-12-22 15:12 [PATCH v2] Bluetooth: Fix atomicity violation in {min,max}_key_size_set Gui-Dong Han
2023-12-22 15:58 ` [v2] " bluez.test.bot
@ 2024-01-11 8:45 ` Gui-Dong Han
1 sibling, 0 replies; 3+ messages in thread
From: Gui-Dong Han @ 2024-01-11 8:45 UTC (permalink / raw)
To: marcel, johan.hedberg, luiz.dentz
Cc: linux-bluetooth, linux-kernel, baijiaju1990, stable
Dear All, I hope this message finds you well. Following up on my
previous email regarding the patch, I want to highlight an important
aspect. In /net/bluetooth/hci_debugfs.c, there are four similar bugs
related to atomicity violations, potentially leading to minimum values
exceeding maximum ones.
https://patchwork.kernel.org/project/bluetooth/patch/20231222162931.6553-1-2045gemini@gmail.com/
https://patchwork.kernel.org/project/bluetooth/patch/20231222162310.6461-1-2045gemini@gmail.com/
https://patchwork.kernel.org/project/bluetooth/patch/20231222161317.6255-1-2045gemini@gmail.com/
https://patchwork.kernel.org/project/bluetooth/patch/20231222151241.4331-1-2045gemini@gmail.com/
I kindly request your review of these issues alongside the original
patch. Understanding your busy schedules, your prompt attention to this
consolidated review would be greatly appreciated. Thank you for your
time and consideration. Best regards, Han
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2024-01-11 8:45 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-12-22 15:12 [PATCH v2] Bluetooth: Fix atomicity violation in {min,max}_key_size_set Gui-Dong Han
2023-12-22 15:58 ` [v2] " bluez.test.bot
2024-01-11 8:45 ` [PATCH v2] " Gui-Dong Han
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).