All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/2] RDMA/iwpm: fix use-after-free of the netlink request object
@ 2026-12-05 14:41 Daniel Wade
  2026-12-05 14:41 ` [PATCH 2/2] RDMA/iwpm: reject a port-mapper reply meant for another request type Daniel Wade
  2026-12-05 14:55 ` [PATCH 1/2] RDMA/iwpm: fix use-after-free of the netlink request object sashiko-bot
  0 siblings, 2 replies; 4+ messages in thread
From: Daniel Wade @ 2026-12-05 14:41 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:
    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] 4+ messages in thread

* [PATCH 2/2] RDMA/iwpm: reject a port-mapper reply meant for another request type
  2026-12-05 14:41 [PATCH 1/2] RDMA/iwpm: fix use-after-free of the netlink request object Daniel Wade
@ 2026-12-05 14:41 ` Daniel Wade
  2026-12-05 14:55   ` sashiko-bot
  2026-12-05 14:55 ` [PATCH 1/2] RDMA/iwpm: fix use-after-free of the netlink request object sashiko-bot
  1 sibling, 1 reply; 4+ messages in thread
From: Daniel Wade @ 2026-12-05 14:41 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 516-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:
    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. The 1/2 test is unchanged by this patch.

 drivers/infiniband/core/iwpm_msg.c  | 24 ++++++++++++++++++++++--
 drivers/infiniband/core/iwpm_util.c |  4 +++-
 drivers/infiniband/core/iwpm_util.h |  4 +++-
 3 files changed, 28 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..d612d0fc2c173 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;
@@ -103,7 +104,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] 4+ messages in thread

* Re: [PATCH 1/2] RDMA/iwpm: fix use-after-free of the netlink request object
  2026-12-05 14:41 [PATCH 1/2] RDMA/iwpm: fix use-after-free of the netlink request object Daniel Wade
  2026-12-05 14:41 ` [PATCH 2/2] RDMA/iwpm: reject a port-mapper reply meant for another request type Daniel Wade
@ 2026-12-05 14:55 ` sashiko-bot
  1 sibling, 0 replies; 4+ messages in thread
From: sashiko-bot @ 2026-12-05 14:55 UTC (permalink / raw)
  To: Daniel Wade; +Cc: linux-rdma

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] RDMA/iwpm: stack use-after-return via req_buffer in netlink
  callbacks
--

commit 77177fe8d60c7890fdb4f0b44b159bac9ca369db
Author: Daniel Wade <danjwade95@gmail.com>

RDMA/iwpm: fix use-after-free of the netlink request object

This commit changes iwpm_nlmsg_request lifetime management to take a single
initial kref, look up requests with kref_get_unless_zero(), drop references
with kref_put() on producer error paths, and call up() before kref_put() in
the netlink callbacks.

> diff --git a/drivers/infiniband/core/iwpm_util.c b/drivers/infiniband/core/iwpm_util.c
> index 215f70cfd1132..fff2a2b6bca39 100644
> --- a/drivers/infiniband/core/iwpm_util.c
> +++ b/drivers/infiniband/core/iwpm_util.c

[ ... ]

> @@ -360,8 +359,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;
>  		}
>  	}

[Severity: High]
Can netlink callbacks still read and write through nlmsg_request->req_buffer
after iw_cm_map() returns and unwinds its stack frame?

iw_cm_map() allocates pm_reg_msg and pm_msg on its stack and stores their
addresses in nlmsg_request->req_buffer.  The kref only pins nlmsg_request,
not the caller's stack frame, and iwpm_wait_complete_req() returns
immediately after kref_put() without waiting for in-flight callbacks:

CPU0                              CPU1
iw_cm_map()
  iwpm_add_mapping(&pm_msg, ...)
    iwpm_wait_complete_req()
      down_timeout() /* expires */
                                  iwpm_add_mapping_cb()
                                    iwpm_find_nlmsg_request()
                                      kref_get_unless_zero() /* 1 -> 2 */
      kref_put() /* 2 -> 1 */
  return /* pm_msg popped */
                                    memcpy(&pm_msg->mapped_loc_addr, ...)
                                      <-- stack use-after-return

Because iwpm_find_nlmsg_request() leaves nlmsg_request linked on
iwpm_nlmsg_req_list and rdma_nl_rcv_msg() only holds a shared down_read(),
moving up(&nlmsg_request->sem) ahead of kref_put() in the callbacks also lets
the waiter unwind its stack while a second callback can still look up the
request via kref_get_unless_zero().  The same window exists on the producer
error paths in iwpm_register_pid(), iwpm_add_mapping(), and
iwpm_add_and_query_mapping() when kref_put() returns while a racing callback
holds a reference.

In iwpm_add_mapping_cb() and iwpm_add_and_query_mapping_cb(), this overwrites
up to 256 bytes of a dead or reused kernel stack frame via memcpy().

Unlinking nlmsg_request on lookup under iwpm_nlmsg_req_lock and embedding the
request/response data inside the kref-managed struct iwpm_nlmsg_request
(copying it back to the caller only on success) would avoid exposing stack
pointers to callbacks.

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

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

* Re: [PATCH 2/2] RDMA/iwpm: reject a port-mapper reply meant for another request type
  2026-12-05 14:41 ` [PATCH 2/2] RDMA/iwpm: reject a port-mapper reply meant for another request type Daniel Wade
@ 2026-12-05 14:55   ` sashiko-bot
  0 siblings, 0 replies; 4+ messages in thread
From: sashiko-bot @ 2026-12-05 14:55 UTC (permalink / raw)
  To: Daniel Wade; +Cc: linux-rdma

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] RDMA/iwpm: missing kernel-doc @nl_op entry for
  iwpm_get_nlmsg_request()
--

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

RDMA/iwpm: reject a port-mapper reply meant for another request type

This commit records the expected netlink op on each iWARP port-mapper
request and rejects replies whose op does not match.

> diff --git a/drivers/infiniband/core/iwpm_util.h b/drivers/infiniband/core/iwpm_util.h
> index d6fc8402158a3..d612d0fc2c173 100644
> --- a/drivers/infiniband/core/iwpm_util.h
> +++ b/drivers/infiniband/core/iwpm_util.h

[ ... ]

> @@ -103,7 +104,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);

[Severity: Low]
Should the kernel-doc comment for iwpm_get_nlmsg_request() also document
the new @nl_op parameter?  Documentation/driver-api/infiniband.rst
includes drivers/infiniband/core/iwpm_util.h via kernel-doc, so omitting
@nl_op introduces a kernel-doc warning on documentation and W=1 builds.

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

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

end of thread, other threads:[~2026-12-05 14:55 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-05 14:41 [PATCH 1/2] RDMA/iwpm: fix use-after-free of the netlink request object Daniel Wade
2026-12-05 14:41 ` [PATCH 2/2] RDMA/iwpm: reject a port-mapper reply meant for another request type Daniel Wade
2026-12-05 14:55   ` sashiko-bot
2026-12-05 14:55 ` [PATCH 1/2] RDMA/iwpm: fix use-after-free of the netlink request object sashiko-bot

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.