add patch fix-sleep-in-atomic-context-in-grab_mapping_entry
[ext4-patch-queue.git] / dont-read-out-of-bounds-when-checking-for-in-inode-xattrs
blob244c18864ee44e2cba547dbd028b100655f9b24e
1 ext4: don't read out of bounds when checking for in-inode xattrs
3 From: Eric Biggers <ebiggers@google.com>
5 With i_extra_isize equal to or close to the available space, it was
6 possible for us to read past the end of the inode when trying to detect
7 or validate in-inode xattrs.  Fix this by checking for the needed extra
8 space first.
10 This patch shouldn't have any noticeable effect on
11 non-corrupted/non-malicious filesystems.
13 Signed-off-by: Eric Biggers <ebiggers@google.com>
14 Signed-off-by: Theodore Ts'o <tytso@mit.edu>
15 Reviewed-by: Andreas Dilger <adilger@dilger.ca>
16 ---
17  fs/ext4/inode.c | 4 +++-
18  fs/ext4/xattr.c | 5 ++---
19  2 files changed, 5 insertions(+), 4 deletions(-)
21 diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
22 index e3e197898c66..59a518ad6bb2 100644
23 --- a/fs/ext4/inode.c
24 +++ b/fs/ext4/inode.c
25 @@ -4527,7 +4527,9 @@ static inline void ext4_iget_extra_inode(struct inode *inode,
26  {
27         __le32 *magic = (void *)raw_inode +
28                         EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize;
29 -       if (*magic == cpu_to_le32(EXT4_XATTR_MAGIC)) {
30 +       if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize + sizeof(__le32) <=
31 +           EXT4_INODE_SIZE(inode->i_sb) &&
32 +           *magic == cpu_to_le32(EXT4_XATTR_MAGIC)) {
33                 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
34                 ext4_find_inline_data_nolock(inode);
35         } else
36 diff --git a/fs/ext4/xattr.c b/fs/ext4/xattr.c
37 index 1846e9168f80..59c9ec7eabae 100644
38 --- a/fs/ext4/xattr.c
39 +++ b/fs/ext4/xattr.c
40 @@ -231,13 +231,12 @@ static int
41  __xattr_check_inode(struct inode *inode, struct ext4_xattr_ibody_header *header,
42                          void *end, const char *function, unsigned int line)
43  {
44 -       struct ext4_xattr_entry *entry = IFIRST(header);
45         int error = -EFSCORRUPTED;
47 -       if (((void *) header >= end) ||
48 +       if (end - (void *)header < sizeof(*header) + sizeof(u32) ||
49             (header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)))
50                 goto errout;
51 -       error = ext4_xattr_check_names(entry, end, entry);
52 +       error = ext4_xattr_check_names(IFIRST(header), end, IFIRST(header));
53  errout:
54         if (error)
55                 __ext4_error_inode(inode, function, line, 0,