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 if (args
->name
== args
->poly
) {
281 switch (args
->polyPoly
) {
282 case MACH_MSG_TYPE_MAKE_SEND
:
283 case MACH_MSG_TYPE_COPY_SEND
:
284 /* fastpath MAKE_SEND / COPY_SEND which is the most common case */
285 rv
= ipc_object_insert_send_right(task
->itk_space
, args
->poly
,
294 rv
= ipc_object_copyin(task
->itk_space
, args
->poly
, args
->polyPoly
,
295 (ipc_object_t
*)&port
, 0, NULL
, IPC_KMSG_FLAGS_ALLOW_IMMOVABLE_SEND
);
296 if (rv
!= KERN_SUCCESS
) {
299 disp
= ipc_object_copyin_type(args
->polyPoly
);
301 rv
= mach_port_insert_right(task
->itk_space
, args
->name
, port
, disp
);
302 if (rv
!= KERN_SUCCESS
&& IP_VALID(port
)) {
303 ipc_object_destroy(ip_to_object(port
), disp
);
308 task_deallocate(task
);
314 _kernelrpc_mach_port_get_attributes_trap(struct _kernelrpc_mach_port_get_attributes_args
*args
)
316 task_inspect_t task
= port_name_to_task_inspect(args
->target
);
317 int rv
= MACH_SEND_INVALID_DEST
;
318 mach_msg_type_number_t count
;
320 if (task
!= current_task()) {
324 // MIG does not define the type or size of the mach_port_info_t out array
325 // anywhere, so derive them from the field in the generated reply struct
326 #define MACH_PORT_INFO_OUT (((__Reply__mach_port_get_attributes_t*)NULL)->port_info_out)
327 #define MACH_PORT_INFO_STACK_LIMIT 80 // current size is 68 == 17 * sizeof(integer_t)
328 _Static_assert(sizeof(MACH_PORT_INFO_OUT
) < MACH_PORT_INFO_STACK_LIMIT
,
329 "mach_port_info_t has grown significantly, reevaluate stack usage");
330 const mach_msg_type_number_t max_count
= (sizeof(MACH_PORT_INFO_OUT
) / sizeof(MACH_PORT_INFO_OUT
[0]));
331 typeof(MACH_PORT_INFO_OUT
[0]) info
[max_count
];
334 * zero out our stack buffer because not all flavors of
335 * port_get_attributes initialize the whole struct
337 bzero(info
, sizeof(MACH_PORT_INFO_OUT
));
339 if (copyin(CAST_USER_ADDR_T(args
->count
), &count
, sizeof(count
))) {
340 rv
= MACH_SEND_INVALID_DATA
;
343 if (count
> max_count
) {
347 rv
= mach_port_get_attributes(task
->itk_space
, args
->name
, args
->flavor
, info
, &count
);
348 if (rv
== KERN_SUCCESS
) {
349 rv
= copyout(&count
, CAST_USER_ADDR_T(args
->count
), sizeof(count
));
351 if (rv
== KERN_SUCCESS
&& count
> 0) {
352 rv
= copyout(info
, CAST_USER_ADDR_T(args
->info
), count
* sizeof(info
[0]));
357 task_deallocate(task
);
363 _kernelrpc_mach_port_insert_member_trap(struct _kernelrpc_mach_port_insert_member_args
*args
)
365 task_t task
= port_name_to_task(args
->target
);
366 int rv
= MACH_SEND_INVALID_DEST
;
368 if (task
!= current_task()) {
372 rv
= mach_port_insert_member(task
->itk_space
, args
->name
, args
->pset
);
376 task_deallocate(task
);
383 _kernelrpc_mach_port_extract_member_trap(struct _kernelrpc_mach_port_extract_member_args
*args
)
385 task_t task
= port_name_to_task(args
->target
);
386 int rv
= MACH_SEND_INVALID_DEST
;
388 if (task
!= current_task()) {
392 rv
= mach_port_extract_member(task
->itk_space
, args
->name
, args
->pset
);
396 task_deallocate(task
);
402 _kernelrpc_mach_port_construct_trap(struct _kernelrpc_mach_port_construct_args
*args
)
404 task_t task
= port_name_to_task(args
->target
);
405 mach_port_name_t name
;
406 int rv
= MACH_SEND_INVALID_DEST
;
407 mach_port_options_t options
;
409 if (copyin(args
->options
, (char *)&options
, sizeof(options
))) {
410 rv
= MACH_SEND_INVALID_DATA
;
414 if (task
!= current_task()) {
418 rv
= mach_port_construct(task
->itk_space
, &options
, args
->context
, &name
);
419 if (rv
== KERN_SUCCESS
) {
420 rv
= copyout(&name
, args
->name
, sizeof(name
));
425 task_deallocate(task
);
431 _kernelrpc_mach_port_destruct_trap(struct _kernelrpc_mach_port_destruct_args
*args
)
433 task_t task
= port_name_to_task(args
->target
);
434 int rv
= MACH_SEND_INVALID_DEST
;
436 if (task
!= current_task()) {
440 rv
= mach_port_destruct(task
->itk_space
, args
->name
, args
->srdelta
, args
->guard
);
444 task_deallocate(task
);
450 _kernelrpc_mach_port_guard_trap(struct _kernelrpc_mach_port_guard_args
*args
)
452 task_t task
= port_name_to_task(args
->target
);
453 int rv
= MACH_SEND_INVALID_DEST
;
455 if (task
!= current_task()) {
459 rv
= mach_port_guard(task
->itk_space
, args
->name
, args
->guard
, args
->strict
);
463 task_deallocate(task
);
469 _kernelrpc_mach_port_unguard_trap(struct _kernelrpc_mach_port_unguard_args
*args
)
471 task_t task
= port_name_to_task(args
->target
);
472 int rv
= MACH_SEND_INVALID_DEST
;
474 if (task
!= current_task()) {
478 rv
= mach_port_unguard(task
->itk_space
, args
->name
, args
->guard
);
482 task_deallocate(task
);
488 _kernelrpc_mach_port_type_trap(struct _kernelrpc_mach_port_type_args
*args
)
490 task_t task
= port_name_to_task(args
->target
);
491 int rv
= MACH_SEND_INVALID_DEST
;
492 mach_port_type_t type
;
494 if (task
!= current_task()) {
498 rv
= mach_port_type(task
->itk_space
, args
->name
, &type
);
499 if (rv
== KERN_SUCCESS
) {
500 rv
= copyout(&type
, args
->ptype
, sizeof(type
));
505 task_deallocate(task
);
511 _kernelrpc_mach_port_request_notification_trap(
512 struct _kernelrpc_mach_port_request_notification_args
*args
)
514 task_t task
= port_name_to_task(args
->target
);
515 int rv
= MACH_SEND_INVALID_DEST
;
516 ipc_port_t notify
, previous
;
517 mach_msg_type_name_t disp
;
518 mach_port_name_t previous_name
= MACH_PORT_NULL
;
520 if (task
!= current_task()) {
524 disp
= ipc_object_copyin_type(args
->notifyPoly
);
525 if (disp
!= MACH_MSG_TYPE_PORT_SEND_ONCE
) {
529 if (MACH_PORT_VALID(args
->notify
)) {
530 rv
= ipc_object_copyin(task
->itk_space
, args
->notify
, args
->notifyPoly
,
531 (ipc_object_t
*)¬ify
, 0, NULL
, 0);
533 notify
= CAST_MACH_NAME_TO_PORT(args
->notify
);
535 if (rv
!= KERN_SUCCESS
) {
539 rv
= mach_port_request_notification(task
->itk_space
, args
->name
,
540 args
->msgid
, args
->sync
, notify
, &previous
);
541 if (rv
!= KERN_SUCCESS
) {
542 ipc_object_destroy(ip_to_object(notify
), disp
);
546 if (IP_VALID(previous
)) {
547 // Remove once <rdar://problem/45522961> is fixed.
548 // We need to make ith_knote NULL as ipc_object_copyout() uses
549 // thread-argument-passing and its value should not be garbage
550 current_thread()->ith_knote
= ITH_KNOTE_NULL
;
551 rv
= ipc_object_copyout(task
->itk_space
, ip_to_object(previous
),
552 MACH_MSG_TYPE_PORT_SEND_ONCE
, NULL
, NULL
, &previous_name
);
553 if (rv
!= KERN_SUCCESS
) {
554 ipc_object_destroy(ip_to_object(previous
),
555 MACH_MSG_TYPE_PORT_SEND_ONCE
);
560 rv
= copyout(&previous_name
, args
->previous
, sizeof(previous_name
));
564 task_deallocate(task
);
570 host_create_mach_voucher_trap(struct host_create_mach_voucher_args
*args
)
572 host_t host
= port_name_to_host(args
->host
);
573 ipc_voucher_t new_voucher
= IV_NULL
;
574 ipc_port_t voucher_port
= IPC_PORT_NULL
;
575 mach_port_name_t voucher_name
= 0;
576 kern_return_t kr
= 0;
578 if (host
== HOST_NULL
) {
579 return MACH_SEND_INVALID_DEST
;
582 if (args
->recipes_size
< 0) {
583 return KERN_INVALID_ARGUMENT
;
584 } else if (args
->recipes_size
> MACH_VOUCHER_ATTR_MAX_RAW_RECIPE_ARRAY_SIZE
) {
585 return MIG_ARRAY_TOO_LARGE
;
588 if (args
->recipes_size
< MACH_VOUCHER_TRAP_STACK_LIMIT
) {
589 /* keep small recipes on the stack for speed */
590 uint8_t krecipes
[args
->recipes_size
];
591 if (copyin(CAST_USER_ADDR_T(args
->recipes
), (void *)krecipes
, args
->recipes_size
)) {
592 kr
= KERN_MEMORY_ERROR
;
595 kr
= host_create_mach_voucher(host
, krecipes
, args
->recipes_size
, &new_voucher
);
597 uint8_t *krecipes
= kalloc((vm_size_t
)args
->recipes_size
);
599 kr
= KERN_RESOURCE_SHORTAGE
;
603 if (copyin(CAST_USER_ADDR_T(args
->recipes
), (void *)krecipes
, args
->recipes_size
)) {
604 kfree(krecipes
, (vm_size_t
)args
->recipes_size
);
605 kr
= KERN_MEMORY_ERROR
;
609 kr
= host_create_mach_voucher(host
, krecipes
, args
->recipes_size
, &new_voucher
);
610 kfree(krecipes
, (vm_size_t
)args
->recipes_size
);
614 voucher_port
= convert_voucher_to_port(new_voucher
);
615 voucher_name
= ipc_port_copyout_send(voucher_port
, current_space());
617 kr
= copyout(&voucher_name
, args
->voucher
, sizeof(voucher_name
));
625 mach_voucher_extract_attr_recipe_trap(struct mach_voucher_extract_attr_recipe_args
*args
)
627 ipc_voucher_t voucher
= IV_NULL
;
628 kern_return_t kr
= KERN_SUCCESS
;
629 mach_msg_type_number_t sz
= 0;
631 if (copyin(args
->recipe_size
, (void *)&sz
, sizeof(sz
))) {
632 return KERN_MEMORY_ERROR
;
635 if (sz
> MACH_VOUCHER_ATTR_MAX_RAW_RECIPE_ARRAY_SIZE
) {
636 return MIG_ARRAY_TOO_LARGE
;
639 voucher
= convert_port_name_to_voucher(args
->voucher_name
);
640 if (voucher
== IV_NULL
) {
641 return MACH_SEND_INVALID_DEST
;
644 mach_msg_type_number_t max_sz
= sz
;
646 if (sz
< MACH_VOUCHER_TRAP_STACK_LIMIT
) {
647 /* keep small recipes on the stack for speed */
650 if (copyin(CAST_USER_ADDR_T(args
->recipe
), (void *)krecipe
, sz
)) {
651 kr
= KERN_MEMORY_ERROR
;
654 kr
= mach_voucher_extract_attr_recipe(voucher
, args
->key
,
655 (mach_voucher_attr_raw_recipe_t
)krecipe
, &sz
);
656 assert(sz
<= max_sz
);
658 if (kr
== KERN_SUCCESS
&& sz
> 0) {
659 kr
= copyout(krecipe
, CAST_USER_ADDR_T(args
->recipe
), sz
);
662 uint8_t *krecipe
= kalloc((vm_size_t
)max_sz
);
664 kr
= KERN_RESOURCE_SHORTAGE
;
668 if (copyin(CAST_USER_ADDR_T(args
->recipe
), (void *)krecipe
, sz
)) {
669 kfree(krecipe
, (vm_size_t
)max_sz
);
670 kr
= KERN_MEMORY_ERROR
;
674 kr
= mach_voucher_extract_attr_recipe(voucher
, args
->key
,
675 (mach_voucher_attr_raw_recipe_t
)krecipe
, &sz
);
676 assert(sz
<= max_sz
);
678 if (kr
== KERN_SUCCESS
&& sz
> 0) {
679 kr
= copyout(krecipe
, CAST_USER_ADDR_T(args
->recipe
), sz
);
681 kfree(krecipe
, (vm_size_t
)max_sz
);
684 if (kr
== KERN_SUCCESS
) {
685 kr
= copyout(&sz
, args
->recipe_size
, sizeof(sz
));
689 ipc_voucher_release(voucher
);