From: Muchun Song Date: Tue, 15 Dec 2020 03:06:35 +0000 (-0800) Subject: mm: memcg/slab: fix use after free in obj_cgroup_charge X-Git-Tag: Ubuntu-5.13.0-19.19~4398^2~141 X-Git-Url: https://git.proxmox.com/?a=commitdiff_plain;h=eefbfa7fd678805b38a46293e78543f98f353d3e;p=mirror_ubuntu-jammy-kernel.git mm: memcg/slab: fix use after free in obj_cgroup_charge The rcu_read_lock/unlock only can guarantee that the memcg will not be freed, but it cannot guarantee the success of css_get to memcg. If the whole process of a cgroup offlining is completed between reading a objcg->memcg pointer and bumping the css reference on another CPU, and there are exactly 0 external references to this memory cgroup (how we get to the obj_cgroup_charge() then?), css_get() can change the ref counter from 0 back to 1. Link: https://lkml.kernel.org/r/20201028035013.99711-2-songmuchun@bytedance.com Fixes: bf4f059954dc ("mm: memcg/slab: obj_cgroup API") Signed-off-by: Muchun Song Acked-by: Roman Gushchin Reviewed-by: Shakeel Butt Cc: Johannes Weiner Cc: Michal Hocko Cc: Vladimir Davydov Cc: Joonsoo Kim Cc: Yafang Shao Cc: Chris Down Cc: Christian Brauner Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds --- diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 4ac4d5edada4..08a8fac29f6a 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -3235,8 +3235,10 @@ int obj_cgroup_charge(struct obj_cgroup *objcg, gfp_t gfp, size_t size) * independently later. */ rcu_read_lock(); +retry: memcg = obj_cgroup_memcg(objcg); - css_get(&memcg->css); + if (unlikely(!css_tryget(&memcg->css))) + goto retry; rcu_read_unlock(); nr_pages = size >> PAGE_SHIFT;