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