ocfs2: validate xattr metadata bounds
v3 cover ocfs2: validate xattr metadata bounds
From: Cen Zhang <[email protected]>
Hi,
This v3 moves the corrupted xattr metadata checks into the metadata validation paths Joseph pointed at.
Patch 1 validates inline xattr metadata from ocfs2_validate_inode_block() and keeps the get/list consumers using the same helper before they derive pointers from i_xattr_inline_size.
Patch 2 validates non-indexed external xattr blocks from ocfs2_validate_xattr_block(). Indexed xattr buckets do not pass through that validator, so their header and per-entry bounds are checked after bucket ECC verification in ocfs2_read_xattr_bucket().
Both issues were reproduced with crafted OCFS2 images under KASAN.
Changes since v2:
- Move inline xattr validation into ocfs2_validate_inode_block().
- Move non-indexed external xattr validation into ocfs2_validate_xattr_block().
- Validate indexed xattr bucket entries at bucket read time, after ECC verification.
- Keep get/list path checks as helper reuse instead of a separate consumer-only policy.
Cen Zhang (2):
ocfs2: validate inline xattrs during inode block validation
ocfs2: validate external xattr entries when reading metadata
fs/ocfs2/inode.c | 4 ++ fs/ocfs2/xattr.c | 211 +++++++++++++++++++++++++++++++++++++++-------- fs/ocfs2/xattr.h | 2 + 3 files changed, 183 insertions(+), 34 deletions(-)
Signature · 1 line
2.43.0
PATCH v3 1/2 ocfs2: validate inline xattrs during inode block validation
From: Cen Zhang <[email protected]>
ocfs2_validate_inode_block() verifies a dinode before OCFS2 users walk metadata from it, but inline xattr metadata is still checked only in some consumers. In particular, ocfs2_xattr_ibody_get() derives the inline xattr header from i_xattr_inline_size before calling ocfs2_xattr_find_entry().
Move the inline xattr size and entry bounds checks behind a shared helper and call it from ocfs2_validate_inode_block(). Keep the get/list paths using the same helper before they derive pointers from i_xattr_inline_size so callers with an already-loaded dinode still get the same corruption check.
Reject corrupted inline xattr metadata before ocfs2_xattr_ibody_get() or listxattr() can walk past the inline storage.
Validation reproduced this kernel report:
BUG: KASAN: use-after-free in ocfs2_xattr_find_entry+0x5a/0x170 Read of size 2 at addr ffff8881242a2000 by task python3/529 Call Trace: dump_stack_lvl+0x66/0xa0 print_report+0xce/0x630 kasan_report+0xe0/0x110 ocfs2_xattr_find_entry+0x5a/0x170 ocfs2_xattr_get_nolock+0x20a/0x820 ocfs2_xattr_get+0x10c/0x1e0 __vfs_getxattr+0xe2/0x130 vfs_getxattr+0x185/0x1b0
--- fs/ocfs2/inode.c | 4 ++ fs/ocfs2/xattr.c | 131 ++++++++++++++++++++++++++++++++++++++++-------------- fs/ocfs2/xattr.h | 2 + 3 files changed, 103 insertions(+), 34 deletions(-)
fs/ocfs2/inode.c +4 -0
@@ -1494,6 +1494,10 @@ int ocfs2_validate_inode_block(struct super_block *sb, goto bail; } + rc =ocfs2_validate_inode_xattr (sb, di); +if (rc) + goto bail; +if (le16_to_cpu (di->i_dyn_features) &OCFS2_INLINE_DATA_FL ) {struct ocfs2_inline_data *data = &di->id2.i_data;
fs/ocfs2/xattr.c +97 -34
161 lines not on this page · raw message
fs/ocfs2/xattr.h +2 -0
@@ -35,6 +35,8 @@ extern const struct xattr_handler * const ocfs2_xattr_handlers[];ssize_t ocfs2_listxattr (struct dentry *,char *,size_t );int ocfs2_xattr_get_nolock (struct inode *,struct buffer_head *,int ,const char *,void *,size_t ); +int ocfs2_validate_inode_xattr (struct super_block *sb, +struct ocfs2_dinode *di);int ocfs2_xattr_set (struct inode *,int ,const char *,const void *,size_t ,int ); int ocfs2_xattr_set_handle(handle_t *, struct inode *, struct buffer_head *,
Signature · 1 line
2.43.0
PATCH v3 2/2 ocfs2: validate external xattr entries when reading metadata
In reply to Zhang CenFrom: Cen Zhang <[email protected]>
ocfs2_validate_xattr_block() checks the xattr block header before the block reaches higher-level xattr users, but it does not verify that a non-indexed block's xh_count and entry offsets fit inside the block. Indexed buckets likewise reach list/get consumers after ECC without an entry-bounds check.
Move the non-indexed external xattr entry checks into ocfs2_validate_xattr_block(). Validate indexed bucket entries immediately after bucket ECC verification in ocfs2_read_xattr_bucket(), because buckets do not pass through ocfs2_validate_xattr_block(). The bucket entry table is still bounded by the first bucket block; the name/value offsets are then checked against the bucket block they target.
Reject corrupted external xattr metadata before listxattr() or getxattr() can walk out-of-range entry arrays or name/value offsets.
Validation reproduced this kernel report:
BUG: KASAN: use-after-free in ocfs2_xattr_list_entries+0xd7/0x190 Read of size 1 at addr ffff88810a654007 by task ocfs2_xattr_lis/630 Call Trace: dump_stack_lvl+0x66/0xa0 print_report+0xce/0x630 kasan_report+0xe0/0x110 ocfs2_xattr_list_entries+0xd7/0x190 ocfs2_listxattr+0x3f6/0x610 listxattr+0x90/0xe0 path_listxattrat+0xed/0x220 do_syscall_64+0x115/0x6a0 entry_SYSCALL_64_after_hwframe+0x77/0x7f
--- fs/ocfs2/xattr.c | 80 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+)
fs/ocfs2/xattr.c +80 -0
@@ -390,6 +390,9 @@ static int ocfs2_init_xattr_bucket(struct ocfs2_xattr_bucket *bucket,return rc; } +static int ocfs2_validate_xattr_bucket (struct ocfs2_xattr_bucket *bucket, +u64 blkno); +/* Read the xattr bucket at xb_blkno */ static int ocfs2_read_xattr_bucket (struct ocfs2_xattr_bucket *bucket,u64 xb_blkno)
@@ -408,6 +411,8 @@ static int ocfs2_read_xattr_bucket(struct ocfs2_xattr_bucket *bucket,spin_unlock (&OCFS2_SB (bucket->bu_inode->i_sb)->osb_xattr_lock);if (rc)mlog_errno (rc); +else + rc =ocfs2_validate_xattr_bucket (bucket, xb_blkno); }if (rc)
@@ -551,6 +556,71 @@ int ocfs2_validate_inode_xattr(struct super_block *sb, struct ocfs2_dinode *di) inline_size, "inline xattr"); } +static int ocfs2_validate_xattr_bucket (struct ocfs2_xattr_bucket *bucket, +u64 blkno) +{ +struct super_block *sb = bucket->bu_inode->i_sb; +struct ocfs2_xattr_header *xh =bucket_xh (bucket); +size_t blocksize = sb->s_blocksize; +size_t bucket_size = blocksize * bucket->bu_blocks; +u16 xattr_count =le16_to_cpu (xh->xh_count); +size_t max_entries; +int i; + +if (bucket_size <sizeof (*xh)) +return ocfs2_error (sb, +"Invalid xattr bucket %llu: storage size %zu is too small\n" , + (unsigned long long )blkno, bucket_size); + + max_entries = (blocksize -sizeof (*xh)) / +sizeof (struct ocfs2_xattr_entry ); +if (xattr_count > max_entries) +return ocfs2_error (sb, +"Invalid xattr bucket %llu: entry count %u exceeds maximum %zu\n" , + (unsigned long long )blkno, xattr_count, + max_entries); + +for (i =0 ; i < xattr_count; i++) { +struct ocfs2_xattr_entry *xe = &xh->xh_entries[i]; +size_t name_offset =le16_to_cpu (xe->xe_name_offset); +size_t block_off = name_offset >> sb->s_blocksize_bits; +size_t block_offset = name_offset % blocksize; +size_t value_offset; + +if (name_offset >= bucket_size || block_off >= bucket->bu_blocks) +return ocfs2_error (sb, +"Invalid xattr bucket %llu: entry %d name block is out of bounds\n" , + (unsigned long long )blkno, i); + +if (xe->xe_name_len > blocksize - block_offset) +return ocfs2_error (sb, +"Invalid xattr bucket %llu: entry %d name crosses block boundary\n" , + (unsigned long long )blkno, i); + + value_offset = block_offset +OCFS2_XATTR_SIZE (xe->xe_name_len); +if (value_offset > blocksize) +return ocfs2_error (sb, +"Invalid xattr bucket %llu: entry %d value starts out of bounds\n" , + (unsigned long long )blkno, i); + +if (ocfs2_xattr_is_local (xe)) { +if (le64_to_cpu (xe->xe_value_size) > + blocksize - value_offset) +return ocfs2_error (sb, +"Invalid xattr bucket %llu: entry %d value is out of bounds\n" , + (unsigned long long )blkno, + i); + }else if (sizeof (struct ocfs2_xattr_value_root ) > + blocksize - value_offset) { +return ocfs2_error (sb, +"Invalid xattr bucket %llu: entry %d value root is out of bounds\n" , + (unsigned long long )blkno, i); + } + } + +return 0 ; +} +static int ocfs2_validate_xattr_block (struct super_block *sb,struct buffer_head *bh) {
@@ -596,6 +666,16 @@ static int ocfs2_validate_xattr_block(struct super_block *sb, le32_to_cpu(xb->xb_fs_generation)); } +if (!(le16_to_cpu (xb->xb_flags) &OCFS2_XATTR_INDEXED )) { +size_t storage_size = sb->s_blocksize - + offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header); + +return ocfs2_validate_xattr_entries (sb, bh->b_blocknr, + &xb->xb_attrs.xb_header, + storage_size, +"xattr block" ); + } +return 0 ; }
Signature · 1 line
2.43.0
Joseph Qi
maintainerHi,
I couldn't apply your patches cleanly upon the lastest master. Could you please do a update?
Thanks,
Joseph
Quoted 36 lines · Zhang Cen
From: Cen Zhang <[email protected]>
Hi,
This v3 moves the corrupted xattr metadata checks into the metadata validation paths Joseph pointed at.
Patch 1 validates inline xattr metadata from
ocfs2_validate_inode_block()and keeps the get/list consumers using the same helper before they derive pointers fromi_xattr_inline_size.Patch 2 validates non-indexed external xattr blocks from
ocfs2_validate_xattr_block(). Indexed xattr buckets do not pass through that validator, so their header and per-entry bounds are checked after bucket ECC verification inocfs2_read_xattr_bucket().Both issues were reproduced with crafted OCFS2 images under KASAN.
Changes since v2:
- Move inline xattr validation intoocfs2_validate_inode_block().
- Move non-indexed external xattr validation intoocfs2_validate_xattr_block().
- Validate indexed xattr bucket entries at bucket read time, after ECC verification.
- Keep get/list path checks as helper reuse instead of a separate consumer-only policy.Cen Zhang (2):
ocfs2: validate inline xattrs during inode block validation
ocfs2: validate external xattr entries when reading metadatafs/ocfs2/inode.c | 4 ++ fs/ocfs2/xattr.c | 211 +++++++++++++++++++++++++++++++++++++++-------- fs/ocfs2/xattr.h | 2 + 3 files changed, 183 insertions(+), 34 deletions(-)
Hi Joseph,
Quoted 3 lines
Hi,
I couldn't apply your patches cleanly upon the lastest master. Could you please do a update?
Thanks for the notice. I’ll rebase the patches on the latest master and resend them.
Best regards,
Cen Zhang