ipc: fix to protect IPCS lookups using RCU instead of semaphore
PATCH 1/1 ipc: fix to protect IPCS lookups using RCU instead of semaphore
In shm_destroy_orphaned(), we are not performing updates to the IPCS and are only calling idr_for_each(), which can be protected by the RCU read-critical section.
And if idr_for_each() is not protected by the RCU read-critical section, then when radix_tree_node_free() is called to free the struct radix_tree_node through call_rcu(), the node will be freed immediately, and when reading the next node in radix_tree_for_each_slot(), the memory that has already been freed may be read.
Therefore, I think it is appropriate to use RCU instead of semaphore to protect it.
--- ipc/shm.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
ipc/shm.c +2 -2
@@ -430,10 +430,10 @@ 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); +rcu_read_lock ();if (shm_ids (ns).in_use)idr_for_each (&shm_ids (ns).ipcs_idr, &shm_try_destroy_orphaned, ns); -up_write (&shm_ids (ns).rwsem); +rcu_read_unlock (); }/* Locking assumes this will only be called with task == current */
--
Quoted 10 lines · Jeongjun Park
In
shm_destroy_orphaned(), we are not performing updates to the IPCS and are only callingidr_for_each(), which can be protected by the RCU read-critical section.And if
idr_for_each()is not protected by the RCU read-critical section, then whenradix_tree_node_free()is called to free the structradix_tree_nodethroughcall_rcu(), the node will be freed immediately, and when reading the next node inradix_tree_for_each_slot(), the memory that has already been freed may be read.
Quoted 4 lines
Therefore, I think it is appropriate to use RCU instead of semaphore to protect it.
Fixes d9a605e40b13 ("ipc: rename ids->rw_mutex")
This does not fix that commit. That's just the last commit that touches this line. Indeed, it doesn't fix anything.
You need to justify why making this change is desirable. Does it speed anything up?
Quoted 23 lines
Signed-off-by Jeongjun Park <[email protected]>--- ipc/shm.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)ipc/shm.c +2
-3@@ -430,10 +430,10 @@ 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); +rcu_read_lock ();if (shm_ids (ns).in_use)idr_for_each (&shm_ids (ns).ipcs_idr, &shm_try_destroy_orphaned, ns); -up_write (&shm_ids (ns).rwsem); +rcu_read_unlock (); }/* Locking assumes this will only be called with task == current */ --
Quoted 9 lines · On Sat, 15 Feb 2025 03:01:57 +0900 Jeongjun Park <[email protected]> wrote:
In
shm_destroy_orphaned(), we are not performing updates to the IPCS and are only callingidr_for_each(), which can be protected by the RCU read-critical section.And if
idr_for_each()is not protected by the RCU read-critical section, then whenradix_tree_node_free()is called to free the structradix_tree_nodethroughcall_rcu(), the node will be freed immediately, and when reading the next node inradix_tree_for_each_slot(), the memory that has already been freed may be read.
A use-after-free?
Is there any report of this occurring, or was this change a result of code inspection? If the former, please share details (Link:, Reported-by:, Closes:, etc).
Quoted 2 lines
Therefore, I think it is appropriate to use RCU instead of semaphore to protect it.
Quoted 13 lines · Andrew Morton
Quoted 9 lines · On Sat, 15 Feb 2025 03:01:57 +0900 Jeongjun Park <[email protected]> wrote:
In
shm_destroy_orphaned(), we are not performing updates to the IPCS and are only callingidr_for_each(), which can be protected by the RCU read-critical section.And if
idr_for_each()is not protected by the RCU read-critical section, then whenradix_tree_node_free()is called to free the structradix_tree_nodethroughcall_rcu(), the node will be freed immediately, and when reading the next node inradix_tree_for_each_slot(), the memory that has already been freed may be read.
Quoted 5 lines
A use-after-free?
Is there any report of this occurring, or was this change a result of code inspection? If the former, please share details (Link:, Reported-by:, Closes:, etc).
Sorry I forgot the Reported-by tag. I think the vulnerability is caused by misusing RCU. In addition, since it is a function that does not perform an update operation, it is possible to protect it through RCU, so we can safely get some performance small benefits by using RCU instead of semaphore.
Regards,
Jeongjun Park
Quoted 4 lines
Quoted 3 lines
Therefore, I think it is appropriate to use RCU instead of semaphore to protect it.
[Fixing Davidlohr's email address -- David could you pop an entry into .mailmap?]
Quoted 14 lines · Jeongjun Park
Quoted 13 lines · Andrew Morton
Quoted 9 lines · On Sat, 15 Feb 2025 03:01:57 +0900 Jeongjun Park <[email protected]> wrote:
In
shm_destroy_orphaned(), we are not performing updates to the IPCS and are only callingidr_for_each(), which can be protected by the RCU read-critical section.And if
idr_for_each()is not protected by the RCU read-critical section, then whenradix_tree_node_free()is called to free the structradix_tree_nodethroughcall_rcu(), the node will be freed immediately, and when reading the next node inradix_tree_for_each_slot(), the memory that has already been freed may be read.
Quoted 7 lines
Quoted 5 lines
A use-after-free?
Is there any report of this occurring, or was this change a result of code inspection? If the former, please share details (Link:, Reported-by:, Closes:, etc).
Reported-by [email protected]
For anyone else trying to understand what the hell this is about, the report is at:
https://lore.kernel.org/linux-kernel/[email protected]/
Quoted 5 lines
Sorry I forgot the Reported-by tag. I think the vulnerability is caused by misusing RCU. In addition, since it is a function that does not perform an update operation, it is possible to protect it through RCU, so we can safely get some performance small benefits by using RCU instead of semaphore.
shm uses RCU in a very weird way. You're absolutely right that we need to hold the RCU read lock here to prevent the tree nodes from being freed below us.
But I'm not convinced that removing the rwsem is safe. Look at what else is in the call chain:
if (shm_ids(ns).in_use)
idr_for_each(&shm_ids(ns).ipcs_idr, &shm_try_destroy_orphaned, ns);shm_try_destroy_orphaned:
shm_destroy(ns, shp);
shm_destroy:
shm_rmid(shp);
shm_rmid:
ipc_rmid(&shm_ids(s->ns), &s->shm_perm);
ipc_rmid:
WARN_ON_ONCE(idr_remove(&ids->ipcs_idr, idx) != ipcp);So what's protecting that idr_remove() if not the rwsem?
(the shortcut for this is that shm_destroy() actually documents that it needs shm_ids.rwsem held. But it doesn't have a lockdep assertion. Perhaps you could add one?)
Quoted 57 lines · Matthew Wilcox
[Fixing Davidlohr's email address -- David could you pop an entry into .mailmap?]
Quoted 14 lines · Jeongjun Park
Quoted 13 lines · Andrew Morton
Quoted 9 lines · On Sat, 15 Feb 2025 03:01:57 +0900 Jeongjun Park <[email protected]> wrote:
In
shm_destroy_orphaned(), we are not performing updates to the IPCS and are only callingidr_for_each(), which can be protected by the RCU read-critical section.And if
idr_for_each()is not protected by the RCU read-critical section, then whenradix_tree_node_free()is called to free the structradix_tree_nodethroughcall_rcu(), the node will be freed immediately, and when reading the next node inradix_tree_for_each_slot(), the memory that has already been freed may be read.Quoted 7 lines
Quoted 5 lines
A use-after-free?
Is there any report of this occurring, or was this change a result of code inspection? If the former, please share details (Link:, Reported-by:, Closes:, etc).
Reported-by [email protected]For anyone else trying to understand what the hell this is about, the report is at:
https://lore.kernel.org/linux-kernel/[email protected]/
Quoted 5 lines
Sorry I forgot the Reported-by tag. I think the vulnerability is caused by misusing RCU. In addition, since it is a function that does not perform an update operation, it is possible to protect it through RCU, so we can safely get some performance small benefits by using RCU instead of semaphore.
shm uses RCU in a very weird way. You're absolutely right that we need to hold the RCU read lock here to prevent the tree nodes from being freed below us.
But I'm not convinced that removing the rwsem is safe. Look at what else is in the call chain:
if (shm_ids(ns).in_use) idr_for_each(&shm_ids(ns).ipcs_idr, &shm_try_destroy_orphaned, ns);
shm_try_destroy_orphaned:shm_destroy(ns, shp); shm_destroy: shm_rmid(shp); shm_rmid: ipc_rmid(&shm_ids(s->ns), &s->shm_perm); ipc_rmid: WARN_ON_ONCE(idr_remove(&ids->ipcs_idr, idx) != ipcp);
Quoted 1 line
So what's protecting that
idr_remove()if not the rwsem?
After looking into it again, I guess removing the semaphore won't work. Thanks for letting me know!
I'll fix it and send you a patch right away.
Regards,
Jeongjun Park
Quoted 4 lines
(the shortcut for this is that
shm_destroy()actually documents that it needs shm_ids.rwsem held. But it doesn't have a lockdep assertion. Perhaps you could add one?)