]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
net: qrtr: ns: Fix use-after-free in driver remove()
authorManivannan Sadhasivam <manivannan.sadhasivam@oss.qualcomm.com>
Thu, 9 Apr 2026 17:34:16 +0000 (23:04 +0530)
committerJakub Kicinski <kuba@kernel.org>
Mon, 13 Apr 2026 22:34:07 +0000 (15:34 -0700)
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: 0c2204a4ad71 ("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>
net/qrtr/ns.c

index c0418764470bed7d536854a903da6af6b414d109..b3f9bbcf9ab9ba1b383670b838a729cb05fb72b5 100644 (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