Skip to content

Commit a85fb91

Browse files
unrav31Vudentz
authored andcommitted
Bluetooth: Fix double free in hci_conn_cleanup
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]>
1 parent 4ed924f commit a85fb91

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
@@ -172,13 +172,11 @@ static void hci_conn_cleanup(struct hci_conn *conn)
172172
hdev->notify(hdev, HCI_NOTIFY_CONN_DEL);
173173
}
174174

175-
hci_conn_del_sysfs(conn);
176-
177175
debugfs_remove_recursive(conn->debugfs);
178176

179-
hci_dev_put(hdev);
177+
hci_conn_del_sysfs(conn);
180178

181-
hci_conn_put(conn);
179+
hci_dev_put(hdev);
182180
}
183181

184182
static void hci_acl_create_connection(struct hci_conn *conn)

net/bluetooth/hci_sysfs.c

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

38-
BT_DBG("conn %p", conn);
38+
bt_dev_dbg(hdev, "conn %p", conn);
3939

4040
conn->dev.type = &bt_link;
4141
conn->dev.class = &bt_class;
@@ -48,27 +48,30 @@ void hci_conn_add_sysfs(struct hci_conn *conn)
4848
{
4949
struct hci_dev *hdev = conn->hdev;
5050

51-
BT_DBG("conn %p", conn);
51+
bt_dev_dbg(hdev, "conn %p", conn);
5252

5353
if (device_is_registered(&conn->dev))
5454
return;
5555

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

58-
if (device_add(&conn->dev) < 0) {
58+
if (device_add(&conn->dev) < 0)
5959
bt_dev_err(hdev, "failed to register connection device");
60-
return;
61-
}
62-
63-
hci_dev_hold(hdev);
6460
}
6561

6662
void hci_conn_del_sysfs(struct hci_conn *conn)
6763
{
6864
struct hci_dev *hdev = conn->hdev;
6965

70-
if (!device_is_registered(&conn->dev))
66+
bt_dev_dbg(hdev, "conn %p", conn);
67+
68+
if (!device_is_registered(&conn->dev)) {
69+
/* If device_add() has *not* succeeded, use *only* put_device()
70+
* to drop the reference count.
71+
*/
72+
put_device(&conn->dev);
7173
return;
74+
}
7275

7376
while (1) {
7477
struct device *dev;
@@ -80,9 +83,7 @@ void hci_conn_del_sysfs(struct hci_conn *conn)
8083
put_device(dev);
8184
}
8285

83-
device_del(&conn->dev);
84-
85-
hci_dev_put(hdev);
86+
device_unregister(&conn->dev);
8687
}
8788

8889
static void bt_host_release(struct device *dev)

0 commit comments

Comments
 (0)