Skip to content

Commit da9065c

Browse files
2045geminiVudentz
authored andcommitted
Bluetooth: Fix atomicity violation in {min,max}_key_size_set
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: 18f8124 ("Bluetooth: Move {min,max}_key_size debugfs ...") Cc: [email protected] Signed-off-by: Gui-Dong Han <[email protected]> Signed-off-by: Luiz Augusto von Dentz <[email protected]>
1 parent 3600860 commit da9065c

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

net/bluetooth/hci_debugfs.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,10 +1046,12 @@ static int min_key_size_set(void *data, u64 val)
10461046
{
10471047
struct hci_dev *hdev = data;
10481048

1049-
if (val > hdev->le_max_key_size || val < SMP_MIN_ENC_KEY_SIZE)
1049+
hci_dev_lock(hdev);
1050+
if (val > hdev->le_max_key_size || val < SMP_MIN_ENC_KEY_SIZE) {
1051+
hci_dev_unlock(hdev);
10501052
return -EINVAL;
1053+
}
10511054

1052-
hci_dev_lock(hdev);
10531055
hdev->le_min_key_size = val;
10541056
hci_dev_unlock(hdev);
10551057

@@ -1074,10 +1076,12 @@ static int max_key_size_set(void *data, u64 val)
10741076
{
10751077
struct hci_dev *hdev = data;
10761078

1077-
if (val > SMP_MAX_ENC_KEY_SIZE || val < hdev->le_min_key_size)
1079+
hci_dev_lock(hdev);
1080+
if (val > SMP_MAX_ENC_KEY_SIZE || val < hdev->le_min_key_size) {
1081+
hci_dev_unlock(hdev);
10781082
return -EINVAL;
1083+
}
10791084

1080-
hci_dev_lock(hdev);
10811085
hdev->le_max_key_size = val;
10821086
hci_dev_unlock(hdev);
10831087

0 commit comments

Comments
 (0)