cs_dingling.m 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. #include "cs_dingling.h"
  2. #include <sys/mman.h>
  3. #include <spawn.h>
  4. #include <sys/stat.h>
  5. #include <mach-o/loader.h>
  6. #import <Foundation/Foundation.h>
  7. #import <CommonCrypto/CommonDigest.h>
  8. // Finds the LC_CODE_SIGNATURE load command
  9. const uint8_t *find_code_signature(img_info_t *info, uint32_t *cs_size) {
  10. #define _LOG_ERROR(str, args...) ERROR("(%s) " str, info->name, ##args)
  11. if (info == NULL || info->addr == NULL) {
  12. return NULL;
  13. }
  14. // mach_header_64 is mach_header + reserved for padding
  15. const struct mach_header *mh = (const struct mach_header*)info->addr;
  16. uint32_t sizeofmh = 0;
  17. switch (mh->magic) {
  18. case MH_MAGIC_64:
  19. sizeofmh = sizeof(struct mach_header_64);
  20. break;
  21. case MH_MAGIC:
  22. sizeofmh = sizeof(struct mach_header);
  23. break;
  24. default:
  25. _LOG_ERROR("your magic is not valid in these lands: %08x", mh->magic);
  26. return NULL;
  27. }
  28. if (mh->sizeofcmds < mh->ncmds * sizeof(struct load_command)) {
  29. _LOG_ERROR("Corrupted macho (sizeofcmds < ncmds * sizeof(lc))");
  30. return NULL;
  31. }
  32. if (mh->sizeofcmds + sizeofmh > info->size) {
  33. _LOG_ERROR("Corrupted macho (sizeofcmds + sizeof(mh) > size)");
  34. return NULL;
  35. }
  36. const struct load_command *cmd = (const struct load_command *)((uintptr_t) info->addr + sizeofmh);
  37. for (int i = 0; i != mh->ncmds; ++i) {
  38. if (cmd->cmd == LC_CODE_SIGNATURE) {
  39. const struct linkedit_data_command* cscmd = (const struct linkedit_data_command*)cmd;
  40. if (cscmd->dataoff + cscmd->datasize > info->size) {
  41. _LOG_ERROR("Corrupted LC_CODE_SIGNATURE: dataoff + datasize > fsize");
  42. return NULL;
  43. }
  44. if (cs_size) {
  45. *cs_size = cscmd->datasize;
  46. }
  47. return (const uint8_t*)((uintptr_t)info->addr + cscmd->dataoff);
  48. }
  49. cmd = (const struct load_command *)((uintptr_t)cmd + cmd->cmdsize);
  50. if ((uintptr_t)cmd + sizeof(struct load_command) > (uintptr_t)info->addr + info->size) {
  51. _LOG_ERROR("Corrupted macho: Unexpected end of file while parsing load commands");
  52. return NULL;
  53. }
  54. }
  55. _LOG_ERROR("Didnt find the code signature");
  56. return NULL;
  57. #undef _LOG_ERROR
  58. }
  59. #define BLOB_FITS(blob, size) ((size >= sizeof(*blob)) && (size >= ntohl(blob->length)))
  60. // xnu-3789.70.16/bsd/kern/ubc_subr.c#470
  61. int find_best_codedir(const void *csblob,
  62. uint32_t blob_size,
  63. const CS_CodeDirectory **chosen_cd,
  64. uint32_t *csb_offset,
  65. const CS_GenericBlob **entitlements) {
  66. *chosen_cd = NULL;
  67. *entitlements = NULL;
  68. const CS_GenericBlob *blob = (const CS_GenericBlob *)csblob;
  69. if (!BLOB_FITS(blob, blob_size)) {
  70. ERROR("csblob too small even for generic blob");
  71. return 1;
  72. }
  73. uint32_t length = ntohl(blob->length);
  74. if (ntohl(blob->magic) == CSMAGIC_EMBEDDED_SIGNATURE) {
  75. const CS_CodeDirectory *best_cd = NULL;
  76. int best_rank = 0;
  77. const CS_SuperBlob *sb = (const CS_SuperBlob *)csblob;
  78. uint32_t count = ntohl(sb->count);
  79. if (!BLOB_FITS(sb, blob_size)) {
  80. ERROR("csblob too small for superblob");
  81. return 1;
  82. }
  83. for (int n = 0; n < count; n++){
  84. const CS_BlobIndex *blobIndex = &sb->index[n];
  85. uint32_t type = ntohl(blobIndex->type);
  86. uint32_t offset = ntohl(blobIndex->offset);
  87. if (length < offset) {
  88. ERROR("offset of blob #%d overflows superblob length", n);
  89. return 1;
  90. }
  91. const CS_GenericBlob *subBlob = (const CS_GenericBlob *)((uintptr_t)csblob + offset);
  92. size_t subLength = ntohl(subBlob->length);
  93. if (type == CSSLOT_CODEDIRECTORY || (type >= CSSLOT_ALTERNATE_CODEDIRECTORIES && type < CSSLOT_ALTERNATE_CODEDIRECTORY_LIMIT)) {
  94. const CS_CodeDirectory *candidate = (const CS_CodeDirectory *)subBlob;
  95. unsigned int rank = hash_rank(candidate);
  96. // Apple's code: `rank > best_rank` (kind of obvious, right?)
  97. // So why is it I have to switch it to get it to work?
  98. // macos-10.12.6-sierra/xnu-3789.70.16/bsd/kern/ubc_subr.c#534
  99. if (best_cd == NULL || rank < best_rank) {
  100. best_cd = candidate;
  101. best_rank = rank;
  102. *chosen_cd = best_cd;
  103. *csb_offset = offset;
  104. }
  105. } else if (type == CSSLOT_ENTITLEMENTS) {
  106. *entitlements = subBlob;
  107. }
  108. }
  109. } else if (ntohl(blob->magic) == CSMAGIC_CODEDIRECTORY) {
  110. *chosen_cd = (const CS_CodeDirectory *)blob;
  111. *csb_offset = 0;
  112. } else {
  113. ERROR("Unknown magic at csblob start: %08x", ntohl(blob->magic));
  114. return 1;
  115. }
  116. if (chosen_cd == NULL) {
  117. ERROR("didn't find codedirectory to hash");
  118. return 1;
  119. }
  120. return 0;
  121. }
  122. // xnu-3789.70.16/bsd/kern/ubc_subr.c#231
  123. static unsigned int hash_rank(const CS_CodeDirectory *cd) {
  124. uint32_t type = cd->hashType;
  125. for (unsigned int n = 0; n < sizeof(hashPriorities) / sizeof(hashPriorities[0]); ++n) {
  126. if (hashPriorities[n] == type) {
  127. return n + 1;
  128. }
  129. }
  130. return 0;
  131. }
  132. int hash_code_directory(const CS_CodeDirectory *directory, uint8_t hash[CS_CDHASH_LEN]) {
  133. uint32_t realsize = ntohl(directory->length);
  134. if (ntohl(directory->magic) != CSMAGIC_CODEDIRECTORY) {
  135. ERROR("expected CSMAGIC_CODEDIRECTORY");
  136. return 1;
  137. }
  138. uint8_t out[CS_HASH_MAX_SIZE];
  139. uint8_t hash_type = directory->hashType;
  140. switch (hash_type) {
  141. case CS_HASHTYPE_SHA1:
  142. CC_SHA1(directory, realsize, out);
  143. break;
  144. case CS_HASHTYPE_SHA256:
  145. case CS_HASHTYPE_SHA256_TRUNCATED:
  146. CC_SHA256(directory, realsize, out);
  147. break;
  148. case CS_HASHTYPE_SHA384:
  149. CC_SHA384(directory, realsize, out);
  150. break;
  151. default:
  152. INFO("Unknown hash type: 0x%x", hash_type);
  153. return 2;
  154. }
  155. memcpy(hash, out, CS_CDHASH_LEN);
  156. return 0;
  157. }
  158. const char *get_hash_name(uint8_t hash_type) {
  159. switch (hash_type) {
  160. case CS_HASHTYPE_SHA1:
  161. return "SHA1";
  162. case CS_HASHTYPE_SHA256:
  163. case CS_HASHTYPE_SHA256_TRUNCATED:
  164. return "SHA256";
  165. case CS_HASHTYPE_SHA384:
  166. return "SHA384";
  167. default:
  168. return "UNKNWON";
  169. }
  170. return "";
  171. }
  172. int open_img(img_info_t* info) {
  173. #define _LOG_ERROR(str, args...) ERROR("(%s) " str, info->name, ##args)
  174. int ret = -1;
  175. if (info == NULL) {
  176. INFO("img info is NULL");
  177. return ret;
  178. }
  179. info->fd = -1;
  180. info->size = 0;
  181. info->addr = NULL;
  182. info->fd = open(info->name, O_RDONLY);
  183. if (info->fd == -1) {
  184. _LOG_ERROR("Couldn't open file");
  185. ret = 1;
  186. goto out;
  187. }
  188. struct stat s;
  189. if (fstat(info->fd, &s) != 0) {
  190. _LOG_ERROR("fstat: 0x%x (%s)", errno, strerror(errno));
  191. ret = 2;
  192. goto out;
  193. }
  194. size_t fsize = s.st_size;
  195. info->size = fsize - info->file_off;
  196. const void *map = mmap(NULL, fsize, PROT_READ, MAP_PRIVATE, info->fd, 0);
  197. if (map == MAP_FAILED) {
  198. _LOG_ERROR("mmap: 0x%x (%s)", errno, strerror(errno));
  199. ret = 4;
  200. goto out;
  201. }
  202. info->addr = (const void*) ((uintptr_t) map + info->file_off);
  203. ret = 0;
  204. out:;
  205. if (ret) {
  206. close_img(info);
  207. }
  208. return ret;
  209. #undef _LOG_ERROR
  210. }
  211. void close_img(img_info_t* info) {
  212. if (info == NULL) {
  213. return;
  214. }
  215. if (info->addr != NULL) {
  216. const void *map = (void*) ((uintptr_t) info->addr - info->file_off);
  217. size_t fsize = info->size + info->file_off;
  218. munmap((void*)map, fsize);
  219. }
  220. if (info->fd != -1) {
  221. close(info->fd);
  222. }
  223. }