authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2020-11-20 15:48:44+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2020-11-26 11:50:09+01:00
loga6e93da717334b6ee354d162107e00080807ce7c
tree512649225eefdeb8cd4322e18b5e24d047d7dc76
parent9dcf7ee9c904b25fbffd264cabeb6638e9d9e2d6

stage2 macho: generate a code sig (not valid yet)


3 files changed, 82 insertions(+), 26 deletions(-)

lib/std/macho.zig+4-4
......@@ -1384,10 +1384,10 @@ pub const CSTYPE_INDEX_REQUIREMENTS: u32 = 0x00000002;
13841384/// Compat with amfi
13851385pub const CSTYPE_INDEX_ENTITLEMENTS: u32 = 0x00000005;
13861386
1387pub const CS_HASHTYPE_SHA1: u32 = 1;
1388pub const CS_HASHTYPE_SHA256: u32 = 2;
1389pub const CS_HASHTYPE_SHA256_TRUNCATED: u32 = 3;
1390pub const CS_HASHTYPE_SHA384: u32 = 4;
1387pub const CS_HASHTYPE_SHA1: u8 = 1;
1388pub const CS_HASHTYPE_SHA256: u8 = 2;
1389pub const CS_HASHTYPE_SHA256_TRUNCATED: u8 = 3;
1390pub const CS_HASHTYPE_SHA384: u8 = 4;
13911391
13921392pub const CS_SHA1_LEN: u32 = 20;
13931393pub const CS_SHA256_LEN: u32 = 32;
src/link/MachO.zig+10-4
......@@ -385,8 +385,6 @@ pub fn flushModule(self: *MachO, comp: *Compilation) !void {
385385 try self.base.file.?.pwriteAll(mem.spanZ(LIB_SYSTEM_PATH), off);
386386 self.libsystem_cmd_dirty = false;
387387 }
388
389 try self.writecodeSignature();
390388 },
391389 .Obj => {},
392390 .Lib => return error.TODOImplementWritingLibFiles,
......@@ -430,6 +428,11 @@ pub fn flushModule(self: *MachO, comp: *Compilation) !void {
430428 assert(!self.cmd_table_dirty);
431429 assert(!self.dylinker_cmd_dirty);
432430 assert(!self.libsystem_cmd_dirty);
431
432 switch (self.base.options.output_mode) {
433 .Exe, .Lib => try self.writeCodeSignature(), // code signing always comes last
434 else => {},
435 }
433436}
434437
435438fn linkWithLLD(self: *MachO, comp: *Compilation) !void {
......@@ -1486,6 +1489,9 @@ pub fn populateMissingMetadata(self: *MachO) !void {
14861489 log.debug("found code signature free space 0x{x} to 0x{x}\n", .{ off, off + file_size });
14871490 code_sig.dataoff = off;
14881491 code_sig.datasize = file_size;
1492
1493 const segment_size = mem.alignForwardGeneric(u64, file_size, self.page_size);
1494 linkedit.vmsize += segment_size;
14891495 }
14901496 }
14911497 if (self.dyld_stub_binder_index == null) {
......@@ -1752,7 +1758,7 @@ fn writeAllUndefSymbols(self: *MachO) !void {
17521758 try self.base.file.?.pwriteAll(mem.sliceAsBytes(self.undef_symbols.items), off);
17531759}
17541760
1755fn writecodeSignature(self: *MachO) !void {
1761fn writeCodeSignature(self: *MachO) !void {
17561762 const code_sig_cmd = &self.load_commands.items[self.code_signature_cmd_index.?].LinkeditData;
17571763
17581764 var code_sig = CodeSignature.init(self.base.allocator);
......@@ -1766,7 +1772,7 @@ fn writecodeSignature(self: *MachO) !void {
17661772 code_sig.write(buffer);
17671773
17681774 try self.base.file.?.pwriteAll(buffer, code_sig_cmd.dataoff);
1769 try self.base.file.?.pwriteAll(&[_]u8{ 0 }, code_sig_cmd.dataoff + code_sig_cmd.datasize - 1);
1775 try self.base.file.?.pwriteAll(&[_]u8{0}, code_sig_cmd.dataoff + code_sig_cmd.datasize - 1);
17701776}
17711777
17721778fn writeExportTrie(self: *MachO) !void {
src/link/MachO/CodeSignature.zig+68-18
......@@ -7,19 +7,24 @@ const macho = std.macho;
77const mem = std.mem;
88const testing = std.testing;
99const Allocator = mem.Allocator;
10const Sha256 = std.crypto.hash.sha2.Sha256;
1011
1112const MachO = @import("../MachO.zig");
1213
13const Blob = struct {
14const hash_size: u8 = 32;
15const page_size: u16 = 0x1000;
16
17const CodeDirectory = struct {
1418 inner: macho.CodeDirectory,
1519 data: std.ArrayListUnmanaged(u8) = .{},
1620
17 fn size(self: Blob) u32 {
21 fn size(self: CodeDirectory) u32 {
1822 return self.inner.length;
1923 }
2024
21 fn write(self: Blob, buffer: []u8) void {
25 fn write(self: CodeDirectory, buffer: []u8) void {
2226 assert(buffer.len >= self.inner.length);
27
2328 mem.writeIntBig(u32, buffer[0..4], self.inner.magic);
2429 mem.writeIntBig(u32, buffer[4..8], self.inner.length);
2530 mem.writeIntBig(u32, buffer[8..12], self.inner.version);
......@@ -29,10 +34,10 @@ const Blob = struct {
2934 mem.writeIntBig(u32, buffer[24..28], self.inner.nSpecialSlots);
3035 mem.writeIntBig(u32, buffer[28..32], self.inner.nCodeSlots);
3136 mem.writeIntBig(u32, buffer[32..36], self.inner.codeLimit);
32 mem.writeIntBig(u8, buffer[36..37], self.inner.hashSize);
33 mem.writeIntBig(u8, buffer[37..38], self.inner.hashType);
34 mem.writeIntBig(u8, buffer[38..39], self.inner.platform);
35 mem.writeIntBig(u8, buffer[39..40], self.inner.pageSize);
37 buffer[36] = self.inner.hashSize;
38 buffer[37] = self.inner.hashType;
39 buffer[38] = self.inner.platform;
40 buffer[39] = self.inner.pageSize;
3641 mem.writeIntBig(u32, buffer[40..44], self.inner.spare2);
3742 mem.writeIntBig(u32, buffer[44..48], self.inner.scatterOffset);
3843 mem.writeIntBig(u32, buffer[48..52], self.inner.teamOffset);
......@@ -41,6 +46,8 @@ const Blob = struct {
4146 mem.writeIntBig(u64, buffer[64..72], self.inner.execSegBase);
4247 mem.writeIntBig(u64, buffer[72..80], self.inner.execSegLimit);
4348 mem.writeIntBig(u64, buffer[80..88], self.inner.execSegFlags);
49
50 mem.copy(u8, buffer[88..], self.data.items);
4451 }
4552};
4653
......@@ -50,7 +57,7 @@ inner: macho.SuperBlob = .{
5057 .length = @sizeOf(macho.SuperBlob),
5158 .count = 0,
5259},
53blob: ?Blob = null,
60cdir: ?CodeDirectory = null,
5461
5562pub fn init(alloc: *Allocator) CodeSignature {
5663 return .{
......@@ -60,10 +67,14 @@ pub fn init(alloc: *Allocator) CodeSignature {
6067
6168pub fn calcAdhocSignature(self: *CodeSignature, bin_file: *const MachO) !void {
6269 const text_segment = bin_file.load_commands.items[bin_file.text_segment_cmd_index.?].Segment;
70 const data_segment = bin_file.load_commands.items[bin_file.data_segment_cmd_index.?].Segment;
71 const linkedit_segment = bin_file.load_commands.items[bin_file.linkedit_segment_cmd_index.?].Segment;
72 const symtab = bin_file.load_commands.items[bin_file.symtab_cmd_index.?].Symtab;
73
6374 const execSegBase: u64 = text_segment.fileoff;
6475 const execSegLimit: u64 = text_segment.filesize;
65 const execSegFlags: u64 = text_segment.flags;
66 var blob = Blob{
76 const execSegFlags: u64 = if (bin_file.base.options.output_mode == .Exe) macho.CS_EXECSEG_MAIN_BINARY else 0;
77 var cdir = CodeDirectory{
6778 .inner = .{
6879 .magic = macho.CSMAGIC_CODEDIRECTORY,
6980 .length = @sizeOf(macho.CodeDirectory),
......@@ -74,10 +85,10 @@ pub fn calcAdhocSignature(self: *CodeSignature, bin_file: *const MachO) !void {
7485 .nSpecialSlots = 0,
7586 .nCodeSlots = 0,
7687 .codeLimit = 0,
77 .hashSize = 0,
78 .hashType = 0,
88 .hashSize = hash_size,
89 .hashType = macho.CS_HASHTYPE_SHA256,
7990 .platform = 0,
80 .pageSize = 0,
91 .pageSize = @truncate(u8, std.math.log2(page_size)),
8192 .spare2 = 0,
8293 .scatterOffset = 0,
8394 .teamOffset = 0,
......@@ -88,9 +99,48 @@ pub fn calcAdhocSignature(self: *CodeSignature, bin_file: *const MachO) !void {
8899 .execSegFlags = execSegFlags,
89100 },
90101 };
91 self.inner.length += @sizeOf(macho.BlobIndex) + blob.size();
102
103 const file_size = symtab.stroff + symtab.strsize;
104 const total_pages = mem.alignForward(file_size, page_size) / page_size;
105 log.debug("Total file size: {}; total number of pages: {}\n", .{ file_size, total_pages });
106
107 var hash: [hash_size]u8 = undefined;
108 var buffer = try bin_file.base.allocator.alloc(u8, page_size);
109 defer bin_file.base.allocator.free(buffer);
110 const macho_file = bin_file.base.file.?;
111
112 const id = bin_file.base.options.emit.?.sub_path;
113 try cdir.data.ensureCapacity(self.alloc, total_pages * hash_size + id.len + 1);
114
115 // 1. Save the identifier and update offsets
116 cdir.inner.identOffset = cdir.inner.length;
117 cdir.data.appendSliceAssumeCapacity(id);
118 cdir.data.appendAssumeCapacity(0);
119
120 // 2. Calculate hash for each page (in file) and write it to the buffer
121 // TODO figure out how we can cache several hashes since we won't update
122 // every page during incremental linking
123 cdir.inner.hashOffset = cdir.inner.identOffset + @intCast(u32, id.len) + 1;
124 var i: usize = 0;
125 while (i < total_pages) : (i += 1) {
126 const fstart = i * page_size;
127 const fsize = if (fstart + page_size > file_size) file_size - fstart else page_size;
128 const len = try macho_file.preadAll(buffer, fstart);
129 assert(fsize <= len);
130
131 Sha256.hash(buffer[0..fsize], &hash, .{});
132 log.debug("Calculated hash for page 0x{x}-0x{x}: 0x{x}\n", .{ fstart, fstart + fsize, hash[0..] });
133
134 cdir.data.appendSliceAssumeCapacity(hash[0..]);
135 cdir.inner.nCodeSlots += 1;
136 }
137
138 // 3. Update CodeDirectory length
139 cdir.inner.length += @intCast(u32, cdir.data.items.len);
140
141 self.inner.length += @sizeOf(macho.BlobIndex) + cdir.size();
92142 self.inner.count = 1;
93 self.blob = blob;
143 self.cdir = cdir;
94144}
95145
96146pub fn size(self: CodeSignature) u32 {
......@@ -102,12 +152,12 @@ pub fn write(self: CodeSignature, buffer: []u8) void {
102152 self.writeHeader(buffer);
103153 const offset: u32 = @sizeOf(macho.SuperBlob) + @sizeOf(macho.BlobIndex);
104154 writeBlobIndex(macho.CSSLOT_CODEDIRECTORY, offset, buffer[@sizeOf(macho.SuperBlob)..]);
105 self.blob.?.write(buffer[offset..]);
155 self.cdir.?.write(buffer[offset..]);
106156}
107157
108158pub fn deinit(self: *CodeSignature) void {
109 if (self.blob) |*b| {
110 b.data.deinit(self.alloc);
159 if (self.cdir) |*cdir| {
160 cdir.data.deinit(self.alloc);
111161 }
112162}
113163