authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2020-11-20 10:11:16+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2020-11-26 11:50:09+01:00
logd14cd59ef0d9f03114ea6467a8446c17bf33ab9a
tree31b67942ac16abe960670a1d1378995532ce953e
parent3ac804628f26ad889701e746d44b21d369cff98d

stage2 macho: move code signature logic into struct


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

src/link/MachO.zig+15-54
......@@ -21,6 +21,7 @@ const Cache = @import("../Cache.zig");
2121const target_util = @import("../target.zig");
2222
2323const Trie = @import("MachO/Trie.zig");
24const CodeSignature = @import("MachO/CodeSignature.zig");
2425
2526pub const base_tag: File.Tag = File.Tag.macho;
2627
......@@ -385,7 +386,7 @@ pub fn flushModule(self: *MachO, comp: *Compilation) !void {
385386 self.libsystem_cmd_dirty = false;
386387 }
387388
388 try self.codeSign();
389 try self.writecodeSignature();
389390 },
390391 .Obj => {},
391392 .Lib => return error.TODOImplementWritingLibFiles,
......@@ -1751,60 +1752,20 @@ fn writeAllUndefSymbols(self: *MachO) !void {
17511752 try self.base.file.?.pwriteAll(mem.sliceAsBytes(self.undef_symbols.items), off);
17521753}
17531754
1754fn codeSign(self: *MachO) !void {
1755fn writecodeSignature(self: *MachO) !void {
17551756 const code_sig_cmd = &self.load_commands.items[self.code_signature_cmd_index.?].LinkeditData;
1756 // TODO add actual code signing mechanism
1757 var buffer: std.ArrayListUnmanaged(u8) = .{};
1758 defer buffer.deinit(self.base.allocator);
1759 // var super_blob = macho.SuperBlob{
1760 // .magic = macho.CSMAGIC_EMBEDDED_SIGNATURE,
1761 // .length = 0,
1762 // .count = 2,
1763 // };
1764 const length: u32 = @sizeOf(u32) * 12;
1765 try buffer.ensureCapacity(self.base.allocator, length);
1766 var buf: [@sizeOf(u32)]u8 = undefined;
1767 mem.writeIntBig(u32, &buf, macho.CSMAGIC_EMBEDDED_SIGNATURE);
1768 buffer.appendSliceAssumeCapacity(buf[0..]);
1769 mem.writeIntBig(u32, &buf, length);
1770 buffer.appendSliceAssumeCapacity(buf[0..]);
1771 mem.writeIntBig(u32, &buf, 2);
1772 buffer.appendSliceAssumeCapacity(buf[0..]);
1773 // macho.BlobIndex{
1774 // .type = macho.CSSLOT_REQUIREMENTS,
1775 // .offset = offset,
1776 // };
1777 mem.writeIntBig(u32, &buf, macho.CSSLOT_REQUIREMENTS);
1778 buffer.appendSliceAssumeCapacity(buf[0..]);
1779 mem.writeIntBig(u32, &buf, 28);
1780 buffer.appendSliceAssumeCapacity(buf[0..]);
1781 // macho.BlobIndex{
1782 // .type = macho.CSSLOT_SIGNATURESLOT,
1783 // .offset = offset,
1784 // };
1785 mem.writeIntBig(u32, &buf, macho.CSSLOT_SIGNATURESLOT);
1786 buffer.appendSliceAssumeCapacity(buf[0..]);
1787 mem.writeIntBig(u32, &buf, 40);
1788 buffer.appendSliceAssumeCapacity(buf[0..]);
1789 // const requirements_blob = macho.GenericBlob{
1790 // .magic = macho.CSMAGIC_REQUIREMENTS,
1791 // .length = 0,
1792 // };
1793 mem.writeIntBig(u32, &buf, macho.CSMAGIC_REQUIREMENTS);
1794 buffer.appendSliceAssumeCapacity(buf[0..]);
1795 mem.writeIntBig(u32, &buf, @sizeOf(u32) * 3);
1796 buffer.appendSliceAssumeCapacity(buf[0..]);
1797 mem.writeIntBig(u32, &buf, 0);
1798 buffer.appendSliceAssumeCapacity(buf[0..]);
1799 // const signature_blob = macho.GenericBlob{
1800 // .magic = macho.CSSLOT_SIGNATURESLOT,
1801 // .length = 0,
1802 // };
1803 mem.writeIntBig(u32, &buf, macho.CSMAGIC_BLOBWRAPPER);
1804 buffer.appendSliceAssumeCapacity(buf[0..]);
1805 mem.writeIntBig(u32, &buf, @sizeOf(u32) * 2);
1806 buffer.appendSliceAssumeCapacity(buf[0..]);
1807 try self.base.file.?.pwriteAll(buffer.items[0..], code_sig_cmd.dataoff);
1757
1758 var code_sig = CodeSignature.init(self.base.allocator);
1759 defer code_sig.deinit();
1760
1761 try code_sig.calcAdhocSignature();
1762
1763 var buffer = try self.base.allocator.alloc(u8, code_sig.size());
1764 defer self.base.allocator.free(buffer);
1765
1766 code_sig.write(buffer);
1767
1768 try self.base.file.?.pwriteAll(buffer, code_sig_cmd.dataoff);
18081769 try self.base.file.?.pwriteAll(&[_]u8{ 0 }, code_sig_cmd.dataoff + code_sig_cmd.datasize - 1);
18091770}
18101771
src/link/MachO/CodeSignature.zig created+59
......@@ -0,0 +1,59 @@
1const CodeSignature = @This();
2
3const std = @import("std");
4const assert = std.debug.assert;
5const log = std.log.scoped(.link);
6const macho = std.macho;
7const mem = std.mem;
8const testing = std.testing;
9const Allocator = mem.Allocator;
10
11// pub const Blob = union(enum) {
12// Signature: struct{
13// inner:
14// }
15// };
16
17alloc: *Allocator,
18inner: macho.SuperBlob = .{
19 .magic = macho.CSMAGIC_EMBEDDED_SIGNATURE,
20 .length = @sizeOf(macho.SuperBlob),
21 .count = 0,
22},
23// blobs: std.ArrayList(Blob),
24
25pub fn init(alloc: *Allocator) CodeSignature {
26 return .{
27 .alloc = alloc,
28 // .indices = std.ArrayList(Blob).init(alloc),
29 };
30}
31
32pub fn calcAdhocSignature(self: *CodeSignature) !void {}
33
34pub fn size(self: CodeSignature) u32 {
35 return self.inner.length;
36}
37
38pub fn write(self: CodeSignature, buffer: []u8) void {
39 assert(buffer.len >= self.inner.length);
40 self.writeHeader(buffer);
41}
42
43pub fn deinit(self: *CodeSignature) void {}
44
45fn writeHeader(self: CodeSignature, buffer: []u8) void {
46 assert(buffer.len >= @sizeOf(macho.SuperBlob));
47 mem.writeIntBig(u32, buffer[0..4], self.inner.magic);
48 mem.writeIntBig(u32, buffer[4..8], self.inner.length);
49 mem.writeIntBig(u32, buffer[8..12], self.inner.count);
50}
51
52test "CodeSignature header" {
53 var code_sig = CodeSignature.init(testing.allocator);
54 defer code_sig.deinit();
55 var buffer: [@sizeOf(macho.SuperBlob)]u8 = undefined;
56 code_sig.writeHeader(buffer[0..]);
57 const expected = &[_]u8{ 0xfa, 0xde, 0x0c, 0xc0, 0x0, 0x0, 0x0, 0xc, 0x0, 0x0, 0x0, 0x0 };
58 testing.expect(mem.eql(u8, expected[0..], buffer[0..]));
59}