提交 acaa5326 编写于 作者: C Constantine Sapuntzakis 提交者: Theodore Ts'o

ext4: fix superblock checksum calculation race

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: NConstantine Sapuntzakis <costa@purestorage.com>
Reviewed-by: NJan Kara <jack@suse.cz>
Suggested-by: NJan Kara <jack@suse.cz>
Link: https://lore.kernel.org/r/20200914161014.22275-1-costa@purestorage.comSigned-off-by: NTheodore Ts'o <tytso@mit.edu>
上级 c9e87161
...@@ -201,7 +201,18 @@ void ext4_superblock_csum_set(struct super_block *sb) ...@@ -201,7 +201,18 @@ void ext4_superblock_csum_set(struct super_block *sb)
if (!ext4_has_metadata_csum(sb)) if (!ext4_has_metadata_csum(sb))
return; 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); es->s_checksum = ext4_superblock_csum(sb, es);
unlock_buffer(EXT4_SB(sb)->s_sbh);
} }
ext4_fsblk_t ext4_block_bitmap(struct super_block *sb, ext4_fsblk_t ext4_block_bitmap(struct super_block *sb,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册