Skip to contentSkip to conversation

erofs-utils: lib: validate h_shared_count in erofs_init_inode_xattrs()

PATCH In review Utkal Singh 2 participants 4 messages Mar 16 – Mar 17 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 erofs-utils: lib: validate h_shared_count in erofs_init_inode_xattrs()

erofs_init_inode_xattrs() reads h_shared_count directly from the on-disk xattr ibody header and uses it to size a heap allocation and drive a read loop without checking whether the implied shared xattr array fits within xattr_isize.

A crafted EROFS image with a large h_shared_count but a minimal xattr_isize causes the subsequent loop to read shared xattr entries beyond the xattr ibody boundary, interpreting unrelated on-disk data as shared xattr IDs. This affects every library consumer -- dump.erofs, erofsfuse, and the rebuild path (lib/rebuild.c) -- none of which call the fsck-only erofs_verify_xattr() before reaching this code.

Validate that h_shared_count fits within the available xattr body space before allocating or reading. Use a division-based check to avoid any theoretical overflow in the multiplication.

The subtraction is safe because callers above already reject xattr_isize < sizeof(struct erofs_xattr_ibody_header).

---
 lib/xattr.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
lib/xattr.c +10 -0
index 565070a..6891812 100644
@@ -1182,6 +1182,16 @@ static int erofs_init_inode_xattrs(struct erofs_inode *vi)
 
 	ih = it.kaddr;
 	vi->xattr_shared_count = ih->h_shared_count;
+	/* validate h_shared_count fits within xattr_isize */
+	if (vi->xattr_shared_count >
+	    (vi->xattr_isize - sizeof(struct erofs_xattr_ibody_header)) /
+			sizeof(u32)) {
+		erofs_err("bogus h_shared_count %u (xattr_isize %u) @ nid %llu",
+			  vi->xattr_shared_count, vi->xattr_isize,
+			  vi->nid | 0ULL);
+		erofs_put_metabuf(&it.buf);
+		return -EFSCORRUPTED;
+	}
 	vi->xattr_shared_xattrs = malloc(vi->xattr_shared_count * sizeof(uint));
 	if (!vi->xattr_shared_xattrs) {
 		erofs_put_metabuf(&it.buf);
Signature · 1 line
2.43.0
#1 lore raw
Quoted 11 lines · Utkal Singh

erofs_init_inode_xattrs() reads h_shared_count directly from the on-disk xattr ibody header and uses it to size a heap allocation and drive a read loop without checking whether the implied shared xattr array fits within xattr_isize.

A crafted EROFS image with a large h_shared_count but a minimal xattr_isize causes the subsequent loop to read shared xattr entries beyond the xattr ibody boundary, interpreting unrelated on-disk data as shared xattr IDs. This affects every library consumer -- dump.erofs, erofsfuse, and the rebuild path (lib/rebuild.c) -- none of which call the fsck-only erofs_verify_xattr() before reaching this code.

I don't think other than fsck tool, this must be checked, since it won't cause any harmful behavior but the filesystem image is already corrupted, and because of the corruption, the user should get the corrupted result, but it still have no impact to the whole system stablity.

Quoted 4 lines

Validate that h_shared_count fits within the available xattr body space before allocating or reading. Use a division-based check to avoid any theoretical overflow in the multiplication.

I don't think it will overflow according to the ondisk format.

Quoted 3 lines

The subtraction is safe because callers above already reject xattr_isize < sizeof(struct erofs_xattr_ibody_header).

Please add a reproducible way.

Quoted 11 lines
---
  lib/xattr.c | 10 ++++++++++
  1 file changed, 10 insertions(+)
lib/xattr.c +0 -0
index 565070a..6891812 100644

lib/xattr.c static int erofs_init_inode_xattrs(struct erofs_inode *vi) · line 1188

Quoted 7 lines
lib/xattr.c +4 -0
@@ -1182,6 +1182,16 @@ static int erofs_init_inode_xattrs(struct erofs_inode *vi)
 
  	ih = it.kaddr;
  	vi->xattr_shared_count = ih->h_shared_count;
+	/* validate h_shared_count fits within xattr_isize */
+	if (vi->xattr_shared_count >
+	    (vi->xattr_isize - sizeof(struct erofs_xattr_ibody_header)) /
+			sizeof(u32)) {

Can we avoid division?

Quoted 9 lines
lib/xattr.c +6 -0
+		erofs_err("bogus h_shared_count %u (xattr_isize %u) @ nid %llu",
+			  vi->xattr_shared_count, vi->xattr_isize,
+			  vi->nid | 0ULL);
+		erofs_put_metabuf(&it.buf);
+		return -EFSCORRUPTED;
+	}
  	vi->xattr_shared_xattrs = malloc(vi->xattr_shared_count * sizeof(uint));
  	if (!vi->xattr_shared_xattrs) {
  		erofs_put_metabuf(&it.buf);
#2 lore raw
Quoted 15 lines · Gao Xiang
Quoted 11 lines · Utkal Singh

erofs_init_inode_xattrs() reads h_shared_count directly from the on-disk xattr ibody header and uses it to size a heap allocation and drive a read loop without checking whether the implied shared xattr array fits within xattr_isize.

A crafted EROFS image with a large h_shared_count but a minimal xattr_isize causes the subsequent loop to read shared xattr entries beyond the xattr ibody boundary, interpreting unrelated on-disk data as shared xattr IDs.  This affects every library consumer -- dump.erofs, erofsfuse, and the rebuild path (lib/rebuild.c) -- none of which call the fsck-only erofs_verify_xattr() before reaching this code.

Quoted 5 lines

I don't think other than fsck tool, this must be checked, since it won't cause any harmful behavior but the filesystem image is already corrupted, and because of the corruption, the user should get the corrupted result, but it still have no impact to the whole system stablity.

Nevertheless, I'm fine if we try to harden this part, but the commit message should clarify the impact: it actually has no stability impact out of those images.

Also there are many threads with your contribution, it's hard for me to follow those threads, now.

Would you mind raising a github issue
https://github.com/erofs/erofs-utils/issues

and list all your patches for merging (with meaningful topics are preferred) ?

Quoted 44 lines
Quoted 5 lines

Validate that h_shared_count fits within the available xattr body space before allocating or reading.  Use a division-based check to avoid any theoretical overflow in the multiplication.

I don't think it will overflow according to the ondisk format.

Quoted 3 lines

The subtraction is safe because callers above already reject xattr_isize < sizeof(struct erofs_xattr_ibody_header).

Please add a reproducible way.

Quoted 11 lines

---
  lib/xattr.c | 10 ++++++++++

  1 file changed, 10 insertions(+)
lib/xattr.c +0 -0
index 565070a..6891812 100644

lib/xattr.c static int erofs_init_inode_xattrs(struct erofs_inode *vi) · line 1187

Quoted 6 lines
lib/xattr.c +4 -0
@@ -1182,6 +1182,16 @@ static int erofs_init_inode_xattrs(struct erofs_inode *vi)
       ih = it.kaddr;
       vi->xattr_shared_count = ih->h_shared_count;
+    /* validate h_shared_count fits within xattr_isize */
+    if (vi->xattr_shared_count >
+        (vi->xattr_isize - sizeof(struct erofs_xattr_ibody_header)) /
+            sizeof(u32)) {

Can we avoid division?

Quoted 9 lines
lib/xattr.c +6 -0
+        erofs_err("bogus h_shared_count %u (xattr_isize %u) @ nid %llu",
+              vi->xattr_shared_count, vi->xattr_isize,
+              vi->nid | 0ULL);
+        erofs_put_metabuf(&it.buf);
+        return -EFSCORRUPTED;
+    }
       vi->xattr_shared_xattrs = malloc(vi->xattr_shared_count * sizeof(uint));
       if (!vi->xattr_shared_xattrs) {
           erofs_put_metabuf(&it.buf);
#3 lore raw

Hi Gao Xiang,

Thank you for the review.

Understood, I will include a reproducible way in v2 for both patches.

I have also created a GitHub issue to track these patches:
https://github.com/erofs/erofs-utils/issues/47

v2 will follow shortly.

Best regards,
Utkal Singh

Quoted 86 lines · Gao Xiang
Quoted 15 lines · Gao Xiang
Quoted 11 lines · Utkal Singh

erofs_init_inode_xattrs() reads h_shared_count directly from the on-disk xattr ibody header and uses it to size a heap allocation and drive a read loop without checking whether the implied shared xattr array fits within xattr_isize.

A crafted EROFS image with a large h_shared_count but a minimal xattr_isize causes the subsequent loop to read shared xattr entries beyond the xattr ibody boundary, interpreting unrelated on-disk data as shared xattr IDs. This affects every library consumer -- dump.erofs, erofsfuse, and the rebuild path (lib/rebuild.c) -- none of which call the fsck-only erofs_verify_xattr() before reaching this code.

Quoted 5 lines

I don't think other than fsck tool, this must be checked, since it won't cause any harmful behavior but the filesystem image is already corrupted, and because of the corruption, the user should get the corrupted result, but it still have no impact to the whole system stablity.

Nevertheless, I'm fine if we try to harden this part, but the commit message should clarify the impact: it actually has no stability impact out of those images.

Also there are many threads with your contribution, it's hard for me to follow those threads, now.

Would you mind raising a github issue
https://github.com/erofs/erofs-utils/issues

and list all your patches for merging (with meaningful topics are preferred) ?

Quoted 20 lines
Quoted 5 lines

Validate that h_shared_count fits within the available xattr body space before allocating or reading. Use a division-based check to avoid any theoretical overflow in the multiplication.

I don't think it will overflow according to the ondisk format.

Quoted 3 lines

The subtraction is safe because callers above already reject xattr_isize < sizeof(struct erofs_xattr_ibody_header).

Please add a reproducible way.

Quoted 6 lines
---
  lib/xattr.c | 10 ++++++++++
  1 file changed, 10 insertions(+)
Quoted 5 lines

lib/xattr.c static int erofs_init_inode_xattrs(struct

Quoted 5 lines
lib/xattr.c +0 -0
index 565070a..6891812 100644
@@ -1182,6 +1182,16 @@ static int erofs_init_inode_xattrs(struct

erofs_inode *vi)

Quoted 10 lines

lib/xattr.c static int erofs_init_inode_xattrs(struct erofs_inode *vi) · line 1188

Quoted 6 lines
lib/xattr.c +4 -0
      ih = it.kaddr;
      vi->xattr_shared_count = ih->h_shared_count;
+    /* validate h_shared_count fits within xattr_isize */
+    if (vi->xattr_shared_count >
+        (vi->xattr_isize - sizeof(struct erofs_xattr_ibody_header)) /
+            sizeof(u32)) {

Can we avoid division?

Quoted 1 line
lib/xattr.c +1 -0
+        erofs_err("bogus h_shared_count %u (xattr_isize %u) @ nid

%llu",

Quoted 6 lines

lib/xattr.c static int erofs_init_inode_xattrs(struct erofs_inode *vi)

Quoted 6 lines
lib/xattr.c +5 -0
+              vi->xattr_shared_count, vi->xattr_isize,
+              vi->nid | 0ULL);
+        erofs_put_metabuf(&it.buf);
+        return -EFSCORRUPTED;
+    }
      vi->xattr_shared_xattrs = malloc(vi->xattr_shared_count *

sizeof(uint));

Quoted 3 lines
Quoted 2 lines
lib/xattr.c +0 -0
      if (!vi->xattr_shared_xattrs) {
          erofs_put_metabuf(&it.buf);