From patchwork Mon Oct 31 18:25:53 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: SeongJae Park X-Patchwork-Id: 13026261 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 001C4ECAAA1 for ; Mon, 31 Oct 2022 18:26:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 29F2D6B0071; Mon, 31 Oct 2022 14:26:08 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 24F056B0072; Mon, 31 Oct 2022 14:26:08 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 13DFA80007; Mon, 31 Oct 2022 14:26:08 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 0547E6B0071 for ; Mon, 31 Oct 2022 14:26:08 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id C144DA0D78 for ; Mon, 31 Oct 2022 18:26:07 +0000 (UTC) X-FDA: 80082073974.29.1F9958B Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf24.hostedemail.com (Postfix) with ESMTP id 4A722180021 for ; Mon, 31 Oct 2022 18:26:07 +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 dfw.source.kernel.org (Postfix) with ESMTPS id B605C613B3; Mon, 31 Oct 2022 18:26:04 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 888BEC433D6; Mon, 31 Oct 2022 18:26:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1667240764; bh=EQ6SCcuU+VCGAwnEUYyTpKsgCpaNR+eNW9wxrdFWY7Y=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=YrNr3rYc0LzaAOhzUJyN74HSydG6tFsLGBCF0OlFBlRLQcTIMKXbfU8v0FBvNGLr4 +ynvDJhYjT3+sHZjnQMGkYB2I7aDbIUjtx/Q7uxmfZSt9Z0XXmFsCGCQQgsqN1YWNq 6g2Cfw/ph3Eiqj40lBkn5tAC4+McSpi8JqLzK1D5WPOQszQRoFsJDQtMG8ytji+JTD Af75o0UweT3zZU0ewPlF/+Pz3FblmqYz6xjlTE0ZcMFqjBKwDf7twSfkiY/AemUHGU jrg6PW0r38LMESgMG7M0Zs0MhcbYXsZJn6uFks9D3z1MkYA2f0FgtHmRMcOU4KQI5r H1J/Rju5qfa+Q== From: SeongJae Park To: Andrew Morton Cc: damon@lists.linux.dev, linux-mm@kvack.org, linux-kernel@vger.kernel.org, SeongJae Park , syzbot+6087eafb76a94c4ac9eb@syzkaller.appspotmail.com, stable@vger.kernel.org Subject: [PATCH 1/2] mm/damon/dbgfs: check if rm_contexts input is for a real context Date: Mon, 31 Oct 2022 18:25:53 +0000 Message-Id: <20221031182554.7882-2-sj@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221031182554.7882-1-sj@kernel.org> References: <20221031182554.7882-1-sj@kernel.org> MIME-Version: 1.0 ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1667240767; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=7qQHlhYX+JB1mXiX1LgdSYUnzilRLEh6XL7ftEcmRp0=; b=bCEelkQKRAtsskD4kEFUGHPhoco3Rad4Br22JgCf8/b8eSrbdzQmmvTBPu60i6AzdyTzJy WviVrvE+Vj7b/1BCeknJ6EWvM9a8LUKbqPkDfaz7axOsbMK+cMzYbJydK5okZHvz9nMBcY vSDk5u4M6DExUfBbp5gUOC8GQNj7eOw= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=YrNr3rYc; spf=pass (imf24.hostedemail.com: domain of sj@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=sj@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1667240767; a=rsa-sha256; cv=none; b=f//iVDpTGByPFqYp7KdwM7uvNnahoDJR7Gov9E3Ys/Ru8QeCSXBRZlk0RjHqHF3qNJAt87 yGRbyyuGeCG6BlEVSjJti3mLzqHPiR4vB80Xr6lmqByNVvgdKIGhGzRbxC/MP2g+PX1Uxr m85k9WBBVaU4prO+EofXM3aWp7fETFQ= X-Stat-Signature: sfwsk9aiaartdk5wg87usdd8a8c5npsx X-Rspamd-Queue-Id: 4A722180021 Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=YrNr3rYc; spf=pass (imf24.hostedemail.com: domain of sj@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=sj@kernel.org; dmarc=pass (policy=none) header.from=kernel.org X-Rspam-User: X-Rspamd-Server: rspam01 X-HE-Tag: 1667240767-280722 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: A user could write a name of a file under 'damon/' debugfs directory, which is not a user-created context, to 'rm_contexts' file. In the case, 'dbgfs_rm_context()' just assumes it's the valid DAMON context directory only if a file of the name exist. As a result, invalid memory access could happen as below. Fix the bug by checking if the given input is for a directory. This check can filter out non-context inputs because directories under 'damon/' debugfs directory can be created via only 'mk_contexts' file. This bug has found by syzbot[1]. [1] https://lore.kernel.org/damon/000000000000ede3ac05ec4abf8e@google.com/ Reported-by: syzbot+6087eafb76a94c4ac9eb@syzkaller.appspotmail.com Fixes: 75c1c2b53c78 ("mm/damon/dbgfs: support multiple contexts") Cc: # 5.15.x Signed-off-by: SeongJae Park --- mm/damon/dbgfs.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/mm/damon/dbgfs.c b/mm/damon/dbgfs.c index 6f0ae7d3ae39..b3f454a5c682 100644 --- a/mm/damon/dbgfs.c +++ b/mm/damon/dbgfs.c @@ -890,6 +890,7 @@ static ssize_t dbgfs_mk_context_write(struct file *file, static int dbgfs_rm_context(char *name) { struct dentry *root, *dir, **new_dirs; + struct inode *inode; struct damon_ctx **new_ctxs; int i, j; int ret = 0; @@ -905,6 +906,12 @@ static int dbgfs_rm_context(char *name) if (!dir) return -ENOENT; + inode = d_inode(dir); + if (!S_ISDIR(inode->i_mode)) { + ret = -EINVAL; + goto out_dput; + } + new_dirs = kmalloc_array(dbgfs_nr_ctxs - 1, sizeof(*dbgfs_dirs), GFP_KERNEL); if (!new_dirs) { From patchwork Mon Oct 31 18:25:54 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: SeongJae Park X-Patchwork-Id: 13026263 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 3A595FA3743 for ; Mon, 31 Oct 2022 18:26:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1186180008; Mon, 31 Oct 2022 14:26:10 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0A18780007; Mon, 31 Oct 2022 14:26:10 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id ED26A80008; Mon, 31 Oct 2022 14:26:09 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id DBEFA80007 for ; Mon, 31 Oct 2022 14:26:09 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id A5CA440508 for ; Mon, 31 Oct 2022 18:26:09 +0000 (UTC) X-FDA: 80082074058.07.6C5B7C8 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf23.hostedemail.com (Postfix) with ESMTP id 42BC6140008 for ; Mon, 31 Oct 2022 18:26:07 +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 dfw.source.kernel.org (Postfix) with ESMTPS id 1227561277; Mon, 31 Oct 2022 18:26:05 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 51E6AC433D7; Mon, 31 Oct 2022 18:26:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1667240764; bh=9HGMZvb82sypIRoPvUis/kF/0CFkMhQKBGHkFijQToc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=gT3GFkLLYhjvWGz4OnEzIQ5xNeOb0lzsOAgIYeEe7IjZwKsqzCokjPYnlWESuDvXl 2qG2q7qyueHS+IPqYNIEkESAg6fXUCs9M3QgBkGE7KR0s8K4k+s9mqzhQ9NkZGAxas LQgPVn8K+JQJr614faMtd5JgZj7JWOAtQmNNV1dtfnjGXPMJ4RiWpXeNvpz58csDg0 4AIUFwxdOW6MGu4NJVnN13Zzjb9XaB089VAq0MOxHaLzf9Tfr7ZeD64HPgQizuf6uv zqSsF0FAzGME9XV5BG3uOVx5b15/VwkaauVO94yF9rEpLEX+rk9Pz8Rd+iq2usdmDw 09fAQmbBMDi6Q== From: SeongJae Park To: Andrew Morton Cc: Shuah Khan , damon@lists.linux.dev, linux-mm@kvack.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, SeongJae Park Subject: [PATCH 2/2] selftests/damon: test non-context inputs to rm_contexts file Date: Mon, 31 Oct 2022 18:25:54 +0000 Message-Id: <20221031182554.7882-3-sj@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221031182554.7882-1-sj@kernel.org> References: <20221031182554.7882-1-sj@kernel.org> MIME-Version: 1.0 ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1667240767; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=VgubQM217VrfKa24NsReiE9VfnfZxpk1OkuE3vLK5tY=; b=yvg9+GVsue3N2A+HV2TyTlGPS6b8lS9915Qnc7SfTysyivDu9yL/e0GSoEVQeT3Engo9SJ KR2PIAiwCwPXA20co7FkQieXOjmBArsZXETTz8qwoVB+emq32qsiN8jMtz1sJ184ikHVLn FTrN/VjY70zOZ5xW2yMLIFe+arJ0EbE= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=gT3GFkLL; spf=pass (imf23.hostedemail.com: domain of sj@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=sj@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1667240767; a=rsa-sha256; cv=none; b=tg8uN4FDu/HGMHPBRP8RCHBMm7O9QXiijbI7TFJX8AdWx2+Ya62LqS6uiRJqXPmFdItarq s95Svsa4x1UjciTRK0OFnGBSDK/vESZkhcXNzXB81KirH5eBGd5tDgDefgTPpohKkHLgfL 7N+CaGPIWQoGozRJbP4hi/lzV/0SmtI= X-Stat-Signature: kxdxd7adtqwartqgj39c6i4pcuszmyg3 X-Rspamd-Queue-Id: 42BC6140008 Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=gT3GFkLL; spf=pass (imf23.hostedemail.com: domain of sj@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=sj@kernel.org; dmarc=pass (policy=none) header.from=kernel.org X-Rspam-User: X-Rspamd-Server: rspam01 X-HE-Tag: 1667240767-721137 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: There was a bug[1] that triggered by writing non-context DAMON debugfs file names to the 'rm_contexts' DAMON debugfs file. Add a selftest for the bug to avoid it happen again. [1] https://lore.kernel.org/damon/000000000000ede3ac05ec4abf8e@google.com/ Signed-off-by: SeongJae Park --- tools/testing/selftests/damon/Makefile | 1 + .../damon/debugfs_rm_non_contexts.sh | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) create mode 100755 tools/testing/selftests/damon/debugfs_rm_non_contexts.sh diff --git a/tools/testing/selftests/damon/Makefile b/tools/testing/selftests/damon/Makefile index af490acc5348..838a8e49f77b 100644 --- a/tools/testing/selftests/damon/Makefile +++ b/tools/testing/selftests/damon/Makefile @@ -7,6 +7,7 @@ TEST_FILES = _chk_dependency.sh _debugfs_common.sh TEST_PROGS = debugfs_attrs.sh debugfs_schemes.sh debugfs_target_ids.sh TEST_PROGS += debugfs_empty_targets.sh debugfs_huge_count_read_write.sh TEST_PROGS += debugfs_duplicate_context_creation.sh +TEST_PROGS += debugfs_rm_non_contexts.sh TEST_PROGS += sysfs.sh TEST_PROGS += reclaim.sh lru_sort.sh diff --git a/tools/testing/selftests/damon/debugfs_rm_non_contexts.sh b/tools/testing/selftests/damon/debugfs_rm_non_contexts.sh new file mode 100755 index 000000000000..48b7af6b022c --- /dev/null +++ b/tools/testing/selftests/damon/debugfs_rm_non_contexts.sh @@ -0,0 +1,19 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0 + +source _debugfs_common.sh + +# Test putting non-ctx files/dirs to rm_contexts file +# =================================================== + +dmesg -C + +for file in "$DBGFS/"* +do + echo "$(basename "$f")" > "$DBGFS/rm_contexts" + if dmesg | grep -q BUG + then + dmesg + exit 1 + fi +done