libfsimage: validate untrusted on-disk metadata in pygrub filesystem plugins
cover libfsimage: validate untrusted on-disk metadata in pygrub filesystem plugins
libfsimage is the set of read-only filesystem parsers that pygrub links in to read a guest's /boot and grub configuration. When pygrub runs, these parsers are handed a disk image that is fully controlled by the guest and are executed in dom0. The on-disk metadata of that image is therefore untrusted input, in exactly the same way the iso9660 metadata addressed by XSA-497 was, and the superblock sanity checking added by XSA-443 does not reach the inode-level and directory-level fields touched here.
Auditing the ext2, reiserfs, ufs and xfs plugins turned up a number of on-disk fields that are used without validation. This series adds bounds/validity checks at the point each field is consumed. The four patches are independent and each is confined to a single plugin:
[PATCH 1/4] libfsimage/ext2: validate attacker-controlled superblock and
extent fields
[PATCH 2/4] libfsimage/reiserfs: validate item counts and journal length
[PATCH 3/4] libfsimage/ufs: validate superblock divisors and directory
record length
[PATCH 4/4] libfsimage/xfs: bound inline extent count and directory
btree traversals
The 14 issues fixed fall into four classes:
- Out-of-bounds write (CWE-787): ext2 s_log_block_size sizes the fixed FSYS_BUFLEN block buffers, so an oversized value makes ext2_rdfsb() write a block past the buffer.
- Out-of-bounds read (CWE-125): ext4 eh_entries; reiserfs blk_nr_item, ih_entry_count and journal j_len; xfs di_nextents. Each is iterated or indexed as an array count without being bounded to the structure that holds it.
- Divide-by-zero / SIGFPE (CWE-369): ext2 s_inode_size and s_inodes_per_group; ufs fs_inopb and fs_ipg. Each is used as a divisor and can be zero.
- Uncontrolled recursion / infinite loop, i.e. denial of service (CWE-674 / CWE-835): ext2 ext4_recurse_extent_index() (self-referencing index block -> stack overflow) and a zero directory rec_len; ufs a zero d_reclen directory entry; xfs the bmap btree descent and the directory da-btree descent in first_dentry() (a node whose child pointer never reaches a leaf, e.g. one pointing to itself, loops forever).
Trust boundary and scope
------------------------
The attacker is a guest that supplies the disk image; the affected code runs in the pygrub process in dom0 while parsing that image's filesystem metadata. The impact demonstrated is limited to crashing or hanging the pygrub parse (out-of-bounds read/write within the plugin, SIGFPE, or an unterminated loop). No sandbox or privilege escape is claimed, and these are not proposed as security fixes: pygrub is only security supported when run de-privileged (XSA-508), so this series is hardening of the same unvalidated-on-disk-metadata class as XSA-497 (iso9660) rather than a fix for a supported configuration.
Each issue was reproduced against a locally built, ASAN-instrumented static link of the affected plugin driven through the normal mount + open path with a hand-crafted image. As a regression control, a genuine well-formed image of each of the four filesystems (e.g. a plain mke2fs ext2 image) still mounts and resolves a file after the change, so the added validation does not reject legitimate on-disk values.
SounLabs (4):
libfsimage/ext2: validate attacker-controlled superblock and extent
fields
libfsimage/reiserfs: validate item counts and journal length libfsimage/ufs: validate superblock divisors and directory record
length
libfsimage/xfs: bound inline extent count and directory btree
traversals
tools/libfsimage/ext2fs/fsys_ext2fs.c | 48 +++++++++++++++++-- tools/libfsimage/reiserfs/fsys_reiserfs.c | 46 +++++++++++++++++- tools/libfsimage/ufs/fsys_ufs.c | 11 ++++- tools/libfsimage/xfs/fsys_xfs.c | 57 +++++++++++++++++++++-- 4 files changed, 153 insertions(+), 9 deletions(-)
Signature · 1 line
2.52.0.windows.1
PATCH 1/4 libfsimage/ext2: validate attacker-controlled superblock and extent fields
libfsimage is used by pygrub to parse the filesystem of an untrusted guest disk image from dom0. Several ext2/ext4 metadata fields are used without validation:
- s_log_block_size sizes the INODE/DATABLOCK1/DATABLOCK2 buffers inside the fixed FSYS_BUFLEN region; a large value makes ext2_rdfsb() write a block past the buffer (heap out-of-bounds write).
- s_inode_size and s_inodes_per_group are used as divisors and can be zero (SIGFPE).
- an ext4 extent header's eh_entries is iterated without bounding it to the region that holds the entries (heap out-of-bounds read).
- ext4_recurse_extent_index() has no depth limit, so a self-referencing index block recurses until the stack overflows.
Reject the out-of-range superblock values at mount time, and bound both the extent-entry count (against the inode inline area or block that holds the header) and the extent-tree recursion depth.
This is the same class of issue addressed for the iso9660 plugin by XSA-497. pygrub is only security supported when run de-privileged (XSA-508), so this is hardening rather than a security fix.
--- tools/libfsimage/ext2fs/fsys_ext2fs.c | 48 ++++++++++++++++++++++++--- 1 file changed, 44 insertions(+), 4 deletions(-)
tools/libfsimage/ext2fs/fsys_ext2fs.c +44 -4
@@ -337,6 +337,22 @@ ext2fs_mount (fsi_file_t *ffi, const char *options) || SUPERBLOCK->s_magic != EXT2_SUPER_MAGIC) retval =0 ; +/* s_log_block_size, s_inode_size and s_inodes_per_group come straight +* from the untrusted on-disk superblock. The buffer-layout macros +* (INODE, DATABLOCK1, DATABLOCK2) place three block-sized buffers in +* the fixed FSYS_BUFLEN region, and later code divides by the inode +* size and by the inodes-per-group count. Reject values that would +* overflow the buffer or divide by zero. s_log_block_size is tested +* first (short-circuit) so EXT2_BLOCK_SIZE() never shifts out of range. */ +if (retval + && (SUPERBLOCK ->s_log_block_size >6 + ||sizeof (struct ext2_super_block ) +sizeof (struct ext2_inode ) + +3UL * (unsigned long )EXT2_BLOCK_SIZE (SUPERBLOCK ) >FSYS_BUFLEN + ||SUPERBLOCK ->s_inode_size ==0 + ||SUPERBLOCK ->s_inode_size >EXT2_BLOCK_SIZE (SUPERBLOCK ) + ||SUPERBLOCK ->s_inodes_per_group ==0 )) + retval =0 ; +return retval; }
@@ -351,14 +367,29 @@ ext2_rdfsb (fsi_file_t *ffi, int fsblock, char *buffer) EXT2_BLOCK_SIZE (SUPERBLOCK), (char *) buffer); } -/* Walk through extents index tree to find the good leaf */ +/* ext4 extent trees are at most 5 levels deep in the on-disk format. */ +#define EXT4_MAX_EXTENT_DEPTH 5 + +/* Walk through extents index tree to find the good leaf. +* max_bytes bounds the region the header and its entries occupy: the +* inode's 60-byte inline area for the first call, a full block for an +* index block read from disk. This stops a corrupted eh_entries from +* being iterated past that region (both ext4_extent and ext4_extent_idx +* are the same size). depth bounds the recursion so a self-referencing +* index block cannot spin or overflow the stack. */ static struct ext4_extent_header * -ext4_recurse_extent_index (fsi_file_t *ffi,struct ext4_extent_header *extent_block,int logical_block) +ext4_recurse_extent_index (fsi_file_t *ffi,struct ext4_extent_header *extent_block,int logical_block,unsigned int max_bytes,int depth) {int i;struct ext4_extent_idx *index = (struct ext4_extent_idx *) (extent_block +1 ); +if (depth >EXT4_MAX_EXTENT_DEPTH ) +return NULL ;if (extent_block->eh_magic !=EXT4_EXT_MAGIC )return NULL ; +if (sizeof (struct ext4_extent_header ) + + (unsigned int ) extent_block->eh_entries *sizeof (struct ext4_extent ) + > max_bytes) +return NULL ;if (extent_block->eh_depth ==0 )return extent_block;for (i =0 ; i < extent_block->eh_entries; i++)
@@ -368,7 +399,7 @@ ext4_recurse_extent_index(fsi_file_t *ffi, struct ext4_extent_header *extent_blo }if (i ==0 || !ext2_rdfsb (ffi, index[i-1 ].ei_leaf,DATABLOCK1 ))return NULL ; -return (ext4_recurse_extent_index (ffi, (struct ext4_extent_header *)DATABLOCK1 , logical_block)); +return (ext4_recurse_extent_index (ffi, (struct ext4_extent_header *)DATABLOCK1 , logical_block,EXT2_BLOCK_SIZE (SUPERBLOCK ), depth +1 )); }
@@ -483,7 +514,7 @@ ext2fs_block_map (fsi_file_t *ffi, int logical_block) {int i;struct ext4_extent_header *extent_hdr = -ext4_recurse_extent_index (ffi, (struct ext4_extent_header *)INODE ->i_block, logical_block); +ext4_recurse_extent_index (ffi, (struct ext4_extent_header *)INODE ->i_block, logical_block,sizeof (INODE ->i_block),0 );struct ext4_extent *extent = (struct ext4_extent *) (extent_hdr +1 );if ( extent_hdr ==NULL || extent_hdr->eh_magic !=EXT4_EXT_MAGIC ) {
@@ -863,6 +894,15 @@ ext2fs_dir (fsi_file_t *ffi, char *dirname) } off = loc & (EXT2_BLOCK_SIZE (SUPERBLOCK ) -1 ); dp = (struct ext2_dir_entry *) (DATABLOCK2 + off); +/* rec_len is attacker-controlled. It must be at least the fixed +* 8-byte directory-entry header; a smaller value (in particular 0) +* would never advance loc and would spin forever. */ +if (dp->rec_len <8 ) + { + errnum =ERR_FSYS_CORRUPT ; + *rest = ch; +return 0 ; + }/* advance loc prematurely to next on-disk directory entry */ loc += dp->rec_len;
Signature · 1 line
2.52.0.windows.1
PATCH 2/4 libfsimage/reiserfs: validate item counts and journal length
In reply to SounLabslibfsimage is used by pygrub to parse the filesystem of an untrusted guest disk image from dom0. Three reiserfs on-disk counts are used without validation:
- block_head.blk_nr_item is iterated as item headers / keys in search_stat() and next_key() without checking they fit in the block (heap OOB read).
- item_head.ih_entry_count is iterated as directory headers in reiserfs_dir() without bounding it to the item body (heap OOB read).
- the journal descriptor/commit j_len indexes commit.j_realblock[], which holds JOURNAL_TRANS_HALF entries, in journal_init() and block_read() without a bound (stack OOB read).
Check each count against the structure that holds it: blk_nr_item against the block size when a node is read (including the root), ih_entry_count against ih_item_len, and j_len against 2 * JOURNAL_TRANS_HALF.
Same class of issue as XSA-497 (iso9660). pygrub is only security supported when run de-privileged (XSA-508), so this is hardening rather than a security fix.
--- tools/libfsimage/reiserfs/fsys_reiserfs.c | 46 ++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-)
tools/libfsimage/reiserfs/fsys_reiserfs.c +45 -1
@@ -420,6 +420,13 @@ block_read (fsi_file_t *ffi, int blockNr, int start, int len, char *buffer)return 0 ; j_len = desc.j_len; +/* See journal_init(): j_len must not index commit.j_realblock[] +* (JOURNAL_TRANS_HALF entries) out of bounds. */ +if (j_len >JOURNAL_TRANS_HALF *2 ) + { + errnum =ERR_FSYS_CORRUPT ; +return 0 ; + }while (i < j_len && i <JOURNAL_TRANS_HALF )if (desc.j_realblock[i++] == blockNr) goto found;
@@ -497,7 +504,11 @@ journal_init (fsi_file_t *ffi) commit_block = (desc_block + desc.j_len +1 ) & (block_count -1 );journal_read (ffi, commit_block,sizeof (commit), (char *) &commit);if (desc.j_trans_id != commit.j_trans_id - || desc.j_len != commit.j_len) + || desc.j_len != commit.j_len +/* j_len indexes desc.j_realblock[] and commit.j_realblock[], +* which together hold at most 2 * JOURNAL_TRANS_HALF entries; +* a larger (attacker-controlled) value reads past 'commit'. */ + || desc.j_len >JOURNAL_TRANS_HALF *2 )/* no more valid transactions */ break ;
@@ -555,6 +566,8 @@ journal_init (fsi_file_t *ffi)return errnum ==0 ; } +static int blkh_nr_item_ok (char *cache,unsigned int blocksize); +/* check filesystem types and read superblock into memory buffer */ static int reiserfs_mount (fsi_file_t *ffi,const char *options)
@@ -646,6 +659,8 @@ reiserfs_mount (fsi_file_t *ffi, const char *options)if (INFO ->tree_depth >=MAX_HEIGHT )return 0 ; +if (!blkh_nr_item_ok (ROOT ,INFO ->blocksize)) +return 0 ;if (INFO ->tree_depth ==DISK_LEAF_NODE_LEVEL ) {/* There is only one node in the whole filesystem,
@@ -676,6 +691,22 @@ reiserfs_mount (fsi_file_t *ffi, const char *options)* efficient, but this really doesn't hurt for grub. */ +/* A block_head's blk_nr_item is attacker-controlled. Confirm the keys or +* item headers it claims actually fit inside the on-disk block, so the +* item/key scans in search_stat() and next_key() cannot run past the +* cached block. */ +static int +blkh_nr_item_ok (char *cache,unsigned int blocksize) +{ +unsigned int nr =BLOCKHEAD (cache)->blk_nr_item; +if (BLOCKHEAD (cache)->blk_level ==DISK_LEAF_NODE_LEVEL ) +/* leaf: block_head followed by nr_item item headers */ +return (unsigned long ) nr *IH_SIZE +BLKH_SIZE <= blocksize; +/* internal: block_head, nr_item keys, nr_item + 1 disk-child pointers */ +return (unsigned long ) nr * (KEY_SIZE +DC_SIZE ) +DC_SIZE +BLKH_SIZE + <= blocksize; +} +/* Read in the node at the current path and depth into the node cache. * You must set INFO->blocks[depth] before. */
@@ -707,6 +738,11 @@ read_tree_node (fsi_file_t *ffi, unsigned int blockNr, int depth) errnum =ERR_FSYS_CORRUPT ;return 0 ; } +if (!blkh_nr_item_ok (cache,INFO ->blocksize)) + { + errnum =ERR_FSYS_CORRUPT ; +return 0 ; + }INFO ->blocks[depth] = blockNr;return cache;
@@ -1140,6 +1176,14 @@ reiserfs_dir (fsi_file_t *ffi, char *dirname) name_end =INFO ->current_item +INFO ->current_ih->ih_item_len; de_head = (struct reiserfs_de_head *)INFO ->current_item; num_entries =INFO ->current_ih->u.ih_entry_count; +/* ih_entry_count is attacker-controlled; the directory headers it +* counts (DEH_SIZE each) must fit within this item's body. */ +if ((unsigned long ) num_entries *DEH_SIZE + >INFO ->current_ih->ih_item_len) + { + errnum =ERR_FSYS_CORRUPT ; +return 0 ; + }while (num_entries >0 ) {char *filename =INFO ->current_item + de_head->deh_location;
Signature · 1 line
2.52.0.windows.1
PATCH 3/4 libfsimage/ufs: validate superblock divisors and directory record length
In reply to SounLabslibfsimage is used by pygrub to parse the filesystem of an untrusted guest disk image from dom0. Three ufs on-disk fields are used without validation:
- fs_inopb and fs_ipg are used as divisors by the itoo()/itog()/itod() macros in openi(); a zero value divides by zero (SIGFPE).
- a directory entry's d_reclen advances the scan in dlook(); a zero value never advances and spins forever.
Reject a non-positive fs_inopb or fs_ipg at mount time, and stop the directory scan on a zero d_reclen.
Same class of issue as XSA-497 (iso9660). pygrub is only security supported when run de-privileged (XSA-508), so this is hardening rather than a security fix.
--- tools/libfsimage/ufs/fsys_ufs.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)
tools/libfsimage/ufs/fsys_ufs.c +10 -1
@@ -49,7 +49,12 @@ ufs_mount(fsi_file_t *ffi, const char *options)if (/*! IS_PC_SLICE_TYPE_SOLARIS(current_slice) || */ !devread (ffi,UFS_SBLOCK ,0 ,UFS_SBSIZE , (char *)SUPERBLOCK ) ||SUPERBLOCK ->fs_magic !=UFS_MAGIC || -MAXBSIZE <SUPERBLOCK ->fs_bsize) +MAXBSIZE <SUPERBLOCK ->fs_bsize || +/* fs_inopb and fs_ipg are attacker-controlled and are used as +* divisors by the itoo()/itog()/itod() macros in openi(); a +* zero (or negative) value divides by zero (SIGFPE). */ +SUPERBLOCK ->fs_inopb <=0 || +SUPERBLOCK ->fs_ipg <=0 )return 0 ;return 1 ;
@@ -257,6 +262,10 @@ dlook(fsi_file_t *ffi, grub_ino_t dir_ino, char *name) dp = (struct direct *)(DIRENT + off);if (dp->d_ino &&substring (name, dp->d_name) ==0 )return (dp->d_ino); +/* d_reclen is attacker-controlled; a zero value would never +* advance loc and would spin forever. */ +if (dp->d_reclen ==0 ) +return (0 ); loc += dp->d_reclen; }return (0 );
Signature · 1 line
2.52.0.windows.1
PATCH 4/4 libfsimage/xfs: bound inline extent count and directory btree traversals
In reply to SounLabslibfsimage is used by pygrub to parse the filesystem of an untrusted guest disk image from dom0. Several xfs on-disk fields are used without validation, leading to an out-of-bounds read and to unbounded tree descents that loop forever:
- init_extents(), XFS_DINODE_FMT_EXTENTS: di_nextents is taken as the number of inline extent records; next_extent() then iterates that many records from the inode data fork without checking they fit (heap OOB read).
- init_extents(), XFS_DINODE_FMT_BTREE: the root-to-leaf descent follows bb_level/ptr0 with no depth limit, so a node whose bb_level does not decrease (e.g. one pointing to itself) loops forever.
- first_dentry(), directory da-btree: the root-to-leaf descent assigns xfs.dablk = btree[0].before with no depth limit or cycle detection, so a self-referential or cyclic directory node loops forever. This is the loop reached by the di_nextents reproducer as well, which therefore triggers both the OOB read above and this unbounded da-descent hang. When the descent is bounded and the directory yields no entries, first_dentry() returns NULL and the caller in xfs_dir() dereferenced that NULL name via substring(dirname, NULL) (NULL read).
Cap di_nextents to the number of records that fit in the inode data fork, bound both the bmap btree and the directory da-btree descents to a fixed maximum depth, and have xfs_dir() fail the lookup gracefully when a directory yields no first entry.
Same class of issue as XSA-497 (iso9660); the superblock sanity check added by XSA-443 does not cover these inode-level and btree-level fields. pygrub is only security supported when run de-privileged (XSA-508), so this is hardening rather than a security fix.
--- tools/libfsimage/xfs/fsys_xfs.c | 57 +++++++++++++++++++++++++++++++-- 1 file changed, 54 insertions(+), 3 deletions(-)
tools/libfsimage/xfs/fsys_xfs.c +54 -3
@@ -201,20 +201,42 @@ di_read (fsi_file_t *ffi, xfs_ino_t ino)return 1 ; } +/* An XFS bmap btree is only a handful of levels deep; use a generous cap +* to bound a traversal that would otherwise loop forever on a node whose +* bb_level does not decrease (e.g. one that points to itself). */ +#define XFS_BM_MAXLEVELS 32 + +/* A directory da-btree is likewise only a few levels deep; bound the +* root-to-leaf descent in first_dentry() so a node whose btree[0].before +* never reaches a leaf (e.g. one that points to itself) cannot loop forever. */ +#define XFS_DA_MAXLEVELS 32 +static void init_extents (fsi_file_t *ffi) {xfs_bmbt_ptr_t ptr0;xfs_btree_lblock_t h; +int levels;switch (icore.di_format) { -case XFS_DINODE_FMT_EXTENTS : +case XFS_DINODE_FMT_EXTENTS : { +/* di_nextents is attacker-controlled; the inline extent +* records (xfs_bmbt_rec_32_t each) must fit in the inode's +* data fork, or next_extent() reads past the inode buffer. */ +int fork_bytes = + (icore.di_forkoff ? (icore.di_forkoff <<3 ) : xfs.isize) + - (int ) offsetof(xfs_dinode_t , di_u); +unsigned int maxext = fork_bytes >0 + ? (unsigned int ) fork_bytes /sizeof (xfs_bmbt_rec_32_t) :0 ; xfs.xt = inode->di_u.di_bmx; xfs.nextents =le32 (icore.di_nextents); +if (xfs.nextents > maxext) + xfs.nextents = maxext;break ; + }case XFS_DINODE_FMT_BTREE : ptr0 = xfs.ptr0; -for (;;) { +for (levels =0 ; ; levels++) { xfs.daddr =fsb2daddr (le64 (ptr0));devread (ffi, xfs.daddr,0 ,sizeof (xfs_btree_lblock_t), (char *)&h);
@@ -224,6 +246,12 @@ init_extents (fsi_file_t *ffi) xfs.fpos =sizeof (xfs_btree_block_t);return ; } +if (levels >=XFS_BM_MAXLEVELS ) { +/* malformed btree: refuse to iterate */ + xfs.nextents =0 ; + xfs.next =0 ; +return ; + }devread (ffi, xfs.daddr, xfs.btnode_ptr0_off,sizeof (xfs_bmbt_ptr_t), (char *)&ptr0); }
@@ -392,6 +420,8 @@ next_dentry (fsi_file_t *ffi, xfs_ino_t *ino)static char *first_dentry (fsi_file_t *ffi,xfs_ino_t *ino) { +int da_levels; + xfs.forw =0 ;switch (icore.di_format) {case XFS_DINODE_FMT_LOCAL :
@@ -413,7 +443,7 @@ first_dentry (fsi_file_t *ffi, xfs_ino_t *ino) xfs.dablk = (1ULL <<35 ) >> xfs.blklog;#define h ((xfs_dir2_leaf_hdr_t *)dirbuf)#define n ((xfs_da_intnode_t *)dirbuf) -for (;;) { +for (da_levels =0 ; ; da_levels++) {xfs_dabread (ffi);if ((n->hdr.info.magic ==le16 (XFS_DIR2_LEAFN_MAGIC )) || (n->hdr.info.magic ==le16 (XFS_DIR2_LEAF1_MAGIC ))) {
@@ -421,6 +451,16 @@ first_dentry (fsi_file_t *ffi, xfs_ino_t *ino) xfs.forw =le32 (h->info.forw);break ; } +if (da_levels >=XFS_DA_MAXLEVELS ) { +/* malformed directory da-btree: a node +* that never descends to a leaf (e.g. a +* btree[0].before that points to itself) +* would otherwise loop forever. Treat +* the directory as empty. */ + xfs.dirmax =0 ; + xfs.forw =0 ; +break ; + } xfs.dablk =le32 (n->btree[0 ].before); }#undef n
@@ -602,6 +642,17 @@ xfs_dir (fsi_file_t *ffi, char *dirname) *rest =0 ; name =first_dentry (ffi, &new_ino); +if (name ==NULL ) { +/* A malformed directory (e.g. one whose da-btree +* descent was bounded above) yields no entries; +* fail the lookup instead of dereferencing NULL. */ +if (print_possibilities <0 ) +return 1 ; + + errnum =ERR_FILE_NOT_FOUND ; + *rest = ch; +return 0 ; + }for (;;) { cmp = (!*dirname) ?-1 :substring (dirname, name);#ifndef STAGE1_5
Signature · 1 line
2.52.0.windows.1