Skip to content

Commit 9803387

Browse files
committed
ext4: validate the debug_want_extra_isize mount option at parse time
Instead of setting s_want_extra_size and then making sure that it is a valid value afterwards, validate the field before we set it. This avoids races and other problems when remounting the file system. Link: https://lore.kernel.org/r/[email protected] Cc: [email protected] Signed-off-by: Theodore Ts'o <[email protected]> Reported-and-tested-by: [email protected]
1 parent a70fd5a commit 9803387

File tree

1 file changed

+69
-74
lines changed

1 file changed

+69
-74
lines changed

fs/ext4/super.c

Lines changed: 69 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1886,6 +1886,13 @@ static int handle_mount_opt(struct super_block *sb, char *opt, int token,
18861886
}
18871887
sbi->s_commit_interval = HZ * arg;
18881888
} else if (token == Opt_debug_want_extra_isize) {
1889+
if ((arg & 1) ||
1890+
(arg < 4) ||
1891+
(arg > (sbi->s_inode_size - EXT4_GOOD_OLD_INODE_SIZE))) {
1892+
ext4_msg(sb, KERN_ERR,
1893+
"Invalid want_extra_isize %d", arg);
1894+
return -1;
1895+
}
18891896
sbi->s_want_extra_isize = arg;
18901897
} else if (token == Opt_max_batch_time) {
18911898
sbi->s_max_batch_time = arg;
@@ -3540,40 +3547,6 @@ int ext4_calculate_overhead(struct super_block *sb)
35403547
return 0;
35413548
}
35423549

3543-
static void ext4_clamp_want_extra_isize(struct super_block *sb)
3544-
{
3545-
struct ext4_sb_info *sbi = EXT4_SB(sb);
3546-
struct ext4_super_block *es = sbi->s_es;
3547-
unsigned def_extra_isize = sizeof(struct ext4_inode) -
3548-
EXT4_GOOD_OLD_INODE_SIZE;
3549-
3550-
if (sbi->s_inode_size == EXT4_GOOD_OLD_INODE_SIZE) {
3551-
sbi->s_want_extra_isize = 0;
3552-
return;
3553-
}
3554-
if (sbi->s_want_extra_isize < 4) {
3555-
sbi->s_want_extra_isize = def_extra_isize;
3556-
if (ext4_has_feature_extra_isize(sb)) {
3557-
if (sbi->s_want_extra_isize <
3558-
le16_to_cpu(es->s_want_extra_isize))
3559-
sbi->s_want_extra_isize =
3560-
le16_to_cpu(es->s_want_extra_isize);
3561-
if (sbi->s_want_extra_isize <
3562-
le16_to_cpu(es->s_min_extra_isize))
3563-
sbi->s_want_extra_isize =
3564-
le16_to_cpu(es->s_min_extra_isize);
3565-
}
3566-
}
3567-
/* Check if enough inode space is available */
3568-
if ((sbi->s_want_extra_isize > sbi->s_inode_size) ||
3569-
(EXT4_GOOD_OLD_INODE_SIZE + sbi->s_want_extra_isize >
3570-
sbi->s_inode_size)) {
3571-
sbi->s_want_extra_isize = def_extra_isize;
3572-
ext4_msg(sb, KERN_INFO,
3573-
"required extra inode space not available");
3574-
}
3575-
}
3576-
35773550
static void ext4_set_resv_clusters(struct super_block *sb)
35783551
{
35793552
ext4_fsblk_t resv_clusters;
@@ -3781,6 +3754,68 @@ static int ext4_fill_super(struct super_block *sb, void *data, int silent)
37813754
*/
37823755
sbi->s_li_wait_mult = EXT4_DEF_LI_WAIT_MULT;
37833756

3757+
if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
3758+
sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
3759+
sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
3760+
} else {
3761+
sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
3762+
sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
3763+
if (sbi->s_first_ino < EXT4_GOOD_OLD_FIRST_INO) {
3764+
ext4_msg(sb, KERN_ERR, "invalid first ino: %u",
3765+
sbi->s_first_ino);
3766+
goto failed_mount;
3767+
}
3768+
if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
3769+
(!is_power_of_2(sbi->s_inode_size)) ||
3770+
(sbi->s_inode_size > blocksize)) {
3771+
ext4_msg(sb, KERN_ERR,
3772+
"unsupported inode size: %d",
3773+
sbi->s_inode_size);
3774+
goto failed_mount;
3775+
}
3776+
/*
3777+
* i_atime_extra is the last extra field available for
3778+
* [acm]times in struct ext4_inode. Checking for that
3779+
* field should suffice to ensure we have extra space
3780+
* for all three.
3781+
*/
3782+
if (sbi->s_inode_size >= offsetof(struct ext4_inode, i_atime_extra) +
3783+
sizeof(((struct ext4_inode *)0)->i_atime_extra)) {
3784+
sb->s_time_gran = 1;
3785+
sb->s_time_max = EXT4_EXTRA_TIMESTAMP_MAX;
3786+
} else {
3787+
sb->s_time_gran = NSEC_PER_SEC;
3788+
sb->s_time_max = EXT4_NON_EXTRA_TIMESTAMP_MAX;
3789+
}
3790+
sb->s_time_min = EXT4_TIMESTAMP_MIN;
3791+
}
3792+
if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
3793+
sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
3794+
EXT4_GOOD_OLD_INODE_SIZE;
3795+
if (ext4_has_feature_extra_isize(sb)) {
3796+
unsigned v, max = (sbi->s_inode_size -
3797+
EXT4_GOOD_OLD_INODE_SIZE);
3798+
3799+
v = le16_to_cpu(es->s_want_extra_isize);
3800+
if (v > max) {
3801+
ext4_msg(sb, KERN_ERR,
3802+
"bad s_want_extra_isize: %d", v);
3803+
goto failed_mount;
3804+
}
3805+
if (sbi->s_want_extra_isize < v)
3806+
sbi->s_want_extra_isize = v;
3807+
3808+
v = le16_to_cpu(es->s_min_extra_isize);
3809+
if (v > max) {
3810+
ext4_msg(sb, KERN_ERR,
3811+
"bad s_min_extra_isize: %d", v);
3812+
goto failed_mount;
3813+
}
3814+
if (sbi->s_want_extra_isize < v)
3815+
sbi->s_want_extra_isize = v;
3816+
}
3817+
}
3818+
37843819
if (sbi->s_es->s_mount_opts[0]) {
37853820
char *s_mount_opts = kstrndup(sbi->s_es->s_mount_opts,
37863821
sizeof(sbi->s_es->s_mount_opts),
@@ -4019,42 +4054,6 @@ static int ext4_fill_super(struct super_block *sb, void *data, int silent)
40194054
has_huge_files);
40204055
sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits, has_huge_files);
40214056

4022-
if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
4023-
sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
4024-
sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
4025-
} else {
4026-
sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
4027-
sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
4028-
if (sbi->s_first_ino < EXT4_GOOD_OLD_FIRST_INO) {
4029-
ext4_msg(sb, KERN_ERR, "invalid first ino: %u",
4030-
sbi->s_first_ino);
4031-
goto failed_mount;
4032-
}
4033-
if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
4034-
(!is_power_of_2(sbi->s_inode_size)) ||
4035-
(sbi->s_inode_size > blocksize)) {
4036-
ext4_msg(sb, KERN_ERR,
4037-
"unsupported inode size: %d",
4038-
sbi->s_inode_size);
4039-
goto failed_mount;
4040-
}
4041-
/*
4042-
* i_atime_extra is the last extra field available for [acm]times in
4043-
* struct ext4_inode. Checking for that field should suffice to ensure
4044-
* we have extra space for all three.
4045-
*/
4046-
if (sbi->s_inode_size >= offsetof(struct ext4_inode, i_atime_extra) +
4047-
sizeof(((struct ext4_inode *)0)->i_atime_extra)) {
4048-
sb->s_time_gran = 1;
4049-
sb->s_time_max = EXT4_EXTRA_TIMESTAMP_MAX;
4050-
} else {
4051-
sb->s_time_gran = NSEC_PER_SEC;
4052-
sb->s_time_max = EXT4_NON_EXTRA_TIMESTAMP_MAX;
4053-
}
4054-
4055-
sb->s_time_min = EXT4_TIMESTAMP_MIN;
4056-
}
4057-
40584057
sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
40594058
if (ext4_has_feature_64bit(sb)) {
40604059
if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
@@ -4503,8 +4502,6 @@ static int ext4_fill_super(struct super_block *sb, void *data, int silent)
45034502
} else if (ret)
45044503
goto failed_mount4a;
45054504

4506-
ext4_clamp_want_extra_isize(sb);
4507-
45084505
ext4_set_resv_clusters(sb);
45094506

45104507
err = ext4_setup_system_zone(sb);
@@ -5292,8 +5289,6 @@ static int ext4_remount(struct super_block *sb, int *flags, char *data)
52925289
goto restore_opts;
52935290
}
52945291

5295-
ext4_clamp_want_extra_isize(sb);
5296-
52975292
if ((old_opts.s_mount_opt & EXT4_MOUNT_JOURNAL_CHECKSUM) ^
52985293
test_opt(sb, JOURNAL_CHECKSUM)) {
52995294
ext4_msg(sb, KERN_ERR, "changing journal_checksum "

0 commit comments

Comments
 (0)