Repository navigation
Description
Description
I have a *.tar.gz file that I am trying to process in C# by passing the GZipStream to TarReader.
This fails at the line
TarHelpers.ParseOctal(buffer.Slice(124, 12))
The
if (num >= 8)
check inside ParseOctal is getting to the ThrowInvalidNumber part as the num being tested is 80.
I have seen a similar issue where non conformance to the spec is blamed for this type of thing.
But Azure Data Factory can process this *.tar.gz fine and additionally Windows Explorer seems able to understand the contents of the *.tar.gz file - showing it as a compressed archive - clicking through shows the details of the single file contained within.
The complete 512 bytes of the header is
6E625F726D73655F727061735F696E76656E746F72792E64617400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003030303036363600303135323036310030313532303631008000000000000011E7310C9D3134353134303736373333003031363332350020300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000075737461722020006F7261636C6500000000000000000000000000000000000000000000000000006F696E7374616C6C000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
So the problematic part is
8000000000000011E7310C9D
The underlying file size is 76893195421 bytes.
Reproduction Steps
Not practical for me to share the actual file but it consists of the 512 byte header already given.
Followed by 76893195421 bytes of arbitrary data that is the actual file contents
Followed by a new line and 8547 null bytes.
You can use
var header = Convert.FromHexString(
"6E625F726D73655F727061735F696E76656E746F72792E64617400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003030303036363600303135323036310030313532303631008000000000000011E7310C9D3134353134303736373333003031363332350020300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000075737461722020006F7261636C6500000000000000000000000000000000000000000000000000006F696E7374616C6C000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000");
System.IO.File.WriteAllBytes(@"C:\somefile.tar", header
);
And then open the archive in 7zip and see that this utility can cope with it as one example
Expected behavior
These types of file have been generated by a system in my company for years.
Every utility used to read them has apparently had no issues with them except for the new .NET framework classes.
I have no idea whether or not this file format does in fact violate any spec but I would expect to be able to open it as other utilities can cope (maybe by setting a mode parameter to ignore such issues as long as they aren't fatal to the extraction)
Actual behavior
System.IO.InvalidDataException: 'Unable to parse number.' exception thrown on initial tarReader.GetNextEntry() call
Regression?
No response
Known Workarounds
No response
Configuration
No response
Other information
No response

Activity
danmoseley commentedon Oct 20, 2023
The magic number in this data is "ustar " (with 2 spaces) which is apparently OLDGNU_MAGIC. 7z reports
Characteristics: 0 GNU ASCII bin_psize bin_sizeThe size (12 bytes at offset 124) is 8000000000000011E7310C9D and it is failing on the first byte.
The code expects this to be octal coded as chars (ie., 0=0x30 through 7=0x37) which it isn't.
Instead the high bit of the first byte is set, then the remaining is base 256. 0x11 0xE7 0x31 0x0C 0x9D is 76 893 195 421 which is what 7z reports.
As to why the high bit is set, I cannot find mention in the GNU tar spec, but Wikipedia says "2001 star introduced a base-256 coding that is indicated by setting the high-order bit of the leftmost byte of a numeric field.[citation needed] GNU-tar and BSD-tar followed this idea"
I think the tar reader just can't handle the format used for archives larger than 8GB?
This isn't my area, deferring to @carlossanlop
danmoseley commentedon Oct 20, 2023
Ah here's another doc mentioning the high bit.
https://manpages.debian.org/testing/libarchive-dev/tar.5.en.html
danmoseley commentedon Oct 20, 2023
Ah it seems we support the PAX extended attributes including "size". If present (it's not here) then we overwrite the size from the header. So I guess we support > 8GB so long as it's PAX with the "size=.." extended attribute AND the size in the header is (any) valid octal chars.
danmoseley commentedon Oct 20, 2023
A minimal fix here may be to just recognize this form of size encoding, but it seems that any(?) numeric field may use this encoding so perhaps that should be recognized using the high bit.
danmoseley commentedon Oct 27, 2023
@carlossanlop do you have a preferred fix? perhaps we can make this help wanted.
just recognize this encoding for the header's size field only?
carlossanlop commentedon Oct 27, 2023
Hi -
In the main TAR spec that we used to implement the .NET version, there's this section:
I originally interpreted the use of the word "attempts" as "unsuccessful additions to the spec". But I see that other tools implemented it anyway.
Yes, @danmoseley, I think we could take the change for the size field for now, as it is the only field that we've got a report that it is causing issues.
danmoseley commentedon Oct 31, 2023
@carlossanlop Do you have a preference?
danmoseley commentedon Oct 31, 2023
Oops disregard, I missed your reply somehow
danmoseley commentedon Oct 31, 2023
@asos-martinsmith any interest in offering a PR as described?
ilabutin commentedon Nov 24, 2023
@danmoseley
I would be happy to try to contribute the fix for this issue.
After looking through the code, I have few questions.
TryReadCommonAttributes:I would introduce one more method like
TarHelpers.ParseNumeric<T>(...)that would check the highest bit of the first byte and do binary parsing or fallback toParseOctal<T>(...).Does this sound like a correct solution?
The maximum possible size encoded in a binary form can be up to 2^95, which is way more than
long sizecan fit. Would it be correct to only accept size up to 2^63 (to fitlong) and throw if larger size was read?What would be the best way to deal with the following line?
The
TarHelpers.MaxSizeLengthis defined as:So, if we accept larger sizes, this assert would not be valid. Should
MaxSizeLengthfield be increased or is there a better solution?asos-martinsmith commentedon Dec 15, 2023
I'm a data engineer rather than a .NET developer so will have to pass on that.
In the meantime I have switched to using SharpCompress to get over this. Thanks
danmoseley commentedon Dec 15, 2023
@ilabutin apologies I missed your message. I don't own this code. @carlossanlop can give pointers so you can get started.
ilabutin commentedon Dec 15, 2023
@danmoseley No worries, I'll be waiting for @carlossanlop to react.
@carlossanlop please take a look at my thoughts above (#93763 (comment)) to verify I'm thinking about steps into the right direction.
ivanjx commentedon Dec 21, 2023
my tar also has 0x80 in its size header so it seems that checking it and just treat the header as big endian long is fine. i have written a simple tar listing function myself here #96209. if my understanding is correct then the MaxSizeLength should be long.MaxValue (or ulong).
tmds commentedon Mar 25, 2024
I ran into this issue while trying to extract a tar file that has a gid of 1001080000 which is stored as
80 00 00 00 3b ab 44 c0in the tar file.The fix should be applied to all numeric fields and timestamps.
https://www.gnu.org/software/tar/manual/html_section/Extensions.html has this description:
@ivanjx are you still looking to make a PR with a fix?
Something like that. You could rename
ParseOctaltoParseNumericand add an argument like:octalOnly. Only the call for reading the checksum should set limit tooctalOnly. The others should accept the base-256 range.Throwing seems appropriate. The exception type can be decided on the PR.
Yes, it should be increased.
Probably, we should also do some work on the writing side.
tmds commentedon Apr 16, 2024
@ivanjx do you still plan to work on a fix for this? If not, I can pick it up.
ivanjx commentedon Apr 16, 2024
hi @tmds, sorry for the late response. please feel free to make a pr as i am not really used to dotnet code. i just provided some sample code and possible workaround above.