* [PATCH] vmscan: fix potential arbitrary pointer passed to kfree in unregister_shrinker
@ 2022-07-27 9:07 tujinjiang
2022-07-27 14:43 ` Michal Hocko
0 siblings, 1 reply; 5+ messages in thread
From: tujinjiang @ 2022-07-27 9:07 UTC (permalink / raw)
To: akpm; +Cc: linux-mm, linux-kernel, Jinjiang Tu
From: Jinjiang Tu <tujinjiang@bytedance.com>
when shrinker is registered with SHRINKER_MEMCG_AWARE flag,
register_shrinker will not initialize shrinker->nr_deferred,
but the pointer will be passed to kfree in unregister_shrinker
when the shrinker is unregistered. This leads to kernel crash
when the shrinker object is dynamically allocated.
To fix it, this patch initialize shrinker->nr_deferred at the
beginning of prealloc_shrinker.
Signed-off-by: Jinjiang Tu <tujinjiang@bytedance.com>
---
mm/vmscan.c | 1 +
1 file changed, 1 insertion(+)
diff --git a/mm/vmscan.c b/mm/vmscan.c
index f7d9a683e3a7..06ab5a398971 100644
--- a/mm/vmscan.c
+++ b/mm/vmscan.c
@@ -613,6 +613,7 @@ int prealloc_shrinker(struct shrinker *shrinker)
unsigned int size;
int err;
+ shrinker->nr_deferred = NULL;
if (shrinker->flags & SHRINKER_MEMCG_AWARE) {
err = prealloc_memcg_shrinker(shrinker);
if (err != -ENOSYS)
--
2.17.1
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH] vmscan: fix potential arbitrary pointer passed to kfree in unregister_shrinker
2022-07-27 9:07 [PATCH] vmscan: fix potential arbitrary pointer passed to kfree in unregister_shrinker tujinjiang
@ 2022-07-27 14:43 ` Michal Hocko
2022-07-27 15:50 ` Yang Shi
0 siblings, 1 reply; 5+ messages in thread
From: Michal Hocko @ 2022-07-27 14:43 UTC (permalink / raw)
To: tujinjiang; +Cc: akpm, linux-mm, linux-kernel, Yang Shi
[Cc Yang Shi]
On Wed 27-07-22 17:07:00, tujinjiang@bytedance.com wrote:
> From: Jinjiang Tu <tujinjiang@bytedance.com>
>
> when shrinker is registered with SHRINKER_MEMCG_AWARE flag,
> register_shrinker will not initialize shrinker->nr_deferred,
> but the pointer will be passed to kfree in unregister_shrinker
> when the shrinker is unregistered. This leads to kernel crash
> when the shrinker object is dynamically allocated.
Is this a real life problem? I thought shrinkers were pre-zeroed
already. Not that we should be relying on that but it would be good to
mention whether this is a code fortification or something that we should
be really worried about.
> To fix it, this patch initialize shrinker->nr_deferred at the
> beginning of prealloc_shrinker.
It would be great to add
Fixes: 476b30a0949a ("mm: vmscan: don't need allocate shrinker->nr_deferred for memcg aware shrinkers")
> Signed-off-by: Jinjiang Tu <tujinjiang@bytedance.com>
> ---
> mm/vmscan.c | 1 +
> 1 file changed, 1 insertion(+)
>
> diff --git a/mm/vmscan.c b/mm/vmscan.c
> index f7d9a683e3a7..06ab5a398971 100644
> --- a/mm/vmscan.c
> +++ b/mm/vmscan.c
> @@ -613,6 +613,7 @@ int prealloc_shrinker(struct shrinker *shrinker)
> unsigned int size;
> int err;
>
> + shrinker->nr_deferred = NULL;
> if (shrinker->flags & SHRINKER_MEMCG_AWARE) {
> err = prealloc_memcg_shrinker(shrinker);
> if (err != -ENOSYS)
You should be able to move it under SHRINKER_MEMCG_AWARE branch, no?
--
Michal Hocko
SUSE Labs
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] vmscan: fix potential arbitrary pointer passed to kfree in unregister_shrinker
2022-07-27 14:43 ` Michal Hocko
@ 2022-07-27 15:50 ` Yang Shi
2022-07-28 2:37 ` [External] " 锦江屠
0 siblings, 1 reply; 5+ messages in thread
From: Yang Shi @ 2022-07-27 15:50 UTC (permalink / raw)
To: Michal Hocko; +Cc: tujinjiang, akpm, linux-mm, linux-kernel
On Wed, Jul 27, 2022 at 7:43 AM Michal Hocko <mhocko@suse.com> wrote:
>
> [Cc Yang Shi]
Thanks, Michal.
> On Wed 27-07-22 17:07:00, tujinjiang@bytedance.com wrote:
> > From: Jinjiang Tu <tujinjiang@bytedance.com>
> >
> > when shrinker is registered with SHRINKER_MEMCG_AWARE flag,
> > register_shrinker will not initialize shrinker->nr_deferred,
> > but the pointer will be passed to kfree in unregister_shrinker
> > when the shrinker is unregistered. This leads to kernel crash
> > when the shrinker object is dynamically allocated.
>
> Is this a real life problem? I thought shrinkers were pre-zeroed
> already. Not that we should be relying on that but it would be good to
> mention whether this is a code fortification or something that we should
> be really worried about.
Yes, all memcg aware shrinkers are actually pre-zeroed. The fs
shrinkers (embedded in super_block) are allocated by kzalloc, all
other shrinkers are static declared. So I don't think it will cause
any crash in real life.
>
> > To fix it, this patch initialize shrinker->nr_deferred at the
> > beginning of prealloc_shrinker.
>
> It would be great to add
> Fixes: 476b30a0949a ("mm: vmscan: don't need allocate shrinker->nr_deferred for memcg aware shrinkers")
>
> > Signed-off-by: Jinjiang Tu <tujinjiang@bytedance.com>
> > ---
> > mm/vmscan.c | 1 +
> > 1 file changed, 1 insertion(+)
> >
> > diff --git a/mm/vmscan.c b/mm/vmscan.c
> > index f7d9a683e3a7..06ab5a398971 100644
> > --- a/mm/vmscan.c
> > +++ b/mm/vmscan.c
> > @@ -613,6 +613,7 @@ int prealloc_shrinker(struct shrinker *shrinker)
> > unsigned int size;
> > int err;
> >
> > + shrinker->nr_deferred = NULL;
> > if (shrinker->flags & SHRINKER_MEMCG_AWARE) {
> > err = prealloc_memcg_shrinker(shrinker);
> > if (err != -ENOSYS)
>
> You should be able to move it under SHRINKER_MEMCG_AWARE branch, no?
>
> --
> Michal Hocko
> SUSE Labs
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [External] Re: [PATCH] vmscan: fix potential arbitrary pointer passed to kfree in unregister_shrinker
2022-07-27 15:50 ` Yang Shi
@ 2022-07-28 2:37 ` 锦江屠
2022-07-28 7:34 ` Michal Hocko
0 siblings, 1 reply; 5+ messages in thread
From: 锦江屠 @ 2022-07-28 2:37 UTC (permalink / raw)
To: Yang Shi; +Cc: Michal Hocko, akpm, linux-mm, linux-kernel
On Wed, Jul 27, 2022 at 11:50 PM Yang Shi <shy828301@gmail.com> wrote:
>
> On Wed, Jul 27, 2022 at 7:43 AM Michal Hocko <mhocko@suse.com> wrote:
> >
> > [Cc Yang Shi]
>
> Thanks, Michal.
>
> > On Wed 27-07-22 17:07:00, tujinjiang@bytedance.com wrote:
> > > From: Jinjiang Tu <tujinjiang@bytedance.com>
> > >
> > > when shrinker is registered with SHRINKER_MEMCG_AWARE flag,
> > > register_shrinker will not initialize shrinker->nr_deferred,
> > > but the pointer will be passed to kfree in unregister_shrinker
> > > when the shrinker is unregistered. This leads to kernel crash
> > > when the shrinker object is dynamically allocated.
> >
> > Is this a real life problem? I thought shrinkers were pre-zeroed
> > already. Not that we should be relying on that but it would be good to
> > mention whether this is a code fortification or something that we should
> > be really worried about.
>
> Yes, all memcg aware shrinkers are actually pre-zeroed. The fs
> shrinkers (embedded in super_block) are allocated by kzalloc, all
> other shrinkers are static declared. So I don't think it will cause
> any crash in real life.
>
Yes, the shrinkers in the current kernel will not cause crash, but a new
memcg aware shrinker may be added in the future, and I think we
should not assume the shrinker is pre-zeroed.
Function free_prealloced_shrinker does not assume the shrinker is pre-zeroed,
and does not call kfree(shrinker->nr_deferred) if the shrinker is memcg aware.
So I think it is better for unregister_shrinker to call kfree only
when the shrinker
is not memcg aware.
> >
> > > To fix it, this patch initialize shrinker->nr_deferred at the
> > > beginning of prealloc_shrinker.
> >
> > It would be great to add
> > Fixes: 476b30a0949a ("mm: vmscan: don't need allocate shrinker->nr_deferred for memcg aware shrinkers")
> >
> > > Signed-off-by: Jinjiang Tu <tujinjiang@bytedance.com>
> > > ---
> > > mm/vmscan.c | 1 +
> > > 1 file changed, 1 insertion(+)
> > >
> > > diff --git a/mm/vmscan.c b/mm/vmscan.c
> > > index f7d9a683e3a7..06ab5a398971 100644
> > > --- a/mm/vmscan.c
> > > +++ b/mm/vmscan.c
> > > @@ -613,6 +613,7 @@ int prealloc_shrinker(struct shrinker *shrinker)
> > > unsigned int size;
> > > int err;
> > >
> > > + shrinker->nr_deferred = NULL;
> > > if (shrinker->flags & SHRINKER_MEMCG_AWARE) {
> > > err = prealloc_memcg_shrinker(shrinker);
> > > if (err != -ENOSYS)
> >
> > You should be able to move it under SHRINKER_MEMCG_AWARE branch, no?
> >
> > --
> > Michal Hocko
> > SUSE Labs
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [External] Re: [PATCH] vmscan: fix potential arbitrary pointer passed to kfree in unregister_shrinker
2022-07-28 2:37 ` [External] " 锦江屠
@ 2022-07-28 7:34 ` Michal Hocko
0 siblings, 0 replies; 5+ messages in thread
From: Michal Hocko @ 2022-07-28 7:34 UTC (permalink / raw)
To: 锦江屠; +Cc: Yang Shi, akpm, linux-mm, linux-kernel
On Thu 28-07-22 10:37:26, 锦江屠 wrote:
> On Wed, Jul 27, 2022 at 11:50 PM Yang Shi <shy828301@gmail.com> wrote:
> >
> > On Wed, Jul 27, 2022 at 7:43 AM Michal Hocko <mhocko@suse.com> wrote:
> > >
> > > [Cc Yang Shi]
> >
> > Thanks, Michal.
> >
> > > On Wed 27-07-22 17:07:00, tujinjiang@bytedance.com wrote:
> > > > From: Jinjiang Tu <tujinjiang@bytedance.com>
> > > >
> > > > when shrinker is registered with SHRINKER_MEMCG_AWARE flag,
> > > > register_shrinker will not initialize shrinker->nr_deferred,
> > > > but the pointer will be passed to kfree in unregister_shrinker
> > > > when the shrinker is unregistered. This leads to kernel crash
> > > > when the shrinker object is dynamically allocated.
> > >
> > > Is this a real life problem? I thought shrinkers were pre-zeroed
> > > already. Not that we should be relying on that but it would be good to
> > > mention whether this is a code fortification or something that we should
> > > be really worried about.
> >
> > Yes, all memcg aware shrinkers are actually pre-zeroed. The fs
> > shrinkers (embedded in super_block) are allocated by kzalloc, all
> > other shrinkers are static declared. So I don't think it will cause
> > any crash in real life.
> >
>
> Yes, the shrinkers in the current kernel will not cause crash, but a new
> memcg aware shrinker may be added in the future, and I think we
> should not assume the shrinker is pre-zeroed.
Agreed. Especially when that is not documented anywhere.
> Function free_prealloced_shrinker does not assume the shrinker is pre-zeroed,
> and does not call kfree(shrinker->nr_deferred) if the shrinker is memcg aware.
> So I think it is better for unregister_shrinker to call kfree only
> when the shrinker
> is not memcg aware.
It would be really great to mention this intention in the changelog.
Your initial wording might make an impression this is a fix for an
existing problem.
> > > > To fix it, this patch initialize shrinker->nr_deferred at the
> > > > beginning of prealloc_shrinker.
> > >
> > > It would be great to add
> > > Fixes: 476b30a0949a ("mm: vmscan: don't need allocate shrinker->nr_deferred for memcg aware shrinkers")
Do not use Fixes tag as this is not a real problem currently.
--
Michal Hocko
SUSE Labs
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2022-07-28 7:34 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-27 9:07 [PATCH] vmscan: fix potential arbitrary pointer passed to kfree in unregister_shrinker tujinjiang
2022-07-27 14:43 ` Michal Hocko
2022-07-27 15:50 ` Yang Shi
2022-07-28 2:37 ` [External] " 锦江屠
2022-07-28 7:34 ` Michal Hocko
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.