Skip to contentSkip to conversation

erofs-utils: lib: bound-check truncated directory blocks

PATCH New Vansh Choudhary 1 participant 1 message Mar 30 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 erofs-utils: lib: bound-check truncated directory blocks

Reject directory blocks smaller than one dirent, and validate the first dirent nameoff against the bytes actually read rather than the block size.

This avoids walking past valid data when the last directory block is truncated in erofs_iterate_dir() or erofs_namei().

---
 lib/dir.c   | 8 ++++++--
 lib/namei.c | 8 ++++++--
 2 files changed, 12 insertions(+), 4 deletions(-)
lib/dir.c +6 -2
index 98edb8e..bf57849 100644
@@ -172,9 +172,13 @@ int erofs_iterate_dir(struct erofs_dir_context *ctx, bool fsck)
 			return err;
 		}
 
+		if (maxsize < sizeof(struct erofs_dirent)) {
+			erofs_err("too small dir block %llu @ nid %llu, lblk %llu",
+				  maxsize | 0ULL, dir->nid | 0ULL, lblk | 0ULL);
+			return -EFSCORRUPTED;
+		}
 		nameoff = le16_to_cpu(de->nameoff);
-		if (nameoff < sizeof(struct erofs_dirent) ||
-		    nameoff >= erofs_blksiz(sbi)) {
+		if (nameoff < sizeof(struct erofs_dirent) || nameoff >= maxsize) {
 			erofs_err("invalid de[0].nameoff %u @ nid %llu, lblk %llu",
 				  nameoff, dir->nid | 0ULL, lblk | 0ULL);
 			return -EFSCORRUPTED;
lib/namei.c +6 -2
index 896e348..a4f68cb 100644
@@ -247,9 +247,13 @@ int erofs_namei(struct nameidata *nd, const char *name, unsigned int len)
 		if (ret)
 			return ret;
 
+		if (maxsize < sizeof(struct erofs_dirent)) {
+			erofs_err("too small dir block %llu @ nid %llu",
+				  maxsize | 0ULL, nid | 0ULL);
+			return -EFSCORRUPTED;
+		}
 		nameoff = le16_to_cpu(de->nameoff);
-		if (nameoff < sizeof(struct erofs_dirent) ||
-		    nameoff >= erofs_blksiz(sbi)) {
+		if (nameoff < sizeof(struct erofs_dirent) || nameoff >= maxsize) {
 			erofs_err("invalid de[0].nameoff %u @ nid %llu",
 				  nameoff, nid | 0ULL);
 			return -EFSCORRUPTED;
Signature · 1 line
2.43.0