diff mbox series

Bluetooth: protect remote oob data in build_pairing_cmd's callsites

Message ID 20220913152016.38691-1-dossche.niels@gmail.com (mailing list archive)
State Superseded
Headers show
Series Bluetooth: protect remote oob data in build_pairing_cmd's callsites | expand

Checks

Context Check Description
tedd_an/pre-ci_am success Success
tedd_an/checkpatch success Checkpatch PASS
tedd_an/gitlint success Gitlint PASS
tedd_an/subjectprefix success PASS
tedd_an/buildkernel fail Build Kernel make FAIL: In file included from net/bluetooth/smp.c:32: net/bluetooth/smp.c: In function ‘smp_cmd_pairing_req’: net/bluetooth/smp.c:1806:15: error: ‘dev’ undeclared (first use in this function); did you mean ‘hdev’? 1806 | hci_dev_lock(dev); | ^~~ ./include/net/bluetooth/hci_core.h:1439:38: note: in definition of macro ‘hci_dev_lock’ 1439 | #define hci_dev_lock(d) mutex_lock(&d->lock) | ^ net/bluetooth/smp.c:1806:15: note: each undeclared identifier is reported only once for each function it appears in 1806 | hci_dev_lock(dev); | ^~~ ./include/net/bluetooth/hci_core.h:1439:38: note: in definition of macro ‘hci_dev_lock’ 1439 | #define hci_dev_lock(d) mutex_lock(&d->lock) | ^ make[2]: *** [scripts/Makefile.build:249: net/bluetooth/smp.o] Error 1 make[2]: *** Waiting for unfinished jobs.... make[1]: *** [scripts/Makefile.build:465: net/bluetooth] Error 2 make: *** [Makefile:1855: net] Error 2
tedd_an/buildkernel32 fail Build Kernel32 make FAIL: In file included from net/bluetooth/smp.c:32: net/bluetooth/smp.c: In function ‘smp_cmd_pairing_req’: net/bluetooth/smp.c:1806:15: error: ‘dev’ undeclared (first use in this function); did you mean ‘hdev’? 1806 | hci_dev_lock(dev); | ^~~ ./include/net/bluetooth/hci_core.h:1439:38: note: in definition of macro ‘hci_dev_lock’ 1439 | #define hci_dev_lock(d) mutex_lock(&d->lock) | ^ net/bluetooth/smp.c:1806:15: note: each undeclared identifier is reported only once for each function it appears in 1806 | hci_dev_lock(dev); | ^~~ ./include/net/bluetooth/hci_core.h:1439:38: note: in definition of macro ‘hci_dev_lock’ 1439 | #define hci_dev_lock(d) mutex_lock(&d->lock) | ^ make[2]: *** [scripts/Makefile.build:249: net/bluetooth/smp.o] Error 1 make[2]: *** Waiting for unfinished jobs.... make[1]: *** [scripts/Makefile.build:465: net/bluetooth] Error 2 make: *** [Makefile:1855: net] Error 2
tedd_an/incremental_build pending Incremental Build Kernel SKIP(Build Fail)
tedd_an/testrunnersetup fail Test Runner Setup Build Kernel FAIL

Commit Message

Niels Dossche Sept. 13, 2022, 3:20 p.m. UTC
Accesses to hci_dev->remote_oob_data are protected by the hdev lock,
except for the access in build_pairing_cmd via hci_find_remote_oob_data.
Adding the lock around the access in build_pairing_cmd would cause a
lock ordering problem: the l2cap_chan_lock is taken in the caller
smp_conn_security, while the hdev lock should be taken before the chan
lock.
The solution is to add the hdev lock to the callsites of
build_pairing_cmd.

Fixes: 02b05bd8b0a6 ("Bluetooth: Set SMP OOB flag if OOB data is available")
Signed-off-by: Niels Dossche <dossche.niels@gmail.com>
---

Note:
I am currently working on a static analyser to detect missing locks
using type-based static analysis, which reported the missing lock on
v6.0-rc5. I manually verified the report by looking at the code,
so that I do not send wrong information or patches.
After concluding that this seems to be a true positive, I created
this patch. I have only managed to compile-test this patch on x86_64.
After applying the patch, my analyser no longer reports the potential
bug.

 net/bluetooth/smp.c | 6 ++++++
 1 file changed, 6 insertions(+)

Comments

Niels Dossche Sept. 13, 2022, 5:40 p.m. UTC | #1
On 9/13/22 17:20, Niels Dossche wrote:
> Accesses to hci_dev->remote_oob_data are protected by the hdev lock,
> except for the access in build_pairing_cmd via hci_find_remote_oob_data.
> Adding the lock around the access in build_pairing_cmd would cause a
> lock ordering problem: the l2cap_chan_lock is taken in the caller
> smp_conn_security, while the hdev lock should be taken before the chan
> lock.
> The solution is to add the hdev lock to the callsites of
> build_pairing_cmd.
> 
> Fixes: 02b05bd8b0a6 ("Bluetooth: Set SMP OOB flag if OOB data is available")
> Signed-off-by: Niels Dossche <dossche.niels@gmail.com>
> ---
> 
> Note:
> I am currently working on a static analyser to detect missing locks
> using type-based static analysis, which reported the missing lock on
> v6.0-rc5. I manually verified the report by looking at the code,
> so that I do not send wrong information or patches.
> After concluding that this seems to be a true positive, I created
> this patch. I have only managed to compile-test this patch on x86_64.
> After applying the patch, my analyser no longer reports the potential
> bug.
> 
>  net/bluetooth/smp.c | 6 ++++++
>  1 file changed, 6 insertions(+)
> 
> diff --git a/net/bluetooth/smp.c b/net/bluetooth/smp.c
> index 11f853d0500f..3efa99fa5062 100644
> --- a/net/bluetooth/smp.c
> +++ b/net/bluetooth/smp.c
> @@ -1803,7 +1803,9 @@ static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb)
>  		return 0;
>  	}
>  
> +	hci_dev_lock(dev);
>  	build_pairing_cmd(conn, req, &rsp, auth);
> +	hci_dev_unlock(hdev);
>  
>  	if (rsp.auth_req & SMP_AUTH_SC) {
>  		set_bit(SMP_FLAG_SC, &smp->flags);
> @@ -2335,7 +2337,9 @@ static u8 smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb)
>  	skb_pull(skb, sizeof(*rp));
>  
>  	memset(&cp, 0, sizeof(cp));
> +	hci_dev_lock(hdev);
>  	build_pairing_cmd(conn, &cp, NULL, auth);
> +	hci_dev_unlock(hdev);
>  
>  	smp->preq[0] = SMP_CMD_PAIRING_REQ;
>  	memcpy(&smp->preq[1], &cp, sizeof(cp));
> @@ -2380,6 +2384,7 @@ int smp_conn_security(struct hci_conn *hcon, __u8 sec_level)
>  		return 1;
>  	}
>  
> +	hci_dev_lock(hcon->hdev);
>  	l2cap_chan_lock(chan);
>  
>  	/* If SMP is already in progress ignore this request */
> @@ -2435,6 +2440,7 @@ int smp_conn_security(struct hci_conn *hcon, __u8 sec_level)
>  
>  unlock:
>  	l2cap_chan_unlock(chan);
> +	hci_dev_unlock(hcon->hdev);
>  	return ret;
>  }
>  

I'm sorry. The build failure is because I forgot to amend my commit locally. I've sent a v2 with all the changes.
diff mbox series

Patch

diff --git a/net/bluetooth/smp.c b/net/bluetooth/smp.c
index 11f853d0500f..3efa99fa5062 100644
--- a/net/bluetooth/smp.c
+++ b/net/bluetooth/smp.c
@@ -1803,7 +1803,9 @@  static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb)
 		return 0;
 	}
 
+	hci_dev_lock(dev);
 	build_pairing_cmd(conn, req, &rsp, auth);
+	hci_dev_unlock(hdev);
 
 	if (rsp.auth_req & SMP_AUTH_SC) {
 		set_bit(SMP_FLAG_SC, &smp->flags);
@@ -2335,7 +2337,9 @@  static u8 smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb)
 	skb_pull(skb, sizeof(*rp));
 
 	memset(&cp, 0, sizeof(cp));
+	hci_dev_lock(hdev);
 	build_pairing_cmd(conn, &cp, NULL, auth);
+	hci_dev_unlock(hdev);
 
 	smp->preq[0] = SMP_CMD_PAIRING_REQ;
 	memcpy(&smp->preq[1], &cp, sizeof(cp));
@@ -2380,6 +2384,7 @@  int smp_conn_security(struct hci_conn *hcon, __u8 sec_level)
 		return 1;
 	}
 
+	hci_dev_lock(hcon->hdev);
 	l2cap_chan_lock(chan);
 
 	/* If SMP is already in progress ignore this request */
@@ -2435,6 +2440,7 @@  int smp_conn_security(struct hci_conn *hcon, __u8 sec_level)
 
 unlock:
 	l2cap_chan_unlock(chan);
+	hci_dev_unlock(hcon->hdev);
 	return ret;
 }