authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-01-14 21:55:26+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-01-15 08:16:47+01:00
log2faf8c53d226cec1445156cafdec59af5e909fb2
tree1b177f07c11e52e99fc569bd217b754c6db7151e
parent2b0e3ee228e01473cf880f719db9bde5b8f34d25

macho: use target arch page_size for codesig

It turns out I was wrong and we can set the page size to the actual page size used by the target architecture when dividing the binary into chunks and calculating a hash of each chunk for embedding within the adhoc code signature. This shaves of a considerable amount of bytes since we divide the code signature section by at least 2x. I've also unified the `write` interface of `CodeSignature` struct to follow that used in every other bit of `MachO`; namely, the functions now accept a writer instead of a buffer, therefore, there is no need to manually track where to write each struct field anymore.

2 files changed, 64 insertions(+), 54 deletions(-)

src/link/MachO.zig+8-3
......@@ -2671,7 +2671,11 @@ fn writeCodeSignaturePadding(self: *MachO) !void {
26712671 const linkedit_segment = &self.load_commands.items[self.linkedit_segment_cmd_index.?].Segment;
26722672 const code_sig_cmd = &self.load_commands.items[self.code_signature_cmd_index.?].LinkeditData;
26732673 const fileoff = linkedit_segment.inner.fileoff + linkedit_segment.inner.filesize;
2674 const needed_size = CodeSignature.calcCodeSignaturePadding(self.base.options.emit.?.sub_path, fileoff);
2674 const needed_size = CodeSignature.calcCodeSignaturePaddingSize(
2675 self.base.options.emit.?.sub_path,
2676 fileoff,
2677 self.page_size,
2678 );
26752679
26762680 if (code_sig_cmd.datasize < needed_size) {
26772681 code_sig_cmd.dataoff = @intCast(u32, fileoff);
......@@ -2697,7 +2701,7 @@ fn writeCodeSignature(self: *MachO) !void {
26972701 const text_segment = self.load_commands.items[self.text_segment_cmd_index.?].Segment;
26982702 const code_sig_cmd = self.load_commands.items[self.code_signature_cmd_index.?].LinkeditData;
26992703
2700 var code_sig = CodeSignature.init(self.base.allocator);
2704 var code_sig = CodeSignature.init(self.base.allocator, self.page_size);
27012705 defer code_sig.deinit();
27022706 try code_sig.calcAdhocSignature(
27032707 self.base.file.?,
......@@ -2709,7 +2713,8 @@ fn writeCodeSignature(self: *MachO) !void {
27092713
27102714 var buffer = try self.base.allocator.alloc(u8, code_sig.size());
27112715 defer self.base.allocator.free(buffer);
2712 code_sig.write(buffer);
2716 var stream = std.io.fixedBufferStream(buffer);
2717 try code_sig.write(stream.writer());
27132718
27142719 log.debug("writing code signature from 0x{x} to 0x{x}", .{ code_sig_cmd.dataoff, code_sig_cmd.dataoff + buffer.len });
27152720
src/link/MachO/CodeSignature.zig+56-51
......@@ -11,7 +11,6 @@ const Allocator = mem.Allocator;
1111const Sha256 = std.crypto.hash.sha2.Sha256;
1212
1313const hash_size: u8 = 32;
14const page_size: u16 = 0x1000;
1514
1615const CodeDirectory = struct {
1716 inner: macho.CodeDirectory,
......@@ -21,45 +20,53 @@ const CodeDirectory = struct {
2120 return self.inner.length;
2221 }
2322
24 fn write(self: CodeDirectory, buffer: []u8) void {
25 assert(buffer.len >= self.inner.length);
26
27 mem.writeIntBig(u32, buffer[0..4], self.inner.magic);
28 mem.writeIntBig(u32, buffer[4..8], self.inner.length);
29 mem.writeIntBig(u32, buffer[8..12], self.inner.version);
30 mem.writeIntBig(u32, buffer[12..16], self.inner.flags);
31 mem.writeIntBig(u32, buffer[16..20], self.inner.hashOffset);
32 mem.writeIntBig(u32, buffer[20..24], self.inner.identOffset);
33 mem.writeIntBig(u32, buffer[24..28], self.inner.nSpecialSlots);
34 mem.writeIntBig(u32, buffer[28..32], self.inner.nCodeSlots);
35 mem.writeIntBig(u32, buffer[32..36], self.inner.codeLimit);
36 buffer[36] = self.inner.hashSize;
37 buffer[37] = self.inner.hashType;
38 buffer[38] = self.inner.platform;
39 buffer[39] = self.inner.pageSize;
40 mem.writeIntBig(u32, buffer[40..44], self.inner.spare2);
41 mem.writeIntBig(u32, buffer[44..48], self.inner.scatterOffset);
42 mem.writeIntBig(u32, buffer[48..52], self.inner.teamOffset);
43 mem.writeIntBig(u32, buffer[52..56], self.inner.spare3);
44 mem.writeIntBig(u64, buffer[56..64], self.inner.codeLimit64);
45 mem.writeIntBig(u64, buffer[64..72], self.inner.execSegBase);
46 mem.writeIntBig(u64, buffer[72..80], self.inner.execSegLimit);
47 mem.writeIntBig(u64, buffer[80..88], self.inner.execSegFlags);
48
49 mem.copy(u8, buffer[88..], self.data.items);
23 fn write(self: CodeDirectory, writer: anytype) !void {
24 try writer.writeIntBig(u32, self.inner.magic);
25 try writer.writeIntBig(u32, self.inner.length);
26 try writer.writeIntBig(u32, self.inner.version);
27 try writer.writeIntBig(u32, self.inner.flags);
28 try writer.writeIntBig(u32, self.inner.hashOffset);
29 try writer.writeIntBig(u32, self.inner.identOffset);
30 try writer.writeIntBig(u32, self.inner.nSpecialSlots);
31 try writer.writeIntBig(u32, self.inner.nCodeSlots);
32 try writer.writeIntBig(u32, self.inner.codeLimit);
33 try writer.writeByte(self.inner.hashSize);
34 try writer.writeByte(self.inner.hashType);
35 try writer.writeByte(self.inner.platform);
36 try writer.writeByte(self.inner.pageSize);
37 try writer.writeIntBig(u32, self.inner.spare2);
38 try writer.writeIntBig(u32, self.inner.scatterOffset);
39 try writer.writeIntBig(u32, self.inner.teamOffset);
40 try writer.writeIntBig(u32, self.inner.spare3);
41 try writer.writeIntBig(u64, self.inner.codeLimit64);
42 try writer.writeIntBig(u64, self.inner.execSegBase);
43 try writer.writeIntBig(u64, self.inner.execSegLimit);
44 try writer.writeIntBig(u64, self.inner.execSegFlags);
45 try writer.writeAll(self.data.items);
5046 }
5147};
5248
5349allocator: *Allocator,
50
51/// Code signature blob header.
5452inner: macho.SuperBlob = .{
5553 .magic = macho.CSMAGIC_EMBEDDED_SIGNATURE,
5654 .length = @sizeOf(macho.SuperBlob),
5755 .count = 0,
5856},
57
58/// CodeDirectory header which holds the hash of the binary.
5959cdir: ?CodeDirectory = null,
6060
61pub fn init(allocator: *Allocator) CodeSignature {
62 return .{ .allocator = allocator };
61/// Page size is dependent on the target cpu architecture.
62/// For x86_64 that's 4KB, whereas for aarch64, that's 16KB.
63page_size: u16,
64
65pub fn init(allocator: *Allocator, page_size: u16) CodeSignature {
66 return .{
67 .allocator = allocator,
68 .page_size = page_size,
69 };
6370}
6471
6572pub fn calcAdhocSignature(
......@@ -88,7 +95,7 @@ pub fn calcAdhocSignature(
8895 .hashSize = hash_size,
8996 .hashType = macho.CS_HASHTYPE_SHA256,
9097 .platform = 0,
91 .pageSize = @truncate(u8, std.math.log2(page_size)),
98 .pageSize = @truncate(u8, std.math.log2(self.page_size)),
9299 .spare2 = 0,
93100 .scatterOffset = 0,
94101 .teamOffset = 0,
......@@ -100,10 +107,10 @@ pub fn calcAdhocSignature(
100107 },
101108 };
102109
103 const total_pages = mem.alignForward(file_size, page_size) / page_size;
110 const total_pages = mem.alignForward(file_size, self.page_size) / self.page_size;
104111
105112 var hash: [hash_size]u8 = undefined;
106 var buffer = try self.allocator.alloc(u8, page_size);
113 var buffer = try self.allocator.alloc(u8, self.page_size);
107114 defer self.allocator.free(buffer);
108115
109116 try cdir.data.ensureCapacity(self.allocator, total_pages * hash_size + id.len + 1);
......@@ -119,8 +126,8 @@ pub fn calcAdhocSignature(
119126 cdir.inner.hashOffset = cdir.inner.identOffset + @intCast(u32, id.len) + 1;
120127 var i: usize = 0;
121128 while (i < total_pages) : (i += 1) {
122 const fstart = i * page_size;
123 const fsize = if (fstart + page_size > file_size) file_size - fstart else page_size;
129 const fstart = i * self.page_size;
130 const fsize = if (fstart + self.page_size > file_size) file_size - fstart else self.page_size;
124131 const len = try file.preadAll(buffer, fstart);
125132 assert(fsize <= len);
126133
......@@ -142,12 +149,11 @@ pub fn size(self: CodeSignature) u32 {
142149 return self.inner.length;
143150}
144151
145pub fn write(self: CodeSignature, buffer: []u8) void {
146 assert(buffer.len >= self.inner.length);
147 self.writeHeader(buffer);
152pub fn write(self: CodeSignature, writer: anytype) !void {
153 try self.writeHeader(writer);
148154 const offset: u32 = @sizeOf(macho.SuperBlob) + @sizeOf(macho.BlobIndex);
149 writeBlobIndex(macho.CSSLOT_CODEDIRECTORY, offset, buffer[@sizeOf(macho.SuperBlob)..]);
150 self.cdir.?.write(buffer[offset..]);
155 try writeBlobIndex(macho.CSSLOT_CODEDIRECTORY, offset, writer);
156 try self.cdir.?.write(writer);
151157}
152158
153159pub fn deinit(self: *CodeSignature) void {
......@@ -156,31 +162,30 @@ pub fn deinit(self: *CodeSignature) void {
156162 }
157163}
158164
159fn writeHeader(self: CodeSignature, buffer: []u8) void {
160 assert(buffer.len >= @sizeOf(macho.SuperBlob));
161 mem.writeIntBig(u32, buffer[0..4], self.inner.magic);
162 mem.writeIntBig(u32, buffer[4..8], self.inner.length);
163 mem.writeIntBig(u32, buffer[8..12], self.inner.count);
165fn writeHeader(self: CodeSignature, writer: anytype) !void {
166 try writer.writeIntBig(u32, self.inner.magic);
167 try writer.writeIntBig(u32, self.inner.length);
168 try writer.writeIntBig(u32, self.inner.count);
164169}
165170
166fn writeBlobIndex(tt: u32, offset: u32, buffer: []u8) void {
167 assert(buffer.len >= @sizeOf(macho.BlobIndex));
168 mem.writeIntBig(u32, buffer[0..4], tt);
169 mem.writeIntBig(u32, buffer[4..8], offset);
171fn writeBlobIndex(tt: u32, offset: u32, writer: anytype) !void {
172 try writer.writeIntBig(u32, tt);
173 try writer.writeIntBig(u32, offset);
170174}
171175
172176test "CodeSignature header" {
173 var code_sig = CodeSignature.init(testing.allocator);
177 var code_sig = CodeSignature.init(testing.allocator, 0x1000);
174178 defer code_sig.deinit();
175179
176180 var buffer: [@sizeOf(macho.SuperBlob)]u8 = undefined;
177 code_sig.writeHeader(&buffer);
181 var stream = std.io.fixedBufferStream(&buffer);
182 try code_sig.writeHeader(stream.writer());
178183
179184 const expected = &[_]u8{ 0xfa, 0xde, 0x0c, 0xc0, 0x0, 0x0, 0x0, 0xc, 0x0, 0x0, 0x0, 0x0 };
180185 testing.expect(mem.eql(u8, expected, &buffer));
181186}
182187
183pub fn calcCodeSignaturePadding(id: []const u8, file_size: u64) u32 {
188pub fn calcCodeSignaturePaddingSize(id: []const u8, file_size: u64, page_size: u16) u32 {
184189 const ident_size = id.len + 1;
185190 const total_pages = mem.alignForwardGeneric(u64, file_size, page_size) / page_size;
186191 const hashed_size = total_pages * hash_size;