Skip to content

Commit 0f313eb

Browse files
Mani-Sadhasivamgregkh
authored andcommitted
net: qrtr: ns: Fix use-after-free in driver remove()
commit 7809fea upstream. 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: 0c2204a ("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> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 3a50236 commit 0f313eb

1 file changed

Lines changed: 11 additions & 0 deletions

File tree

net/qrtr/ns.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ static struct {
2424
struct list_head lookups;
2525
struct workqueue_struct *workqueue;
2626
struct work_struct work;
27+
void (*saved_data_ready)(struct sock *sk);
2728
int local_node;
2829
} qrtr_ns;
2930

@@ -706,6 +707,7 @@ int qrtr_ns_init(void)
706707
goto err_sock;
707708
}
708709

710+
qrtr_ns.saved_data_ready = qrtr_ns.sock->sk->sk_data_ready;
709711
qrtr_ns.sock->sk->sk_data_ready = qrtr_ns_data_ready;
710712

711713
sq.sq_port = QRTR_PORT_CTRL;
@@ -746,6 +748,10 @@ int qrtr_ns_init(void)
746748
return 0;
747749

748750
err_wq:
751+
write_lock_bh(&qrtr_ns.sock->sk->sk_callback_lock);
752+
qrtr_ns.sock->sk->sk_data_ready = qrtr_ns.saved_data_ready;
753+
write_unlock_bh(&qrtr_ns.sock->sk->sk_callback_lock);
754+
749755
destroy_workqueue(qrtr_ns.workqueue);
750756
err_sock:
751757
sock_release(qrtr_ns.sock);
@@ -755,7 +761,12 @@ EXPORT_SYMBOL_GPL(qrtr_ns_init);
755761

756762
void qrtr_ns_remove(void)
757763
{
764+
write_lock_bh(&qrtr_ns.sock->sk->sk_callback_lock);
765+
qrtr_ns.sock->sk->sk_data_ready = qrtr_ns.saved_data_ready;
766+
write_unlock_bh(&qrtr_ns.sock->sk->sk_callback_lock);
767+
758768
cancel_work_sync(&qrtr_ns.work);
769+
synchronize_net();
759770
destroy_workqueue(qrtr_ns.workqueue);
760771

761772
/* sock_release() expects the two references that were put during

0 commit comments

Comments
 (0)