mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v3] ipc: fix to protect IPCS lookups using RCU
@ 2025-04-24 14:33 Jeongjun Park
  0 siblings, 0 replies; only message in thread
From: Jeongjun Park @ 2025-04-24 14:33 UTC (permalink / raw)
  To: akpm, Liam.Howlett
  Cc: lorenzo.stoakes, willy, linux-kernel,
	syzbot+a2b84e569d06ca3a949c, Jeongjun Park

syzbot reported that it discovered a use-after-free vulnerability, [0]

[0]: https://lore.kernel.org/all/67af13f8.050a0220.21dd3.0038.GAE@google.com/

idr_for_each() is protected by rwsem, but this is not enough. If it is not
protected by RCU read-critical region, when idr_for_each() calls
radix_tree_node_free() through call_rcu() to free the radix_tree_node
structure, the node will be freed immediately, and when reading the next
node in radix_tree_for_each_slot(), the already freed memory may be read.

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")
Signed-off-by: Jeongjun Park <aha310510@gmail.com>
---
v3: Add necessary descriptions and links as advised by Lorenzo Stoakes
- Link to v2: https://lore.kernel.org/all/20250422124843.17188-1-aha310510@gmail.com/

v2: Change to use RCU read critical section only when it is certain that idr_for_each() is called
- Link to v1: https://lore.kernel.org/all/20250219132905.8214-1-aha310510@gmail.com/
---
 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);
 }
 
--

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2025-04-24 14:33 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-04-24 14:33 [PATCH v3] ipc: fix to protect IPCS lookups using RCU Jeongjun Park

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®