Linux RDMA and InfiniBand development
 help / color / mirror / Atom feed
* [PATCH v2 0/3] RDMA/iwpm: fix the port-mapper request lifetime
@ 2026-12-06  6:57 Daniel Wade
  2026-12-06  6:57 ` [PATCH v2 1/3] RDMA/iwpm: fix use-after-free of the netlink request object Daniel Wade
                   ` (3 more replies)
  0 siblings, 4 replies; 7+ messages in thread
From: Daniel Wade @ 2026-12-06  6:57 UTC (permalink / raw)
  To: Jason Gunthorpe, Leon Romanovsky
  Cc: Jeffin Philip, Shuangpeng Bai, linux-rdma, linux-kernel

Patch 1 gives the request a single owner. Patch 2 records which reply
each request expects. Patch 3 uses that to keep the data inside the
request, so no stack address reaches a netlink callback. Patch 3 needs
the field patch 2 adds.

Only patch 1 is tagged for stable.

v1: https://lore.kernel.org/all/20261205144111.8366-1-danjwade95@gmail.com/
v2: document @nl_op, which kernel-doc warns about at W=1;
    new patch 3, which removes the stack pointer raised against
    v1 1/2.


Daniel Wade (3):
  RDMA/iwpm: fix use-after-free of the netlink request object
  RDMA/iwpm: reject a port-mapper reply meant for another request type
  RDMA/iwpm: keep the port-mapper request data inside the request

 drivers/infiniband/core/iwpm_msg.c  | 65 ++++++++++++++++++++---------
 drivers/infiniband/core/iwpm_util.c | 10 ++---
 drivers/infiniband/core/iwpm_util.h | 14 +++++--
 3 files changed, 62 insertions(+), 27 deletions(-)


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH v2 1/3] RDMA/iwpm: fix use-after-free of the netlink request object
  2026-12-06  6:57 [PATCH v2 0/3] RDMA/iwpm: fix the port-mapper request lifetime Daniel Wade
@ 2026-12-06  6:57 ` Daniel Wade
  2026-12-06  6:57 ` [PATCH v2 2/3] RDMA/iwpm: reject a port-mapper reply meant for another request type Daniel Wade
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 7+ messages in thread
From: Daniel Wade @ 2026-12-06  6:57 UTC (permalink / raw)
  To: Jason Gunthorpe, Leon Romanovsky
  Cc: Jeffin Philip, Shuangpeng Bai, linux-rdma, linux-kernel

iwpm_get_nlmsg_request() takes two references but only one is ever
dropped, so the request is never freed. It stays on iwpm_nlmsg_req_list
with req_buffer still pointing at iw_cm_map()'s stack frame.

The three producer error paths get rid of it by calling
iwpm_free_nlmsg_request() directly. That frees the object while a
netlink callback is still holding it, and the callback then calls up()
on the semaphore inside it.

A reply can quote the sequence number of a listen that has already
returned. That finds the stale request and reads through req_buffer
into another function's frame:

  BUG: KASAN: stack-out-of-bounds in iwpm_compare_sockaddr+0x1d5/0x1f0
  Read of size 2 at addr ffff888008227678 by task init/1
  CPU: 1 UID: 0 PID: 1 Comm: init Not tainted 7.4.0-rc3 #1 PREEMPT(lazy)
  Call Trace:
   <TASK>
   iwpm_compare_sockaddr+0x1d5/0x1f0
   iwpm_add_and_query_mapping_cb+0x1fb/0x680
   netlink_dump+0x48f/0x11a0
   __netlink_dump_start+0x652/0x8c0
   rdma_nl_rcv_msg+0x3ee/0x4e0
   rdma_nl_rcv_skb.constprop.0.isra.0+0x21d/0x370
   netlink_unicast+0x68f/0x940
   netlink_sendmsg+0x810/0xf20
   __sys_sendto+0x337/0x390
   __x64_sys_sendto+0xdb/0x1b0
   do_syscall_64+0xdf/0x4c0
   entry_SYSCALL_64_after_hwframe+0x77/0x7f
   </TASK>
  This frame has 1 object:
   [32, 88) 'nltb'

Take one reference. Look the request up with kref_get_unless_zero() and
release it with kref_put() on every path. Wake the waiter before the
callback drops its reference.

Reported-by: Shuangpeng Bai <shuangpeng.kernel@gmail.com>
Closes: https://lore.kernel.org/all/20261105233640.1667884-1-shuangpeng.kernel@gmail.com/
Fixes: 30dc5e63d6a5 ("RDMA/core: Add support for iWARP Port Mapper user space service")
Cc: stable@vger.kernel.org
Signed-off-by: Daniel Wade <danjwade95@gmail.com>
---

Notes:
    v2: unchanged from v1.
    
    Reproduced on v7.4-rc3 with CONFIG_RDMA_SIW and KASAN, on an unmodified kernel
    with no inserted delay and no error injection. Before the patch four completed
    requests were still findable and the stale lookup tripped KASAN; after it all
    twelve probes report no matching request and KASAN is silent.

 drivers/infiniband/core/iwpm_msg.c  | 22 +++++++++++-----------
 drivers/infiniband/core/iwpm_util.c |  5 ++---
 2 files changed, 13 insertions(+), 14 deletions(-)

diff --git a/drivers/infiniband/core/iwpm_msg.c b/drivers/infiniband/core/iwpm_msg.c
index 1b10f2973ad9d..d050aa0db30fb 100644
--- a/drivers/infiniband/core/iwpm_msg.c
+++ b/drivers/infiniband/core/iwpm_msg.c
@@ -122,7 +122,7 @@ int iwpm_register_pid(struct iwpm_dev_data *pm_msg, u8 nl_client)
 	pr_info("%s: %s (client = %u)\n", __func__, err_str, nl_client);
 	dev_kfree_skb(skb);
 	if (nlmsg_request)
-		iwpm_free_nlmsg_request(&nlmsg_request->kref);
+		kref_put(&nlmsg_request->kref, iwpm_free_nlmsg_request);
 	return ret;
 }
 
@@ -207,7 +207,7 @@ int iwpm_add_mapping(struct iwpm_sa_data *pm_msg, u8 nl_client)
 add_mapping_error_nowarn:
 	dev_kfree_skb(skb);
 	if (nlmsg_request)
-		iwpm_free_nlmsg_request(&nlmsg_request->kref);
+		kref_put(&nlmsg_request->kref, iwpm_free_nlmsg_request);
 	return ret;
 }
 
@@ -296,7 +296,7 @@ int iwpm_add_and_query_mapping(struct iwpm_sa_data *pm_msg, u8 nl_client)
 query_mapping_error_nowarn:
 	dev_kfree_skb(skb);
 	if (nlmsg_request)
-		iwpm_free_nlmsg_request(&nlmsg_request->kref);
+		kref_put(&nlmsg_request->kref, iwpm_free_nlmsg_request);
 	return ret;
 }
 
@@ -431,10 +431,10 @@ int iwpm_register_pid_cb(struct sk_buff *skb, struct netlink_callback *cb)
 	iwpm_set_registration(nl_client, IWPM_REG_VALID);
 register_pid_response_exit:
 	nlmsg_request->request_done = 1;
-	/* always for found nlmsg_request */
-	kref_put(&nlmsg_request->kref, iwpm_free_nlmsg_request);
 	barrier();
 	up(&nlmsg_request->sem);
+	/* always for found nlmsg_request */
+	kref_put(&nlmsg_request->kref, iwpm_free_nlmsg_request);
 	return 0;
 }
 
@@ -503,10 +503,10 @@ int iwpm_add_mapping_cb(struct sk_buff *skb, struct netlink_callback *cb)
 
 add_mapping_response_exit:
 	nlmsg_request->request_done = 1;
-	/* always for found request */
-	kref_put(&nlmsg_request->kref, iwpm_free_nlmsg_request);
 	barrier();
 	up(&nlmsg_request->sem);
+	/* always for found request */
+	kref_put(&nlmsg_request->kref, iwpm_free_nlmsg_request);
 	return 0;
 }
 
@@ -601,10 +601,10 @@ int iwpm_add_and_query_mapping_cb(struct sk_buff *skb,
 			"query_mapping: Mapped remote sockaddr:");
 query_mapping_response_exit:
 	nlmsg_request->request_done = 1;
-	/* always for found request */
-	kref_put(&nlmsg_request->kref, iwpm_free_nlmsg_request);
 	barrier();
 	up(&nlmsg_request->sem);
+	/* always for found request */
+	kref_put(&nlmsg_request->kref, iwpm_free_nlmsg_request);
 	return 0;
 }
 
@@ -800,10 +800,10 @@ int iwpm_mapping_error_cb(struct sk_buff *skb, struct netlink_callback *cb)
 	atomic_set(&echo_nlmsg_seq, cb->nlh->nlmsg_seq);
 	nlmsg_request->err_code = err_code;
 	nlmsg_request->request_done = 1;
-	/* always for found request */
-	kref_put(&nlmsg_request->kref, iwpm_free_nlmsg_request);
 	barrier();
 	up(&nlmsg_request->sem);
+	/* always for found request */
+	kref_put(&nlmsg_request->kref, iwpm_free_nlmsg_request);
 	return 0;
 }
 
diff --git a/drivers/infiniband/core/iwpm_util.c b/drivers/infiniband/core/iwpm_util.c
index 51af8c1c49f9e..639ea80373ea6 100644
--- a/drivers/infiniband/core/iwpm_util.c
+++ b/drivers/infiniband/core/iwpm_util.c
@@ -315,7 +315,6 @@ struct iwpm_nlmsg_request *iwpm_get_nlmsg_request(__u32 nlmsg_seq,
 		return NULL;
 
 	kref_init(&nlmsg_request->kref);
-	kref_get(&nlmsg_request->kref);
 	nlmsg_request->nlmsg_seq = nlmsg_seq;
 	nlmsg_request->nl_client = nl_client;
 	nlmsg_request->request_done = 0;
@@ -357,8 +356,8 @@ struct iwpm_nlmsg_request *iwpm_find_nlmsg_request(__u32 echo_seq)
 	list_for_each_entry(nlmsg_request, &iwpm_nlmsg_req_list,
 			    inprocess_list) {
 		if (nlmsg_request->nlmsg_seq == echo_seq) {
-			found_request = nlmsg_request;
-			kref_get(&nlmsg_request->kref);
+			if (kref_get_unless_zero(&nlmsg_request->kref))
+				found_request = nlmsg_request;
 			break;
 		}
 	}

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH v2 2/3] RDMA/iwpm: reject a port-mapper reply meant for another request type
  2026-12-06  6:57 [PATCH v2 0/3] RDMA/iwpm: fix the port-mapper request lifetime Daniel Wade
  2026-12-06  6:57 ` [PATCH v2 1/3] RDMA/iwpm: fix use-after-free of the netlink request object Daniel Wade
@ 2026-12-06  6:57 ` Daniel Wade
  2026-12-06  6:57 ` [PATCH v2 3/3] RDMA/iwpm: keep the port-mapper request data inside the request Daniel Wade
  2026-12-06  7:12 ` [PATCH v2 0/3] RDMA/iwpm: fix the port-mapper request lifetime sashiko-bot
  3 siblings, 0 replies; 7+ messages in thread
From: Daniel Wade @ 2026-12-06  6:57 UTC (permalink / raw)
  To: Jason Gunthorpe, Leon Romanovsky
  Cc: Jeffin Philip, Shuangpeng Bai, linux-rdma, linux-kernel

Requests are looked up by sequence number only, and req_buffer is a
void * that the two producers point at different types: a 48-byte
struct iwpm_dev_data from iwpm_register_pid(), a 520-byte struct
iwpm_sa_data from iwpm_add_mapping().

So an ADD_MAPPING reply to a REG_PID request copies 128 bytes to offset
128 of the 48-byte object, over another local of iw_cm_map(). The copy
only happens if the sockaddr family matches first. That family is read
from dev_name[]. So it takes a device whose name begins with those two
bytes, which needs CAP_NET_ADMIN to create.

Record the op each request expects and refuse a reply that does not
match.

Fixes: 30dc5e63d6a5 ("RDMA/core: Add support for iWARP Port Mapper user space service")
Signed-off-by: Daniel Wade <danjwade95@gmail.com>
---

Notes:
    v2: document the new @nl_op parameter, which kernel-doc warns about at W=1.
    Confirmed against a clean control with tools/docs/kernel-doc.
    
    Reproduced on the same kernel: an ADD_MAPPING reply to a REG_PID request is
    refused with "Request does not expect this reply" and the request then times
    out normally.

 drivers/infiniband/core/iwpm_msg.c  | 24 ++++++++++++++++++++++--
 drivers/infiniband/core/iwpm_util.c |  4 +++-
 drivers/infiniband/core/iwpm_util.h |  5 ++++-
 3 files changed, 29 insertions(+), 4 deletions(-)

diff --git a/drivers/infiniband/core/iwpm_msg.c b/drivers/infiniband/core/iwpm_msg.c
index d050aa0db30fb..1d77349db51e5 100644
--- a/drivers/infiniband/core/iwpm_msg.c
+++ b/drivers/infiniband/core/iwpm_msg.c
@@ -78,7 +78,9 @@ int iwpm_register_pid(struct iwpm_dev_data *pm_msg, u8 nl_client)
 		goto pid_query_error;
 	}
 	nlh->nlmsg_seq = iwpm_get_nlmsg_seq();
-	nlmsg_request = iwpm_get_nlmsg_request(nlh->nlmsg_seq, nl_client, GFP_KERNEL);
+	nlmsg_request = iwpm_get_nlmsg_request(nlh->nlmsg_seq,
+					       RDMA_NL_IWPM_REG_PID,
+					       nl_client, GFP_KERNEL);
 	if (!nlmsg_request) {
 		err_str = "Unable to allocate netlink request";
 		goto pid_query_error;
@@ -161,7 +163,9 @@ int iwpm_add_mapping(struct iwpm_sa_data *pm_msg, u8 nl_client)
 		goto add_mapping_error;
 	}
 	nlh->nlmsg_seq = iwpm_get_nlmsg_seq();
-	nlmsg_request = iwpm_get_nlmsg_request(nlh->nlmsg_seq, nl_client, GFP_KERNEL);
+	nlmsg_request = iwpm_get_nlmsg_request(nlh->nlmsg_seq,
+					       RDMA_NL_IWPM_ADD_MAPPING,
+					       nl_client, GFP_KERNEL);
 	if (!nlmsg_request) {
 		err_str = "Unable to allocate netlink request";
 		goto add_mapping_error;
@@ -246,6 +250,7 @@ int iwpm_add_and_query_mapping(struct iwpm_sa_data *pm_msg, u8 nl_client)
 	}
 	nlh->nlmsg_seq = iwpm_get_nlmsg_seq();
 	nlmsg_request = iwpm_get_nlmsg_request(nlh->nlmsg_seq,
+				RDMA_NL_IWPM_QUERY_MAPPING,
 				nl_client, GFP_KERNEL);
 	if (!nlmsg_request) {
 		err_str = "Unable to allocate netlink request";
@@ -404,6 +409,11 @@ int iwpm_register_pid_cb(struct sk_buff *skb, struct netlink_callback *cb)
 				 __func__, msg_seq);
 		return -EINVAL;
 	}
+	if (nlmsg_request->nl_op != RDMA_NL_IWPM_REG_PID) {
+		pr_info("%s: Request does not expect this reply\n", __func__);
+		kref_put(&nlmsg_request->kref, iwpm_free_nlmsg_request);
+		return -EINVAL;
+	}
 	pm_msg = nlmsg_request->req_buffer;
 	nl_client = nlmsg_request->nl_client;
 	dev_name = (char *)nla_data(nltb[IWPM_NLA_RREG_IBDEV_NAME]);
@@ -478,6 +488,11 @@ int iwpm_add_mapping_cb(struct sk_buff *skb, struct netlink_callback *cb)
 				 __func__, msg_seq);
 		return -EINVAL;
 	}
+	if (nlmsg_request->nl_op != RDMA_NL_IWPM_ADD_MAPPING) {
+		pr_info("%s: Request does not expect this reply\n", __func__);
+		kref_put(&nlmsg_request->kref, iwpm_free_nlmsg_request);
+		return -EINVAL;
+	}
 	pm_msg = nlmsg_request->req_buffer;
 	local_sockaddr = (struct sockaddr_storage *)
 			nla_data(nltb[IWPM_NLA_RMANAGE_ADDR]);
@@ -557,6 +572,11 @@ int iwpm_add_and_query_mapping_cb(struct sk_buff *skb,
 				 __func__, msg_seq);
 		return -EINVAL;
 	}
+	if (nlmsg_request->nl_op != RDMA_NL_IWPM_QUERY_MAPPING) {
+		pr_info("%s: Request does not expect this reply\n", __func__);
+		kref_put(&nlmsg_request->kref, iwpm_free_nlmsg_request);
+		return -EINVAL;
+	}
 	pm_msg = nlmsg_request->req_buffer;
 	local_sockaddr = (struct sockaddr_storage *)
 			nla_data(nltb[IWPM_NLA_RQUERY_LOCAL_ADDR]);
diff --git a/drivers/infiniband/core/iwpm_util.c b/drivers/infiniband/core/iwpm_util.c
index 639ea80373ea6..2b336e1781b33 100644
--- a/drivers/infiniband/core/iwpm_util.c
+++ b/drivers/infiniband/core/iwpm_util.c
@@ -305,7 +305,8 @@ int iwpm_get_remote_info(struct sockaddr_storage *mapped_loc_addr,
 }
 
 struct iwpm_nlmsg_request *iwpm_get_nlmsg_request(__u32 nlmsg_seq,
-					u8 nl_client, gfp_t gfp)
+					u16 nl_op, u8 nl_client,
+					gfp_t gfp)
 {
 	struct iwpm_nlmsg_request *nlmsg_request;
 	unsigned long flags;
@@ -316,6 +317,7 @@ struct iwpm_nlmsg_request *iwpm_get_nlmsg_request(__u32 nlmsg_seq,
 
 	kref_init(&nlmsg_request->kref);
 	nlmsg_request->nlmsg_seq = nlmsg_seq;
+	nlmsg_request->nl_op = nl_op;
 	nlmsg_request->nl_client = nl_client;
 	nlmsg_request->request_done = 0;
 	nlmsg_request->err_code = 0;
diff --git a/drivers/infiniband/core/iwpm_util.h b/drivers/infiniband/core/iwpm_util.h
index d6fc8402158a3..007d232982c84 100644
--- a/drivers/infiniband/core/iwpm_util.h
+++ b/drivers/infiniband/core/iwpm_util.h
@@ -65,6 +65,7 @@ struct iwpm_nlmsg_request {
 	struct list_head    inprocess_list;
 	__u32               nlmsg_seq;
 	void                *req_buffer;
+	u16                 nl_op;
 	u8	            nl_client;
 	u8                  request_done;
 	u16                 err_code;
@@ -96,6 +97,7 @@ struct iwpm_admin_data {
 /**
  * iwpm_get_nlmsg_request - Allocate and initialize netlink message request
  * @nlmsg_seq: Sequence number of the netlink message
+ * @nl_op: The netlink op whose reply this request expects
  * @nl_client: The index of the netlink client
  * @gfp: Indicates how the memory for the request should be allocated
  *
@@ -103,7 +105,8 @@ struct iwpm_admin_data {
  * otherwise returns NULL
  */
 struct iwpm_nlmsg_request *iwpm_get_nlmsg_request(__u32 nlmsg_seq,
-						u8 nl_client, gfp_t gfp);
+						u16 nl_op, u8 nl_client,
+						gfp_t gfp);
 
 /**
  * iwpm_free_nlmsg_request - Deallocate netlink message request

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH v2 3/3] RDMA/iwpm: keep the port-mapper request data inside the request
  2026-12-06  6:57 [PATCH v2 0/3] RDMA/iwpm: fix the port-mapper request lifetime Daniel Wade
  2026-12-06  6:57 ` [PATCH v2 1/3] RDMA/iwpm: fix use-after-free of the netlink request object Daniel Wade
  2026-12-06  6:57 ` [PATCH v2 2/3] RDMA/iwpm: reject a port-mapper reply meant for another request type Daniel Wade
@ 2026-12-06  6:57 ` Daniel Wade
  2026-12-06  7:10   ` sashiko-bot
  2026-12-06  7:12 ` [PATCH v2 0/3] RDMA/iwpm: fix the port-mapper request lifetime sashiko-bot
  3 siblings, 1 reply; 7+ messages in thread
From: Daniel Wade @ 2026-12-06  6:57 UTC (permalink / raw)
  To: Jason Gunthorpe, Leon Romanovsky
  Cc: Jeffin Philip, Shuangpeng Bai, linux-rdma, linux-kernel

iw_cm_map() keeps pm_reg_msg and pm_msg on its stack and publishes their
addresses in req_buffer, where any netlink callback can reach them. The
kref pins the request, not the frame. So after the ten second timeout
the producer returns and unwinds, and a callback that already took a
reference still writes through req_buffer into a frame that has gone.

Move the data into the request itself. The op check added by the previous
patch is what keeps each callback on the member its producer wrote. Copy
the answer back to the caller after the wait returns. The caller now
drops its own reference once it has copied, so iwpm_wait_complete_req()
no longer drops it.

iwpm_register_pid() also set req_buffer only after the multicast, so a
reply that arrived first found it NULL. The data is now in place before
the message goes out.

Fixes: 30dc5e63d6a5 ("RDMA/core: Add support for iWARP Port Mapper user space service")
Signed-off-by: Daniel Wade <danjwade95@gmail.com>
---

Notes:
    v2: new patch. This is the stack use-after-return raised against v1 1/2,
      https://lore.kernel.org/all/sashiko-outbox-251664@kernel.org/
    removed at the source rather than guarded.
    
    Tested on the same kernel with both a success and an error reply to
    ADD_MAPPING, so the copy-out runs under KASAN: twelve stale probes all
    report no matching request, and KASAN stays silent.

 drivers/infiniband/core/iwpm_msg.c  | 19 +++++++++++++------
 drivers/infiniband/core/iwpm_util.c |  1 -
 drivers/infiniband/core/iwpm_util.h |  9 +++++++--
 3 files changed, 20 insertions(+), 9 deletions(-)

diff --git a/drivers/infiniband/core/iwpm_msg.c b/drivers/infiniband/core/iwpm_msg.c
index 1d77349db51e5..ecf7e12674732 100644
--- a/drivers/infiniband/core/iwpm_msg.c
+++ b/drivers/infiniband/core/iwpm_msg.c
@@ -106,6 +106,7 @@ int iwpm_register_pid(struct iwpm_dev_data *pm_msg, u8 nl_client)
 		goto pid_query_error;
 
 	nlmsg_end(skb, nlh);
+	nlmsg_request->data.dev = *pm_msg;
 
 	pr_debug("%s: Multicasting a nlmsg (dev = %s ifname = %s iwpm = %s)\n",
 		__func__, pm_msg->dev_name, pm_msg->if_name, iwpm_ulib_name);
@@ -117,8 +118,8 @@ int iwpm_register_pid(struct iwpm_dev_data *pm_msg, u8 nl_client)
 		err_str = "Unable to send a nlmsg";
 		goto pid_query_error;
 	}
-	nlmsg_request->req_buffer = pm_msg;
 	ret = iwpm_wait_complete_req(nlmsg_request);
+	kref_put(&nlmsg_request->kref, iwpm_free_nlmsg_request);
 	return ret;
 pid_query_error:
 	pr_info("%s: %s (client = %u)\n", __func__, err_str, nl_client);
@@ -195,7 +196,7 @@ int iwpm_add_mapping(struct iwpm_sa_data *pm_msg, u8 nl_client)
 	}
 
 	nlmsg_end(skb, nlh);
-	nlmsg_request->req_buffer = pm_msg;
+	nlmsg_request->data.sa = *pm_msg;
 
 	ret = rdma_nl_unicast_wait(&init_net, skb, iwpm_user_pid);
 	if (ret) {
@@ -205,6 +206,9 @@ int iwpm_add_mapping(struct iwpm_sa_data *pm_msg, u8 nl_client)
 		goto add_mapping_error;
 	}
 	ret = iwpm_wait_complete_req(nlmsg_request);
+	if (!ret)
+		*pm_msg = nlmsg_request->data.sa;
+	kref_put(&nlmsg_request->kref, iwpm_free_nlmsg_request);
 	return ret;
 add_mapping_error:
 	pr_info("%s: %s (client = %u)\n", __func__, err_str, nl_client);
@@ -286,7 +290,7 @@ int iwpm_add_and_query_mapping(struct iwpm_sa_data *pm_msg, u8 nl_client)
 	}
 
 	nlmsg_end(skb, nlh);
-	nlmsg_request->req_buffer = pm_msg;
+	nlmsg_request->data.sa = *pm_msg;
 
 	ret = rdma_nl_unicast_wait(&init_net, skb, iwpm_user_pid);
 	if (ret) {
@@ -295,6 +299,9 @@ int iwpm_add_and_query_mapping(struct iwpm_sa_data *pm_msg, u8 nl_client)
 		goto query_mapping_error;
 	}
 	ret = iwpm_wait_complete_req(nlmsg_request);
+	if (!ret)
+		*pm_msg = nlmsg_request->data.sa;
+	kref_put(&nlmsg_request->kref, iwpm_free_nlmsg_request);
 	return ret;
 query_mapping_error:
 	pr_info("%s: %s (client = %u)\n", __func__, err_str, nl_client);
@@ -414,7 +421,7 @@ int iwpm_register_pid_cb(struct sk_buff *skb, struct netlink_callback *cb)
 		kref_put(&nlmsg_request->kref, iwpm_free_nlmsg_request);
 		return -EINVAL;
 	}
-	pm_msg = nlmsg_request->req_buffer;
+	pm_msg = &nlmsg_request->data.dev;
 	nl_client = nlmsg_request->nl_client;
 	dev_name = (char *)nla_data(nltb[IWPM_NLA_RREG_IBDEV_NAME]);
 	iwpm_name = (char *)nla_data(nltb[IWPM_NLA_RREG_ULIB_NAME]);
@@ -493,7 +500,7 @@ int iwpm_add_mapping_cb(struct sk_buff *skb, struct netlink_callback *cb)
 		kref_put(&nlmsg_request->kref, iwpm_free_nlmsg_request);
 		return -EINVAL;
 	}
-	pm_msg = nlmsg_request->req_buffer;
+	pm_msg = &nlmsg_request->data.sa;
 	local_sockaddr = (struct sockaddr_storage *)
 			nla_data(nltb[IWPM_NLA_RMANAGE_ADDR]);
 	mapped_sockaddr = (struct sockaddr_storage *)
@@ -577,7 +584,7 @@ int iwpm_add_and_query_mapping_cb(struct sk_buff *skb,
 		kref_put(&nlmsg_request->kref, iwpm_free_nlmsg_request);
 		return -EINVAL;
 	}
-	pm_msg = nlmsg_request->req_buffer;
+	pm_msg = &nlmsg_request->data.sa;
 	local_sockaddr = (struct sockaddr_storage *)
 			nla_data(nltb[IWPM_NLA_RQUERY_LOCAL_ADDR]);
 	remote_sockaddr = (struct sockaddr_storage *)
diff --git a/drivers/infiniband/core/iwpm_util.c b/drivers/infiniband/core/iwpm_util.c
index 2b336e1781b33..c0218e93ab7da 100644
--- a/drivers/infiniband/core/iwpm_util.c
+++ b/drivers/infiniband/core/iwpm_util.c
@@ -379,7 +379,6 @@ int iwpm_wait_complete_req(struct iwpm_nlmsg_request *nlmsg_request)
 	} else {
 		ret = nlmsg_request->err_code;
 	}
-	kref_put(&nlmsg_request->kref, iwpm_free_nlmsg_request);
 	return ret;
 }
 
diff --git a/drivers/infiniband/core/iwpm_util.h b/drivers/infiniband/core/iwpm_util.h
index 007d232982c84..860f953f47c55 100644
--- a/drivers/infiniband/core/iwpm_util.h
+++ b/drivers/infiniband/core/iwpm_util.h
@@ -64,11 +64,14 @@
 struct iwpm_nlmsg_request {
 	struct list_head    inprocess_list;
 	__u32               nlmsg_seq;
-	void                *req_buffer;
 	u16                 nl_op;
 	u8	            nl_client;
 	u8                  request_done;
 	u16                 err_code;
+	union {
+		struct iwpm_dev_data dev;
+		struct iwpm_sa_data  sa;
+	} data;
 	struct semaphore    sem;
 	struct kref         kref;
 };
@@ -127,7 +130,9 @@ struct iwpm_nlmsg_request *iwpm_find_nlmsg_request(__u32 echo_seq);
  * iwpm_wait_complete_req - Block while servicing the netlink request
  * @nlmsg_request: Netlink message request to service
  *
- * Wakes up, after the request is completed or expired
+ * Wakes up, after the request is completed or expired.
+ * The caller keeps its reference, so nlmsg_request->data stays
+ * readable until the caller drops it.
  * Returns 0 if the request is complete without error
  */
 int iwpm_wait_complete_req(struct iwpm_nlmsg_request *nlmsg_request);

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH v2 3/3] RDMA/iwpm: keep the port-mapper request data inside the request
  2026-12-06  6:57 ` [PATCH v2 3/3] RDMA/iwpm: keep the port-mapper request data inside the request Daniel Wade
@ 2026-12-06  7:10   ` sashiko-bot
  2026-12-06  7:41     ` Daniel Wade
  0 siblings, 1 reply; 7+ messages in thread
From: sashiko-bot @ 2026-12-06  7:10 UTC (permalink / raw)
  To: Daniel Wade; +Cc: linux-rdma

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] RDMA/iwpm: data race on nlmsg_request->data.sa during
  post-wait copy-out
- [Medium] RDMA/iwpm: nlmsg_request published before nlmsg_request->data
  is initialized
- [Low] RDMA/iwpm: whole-struct copy reads uninitialized stack fields
  from pm_msg
--

commit 5384e5afb7f8b54dc9f77b2c25ecb5a44fd3f7e4
Author: Daniel Wade <danjwade95@gmail.com>

RDMA/iwpm: keep the port-mapper request data inside the request

This commit embeds the port-mapper request data inside
struct iwpm_nlmsg_request instead of storing a pointer to the caller's
stack buffer in req_buffer, and moves the caller's kref_put() out of
iwpm_wait_complete_req() so the response can be copied back before the
request is freed.

> diff --git a/drivers/infiniband/core/iwpm_msg.c b/drivers/infiniband/core/iwpm_msg.c
> index 1d77349db51e5..ecf7e12674732 100644
> --- a/drivers/infiniband/core/iwpm_msg.c
> +++ b/drivers/infiniband/core/iwpm_msg.c
> @@ -106,6 +106,7 @@ int iwpm_register_pid(struct iwpm_dev_data *pm_msg, u8 nl_client)
>  		goto pid_query_error;
>  
>  	nlmsg_end(skb, nlh);
> +	nlmsg_request->data.dev = *pm_msg;

[Severity: Medium]
Can an early netlink reply race with this initialization of
nlmsg_request->data?

CPU0                                 CPU1
iwpm_register_pid()
  iwpm_get_nlmsg_request()
    list_add_tail(..., &iwpm_nlmsg_req_list)
                                     iwpm_register_pid_cb()
                                       iwpm_find_nlmsg_request()
                                       strcmp(pm_msg->dev_name, dev_name)
                                         <-- reads all-zero dev_name
  nlmsg_request->data.dev = *pm_msg

iwpm_get_nlmsg_request() publishes the zero-allocated request on
iwpm_nlmsg_req_list before the caller populates nlmsg_request->data.
A privileged netlink reply with the next sequence number can match an
empty dev_name and register iwpm_user_pid without verifying the device.

The same window exists in iwpm_add_mapping() and
iwpm_add_and_query_mapping(), where a concurrent callback's write to
mapped_loc_addr can be overwritten by nlmsg_request->data.sa = *pm_msg.

Populating nlmsg_request->data before inserting the request onto
iwpm_nlmsg_req_list would close this window.

[ ... ]

> @@ -195,7 +196,7 @@ int iwpm_add_mapping(struct iwpm_sa_data *pm_msg, u8 nl_client)
>  	}
>  
>  	nlmsg_end(skb, nlh);
> -	nlmsg_request->req_buffer = pm_msg;
> +	nlmsg_request->data.sa = *pm_msg;

[Severity: Low]
Does this whole-struct assignment copy uninitialized stack bytes from
pm_msg into nlmsg_request->data.sa?

iw_cm_map() leaves mapped_loc_addr and mapped_rem_addr uninitialized in
its stack pm_msg.  Assigning *pm_msg here and in
iwpm_add_and_query_mapping() clobbers the zeroed heap fields with
uninitialized stack bytes.

Any completion path that skips writing those fields will then propagate
uninitialized stack memory instead of zeroes, and iwpm_add_mapping()
always copies the unwritten mapped_rem_addr back out.

Copying only the input fields in and output fields out, or
zero-initializing pm_msg in iw_cm_map(), would avoid this.

> @@ -205,6 +206,9 @@ int iwpm_add_mapping(struct iwpm_sa_data *pm_msg, u8 nl_client)
>  		goto add_mapping_error;
>  	}
>  	ret = iwpm_wait_complete_req(nlmsg_request);
> +	if (!ret)
> +		*pm_msg = nlmsg_request->data.sa;
> +	kref_put(&nlmsg_request->kref, iwpm_free_nlmsg_request);
>  	return ret;

[Severity: Medium]
Can a second netlink reply race with this copy of nlmsg_request->data.sa?

CPU0                                 CPU1
iwpm_add_mapping()
  iwpm_wait_complete_req()
    ...                              iwpm_add_mapping_cb() [reply 1]
                                       memcpy(&pm_msg->mapped_loc_addr, ...)
                                       up(&nlmsg_request->sem)
                                       kref_put()  /* kref 2 -> 1 */
  *pm_msg = nlmsg_request->data.sa   iwpm_add_mapping_cb() [reply 2]
    <-- data race / torn read          iwpm_find_nlmsg_request()
                                       memcpy(&pm_msg->mapped_loc_addr, ...)
  kref_put()  /* unlinks here */

nlmsg_request is only unlinked from iwpm_nlmsg_req_list inside
iwpm_free_nlmsg_request() when kref reaches zero, and
iwpm_find_nlmsg_request() neither unlinks it nor checks request_done.

Because rdma_nl_rcv_msg() takes a shared read lock on
rdma_nl_types[index].sem, a second privileged reply on another netlink
socket can find the request and overwrite data.sa (or err_code) while
this function or iwpm_add_and_query_mapping() copies it out.

This causes a data race and can produce torn mapped_loc_addr or
mapped_rem_addr values.  Unlinking or claiming the request under
iwpm_nlmsg_req_lock on the first reply or timeout would give the caller
exclusive access during copy-out.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261206065723.147-1-danjwade95@gmail.com?part=3

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v2 0/3] RDMA/iwpm: fix the port-mapper request lifetime
  2026-12-06  6:57 [PATCH v2 0/3] RDMA/iwpm: fix the port-mapper request lifetime Daniel Wade
                   ` (2 preceding siblings ...)
  2026-12-06  6:57 ` [PATCH v2 3/3] RDMA/iwpm: keep the port-mapper request data inside the request Daniel Wade
@ 2026-12-06  7:12 ` sashiko-bot
  3 siblings, 0 replies; 7+ messages in thread
From: sashiko-bot @ 2026-12-06  7:12 UTC (permalink / raw)
  To: Daniel Wade; +Cc: linux-rdma

Sashiko has finished reviewing this 3-patch series:

- Patches 1 and 2: no issues found.
- Patch 3 ("RDMA/iwpm: keep the port-mapper request data inside..."): 3
  potential issues (2 Medium, 1 Low)

A detailed review has been sent as a separate reply to patch 3.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261206065723.147-1-danjwade95@gmail.com

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v2 3/3] RDMA/iwpm: keep the port-mapper request data inside the request
  2026-12-06  7:10   ` sashiko-bot
@ 2026-12-06  7:41     ` Daniel Wade
  0 siblings, 0 replies; 7+ messages in thread
From: Daniel Wade @ 2026-12-06  7:41 UTC (permalink / raw)
  To: Jason Gunthorpe, Leon Romanovsky
  Cc: Jeffin Philip, Shuangpeng Bai, linux-rdma, linux-kernel

On Sun, Dec 06, 2026 at 07:10:45AM +0000, sashiko-bot@kernel.org wrote:
> - [Medium] RDMA/iwpm: data race on nlmsg_request->data.sa during
>   post-wait copy-out
> - [Medium] RDMA/iwpm: nlmsg_request published before nlmsg_request->data
>   is initialized
> - [Low] RDMA/iwpm: whole-struct copy reads uninitialized stack fields
>   from pm_msg

The clanker strikes again. All three look valid, will fix in v3 once
others have had a chance to review.

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2026-12-06  7:42 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-06  6:57 [PATCH v2 0/3] RDMA/iwpm: fix the port-mapper request lifetime Daniel Wade
2026-12-06  6:57 ` [PATCH v2 1/3] RDMA/iwpm: fix use-after-free of the netlink request object Daniel Wade
2026-12-06  6:57 ` [PATCH v2 2/3] RDMA/iwpm: reject a port-mapper reply meant for another request type Daniel Wade
2026-12-06  6:57 ` [PATCH v2 3/3] RDMA/iwpm: keep the port-mapper request data inside the request Daniel Wade
2026-12-06  7:10   ` sashiko-bot
2026-12-06  7:41     ` Daniel Wade
2026-12-06  7:12 ` [PATCH v2 0/3] RDMA/iwpm: fix the port-mapper request lifetime sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox