macho.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. /*************************************************************************/
  2. /* macho.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "macho.h"
  31. uint32_t MachO::seg_align(uint64_t p_vmaddr, uint32_t p_min, uint32_t p_max) {
  32. uint32_t salign = p_max;
  33. if (p_vmaddr != 0) {
  34. uint64_t seg_align = 1;
  35. salign = 0;
  36. while ((seg_align & p_vmaddr) == 0) {
  37. seg_align = seg_align << 1;
  38. salign++;
  39. }
  40. salign = CLAMP(salign, p_min, p_max);
  41. }
  42. return salign;
  43. }
  44. bool MachO::alloc_signature(uint64_t p_size) {
  45. ERR_FAIL_COND_V_MSG(fa.is_null(), false, "MachO: File not opened.");
  46. if (signature_offset != 0) {
  47. // Nothing to do, already have signature load command.
  48. return true;
  49. }
  50. if (lc_limit == 0 || lc_limit + 16 > exe_base) {
  51. ERR_FAIL_V_MSG(false, "MachO: Can't allocate signature load command, please use \"codesign_allocate\" utility first.");
  52. } else {
  53. // Add signature load command.
  54. signature_offset = lc_limit;
  55. fa->seek(lc_limit);
  56. LoadCommandHeader lc;
  57. lc.cmd = LC_CODE_SIGNATURE;
  58. lc.cmdsize = 16;
  59. if (swap) {
  60. lc.cmdsize = BSWAP32(lc.cmdsize);
  61. }
  62. fa->store_buffer((const uint8_t *)&lc, sizeof(LoadCommandHeader));
  63. uint32_t lc_offset = fa->get_length() + PAD(fa->get_length(), 16);
  64. uint32_t lc_size = 0;
  65. if (swap) {
  66. lc_offset = BSWAP32(lc_offset);
  67. lc_size = BSWAP32(lc_size);
  68. }
  69. fa->store_32(lc_offset);
  70. fa->store_32(lc_size);
  71. // Write new command number.
  72. fa->seek(0x10);
  73. uint32_t ncmds = fa->get_32();
  74. uint32_t cmdssize = fa->get_32();
  75. if (swap) {
  76. ncmds = BSWAP32(ncmds);
  77. cmdssize = BSWAP32(cmdssize);
  78. }
  79. ncmds += 1;
  80. cmdssize += 16;
  81. if (swap) {
  82. ncmds = BSWAP32(ncmds);
  83. cmdssize = BSWAP32(cmdssize);
  84. }
  85. fa->seek(0x10);
  86. fa->store_32(ncmds);
  87. fa->store_32(cmdssize);
  88. lc_limit = lc_limit + sizeof(LoadCommandHeader) + 8;
  89. return true;
  90. }
  91. }
  92. bool MachO::is_macho(const String &p_path) {
  93. Ref<FileAccess> fb = FileAccess::open(p_path, FileAccess::READ);
  94. ERR_FAIL_COND_V_MSG(fb.is_null(), false, vformat("MachO: Can't open file: \"%s\".", p_path));
  95. uint32_t magic = fb->get_32();
  96. return (magic == 0xcefaedfe || magic == 0xfeedface || magic == 0xcffaedfe || magic == 0xfeedfacf);
  97. }
  98. bool MachO::open_file(const String &p_path) {
  99. fa = FileAccess::open(p_path, FileAccess::READ_WRITE);
  100. ERR_FAIL_COND_V_MSG(fa.is_null(), false, vformat("MachO: Can't open file: \"%s\".", p_path));
  101. uint32_t magic = fa->get_32();
  102. MachHeader mach_header;
  103. // Read MachO header.
  104. swap = (magic == 0xcffaedfe || magic == 0xcefaedfe);
  105. if (magic == 0xcefaedfe || magic == 0xfeedface) {
  106. // Thin 32-bit binary.
  107. fa->get_buffer((uint8_t *)&mach_header, sizeof(MachHeader));
  108. } else if (magic == 0xcffaedfe || magic == 0xfeedfacf) {
  109. // Thin 64-bit binary.
  110. fa->get_buffer((uint8_t *)&mach_header, sizeof(MachHeader));
  111. fa->get_32(); // Skip extra reserved field.
  112. } else {
  113. ERR_FAIL_V_MSG(false, vformat("MachO: File is not a valid MachO binary: \"%s\".", p_path));
  114. }
  115. if (swap) {
  116. mach_header.ncmds = BSWAP32(mach_header.ncmds);
  117. mach_header.cpusubtype = BSWAP32(mach_header.cpusubtype);
  118. mach_header.cputype = BSWAP32(mach_header.cputype);
  119. }
  120. cpusubtype = mach_header.cpusubtype;
  121. cputype = mach_header.cputype;
  122. align = 0;
  123. exe_base = std::numeric_limits<uint64_t>::max();
  124. exe_limit = 0;
  125. lc_limit = 0;
  126. link_edit_offset = 0;
  127. signature_offset = 0;
  128. // Read load commands.
  129. for (uint32_t i = 0; i < mach_header.ncmds; i++) {
  130. LoadCommandHeader lc;
  131. fa->get_buffer((uint8_t *)&lc, sizeof(LoadCommandHeader));
  132. if (swap) {
  133. lc.cmd = BSWAP32(lc.cmd);
  134. lc.cmdsize = BSWAP32(lc.cmdsize);
  135. }
  136. uint64_t ps = fa->get_position();
  137. switch (lc.cmd) {
  138. case LC_SEGMENT: {
  139. LoadCommandSegment lc_seg;
  140. fa->get_buffer((uint8_t *)&lc_seg, sizeof(LoadCommandSegment));
  141. if (swap) {
  142. lc_seg.nsects = BSWAP32(lc_seg.nsects);
  143. lc_seg.vmaddr = BSWAP32(lc_seg.vmaddr);
  144. lc_seg.vmsize = BSWAP32(lc_seg.vmsize);
  145. }
  146. align = MAX(align, seg_align(lc_seg.vmaddr, 2, 15));
  147. if (String(lc_seg.segname) == "__TEXT") {
  148. exe_limit = MAX(exe_limit, lc_seg.vmsize);
  149. for (uint32_t j = 0; j < lc_seg.nsects; j++) {
  150. Section lc_sect;
  151. fa->get_buffer((uint8_t *)&lc_sect, sizeof(Section));
  152. if (String(lc_sect.sectname) == "__text") {
  153. if (swap) {
  154. exe_base = MIN(exe_base, BSWAP32(lc_sect.offset));
  155. } else {
  156. exe_base = MIN(exe_base, lc_sect.offset);
  157. }
  158. }
  159. if (swap) {
  160. align = MAX(align, BSWAP32(lc_sect.align));
  161. } else {
  162. align = MAX(align, lc_sect.align);
  163. }
  164. }
  165. } else if (String(lc_seg.segname) == "__LINKEDIT") {
  166. link_edit_offset = ps - 8;
  167. }
  168. } break;
  169. case LC_SEGMENT_64: {
  170. LoadCommandSegment64 lc_seg;
  171. fa->get_buffer((uint8_t *)&lc_seg, sizeof(LoadCommandSegment64));
  172. if (swap) {
  173. lc_seg.nsects = BSWAP32(lc_seg.nsects);
  174. lc_seg.vmaddr = BSWAP64(lc_seg.vmaddr);
  175. lc_seg.vmsize = BSWAP64(lc_seg.vmsize);
  176. }
  177. align = MAX(align, seg_align(lc_seg.vmaddr, 3, 15));
  178. if (String(lc_seg.segname) == "__TEXT") {
  179. exe_limit = MAX(exe_limit, lc_seg.vmsize);
  180. for (uint32_t j = 0; j < lc_seg.nsects; j++) {
  181. Section64 lc_sect;
  182. fa->get_buffer((uint8_t *)&lc_sect, sizeof(Section64));
  183. if (String(lc_sect.sectname) == "__text") {
  184. if (swap) {
  185. exe_base = MIN(exe_base, BSWAP32(lc_sect.offset));
  186. } else {
  187. exe_base = MIN(exe_base, lc_sect.offset);
  188. }
  189. if (swap) {
  190. align = MAX(align, BSWAP32(lc_sect.align));
  191. } else {
  192. align = MAX(align, lc_sect.align);
  193. }
  194. }
  195. }
  196. } else if (String(lc_seg.segname) == "__LINKEDIT") {
  197. link_edit_offset = ps - 8;
  198. }
  199. } break;
  200. case LC_CODE_SIGNATURE: {
  201. signature_offset = ps - 8;
  202. } break;
  203. default: {
  204. } break;
  205. }
  206. fa->seek(ps + lc.cmdsize - 8);
  207. lc_limit = ps + lc.cmdsize - 8;
  208. }
  209. if (exe_limit == 0 || lc_limit == 0) {
  210. ERR_FAIL_V_MSG(false, vformat("MachO: No load commands or executable code found: \"%s\".", p_path));
  211. }
  212. return true;
  213. }
  214. uint64_t MachO::get_exe_base() {
  215. ERR_FAIL_COND_V_MSG(fa.is_null(), 0, "MachO: File not opened.");
  216. return exe_base;
  217. }
  218. uint64_t MachO::get_exe_limit() {
  219. ERR_FAIL_COND_V_MSG(fa.is_null(), 0, "MachO: File not opened.");
  220. return exe_limit;
  221. }
  222. int32_t MachO::get_align() {
  223. ERR_FAIL_COND_V_MSG(fa.is_null(), 0, "MachO: File not opened.");
  224. return align;
  225. }
  226. uint32_t MachO::get_cputype() {
  227. ERR_FAIL_COND_V_MSG(fa.is_null(), 0, "MachO: File not opened.");
  228. return cputype;
  229. }
  230. uint32_t MachO::get_cpusubtype() {
  231. ERR_FAIL_COND_V_MSG(fa.is_null(), 0, "MachO: File not opened.");
  232. return cpusubtype;
  233. }
  234. uint64_t MachO::get_size() {
  235. ERR_FAIL_COND_V_MSG(fa.is_null(), 0, "MachO: File not opened.");
  236. return fa->get_length();
  237. }
  238. uint64_t MachO::get_signature_offset() {
  239. ERR_FAIL_COND_V_MSG(fa.is_null(), 0, "MachO: File not opened.");
  240. ERR_FAIL_COND_V_MSG(signature_offset == 0, 0, "MachO: No signature load command.");
  241. fa->seek(signature_offset + 8);
  242. if (swap) {
  243. return BSWAP32(fa->get_32());
  244. } else {
  245. return fa->get_32();
  246. }
  247. }
  248. uint64_t MachO::get_code_limit() {
  249. ERR_FAIL_COND_V_MSG(fa.is_null(), 0, "MachO: File not opened.");
  250. if (signature_offset == 0) {
  251. return fa->get_length() + PAD(fa->get_length(), 16);
  252. } else {
  253. return get_signature_offset();
  254. }
  255. }
  256. uint64_t MachO::get_signature_size() {
  257. ERR_FAIL_COND_V_MSG(fa.is_null(), 0, "MachO: File not opened.");
  258. ERR_FAIL_COND_V_MSG(signature_offset == 0, 0, "MachO: No signature load command.");
  259. fa->seek(signature_offset + 12);
  260. if (swap) {
  261. return BSWAP32(fa->get_32());
  262. } else {
  263. return fa->get_32();
  264. }
  265. }
  266. bool MachO::is_signed() {
  267. ERR_FAIL_COND_V_MSG(fa.is_null(), false, "MachO: File not opened.");
  268. if (signature_offset == 0) {
  269. return false;
  270. }
  271. fa->seek(get_signature_offset());
  272. uint32_t magic = BSWAP32(fa->get_32());
  273. if (magic != 0xfade0cc0) {
  274. return false; // No SuperBlob found.
  275. }
  276. fa->get_32(); // Skip size field, unused.
  277. uint32_t count = BSWAP32(fa->get_32());
  278. for (uint32_t i = 0; i < count; i++) {
  279. uint32_t index_type = BSWAP32(fa->get_32());
  280. uint32_t offset = BSWAP32(fa->get_32());
  281. if (index_type == 0x00000000) { // CodeDirectory index type.
  282. fa->seek(get_signature_offset() + offset + 12);
  283. uint32_t flags = BSWAP32(fa->get_32());
  284. if (flags & 0x20000) {
  285. return false; // Found CD, linker-signed.
  286. } else {
  287. return true; // Found CD, not linker-signed.
  288. }
  289. }
  290. }
  291. return false; // No CD found.
  292. }
  293. PackedByteArray MachO::get_cdhash_sha1() {
  294. ERR_FAIL_COND_V_MSG(fa.is_null(), PackedByteArray(), "MachO: File not opened.");
  295. if (signature_offset == 0) {
  296. return PackedByteArray();
  297. }
  298. fa->seek(get_signature_offset());
  299. uint32_t magic = BSWAP32(fa->get_32());
  300. if (magic != 0xfade0cc0) {
  301. return PackedByteArray(); // No SuperBlob found.
  302. }
  303. fa->get_32(); // Skip size field, unused.
  304. uint32_t count = BSWAP32(fa->get_32());
  305. for (uint32_t i = 0; i < count; i++) {
  306. fa->get_32(); // Index type, skip.
  307. uint32_t offset = BSWAP32(fa->get_32());
  308. uint64_t pos = fa->get_position();
  309. fa->seek(get_signature_offset() + offset);
  310. uint32_t cdmagic = BSWAP32(fa->get_32());
  311. uint32_t cdsize = BSWAP32(fa->get_32());
  312. if (cdmagic == 0xfade0c02) { // CodeDirectory.
  313. fa->seek(get_signature_offset() + offset + 36);
  314. uint8_t hash_size = fa->get_8();
  315. uint8_t hash_type = fa->get_8();
  316. if (hash_size == 0x14 && hash_type == 0x01) { /* SHA-1 */
  317. PackedByteArray hash;
  318. hash.resize(0x14);
  319. fa->seek(get_signature_offset() + offset);
  320. PackedByteArray blob;
  321. blob.resize(cdsize);
  322. fa->get_buffer(blob.ptrw(), cdsize);
  323. CryptoCore::SHA1Context ctx;
  324. ctx.start();
  325. ctx.update(blob.ptr(), blob.size());
  326. ctx.finish(hash.ptrw());
  327. return hash;
  328. }
  329. }
  330. fa->seek(pos);
  331. }
  332. return PackedByteArray();
  333. }
  334. PackedByteArray MachO::get_cdhash_sha256() {
  335. ERR_FAIL_COND_V_MSG(fa.is_null(), PackedByteArray(), "MachO: File not opened.");
  336. if (signature_offset == 0) {
  337. return PackedByteArray();
  338. }
  339. fa->seek(get_signature_offset());
  340. uint32_t magic = BSWAP32(fa->get_32());
  341. if (magic != 0xfade0cc0) {
  342. return PackedByteArray(); // No SuperBlob found.
  343. }
  344. fa->get_32(); // Skip size field, unused.
  345. uint32_t count = BSWAP32(fa->get_32());
  346. for (uint32_t i = 0; i < count; i++) {
  347. fa->get_32(); // Index type, skip.
  348. uint32_t offset = BSWAP32(fa->get_32());
  349. uint64_t pos = fa->get_position();
  350. fa->seek(get_signature_offset() + offset);
  351. uint32_t cdmagic = BSWAP32(fa->get_32());
  352. uint32_t cdsize = BSWAP32(fa->get_32());
  353. if (cdmagic == 0xfade0c02) { // CodeDirectory.
  354. fa->seek(get_signature_offset() + offset + 36);
  355. uint8_t hash_size = fa->get_8();
  356. uint8_t hash_type = fa->get_8();
  357. if (hash_size == 0x20 && hash_type == 0x02) { /* SHA-256 */
  358. PackedByteArray hash;
  359. hash.resize(0x20);
  360. fa->seek(get_signature_offset() + offset);
  361. PackedByteArray blob;
  362. blob.resize(cdsize);
  363. fa->get_buffer(blob.ptrw(), cdsize);
  364. CryptoCore::SHA256Context ctx;
  365. ctx.start();
  366. ctx.update(blob.ptr(), blob.size());
  367. ctx.finish(hash.ptrw());
  368. return hash;
  369. }
  370. }
  371. fa->seek(pos);
  372. }
  373. return PackedByteArray();
  374. }
  375. PackedByteArray MachO::get_requirements() {
  376. ERR_FAIL_COND_V_MSG(fa.is_null(), PackedByteArray(), "MachO: File not opened.");
  377. if (signature_offset == 0) {
  378. return PackedByteArray();
  379. }
  380. fa->seek(get_signature_offset());
  381. uint32_t magic = BSWAP32(fa->get_32());
  382. if (magic != 0xfade0cc0) {
  383. return PackedByteArray(); // No SuperBlob found.
  384. }
  385. fa->get_32(); // Skip size field, unused.
  386. uint32_t count = BSWAP32(fa->get_32());
  387. for (uint32_t i = 0; i < count; i++) {
  388. fa->get_32(); // Index type, skip.
  389. uint32_t offset = BSWAP32(fa->get_32());
  390. uint64_t pos = fa->get_position();
  391. fa->seek(get_signature_offset() + offset);
  392. uint32_t rqmagic = BSWAP32(fa->get_32());
  393. uint32_t rqsize = BSWAP32(fa->get_32());
  394. if (rqmagic == 0xfade0c01) { // Requirements.
  395. PackedByteArray blob;
  396. fa->seek(get_signature_offset() + offset);
  397. blob.resize(rqsize);
  398. fa->get_buffer(blob.ptrw(), rqsize);
  399. return blob;
  400. }
  401. fa->seek(pos);
  402. }
  403. return PackedByteArray();
  404. }
  405. const Ref<FileAccess> MachO::get_file() const {
  406. return fa;
  407. }
  408. Ref<FileAccess> MachO::get_file() {
  409. return fa;
  410. }
  411. bool MachO::set_signature_size(uint64_t p_size) {
  412. ERR_FAIL_COND_V_MSG(fa.is_null(), false, "MachO: File not opened.");
  413. // Ensure signature load command exists.
  414. ERR_FAIL_COND_V_MSG(link_edit_offset == 0, false, "MachO: No __LINKEDIT segment found.");
  415. ERR_FAIL_COND_V_MSG(!alloc_signature(p_size), false, "MachO: Can't allocate signature load command.");
  416. // Update signature load command.
  417. uint64_t old_size = get_signature_size();
  418. uint64_t new_size = p_size + PAD(p_size, 16384);
  419. if (new_size <= old_size) {
  420. fa->seek(get_signature_offset());
  421. for (uint64_t i = 0; i < old_size; i++) {
  422. fa->store_8(0x00);
  423. }
  424. return true;
  425. }
  426. fa->seek(signature_offset + 12);
  427. if (swap) {
  428. fa->store_32(BSWAP32(new_size));
  429. } else {
  430. fa->store_32(new_size);
  431. }
  432. uint64_t end = get_signature_offset() + new_size;
  433. // Update "__LINKEDIT" segment.
  434. LoadCommandHeader lc;
  435. fa->seek(link_edit_offset);
  436. fa->get_buffer((uint8_t *)&lc, sizeof(LoadCommandHeader));
  437. if (swap) {
  438. lc.cmd = BSWAP32(lc.cmd);
  439. lc.cmdsize = BSWAP32(lc.cmdsize);
  440. }
  441. switch (lc.cmd) {
  442. case LC_SEGMENT: {
  443. LoadCommandSegment lc_seg;
  444. fa->get_buffer((uint8_t *)&lc_seg, sizeof(LoadCommandSegment));
  445. if (swap) {
  446. lc_seg.vmsize = BSWAP32(lc_seg.vmsize);
  447. lc_seg.filesize = BSWAP32(lc_seg.filesize);
  448. lc_seg.fileoff = BSWAP32(lc_seg.fileoff);
  449. }
  450. lc_seg.vmsize = end - lc_seg.fileoff;
  451. lc_seg.vmsize += PAD(lc_seg.vmsize, 4096);
  452. lc_seg.filesize = end - lc_seg.fileoff;
  453. if (swap) {
  454. lc_seg.vmsize = BSWAP32(lc_seg.vmsize);
  455. lc_seg.filesize = BSWAP32(lc_seg.filesize);
  456. }
  457. fa->seek(link_edit_offset + 8);
  458. fa->store_buffer((const uint8_t *)&lc_seg, sizeof(LoadCommandSegment));
  459. } break;
  460. case LC_SEGMENT_64: {
  461. LoadCommandSegment64 lc_seg;
  462. fa->get_buffer((uint8_t *)&lc_seg, sizeof(LoadCommandSegment64));
  463. if (swap) {
  464. lc_seg.vmsize = BSWAP64(lc_seg.vmsize);
  465. lc_seg.filesize = BSWAP64(lc_seg.filesize);
  466. lc_seg.fileoff = BSWAP64(lc_seg.fileoff);
  467. }
  468. lc_seg.vmsize = end - lc_seg.fileoff;
  469. lc_seg.vmsize += PAD(lc_seg.vmsize, 4096);
  470. lc_seg.filesize = end - lc_seg.fileoff;
  471. if (swap) {
  472. lc_seg.vmsize = BSWAP64(lc_seg.vmsize);
  473. lc_seg.filesize = BSWAP64(lc_seg.filesize);
  474. }
  475. fa->seek(link_edit_offset + 8);
  476. fa->store_buffer((const uint8_t *)&lc_seg, sizeof(LoadCommandSegment64));
  477. } break;
  478. default: {
  479. ERR_FAIL_V_MSG(false, "MachO: Invalid __LINKEDIT segment type.");
  480. } break;
  481. }
  482. fa->seek(get_signature_offset());
  483. for (uint64_t i = 0; i < new_size; i++) {
  484. fa->store_8(0x00);
  485. }
  486. return true;
  487. }