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: 13404 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp2474058wru; Mon, 31 Oct 2022 11:31:54 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4Ayxt5ranALw/8LZAweusphgthGrJSRmt/JhWL8N2lEtKQCXKmjQqC8F/GxF4OZuQ9A1B8 X-Received: by 2002:a17:907:97d5:b0:7ac:5f72:6c1a with SMTP id js21-20020a17090797d500b007ac5f726c1amr14208670ejc.126.1667241114293; Mon, 31 Oct 2022 11:31:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667241114; cv=none; d=google.com; s=arc-20160816; b=hqndYo0FbHldQiMskmN1RXyYRlbKtCXbNQEH2+xayTVFWbr1Tmg+FwblGWJNVpVvF4 2bFg3ztfmHTKSCJXLpBNOUwd7M1SIQj66WpLfVV/m+ESi02ROvnRtKdxzNfWb2hr/hde zWMOv0MrkaNkwQfAGpSwRNZeNi7if+3nYYy5ndD0Dxld+v9mM8CLy0+/mhfhhU6y0lFk KDfM7UZDlXUTfgruRHzXGEHWkmeFa1SbwyY2iSVJqn+pW+9qWRddy3vW5h7XQ7u6ofDK 8LCoAL2+VTxYJjM75uQwylXk9kn9lxwK7qhZkIJc77kbzPGhAGnknP4dOplzjF+EPDWn qmPg== 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=vWBoUqkuRMGUhCI0jm+dgPQqGz9kCKypybcybDdX7A1jgFHCPlbMFOke8FWc/xrTsc lDoaAao3saUE7YsF0dKgx9O5Vao+Bjd0rXYQuhma+rI3oOlPsFT5nAXHscghh28gUwio 6FsKUNzGAOcsGByApbzGg3em4XJuZ795tiBECXuJ/3DrmdAAdrIaaGjZiVeDP+nGh7o8 4eLBREJmYSLna2NFtQRnR+PRbo702CY/M7Q069kbrOplway0EVAxGr/dGmVtYqXWgqBy u/qm5tQSj2YqvOk51QiMbeT6w1ThN7oA1XHuZbZo5caPb3G61I6Lb8oL6ACHQvh6Tsuk DX8A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=YrNr3rYc; 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 g16-20020a1709065d1000b007ade14a74a7si1638129ejt.90.2022.10.31.11.31.29; Mon, 31 Oct 2022 11:31:54 -0700 (PDT) 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=YrNr3rYc; 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 S230055AbiJaS0O (ORCPT + 99 others); Mon, 31 Oct 2022 14:26:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57340 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230007AbiJaS0L (ORCPT ); Mon, 31 Oct 2022 14:26:11 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C8AF112AFF; Mon, 31 Oct 2022 11:26:06 -0700 (PDT) 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 72A40B819EB; Mon, 31 Oct 2022 18:26:05 +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 X-Spam-Status: No, score=-8.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?1748229018525324714?= X-GMAIL-MSGID: =?utf-8?q?1748229018525324714?= 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: 13405 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp2474632wru; Mon, 31 Oct 2022 11:33:03 -0700 (PDT) X-Google-Smtp-Source: AMsMyM53UKx0SLHts4mjANu9oNvIy6dTMBIBcO/eFKNEhe8t9A9mQWdkhRxGPjS4bF5Xs2/Zl3EZ X-Received: by 2002:a50:bb06:0:b0:461:4acc:4540 with SMTP id y6-20020a50bb06000000b004614acc4540mr15600100ede.307.1667241182876; Mon, 31 Oct 2022 11:33:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667241182; cv=none; d=google.com; s=arc-20160816; b=MGCiK8wbUnLg/Q4LXvv7M7no1QIDjfHS/URsr9ZoAqgvZXId86A1mrxt2ykf7Qi2ok eOZ8ujM2OiJKla3rxzgYZlyp5U1QetKI9u4gl1r5Zvrkq+NULABheBUpNC2ThxfHfFGz v55Oc85IQnY8rdhXaTJKwZ/ITQoFd/hbdHII+vdShk/6fQh/gs2LQGsA3ZETfLN+0RhP txNZx/CH7g3qb63Lbc+CGq568ztoKFBw6MAXtUBhE7+3MYFQgYldYbWKfyPKomzs51MF isCgD/4sAQMMznsKVg6V9PkFUkKi4JWZH+JgTi49x2ClUJht/P2FeeYIk4nNnTTDBKrM uc0g== 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=VqxqfBOLtf7axjpeFq3fv/nVQuS2WD3+ULoHD1FG6BASdwsj1hvEnkt0hekhes9mSr j4HOWEPhvuW25TmzluISwrI7DFsEW2TaWxeEBOeCEsenTMc7vP6//7nt/NhJ9lNFPvvA /Fo7vsJAp+K7kv5HFi6IqVG2Mq818WupFqQho312VSQ43TzkyAd32sS3/S479o/dGmnc 2UJpWnpeURhagS3RXVTk/SLzSHezwCc7xyyC8rKVRPmxutBgC5SoyZ913Xl+whakyQ1V 9QdaI1Qrb19uENO1zl0OLggfBi/HZYy9E/muPs8Ga380hD3Jq/zvSiaLl2UEdN6XeiX5 MhrA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=gT3GFkLL; 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 hq28-20020a1709073f1c00b0078db3f08a6bsi9785997ejc.720.2022.10.31.11.32.38; Mon, 31 Oct 2022 11:33:02 -0700 (PDT) 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=gT3GFkLL; 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 S230077AbiJaS0Q (ORCPT + 99 others); Mon, 31 Oct 2022 14:26:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57344 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230013AbiJaS0L (ORCPT ); Mon, 31 Oct 2022 14:26:11 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 98C4813D51; Mon, 31 Oct 2022 11:26:07 -0700 (PDT) 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 41BB3B815FB; Mon, 31 Oct 2022 18:26:06 +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 X-Spam-Status: No, score=-8.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?1748229090594729999?= X-GMAIL-MSGID: =?utf-8?q?1748229090594729999?= 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