Skip to contentSkip to conversation

erofs-utils: tar: fix negative GNU base-256 number parsing

PATCH In review Vansh Choudhary 2 participants 6 messages Apr 5 – Apr 8 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 erofs-utils: tar: fix negative GNU base-256 number parsing

GNU base-256 fields use a 0xff prefix for negative values, but tarerofs_parsenum() currently accumulates them in signed long long. That does not sign-extend negative values correctly and can also trigger signed-overflow undefined behavior while shifting.

Handle positive and negative GNU base-256 fields separately and do the byte accumulation in unsigned long long instead.

This fixes GNU base-256 decoding for negative tar metadata values such as mtime, uid, gid and device numbers.

Fixes 95d315fd7958 ("erofs-utils: introduce tarerofs")
---
 lib/tar.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
lib/tar.c +12 -3
index 871779a..05d1a74 100644
@@ -328,17 +328,26 @@ static long long tarerofs_otoi(const char *ptr, int len)
 
 static long long tarerofs_parsenum(const char *ptr, int len)
 {
+	const u8 *p = (const u8 *)ptr;
+
 	errno = 0;
 	/*
 	 * For fields containing numbers or timestamps that are out of range
 	 * for the basic format, the GNU format uses a base-256 representation
 	 * instead of an ASCII octal number.
 	 */
-	if (*(char *)ptr == '\200' || *(char *)ptr == '\377') {
-		long long res = 0;
+	if (*(char *)ptr == '\200') {
+		unsigned long long res = 0;
 
 		while (--len)
-			res = (res << 8) | (u8)*(++ptr);
+			res = (res << 8) | *(++p);
+		return res;
+	}
+	if (*(char *)ptr == '\377') {
+		unsigned long long res = -1ULL;
+
+		while (len--)
+			res = (res << 8) | *(p++);
 		return res;
 	}
 	return tarerofs_otoi(ptr, len);
Signature · 1 line
2.43.0
#1 lore raw
Quoted 5 lines · Vansh Choudhary

GNU base-256 fields use a 0xff prefix for negative values, but tarerofs_parsenum() currently accumulates them in signed long long. That does not sign-extend negative values correctly and can also trigger signed-overflow undefined behavior while shifting.

Quoted 8 lines

Handle positive and negative GNU base-256 fields separately and do the byte accumulation in unsigned long long instead.

This fixes GNU base-256 decoding for negative tar metadata values such as mtime, uid, gid and device numbers.

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

Provide a testcase for this?

Thanks,
Gao Xiang

Quoted 38 lines
---
  lib/tar.c | 15 ++++++++++++---
  1 file changed, 12 insertions(+), 3 deletions(-)
lib/tar.c +12 -3
index 871779a..05d1a74 100644
@@ -328,17 +328,26 @@ static long long tarerofs_otoi(const char *ptr, int len)
 
  static long long tarerofs_parsenum(const char *ptr, int len)
  {
+	const u8 *p = (const u8 *)ptr;
+
  	errno = 0;
  	/*
  	 * For fields containing numbers or timestamps that are out of range
  	 * for the basic format, the GNU format uses a base-256 representation
  	 * instead of an ASCII octal number.
  	 */
-	if (*(char *)ptr == '\200' || *(char *)ptr == '\377') {
-		long long res = 0;
+	if (*(char *)ptr == '\200') {
+		unsigned long long res = 0;
 
  		while (--len)
-			res = (res << 8) | (u8)*(++ptr);
+			res = (res << 8) | *(++p);
+		return res;
+	}
+	if (*(char *)ptr == '\377') {
+		unsigned long long res = -1ULL;
+
+		while (len--)
+			res = (res << 8) | *(p++);
  		return res;
  	}
  	return tarerofs_otoi(ptr, len);
#2 lore raw

PATCH 1/1 erofs-utils: tests: add test for negative GNU tar mtimes

Add a regression test for negative GNU tar mtimes.

It creates a tarball with a file whose mtime is -1, checks that the mtime field is encoded in GNU base-256 format, and verifies that the timestamp is preserved after building and extracting the image.

---
 tests/Makefile.am   |  3 +++
 tests/erofs/030     | 57 +++++++++++++++++++++++++++++++++++++++++++++
 tests/erofs/030.out |  2 ++
 3 files changed, 62 insertions(+)
 create mode 100755 tests/erofs/030
 create mode 100644 tests/erofs/030.out
tests/Makefile.am +3 -0
index d8ac067..28edc0d 100644
@@ -126,6 +126,9 @@ TESTS += erofs/028
 # 029 - test FUSE daemon and kernel error handling on corrupted inodes
 TESTS += erofs/029
 
+# 030 - regression test for negative GNU tar mtimes
+TESTS += erofs/030
+
 # NEW TEST CASE HERE
 # TESTS += erofs/999
 
tests/erofs/030 new file +57 -0

57 lines not on this page · raw message

tests/erofs/030.out new file +2 -0
new file mode 100644
index 0000000..06a1c8f
@@ -0,0 +1,2 @@
+QA output created by 030
+Silence is golden
Signature · 1 line
2.43.0
#3 lore raw

PATCH v2 1/1 erofs-utils: tests: add test for negative GNU tar mtimes

From: Vansh Choudhary <[email protected]>

Add a regression test for negative GNU tar mtimes.

It creates a tarball with a file whose mtime is -1, checks that the mtime field is encoded in GNU base-256 format, and verifies that the timestamp is preserved after building and extracting the image.

---
v2:
- use dump.erofs instead of fsck.erofs;
- switch to MIT license.

 tests/Makefile.am   |  3 +++
 tests/erofs/030     | 55 +++++++++++++++++++++++++++++++++++++++++++++
 tests/erofs/030.out |  2 ++
 3 files changed, 60 insertions(+)
 create mode 100755 tests/erofs/030
 create mode 100644 tests/erofs/030.out
tests/Makefile.am +3 -0
index d8ac067805e8..28edc0d744d1 100644
@@ -126,6 +126,9 @@ TESTS += erofs/028
 # 029 - test FUSE daemon and kernel error handling on corrupted inodes
 TESTS += erofs/029
 
+# 030 - regression test for negative GNU tar mtimes
+TESTS += erofs/030
+
 # NEW TEST CASE HERE
 # TESTS += erofs/999
 
tests/erofs/030 new file +55 -0

55 lines not on this page · raw message

tests/erofs/030.out new file +2 -0
new file mode 100644
index 000000000000..06a1c8fe02bb
@@ -0,0 +1,2 @@
+QA output created by 030
+Silence is golden
Signature · 1 line
2.43.5
#4 lore raw
In reply to Vansh Choudhary
Quoted 5 lines · Vansh Choudhary

GNU base-256 fields use a 0xff prefix for negative values, but tarerofs_parsenum() currently accumulates them in signed long long. That does not sign-extend negative values correctly and can also trigger signed-overflow undefined behavior while shifting.

Quoted 8 lines

Handle positive and negative GNU base-256 fields separately and do the byte accumulation in unsigned long long instead.

This fixes GNU base-256 decoding for negative tar metadata values such as mtime, uid, gid and device numbers.

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

I ran the test but it succeeds, so is it just a UB or it really impacts end users?

Thanks,
Gao Xiang

Quoted 38 lines
---
  lib/tar.c | 15 ++++++++++++---
  1 file changed, 12 insertions(+), 3 deletions(-)
lib/tar.c +12 -3
index 871779a..05d1a74 100644
@@ -328,17 +328,26 @@ static long long tarerofs_otoi(const char *ptr, int len)
 
  static long long tarerofs_parsenum(const char *ptr, int len)
  {
+	const u8 *p = (const u8 *)ptr;
+
  	errno = 0;
  	/*
  	 * For fields containing numbers or timestamps that are out of range
  	 * for the basic format, the GNU format uses a base-256 representation
  	 * instead of an ASCII octal number.
  	 */
-	if (*(char *)ptr == '\200' || *(char *)ptr == '\377') {
-		long long res = 0;
+	if (*(char *)ptr == '\200') {
+		unsigned long long res = 0;
 
  		while (--len)
-			res = (res << 8) | (u8)*(++ptr);
+			res = (res << 8) | *(++p);
+		return res;
+	}
+	if (*(char *)ptr == '\377') {
+		unsigned long long res = -1ULL;
+
+		while (len--)
+			res = (res << 8) | *(p++);
  		return res;
  	}
  	return tarerofs_otoi(ptr, len);
#5 lore raw

Yes, this definitely fixes the signed-overflow UB in tarerofs_parsenum(), and also makes the negative GNU base-256 decoding logic correct. But from the current testcase alone, I cannot firmly show a visible end-user failure on the old code path.

So I think this is safest to describe as a correctness + UB fix.

Thanks,
Vansh