cs_blobs.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // credits @stek29 - https://github.com/stek29/electra/blob/amfid_fix/basebinaries/amfid_payload/
  2. // from: xnu osfmk/kern/cs_blobs.h
  3. #define CS_VALID 0x0000001 /* dynamically valid */
  4. #define CS_ADHOC 0x0000002 /* ad hoc signed */
  5. #define CS_GET_TASK_ALLOW 0x0000004 /* has get-task-allow entitlement */
  6. #define CS_INSTALLER 0x0000008 /* has installer entitlement */
  7. #define CS_HARD 0x0000100 /* don't load invalid pages */
  8. #define CS_KILL 0x0000200 /* kill process if it becomes invalid */
  9. #define CS_CHECK_EXPIRATION 0x0000400 /* force expiration checking */
  10. #define CS_RESTRICT 0x0000800 /* tell dyld to treat restricted */
  11. #define CS_ENFORCEMENT 0x0001000 /* require enforcement */
  12. #define CS_REQUIRE_LV 0x0002000 /* require library validation */
  13. #define CS_ENTITLEMENTS_VALIDATED 0x0004000 /* code signature permits restricted entitlements */
  14. #define CS_ALLOWED_MACHO (CS_ADHOC | CS_HARD | CS_KILL | CS_CHECK_EXPIRATION | CS_RESTRICT | CS_ENFORCEMENT | CS_REQUIRE_LV)
  15. #define CS_EXEC_SET_HARD 0x0100000 /* set CS_HARD on any exec'ed process */
  16. #define CS_EXEC_SET_KILL 0x0200000 /* set CS_KILL on any exec'ed process */
  17. #define CS_EXEC_SET_ENFORCEMENT 0x0400000 /* set CS_ENFORCEMENT on any exec'ed process */
  18. #define CS_EXEC_SET_INSTALLER 0x0800000 /* set CS_INSTALLER on any exec'ed process */
  19. #define CS_KILLED 0x1000000 /* was killed by kernel for invalidity */
  20. #define CS_DYLD_PLATFORM 0x2000000 /* dyld used to load this is a platform binary */
  21. #define CS_PLATFORM_BINARY 0x4000000 /* this is a platform binary */
  22. #define CS_PLATFORM_PATH 0x8000000 /* platform binary by the fact of path (osx only) */
  23. #define CS_DEBUGGED 0x10000000 /* process is currently or has previously been debugged and allowed to run with invalid pages */
  24. #define CS_SIGNED 0x20000000 /* process has a signature (may have gone invalid) */
  25. #define CS_DEV_CODE 0x40000000 /* code is dev signed, cannot be loaded into prod signed code (will go away with rdar://problem/28322552) */
  26. #define CS_ENTITLEMENT_FLAGS (CS_GET_TASK_ALLOW | CS_INSTALLER)
  27. typedef struct __attribute__((packed)) {
  28. uint32_t magic; /* magic number (CSMAGIC_CODEDIRECTORY) */
  29. uint32_t length; /* total length of CodeDirectory blob */
  30. uint32_t version; /* compatibility version */
  31. uint32_t flags; /* setup and mode flags */
  32. uint32_t hashOffset; /* offset of hash slot element at index zero */
  33. uint32_t identOffset; /* offset of identifier string */
  34. uint32_t nSpecialSlots; /* number of special hash slots */
  35. uint32_t nCodeSlots; /* number of ordinary (code) hash slots */
  36. uint32_t codeLimit; /* limit to main image signature range */
  37. uint8_t hashSize; /* size of each hash in bytes */
  38. uint8_t hashType; /* type of hash (cdHashType* constants) */
  39. uint8_t platform; /* platform identifier; zero if not platform binary */
  40. uint8_t pageSize; /* log2(page size in bytes); 0 => infinite */
  41. uint32_t spare2; /* unused (must be zero) */
  42. char end_earliest[0];
  43. /* Version 0x20100 */
  44. uint32_t scatterOffset; /* offset of optional scatter vector */
  45. char end_withScatter[0];
  46. /* Version 0x20200 */
  47. uint32_t teamOffset; /* offset of optional team identifier */
  48. char end_withTeam[0];
  49. /* Version 0x20300 */
  50. uint32_t spare3; /* unused (must be zero) */
  51. uint64_t codeLimit64; /* limit to main image signature range, 64 bits */
  52. char end_withCodeLimit64[0];
  53. /* Version 0x20400 */
  54. uint64_t execSegBase; /* offset of executable segment */
  55. uint64_t execSegLimit; /* limit of executable segment */
  56. uint64_t execSegFlags; /* executable segment flags */
  57. char end_withExecSeg[0];
  58. } CS_CodeDirectory;
  59. typedef struct __attribute__((packed)) {
  60. uint32_t type; /* type of entry */
  61. uint32_t offset; /* offset of entry */
  62. } CS_BlobIndex;
  63. typedef struct __attribute__((packed)) {
  64. uint32_t magic; /* magic number */
  65. uint32_t length; /* total length of SuperBlob */
  66. uint32_t count; /* number of index entries following */
  67. CS_BlobIndex index[]; /* (count) entries */
  68. /* followed by Blobs in no particular order as indicated by offsets in index */
  69. } CS_SuperBlob;
  70. typedef struct __attribute__((packed)) {
  71. uint32_t magic; /* magic number */
  72. uint32_t length; /* total length of blob */
  73. char data[];
  74. } CS_GenericBlob;
  75. typedef struct __SC_Scatter {
  76. uint32_t count; // number of pages; zero for sentinel (only)
  77. uint32_t base; // first page number
  78. uint64_t targetOffset; // offset in target
  79. uint64_t spare; // reserved
  80. } SC_Scatter;
  81. /*
  82. * Magic numbers used by Code Signing
  83. */
  84. enum {
  85. CSMAGIC_REQUIREMENT = 0xfade0c00, /* single Requirement blob */
  86. CSMAGIC_REQUIREMENTS = 0xfade0c01, /* Requirements vector (internal requirements) */
  87. CSMAGIC_CODEDIRECTORY = 0xfade0c02, /* CodeDirectory blob */
  88. CSMAGIC_EMBEDDED_SIGNATURE = 0xfade0cc0, /* embedded form of signature data */
  89. CSMAGIC_EMBEDDED_SIGNATURE_OLD = 0xfade0b02, /* XXX */
  90. CSMAGIC_EMBEDDED_ENTITLEMENTS = 0xfade7171, /* embedded entitlements */
  91. CSMAGIC_DETACHED_SIGNATURE = 0xfade0cc1, /* multi-arch collection of embedded signatures */
  92. CSMAGIC_BLOBWRAPPER = 0xfade0b01, /* CMS Signature, among other things */
  93. CS_SUPPORTSSCATTER = 0x20100,
  94. CS_SUPPORTSTEAMID = 0x20200,
  95. CS_SUPPORTSCODELIMIT64 = 0x20300,
  96. CS_SUPPORTSEXECSEG = 0x20400,
  97. CSSLOT_CODEDIRECTORY = 0, /* slot index for CodeDirectory */
  98. CSSLOT_INFOSLOT = 1,
  99. CSSLOT_REQUIREMENTS = 2,
  100. CSSLOT_RESOURCEDIR = 3,
  101. CSSLOT_APPLICATION = 4,
  102. CSSLOT_ENTITLEMENTS = 5,
  103. CSSLOT_ALTERNATE_CODEDIRECTORIES = 0x1000, /* first alternate CodeDirectory, if any */
  104. CSSLOT_ALTERNATE_CODEDIRECTORY_MAX = 5, /* max number of alternate CD slots */
  105. CSSLOT_ALTERNATE_CODEDIRECTORY_LIMIT = /* one past the last */
  106. CSSLOT_ALTERNATE_CODEDIRECTORIES +
  107. CSSLOT_ALTERNATE_CODEDIRECTORY_MAX,
  108. CSSLOT_SIGNATURESLOT = 0x10000, /* CMS Signature */
  109. CSTYPE_INDEX_REQUIREMENTS = 0x00000002, /* compat with amfi */
  110. CSTYPE_INDEX_ENTITLEMENTS = 0x00000005, /* compat with amfi */
  111. CS_HASHTYPE_SHA1 = 1,
  112. CS_HASHTYPE_SHA256 = 2,
  113. CS_HASHTYPE_SHA256_TRUNCATED = 3,
  114. CS_HASHTYPE_SHA384 = 4,
  115. CS_SHA1_LEN = 20,
  116. CS_SHA256_LEN = 32,
  117. CS_SHA256_TRUNCATED_LEN = 20,
  118. CS_CDHASH_LEN = 20, /* always - larger hashes are truncated */
  119. CS_HASH_MAX_SIZE = 48, /* max size of the hash we'll support */
  120. /*
  121. * Currently only to support Legacy VPN plugins,
  122. * but intended to replace all the various platform code, dev code etc. bits.
  123. */
  124. CS_SIGNER_TYPE_UNKNOWN = 0,
  125. CS_SIGNER_TYPE_LEGACYVPN = 5,
  126. };
  127. /*
  128. * Choose among different hash algorithms.
  129. * Higher is better, 0 => don't use at all.
  130. */
  131. static const uint32_t hashPriorities[] = {
  132. CS_HASHTYPE_SHA1,
  133. CS_HASHTYPE_SHA256_TRUNCATED,
  134. CS_HASHTYPE_SHA256,
  135. CS_HASHTYPE_SHA384,
  136. };