From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 45285C04A68 for ; Thu, 28 Jul 2022 07:34:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233283AbiG1Hem (ORCPT ); Thu, 28 Jul 2022 03:34:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55874 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232635AbiG1Hej (ORCPT ); Thu, 28 Jul 2022 03:34:39 -0400 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3434C5FAEB for ; Thu, 28 Jul 2022 00:34:38 -0700 (PDT) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id E922D33A20; Thu, 28 Jul 2022 07:34:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1658993676; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=bNidsR0p1xyDX6IFAYEZX8Yv7vk0YXivGgQvV4ylVv0=; b=aaC4u3Suh3mW8YlM8QkRb+dD7I1g2nGJi8LnktSdTaJWvqILuCmO7vMAgeajPpWZDLpO6D uhsQIyiP5naW/fhD8JsHXjdsr4JdEUFZyJ9jr/Zr1qwQ9a7NWNpXJhih2drXMuQMATz2LP xEt1i9EmqspomOdb2HXeUlrQ+g1nsJA= Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id BEC9E13A7E; Thu, 28 Jul 2022 07:34:36 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id vZWLLAw84mK+FgAAMHmgww (envelope-from ); Thu, 28 Jul 2022 07:34:36 +0000 Date: Thu, 28 Jul 2022 09:34:36 +0200 From: Michal Hocko To: =?utf-8?B?6ZSm5rGf5bGg?= Cc: Yang Shi , akpm@linux-foundation.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: Re: [External] Re: [PATCH] vmscan: fix potential arbitrary pointer passed to kfree in unregister_shrinker Message-ID: References: <20220727090700.3238-1-tujinjiang@bytedance.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu 28-07-22 10:37:26, 锦江屠 wrote: > On Wed, Jul 27, 2022 at 11:50 PM Yang Shi wrote: > > > > On Wed, Jul 27, 2022 at 7:43 AM Michal Hocko wrote: > > > > > > [Cc Yang Shi] > > > > Thanks, Michal. > > > > > On Wed 27-07-22 17:07:00, tujinjiang@bytedance.com wrote: > > > > From: Jinjiang Tu > > > > > > > > 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