Bluetooth: Fix SMP pairing method selection
Subsystems (2)
PATCH 1/2 Bluetooth: Fix SMP pairing method selection
From: Johan Hedberg <[email protected]>
The tk_request function takes the local IO capability as the second last parameter and the remote IO capability as the first parameter. They were previously swapped: when we receive a pairing response req->io_capability contains the local one and rsp->io_capability the remote one.
--- net/bluetooth/smp.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/bluetooth/smp.c +1 -1
@@ -649,7 +649,7 @@ static u8 smp_cmd_pairing_rsp(struct l2cap_conn *conn, struct sk_buff *skb) auth |= (req->auth_req | rsp->auth_req) &SMP_AUTH_MITM ; - ret =tk_request (conn,0 , auth, rsp->io_capability, req->io_capability); + ret =tk_request (conn,0 , auth, req->io_capability, rsp->io_capability);if (ret)return SMP_UNSPECIFIED ;
Signature · 1 line
1.7.10
PATCH 2/2 Bluetooth: Fix deadlock and crash when SMP pairing times out
From: Johan Hedberg <[email protected]>
The l2cap_conn_del function tries to cancel_sync the security timer, but when it's called from the timeout function itself a deadlock occurs. Subsequently the "hcon->l2cap_data = NULL" that's supposed to protect multiple calls to l2cap_conn_del never gets cleared and when the connection finally drops we double free's etc which will crash the kernel.
This patch fixes the issue by using the HCI_CONN_LE_SMP_PEND for protecting against this. The same flag is also used for the same purpose in other places in the SMP code.
--- net/bluetooth/l2cap_core.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
net/bluetooth/l2cap_core.c +6 -1
@@ -1314,7 +1314,12 @@ static void security_timeout(struct work_struct *work) struct l2cap_conn *conn = container_of(work, struct l2cap_conn, security_timer.work); -l2cap_conn_del (conn->hcon,ETIMEDOUT ); +BT_DBG ("conn %p" , conn); + +if (test_and_clear_bit (HCI_CONN_LE_SMP_PEND , &conn->hcon->flags)) { +smp_chan_destroy (conn); +l2cap_conn_del (conn->hcon,ETIMEDOUT ); + } }static struct l2cap_conn *l2cap_conn_add (struct hci_conn *hcon,u8 status)
Signature · 1 line
1.7.10
PATCH v2 1/2 Bluetooth: Fix SMP pairing method selection
In reply to Johan HedbergFrom: Johan Hedberg <[email protected]>
The tk_request function takes the local IO capability as the second last parameter and the remote IO capability as the last parameter. They were previously swapped: when we receive a pairing response req->io_capability contains the local one and rsp->io_capability the remote one.
---
v2: s/first/last/ in the commit message.
net/bluetooth/smp.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/bluetooth/smp.c +1 -1
@@ -649,7 +649,7 @@ static u8 smp_cmd_pairing_rsp(struct l2cap_conn *conn, struct sk_buff *skb) auth |= (req->auth_req | rsp->auth_req) &SMP_AUTH_MITM ; - ret =tk_request (conn,0 , auth, rsp->io_capability, req->io_capability); + ret =tk_request (conn,0 , auth, req->io_capability, rsp->io_capability);if (ret)return SMP_UNSPECIFIED ;
Signature · 1 line
1.7.10
Hi Johan,
Quoted 6 lines
The
tk_requestfunction takes the local IO capability as the second last parameter and the remote IO capability as the last parameter. They were previously swapped: when we receive a pairing responsereq->io_capabilitycontains the local one andrsp->io_capabilitythe remote one.
Quoted 6 lines
Signed-off-by Johan Hedberg <[email protected]>---
v2: s/first/last/ in the commit message.net/bluetooth/smp.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
Regards
Marcel
Re: [PATCH 2/2] Bluetooth: Fix deadlock and crash when SMP pairing times out
In reply to Johan HedbergHi Johan,
Quoted 8 lines · Johan Hedberg
From: Johan Hedberg <[email protected]>
The
l2cap_conn_delfunction tries tocancel_syncthe security timer, but when it's called from the timeout function itself a deadlock occurs. Subsequently the "hcon->l2cap_data= NULL" that's supposed to protect multiple calls tol2cap_conn_delnever gets cleared and when the connection finally drops we double free's etc which will crash the kernel.
I wonder if (inside l2cap_conn_del()) we move "hcon->l2cap_data = NULL" up in the function, probably next to the check for "!conn", would be a safer alternative.
Quoted 35 lines
This patch fixes the issue by using the
HCI_CONN_LE_SMP_PENDfor protecting against this. The same flag is also used for the same purpose in other places in the SMP code.Signed-off-by Johan Hedberg <[email protected]>--- net/bluetooth/l2cap_core.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)net/bluetooth/l2cap_core.c +6
-1@@ -1314,7 +1314,12 @@ static void security_timeout(struct work_struct *work) struct l2cap_conn *conn = container_of(work, struct l2cap_conn, security_timer.work); -l2cap_conn_del (conn->hcon,ETIMEDOUT ); +BT_DBG ("conn %p" , conn); + +if (test_and_clear_bit (HCI_CONN_LE_SMP_PEND , &conn->hcon->flags)) { +smp_chan_destroy (conn); +l2cap_conn_del (conn->hcon,ETIMEDOUT ); + } }static struct l2cap_conn *l2cap_conn_add (struct hci_conn *hcon,u8 status)Signature · 6 lines
1.7.10 -- To unsubscribe from this list: send the line "unsubscribe linux-bluetooth" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Cheers,
Signature · 1 line
Vinicius
Re: [PATCH 2/2] Bluetooth: Fix deadlock and crash when SMP pairing times out
Hi Vinicius,
Quoted 10 lines · Vinicius Costa Gomes
Quoted 6 lines
The
l2cap_conn_delfunction tries tocancel_syncthe security timer, but when it's called from the timeout function itself a deadlock occurs. Subsequently the "hcon->l2cap_data= NULL" that's supposed to protect multiple calls tol2cap_conn_delnever gets cleared and when the connection finally drops we double free's etc which will crash the kernel.I wonder if (inside
l2cap_conn_del()) we move "hcon->l2cap_data= NULL" up in the function, probably next to the check for "!conn", would be a safer alternative.
That was one of the things I tried first as well (and it did remove the crash) but it doesn't remove the deadlock. The l2cap_conn_del would still deadlock in cancel_sync(sec_timer) when called from security_timeout().
Moving the NULL assignment to the top would certainly help decrease the chance of calling l2cap_conn_del twice but it wouldn't completely remove this race condition as clearing the variable + testing its value isn't a single atomic operation.
So I'd still consider this deadlock removal patch by itself as necessary and valid but additionally the race of calling l2cap_conn_del twice would need to be fixed with a separate patch, possibly involving some new or existing lock.
Johan
Re: [PATCH 2/2] Bluetooth: Fix deadlock and crash when SMP pairing times out
Hi Johan,
Quoted 10 lines · Johan Hedberg
Hi Vinicius,
Quoted 7 lines · Vinicius Costa Gomes
Quoted 6 lines
The
l2cap_conn_delfunction tries tocancel_syncthe security timer, but when it's called from the timeout function itself a deadlock occurs. Subsequently the "hcon->l2cap_data= NULL" that's supposed to protect multiple calls tol2cap_conn_delnever gets cleared and when the connection finally drops we double free's etc which will crash the kernel.
Quoted 8 lines
Quoted 3 lines
I wonder if (inside
l2cap_conn_del()) we move "hcon->l2cap_data= NULL" up in the function, probably next to the check for "!conn", would be a safer alternative.That was one of the things I tried first as well (and it did remove the crash) but it doesn't remove the deadlock. The
l2cap_conn_delwould still deadlock incancel_sync(sec_timer)when called fromsecurity_timeout().
Have you checked approach with refcnt I sent as RFC? It overcomes those problems using similar techniques like for l2cap_chan delayed works.
Quoted 9 lines
Moving the NULL assignment to the top would certainly help decrease the chance of calling
l2cap_conn_deltwice but it wouldn't completely remove this race condition as clearing the variable + testing its value isn't a single atomic operation.So I'd still consider this deadlock removal patch by itself as necessary and valid but additionally the race of calling
l2cap_conn_deltwice would need to be fixed with a separate patch, possibly involving some new or existing lock.
I will send updated RFC today. It works for BREDR, it would be good to test it with LE.
Best regards
Andrei Emeltchenko
Hi Johan,
Quoted 8 lines · Johan Hedberg
From: Johan Hedberg <[email protected]>
The
tk_requestfunction takes the local IO capability as the second last parameter and the remote IO capability as the last parameter. They were previously swapped: when we receive a pairing responsereq->io_capabilitycontains the local one andrsp->io_capabilitythe remote one.
Quoted 6 lines
Signed-off-by Johan Hedberg <[email protected]>---
v2: s/first/last/ in the commit message.net/bluetooth/smp.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
Patch has been applied to bluetooth.git, thanks.
Gustavo
Re: [PATCH 2/2] Bluetooth: Fix deadlock and crash when SMP pairing times out
In reply to Johan HedbergHi Johan,
Quoted 9 lines · Johan Hedberg
From: Johan Hedberg <[email protected]>
The
l2cap_conn_delfunction tries tocancel_syncthe security timer, but when it's called from the timeout function itself a deadlock occurs. Subsequently the "hcon->l2cap_data= NULL" that's supposed to protect multiple calls tol2cap_conn_delnever gets cleared and when the connection finally drops we double free's etc which will crash the kernel.
Quoted 8 lines
This patch fixes the issue by using the
HCI_CONN_LE_SMP_PENDfor protecting against this. The same flag is also used for the same purpose in other places in the SMP code.Signed-off-by Johan Hedberg <[email protected]>--- net/bluetooth/l2cap_core.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
It looks ok to me, patch has been applied to bluetooth.git. Thanks.
Gustavo