xref: /xnu-11215.61.5/bsd/sys/trust_caches.h (revision 4f1223e81cd707a65cc109d0b8ad6653699da3c4)
1 /*
2  * Copyright (c) 2022 Apple Computer, Inc. All rights reserved.
3  *
4  * @APPLE_LICENSE_HEADER_START@
5  *
6  * The contents of this file constitute Original Code as defined in and
7  * are subject to the Apple Public Source License Version 1.1 (the
8  * "License").  You may not use this file except in compliance with the
9  * License.  Please obtain a copy of the License at
10  * http://www.apple.com/publicsource and read it before using this file.
11  *
12  * This Original Code and all software distributed under the License are
13  * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT.  Please see the
17  * License for the specific language governing rights and limitations
18  * under the License.
19  *
20  * @APPLE_LICENSE_HEADER_END@
21  */
22 
23 #ifndef _SYS_TRUST_CACHES_H_
24 #define _SYS_TRUST_CACHES_H_
25 
26 #ifdef KERNEL_PRIVATE
27 
28 #include <mach/kern_return.h>
29 #include <sys/cdefs.h>
30 #include <TrustCache/API.h>
31 
32 #if (DEVELOPMENT || DEBUG)
33 #define TRUST_CACHE_INCLUDE_INTERNAL_CODE 1
34 #endif
35 
36 /* Availability macros to check for support */
37 #define XNU_HAS_TRUST_CACHE_LOADING 1
38 #define XNU_HAS_TRUST_CACHE_CHECK_RUNTIME_FOR_UUID 1
39 #if kLibTrustCacheHasQueryForREM
40 #define XNU_HAS_TRUST_CACHE_QUERY_FOR_REM 1
41 #endif
42 
43 #ifdef XNU_PLATFORM_BridgeOS
44 #define XNU_HAS_LEGACY_TRUST_CACHE_LOADING 1
45 #elif defined(TARGET_OS_BRIDGE) && TARGET_OS_BRIDGE
46 #define XNU_HAS_LEGACY_TRUST_CACHE_LOADING 1
47 #else
48 #define XNU_HAS_LEGACY_TRUST_CACHE_LOADING 0
49 #endif
50 
51 __BEGIN_DECLS
52 
53 #if XNU_KERNEL_PRIVATE
54 
55 /* Temporary definition until we get a proper shared one */
56 typedef struct DTTrustCacheRange {
57 	vm_offset_t paddr;
58 	size_t length;
59 } DTTrustCacheRange;
60 
61 /* This is the structure iBoot uses to deliver the trust caches to the system */
62 typedef struct _trust_cache_offsets {
63 	/* The number of trust caches provided */
64 	uint32_t num_caches;
65 
66 	/* Offset of each from beginning of the structure */
67 	uint32_t offsets[0];
68 } __attribute__((__packed__)) trust_cache_offsets_t;
69 
70 /**
71  * Initialize the trust cache runtime for the system environment.
72  */
73 void
74 trust_cache_runtime_init(void);
75 
76 /**
77  * Load the static and engineering trust caches passed over to the system by the boot loader.
78  */
79 void
80 load_static_trust_cache(void);
81 
82 #endif /* XNU_KERNEL_PRIVATE */
83 
84 /**
85  * Check the capabilities of the static trust caches on the system. Since the static trust
86  * caches are loaded at boot, kernel extensions don't get a chance to observe their format
87  * and miss out on the information.
88  *
89  * This function can be queried to obtain this information.
90  */
91 kern_return_t
92 static_trust_cache_capabilities(
93 	uint32_t *num_static_trust_caches_ret,
94 	TCCapabilities_t *capabilities0_ret,
95 	TCCapabilities_t *capabilities1_ret);
96 
97 /**
98  * Check if a particular trust cache has already been loaded into the system on the basis
99  * of a provided UUID.
100  *
101  * Based on the system environment, this request may trap into the kernel's code signing
102  * monitor environment as the trust cache data structures need to be locked down.
103  */
104 kern_return_t
105 check_trust_cache_runtime_for_uuid(
106 	const uint8_t check_uuid[kUUIDSize]);
107 
108 /**
109  * Load an image4 trust cache. Since the type of trust cache isn't specified, this interface
110  * attempts to validate the trust cache through all known types. Therefore, this evaluation
111  * can be expensive.
112  *
113  * This is a deprecated interface and should no longer be used. It also doesn't support usage
114  * of the auxiliary manifest. Please use the newer interface "load_trust_cache_with_type".
115  */
116 kern_return_t
117 load_trust_cache(
118 	const uint8_t *img4_object, const size_t img4_object_len,
119 	const uint8_t *img4_ext_manifest, const size_t img4_ext_manifest_len);
120 
121 /**
122  * Load an image4 based trust cache of a particular type. This function performs an entitlement
123  * check on the calling process to ensure it has the entitlement for loading the specified trust
124  * cache.
125  *
126  * Based on the system environment, the trust cache may be loaded into kernel memory, or it may
127  * be loaded into memory controlled by the kernel monitor environment. In either case, this
128  * function creates its own allocations for the data, and the caller may free their allocations,
129  * if any.
130  */
131 kern_return_t
132 load_trust_cache_with_type(
133 	TCType_t type,
134 	const uint8_t *img4_object, const size_t img4_object_len,
135 	const uint8_t *img4_ext_manifest, const size_t img4_ext_manifest_len,
136 	const uint8_t *img4_aux_manifest, const size_t img4_aux_manifest_len);
137 
138 /**
139  * Load a legacy trust cache module for supported platforms. Availability for the KPI can
140  * be checked by querying the macro "XNU_HAS_LEGACY_TRUST_CACHE_LOADING". Using this KPI
141  * on an unsupported platform will panic the system.
142  */
143 kern_return_t
144 load_legacy_trust_cache(
145 	const uint8_t *module_data, const size_t module_size);
146 
147 /**
148  * Query a trust cache based on the type passed in.
149  *
150  * Based on the system environment, the trust cache may be queried from kernel memory, or it may
151  * be queried from memory controlled by the kernel monitor environment.
152  */
153 kern_return_t
154 query_trust_cache(
155 	TCQueryType_t query_type,
156 	const uint8_t cdhash[kTCEntryHashSize],
157 	TrustCacheQueryToken_t *query_token);
158 
159 /**
160  * Query the set of loaded trust caches to find the most relevant REM permissions for a given
161  * CDHash.
162  *
163  * Based on the system environment, the trust cache may be queried from kernel memory, or it may
164  * be queried from memory controlled by the kernel monitor environment.
165  */
166 kern_return_t
167 query_trust_cache_for_rem(
168 	const uint8_t cdhash[kTCEntryHashSize],
169 	uint8_t *rem_perms);
170 
171 __END_DECLS
172 
173 #endif /* KERNEL_PRIVATE */
174 #endif /* _SYS_TRUST_CACHES_H_ */
175