Skip to contentSkip to conversation

ext4: prevent very high s_inode_readahead_blks

PATCH In review Dan Carpenter 2 participants 2 messages Feb 11, 2019

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ext4: prevent very high s_inode_readahead_blks

My static checker complains that "arg" can be negative. That does seem possible. I don't know if it causes an issue at run time but it seems safest to allow negatives.

---
 fs/ext4/super.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/ext4/super.c +1 -1
index 60da0a6e4d86..4e0845708c52 100644
@@ -1838,7 +1838,7 @@ static int handle_mount_opt(struct super_block *sb, char *opt, int token,
 	} else if (token = Opt_min_batch_time) {
 		sbi->s_min_batch_time = arg;
 	} else if (token = Opt_inode_readahead_blks) {
-		if (arg && (arg > (1 << 30) || !is_power_of_2(arg))) {
+		if (arg && (arg > (1U << 30) || !is_power_of_2(arg))) {
 			ext4_msg(sb, KERN_ERR,
 				 "EXT4-fs: inode_readahead_blks must be "
 				 "0 or a power of 2 smaller than 2^31");
Signature · 1 line
2.17.1
#1 lore raw
Quoted 4 lines · Dan Carpenter

My static checker complains that "arg" can be negative. That does seem possible. I don't know if it causes an issue at run time but it seems safest to allow negatives.

The option declaration requires that "arg" be greater than zero:

        {Opt_inode_readahead_blks, 2, MOPT_GTE0},

and this is checked earlier in handle_mount_opt():

        if (args->from && (m->flags & MOPT_GTE0) && (arg < 0))
                return -1;

but I agree that having false static checking warnings is annoying and potentially also hides other issues.

Quoted 10 lines

---
fs/ext4/super.c | 2 +-

1 file changed, 1 insertion(+), 1 deletion(-)
fs/ext4/super.c +0 -0
index 60da0a6e4d86..4e0845708c52 100644

fs/ext4/super.c static int handle_mount_opt(struct super_block *sb, · line 1841

Quoted 5 lines
fs/ext4/super.c +1 -1
@@ -1838,7 +1838,7 @@ static int handle_mount_opt(struct super_block *sb,
 	} else if (token == Opt_min_batch_time) {
 		sbi->s_min_batch_time = arg;
 	} else if (token == Opt_inode_readahead_blks) {
-		if (arg && (arg > (1 << 30) || !is_power_of_2(arg))) {
+		if (arg && (arg > (1U << 30) || !is_power_of_2(arg))) {

This may "fix" the problem by virtue of implicitly forcing an unsigned comparison, but doesn't necessarily make the issue more obvious to the reader.

That said, it doesn't look like _any_ use of "arg" allows a negative value, regardless of whether MOPT_GTE0 is set or not, so we should just declare arg as an unsigned int or explicitly refuse all negatives:

	if (args->from && !(m->flags & MOPT_STRING)) {
		if (match_int(args, &arg))
			return -1;
		if (arg < 0)
			return -1;
	}

This should keep the static checker happy, since the only place that "arg" is set away from zero it is also verified not to be negative. At that point it would also be possible to remove MOPT_GTE0 completely.

Cheers, Andreas