From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pf1-f174.google.com (mail-pf1-f174.google.com [209.85.210.174]) (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 2A7D826F2B9 for ; Sun, 5 Jul 2026 02:53:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.174 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783220002; cv=none; b=r1HLYqgmHelGB7mSPYTksBofOUvkmzC3NiisL/YG8p4HvoBUYuB3izvKPKftnYiJ7ACyT3vmudFdb3IuacRzJBlT/2AXzB1n5ImnsrQFfvDX5kOTvZqzAwqevNtUQdxYb7TithWeEldtwe9rft3KOaS62cDPlsogZY4CP+ycY5s= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783220002; c=relaxed/simple; bh=8uw1hcjuhZI+frJMRrQ/82wwOdUpAPEqjUdRrd2ZArc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=lNhu1BNT+k7dhBWT45hfNe1n3lP4QHXzAIrri6kDrqKN/3zWNK8e12iBa04Hk7pZInBfRNXuEb2IzGvT1wMKAadGKYygobtHhoLCqHCfDv5tYEvk48NMAnO85ResdvmrRVupHaYl+lwywfYymtrTIRA2StuZJ4RRC1E4zgPLrlA= 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=ZG4KDl/w; arc=none smtp.client-ip=209.85.210.174 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="ZG4KDl/w" Received: by mail-pf1-f174.google.com with SMTP id d2e1a72fcca58-845537740ddso1137514b3a.0 for ; Sat, 04 Jul 2026 19:53:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1783220000; x=1783824800; 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:content-type; bh=9BFKQS5Hoo1UKGdannPcfOtCqVmG55CXJXvYTzK0mTI=; b=ZG4KDl/ws9MEC/6ZxQnlr9AuyidRU0K8jfjCXY8Btv2bkW7y6v06m9I8Q8ui8QSocI 8mqd8gp9oQ+FplMmn171aWlrzV3nygHjjNIWLMvfgE86bJWtRNF7HhPDX7jFsomj99s/ 7UMaHl29rDdULZjV/bR38jhbBkWqsnaLMTCeO0J5BR/stMqidYAzkuISDzzHEtDwbm8i am9XgC9ODWoNgabQo4hivhweFTPF06Z5ReYbQBYb2nc7j8edNJP0lbSxYnEAmQ7QvL7T YNFB+S8W/5Zmb19/Owztpz7YBM05hS0OhIxs8lb9E7p1TS0Z3VbmjHLLvesqENKtsVJw Q/NA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1783220000; x=1783824800; 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:content-type; bh=9BFKQS5Hoo1UKGdannPcfOtCqVmG55CXJXvYTzK0mTI=; b=L86qQY+PRZfEaXm6+CxiTIkUOGNq/+8CzEUxW9ihhrkw6/LVBcaUsBrbZ+I27NoOGU 5nug5s16ZHgrJ1FVCfooiY8XTYPryCoZMlB/Pr969sWROyjVwCSPYYldUpaH8f+vhQox W6Z6pNwzqyBMjohrFguW/vdBJiJqoZtjHK/RDvne3ANeSM5fcU9OLIJQH/SUca2VykXx ZXFB0Bb/Ula0VRHwIQFarRXp87C1zjCk/4Dxjltif1G00ulWnXVGtckm2hwlK+YI1uuc OBsaOcik81vaMkx38E4h9W4wNxa+UV4kBMrPOziG+XnVg3MiIh0JmYrGokF7ZCSCIEzc uaig== X-Forwarded-Encrypted: i=1; AFNElJ+pyiVXq7CFluJT+4an6f7mYPD3UGl7UoK2WUGqtw0swUXHZbnAKSMegTf93+VQzI9Mmo/McWhOw6vnmz4=@vger.kernel.org X-Gm-Message-State: AOJu0Yz7o+3FPMzJh6R/5FocNfJu9T7BxnF4QCl6j1Y7ZMnOy2A1CkxO Pc72m+inI6kZMJ0Xf16I3DJIs4J9jwIGufVMrut50f/qjCqRetduC5M3 X-Gm-Gg: AfdE7clP0Cs33uotGlcxyfQ8X9AWlxv7lsksrSNyAadRmSRk+2Uge19UFolLYYU+xvt r2atDe9TJyvr56ciAd8jCXzE39gV48ePM+zKMu6uwo5WYyNlV2q/qIw5/Mm5y1bOhx3vJ/S8/eJ kdlKBbCW1xnhMCKJhRgr6SrZ4EoFHlu5jChbfwS9diMdjnWxtHqJmWAmL5mASS/jrlhA/atCn7o d3/q472QHIGg0OzdlFmPNXf+sQAEDnSnGU3gY7qatGufeQiA+Lmt3jYrAoRH+yGSvo6iCCdEUFR etaVkI79oPfJYJkul9mBiu3B/vr/jNoShcd0o1b0KfrX4g/gadnaPqd5rloekXSwn2hLTfy8tma Z287VNzeY4Q4WPen9V7YPPwGZ9ZyNPzGkl/XyII8KD+833n+99INH2VLN/c8R7+wXnYFFI7MjRw UOsudt2Gk= X-Received: by 2002:a05:6a20:9f96:b0:3bf:bde7:d679 with SMTP id adf61e73a8af0-3c03e572be8mr5375734637.40.1783220000347; Sat, 04 Jul 2026 19:53:20 -0700 (PDT) Received: from localhost ([111.228.63.84]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-c9e8bd30ca5sm4253679a12.0.2026.07.04.19.53.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 04 Jul 2026 19:53:19 -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 1/2] ocfs2: validate inline xattrs during inode block validation Date: Sun, 5 Jul 2026 10:53:10 +0800 Message-Id: <20260705025311.3429854-2-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_inode_block() verifies a dinode before OCFS2 users walk metadata from it, but inline xattr metadata is still checked only in operation-specific consumers. The existing ibody lookup helper validates inline header placement and entry count, but inode block validation does not reject entry name/value bounds. Add a flat xattr entry validator and call it from inode block validation for inline xattrs. Keep the operation paths on their existing header/count lookup checks; the full entry bounds check now runs when the inode block is validated at read time. 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 Fixes: cf1d6c763fbc ("ocfs2: Add extended attribute support") Assisted-by: Codex:gpt-5.5 Signed-off-by: Cen Zhang --- fs/ocfs2/inode.c | 4 ++ fs/ocfs2/xattr.c | 118 +++++++++++++++++++++++++++++++++++++++++------ fs/ocfs2/xattr.h | 2 + 3 files changed, 111 insertions(+), 13 deletions(-) diff --git a/fs/ocfs2/inode.c b/fs/ocfs2/inode.c index 662dbc845b8b..815bf3f659da 100644 --- a/fs/ocfs2/inode.c +++ b/fs/ocfs2/inode.c @@ -1608,6 +1608,10 @@ int ocfs2_validate_inode_block(struct super_block *sb, goto bail; } + rc = ocfs2_validate_inode_xattr(sb, bh->b_blocknr, 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; diff --git a/fs/ocfs2/xattr.c b/fs/ocfs2/xattr.c index fcddd3c13acd..ca76441625db 100644 --- a/fs/ocfs2/xattr.c +++ b/fs/ocfs2/xattr.c @@ -950,41 +950,133 @@ static int ocfs2_xattr_list_entries(struct inode *inode, return result; } -static int ocfs2_xattr_ibody_lookup_header(struct inode *inode, - struct ocfs2_dinode *di, - struct ocfs2_xattr_header **header) +static int ocfs2_validate_xattr_entries_flat(struct super_block *sb, u64 blkno, + struct ocfs2_xattr_header *xh, + size_t region_size) +{ + u16 xattr_count = le16_to_cpu(xh->xh_count); + size_t entries_limit = region_size; + size_t nv_limit = region_size; + size_t max_entries; + int i; + + if (region_size < sizeof(*xh)) + return ocfs2_error(sb, + "Invalid xattr in block %llu: region size %zu is too small\n", + (unsigned long long)blkno, region_size); + + max_entries = (entries_limit - sizeof(*xh)) / + sizeof(struct ocfs2_xattr_entry); + + if (xattr_count > max_entries) + return ocfs2_error(sb, + "Invalid xattr in block %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 value_offset; + + if (name_offset > nv_limit || + xe->xe_name_len > nv_limit - name_offset) + return ocfs2_error(sb, + "Invalid xattr in block %llu: entry %d name is out of bounds\n", + (unsigned long long)blkno, i); + + value_offset = name_offset + OCFS2_XATTR_SIZE(xe->xe_name_len); + if (value_offset > nv_limit) + return ocfs2_error(sb, + "Invalid xattr in block %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 in block %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 in block %llu: entry %d value root is out of bounds\n", + (unsigned long long)blkno, i); + } + } + + return 0; +} + +static int ocfs2_xattr_ibody_lookup_header_raw(struct super_block *sb, + u64 blkno, + struct ocfs2_dinode *di, + struct ocfs2_xattr_header **header, + u16 *inline_size_ret) { + struct ocfs2_xattr_header *xh; u16 xattr_count; size_t max_entries; u16 inline_size = le16_to_cpu(di->i_xattr_inline_size); - if (inline_size > inode->i_sb->s_blocksize || + if (inline_size > sb->s_blocksize || inline_size < sizeof(struct ocfs2_xattr_header)) { - ocfs2_error(inode->i_sb, - "Invalid xattr inline size %u in inode %llu\n", - inline_size, - (unsigned long long)OCFS2_I(inode)->ip_blkno); + ocfs2_error(sb, + "Invalid inode %llu: xattr inline size %u\n", + (unsigned long long)blkno, inline_size); return -EFSCORRUPTED; } - *header = (struct ocfs2_xattr_header *) - ((void *)di + inode->i_sb->s_blocksize - inline_size); + xh = (struct ocfs2_xattr_header *) + ((void *)di + sb->s_blocksize - inline_size); - xattr_count = le16_to_cpu((*header)->xh_count); + xattr_count = le16_to_cpu(xh->xh_count); max_entries = (inline_size - sizeof(struct ocfs2_xattr_header)) / sizeof(struct ocfs2_xattr_entry); if (xattr_count > max_entries) { - ocfs2_error(inode->i_sb, + ocfs2_error(sb, "xattr entry count %u exceeds maximum %zu in inode %llu\n", xattr_count, max_entries, - (unsigned long long)OCFS2_I(inode)->ip_blkno); + (unsigned long long)blkno); return -EFSCORRUPTED; } + *header = xh; + if (inline_size_ret) + *inline_size_ret = inline_size; + return 0; } +int ocfs2_validate_inode_xattr(struct super_block *sb, u64 blkno, + struct ocfs2_dinode *di) +{ + struct ocfs2_xattr_header *xh; + u16 inline_size; + int ret; + + if (!(le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_XATTR_FL)) + return 0; + + ret = ocfs2_xattr_ibody_lookup_header_raw(sb, blkno, di, &xh, + &inline_size); + if (ret) + return ret; + + return ocfs2_validate_xattr_entries_flat(sb, blkno, xh, inline_size); +} + +static int ocfs2_xattr_ibody_lookup_header(struct inode *inode, + struct ocfs2_dinode *di, + struct ocfs2_xattr_header **header) +{ + return ocfs2_xattr_ibody_lookup_header_raw(inode->i_sb, + OCFS2_I(inode)->ip_blkno, + di, header, NULL); +} + int ocfs2_has_inline_xattr_value_outside(struct inode *inode, struct ocfs2_dinode *di) { diff --git a/fs/ocfs2/xattr.h b/fs/ocfs2/xattr.h index 65e9aa743919..6b7589941315 100644 --- a/fs/ocfs2/xattr.h +++ b/fs/ocfs2/xattr.h @@ -43,6 +43,8 @@ int ocfs2_xattr_set_handle(handle_t *, struct inode *, struct buffer_head *, struct ocfs2_alloc_context *); int ocfs2_has_inline_xattr_value_outside(struct inode *inode, struct ocfs2_dinode *di); +int ocfs2_validate_inode_xattr(struct super_block *sb, u64 blkno, + struct ocfs2_dinode *di); int ocfs2_xattr_remove(struct inode *, struct buffer_head *); int ocfs2_init_security_get(struct inode *, struct inode *, const struct qstr *, -- 2.43.0