Skip to contentSkip to conversation

erofs: fix the out-of-bounds nameoff handling for trailing dirents

PATCH Superseded Gao Xiang 2 participants 8 messages Apr 16 – Apr 21

Subsystems (2)
lore
Share & tools
lore

Gao Xiang

author
#0 lore raw

PATCH 1/1 erofs: fix the out-of-bounds nameoff handling for trailing dirents

Currently we already have boundary-checks for nameoffs, but the trailing dirents are special since the namelens are calculated with strnlen() with unchecked nameoffs.

If a crafted EROFS has a trailing dirent with nameoff >= maxsize, maxsize - nameoff can underflow, causing strnlen() to read past the directory block.

Fixes 3aa8ec716e52 ("staging: erofs: add directory operations")
Fixes 33bac912840f ("staging: erofs: keep corrupted fs from crashing kernel in erofs_readdir()")
---
 fs/erofs/dir.c | 26 ++++++++++++++------------
 1 file changed, 14 insertions(+), 12 deletions(-)
fs/erofs/dir.c +14 -12
index e5132575b9d3..ecfac9b49322 100644
@@ -19,20 +19,18 @@ static int erofs_fill_dentries(struct inode *dir, struct dir_context *ctx,
 		const char *de_name = (char *)dentry_blk + nameoff;
 		unsigned int de_namelen;
 
-		/* the last dirent in the block? */
-		if (de + 1 >= end)
-			de_namelen = strnlen(de_name, maxsize - nameoff);
-		else
+		/* non-trailing dirent in the directory block? */
+		if (de + 1 < end)
 			de_namelen = le16_to_cpu(de[1].nameoff) - nameoff;
+		else if (maxsize <= nameoff)
+			goto err_bogus;
+		else
+			de_namelen = strnlen(de_name, maxsize - nameoff);
 
-		/* a corrupted entry is found */
-		if (nameoff + de_namelen > maxsize ||
-		    de_namelen > EROFS_NAME_LEN) {
-			erofs_err(dir->i_sb, "bogus dirent @ nid %llu",
-				  EROFS_I(dir)->nid);
-			DBG_BUGON(1);
-			return -EFSCORRUPTED;
-		}
+		/* a corrupted entry is found (including negative namelen) */
+		if (!clamp_t(unsigned int, de_namelen, 1, EROFS_NAME_LEN) ||
+		    nameoff + de_namelen > maxsize)
+			goto err_bogus;
 
 		if (!dir_emit(ctx, de_name, de_namelen,
 			      erofs_nid_to_ino64(EROFS_SB(dir->i_sb),
@@ -42,6 +40,10 @@ static int erofs_fill_dentries(struct inode *dir, struct dir_context *ctx,
 		ctx->pos += sizeof(struct erofs_dirent);
 	}
 	return 0;
+err_bogus:
+	erofs_err(dir->i_sb, "bogus dirent @ nid %llu", EROFS_I(dir)->nid);
+	DBG_BUGON(1);
+	return -EFSCORRUPTED;
 }
 
 static int erofs_readdir(struct file *f, struct dir_context *ctx)
Signature · 1 line
2.43.5
author
#1 lore raw

PATCH v2 1/1 erofs: fix the out-of-bounds nameoff handling for trailing dirents

Currently we already have boundary-checks for nameoffs, but the trailing dirents are special since the namelens are calculated with strnlen() with unchecked nameoffs.

If a crafted EROFS has a trailing dirent with nameoff >= maxsize, maxsize - nameoff can underflow, causing strnlen() to read past the directory block.

Fixes 3aa8ec716e52 ("staging: erofs: add directory operations")
Fixes 33bac912840f ("staging: erofs: keep corrupted fs from crashing kernel in erofs_readdir()")

---
v2:
- should use in_range32() instead.

 fs/erofs/dir.c | 26 ++++++++++++++------------
 1 file changed, 14 insertions(+), 12 deletions(-)
fs/erofs/dir.c +14 -12
index e5132575b9d3..c7717149c5ed 100644
@@ -19,20 +19,18 @@ static int erofs_fill_dentries(struct inode *dir, struct dir_context *ctx,
 		const char *de_name = (char *)dentry_blk + nameoff;
 		unsigned int de_namelen;
 
-		/* the last dirent in the block? */
-		if (de + 1 >= end)
-			de_namelen = strnlen(de_name, maxsize - nameoff);
-		else
+		/* non-trailing dirent in the directory block? */
+		if (de + 1 < end)
 			de_namelen = le16_to_cpu(de[1].nameoff) - nameoff;
+		else if (maxsize <= nameoff)
+			goto err_bogus;
+		else
+			de_namelen = strnlen(de_name, maxsize - nameoff);
 
-		/* a corrupted entry is found */
-		if (nameoff + de_namelen > maxsize ||
-		    de_namelen > EROFS_NAME_LEN) {
-			erofs_err(dir->i_sb, "bogus dirent @ nid %llu",
-				  EROFS_I(dir)->nid);
-			DBG_BUGON(1);
-			return -EFSCORRUPTED;
-		}
+		/* a corrupted entry is found (including negative namelen) */
+		if (!in_range32(de_namelen, 1, EROFS_NAME_LEN) ||
+		    nameoff + de_namelen > maxsize)
+			goto err_bogus;
 
 		if (!dir_emit(ctx, de_name, de_namelen,
 			      erofs_nid_to_ino64(EROFS_SB(dir->i_sb),
@@ -42,6 +40,10 @@ static int erofs_fill_dentries(struct inode *dir, struct dir_context *ctx,
 		ctx->pos += sizeof(struct erofs_dirent);
 	}
 	return 0;
+err_bogus:
+	erofs_err(dir->i_sb, "bogus dirent @ nid %llu", EROFS_I(dir)->nid);
+	DBG_BUGON(1);
+	return -EFSCORRUPTED;
 }
 
 static int erofs_readdir(struct file *f, struct dir_context *ctx)
Signature · 1 line
2.43.5
author
#2 lore raw

PATCH v3 1/1 erofs: fix the out-of-bounds nameoff handling for trailing dirents

Currently we already have boundary-checks for nameoffs, but the trailing dirents are special since the namelens are calculated with strnlen() with unchecked nameoffs.

If a crafted EROFS has a trailing dirent with nameoff >= maxsize, maxsize - nameoff can underflow, causing strnlen() to read past the directory block.

nameoff0 should also be verified to be a multiple of sizeof(struct erofs_dirent) as well [1].

[1] https://sashiko.dev/#/patchset/20260416063511.3173774-1-hsiangkao%40linux.alibaba.com

Fixes 3aa8ec716e52 ("staging: erofs: add directory operations")
Fixes 33bac912840f ("staging: erofs: keep corrupted fs from crashing kernel in erofs_readdir()")

---
v3:
- Disallow unaligned nameoff0 to avoid petential oob reads as well.

 fs/erofs/dir.c | 29 ++++++++++++++++-------------
 1 file changed, 16 insertions(+), 13 deletions(-)
fs/erofs/dir.c +16 -13
index e5132575b9d3..d074fded1577 100644
@@ -19,20 +19,18 @@ static int erofs_fill_dentries(struct inode *dir, struct dir_context *ctx,
 		const char *de_name = (char *)dentry_blk + nameoff;
 		unsigned int de_namelen;
 
-		/* the last dirent in the block? */
-		if (de + 1 >= end)
-			de_namelen = strnlen(de_name, maxsize - nameoff);
-		else
+		/* non-trailing dirent in the directory block? */
+		if (de + 1 < end)
 			de_namelen = le16_to_cpu(de[1].nameoff) - nameoff;
+		else if (maxsize <= nameoff)
+			goto err_bogus;
+		else
+			de_namelen = strnlen(de_name, maxsize - nameoff);
 
-		/* a corrupted entry is found */
-		if (nameoff + de_namelen > maxsize ||
-		    de_namelen > EROFS_NAME_LEN) {
-			erofs_err(dir->i_sb, "bogus dirent @ nid %llu",
-				  EROFS_I(dir)->nid);
-			DBG_BUGON(1);
-			return -EFSCORRUPTED;
-		}
+		/* a corrupted entry is found (including negative namelen) */
+		if (!in_range32(de_namelen, 1, EROFS_NAME_LEN) ||
+		    nameoff + de_namelen > maxsize)
+			goto err_bogus;
 
 		if (!dir_emit(ctx, de_name, de_namelen,
 			      erofs_nid_to_ino64(EROFS_SB(dir->i_sb),
@@ -42,6 +40,10 @@ static int erofs_fill_dentries(struct inode *dir, struct dir_context *ctx,
 		ctx->pos += sizeof(struct erofs_dirent);
 	}
 	return 0;
+err_bogus:
+	erofs_err(dir->i_sb, "bogus dirent @ nid %llu", EROFS_I(dir)->nid);
+	DBG_BUGON(1);
+	return -EFSCORRUPTED;
 }
 
 static int erofs_readdir(struct file *f, struct dir_context *ctx)
@@ -88,7 +90,8 @@ static int erofs_readdir(struct file *f, struct dir_context *ctx)
 		}
 
 		nameoff = le16_to_cpu(de->nameoff);
-		if (nameoff < sizeof(struct erofs_dirent) || nameoff >= bsz) {
+		if (nameoff < sizeof(struct erofs_dirent) || nameoff >= bsz ||
+		    (nameoff % sizeof(struct erofs_dirent))) {
 			erofs_err(sb, "invalid de[0].nameoff %u @ nid %llu",
 				  nameoff, EROFS_I(dir)->nid);
 			err = -EFSCORRUPTED;
Signature · 1 line
2.43.5
maintainer
#3 lore raw
Quoted 73 lines · Gao Xiang

Currently we already have boundary-checks for nameoffs, but the trailing dirents are special since the namelens are calculated with strnlen() with unchecked nameoffs.

If a crafted EROFS has a trailing dirent with nameoff >= maxsize, maxsize - nameoff can underflow, causing strnlen() to read past the directory block.

nameoff0 should also be verified to be a multiple of sizeof(struct erofs_dirent) as well [1].

[1] https://sashiko.dev/#/patchset/20260416063511.3173774-1-hsiangkao%40linux.alibaba.com

Fixes 3aa8ec716e52 ("staging: erofs: add directory operations")
Fixes 33bac912840f ("staging: erofs: keep corrupted fs from crashing kernel in erofs_readdir()")

---
v3:
- Disallow unaligned nameoff0 to avoid petential oob reads as well.

  fs/erofs/dir.c | 29 ++++++++++++++++-------------
  1 file changed, 16 insertions(+), 13 deletions(-)
fs/erofs/dir.c +14 -12
index e5132575b9d3..d074fded1577 100644
@@ -19,20 +19,18 @@ static int erofs_fill_dentries(struct inode *dir, struct dir_context *ctx,
  		const char *de_name = (char *)dentry_blk + nameoff;
  		unsigned int de_namelen;
 
-		/* the last dirent in the block? */
-		if (de + 1 >= end)
-			de_namelen = strnlen(de_name, maxsize - nameoff);
-		else
+		/* non-trailing dirent in the directory block? */
+		if (de + 1 < end)
  			de_namelen = le16_to_cpu(de[1].nameoff) - nameoff;
+		else if (maxsize <= nameoff)
+			goto err_bogus;
+		else
+			de_namelen = strnlen(de_name, maxsize - nameoff);
 
-		/* a corrupted entry is found */
-		if (nameoff + de_namelen > maxsize ||
-		    de_namelen > EROFS_NAME_LEN) {
-			erofs_err(dir->i_sb, "bogus dirent @ nid %llu",
-				  EROFS_I(dir)->nid);
-			DBG_BUGON(1);
-			return -EFSCORRUPTED;
-		}
+		/* a corrupted entry is found (including negative namelen) */
+		if (!in_range32(de_namelen, 1, EROFS_NAME_LEN) ||
+		    nameoff + de_namelen > maxsize)
+			goto err_bogus;
 
  		if (!dir_emit(ctx, de_name, de_namelen,
  			      erofs_nid_to_ino64(EROFS_SB(dir->i_sb),
@@ -42,6 +40,10 @@ static int erofs_fill_dentries(struct inode *dir, struct dir_context *ctx,
  		ctx->pos += sizeof(struct erofs_dirent);
  	}
  	return 0;
+err_bogus:
+	erofs_err(dir->i_sb, "bogus dirent @ nid %llu", EROFS_I(dir)->nid);
+	DBG_BUGON(1);
+	return -EFSCORRUPTED;
  }
 
  static int erofs_readdir(struct file *f, struct dir_context *ctx)

fs/erofs/dir.c static int erofs_readdir(struct file *f, struct dir_context *ctx) · line 91

Quoted 4 lines
fs/erofs/dir.c +0 -1
@@ -88,7 +90,8 @@ static int erofs_readdir(struct file *f, struct dir_context *ctx)
  		}
 
  		nameoff = le16_to_cpu(de->nameoff);
-		if (nameoff < sizeof(struct erofs_dirent) || nameoff >= bsz) {

You mean?

if (!nameoff || nameoff >= bsz || nameoff % sizeof(struct erofs_dirent))

Thanks,

Quoted 5 lines
fs/erofs/dir.c +2 -0
+		if (nameoff < sizeof(struct erofs_dirent) || nameoff >= bsz ||
+		    (nameoff % sizeof(struct erofs_dirent))) {
  			erofs_err(sb, "invalid de[0].nameoff %u @ nid %llu",
  				  nameoff, EROFS_I(dir)->nid);
  			err = -EFSCORRUPTED;
author
#4 lore raw
Quoted 76 lines · Chao Yu
Quoted 69 lines · Gao Xiang

Currently we already have boundary-checks for nameoffs, but the trailing dirents are special since the namelens are calculated with strnlen() with unchecked nameoffs.

If a crafted EROFS has a trailing dirent with nameoff >= maxsize, maxsize - nameoff can underflow, causing strnlen() to read past the directory block.

nameoff0 should also be verified to be a multiple of sizeof(struct erofs_dirent) as well [1].

[1] https://sashiko.dev/#/patchset/20260416063511.3173774-1-hsiangkao%40linux.alibaba.com

Fixes 3aa8ec716e52 ("staging: erofs: add directory operations")
Fixes 33bac912840f ("staging: erofs: keep corrupted fs from crashing kernel in erofs_readdir()")

---
v3:
  - Disallow unaligned nameoff0 to avoid petential oob reads as well.

  fs/erofs/dir.c | 29 ++++++++++++++++-------------

  1 file changed, 16 insertions(+), 13 deletions(-)
fs/erofs/dir.c +14 -12
index e5132575b9d3..d074fded1577 100644
@@ -19,20 +19,18 @@ static int erofs_fill_dentries(struct inode *dir, struct dir_context *ctx,
           const char *de_name = (char *)dentry_blk + nameoff;
           unsigned int de_namelen;
-        /* the last dirent in the block? */
-        if (de + 1 >= end)
-            de_namelen = strnlen(de_name, maxsize - nameoff);
-        else
+        /* non-trailing dirent in the directory block? */
+        if (de + 1 < end)
               de_namelen = le16_to_cpu(de[1].nameoff) - nameoff;
+        else if (maxsize <= nameoff)
+            goto err_bogus;
+        else
+            de_namelen = strnlen(de_name, maxsize - nameoff);
-        /* a corrupted entry is found */
-        if (nameoff + de_namelen > maxsize ||
-            de_namelen > EROFS_NAME_LEN) {
-            erofs_err(dir->i_sb, "bogus dirent @ nid %llu",
-                  EROFS_I(dir)->nid);
-            DBG_BUGON(1);
-            return -EFSCORRUPTED;
-        }
+        /* a corrupted entry is found (including negative namelen) */
+        if (!in_range32(de_namelen, 1, EROFS_NAME_LEN) ||
+            nameoff + de_namelen > maxsize)
+            goto err_bogus;
           if (!dir_emit(ctx, de_name, de_namelen,
                     erofs_nid_to_ino64(EROFS_SB(dir->i_sb),
@@ -42,6 +40,10 @@ static int erofs_fill_dentries(struct inode *dir, struct dir_context *ctx,
           ctx->pos += sizeof(struct erofs_dirent);
       }
       return 0;
+err_bogus:
+    erofs_err(dir->i_sb, "bogus dirent @ nid %llu", EROFS_I(dir)->nid);
+    DBG_BUGON(1);
+    return -EFSCORRUPTED;
   }
   static int erofs_readdir(struct file *f, struct dir_context *ctx)

fs/erofs/dir.c static int erofs_readdir(struct file *f, struct dir_context *ctx) · line 90

Quoted 3 lines
fs/erofs/dir.c +0 -1
@@ -88,7 +90,8 @@ static int erofs_readdir(struct file *f, struct dir_context *ctx)
           }
           nameoff = le16_to_cpu(de->nameoff);
-        if (nameoff < sizeof(struct erofs_dirent) || nameoff >= bsz) {

You mean?

Quoted 1 line

if (!nameoff || nameoff >= bsz || nameoff % sizeof(struct erofs_dirent))

The explanation can be seen as:
https://sashiko.dev/#/patchset/20260416063511.3173774-1-hsiangkao%40linux.alibaba.com

But I think nameoff < sizeof(struct erofs_dirent) is also fine? I could also switch to your suggested version.

Thanks,
Gao Xiang

maintainer
#5 lore raw
Quoted 79 lines · Gao Xiang
Quoted 76 lines · Chao Yu
Quoted 69 lines · Gao Xiang

Currently we already have boundary-checks for nameoffs, but the trailing dirents are special since the namelens are calculated with strnlen() with unchecked nameoffs.

If a crafted EROFS has a trailing dirent with nameoff >= maxsize, maxsize - nameoff can underflow, causing strnlen() to read past the directory block.

nameoff0 should also be verified to be a multiple of sizeof(struct erofs_dirent) as well [1].

[1] https://sashiko.dev/#/patchset/20260416063511.3173774-1-hsiangkao%40linux.alibaba.com

Fixes 3aa8ec716e52 ("staging: erofs: add directory operations")
Fixes 33bac912840f ("staging: erofs: keep corrupted fs from crashing kernel in erofs_readdir()")

---
v3:
  - Disallow unaligned nameoff0 to avoid petential oob reads as well.

   fs/erofs/dir.c | 29 ++++++++++++++++-------------
   1 file changed, 16 insertions(+), 13 deletions(-)
fs/erofs/dir.c +14 -12
index e5132575b9d3..d074fded1577 100644
@@ -19,20 +19,18 @@ static int erofs_fill_dentries(struct inode *dir, struct dir_context *ctx,
           const char *de_name = (char *)dentry_blk + nameoff;
           unsigned int de_namelen;
-        /* the last dirent in the block? */
-        if (de + 1 >= end)
-            de_namelen = strnlen(de_name, maxsize - nameoff);
-        else
+        /* non-trailing dirent in the directory block? */
+        if (de + 1 < end)
               de_namelen = le16_to_cpu(de[1].nameoff) - nameoff;
+        else if (maxsize <= nameoff)
+            goto err_bogus;
+        else
+            de_namelen = strnlen(de_name, maxsize - nameoff);
-        /* a corrupted entry is found */
-        if (nameoff + de_namelen > maxsize ||
-            de_namelen > EROFS_NAME_LEN) {
-            erofs_err(dir->i_sb, "bogus dirent @ nid %llu",
-                  EROFS_I(dir)->nid);
-            DBG_BUGON(1);
-            return -EFSCORRUPTED;
-        }
+        /* a corrupted entry is found (including negative namelen) */
+        if (!in_range32(de_namelen, 1, EROFS_NAME_LEN) ||
+            nameoff + de_namelen > maxsize)
+            goto err_bogus;
           if (!dir_emit(ctx, de_name, de_namelen,
                     erofs_nid_to_ino64(EROFS_SB(dir->i_sb),
@@ -42,6 +40,10 @@ static int erofs_fill_dentries(struct inode *dir, struct dir_context *ctx,
           ctx->pos += sizeof(struct erofs_dirent);
       }
       return 0;
+err_bogus:
+    erofs_err(dir->i_sb, "bogus dirent @ nid %llu", EROFS_I(dir)->nid);
+    DBG_BUGON(1);
+    return -EFSCORRUPTED;
   }
   static int erofs_readdir(struct file *f, struct dir_context *ctx)

fs/erofs/dir.c static int erofs_readdir(struct file *f, struct dir_context *ctx) · line 90

Quoted 3 lines
fs/erofs/dir.c +0 -1
@@ -88,7 +90,8 @@ static int erofs_readdir(struct file *f, struct dir_context *ctx)
           }
           nameoff = le16_to_cpu(de->nameoff);
-        if (nameoff < sizeof(struct erofs_dirent) || nameoff >= bsz) {

You mean?

Quoted 6 lines
Quoted 1 line

if (!nameoff || nameoff >= bsz || nameoff % sizeof(struct erofs_dirent))

The explanation can be seen as:
https://sashiko.dev/#/patchset/20260416063511.3173774-1-hsiangkao%40linux.alibaba.com

But I think nameoff < sizeof(struct erofs_dirent) is also fine?

Yes, it's fine to use "nameoff < sizeof(struct erofs_dirent)", it's a minor cleanup to use "!nameof".

Thanks,

Quoted 5 lines

I could also switch to your suggested version.

Thanks,
Gao Xiang

author
#6 lore raw

PATCH v4 1/1 erofs: fix the out-of-bounds nameoff handling for trailing dirents

In reply to Chao Yu

Currently we already have boundary-checks for nameoffs, but the trailing dirents are special since the namelens are calculated with strnlen() with unchecked nameoffs.

If a crafted EROFS has a trailing dirent with nameoff >= maxsize, maxsize - nameoff can underflow, causing strnlen() to read past the directory block.

nameoff0 should also be verified to be a multiple of sizeof(struct erofs_dirent) as well [1].

[1] https://sashiko.dev/#/patchset/20260416063511.3173774-1-hsiangkao%40linux.alibaba.com

Fixes 3aa8ec716e52 ("staging: erofs: add directory operations")
Fixes 33bac912840f ("staging: erofs: keep corrupted fs from crashing kernel in erofs_readdir()")

---
v4:
- switch to if (!nameoff || nameoff >= bsz || (nameoff % sizeof(*de))) as suggested by Chao.

 fs/erofs/dir.c | 28 +++++++++++++++-------------
 1 file changed, 15 insertions(+), 13 deletions(-)
fs/erofs/dir.c +15 -13
index e5132575b9d3..4aa52a5f204a 100644
@@ -19,20 +19,18 @@ static int erofs_fill_dentries(struct inode *dir, struct dir_context *ctx,
 		const char *de_name = (char *)dentry_blk + nameoff;
 		unsigned int de_namelen;
 
-		/* the last dirent in the block? */
-		if (de + 1 >= end)
-			de_namelen = strnlen(de_name, maxsize - nameoff);
-		else
+		/* non-trailing dirent in the directory block? */
+		if (de + 1 < end)
 			de_namelen = le16_to_cpu(de[1].nameoff) - nameoff;
+		else if (maxsize <= nameoff)
+			goto err_bogus;
+		else
+			de_namelen = strnlen(de_name, maxsize - nameoff);
 
-		/* a corrupted entry is found */
-		if (nameoff + de_namelen > maxsize ||
-		    de_namelen > EROFS_NAME_LEN) {
-			erofs_err(dir->i_sb, "bogus dirent @ nid %llu",
-				  EROFS_I(dir)->nid);
-			DBG_BUGON(1);
-			return -EFSCORRUPTED;
-		}
+		/* a corrupted entry is found (including negative namelen) */
+		if (!in_range32(de_namelen, 1, EROFS_NAME_LEN) ||
+		    nameoff + de_namelen > maxsize)
+			goto err_bogus;
 
 		if (!dir_emit(ctx, de_name, de_namelen,
 			      erofs_nid_to_ino64(EROFS_SB(dir->i_sb),
@@ -42,6 +40,10 @@ static int erofs_fill_dentries(struct inode *dir, struct dir_context *ctx,
 		ctx->pos += sizeof(struct erofs_dirent);
 	}
 	return 0;
+err_bogus:
+	erofs_err(dir->i_sb, "bogus dirent @ nid %llu", EROFS_I(dir)->nid);
+	DBG_BUGON(1);
+	return -EFSCORRUPTED;
 }
 
 static int erofs_readdir(struct file *f, struct dir_context *ctx)
@@ -88,7 +90,7 @@ static int erofs_readdir(struct file *f, struct dir_context *ctx)
 		}
 
 		nameoff = le16_to_cpu(de->nameoff);
-		if (nameoff < sizeof(struct erofs_dirent) || nameoff >= bsz) {
+		if (!nameoff || nameoff >= bsz || (nameoff % sizeof(*de))) {
 			erofs_err(sb, "invalid de[0].nameoff %u @ nid %llu",
 				  nameoff, EROFS_I(dir)->nid);
 			err = -EFSCORRUPTED;
Signature · 1 line
2.43.5
maintainer Reviewed-by on v4 1/1
#7 lore raw
Quoted 13 lines · Gao Xiang

Currently we already have boundary-checks for nameoffs, but the trailing dirents are special since the namelens are calculated with strnlen() with unchecked nameoffs.

If a crafted EROFS has a trailing dirent with nameoff >= maxsize, maxsize - nameoff can underflow, causing strnlen() to read past the directory block.

nameoff0 should also be verified to be a multiple of sizeof(struct erofs_dirent) as well [1].

[1] https://sashiko.dev/#/patchset/20260416063511.3173774-1-hsiangkao%40linux.alibaba.com

Quoted 7 lines
Fixes 3aa8ec716e52 ("staging: erofs: add directory operations")
Fixes 33bac912840f ("staging: erofs: keep corrupted fs from crashing kernel in erofs_readdir()")

Thanks,