Skip to contentSkip to conversation

erofs-utils: lib/tar: fix PAX header parsing issues

PATCH Superseded Utkal Singh 2 participants 5 messages Mar 16 Library code

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover erofs-utils: lib/tar: fix PAX header parsing issues

These two patches fix input validation bugs in the PAX extended header parser in lib/tar.c that can trigger crashes on malformed or crafted tar archives.

Patch 1 skips PAX entries with empty path= value to avoid out-of-bounds access on zero-length strings.

Patch 2 rejects negative size= values to prevent heap corruption from incorrect allocation sizes in subsequent operations.

Changes in v2:
- Fix mixed indentation in patch 2/2 (use tabs, not spaces)

Utkal Singh (2):
erofs-utils: lib/tar: skip PAX entries with empty path
erofs-utils: lib/tar: reject negative size= value in PAX header

 lib/tar.c | 7 +++++++
 1 file changed, 7 insertions(+)
Signature · 1 line
2.43.0
#1 lore raw

PATCH v2 1/2 erofs-utils: lib/tar: skip PAX entries with empty path

When a PAX extended header contains 'path=' with an empty value, the computed length becomes zero. The subsequent trailing-slash removal loop accesses eh->path[j - 1] where j is zero, resulting in an out-of-bounds read and undefined behavior.

Skip such entries to avoid unsafe pointer arithmetic and invalid filename handling.

---
 lib/tar.c | 2 ++
 1 file changed, 2 insertions(+)
lib/tar.c +2 -0
index 26461f8..be86984 100644
@@ -510,6 +510,8 @@ int tarerofs_parse_pax_header(struct erofs_iostream *ios,
 
 			if (!strncmp(kv, "path=", sizeof("path=") - 1)) {
 				int j = p - 1 - value;
+				if (!j)
+					continue;
 				free(eh->path);
 				eh->path = strdup(value);
 				while (eh->path[j - 1] == '/')
Signature · 1 line
2.43.0
#2 lore raw

PATCH v2 2/2 erofs-utils: lib/tar: reject negative size= value in PAX header

In reply to Utkal Singh

The PAX extended header size= field is parsed into a signed long long but no check is made for negative values before assigning to eh->st.st_size. A crafted PAX header with size=-1 passes the existing format check, resulting in a negative file size that can cause incorrect memory allocation and heap corruption in subsequent read or seek operations.

Add an explicit check to reject negative size= values with -EINVAL.

---
 lib/tar.c | 5 +++++
 1 file changed, 5 insertions(+)
lib/tar.c +5 -0
index be86984..6fa2cda 100644
@@ -546,6 +546,11 @@ int tarerofs_parse_pax_header(struct erofs_iostream *ios,
 					ret = -EIO;
 					goto out;
 				}
+				if (lln < 0) {
+					erofs_err("invalid negative size= in PAX header");
+					ret = -EINVAL;
+					goto out;
+				}
 				eh->st.st_size = lln;
 				eh->use_size = true;
 			} else if (!strncmp(kv, "uid=", sizeof("uid=") - 1)) {
Signature · 1 line
2.43.0
#3 lore raw
In reply to Utkal Singh
Quoted 9 lines · Utkal Singh

These two patches fix input validation bugs in the PAX extended header parser in lib/tar.c that can trigger crashes on malformed or crafted tar archives.

Patch 1 skips PAX entries with empty path= value to avoid out-of-bounds access on zero-length strings.

Patch 2 rejects negative size= values to prevent heap corruption from incorrect allocation sizes in subsequent operations.

Do you have any testcases or reproduciable tar? You can list them in the compressed-base64 format in the commit message.

#4 lore raw

Hi Gao,

Thanks for the review.

I have prepared minimal reproducible tar test cases for both issues. I will include them in the commit messages in compressed base64 format and send v3 shortly.

Thanks,
Utkal

Quoted 16 lines · Gao Xiang
Quoted 9 lines · Utkal Singh

These two patches fix input validation bugs in the PAX extended header parser in lib/tar.c that can trigger crashes on malformed or crafted tar archives.

Patch 1 skips PAX entries with empty path= value to avoid out-of-bounds access on zero-length strings.

Patch 2 rejects negative size= values to prevent heap corruption from incorrect allocation sizes in subsequent operations.

Do you have any testcases or reproduciable tar? You can list them in the compressed-base64 format in the commit message.