]> git.saurik.com Git - apple/xnu.git/blob - osfmk/ipc/mach_kernelrpc.c
b4ee58fecec039d0b671ae984dbf83b92611f4c3
[apple/xnu.git] / osfmk / ipc / mach_kernelrpc.c
1 /*
2 * Copyright (c) 2011 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 #include <mach/mach_types.h>
30 #include <mach/mach_traps.h>
31 #include <mach/mach_vm_server.h>
32 #include <mach/mach_port_server.h>
33 #include <mach/mach_host_server.h>
34 #include <mach/mach_voucher_server.h>
35 #include <mach/vm_map.h>
36 #include <kern/task.h>
37 #include <kern/ipc_tt.h>
38 #include <kern/kalloc.h>
39 #include <vm/vm_protos.h>
40
41 int
42 _kernelrpc_mach_vm_allocate_trap(struct _kernelrpc_mach_vm_allocate_trap_args *args)
43 {
44 mach_vm_offset_t addr;
45 task_t task = port_name_to_task(args->target);
46 int rv = MACH_SEND_INVALID_DEST;
47
48 if (task != current_task())
49 goto done;
50
51 if (copyin(args->addr, (char *)&addr, sizeof (addr)))
52 goto done;
53
54 rv = mach_vm_allocate_external(task->map, &addr, args->size, args->flags);
55 if (rv == KERN_SUCCESS)
56 rv = copyout(&addr, args->addr, sizeof (addr));
57
58 done:
59 if (task)
60 task_deallocate(task);
61 return (rv);
62 }
63
64 int
65 _kernelrpc_mach_vm_deallocate_trap(struct _kernelrpc_mach_vm_deallocate_args *args)
66 {
67 task_t task = port_name_to_task(args->target);
68 int rv = MACH_SEND_INVALID_DEST;
69
70 if (task != current_task())
71 goto done;
72
73 rv = mach_vm_deallocate(task->map, args->address, args->size);
74
75 done:
76 if (task)
77 task_deallocate(task);
78 return (rv);
79 }
80
81 int
82 _kernelrpc_mach_vm_protect_trap(struct _kernelrpc_mach_vm_protect_args *args)
83 {
84 task_t task = port_name_to_task(args->target);
85 int rv = MACH_SEND_INVALID_DEST;
86
87 if (task != current_task())
88 goto done;
89
90 rv = mach_vm_protect(task->map, args->address, args->size,
91 args->set_maximum, args->new_protection);
92
93 done:
94 if (task)
95 task_deallocate(task);
96 return (rv);
97 }
98
99 int
100 _kernelrpc_mach_vm_map_trap(struct _kernelrpc_mach_vm_map_trap_args *args)
101 {
102 mach_vm_offset_t addr;
103 task_t task = port_name_to_task(args->target);
104 int rv = MACH_SEND_INVALID_DEST;
105
106 if (task != current_task())
107 goto done;
108
109 if (copyin(args->addr, (char *)&addr, sizeof (addr)))
110 goto done;
111
112 rv = mach_vm_map_external(task->map, &addr, args->size, args->mask, args->flags,
113 IPC_PORT_NULL, 0, FALSE, args->cur_protection, VM_PROT_ALL,
114 VM_INHERIT_DEFAULT);
115 if (rv == KERN_SUCCESS)
116 rv = copyout(&addr, args->addr, sizeof (addr));
117
118 done:
119 if (task)
120 task_deallocate(task);
121 return (rv);
122 }
123
124 int
125 _kernelrpc_mach_vm_purgable_control_trap(
126 struct _kernelrpc_mach_vm_purgable_control_trap_args *args)
127 {
128 int state;
129 task_t task = port_name_to_task(args->target);
130 int rv = MACH_SEND_INVALID_DEST;
131
132 if (task != current_task())
133 goto done;
134
135 if (copyin(args->state, (char *)&state, sizeof (state)))
136 goto done;
137
138 rv = mach_vm_purgable_control(task->map,
139 args->address,
140 args->control,
141 &state);
142 if (rv == KERN_SUCCESS)
143 rv = copyout(&state, args->state, sizeof (state));
144
145 done:
146 if (task)
147 task_deallocate(task);
148 return (rv);
149 }
150
151 int
152 _kernelrpc_mach_port_allocate_trap(struct _kernelrpc_mach_port_allocate_args *args)
153 {
154 task_t task = port_name_to_task(args->target);
155 mach_port_name_t name;
156 int rv = MACH_SEND_INVALID_DEST;
157
158 if (task != current_task())
159 goto done;
160
161 rv = mach_port_allocate(task->itk_space, args->right, &name);
162 if (rv == KERN_SUCCESS)
163 rv = copyout(&name, args->name, sizeof (name));
164
165
166 done:
167 if (task)
168 task_deallocate(task);
169 return (rv);
170 }
171
172 int
173 _kernelrpc_mach_port_destroy_trap(struct _kernelrpc_mach_port_destroy_args *args)
174 {
175 task_t task = port_name_to_task(args->target);
176 int rv = MACH_SEND_INVALID_DEST;
177
178 if (task != current_task())
179 goto done;
180
181 rv = mach_port_destroy(task->itk_space, args->name);
182
183 done:
184 if (task)
185 task_deallocate(task);
186 return (rv);
187 }
188
189 int
190 _kernelrpc_mach_port_deallocate_trap(struct _kernelrpc_mach_port_deallocate_args *args)
191 {
192 task_t task = port_name_to_task(args->target);
193 int rv = MACH_SEND_INVALID_DEST;
194
195 if (task != current_task())
196 goto done;
197
198 rv = mach_port_deallocate(task->itk_space, args->name);
199
200 done:
201 if (task)
202 task_deallocate(task);
203 return (rv);
204 }
205
206 int
207 _kernelrpc_mach_port_mod_refs_trap(struct _kernelrpc_mach_port_mod_refs_args *args)
208 {
209 task_t task = port_name_to_task(args->target);
210 int rv = MACH_SEND_INVALID_DEST;
211
212 if (task != current_task())
213 goto done;
214
215 rv = mach_port_mod_refs(task->itk_space, args->name, args->right, args->delta);
216
217 done:
218 if (task)
219 task_deallocate(task);
220 return (rv);
221 }
222
223
224 int
225 _kernelrpc_mach_port_move_member_trap(struct _kernelrpc_mach_port_move_member_args *args)
226 {
227 task_t task = port_name_to_task(args->target);
228 int rv = MACH_SEND_INVALID_DEST;
229
230 if (task != current_task())
231 goto done;
232
233 rv = mach_port_move_member(task->itk_space, args->member, args->after);
234
235 done:
236 if (task)
237 task_deallocate(task);
238 return (rv);
239 }
240
241 int
242 _kernelrpc_mach_port_insert_right_trap(struct _kernelrpc_mach_port_insert_right_args *args)
243 {
244 task_t task = port_name_to_task(args->target);
245 ipc_port_t port;
246 mach_msg_type_name_t disp;
247 int rv = MACH_SEND_INVALID_DEST;
248
249 if (task != current_task())
250 goto done;
251
252 rv = ipc_object_copyin(task->itk_space, args->poly, args->polyPoly,
253 (ipc_object_t *)&port);
254 if (rv != KERN_SUCCESS)
255 goto done;
256 disp = ipc_object_copyin_type(args->polyPoly);
257
258 rv = mach_port_insert_right(task->itk_space, args->name, port, disp);
259 if (rv != KERN_SUCCESS) {
260 if (IO_VALID((ipc_object_t)port)) {
261 ipc_object_destroy((ipc_object_t)port, disp);
262 }
263 }
264
265 done:
266 if (task)
267 task_deallocate(task);
268 return (rv);
269 }
270
271 int
272 _kernelrpc_mach_port_insert_member_trap(struct _kernelrpc_mach_port_insert_member_args *args)
273 {
274 task_t task = port_name_to_task(args->target);
275 int rv = MACH_SEND_INVALID_DEST;
276
277 if (task != current_task())
278 goto done;
279
280 rv = mach_port_insert_member(task->itk_space, args->name, args->pset);
281
282 done:
283 if (task)
284 task_deallocate(task);
285 return (rv);
286 }
287
288
289 int
290 _kernelrpc_mach_port_extract_member_trap(struct _kernelrpc_mach_port_extract_member_args *args)
291 {
292 task_t task = port_name_to_task(args->target);
293 int rv = MACH_SEND_INVALID_DEST;
294
295 if (task != current_task())
296 goto done;
297
298 rv = mach_port_extract_member(task->itk_space, args->name, args->pset);
299
300 done:
301 if (task)
302 task_deallocate(task);
303 return (rv);
304 }
305
306 int
307 _kernelrpc_mach_port_construct_trap(struct _kernelrpc_mach_port_construct_args *args)
308 {
309 task_t task = port_name_to_task(args->target);
310 mach_port_name_t name;
311 int rv = MACH_SEND_INVALID_DEST;
312 mach_port_options_t options;
313
314 if (copyin(args->options, (char *)&options, sizeof (options))) {
315 rv = MACH_SEND_INVALID_DATA;
316 goto done;
317 }
318
319 if (task != current_task())
320 goto done;
321
322 rv = mach_port_construct(task->itk_space, &options, args->context, &name);
323 if (rv == KERN_SUCCESS)
324 rv = copyout(&name, args->name, sizeof (name));
325
326 done:
327 if (task)
328 task_deallocate(task);
329 return (rv);
330 }
331
332 int
333 _kernelrpc_mach_port_destruct_trap(struct _kernelrpc_mach_port_destruct_args *args)
334 {
335 task_t task = port_name_to_task(args->target);
336 int rv = MACH_SEND_INVALID_DEST;
337
338 if (task != current_task())
339 goto done;
340
341 rv = mach_port_destruct(task->itk_space, args->name, args->srdelta, args->guard);
342
343 done:
344 if (task)
345 task_deallocate(task);
346 return (rv);
347 }
348
349 int
350 _kernelrpc_mach_port_guard_trap(struct _kernelrpc_mach_port_guard_args *args)
351 {
352 task_t task = port_name_to_task(args->target);
353 int rv = MACH_SEND_INVALID_DEST;
354
355 if (task != current_task())
356 goto done;
357
358 rv = mach_port_guard(task->itk_space, args->name, args->guard, args->strict);
359
360 done:
361 if (task)
362 task_deallocate(task);
363 return (rv);
364 }
365
366 int
367 _kernelrpc_mach_port_unguard_trap(struct _kernelrpc_mach_port_unguard_args *args)
368 {
369 task_t task = port_name_to_task(args->target);
370 int rv = MACH_SEND_INVALID_DEST;
371
372 if (task != current_task())
373 goto done;
374
375 rv = mach_port_unguard(task->itk_space, args->name, args->guard);
376
377 done:
378 if (task)
379 task_deallocate(task);
380 return (rv);
381 }
382
383 kern_return_t
384 host_create_mach_voucher_trap(struct host_create_mach_voucher_args *args)
385 {
386 host_t host = port_name_to_host(args->host);
387 ipc_voucher_t new_voucher = IV_NULL;
388 ipc_port_t voucher_port = IPC_PORT_NULL;
389 mach_port_name_t voucher_name = 0;
390 kern_return_t kr = 0;
391
392 if (host == HOST_NULL)
393 return MACH_SEND_INVALID_DEST;
394
395 if (args->recipes_size < 0)
396 return KERN_INVALID_ARGUMENT;
397 else if (args->recipes_size > MACH_VOUCHER_ATTR_MAX_RAW_RECIPE_ARRAY_SIZE)
398 return MIG_ARRAY_TOO_LARGE;
399
400 if (args->recipes_size < MACH_VOUCHER_TRAP_STACK_LIMIT) {
401 /* keep small recipes on the stack for speed */
402 uint8_t krecipes[args->recipes_size];
403 if (copyin(CAST_USER_ADDR_T(args->recipes), (void *)krecipes, args->recipes_size)) {
404 kr = KERN_MEMORY_ERROR;
405 goto done;
406 }
407 kr = host_create_mach_voucher(host, krecipes, args->recipes_size, &new_voucher);
408 } else {
409 uint8_t *krecipes = kalloc((vm_size_t)args->recipes_size);
410 if (!krecipes) {
411 kr = KERN_RESOURCE_SHORTAGE;
412 goto done;
413 }
414
415 if (copyin(CAST_USER_ADDR_T(args->recipes), (void *)krecipes, args->recipes_size)) {
416 kfree(krecipes, (vm_size_t)args->recipes_size);
417 kr = KERN_MEMORY_ERROR;
418 goto done;
419 }
420
421 kr = host_create_mach_voucher(host, krecipes, args->recipes_size, &new_voucher);
422 kfree(krecipes, (vm_size_t)args->recipes_size);
423 }
424
425 if (kr == 0) {
426 voucher_port = convert_voucher_to_port(new_voucher);
427 voucher_name = ipc_port_copyout_send(voucher_port, current_space());
428
429 kr = copyout(&voucher_name, args->voucher, sizeof(voucher_name));
430 }
431
432 done:
433 return kr;
434 }
435
436 kern_return_t
437 mach_voucher_extract_attr_recipe_trap(struct mach_voucher_extract_attr_recipe_args *args)
438 {
439 ipc_voucher_t voucher = IV_NULL;
440 kern_return_t kr = KERN_SUCCESS;
441 mach_msg_type_number_t sz = 0;
442
443 if (copyin(args->recipe_size, (void *)&sz, sizeof(sz)))
444 return KERN_MEMORY_ERROR;
445
446 if (sz > MACH_VOUCHER_ATTR_MAX_RAW_RECIPE_ARRAY_SIZE)
447 return MIG_ARRAY_TOO_LARGE;
448
449 voucher = convert_port_name_to_voucher(args->voucher_name);
450 if (voucher == IV_NULL)
451 return MACH_SEND_INVALID_DEST;
452
453 mach_msg_type_number_t max_sz = sz;
454
455 if (sz < MACH_VOUCHER_TRAP_STACK_LIMIT) {
456 /* keep small recipes on the stack for speed */
457 uint8_t krecipe[sz];
458 if (copyin(CAST_USER_ADDR_T(args->recipe), (void *)krecipe, sz)) {
459 kr = KERN_MEMORY_ERROR;
460 goto done;
461 }
462 kr = mach_voucher_extract_attr_recipe(voucher, args->key,
463 (mach_voucher_attr_raw_recipe_t)krecipe, &sz);
464 assert(sz <= max_sz);
465
466 if (kr == KERN_SUCCESS && sz > 0)
467 kr = copyout(krecipe, CAST_USER_ADDR_T(args->recipe), sz);
468 } else {
469 uint8_t *krecipe = kalloc((vm_size_t)max_sz);
470 if (!krecipe) {
471 kr = KERN_RESOURCE_SHORTAGE;
472 goto done;
473 }
474
475 if (copyin(CAST_USER_ADDR_T(args->recipe), (void *)krecipe, sz)) {
476 kfree(krecipe, (vm_size_t)max_sz);
477 kr = KERN_MEMORY_ERROR;
478 goto done;
479 }
480
481 kr = mach_voucher_extract_attr_recipe(voucher, args->key,
482 (mach_voucher_attr_raw_recipe_t)krecipe, &sz);
483 assert(sz <= max_sz);
484
485 if (kr == KERN_SUCCESS && sz > 0)
486 kr = copyout(krecipe, CAST_USER_ADDR_T(args->recipe), sz);
487 kfree(krecipe, (vm_size_t)max_sz);
488 }
489
490 kr = copyout(&sz, args->recipe_size, sizeof(sz));
491
492 done:
493 ipc_voucher_release(voucher);
494 return kr;
495 }