Repository navigation
Description
Description
TarReader.GetNextEntry documents InvalidDataException for malformed archives and IOException for I/O problems. Two malformed numeric header fields escape that contract instead, each from a different place:
-
OverflowExceptionfromTarHelpers.ParseNumeric<T>. For the GNU base-256 representation (lead byte0x80/0xff) the value goes straight toT.ReadBigEndian, which throwsOverflowExceptionwhen the field is wider thanT. The octal path right next to it already funnels bad input intoInvalidDataExceptionviaThrowInvalidNumber(), so the two representations disagree about what a bad number is. -
ArgumentOutOfRangeExceptionfromTarHeader.ProcessDataBlock. Asizefield that overflows reachesStream.Positionunvalidated, andMemoryStreamrejects it with "Stream length must be non-negative and less than 2^31 - 1 - origin."
Both are reachable from a 1536-byte archive with a correct header checksum, so an application that reads untrusted tar data and catches the documented exceptions still gets an unexpected throw. GNU tar rejects both archives.
Reproduction Steps
using System.Formats.Tar;
using System.Text;
// Builds a single-entry tar archive with a valid checksum and one patched numeric field.
static byte[] Archive(int offset, byte[] value)
{
byte[] tar = new byte[1536]; // header + two zero blocks (end of archive)
void Write(int off, string s) => Encoding.ASCII.GetBytes(s).CopyTo(tar, off);
Write(0, "f.txt"); // name
Write(100, "0000644\0"); // mode
Write(108, "0000000\0"); // uid
Write(116, "0000000\0"); // gid
Write(124, "00000000000\0"); // size
Write(136, "15270727363\0"); // mtime
tar[156] = (byte)'0'; // typeflag: regular file
Write(257, "ustar\0" + "00"); // magic + version
value.CopyTo(tar, offset); // the field under test
for (int i = 148; i < 156; i++) tar[i] = (byte)' '; // checksum field counts as spaces
int sum = 0;
for (int i = 0; i < 512; i++) sum += tar[i];
Write(148, Convert.ToString(sum, 8).PadLeft(6, '0') + "\0 ");
return tar;
}
static void Read(string label, byte[] tar)
{
try
{
using var stream = new MemoryStream(tar);
using var reader = new TarReader(stream);
while (reader.GetNextEntry() is TarEntry entry) { _ = entry.Name; _ = entry.ModificationTime; }
Console.WriteLine($"{label}: no exception");
}
catch (Exception ex)
{
bool documented = ex is InvalidDataException or IOException;
Console.WriteLine($"{label}: {ex.GetType().Name}{(documented ? "" : " <-- not documented")}");
Console.WriteLine($" {ex.Message}");
Console.WriteLine($" {ex.StackTrace.Split('\n')[0].Trim()}");
}
}
Console.WriteLine(System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription);
Console.WriteLine();
// 1. mtime (offset 136), GNU base-256 with the 0x80 lead byte: 11 payload bytes of 0xff.
Read("mtime, GNU base-256", Archive(136, new byte[] { 0x80, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }));
// 2. size (offset 124), octal digits that exceed the field's range.
Read("size, octal overflow", Archive(124, Encoding.ASCII.GetBytes("777777777777")));
// 3. control: both fields well-formed.
Read("control", Archive(136, Encoding.ASCII.GetBytes("15270727363\0")));Expected behavior
InvalidDataException, which is what TarReader.GetNextEntry documents for a malformed archive and what the octal path already throws for a digit outside 0-7.
Actual behavior
.NET 10.0.14
mtime, GNU base-256: OverflowException <-- not documented
Arithmetic operation resulted in an overflow.
at System.Numerics.IBinaryInteger`1.ReadBigEndian(ReadOnlySpan`1 source, Boolean isUnsigned)
size, octal overflow: ArgumentOutOfRangeException <-- not documented
Stream length must be non-negative and less than 2^31 - 1 - origin. (Parameter 'value')
at System.IO.MemoryStream.set_Position(Int64 value)
control: no exception
Full stacks:
System.OverflowException: Arithmetic operation resulted in an overflow.
at System.Numerics.IBinaryInteger`1.ReadBigEndian(ReadOnlySpan`1 source, Boolean isUnsigned)
at System.Formats.Tar.TarHelpers.ParseNumeric[T](ReadOnlySpan`1 buffer)
at System.Formats.Tar.TarHeader.ParseAsTimestamp(ReadOnlySpan`1 buffer)
at System.Formats.Tar.TarHeader.TryReadCommonAttributes(ReadOnlySpan`1 buffer, TarEntryFormat initialFormat)
System.ArgumentOutOfRangeException: Stream length must be non-negative and less than 2^31 - 1 - origin. (Parameter 'value')
at System.IO.MemoryStream.set_Position(Int64 value)
at System.Formats.Tar.TarHeader.ProcessDataBlock(Stream archiveStream, Boolean copyData)
at System.Formats.Tar.TarReader.TryGetNextEntryHeader(Boolean copyData)
Regression?
No. The parsing code is unchanged on main, and I reproduced the same behavior on the current 10.0 servicing build.
Known Workarounds
Catching Exception rather than the documented InvalidDataException/IOException around GetNextEntry.
Configuration
.NET 10.0.14, Linux x64. Not specific to this configuration: the code paths are platform independent. ParseNumeric is reached from mtime, size, mode, uid, gid, devmajor, devminor, atime and ctime.
Other information
ParseUidGid was given an unchecked conversion in #134850, which covered uid/gid only; ParseNumeric itself still propagates the overflow for the other fields.
Both cases were found by the TarReader fuzzer I am contributing for #136371, within seconds. For calibration, 7000 mutated inputs (random bytes, bit flips over a valid archive, and truncations) produced only InvalidDataException and EndOfStreamException, so these two are the only escapes.
Worth noting for anyone reproducing: TarReader does not verify the header checksum, so 487 of 500 single-bit flips over a valid archive still parse successfully. The archives above keep a correct checksum anyway so the repro does not depend on that.
Activity
Add a TarReader fuzzer
dotnet-policy-service commentedon Oct 29, 2026
Tagging subscribers to this area: @dotnet/area-system-formats-tar
See info in area-owners.md if you want to be subscribed.
rzikm commentedon Nov 5, 2026
Triage: we should make sure the exceptions we throw are consistent and update the documentation if necessary.
earbona23 commentedon Nov 5, 2026
Thanks for the triage. On "make sure the exceptions we throw are consistent": there is a second, independent path that throws the same undocumented
OverflowException, and a fix confined toTarHelpers.ParseNumeric<T>would not cover it. Posting it here so the consistency pass covers both.The second path: PAX extended attribute values, parsed by
TarHeader.ReplaceNormalAttributesWithExtended. The original report covers the binary header field (GNU base-256); this one is the text of thelen key=value\nrecords in the data block of anx-typeflag header.Measured on
.NET 10.0.14(System.Formats.Tar.dll,FileVer 10.0.1426.52326), from bothTarReader.GetNextEntryandTarFile.ExtractToDirectory:size=777777777777777777773TryGetStringAsBaseTenLong->long.ParseOverflowException(Int64)mtime=17930586772622498093TryGetDateTimeOffsetFromTimestampString->(long)(value * TicksPerSecond)OverflowException(Int64)mode/devmajor/devminor=4294967296TryGetStringAsBaseTenInteger->int.ParseOverflowException(Int32)uid/gid=4294967296TryGetStringAsBaseTenInteger->int.ParseOverflowException(Int32)Two notes that matter for the fix:
mtimecase is the least obvious one.decimal.TryParsesucceeds —decimalholds the value — and the overflow happens afterwards, in the(long)conversion insideGetDateTimeOffsetFromSecondsSinceEpoch(decimal). TheTryParsevalidates that the input is a number, not that it fits.uid/gidalready behave differently inmain, where they go through the newerTryGetStringAsUidGidand out-of-range values are clamped rather than thrown. The other five fields still calllong.Parse/int.Parsedirectly inmain, so the inconsistency is currently inside the PAX path as well as between the two paths.For reference on the expected behaviour, GNU
tarrejects these archives with an explicit range diagnostic —tar: Extended header size=... is out of range 0..9223372036854775807— so both the field name and the limit survive into the error message.Minimal repro is a 2560-byte archive: one
xheader plus a single PAX record. Happy to attach the generator or a.tarfor any of the rows above if that is useful for the fix or for a test case.Throw InvalidDataException for malformed tar numeric header fields (d…