]> git.saurik.com Git - apple/xnu.git/blob - bsd/sys/codesign.h
xnu-2782.40.9.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
56 #define CS_ENTITLEMENT_FLAGS (CS_GET_TASK_ALLOW | CS_INSTALLER)
57
58 /* MAC flags used by F_ADDFILESIGS_* */
59 #define MAC_VNODE_CHECK_DYLD_SIM 0x1 /* tells the MAC framework that dyld-sim is being loaded */
60
61 /* csops operations */
62 #define CS_OPS_STATUS 0 /* return status */
63 #define CS_OPS_MARKINVALID 1 /* invalidate process */
64 #define CS_OPS_MARKHARD 2 /* set HARD flag */
65 #define CS_OPS_MARKKILL 3 /* set KILL flag (sticky) */
66 #ifdef KERNEL_PRIVATE
67 /* CS_OPS_PIDPATH 4 */
68 #endif
69 #define CS_OPS_CDHASH 5 /* get code directory hash */
70 #define CS_OPS_PIDOFFSET 6 /* get offset of active Mach-o slice */
71 #define CS_OPS_ENTITLEMENTS_BLOB 7 /* get entitlements blob */
72 #define CS_OPS_MARKRESTRICT 8 /* set RESTRICT flag (sticky) */
73 #define CS_OPS_SET_STATUS 9 /* set codesign flags */
74 #define CS_OPS_BLOB 10 /* get codesign blob */
75 #define CS_OPS_IDENTITY 11 /* get codesign identity */
76
77 /* SigPUP */
78 #define CS_OPS_SIGPUP_INSTALL 20
79 #define CS_OPS_SIGPUP_DROP 21
80 #define CS_OPS_SIGPUP_VALIDATE 22
81
82 struct sigpup_install_table {
83 uint64_t data;
84 uint64_t length;
85 uint64_t path;
86 };
87
88
89 /*
90 * Magic numbers used by Code Signing
91 */
92 enum {
93 CSMAGIC_REQUIREMENT = 0xfade0c00, /* single Requirement blob */
94 CSMAGIC_REQUIREMENTS = 0xfade0c01, /* Requirements vector (internal requirements) */
95 CSMAGIC_CODEDIRECTORY = 0xfade0c02, /* CodeDirectory blob */
96 CSMAGIC_EMBEDDED_SIGNATURE = 0xfade0cc0, /* embedded form of signature data */
97 CSMAGIC_EMBEDDED_SIGNATURE_OLD = 0xfade0b02, /* XXX */
98 CSMAGIC_EMBEDDED_ENTITLEMENTS = 0xfade7171, /* embedded entitlements */
99 CSMAGIC_DETACHED_SIGNATURE = 0xfade0cc1, /* multi-arch collection of embedded signatures */
100 CSMAGIC_BLOBWRAPPER = 0xfade0b01, /* CMS Signature, among other things */
101
102 CS_SUPPORTSSCATTER = 0x20100,
103 CS_SUPPORTSTEAMID = 0x20200,
104
105 CSSLOT_CODEDIRECTORY = 0, /* slot index for CodeDirectory */
106 CSSLOT_INFOSLOT = 1,
107 CSSLOT_REQUIREMENTS = 2,
108 CSSLOT_RESOURCEDIR = 3,
109 CSSLOT_APPLICATION = 4,
110 CSSLOT_ENTITLEMENTS = 5,
111
112 CSSLOT_SIGNATURESLOT = 0x10000, /* CMS Signature */
113
114 CSTYPE_INDEX_REQUIREMENTS = 0x00000002, /* compat with amfi */
115 CSTYPE_INDEX_ENTITLEMENTS = 0x00000005, /* compat with amfi */
116
117 CS_HASHTYPE_SHA1 = 1
118 };
119
120
121 #define KERNEL_HAVE_CS_CODEDIRECTORY 1
122
123 /*
124 * C form of a CodeDirectory.
125 */
126 typedef struct __CodeDirectory {
127 uint32_t magic; /* magic number (CSMAGIC_CODEDIRECTORY) */
128 uint32_t length; /* total length of CodeDirectory blob */
129 uint32_t version; /* compatibility version */
130 uint32_t flags; /* setup and mode flags */
131 uint32_t hashOffset; /* offset of hash slot element at index zero */
132 uint32_t identOffset; /* offset of identifier string */
133 uint32_t nSpecialSlots; /* number of special hash slots */
134 uint32_t nCodeSlots; /* number of ordinary (code) hash slots */
135 uint32_t codeLimit; /* limit to main image signature range */
136 uint8_t hashSize; /* size of each hash in bytes */
137 uint8_t hashType; /* type of hash (cdHashType* constants) */
138 uint8_t spare1; /* unused (must be zero) */
139 uint8_t pageSize; /* log2(page size in bytes); 0 => infinite */
140 uint32_t spare2; /* unused (must be zero) */
141 /* Version 0x20100 */
142 uint32_t scatterOffset; /* offset of optional scatter vector */
143 /* Version 0x20200 */
144 uint32_t teamOffset; /* offset of optional team identifier */
145 /* followed by dynamic content as located by offset fields above */
146 } CS_CodeDirectory;
147
148 /*
149 * Structure of an embedded-signature SuperBlob
150 */
151
152 typedef struct __BlobIndex {
153 uint32_t type; /* type of entry */
154 uint32_t offset; /* offset of entry */
155 } CS_BlobIndex;
156
157 typedef struct __SC_SuperBlob {
158 uint32_t magic; /* magic number */
159 uint32_t length; /* total length of SuperBlob */
160 uint32_t count; /* number of index entries following */
161 CS_BlobIndex index[]; /* (count) entries */
162 /* followed by Blobs in no particular order as indicated by offsets in index */
163 } CS_SuperBlob;
164
165 typedef struct __SC_GenericBlob {
166 uint32_t magic; /* magic number */
167 uint32_t length; /* total length of blob */
168 char data[];
169 } CS_GenericBlob;
170
171 typedef struct __SC_Scatter {
172 uint32_t count; // number of pages; zero for sentinel (only)
173 uint32_t base; // first page number
174 uint64_t targetOffset; // offset in target
175 uint64_t spare; // reserved
176 } SC_Scatter;
177
178
179 #ifndef KERNEL
180
181 #include <sys/types.h>
182 #include <mach/message.h>
183
184 __BEGIN_DECLS
185 /* code sign operations */
186 int csops(pid_t pid, unsigned int ops, void * useraddr, size_t usersize);
187 int csops_audittoken(pid_t pid, unsigned int ops, void * useraddr, size_t usersize, audit_token_t * token);
188 __END_DECLS
189
190 #else /* !KERNEL */
191
192 #include <sys/cdefs.h>
193 #include <sys/_types/_off_t.h>
194
195 struct vnode;
196 struct cs_blob;
197 struct fileglob;
198
199 struct cscsr_functions {
200 int csr_version;
201 #define CSCSR_VERSION 1
202 int (*csr_validate_header)(const uint8_t *, size_t);
203 const void* (*csr_find_file_codedirectory)(struct vnode *, const uint8_t *, size_t, size_t *);
204 };
205
206 __BEGIN_DECLS
207 int cs_enforcement(struct proc *);
208 int cs_require_lv(struct proc *);
209 uint32_t cs_entitlement_flags(struct proc *p);
210 int cs_entitlements_blob_get(struct proc *, void **, size_t *);
211 uint8_t * cs_get_cdhash(struct proc *);
212 void cs_register_cscsr(struct cscsr_functions *);
213
214 const CS_GenericBlob *
215 cs_find_blob(struct cs_blob *, uint32_t, uint32_t);
216
217 const char * csblob_get_teamid(struct cs_blob *);
218 const char * csproc_get_teamid(struct proc *);
219 const char * csvnode_get_teamid(struct vnode *, off_t);
220 int csproc_get_platform_binary(struct proc *);
221 const char * csfg_get_teamid(struct fileglob *);
222 int csfg_get_path(struct fileglob *, char *, int *);
223 int csfg_get_platform_binary(struct fileglob *);
224
225 __END_DECLS
226
227 #ifdef XNU_KERNEL_PRIVATE
228
229 void cs_init(void);
230 int cs_allow_invalid(struct proc *);
231 int cs_invalid_page(addr64_t);
232 int sigpup_install(user_addr_t);
233 int sigpup_drop(void);
234
235 extern int cs_debug;
236 extern int cs_validation;
237 #if !SECURE_KERNEL
238 extern int cs_enforcement_panic;
239 #endif
240
241 #endif /* XNU_KERNEL_PRIVATE */
242
243 #endif /* KERNEL */
244
245 #endif /* _SYS_CODESIGN_H_ */