Skip to content

Commit 1785dbf

Browse files
peterhurleyGustavo F. Padovan
authored andcommitted
Bluetooth: hidp: safely acquire hci connection
Claim device lock to safely enumerate hci connection list and bump hci connection proxy device ref count simultaneously. This patch incorporates David Herrmann's fix to prevent adding an HID device when the hci connection no longer exists. Signed-off-by: David Herrmann <dh.herrmann@googlemail.com> Signed-off-by: Peter Hurley <peter@hurleysoftware.com> Signed-off-by: Gustavo F. Padovan <padovan@profusion.mobi>
1 parent ab88f71 commit 1785dbf

1 file changed

Lines changed: 20 additions & 14 deletions

File tree

‎net/bluetooth/hidp/core.c‎

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,6 @@ static void __hidp_link_session(struct hidp_session *session)
9595
{
9696
__module_get(THIS_MODULE);
9797
list_add(&session->list, &hidp_session_list);
98-
99-
hci_conn_hold_device(session->conn);
10098
}
10199

102100
static void __hidp_unlink_session(struct hidp_session *session)
@@ -785,24 +783,26 @@ static int hidp_session(void *arg)
785783
return 0;
786784
}
787785

788-
static struct device *hidp_get_device(struct hidp_session *session)
786+
static struct hci_conn *hidp_find_connection(struct hidp_session *session)
789787
{
790788
bdaddr_t *src = &bt_sk(session->ctrl_sock->sk)->src;
791789
bdaddr_t *dst = &bt_sk(session->ctrl_sock->sk)->dst;
792-
struct device *device = NULL;
790+
struct hci_conn *conn;
793791
struct hci_dev *hdev;
794792

795793
hdev = hci_get_route(dst, src);
796794
if (!hdev)
797795
return NULL;
798796

799-
session->conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, dst);
800-
if (session->conn)
801-
device = &session->conn->dev;
797+
hci_dev_lock_bh(hdev);
798+
conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, dst);
799+
if (conn)
800+
hci_conn_hold_device(conn);
801+
hci_dev_unlock_bh(hdev);
802802

803803
hci_dev_put(hdev);
804804

805-
return device;
805+
return conn;
806806
}
807807

808808
static int hidp_setup_input(struct hidp_session *session,
@@ -852,7 +852,7 @@ static int hidp_setup_input(struct hidp_session *session,
852852
input->relbit[0] |= BIT_MASK(REL_WHEEL);
853853
}
854854

855-
input->dev.parent = hidp_get_device(session);
855+
input->dev.parent = &session->conn->dev;
856856

857857
input->event = hidp_input_event;
858858

@@ -952,7 +952,7 @@ static int hidp_setup_hid(struct hidp_session *session,
952952
strncpy(hid->phys, batostr(&bt_sk(session->ctrl_sock->sk)->src), 64);
953953
strncpy(hid->uniq, batostr(&bt_sk(session->ctrl_sock->sk)->dst), 64);
954954

955-
hid->dev.parent = hidp_get_device(session);
955+
hid->dev.parent = &session->conn->dev;
956956
hid->ll_driver = &hidp_hid_driver;
957957

958958
hid->hid_get_raw_report = hidp_get_raw_report;
@@ -993,6 +993,12 @@ int hidp_add_connection(struct hidp_connadd_req *req, struct socket *ctrl_sock,
993993
goto failed;
994994
}
995995

996+
session->conn = hidp_find_connection(session);
997+
if (!session->conn) {
998+
err = -ENOTCONN;
999+
goto failed;
1000+
}
1001+
9961002
bacpy(&session->bdaddr, &bt_sk(ctrl_sock->sk)->dst);
9971003

9981004
session->ctrl_mtu = min_t(uint, l2cap_pi(ctrl_sock->sk)->chan->omtu,
@@ -1018,6 +1024,8 @@ int hidp_add_connection(struct hidp_connadd_req *req, struct socket *ctrl_sock,
10181024
session->flags = req->flags & (1 << HIDP_BLUETOOTH_VENDOR_ID);
10191025
session->idle_to = req->idle_to;
10201026

1027+
__hidp_link_session(session);
1028+
10211029
if (req->rd_size > 0) {
10221030
err = hidp_setup_hid(session, req);
10231031
if (err)
@@ -1030,8 +1038,6 @@ int hidp_add_connection(struct hidp_connadd_req *req, struct socket *ctrl_sock,
10301038
goto purge;
10311039
}
10321040

1033-
__hidp_link_session(session);
1034-
10351041
hidp_set_timer(session);
10361042

10371043
if (session->hid) {
@@ -1084,8 +1090,6 @@ int hidp_add_connection(struct hidp_connadd_req *req, struct socket *ctrl_sock,
10841090
unlink:
10851091
hidp_del_timer(session);
10861092

1087-
__hidp_unlink_session(session);
1088-
10891093
if (session->input) {
10901094
input_unregister_device(session->input);
10911095
session->input = NULL;
@@ -1100,6 +1104,8 @@ int hidp_add_connection(struct hidp_connadd_req *req, struct socket *ctrl_sock,
11001104
session->rd_data = NULL;
11011105

11021106
purge:
1107+
__hidp_unlink_session(session);
1108+
11031109
skb_queue_purge(&session->ctrl_transmit);
11041110
skb_queue_purge(&session->intr_transmit);
11051111

0 commit comments

Comments
 (0)