From: Bin Luo Commit d793458c45df ("nfc: rawsock: cancel tx_work before socket teardown") set SEND_SHUTDOWN and called cancel_work_sync(&tx_work) before rawsock_write_queue_purge() in rawsock_release(). However, two races remain on tx_work and sk_write_queue: 1. rawsock_release() sets SEND_SHUTDOWN without holding sk_write_queue.lock and purges sk_write_queue only after cancel_work_sync(). Meanwhile, rawsock_data_exchange_complete() checks !skb_queue_empty(&sk->sk_write_queue) without checking SEND_SHUTDOWN and schedules tx_work without holding a socket reference. If an in-flight completion runs between cancel_work_sync() and rawsock_write_queue_purge(), it reschedules tx_work while rawsock_release() proceeds to drop the final socket reference, leaving tx_work queued on a freed sock. 2. rawsock_tx_work() checks SEND_SHUTDOWN without sk_write_queue.lock and then passes skb = skb_dequeue(&sk->sk_write_queue) directly to skb_get_kcov_handle(skb) and nfc_data_exchange() without checking for NULL. If rawsock_report_error() purges sk_write_queue in between, skb is NULL and dereferencing it crashes. Serialize SEND_SHUTDOWN updates/checks, write-queue purging, and tx_work scheduling under sk->sk_write_queue.lock: - In rawsock_release(), set SEND_SHUTDOWN and purge sk_write_queue under sk_write_queue.lock before calling cancel_work_sync(). - In rawsock_data_exchange_complete() and rawsock_sendmsg(), refuse to enqueue or schedule tx_work once SEND_SHUTDOWN is set. - In rawsock_tx_work(), check SEND_SHUTDOWN and dequeue skb under sk_write_queue.lock, returning cleanly if the queue is empty. - In rawsock_report_error(), record sk->sk_err with WRITE_ONCE() before acquiring sk_write_queue.lock so any waiters woken by sock_wfree() during queue purging observe the NFC error rather than -EPIPE, then call sk_error_report() after unlocking. Fixes: d793458c45df ("nfc: rawsock: cancel tx_work before socket teardown") Fixes: 23b7869c0fd0 ("NFC: add the NFC socket raw protocol") Cc: stable@vger.kernel.org Signed-off-by: Bin Luo --- Changes in v3: - In rawsock_report_error(), write sk->sk_err with WRITE_ONCE() before acquiring sk_write_queue.lock so waiters woken by sock_wfree() during write-queue purging see the NFC error instead of -EPIPE. - Drop the inaccurate rawsock_release() mention from the race 2 description and fix the second Fixes tag to 23b7869c0fd0. - Normalize From:/Signed-off-by: author metadata. Changes in v2: - Resend cleanly via b4 web relay and include both dependent rawsock patches as a threaded series. net/nfc/rawsock.c | 46 +++++++++++++++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 15 deletions(-) diff --git a/net/nfc/rawsock.c b/net/nfc/rawsock.c index f7d7a59..2fa0f05 100644 --- a/net/nfc/rawsock.c +++ b/net/nfc/rawsock.c @@ -34,25 +34,25 @@ static void nfc_sock_unlink(struct nfc_sock_list *l, struct sock *sk) write_unlock(&l->lock); } -static void rawsock_write_queue_purge(struct sock *sk) +static void __rawsock_write_queue_purge(struct sock *sk) { pr_debug("sk=%p\n", sk); - spin_lock_bh(&sk->sk_write_queue.lock); __skb_queue_purge(&sk->sk_write_queue); nfc_rawsock(sk)->tx_work_scheduled = false; - spin_unlock_bh(&sk->sk_write_queue.lock); } static void rawsock_report_error(struct sock *sk, int err) { pr_debug("sk=%p err=%d\n", sk, err); + WRITE_ONCE(sk->sk_err, -err); + spin_lock_bh(&sk->sk_write_queue.lock); sk->sk_shutdown = SHUTDOWN_MASK; - sk->sk_err = -err; - sk_error_report(sk); + __rawsock_write_queue_purge(sk); + spin_unlock_bh(&sk->sk_write_queue.lock); - rawsock_write_queue_purge(sk); + sk_error_report(sk); } static int rawsock_release(struct socket *sock) @@ -73,9 +73,11 @@ static int rawsock_release(struct socket *sock) * before the socket is orphaned to avoid a race where * rawsock_tx_work runs after the NCI device has been freed. */ + spin_lock_bh(&sk->sk_write_queue.lock); sk->sk_shutdown |= SEND_SHUTDOWN; + __rawsock_write_queue_purge(sk); + spin_unlock_bh(&sk->sk_write_queue.lock); cancel_work_sync(&nfc_rawsock(sk)->tx_work); - rawsock_write_queue_purge(sk); } sock_orphan(sk); @@ -168,7 +170,8 @@ static void rawsock_data_exchange_complete(void *context, struct sk_buff *skb, goto error_skb; spin_lock_bh(&sk->sk_write_queue.lock); - if (!skb_queue_empty(&sk->sk_write_queue)) + if (!(sk->sk_shutdown & SEND_SHUTDOWN) && + !skb_queue_empty(&sk->sk_write_queue)) schedule_work(&nfc_rawsock(sk)->tx_work); else nfc_rawsock(sk)->tx_work_scheduled = false; @@ -195,12 +198,20 @@ static void rawsock_tx_work(struct work_struct *work) pr_debug("sk=%p target_idx=%u\n", sk, target_idx); + spin_lock_bh(&sk->sk_write_queue.lock); if (sk->sk_shutdown & SEND_SHUTDOWN) { - rawsock_write_queue_purge(sk); + __rawsock_write_queue_purge(sk); + spin_unlock_bh(&sk->sk_write_queue.lock); return; } - skb = skb_dequeue(&sk->sk_write_queue); + skb = __skb_dequeue(&sk->sk_write_queue); + if (!skb) { + nfc_rawsock(sk)->tx_work_scheduled = false; + spin_unlock_bh(&sk->sk_write_queue.lock); + return; + } + spin_unlock_bh(&sk->sk_write_queue.lock); kcov_remote_start_common(skb_get_kcov_handle(skb)); sock_hold(sk); @@ -239,14 +250,19 @@ static int rawsock_sendmsg(struct socket *sock, struct msghdr *msg, size_t len) } spin_lock_bh(&sk->sk_write_queue.lock); - __skb_queue_tail(&sk->sk_write_queue, skb); - if (!nfc_rawsock(sk)->tx_work_scheduled) { - schedule_work(&nfc_rawsock(sk)->tx_work); - nfc_rawsock(sk)->tx_work_scheduled = true; + if (sk->sk_shutdown & SEND_SHUTDOWN) { + rc = -EPIPE; + kfree_skb(skb); + } else { + __skb_queue_tail(&sk->sk_write_queue, skb); + if (!nfc_rawsock(sk)->tx_work_scheduled) { + schedule_work(&nfc_rawsock(sk)->tx_work); + nfc_rawsock(sk)->tx_work_scheduled = true; + } } spin_unlock_bh(&sk->sk_write_queue.lock); - return len; + return rc ? : len; } static int rawsock_recvmsg(struct socket *sock, struct msghdr *msg, size_t len, -- 2.43.0