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