Received: by 2002:a5d:9c59:0:0:0:0:0 with SMTP id 25csp54733iof; Sun, 5 Jun 2022 20:55:53 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz84/4GokE/YepRrPBLujTMpFsEhTjPf76xgI6FY1yLLKEsI+g4i1y6Om3l5MCmHndwLOjb X-Received: by 2002:a63:1209:0:b0:3fc:e453:5420 with SMTP id h9-20020a631209000000b003fce4535420mr17545561pgl.189.1654487753218; Sun, 05 Jun 2022 20:55:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654487753; cv=none; d=google.com; s=arc-20160816; b=TDnAGgNq2cHNLPqUU5IvnHyuKfenvaMPlHBP75ejIOIXOxDHxIq0Rkm+Oj2TWOBvpg aS4zV2T3yBBnCs8n/jl5rDHQX3/fwCy61JKxcoRxrwt10qAEGGMICEyqOMlCBfF9nTvN 76tIoFcmpqIwppgyiPkoWDQ/SQ0VFPMVorljRaRHHu9QdQLR3bqlGmgaoPpMENo7DUfc GrZCZZJesDVHnIY30dxJFkErF4xXSqFnuKxQeHL08OHbDXvoLh6NJKCgSAEkwGrcPvU8 JPXiXSYIy4v4bPYtDaipZSK7zXEkJIbf5ucRF8J/BjQaZZQRHczMMOuumEZvBktNhMNi fneQ== 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=AQv4veeWEZHbrv3d5PONrK982TRbo26rSsLGng3e0f0=; b=AbPj7Jf9Cln6vsBnYQHdX67/lYCqzg9fpKaoL//AijasxDTurPHe2nPEN2g0USOkem z2vP523LEH1j70dDom97qnlxrI1n3bLWCZVchqHk3ZoBa6gFy6DmtdZ8UY/YtFirMasi J0jj/jj7kFVClwvM6EbC2sG4f/7B3u41ahjF8noNucGkCSYjkaq8XKvFxjJrMh+wQ3a2 hD9s8C+iN/QaTNknWxZxoYU937MDHiztuS4xyq95uQg4/NbjCRV4sjeTpI4J1ph6wKZQ TGGhwcrIZUdrdCnnk/TNWjX+hauyL7qNfHm0a+1IZmJOFDjOf0he95zBfkwLJfwbIgiO hY/w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=ZT2XqWWl; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1: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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id g132-20020a636b8a000000b003fcc510ca4dsi15171697pgc.715.2022.06.05.20.55.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 05 Jun 2022 20:55:53 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=ZT2XqWWl; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1: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: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id AED52633B5; Sun, 5 Jun 2022 20:44:01 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231241AbiFCR5x (ORCPT + 99 others); Fri, 3 Jun 2022 13:57:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58038 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346700AbiFCRv1 (ORCPT ); Fri, 3 Jun 2022 13:51:27 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BB834544D9; Fri, 3 Jun 2022 10:49:14 -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 4AB8160F4E; Fri, 3 Jun 2022 17:49:14 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4B3FBC385A9; Fri, 3 Jun 2022 17:49:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1654278553; bh=ANH8V7vwcVhdW15W3pPGiL3w0hAGuSIPd+qqSu0L72w=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ZT2XqWWlNekzawtn7nLvsfJNuUvTJmKKK2PZbO0ekadarmzIqWma7xh3Xngr3kXY8 +ulyFicBtC4Mu6DXkvJWgBAKG0UZsrK4F4LDPIhQxgRK7LHowRf8NPKwY41nJmDpKm uLtFq3EaBGkctYFNH2UChFWQtknuVI7SMe2k7jas= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, syzbot+a4087e40b9c13aad7892@syzkaller.appspotmail.com, Tadeusz Struk , Sungjong Seo , Namjae Jeon Subject: [PATCH 5.15 17/66] exfat: check if cluster num is valid Date: Fri, 3 Jun 2022 19:42:57 +0200 Message-Id: <20220603173821.160226274@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220603173820.663747061@linuxfoundation.org> References: <20220603173820.663747061@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=-3.1 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=unavailable 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: Tadeusz Struk commit 64ba4b15e5c045f8b746c6da5fc9be9a6b00b61d upstream. Syzbot reported slab-out-of-bounds read in exfat_clear_bitmap. This was triggered by reproducer calling truncute with size 0, which causes the following trace: BUG: KASAN: slab-out-of-bounds in exfat_clear_bitmap+0x147/0x490 fs/exfat/balloc.c:174 Read of size 8 at addr ffff888115aa9508 by task syz-executor251/365 Call Trace: __dump_stack lib/dump_stack.c:77 [inline] dump_stack_lvl+0x1e2/0x24b lib/dump_stack.c:118 print_address_description+0x81/0x3c0 mm/kasan/report.c:233 __kasan_report mm/kasan/report.c:419 [inline] kasan_report+0x1a4/0x1f0 mm/kasan/report.c:436 __asan_report_load8_noabort+0x14/0x20 mm/kasan/report_generic.c:309 exfat_clear_bitmap+0x147/0x490 fs/exfat/balloc.c:174 exfat_free_cluster+0x25a/0x4a0 fs/exfat/fatent.c:181 __exfat_truncate+0x99e/0xe00 fs/exfat/file.c:217 exfat_truncate+0x11b/0x4f0 fs/exfat/file.c:243 exfat_setattr+0xa03/0xd40 fs/exfat/file.c:339 notify_change+0xb76/0xe10 fs/attr.c:336 do_truncate+0x1ea/0x2d0 fs/open.c:65 Move the is_valid_cluster() helper from fatent.c to a common header to make it reusable in other *.c files. And add is_valid_cluster() to validate if cluster number is within valid range in exfat_clear_bitmap() and exfat_set_bitmap(). Link: https://syzkaller.appspot.com/bug?id=50381fc73821ecae743b8cf24b4c9a04776f767c Reported-by: syzbot+a4087e40b9c13aad7892@syzkaller.appspotmail.com Fixes: 1e49a94cf707 ("exfat: add bitmap operations") Cc: stable@vger.kernel.org # v5.7+ Signed-off-by: Tadeusz Struk Reviewed-by: Sungjong Seo Signed-off-by: Namjae Jeon Signed-off-by: Greg Kroah-Hartman --- fs/exfat/balloc.c | 8 ++++++-- fs/exfat/exfat_fs.h | 8 ++++++++ fs/exfat/fatent.c | 8 -------- 3 files changed, 14 insertions(+), 10 deletions(-) --- a/fs/exfat/balloc.c +++ b/fs/exfat/balloc.c @@ -148,7 +148,9 @@ int exfat_set_bitmap(struct inode *inode struct super_block *sb = inode->i_sb; struct exfat_sb_info *sbi = EXFAT_SB(sb); - WARN_ON(clu < EXFAT_FIRST_CLUSTER); + if (!is_valid_cluster(sbi, clu)) + return -EINVAL; + ent_idx = CLUSTER_TO_BITMAP_ENT(clu); i = BITMAP_OFFSET_SECTOR_INDEX(sb, ent_idx); b = BITMAP_OFFSET_BIT_IN_SECTOR(sb, ent_idx); @@ -166,7 +168,9 @@ void exfat_clear_bitmap(struct inode *in struct exfat_sb_info *sbi = EXFAT_SB(sb); struct exfat_mount_options *opts = &sbi->options; - WARN_ON(clu < EXFAT_FIRST_CLUSTER); + if (!is_valid_cluster(sbi, clu)) + return; + ent_idx = CLUSTER_TO_BITMAP_ENT(clu); i = BITMAP_OFFSET_SECTOR_INDEX(sb, ent_idx); b = BITMAP_OFFSET_BIT_IN_SECTOR(sb, ent_idx); --- a/fs/exfat/exfat_fs.h +++ b/fs/exfat/exfat_fs.h @@ -381,6 +381,14 @@ static inline int exfat_sector_to_cluste EXFAT_RESERVED_CLUSTERS; } +static inline bool is_valid_cluster(struct exfat_sb_info *sbi, + unsigned int clus) +{ + if (clus < EXFAT_FIRST_CLUSTER || sbi->num_clusters <= clus) + return false; + return true; +} + /* super.c */ int exfat_set_volume_dirty(struct super_block *sb); int exfat_clear_volume_dirty(struct super_block *sb); --- a/fs/exfat/fatent.c +++ b/fs/exfat/fatent.c @@ -81,14 +81,6 @@ int exfat_ent_set(struct super_block *sb return 0; } -static inline bool is_valid_cluster(struct exfat_sb_info *sbi, - unsigned int clus) -{ - if (clus < EXFAT_FIRST_CLUSTER || sbi->num_clusters <= clus) - return false; - return true; -} - int exfat_ent_get(struct super_block *sb, unsigned int loc, unsigned int *content) {