Received: by 2002:a05:7412:cfc7:b0:fc:a2b0:25d7 with SMTP id by7csp2264829rdb; Wed, 21 Feb 2024 02:08:17 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVAjMhnK277dUYZmaitDQKTiQCbiwH47+BWvKkVhcJZ6nldTL7/9QsfDA+C+DQIySNbkEx3u+wpjhnFMsf3qgcZcWUNzROkrl/WAMaVHQ== X-Google-Smtp-Source: AGHT+IGyUOtCLFKkJl6hBu0nVMgB3pgf+M8FFG29QW2TfCdmQNN2rDOWrguREZyeUHaCbweH+PKx X-Received: by 2002:a17:906:b191:b0:a3e:9ad2:b555 with SMTP id w17-20020a170906b19100b00a3e9ad2b555mr5672011ejy.24.1708510097454; Wed, 21 Feb 2024 02:08:17 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708510097; cv=pass; d=google.com; s=arc-20160816; b=z+9r5XhQq3RIY7y4VUPSFF2AukDYIfkTBW5Im3hK1UyctjmXzMLgwxOKvR3+sroovd fWIl3gN2UunfSqPfh3zSmX6s1wxJoWcWO1sCPJSuc/cMPPzlt6ZO2hymdCJ9dVruxagn yfVzes+JAD9Xe/8JNyyJrvfS8bM/r9Dkz2E7vQcFJ1vvmdaEMoHKicME37B5sFUZwh+V YVTaO7mk1l6cOUtQQkwA1hZWgvWT+LK+1aeUWWcX5z3fLc2UGTCMMK6n7aySRtxKg6sh RjetllFSb8l47/QtjsakLRjEp3GmrPbf3rs0V/A6qUy+XL5vV8cOffkoc+WAhmb5LWhV ZNVg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:dkim-signature:dkim-signature; bh=xZGJ3olY1kWj1dk7MtZO1ibtEdnIoZrzoUYLjjxmvX0=; fh=15Aolu+jmjO2eno1qO5NYfbBO3ogvzu2aVn/g5KqOtU=; b=qIP2sE1nmHYuKt3s4Y9nHhw7WiYADFAbnMwuYIT+pNAmTpvhMjcFxJpFxTlJ97cOqW nANXL4vOa1qVKYRHsgUXWV+gZzL0P+2UW4AR/fB0+3ajamzFAh/NTjOj8M9q332DfMcd 2BWIGhiAWUDcMF2L45l1jZQ8vhqYisVgq8316AH/sSZwV9CpJG6NKs1yPbK17cFW87x0 F+ZudyRtPe9UPyUAHATcdre6mizTklMyrGYstXg1ru8Qo2WPHcu8UQ7P2P98j/V8OB7c z00WCu8xyyZv9UmNXwJClCz3WPmyH2DlM8y8+Cq0/J93pUaRkX9+Al07pV22SRNhoUDD UoyA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@suse.com header.s=susede1 header.b=BAbuwTrz; dkim=pass header.i=@suse.com header.s=susede1 header.b=eznXXzg4; arc=pass (i=1 spf=pass spfdomain=suse.com dkim=pass dkdomain=suse.com dkim=pass dkdomain=suse.com dmarc=pass fromdomain=suse.com); spf=pass (google.com: domain of linux-kernel+bounces-74500-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-74500-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=suse.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id hv15-20020a17090760cf00b00a3f4f612765si185067ejc.902.2024.02.21.02.08.17 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Feb 2024 02:08:17 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-74500-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.com header.s=susede1 header.b=BAbuwTrz; dkim=pass header.i=@suse.com header.s=susede1 header.b=eznXXzg4; arc=pass (i=1 spf=pass spfdomain=suse.com dkim=pass dkdomain=suse.com dkim=pass dkdomain=suse.com dmarc=pass fromdomain=suse.com); spf=pass (google.com: domain of linux-kernel+bounces-74500-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-74500-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=suse.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 0ECEA1F219FF for ; Wed, 21 Feb 2024 10:08:17 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5D89D3D980; Wed, 21 Feb 2024 10:08:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="BAbuwTrz"; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="eznXXzg4" Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.223.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B09A13C493; Wed, 21 Feb 2024 10:08:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=195.135.223.131 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708510087; cv=none; b=o8UTyAxs9OHrlk0E8jOL+8O5yk0kGjWBoWELJdoiGFxVpG0SBEtAOp04n/NBWuWhRnzTBmDEzHb7jojOceTXZlH+BEX+4xgFGXRIcPmWYJ5+gZ8s+DhYOhtC+Kj9zh0NZ+//LMi+dyoIuPqvWL9bO8ei1lLbEHxatlQSomVsmBU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708510087; c=relaxed/simple; bh=7XYolvuFmbS0LBqmUQ9KKTmR9uu6+pL4kH1WaTQzMk0=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=cNYVvwa1CUSv8OO8aFBTWkV4hubeLx+PAg5/6qc8UVcQCLqis2rqBeOzjTJEggi0hHcHpJKJX0+t95XAfJjJCtOIyRBwnowFsEcGDRu83hUvMPVHoa/Hi3nyuZGBIpvuz0LJoy7i2EVWGX81kP22X5ntpRIT/l+lLOISNaxzKkY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com; spf=pass smtp.mailfrom=suse.com; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b=BAbuwTrz; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b=eznXXzg4; arc=none smtp.client-ip=195.135.223.131 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.com Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [IPv6:2a07:de40:b281:104:10:150:64:97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id C9C9A1FB4A; Wed, 21 Feb 2024 10:08:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1708510083; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=xZGJ3olY1kWj1dk7MtZO1ibtEdnIoZrzoUYLjjxmvX0=; b=BAbuwTrzJs/DVFKRHFbDFTMrOfHETXJMisKC7tj8M6JjdNEOXo93jcYFaNdeQJjEAyfekQ CrWyK55GtVGUQVgEG0K6dxNYX167qbb3oWip8LFfLjTQ6Xe1gF1jehGKrTY0i0DAvPcKNH EDKodBHi1qarR9EShsI0U7wX2BQobl8= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1708510082; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=xZGJ3olY1kWj1dk7MtZO1ibtEdnIoZrzoUYLjjxmvX0=; b=eznXXzg4qM0A3TmtjnVpTQmOa20c6dDKPyNZj3w3j7MVgFsxv/M5WS8FbQNP/f2QOO1VnF nvww1JNkoDaOiWtTt/Vga4ezs7hNrjfk0TWvaU7SBvELlCYWYPJglPfWuXzv/l7B1nQyfm HFJxqzEV5z/gbJCCXo07IFcHySDtCEE= Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id AA4A813A69; Wed, 21 Feb 2024 10:08:02 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id qRKQJoLL1WXHQAAAD6G6ig (envelope-from ); Wed, 21 Feb 2024 10:08:02 +0000 Date: Wed, 21 Feb 2024 11:08:02 +0100 From: Michal Hocko To: Gong Ruiqi Cc: linux-kernel@vger.kernel.org, stable@vger.kernel.org, Johannes Weiner , Roman Gushchin , Shakeel Butt , Muchun Song , cgroups@vger.kernel.org, linux-mm@kvack.org, Wang Weiyang , Xiu Jianfeng Subject: Re: [PATCH stable] memcg: add refcnt for pcpu stock to avoid UAF problem in drain_all_stock() Message-ID: References: <20240221081801.69764-1-gongruiqi1@huawei.com> <5436af7a-26d4-7c04-466a-7163d5a26040@huawei.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <5436af7a-26d4-7c04-466a-7163d5a26040@huawei.com> Authentication-Results: smtp-out2.suse.de; dkim=pass header.d=suse.com header.s=susede1 header.b=eznXXzg4 X-Spamd-Result: default: False [-2.81 / 50.00]; ARC_NA(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; R_DKIM_ALLOW(-0.20)[suse.com:s=susede1]; SPAMHAUS_XBL(0.00)[2a07:de40:b281:104:10:150:64:97:from]; FROM_HAS_DN(0.00)[]; TO_DN_SOME(0.00)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; MIME_GOOD(-0.10)[text/plain]; RCVD_COUNT_THREE(0.00)[3]; DKIM_SIGNED(0.00)[suse.com:s=susede1]; DKIM_TRACE(0.00)[suse.com:+]; MX_GOOD(-0.01)[]; RCPT_COUNT_SEVEN(0.00)[11]; DBL_BLOCKED_OPENRESOLVER(0.00)[suse.com:dkim,suse.com:email,huawei.com:email]; FUZZY_BLOCKED(0.00)[rspamd.com]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+]; MID_RHS_NOT_FQDN(0.50)[]; RCVD_TLS_ALL(0.00)[]; BAYES_HAM(-3.00)[100.00%]; RBL_SPAMHAUS_BLOCKED_OPENRESOLVER(0.00)[2a07:de40:b281:104:10:150:64:97:from] X-Rspamd-Server: rspamd1.dmz-prg2.suse.org X-Rspamd-Queue-Id: C9C9A1FB4A X-Spam-Level: X-Spam-Score: -2.81 X-Spam-Flag: NO 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 Acked-by: Michal Hocko Thanks! -- Michal Hocko SUSE Labs