* [PATCH] ipc: fix to protect IPCS lookups using RCU instead of semaphore
@ 2025-02-14 18:01 Jeongjun Park
2025-02-14 18:58 ` Matthew Wilcox
2025-02-14 23:21 ` Andrew Morton
0 siblings, 2 replies; 6+ messages in thread
From: Jeongjun Park @ 2025-02-14 18:01 UTC (permalink / raw)
To: akpm
Cc: Liam.Howlett, brauner, lorenzo.stoakes, willy, davidlohr.bueso,
linux-kernel, Jeongjun Park
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.
Fixes: d9a605e40b13 ("ipc: rename ids->rw_mutex")
Signed-off-by: Jeongjun Park <aha310510@gmail.com>
---
ipc/shm.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/ipc/shm.c b/ipc/shm.c
index 99564c870084..8fd87c4e5dc8 100644
--- a/ipc/shm.c
+++ b/ipc/shm.c
@@ -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 */
--
^ permalink raw reply related [flat|nested] 6+ messages in thread* Re: [PATCH] ipc: fix to protect IPCS lookups using RCU instead of semaphore
2025-02-14 18:01 [PATCH] ipc: fix to protect IPCS lookups using RCU instead of semaphore Jeongjun Park
@ 2025-02-14 18:58 ` Matthew Wilcox
2025-02-14 23:21 ` Andrew Morton
1 sibling, 0 replies; 6+ messages in thread
From: Matthew Wilcox @ 2025-02-14 18:58 UTC (permalink / raw)
To: Jeongjun Park
Cc: akpm, Liam.Howlett, brauner, lorenzo.stoakes, davidlohr.bueso,
linux-kernel
On Sat, Feb 15, 2025 at 03:01:57AM +0900, Jeongjun Park wrote:
> 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.
>
> 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?
> Signed-off-by: Jeongjun Park <aha310510@gmail.com>
> ---
> ipc/shm.c | 4 ++--
> 1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/ipc/shm.c b/ipc/shm.c
> index 99564c870084..8fd87c4e5dc8 100644
> --- a/ipc/shm.c
> +++ b/ipc/shm.c
> @@ -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 */
> --
^ permalink raw reply [flat|nested] 6+ messages in thread* Re: [PATCH] ipc: fix to protect IPCS lookups using RCU instead of semaphore
2025-02-14 18:01 [PATCH] ipc: fix to protect IPCS lookups using RCU instead of semaphore Jeongjun Park
2025-02-14 18:58 ` Matthew Wilcox
@ 2025-02-14 23:21 ` Andrew Morton
2025-02-15 5:00 ` Jeongjun Park
1 sibling, 1 reply; 6+ messages in thread
From: Andrew Morton @ 2025-02-14 23:21 UTC (permalink / raw)
To: Jeongjun Park
Cc: Liam.Howlett, brauner, lorenzo.stoakes, willy, davidlohr.bueso,
linux-kernel
On Sat, 15 Feb 2025 03:01:57 +0900 Jeongjun Park <aha310510@gmail.com> wrote:
> 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.
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).
> Therefore, I think it is appropriate to use RCU instead of semaphore to
> protect it.
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] ipc: fix to protect IPCS lookups using RCU instead of semaphore
2025-02-14 23:21 ` Andrew Morton
@ 2025-02-15 5:00 ` Jeongjun Park
2025-02-15 17:39 ` Matthew Wilcox
0 siblings, 1 reply; 6+ messages in thread
From: Jeongjun Park @ 2025-02-15 5:00 UTC (permalink / raw)
To: Andrew Morton
Cc: Liam.Howlett, brauner, lorenzo.stoakes, willy, davidlohr.bueso,
linux-kernel
Andrew Morton <akpm@linux-foundation.org> wrote:
>
> On Sat, 15 Feb 2025 03:01:57 +0900 Jeongjun Park <aha310510@gmail.com> wrote:
>
> > 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.
>
> 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: syzbot+a2b84e569d06ca3a949c@syzkaller.appspotmail.com
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
>
> > Therefore, I think it is appropriate to use RCU instead of semaphore to
> > protect it.
>
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] ipc: fix to protect IPCS lookups using RCU instead of semaphore
2025-02-15 5:00 ` Jeongjun Park
@ 2025-02-15 17:39 ` Matthew Wilcox
2025-02-19 10:07 ` Jeongjun Park
0 siblings, 1 reply; 6+ messages in thread
From: Matthew Wilcox @ 2025-02-15 17:39 UTC (permalink / raw)
To: Jeongjun Park
Cc: Andrew Morton, Liam.Howlett, brauner, lorenzo.stoakes,
Davidlohr Bueso, linux-kernel
[Fixing Davidlohr's email address -- David could you pop an entry into
.mailmap?]
On Sat, Feb 15, 2025 at 02:00:23PM +0900, Jeongjun Park wrote:
> Andrew Morton <akpm@linux-foundation.org> wrote:
> >
> > On Sat, 15 Feb 2025 03:01:57 +0900 Jeongjun Park <aha310510@gmail.com> wrote:
> >
> > > 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.
> >
> > 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: syzbot+a2b84e569d06ca3a949c@syzkaller.appspotmail.com
For anyone else trying to understand what the hell this is about,
the report is at:
https://lore.kernel.org/linux-kernel/67af13f8.050a0220.21dd3.0038.GAE@google.com/
> 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?)
^ permalink raw reply [flat|nested] 6+ messages in thread* Re: [PATCH] ipc: fix to protect IPCS lookups using RCU instead of semaphore
2025-02-15 17:39 ` Matthew Wilcox
@ 2025-02-19 10:07 ` Jeongjun Park
0 siblings, 0 replies; 6+ messages in thread
From: Jeongjun Park @ 2025-02-19 10:07 UTC (permalink / raw)
To: Matthew Wilcox
Cc: Andrew Morton, Liam.Howlett, brauner, lorenzo.stoakes,
Davidlohr Bueso, linux-kernel
Matthew Wilcox <willy@infradead.org> wrote:
>
> [Fixing Davidlohr's email address -- David could you pop an entry into
> .mailmap?]
>
> On Sat, Feb 15, 2025 at 02:00:23PM +0900, Jeongjun Park wrote:
> > Andrew Morton <akpm@linux-foundation.org> wrote:
> > >
> > > On Sat, 15 Feb 2025 03:01:57 +0900 Jeongjun Park <aha310510@gmail.com> wrote:
> > >
> > > > 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.
> > >
> > > 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: syzbot+a2b84e569d06ca3a949c@syzkaller.appspotmail.com
>
> For anyone else trying to understand what the hell this is about,
> the report is at:
>
> https://lore.kernel.org/linux-kernel/67af13f8.050a0220.21dd3.0038.GAE@google.com/
>
> > 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?
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
>
> (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?)
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2025-02-19 10:07 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-02-14 18:01 [PATCH] ipc: fix to protect IPCS lookups using RCU instead of semaphore Jeongjun Park
2025-02-14 18:58 ` Matthew Wilcox
2025-02-14 23:21 ` Andrew Morton
2025-02-15 5:00 ` Jeongjun Park
2025-02-15 17:39 ` Matthew Wilcox
2025-02-19 10:07 ` Jeongjun Park
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.