From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f175.google.com (mail-pl1-f175.google.com [209.85.214.175]) (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 0F4A33B0AE8 for ; Mon, 22 Jun 2026 13:03:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.175 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782133428; cv=none; b=cxmY1K6NUiwWgPTXpY148+lcA13/V+jYj7/i/5uI/1dyn670g0UHxdexOrG1/fnIjIVjgh2vWB67VezexEdPo37wJTp655jAjHi/iEGrprmPzMExywzb1FszgF7A/9v9SyyXz4USaY/Y17QJ75hRIFR4rRB6Aah5acv2kqusWy8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782133428; c=relaxed/simple; bh=M0XZvJqWNfwGrcd0WsBNoEzSRV1QGvz3NJH5XZ3Gir8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=G0dfQhWAO1eDQmmDW01mSmyzJT9xSPmtxHeCIlix/2xg5iNvZyGkebYPiO6PKIxhTFEKFsP82kHX3MRMBs0eYdpl8+UcQ5FKt0N2ulDsXf+n4Q2D1NJAR5lOxoOvrfR3CXkglrSPRae6SCFvLa9ofpKU5+/OMNQSqOV87TcxbuI= 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=PQiAnjHf; arc=none smtp.client-ip=209.85.214.175 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="PQiAnjHf" Received: by mail-pl1-f175.google.com with SMTP id d9443c01a7336-2c0c3546924so32870635ad.3 for ; Mon, 22 Jun 2026 06:03:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1782133424; x=1782738224; 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=JSk0/WQjsb78f8Lzzg7Zyxxk0xSQ7idbJ8adIcT9Oyk=; b=PQiAnjHffSSKck0SmcM7nWQ1PoTYcZIe9IkMTddoRQA+h/buyYfzpHzPFeVWVRNYe8 Tg1IY4bCWKMQyaoYwbNxpWSHRxa1FP05fqWupuGtwHeE1xjVr2eXtIC8SS5v6e1Y9ve4 /MGQDKCu+7eIB4ojThoLFsu1AcbgLZH+BZJqLO7mIIbz50KmXXqog6N6N4luSL0aCIHd xrWMIlZMtly+LoebDPIINH7WVRvsDEDvLatLtZLdwSa/hYCBKp3yiABYH44XsshYOOSU RDsJCVSaWn6w5Q2gitLULcOTuRms5c1TrW9sFWXwZSXvY3PGlCxW9/t3U50M5znQvRHd R6lA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1782133424; x=1782738224; 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=JSk0/WQjsb78f8Lzzg7Zyxxk0xSQ7idbJ8adIcT9Oyk=; b=ioJNlHp4GHoMpcVaUmG/k62D203nobeBbTymNDV+jqmSc4pEf18S3aEZ3IK+vNBrjI uZy/zGTDt8FiIpKhTtcACJ+hLjI/p2qNJe/M/ck6LUsRsIxKTottVL9LJvXBjmnZY6m2 /4slB74r6aC/Cw8PBfeG7P9kT1qYucv+h957Cou0YhJ1QuXkRs0WsBtZXildQCzFibUV Deo6dRVPmtXNXTad3tYBPmfnh0TKvQlA7DVlgp9tWgALULqKJeh9yliVl4yVdleVBuQw ZvN+/4/4ez02yqn447bjErfIYSRkPkKiz43XqMgK7nM35OE+LtdbiWe/iIRBAZQ4E4Ok 3sZg== X-Forwarded-Encrypted: i=1; AHgh+Roux3ICwxfyv71SP+o15hu2YO2OZFC4FNQXLH0+m5vpMmN6aEc/xGN6Swi8F+giCqzAzRtcKNf38ed49Xc=@vger.kernel.org X-Gm-Message-State: AOJu0YwewAj68K7DWB7lC7gN1/hlxfLY/t/jkd+9QSAQQVrXpa5seAS8 0/x1ItspssHyRoDg8tLfpBIWUghwP+9NxM9vl58lD5BYoiEDfim9pqKq X-Gm-Gg: AfdE7cnn8fdMVW8cdSWjgqc1ZYXVyMulbgjFfM+Ca36BJCL23MTUAH/rG970itZgXfr /lVCY6KJDtRTx8EMOjiEQj4vG62wLl2YsR3Ze8dbVCRclzshWoHLxUNcOTTSGsuql/vKrJyz84Q NqvtqA1z15osftjUOJm3qKmykcytTXAH6a5nE6tXark18FNd8QRecNRq5Omp54EqiDwYg3HgggM cD6X0GzPFhfkBVZAq5Dit7csKs0m00IaayUvaHsyDx/C73SvcMZavGHxkY506KbyhT4QXO/Yh0l 6aIcxme3IFJdHlgR4zPAwVx+Yyc2EjgZUtx8yzU6OVphbEqQUhpFXUqsLIcpF56Qn70Z01lgmvD nggZJGoWEfflgBJG6Qn3luGDYx25G3m7YtySjgSbEKfNNPoOY/p4dFm9ZzYzKeZKmKLs5+9lr0C Fs056CSn0= X-Received: by 2002:a17:902:db04:b0:2c1:6020:7398 with SMTP id d9443c01a7336-2c718f5fa6bmr153097095ad.12.1782133423813; Mon, 22 Jun 2026 06:03:43 -0700 (PDT) Received: from localhost ([111.228.63.84]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2c743fd6420sm78895885ad.70.2026.06.22.06.03.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Jun 2026 06:03:43 -0700 (PDT) From: Cen Zhang To: Joseph Qi , Mark Fasheh , Joel Becker Cc: ocfs2-devel@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH v4 2/2] ocfs2: validate external xattr entries when reading metadata Date: Mon, 22 Jun 2026 21:03:32 +0800 Message-Id: <20260622130332.2094018-3-zzzccc427@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260622130332.2094018-1-zzzccc427@gmail.com> References: <20260622130332.2094018-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. 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 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 | 84 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) diff --git a/fs/ocfs2/xattr.c b/fs/ocfs2/xattr.c index 00900e65634d..8147d5c5f293 100644 --- a/fs/ocfs2/xattr.c +++ b/fs/ocfs2/xattr.c @@ -390,6 +390,13 @@ static int ocfs2_init_xattr_bucket(struct ocfs2_xattr_bucket *bucket, return rc; } +static int ocfs2_validate_xattr_entries(struct super_block *sb, u64 blkno, + struct ocfs2_xattr_header *xh, + size_t storage_size, + const char *where); +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 +415,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 +518,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; } @@ -1042,6 +1061,71 @@ int ocfs2_validate_inode_xattr(struct super_block *sb, u64 blkno, return ocfs2_validate_xattr_ibody_header(sb, blkno, di, NULL); } +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_xattr_ibody_lookup_header(struct inode *inode, struct ocfs2_dinode *di, struct ocfs2_xattr_header **header) -- 2.43.0