Skip to contentSkip to conversation

erofs-utils: tar: fix overly large size in tar header

PATCH New Gao Xiang 1 participant 2 messages Jul 16 – Jul 17 Library code

lore
Share & tools
lore

Gao Xiang

author
#0 lore raw

PATCH 1/1 erofs-utils: tar: fix overly large size in tar header

As @oscarjhk reported:

When tar-index mode is used, the inode chunk count will be calculated as 0 if a crafted GNU base-256 size is UINT64_MAX, which will cause a heap out-of-bounds write.

Fixes 95d315fd7958 ("erofs-utils: introduce tarerofs")
---
 lib/blobchunk.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
lib/blobchunk.c +3 -1
index 0523873570a2..5e0fb70be687 100644
@@ -507,7 +507,9 @@ int tarerofs_write_chunkes(struct erofs_inode *inode, erofs_off_t data_offset)
 		datablob_size += round_up(inode->i_size, erofs_blksiz(sbi));
 	}
 	chunksize = 1ULL << chunkbits;
-	count = DIV_ROUND_UP(inode->i_size, chunksize);
+	/* e.g. DIV_ROUND_UP(UINT64_MAX, 1 TiB) can overflow */
+	count = (inode->i_size >> chunkbits) +
+		!!(inode->i_size & (chunksize - 1));
 
 	inode->extent_isize = count * unit;
 	idx = calloc(count, max(sizeof(*idx), sizeof(void *)));
Signature · 1 line
2.43.5
author
#1 lore raw

PATCH 1/1 erofs-utils: tar: refuse negative size in tar header

As @oscarjhk reported:

When tar-index mode is used, the inode chunk count will be calculated as 0 if a crafted GNU base-256 size is UINT64_MAX, which will cause a heap out-of-bounds write.

Fixes 95d315fd7958 ("erofs-utils: introduce tarerofs")
---
 lib/tar.c | 7 +++++++
 1 file changed, 7 insertions(+)
lib/tar.c +7 -0
index 242f27ae49fd..cf60b02daf2b 100644
@@ -886,6 +886,13 @@ out_eot:
 			goto invalid_tar;
 	}
 
+	if ((s64)st.st_size < 0) {
+		erofs_err("invalid negative size=%lld @ %lld",
+			  (s64)st.st_size, tar_offset);
+		ret = -EFSCORRUPTED;
+		goto out;
+	}
+
 	if (th->typeflag <= '7' && !eh.path) {
 		eh.path = path;
 		j = 0;
Signature · 1 line
2.43.5