f2fs: check validation of fault attrs in f2fs_build_fault_attr()

- It missed to check validation of fault attrs in parse_options(),
let's fix to add check condition in f2fs_build_fault_attr().
- Use f2fs_build_fault_attr() in __sbi_store() to clean up code.

Signed-off-by: Chao Yu <chao@kernel.org>
Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
This commit is contained in:
Chao Yu 2024-05-07 11:38:47 +08:00 committed by Jaegeuk Kim
parent c521a6ab4a
commit 4ed886b187
3 changed files with 38 additions and 15 deletions

View File

@ -72,7 +72,7 @@ enum {
struct f2fs_fault_info { struct f2fs_fault_info {
atomic_t inject_ops; atomic_t inject_ops;
unsigned int inject_rate; int inject_rate;
unsigned int inject_type; unsigned int inject_type;
}; };
@ -4598,10 +4598,14 @@ static inline bool f2fs_need_verity(const struct inode *inode, pgoff_t idx)
} }
#ifdef CONFIG_F2FS_FAULT_INJECTION #ifdef CONFIG_F2FS_FAULT_INJECTION
extern void f2fs_build_fault_attr(struct f2fs_sb_info *sbi, unsigned int rate, extern int f2fs_build_fault_attr(struct f2fs_sb_info *sbi, unsigned long rate,
unsigned int type); unsigned long type);
#else #else
#define f2fs_build_fault_attr(sbi, rate, type) do { } while (0) static int f2fs_build_fault_attr(struct f2fs_sb_info *sbi, unsigned long rate,
unsigned long type)
{
return 0;
}
#endif #endif
static inline bool is_journalled_quota(struct f2fs_sb_info *sbi) static inline bool is_journalled_quota(struct f2fs_sb_info *sbi)

View File

@ -66,21 +66,31 @@ const char *f2fs_fault_name[FAULT_MAX] = {
[FAULT_NO_SEGMENT] = "no free segment", [FAULT_NO_SEGMENT] = "no free segment",
}; };
void f2fs_build_fault_attr(struct f2fs_sb_info *sbi, unsigned int rate, int f2fs_build_fault_attr(struct f2fs_sb_info *sbi, unsigned long rate,
unsigned int type) unsigned long type)
{ {
struct f2fs_fault_info *ffi = &F2FS_OPTION(sbi).fault_info; struct f2fs_fault_info *ffi = &F2FS_OPTION(sbi).fault_info;
if (rate) { if (rate) {
if (rate > INT_MAX)
return -EINVAL;
atomic_set(&ffi->inject_ops, 0); atomic_set(&ffi->inject_ops, 0);
ffi->inject_rate = rate; ffi->inject_rate = (int)rate;
} }
if (type) if (type) {
ffi->inject_type = type; if (type >= BIT(FAULT_MAX))
return -EINVAL;
ffi->inject_type = (unsigned int)type;
}
if (!rate && !type) if (!rate && !type)
memset(ffi, 0, sizeof(struct f2fs_fault_info)); memset(ffi, 0, sizeof(struct f2fs_fault_info));
else
f2fs_info(sbi,
"build fault injection attr: rate: %lu, type: 0x%lx",
rate, type);
return 0;
} }
#endif #endif
@ -886,14 +896,17 @@ static int parse_options(struct super_block *sb, char *options, bool is_remount)
case Opt_fault_injection: case Opt_fault_injection:
if (args->from && match_int(args, &arg)) if (args->from && match_int(args, &arg))
return -EINVAL; return -EINVAL;
f2fs_build_fault_attr(sbi, arg, F2FS_ALL_FAULT_TYPE); if (f2fs_build_fault_attr(sbi, arg,
F2FS_ALL_FAULT_TYPE))
return -EINVAL;
set_opt(sbi, FAULT_INJECTION); set_opt(sbi, FAULT_INJECTION);
break; break;
case Opt_fault_type: case Opt_fault_type:
if (args->from && match_int(args, &arg)) if (args->from && match_int(args, &arg))
return -EINVAL; return -EINVAL;
f2fs_build_fault_attr(sbi, 0, arg); if (f2fs_build_fault_attr(sbi, 0, arg))
return -EINVAL;
set_opt(sbi, FAULT_INJECTION); set_opt(sbi, FAULT_INJECTION);
break; break;
#else #else

View File

@ -484,10 +484,16 @@ out:
if (ret < 0) if (ret < 0)
return ret; return ret;
#ifdef CONFIG_F2FS_FAULT_INJECTION #ifdef CONFIG_F2FS_FAULT_INJECTION
if (a->struct_type == FAULT_INFO_TYPE && t >= BIT(FAULT_MAX)) if (a->struct_type == FAULT_INFO_TYPE) {
return -EINVAL; if (f2fs_build_fault_attr(sbi, 0, t))
if (a->struct_type == FAULT_INFO_RATE && t >= UINT_MAX) return -EINVAL;
return -EINVAL; return count;
}
if (a->struct_type == FAULT_INFO_RATE) {
if (f2fs_build_fault_attr(sbi, t, 0))
return -EINVAL;
return count;
}
#endif #endif
if (a->struct_type == RESERVED_BLOCKS) { if (a->struct_type == RESERVED_BLOCKS) {
spin_lock(&sbi->stat_lock); spin_lock(&sbi->stat_lock);