lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-Id: <5E4A9CEF-3355-4362-A17D-9BFBA81D2094@dilger.ca>
Date: Thu, 19 Sep 2024 14:36:28 -0600
From: Andreas Dilger <adilger@...ger.ca>
To: Jeongjun Park <aha310510@...il.com>
Cc: Theodore Ts'o <tytso@....edu>,
 aneesh.kumar@...ux.vnet.ibm.com,
 shaggy@...tin.ibm.com,
 akpm@...l.org,
 alexandre.ratchov@...l.net,
 Ext4 Developers List <linux-ext4@...r.kernel.org>,
 LKML <linux-kernel@...r.kernel.org>
Subject: Re: [PATCH] ext4: prevent data-race that occur when read/write
 ext4_group_desc structure members

On Sep 18, 2024, at 3:21 AM, Jeongjun Park <aha310510@...il.com> wrote:
> 
> Currently, data-race like [1] occur in fs/ext4/super.c
> 
> Since the ext4_group_desc structure does not have a lock to protect its
> member variables and there is a possibility that unexpected behavior may
> occur in the future if a data-race occurs, I think it would be appropriate
> to modify the ext4_group_desc member variables to be read/written atomically.

It is unclear if this is enough, or alternately whether it is needed at all?

There is still a race window between accessing/setting the _hi and _lo
fields in the group descriptor that could result in torn writes.
This will _rarely_ be an issue for many filesystems, since block/inode
bitmap numbers are only read, so it is only the block and inode counts
that are changed.

Also, it is only a fairly rare use case where there are more than 2^16
blocks or inodes in a single group, so the _hi field should almost always
be zero, unless this is a 64KB blocksize filesystem?

It looks like ext4_lock_group() could be used to protect this access, but
that is very expensive for the read side, and unnecessary for 4KB blocksize
filesystems (the vast majority of cases).

I suspect that the inode allocation side is *already* holding the group
lock so that the bitmap update and other fields are atomic?  It probably
makes sense to get ext4_lock_group() at a higher level (e.g. ext4_symlink()
or __ext4_new_inode() when it is allocating the inode) rather than pretend
the two _lo and _hi accesses are atomic at the lower level.

Cheers, Andreas

> [1]
> 
> ==================================================================
> BUG: KCSAN: data-race in ext4_free_inodes_count / ext4_free_inodes_set
> 
> write to 0xffff88810404300e of 2 bytes by task 6254 on cpu 1:
> ext4_free_inodes_set+0x1f/0x80 fs/ext4/super.c:405
> __ext4_new_inode+0x15ca/0x2200 fs/ext4/ialloc.c:1216
> ext4_symlink+0x242/0x5a0 fs/ext4/namei.c:3391
> vfs_symlink+0xca/0x1d0 fs/namei.c:4615
> do_symlinkat+0xe3/0x340 fs/namei.c:4641
> __do_sys_symlinkat fs/namei.c:4657 [inline]
> __se_sys_symlinkat fs/namei.c:4654 [inline]
> __x64_sys_symlinkat+0x5e/0x70 fs/namei.c:4654
> x64_sys_call+0x1dda/0x2d60 arch/x86/include/generated/asm/syscalls_64.h:267
> do_syscall_x64 arch/x86/entry/common.c:52 [inline]
> do_syscall_64+0x54/0x120 arch/x86/entry/common.c:83
> entry_SYSCALL_64_after_hwframe+0x76/0x7e
> 
> read to 0xffff88810404300e of 2 bytes by task 6257 on cpu 0:
> ext4_free_inodes_count+0x1c/0x80 fs/ext4/super.c:349
> find_group_other fs/ext4/ialloc.c:594 [inline]
> __ext4_new_inode+0x6ec/0x2200 fs/ext4/ialloc.c:1017
> ext4_symlink+0x242/0x5a0 fs/ext4/namei.c:3391
> vfs_symlink+0xca/0x1d0 fs/namei.c:4615
> do_symlinkat+0xe3/0x340 fs/namei.c:4641
> __do_sys_symlinkat fs/namei.c:4657 [inline]
> __se_sys_symlinkat fs/namei.c:4654 [inline]
> __x64_sys_symlinkat+0x5e/0x70 fs/namei.c:4654
> x64_sys_call+0x1dda/0x2d60 arch/x86/include/generated/asm/syscalls_64.h:267
> do_syscall_x64 arch/x86/entry/common.c:52 [inline]
> do_syscall_64+0x54/0x120 arch/x86/entry/common.c:83
> entry_SYSCALL_64_after_hwframe+0x76/0x7e
> 
> value changed: 0x185c -> 0x185b
> 
> Fixes: 560671a0d3c9 ("ext4: Use high 16 bits of the block group descriptor's free counts fields")
> Fixes: 8fadc1432368 ("[PATCH] ext4: move block number hi bits")
> Signed-off-by: Jeongjun Park <aha310510@...il.com>
> ---
> fs/ext4/super.c | 56 ++++++++++++++++++++++++-------------------------
> 1 file changed, 28 insertions(+), 28 deletions(-)
> 
> diff --git a/fs/ext4/super.c b/fs/ext4/super.c
> index e72145c4ae5a..9c918cf2eb7e 100644
> --- a/fs/ext4/super.c
> +++ b/fs/ext4/super.c
> @@ -314,113 +314,113 @@ void ext4_superblock_csum_set(struct super_block *sb)
> ext4_fsblk_t ext4_block_bitmap(struct super_block *sb,
> 			       struct ext4_group_desc *bg)
> {
> -	return le32_to_cpu(bg->bg_block_bitmap_lo) |
> +	return le32_to_cpu(READ_ONCE(bg->bg_block_bitmap_lo)) |
> 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
> -		 (ext4_fsblk_t)le32_to_cpu(bg->bg_block_bitmap_hi) << 32 : 0);
> +		 (ext4_fsblk_t)le32_to_cpu(READ_ONCE(bg->bg_block_bitmap_hi)) << 32 : 0);
> }
> 
> ext4_fsblk_t ext4_inode_bitmap(struct super_block *sb,
> 			       struct ext4_group_desc *bg)
> {
> -	return le32_to_cpu(bg->bg_inode_bitmap_lo) |
> +	return le32_to_cpu(READ_ONCE(bg->bg_inode_bitmap_lo)) |
> 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
> -		 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_bitmap_hi) << 32 : 0);
> +		 (ext4_fsblk_t)le32_to_cpu(READ_ONCE(bg->bg_inode_bitmap_hi)) << 32 : 0);
> }
> 
> ext4_fsblk_t ext4_inode_table(struct super_block *sb,
> 			      struct ext4_group_desc *bg)
> {
> -	return le32_to_cpu(bg->bg_inode_table_lo) |
> +	return le32_to_cpu(READ_ONCE(bg->bg_inode_table_lo)) |
> 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
> -		 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_table_hi) << 32 : 0);
> +		 (ext4_fsblk_t)le32_to_cpu(READ_ONCE(bg->bg_inode_table_hi)) << 32 : 0);
> }
> 
> __u32 ext4_free_group_clusters(struct super_block *sb,
> 			       struct ext4_group_desc *bg)
> {
> -	return le16_to_cpu(bg->bg_free_blocks_count_lo) |
> +	return le16_to_cpu(READ_ONCE(bg->bg_free_blocks_count_lo)) |
> 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
> -		 (__u32)le16_to_cpu(bg->bg_free_blocks_count_hi) << 16 : 0);
> +		 (__u32)le16_to_cpu(READ_ONCE(bg->bg_free_blocks_count_hi)) << 16 : 0);
> }
> 
> __u32 ext4_free_inodes_count(struct super_block *sb,
> 			      struct ext4_group_desc *bg)
> {
> -	return le16_to_cpu(bg->bg_free_inodes_count_lo) |
> +	return le16_to_cpu(READ_ONCE(bg->bg_free_inodes_count_lo)) |
> 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
> -		 (__u32)le16_to_cpu(bg->bg_free_inodes_count_hi) << 16 : 0);
> +		 (__u32)le16_to_cpu(READ_ONCE(bg->bg_free_inodes_count_hi)) << 16 : 0);
> }
> 
> __u32 ext4_used_dirs_count(struct super_block *sb,
> 			      struct ext4_group_desc *bg)
> {
> -	return le16_to_cpu(bg->bg_used_dirs_count_lo) |
> +	return le16_to_cpu(READ_ONCE(bg->bg_used_dirs_count_lo)) |
> 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
> -		 (__u32)le16_to_cpu(bg->bg_used_dirs_count_hi) << 16 : 0);
> +		 (__u32)le16_to_cpu(READ_ONCE(bg->bg_used_dirs_count_hi)) << 16 : 0);
> }
> 
> __u32 ext4_itable_unused_count(struct super_block *sb,
> 			      struct ext4_group_desc *bg)
> {
> -	return le16_to_cpu(bg->bg_itable_unused_lo) |
> +	return le16_to_cpu(READ_ONCE(bg->bg_itable_unused_lo)) |
> 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
> -		 (__u32)le16_to_cpu(bg->bg_itable_unused_hi) << 16 : 0);
> +		 (__u32)le16_to_cpu(READ_ONCE(bg->bg_itable_unused_hi)) << 16 : 0);
> }
> 
> void ext4_block_bitmap_set(struct super_block *sb,
> 			   struct ext4_group_desc *bg, ext4_fsblk_t blk)
> {
> -	bg->bg_block_bitmap_lo = cpu_to_le32((u32)blk);
> +	WRITE_ONCE(bg->bg_block_bitmap_lo, cpu_to_le32((u32)blk));
> 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
> -		bg->bg_block_bitmap_hi = cpu_to_le32(blk >> 32);
> +		WRITE_ONCE(bg->bg_block_bitmap_hi, cpu_to_le32(blk >> 32));
> }
> 
> void ext4_inode_bitmap_set(struct super_block *sb,
> 			   struct ext4_group_desc *bg, ext4_fsblk_t blk)
> {
> -	bg->bg_inode_bitmap_lo  = cpu_to_le32((u32)blk);
> +	WRITE_ONCE(bg->bg_inode_bitmap_lo, cpu_to_le32((u32)blk));
> 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
> -		bg->bg_inode_bitmap_hi = cpu_to_le32(blk >> 32);
> +		WRITE_ONCE(bg->bg_inode_bitmap_hi, cpu_to_le32(blk >> 32));
> }
> 
> void ext4_inode_table_set(struct super_block *sb,
> 			  struct ext4_group_desc *bg, ext4_fsblk_t blk)
> {
> -	bg->bg_inode_table_lo = cpu_to_le32((u32)blk);
> +	WRITE_ONCE(bg->bg_inode_table_lo, cpu_to_le32((u32)blk));
> 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
> -		bg->bg_inode_table_hi = cpu_to_le32(blk >> 32);
> +		WRITE_ONCE(bg->bg_inode_table_hi, cpu_to_le32(blk >> 32));
> }
> 
> void ext4_free_group_clusters_set(struct super_block *sb,
> 				  struct ext4_group_desc *bg, __u32 count)
> {
> -	bg->bg_free_blocks_count_lo = cpu_to_le16((__u16)count);
> +	WRITE_ONCE(bg->bg_free_blocks_count_lo, cpu_to_le16((__u16)count));
> 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
> -		bg->bg_free_blocks_count_hi = cpu_to_le16(count >> 16);
> +		WRITE_ONCE(bg->bg_free_blocks_count_hi, cpu_to_le16(count >> 16));
> }
> 
> void ext4_free_inodes_set(struct super_block *sb,
> 			  struct ext4_group_desc *bg, __u32 count)
> {
> -	bg->bg_free_inodes_count_lo = cpu_to_le16((__u16)count);
> +	WRITE_ONCE(bg->bg_free_inodes_count_lo, cpu_to_le16((__u16)count));
> 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
> -		bg->bg_free_inodes_count_hi = cpu_to_le16(count >> 16);
> +		WRITE_ONCE(bg->bg_free_inodes_count_hi, cpu_to_le16(count >> 16));
> }
> 
> void ext4_used_dirs_set(struct super_block *sb,
> 			  struct ext4_group_desc *bg, __u32 count)
> {
> -	bg->bg_used_dirs_count_lo = cpu_to_le16((__u16)count);
> +	WRITE_ONCE(bg->bg_used_dirs_count_lo, cpu_to_le16((__u16)count));
> 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
> -		bg->bg_used_dirs_count_hi = cpu_to_le16(count >> 16);
> +		WRITE_ONCE(bg->bg_used_dirs_count_hi, cpu_to_le16(count >> 16));
> }
> 
> void ext4_itable_unused_set(struct super_block *sb,
> 			  struct ext4_group_desc *bg, __u32 count)
> {
> -	bg->bg_itable_unused_lo = cpu_to_le16((__u16)count);
> +	WRITE_ONCE(bg->bg_itable_unused_lo, cpu_to_le16((__u16)count));
> 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
> -		bg->bg_itable_unused_hi = cpu_to_le16(count >> 16);
> +		WRITE_ONCE(bg->bg_itable_unused_hi, cpu_to_le16(count >> 16));
> }
> 
> static void __ext4_update_tstamp(__le32 *lo, __u8 *hi, time64_t now)
> --


Cheers, Andreas






Download attachment "signature.asc" of type "application/pgp-signature" (874 bytes)

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ