xref: /xnu-11215/osfmk/kern/cs_blobs.h (revision 1031c584)
1 /*
2  * Copyright (c) 2017 Apple Computer, Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 
29 #ifndef _KERN_CODESIGN_H_
30 #define _KERN_CODESIGN_H_
31 
32 #include <stdint.h>
33 #include <string.h>
34 
35 /* code signing attributes of a process */
36 #define CS_VALID                    0x00000001  /* dynamically valid */
37 #define CS_ADHOC                    0x00000002  /* ad hoc signed */
38 #define CS_GET_TASK_ALLOW           0x00000004  /* has get-task-allow entitlement */
39 #define CS_INSTALLER                0x00000008  /* has installer entitlement */
40 
41 #define CS_FORCED_LV                0x00000010  /* Library Validation required by Hardened System Policy */
42 #define CS_INVALID_ALLOWED          0x00000020  /* (macOS Only) Page invalidation allowed by task port policy */
43 
44 #define CS_HARD                     0x00000100  /* don't load invalid pages */
45 #define CS_KILL                     0x00000200  /* kill process if it becomes invalid */
46 #define CS_CHECK_EXPIRATION         0x00000400  /* force expiration checking */
47 #define CS_RESTRICT                 0x00000800  /* tell dyld to treat restricted */
48 
49 #define CS_ENFORCEMENT              0x00001000  /* require enforcement */
50 #define CS_REQUIRE_LV               0x00002000  /* require library validation */
51 #define CS_ENTITLEMENTS_VALIDATED   0x00004000  /* code signature permits restricted entitlements */
52 #define CS_NVRAM_UNRESTRICTED       0x00008000  /* has com.apple.rootless.restricted-nvram-variables.heritable entitlement */
53 
54 #define CS_RUNTIME                  0x00010000  /* Apply hardened runtime policies */
55 #define CS_LINKER_SIGNED            0x00020000  /* Automatically signed by the linker */
56 
57 #define CS_ALLOWED_MACHO            (CS_ADHOC | CS_HARD | CS_KILL | CS_CHECK_EXPIRATION | \
58 	                             CS_RESTRICT | CS_ENFORCEMENT | CS_REQUIRE_LV | CS_RUNTIME | CS_LINKER_SIGNED)
59 
60 #define CS_EXEC_SET_HARD            0x00100000  /* set CS_HARD on any exec'ed process */
61 #define CS_EXEC_SET_KILL            0x00200000  /* set CS_KILL on any exec'ed process */
62 #define CS_EXEC_SET_ENFORCEMENT     0x00400000  /* set CS_ENFORCEMENT on any exec'ed process */
63 #define CS_EXEC_INHERIT_SIP         0x00800000  /* set CS_INSTALLER on any exec'ed process */
64 
65 #define CS_KILLED                   0x01000000  /* was killed by kernel for invalidity */
66 #define CS_NO_UNTRUSTED_HELPERS     0x02000000  /* kernel did not load a non-platform-binary dyld or Rosetta runtime */
67 #define CS_DYLD_PLATFORM            CS_NO_UNTRUSTED_HELPERS /* old name */
68 #define CS_PLATFORM_BINARY          0x04000000  /* this is a platform binary */
69 #define CS_PLATFORM_PATH            0x08000000  /* platform binary by the fact of path (osx only) */
70 
71 #define CS_DEBUGGED                 0x10000000  /* process is currently or has previously been debugged and allowed to run with invalid pages */
72 #define CS_SIGNED                   0x20000000  /* process has a signature (may have gone invalid) */
73 #define CS_DEV_CODE                 0x40000000  /* code is dev signed, cannot be loaded into prod signed code (will go away with rdar://problem/28322552) */
74 #define CS_DATAVAULT_CONTROLLER     0x80000000  /* has Data Vault controller entitlement */
75 
76 #define CS_ENTITLEMENT_FLAGS        (CS_GET_TASK_ALLOW | CS_INSTALLER | CS_DATAVAULT_CONTROLLER | CS_NVRAM_UNRESTRICTED)
77 
78 /* executable segment flags */
79 
80 #define CS_EXECSEG_MAIN_BINARY          0x1             /* executable segment denotes main binary */
81 #define CS_EXECSEG_ALLOW_UNSIGNED       0x10            /* allow unsigned pages (for debugging) */
82 #define CS_EXECSEG_DEBUGGER             0x20            /* main binary is debugger */
83 #define CS_EXECSEG_JIT                  0x40            /* JIT enabled */
84 #define CS_EXECSEG_SKIP_LV              0x80            /* OBSOLETE: skip library validation */
85 #define CS_EXECSEG_CAN_LOAD_CDHASH      0x100           /* can bless cdhash for execution */
86 #define CS_EXECSEG_CAN_EXEC_CDHASH      0x200           /* can execute blessed cdhash */
87 
88 /*
89  * Magic numbers used by Code Signing
90  */
91 enum {
92 	CSMAGIC_REQUIREMENT = 0xfade0c00,               /* single Requirement blob */
93 	CSMAGIC_REQUIREMENTS = 0xfade0c01,              /* Requirements vector (internal requirements) */
94 	CSMAGIC_CODEDIRECTORY = 0xfade0c02,             /* CodeDirectory blob */
95 	CSMAGIC_EMBEDDED_SIGNATURE = 0xfade0cc0, /* embedded form of signature data */
96 	CSMAGIC_EMBEDDED_SIGNATURE_OLD = 0xfade0b02,    /* XXX */
97 	CSMAGIC_EMBEDDED_ENTITLEMENTS = 0xfade7171,     /* embedded entitlements */
98 	CSMAGIC_EMBEDDED_DER_ENTITLEMENTS = 0xfade7172, /* embedded DER encoded entitlements */
99 	CSMAGIC_DETACHED_SIGNATURE = 0xfade0cc1, /* multi-arch collection of embedded signatures */
100 	CSMAGIC_BLOBWRAPPER = 0xfade0b01,       /* CMS Signature, among other things */
101 	CSMAGIC_EMBEDDED_LAUNCH_CONSTRAINT = 0xfade8181, /* Light weight code requirement */
102 
103 	CS_SUPPORTSSCATTER = 0x20100,
104 	CS_SUPPORTSTEAMID = 0x20200,
105 	CS_SUPPORTSCODELIMIT64 = 0x20300,
106 	CS_SUPPORTSEXECSEG = 0x20400,
107 	CS_SUPPORTSRUNTIME = 0x20500,
108 	CS_SUPPORTSLINKAGE = 0x20600,
109 
110 	CSSLOT_CODEDIRECTORY = 0,                               /* slot index for CodeDirectory */
111 	CSSLOT_INFOSLOT = 1,
112 	CSSLOT_REQUIREMENTS = 2,
113 	CSSLOT_RESOURCEDIR = 3,
114 	CSSLOT_APPLICATION = 4,
115 	CSSLOT_ENTITLEMENTS = 5,
116 	CSSLOT_DER_ENTITLEMENTS = 7,
117 	CSSLOT_LAUNCH_CONSTRAINT_SELF = 8,
118 	CSSLOT_LAUNCH_CONSTRAINT_PARENT = 9,
119 	CSSLOT_LAUNCH_CONSTRAINT_RESPONSIBLE = 10,
120 	CSSLOT_LIBRARY_CONSTRAINT = 11,
121 
122 	CSSLOT_ALTERNATE_CODEDIRECTORIES = 0x1000, /* first alternate CodeDirectory, if any */
123 	CSSLOT_ALTERNATE_CODEDIRECTORY_MAX = 5,         /* max number of alternate CD slots */
124 	CSSLOT_ALTERNATE_CODEDIRECTORY_LIMIT = CSSLOT_ALTERNATE_CODEDIRECTORIES + CSSLOT_ALTERNATE_CODEDIRECTORY_MAX, /* one past the last */
125 
126 	CSSLOT_SIGNATURESLOT = 0x10000,                 /* CMS Signature */
127 	CSSLOT_IDENTIFICATIONSLOT = 0x10001,
128 	CSSLOT_TICKETSLOT = 0x10002,
129 
130 	CSTYPE_INDEX_REQUIREMENTS = 0x00000002,         /* compat with amfi */
131 	CSTYPE_INDEX_ENTITLEMENTS = 0x00000005,         /* compat with amfi */
132 
133 	CS_HASHTYPE_SHA1 = 1,
134 	CS_HASHTYPE_SHA256 = 2,
135 	CS_HASHTYPE_SHA256_TRUNCATED = 3,
136 	CS_HASHTYPE_SHA384 = 4,
137 
138 	CS_SHA1_LEN = 20,
139 	CS_SHA256_LEN = 32,
140 	CS_SHA256_TRUNCATED_LEN = 20,
141 
142 	CS_CDHASH_LEN = 20,                                             /* always - larger hashes are truncated */
143 	CS_HASH_MAX_SIZE = 48, /* max size of the hash we'll support */
144 
145 /*
146  * Currently only to support Legacy VPN plugins, and Mac App Store
147  * but intended to replace all the various platform code, dev code etc. bits.
148  */
149 	CS_SIGNER_TYPE_UNKNOWN = 0,
150 	CS_SIGNER_TYPE_LEGACYVPN = 5,
151 	CS_SIGNER_TYPE_MAC_APP_STORE = 6,
152 
153 	CS_SUPPL_SIGNER_TYPE_UNKNOWN = 0,
154 	CS_SUPPL_SIGNER_TYPE_TRUSTCACHE = 7,
155 	CS_SUPPL_SIGNER_TYPE_LOCAL = 8,
156 
157 	CS_SIGNER_TYPE_OOPJIT = 9,
158 
159 	/* Validation categories used for trusted launch environment */
160 	CS_VALIDATION_CATEGORY_INVALID = 0,
161 	CS_VALIDATION_CATEGORY_PLATFORM = 1,
162 	CS_VALIDATION_CATEGORY_TESTFLIGHT = 2,
163 	CS_VALIDATION_CATEGORY_DEVELOPMENT = 3,
164 	CS_VALIDATION_CATEGORY_APP_STORE = 4,
165 	CS_VALIDATION_CATEGORY_ENTERPRISE = 5,
166 	CS_VALIDATION_CATEGORY_DEVELOPER_ID = 6,
167 	CS_VALIDATION_CATEGORY_LOCAL_SIGNING = 7,
168 	CS_VALIDATION_CATEGORY_ROSETTA = 8,
169 	CS_VALIDATION_CATEGORY_OOPJIT = 9,
170 	CS_VALIDATION_CATEGORY_NONE = 10,
171 };
172 
173 /* The set of application types we support for linkage signatures */
174 enum {
175 	CS_LINKAGE_APPLICATION_INVALID = 0,
176 	CS_LINKAGE_APPLICATION_ROSETTA = 1,
177 
178 	/* XOJIT has been renamed to OOP-JIT */
179 	CS_LINKAGE_APPLICATION_XOJIT = 2,
180 	CS_LINKAGE_APPLICATION_OOPJIT = 2,
181 };
182 
183 /* The set of application sub-types we support for linkage signatures */
184 enum {
185 	/*
186 	 * For backwards compatibility with older signatures, the AOT sub-type is kept
187 	 * as 0.
188 	 */
189 	CS_LINKAGE_APPLICATION_ROSETTA_AOT = 0,
190 
191 	/* OOP-JIT sub-types -- XOJIT type kept for external dependencies */
192 	CS_LINKAGE_APPLICATION_XOJIT_PREVIEWS = 1,
193 	CS_LINKAGE_APPLICATION_OOPJIT_INVALID = 0,
194 	CS_LINKAGE_APPLICATION_OOPJIT_PREVIEWS = 1,
195 	CS_LINKAGE_APPLICATION_OOPJIT_MLCOMPILER = 2,
196 	CS_LINKAGE_APPLICATION_OOPJIT_TOTAL,
197 };
198 
199 /* Integer to string conversion of OOP-JIT types */
200 static const char *oop_jit_conversion[CS_LINKAGE_APPLICATION_OOPJIT_TOTAL] = {
201 	[CS_LINKAGE_APPLICATION_OOPJIT_INVALID] = NULL,
202 	[CS_LINKAGE_APPLICATION_OOPJIT_PREVIEWS] = "previews",
203 	[CS_LINKAGE_APPLICATION_OOPJIT_MLCOMPILER] = "ml-compiler",
204 };
205 
206 #define KERNEL_HAVE_CS_CODEDIRECTORY 1
207 #define KERNEL_CS_CODEDIRECTORY_HAVE_PLATFORM 1
208 
209 /*
210  * C form of a CodeDirectory.
211  */
212 typedef struct __CodeDirectory {
213 	uint32_t magic;                                 /* magic number (CSMAGIC_CODEDIRECTORY) */
214 	uint32_t length;                                /* total length of CodeDirectory blob */
215 	uint32_t version;                               /* compatibility version */
216 	uint32_t flags;                                 /* setup and mode flags */
217 	uint32_t hashOffset;                    /* offset of hash slot element at index zero */
218 	uint32_t identOffset;                   /* offset of identifier string */
219 	uint32_t nSpecialSlots;                 /* number of special hash slots */
220 	uint32_t nCodeSlots;                    /* number of ordinary (code) hash slots */
221 	uint32_t codeLimit;                             /* limit to main image signature range */
222 	uint8_t hashSize;                               /* size of each hash in bytes */
223 	uint8_t hashType;                               /* type of hash (cdHashType* constants) */
224 	uint8_t platform;                               /* platform identifier; zero if not platform binary */
225 	uint8_t pageSize;                               /* log2(page size in bytes); 0 => infinite */
226 	uint32_t spare2;                                /* unused (must be zero) */
227 
228 	char end_earliest[0];
229 
230 	/* Version 0x20100 */
231 	uint32_t scatterOffset;                 /* offset of optional scatter vector */
232 	char end_withScatter[0];
233 
234 	/* Version 0x20200 */
235 	uint32_t teamOffset;                    /* offset of optional team identifier */
236 	char end_withTeam[0];
237 
238 	/* Version 0x20300 */
239 	uint32_t spare3;                                /* unused (must be zero) */
240 	uint64_t codeLimit64;                   /* limit to main image signature range, 64 bits */
241 	char end_withCodeLimit64[0];
242 
243 	/* Version 0x20400 */
244 	uint64_t execSegBase;                   /* offset of executable segment */
245 	uint64_t execSegLimit;                  /* limit of executable segment */
246 	uint64_t execSegFlags;                  /* executable segment flags */
247 	char end_withExecSeg[0];
248 
249 	/* Version 0x20500 */
250 	uint32_t runtime;
251 	uint32_t preEncryptOffset;
252 	char end_withPreEncryptOffset[0];
253 
254 	/* Version 0x20600 */
255 	uint8_t linkageHashType;
256 	uint8_t linkageApplicationType;
257 	uint16_t linkageApplicationSubType;
258 	uint32_t linkageOffset;
259 	uint32_t linkageSize;
260 	char end_withLinkage[0];
261 
262 	/* followed by dynamic content as located by offset fields above */
263 } CS_CodeDirectory
264 __attribute__ ((aligned(1)));
265 
266 /*
267  * Structure of an embedded-signature SuperBlob
268  */
269 
270 typedef struct __BlobIndex {
271 	uint32_t type;                                  /* type of entry */
272 	uint32_t offset;                                /* offset of entry */
273 } CS_BlobIndex
274 __attribute__ ((aligned(1)));
275 
276 typedef struct __SC_SuperBlob {
277 	uint32_t magic;                                 /* magic number */
278 	uint32_t length;                                /* total length of SuperBlob */
279 	uint32_t count;                                 /* number of index entries following */
280 	CS_BlobIndex index[];                   /* (count) entries */
281 	/* followed by Blobs in no particular order as indicated by offsets in index */
282 } CS_SuperBlob
283 __attribute__ ((aligned(1)));
284 
285 #define KERNEL_HAVE_CS_GENERICBLOB 1
286 typedef struct __SC_GenericBlob {
287 	uint32_t magic;                                 /* magic number */
288 	uint32_t length;                                /* total length of blob */
289 	char data[];
290 } CS_GenericBlob
291 __attribute__ ((aligned(1)));
292 
293 typedef struct __SC_Scatter {
294 	uint32_t count;                                 // number of pages; zero for sentinel (only)
295 	uint32_t base;                                  // first page number
296 	uint64_t targetOffset;                  // offset in target
297 	uint64_t spare;                                 // reserved
298 } SC_Scatter
299 __attribute__ ((aligned(1)));
300 
301 
302 /*
303  * Defined launch types
304  */
305 __enum_decl(cs_launch_type_t, uint8_t, {
306 	CS_LAUNCH_TYPE_NONE = 0,
307 	CS_LAUNCH_TYPE_SYSTEM_SERVICE = 1,
308 	CS_LAUNCH_TYPE_SYSDIAGNOSE = 2,
309 	CS_LAUNCH_TYPE_APPLICATION = 3,
310 });
311 
312 struct launch_constraint_data {
313 	cs_launch_type_t launch_type;
314 };
315 typedef struct launch_constraint_data* launch_constraint_data_t;
316 
317 #endif /* _KERN_CODESIGN_H */
318