Skip to content

Commit 826af9d

Browse files
swkim101gregkh
authored andcommitted
Bluetooth: L2CAP: Fix slab-use-after-free in l2cap_connect()
commit 4d7b41c upstream. Extend a critical section to prevent chan from early freeing. Also make the l2cap_connect() return type void. Nothing is using the returned value but it is ugly to return a potentially freed pointer. Making it void will help with backports because earlier kernels did use the return value. Now the compile will break for kernels where this patch is not a complete fix. Call stack summary: [use] l2cap_bredr_sig_cmd l2cap_connect ┌ mutex_lock(&conn->chan_lock); │ chan = pchan->ops->new_connection(pchan); <- alloc chan │ __l2cap_chan_add(conn, chan); │ l2cap_chan_hold(chan); │ list_add(&chan->list, &conn->chan_l); ... (1) └ mutex_unlock(&conn->chan_lock); chan->conf_state ... (4) <- use after free [free] l2cap_conn_del ┌ mutex_lock(&conn->chan_lock); │ foreach chan in conn->chan_l: ... (2) │ l2cap_chan_put(chan); │ l2cap_chan_destroy │ kfree(chan) ... (3) <- chan freed └ mutex_unlock(&conn->chan_lock); ================================================================== BUG: KASAN: slab-use-after-free in instrument_atomic_read include/linux/instrumented.h:68 [inline] BUG: KASAN: slab-use-after-free in _test_bit include/asm-generic/bitops/instrumented-non-atomic.h:141 [inline] BUG: KASAN: slab-use-after-free in l2cap_connect+0xa67/0x11a0 net/bluetooth/l2cap_core.c:4260 Read of size 8 at addr ffff88810bf040a0 by task kworker/u3:1/311 Fixes: 73ffa90 ("Bluetooth: Move conf_{req,rsp} stuff to struct l2cap_chan") Signed-off-by: Sungwoo Kim <[email protected]> Signed-off-by: Luiz Augusto von Dentz <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 33a93da commit 826af9d

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

net/bluetooth/l2cap_core.c

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3905,13 +3905,12 @@ static inline int l2cap_command_rej(struct l2cap_conn *conn,
39053905
return 0;
39063906
}
39073907

3908-
static struct l2cap_chan *l2cap_connect(struct l2cap_conn *conn,
3909-
struct l2cap_cmd_hdr *cmd,
3910-
u8 *data, u8 rsp_code, u8 amp_id)
3908+
static void l2cap_connect(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd,
3909+
u8 *data, u8 rsp_code, u8 amp_id)
39113910
{
39123911
struct l2cap_conn_req *req = (struct l2cap_conn_req *) data;
39133912
struct l2cap_conn_rsp rsp;
3914-
struct l2cap_chan *chan = NULL, *pchan;
3913+
struct l2cap_chan *chan = NULL, *pchan = NULL;
39153914
int result, status = L2CAP_CS_NO_INFO;
39163915

39173916
u16 dcid = 0, scid = __le16_to_cpu(req->scid);
@@ -3924,7 +3923,7 @@ static struct l2cap_chan *l2cap_connect(struct l2cap_conn *conn,
39243923
&conn->hcon->dst, ACL_LINK);
39253924
if (!pchan) {
39263925
result = L2CAP_CR_BAD_PSM;
3927-
goto sendresp;
3926+
goto response;
39283927
}
39293928

39303929
mutex_lock(&conn->chan_lock);
@@ -4011,17 +4010,15 @@ static struct l2cap_chan *l2cap_connect(struct l2cap_conn *conn,
40114010
}
40124011

40134012
response:
4014-
l2cap_chan_unlock(pchan);
4015-
mutex_unlock(&conn->chan_lock);
4016-
l2cap_chan_put(pchan);
4017-
4018-
sendresp:
40194013
rsp.scid = cpu_to_le16(scid);
40204014
rsp.dcid = cpu_to_le16(dcid);
40214015
rsp.result = cpu_to_le16(result);
40224016
rsp.status = cpu_to_le16(status);
40234017
l2cap_send_cmd(conn, cmd->ident, rsp_code, sizeof(rsp), &rsp);
40244018

4019+
if (!pchan)
4020+
return;
4021+
40254022
if (result == L2CAP_CR_PEND && status == L2CAP_CS_NO_INFO) {
40264023
struct l2cap_info_req info;
40274024
info.type = cpu_to_le16(L2CAP_IT_FEAT_MASK);
@@ -4044,7 +4041,9 @@ static struct l2cap_chan *l2cap_connect(struct l2cap_conn *conn,
40444041
chan->num_conf_req++;
40454042
}
40464043

4047-
return chan;
4044+
l2cap_chan_unlock(pchan);
4045+
mutex_unlock(&conn->chan_lock);
4046+
l2cap_chan_put(pchan);
40484047
}
40494048

40504049
static int l2cap_connect_req(struct l2cap_conn *conn,

0 commit comments

Comments
 (0)