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