Bluetooth: L2CAP: validate option length before reading conf opt value
Subsystems (3)
PATCH 1/1 Bluetooth: L2CAP: validate option length before reading conf opt value
l2cap_get_conf_opt() derives the option length from the attacker-controlled opt->len field and immediately dereferences opt->val (as u8, get_unaligned_le16() or get_unaligned_le32(), or a raw pointer for the default case) before any caller has confirmed that opt->len bytes are present in the buffer. The callers (l2cap_parse_conf_req(), l2cap_parse_conf_rsp() and l2cap_conf_rfc_get()) only detect a malformed option afterwards, once the running length has gone negative, by which point the out-of-bounds read has already executed.
An existing post-hoc length check keeps the garbage value from being consumed, so this is not a data leak in the current control flow. It is still a validate-after-use ordering bug: up to 4 bytes are read past the end of the buffer before it is known to contain them, and it is fragile to future changes in the callers.
Fix it at the source. Pass the end of the buffer into l2cap_get_conf_opt() and refuse to touch opt->val unless the full option (header + value) fits. Each caller computes an end pointer once before the loop and checks the return value directly instead of inferring the error from a negative length.
--- net/bluetooth/l2cap_core.c | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-)
net/bluetooth/l2cap_core.c +28 -8
@@ -3052,13 +3052,24 @@ static struct sk_buff *l2cap_build_cmd(struct l2cap_conn *conn, u8 code,return NULL ; } -static inline int l2cap_get_conf_opt (void **ptr,int *type,int *olen, -unsigned long *val) +static inline int l2cap_get_conf_opt (void **ptr,void *end,int *type, +int *olen,unsigned long *val) {struct l2cap_conf_opt *opt = *ptr;int len; +/* opt->len is attacker-controlled. Validate that the full option +* (header + value) actually fits in the buffer before touching +* opt->val, otherwise the switch below reads past the end of the +* caller's buffer. +*/ +if (end - *ptr <L2CAP_CONF_OPT_SIZE ) +return -EINVAL ; + len =L2CAP_CONF_OPT_SIZE + opt->len; +if (end - *ptr < len) +return -EINVAL ; + *ptr += len; *type = opt->type;
@@ -3426,6 +3437,7 @@ static int l2cap_parse_conf_req(struct l2cap_chan *chan, void *data, size_t datavoid *ptr = rsp->data;void *endptr = data + data_size;void *req = chan->conf_req; +void *req_end = req + chan->conf_len;int len = chan->conf_len;int type, hint, olen;unsigned long val;
@@ -3439,9 +3451,11 @@ static int l2cap_parse_conf_req(struct l2cap_chan *chan, void *data, size_t dataBT_DBG ("chan %p" , chan);while (len >=L2CAP_CONF_OPT_SIZE ) { - len -=l2cap_get_conf_opt (&req, &type, &olen, &val); -if (len <0 ) +int ret =l2cap_get_conf_opt (&req, req_end, &type, &olen, &val); + +if (ret <0 )break ; + len -= ret; hint = type &L2CAP_CONF_HINT ; type &=L2CAP_CONF_MASK ;
@@ -3669,6 +3683,7 @@ static int l2cap_parse_conf_rsp(struct l2cap_chan *chan, void *rsp, int len,struct l2cap_conf_req *req = data;void *ptr = req->data;void *endptr = data + size; +void *rsp_end = rsp + len;int type, olen;unsigned long val;struct l2cap_conf_rfc rfc = { .mode =L2CAP_MODE_BASIC };
@@ -3677,9 +3692,11 @@ static int l2cap_parse_conf_rsp(struct l2cap_chan *chan, void *rsp, int len,BT_DBG ("chan %p, rsp %p, len %d, req %p" , chan, rsp, len, data);while (len >=L2CAP_CONF_OPT_SIZE ) { - len -=l2cap_get_conf_opt (&rsp, &type, &olen, &val); -if (len <0 ) +int ret =l2cap_get_conf_opt (&rsp, rsp_end, &type, &olen, &val); + +if (ret <0 )break ; + len -= ret;switch (type) {case L2CAP_CONF_MTU :
@@ -3930,6 +3947,7 @@ static void l2cap_conf_rfc_get(struct l2cap_chan *chan, void *rsp, int len) {int type, olen;unsigned long val; +void *rsp_end = rsp + len;/* Use sane default values in case a misbehaving remote device * did not send an RFC or extended window size option. */
@@ -3948,9 +3966,11 @@ static void l2cap_conf_rfc_get(struct l2cap_chan *chan, void *rsp, int len)return ;while (len >=L2CAP_CONF_OPT_SIZE ) { - len -=l2cap_get_conf_opt (&rsp, &type, &olen, &val); -if (len <0 ) +int ret =l2cap_get_conf_opt (&rsp, rsp_end, &type, &olen, &val); + +if (ret <0 )break ; + len -= ret;switch (type) {case L2CAP_CONF_RFC :
Signature · 1 line
2.54.0
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=1114248
---Test result---
Test Summary:
CheckPatch PASS 0.84 seconds VerifyFixes PASS 0.16 seconds VerifySignedoff PASS 0.16 seconds GitLint PASS 0.37 seconds SubjectPrefix PASS 0.16 seconds BuildKernel PASS 26.86 seconds CheckAllWarning PASS 29.46 seconds CheckSparse PASS 28.43 seconds BuildKernel32 PASS 26.20 seconds CheckKernelLLVM SKIP 0.00 seconds TestRunnerSetup PASS 576.66 seconds TestRunner_l2cap-tester PASS 58.98 seconds IncrementalBuild PASS 25.33 seconds
Details
##############################
Test: CheckKernelLLVM - SKIP
Desc: Build kernel with LLVM + context analysis
Output:
Clang not found
https://github.com/bluez/bluetooth-next/pull/335
---
Regards,
Linux Bluetooth
This patch was applied to bluetooth/bluetooth-next.git (master) by Luiz Augusto von Dentz <[email protected]>:
Hello:
This patch was applied to bluetooth/bluetooth-next.git (master) by Luiz Augusto von Dentz <[email protected]>:
Quoted 11 lines · On Sun, 21 Jun 2026 00:56:35 +0500 you wrote:
l2cap_get_conf_opt()derives the option length from the attacker-controlledopt->lenfield and immediately dereferencesopt->val(as u8,get_unaligned_le16()orget_unaligned_le32(), or a raw pointer for the default case) before any caller has confirmed thatopt->lenbytes are present in the buffer. The callers (l2cap_parse_conf_req(),l2cap_parse_conf_rsp()andl2cap_conf_rfc_get()) only detect a malformed option afterwards, once the running length has gone negative, by which point the out-of-bounds read has already executed.[...]
Here is the summary with links:
- Bluetooth: L2CAP: validate option length before reading conf opt value
https://git.kernel.org/bluetooth/bluetooth-next/c/64522263b6e3
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html