From 9edfe6657fee645d47871c3fd31ac3fd135058a0 Mon Sep 17 00:00:00 2001 From: Chris Lew Date: Mon, 3 Oct 2022 09:57:42 -0700 Subject: [PATCH] net: qrtr: Change port allocation to use cyclic xa There is a race for clients that open sockets before the control port is bound. If a client gets an idr that was allocated before the control port is bound, there is a chance the previous address owner sent lookup packets to the control port. The new address owner will get residual responses to these lookup packets. Change the xa_alloc to xa_alloc_cyclic so new ids are allocated instead of trying to reuse the freed ids. Change-Id: Ie1bda7a818309503f80542e739bac646327296f7 Signed-off-by: Chris Lew --- net/qrtr/af_qrtr.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/net/qrtr/af_qrtr.c b/net/qrtr/af_qrtr.c index bf641890ecd1..05030c63df77 100644 --- a/net/qrtr/af_qrtr.c +++ b/net/qrtr/af_qrtr.c @@ -117,6 +117,7 @@ static DECLARE_RWSEM(qrtr_node_lock); /* local port allocation management */ static DEFINE_XARRAY_ALLOC(qrtr_ports); +u32 qrtr_ports_next = QRTR_MIN_EPH_SOCKET; /** * struct qrtr_node - endpoint node @@ -802,8 +803,9 @@ static int qrtr_port_assign(struct qrtr_sock *ipc, int *port) int rc; if (!*port) { - rc = xa_alloc(&qrtr_ports, port, ipc, QRTR_EPH_PORT_RANGE, - GFP_KERNEL); + rc = xa_alloc_cyclic(&qrtr_ports, port, ipc, + QRTR_EPH_PORT_RANGE, &qrtr_ports_next, + GFP_KERNEL); } else if (*port < QRTR_MIN_EPH_SOCKET && !capable(CAP_NET_ADMIN)) { rc = -EACCES; } else if (*port == QRTR_PORT_CTRL) {