Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp2129107rwb; Mon, 7 Nov 2022 09:27:45 -0800 (PST) X-Google-Smtp-Source: AMsMyM7H3A2az8rMWspBtLrvq1zbWFVe3QZ17WloregXTbok8KemqlAQK8AfGAI27Y0p0isJmJ4I X-Received: by 2002:a17:907:6d25:b0:7aa:f5a4:5f66 with SMTP id sa37-20020a1709076d2500b007aaf5a45f66mr47831535ejc.216.1667842065723; Mon, 07 Nov 2022 09:27:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1667842065; cv=none; d=google.com; s=arc-20160816; b=GZt054DjK4d/U1FxBLXZQGWGRXJbYqbI/EgEN5zGQJgE0pUBfALQh5cDmZMCC38+BL vBb3af6pcmJuTsChARuQoFnrUoL/1amyrV1tqzbnu+Bp+50cp8hBZN59e6rHF3zuYJm3 fih+Yf4nhyI+m4IOzbdPXLKiezKuC2sNbFLloux2pLZM5/Jsd4LMPospI4C/T2tpcMDu uVDubkO1O4Y5cUCylZjaAfVE7BUnKafxOFA57rFnrgCnJGRWvyKL76FJ6OJAvZiaUIbF FCu8h0UPIfypGDVqD2Dhbxu/u0zhBEmm8df7Tk8eBzoNVZygXh1sCrIxplMK1i941QkQ kKNQ== 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id n10-20020a056402060a00b00461a3438b24si8769748edv.182.2022.11.07.09.27.23; Mon, 07 Nov 2022 09:27:45 -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 + 92 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 Content-Transfer-Encoding: 8bit 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 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) { -- 2.25.1