|
3 | 3 | // See the license.txt file in the project root for more information. |
4 | 4 |
|
5 | 5 | using System; |
| 6 | +using System.Buffers.Binary; |
6 | 7 | using System.Collections.Generic; |
7 | 8 | using System.IO; |
8 | 9 | using System.Linq; |
@@ -497,6 +498,13 @@ public void ReadsAndWritesAUniversalBinary() |
497 | 498 | tooFar.Slices[1].FileOffset = 0x1_0000_0000; |
498 | 499 | tooFar.Slices[1].Size = 0x10; |
499 | 500 | Assert.IsTrue(tooFar.Verify().Messages.Any(m => m.Id == DiagnosticId.MACHO_ERR_ValueTooLargeFor32Bit)); |
| 501 | + |
| 502 | + // The table is what the loader uses to select a slice, so it has to describe the image |
| 503 | + // that will actually be found there. |
| 504 | + var mismatched = MachOFatFile.Read(new MemoryStream(original)); |
| 505 | + mismatched.Slices[0].CpuType = MachOCpuType.X86; |
| 506 | + Assert.IsFalse(mismatched.TryWrite(new MemoryStream(), out var mismatchDiagnostics)); |
| 507 | + Assert.IsTrue(mismatchDiagnostics.Messages.Any(m => m.Id == DiagnosticId.MACHO_ERR_FatSliceArchitectureMismatch), string.Join("; ", mismatchDiagnostics.Messages)); |
500 | 508 | } |
501 | 509 |
|
502 | 510 | /// <summary> |
@@ -646,5 +654,35 @@ public void ReportsRatherThanThrowsOnUnreadableInput() |
646 | 654 | BitConverter.GetBytes(0u).Reverse().ToArray().CopyTo(fat, MachOFatFile.HeaderSize + 12); |
647 | 655 | Assert.IsFalse(MachOFatFile.TryRead(new MemoryStream(fat), out _, out var emptySlice)); |
648 | 656 | Assert.IsTrue(emptySlice.Messages.Any(m => m.Id == DiagnosticId.MACHO_ERR_InvalidFatArchRange), string.Join("; ", emptySlice.Messages)); |
| 657 | + |
| 658 | + // FAT64 offsets and sizes are unsigned 64-bit fields. An end that wraps must be rejected |
| 659 | + // before either value is narrowed for SubStream. |
| 660 | + var wrappingFatRange = new byte[MachOFatFile.HeaderSize + MachOFatFile.GetSliceEntrySize(true)]; |
| 661 | + BinaryPrimitives.WriteUInt32BigEndian(wrappingFatRange, MachOMagic.FatMagic64); |
| 662 | + BinaryPrimitives.WriteUInt32BigEndian(wrappingFatRange.AsSpan(4), 1); |
| 663 | + BinaryPrimitives.WriteUInt64BigEndian(wrappingFatRange.AsSpan(MachOFatFile.HeaderSize + 8), ulong.MaxValue - 15); |
| 664 | + BinaryPrimitives.WriteUInt64BigEndian(wrappingFatRange.AsSpan(MachOFatFile.HeaderSize + 16), MachOFile.MinHeaderSize); |
| 665 | + Assert.IsFalse(MachOFatFile.TryRead(new MemoryStream(wrappingFatRange), out _, out var wrappingFatSlice)); |
| 666 | + Assert.IsTrue(wrappingFatSlice.Messages.Any(m => m.Id == DiagnosticId.MACHO_ERR_InvalidFatArchRange), string.Join("; ", wrappingFatSlice.Messages)); |
| 667 | + |
| 668 | + // The same overflow is possible in a 64-bit section size. Build the smallest image with |
| 669 | + // one LC_SEGMENT_64 and one section whose declared end wraps back into the file. |
| 670 | + const int machHeaderSize = 32; |
| 671 | + const int segmentCommandSize = 72; |
| 672 | + const int sectionSize = 80; |
| 673 | + var wrappingSectionRange = new byte[machHeaderSize + segmentCommandSize + sectionSize]; |
| 674 | + BinaryPrimitives.WriteUInt32LittleEndian(wrappingSectionRange, MachOMagic.Magic64); |
| 675 | + BinaryPrimitives.WriteUInt32LittleEndian(wrappingSectionRange.AsSpan(4), (uint)MachOCpuType.X86_64); |
| 676 | + BinaryPrimitives.WriteUInt32LittleEndian(wrappingSectionRange.AsSpan(12), (uint)MachOFileType.Object); |
| 677 | + BinaryPrimitives.WriteUInt32LittleEndian(wrappingSectionRange.AsSpan(16), 1); |
| 678 | + BinaryPrimitives.WriteUInt32LittleEndian(wrappingSectionRange.AsSpan(20), segmentCommandSize + sectionSize); |
| 679 | + BinaryPrimitives.WriteUInt32LittleEndian(wrappingSectionRange.AsSpan(machHeaderSize), (uint)MachOLoadCommandType.Segment64); |
| 680 | + BinaryPrimitives.WriteUInt32LittleEndian(wrappingSectionRange.AsSpan(machHeaderSize + 4), segmentCommandSize + sectionSize); |
| 681 | + BinaryPrimitives.WriteUInt32LittleEndian(wrappingSectionRange.AsSpan(machHeaderSize + 64), 1); |
| 682 | + var sectionOffset = machHeaderSize + segmentCommandSize; |
| 683 | + BinaryPrimitives.WriteUInt64LittleEndian(wrappingSectionRange.AsSpan(sectionOffset + 40), unchecked(ulong.MaxValue - (ulong)wrappingSectionRange.Length + 11)); |
| 684 | + BinaryPrimitives.WriteUInt32LittleEndian(wrappingSectionRange.AsSpan(sectionOffset + 48), (uint)wrappingSectionRange.Length); |
| 685 | + Assert.IsFalse(MachOFile.TryRead(new MemoryStream(wrappingSectionRange), out _, out var wrappingSection)); |
| 686 | + Assert.IsTrue(wrappingSection.Messages.Any(m => m.Id == DiagnosticId.MACHO_ERR_InvalidContentFileRange), string.Join("; ", wrappingSection.Messages)); |
649 | 687 | } |
650 | 688 | } |
0 commit comments