Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp1828282rwb; Wed, 5 Oct 2022 05:32:25 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6AwbuVXYMaM4yUqbGsQPRjD8BOVuaydEXbztS1LvU14LTtNB5mKLI3eZ80g/qMVyyszFhS X-Received: by 2002:a05:6402:440c:b0:43a:1124:e56a with SMTP id y12-20020a056402440c00b0043a1124e56amr29177220eda.134.1664973145107; Wed, 05 Oct 2022 05:32:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664973145; cv=none; d=google.com; s=arc-20160816; b=hHOZ9eWt9/F0Iy+f7pYPJ+E/5aPVOsxWYXfNeXMSC1EVyaK4ahTVxOAKBycQVvl7dn cW+PcStoteNLk+tiWwAoh07qDh8m3CMsMXAOZk4M9Imiz7fsQLcehydQh3x4zzQm1iZH xgRc60JodOHk66I45V+X0R31hjOElNIce8dHJq5c6L4ws/J8XV08GOb8r+SMXbyAI2yf IRbuY9IEJKAarwKM/XyYVWVJJTf1aAPRQ7+l2YEFrl4g+jMMx1d4Gq45ADA9TIkVns0U LLz6ih1wqGqaoaiaEAOxAhluF57t+k6grI/ug6NVZtVfbBiXUwFmPLwHWKhqww3YuUtF VE4Q== 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=JCZSm+5yH2xFTp7E4uxiGbdE9W+0+a0OKsiZ9spCj0U=; b=WVXrSMl46Bveavfb8rKaiYp0d6xcTRiVlpWH8VdTFenx7LrQqpVzF3ijz/uXNkQaWj y0aKa4cvaXi/Lg9yvY/WciCjC3AuUV0euBXuf2LTQZXqa4z1nb9GYi7gOPumqEMee2QU pqXRZ8rjV/Nx4Ouf+SZ6NIOr0ozagI36GHVVf5aP7FUvCtJB4KzWxiIKcG8THQHRgiiY M8vAo9uIIKCQTN2MLpujHW0YPh+h84+6NHakgVRlV9ebwGe7kjhbpwKv8B3pAxPafE3N DFnFy3nAi0U/9nxDY+OB0yELja+9u02XZ03K1hFwJpfzGfFxaT0F9K0D/raslmSoE3MH 8SmA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=xEOhu5Jg; 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=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id p6-20020a056402500600b0045745ecd5a1si11652625eda.304.2022.10.05.05.31.57; Wed, 05 Oct 2022 05:32:25 -0700 (PDT) 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=@linuxfoundation.org header.s=korg header.b=xEOhu5Jg; 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=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229936AbiJELhr (ORCPT + 99 others); Wed, 5 Oct 2022 07:37:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36570 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229678AbiJELhH (ORCPT ); Wed, 5 Oct 2022 07:37:07 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 154711FCC2; Wed, 5 Oct 2022 04:34:50 -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 ams.source.kernel.org (Postfix) with ESMTPS id 153FBB81DB5; Wed, 5 Oct 2022 11:34:24 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8226AC433C1; Wed, 5 Oct 2022 11:34:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1664969662; bh=UJOpOicRU4NtFi9i6xwfp3NCfWObWtL19QToa62WIt4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=xEOhu5JgFLDG9iHKVfS+DzNMfAtOzXTkTSmL4gTaqmneDpkiHQYHu96DMpo4Opuui XK1Bs7PsShbSIQbyueMzHaqpsWNEN+ETwBFQoKohk0Xjn0uJESN32NQ6R2DM/UfsCx araMO7WAA2t5L1QfvPfLwg8Z+QILgOtHjG7J9Emc= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, "Darrick J. Wong" , Christoph Hellwig , "Darrick J. Wong" , Chandan Babu R Subject: [PATCH 5.4 41/51] xfs: fix s_maxbytes computation on 32-bit kernels Date: Wed, 5 Oct 2022 13:32:29 +0200 Message-Id: <20221005113212.190566979@linuxfoundation.org> X-Mailer: git-send-email 2.38.0 In-Reply-To: <20221005113210.255710920@linuxfoundation.org> References: <20221005113210.255710920@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 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 From: "Darrick J. Wong" commit 932befe39ddea29cf47f4f1dc080d3dba668f0ca upstream. I observed a hang in generic/308 while running fstests on a i686 kernel. The hang occurred when trying to purge the pagecache on a large sparse file that had a page created past MAX_LFS_FILESIZE, which caused an integer overflow in the pagecache xarray and resulted in an infinite loop. I then noticed that Linus changed the definition of MAX_LFS_FILESIZE in commit 0cc3b0ec23ce ("Clarify (and fix) MAX_LFS_FILESIZE macros") so that it is now one page short of the maximum page index on 32-bit kernels. Because the XFS function to compute max offset open-codes the 2005-era MAX_LFS_FILESIZE computation and neither the vfs nor mm perform any sanity checking of s_maxbytes, the code in generic/308 can create a page above the pagecache's limit and kaboom. Fix all this by setting s_maxbytes to MAX_LFS_FILESIZE directly and aborting the mount with a warning if our assumptions ever break. I have no answer for why this seems to have been broken for years and nobody noticed. Signed-off-by: Darrick J. Wong Reviewed-by: Christoph Hellwig Acked-by: Darrick J. Wong Signed-off-by: Chandan Babu R Signed-off-by: Greg Kroah-Hartman --- fs/xfs/xfs_super.c | 48 +++++++++++++++++++++--------------------------- 1 file changed, 21 insertions(+), 27 deletions(-) --- a/fs/xfs/xfs_super.c +++ b/fs/xfs/xfs_super.c @@ -512,32 +512,6 @@ xfs_showargs( seq_puts(m, ",noquota"); } -static uint64_t -xfs_max_file_offset( - unsigned int blockshift) -{ - unsigned int pagefactor = 1; - unsigned int bitshift = BITS_PER_LONG - 1; - - /* Figure out maximum filesize, on Linux this can depend on - * the filesystem blocksize (on 32 bit platforms). - * __block_write_begin does this in an [unsigned] long long... - * page->index << (PAGE_SHIFT - bbits) - * So, for page sized blocks (4K on 32 bit platforms), - * this wraps at around 8Tb (hence MAX_LFS_FILESIZE which is - * (((u64)PAGE_SIZE << (BITS_PER_LONG-1))-1) - * but for smaller blocksizes it is less (bbits = log2 bsize). - */ - -#if BITS_PER_LONG == 32 - ASSERT(sizeof(sector_t) == 8); - pagefactor = PAGE_SIZE; - bitshift = BITS_PER_LONG; -#endif - - return (((uint64_t)pagefactor) << bitshift) - 1; -} - /* * Set parameters for inode allocation heuristics, taking into account * filesystem size and inode32/inode64 mount options; i.e. specifically @@ -1650,6 +1624,26 @@ xfs_fs_fill_super( if (error) goto out_free_sb; + /* + * XFS block mappings use 54 bits to store the logical block offset. + * This should suffice to handle the maximum file size that the VFS + * supports (currently 2^63 bytes on 64-bit and ULONG_MAX << PAGE_SHIFT + * bytes on 32-bit), but as XFS and VFS have gotten the s_maxbytes + * calculation wrong on 32-bit kernels in the past, we'll add a WARN_ON + * to check this assertion. + * + * Avoid integer overflow by comparing the maximum bmbt offset to the + * maximum pagecache offset in units of fs blocks. + */ + if (XFS_B_TO_FSBT(mp, MAX_LFS_FILESIZE) > XFS_MAX_FILEOFF) { + xfs_warn(mp, +"MAX_LFS_FILESIZE block offset (%llu) exceeds extent map maximum (%llu)!", + XFS_B_TO_FSBT(mp, MAX_LFS_FILESIZE), + XFS_MAX_FILEOFF); + error = -EINVAL; + goto out_free_sb; + } + error = xfs_filestream_mount(mp); if (error) goto out_free_sb; @@ -1661,7 +1655,7 @@ xfs_fs_fill_super( sb->s_magic = XFS_SUPER_MAGIC; sb->s_blocksize = mp->m_sb.sb_blocksize; sb->s_blocksize_bits = ffs(sb->s_blocksize) - 1; - sb->s_maxbytes = xfs_max_file_offset(sb->s_blocksize_bits); + sb->s_maxbytes = MAX_LFS_FILESIZE; sb->s_max_links = XFS_MAXLINK; sb->s_time_gran = 1; sb->s_time_min = S32_MIN;