mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Daniel Wade <danjwade95@gmail.com>
To: Jason Gunthorpe <jgg@ziepe.ca>, Leon Romanovsky <leon@kernel.org>
Cc: Jeffin Philip <jeffinphilip14@gmail.com>,
	Shuangpeng Bai <shuangpeng.kernel@gmail.com>,
	linux-rdma@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH v2 3/3] RDMA/iwpm: keep the port-mapper request data inside the request
Date: Sun,  6 Dec 2026 17:27:23 +1030	[thread overview]
Message-ID: <20261206065723.147-4-danjwade95@gmail.com> (raw)
In-Reply-To: <20261206065723.147-1-danjwade95@gmail.com>

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);

  parent reply	other threads:[~2026-12-06  6:57 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
     [not found]   ` <sashiko-outbox-252235@kernel.org>
2026-12-06  7:41     ` [PATCH v2 3/3] RDMA/iwpm: keep the port-mapper request data inside the request Daniel Wade

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261206065723.147-4-danjwade95@gmail.com \
    --to=danjwade95@gmail.com \
    --cc=jeffinphilip14@gmail.com \
    --cc=jgg@ziepe.ca \
    --cc=leon@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-rdma@vger.kernel.org \
    --cc=shuangpeng.kernel@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®