add patch fix-comments-in-ext4_swap_extents
[ext4-patch-queue.git] / add-bitmap-block-validity-checks
blobfa37d6c4b0d9f3b9977a348fcd5978b8686c35a0
1 ext4: add validity checks for bitmap block numbers
3 https://bugzilla.kernel.org/show_bug.cgi?id=199181
5 Reported-by: Wen Xu <wen.xu@gatech.edu>
6 Signed-off-by: Theodore Ts'o <tytso@mit.edu>
7 Cc: stable@vger.kernel.org
8 ---
9  fs/ext4/balloc.c | 16 ++++++++++++++--
10  fs/ext4/ialloc.c |  7 +++++++
11  2 files changed, 21 insertions(+), 2 deletions(-)
13 diff --git a/fs/ext4/balloc.c b/fs/ext4/balloc.c
14 index f82c4966f4ce..a33d8fb1bf2a 100644
15 --- a/fs/ext4/balloc.c
16 +++ b/fs/ext4/balloc.c
17 @@ -338,20 +338,25 @@ static ext4_fsblk_t ext4_valid_block_bitmap(struct super_block *sb,
18         /* check whether block bitmap block number is set */
19         blk = ext4_block_bitmap(sb, desc);
20         offset = blk - group_first_block;
21 -       if (!ext4_test_bit(EXT4_B2C(sbi, offset), bh->b_data))
22 +       if (offset < 0 || EXT4_B2C(sbi, offset) >= sb->s_blocksize ||
23 +           !ext4_test_bit(EXT4_B2C(sbi, offset), bh->b_data))
24                 /* bad block bitmap */
25                 return blk;
27         /* check whether the inode bitmap block number is set */
28         blk = ext4_inode_bitmap(sb, desc);
29         offset = blk - group_first_block;
30 -       if (!ext4_test_bit(EXT4_B2C(sbi, offset), bh->b_data))
31 +       if (offset < 0 || EXT4_B2C(sbi, offset) >= sb->s_blocksize ||
32 +           !ext4_test_bit(EXT4_B2C(sbi, offset), bh->b_data))
33                 /* bad block bitmap */
34                 return blk;
36         /* check whether the inode table block number is set */
37         blk = ext4_inode_table(sb, desc);
38         offset = blk - group_first_block;
39 +       if (offset < 0 || EXT4_B2C(sbi, offset) >= sb->s_blocksize ||
40 +           EXT4_B2C(sbi, offset + sbi->s_itb_per_group) >= sb->s_blocksize)
41 +               return blk;
42         next_zero_bit = ext4_find_next_zero_bit(bh->b_data,
43                         EXT4_B2C(sbi, offset + sbi->s_itb_per_group),
44                         EXT4_B2C(sbi, offset));
45 @@ -417,6 +422,7 @@ struct buffer_head *
46  ext4_read_block_bitmap_nowait(struct super_block *sb, ext4_group_t block_group)
47  {
48         struct ext4_group_desc *desc;
49 +       struct ext4_sb_info *sbi = EXT4_SB(sb);
50         struct buffer_head *bh;
51         ext4_fsblk_t bitmap_blk;
52         int err;
53 @@ -425,6 +431,12 @@ ext4_read_block_bitmap_nowait(struct super_block *sb, ext4_group_t block_group)
54         if (!desc)
55                 return ERR_PTR(-EFSCORRUPTED);
56         bitmap_blk = ext4_block_bitmap(sb, desc);
57 +       if ((bitmap_blk <= le32_to_cpu(sbi->s_es->s_first_data_block)) ||
58 +           (bitmap_blk >= ext4_blocks_count(sbi->s_es))) {
59 +               ext4_error(sb, "Invalid block bitmap block %llu in "
60 +                          "block_group %u", bitmap_blk, block_group);
61 +               return ERR_PTR(-EFSCORRUPTED);
62 +       }
63         bh = sb_getblk(sb, bitmap_blk);
64         if (unlikely(!bh)) {
65                 ext4_error(sb, "Cannot get buffer for block bitmap - "
66 diff --git a/fs/ext4/ialloc.c b/fs/ext4/ialloc.c
67 index 3fa93665b4a3..df92e3ec9913 100644
68 --- a/fs/ext4/ialloc.c
69 +++ b/fs/ext4/ialloc.c
70 @@ -122,6 +122,7 @@ static struct buffer_head *
71  ext4_read_inode_bitmap(struct super_block *sb, ext4_group_t block_group)
72  {
73         struct ext4_group_desc *desc;
74 +       struct ext4_sb_info *sbi = EXT4_SB(sb);
75         struct buffer_head *bh = NULL;
76         ext4_fsblk_t bitmap_blk;
77         int err;
78 @@ -131,6 +132,12 @@ ext4_read_inode_bitmap(struct super_block *sb, ext4_group_t block_group)
79                 return ERR_PTR(-EFSCORRUPTED);
81         bitmap_blk = ext4_inode_bitmap(sb, desc);
82 +       if ((bitmap_blk <= le32_to_cpu(sbi->s_es->s_first_data_block)) ||
83 +           (bitmap_blk >= ext4_blocks_count(sbi->s_es))) {
84 +               ext4_error(sb, "Invalid inode bitmap blk %llu in "
85 +                          "block_group %u", bitmap_blk, block_group);
86 +               return ERR_PTR(-EFSCORRUPTED);
87 +       }
88         bh = sb_getblk(sb, bitmap_blk);
89         if (unlikely(!bh)) {
90                 ext4_error(sb, "Cannot read inode bitmap - "