History

Bluetooth: ISO: avoid deadlocks in iso_sock_timeout

iso_sock_timeout() takes lock_sock, so sync disabling the timer while holding that lock may deadlock. iso_sock_timeout() may also run concurrently with iso_conn_del(), which leads to UAF [Task 1] [Task hdev->workqueue] iso_sock_timeout iso_conn_del iso_conn_hold_unless_zero iso_chan_del `------------> iso_conn_put caller frees hcon iso_conn_put iso_conn_free conn->hcon->iso_data = NULL; /* UAF */ Fix the deadlock by removing the disable from the lock_sock sections. Move the timer from iso_conn to iso_pinfo to decouple it from iso_conn which may need to be freed in lock_sock section. Convert some of the clear_timer to disable_timer.

Pauli Virtanen committed 3 months ago

Commit 200fa161 parent ce574421 file · +29 −31Browse files

Unified · Split

net/bluetooth/iso.c+29−31
@@ -37,8 +37,6 @@ struct iso_conn { · 36 unmodified lines
37 spinlock_t lock;37 spinlock_t lock;
38 struct sock *sk;38 struct sock *sk;
39 39
40 struct delayed_work timeout_work;
41
42 struct sk_buff *rx_skb;40 struct sk_buff *rx_skb;
43 __u32 rx_len;41 __u32 rx_len;
44 __u16 tx_sn;42 __u16 tx_sn;
@@ -81,6 +79,7 @@ struct iso_pinfo { · 36 unmodified lines
81 __u8 base_len;79 __u8 base_len;
82 __u8 base[BASE_MAX_LENGTH];80 __u8 base[BASE_MAX_LENGTH];
83 struct iso_conn *conn;81 struct iso_conn *conn;
82 struct delayed_work timeout_work;
84};83};
85 84
86static struct bt_iso_qos default_qos;85static struct bt_iso_qos default_qos;
@@ -118,9 +117,6 @@ static void iso_conn_free(struct kref *ref) · 31 unmodified lines
118 hci_conn_drop(conn->hcon);117 hci_conn_drop(conn->hcon);
119 }118 }
120 119
121 /* Ensure no more work items will run since hci_conn has been dropped */
122 disable_delayed_work_sync(&conn->timeout_work);
123
124 kfree_skb(conn->rx_skb);120 kfree_skb(conn->rx_skb);
125 121
126 kfree(conn);122 kfree(conn);
@@ -161,48 +157,45 @@ static struct sock *iso_sock_hold(struct iso_conn *conn) · 34 unmodified lines
161 157
162static void iso_sock_timeout(struct work_struct *work)158static void iso_sock_timeout(struct work_struct *work)
163{159{
164 struct iso_conn *conn = container_of(work, struct iso_conn,160 struct iso_pinfo *pi = container_of(work, struct iso_pinfo,
165 timeout_work.work);161 timeout_work.work);
166 struct sock *sk;162 struct sock *sk = &pi->bt.sk;
167
168 conn = iso_conn_hold_unless_zero(conn);
169 if (!conn)
170 return;
171
172 iso_conn_lock(conn);
173 sk = iso_sock_hold(conn);
174 iso_conn_unlock(conn);
175 iso_conn_put(conn);
176
177 if (!sk)
178 return;
179 163
180 BT_DBG("sock %p state %d", sk, sk->sk_state);164 BT_DBG("sock %p state %d", sk, sk->sk_state);
181 165
182 lock_sock(sk);166 lock_sock(sk);
183 sk->sk_err = ETIMEDOUT;167 if (!sock_flag(sk, SOCK_ZAPPED)) {
184 sk->sk_state_change(sk);168 sk->sk_err = ETIMEDOUT;
169 sk->sk_state_change(sk);
170 }
185 release_sock(sk);171 release_sock(sk);
186 sock_put(sk);
187}172}
188 173
189static void iso_sock_set_timer(struct sock *sk, long timeout)174static void iso_sock_set_timer(struct sock *sk, long timeout)
190{175{
176 lockdep_assert(lockdep_sock_is_held(sk));
177
178 cancel_delayed_work(&iso_pi(sk)->timeout_work);
179
191 if (!iso_pi(sk)->conn)180 if (!iso_pi(sk)->conn)
192 return;181 return;
193 182
194 BT_DBG("sock %p state %d timeout %ld", sk, sk->sk_state, timeout);183 BT_DBG("sock %p state %d timeout %ld", sk, sk->sk_state, timeout);
195 cancel_delayed_work(&iso_pi(sk)->conn->timeout_work);184 schedule_delayed_work(&iso_pi(sk)->timeout_work, timeout);
196 schedule_delayed_work(&iso_pi(sk)->conn->timeout_work, timeout);
197}185}
198 186
199static void iso_sock_clear_timer(struct sock *sk)187static void iso_sock_clear_timer(struct sock *sk)
200{188{
201 if (!iso_pi(sk)->conn)189 BT_DBG("sock %p state %d", sk, sk->sk_state);
202 return;190 cancel_delayed_work(&iso_pi(sk)->timeout_work);
191}
192
193static void iso_sock_disable_timer(struct sock *sk)
194{
195 lockdep_assert(!lockdep_sock_is_held(sk));
203 196
204 BT_DBG("sock %p state %d", sk, sk->sk_state);197 BT_DBG("sock %p state %d", sk, sk->sk_state);
205 cancel_delayed_work(&iso_pi(sk)->conn->timeout_work);198 disable_delayed_work_sync(&iso_pi(sk)->timeout_work);
206}199}
207 200
208/* ---- ISO connections ---- */201/* ---- ISO connections ---- */
@@ -227,7 +220,6 @@ static struct iso_conn *iso_conn_add(struct hci_conn *hcon) · 18 unmodified lines
227 220
228 kref_init(&conn->ref);221 kref_init(&conn->ref);
229 spin_lock_init(&conn->lock);222 spin_lock_init(&conn->lock);
230 INIT_DELAYED_WORK(&conn->timeout_work, iso_sock_timeout);
231 223
232 hcon->iso_data = conn;224 hcon->iso_data = conn;
233 conn->hcon = hcon;225 conn->hcon = hcon;
@@ -292,8 +284,9 @@ static void iso_conn_del(struct hci_conn *hcon, int err) · 58 unmodified lines
292 return;284 return;
293 }285 }
294 286
287 iso_sock_disable_timer(sk);
288
295 lock_sock(sk);289 lock_sock(sk);
296 iso_sock_clear_timer(sk);
297 iso_chan_del(sk, err);290 iso_chan_del(sk, err);
298 release_sock(sk);291 release_sock(sk);
299 iso_sock_kill(sk);292 iso_sock_kill(sk);
@@ -800,6 +793,8 @@ static void iso_sock_cleanup_listen(struct sock *parent) · 500 unmodified lines
800 */793 */
801static void iso_sock_kill(struct sock *sk)794static void iso_sock_kill(struct sock *sk)
802{795{
796 iso_sock_disable_timer(sk);
797
803 lock_sock(sk);798 lock_sock(sk);
804 799
805 if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket ||800 if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket ||
@@ -895,8 +890,9 @@ static void __iso_sock_close(struct sock *sk) · 89 unmodified lines
895/* Must be called on unlocked socket. */890/* Must be called on unlocked socket. */
896static void iso_sock_close(struct sock *sk)891static void iso_sock_close(struct sock *sk)
897{892{
893 iso_sock_disable_timer(sk);
894
898 lock_sock(sk);895 lock_sock(sk);
899 iso_sock_clear_timer(sk);
900 __iso_sock_close(sk);896 __iso_sock_close(sk);
901 release_sock(sk);897 release_sock(sk);
902}898}
@@ -965,6 +961,8 @@ static struct sock *iso_sock_alloc(struct net *net, struct socket *sock, · 62 unmodified lines
965 iso_pi(sk)->qos = default_qos;961 iso_pi(sk)->qos = default_qos;
966 iso_pi(sk)->sync_handle = -1;962 iso_pi(sk)->sync_handle = -1;
967 963
964 INIT_DELAYED_WORK(&iso_pi(sk)->timeout_work, iso_sock_timeout);
965
968 bt_sock_link(&iso_sk_list, sk);966 bt_sock_link(&iso_sk_list, sk);
969 return sk;967 return sk;
970}968}

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t