diff mbox series

Bluetooth: Fix atomicity violation in {min,max}_key_size_set

Message ID 20231222105813.9245-1-2045gemini@gmail.com (mailing list archive)
State Superseded
Headers show
Series Bluetooth: Fix atomicity violation in {min,max}_key_size_set | expand

Checks

Context Check Description
tedd_an/pre-ci_am success Success
tedd_an/CheckPatch fail WARNING: Please use correct Fixes: style 'Fixes: <12 chars of sha1> ("<title line>")' - ie: 'Fixes: ("Bluetooth: Move {min,max}_key_size debugfs ...")' #120: Fixes: 18f81241b74fb ("Bluetooth: Move {min,max}_key_size debugfs ...") WARNING: Reported-by: should be immediately followed by Closes: with a URL to the report #122: Reported-by: BassCheck <bass@buaa.edu.cn> Signed-off-by: Gui-Dong Han <2045gemini@gmail.com> ERROR: trailing whitespace #138: FILE: net/bluetooth/hci_debugfs.c:1048: +^I$ ERROR: trailing whitespace #155: FILE: net/bluetooth/hci_debugfs.c:1078: +^I$ total: 2 errors, 2 warnings, 0 checks, 32 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/13503200.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.
tedd_an/GitLint fail 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 > hdev->le_max_key_size || val < SMP_MIN_ENC_KEY_SIZE)" 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_min_key_size = val;" 8: B3 Line contains hard tab characters (\t): " hci_dev_unlock(hdev);" 11: B3 Line contains hard tab characters (\t): " if (val > SMP_MAX_ENC_KEY_SIZE || val < hdev->le_min_key_size)" 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_max_key_size = val;" 15: B3 Line contains hard tab characters (\t): " hci_dev_unlock(hdev);"
tedd_an/SubjectPrefix success Gitlint PASS
tedd_an/BuildKernel success BuildKernel PASS
tedd_an/CheckAllWarning success CheckAllWarning PASS
tedd_an/CheckSparse success CheckSparse PASS
tedd_an/CheckSmatch success CheckSparse PASS
tedd_an/BuildKernel32 success BuildKernel32 PASS
tedd_an/TestRunnerSetup success TestRunnerSetup PASS
tedd_an/TestRunner_l2cap-tester success TestRunner PASS
tedd_an/TestRunner_iso-tester success TestRunner PASS
tedd_an/TestRunner_bnep-tester success TestRunner PASS
tedd_an/TestRunner_mgmt-tester success TestRunner PASS
tedd_an/TestRunner_rfcomm-tester success TestRunner PASS
tedd_an/TestRunner_sco-tester success TestRunner PASS
tedd_an/TestRunner_ioctl-tester success TestRunner PASS
tedd_an/TestRunner_mesh-tester success TestRunner PASS
tedd_an/TestRunner_smp-tester success TestRunner PASS
tedd_an/TestRunner_userchan-tester success TestRunner PASS
tedd_an/IncrementalBuild success Incremental Build PASS

Commit Message

Gui-Dong Han Dec. 22, 2023, 10:58 a.m. UTC
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 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: 18f81241b74fb ("Bluetooth: Move {min,max}_key_size debugfs ...")
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 | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)

Comments

bluez.test.bot@gmail.com Dec. 22, 2023, 11:31 a.m. UTC | #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=812453

---Test result---

Test Summary:
CheckPatch                    FAIL      0.86 seconds
GitLint                       FAIL      4.55 seconds
SubjectPrefix                 PASS      0.29 seconds
BuildKernel                   PASS      28.09 seconds
CheckAllWarning               PASS      30.56 seconds
CheckSparse                   PASS      36.39 seconds
CheckSmatch                   PASS      104.63 seconds
BuildKernel32                 PASS      27.69 seconds
TestRunnerSetup               PASS      437.81 seconds
TestRunner_l2cap-tester       PASS      23.23 seconds
TestRunner_iso-tester         PASS      43.32 seconds
TestRunner_bnep-tester        PASS      7.37 seconds
TestRunner_mgmt-tester        PASS      164.45 seconds
TestRunner_rfcomm-tester      PASS      11.13 seconds
TestRunner_sco-tester         PASS      14.89 seconds
TestRunner_ioctl-tester       PASS      12.20 seconds
TestRunner_mesh-tester        PASS      8.92 seconds
TestRunner_smp-tester         PASS      10.32 seconds
TestRunner_userchan-tester    PASS      7.44 seconds
IncrementalBuild              PASS      26.40 seconds

Details
##############################
Test: CheckPatch - FAIL
Desc: Run checkpatch.pl script
Output:
Bluetooth: Fix atomicity violation in {min,max}_key_size_set
WARNING: Please use correct Fixes: style 'Fixes: <12 chars of sha1> ("<title line>")' - ie: 'Fixes:  ("Bluetooth: Move {min,max}_key_size debugfs ...")'
#120: 
Fixes: 18f81241b74fb ("Bluetooth: Move {min,max}_key_size debugfs ...")

WARNING: Reported-by: should be immediately followed by Closes: with a URL to the report
#122: 
Reported-by: BassCheck <bass@buaa.edu.cn>
Signed-off-by: Gui-Dong Han <2045gemini@gmail.com>

ERROR: trailing whitespace
#138: FILE: net/bluetooth/hci_debugfs.c:1048:
+^I$

ERROR: trailing whitespace
#155: FILE: net/bluetooth/hci_debugfs.c:1078:
+^I$

total: 2 errors, 2 warnings, 0 checks, 32 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/13503200.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 {min,max}_key_size_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 > hdev->le_max_key_size || val < SMP_MIN_ENC_KEY_SIZE)"
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_min_key_size = val;"
8: B3 Line contains hard tab characters (\t): "	hci_dev_unlock(hdev);"
11: B3 Line contains hard tab characters (\t): "	if (val > SMP_MAX_ENC_KEY_SIZE || val < hdev->le_min_key_size)"
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_max_key_size = val;"
15: B3 Line contains hard tab characters (\t): "	hci_dev_unlock(hdev);"


---
Regards,
Linux Bluetooth
diff mbox series

Patch

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);