Linux EXT4 FS development
 help / color / mirror / Atom feed
From: Eric Sandeen <sandeen@redhat.com>
To: linux-ext4@vger.kernel.org
Cc: tytso@mit.edu, sandeen@redhat.com, agruenba@redhat.com
Subject: [PATCH 5/8] libext2fs: fix ea loop in read_xattrs_from_buffer
Date: Thu, 24 Sep 2026 17:23:39 -0500	[thread overview]
Message-ID: <20260924222944.3683556-6-sandeen@redhat.com> (raw)
In-Reply-To: <20260924222944.3683556-1-sandeen@redhat.com>

From: Andreas Gruenbacher <agruenba@redhat.com>

Fix the same problems as in check_ea_in_inode().

There is no need for recalculating the remaining space in the second
iteration.

Signed-off-by: Andreas Gruenbacher <agruenba@redhat.com>
Signed-off-by: Eric Sandeen <sandeen@redhat.com>
---
 lib/ext2fs/ext_attr.c | 38 ++++++++++++--------------------------
 1 file changed, 12 insertions(+), 26 deletions(-)

diff --git a/lib/ext2fs/ext_attr.c b/lib/ext2fs/ext_attr.c
index 914bff1d..6f6d2f4f 100644
--- a/lib/ext2fs/ext_attr.c
+++ b/lib/ext2fs/ext_attr.c
@@ -884,26 +884,23 @@ static errcode_t read_xattrs_from_buffer(struct ext2_xattr_handle *handle,
 
 	/* find the end */
 	end = entries;
-	remain = storage_size;
-	while (remain >= sizeof(struct ext2_ext_attr_entry) &&
-	       !EXT2_EXT_IS_LAST_ENTRY(end)) {
-
-		/* header eats this space */
-		remain -= sizeof(struct ext2_ext_attr_entry);
-
-		/* is attribute name valid? */
-		if (EXT2_EXT_ATTR_SIZE(end->e_name_len) > remain)
+	/* take finish entry 0UL into account */
+	remain = storage_size - sizeof(__u32);
+	while (!EXT2_EXT_IS_LAST_ENTRY(end)) {
+		/* end->e_name_len is within the first four bytes */
+		if (EXT2_EXT_ATTR_LEN(end->e_name_len) > remain)
 			return EXT2_ET_EA_BAD_NAME_LEN;
-
-		/* attribute len eats this space */
-		remain -= EXT2_EXT_ATTR_SIZE(end->e_name_len);
+		remain -= EXT2_EXT_ATTR_LEN(end->e_name_len);
+		if (!end->e_value_inum) {
+			if (EXT2_EXT_ATTR_SIZE(end->e_value_size) > remain)
+				return EXT2_ET_EA_BAD_VALUE_SIZE;
+			remain -= EXT2_EXT_ATTR_SIZE(end->e_value_size);
+		}
 		end = EXT2_EXT_ATTR_NEXT(end);
 	}
 
 	entry = entries;
-	remain = storage_size;
-	while (remain >= sizeof(struct ext2_ext_attr_entry) &&
-	       !EXT2_EXT_IS_LAST_ENTRY(entry)) {
+	while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
 
 		/* Allocate space for more attrs? */
 		if (handle->count == handle->capacity) {
@@ -914,12 +911,6 @@ static errcode_t read_xattrs_from_buffer(struct ext2_xattr_handle *handle,
 
 		x = handle->attrs + handle->count;
 
-		/* header eats this space */
-		remain -= sizeof(struct ext2_ext_attr_entry);
-
-		/* attribute len eats this space */
-		remain -= EXT2_EXT_ATTR_SIZE(entry->e_name_len);
-
 		/* Extract name */
 		prefix = find_ea_prefix(entry->e_name_index);
 		prefix_len = (prefix ? strlen(prefix) : 0);
@@ -942,9 +933,6 @@ static errcode_t read_xattrs_from_buffer(struct ext2_xattr_handle *handle,
 			return EXT2_ET_BAD_EA_BLOCK_NUM;
 
 		if (entry->e_value_inum == 0) {
-			if (entry->e_value_size > remain)
-				return EXT2_ET_EA_BAD_VALUE_SIZE;
-
 			if (entry->e_value_offs + entry->e_value_size > values_size)
 				return EXT2_ET_EA_BAD_VALUE_OFFSET;
 
@@ -953,8 +941,6 @@ static errcode_t read_xattrs_from_buffer(struct ext2_xattr_handle *handle,
 			    (char *)end + sizeof(__u32))
 				return EXT2_ET_EA_BAD_VALUE_OFFSET;
 
-			remain -= entry->e_value_size;
-
 			err = ext2fs_get_mem(entry->e_value_size, &x->value);
 			if (err)
 				return err;
-- 
2.55.0


  parent reply	other threads:[~2026-09-24 22:29 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-24 22:23 [PATCH 0/8] e2fsprogs: fix extended attribute iteration loop bounds checking Eric Sandeen
2026-09-24 22:23 ` [PATCH 1/8] e2fsck: fix in-inode extended attribute checking Eric Sandeen
2026-09-24 22:23 ` [PATCH 2/8] e2fsck: fix extended attribute block checking Eric Sandeen
2026-09-24 22:23 ` [PATCH 3/8] e2fsck: fix ea loop in inc_ea_inode_refs Eric Sandeen
2026-09-24 22:23 ` [PATCH 4/8] libext2fs: fix ea loop in ext2fs_ext_attr_block_rehash Eric Sandeen
2026-09-24 22:23 ` Eric Sandeen [this message]
2026-09-24 22:23 ` [PATCH 6/8] tune2fs: fix ea loop in update_xattr_entry_hashes Eric Sandeen
2026-09-24 22:23 ` [PATCH 7/8] resize2fs: " Eric Sandeen
2026-09-24 22:23 ` [PATCH 8/8] libext2fs: fix ea loop in ext2fs_xattr_inode_max_size Eric Sandeen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260924222944.3683556-6-sandeen@redhat.com \
    --to=sandeen@redhat.com \
    --cc=agruenba@redhat.com \
    --cc=linux-ext4@vger.kernel.org \
    --cc=tytso@mit.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox