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) {