xref: /xnu-8792.81.2/osfmk/kdp/output_stages/out_aea.c (revision 19c3b8c28c31cb8130e034cfb5df6bf9ba342d90)
1 /*
2  * Copyright (c) 2021 Apple 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 #ifdef CONFIG_KDP_INTERACTIVE_DEBUGGING
30 
31 #include <mach/mach_types.h>
32 #include <IOKit/IOTypes.h>
33 #include <kdp/output_stages/output_stages.h>
34 #include <kdp/kdp_core.h>
35 #include <kdp/processor_core.h>
36 #include <machine/param.h>
37 #include <libkern/apple_encrypted_archive/apple_encrypted_archive.h>
38 
39 struct aea_stage_data {
40 	bool     encryption_open;
41 	uint64_t starting_corefile_offset;;
42 	uint64_t current_corefile_offset;
43 	size_t   state_size;
44 	char     state[];
45 };
46 
47 static ssize_t
aea_write_callback(void * context,const void * buffer,size_t length,off_t offset)48 aea_write_callback(void *context, const void *buffer, size_t length, off_t offset)
49 {
50 	kern_return_t            err = KERN_SUCCESS;
51 	struct kdp_output_stage *stage = (struct kdp_output_stage *) context;
52 	struct kdp_output_stage *next_stage = STAILQ_NEXT(stage, kos_next);
53 	struct aea_stage_data   *stage_data = (struct aea_stage_data *) stage->kos_data;
54 	uint64_t absolute_corefile_offset = stage_data->starting_corefile_offset + offset;
55 
56 	err = disk_stage_write(next_stage, absolute_corefile_offset, length, buffer);
57 
58 	if (KERN_SUCCESS != err) {
59 		kern_coredump_log(NULL, "(aea_write_callback) next stage outproc returned 0x%x\n", err);
60 		return -1;
61 	}
62 
63 	stage_data->current_corefile_offset = absolute_corefile_offset + length;
64 	if (offset + length > stage->kos_bytes_written) {
65 		stage->kos_bytes_written = offset + length;
66 		next_stage->kos_bytes_written = stage->kos_bytes_written;
67 	}
68 
69 	return length;
70 }
71 
72 static ssize_t
aea_read_callback(void * context,void * buffer,size_t length,off_t offset)73 aea_read_callback(void *context, void *buffer, size_t length, off_t offset)
74 {
75 	kern_return_t            err = KERN_SUCCESS;
76 	struct kdp_output_stage *stage = (struct kdp_output_stage *) context;
77 	struct kdp_output_stage *next_stage = STAILQ_NEXT(stage, kos_next);
78 	struct aea_stage_data   *stage_data = (struct aea_stage_data *) stage->kos_data;
79 	uint64_t absolute_corefile_offset = stage_data->starting_corefile_offset + offset;
80 
81 	err = disk_stage_read(next_stage, absolute_corefile_offset, length, buffer);
82 	if (KERN_SUCCESS != err) {
83 		kern_coredump_log(NULL, "(aea_read_callback) next stage read proc returned 0x%x\n", err);
84 		return -1;
85 	}
86 
87 	stage_data->current_corefile_offset = absolute_corefile_offset + length;
88 
89 	return length;
90 }
91 
92 static void
aea_stage_reset(struct kdp_output_stage * stage)93 aea_stage_reset(struct kdp_output_stage *stage)
94 {
95 	int aea_ret = 0;
96 	struct aea_stage_data *stage_data = (struct aea_stage_data *) stage->kos_data;
97 
98 	if (stage_data->encryption_open) {
99 		aea_ret = apple_encrypted_archive->aea_close(stage_data->state, stage_data->state_size);
100 		if (aea_ret != 0) {
101 			kern_coredump_log(NULL, "(aea_stage_reset) aea_close() returned %d\n", aea_ret);
102 			// TODO return the error?
103 		} else {
104 			stage_data->encryption_open = false;
105 		}
106 	}
107 
108 	stage->kos_bypass = false;
109 	stage->kos_bytes_written = 0;
110 }
111 
112 static kern_return_t
aea_stage_outproc(struct kdp_output_stage * stage,unsigned int request,__unused char * corename,uint64_t length,void * panic_data)113 aea_stage_outproc(struct kdp_output_stage *stage, unsigned int request,
114     __unused char *corename, uint64_t length, void *panic_data)
115 {
116 	kern_return_t            err = KERN_SUCCESS;
117 	int                      aea_ret = 0;
118 	struct aea_stage_data   *stage_data = (struct aea_stage_data *) stage->kos_data;
119 	struct kdp_output_stage *next_stage = STAILQ_NEXT(stage, kos_next);
120 	size_t                   chunk = 0;
121 
122 	assert(next_stage != NULL);
123 
124 	switch (request) {
125 	case KDP_SEEK:
126 		stage->kos_bypass = true;
127 		if (stage_data->encryption_open) {
128 			aea_ret = apple_encrypted_archive->aea_close(stage_data->state, stage_data->state_size);
129 			if (aea_ret != 0) {
130 				kern_coredump_log(NULL, "(aea_stage_outproc) aea_close() returned %d\n", aea_ret);
131 				err = KERN_FAILURE;
132 			} else {
133 				stage_data->encryption_open = false;
134 			}
135 		}
136 		if (KERN_SUCCESS == err) {
137 			err = next_stage->kos_funcs.kosf_outproc(next_stage, request, corename, length, panic_data);
138 		}
139 		if (KERN_SUCCESS == err) {
140 			stage_data->starting_corefile_offset = *((uint64_t *) panic_data);
141 			stage_data->current_corefile_offset = stage_data->starting_corefile_offset;
142 		}
143 		break;
144 	case KDP_DATA:
145 		if (!stage->kos_bypass) {
146 			if (!length && !panic_data) {
147 				// Flush
148 				if (stage_data->encryption_open) {
149 					aea_ret = apple_encrypted_archive->aea_close(stage_data->state, stage_data->state_size);
150 					if (aea_ret != 0) {
151 						kern_coredump_log(NULL, "(aea_stage_outproc) aea_close() returned %d\n", aea_ret);
152 						err = KERN_FAILURE;
153 					} else {
154 						stage_data->encryption_open = false;
155 					}
156 				}
157 			} else {
158 				if (stage_data->encryption_open == false) {
159 					aea_ret = apple_encrypted_archive->aea_open(stage_data->state, stage_data->state_size, (void *) stage, aea_write_callback, aea_read_callback);
160 					if (aea_ret != 0) {
161 						kern_coredump_log(NULL, "(aea_stage_outproc) aea_open() returned %d\n", aea_ret);
162 						err = KERN_FAILURE;
163 					} else {
164 						stage_data->encryption_open = true;
165 					}
166 				}
167 				if (KERN_SUCCESS == err) {
168 					do{
169 						ssize_t write_result;
170 
171 						if (length <= UINT32_MAX) {
172 							chunk = (size_t) length;
173 						} else {
174 							chunk = UINT32_MAX;
175 						}
176 						write_result = apple_encrypted_archive->aea_write(stage_data->state, stage_data->state_size, panic_data, chunk);
177 						if (write_result != chunk) {
178 							kern_coredump_log(NULL, "(aea_stage_outproc) aea_write() returned %zd\n", write_result);
179 							err = KERN_FAILURE;
180 						}
181 
182 						length -= chunk;
183 
184 						if (panic_data) {
185 							panic_data = (void *) (((uintptr_t) panic_data) + chunk);
186 						}
187 					} while (length && (KERN_SUCCESS == err));
188 				}
189 			}
190 		} else {
191 			err = next_stage->kos_funcs.kosf_outproc(next_stage, request, corename, length, panic_data);
192 		}
193 		break;
194 	case KDP_WRQ:
195 	/* Fall-through */
196 	case KDP_FLUSH:
197 	/* Fall-through */
198 	case KDP_EOF:
199 		err = next_stage->kos_funcs.kosf_outproc(next_stage, request, corename, length, panic_data);
200 		break;
201 	default:
202 		break;
203 	}
204 
205 	return err;
206 }
207 
208 static void
aea_stage_free(struct kdp_output_stage * stage)209 aea_stage_free(struct kdp_output_stage *stage)
210 {
211 	kmem_free(kernel_map, (vm_offset_t) stage->kos_data, stage->kos_data_size);
212 
213 	stage->kos_data = NULL;
214 	stage->kos_data_size = 0;
215 	stage->kos_initialized = false;
216 }
217 
218 kern_return_t
aea_stage_initialize(struct kdp_output_stage * stage,const void * recipient_public_key,size_t recipient_public_key_size)219 aea_stage_initialize(struct kdp_output_stage *stage, const void *recipient_public_key, size_t recipient_public_key_size)
220 {
221 	kern_return_t ret = KERN_SUCCESS;
222 	int aea_ret = 0;
223 	struct aea_stage_data *data = NULL;
224 	size_t state_size = 0;
225 
226 	assert(apple_encrypted_archive != NULL);
227 	assert(stage != NULL);
228 	assert(stage->kos_initialized == false);
229 	assert(stage->kos_data == NULL);
230 	assert(recipient_public_key != NULL);
231 	assert(recipient_public_key_size != 0);
232 
233 	state_size = apple_encrypted_archive->aea_get_state_size();
234 	if (0 == state_size) {
235 		printf("AEA kext returned an error while calculating state size.");
236 		ret = KERN_FAILURE;
237 		return ret;
238 	}
239 	stage->kos_data_size = sizeof(struct aea_stage_data) + state_size;
240 	ret = kmem_alloc(kernel_map, (vm_offset_t*) &stage->kos_data, stage->kos_data_size,
241 	    KMA_DATA, VM_KERN_MEMORY_DIAG);
242 	if (KERN_SUCCESS != ret) {
243 		printf("Failed to allocate memory (%zu bytes) for the AEA stage. Error 0x%x\n", stage->kos_data_size, ret);
244 		return ret;
245 	}
246 
247 	data = (struct aea_stage_data *) stage->kos_data;
248 	data->encryption_open = false;
249 	data->starting_corefile_offset = 0;
250 	data->current_corefile_offset = 0;
251 	data->state_size = state_size;
252 
253 	aea_ret = apple_encrypted_archive->aea_initialize_state(data->state, data->state_size, (const uint8_t *)recipient_public_key, recipient_public_key_size);
254 	if (aea_ret != 0) {
255 		printf("WARNING: Coredump encryption failed to initialize. aea_initialize_state() returned %d\n", aea_ret);
256 		aea_stage_free(stage);
257 		return KERN_FAILURE;
258 	}
259 
260 	stage->kos_funcs.kosf_reset = aea_stage_reset;
261 	stage->kos_funcs.kosf_outproc = aea_stage_outproc;
262 	stage->kos_funcs.kosf_free = aea_stage_free;
263 
264 	stage->kos_initialized = true;
265 
266 	return ret;
267 }
268 
269 static void
aea_availability_callback(void)270 aea_availability_callback(void)
271 {
272 	kern_return_t ret = kdp_core_handle_encryption_available();
273 	if (KERN_SUCCESS != ret) {
274 		printf("(aea_availability_callback) Failed to handle availability of encryption. Error 0x%x\n", ret);
275 	}
276 }
277 
278 void
aea_stage_monitor_availability(void)279 aea_stage_monitor_availability(void)
280 {
281 	apple_encrypted_archive_interface_set_registration_callback(aea_availability_callback);
282 }
283 
284 bool
aea_stage_is_available(void)285 aea_stage_is_available(void)
286 {
287 	return apple_encrypted_archive != NULL;
288 }
289 
290 #endif /* CONFIG_KDP_INTERACTIVE_DEBUGGING */
291