mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git
synced 2026-09-22 09:34:56 +02:00
Bluetooth: L2CAP: validate option length before reading conf opt value
commit687617555cupstream. 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. Fixes:7c9cbd0b5e("Bluetooth: Verify that l2cap_get_conf_opt provides large enough buffer") Cc: stable@vger.kernel.org Signed-off-by: Muhammad Bilal <meatuni001@gmail.com> Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
This commit is contained in:
committed by
Greg Kroah-Hartman
parent
d5616beb33
commit
73abbaf91a
@@ -3048,13 +3048,24 @@ fail:
|
||||
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 data
|
||||
void *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 data
|
||||
BT_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:
|
||||
|
||||
Reference in New Issue
Block a user