From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pg1-f179.google.com (mail-pg1-f179.google.com [209.85.215.179]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1E60C27CCE0 for ; Sun, 5 Jul 2026 02:53:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.179 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783220006; cv=none; b=JwIM1eQQz7mbHuqLStACaKajPOMTihGSHObgEyFH0Ss+F3L9fUW2Fbx8YqCbX5Ql3+Kv6IHl/nQ8mfCEqmIKXh86JB+zYsVvLYTNq5BnzAN09x+Dz9N7FTQts0t7FU98zV5vU4CtuJTEc5qapRkP/U7CvPuZGbxBGh52ltq6BNY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783220006; c=relaxed/simple; bh=XEK36qBq2IpL2H+HrmC//ajSUB207Haaxm3E0Pzih7c=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Wx1vQqL1Q+4hAXNzQZIrO7E5HvOmfpXrdw9bKrvkuhU0R20W5S6Ni8csQTUYla/mhksKDPSJdRXlRig+eBi/edsFXrd3cgK+pHKWKLmWbSwz/23VtX7Mf+xrXNYW+tD7bA6aHFV9spmCFmXyhY7tESLyt0KUIKrHp3Qxotwg4/k= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=YpdCVCiR; arc=none smtp.client-ip=209.85.215.179 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="YpdCVCiR" Received: by mail-pg1-f179.google.com with SMTP id 41be03b00d2f7-c88a4d79ba5so1217322a12.2 for ; Sat, 04 Jul 2026 19:53:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1783220003; x=1783824803; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=ZwAX1ZN395zmoLg6cQyDC1uji9N88Kvypjg/r2X0TS0=; b=YpdCVCiRiOjipvjd/l2PMcrEaVgnTHbFMf6Y+lLF6J4DfgNSENMoAfaMPYvGNS795P 7COmr+lbhexMqlG6Cakc/hXZ6VrLAQr5RJrIpFSMM+7VbpgOWR+0yP22sSe7I5/BStgw VS8OeUf49z9qMdNnL2dxX7W0t4P4IeHN6FbcZD/nlyb8XiiYjwayYq4jSYtkfkyt9aqo MwUH+CEyaf+uc58d/86SAQrklcEjm8ZUaSsAmWW7XdVlUNQZwckYPne6YGCAWQC13FqC 7gHagQKiTcf0IKfxISIOUtKqS3iY5bgE1WTOLl9ZFyMrQnlG9+6L6ay6zJg5QCsT1xf1 mXlw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1783220003; x=1783824803; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=ZwAX1ZN395zmoLg6cQyDC1uji9N88Kvypjg/r2X0TS0=; b=hs7N3PwlWOuMNtfDvXjZu31a/p5JI2iSbcGpJJWdhktq214wpy5SIxQF/aJEqaQ7zK P4PivfCFpkHUfec5mcWVCc2UeI+DmCBj2TDSqQHGW8qIdtTbZ7+I2h0T8YEsbdtbdM8u CYQU6x+SX4IfdQ1zWw2tPTBaaJFtJ5Wk7r732yQW4722N3/Hh8RhR82/9rk7ChtEXRJY PuDerL9IP3awzYgVb9DyMsa+McqpMLfWJZImCNFvTsKqPVGuEIXlbHvS+1P7bjI7MZvP WGJKqvvqFo/cfjjD/wyhKrJLJLKul/APRuiHJZJtUqP5jsWEEnSjv4Lq4c00DfjjjPRe 17gQ== X-Forwarded-Encrypted: i=1; AFNElJ+g8xSC2Z3O+IdDFfPZ0yh002tTdbB1bClxhg45YNmglpx47f3kKjeVXlDpih81F9t0Lu3ekOekNf3p86o=@vger.kernel.org X-Gm-Message-State: AOJu0YxYHfQcrGhgZOxftZ5h3ESvLF0I6JM/JiRmfp3fZilN5e0EghLm L2D/Wvl2ZVjGFbpz7qKrDGVTm8cnUi4lypwJF48KeAEXRxLbgFrqZ1tA X-Gm-Gg: AfdE7cm3DIHUMqGo+zz614x87Wz/h1AylVvflFSUFsN4DnyCPHLKLsRqOh0rw1ts/94 pjYE8/JGt+92Dd2WYzcHBPg8IU/l4Qc47XGTdd9VMAREJpPG/zIkxH/Q2hnlLKhIAEwhYxzafCY nSWv4bKdf50o6OwySoxbDq64+fqc0KGiny1+2TND/Dux5lKDi+gm6C46MKu96FvvtdKwcV32shs mfPBM38c9GPjSxudkcHYvHYsXY/QDWHHIZt8kwLXOmXkJUIzNa7PAMqu/Ei4a7DbMEGrMRd52Xc np1h8u9GuSqzp9/MbE4QjgRslu69W/mHzaCCLR3noQo+58Oput6uEY5TJHPuNNy2qrvkTWEQC7L ObmYQ1UpSutYKJKu91qb3TzyGLGKB4x0JmBBlOTJ0WwfBPGRG51JsTK5yb6qDY5+6xWRm8nrkW1 HFz/f4ZSg= X-Received: by 2002:a05:6300:6182:b0:3bf:6f30:1ccd with SMTP id adf61e73a8af0-3c03e504735mr5321584637.42.1783220003333; Sat, 04 Jul 2026 19:53:23 -0700 (PDT) Received: from localhost ([111.228.63.84]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-c9e8bd30ca5sm4253679a12.0.2026.07.04.19.53.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 04 Jul 2026 19:53:22 -0700 (PDT) From: Cen Zhang To: Joseph Qi , Mark Fasheh , Joel Becker Cc: ocfs2-devel@lists.linux.dev, linux-kernel@vger.kernel.org, zzzccc427@gmail.com Subject: [PATCH v7 2/2] ocfs2: validate external xattr entries when reading metadata Date: Sun, 5 Jul 2026 10:53:11 +0800 Message-Id: <20260705025311.3429854-3-zzzccc427@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260705025311.3429854-1-zzzccc427@gmail.com> References: <20260705025311.3429854-1-zzzccc427@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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. Use the flat xattr entry validator for non-indexed external xattr blocks, and use a bucket-specific validator for indexed buckets at metadata read time. The bucket validator keeps the entry array bounded by the first bucket block while checking name/value offsets 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 Fixes: cf1d6c763fbc ("ocfs2: Add extended attribute support") Fixes: 0c044f0b24b9 ("ocfs2: Add xattr bucket iteration for large numbers of EAs") Assisted-by: Codex:gpt-5.5 Signed-off-by: Cen Zhang --- fs/ocfs2/xattr.c | 97 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) diff --git a/fs/ocfs2/xattr.c b/fs/ocfs2/xattr.c index ca76441625db..d23364c30e1a 100644 --- a/fs/ocfs2/xattr.c +++ b/fs/ocfs2/xattr.c @@ -390,6 +390,12 @@ static int ocfs2_init_xattr_bucket(struct ocfs2_xattr_bucket *bucket, return rc; } +static int ocfs2_validate_xattr_entries_flat(struct super_block *sb, u64 blkno, + struct ocfs2_xattr_header *xh, + size_t region_size); +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 +414,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) @@ -509,6 +517,22 @@ 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 region_offset = + offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header); + + if (bh->b_size < region_offset) + return ocfs2_error(sb, + "Invalid xattr block %llu: block size %zu is too small\n", + (unsigned long long)bh->b_blocknr, + bh->b_size); + + return ocfs2_validate_xattr_entries_flat(sb, bh->b_blocknr, + &xb->xb_attrs.xb_header, + bh->b_size - + region_offset); + } + return 0; } @@ -1068,6 +1092,79 @@ int ocfs2_validate_inode_xattr(struct super_block *sb, u64 blkno, return ocfs2_validate_xattr_entries_flat(sb, blkno, xh, inline_size); } +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); + u16 xattr_count = le16_to_cpu(xh->xh_count); + size_t region_size = (size_t)sb->s_blocksize * bucket->bu_blocks; + size_t entries_limit = sb->s_blocksize; + size_t nv_limit = sb->s_blocksize; + size_t max_entries; + int i; + + if (region_size < sizeof(*xh)) + return ocfs2_error(sb, + "Invalid xattr bucket %llu: region size %zu is too small\n", + (unsigned long long)blkno, region_size); + + if (entries_limit < sizeof(*xh)) + return ocfs2_error(sb, + "Invalid xattr bucket %llu: entries limit %zu is too small\n", + (unsigned long long)blkno, + entries_limit); + + max_entries = (entries_limit - 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 % nv_limit; + size_t value_offset; + + if (name_offset >= region_size || block_off >= bucket->bu_blocks) + return ocfs2_error(sb, + "Invalid xattr bucket %llu: entry %d name is out of bounds\n", + (unsigned long long)blkno, i); + + if (xe->xe_name_len > nv_limit - 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 > nv_limit) + 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) > + nv_limit - 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) > + nv_limit - 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_xattr_ibody_lookup_header(struct inode *inode, struct ocfs2_dinode *di, struct ocfs2_xattr_header **header) -- 2.43.0