Skip to content

Commit 5e3365c

Browse files
chaseyuceladon
authored and
celadon
committed
f2fs: check validation of fault attrs in f2fs_build_fault_attr()
[ Upstream commit 4ed886b187f47447ad559619c48c086f432d2b77 ] - 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]> Signed-off-by: Sasha Levin <[email protected]>
1 parent 33a44a1 commit 5e3365c

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
@@ -65,7 +65,7 @@ enum {
6565

6666
struct f2fs_fault_info {
6767
atomic_t inject_ops;
68-
unsigned int inject_rate;
68+
int inject_rate;
6969
unsigned int inject_type;
7070
};
7171

@@ -4513,10 +4513,14 @@ static inline bool f2fs_need_verity(const struct inode *inode, pgoff_t idx)
45134513
}
45144514

45154515
#ifdef CONFIG_F2FS_FAULT_INJECTION
4516-
extern void f2fs_build_fault_attr(struct f2fs_sb_info *sbi, unsigned int rate,
4517-
unsigned int type);
4516+
extern int f2fs_build_fault_attr(struct f2fs_sb_info *sbi, unsigned long rate,
4517+
unsigned long type);
45184518
#else
4519-
#define f2fs_build_fault_attr(sbi, rate, type) do { } while (0)
4519+
static int f2fs_build_fault_attr(struct f2fs_sb_info *sbi, unsigned long rate,
4520+
unsigned long type)
4521+
{
4522+
return 0;
4523+
}
45204524
#endif
45214525

45224526
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
@@ -62,21 +62,31 @@ const char *f2fs_fault_name[FAULT_MAX] = {
6262
[FAULT_LOCK_OP] = "lock_op",
6363
};
6464

65-
void f2fs_build_fault_attr(struct f2fs_sb_info *sbi, unsigned int rate,
66-
unsigned int type)
65+
int f2fs_build_fault_attr(struct f2fs_sb_info *sbi, unsigned long rate,
66+
unsigned long type)
6767
{
6868
struct f2fs_fault_info *ffi = &F2FS_OPTION(sbi).fault_info;
6969

7070
if (rate) {
71+
if (rate > INT_MAX)
72+
return -EINVAL;
7173
atomic_set(&ffi->inject_ops, 0);
72-
ffi->inject_rate = rate;
74+
ffi->inject_rate = (int)rate;
7375
}
7476

75-
if (type)
76-
ffi->inject_type = type;
77+
if (type) {
78+
if (type >= BIT(FAULT_MAX))
79+
return -EINVAL;
80+
ffi->inject_type = (unsigned int)type;
81+
}
7782

7883
if (!rate && !type)
7984
memset(ffi, 0, sizeof(struct f2fs_fault_info));
85+
else
86+
f2fs_info(sbi,
87+
"build fault injection attr: rate: %lu, type: 0x%lx",
88+
rate, type);
89+
return 0;
8090
}
8191
#endif
8292

@@ -906,14 +916,17 @@ static int parse_options(struct super_block *sb, char *options, bool is_remount)
906916
case Opt_fault_injection:
907917
if (args->from && match_int(args, &arg))
908918
return -EINVAL;
909-
f2fs_build_fault_attr(sbi, arg, F2FS_ALL_FAULT_TYPE);
919+
if (f2fs_build_fault_attr(sbi, arg,
920+
F2FS_ALL_FAULT_TYPE))
921+
return -EINVAL;
910922
set_opt(sbi, FAULT_INJECTION);
911923
break;
912924

913925
case Opt_fault_type:
914926
if (args->from && match_int(args, &arg))
915927
return -EINVAL;
916-
f2fs_build_fault_attr(sbi, 0, arg);
928+
if (f2fs_build_fault_attr(sbi, 0, arg))
929+
return -EINVAL;
917930
set_opt(sbi, FAULT_INJECTION);
918931
break;
919932
#else

fs/f2fs/sysfs.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -431,10 +431,16 @@ static ssize_t __sbi_store(struct f2fs_attr *a,
431431
if (ret < 0)
432432
return ret;
433433
#ifdef CONFIG_F2FS_FAULT_INJECTION
434-
if (a->struct_type == FAULT_INFO_TYPE && t >= (1 << FAULT_MAX))
435-
return -EINVAL;
436-
if (a->struct_type == FAULT_INFO_RATE && t >= UINT_MAX)
437-
return -EINVAL;
434+
if (a->struct_type == FAULT_INFO_TYPE) {
435+
if (f2fs_build_fault_attr(sbi, 0, t))
436+
return -EINVAL;
437+
return count;
438+
}
439+
if (a->struct_type == FAULT_INFO_RATE) {
440+
if (f2fs_build_fault_attr(sbi, t, 0))
441+
return -EINVAL;
442+
return count;
443+
}
438444
#endif
439445
if (a->struct_type == RESERVED_BLOCKS) {
440446
spin_lock(&sbi->stat_lock);

0 commit comments

Comments
 (0)