2 * Copyright (c) 2011 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
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>
42 _kernelrpc_mach_vm_allocate_trap(struct _kernelrpc_mach_vm_allocate_trap_args
*args
)
44 mach_vm_offset_t addr
;
45 task_t task
= port_name_to_task(args
->target
);
46 int rv
= MACH_SEND_INVALID_DEST
;
48 if (task
!= current_task()) {
52 if (copyin(args
->addr
, (char *)&addr
, sizeof(addr
))) {
56 rv
= mach_vm_allocate_external(task
->map
, &addr
, args
->size
, args
->flags
);
57 if (rv
== KERN_SUCCESS
) {
58 rv
= copyout(&addr
, args
->addr
, sizeof(addr
));
63 task_deallocate(task
);
69 _kernelrpc_mach_vm_deallocate_trap(struct _kernelrpc_mach_vm_deallocate_args
*args
)
71 task_t task
= port_name_to_task(args
->target
);
72 int rv
= MACH_SEND_INVALID_DEST
;
74 if (task
!= current_task()) {
78 rv
= mach_vm_deallocate(task
->map
, args
->address
, args
->size
);
82 task_deallocate(task
);
88 _kernelrpc_mach_vm_protect_trap(struct _kernelrpc_mach_vm_protect_args
*args
)
90 task_t task
= port_name_to_task(args
->target
);
91 int rv
= MACH_SEND_INVALID_DEST
;
93 if (task
!= current_task()) {
97 rv
= mach_vm_protect(task
->map
, args
->address
, args
->size
,
98 args
->set_maximum
, args
->new_protection
);
102 task_deallocate(task
);
108 _kernelrpc_mach_vm_map_trap(struct _kernelrpc_mach_vm_map_trap_args
*args
)
110 mach_vm_offset_t addr
;
111 task_t task
= port_name_to_task(args
->target
);
112 int rv
= MACH_SEND_INVALID_DEST
;
114 if (task
!= current_task()) {
118 if (copyin(args
->addr
, (char *)&addr
, sizeof(addr
))) {
122 rv
= mach_vm_map_external(task
->map
, &addr
, args
->size
, args
->mask
, args
->flags
,
123 IPC_PORT_NULL
, 0, FALSE
, args
->cur_protection
, VM_PROT_ALL
,
125 if (rv
== KERN_SUCCESS
) {
126 rv
= copyout(&addr
, args
->addr
, sizeof(addr
));
131 task_deallocate(task
);
137 _kernelrpc_mach_vm_purgable_control_trap(
138 struct _kernelrpc_mach_vm_purgable_control_trap_args
*args
)
141 task_t task
= port_name_to_task(args
->target
);
142 int rv
= MACH_SEND_INVALID_DEST
;
144 if (task
!= current_task()) {
148 if (copyin(args
->state
, (char *)&state
, sizeof(state
))) {
152 rv
= mach_vm_purgable_control(task
->map
,
156 if (rv
== KERN_SUCCESS
) {
157 rv
= copyout(&state
, args
->state
, sizeof(state
));
162 task_deallocate(task
);
168 _kernelrpc_mach_port_allocate_trap(struct _kernelrpc_mach_port_allocate_args
*args
)
170 task_t task
= port_name_to_task(args
->target
);
171 mach_port_name_t name
;
172 int rv
= MACH_SEND_INVALID_DEST
;
174 if (task
!= current_task()) {
178 rv
= mach_port_allocate(task
->itk_space
, args
->right
, &name
);
179 if (rv
== KERN_SUCCESS
) {
180 rv
= copyout(&name
, args
->name
, sizeof(name
));
186 task_deallocate(task
);
192 _kernelrpc_mach_port_destroy_trap(struct _kernelrpc_mach_port_destroy_args
*args
)
194 task_t task
= port_name_to_task(args
->target
);
195 int rv
= MACH_SEND_INVALID_DEST
;
197 if (task
!= current_task()) {
201 rv
= mach_port_destroy(task
->itk_space
, args
->name
);
205 task_deallocate(task
);
211 _kernelrpc_mach_port_deallocate_trap(struct _kernelrpc_mach_port_deallocate_args
*args
)
213 task_t task
= port_name_to_task(args
->target
);
214 int rv
= MACH_SEND_INVALID_DEST
;
216 if (task
!= current_task()) {
220 rv
= mach_port_deallocate(task
->itk_space
, args
->name
);
224 task_deallocate(task
);
230 _kernelrpc_mach_port_mod_refs_trap(struct _kernelrpc_mach_port_mod_refs_args
*args
)
232 task_t task
= port_name_to_task(args
->target
);
233 int rv
= MACH_SEND_INVALID_DEST
;
235 if (task
!= current_task()) {
239 rv
= mach_port_mod_refs(task
->itk_space
, args
->name
, args
->right
, args
->delta
);
243 task_deallocate(task
);
250 _kernelrpc_mach_port_move_member_trap(struct _kernelrpc_mach_port_move_member_args
*args
)
252 task_t task
= port_name_to_task(args
->target
);
253 int rv
= MACH_SEND_INVALID_DEST
;
255 if (task
!= current_task()) {
259 rv
= mach_port_move_member(task
->itk_space
, args
->member
, args
->after
);
263 task_deallocate(task
);
269 _kernelrpc_mach_port_insert_right_trap(struct _kernelrpc_mach_port_insert_right_args
*args
)
271 task_t task
= port_name_to_task(args
->target
);
273 mach_msg_type_name_t disp
;
274 int rv
= MACH_SEND_INVALID_DEST
;
276 if (task
!= current_task()) {
280 rv
= ipc_object_copyin(task
->itk_space
, args
->poly
, args
->polyPoly
,
281 (ipc_object_t
*)&port
);
282 if (rv
!= KERN_SUCCESS
) {
285 disp
= ipc_object_copyin_type(args
->polyPoly
);
287 rv
= mach_port_insert_right(task
->itk_space
, args
->name
, port
, disp
);
288 if (rv
!= KERN_SUCCESS
) {
289 if (IO_VALID((ipc_object_t
)port
)) {
290 ipc_object_destroy((ipc_object_t
)port
, disp
);
296 task_deallocate(task
);
302 _kernelrpc_mach_port_get_attributes_trap(struct _kernelrpc_mach_port_get_attributes_args
*args
)
304 task_inspect_t task
= port_name_to_task_inspect(args
->target
);
305 int rv
= MACH_SEND_INVALID_DEST
;
306 mach_msg_type_number_t count
;
308 if (task
!= current_task()) {
312 // MIG does not define the type or size of the mach_port_info_t out array
313 // anywhere, so derive them from the field in the generated reply struct
314 #define MACH_PORT_INFO_OUT (((__Reply__mach_port_get_attributes_t*)NULL)->port_info_out)
315 #define MACH_PORT_INFO_STACK_LIMIT 80 // current size is 68 == 17 * sizeof(integer_t)
316 _Static_assert(sizeof(MACH_PORT_INFO_OUT
) < MACH_PORT_INFO_STACK_LIMIT
,
317 "mach_port_info_t has grown significantly, reevaluate stack usage");
318 const mach_msg_type_number_t max_count
= (sizeof(MACH_PORT_INFO_OUT
) / sizeof(MACH_PORT_INFO_OUT
[0]));
319 typeof(MACH_PORT_INFO_OUT
[0]) info
[max_count
];
322 * zero out our stack buffer because not all flavors of
323 * port_get_attributes initialize the whole struct
325 bzero(info
, sizeof(MACH_PORT_INFO_OUT
));
327 if (copyin(CAST_USER_ADDR_T(args
->count
), &count
, sizeof(count
))) {
328 rv
= MACH_SEND_INVALID_DATA
;
331 if (count
> max_count
) {
335 rv
= mach_port_get_attributes(task
->itk_space
, args
->name
, args
->flavor
, info
, &count
);
336 if (rv
== KERN_SUCCESS
) {
337 rv
= copyout(&count
, CAST_USER_ADDR_T(args
->count
), sizeof(count
));
339 if (rv
== KERN_SUCCESS
&& count
> 0) {
340 rv
= copyout(info
, CAST_USER_ADDR_T(args
->info
), count
* sizeof(info
[0]));
345 task_deallocate(task
);
351 _kernelrpc_mach_port_insert_member_trap(struct _kernelrpc_mach_port_insert_member_args
*args
)
353 task_t task
= port_name_to_task(args
->target
);
354 int rv
= MACH_SEND_INVALID_DEST
;
356 if (task
!= current_task()) {
360 rv
= mach_port_insert_member(task
->itk_space
, args
->name
, args
->pset
);
364 task_deallocate(task
);
371 _kernelrpc_mach_port_extract_member_trap(struct _kernelrpc_mach_port_extract_member_args
*args
)
373 task_t task
= port_name_to_task(args
->target
);
374 int rv
= MACH_SEND_INVALID_DEST
;
376 if (task
!= current_task()) {
380 rv
= mach_port_extract_member(task
->itk_space
, args
->name
, args
->pset
);
384 task_deallocate(task
);
390 _kernelrpc_mach_port_construct_trap(struct _kernelrpc_mach_port_construct_args
*args
)
392 task_t task
= port_name_to_task(args
->target
);
393 mach_port_name_t name
;
394 int rv
= MACH_SEND_INVALID_DEST
;
395 mach_port_options_t options
;
397 if (copyin(args
->options
, (char *)&options
, sizeof(options
))) {
398 rv
= MACH_SEND_INVALID_DATA
;
402 if (task
!= current_task()) {
406 rv
= mach_port_construct(task
->itk_space
, &options
, args
->context
, &name
);
407 if (rv
== KERN_SUCCESS
) {
408 rv
= copyout(&name
, args
->name
, sizeof(name
));
413 task_deallocate(task
);
419 _kernelrpc_mach_port_destruct_trap(struct _kernelrpc_mach_port_destruct_args
*args
)
421 task_t task
= port_name_to_task(args
->target
);
422 int rv
= MACH_SEND_INVALID_DEST
;
424 if (task
!= current_task()) {
428 rv
= mach_port_destruct(task
->itk_space
, args
->name
, args
->srdelta
, args
->guard
);
432 task_deallocate(task
);
438 _kernelrpc_mach_port_guard_trap(struct _kernelrpc_mach_port_guard_args
*args
)
440 task_t task
= port_name_to_task(args
->target
);
441 int rv
= MACH_SEND_INVALID_DEST
;
443 if (task
!= current_task()) {
447 rv
= mach_port_guard(task
->itk_space
, args
->name
, args
->guard
, args
->strict
);
451 task_deallocate(task
);
457 _kernelrpc_mach_port_unguard_trap(struct _kernelrpc_mach_port_unguard_args
*args
)
459 task_t task
= port_name_to_task(args
->target
);
460 int rv
= MACH_SEND_INVALID_DEST
;
462 if (task
!= current_task()) {
466 rv
= mach_port_unguard(task
->itk_space
, args
->name
, args
->guard
);
470 task_deallocate(task
);
476 host_create_mach_voucher_trap(struct host_create_mach_voucher_args
*args
)
478 host_t host
= port_name_to_host(args
->host
);
479 ipc_voucher_t new_voucher
= IV_NULL
;
480 ipc_port_t voucher_port
= IPC_PORT_NULL
;
481 mach_port_name_t voucher_name
= 0;
482 kern_return_t kr
= 0;
484 if (host
== HOST_NULL
) {
485 return MACH_SEND_INVALID_DEST
;
488 if (args
->recipes_size
< 0) {
489 return KERN_INVALID_ARGUMENT
;
490 } else if (args
->recipes_size
> MACH_VOUCHER_ATTR_MAX_RAW_RECIPE_ARRAY_SIZE
) {
491 return MIG_ARRAY_TOO_LARGE
;
494 if (args
->recipes_size
< MACH_VOUCHER_TRAP_STACK_LIMIT
) {
495 /* keep small recipes on the stack for speed */
496 uint8_t krecipes
[args
->recipes_size
];
497 if (copyin(CAST_USER_ADDR_T(args
->recipes
), (void *)krecipes
, args
->recipes_size
)) {
498 kr
= KERN_MEMORY_ERROR
;
501 kr
= host_create_mach_voucher(host
, krecipes
, args
->recipes_size
, &new_voucher
);
503 uint8_t *krecipes
= kalloc((vm_size_t
)args
->recipes_size
);
505 kr
= KERN_RESOURCE_SHORTAGE
;
509 if (copyin(CAST_USER_ADDR_T(args
->recipes
), (void *)krecipes
, args
->recipes_size
)) {
510 kfree(krecipes
, (vm_size_t
)args
->recipes_size
);
511 kr
= KERN_MEMORY_ERROR
;
515 kr
= host_create_mach_voucher(host
, krecipes
, args
->recipes_size
, &new_voucher
);
516 kfree(krecipes
, (vm_size_t
)args
->recipes_size
);
520 voucher_port
= convert_voucher_to_port(new_voucher
);
521 voucher_name
= ipc_port_copyout_send(voucher_port
, current_space());
523 kr
= copyout(&voucher_name
, args
->voucher
, sizeof(voucher_name
));
531 mach_voucher_extract_attr_recipe_trap(struct mach_voucher_extract_attr_recipe_args
*args
)
533 ipc_voucher_t voucher
= IV_NULL
;
534 kern_return_t kr
= KERN_SUCCESS
;
535 mach_msg_type_number_t sz
= 0;
537 if (copyin(args
->recipe_size
, (void *)&sz
, sizeof(sz
))) {
538 return KERN_MEMORY_ERROR
;
541 if (sz
> MACH_VOUCHER_ATTR_MAX_RAW_RECIPE_ARRAY_SIZE
) {
542 return MIG_ARRAY_TOO_LARGE
;
545 voucher
= convert_port_name_to_voucher(args
->voucher_name
);
546 if (voucher
== IV_NULL
) {
547 return MACH_SEND_INVALID_DEST
;
550 mach_msg_type_number_t max_sz
= sz
;
552 if (sz
< MACH_VOUCHER_TRAP_STACK_LIMIT
) {
553 /* keep small recipes on the stack for speed */
556 if (copyin(CAST_USER_ADDR_T(args
->recipe
), (void *)krecipe
, sz
)) {
557 kr
= KERN_MEMORY_ERROR
;
560 kr
= mach_voucher_extract_attr_recipe(voucher
, args
->key
,
561 (mach_voucher_attr_raw_recipe_t
)krecipe
, &sz
);
562 assert(sz
<= max_sz
);
564 if (kr
== KERN_SUCCESS
&& sz
> 0) {
565 kr
= copyout(krecipe
, CAST_USER_ADDR_T(args
->recipe
), sz
);
568 uint8_t *krecipe
= kalloc((vm_size_t
)max_sz
);
570 kr
= KERN_RESOURCE_SHORTAGE
;
574 if (copyin(CAST_USER_ADDR_T(args
->recipe
), (void *)krecipe
, sz
)) {
575 kfree(krecipe
, (vm_size_t
)max_sz
);
576 kr
= KERN_MEMORY_ERROR
;
580 kr
= mach_voucher_extract_attr_recipe(voucher
, args
->key
,
581 (mach_voucher_attr_raw_recipe_t
)krecipe
, &sz
);
582 assert(sz
<= max_sz
);
584 if (kr
== KERN_SUCCESS
&& sz
> 0) {
585 kr
= copyout(krecipe
, CAST_USER_ADDR_T(args
->recipe
), sz
);
587 kfree(krecipe
, (vm_size_t
)max_sz
);
590 if (kr
== KERN_SUCCESS
) {
591 kr
= copyout(&sz
, args
->recipe_size
, sizeof(sz
));
595 ipc_voucher_release(voucher
);