From: Bin Luo (University of Electronic Science and Technology of China) 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) without checking for NULL. If rawsock_report_error() or rawsock_release() purges sk_write_queue in between, skb is NULL and skb_get_kcov_handle() dereferences NULL. 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(), update sk_shutdown and purge sk_write_queue under sk_write_queue.lock. Discovered by static analysis and AI-assisted code review. Confirmed by inspecting the window between cancel_work_sync() and rawsock_write_queue_purge() via ftrace without modifying the kernel; not triggered standalone without delay injection, and regression-tested in QEMU KVM (KASAN, LOCKDEP, DEBUG_OBJECTS) against virtual_ncidev teardown loops and tools/testing/selftests/nci/nci_dev (8/8 passed). Fixes: d793458c45df ("nfc: rawsock: cancel tx_work before socket teardown") Fixes: 7e8cdc97148c ("nfc: Add KCOV annotations") Cc: stable@vger.kernel.org Signed-off-by: Bin Luo (University of Electronic Science and Technology of China) --- net/nfc/rawsock.c | 44 ++++++++++++++++++++++++++++++-------------- 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/net/nfc/rawsock.c b/net/nfc/rawsock.c index f7d7a59..291f509 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); + spin_lock_bh(&sk->sk_write_queue.lock); sk->sk_shutdown = SHUTDOWN_MASK; + __rawsock_write_queue_purge(sk); + spin_unlock_bh(&sk->sk_write_queue.lock); + sk->sk_err = -err; sk_error_report(sk); - - rawsock_write_queue_purge(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