Commit d793458c45df ("nfc: rawsock: cancel tx_work before socket
teardown") added SEND_SHUTDOWN and cancel_work_sync(&tx_work) to
rawsock_release() to prevent rawsock_tx_work() from running against a
freed socket or NCI device.
However, two races remain on an in-flight nfc_data_exchange() transmit:
1. cancel_work_sync() only cancels queued work and waits for any
running tx_work instance; it does not stop an asynchronous completion
callback from re-queueing tx_work afterwards. If
rawsock_data_exchange_complete() finishes on another context after
cancel_work_sync() returns, it does not check SEND_SHUTDOWN and
unconditionally calls schedule_work(&tx_work) when sk_write_queue
is non-empty. That can re-arm tx_work past rawsock_release() and
sock_put(sk), leading to a use-after-free in rawsock_tx_work().
Likewise, rawsock_report_error() sets SHUTDOWN_MASK without holding
sk_write_queue.lock before purging sk_write_queue.
2. If rawsock_tx_work() starts concurrently with an error completion
that calls rawsock_report_error() and purges sk_write_queue after
tx_work checks SEND_SHUTDOWN, skb_dequeue() returns NULL and passes
NULL to skb_get_kcov_handle(skb).
Serialize SEND_SHUTDOWN updates under sk_write_queue.lock, replace
cancel_work_sync() with disable_work_sync() in rawsock_release(), check
SEND_SHUTDOWN under sk_write_queue.lock before scheduling tx_work in
rawsock_data_exchange_complete(), and bail out in rawsock_tx_work() if
skb_dequeue() returns NULL.
Found during a GLM-5.4-assisted code audit and verified manually against
mainline; compile-tested only (KCFLAGS="-Werror" on net/nfc/rawsock.o).
Fixes: d793458c45df ("nfc: rawsock: cancel tx_work before socket teardown")
Cc: stab…@vger.kernel.org
Signed-off-by: Hu Xinyao <huxi…@kylinsec.com.cn>
---
net/nfc/rawsock.c | 25 +++++++++++++++++--------
1 file changed, 17 insertions(+), 8 deletions(-)
diff --git a/net/nfc/rawsock.c b/net/nfc/rawsock.c
index f7d7a59..5bec4b6 100644
--- a/net/nfc/rawsock.c
+++ b/net/nfc/rawsock.c
@@ -48,11 +48,14 @@ 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;
+ __skb_queue_purge(&sk->sk_write_queue);
+ nfc_rawsock(sk)->tx_work_scheduled = false;
+ 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)
@@ -68,13 +71,16 @@ static int rawsock_release(struct socket *sock)
nfc_sock_unlink(&raw_sk_list, sk);
if (sk->sk_state == TCP_ESTABLISHED) {
- /* Prevent rawsock_tx_work from starting new transmits and
- * wait for any in-progress work to finish. This must happen
- * before the socket is orphaned to avoid a race where
- * rawsock_tx_work runs after the NCI device has been freed.
+ /* Prevent rawsock_tx_work from starting new transmits or
+ * being re-queued by rawsock_data_exchange_complete(), and
+ * wait for any in-progress work to finish before orphaning
+ * the socket 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;
- cancel_work_sync(&nfc_rawsock(sk)->tx_work);
+ spin_unlock_bh(&sk->sk_write_queue.lock);
+ disable_work_sync(&nfc_rawsock(sk)->tx_work);
rawsock_write_queue_purge(sk);
}
@@ -168,7 +174,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;
@@ -201,6 +208,8 @@ static void rawsock_tx_work(struct work_struct *work)
}
skb = skb_dequeue(&sk->sk_write_queue);
+ if (!skb)
+ return;
kcov_remote_start_common(skb_get_kcov_handle(skb));
sock_hold(sk);
--
2.43.0