Skip to content

Commit 64ba4b1

Browse files
tstruknamjaejeon
authored andcommitted
exfat: check if cluster num is valid
Syzbot reported slab-out-of-bounds read in exfat_clear_bitmap. This was triggered by reproducer calling truncute with size 0, which causes the following trace: BUG: KASAN: slab-out-of-bounds in exfat_clear_bitmap+0x147/0x490 fs/exfat/balloc.c:174 Read of size 8 at addr ffff888115aa9508 by task syz-executor251/365 Call Trace: __dump_stack lib/dump_stack.c:77 [inline] dump_stack_lvl+0x1e2/0x24b lib/dump_stack.c:118 print_address_description+0x81/0x3c0 mm/kasan/report.c:233 __kasan_report mm/kasan/report.c:419 [inline] kasan_report+0x1a4/0x1f0 mm/kasan/report.c:436 __asan_report_load8_noabort+0x14/0x20 mm/kasan/report_generic.c:309 exfat_clear_bitmap+0x147/0x490 fs/exfat/balloc.c:174 exfat_free_cluster+0x25a/0x4a0 fs/exfat/fatent.c:181 __exfat_truncate+0x99e/0xe00 fs/exfat/file.c:217 exfat_truncate+0x11b/0x4f0 fs/exfat/file.c:243 exfat_setattr+0xa03/0xd40 fs/exfat/file.c:339 notify_change+0xb76/0xe10 fs/attr.c:336 do_truncate+0x1ea/0x2d0 fs/open.c:65 Move the is_valid_cluster() helper from fatent.c to a common header to make it reusable in other *.c files. And add is_valid_cluster() to validate if cluster number is within valid range in exfat_clear_bitmap() and exfat_set_bitmap(). Link: https://syzkaller.appspot.com/bug?id=50381fc73821ecae743b8cf24b4c9a04776f767c Reported-by: [email protected] Fixes: 1e49a94 ("exfat: add bitmap operations") Cc: [email protected] # v5.7+ Signed-off-by: Tadeusz Struk <[email protected]> Reviewed-by: Sungjong Seo <[email protected]> Signed-off-by: Namjae Jeon <[email protected]>
1 parent 1b61383 commit 64ba4b1

File tree

3 files changed

+12
-8
lines changed

3 files changed

+12
-8
lines changed

fs/exfat/balloc.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,9 @@ int exfat_set_bitmap(struct inode *inode, unsigned int clu, bool sync)
148148
struct super_block *sb = inode->i_sb;
149149
struct exfat_sb_info *sbi = EXFAT_SB(sb);
150150

151-
WARN_ON(clu < EXFAT_FIRST_CLUSTER);
151+
if (!is_valid_cluster(sbi, clu))
152+
return -EINVAL;
153+
152154
ent_idx = CLUSTER_TO_BITMAP_ENT(clu);
153155
i = BITMAP_OFFSET_SECTOR_INDEX(sb, ent_idx);
154156
b = BITMAP_OFFSET_BIT_IN_SECTOR(sb, ent_idx);
@@ -166,7 +168,9 @@ void exfat_clear_bitmap(struct inode *inode, unsigned int clu, bool sync)
166168
struct exfat_sb_info *sbi = EXFAT_SB(sb);
167169
struct exfat_mount_options *opts = &sbi->options;
168170

169-
WARN_ON(clu < EXFAT_FIRST_CLUSTER);
171+
if (!is_valid_cluster(sbi, clu))
172+
return;
173+
170174
ent_idx = CLUSTER_TO_BITMAP_ENT(clu);
171175
i = BITMAP_OFFSET_SECTOR_INDEX(sb, ent_idx);
172176
b = BITMAP_OFFSET_BIT_IN_SECTOR(sb, ent_idx);

fs/exfat/exfat_fs.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,12 @@ static inline int exfat_sector_to_cluster(struct exfat_sb_info *sbi,
382382
EXFAT_RESERVED_CLUSTERS;
383383
}
384384

385+
static inline bool is_valid_cluster(struct exfat_sb_info *sbi,
386+
unsigned int clus)
387+
{
388+
return clus >= EXFAT_FIRST_CLUSTER && clus < sbi->num_clusters;
389+
}
390+
385391
/* super.c */
386392
int exfat_set_volume_dirty(struct super_block *sb);
387393
int exfat_clear_volume_dirty(struct super_block *sb);

fs/exfat/fatent.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,6 @@ int exfat_ent_set(struct super_block *sb, unsigned int loc,
8282
return 0;
8383
}
8484

85-
static inline bool is_valid_cluster(struct exfat_sb_info *sbi,
86-
unsigned int clus)
87-
{
88-
return clus >= EXFAT_FIRST_CLUSTER && clus < sbi->num_clusters;
89-
}
90-
9185
int exfat_ent_get(struct super_block *sb, unsigned int loc,
9286
unsigned int *content)
9387
{

0 commit comments

Comments
 (0)