All of lore.kernel.org
 help / color / mirror / Atom feed
From: Michal Hocko <mhocko@suse.com>
To: Gong Ruiqi <gongruiqi1@huawei.com>
Cc: linux-kernel@vger.kernel.org, stable@vger.kernel.org,
	Johannes Weiner <hannes@cmpxchg.org>,
	Roman Gushchin <roman.gushchin@linux.dev>,
	Shakeel Butt <shakeelb@google.com>,
	Muchun Song <muchun.song@linux.dev>,
	cgroups@vger.kernel.org, linux-mm@kvack.org,
	Wang Weiyang <wangweiyang2@huawei.com>,
	Xiu Jianfeng <xiujianfeng@huawei.com>
Subject: Re: [PATCH stable] memcg: add refcnt for pcpu stock to avoid UAF problem in drain_all_stock()
Date: Wed, 21 Feb 2024 11:08:02 +0100	[thread overview]
Message-ID: <ZdXLgjpUfpwEwAe0@tiehlicka> (raw)
In-Reply-To: <5436af7a-26d4-7c04-466a-7163d5a26040@huawei.com>

On Wed 21-02-24 17:50:27, Gong Ruiqi wrote:
> 
> On 2024/02/21 16:38, Michal Hocko wrote:
> > On Wed 21-02-24 16:18:01, GONG, Ruiqi wrote:
> >> commit 1a3e1f40962c445b997151a542314f3c6097f8c3 upstream.
> > 
> > I think it would be good to mention that this is only a partial backport
> > and also explain why to do a partial rather than the full one.
> >
> 
> Okay. I think to fix this problem we should add refcnt relation between
> memcg and stock, and since higher versions have achieved this, maybe
> it's better to use the same code and align with them. So I put a "commit
> xxx upstream" here, as requested in kernel docs[1]. So yes it's a
> partial backport as we only need the stock part.

I think it is sufficient to mention that this is a partial backport to
minimize the fix to the bare minimum.

[...]
> > What does prevent from the following?
> > 
> > refill_stock(memcgC)		drain_all_stock(memcgB)
> >   drain_stock(memcgA)		  rcu_read_lock()
> >     css_put(old->css)		  memcgA = stock->cached
> >                                   mem_cgroup_is_descendant(memcgA, memcgB) UAF
> >     stock->cached = NULL
> > 
> 
> I think it's not a problem since refill_stock() has disabled irq before
> calling drain_stock():
> 
> refill_stock(memcgC)
>   local_irq_save
>   drain_stock(memcgA)
>     css_put(old->css)
>     <1>
>     stock->cached = NULL
>   local_irq_restore
>   <2>
> 
> And since css_put(old->css) is an RCU free, memcgA would not be freed at
> <1> as it's still in grace period. The actual release of memcgA could
> happen only after irq is enabled (at <2>).
> 
> And for CPU2, the access to stock->cached in drain_all_stock() is
> protected by rcu_read_lock(), so from stock->cached we get either NULL,
> or a memcgA that is still not freed.
> 
> Please correct me if I have some wrong understanding to RCU.

You are right. Thanks! IRQ disabling is there in one form or the other
since db2ba40c277d ("mm: memcontrol: make per-cpu charge cache IRQ-safe
for socket accounting") so 4.8+ is safe. Backports to older kernels
would nee to pull this one as well.

> >> Cc: stable@vger.kernel.org      # 4.19 5.4
> >> Fixes: cdec2e4265df ("memcg: coalesce charging via percpu storage")
> >> Signed-off-by: GONG, Ruiqi <gongruiqi1@huawei.com>

Acked-by: Michal Hocko <mhocko@suse.com>

Thanks!

-- 
Michal Hocko
SUSE Labs

  reply	other threads:[~2024-02-21 10:08 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-21  8:18 [PATCH stable] memcg: add refcnt for pcpu stock to avoid UAF problem in drain_all_stock() GONG, Ruiqi
2024-02-21  8:38 ` Michal Hocko
2024-02-21  9:50   ` Gong Ruiqi
2024-02-21 10:08     ` Michal Hocko [this message]
2024-02-21 10:22       ` Michal Hocko
2024-02-22  2:01       ` Gong Ruiqi
2024-02-21  8:40 ` Greg KH
2024-02-21  8:48   ` Michal Hocko
2024-02-21 10:33     ` Greg KH
2024-02-21 10:48       ` Michal Hocko

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=ZdXLgjpUfpwEwAe0@tiehlicka \
    --to=mhocko@suse.com \
    --cc=cgroups@vger.kernel.org \
    --cc=gongruiqi1@huawei.com \
    --cc=hannes@cmpxchg.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=muchun.song@linux.dev \
    --cc=roman.gushchin@linux.dev \
    --cc=shakeelb@google.com \
    --cc=stable@vger.kernel.org \
    --cc=wangweiyang2@huawei.com \
    --cc=xiujianfeng@huawei.com \
    /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 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.