Skip to content

Commit db13eb2

Browse files
Bluetooth: Fix double free in hci_conn_cleanup
jira VULN-329 cve CVE-2023-28464 commit-author ZhengHan Wang <[email protected]> commit a85fb91 syzbot reports a slab use-after-free in hci_conn_hash_flush [1]. After releasing an object using hci_conn_del_sysfs in the hci_conn_cleanup function, releasing the same object again using the hci_dev_put and hci_conn_put functions causes a double free. Here's a simplified flow: hci_conn_del_sysfs: hci_dev_put put_device kobject_put kref_put kobject_release kobject_cleanup kfree_const kfree(name) hci_dev_put: ... kfree(name) hci_conn_put: put_device ... kfree(name) This patch drop the hci_dev_put and hci_conn_put function call in hci_conn_cleanup function, because the object is freed in hci_conn_del_sysfs function. This patch also fixes the refcounting in hci_conn_add_sysfs() and hci_conn_del_sysfs() to take into account device_add() failures. This fixes CVE-2023-28464. Link: https://syzkaller.appspot.com/bug?id=1bb51491ca5df96a5f724899d1dbb87afda61419 [1] Signed-off-by: ZhengHan Wang <[email protected]> Co-developed-by: Luiz Augusto von Dentz <[email protected]> Signed-off-by: Luiz Augusto von Dentz <[email protected]> (cherry picked from commit a85fb91) Signed-off-by: Pratham Patel <[email protected]>
1 parent d475fa2 commit db13eb2

File tree

2 files changed

+14
-15
lines changed

2 files changed

+14
-15
lines changed

net/bluetooth/hci_conn.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,11 @@ static void hci_conn_cleanup(struct hci_conn *conn)
135135
hdev->notify(hdev, HCI_NOTIFY_CONN_DEL);
136136
}
137137

138-
hci_conn_del_sysfs(conn);
139-
140138
debugfs_remove_recursive(conn->debugfs);
141139

142-
hci_dev_put(hdev);
140+
hci_conn_del_sysfs(conn);
143141

144-
hci_conn_put(conn);
142+
hci_dev_put(hdev);
145143
}
146144

147145
static void le_scan_cleanup(struct work_struct *work)

net/bluetooth/hci_sysfs.c

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ void hci_conn_init_sysfs(struct hci_conn *conn)
3333
{
3434
struct hci_dev *hdev = conn->hdev;
3535

36-
BT_DBG("conn %p", conn);
36+
bt_dev_dbg(hdev, "conn %p", conn);
3737

3838
conn->dev.type = &bt_link;
3939
conn->dev.class = bt_class;
@@ -46,24 +46,27 @@ void hci_conn_add_sysfs(struct hci_conn *conn)
4646
{
4747
struct hci_dev *hdev = conn->hdev;
4848

49-
BT_DBG("conn %p", conn);
49+
bt_dev_dbg(hdev, "conn %p", conn);
5050

5151
dev_set_name(&conn->dev, "%s:%d", hdev->name, conn->handle);
5252

53-
if (device_add(&conn->dev) < 0) {
53+
if (device_add(&conn->dev) < 0)
5454
bt_dev_err(hdev, "failed to register connection device");
55-
return;
56-
}
57-
58-
hci_dev_hold(hdev);
5955
}
6056

6157
void hci_conn_del_sysfs(struct hci_conn *conn)
6258
{
6359
struct hci_dev *hdev = conn->hdev;
6460

65-
if (!device_is_registered(&conn->dev))
61+
bt_dev_dbg(hdev, "conn %p", conn);
62+
63+
if (!device_is_registered(&conn->dev)) {
64+
/* If device_add() has *not* succeeded, use *only* put_device()
65+
* to drop the reference count.
66+
*/
67+
put_device(&conn->dev);
6668
return;
69+
}
6770

6871
while (1) {
6972
struct device *dev;
@@ -75,9 +78,7 @@ void hci_conn_del_sysfs(struct hci_conn *conn)
7578
put_device(dev);
7679
}
7780

78-
device_del(&conn->dev);
79-
80-
hci_dev_put(hdev);
81+
device_unregister(&conn->dev);
8182
}
8283

8384
static void bt_host_release(struct device *dev)

0 commit comments

Comments
 (0)