Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp9108030pxu; Mon, 28 Dec 2020 06:50:59 -0800 (PST) X-Google-Smtp-Source: ABdhPJyTHkcMQVqEYd4zp2mNx+ZccRcizx2uhnHmxKesRQ313bpvr9X9q9pnm0bVfXCaR44sxu+y X-Received: by 2002:a17:906:eb5b:: with SMTP id mc27mr41480112ejb.163.1609167059188; Mon, 28 Dec 2020 06:50:59 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1609167059; cv=none; d=google.com; s=arc-20160816; b=nduY77vnL2fG8UmLJGoIlvT4tm1YDiHmQoLutKggHB6nJtUoLt5n0U/BHuJHy+3mlt svPQo6RhK6+XfBRzv9NfXLEnn/EezbmuI2VE82/wUXF4ypY7oyB9nSMIkAi9FWY5P9qB Mt3/Qp+YcD5YFVWQ3/TDh4OAJNGpaDsq0xi46Ni/gbC+KjrcGHD3K5B6uTqRpWjjUkAU N758jUbqnqAfUDfZhzW6xuaZifEUCClfXyiap5HaWbODYNW4Pkk6E3UqKgWJIM7a0XAW SLNealVF0b8a5YZLSn7U+NCls1ozA+mTOw+wvDM+tPR+MKwUBf2yYQq8ygpBogWhCszq iyVg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=+Gca8fGDgVdpxB2lIv87ID5lM9MpLvB3oejR3mPzQJk=; b=kkZYT6hKqI8iCRJiddlmgrsXUcX3G3XfTHVifQqt2vpLGfBVbUFaU9gmro5+OWgzcq p2/n8ZRLxcY0tFjSfev9LbvDZWT6qE9a1WhRQN/YrggvNalvPiLFKptrmFk2ZdXioBs8 mKcLP7i9rKL5rKjF3FzZlYQIoT/92jmhFhYvo9oAoBfGQGOX6imAz2swW40AgD1761YS B1kIB+JEIoR4kQFYP7Sj7S4bIhvRmOwn80QNzA6m+rQMjWlTMaj+E5GNeSIWtSEz9+7k tCVMkHIGt5tpsC9ChptW+C7dWbLYhgJKUHGUESxf7vtj2lZEB3jxDJhkG/7BZXLtN37/ mTCQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=TxcLn54t; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id t23si22386961edc.387.2020.12.28.06.50.36; Mon, 28 Dec 2020 06:50:59 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=TxcLn54t; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2437885AbgL1Osj (ORCPT + 99 others); Mon, 28 Dec 2020 09:48:39 -0500 Received: from mail.kernel.org ([198.145.29.99]:57788 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2441675AbgL1OWM (ORCPT ); Mon, 28 Dec 2020 09:22:12 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 2E80F229C4; Mon, 28 Dec 2020 14:21:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1609165291; bh=gDMSkKdkI6DuwgGljiqXB6oVSHWFOZH1OMuzsX0sd3U=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=TxcLn54tm+T52zNvjH3fXEpVmF24GHvwZZu6s0f7CPjngvyU1AkLf8097kMuG0bFr PgTWwyK/xP2MkaF0ekyWSa0vcSbB/aifXweFlE3Y+bolRfRmL1ICDY086tvzPMX5pL AwyCT0a9zqAcIpnSMOaQ+lnFLdS6EK6LBOGSnAS8= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Muchun Song , Roman Gushchin , Shakeel Butt , Johannes Weiner , Michal Hocko , Vladimir Davydov , Joonsoo Kim , Yafang Shao , Chris Down , Christian Brauner , Andrew Morton , Linus Torvalds , Sasha Levin Subject: [PATCH 5.10 450/717] mm: memcg/slab: fix use after free in obj_cgroup_charge Date: Mon, 28 Dec 2020 13:47:28 +0100 Message-Id: <20201228125042.539219061@linuxfoundation.org> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201228125020.963311703@linuxfoundation.org> References: <20201228125020.963311703@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Muchun Song [ Upstream commit eefbfa7fd678805b38a46293e78543f98f353d3e ] 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 Signed-off-by: Sasha Levin --- mm/memcontrol.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 74b85077f89ad..a717728cc7b4a 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -3247,8 +3247,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; -- 2.27.0