Skip to content

Commit 44a4bc9

Browse files
tytsogregkh
authored andcommitted
ext4: only look at the bg_flags field if it is valid
commit 8844618 upstream. The bg_flags field in the block group descripts is only valid if the uninit_bg or metadata_csum feature is enabled. We were not consistently looking at this field; fix this. Also block group #0 must never have uninitialized allocation bitmaps, or need to be zeroed, since that's where the root inode, and other special inodes are set up. Check for these conditions and mark the file system as corrupted if they are detected. This addresses CVE-2018-10876. https://bugzilla.kernel.org/show_bug.cgi?id=199403 Signed-off-by: Theodore Ts'o <tytso@mit.edu> Cc: stable@kernel.org Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent ac48bb9 commit 44a4bc9

4 files changed

Lines changed: 36 additions & 6 deletions

File tree

‎fs/ext4/balloc.c‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,16 @@ ext4_read_block_bitmap_nowait(struct super_block *sb, ext4_group_t block_group)
451451
goto verify;
452452
}
453453
ext4_lock_group(sb, block_group);
454-
if (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
454+
if (ext4_has_group_desc_csum(sb) &&
455+
(desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT))) {
456+
if (block_group == 0) {
457+
ext4_unlock_group(sb, block_group);
458+
unlock_buffer(bh);
459+
ext4_error(sb, "Block bitmap for bg 0 marked "
460+
"uninitialized");
461+
err = -EFSCORRUPTED;
462+
goto out;
463+
}
455464
err = ext4_init_block_bitmap(sb, bh, block_group, desc);
456465
set_bitmap_uptodate(bh);
457466
set_buffer_uptodate(bh);

‎fs/ext4/ialloc.c‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,16 @@ ext4_read_inode_bitmap(struct super_block *sb, ext4_group_t block_group)
155155
}
156156

157157
ext4_lock_group(sb, block_group);
158-
if (desc->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
158+
if (ext4_has_group_desc_csum(sb) &&
159+
(desc->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT))) {
160+
if (block_group == 0) {
161+
ext4_unlock_group(sb, block_group);
162+
unlock_buffer(bh);
163+
ext4_error(sb, "Inode bitmap for bg 0 marked "
164+
"uninitialized");
165+
err = -EFSCORRUPTED;
166+
goto out;
167+
}
159168
memset(bh->b_data, 0, (EXT4_INODES_PER_GROUP(sb) + 7) / 8);
160169
ext4_mark_bitmap_end(EXT4_INODES_PER_GROUP(sb),
161170
sb->s_blocksize * 8, bh->b_data);
@@ -1000,7 +1009,8 @@ struct inode *__ext4_new_inode(handle_t *handle, struct inode *dir,
10001009

10011010
/* recheck and clear flag under lock if we still need to */
10021011
ext4_lock_group(sb, group);
1003-
if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
1012+
if (ext4_has_group_desc_csum(sb) &&
1013+
(gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT))) {
10041014
gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
10051015
ext4_free_group_clusters_set(sb, gdp,
10061016
ext4_free_clusters_after_init(sb, group, gdp));

‎fs/ext4/mballoc.c‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2456,7 +2456,8 @@ int ext4_mb_add_groupinfo(struct super_block *sb, ext4_group_t group,
24562456
* initialize bb_free to be able to skip
24572457
* empty groups without initialization
24582458
*/
2459-
if (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
2459+
if (ext4_has_group_desc_csum(sb) &&
2460+
(desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT))) {
24602461
meta_group_info[i]->bb_free =
24612462
ext4_free_clusters_after_init(sb, group, desc);
24622463
} else {
@@ -3023,7 +3024,8 @@ ext4_mb_mark_diskspace_used(struct ext4_allocation_context *ac,
30233024
#endif
30243025
ext4_set_bits(bitmap_bh->b_data, ac->ac_b_ex.fe_start,
30253026
ac->ac_b_ex.fe_len);
3026-
if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
3027+
if (ext4_has_group_desc_csum(sb) &&
3028+
(gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT))) {
30273029
gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
30283030
ext4_free_group_clusters_set(sb, gdp,
30293031
ext4_free_clusters_after_init(sb,

‎fs/ext4/super.c‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3095,13 +3095,22 @@ static ext4_group_t ext4_has_uninit_itable(struct super_block *sb)
30953095
ext4_group_t group, ngroups = EXT4_SB(sb)->s_groups_count;
30963096
struct ext4_group_desc *gdp = NULL;
30973097

3098+
if (!ext4_has_group_desc_csum(sb))
3099+
return ngroups;
3100+
30983101
for (group = 0; group < ngroups; group++) {
30993102
gdp = ext4_get_group_desc(sb, group, NULL);
31003103
if (!gdp)
31013104
continue;
31023105

3103-
if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
3106+
if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED))
3107+
continue;
3108+
if (group != 0)
31043109
break;
3110+
ext4_error(sb, "Inode table for bg 0 marked as "
3111+
"needing zeroing");
3112+
if (sb_rdonly(sb))
3113+
return ngroups;
31053114
}
31063115

31073116
return group;

0 commit comments

Comments
 (0)