From patchwork Mon Nov 7 16:50:00 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: SeongJae Park X-Patchwork-Id: 13034892 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 45EEEC4321E for ; Mon, 7 Nov 2022 16:50:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 78F838E000C; Mon, 7 Nov 2022 11:50:08 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6EEA68E0002; Mon, 7 Nov 2022 11:50:08 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 500938E000D; Mon, 7 Nov 2022 11:50:08 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 1C8038E0002 for ; Mon, 7 Nov 2022 11:50:08 -0500 (EST) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id CC93C12025E for ; Mon, 7 Nov 2022 16:50:07 +0000 (UTC) X-FDA: 80107233654.27.A71E782 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf04.hostedemail.com (Postfix) with ESMTP id 4C51040006 for ; Mon, 7 Nov 2022 16:50:06 +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 752B7611CB; Mon, 7 Nov 2022 16:50:05 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B7189C43144; Mon, 7 Nov 2022 16:50:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1667839805; bh=EQ6SCcuU+VCGAwnEUYyTpKsgCpaNR+eNW9wxrdFWY7Y=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Hz8K1E5g7QuSU1lPUHBQYPuAvdbZF+hpRkL+DbqzRCKz2uBZYiJpRYKMvKp1FgJ/f 2bzqOdR6ml/X6jHJlCVl6VpdVeNyNlBs0pQUnargw1LCUyL/DDm5jit+3Cg31BMjBh 7WKRWz6ng+lWUYPdGld2DEDcMlzEmMozObeFiW2lP65MdaizkIbWMYKyjUyxtT4tot 7z/V987uJ9UH8z6rX8+5iNmvuWujS1khnS1ZzlsJE8DaUkAOhEpiSaoF/+Bp9a6eaZ pHPh5RzQ9eNPwdAYsB6T4VxYxImAddWoltu693R6CIBhDam/Edg71DBJGBso3arWX6 L3BHXF1+CThhA== 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 v2 1/2] mm/damon/dbgfs: check if rm_contexts input is for a real context Date: Mon, 7 Nov 2022 16:50:00 +0000 Message-Id: <20221107165001.5717-2-sj@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221107165001.5717-1-sj@kernel.org> References: <20221107165001.5717-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=1667839806; 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=XQOKLRgr4W3u6YCt3pAUR3fRSEU60WRPZ99y96BmXDNxBxSVWXCPeN3pgsWGU9AH9LIoh9 1WQvrQuFCZB6SSMVqrYMfVh0LU3dZw43pULpDuG7QTq/lppbabXhiCJG3rdohUIJRuJZSI t56qqFvHLEQLurCV9wjLtcTkDsx5vLg= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=Hz8K1E5g; spf=pass (imf04.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=1667839806; a=rsa-sha256; cv=none; b=VWwRD2bQtRyZgLlW03HDive8ARWb80FyOTEvxo5q3fDUKXTgpT8BCsXD6YvKR2nFudnc1H zucqGXaBKnXegyleUfVyoodjaRcRAJ/MVbYWPeEM4z248z/fdhkHXDVRgucNNy2jesHg/H n8L0c0KSPN0qSW/0ExMX3yIW0fX75Ck= Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=Hz8K1E5g; spf=pass (imf04.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: rspam03 X-Rspamd-Queue-Id: 4C51040006 X-Stat-Signature: d7mkfeqpmccxo5xwbn1gp9jm4imcgeq8 X-HE-Tag: 1667839806-592861 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 Nov 7 16:50:01 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: SeongJae Park X-Patchwork-Id: 13034891 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 D0630C43219 for ; Mon, 7 Nov 2022 16:50:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 30DC48E000B; Mon, 7 Nov 2022 11:50:08 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 2F9028E000C; Mon, 7 Nov 2022 11:50:08 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 00F598E000B; Mon, 7 Nov 2022 11:50:07 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id E1CBD8E0002 for ; Mon, 7 Nov 2022 11:50:07 -0500 (EST) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id AD7DBC0EFF for ; Mon, 7 Nov 2022 16:50:07 +0000 (UTC) X-FDA: 80107233654.03.8E1813E Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf06.hostedemail.com (Postfix) with ESMTP id 0591C180009 for ; Mon, 7 Nov 2022 16:50:06 +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 37B2A611B9; Mon, 7 Nov 2022 16:50:06 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7D3E2C4347C; Mon, 7 Nov 2022 16:50:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1667839806; bh=9HGMZvb82sypIRoPvUis/kF/0CFkMhQKBGHkFijQToc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=nPectMM1/5QJ1zmK7cZ7bFJgbJp7443q6YznAqCicO+0SL7aIL9Ou2v5xPpXfmSAr 0t3jEcSv68Hquf8wJKa8BFtg1tdZKx+GZV+3JQksHxTobX2UIy36CKcbe8WB6mSjHX LVA0JKqVZ7dGy7I5p6DnZ+XgB1Y9bUONIy2kwYw0Z5WNEng7FgZzKdSsSjdNP7AeyE HAwkI+upl+9Z3udy7DsKPi72rtgbiRJ4XxS1eDPBrlIch1b3WYrELHSZ/i7k5X3yLV vvV0sSmOfuqImb6yC++5lvOQ338tCW1j8ooYAfkAQRvzrhqoxYrCDMdoXC1XvfP21k QbWp0n7zkh3Kg== 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 v2 2/2] selftests/damon: test non-context inputs to rm_contexts file Date: Mon, 7 Nov 2022 16:50:01 +0000 Message-Id: <20221107165001.5717-3-sj@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221107165001.5717-1-sj@kernel.org> References: <20221107165001.5717-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=1667839807; 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=sBFOecAFn0Fu+hT/JLgR9APFCVukIBe0Drg8l5+kt5HgjuqrkDrvicS/iyPiRnUdV73hxB EqN75DvoLwg3XA2v2WhTMDd6A/D0eJuvw/yImX2eg/TCHC0wTWNDMTUfoZN9kuZtg5WkvW bNAkWgKQYLCquQCm1tKBYqSSqIdXkxg= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=nPectMM1; spf=pass (imf06.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=1667839807; a=rsa-sha256; cv=none; b=CzZdcwRPuNYWc4mrTXsAXhBhjhi7Zy0GiUMKNhbeSR6AOZjYiEvMyOlJM71ZeNjZkr7sxo wXcHuusZXP26mzRioadA1ocUFmR5dKfgbTzhpTy/e0/WFfU58kUDKA1MH9F+hcV/hLXWUh fa3C72w0fmVYRrbyqIwcLcvlXLrH+mY= X-Rspam-User: X-Stat-Signature: iymkicdrhqw1qfwe1eyeybjc46fh6bcx X-Rspamd-Queue-Id: 0591C180009 Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=nPectMM1; spf=pass (imf06.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-Rspamd-Server: rspam07 X-HE-Tag: 1667839806-666285 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000001, 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