Received: by 2002:a19:f614:0:0:0:0:0 with SMTP id x20csp61117lfe; Fri, 15 Apr 2022 19:32:25 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz4s+X2u8o+ZVXT8ijscJK0/7IS7VNkf+CT+2RyeXB9fC++BHxnl0hqSVxgllWXXRLNG/DN X-Received: by 2002:a17:90a:e692:b0:1cb:a04d:eb8c with SMTP id s18-20020a17090ae69200b001cba04deb8cmr7138829pjy.27.1650076345710; Fri, 15 Apr 2022 19:32:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1650076345; cv=none; d=google.com; s=arc-20160816; b=JEKE7sjIH43CcusynlXavqOI0olKOkSLeTTBnNT9RKeGQIbdQAjHXY/kndgUbaw1fQ xVmKiOo85/MC1KLAlTQIgWqhKp2/k8aF9zDEObK8fNdKZkiMzZc7wn/f6BCAUYGLJzZV 211+cNg3LAQGeRzARdFLBDuckEUhwrbNY0ciltswTzutc5RfQNti+0IJnBNFXhD2Ap/A T5rK4FF73pUFuTIByaTSyTHDIehD11GAwbi42WEEEMB4bpbUrZdkU4W9uWNliJOJUMZV ApeTnRWVvdZcobR5DnCHHkDPbXtr4YJnOkIlSIfBnG+LAzefAbgBdwHM7GJcSZDJdLrC evcg== 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=3CswyzU/Z/+wKnWtBat5NGw1PFeOgnQm7teRh1PGRYw=; b=AmhpmOBATVxcSsjTRjWPRbB7S3o8vRgvyuFFk7g4XGf4qb+9Iq8wvC/JsNCn1krUU8 BPrPIZacNw4Ci/eC87JeoT8cEYXhzB/bNKIKUW5hYPRvqaR1DLRpKzhnpRtAFNZrnvKY nCdm+m+3G8XkA8OEyrUM0mjWwCqUSM8fEmesXpu5es2/XBo/ZqJ319CdOmGr27Yz7e54 pyau98KE+lAy9v+l6Lrf6bGVtDDS+vkn9Ekgx9UJ9hx3ln5Cyob8Nv1qnXAP2Laq0GVK 3zV075Zd9YFkHvVPnFMdhCv1tumlqrKj1jDbrpNjmocNLctWR3ux0lSrpKLfnQ7gSEH9 D+Tg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=fQo86cFk; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id q12-20020a170902dacc00b00155e8c68779si2519233plx.601.2022.04.15.19.32.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Apr 2022 19:32:25 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=fQo86cFk; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id BB26F100916; Fri, 15 Apr 2022 18:42:16 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243842AbiDNNTf (ORCPT + 99 others); Thu, 14 Apr 2022 09:19:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38386 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243775AbiDNNSc (ORCPT ); Thu, 14 Apr 2022 09:18:32 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 232A892876; Thu, 14 Apr 2022 06:15:51 -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 dfw.source.kernel.org (Postfix) with ESMTPS id A643C612D6; Thu, 14 Apr 2022 13:15:50 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B0AEEC385A9; Thu, 14 Apr 2022 13:15:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1649942150; bh=UyDGyEQHkN4jo5VfUZ5gpnpySdseI/gx5qNUCF+oui0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=fQo86cFkK/lNaEax0BpkK8mPByP0YHJgFkWSvBJfpb5ecHR/EDKU5E/m94wL5cbpq GlMBnmD1/LjBgD6hw2vtnJ7dbTNOvVViql/7i5tJo71NqtpMqDksewN14ugQDXoJGi sIPhz4GHbjmywTvziuurUSyvjFxF9Ao71ZR7KXto= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Hulk Robot , Baokun Li , Richard Weinberger Subject: [PATCH 4.19 033/338] jffs2: fix use-after-free in jffs2_clear_xattr_subsystem Date: Thu, 14 Apr 2022 15:08:56 +0200 Message-Id: <20220414110839.835657525@linuxfoundation.org> X-Mailer: git-send-email 2.35.2 In-Reply-To: <20220414110838.883074566@linuxfoundation.org> References: <20220414110838.883074566@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no 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 From: Baokun Li commit 4c7c44ee1650677fbe89d86edbad9497b7679b5c upstream. When we mount a jffs2 image, assume that the first few blocks of the image are normal and contain at least one xattr-related inode, but the next block is abnormal. As a result, an error is returned in jffs2_scan_eraseblock(). jffs2_clear_xattr_subsystem() is then called in jffs2_build_filesystem() and then again in jffs2_do_fill_super(). Finally we can observe the following report: ================================================================== BUG: KASAN: use-after-free in jffs2_clear_xattr_subsystem+0x95/0x6ac Read of size 8 at addr ffff8881243384e0 by task mount/719 Call Trace: dump_stack+0x115/0x16b jffs2_clear_xattr_subsystem+0x95/0x6ac jffs2_do_fill_super+0x84f/0xc30 jffs2_fill_super+0x2ea/0x4c0 mtd_get_sb+0x254/0x400 mtd_get_sb_by_nr+0x4f/0xd0 get_tree_mtd+0x498/0x840 jffs2_get_tree+0x25/0x30 vfs_get_tree+0x8d/0x2e0 path_mount+0x50f/0x1e50 do_mount+0x107/0x130 __se_sys_mount+0x1c5/0x2f0 __x64_sys_mount+0xc7/0x160 do_syscall_64+0x45/0x70 entry_SYSCALL_64_after_hwframe+0x44/0xa9 Allocated by task 719: kasan_save_stack+0x23/0x60 __kasan_kmalloc.constprop.0+0x10b/0x120 kasan_slab_alloc+0x12/0x20 kmem_cache_alloc+0x1c0/0x870 jffs2_alloc_xattr_ref+0x2f/0xa0 jffs2_scan_medium.cold+0x3713/0x4794 jffs2_do_mount_fs.cold+0xa7/0x2253 jffs2_do_fill_super+0x383/0xc30 jffs2_fill_super+0x2ea/0x4c0 [...] Freed by task 719: kmem_cache_free+0xcc/0x7b0 jffs2_free_xattr_ref+0x78/0x98 jffs2_clear_xattr_subsystem+0xa1/0x6ac jffs2_do_mount_fs.cold+0x5e6/0x2253 jffs2_do_fill_super+0x383/0xc30 jffs2_fill_super+0x2ea/0x4c0 [...] The buggy address belongs to the object at ffff8881243384b8 which belongs to the cache jffs2_xattr_ref of size 48 The buggy address is located 40 bytes inside of 48-byte region [ffff8881243384b8, ffff8881243384e8) [...] ================================================================== The triggering of the BUG is shown in the following stack: ----------------------------------------------------------- jffs2_fill_super jffs2_do_fill_super jffs2_do_mount_fs jffs2_build_filesystem jffs2_scan_medium jffs2_scan_eraseblock <--- ERROR jffs2_clear_xattr_subsystem <--- free jffs2_clear_xattr_subsystem <--- free again ----------------------------------------------------------- An error is returned in jffs2_do_mount_fs(). If the error is returned by jffs2_sum_init(), the jffs2_clear_xattr_subsystem() does not need to be executed. If the error is returned by jffs2_build_filesystem(), the jffs2_clear_xattr_subsystem() also does not need to be executed again. So move jffs2_clear_xattr_subsystem() from 'out_inohash' to 'out_root' to fix this UAF problem. Fixes: aa98d7cf59b5 ("[JFFS2][XATTR] XATTR support on JFFS2 (version. 5)") Cc: stable@vger.kernel.org Reported-by: Hulk Robot Signed-off-by: Baokun Li Signed-off-by: Richard Weinberger Signed-off-by: Greg Kroah-Hartman --- fs/jffs2/fs.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) --- a/fs/jffs2/fs.c +++ b/fs/jffs2/fs.c @@ -597,8 +597,8 @@ out_root: jffs2_free_ino_caches(c); jffs2_free_raw_node_refs(c); kvfree(c->blocks); - out_inohash: jffs2_clear_xattr_subsystem(c); + out_inohash: kfree(c->inocache_list); out_wbuf: jffs2_flash_cleanup(c);