net: qrtr: ns: Fix use-after-free in driver remove()

In the remove callback, if a packet arrives after destroy_workqueue() is
called, but before sock_release(), the qrtr_ns_data_ready() callback will
try to queue the work, causing use-after-free issue.

Fix this issue by saving the default 'sk_data_ready' callback during
qrtr_ns_init() and use it to replace the qrtr_ns_data_ready() callback at
the start of remove(). This ensures that even if a packet arrives after
destroy_workqueue(), the work struct will not be dereferenced.

Note that it is also required to ensure that the RX threads are completed
before destroying the workqueue, because the threads could be using the
qrtr_ns_data_ready() callback.

Cc: stable@vger.kernel.org
Fixes: 0c2204a4ad ("net: qrtr: Migrate nameservice to kernel from userspace")
Signed-off-by: Manivannan Sadhasivam <manivannan.sadhasivam@oss.qualcomm.com>
Link: https://patch.msgid.link/20260409-qrtr-fix-v3-5-00a8a5ff2b51@oss.qualcomm.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
This commit is contained in:
Manivannan Sadhasivam 2026-04-09 23:04:16 +05:30 committed by Jakub Kicinski
parent 27d5e84e81
commit 7809fea20c

View File

@ -25,6 +25,7 @@ static struct {
u32 lookup_count;
struct workqueue_struct *workqueue;
struct work_struct work;
void (*saved_data_ready)(struct sock *sk);
int local_node;
} qrtr_ns;
@ -757,6 +758,7 @@ int qrtr_ns_init(void)
goto err_sock;
}
qrtr_ns.saved_data_ready = qrtr_ns.sock->sk->sk_data_ready;
qrtr_ns.sock->sk->sk_data_ready = qrtr_ns_data_ready;
sq.sq_port = QRTR_PORT_CTRL;
@ -797,6 +799,10 @@ int qrtr_ns_init(void)
return 0;
err_wq:
write_lock_bh(&qrtr_ns.sock->sk->sk_callback_lock);
qrtr_ns.sock->sk->sk_data_ready = qrtr_ns.saved_data_ready;
write_unlock_bh(&qrtr_ns.sock->sk->sk_callback_lock);
destroy_workqueue(qrtr_ns.workqueue);
err_sock:
sock_release(qrtr_ns.sock);
@ -806,7 +812,12 @@ EXPORT_SYMBOL_GPL(qrtr_ns_init);
void qrtr_ns_remove(void)
{
write_lock_bh(&qrtr_ns.sock->sk->sk_callback_lock);
qrtr_ns.sock->sk->sk_data_ready = qrtr_ns.saved_data_ready;
write_unlock_bh(&qrtr_ns.sock->sk->sk_callback_lock);
cancel_work_sync(&qrtr_ns.work);
synchronize_net();
destroy_workqueue(qrtr_ns.workqueue);
/* sock_release() expects the two references that were put during