Skip to content

Commit 4ed886b

Browse files
chaseyuJaegeuk Kim
authored andcommitted
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 <[email protected]> Signed-off-by: Jaegeuk Kim <[email protected]>
1 parent c521a6a commit 4ed886b

File tree

3 files changed

+38
-15
lines changed

3 files changed

+38
-15
lines changed

fs/f2fs/f2fs.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ enum {
7272

7373
struct f2fs_fault_info {
7474
atomic_t inject_ops;
75-
unsigned int inject_rate;
75+
int inject_rate;
7676
unsigned int inject_type;
7777
};
7878

@@ -4598,10 +4598,14 @@ static inline bool f2fs_need_verity(const struct inode *inode, pgoff_t idx)
45984598
}
45994599

46004600
#ifdef CONFIG_F2FS_FAULT_INJECTION
4601-
extern void f2fs_build_fault_attr(struct f2fs_sb_info *sbi, unsigned int rate,
4602-
unsigned int type);
4601+
extern int f2fs_build_fault_attr(struct f2fs_sb_info *sbi, unsigned long rate,
4602+
unsigned long type);
46034603
#else
4604-
#define f2fs_build_fault_attr(sbi, rate, type) do { } while (0)
4604+
static int f2fs_build_fault_attr(struct f2fs_sb_info *sbi, unsigned long rate,
4605+
unsigned long type)
4606+
{
4607+
return 0;
4608+
}
46054609
#endif
46064610

46074611
static inline bool is_journalled_quota(struct f2fs_sb_info *sbi)

fs/f2fs/super.c

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,21 +66,31 @@ const char *f2fs_fault_name[FAULT_MAX] = {
6666
[FAULT_NO_SEGMENT] = "no free segment",
6767
};
6868

69-
void f2fs_build_fault_attr(struct f2fs_sb_info *sbi, unsigned int rate,
70-
unsigned int type)
69+
int f2fs_build_fault_attr(struct f2fs_sb_info *sbi, unsigned long rate,
70+
unsigned long type)
7171
{
7272
struct f2fs_fault_info *ffi = &F2FS_OPTION(sbi).fault_info;
7373

7474
if (rate) {
75+
if (rate > INT_MAX)
76+
return -EINVAL;
7577
atomic_set(&ffi->inject_ops, 0);
76-
ffi->inject_rate = rate;
78+
ffi->inject_rate = (int)rate;
7779
}
7880

79-
if (type)
80-
ffi->inject_type = type;
81+
if (type) {
82+
if (type >= BIT(FAULT_MAX))
83+
return -EINVAL;
84+
ffi->inject_type = (unsigned int)type;
85+
}
8186

8287
if (!rate && !type)
8388
memset(ffi, 0, sizeof(struct f2fs_fault_info));
89+
else
90+
f2fs_info(sbi,
91+
"build fault injection attr: rate: %lu, type: 0x%lx",
92+
rate, type);
93+
return 0;
8494
}
8595
#endif
8696

@@ -886,14 +896,17 @@ static int parse_options(struct super_block *sb, char *options, bool is_remount)
886896
case Opt_fault_injection:
887897
if (args->from && match_int(args, &arg))
888898
return -EINVAL;
889-
f2fs_build_fault_attr(sbi, arg, F2FS_ALL_FAULT_TYPE);
899+
if (f2fs_build_fault_attr(sbi, arg,
900+
F2FS_ALL_FAULT_TYPE))
901+
return -EINVAL;
890902
set_opt(sbi, FAULT_INJECTION);
891903
break;
892904

893905
case Opt_fault_type:
894906
if (args->from && match_int(args, &arg))
895907
return -EINVAL;
896-
f2fs_build_fault_attr(sbi, 0, arg);
908+
if (f2fs_build_fault_attr(sbi, 0, arg))
909+
return -EINVAL;
897910
set_opt(sbi, FAULT_INJECTION);
898911
break;
899912
#else

fs/f2fs/sysfs.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -484,10 +484,16 @@ static ssize_t __sbi_store(struct f2fs_attr *a,
484484
if (ret < 0)
485485
return ret;
486486
#ifdef CONFIG_F2FS_FAULT_INJECTION
487-
if (a->struct_type == FAULT_INFO_TYPE && t >= BIT(FAULT_MAX))
488-
return -EINVAL;
489-
if (a->struct_type == FAULT_INFO_RATE && t >= UINT_MAX)
490-
return -EINVAL;
487+
if (a->struct_type == FAULT_INFO_TYPE) {
488+
if (f2fs_build_fault_attr(sbi, 0, t))
489+
return -EINVAL;
490+
return count;
491+
}
492+
if (a->struct_type == FAULT_INFO_RATE) {
493+
if (f2fs_build_fault_attr(sbi, t, 0))
494+
return -EINVAL;
495+
return count;
496+
}
491497
#endif
492498
if (a->struct_type == RESERVED_BLOCKS) {
493499
spin_lock(&sbi->stat_lock);

0 commit comments

Comments
 (0)