From patchwork Tue Dec 21 09:44:47 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: SeongJae Park X-Patchwork-Id: 12689331 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id A604DC433FE for ; Tue, 21 Dec 2021 09:44:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1A5086B0071; Tue, 21 Dec 2021 04:44:56 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 1553C6B0074; Tue, 21 Dec 2021 04:44:56 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0441D6B0078; Tue, 21 Dec 2021 04:44:56 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0237.hostedemail.com [216.40.44.237]) by kanga.kvack.org (Postfix) with ESMTP id EB1EA6B0071 for ; Tue, 21 Dec 2021 04:44:55 -0500 (EST) Received: from smtpin27.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id A88618249980 for ; Tue, 21 Dec 2021 09:44:55 +0000 (UTC) X-FDA: 78941317350.27.E3D4D4A Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by imf20.hostedemail.com (Postfix) with ESMTP id 0B9471C0046 for ; Tue, 21 Dec 2021 09:44:50 +0000 (UTC) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 73723B8106E; Tue, 21 Dec 2021 09:44:53 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id CFDC3C36AE2; Tue, 21 Dec 2021 09:44:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1640079892; bh=Pz/iubvYizxlg2FCVM2vG0pRngm0zhdsMP6AU472feQ=; h=From:To:Cc:Subject:Date:From; b=Tw7kNDzUE9l4ONjircCWFj8twDmqDRtDd+2y/4yo9BOFzGQIg7NFI0qDH6+uHs/Gh 6E15FzvsxR/+cF6jDSWWBr4RuoNJMxd8ESBUprZECJJZbfxkaoGgIoytaLUY0aTDwr deFzjw5Vpy8JQoferKqpmPl0CcRLtdlKE2fFBL3F6gXrP4lDC22OyL9XEIou1bZDLa Cn3Q0nu+PJRwkGdjNs7qhACocrDdYvZ6EE5x1Y0Mg9r8XJaN1fMv9zigqgbDmuqnwj nbLzvkoVzETN+jS36/detHVCMDT0ZRF+hxidE+rMvzXBnk0w8ywDX2WP7XB1GsXuIH Vrzu6lKBI8Unw== From: SeongJae Park To: akpm@linux-foundation.org Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, SeongJae Park , stable@vger.kernel.org Subject: [PATCH] mm/damon/dbgfs: Protect targets destructions with kdamond_lock Date: Tue, 21 Dec 2021 09:44:47 +0000 Message-Id: <20211221094447.2241-1-sj@kernel.org> X-Mailer: git-send-email 2.17.1 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 0B9471C0046 X-Stat-Signature: pp6jskwxi1a5j9shq3frrd5xfdbqew73 Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=Tw7kNDzU; spf=pass (imf20.hostedemail.com: domain of sj@kernel.org designates 145.40.68.75 as permitted sender) smtp.mailfrom=sj@kernel.org; dmarc=pass (policy=none) header.from=kernel.org X-HE-Tag: 1640079890-264486 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: DAMON debugfs interface iterates current monitoring targets in 'dbgfs_target_ids_read()' while holding the corresponding 'kdamond_lock'. However, it also destructs the monitoring targets in 'dbgfs_before_terminate()' without holding the lock. This can result in a use_after_free bug. This commit avoids the race by protecting the destruction with the corresponding 'kdamond_lock'. Reported-by: Sangwoo Bae Fixes: 4bc05954d007 ("mm/damon: implement a debugfs-based user space interface") Signed-off-by: SeongJae Park Cc: # 5.15.x --- This cannot cleanly applied on 5.15.y tree. I will post a backport as soon as this is applied on the mainline. mm/damon/dbgfs.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/mm/damon/dbgfs.c b/mm/damon/dbgfs.c index 58dbb9692279..489be9c830c4 100644 --- a/mm/damon/dbgfs.c +++ b/mm/damon/dbgfs.c @@ -659,10 +659,12 @@ static void dbgfs_before_terminate(struct damon_ctx *ctx) if (!targetid_is_pid(ctx)) return; + mutex_lock(&ctx->kdamond_lock); damon_for_each_target_safe(t, next, ctx) { put_pid((struct pid *)t->id); damon_destroy_target(t); } + mutex_unlock(&ctx->kdamond_lock); } static struct damon_ctx *dbgfs_new_ctx(void)