1*8d741a5dSApple OSS Distributions /*
2*8d741a5dSApple OSS Distributions * Test voucher trap APIs.
3*8d741a5dSApple OSS Distributions * There was an unfortunate bug in the trap interface that used the user space
4*8d741a5dSApple OSS Distributions * _address_ of a trap parameter as a copyin size. This test validates there
5*8d741a5dSApple OSS Distributions * are no other kernel panics in the voucher create and voucher attribute
6*8d741a5dSApple OSS Distributions * extraction mach traps.
7*8d741a5dSApple OSS Distributions *
8*8d741a5dSApple OSS Distributions * clang -o voucher_traps voucher_traps.c -ldarwintest -Weverything -Wno-gnu-flexible-array-initializer
9*8d741a5dSApple OSS Distributions *
10*8d741a5dSApple OSS Distributions * <rdar://problem/29379175>
11*8d741a5dSApple OSS Distributions */
12*8d741a5dSApple OSS Distributions
13*8d741a5dSApple OSS Distributions #include <stdint.h>
14*8d741a5dSApple OSS Distributions #include <stdlib.h>
15*8d741a5dSApple OSS Distributions #include <mach/mach.h>
16*8d741a5dSApple OSS Distributions #include <mach/mach_vm.h>
17*8d741a5dSApple OSS Distributions #include <mach/mach_traps.h>
18*8d741a5dSApple OSS Distributions
19*8d741a5dSApple OSS Distributions #include <atm/atm_types.h>
20*8d741a5dSApple OSS Distributions
21*8d741a5dSApple OSS Distributions #include <darwintest.h>
22*8d741a5dSApple OSS Distributions
23*8d741a5dSApple OSS Distributions T_GLOBAL_META(
24*8d741a5dSApple OSS Distributions T_META_NAMESPACE("xnu.ipc"),
25*8d741a5dSApple OSS Distributions T_META_RUN_CONCURRENTLY(TRUE),
26*8d741a5dSApple OSS Distributions T_META_RADAR_COMPONENT_NAME("xnu"),
27*8d741a5dSApple OSS Distributions T_META_RADAR_COMPONENT_VERSION("IPC"));
28*8d741a5dSApple OSS Distributions
29*8d741a5dSApple OSS Distributions static mach_port_t
get_user_data_port(mach_msg_type_number_t * size)30*8d741a5dSApple OSS Distributions get_user_data_port(mach_msg_type_number_t *size)
31*8d741a5dSApple OSS Distributions {
32*8d741a5dSApple OSS Distributions #define DATA "Hello World!"
33*8d741a5dSApple OSS Distributions struct {
34*8d741a5dSApple OSS Distributions mach_voucher_attr_recipe_data_t recipe;
35*8d741a5dSApple OSS Distributions char data[sizeof(DATA)];
36*8d741a5dSApple OSS Distributions } buf = {
37*8d741a5dSApple OSS Distributions .recipe = {
38*8d741a5dSApple OSS Distributions .key = MACH_VOUCHER_ATTR_KEY_USER_DATA,
39*8d741a5dSApple OSS Distributions .command = MACH_VOUCHER_ATTR_USER_DATA_STORE,
40*8d741a5dSApple OSS Distributions .content_size = sizeof(DATA),
41*8d741a5dSApple OSS Distributions },
42*8d741a5dSApple OSS Distributions .data = DATA,
43*8d741a5dSApple OSS Distributions };
44*8d741a5dSApple OSS Distributions
45*8d741a5dSApple OSS Distributions mach_port_t port = MACH_PORT_NULL;
46*8d741a5dSApple OSS Distributions kern_return_t kr = host_create_mach_voucher(mach_host_self(),
47*8d741a5dSApple OSS Distributions (mach_voucher_attr_raw_recipe_array_t)&buf,
48*8d741a5dSApple OSS Distributions sizeof(buf), &port);
49*8d741a5dSApple OSS Distributions #if !TARGET_OS_OSX
50*8d741a5dSApple OSS Distributions T_ASSERT_NE(kr, KERN_SUCCESS, "User data manager removed on embedded");
51*8d741a5dSApple OSS Distributions T_END;
52*8d741a5dSApple OSS Distributions #endif
53*8d741a5dSApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "Create USER_DATA voucher: 0x%x",
54*8d741a5dSApple OSS Distributions (unsigned int)port);
55*8d741a5dSApple OSS Distributions
56*8d741a5dSApple OSS Distributions if (size) {
57*8d741a5dSApple OSS Distributions *size = sizeof(buf);
58*8d741a5dSApple OSS Distributions }
59*8d741a5dSApple OSS Distributions return port;
60*8d741a5dSApple OSS Distributions }
61*8d741a5dSApple OSS Distributions
62*8d741a5dSApple OSS Distributions
63*8d741a5dSApple OSS Distributions T_DECL(voucher_extract_attr_recipe, "voucher_extract_attr_recipe", T_META_TAG_VM_PREFERRED)
64*8d741a5dSApple OSS Distributions {
65*8d741a5dSApple OSS Distributions kern_return_t kr;
66*8d741a5dSApple OSS Distributions mach_vm_size_t alloc_sz;
67*8d741a5dSApple OSS Distributions mach_port_t port;
68*8d741a5dSApple OSS Distributions mach_vm_address_t alloc_addr;
69*8d741a5dSApple OSS Distributions mach_msg_type_number_t expected_size;
70*8d741a5dSApple OSS Distributions
71*8d741a5dSApple OSS Distributions /* map at least a page of memory at some arbitrary location */
72*8d741a5dSApple OSS Distributions alloc_sz = (mach_vm_size_t)round_page(MACH_VOUCHER_TRAP_STACK_LIMIT + 1);
73*8d741a5dSApple OSS Distributions
74*8d741a5dSApple OSS Distributions /*
75*8d741a5dSApple OSS Distributions * We could theoretically ask for a fixed location, but this is more
76*8d741a5dSApple OSS Distributions * reliable, and we're not actually trying to exploit anything - a
77*8d741a5dSApple OSS Distributions * kernel panic on failure should suffice :-)
78*8d741a5dSApple OSS Distributions */
79*8d741a5dSApple OSS Distributions alloc_addr = (mach_vm_address_t)round_page(MACH_VOUCHER_ATTR_MAX_RAW_RECIPE_ARRAY_SIZE + 1);
80*8d741a5dSApple OSS Distributions kr = mach_vm_allocate(mach_task_self(), &alloc_addr,
81*8d741a5dSApple OSS Distributions alloc_sz, VM_FLAGS_ANYWHERE);
82*8d741a5dSApple OSS Distributions
83*8d741a5dSApple OSS Distributions /*
84*8d741a5dSApple OSS Distributions * Make sure that the address of the allocation is larger than the
85*8d741a5dSApple OSS Distributions * maximum recipe size: this will test for the bug that was fixed in
86*8d741a5dSApple OSS Distributions * <rdar://problem/29379175>.
87*8d741a5dSApple OSS Distributions */
88*8d741a5dSApple OSS Distributions T_ASSERT_GT_ULLONG((uint64_t)alloc_addr,
89*8d741a5dSApple OSS Distributions (uint64_t)MACH_VOUCHER_ATTR_MAX_RAW_RECIPE_ARRAY_SIZE,
90*8d741a5dSApple OSS Distributions "Recipe addr (%llu bytes): 0x%llx > max recipe sz: %llu",
91*8d741a5dSApple OSS Distributions (uint64_t)alloc_sz, (uint64_t)alloc_addr,
92*8d741a5dSApple OSS Distributions (uint64_t)MACH_VOUCHER_ATTR_MAX_RAW_RECIPE_ARRAY_SIZE);
93*8d741a5dSApple OSS Distributions
94*8d741a5dSApple OSS Distributions /* make the allocation look like a pointer to an int */
95*8d741a5dSApple OSS Distributions mach_msg_type_number_t *recipe_size;
96*8d741a5dSApple OSS Distributions recipe_size = (mach_msg_type_number_t *)((uintptr_t)alloc_addr);
97*8d741a5dSApple OSS Distributions bzero(recipe_size, (unsigned long)alloc_sz);
98*8d741a5dSApple OSS Distributions if (alloc_sz > MACH_VOUCHER_ATTR_MAX_RAW_RECIPE_ARRAY_SIZE) {
99*8d741a5dSApple OSS Distributions *recipe_size = MACH_VOUCHER_ATTR_MAX_RAW_RECIPE_ARRAY_SIZE;
100*8d741a5dSApple OSS Distributions } else {
101*8d741a5dSApple OSS Distributions *recipe_size = (mach_msg_type_number_t)alloc_sz;
102*8d741a5dSApple OSS Distributions }
103*8d741a5dSApple OSS Distributions
104*8d741a5dSApple OSS Distributions /* recipe buffer on the heap: memset it so panics show up loudly */
105*8d741a5dSApple OSS Distributions size_t size = (size_t)(10 * 1024 * 1024);
106*8d741a5dSApple OSS Distributions void *recipe = malloc(size);
107*8d741a5dSApple OSS Distributions memset(recipe, 0x41, size);
108*8d741a5dSApple OSS Distributions
109*8d741a5dSApple OSS Distributions port = get_user_data_port(&expected_size);
110*8d741a5dSApple OSS Distributions
111*8d741a5dSApple OSS Distributions /*
112*8d741a5dSApple OSS Distributions * This should try to extract the USER_DATA attribute using a buffer on the
113*8d741a5dSApple OSS Distributions * kernel heap (probably zone memory).
114*8d741a5dSApple OSS Distributions */
115*8d741a5dSApple OSS Distributions kr = mach_voucher_extract_attr_recipe_trap(port,
116*8d741a5dSApple OSS Distributions MACH_VOUCHER_ATTR_KEY_USER_DATA, recipe, recipe_size);
117*8d741a5dSApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "Extract attribute data with recipe: heap");
118*8d741a5dSApple OSS Distributions T_ASSERT_EQ(*recipe_size, expected_size, "size should match");
119*8d741a5dSApple OSS Distributions
120*8d741a5dSApple OSS Distributions /* reset the recipe memory */
121*8d741a5dSApple OSS Distributions memset(recipe, 0x41, size);
122*8d741a5dSApple OSS Distributions /* reduce the size to get an allocation on the kernel stack */
123*8d741a5dSApple OSS Distributions *recipe_size = MACH_VOUCHER_TRAP_STACK_LIMIT - 1;
124*8d741a5dSApple OSS Distributions
125*8d741a5dSApple OSS Distributions /*
126*8d741a5dSApple OSS Distributions * This should try to extract the USER_DATA attribute using a buffer on the
127*8d741a5dSApple OSS Distributions * kernel stack.
128*8d741a5dSApple OSS Distributions */
129*8d741a5dSApple OSS Distributions kr = mach_voucher_extract_attr_recipe_trap(port,
130*8d741a5dSApple OSS Distributions MACH_VOUCHER_ATTR_KEY_USER_DATA, recipe, recipe_size);
131*8d741a5dSApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "Extract attribute data with recipe: stack");
132*8d741a5dSApple OSS Distributions T_ASSERT_EQ(*recipe_size, expected_size, "size should match");
133*8d741a5dSApple OSS Distributions
134*8d741a5dSApple OSS Distributions /* cleanup */
135*8d741a5dSApple OSS Distributions
136*8d741a5dSApple OSS Distributions free(recipe);
137*8d741a5dSApple OSS Distributions kr = mach_vm_deallocate(mach_task_self(), alloc_addr, alloc_sz);
138*8d741a5dSApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "Deallocate recipe buffers");
139*8d741a5dSApple OSS Distributions }
140