Skip to content

Commit 561fa34

Browse files
authored
Handle TAR uid/gid values larger than int.MaxValue (#134850)
Unix uid_t/gid_t are unsigned. Reading a GNU base-256 or PAX uid/gid above Int32.MaxValue threw OverflowException. Reinterpret such values as int without an overflow check, matching what TarWriter already does for file system ids, and write negative ids back as unsigned in the GNU header field and PAX extended attributes so they roundtrip. Fixes #127006
1 parent 107b47b commit 561fa34

7 files changed

Lines changed: 219 additions & 18 deletions

File tree

‎src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarEntry.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public int Gid
9191
set
9292
{
9393
_header._gid = value;
94-
_header.SyncNumericExtendedAttribute(TarHeader.PaxEaGid, value, TarHeader.Octal8ByteFieldMaxValue);
94+
_header.SyncNumericExtendedAttribute(TarHeader.PaxEaGid, TarHeader.UidGidAsUnsigned(value), TarHeader.Octal8ByteFieldMaxValue);
9595
}
9696
}
9797

@@ -186,7 +186,7 @@ public int Uid
186186
set
187187
{
188188
_header._uid = value;
189-
_header.SyncNumericExtendedAttribute(TarHeader.PaxEaUid, value, TarHeader.Octal8ByteFieldMaxValue);
189+
_header.SyncNumericExtendedAttribute(TarHeader.PaxEaUid, TarHeader.UidGidAsUnsigned(value), TarHeader.Octal8ByteFieldMaxValue);
190190
}
191191
}
192192

‎src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarHeader.Read.cs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,13 +231,13 @@ internal void ReplaceNormalAttributesWithExtended(IEnumerable<KeyValuePair<strin
231231
}
232232

233233
// The 'uid' header field only fits 8 bytes, or the user could've stored an override in the extended attributes
234-
if (TarHelpers.TryGetStringAsBaseTenInteger(values.Uid, out int uid))
234+
if (TarHelpers.TryGetStringAsUidGid(values.Uid, out int uid))
235235
{
236236
_uid = uid;
237237
}
238238

239239
// The 'gid' header field only fits 8 bytes, or the user could've stored an override in the extended attributes
240-
if (TarHelpers.TryGetStringAsBaseTenInteger(values.Gid, out int gid))
240+
if (TarHelpers.TryGetStringAsUidGid(values.Gid, out int gid))
241241
{
242242
_gid = gid;
243243
}
@@ -453,8 +453,8 @@ internal async ValueTask ProcessDataBlockCoreAsync<TAdapter>(Stream archiveStrea
453453
{
454454
_checksum = checksum,
455455
_size = size,
456-
_uid = TarHelpers.ParseNumeric<int>(buffer.Slice(FieldLocations.Uid, FieldLengths.Uid)),
457-
_gid = TarHelpers.ParseNumeric<int>(buffer.Slice(FieldLocations.Gid, FieldLengths.Gid)),
456+
_uid = TarHelpers.ParseUidGid(buffer.Slice(FieldLocations.Uid, FieldLengths.Uid)),
457+
_gid = TarHelpers.ParseUidGid(buffer.Slice(FieldLocations.Gid, FieldLengths.Gid)),
458458
_linkName = TarHelpers.ParseUtf8String(buffer.Slice(FieldLocations.LinkName, FieldLengths.LinkName))
459459
};
460460

‎src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarHeader.Write.cs‎

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -621,15 +621,8 @@ private int WriteCommonFields(Span<byte> buffer, TarEntryType actualEntryType)
621621
checksum += FormatNumeric(_mode, buffer.Slice(FieldLocations.Mode, FieldLengths.Mode));
622622
}
623623

624-
if (_uid >= 0)
625-
{
626-
checksum += FormatNumeric(_uid, buffer.Slice(FieldLocations.Uid, FieldLengths.Uid));
627-
}
628-
629-
if (_gid >= 0)
630-
{
631-
checksum += FormatNumeric(_gid, buffer.Slice(FieldLocations.Gid, FieldLengths.Gid));
632-
}
624+
checksum += FormatUidGid(_uid, buffer.Slice(FieldLocations.Uid, FieldLengths.Uid));
625+
checksum += FormatUidGid(_gid, buffer.Slice(FieldLocations.Gid, FieldLengths.Gid));
633626

634627
if (_size >= 0)
635628
{
@@ -1056,6 +1049,26 @@ private int FormatNumeric(int value, Span<byte> destination)
10561049
}
10571050
}
10581051

1052+
// Unix uid_t and gid_t are unsigned: a negative value represents an id larger than int.MaxValue.
1053+
// Only the GNU format can store those ids in the header field. PAX stores them in the extended attributes,
1054+
// and for the other formats the field is left empty.
1055+
private int FormatUidGid(int value, Span<byte> destination)
1056+
{
1057+
if (value >= 0)
1058+
{
1059+
return FormatNumeric(value, destination);
1060+
}
1061+
1062+
if (_format == TarEntryFormat.Gnu)
1063+
{
1064+
// Store in big endian format with leading '0x80' byte.
1065+
BinaryPrimitives.WriteInt64BigEndian(destination, (long)unchecked((uint)value) | (1L << 63));
1066+
return Checksum(destination);
1067+
}
1068+
1069+
return 0;
1070+
}
1071+
10591072
private int FormatNumeric(long value, Span<byte> destination)
10601073
{
10611074
Debug.Assert(destination.Length == 12, "12 byte field expected.");

‎src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarHeader.cs‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,10 @@ internal void SyncTimestampExtendedAttribute(string key, DateTimeOffset value)
216216
// Only updates if the format is PAX and the ExtendedAttributes dictionary has been initialized.
217217
// Uses the same logic as CollectExtendedAttributesFromStandardFieldsIfNeeded to determine
218218
// whether to add or remove the attribute.
219-
internal void SyncNumericExtendedAttribute(string key, int value, int maxNonextendedValue)
219+
// Unix uid_t and gid_t are unsigned: a negative value represents an id larger than int.MaxValue.
220+
internal static long UidGidAsUnsigned(int value) => unchecked((uint)value);
221+
222+
internal void SyncNumericExtendedAttribute(string key, long value, int maxNonextendedValue)
220223
{
221224
if (_format == TarEntryFormat.Pax && _ea is not null)
222225
{
@@ -266,8 +269,8 @@ private void AddOrUpdateStandardFieldExtendedAttributes(Dictionary<string, strin
266269
}
267270

268271
AddOrRemoveNumericField(ea, PaxEaSize, _size, Octal12ByteFieldMaxValue, removeIfUnneeded);
269-
AddOrRemoveNumericField(ea, PaxEaUid, _uid, Octal8ByteFieldMaxValue, removeIfUnneeded);
270-
AddOrRemoveNumericField(ea, PaxEaGid, _gid, Octal8ByteFieldMaxValue, removeIfUnneeded);
272+
AddOrRemoveNumericField(ea, PaxEaUid, UidGidAsUnsigned(_uid), Octal8ByteFieldMaxValue, removeIfUnneeded);
273+
AddOrRemoveNumericField(ea, PaxEaGid, UidGidAsUnsigned(_gid), Octal8ByteFieldMaxValue, removeIfUnneeded);
271274
AddOrRemoveNumericField(ea, PaxEaDevMajor, _devMajor, Octal8ByteFieldMaxValue, removeIfUnneeded);
272275
AddOrRemoveNumericField(ea, PaxEaDevMinor, _devMinor, Octal8ByteFieldMaxValue, removeIfUnneeded);
273276

‎src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarHelpers.cs‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,19 @@ internal static bool TryGetStringAsBaseTenInteger(IReadOnlyDictionary<string, st
162162
out baseTenInteger);
163163
}
164164

165+
/// <summary>Parses a uid or gid extended attribute value. See <see cref="ParseUidGid"/> for how out of range values are handled.</summary>
166+
internal static bool TryGetStringAsUidGid(string? value, out int id)
167+
{
168+
if (!string.IsNullOrEmpty(value))
169+
{
170+
id = ToUidGid(long.Parse(value, CultureInfo.InvariantCulture));
171+
return true;
172+
}
173+
174+
id = 0;
175+
return false;
176+
}
177+
165178
internal static bool TryGetStringAsBaseTenInteger(string? value, out int baseTenInteger)
166179
{
167180
if (!string.IsNullOrEmpty(value))
@@ -208,6 +221,25 @@ internal static TarEntryType GetCorrectTypeFlagForFormat(TarEntryFormat format,
208221
_ => entryType,
209222
};
210223

224+
/// <summary>Parses a uid or gid numeric field.</summary>
225+
/// <remarks>
226+
/// Unix uid_t and gid_t are 32-bit unsigned, and archives may contain values larger than <see cref="int.MaxValue"/>
227+
/// (for example, GNU base-256 encoded fields). Such values are reinterpreted as <see cref="int"/> without an
228+
/// overflow check, which matches how <see cref="TarWriter"/> stores uid and gid values read from the file system.
229+
/// Values that don't fit in 32 bits can't be a valid id and are rejected.
230+
/// </remarks>
231+
internal static int ParseUidGid(ReadOnlySpan<byte> buffer) => ToUidGid(ParseNumeric<long>(buffer));
232+
233+
private static int ToUidGid(long value)
234+
{
235+
if (value < int.MinValue || value > uint.MaxValue)
236+
{
237+
ThrowInvalidNumber();
238+
}
239+
240+
return unchecked((int)value);
241+
}
242+
211243
/// <summary>Parses a numeric field.</summary>
212244
internal static T ParseNumeric<T>(ReadOnlySpan<byte> buffer) where T : struct, INumber<T>, IBinaryInteger<T>
213245
{

‎src/libraries/System.Formats.Tar/tests/TarReader/TarReader.Tests.cs‎

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,5 +179,124 @@ public void TarReader_InvalidChecksum_ThrowsException(bool corrupted)
179179
Assert.Contains("Checksum", exception.Message);
180180
}
181181
}
182+
183+
public static IEnumerable<object[]> GnuBase256UidGidTestData()
184+
{
185+
// Leading 0x80 byte: the remaining bytes are a positive big-endian value.
186+
yield return new object[] { new byte[] { 0x80, 0, 0, 0, 0x7F, 0xFF, 0xFF, 0xFF }, int.MaxValue };
187+
yield return new object[] { new byte[] { 0x80, 0, 0, 0, 0xB6, 0x5A, 0x65, 0x38 }, unchecked((int)0xB65A6538u) };
188+
yield return new object[] { new byte[] { 0x80, 0, 0, 0, 0xFF, 0x5A, 0x65, 0x38 }, unchecked((int)0xFF5A6538u) };
189+
yield return new object[] { new byte[] { 0x80, 0, 0, 0, 0xFF, 0xFF, 0xFF, 0xFF }, -1 };
190+
// Leading 0xFF byte: the field is a negative big-endian value.
191+
yield return new object[] { new byte[] { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE }, -2 };
192+
}
193+
194+
[Theory]
195+
[MemberData(nameof(GnuBase256UidGidTestData))]
196+
public void GnuBase256UidGid_LargerThanInt32MaxValue_DoesNotThrow(byte[] fieldBytes, int expected)
197+
{
198+
byte[] tarData = CreateEntryWithRawUidGid(fieldBytes, fieldBytes);
199+
200+
using TarReader reader = new TarReader(new MemoryStream(tarData));
201+
TarEntry entry = reader.GetNextEntry();
202+
Assert.NotNull(entry);
203+
Assert.Equal(expected, entry.Uid);
204+
Assert.Equal(expected, entry.Gid);
205+
Assert.Null(reader.GetNextEntry());
206+
}
207+
208+
[Theory]
209+
[MemberData(nameof(GnuBase256UidGidTestData))]
210+
public async Task GnuBase256UidGid_LargerThanInt32MaxValue_DoesNotThrow_Async(byte[] fieldBytes, int expected)
211+
{
212+
byte[] tarData = CreateEntryWithRawUidGid(fieldBytes, fieldBytes);
213+
214+
await using TarReader reader = new TarReader(new MemoryStream(tarData));
215+
TarEntry entry = await reader.GetNextEntryAsync();
216+
Assert.NotNull(entry);
217+
Assert.Equal(expected, entry.Uid);
218+
Assert.Equal(expected, entry.Gid);
219+
Assert.Null(await reader.GetNextEntryAsync());
220+
}
221+
222+
[Theory]
223+
[InlineData(new byte[] { 0x80, 0, 0, 1, 0, 0, 0, 0 })] // uint.MaxValue + 1
224+
[InlineData(new byte[] { 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF })]
225+
[InlineData(new byte[] { 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0xFF, 0xFF, 0xFF })] // int.MinValue - 1
226+
public void GnuBase256UidGid_LargerThan32Bits_Throws(byte[] fieldBytes)
227+
{
228+
byte[] tarData = CreateEntryWithRawUidGid(fieldBytes, fieldBytes);
229+
230+
using TarReader reader = new TarReader(new MemoryStream(tarData));
231+
Assert.Throws<InvalidDataException>(() => reader.GetNextEntry());
232+
}
233+
234+
[Fact]
235+
public void PaxUidGid_LargerThan32Bits_Throws()
236+
{
237+
// Write a valid 10 digit uid, then replace it in the extended attributes with one larger than uint.MaxValue.
238+
MemoryStream stream = new MemoryStream();
239+
using (TarWriter writer = new TarWriter(stream, leaveOpen: true))
240+
{
241+
writer.WriteEntry(new PaxTarEntry(TarEntryType.Directory, "dir", new Dictionary<string, string>() { { "uid", "3059377464" } }));
242+
}
243+
244+
byte[] tarData = stream.ToArray();
245+
byte[] original = System.Text.Encoding.ASCII.GetBytes("uid=3059377464");
246+
int index = tarData.AsSpan().IndexOf(original);
247+
Assert.True(index >= 0);
248+
System.Text.Encoding.ASCII.GetBytes("uid=9999999999").CopyTo(tarData, index);
249+
250+
using TarReader reader = new TarReader(new MemoryStream(tarData));
251+
Assert.Throws<InvalidDataException>(() => reader.GetNextEntry());
252+
}
253+
254+
[Theory]
255+
[InlineData("2147483647", int.MaxValue)]
256+
[InlineData("3059377464", unchecked((int)3059377464u))]
257+
[InlineData("4294967295", -1)]
258+
public void PaxUidGid_LargerThanInt32MaxValue_DoesNotThrow(string value, int expected)
259+
{
260+
MemoryStream stream = new MemoryStream();
261+
using (TarWriter writer = new TarWriter(stream, leaveOpen: true))
262+
{
263+
writer.WriteEntry(new PaxTarEntry(TarEntryType.Directory, "dir", new Dictionary<string, string>() { { "uid", value }, { "gid", value } }));
264+
}
265+
266+
stream.Position = 0;
267+
using TarReader reader = new TarReader(stream);
268+
PaxTarEntry entry = Assert.IsType<PaxTarEntry>(reader.GetNextEntry());
269+
Assert.Equal(expected, entry.Uid);
270+
Assert.Equal(expected, entry.Gid);
271+
Assert.Equal(value, entry.ExtendedAttributes["uid"]);
272+
Assert.Equal(value, entry.ExtendedAttributes["gid"]);
273+
}
274+
275+
// Writes a GNU entry, then overwrites its uid and gid fields with the given raw bytes and fixes up the checksum.
276+
private static byte[] CreateEntryWithRawUidGid(byte[] uid, byte[] gid)
277+
{
278+
const int UidOffset = 108, GidOffset = 116, ChecksumOffset = 148, FieldLength = 8;
279+
280+
MemoryStream ms = new MemoryStream();
281+
using (TarWriter writer = new TarWriter(ms, TarEntryFormat.Gnu, leaveOpen: true))
282+
{
283+
writer.WriteEntry(new GnuTarEntry(TarEntryType.Directory, "dir"));
284+
}
285+
286+
byte[] tarData = ms.ToArray();
287+
uid.CopyTo(tarData, UidOffset);
288+
gid.CopyTo(tarData, GidOffset);
289+
290+
// The checksum is computed with the checksum field itself filled with spaces.
291+
tarData.AsSpan(ChecksumOffset, FieldLength).Fill((byte)' ');
292+
int checksum = 0;
293+
for (int i = 0; i < 512; i++)
294+
{
295+
checksum += tarData[i];
296+
}
297+
System.Text.Encoding.ASCII.GetBytes(Convert.ToString(checksum, 8).PadLeft(6, '0') + "\0 ").CopyTo(tarData, ChecksumOffset);
298+
299+
return tarData;
300+
}
182301
}
183302
}

‎src/libraries/System.Formats.Tar/tests/TarWriter/TarWriter.WriteEntry.Entry.Roundtrip.Tests.cs‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,5 +288,39 @@ public async Task PaxExtendedAttributes_DoNotOverwritePublicProperties_WhenLarge
288288
Assert.Equal(writeEntry.ModificationTime, readEntry.ModificationTime);
289289
Assert.Equal(writeEntry.LinkName, readEntry.LinkName);
290290
}
291+
292+
[Theory]
293+
[InlineData(TarEntryFormat.Gnu, int.MaxValue)]
294+
[InlineData(TarEntryFormat.Gnu, unchecked((int)0xB65A6538u))]
295+
[InlineData(TarEntryFormat.Gnu, -1)]
296+
[InlineData(TarEntryFormat.Pax, int.MaxValue)]
297+
[InlineData(TarEntryFormat.Pax, unchecked((int)0xB65A6538u))]
298+
[InlineData(TarEntryFormat.Pax, -1)]
299+
public void UidGid_LargerThanInt32MaxValue_Roundtrips(TarEntryFormat format, int id)
300+
{
301+
TarEntry writeEntry = InvokeTarEntryCreationConstructor(format, TarEntryType.Directory, "dir");
302+
writeEntry.Uid = id;
303+
writeEntry.Gid = id;
304+
305+
using MemoryStream ms = new MemoryStream();
306+
using (TarWriter writer = new TarWriter(ms, leaveOpen: true))
307+
{
308+
writer.WriteEntry(writeEntry);
309+
}
310+
311+
ms.Position = 0;
312+
using TarReader reader = new TarReader(ms);
313+
TarEntry readEntry = reader.GetNextEntry();
314+
Assert.NotNull(readEntry);
315+
Assert.Equal(id, readEntry.Uid);
316+
Assert.Equal(id, readEntry.Gid);
317+
318+
if (readEntry is PaxTarEntry paxEntry && id < 0)
319+
{
320+
string expected = unchecked((uint)id).ToString();
321+
Assert.Equal(expected, paxEntry.ExtendedAttributes["uid"]);
322+
Assert.Equal(expected, paxEntry.ExtendedAttributes["gid"]);
323+
}
324+
}
291325
}
292326
}

0 commit comments

Comments
 (0)