Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp453642pxj; Tue, 18 May 2021 07:08:10 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzCr991eF7UPIQGOQkZ+NOkUJqhq/IimxaXLi1/i38HSgFnzyiMqBESDPTIb5SH1jO1ATPe X-Received: by 2002:a02:92cc:: with SMTP id c12mr5584705jah.30.1621346890044; Tue, 18 May 2021 07:08:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1621346890; cv=none; d=google.com; s=arc-20160816; b=VzrehxxYN6kCs9rDZw8/SNxZoqsmV0NTrgMfQuIpqaHPi4V10hEvVJcMezUd77B5Gi xgOKMvslCF1zglSMEvxz5sA/GEb7Vup2B+4RmDyOuOoSKO+stN2UmPy3P02IOXvSDS7X 0HLvYbVg7hDENuCkyxLP7GLao3wn40C45OnPiUANVvWEjI6dSvSRxM8xuUfNryZnI/bS yWKKOesZoESYe6KnG6TCRc0e9TQG+fGKQXXKCLL2hp5eq2pTE/rfcoY7/hnd8lY938OE oDcPl5dWUH2r546i2gMh4uStqkQARN6pB6XpQMoBzFUkyaGFREvct1OIcEj3HNHlWJmo MX9w== 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=4dc4TsB5Q6fMRnbjs1TA7y85d7qD53l8h6xuRxT1Fm4=; b=iOX9kV9Iyfhwm38q6/l7dGWhuoQx19ELtxZaaVPHOuE8ILg6CTKqSzTg7COB8NnUWA VTCxlwFOQz7AYr7OHZl6GYsjYcErLHT/rMoPOF2a9UP5shXWUV2LjM7fFmlxoevj+0E3 R9CX+95X5akw8DTnMf8qQIKUCCyPaJNnQWhBWDS1RPOwgYYxiukOfVydarCU9Z+NkNis BtbHkA6tn6g/JWY2IRu1GLjRi4yODEIzDAPOgzOo9AR2e8xDp9H5dtWbP92cP3nAUh06 OGz9lY8x5EiSFp+rzjSSa97Q/uoRIqUDVBXpT3vYGKtuzzAOKGY15b264CPMs5ZFqJ+/ rLeg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=K62xMHFf; 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 x5si22902217ilu.59.2021.05.18.07.07.57; Tue, 18 May 2021 07:08:09 -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=K62xMHFf; 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 S242725AbhEQPlt (ORCPT + 99 others); Mon, 17 May 2021 11:41:49 -0400 Received: from mail.kernel.org ([198.145.29.99]:41268 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243751AbhEQP0e (ORCPT ); Mon, 17 May 2021 11:26:34 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 415A561C98; Mon, 17 May 2021 14:36:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1621262188; bh=e6rbGkevxH80htg2S1pnTYsN/VIFVvqRPI21soQ99II=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=K62xMHFf1YCR+CSL680fmgEnDB9WYrUTESimKWOPqwdZYMZoRH4dSBXdp4p6ddR8h wQB3GjmT4gBlgESSZsJx7X7mcQmQa+yfJKluybQVT8fWFPzzqXUUcxxsaA/lHmlAzd 5Trb2z58tb6VC3PwJTF8NmViPAFLRVYl5H1P6gi4= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Jouni Roivas , Anton Altaparmakov , Anatoly Trosinenko , Viacheslav Dubeyko , Andrew Morton , Linus Torvalds Subject: [PATCH 5.11 235/329] hfsplus: prevent corruption in shrinking truncate Date: Mon, 17 May 2021 16:02:26 +0200 Message-Id: <20210517140310.065784644@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210517140302.043055203@linuxfoundation.org> References: <20210517140302.043055203@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: Jouni Roivas commit c3187cf32216313fb316084efac4dab3a8459b1d upstream. I believe there are some issues introduced by commit 31651c607151 ("hfsplus: avoid deadlock on file truncation") HFS+ has extent records which always contains 8 extents. In case the first extent record in catalog file gets full, new ones are allocated from extents overflow file. In case shrinking truncate happens to middle of an extent record which locates in extents overflow file, the logic in hfsplus_file_truncate() was changed so that call to hfs_brec_remove() is not guarded any more. Right action would be just freeing the extents that exceed the new size inside extent record by calling hfsplus_free_extents(), and then check if the whole extent record should be removed. However since the guard (blk_cnt > start) is now after the call to hfs_brec_remove(), this has unfortunate effect that the last matching extent record is removed unconditionally. To reproduce this issue, create a file which has at least 10 extents, and then perform shrinking truncate into middle of the last extent record, so that the number of remaining extents is not under or divisible by 8. This causes the last extent record (8 extents) to be removed totally instead of truncating into middle of it. Thus this causes corruption, and lost data. Fix for this is simply checking if the new truncated end is below the start of this extent record, making it safe to remove the full extent record. However call to hfs_brec_remove() can't be moved to it's previous place since we're dropping ->tree_lock and it can cause a race condition and the cached info being invalidated possibly corrupting the node data. Another issue is related to this one. When entering into the block (blk_cnt > start) we are not holding the ->tree_lock. We break out from the loop not holding the lock, but hfs_find_exit() does unlock it. Not sure if it's possible for someone else to take the lock under our feet, but it can cause hard to debug errors and premature unlocking. Even if there's no real risk of it, the locking should still always be kept in balance. Thus taking the lock now just before the check. Link: https://lkml.kernel.org/r/20210429165139.3082828-1-jouni.roivas@tuxera.com Fixes: 31651c607151f ("hfsplus: avoid deadlock on file truncation") Signed-off-by: Jouni Roivas Reviewed-by: Anton Altaparmakov Cc: Anatoly Trosinenko Cc: Viacheslav Dubeyko Cc: Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Greg Kroah-Hartman --- fs/hfsplus/extents.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) --- a/fs/hfsplus/extents.c +++ b/fs/hfsplus/extents.c @@ -598,13 +598,15 @@ void hfsplus_file_truncate(struct inode res = __hfsplus_ext_cache_extent(&fd, inode, alloc_cnt); if (res) break; - hfs_brec_remove(&fd); - mutex_unlock(&fd.tree->tree_lock); start = hip->cached_start; + if (blk_cnt <= start) + hfs_brec_remove(&fd); + mutex_unlock(&fd.tree->tree_lock); hfsplus_free_extents(sb, hip->cached_extents, alloc_cnt - start, alloc_cnt - blk_cnt); hfsplus_dump_extent(hip->cached_extents); + mutex_lock(&fd.tree->tree_lock); if (blk_cnt > start) { hip->extent_state |= HFSPLUS_EXT_DIRTY; break; @@ -612,7 +614,6 @@ void hfsplus_file_truncate(struct inode alloc_cnt = start; hip->cached_start = hip->cached_blocks = 0; hip->extent_state &= ~(HFSPLUS_EXT_DIRTY | HFSPLUS_EXT_NEW); - mutex_lock(&fd.tree->tree_lock); } hfs_find_exit(&fd);