Skip to content

Commit 345c0db

Browse files
committed
ext4: protect journal inode's blocks using block_validity
Add the blocks which belong to the journal inode to block_validity's system zone so attempts to deallocate or overwrite the journal due a corrupted file system where the journal blocks are also claimed by another inode. Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=202879 Signed-off-by: Theodore Ts'o <[email protected]> Cc: [email protected]
1 parent 1e83bc8 commit 345c0db

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

fs/ext4/block_validity.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,48 @@ static void debug_print_tree(struct ext4_sb_info *sbi)
137137
printk(KERN_CONT "\n");
138138
}
139139

140+
static int ext4_protect_reserved_inode(struct super_block *sb, u32 ino)
141+
{
142+
struct inode *inode;
143+
struct ext4_sb_info *sbi = EXT4_SB(sb);
144+
struct ext4_map_blocks map;
145+
u32 i = 0, err = 0, num, n;
146+
147+
if ((ino < EXT4_ROOT_INO) ||
148+
(ino > le32_to_cpu(sbi->s_es->s_inodes_count)))
149+
return -EINVAL;
150+
inode = ext4_iget(sb, ino, EXT4_IGET_SPECIAL);
151+
if (IS_ERR(inode))
152+
return PTR_ERR(inode);
153+
num = (inode->i_size + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
154+
while (i < num) {
155+
map.m_lblk = i;
156+
map.m_len = num - i;
157+
n = ext4_map_blocks(NULL, inode, &map, 0);
158+
if (n < 0) {
159+
err = n;
160+
break;
161+
}
162+
if (n == 0) {
163+
i++;
164+
} else {
165+
if (!ext4_data_block_valid(sbi, map.m_pblk, n)) {
166+
ext4_error(sb, "blocks %llu-%llu from inode %u "
167+
"overlap system zone", map.m_pblk,
168+
map.m_pblk + map.m_len - 1, ino);
169+
err = -EFSCORRUPTED;
170+
break;
171+
}
172+
err = add_system_zone(sbi, map.m_pblk, n);
173+
if (err < 0)
174+
break;
175+
i += n;
176+
}
177+
}
178+
iput(inode);
179+
return err;
180+
}
181+
140182
int ext4_setup_system_zone(struct super_block *sb)
141183
{
142184
ext4_group_t ngroups = ext4_get_groups_count(sb);
@@ -171,6 +213,12 @@ int ext4_setup_system_zone(struct super_block *sb)
171213
if (ret)
172214
return ret;
173215
}
216+
if (ext4_has_feature_journal(sb) && sbi->s_es->s_journal_inum) {
217+
ret = ext4_protect_reserved_inode(sb,
218+
le32_to_cpu(sbi->s_es->s_journal_inum));
219+
if (ret)
220+
return ret;
221+
}
174222

175223
if (test_opt(sb, DEBUG))
176224
debug_print_tree(sbi);

fs/ext4/inode.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,10 @@ static int __check_block_validity(struct inode *inode, const char *func,
399399
unsigned int line,
400400
struct ext4_map_blocks *map)
401401
{
402+
if (ext4_has_feature_journal(inode->i_sb) &&
403+
(inode->i_ino ==
404+
le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_journal_inum)))
405+
return 0;
402406
if (!ext4_data_block_valid(EXT4_SB(inode->i_sb), map->m_pblk,
403407
map->m_len)) {
404408
ext4_error_inode(inode, func, line, map->m_pblk,

0 commit comments

Comments
 (0)