|
4 | 4 |
|
5 | 5 | using System; |
6 | 6 | using System.IO; |
| 7 | +using System.Collections.Generic; |
7 | 8 | using System.Linq; |
8 | 9 | using LibObjectFile.MachO.CodeSign; |
9 | 10 | using LibObjectFile.Utils; |
@@ -41,72 +42,121 @@ public void AdHocSign(string identifier) |
41 | 42 | var text = FindSegment("__TEXT") |
42 | 43 | ?? throw new InvalidOperationException("The image has no __TEXT segment, so the signature cannot describe its executable range."); |
43 | 44 |
|
| 45 | + // Everything below can fail, and a half-signed image is worse than an unsigned one, so |
| 46 | + // what changes is remembered first and put back if it does. The state is captured before |
| 47 | + // the command is added, since adding one is itself a change to undo. |
| 48 | + var previousFileSize = linkEdit.FileSize; |
| 49 | + var previousVmSize = linkEdit.VmSize; |
| 50 | + var previousStale = IsCodeSignatureStale; |
| 51 | + var added = new List<MachOContent>(); |
| 52 | + MachOContent? removed = null; |
| 53 | + var removedIndex = -1; |
| 54 | + MachOLinkEditDataCommand? addedCommand = null; |
| 55 | + |
44 | 56 | var command = CodeSignature; |
45 | | - if (command is null) |
| 57 | + var previousOffset = command?.DataOffset ?? 0; |
| 58 | + var previousSize = command?.DataSize ?? 0; |
| 59 | + |
| 60 | + try |
46 | 61 | { |
47 | | - command = new MachOLinkEditDataCommand |
| 62 | + if (command is null) |
| 63 | + { |
| 64 | + command = new MachOLinkEditDataCommand |
| 65 | + { |
| 66 | + Type = MachOLoadCommandType.CodeSignature, |
| 67 | + Size = MachOLinkEditDataCommand.CommandSize, |
| 68 | + }; |
| 69 | + AppendCommand(command); |
| 70 | + addedCommand = command; |
| 71 | + } |
| 72 | + // A signature covers everything before it, so it has to be the last thing in the file. |
| 73 | + // Whatever occupied that place before, including a previous signature, is replaced. |
| 74 | + if (command.DataOffset != 0) |
48 | 75 | { |
49 | | - Type = MachOLoadCommandType.CodeSignature, |
50 | | - Size = MachOLinkEditDataCommand.CommandSize, |
| 76 | + for (var i = Content.Count - 1; i >= 0; i--) |
| 77 | + { |
| 78 | + if (Content[i].Position == command.DataOffset) |
| 79 | + { |
| 80 | + removed = Content[i]; |
| 81 | + removedIndex = i; |
| 82 | + Content.RemoveAt(i); |
| 83 | + break; |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + var contentEnd = ComputeFileSize(); |
| 89 | + var signatureOffset = AlignHelper.AlignUp((uint)contentEnd, (uint)MachOCodeSignatureConstants.SignatureAlignment); |
| 90 | + |
| 91 | + var builder = new MachOAdHocSignatureBuilder(identifier) |
| 92 | + { |
| 93 | + CodeLimit = signatureOffset, |
| 94 | + ExecSegmentBase = text.FileOffset, |
| 95 | + ExecSegmentLimit = text.FileSize, |
| 96 | + ExecSegmentFlags = FileType == MachOFileType.Execute ? MachOCodeSignatureConstants.ExecSegMainBinary : 0, |
51 | 97 | }; |
52 | | - AppendCommand(command); |
53 | | - } |
| 98 | + var signatureSize = builder.ComputeSize(); |
54 | 99 |
|
55 | | - // A signature covers everything before it, so it has to be the last thing in the file. |
56 | | - // Whatever occupied that place before, including a previous signature, is replaced. |
57 | | - if (command.DataOffset != 0) |
58 | | - { |
59 | | - for (var i = Content.Count - 1; i >= 0; i--) |
| 100 | + command.DataOffset = signatureOffset; |
| 101 | + command.DataSize = signatureSize; |
| 102 | + |
| 103 | + linkEdit.FileSize = signatureOffset + signatureSize - linkEdit.FileOffset; |
| 104 | + linkEdit.VmSize = Math.Max(linkEdit.VmSize, linkEdit.FileSize); |
| 105 | + |
| 106 | + // Aligning the signature can leave a gap, and every byte of the file has to belong to |
| 107 | + // some content, so the padding is added rather than left as a hole in the list. |
| 108 | + if (signatureOffset > contentEnd) |
60 | 109 | { |
61 | | - if (Content[i].Position == command.DataOffset) |
| 110 | + var gap = new MachOStreamContent(new MemoryStream(new byte[signatureOffset - contentEnd])) |
62 | 111 | { |
63 | | - Content.RemoveAt(i); |
64 | | - break; |
65 | | - } |
| 112 | + Position = contentEnd, |
| 113 | + }; |
| 114 | + Content.Add(gap); |
| 115 | + added.Add(gap); |
66 | 116 | } |
67 | | - } |
68 | 117 |
|
69 | | - var contentEnd = ComputeFileSize(); |
70 | | - var signatureOffset = AlignHelper.AlignUp((uint)contentEnd, (uint)MachOCodeSignatureConstants.SignatureAlignment); |
| 118 | + var placeholder = new MachOStreamContent(new MemoryStream(new byte[signatureSize])) { Position = signatureOffset }; |
| 119 | + Content.Add(placeholder); |
| 120 | + added.Add(placeholder); |
71 | 121 |
|
72 | | - var builder = new MachOAdHocSignatureBuilder(identifier) |
73 | | - { |
74 | | - CodeLimit = signatureOffset, |
75 | | - ExecSegmentBase = text.FileOffset, |
76 | | - ExecSegmentLimit = text.FileSize, |
77 | | - ExecSegmentFlags = FileType == MachOFileType.Execute ? MachOCodeSignatureConstants.ExecSegMainBinary : 0, |
78 | | - }; |
79 | | - var signatureSize = builder.ComputeSize(); |
80 | | - |
81 | | - command.DataOffset = signatureOffset; |
82 | | - command.DataSize = signatureSize; |
83 | | - |
84 | | - linkEdit.FileSize = signatureOffset + signatureSize - linkEdit.FileOffset; |
85 | | - linkEdit.VmSize = Math.Max(linkEdit.VmSize, linkEdit.FileSize); |
86 | | - |
87 | | - // Aligning the signature can leave a gap, and every byte of the file has to belong to |
88 | | - // some content, so the padding is added rather than left as a hole in the list. |
89 | | - if (signatureOffset > contentEnd) |
| 122 | + // Signing is what makes the image match its signature again, so the edit is settled here |
| 123 | + // rather than after the digests are taken. Writing checks this, and the digests are taken |
| 124 | + // by writing the image out. |
| 125 | + IsCodeSignatureStale = false; |
| 126 | + |
| 127 | + // The digests cover the image as it will finally be written, so the header, the command |
| 128 | + // table and the signature's own offsets all have to be settled before they are taken. |
| 129 | + var image = new MemoryStream(); |
| 130 | + Write(image); |
| 131 | + placeholder.Content = new MemoryStream(builder.Build(image.GetBuffer().AsSpan(0, (int)signatureOffset))); |
| 132 | + } |
| 133 | + catch |
90 | 134 | { |
91 | | - Content.Add(new MachOStreamContent(new MemoryStream(new byte[signatureOffset - contentEnd])) |
| 135 | + foreach (var content in added) |
92 | 136 | { |
93 | | - Position = contentEnd, |
94 | | - }); |
95 | | - } |
| 137 | + Content.Remove(content); |
| 138 | + } |
96 | 139 |
|
97 | | - var placeholder = new MachOStreamContent(new MemoryStream(new byte[signatureSize])) { Position = signatureOffset }; |
98 | | - Content.Add(placeholder); |
| 140 | + if (removed is not null) |
| 141 | + { |
| 142 | + Content.Insert(removedIndex, removed); |
| 143 | + } |
99 | 144 |
|
100 | | - // Signing is what makes the image match its signature again, so the edit is settled here |
101 | | - // rather than after the digests are taken. Writing checks this, and the digests are taken |
102 | | - // by writing the image out. |
103 | | - IsCodeSignatureStale = false; |
| 145 | + if (addedCommand is not null) |
| 146 | + { |
| 147 | + LoadCommands.Remove(addedCommand); |
| 148 | + } |
| 149 | + else if (command is not null) |
| 150 | + { |
| 151 | + command.DataOffset = previousOffset; |
| 152 | + command.DataSize = previousSize; |
| 153 | + } |
104 | 154 |
|
105 | | - // The digests cover the image as it will finally be written, so the header, the command |
106 | | - // table and the signature's own offsets all have to be settled before they are taken. |
107 | | - var image = new MemoryStream(); |
108 | | - Write(image); |
109 | | - placeholder.Content = new MemoryStream(builder.Build(image.GetBuffer().AsSpan(0, (int)signatureOffset))); |
| 155 | + linkEdit.FileSize = previousFileSize; |
| 156 | + linkEdit.VmSize = previousVmSize; |
| 157 | + IsCodeSignatureStale = previousStale; |
| 158 | + throw; |
| 159 | + } |
110 | 160 | } |
111 | 161 |
|
112 | 162 | } |
0 commit comments