L2CAP: Fix channels being established with insufficient security
Subsystems (2)
Luiz Augusto von Dentz
maintainerv5 cover L2CAP: Fix channels being established with insufficient security
From: Luiz Augusto von Dentz <[email protected]>
l2cap_security_cfm() lowers the security level of a channel to the one of the link when encryption completes, and accepts channels waiting for the security to be met as long as the status is success, so a channel can end up established, or be offered to a listener using BT_DEFER_SETUP, over a link that doesn't meet the security level it requires, e.g. if the peer disables encryption while the channel waits for userspace to authorize it.
The first patch makes l2cap_security_cfm() compare the security level of the link with the one required by the channel, which is now kept apart from the one of the link in min_sec_level: encryption completing below it is a downgrade, so the channel is no longer lowered to match it but disconnected, or refused if it was still pending, and the link is disconnected if the channel is a fixed one since it can't be disconnected at L2CAP. Channels waiting to be accepted or started only proceed if the link is encrypted and meets their security level, and deferred LE and Enhanced Credit Based channels are refused when userspace authorizes them if it no longer does.
The second patch makes a BR/EDR channel visible to the listener only once it is offered to it, after the security is met, instead of as soon as the connection request is responded as pending, so a listener that accepts without waiting to be notified can't authorize a connection while authentication is still pending.
Tests for both are sent to the BlueZ mailing list as "[PATCH BlueZ v4 0/5] Add tests for security of deferred setup".
A channel requiring a security level that pairing can't provide, e.g. BT_SECURITY_HIGH when the IO capability is NoInputNoOutput, is now refused instead of being connected with the security level the link ended up with.
v5:
- Require the link to be encrypted for channels above BT_SECURITY_LOW, since the security level of the link is updated when authentication completes, before encryption is enabled.
- Disconnect the link if a fixed channel is downgraded no matter its state, so one still waiting for security doesn't bypass it.
- Keep the security level required by a channel in min_sec_level instead of comparing with sec_level, which follows the one of the link, so channels that only asked for BT_SECURITY_LOW, like SMP, are not considered downgraded when the link is paired again with a lower security level.
- Refuse deferred LE Credit Based and Enhanced Credit Based channels when userspace authorizes them if the link doesn't meet their security level, instead of closing them from l2cap_security_cfm(), since rejecting an Enhanced Credit Based channel deletes the others of the same request.
- Refuse deferred LE Credit Based channels in l2cap_security_cfm() when the link doesn't meet their security level.
v4:
- Disconnect the link instead of the channel if a fixed channel is downgraded, since it can't be disconnected at L2CAP.
- Call chan->ops->close() when a channel was deleted by the downgrade, so its socket is not leaked, and also refuse deferred LE Credit Based channels in BT_CONNECT2.
- Only start channels in BT_CONNECT if the link meets their security level.
v3:
- Don't use l2cap_chan_check_security(), which may start authentication or encryption, compare the security levels instead.
- Treat encryption completing below the security level of a channel as a downgrade and disconnect it instead of lowering its security level.
- Only let accept() return children in BT_CONNECT2, as bt_accept_poll() does.
v2:
- Drop BT_SK_ACCEPTED and the changes to l2cap_sock_defer().
- Add the second patch for listeners accepting while the security is still pending.
Luiz Augusto von Dentz (1):
Bluetooth: L2CAP: Don't offer deferred channels before security is met
Mohammed Alquraini (1):
Bluetooth: L2CAP: Fix accepting channels with insufficient security
include/net/bluetooth/l2cap.h | 6 +++ net/bluetooth/af_bluetooth.c | 6 ++- net/bluetooth/l2cap_core.c | 83 ++++++++++++++++++++++++++++++++--- net/bluetooth/l2cap_sock.c | 43 ++++++++++++++---- net/bluetooth/rfcomm/core.c | 1 + 5 files changed, 124 insertions(+), 15 deletions(-)
Signature · 1 line
2.56.0
Luiz Augusto von Dentz
maintainerPATCH v5 1/2 Bluetooth: L2CAP: Fix accepting channels with insufficient security
From: Mohammed Alquraini <[email protected]>
When the link security changes, l2cap_security_cfm() doesn't check that the result meets the security level required by the channels:
- If encryption completes it sets chan->sec_level to hcon->sec_level, so a channel requiring a higher security level than the one the link ended up with has its requirement lowered instead of being refused.
- A channel in BT_CONNECT2 is accepted, or offered to the listener if it uses BT_DEFER_SETUP, as long as the status is success and the encryption key size is valid, e.g. if the peer disables encryption while the channel waits for userspace to authorize it, the channel is offered again and, once authorized, it is established over an unencrypted link.
- A channel in BT_CONNECT is started as long as the status is success, no matter if the link meets its security level.
- The security level of the link is updated when authentication completes, before encryption is enabled, so it can't be used alone to tell if the link is secure enough.
Fix this by treating encryption completing with a security level below the one required by a channel as a downgrade: the security level of the channel is no longer lowered and the channel is disconnected, or refused with L2CAP_CR_SEC_BLOCK if it was pending. There is no way to disconnect a fixed channel at L2CAP, so for those the link is disconnected instead. Channels in BT_CONNECT2 and BT_CONNECT are only accepted, or started, if the link is encrypted and meets their security level.
Since chan->sec_level follows the security level of the link once encryption is enabled it can't tell what a channel requires, so keep that in the new min_sec_level, which is set wherever the security level is requested for the channel, otherwise channels that never asked for more than BT_SECURITY_LOW, like SMP, would be considered downgraded when the link is paired again with a lower security level.
Deferred LE Credit Based and Enhanced Credit Based channels wait in BT_CONNECT2 for userspace to authorize them, which doesn't check the security level, so refuse them there if the link no longer meets it.
This means a channel requiring a security level that pairing can't provide, e.g. BT_SECURITY_HIGH when the IO capability is NoInputNoOutput, is now refused instead of being connected with the security level the link ended up with.
AI assistance was used in finding and preparing this patch.
--- include/net/bluetooth/l2cap.h | 6 +++ net/bluetooth/l2cap_core.c | 83 ++++++++++++++++++++++++++++++++--- net/bluetooth/l2cap_sock.c | 33 +++++++++++--- net/bluetooth/rfcomm/core.c | 1 + 4 files changed, 110 insertions(+), 13 deletions(-)
include/net/bluetooth/l2cap.h +6 -0
@@ -544,7 +544,12 @@ struct l2cap_chan {__u8 chan_type;__u8 chan_policy; +/* The security level of the link, which sec_level follows once +* encryption is enabled, so it can't tell what the channel requires, +* that is what min_sec_level is for. +*/ __u8 sec_level; +__u8 min_sec_level;__u8 mic_len;__u8 ident;
@@ -987,6 +992,7 @@ int l2cap_add_psm(struct l2cap_chan *chan, bdaddr_t *src, __le16 psm);int l2cap_add_scid (struct l2cap_chan *chan,__u16 scid);struct l2cap_chan *l2cap_chan_create (void ); +bool l2cap_chan_sec_level_ok (struct l2cap_chan *chan); void l2cap_chan_close_unlocked(struct l2cap_chan *chan, int reason) __must_not_hold(&chan->lock); int l2cap_chan_connect(struct l2cap_chan *chan, __le16 psm, u16 cid,
net/bluetooth/l2cap_core.c +77 -6
147 lines not on this page · raw message
net/bluetooth/l2cap_sock.c +26 -7
@@ -47,6 +47,15 @@ static struct sock *l2cap_sock_alloc(struct net *net, struct socket *sock, struct l2cap_chan *chan);static void l2cap_sock_cleanup_listen (struct sock *parent); +/* Set the security level required by the channel, as opposed to the one of the +* link that chan->sec_level follows. +*/ +static void l2cap_sock_set_sec_level (struct l2cap_chan *chan,__u8 level) +{ + chan->sec_level = level; + chan->min_sec_level = level; +} +bool l2cap_is_socket (struct socket *sock) {return sock && sock->ops == &l2cap_sock_ops;
@@ -143,15 +152,15 @@ static int l2cap_sock_bind(struct socket *sock, struct sockaddr_unsized *addr, iswitch (chan->chan_type) {case L2CAP_CHAN_CONN_LESS :if (__le16_to_cpu (la.l2_psm) ==L2CAP_PSM_3DSP ) - chan->sec_level =BT_SECURITY_SDP ; +l2cap_sock_set_sec_level (chan,BT_SECURITY_SDP );break ;case L2CAP_CHAN_CONN_ORIENTED :if (__le16_to_cpu (la.l2_psm) ==L2CAP_PSM_SDP ||__le16_to_cpu (la.l2_psm) ==L2CAP_PSM_RFCOMM ) - chan->sec_level =BT_SECURITY_SDP ; +l2cap_sock_set_sec_level (chan,BT_SECURITY_SDP );break ;case L2CAP_CHAN_RAW : - chan->sec_level =BT_SECURITY_SDP ; +l2cap_sock_set_sec_level (chan,BT_SECURITY_SDP );break ;case L2CAP_CHAN_FIXED :/* Fixed channels default to the L2CAP core not holding a
@@ -867,11 +876,11 @@ static int l2cap_sock_setsockopt_old(struct socket *sock, int optname, }if (opt &L2CAP_LM_AUTH ) - chan->sec_level =BT_SECURITY_LOW ; +l2cap_sock_set_sec_level (chan,BT_SECURITY_LOW );if (opt &L2CAP_LM_ENCRYPT ) - chan->sec_level =BT_SECURITY_MEDIUM ; +l2cap_sock_set_sec_level (chan,BT_SECURITY_MEDIUM );if (opt &L2CAP_LM_SECURE ) - chan->sec_level =BT_SECURITY_HIGH ; +l2cap_sock_set_sec_level (chan,BT_SECURITY_HIGH );assign_bit (FLAG_ROLE_SWITCH , &chan->flags, opt &L2CAP_LM_MASTER );
@@ -991,7 +1000,7 @@ static int l2cap_sock_setsockopt(struct socket *sock, int level, int optname,break ; } - chan->sec_level = sec.level; +l2cap_sock_set_sec_level (chan, sec.level); chan->mic_len = sec.mic_len; conn =l2cap_chan_conn (chan);
@@ -1284,6 +1293,16 @@ static int l2cap_sock_defer(struct sock *sk)if (!have_conn) {release_sock (sk); err = -ENOTCONN ; + }else if ((chan->mode ==L2CAP_MODE_EXT_FLOWCTL || +bdaddr_type_is_le (chan->src_type)) && + !l2cap_chan_sec_level_ok (chan)) { +/* The link doesn't meet the security level required +* anymore, refuse the connection like closing it would. +*/ +release_sock (sk); +l2cap_chan_unlock_conn (chan, have_conn); +l2cap_chan_close_unlocked (chan,ECONNREFUSED ); +return -ECONNREFUSED ; }else if (chan->mode ==L2CAP_MODE_EXT_FLOWCTL ) { sk->sk_state =BT_CONNECTED ; chan->state =BT_CONNECTED ;
net/bluetooth/rfcomm/core.c +1 -0
@@ -852,6 +852,7 @@ static struct socket *rfcomm_session_connect(bdaddr_t *src, bdaddr_t *dst,/* Set MTU to 0 so L2CAP can auto select the MTU */ l2cap_pi (sk)->chan->imtu =0 ;l2cap_pi (sk)->chan->sec_level = sec_level; +l2cap_pi (sk)->chan->min_sec_level = sec_level;if (l2cap_ertm)l2cap_pi (sk)->chan->mode =L2CAP_MODE_ERTM ;release_sock (sk);
Signature · 1 line
2.56.0
Luiz Augusto von Dentz
maintainerPATCH v5 2/2 Bluetooth: L2CAP: Don't offer deferred channels before security is met
In reply to Luiz Augusto von DentzFrom: Luiz Augusto von Dentz <[email protected]>
A BR/EDR channel is moved to BT_CONNECT2 as soon as the connection request is answered as pending, no matter if it is waiting for the information exchange, for the link security or for userspace to authorize it, and the socket state follows the channel state.
bt_accept_poll() assumes that a child in BT_CONNECT2 of a listener using BT_DEFER_SETUP is waiting to be authorized, and bt_accept_dequeue() returns any child of such a listener, so although the listener is only notified once the security is met, a listener that doesn't wait for it can accept() the child and then authorize it with recvmsg() while authentication is still pending. l2cap_sock_defer() then moves the channel to BT_CONFIG and responds with L2CAP_CR_SUCCESS, and if authentication fails afterwards the channel is left open since l2cap_security_cfm() doesn't close channels in BT_CONFIG.
Fix this by only moving the socket to BT_CONNECT2 when the channel is offered to the listener with l2cap_sock_defer_cb(), which is only called once the security is met, and by only letting accept() return a child of a listener using BT_DEFER_SETUP once it is in BT_CONNECT2, as bt_accept_poll() does. The child stays in the accept queue so the backlog limit and the cleanup done when the listener is closed are not affected.
--- net/bluetooth/af_bluetooth.c | 6 +++++- net/bluetooth/l2cap_sock.c | 10 +++++++++- 2 files changed, 14 insertions(+), 2 deletions(-)
net/bluetooth/af_bluetooth.c +5 -1
@@ -330,8 +330,12 @@ struct sock *bt_accept_dequeue(struct sock *parent, struct socket *newsock)continue ; } +/* With deferred setup only a child in BT_CONNECT2 has been +* offered to the listener, see bt_accept_poll(). +*/ if (sk->sk_state ==BT_CONNECTED || !newsock || -test_bit (BT_SK_DEFER_SETUP , &bt_sk (parent)->flags)) { + (test_bit (BT_SK_DEFER_SETUP , &bt_sk (parent)->flags) && + sk->sk_state ==BT_CONNECT2 )) {bt_accept_unlink (sk);if (newsock)sock_graft (sk, newsock);
net/bluetooth/l2cap_sock.c +9 -1
@@ -1831,7 +1831,13 @@ static void l2cap_sock_state_change_cb(struct l2cap_chan *chan, int state,lock_sock (sk); - sk->sk_state = state; +/* BT_CONNECT2 also covers a channel still waiting for the information +* exchange or link security. Only l2cap_sock_defer_cb() moves the +* socket to BT_CONNECT2, once the channel can be offered to the +* listener. +*/ +if (state !=BT_CONNECT2 ) + sk->sk_state = state;if (err) sk->sk_err = err;
@@ -1899,6 +1905,8 @@ static void l2cap_sock_defer_cb(struct l2cap_chan *chan)lock_sock (sk); + sk->sk_state =BT_CONNECT2 ; + parent =bt_sk (sk)->parent;if (parent) parent->sk_data_ready (parent);
Signature · 1 line
2.56.0