Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp3525573pxf; Mon, 15 Mar 2021 11:30:14 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzKUGNDDVdezDrXZEVQubI0gtWC3oA7lzwkoeEdGOP1vvCb24rjaAoEdAKZYfpSYDuXlULH X-Received: by 2002:a05:6402:4309:: with SMTP id m9mr32533001edc.25.1615833014471; Mon, 15 Mar 2021 11:30:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1615833014; cv=none; d=google.com; s=arc-20160816; b=qpveqwqvQUzH9amQsuZN30p7/NiblX932iF5ksZwaTeIbqN1Xa4PSOu6P49/S/OtYV MHkBSp68qtzA71K8w9gU9BgM98FZ5UEWmnai8IyfujHyav0bi4E+BDm/1YRhgz2hHlJx LTJCnmQEqbYYHKPcuw0GF/zBWQy2+Uo584EqPWeAl6/UfX01L74VHh3ExoR1+TTazSLs VdPpJ9uB4nvx6u+UA8wkkGgVknt4d+cXMsi0y1bwCNgFc/uMMqSz07dRx7RUOg6FJ+is EdY+uefrR5QY4B2BH26zlUnSsJ+suMRjqUXmQygootZ4MnT01SkGXaRqbmW8vZ94rx09 DPvA== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=j3tlUpa/kRQBS3fW0aF+Xcw6Z8+WdunZG+fLUL7Y4Ss=; b=ta7a2hRE8Ye16Opn+GPJlMI+nI4s/MOTW1ws6/AZFFWxE3Lh872/hXD/Z9arSzDcwx vk/T9kXddg342fPW3K6r9kT5aTv/WAeMCg2zpxFr8jD/JoFuYsC6rIAcUvxnYeDEKWJQ 9LmEqhisNO3HnIbzwIX9g/7w/AlFopk464KcQmA2AQz24IMIGwV2T1dt/cFVZO4Fr0LO WfifzvDeSKMcbn0SdI79ltIdGLPVXg0kMmDcMasZTCUodXSDUg2xFCwq6K6xtL5/D6Dl Q+QCQy1LDY01lkSIjKyG8WcSMZc7Qsy5KpqhKHDIpMGEqWsrYOPTEUGrG2JsbIQ3XmOl 3pXw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=jYkDzg4J; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id r11si11016597eja.563.2021.03.15.11.29.52; Mon, 15 Mar 2021 11:30:14 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=jYkDzg4J; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239545AbhCOOcl (ORCPT + 99 others); Mon, 15 Mar 2021 10:32:41 -0400 Received: from mail.kernel.org ([198.145.29.99]:37612 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233232AbhCOOBM (ORCPT ); Mon, 15 Mar 2021 10:01:12 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id BF88D64F44; Mon, 15 Mar 2021 14:00:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1615816847; bh=5TmW79MX+sQntVj+pmMj8FlTzr7FF3Iz9vc3g/jETvE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=jYkDzg4J2DbiGboDHvNzQ6J3DfPr1PmNeL8rERFxiOHTKOH5+tVb6RVqO5kbQ0nL+ G1xKA7dcuLgztoOgvIrhL/w1mu8aRUmTcXFmCZvHJKPG1idTBCG1LPQjB02NsiIbCo PTvQljfvS/c+UWMP4zVGanAd/Qlvwr0EDmF9lFCQ= From: gregkh@linuxfoundation.org To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Daiyue Zhang , Yi Chen , Ge Qiu , Chao Yu , Al Viro , Christoph Hellwig , Sasha Levin Subject: [PATCH 5.4 150/168] configfs: fix a use-after-free in __configfs_open_file Date: Mon, 15 Mar 2021 14:56:22 +0100 Message-Id: <20210315135555.275834050@linuxfoundation.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210315135550.333963635@linuxfoundation.org> References: <20210315135550.333963635@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Greg Kroah-Hartman From: Daiyue Zhang [ Upstream commit 14fbbc8297728e880070f7b077b3301a8c698ef9 ] Commit b0841eefd969 ("configfs: provide exclusion between IO and removals") uses ->frag_dead to mark the fragment state, thus no bothering with extra refcount on config_item when opening a file. The configfs_get_config_item was removed in __configfs_open_file, but not with config_item_put. So the refcount on config_item will lost its balance, causing use-after-free issues in some occasions like this: Test: 1. Mount configfs on /config with read-only items: drwxrwx--- 289 root root 0 2021-04-01 11:55 /config drwxr-xr-x 2 root root 0 2021-04-01 11:54 /config/a --w--w--w- 1 root root 4096 2021-04-01 11:53 /config/a/1.txt ...... 2. Then run: for file in /config do echo $file grep -R 'key' $file done 3. __configfs_open_file will be called in parallel, the first one got called will do: if (file->f_mode & FMODE_READ) { if (!(inode->i_mode & S_IRUGO)) goto out_put_module; config_item_put(buffer->item); kref_put() package_details_release() kfree() the other one will run into use-after-free issues like this: BUG: KASAN: use-after-free in __configfs_open_file+0x1bc/0x3b0 Read of size 8 at addr fffffff155f02480 by task grep/13096 CPU: 0 PID: 13096 Comm: grep VIP: 00 Tainted: G W 4.14.116-kasan #1 TGID: 13096 Comm: grep Call trace: dump_stack+0x118/0x160 kasan_report+0x22c/0x294 __asan_load8+0x80/0x88 __configfs_open_file+0x1bc/0x3b0 configfs_open_file+0x28/0x34 do_dentry_open+0x2cc/0x5c0 vfs_open+0x80/0xe0 path_openat+0xd8c/0x2988 do_filp_open+0x1c4/0x2fc do_sys_open+0x23c/0x404 SyS_openat+0x38/0x48 Allocated by task 2138: kasan_kmalloc+0xe0/0x1ac kmem_cache_alloc_trace+0x334/0x394 packages_make_item+0x4c/0x180 configfs_mkdir+0x358/0x740 vfs_mkdir2+0x1bc/0x2e8 SyS_mkdirat+0x154/0x23c el0_svc_naked+0x34/0x38 Freed by task 13096: kasan_slab_free+0xb8/0x194 kfree+0x13c/0x910 package_details_release+0x524/0x56c kref_put+0xc4/0x104 config_item_put+0x24/0x34 __configfs_open_file+0x35c/0x3b0 configfs_open_file+0x28/0x34 do_dentry_open+0x2cc/0x5c0 vfs_open+0x80/0xe0 path_openat+0xd8c/0x2988 do_filp_open+0x1c4/0x2fc do_sys_open+0x23c/0x404 SyS_openat+0x38/0x48 el0_svc_naked+0x34/0x38 To fix this issue, remove the config_item_put in __configfs_open_file to balance the refcount of config_item. Fixes: b0841eefd969 ("configfs: provide exclusion between IO and removals") Signed-off-by: Daiyue Zhang Signed-off-by: Yi Chen Signed-off-by: Ge Qiu Reviewed-by: Chao Yu Acked-by: Al Viro Signed-off-by: Christoph Hellwig Signed-off-by: Sasha Levin --- fs/configfs/file.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/fs/configfs/file.c b/fs/configfs/file.c index fb65b706cc0d..84b4d58fc65f 100644 --- a/fs/configfs/file.c +++ b/fs/configfs/file.c @@ -378,7 +378,7 @@ static int __configfs_open_file(struct inode *inode, struct file *file, int type attr = to_attr(dentry); if (!attr) - goto out_put_item; + goto out_free_buffer; if (type & CONFIGFS_ITEM_BIN_ATTR) { buffer->bin_attr = to_bin_attr(dentry); @@ -391,7 +391,7 @@ static int __configfs_open_file(struct inode *inode, struct file *file, int type /* Grab the module reference for this attribute if we have one */ error = -ENODEV; if (!try_module_get(buffer->owner)) - goto out_put_item; + goto out_free_buffer; error = -EACCES; if (!buffer->item->ci_type) @@ -435,8 +435,6 @@ static int __configfs_open_file(struct inode *inode, struct file *file, int type out_put_module: module_put(buffer->owner); -out_put_item: - config_item_put(buffer->item); out_free_buffer: up_read(&frag->frag_sem); kfree(buffer); -- 2.30.1