]> git.saurik.com Git - apple/xnu.git/blob - bsd/sys/codesign.h
xnu-3248.60.10.tar.gz
[apple/xnu.git] / bsd / sys / codesign.h
1 /*
2 * Copyright (c) 2006 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 _SYS_CODESIGN_H_
30 #define _SYS_CODESIGN_H_
31
32 /* code signing attributes of a process */
33 #define CS_VALID 0x0000001 /* dynamically valid */
34 #define CS_ADHOC 0x0000002 /* ad hoc signed */
35 #define CS_GET_TASK_ALLOW 0x0000004 /* has get-task-allow entitlement */
36 #define CS_INSTALLER 0x0000008 /* has installer entitlement */
37
38 #define CS_HARD 0x0000100 /* don't load invalid pages */
39 #define CS_KILL 0x0000200 /* kill process if it becomes invalid */
40 #define CS_CHECK_EXPIRATION 0x0000400 /* force expiration checking */
41 #define CS_RESTRICT 0x0000800 /* tell dyld to treat restricted */
42 #define CS_ENFORCEMENT 0x0001000 /* require enforcement */
43 #define CS_REQUIRE_LV 0x0002000 /* require library validation */
44 #define CS_ENTITLEMENTS_VALIDATED 0x0004000
45
46 #define CS_ALLOWED_MACHO 0x00ffffe
47
48 #define CS_EXEC_SET_HARD 0x0100000 /* set CS_HARD on any exec'ed process */
49 #define CS_EXEC_SET_KILL 0x0200000 /* set CS_KILL on any exec'ed process */
50 #define CS_EXEC_SET_ENFORCEMENT 0x0400000 /* set CS_ENFORCEMENT on any exec'ed process */
51 #define CS_EXEC_SET_INSTALLER 0x0800000 /* set CS_INSTALLER on any exec'ed process */
52
53 #define CS_KILLED 0x1000000 /* was killed by kernel for invalidity */
54 #define CS_DYLD_PLATFORM 0x2000000 /* dyld used to load this is a platform binary */
55 #define CS_PLATFORM_BINARY 0x4000000 /* this is a platform binary */
56 #define CS_PLATFORM_PATH 0x8000000 /* platform binary by the fact of path (osx only) */
57 #define CS_DEBUGGED 0x10000000 /* process is currently or has previously been debugged and allowed to run with invalid pages */
58
59 #define CS_ENTITLEMENT_FLAGS (CS_GET_TASK_ALLOW | CS_INSTALLER)
60
61 /* MAC flags used by F_ADDFILESIGS_* */
62 #define MAC_VNODE_CHECK_DYLD_SIM 0x1 /* tells the MAC framework that dyld-sim is being loaded */
63
64 /* csops operations */
65 #define CS_OPS_STATUS 0 /* return status */
66 #define CS_OPS_MARKINVALID 1 /* invalidate process */
67 #define CS_OPS_MARKHARD 2 /* set HARD flag */
68 #define CS_OPS_MARKKILL 3 /* set KILL flag (sticky) */
69 #ifdef KERNEL_PRIVATE
70 /* CS_OPS_PIDPATH 4 */
71 #endif
72 #define CS_OPS_CDHASH 5 /* get code directory hash */
73 #define CS_OPS_PIDOFFSET 6 /* get offset of active Mach-o slice */
74 #define CS_OPS_ENTITLEMENTS_BLOB 7 /* get entitlements blob */
75 #define CS_OPS_MARKRESTRICT 8 /* set RESTRICT flag (sticky) */
76 #define CS_OPS_SET_STATUS 9 /* set codesign flags */
77 #define CS_OPS_BLOB 10 /* get codesign blob */
78 #define CS_OPS_IDENTITY 11 /* get codesign identity */
79
80 /*
81 * Magic numbers used by Code Signing
82 */
83 enum {
84 CSMAGIC_REQUIREMENT = 0xfade0c00, /* single Requirement blob */
85 CSMAGIC_REQUIREMENTS = 0xfade0c01, /* Requirements vector (internal requirements) */
86 CSMAGIC_CODEDIRECTORY = 0xfade0c02, /* CodeDirectory blob */
87 CSMAGIC_EMBEDDED_SIGNATURE = 0xfade0cc0, /* embedded form of signature data */
88 CSMAGIC_EMBEDDED_SIGNATURE_OLD = 0xfade0b02, /* XXX */
89 CSMAGIC_EMBEDDED_ENTITLEMENTS = 0xfade7171, /* embedded entitlements */
90 CSMAGIC_DETACHED_SIGNATURE = 0xfade0cc1, /* multi-arch collection of embedded signatures */
91 CSMAGIC_BLOBWRAPPER = 0xfade0b01, /* CMS Signature, among other things */
92
93 CS_SUPPORTSSCATTER = 0x20100,
94 CS_SUPPORTSTEAMID = 0x20200,
95
96 CSSLOT_CODEDIRECTORY = 0, /* slot index for CodeDirectory */
97 CSSLOT_INFOSLOT = 1,
98 CSSLOT_REQUIREMENTS = 2,
99 CSSLOT_RESOURCEDIR = 3,
100 CSSLOT_APPLICATION = 4,
101 CSSLOT_ENTITLEMENTS = 5,
102
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 = CSSLOT_ALTERNATE_CODEDIRECTORIES + CSSLOT_ALTERNATE_CODEDIRECTORY_MAX, /* one past the last */
106
107 CSSLOT_SIGNATURESLOT = 0x10000, /* CMS Signature */
108
109 CSTYPE_INDEX_REQUIREMENTS = 0x00000002, /* compat with amfi */
110 CSTYPE_INDEX_ENTITLEMENTS = 0x00000005, /* compat with amfi */
111
112 CS_HASHTYPE_SHA1 = 1,
113 CS_HASHTYPE_SHA256 = 2,
114 CS_HASHTYPE_SHA256_TRUNCATED = 3,
115 CS_HASHTYPE_SHA384 = 4,
116
117 CS_SHA1_LEN = 20,
118 CS_SHA256_TRUNCATED_LEN = 20,
119
120 CS_CDHASH_LEN = 20, /* always - larger hashes are truncated */
121 CS_HASH_MAX_SIZE = 48, /* max size of the hash we'll support */
122 };
123
124
125 #define KERNEL_HAVE_CS_CODEDIRECTORY 1
126 #define KERNEL_CS_CODEDIRECTORY_HAVE_PLATFORM 1
127
128 /*
129 * C form of a CodeDirectory.
130 */
131 typedef struct __CodeDirectory {
132 uint32_t magic; /* magic number (CSMAGIC_CODEDIRECTORY) */
133 uint32_t length; /* total length of CodeDirectory blob */
134 uint32_t version; /* compatibility version */
135 uint32_t flags; /* setup and mode flags */
136 uint32_t hashOffset; /* offset of hash slot element at index zero */
137 uint32_t identOffset; /* offset of identifier string */
138 uint32_t nSpecialSlots; /* number of special hash slots */
139 uint32_t nCodeSlots; /* number of ordinary (code) hash slots */
140 uint32_t codeLimit; /* limit to main image signature range */
141 uint8_t hashSize; /* size of each hash in bytes */
142 uint8_t hashType; /* type of hash (cdHashType* constants) */
143 uint8_t platform; /* platform identifier; zero if not platform binary */
144 uint8_t pageSize; /* log2(page size in bytes); 0 => infinite */
145 uint32_t spare2; /* unused (must be zero) */
146 /* Version 0x20100 */
147 uint32_t scatterOffset; /* offset of optional scatter vector */
148 /* Version 0x20200 */
149 uint32_t teamOffset; /* offset of optional team identifier */
150 /* followed by dynamic content as located by offset fields above */
151 } CS_CodeDirectory;
152
153 /*
154 * Structure of an embedded-signature SuperBlob
155 */
156
157 typedef struct __BlobIndex {
158 uint32_t type; /* type of entry */
159 uint32_t offset; /* offset of entry */
160 } CS_BlobIndex;
161
162 typedef struct __SC_SuperBlob {
163 uint32_t magic; /* magic number */
164 uint32_t length; /* total length of SuperBlob */
165 uint32_t count; /* number of index entries following */
166 CS_BlobIndex index[]; /* (count) entries */
167 /* followed by Blobs in no particular order as indicated by offsets in index */
168 } CS_SuperBlob;
169
170 #define KERNEL_HAVE_CS_GENERICBLOB 1
171 typedef struct __SC_GenericBlob {
172 uint32_t magic; /* magic number */
173 uint32_t length; /* total length of blob */
174 char data[];
175 } CS_GenericBlob;
176
177 typedef struct __SC_Scatter {
178 uint32_t count; // number of pages; zero for sentinel (only)
179 uint32_t base; // first page number
180 uint64_t targetOffset; // offset in target
181 uint64_t spare; // reserved
182 } SC_Scatter;
183
184
185 #ifndef KERNEL
186
187 #include <sys/types.h>
188 #include <mach/message.h>
189
190 __BEGIN_DECLS
191 /* code sign operations */
192 int csops(pid_t pid, unsigned int ops, void * useraddr, size_t usersize);
193 int csops_audittoken(pid_t pid, unsigned int ops, void * useraddr, size_t usersize, audit_token_t * token);
194 __END_DECLS
195
196 #else /* !KERNEL */
197
198 #include <sys/cdefs.h>
199 #include <sys/_types/_off_t.h>
200
201 struct vnode;
202 struct cs_blob;
203 struct fileglob;
204
205 __BEGIN_DECLS
206 int cs_enforcement(struct proc *);
207 int cs_require_lv(struct proc *);
208 uint32_t cs_entitlement_flags(struct proc *p);
209 int cs_entitlements_blob_get(struct proc *, void **, size_t *);
210 int cs_restricted(struct proc *);
211 uint8_t * cs_get_cdhash(struct proc *);
212
213 struct cs_blob * csproc_get_blob(struct proc *);
214 struct cs_blob * csvnode_get_blob(struct vnode *, off_t);
215 void csvnode_print_debug(struct vnode *);
216
217 const char * csblob_get_teamid(struct cs_blob *);
218 const char * csblob_get_identity(struct cs_blob *);
219 const uint8_t * csblob_get_cdhash(struct cs_blob *);
220 int csblob_get_platform_binary(struct cs_blob *);
221 unsigned int csblob_get_flags(struct cs_blob *blob);
222 int csblob_get_entitlements(struct cs_blob *, void **, size_t *);
223 const CS_GenericBlob *
224 csblob_find_blob(struct cs_blob *, uint32_t, uint32_t);
225 const CS_GenericBlob *
226 csblob_find_blob_bytes(const uint8_t *, size_t, uint32_t, uint32_t);
227
228 /*
229 * Mostly convenience functions below
230 */
231
232 const char * csproc_get_teamid(struct proc *);
233 const char * csvnode_get_teamid(struct vnode *, off_t);
234 int csproc_get_platform_binary(struct proc *);
235 const char * csfg_get_teamid(struct fileglob *);
236 int csfg_get_path(struct fileglob *, char *, int *);
237 int csfg_get_platform_binary(struct fileglob *);
238 uint8_t * csfg_get_cdhash(struct fileglob *, uint64_t, size_t *);
239
240 extern int cs_debug;
241
242 #ifdef XNU_KERNEL_PRIVATE
243
244 void cs_init(void);
245 int cs_allow_invalid(struct proc *);
246 int cs_invalid_page(addr64_t);
247 int csproc_get_platform_path(struct proc *);
248
249 #if !SECURE_KERNEL
250 extern int cs_enforcement_panic;
251 #endif
252
253 #endif /* XNU_KERNEL_PRIVATE */
254
255
256 __END_DECLS
257
258
259
260 #endif /* KERNEL */
261
262 #endif /* _SYS_CODESIGN_H_ */