]> git.itanic.dy.fi Git - linux-stable/commitdiff
ext4: add bounds checking in get_max_inline_xattr_value_size()
authorTheodore Ts'o <tytso@mit.edu>
Fri, 12 May 2023 19:11:02 +0000 (15:11 -0400)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Wed, 17 May 2023 09:50:30 +0000 (11:50 +0200)
commit 2220eaf90992c11d888fe771055d4de330385f01 upstream.

Normally the extended attributes in the inode body would have been
checked when the inode is first opened, but if someone is writing to
the block device while the file system is mounted, it's possible for
the inode table to get corrupted.  Add bounds checking to avoid
reading beyond the end of allocated memory if this happens.

Reported-by: syzbot+1966db24521e5f6e23f7@syzkaller.appspotmail.com
Link: https://syzkaller.appspot.com/bug?extid=1966db24521e5f6e23f7
Cc: stable@kernel.org
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
fs/ext4/inline.c

index 6bd2c53b431143e7de27984cc6a5faf799aebf70..a7cd29218377c9b669e7f1fcc6e843f5a144ad03 100644 (file)
@@ -33,6 +33,7 @@ static int get_max_inline_xattr_value_size(struct inode *inode,
        struct ext4_xattr_ibody_header *header;
        struct ext4_xattr_entry *entry;
        struct ext4_inode *raw_inode;
+       void *end;
        int free, min_offs;
 
        if (!EXT4_INODE_HAS_XATTR_SPACE(inode))
@@ -56,14 +57,23 @@ static int get_max_inline_xattr_value_size(struct inode *inode,
        raw_inode = ext4_raw_inode(iloc);
        header = IHDR(inode, raw_inode);
        entry = IFIRST(header);
+       end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
 
        /* Compute min_offs. */
-       for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
+       while (!IS_LAST_ENTRY(entry)) {
+               void *next = EXT4_XATTR_NEXT(entry);
+
+               if (next >= end) {
+                       EXT4_ERROR_INODE(inode,
+                                        "corrupt xattr in inline inode");
+                       return 0;
+               }
                if (!entry->e_value_inum && entry->e_value_size) {
                        size_t offs = le16_to_cpu(entry->e_value_offs);
                        if (offs < min_offs)
                                min_offs = offs;
                }
+               entry = next;
        }
        free = min_offs -
                ((void *)entry - (void *)IFIRST(header)) - sizeof(__u32);