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())
51 if (copyin(args
->addr
, (char *)&addr
, sizeof (addr
)))
54 rv
= mach_vm_allocate(task
->map
, &addr
, args
->size
, args
->flags
);
55 if (rv
== KERN_SUCCESS
)
56 rv
= copyout(&addr
, args
->addr
, sizeof (addr
));
60 task_deallocate(task
);
65 _kernelrpc_mach_vm_deallocate_trap(struct _kernelrpc_mach_vm_deallocate_args
*args
)
67 task_t task
= port_name_to_task(args
->target
);
68 int rv
= MACH_SEND_INVALID_DEST
;
70 if (task
!= current_task())
73 rv
= mach_vm_deallocate(task
->map
, args
->address
, args
->size
);
77 task_deallocate(task
);
82 _kernelrpc_mach_vm_protect_trap(struct _kernelrpc_mach_vm_protect_args
*args
)
84 task_t task
= port_name_to_task(args
->target
);
85 int rv
= MACH_SEND_INVALID_DEST
;
87 if (task
!= current_task())
90 rv
= mach_vm_protect(task
->map
, args
->address
, args
->size
,
91 args
->set_maximum
, args
->new_protection
);
95 task_deallocate(task
);
100 _kernelrpc_mach_vm_map_trap(struct _kernelrpc_mach_vm_map_trap_args
*args
)
102 mach_vm_offset_t addr
;
103 task_t task
= port_name_to_task(args
->target
);
104 int rv
= MACH_SEND_INVALID_DEST
;
106 if (task
!= current_task())
109 if (copyin(args
->addr
, (char *)&addr
, sizeof (addr
)))
112 rv
= mach_vm_map(task
->map
, &addr
, args
->size
, args
->mask
, args
->flags
,
113 IPC_PORT_NULL
, 0, FALSE
, args
->cur_protection
, VM_PROT_ALL
,
115 if (rv
== KERN_SUCCESS
)
116 rv
= copyout(&addr
, args
->addr
, sizeof (addr
));
120 task_deallocate(task
);
125 _kernelrpc_mach_vm_purgable_control_trap(
126 struct _kernelrpc_mach_vm_purgable_control_trap_args
*args
)
129 task_t task
= port_name_to_task(args
->target
);
130 int rv
= MACH_SEND_INVALID_DEST
;
132 if (task
!= current_task())
135 if (copyin(args
->state
, (char *)&state
, sizeof (state
)))
138 rv
= mach_vm_purgable_control(task
->map
,
142 if (rv
== KERN_SUCCESS
)
143 rv
= copyout(&state
, args
->state
, sizeof (state
));
147 task_deallocate(task
);
152 _kernelrpc_mach_port_allocate_trap(struct _kernelrpc_mach_port_allocate_args
*args
)
154 task_t task
= port_name_to_task(args
->target
);
155 mach_port_name_t name
;
156 int rv
= MACH_SEND_INVALID_DEST
;
158 if (task
!= current_task())
161 rv
= mach_port_allocate(task
->itk_space
, args
->right
, &name
);
162 if (rv
== KERN_SUCCESS
)
163 rv
= copyout(&name
, args
->name
, sizeof (name
));
168 task_deallocate(task
);
173 _kernelrpc_mach_port_destroy_trap(struct _kernelrpc_mach_port_destroy_args
*args
)
175 task_t task
= port_name_to_task(args
->target
);
176 int rv
= MACH_SEND_INVALID_DEST
;
178 if (task
!= current_task())
181 rv
= mach_port_destroy(task
->itk_space
, args
->name
);
185 task_deallocate(task
);
190 _kernelrpc_mach_port_deallocate_trap(struct _kernelrpc_mach_port_deallocate_args
*args
)
192 task_t task
= port_name_to_task(args
->target
);
193 int rv
= MACH_SEND_INVALID_DEST
;
195 if (task
!= current_task())
198 rv
= mach_port_deallocate(task
->itk_space
, args
->name
);
202 task_deallocate(task
);
207 _kernelrpc_mach_port_mod_refs_trap(struct _kernelrpc_mach_port_mod_refs_args
*args
)
209 task_t task
= port_name_to_task(args
->target
);
210 int rv
= MACH_SEND_INVALID_DEST
;
212 if (task
!= current_task())
215 rv
= mach_port_mod_refs(task
->itk_space
, args
->name
, args
->right
, args
->delta
);
219 task_deallocate(task
);
225 _kernelrpc_mach_port_move_member_trap(struct _kernelrpc_mach_port_move_member_args
*args
)
227 task_t task
= port_name_to_task(args
->target
);
228 int rv
= MACH_SEND_INVALID_DEST
;
230 if (task
!= current_task())
233 rv
= mach_port_move_member(task
->itk_space
, args
->member
, args
->after
);
237 task_deallocate(task
);
242 _kernelrpc_mach_port_insert_right_trap(struct _kernelrpc_mach_port_insert_right_args
*args
)
244 task_t task
= port_name_to_task(args
->target
);
246 mach_msg_type_name_t disp
;
247 int rv
= MACH_SEND_INVALID_DEST
;
249 if (task
!= current_task())
252 rv
= ipc_object_copyin(task
->itk_space
, args
->poly
, args
->polyPoly
,
253 (ipc_object_t
*)&port
);
254 if (rv
!= KERN_SUCCESS
)
256 disp
= ipc_object_copyin_type(args
->polyPoly
);
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
);
267 task_deallocate(task
);
272 _kernelrpc_mach_port_insert_member_trap(struct _kernelrpc_mach_port_insert_member_args
*args
)
274 task_t task
= port_name_to_task(args
->target
);
275 int rv
= MACH_SEND_INVALID_DEST
;
277 if (task
!= current_task())
280 rv
= mach_port_insert_member(task
->itk_space
, args
->name
, args
->pset
);
284 task_deallocate(task
);
290 _kernelrpc_mach_port_extract_member_trap(struct _kernelrpc_mach_port_extract_member_args
*args
)
292 task_t task
= port_name_to_task(args
->target
);
293 int rv
= MACH_SEND_INVALID_DEST
;
295 if (task
!= current_task())
298 rv
= mach_port_extract_member(task
->itk_space
, args
->name
, args
->pset
);
302 task_deallocate(task
);
307 _kernelrpc_mach_port_construct_trap(struct _kernelrpc_mach_port_construct_args
*args
)
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
;
314 if (copyin(args
->options
, (char *)&options
, sizeof (options
))) {
315 rv
= MACH_SEND_INVALID_DATA
;
319 if (task
!= current_task())
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
));
328 task_deallocate(task
);
333 _kernelrpc_mach_port_destruct_trap(struct _kernelrpc_mach_port_destruct_args
*args
)
335 task_t task
= port_name_to_task(args
->target
);
336 int rv
= MACH_SEND_INVALID_DEST
;
338 if (task
!= current_task())
341 rv
= mach_port_destruct(task
->itk_space
, args
->name
, args
->srdelta
, args
->guard
);
345 task_deallocate(task
);
350 _kernelrpc_mach_port_guard_trap(struct _kernelrpc_mach_port_guard_args
*args
)
352 task_t task
= port_name_to_task(args
->target
);
353 int rv
= MACH_SEND_INVALID_DEST
;
355 if (task
!= current_task())
358 rv
= mach_port_guard(task
->itk_space
, args
->name
, args
->guard
, args
->strict
);
362 task_deallocate(task
);
367 _kernelrpc_mach_port_unguard_trap(struct _kernelrpc_mach_port_unguard_args
*args
)
369 task_t task
= port_name_to_task(args
->target
);
370 int rv
= MACH_SEND_INVALID_DEST
;
372 if (task
!= current_task())
375 rv
= mach_port_unguard(task
->itk_space
, args
->name
, args
->guard
);
379 task_deallocate(task
);
384 host_create_mach_voucher_trap(struct host_create_mach_voucher_args
*args
)
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;
392 if (host
== HOST_NULL
)
393 return MACH_SEND_INVALID_DEST
;
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
;
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(args
->recipes
, (void *)krecipes
, args
->recipes_size
)) {
404 kr
= KERN_MEMORY_ERROR
;
407 kr
= host_create_mach_voucher(host
, krecipes
, args
->recipes_size
, &new_voucher
);
409 uint8_t *krecipes
= kalloc((vm_size_t
)args
->recipes_size
);
411 kr
= KERN_RESOURCE_SHORTAGE
;
415 if (copyin(args
->recipes
, (void *)krecipes
, args
->recipes_size
)) {
416 kfree(krecipes
, (vm_size_t
)args
->recipes_size
);
417 kr
= KERN_MEMORY_ERROR
;
421 kr
= host_create_mach_voucher(host
, krecipes
, args
->recipes_size
, &new_voucher
);
422 kfree(krecipes
, (vm_size_t
)args
->recipes_size
);
426 voucher_port
= convert_voucher_to_port(new_voucher
);
427 voucher_name
= ipc_port_copyout_send(voucher_port
, current_space());
429 kr
= copyout(&voucher_name
, args
->voucher
, sizeof(voucher_name
));
437 mach_voucher_extract_attr_recipe_trap(struct mach_voucher_extract_attr_recipe_args
*args
)
439 ipc_voucher_t voucher
= IV_NULL
;
440 kern_return_t kr
= KERN_SUCCESS
;
441 mach_msg_type_number_t sz
= 0;
443 if (copyin(args
->recipe_size
, (void *)&sz
, sizeof(sz
)))
444 return KERN_MEMORY_ERROR
;
446 if (sz
> MACH_VOUCHER_ATTR_MAX_RAW_RECIPE_ARRAY_SIZE
)
447 return MIG_ARRAY_TOO_LARGE
;
449 voucher
= convert_port_name_to_voucher(args
->voucher_name
);
450 if (voucher
== IV_NULL
)
451 return MACH_SEND_INVALID_DEST
;
453 mach_msg_type_number_t max_sz
= sz
;
455 if (sz
< MACH_VOUCHER_TRAP_STACK_LIMIT
) {
456 /* keep small recipes on the stack for speed */
458 if (copyin(args
->recipe
, (void *)krecipe
, sz
)) {
459 kr
= KERN_MEMORY_ERROR
;
462 kr
= mach_voucher_extract_attr_recipe(voucher
, args
->key
,
463 (mach_voucher_attr_raw_recipe_t
)krecipe
, &sz
);
464 assert(sz
<= max_sz
);
466 if (kr
== KERN_SUCCESS
&& sz
> 0)
467 kr
= copyout(krecipe
, (void *)args
->recipe
, sz
);
469 uint8_t *krecipe
= kalloc((vm_size_t
)max_sz
);
471 kr
= KERN_RESOURCE_SHORTAGE
;
475 if (copyin(args
->recipe
, (void *)krecipe
, sz
)) {
476 kfree(krecipe
, (vm_size_t
)max_sz
);
477 kr
= KERN_MEMORY_ERROR
;
481 kr
= mach_voucher_extract_attr_recipe(voucher
, args
->key
,
482 (mach_voucher_attr_raw_recipe_t
)krecipe
, &sz
);
483 assert(sz
<= max_sz
);
485 if (kr
== KERN_SUCCESS
&& sz
> 0)
486 kr
= copyout(krecipe
, (void *)args
->recipe
, sz
);
487 kfree(krecipe
, (vm_size_t
)max_sz
);
490 kr
= copyout(&sz
, args
->recipe_size
, sizeof(sz
));
493 ipc_voucher_release(voucher
);