All of lore.kernel.org
 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 1/2] RDMA/iwpm: fix use-after-free of the netlink request object
Date: Sun,  6 Dec 2026 01:11:10 +1030	[thread overview]
Message-ID: <20261205144111.8366-1-danjwade95@gmail.com> (raw)

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

             reply	other threads:[~2026-12-05 14:41 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-05 14:41 Daniel Wade [this message]
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

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=20261205144111.8366-1-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 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.