From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D13F2C2D0A3 for ; Tue, 3 Nov 2020 21:13:58 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7AA8A206B5 for ; Tue, 3 Nov 2020 21:13:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1604438038; bh=BQAiSB5HKL8GvcYtruKKpWLmUAS2rG95pW3ourTel+k=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-ID:From; b=aQc55rUC8bBzSJ89vA+6sXTguhY/ONYxpJJ3PjfcQKGN86Zn8Kl0Fr7WL/lBYEQuY O5vqbZxF2UFpfSVosLSVwb/bbJKhp67OBcRTFiisgOvQXKvoI2pt7/Oo75Rl4zAjU/ gmyzlZgkUnDUocRYoBfEM2M3DNdUGE7Ya53GHwrE= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2389340AbgKCVNo (ORCPT ); Tue, 3 Nov 2020 16:13:44 -0500 Received: from mail.kernel.org ([198.145.29.99]:56994 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388788AbgKCVNn (ORCPT ); Tue, 3 Nov 2020 16:13:43 -0500 Received: from localhost (83-86-74-64.cable.dynamic.v4.ziggo.nl [83.86.74.64]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id C53D6207BC; Tue, 3 Nov 2020 21:13:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1604438023; bh=BQAiSB5HKL8GvcYtruKKpWLmUAS2rG95pW3ourTel+k=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=uQnlR6Re6JtlIlERXHaBqrQDPum2xE8oKlotYAmkG0BeEImdccaX5RQ2rRo0S3Lmq 2C9vJd/IY1V+XDp41QVU3g3Ld0WACxVv2miy8N7E74FMgi0Pg1BiapFUu4n+dTEkT6 beHR0OJpuF0RWyxmDp5azT58tALfGgYmuTp0uUak= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, stable@kernel.org, Constantine Sapuntzakis , Jan Kara , Theodore Tso Subject: [PATCH 4.14 110/125] ext4: fix superblock checksum calculation race Date: Tue, 3 Nov 2020 21:38:07 +0100 Message-Id: <20201103203213.463517379@linuxfoundation.org> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201103203156.372184213@linuxfoundation.org> References: <20201103203156.372184213@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: stable@vger.kernel.org From: Constantine Sapuntzakis commit acaa532687cdc3a03757defafece9c27aa667546 upstream. The race condition could cause the persisted superblock checksum to not match the contents of the superblock, causing the superblock to be considered corrupt. An example of the race follows. A first thread is interrupted in the middle of a checksum calculation. Then, another thread changes the superblock, calculates a new checksum, and sets it. Then, the first thread resumes and sets the checksum based on the older superblock. To fix, serialize the superblock checksum calculation using the buffer header lock. While a spinlock is sufficient, the buffer header is already there and there is precedent for locking it (e.g. in ext4_commit_super). Tested the patch by booting up a kernel with the patch, creating a filesystem and some files (including some orphans), and then unmounting and remounting the file system. Cc: stable@kernel.org Signed-off-by: Constantine Sapuntzakis Reviewed-by: Jan Kara Suggested-by: Jan Kara Link: https://lore.kernel.org/r/20200914161014.22275-1-costa@purestorage.com Signed-off-by: Theodore Ts'o Signed-off-by: Greg Kroah-Hartman --- fs/ext4/super.c | 11 +++++++++++ 1 file changed, 11 insertions(+) --- a/fs/ext4/super.c +++ b/fs/ext4/super.c @@ -200,7 +200,18 @@ void ext4_superblock_csum_set(struct sup if (!ext4_has_metadata_csum(sb)) return; + /* + * Locking the superblock prevents the scenario + * where: + * 1) a first thread pauses during checksum calculation. + * 2) a second thread updates the superblock, recalculates + * the checksum, and updates s_checksum + * 3) the first thread resumes and finishes its checksum calculation + * and updates s_checksum with a potentially stale or torn value. + */ + lock_buffer(EXT4_SB(sb)->s_sbh); es->s_checksum = ext4_superblock_csum(sb, es); + unlock_buffer(EXT4_SB(sb)->s_sbh); } void *ext4_kvmalloc(size_t size, gfp_t flags)