From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.221.173 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796151819; b=dP84rsD95XcLKKzEQpQUc9oeriUAFBGJ/rXpxOcsjrb1wewfrIEl9k3ILtI3Vjj4V6Bv 8DHeuEPe4/VBxxCTdhrYD+HBpRB6CqBFkbFt3Fk8v2qmjddpryGEE5hmSl6p20CYMU8b5 jM7LXK1zaOVUE6SGa3bWSQsgCtOMUSQecUkn4LHiNrZsYQZrI2TnnVaaZ3e6yhmmiSsrf OTRT/UPHBWGa7uN4zMeGAgc5S/drNr3TLvTjnKlUrfFRtjf0qJz/gz5N7AcOaiG8uCTUj IdPMAkPbeSELiZT57qUiFHYu/P/GIzOMeGoxsA8Ikfa6Cg7B6jueZfoUg81N8JGwEzA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796151819; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Gm-Gg:X-Received:Received:From:To:Subject:Date: Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=g8HPeTKKhYBPZIT7F70LA6n0+7RISdYPnDE+eHZvRrw=; b=lxpt8FY0XuTHWFm3KnL6A5Sor2Np1clhP3my/wCupBRJPbS6B+EkDNiGUlEoGGxky987 +TBSIZSH3vrrqws6IJ0VdmvgHHDfvqibHzyyGOesBBYdeTXIphhyfLV0gDe2hSPokdHWO Q5TILTrbjiocFgKkRGgipaiawqAstgqgXM5Y5nybvUEqz4SlV5YYSQr4y68b4HWWXSKj1 oAIArzweqRwvVt7onV8lef4uoc3tKKDbKmpfXMnGRBhiDelIUyclKfwpOdott/TeUHeEK /irSJnKiNtOSLdU0sT8tylKvgyXIMveYe0l26OpdXexRV2MYxy/YupyrOLAi14IyiFg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=kV9DdRXC; arc=none smtp.remote-ip=209.85.221.173 Received: from mail-vk1-f173.google.com (mail-vk1-f173.google.com [209.85.221.173]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 03800606A320 for ; Tue, 1 Dec 2026 19:03:37 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 03800606A320 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=kV9DdRXC Received: by mail-vk1-f173.google.com with SMTP id 71dfb90a1353d-5efe82316d2so2299639e0c.0 for ; Tue, 01 Dec 2026 11:03:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796151817; x=1796756617; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to:content-type; bh=g8HPeTKKhYBPZIT7F70LA6n0+7RISdYPnDE+eHZvRrw=; b=kV9DdRXCxllgJFMh7KiQTZ+jvszCV1a1wpKD1/SAP1eGKtO8P8+vEjr9bwrZYUFy70 AAVtgYWlzd6km47PJF036osE+aJcnnX9O4eNVKjgtez1M89nuyJe5xwgp8qCziQroQFy f3Dv+1xiNHNPjcHVbe6cqIj7A2M5RMHdgsmsIwWy0JEaIjLzooF4JI65NnT0Fz1f/PAm RmB7/LvLlW5ULe4pT1ZDIkt6WP2BLoxKMxjhKEuDDI4p6ZrXigwiQ+V36lslRZ4QSEzR OfCqKn5MXzOsx8lpEOw0+PxjcUioFgwW4UZybaKVEgtbTOi7NualAroANzjzHWTT9Gwi wPBA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796151817; x=1796756617; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to:content-type; bh=g8HPeTKKhYBPZIT7F70LA6n0+7RISdYPnDE+eHZvRrw=; b=L5PU3dNFu1GhUnDKLnehnPK8wPt37LjTXLqU5LN5F3fYBu2+oHbaXuJ2VHdOKT+wkL LrFFQnmbknZmsIPrJJsYGcfpSn/RX9jCtb7zXUOc6E8HW3RaUNQ1EEr3lpDoTivcp9vk RHUEoxGyMe+onmHb+mk4+vU3vtGMghqrJDyFBTfZya6Su2BPbCEMAEEm0pYFeiVL3PWn z8WrQhkptJkVcUtJw7/9XYRIsHJ5qXPylZRnoR5i64pGQBUOR7TNdntgADtLnoHDtSoP SQX4afR89BWzg+q0ighdhWQlfap2CUyhdYhzYVydJL3Ti5ltgnpY7MrYfi26qswoMPbT J16Q== X-Gm-Message-State: APuU4Z9LFznR44+v+rmpiGy3PH7sfcYj7TOwEJuoDweBGCxZ6doChzmg N2UZ3VM0ziJwRCzj5bWCt6ELCJ0/qJ6bAJmNtoLDebbujIWjp5iNGLIaMt+IZ6+ckB0= X-Gm-Gg: AZFdTZfkDlKelkTAHTsRwzeZ20y7Ra/3fxLPRcwyHmAmVpv08+xUWzQyZz6w5fDqAnE 6fHy1QkkHsoFJ7IiUyqzXX5YKhH0wecNu+k3/ndMxI/P2dbCtb8G6q/WC8+uKrFYnSnCBmfs4+b K/gfGwPXUcyQZh26Uz2CDmMAUEl2sLOCfZii1Eqos9S8oji8rOA/++oepaQU8L206ben7a8m4jz OHelnI506WcWq0iwHQinDYuRq/AD4jgNfEvUcd0L/1/y56XXS4n59RcXHiq8QZmw3ipvxNX4PsV N96yjbSH4ve15aO9owLWv9CwKSrwVw90dodcgU7/+fjaTqIBq8CAcSSx/JKfqnxYEXv3Bgi4zXt u8vYmLOLXnAgRDIXH+POD6Kh94GUsd/8/L7gR8kzC8pHECy6mCiDLTKle6oK766lbGzIlYh3Cvh 6qudui17PsdKSXPdkXlOazcIZcGeW33/4zUu+Dp1VJqYCGmOjlc5U9xBbFq+70xquu5Nfi/5b86 jxmiUgzZFfEqGHnMjYxtrbDPjaqtDCRl9WK3xxPfLbMDF0mzdF4oxZmpuTHP6Xf/PjpVTYsIi6j ivDyKLCI732Y3QhbqgPYgXsdRijhJt3kwT0L41fdEvkpO2201ZR+EWZsS0+I4Gfaew9mJhmqro6 k8erzbxn8u0al4oAApGbOvm+MoZDHqoTst36QyMdYWLfPe/V6R2QhIF8= X-Received: by 2002:ac5:c5b5:0:b0:5ec:d9c6:73b2 with SMTP id 71dfb90a1353d-5ee82617c5emr14173486e0c.7.1796151816744; Tue, 01 Dec 2026 11:03:36 -0800 (PST) Received: from lvondent-mobl5 ([72.188.211.115]) by smtp.gmail.com with ESMTPSA id a1e0cc1a2514c-99e6dfc0b22sm38919229241.6.2026.12.01.11.03.36 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Dec 2026 11:03:36 -0800 (PST) From: Luiz Augusto von Dentz To: linux-bluetooth@vger.kernel.org Subject: [PATCH v12 5/6] Bluetooth: L2CAP: Fix accepting channels with insufficient security Date: Tue, 1 Dec 2026 14:03:24 -0500 Message-ID: <20261201190325.110874-6-luiz.dentz@gmail.com> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261201190325.110874-1-luiz.dentz@gmail.com> References: <20261201190325.110874-1-luiz.dentz@gmail.com> X-Mailing-List: linux-bluetooth@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Mohammed Alquraini 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, which also applies when encryption is disabled since l2cap_check_encryption() doesn't handle them. Channels in BT_CONNECT2 and BT_CONNECT are only accepted, or started, if the link is encrypted and meets their security level, and established channels are not resumed if it doesn't. Authentication completing before encryption is enabled is not considered encryption being disabled, the channels just wait for it. 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 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, e.g. if the security level was raised with BT_SECURITY and the authentication has not completed yet, with the channel kept locked until it is closed so it can't be authorized meanwhile. Only that channel is refused, the others of the same Enhanced Credit Based request are responded individually. The Enhanced Credit Based channels accepted while the response of their request is pending are not established yet, so they are left alone by l2cap_security_cfm(), which can't send the response while walking the channels, and the response refuses them if the link doesn't meet their security level by then. Outgoing deferred Enhanced Credit Based channels the link doesn't meet the security level of are not included in the request of the others, l2cap_security_cfm() refuses them instead. A security upgrade requested for an established channel that fails leaves the channel below its security level, so it is disconnected as well instead of being left suspended, by disconnecting the link for the fixed channels. The security level requested with BT_SECURITY is only set once it is accepted, so a request that fails is never seen by the other channels. A deferred channel whose socket was suspended while a security level requested with BT_SECURITY was pending is resumed once it is met, so it doesn't stay suspended after being authorized. 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. Fixes: f66dc81f44d9 ("Bluetooth: Add support for deferring L2CAP connection setup") Assisted-by: OpenCode:claude-fable-5.2 Signed-off-by: Mohammed Alquraini Signed-off-by: Luiz Augusto von Dentz --- include/net/bluetooth/l2cap.h | 7 ++ net/bluetooth/l2cap_core.c | 170 ++++++++++++++++++++++++++++++++-- net/bluetooth/l2cap_sock.c | 40 ++++++-- net/bluetooth/rfcomm/core.c | 1 + 4 files changed, 202 insertions(+), 16 deletions(-) diff --git a/include/net/bluetooth/l2cap.h b/include/net/bluetooth/l2cap.h index e1820b7dca67..28a2c0f4eff8 100644 --- a/include/net/bluetooth/l2cap.h +++ b/include/net/bluetooth/l2cap.h @@ -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; @@ -988,6 +993,8 @@ 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(struct l2cap_chan *chan, int reason); 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, diff --git a/net/bluetooth/l2cap_core.c b/net/bluetooth/l2cap_core.c index ad5a3cba708c..6dbc0bdcbe3f 100644 --- a/net/bluetooth/l2cap_core.c +++ b/net/bluetooth/l2cap_core.c @@ -551,6 +551,7 @@ void l2cap_chan_set_defaults(struct l2cap_chan *chan, struct l2cap_chan *pchan) chan->remote_tx_win = chan->tx_win; chan->ack_win = L2CAP_DEFAULT_TX_WINDOW; chan->sec_level = BT_SECURITY_LOW; + chan->min_sec_level = BT_SECURITY_LOW; chan->flush_to = L2CAP_DEFAULT_FLUSH_TO; chan->retrans_timeout = L2CAP_DEFAULT_RETRANS_TO; chan->monitor_timeout = L2CAP_DEFAULT_MONITOR_TO; @@ -567,6 +568,7 @@ void l2cap_chan_set_defaults(struct l2cap_chan *chan, struct l2cap_chan *pchan) chan->tx_win = pchan->tx_win; chan->tx_win_max = pchan->tx_win_max; chan->sec_level = pchan->sec_level; + chan->min_sec_level = pchan->min_sec_level; chan->mic_len = pchan->mic_len; chan->conf_state = pchan->conf_state; chan->flags = pchan->flags; @@ -1483,6 +1485,13 @@ static void l2cap_ecred_defer_connect(struct l2cap_chan *chan, void *data) chan->mode != L2CAP_MODE_EXT_FLOWCTL || chan->state != BT_CONNECT) return; + /* Leave out the channels the link doesn't meet the security level of, + * they are refused by l2cap_security_cfm() instead of being included + * in a request they would be established by. + */ + if (!l2cap_chan_sec_level_ok(chan)) + return; + if (!test_and_clear_bit(FLAG_DEFER_SETUP, &chan->flags)) return; @@ -4208,7 +4217,7 @@ static void l2cap_ecred_rsp_defer(struct l2cap_chan *chan, void *data) * result: the refused ones are responded with no destination CID, * which are dropped if all of them were refused. */ - if (chan->state == BT_CONNECTED) { + if (chan->state == BT_CONNECTED && l2cap_chan_sec_level_ok(chan)) { rsp_flex->dcid[rsp->count++] = cpu_to_le16(chan->scid); rsp->accepted++; @@ -5265,6 +5274,8 @@ static int l2cap_le_connect_rsp(struct l2cap_conn *conn, sec_level = hcon->sec_level + 1; if (chan->sec_level < sec_level) chan->sec_level = sec_level; + if (chan->min_sec_level < sec_level) + chan->min_sec_level = sec_level; /* We'll need to send a new Connect Request */ clear_bit(FLAG_LE_CONN_REQ_SENT, &chan->flags); @@ -5881,6 +5892,8 @@ static inline int l2cap_ecred_conn_rsp(struct l2cap_conn *conn, sec_level = hcon->sec_level + 1; if (chan->sec_level < sec_level) chan->sec_level = sec_level; + if (chan->min_sec_level < sec_level) + chan->min_sec_level = sec_level; /* We'll need to send a new Connect Request */ clear_bit(FLAG_ECRED_CONN_REQ_SENT, &chan->flags); @@ -8187,11 +8200,37 @@ static inline void l2cap_check_encryption(struct l2cap_chan *chan, u8 encrypt) } } +/* Check if the link meets the security level required by the channel, which + * for anything above BT_SECURITY_LOW needs the link to be encrypted since the + * security level of the link is updated when authentication completes, before + * encryption is enabled. + */ +static bool l2cap_chan_sec_level_met(struct hci_conn *hcon, + struct l2cap_chan *chan) +{ + if (chan->min_sec_level <= BT_SECURITY_LOW) + return true; + + return test_bit(HCI_CONN_ENCRYPT, hcon->flags) && + hcon->sec_level >= chan->min_sec_level; +} + +void l2cap_chan_close(struct l2cap_chan *chan, int reason) +{ + /* Context analysis: caller holds chan->lock and chan->conn->lock */ + context_unsafe(__l2cap_chan_close(chan, reason)); +} + +bool l2cap_chan_sec_level_ok(struct l2cap_chan *chan) +{ + return l2cap_chan_sec_level_met(chan->conn->hcon, chan); +} + static void l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt) __must_hold(&hcon->hdev->lock) { struct l2cap_conn *conn = hcon->l2cap_data; - struct l2cap_chan *chan; + struct l2cap_chan *chan, *tmp; if (!conn) return; @@ -8200,7 +8239,9 @@ static void l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt) mutex_lock(&conn->lock); - list_for_each_entry(chan, &conn->chan_l, list) { + list_for_each_entry_safe(chan, tmp, &conn->chan_l, list) { + bool unmet, encrypting, failed, downgrade = false; + l2cap_chan_lock(chan); lockdep_assert_held(&chan->conn->lock); @@ -8208,24 +8249,136 @@ static void l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt) BT_DBG("chan %p scid 0x%4.4x state %s", chan, chan->scid, state_to_string(chan->state)); - if (!status && encrypt) - chan->sec_level = hcon->sec_level; + /* Authentication completing before encryption is enabled + * updates the security level of the link, so it already meets + * the one required, but it is not encrypted yet: encryption is + * expected to follow instead of it having been disabled. + */ + encrypting = !status && !encrypt && + chan->min_sec_level > BT_SECURITY_LOW && + !test_bit(HCI_CONN_ENCRYPT, hcon->flags) && + hcon->sec_level >= chan->min_sec_level; + + /* The requirement of the channel may change under chan->lock */ + unmet = !status && !encrypting && + !l2cap_chan_sec_level_met(hcon, chan); + + /* A security upgrade requested for an established channel, e.g. + * with BT_SECURITY, that fails leaves it below its security + * level, the link security didn't change otherwise. + */ + failed = status && !l2cap_chan_sec_level_met(hcon, chan); + + if (!status && encrypt) { + /* Encryption completing with a security level below + * the one the channel requires is a downgrade, don't + * lower the requirement of the channel to match it. + */ + if (unmet) + downgrade = true; + else + chan->sec_level = hcon->sec_level; + } else if ((unmet || failed) && + chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) { + /* Unlike the other channels, l2cap_check_encryption() + * doesn't handle fixed ones, so encryption being + * disabled is a downgrade for them as well, and so is + * a security upgrade they requested that failed since + * they can't be disconnected either. + */ + downgrade = true; + } + + /* The response to an Enhanced Credit Based request is only sent + * once all of its channels are authorized or refused, so the + * ones accepted while waiting for it are not established yet + * and can't be disconnected, which would send the response + * while walking the channels: the response refuses them if the + * link doesn't meet their security level by then. + */ + if (chan->state == BT_CONNECTED && + l2cap_chan_ecred_rsp_pending(chan)) { + l2cap_chan_unlock(chan); + continue; + } + + /* A channel that is not established yet is refused as soon as + * the link doesn't meet its security level, while an + * established one is only closed on a downgrade since + * l2cap_check_encryption() handles encryption being disabled. + * + * Enhanced Credit Based channels waiting in BT_CONNECT2 are + * refused when userspace authorizes them instead, since the + * response of the request is only sent once all of its + * channels have been authorized or refused. + */ + if ((downgrade && (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED || + chan->state == BT_CONNECTED || + chan->state == BT_CONFIG)) || + (failed && chan->chan_type == L2CAP_CHAN_CONN_ORIENTED && + (chan->state == BT_CONNECTED || + chan->state == BT_CONFIG)) || + (unmet && (chan->state == BT_CONNECT || + (chan->state == BT_CONNECT2 && + chan->mode == L2CAP_MODE_LE_FLOWCTL)))) { + /* There is no way to disconnect a fixed channel at + * L2CAP, so drop the link, which takes down the rest + * of the channels as well. + */ + if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) { + hci_disconnect(hcon, HCI_ERROR_AUTH_FAILURE); + l2cap_chan_unlock(chan); + break; + } + + /* The channel may be removed from the list */ + l2cap_chan_hold(chan); + __l2cap_chan_close(chan, ECONNREFUSED); + + if (test_bit(FLAG_DEL, &chan->flags)) + chan->ops->close(chan); + + l2cap_chan_unlock(chan); + l2cap_chan_put(chan); + continue; + } + + /* A channel waiting for the encryption to be enabled has to + * wait for it before being accepted, started or resumed. + */ + if (encrypting) { + l2cap_chan_unlock(chan); + continue; + } if (!__l2cap_no_conn_pending(chan)) { l2cap_chan_unlock(chan); continue; } + /* A deferred channel waiting for userspace to authorize it may + * have been suspended while a security level it requested with + * BT_SECURITY was pending, so resume it now that it is met. + */ + if (!status && !unmet && chan->state == BT_CONNECT2 && + test_bit(FLAG_DEFER_SETUP, &chan->flags)) + chan->ops->resume(chan); + if (!status && (chan->state == BT_CONNECTED || chan->state == BT_CONFIG)) { - chan->ops->resume(chan); + /* Don't resume a channel the link is not secure enough + * for, l2cap_check_encryption() deals with it. + */ + if (!unmet) + chan->ops->resume(chan); l2cap_check_encryption(chan, encrypt); l2cap_chan_unlock(chan); continue; } if (chan->state == BT_CONNECT) { - if (!status && l2cap_check_enc_key_size(hcon, chan)) + if (!status && l2cap_chan_sec_level_met(hcon, chan) && + l2cap_check_enc_key_size(hcon, chan)) l2cap_start_connection(chan); else __set_chan_timer(chan, L2CAP_DISC_TIMEOUT); @@ -8235,7 +8388,8 @@ static void l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt) struct l2cap_conn_rsp rsp; __u16 res, stat; - if (!status && l2cap_check_enc_key_size(hcon, chan)) { + if (!status && l2cap_check_enc_key_size(hcon, chan) && + l2cap_chan_sec_level_met(hcon, chan)) { if (test_bit(FLAG_DEFER_SETUP, &chan->flags)) { res = L2CAP_CR_PEND; stat = L2CAP_CS_AUTHOR_PEND; diff --git a/net/bluetooth/l2cap_sock.c b/net/bluetooth/l2cap_sock.c index e46487bfabf8..4e45da2b5975 100644 --- a/net/bluetooth/l2cap_sock.c +++ b/net/bluetooth/l2cap_sock.c @@ -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, i switch (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,12 +1000,16 @@ static int l2cap_sock_setsockopt(struct socket *sock, int level, int optname, break; } - chan->sec_level = sec.level; chan->mic_len = sec.mic_len; + /* The security level is only set once it is accepted, so the + * other channels never see a requirement that failed. + */ conn = l2cap_chan_conn(chan); - if (!conn) + if (!conn) { + l2cap_sock_set_sec_level(chan, sec.level); break; + } /* change security for LE channels */ if (chan->scid == L2CAP_CID_ATT) { @@ -1006,6 +1019,7 @@ static int l2cap_sock_setsockopt(struct socket *sock, int level, int optname, break; } + l2cap_sock_set_sec_level(chan, sec.level); set_bit(FLAG_PENDING_SECURITY, &chan->flags); sk->sk_state = BT_CONFIG; chan->state = BT_CONFIG; @@ -1014,6 +1028,7 @@ static int l2cap_sock_setsockopt(struct socket *sock, int level, int optname, } else if ((sk->sk_state == BT_CONNECT2 && test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) || sk->sk_state == BT_CONNECTED) { + l2cap_sock_set_sec_level(chan, sec.level); if (!l2cap_chan_check_security(chan, true)) set_bit(BT_SK_SUSPEND, &bt_sk(sk)->flags); else @@ -1284,6 +1299,15 @@ static int l2cap_sock_defer(struct sock *sk) if (!have_conn) { release_sock(sk); err = -ENOTCONN; + } else if (!l2cap_chan_sec_level_ok(chan)) { + /* The link doesn't meet the security level required + * anymore, refuse the connection like closing it would, + * without releasing the channel so it can't be + * authorized meanwhile. + */ + release_sock(sk); + l2cap_chan_close(chan, ECONNREFUSED); + err = -ECONNREFUSED; } else if (chan->mode == L2CAP_MODE_EXT_FLOWCTL) { sk->sk_state = BT_CONNECTED; chan->state = BT_CONNECTED; diff --git a/net/bluetooth/rfcomm/core.c b/net/bluetooth/rfcomm/core.c index e6af6f5f5914..66c88390ca88 100644 --- a/net/bluetooth/rfcomm/core.c +++ b/net/bluetooth/rfcomm/core.c @@ -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); -- 2.56.1