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.
Unified · Split
| @@ -37,8 +37,6 @@ struct iso_conn { · 36 unmodified lines | ||
| 37 | 37 | spinlock_t lock; |
| 38 | 38 | struct sock *sk; |
| 39 | 39 | |
| 40 | − struct delayed_work timeout_work; | |
| 41 | − | |
| 42 | 40 | struct sk_buff *rx_skb; |
| 43 | 41 | __u32 rx_len; |
| 44 | 42 | __u16 tx_sn; |
| @@ -81,6 +79,7 @@ struct iso_pinfo { · 36 unmodified lines | ||
| 81 | 79 | __u8 base_len; |
| 82 | 80 | __u8 base[BASE_MAX_LENGTH]; |
| 83 | 81 | struct iso_conn *conn; |
| 82 | + struct delayed_work timeout_work; | |
| 84 | 83 | }; |
| 85 | 84 | |
| 86 | 85 | static struct bt_iso_qos default_qos; |
| @@ -118,9 +117,6 @@ static void iso_conn_free(struct kref *ref) · 31 unmodified lines | ||
| 118 | 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 | 120 | kfree_skb(conn->rx_skb); |
| 125 | 121 | |
| 126 | 122 | kfree(conn); |
| @@ -161,48 +157,45 @@ static struct sock *iso_sock_hold(struct iso_conn *conn) · 34 unmodified lines | ||
| 161 | 157 | |
| 162 | 158 | static void iso_sock_timeout(struct work_struct *work) |
| 163 | 159 | { |
| 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; | |
| 179 | 163 | |
| 180 | 164 | BT_DBG("sock %p state %d", sk, sk->sk_state); |
| 181 | 165 | |
| 182 | 166 | 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 | + } | |
| 185 | 171 | release_sock(sk); |
| 186 | − sock_put(sk); | |
| 187 | 172 | } |
| 188 | 173 | |
| 189 | 174 | static 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 | 180 | if (!iso_pi(sk)->conn) |
| 192 | 181 | return; |
| 193 | 182 | |
| 194 | 183 | 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); | |
| 197 | 185 | } |
| 198 | 186 | |
| 199 | 187 | static void iso_sock_clear_timer(struct sock *sk) |
| 200 | 188 | { |
| 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)); | |
| 203 | 196 | |
| 204 | 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 | 201 | /* ---- ISO connections ---- */ |
| @@ -227,7 +220,6 @@ static struct iso_conn *iso_conn_add(struct hci_conn *hcon) · 18 unmodified lines | ||
| 227 | 220 | |
| 228 | 221 | kref_init(&conn->ref); |
| 229 | 222 | spin_lock_init(&conn->lock); |
| 230 | − INIT_DELAYED_WORK(&conn->timeout_work, iso_sock_timeout); | |
| 231 | 223 | |
| 232 | 224 | hcon->iso_data = conn; |
| 233 | 225 | conn->hcon = hcon; |
| @@ -292,8 +284,9 @@ static void iso_conn_del(struct hci_conn *hcon, int err) · 58 unmodified lines | ||
| 292 | 284 | return; |
| 293 | 285 | } |
| 294 | 286 | |
| 287 | + iso_sock_disable_timer(sk); | |
| 288 | + | |
| 295 | 289 | lock_sock(sk); |
| 296 | − iso_sock_clear_timer(sk); | |
| 297 | 290 | iso_chan_del(sk, err); |
| 298 | 291 | release_sock(sk); |
| 299 | 292 | iso_sock_kill(sk); |
| @@ -800,6 +793,8 @@ static void iso_sock_cleanup_listen(struct sock *parent) · 500 unmodified lines | ||
| 800 | 793 | */ |
| 801 | 794 | static void iso_sock_kill(struct sock *sk) |
| 802 | 795 | { |
| 796 | + iso_sock_disable_timer(sk); | |
| 797 | + | |
| 803 | 798 | lock_sock(sk); |
| 804 | 799 | |
| 805 | 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 | 890 | /* Must be called on unlocked socket. */ |
| 896 | 891 | static void iso_sock_close(struct sock *sk) |
| 897 | 892 | { |
| 893 | + iso_sock_disable_timer(sk); | |
| 894 | + | |
| 898 | 895 | lock_sock(sk); |
| 899 | − iso_sock_clear_timer(sk); | |
| 900 | 896 | __iso_sock_close(sk); |
| 901 | 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 | 961 | iso_pi(sk)->qos = default_qos; |
| 966 | 962 | iso_pi(sk)->sync_handle = -1; |
| 967 | 963 | |
| 964 | + INIT_DELAYED_WORK(&iso_pi(sk)->timeout_work, iso_sock_timeout); | |
| 965 | + | |
| 968 | 966 | bt_sock_link(&iso_sk_list, sk); |
| 969 | 967 | return sk; |
| 970 | 968 | } |