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
3737 spinlock_t lock;
3838 struct sock *sk;
3939
40− struct delayed_work timeout_work;
41−
4240 struct sk_buff *rx_skb;
4341 __u32 rx_len;
4442 __u16 tx_sn;
@@ -81,6 +79,7 @@ struct iso_pinfo { · 36 unmodified lines
8179 __u8 base_len;
8280 __u8 base[BASE_MAX_LENGTH];
8381 struct iso_conn *conn;
82+ struct delayed_work timeout_work;
8483 };
8584
8685 static struct bt_iso_qos default_qos;
@@ -118,9 +117,6 @@ static void iso_conn_free(struct kref *ref) · 31 unmodified lines
118117 hci_conn_drop(conn->hcon);
119118 }
120119
121− /* Ensure no more work items will run since hci_conn has been dropped */
122− disable_delayed_work_sync(&conn->timeout_work);
123−
124120 kfree_skb(conn->rx_skb);
125121
126122 kfree(conn);
@@ -161,48 +157,45 @@ static struct sock *iso_sock_hold(struct iso_conn *conn) · 34 unmodified lines
161157
162158 static void iso_sock_timeout(struct work_struct *work)
163159 {
164− struct iso_conn *conn = container_of(work, struct iso_conn,
165− timeout_work.work);
166− struct sock *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;
160+ struct iso_pinfo *pi = container_of(work, struct iso_pinfo,
161+ timeout_work.work);
162+ struct sock *sk = &pi->bt.sk;
179163
180164 BT_DBG("sock %p state %d", sk, sk->sk_state);
181165
182166 lock_sock(sk);
183− sk->sk_err = ETIMEDOUT;
184− sk->sk_state_change(sk);
167+ if (!sock_flag(sk, SOCK_ZAPPED)) {
168+ sk->sk_err = ETIMEDOUT;
169+ sk->sk_state_change(sk);
170+ }
185171 release_sock(sk);
186− sock_put(sk);
187172 }
188173
189174 static void iso_sock_set_timer(struct sock *sk, long timeout)
190175 {
176+ lockdep_assert(lockdep_sock_is_held(sk));
177+
178+ cancel_delayed_work(&iso_pi(sk)->timeout_work);
179+
191180 if (!iso_pi(sk)->conn)
192181 return;
193182
194183 BT_DBG("sock %p state %d timeout %ld", sk, sk->sk_state, timeout);
195− cancel_delayed_work(&iso_pi(sk)->conn->timeout_work);
196− schedule_delayed_work(&iso_pi(sk)->conn->timeout_work, timeout);
184+ schedule_delayed_work(&iso_pi(sk)->timeout_work, timeout);
197185 }
198186
199187 static void iso_sock_clear_timer(struct sock *sk)
200188 {
201− if (!iso_pi(sk)->conn)
202− return;
189+ BT_DBG("sock %p state %d", sk, sk->sk_state);
190+ cancel_delayed_work(&iso_pi(sk)->timeout_work);
191+}
192+
193+static void iso_sock_disable_timer(struct sock *sk)
194+{
195+ lockdep_assert(!lockdep_sock_is_held(sk));
203196
204197 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);
206199 }
207200
208201 /* ---- ISO connections ---- */
@@ -227,7 +220,6 @@ static struct iso_conn *iso_conn_add(struct hci_conn *hcon) · 18 unmodified lines
227220
228221 kref_init(&conn->ref);
229222 spin_lock_init(&conn->lock);
230− INIT_DELAYED_WORK(&conn->timeout_work, iso_sock_timeout);
231223
232224 hcon->iso_data = conn;
233225 conn->hcon = hcon;
@@ -292,8 +284,9 @@ static void iso_conn_del(struct hci_conn *hcon, int err) · 58 unmodified lines
292284 return;
293285 }
294286
287+ iso_sock_disable_timer(sk);
288+
295289 lock_sock(sk);
296− iso_sock_clear_timer(sk);
297290 iso_chan_del(sk, err);
298291 release_sock(sk);
299292 iso_sock_kill(sk);
@@ -800,6 +793,8 @@ static void iso_sock_cleanup_listen(struct sock *parent) · 500 unmodified lines
800793 */
801794 static void iso_sock_kill(struct sock *sk)
802795 {
796+ iso_sock_disable_timer(sk);
797+
803798 lock_sock(sk);
804799
805800 if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket ||
@@ -895,8 +890,9 @@ static void __iso_sock_close(struct sock *sk) · 89 unmodified lines
895890 /* Must be called on unlocked socket. */
896891 static void iso_sock_close(struct sock *sk)
897892 {
893+ iso_sock_disable_timer(sk);
894+
898895 lock_sock(sk);
899− iso_sock_clear_timer(sk);
900896 __iso_sock_close(sk);
901897 release_sock(sk);
902898 }
@@ -965,6 +961,8 @@ static struct sock *iso_sock_alloc(struct net *net, struct socket *sock, · 62 unmodified lines
965961 iso_pi(sk)->qos = default_qos;
966962 iso_pi(sk)->sync_handle = -1;
967963
964+ INIT_DELAYED_WORK(&iso_pi(sk)->timeout_work, iso_sock_timeout);
965+
968966 bt_sock_link(&iso_sk_list, sk);
969967 return sk;
970968 }

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