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: 16572 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp2167643wru; Mon, 7 Nov 2022 08:53:25 -0800 (PST) X-Google-Smtp-Source: AMsMyM569A3Zp5tcwvj8jpaLREvuoCcSw0KuCNfCClOXBXRAAJwRbsT+y7WuDdkMws2dd42Ejujc X-Received: by 2002:a17:906:5a6a:b0:7a6:bad5:8295 with SMTP id my42-20020a1709065a6a00b007a6bad58295mr49782340ejc.647.1667840004867; Mon, 07 Nov 2022 08:53:24 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1667840004; cv=none; d=google.com; s=arc-20160816; b=oNLRf1CXuRPupYhbik7HhnBTiwEAZ9KSSNeoo+l92N+501Df2Up++tpxmBecPS+PIl xyOVNaeLfF8DgiwkZVkZzUlIIpwg0DMX8bHKy/lTko9g2L+nUedvuWaFj4Zwz6UItY52 InAjeKAXrCncwQ82LS/u9PbtmHxGoj70SE1AlOEcvKaBDQpO0DJJ92t2XPz55cPES+dV gEgxb3gA4jWWfSMffR7pwqDP4kNZtW6xaCM6mpvQp5DJQ2DAStCDxF5p/edFB08cEfdS AVQcJtx0eZej3GYtyPbrpeZYPI7zaw2RLSHIPepznLL6kDWAu62TT2DYuTMSEpGu7TDO L7Hw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=7qQHlhYX+JB1mXiX1LgdSYUnzilRLEh6XL7ftEcmRp0=; b=TmL9ED+vym1M/Rc219o00Hkm/wAgHn7ijTLzfh3QE2exIlpX+fxIESX4dYa5koS967 O4liSn8Du4A/L0E8+vj3EYLB8fJKR2tJQPYIdpj0MENZlUh2PNOZqenW0ozol+YCSnOP GMqg+0OF3LAvKysz3rNvSAJKdWzcit2vFTpoFktTBnoJlmZD/6oXl/pN59ZwNbmS5yIe 2qmQ1VI1HDNH9EM8Ztbr2P24Ig2P3+bLrB/15TEnaBvYPZ0jO1p490Drey0fmffP1nZY tUOJVFWAa+4lanDu65PUhDQDbD/PF+W2np1dvDbYFDLUrTY9Q2lchRRLawXiwVSitqem 2Y7Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Hz8K1E5g; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id hk20-20020a170906c9d400b00730a4246dd0si7636020ejb.593.2022.11.07.08.53.00; Mon, 07 Nov 2022 08:53:24 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Hz8K1E5g; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232827AbiKGQu0 (ORCPT + 99 others); Mon, 7 Nov 2022 11:50:26 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43396 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232664AbiKGQuG (ORCPT ); Mon, 7 Nov 2022 11:50:06 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5893C193DE; Mon, 7 Nov 2022 08:50:06 -0800 (PST) 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 D34A3611CC; 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 X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1748857001101361643?= X-GMAIL-MSGID: =?utf-8?q?1748857001101361643?= 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: 16575 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp2168158wru; Mon, 7 Nov 2022 08:54:30 -0800 (PST) X-Google-Smtp-Source: AMsMyM4blUiR4hKxBR7sZdXZM1fQ9/WJN6Jd7TtKw/+/PNSOTufEX9cqdxNuSXhOsb75ndV9PvZJ X-Received: by 2002:a62:5e06:0:b0:56b:e3f8:824f with SMTP id s6-20020a625e06000000b0056be3f8824fmr51692003pfb.84.1667840069944; Mon, 07 Nov 2022 08:54:29 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1667840069; cv=none; d=google.com; s=arc-20160816; b=GTZ1OtORCWvRuOwQcAn0nDUaqRWaOsSr93UBeROFQ/fjIqKoaPWSu8uH3qlcDZ5h70 Nl5r6OyMyIthPCUFQCUQMeoZGSkcXAgplpyijdXbMX/o9a6+nSKd/7yRINKSjH0WzUJy ebtJpmRvGw++Mip9/W5KEIeRcYMfltOBmR4uWfWQVopyk+tmEeZ1kUJVO4cv7kGUTAHF t5GhDY2BjXBTNdng8eUDaSoyzvQLY782dCDtmEfDKifcRd1wmH9VyPIodtEg7lhX0Bnl Z0Zt+Zi5gg+y4IuEbQlKqySQypdltvCeGPgIOFBYJUce3oKCK7pCnvefbSrfwWj6POOq dqzA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=VgubQM217VrfKa24NsReiE9VfnfZxpk1OkuE3vLK5tY=; b=DtJJcRVaNXpUobfsYpw/oJ5igGNJ1rQj6jTZgxV1zj8UV9QZxgwXL+yZKH7TU3YIWu GxDvmnKYI0Rl10Dhu4STQCFhz+y47JfJzE5+w4Pl2GLpgC9h+j/thyAt802cl34ZtiFD Jf0oQeJ5EOac4xoEoJw2hTifGEZhz23CB8wzygxQHeyke1eabz544JwpLKmpYc9nmkNO s5cx3B7zzqHwdWpVTeHwnQqlyLUqDgjhC7GZrpdtgsK75lTjHXCt9CN2cmI+mJqCE3m0 sVjlOCHLwbkfPOj3AaAPhSkdoyheQ4SBxOxZFmRFaWheTecYhN9R0iObP672Vfee2sFR F2dA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=nPectMM1; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id b5-20020a056a000a8500b0056e4c95266bsi11406197pfl.77.2022.11.07.08.54.17; Mon, 07 Nov 2022 08:54:29 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=nPectMM1; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232856AbiKGQuc (ORCPT + 99 others); Mon, 7 Nov 2022 11:50:32 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43380 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232690AbiKGQuJ (ORCPT ); Mon, 7 Nov 2022 11:50:09 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A226E20BFE; Mon, 7 Nov 2022 08:50:08 -0800 (PST) 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 5ED08B815D5; Mon, 7 Nov 2022 16:50:07 +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 X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1748857069110768160?= X-GMAIL-MSGID: =?utf-8?q?1748857069110768160?= 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