Skip to contentSkip to conversation

tar: fix parsing issues for pax and GNU extensions

PATCH In review Zhan Xusheng 2 participants 7 messages Apr 14 Library code

lore
Share & tools
lore
#0 lore raw

cover tar: fix parsing issues for pax and GNU extensions

This series fixes two issues in tar parsing:

- An out-of-bounds access when trimming PAX path entries
- Missing NULL pointer checks when handling GNU long name/link records

These issues can be triggered by malformed tar archives and may lead to crashes. The fixes improve robustness when processing untrusted inputs.

Zhan Xusheng (2):
erofs-utils: tar: fix out-of-bounds access when trimming pax path
erofs-utils: tar: add missing NULL checks for GNU long name/link

 lib/tar.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/2 erofs-utils: tar: fix out-of-bounds access when trimming pax path

When a PAX extended header contains a path consisting entirely of '/' characters (e.g., "path=/"), the trailing-slash trimming loop in tarerofs_parse_pax_header() decrements j to 0, then accesses eh->path[-1] which is an out-of-bounds heap read.

The tar header path trimming had a similar issue fixed by commit dcd06f421003 ("erofs-utils: mkfs: tar: fix SIGSEGV on / entry"), but the PAX header path trimming was not addressed.

Add a j > 0 guard to the while condition.

Fixes 95d315fd7958 ("erofs-utils: introduce tarerofs")
---
 lib/tar.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/tar.c +1 -1
index eca29f5..3d92f48 100644
@@ -509,7 +509,7 @@ int tarerofs_parse_pax_header(struct erofs_iostream *ios,
 				int j = p - 1 - value;
 				free(eh->path);
 				eh->path = strdup(value);
-				while (eh->path[j - 1] == '/')
+				while (j > 0 && eh->path[j - 1] == '/')
 					eh->path[--j] = '\0';
 			} else if (!strncmp(kv, "linkpath=",
 					sizeof("linkpath=") - 1)) {
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH 1/2] erofs-utils: tar: fix out-of-bounds access when trimming pax path

Hi,

Quoted 5 lines · Zhan Xusheng

When a PAX extended header contains a path consisting entirely of '/' characters (e.g., "path=/"), the trailing-slash trimming loop in tarerofs_parse_pax_header() decrements j to 0, then accesses eh->path[-1] which is an out-of-bounds heap read.

Quoted 8 lines

The tar header path trimming had a similar issue fixed by commit dcd06f421003 ("erofs-utils: mkfs: tar: fix SIGSEGV on / entry"), but the PAX header path trimming was not addressed.

Add a j > 0 guard to the while condition.

Fixes 95d315fd7958 ("erofs-utils: introduce tarerofs")

That was addressed by others before: I think I will add your Signed-off-by: into the original patch.

BTW, are you using LLM to discover bugs too?

Thanks,
Gao Xiang

Quoted 17 lines
---
  lib/tar.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
lib/tar.c +1 -1
index eca29f5..3d92f48 100644
@@ -509,7 +509,7 @@ int tarerofs_parse_pax_header(struct erofs_iostream *ios,
  				int j = p - 1 - value;
  				free(eh->path);
  				eh->path = strdup(value);
-				while (eh->path[j - 1] == '/')
+				while (j > 0 && eh->path[j - 1] == '/')
  					eh->path[--j] = '\0';
  			} else if (!strncmp(kv, "linkpath=",
  					sizeof("linkpath=") - 1)) {
#3 lore raw

Hi Gao,

Thanks!

The issue was identified during manual code review. I occasionally use LLMs to help polish commit messages or double-check wording.

Thanks for adding my Signed-off-by.

Best regards,
Zhan Xusheng

#4 lore raw

Hi Zhan,

Quoted 8 lines · Zhan Xusheng

Hi Gao,

Thanks!

The issue was identified during manual code review. I occasionally use LLMs to help polish commit messages or double-check wording.

Thanks for adding my Signed-off-by.

For example, this:
https://lore.kernel.org/linux-erofs/[email protected]

I've lost my similiar commits like this,
but I will address them all soon.

Thanks,
Gao Xiang

Quoted 3 lines

Best regards,
Zhan Xusheng

#5 lore raw

Hi Gao,

Thanks for the reference!

I see, that looks like a similar issue. Please feel free to take over and address them together.

Let me know if I can help with anything.

Best regards,
Zhan Xusheng

#6 lore raw

PATCH 2/2 erofs-utils: tar: add missing NULL checks for GNU long name/link

In reply to Zhan Xusheng

In the GNU long name ('L') and long link ('K') handling, malloc() return values are not checked. If st.st_size is excessively large from a crafted tar header, malloc() fails and returns NULL, then erofs_iostream_bread() writes to a NULL pointer causing a crash.

Also add the missing PATH_MAX bound check for 'L' entries, which the 'K' path already had.

Fixes 95d315fd7958 ("erofs-utils: introduce tarerofs")
---
 lib/tar.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
lib/tar.c +5 -3
index 3d92f48..24d8314 100644
@@ -886,7 +886,8 @@ out_eot:
 	case 'L':
 		free(eh.path);
 		eh.path = malloc(st.st_size + 1);
-		if (st.st_size != erofs_iostream_bread(&tar->ios, eh.path,
+		if (!eh.path || st.st_size > PATH_MAX ||
+		    st.st_size != erofs_iostream_bread(&tar->ios, eh.path,
 						       st.st_size))
 			goto invalid_tar;
 		eh.path[st.st_size] = '\0';
@@ -894,8 +895,9 @@ out_eot:
 	case 'K':
 		free(eh.link);
 		eh.link = malloc(st.st_size + 1);
-		if (st.st_size > PATH_MAX || st.st_size !=
-		    erofs_iostream_bread(&tar->ios, eh.link, st.st_size))
+		if (!eh.link || st.st_size > PATH_MAX ||
+		    st.st_size != erofs_iostream_bread(&tar->ios, eh.link,
+						       st.st_size))
 			goto invalid_tar;
 		eh.link[st.st_size] = '\0';
 		goto restart;
Signature · 1 line
2.43.0