1 /* ldid - (Mach-O) Link-Loader Identity Editor
2 * Copyright (C) 2007-2012 Jay Freeman (saurik)
5 /* GNU Affero General Public License, Version 3 {{{ */
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "minimal/stdlib.h"
34 #include <openssl/sha.h>
36 #include <plist/plist.h>
43 #define FAT_MAGIC 0xcafebabe
44 #define FAT_CIGAM 0xbebafeca
64 #define MH_MAGIC 0xfeedface
65 #define MH_CIGAM 0xcefaedfe
67 #define MH_MAGIC_64 0xfeedfacf
68 #define MH_CIGAM_64 0xcffaedfe
70 #define MH_DYLDLINK 0x4
73 #define MH_EXECUTE 0x2
76 #define MH_DYLIB_STUB 0x9
83 #define LC_REQ_DYLD uint32_t(0x80000000)
85 #define LC_SEGMENT uint32_t(0x01)
86 #define LC_SYMTAB uint32_t(0x02)
87 #define LC_DYSYMTAB uint32_t(0x0b)
88 #define LC_LOAD_DYLIB uint32_t(0x0c)
89 #define LC_ID_DYLIB uint32_t(0x0d)
90 #define LC_SEGMENT_64 uint32_t(0x19)
91 #define LC_UUID uint32_t(0x1b)
92 #define LC_CODE_SIGNATURE uint32_t(0x1d)
93 #define LC_SEGMENT_SPLIT_INFO uint32_t(0x1e)
94 #define LC_REEXPORT_DYLIB uint32_t(0x1f | LC_REQ_DYLD)
95 #define LC_ENCRYPTION_INFO uint32_t(0x21)
96 #define LC_DYLD_INFO uint32_t(0x22)
97 #define LC_DYLD_INFO_ONLY uint32_t(0x22 | LC_REQ_DYLD)
98 #define LC_ENCRYPTION_INFO_64 uint32_t(0x2c)
103 uint32_t current_version
;
104 uint32_t compatibility_version
;
107 struct dylib_command
{
113 struct uuid_command
{
119 struct symtab_command
{
128 struct dyld_info_command
{
132 uint32_t rebase_size
;
135 uint32_t weak_bind_off
;
136 uint32_t weak_bind_size
;
137 uint32_t lazy_bind_off
;
138 uint32_t lazy_bind_size
;
140 uint32_t export_size
;
143 struct dysymtab_command
{
156 uint32_t extrefsymoff
;
157 uint32_t nextrefsyms
;
158 uint32_t indirectsymoff
;
159 uint32_t nindirectsyms
;
166 struct dylib_table_of_contents
{
167 uint32_t symbol_index
;
168 uint32_t module_index
;
171 struct dylib_module
{
172 uint32_t module_name
;
181 uint32_t iinit_iterm
;
182 uint32_t ninit_nterm
;
183 uint32_t objc_module_info_addr
;
184 uint32_t objc_module_info_size
;
187 struct dylib_reference
{
192 struct relocation_info
{
194 uint32_t r_symbolnum
:24;
213 struct segment_command
{
227 struct segment_command_64
{
269 struct linkedit_data_command
{
276 struct encryption_info_command
{
284 #define BIND_OPCODE_MASK 0xf0
285 #define BIND_IMMEDIATE_MASK 0x0f
286 #define BIND_OPCODE_DONE 0x00
287 #define BIND_OPCODE_SET_DYLIB_ORDINAL_IMM 0x10
288 #define BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB 0x20
289 #define BIND_OPCODE_SET_DYLIB_SPECIAL_IMM 0x30
290 #define BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM 0x40
291 #define BIND_OPCODE_SET_TYPE_IMM 0x50
292 #define BIND_OPCODE_SET_ADDEND_SLEB 0x60
293 #define BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB 0x70
294 #define BIND_OPCODE_ADD_ADDR_ULEB 0x80
295 #define BIND_OPCODE_DO_BIND 0x90
296 #define BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB 0xa0
297 #define BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED 0xb0
298 #define BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB 0xc0
300 template <typename Type_
>
301 Type_
Align(Type_ value
, size_t align
) {
308 uint16_t Swap_(uint16_t value
) {
310 ((value
>> 8) & 0x00ff) |
311 ((value
<< 8) & 0xff00);
314 uint32_t Swap_(uint32_t value
) {
315 value
= ((value
>> 8) & 0x00ff00ff) |
316 ((value
<< 8) & 0xff00ff00);
317 value
= ((value
>> 16) & 0x0000ffff) |
318 ((value
<< 16) & 0xffff0000);
322 uint64_t Swap_(uint64_t value
) {
323 value
= (value
& 0x00000000ffffffff) << 32 | (value
& 0xffffffff00000000) >> 32;
324 value
= (value
& 0x0000ffff0000ffff) << 16 | (value
& 0xffff0000ffff0000) >> 16;
325 value
= (value
& 0x00ff00ff00ff00ff) << 8 | (value
& 0xff00ff00ff00ff00) >> 8;
329 int16_t Swap_(int16_t value
) {
330 return Swap_(static_cast<uint16_t>(value
));
333 int32_t Swap_(int32_t value
) {
334 return Swap_(static_cast<uint32_t>(value
));
337 int64_t Swap_(int64_t value
) {
338 return Swap_(static_cast<uint64_t>(value
));
343 uint16_t Swap(uint16_t value
) {
344 return little_
? Swap_(value
) : value
;
347 uint32_t Swap(uint32_t value
) {
348 return little_
? Swap_(value
) : value
;
351 uint64_t Swap(uint64_t value
) {
352 return little_
? Swap_(value
) : value
;
355 int16_t Swap(int16_t value
) {
356 return Swap(static_cast<uint16_t>(value
));
359 int32_t Swap(int32_t value
) {
360 return Swap(static_cast<uint32_t>(value
));
363 int64_t Swap(int64_t value
) {
364 return Swap(static_cast<uint64_t>(value
));
367 template <typename Target_
>
379 Data(void *base
, size_t size
) :
386 uint16_t Swap(uint16_t value
) const {
387 return swapped_
? Swap_(value
) : value
;
390 uint32_t Swap(uint32_t value
) const {
391 return swapped_
? Swap_(value
) : value
;
394 uint64_t Swap(uint64_t value
) const {
395 return swapped_
? Swap_(value
) : value
;
398 int16_t Swap(int16_t value
) const {
399 return Swap(static_cast<uint16_t>(value
));
402 int32_t Swap(int32_t value
) const {
403 return Swap(static_cast<uint32_t>(value
));
406 int64_t Swap(int64_t value
) const {
407 return Swap(static_cast<uint64_t>(value
));
410 void *GetBase() const {
414 size_t GetSize() const {
425 struct mach_header
*mach_header_
;
426 struct load_command
*load_command_
;
429 MachHeader(void *base
, size_t size
) :
432 mach_header_
= (mach_header
*) base
;
434 switch (Swap(mach_header_
->magic
)) {
436 swapped_
= !swapped_
;
442 swapped_
= !swapped_
;
451 void *post
= mach_header_
+ 1;
453 post
= (uint32_t *) post
+ 1;
454 load_command_
= (struct load_command
*) post
;
457 Swap(mach_header_
->filetype
) == MH_EXECUTE
||
458 Swap(mach_header_
->filetype
) == MH_DYLIB
||
459 Swap(mach_header_
->filetype
) == MH_BUNDLE
463 struct mach_header
*operator ->() const {
467 operator struct mach_header
*() const {
471 uint32_t GetCPUType() const {
472 return Swap(mach_header_
->cputype
);
475 uint32_t GetCPUSubtype() const {
476 return Swap(mach_header_
->cpusubtype
) & 0xff;
479 struct load_command
*GetLoadCommand() const {
480 return load_command_
;
483 std::vector
<struct load_command
*> GetLoadCommands() const {
484 std::vector
<struct load_command
*> load_commands
;
486 struct load_command
*load_command
= load_command_
;
487 for (uint32_t cmd
= 0; cmd
!= Swap(mach_header_
->ncmds
); ++cmd
) {
488 load_commands
.push_back(load_command
);
489 load_command
= (struct load_command
*) ((uint8_t *) load_command
+ Swap(load_command
->cmdsize
));
492 return load_commands
;
495 std::vector
<segment_command
*> GetSegments(const char *segment_name
) const {
496 std::vector
<struct segment_command
*> segment_commands
;
498 _foreach (load_command
, GetLoadCommands()) {
499 if (Swap(load_command
->cmd
) == LC_SEGMENT
) {
500 segment_command
*segment_command
= reinterpret_cast<struct segment_command
*>(load_command
);
501 if (strncmp(segment_command
->segname
, segment_name
, 16) == 0)
502 segment_commands
.push_back(segment_command
);
506 return segment_commands
;
509 std::vector
<segment_command_64
*> GetSegments64(const char *segment_name
) const {
510 std::vector
<struct segment_command_64
*> segment_commands
;
512 _foreach (load_command
, GetLoadCommands()) {
513 if (Swap(load_command
->cmd
) == LC_SEGMENT_64
) {
514 segment_command_64
*segment_command
= reinterpret_cast<struct segment_command_64
*>(load_command
);
515 if (strncmp(segment_command
->segname
, segment_name
, 16) == 0)
516 segment_commands
.push_back(segment_command
);
520 return segment_commands
;
523 std::vector
<section
*> GetSections(const char *segment_name
, const char *section_name
) const {
524 std::vector
<section
*> sections
;
526 _foreach (segment
, GetSegments(segment_name
)) {
527 section
*section
= (struct section
*) (segment
+ 1);
530 for (sect
= 0; sect
!= Swap(segment
->nsects
); ++sect
) {
531 if (strncmp(section
->sectname
, section_name
, 16) == 0)
532 sections
.push_back(section
);
540 template <typename Target_
>
541 Pointer
<Target_
> GetPointer(uint32_t address
, const char *segment_name
= NULL
) const {
542 load_command
*load_command
= (struct load_command
*) (mach_header_
+ 1);
545 for (cmd
= 0; cmd
!= Swap(mach_header_
->ncmds
); ++cmd
) {
546 if (Swap(load_command
->cmd
) == LC_SEGMENT
) {
547 segment_command
*segment_command
= (struct segment_command
*) load_command
;
548 if (segment_name
!= NULL
&& strncmp(segment_command
->segname
, segment_name
, 16) != 0)
551 section
*sections
= (struct section
*) (segment_command
+ 1);
554 for (sect
= 0; sect
!= Swap(segment_command
->nsects
); ++sect
) {
555 section
*section
= §ions
[sect
];
556 //printf("%s %u %p %p %u\n", segment_command->segname, sect, address, section->addr, section->size);
557 if (address
>= Swap(section
->addr
) && address
< Swap(section
->addr
) + Swap(section
->size
)) {
558 //printf("0x%.8x %s\n", address, segment_command->segname);
559 return Pointer
<Target_
>(this, reinterpret_cast<Target_
*>(address
- Swap(section
->addr
) + Swap(section
->offset
) + (char *) mach_header_
));
565 load_command
= (struct load_command
*) ((char *) load_command
+ Swap(load_command
->cmdsize
));
568 return Pointer
<Target_
>(this);
571 template <typename Target_
>
572 Pointer
<Target_
> GetOffset(uint32_t offset
) {
573 return Pointer
<Target_
>(this, reinterpret_cast<Target_
*>(offset
+ (uint8_t *) mach_header_
));
577 class FatMachHeader
:
584 FatMachHeader(void *base
, size_t size
, fat_arch
*fat_arch
) :
585 MachHeader(base
, size
),
590 fat_arch
*GetFatArch() const {
599 fat_header
*fat_header_
;
600 std::vector
<FatMachHeader
> mach_headers_
;
603 FatHeader(void *base
, size_t size
) :
606 fat_header_
= reinterpret_cast<struct fat_header
*>(base
);
608 if (Swap(fat_header_
->magic
) == FAT_CIGAM
) {
609 swapped_
= !swapped_
;
611 } else if (Swap(fat_header_
->magic
) != FAT_MAGIC
) {
613 mach_headers_
.push_back(FatMachHeader(base
, size
, NULL
));
615 size_t fat_narch
= Swap(fat_header_
->nfat_arch
);
616 fat_arch
*fat_arch
= reinterpret_cast<struct fat_arch
*>(fat_header_
+ 1);
618 for (arch
= 0; arch
!= fat_narch
; ++arch
) {
619 uint32_t arch_offset
= Swap(fat_arch
->offset
);
620 uint32_t arch_size
= Swap(fat_arch
->size
);
621 mach_headers_
.push_back(FatMachHeader((uint8_t *) base
+ arch_offset
, arch_size
, fat_arch
));
627 std::vector
<FatMachHeader
> &GetMachHeaders() {
628 return mach_headers_
;
632 return fat_header_
!= NULL
;
635 struct fat_header
*operator ->() const {
639 operator struct fat_header
*() const {
644 template <typename Target_
>
647 const MachHeader
*framework_
;
648 const Target_
*pointer_
;
651 Pointer(const MachHeader
*framework
= NULL
, const Target_
*pointer
= NULL
) :
652 framework_(framework
),
657 operator const Target_
*() const {
661 const Target_
*operator ->() const {
665 Pointer
<Target_
> &operator ++() {
670 template <typename Value_
>
671 Value_
Swap(Value_ value
) {
672 return framework_
->Swap(value
);
676 #define CSMAGIC_CODEDIRECTORY uint32_t(0xfade0c02)
677 #define CSMAGIC_EMBEDDED_SIGNATURE uint32_t(0xfade0cc0)
678 #define CSMAGIC_ENTITLEMENTS uint32_t(0xfade7171)
680 #define CSSLOT_CODEDIRECTORY uint32_t(0)
681 #define CSSLOT_REQUIREMENTS uint32_t(2)
682 #define CSSLOT_ENTITLEMENTS uint32_t(5)
697 struct BlobIndex index
[];
700 struct CodeDirectory
{
705 uint32_t identOffset
;
706 uint32_t nSpecialSlots
;
716 extern "C" uint32_t hash(uint8_t *k
, uint32_t length
, uint32_t initval
);
718 void sha1(uint8_t *hash
, uint8_t *data
, size_t size
) {
719 SHA1(data
, size
, hash
);
722 struct CodesignAllocation
{
723 FatMachHeader mach_header_
;
729 CodesignAllocation(FatMachHeader mach_header
, size_t offset
, size_t size
, size_t alloc
, size_t align
) :
730 mach_header_(mach_header
),
751 _syscall(close(file_
));
754 void open(const char *path
, int flags
) {
755 _assert(file_
== -1);
756 _syscall(file_
= ::open(path
, flags
));
773 _syscall(munmap(data_
, size_
));
785 Map(const char *path
, int oflag
, int pflag
, int mflag
) :
788 open(path
, oflag
, pflag
, mflag
);
791 Map(const char *path
, bool edit
) :
801 void open(const char *path
, int oflag
, int pflag
, int mflag
) {
804 file_
.open(path
, oflag
);
805 int file(file_
.file());
808 _syscall(fstat(file
, &stat
));
809 size_
= stat
.st_size
;
811 _syscall(data_
= mmap(NULL
, size_
, pflag
, mflag
, file
, 0));
814 void open(const char *path
, bool edit
) {
816 open(path
, O_RDWR
, PROT_READ
| PROT_WRITE
, MAP_SHARED
);
818 open(path
, O_RDONLY
, PROT_READ
, MAP_PRIVATE
);
825 size_t size() const {
829 operator std::string() const {
830 return std::string(static_cast<char *>(data_
), size_
);
834 void resign(const char *path
, const char *temp
, const char *name
, const std::string
&xml
) {
835 Map
input(path
, O_RDONLY
, PROT_READ
| PROT_WRITE
, MAP_PRIVATE
);
836 FatHeader
source(input
.data(), input
.size());
840 offset
+= sizeof(fat_header
) + sizeof(fat_arch
) * source
.Swap(source
->nfat_arch
);
842 std::vector
<CodesignAllocation
> allocations
;
843 _foreach (mach_header
, source
.GetMachHeaders()) {
844 struct linkedit_data_command
*signature(NULL
);
845 struct symtab_command
*symtab(NULL
);
847 _foreach (load_command
, mach_header
.GetLoadCommands()) {
848 uint32_t cmd(mach_header
.Swap(load_command
->cmd
));
850 else if (cmd
== LC_CODE_SIGNATURE
)
851 signature
= reinterpret_cast<struct linkedit_data_command
*>(load_command
);
852 else if (cmd
== LC_SYMTAB
)
853 symtab
= reinterpret_cast<struct symtab_command
*>(load_command
);
857 if (signature
== NULL
)
858 size
= mach_header
.GetSize();
860 size
= mach_header
.Swap(signature
->dataoff
);
861 _assert(size
<= mach_header
.GetSize());
864 if (symtab
!= NULL
) {
865 auto end(mach_header
.Swap(symtab
->stroff
) + mach_header
.Swap(symtab
->strsize
));
866 _assert(end
<= size
);
867 _assert(end
>= size
- 0x10);
873 alloc
+= sizeof(struct SuperBlob
);
876 special
= std::max(special
, CSSLOT_CODEDIRECTORY
);
877 alloc
+= sizeof(struct BlobIndex
);
878 alloc
+= sizeof(struct CodeDirectory
);
879 alloc
+= strlen(name
) + 1;
881 special
= std::max(special
, CSSLOT_REQUIREMENTS
);
882 alloc
+= sizeof(struct BlobIndex
);
885 if (xml
.size() != 0) {
886 special
= std::max(special
, CSSLOT_ENTITLEMENTS
);
887 alloc
+= sizeof(struct BlobIndex
);
888 alloc
+= sizeof(struct Blob
);
892 size_t normal((size
+ 0x1000 - 1) / 0x1000);
893 alloc
= Align(alloc
+ (special
+ normal
) * 0x14, 16);
896 auto *fat_arch(mach_header
.GetFatArch());
897 uint32_t align(fat_arch
== NULL
? 0 : source
.Swap(fat_arch
->align
));
898 offset
= Align(offset
, 1 << align
);
900 allocations
.push_back(CodesignAllocation(mach_header
, offset
, size
, alloc
, align
));
901 offset
+= size
+ alloc
;
902 offset
= Align(offset
, 16);
905 fclose(fopen(temp
, "w+"));
906 _syscall(truncate(temp
, offset
));
908 Map
output(temp
, O_RDWR
, PROT_READ
| PROT_WRITE
, MAP_SHARED
);
909 _assert(output
.size() == offset
);
910 void *file(output
.data());
911 memset(file
, 0, offset
);
917 auto *fat_header(reinterpret_cast<struct fat_header
*>(file
));
918 fat_header
->magic
= Swap(FAT_MAGIC
);
919 fat_header
->nfat_arch
= Swap(source
.Swap(source
->nfat_arch
));
920 fat_arch
= reinterpret_cast<struct fat_arch
*>(fat_header
+ 1);
923 _foreach (allocation
, allocations
) {
924 auto &source(allocation
.mach_header_
);
926 uint32_t align(allocation
.size_
);
927 if (allocation
.alloc_
!= 0)
928 align
= Align(align
, 0x10);
930 if (fat_arch
!= NULL
) {
931 fat_arch
->cputype
= Swap(source
->cputype
);
932 fat_arch
->cpusubtype
= Swap(source
->cpusubtype
);
933 fat_arch
->offset
= Swap(allocation
.offset_
);
934 fat_arch
->size
= Swap(align
+ allocation
.alloc_
);
935 fat_arch
->align
= Swap(allocation
.align_
);
939 void *target(reinterpret_cast<uint8_t *>(file
) + allocation
.offset_
);
940 memcpy(target
, source
, allocation
.size_
);
941 MachHeader
mach_header(target
, align
+ allocation
.alloc_
);
943 struct linkedit_data_command
*signature(NULL
);
944 _foreach (load_command
, mach_header
.GetLoadCommands()) {
945 uint32_t cmd(mach_header
.Swap(load_command
->cmd
));
946 if (cmd
!= LC_CODE_SIGNATURE
)
948 signature
= reinterpret_cast<struct linkedit_data_command
*>(load_command
);
952 _foreach (segment
, mach_header
.GetSegments("__LINKEDIT")) {
953 size_t size(mach_header
.Swap(align
+ allocation
.alloc_
- mach_header
.Swap(segment
->fileoff
)));
954 segment
->filesize
= size
;
955 segment
->vmsize
= Align(size
, 0x1000);
958 _foreach (segment
, mach_header
.GetSegments64("__LINKEDIT")) {
959 size_t size(mach_header
.Swap(align
+ allocation
.alloc_
- mach_header
.Swap(segment
->fileoff
)));
960 segment
->filesize
= size
;
961 segment
->vmsize
= Align(size
, 0x1000);
964 if (name
== NULL
&& signature
!= NULL
) {
965 auto before(reinterpret_cast<uint8_t *>(mach_header
.GetLoadCommand()));
966 auto after(reinterpret_cast<uint8_t *>(signature
));
967 auto next(mach_header
.Swap(signature
->cmdsize
));
968 auto total(mach_header
.Swap(mach_header
->sizeofcmds
));
969 memmove(signature
, after
+ next
, before
+ total
- after
- next
);
970 memset(before
+ total
- next
, 0, next
);
971 mach_header
->ncmds
= mach_header
.Swap(mach_header
.Swap(mach_header
->ncmds
) - 1);
972 mach_header
->sizeofcmds
= mach_header
.Swap(total
- next
);
977 if (signature
== NULL
) {
978 signature
= reinterpret_cast<struct linkedit_data_command
*>(reinterpret_cast<uint8_t *>(mach_header
.GetLoadCommand()) + mach_header
.Swap(mach_header
->sizeofcmds
));
979 signature
->cmd
= mach_header
.Swap(LC_CODE_SIGNATURE
);
980 signature
->cmdsize
= mach_header
.Swap(uint32_t(sizeof(*signature
)));
981 mach_header
->ncmds
= mach_header
.Swap(mach_header
.Swap(mach_header
->ncmds
) + 1);
982 mach_header
->sizeofcmds
= mach_header
.Swap(mach_header
.Swap(mach_header
->sizeofcmds
) + uint32_t(sizeof(*signature
)));
985 signature
->dataoff
= mach_header
.Swap(align
);
986 signature
->datasize
= mach_header
.Swap(allocation
.alloc_
);
988 uint32_t data
= mach_header
.Swap(signature
->dataoff
);
989 uint32_t size
= mach_header
.Swap(signature
->datasize
);
991 uint8_t *top
= reinterpret_cast<uint8_t *>(mach_header
.GetBase());
992 uint8_t *blob
= top
+ data
;
993 struct SuperBlob
*super
= reinterpret_cast<struct SuperBlob
*>(blob
);
994 super
->blob
.magic
= Swap(CSMAGIC_EMBEDDED_SIGNATURE
);
996 uint32_t count
= xml
.size() == 0 ? 2 : 3;
997 uint32_t offset
= sizeof(struct SuperBlob
) + count
* sizeof(struct BlobIndex
);
999 super
->index
[0].type
= Swap(CSSLOT_CODEDIRECTORY
);
1000 super
->index
[0].offset
= Swap(offset
);
1002 uint32_t begin
= offset
;
1003 struct CodeDirectory
*directory
= reinterpret_cast<struct CodeDirectory
*>(blob
+ begin
);
1004 offset
+= sizeof(struct CodeDirectory
);
1006 directory
->blob
.magic
= Swap(CSMAGIC_CODEDIRECTORY
);
1007 directory
->version
= Swap(uint32_t(0x00020001));
1008 directory
->flags
= Swap(uint32_t(0));
1009 directory
->codeLimit
= Swap(data
);
1010 directory
->hashSize
= 0x14;
1011 directory
->hashType
= 0x01;
1012 directory
->spare1
= 0x00;
1013 directory
->pageSize
= 0x0c;
1014 directory
->spare2
= Swap(uint32_t(0));
1016 directory
->identOffset
= Swap(offset
- begin
);
1017 strcpy(reinterpret_cast<char *>(blob
+ offset
), name
);
1018 offset
+= strlen(name
) + 1;
1020 uint32_t special
= xml
.size() == 0 ? CSSLOT_REQUIREMENTS
: CSSLOT_ENTITLEMENTS
;
1021 directory
->nSpecialSlots
= Swap(special
);
1023 uint8_t (*hashes
)[20] = reinterpret_cast<uint8_t (*)[20]>(blob
+ offset
);
1024 memset(hashes
, 0, sizeof(*hashes
) * special
);
1026 offset
+= sizeof(*hashes
) * special
;
1029 uint32_t pages
= (data
+ 0x1000 - 1) / 0x1000;
1030 directory
->nCodeSlots
= Swap(pages
);
1033 for (size_t i
= 0; i
!= pages
- 1; ++i
)
1034 sha1(hashes
[i
], top
+ 0x1000 * i
, 0x1000);
1036 sha1(hashes
[pages
- 1], top
+ 0x1000 * (pages
- 1), ((data
- 1) % 0x1000) + 1);
1038 directory
->hashOffset
= Swap(offset
- begin
);
1039 offset
+= sizeof(*hashes
) * pages
;
1040 directory
->blob
.length
= Swap(offset
- begin
);
1042 super
->index
[1].type
= Swap(CSSLOT_REQUIREMENTS
);
1043 super
->index
[1].offset
= Swap(offset
);
1045 memcpy(blob
+ offset
, "\xfa\xde\x0c\x01\x00\x00\x00\x0c\x00\x00\x00\x00", 0xc);
1048 if (xml
.size() != 0) {
1049 super
->index
[2].type
= Swap(CSSLOT_ENTITLEMENTS
);
1050 super
->index
[2].offset
= Swap(offset
);
1052 uint32_t begin
= offset
;
1053 struct Blob
*entitlements
= reinterpret_cast<struct Blob
*>(blob
+ begin
);
1054 offset
+= sizeof(struct Blob
);
1056 memcpy(blob
+ offset
, xml
.data(), xml
.size());
1057 offset
+= xml
.size();
1059 entitlements
->magic
= Swap(CSMAGIC_ENTITLEMENTS
);
1060 entitlements
->length
= Swap(offset
- begin
);
1063 for (size_t index(0); index
!= count
; ++index
) {
1064 uint32_t type
= Swap(super
->index
[index
].type
);
1065 if (type
!= 0 && type
<= special
) {
1066 uint32_t offset
= Swap(super
->index
[index
].offset
);
1067 struct Blob
*local
= (struct Blob
*) (blob
+ offset
);
1068 sha1((uint8_t *) (hashes
- type
), (uint8_t *) local
, Swap(local
->length
));
1072 super
->count
= Swap(count
);
1073 super
->blob
.length
= Swap(offset
);
1075 if (offset
> size
) {
1076 fprintf(stderr
, "offset (%u) > size (%u)\n", offset
, size
);
1078 } //else fprintf(stderr, "offset (%zu) <= size (%zu)\n", offset, size);
1080 memset(blob
+ offset
, 0, size
- offset
);
1085 int main(int argc
, const char *argv
[]) {
1091 little_
= endian
.byte
[0];
1106 uint32_t flag_CPUType(_not(uint32_t));
1107 uint32_t flag_CPUSubtype(_not(uint32_t));
1109 const char *flag_I(NULL
);
1116 std::vector
<std::string
> files
;
1119 fprintf(stderr
, "usage: %s -S[entitlements.xml] <binary>\n", argv
[0]);
1120 fprintf(stderr
, " %s -e MobileSafari\n", argv
[0]);
1121 fprintf(stderr
, " %s -S cat\n", argv
[0]);
1122 fprintf(stderr
, " %s -Stfp.xml gdb\n", argv
[0]);
1126 for (int argi(1); argi
!= argc
; ++argi
)
1127 if (argv
[argi
][0] != '-')
1128 files
.push_back(argv
[argi
]);
1129 else switch (argv
[argi
][1]) {
1130 case 'r': flag_r
= true; break;
1131 case 'e': flag_e
= true; break;
1133 case 'D': flag_D
= true; break;
1135 case 'a': flag_a
= true; break;
1139 if (argv
[argi
][2] != '\0') {
1140 const char *cpu
= argv
[argi
] + 2;
1141 const char *colon
= strchr(cpu
, ':');
1142 _assert(colon
!= NULL
);
1144 flag_CPUType
= strtoul(cpu
, &arge
, 0);
1145 _assert(arge
== colon
);
1146 flag_CPUSubtype
= strtoul(colon
+ 1, &arge
, 0);
1147 _assert(arge
== argv
[argi
] + strlen(argv
[argi
]));
1159 if (argv
[argi
][2] != '\0') {
1160 const char *xml
= argv
[argi
] + 2;
1161 xmlm
.open(xml
, O_RDONLY
, PROT_READ
, MAP_PRIVATE
);
1167 if (argv
[argi
][2] == '-')
1171 timev
= strtoul(argv
[argi
] + 2, &arge
, 0);
1172 _assert(arge
== argv
[argi
] + strlen(argv
[argi
]));
1177 flag_I
= argv
[argi
] + 2;
1185 _assert(!flag_S
|| !flag_r
);
1187 if (files
.empty()) usage
: {
1191 size_t filei(0), filee(0);
1192 _foreach (file
, files
) try {
1193 const char *path(file
.c_str());
1194 const char *base
= strrchr(path
, '/');
1198 dir
.assign(path
, base
++ - path
+ 1);
1202 const char *name(flag_I
?: base
);
1205 if (flag_S
|| flag_r
) {
1206 asprintf(&temp
, "%s.%s.cs", dir
.c_str(), base
);
1207 resign(path
, temp
, flag_S
? name
: NULL
, xmlm
);
1210 Map
mapping(temp
?: path
, flag_T
|| flag_s
);
1211 FatHeader
fat_header(mapping
.data(), mapping
.size());
1213 _foreach (mach_header
, fat_header
.GetMachHeaders()) {
1214 struct linkedit_data_command
*signature(NULL
);
1215 struct encryption_info_command
*encryption(NULL
);
1218 if (mach_header
.GetCPUType() != flag_CPUType
)
1220 if (mach_header
.GetCPUSubtype() != flag_CPUSubtype
)
1225 printf("cpu=0x%x:0x%x\n", mach_header
.GetCPUType(), mach_header
.GetCPUSubtype());
1227 _foreach (load_command
, mach_header
.GetLoadCommands()) {
1228 uint32_t cmd(mach_header
.Swap(load_command
->cmd
));
1231 else if (cmd
== LC_CODE_SIGNATURE
)
1232 signature
= reinterpret_cast<struct linkedit_data_command
*>(load_command
);
1233 else if (cmd
== LC_ENCRYPTION_INFO
|| cmd
== LC_ENCRYPTION_INFO_64
)
1234 encryption
= reinterpret_cast<struct encryption_info_command
*>(load_command
);
1235 else if (cmd
== LC_ID_DYLIB
) {
1236 volatile struct dylib_command
*dylib_command(reinterpret_cast<struct dylib_command
*>(load_command
));
1244 dylib_command
->dylib
.timestamp
= 0;
1245 timed
= hash(reinterpret_cast<uint8_t *>(mach_header
.GetBase()), mach_header
.GetSize(), timev
);
1248 dylib_command
->dylib
.timestamp
= mach_header
.Swap(timed
);
1254 _assert(encryption
!= NULL
);
1255 encryption
->cryptid
= mach_header
.Swap(0);
1259 _assert(signature
!= NULL
);
1261 uint32_t data
= mach_header
.Swap(signature
->dataoff
);
1263 uint8_t *top
= reinterpret_cast<uint8_t *>(mach_header
.GetBase());
1264 uint8_t *blob
= top
+ data
;
1265 struct SuperBlob
*super
= reinterpret_cast<struct SuperBlob
*>(blob
);
1267 for (size_t index(0); index
!= Swap(super
->count
); ++index
)
1268 if (Swap(super
->index
[index
].type
) == CSSLOT_ENTITLEMENTS
) {
1269 uint32_t begin
= Swap(super
->index
[index
].offset
);
1270 struct Blob
*entitlements
= reinterpret_cast<struct Blob
*>(blob
+ begin
);
1271 fwrite(entitlements
+ 1, 1, Swap(entitlements
->length
) - sizeof(struct Blob
), stdout
);
1276 _assert(signature
!= NULL
);
1278 uint32_t data
= mach_header
.Swap(signature
->dataoff
);
1280 uint8_t *top
= reinterpret_cast<uint8_t *>(mach_header
.GetBase());
1281 uint8_t *blob
= top
+ data
;
1282 struct SuperBlob
*super
= reinterpret_cast<struct SuperBlob
*>(blob
);
1284 for (size_t index(0); index
!= Swap(super
->count
); ++index
)
1285 if (Swap(super
->index
[index
].type
) == CSSLOT_CODEDIRECTORY
) {
1286 uint32_t begin
= Swap(super
->index
[index
].offset
);
1287 struct CodeDirectory
*directory
= reinterpret_cast<struct CodeDirectory
*>(blob
+ begin
);
1289 uint8_t (*hashes
)[20] = reinterpret_cast<uint8_t (*)[20]>(blob
+ begin
+ Swap(directory
->hashOffset
));
1290 uint32_t pages
= Swap(directory
->nCodeSlots
);
1293 for (size_t i
= 0; i
!= pages
- 1; ++i
)
1294 sha1(hashes
[i
], top
+ 0x1000 * i
, 0x1000);
1296 sha1(hashes
[pages
- 1], top
+ 0x1000 * (pages
- 1), ((data
- 1) % 0x1000) + 1);
1303 _syscall(stat(path
, &info
));
1304 _syscall(chown(temp
, info
.st_uid
, info
.st_gid
));
1305 _syscall(chmod(temp
, info
.st_mode
));
1306 _syscall(unlink(path
));
1307 _syscall(rename(temp
, path
));
1312 } catch (const char *) {