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