From: Utkal Singh <singhutkal015@gmail.com>
To: linux-erofs@lists.ozlabs.org
Cc: xiang@kernel.org, Utkal Singh <singhutkal015@gmail.com>
Subject: [PATCH v2] erofs-utils: lib: harden h_shared_count in erofs_init_inode_xattrs()
Date: Tue, 17 Mar 2026 15:15:14 +0000 [thread overview]
Message-ID: <20260317151514.3529-1-singhutkal015@gmail.com> (raw)
`u8 h_shared_count` indicates the shared xattr count of an inode. It is
read from the on-disk xattr ibody header, which should be corrupted if
the size of the shared xattr array exceeds the space available in
`xattr_isize`.
It does not cause harmful consequence (e.g. crashes), since the image is
already considered corrupted, it indeed results in the silent processing
of garbage metadata.
Let's harden it to report -EFSCORRUPTED earlier.
Reproducer:
mkdir testdir && echo hello > testdir/a.txt
setfattr -n user.test -v val testdir/a.txt
mkfs.erofs test.img testdir
# corrupt h_shared_count (offset = nid*32 + inode_size + 4) to 0xFF
# then: fsck.erofs --extract=/tmp/out --xattrs test_corrupted.img
# Without patch: silently processes invalid shared xattr IDs
# With patch: returns -EFSCORRUPTED
Signed-off-by: Utkal Singh <singhutkal015@gmail.com>
---
lib/xattr.c | 7 +++++++
1 file changed, 7 insertions(+)
diff --git a/lib/xattr.c b/lib/xattr.c
index 565070a..9d52a18 100644
--- a/lib/xattr.c
+++ b/lib/xattr.c
@@ -1182,6 +1182,13 @@ static int erofs_init_inode_xattrs(struct erofs_inode *vi)
ih = it.kaddr;
vi->xattr_shared_count = ih->h_shared_count;
+ if (vi->xattr_shared_count * sizeof(__le32) >
+ vi->xattr_isize - sizeof(struct erofs_xattr_ibody_header)) {
+ erofs_err("invalid h_shared_count %u in nid %llu",
+ vi->xattr_shared_count, 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.43.0
next reply other threads:[~2026-03-17 15:15 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-17 15:15 Utkal Singh [this message]
2026-03-17 15:25 ` [PATCH v2] erofs-utils: lib: harden h_shared_count in erofs_init_inode_xattrs() Gao Xiang
2026-03-17 15:27 ` Gao Xiang
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=20260317151514.3529-1-singhutkal015@gmail.com \
--to=singhutkal015@gmail.com \
--cc=linux-erofs@lists.ozlabs.org \
--cc=xiang@kernel.org \
/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