The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Jeongjun Park <aha310510@gmail.com>
To: Liam.Howlett@Oracle.com, akpm@linux-foundation.org
Cc: lorenzo.stoakes@oracle.com, willy@infradead.org,
	linux-kernel@vger.kernel.org,
	syzbot+a2b84e569d06ca3a949c@syzkaller.appspotmail.com,
	Jeongjun Park <aha310510@gmail.com>
Subject: [PATCH v2] ipc: fix to protect IPCS lookups using RCU
Date: Tue, 22 Apr 2025 21:48:43 +0900	[thread overview]
Message-ID: <20250422124843.17188-1-aha310510@gmail.com> (raw)

idr_for_each() is protected by rwsem, but this is not enough. If it is not
protected by the RCU read-critical region, when we call radix_tree_node_free()
via call_rcu() to free the struct radix_tree_node, the node will be freed
immediately, and when we read the next node in radix_tree_for_each_slot(),
we can read the already freed memory.

Therefore, we need to add code to make sure that idr_for_each() is protected
within the RCU read-critical region when we call it in shm_destroy_orphaned().

Reported-by: syzbot+a2b84e569d06ca3a949c@syzkaller.appspotmail.com
Fixes: b34a6b1da371 ("ipc: introduce shm_rmid_forced sysctl")
Cc: Matthew Wilcox <willy@infradead.org>
Signed-off-by: Jeongjun Park <aha310510@gmail.com>
---
v2: Change description and coding style
---
 ipc/shm.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/ipc/shm.c b/ipc/shm.c
index 99564c870084..492fcc699985 100644
--- a/ipc/shm.c
+++ b/ipc/shm.c
@@ -431,8 +431,11 @@ static int shm_try_destroy_orphaned(int id, void *p, void *data)
 void shm_destroy_orphaned(struct ipc_namespace *ns)
 {
 	down_write(&shm_ids(ns).rwsem);
-	if (shm_ids(ns).in_use)
+	if (shm_ids(ns).in_use) {
+		rcu_read_lock();
 		idr_for_each(&shm_ids(ns).ipcs_idr, &shm_try_destroy_orphaned, ns);
+		rcu_read_unlock();
+	}
 	up_write(&shm_ids(ns).rwsem);
 }
 
--

             reply	other threads:[~2025-04-22 12:48 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-04-22 12:48 Jeongjun Park [this message]
2025-04-22 17:55 ` [PATCH v2] ipc: fix to protect IPCS lookups using RCU Lorenzo Stoakes
2025-04-24  6:36   ` Jeongjun Park

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=20250422124843.17188-1-aha310510@gmail.com \
    --to=aha310510@gmail.com \
    --cc=Liam.Howlett@Oracle.com \
    --cc=akpm@linux-foundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lorenzo.stoakes@oracle.com \
    --cc=syzbot+a2b84e569d06ca3a949c@syzkaller.appspotmail.com \
    --cc=willy@infradead.org \
    /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