xref: /xnu-8796.141.3/tests/persona_adoption.c (revision 1b191cb58250d0705d8a51287127505aa4bc0789)
1*1b191cb5SApple OSS Distributions #include <darwintest.h>
2*1b191cb5SApple OSS Distributions #include <darwintest_multiprocess.h>
3*1b191cb5SApple OSS Distributions 
4*1b191cb5SApple OSS Distributions #include <bank/bank_types.h>
5*1b191cb5SApple OSS Distributions #include <libproc.h>
6*1b191cb5SApple OSS Distributions #include <mach/mach.h>
7*1b191cb5SApple OSS Distributions #include <mach/mach_voucher.h>
8*1b191cb5SApple OSS Distributions #include <mach/mach_voucher_types.h>
9*1b191cb5SApple OSS Distributions #include <os/voucher_private.h>
10*1b191cb5SApple OSS Distributions #include <sys/kauth.h>
11*1b191cb5SApple OSS Distributions #include <sys/persona.h>
12*1b191cb5SApple OSS Distributions #include <sys/proc_info.h>
13*1b191cb5SApple OSS Distributions #include <unistd.h>
14*1b191cb5SApple OSS Distributions #include <uuid/uuid.h>
15*1b191cb5SApple OSS Distributions 
16*1b191cb5SApple OSS Distributions T_GLOBAL_META(
17*1b191cb5SApple OSS Distributions 	T_META_NAMESPACE("xnu.persona_adoption"),
18*1b191cb5SApple OSS Distributions 	T_META_CHECK_LEAKS(false),
19*1b191cb5SApple OSS Distributions 	T_META_RUN_CONCURRENTLY(true),
20*1b191cb5SApple OSS Distributions 	T_META_ENABLED(!TARGET_OS_WATCH) // rdar://81809878
21*1b191cb5SApple OSS Distributions 	);
22*1b191cb5SApple OSS Distributions 
23*1b191cb5SApple OSS Distributions static uid_t
_persona_create(int persona_type,uid_t persona_uid)24*1b191cb5SApple OSS Distributions _persona_create(int persona_type, uid_t persona_uid)
25*1b191cb5SApple OSS Distributions {
26*1b191cb5SApple OSS Distributions 	struct kpersona_info pinfo = {
27*1b191cb5SApple OSS Distributions 		.persona_info_version = PERSONA_INFO_V2,
28*1b191cb5SApple OSS Distributions 		.persona_type = persona_type,
29*1b191cb5SApple OSS Distributions 		.persona_uid = persona_uid,
30*1b191cb5SApple OSS Distributions 	};
31*1b191cb5SApple OSS Distributions 
32*1b191cb5SApple OSS Distributions 	uuid_t uuid;
33*1b191cb5SApple OSS Distributions 	uuid_generate(uuid);
34*1b191cb5SApple OSS Distributions 	uuid_string_t uuid_string;
35*1b191cb5SApple OSS Distributions 	uuid_unparse(uuid, uuid_string);
36*1b191cb5SApple OSS Distributions 	snprintf(pinfo.persona_name, MAXLOGNAME, "persona_adoption_test.%s", uuid_string);
37*1b191cb5SApple OSS Distributions 
38*1b191cb5SApple OSS Distributions 	uid_t persona_id = 0;
39*1b191cb5SApple OSS Distributions 	int ret = kpersona_alloc(&pinfo, &persona_id);
40*1b191cb5SApple OSS Distributions 	T_WITH_ERRNO; T_ASSERT_EQ(ret, 0, NULL);
41*1b191cb5SApple OSS Distributions 	T_ASSERT_GT(persona_id, 0, NULL);
42*1b191cb5SApple OSS Distributions 
43*1b191cb5SApple OSS Distributions 	return persona_id;
44*1b191cb5SApple OSS Distributions }
45*1b191cb5SApple OSS Distributions 
46*1b191cb5SApple OSS Distributions static kern_return_t
_persona_try_adopting(uid_t persona_id)47*1b191cb5SApple OSS Distributions _persona_try_adopting(uid_t persona_id)
48*1b191cb5SApple OSS Distributions {
49*1b191cb5SApple OSS Distributions 	struct proc_uniqidentifierinfo uniqidinfo;
50*1b191cb5SApple OSS Distributions 	int error = proc_pidinfo(getpid(), PROC_PIDUNIQIDENTIFIERINFO, 0, &uniqidinfo, sizeof(uniqidinfo));
51*1b191cb5SApple OSS Distributions 	T_ASSERT_GT(error, 0, NULL);
52*1b191cb5SApple OSS Distributions 
53*1b191cb5SApple OSS Distributions 	struct persona_modify_info pmi = {
54*1b191cb5SApple OSS Distributions 		.persona_id = persona_id,
55*1b191cb5SApple OSS Distributions 		.unique_pid = uniqidinfo.p_uniqueid,
56*1b191cb5SApple OSS Distributions 	};
57*1b191cb5SApple OSS Distributions 
58*1b191cb5SApple OSS Distributions 	mach_voucher_t current_voucher = MACH_VOUCHER_NULL;
59*1b191cb5SApple OSS Distributions 	kern_return_t kr = mach_voucher_persona_self(&current_voucher);
60*1b191cb5SApple OSS Distributions 	T_ASSERT_EQ(kr, 0, NULL);
61*1b191cb5SApple OSS Distributions 	T_ASSERT_NE(current_voucher, MACH_VOUCHER_NULL, NULL);
62*1b191cb5SApple OSS Distributions 
63*1b191cb5SApple OSS Distributions 	char voucher_buf[sizeof(mach_voucher_attr_recipe_data_t) + sizeof(pmi)];
64*1b191cb5SApple OSS Distributions 
65*1b191cb5SApple OSS Distributions 	mach_voucher_attr_recipe_t recipe = (mach_voucher_attr_recipe_t)&voucher_buf[0];
66*1b191cb5SApple OSS Distributions 	recipe->key = MACH_VOUCHER_ATTR_KEY_BANK;
67*1b191cb5SApple OSS Distributions 	recipe->command = MACH_VOUCHER_ATTR_BANK_MODIFY_PERSONA;
68*1b191cb5SApple OSS Distributions 	recipe->content_size = sizeof(pmi);
69*1b191cb5SApple OSS Distributions 	recipe->previous_voucher = current_voucher;
70*1b191cb5SApple OSS Distributions 	memcpy(recipe->content, (void *)&pmi, sizeof(pmi));
71*1b191cb5SApple OSS Distributions 
72*1b191cb5SApple OSS Distributions 	mach_voucher_attr_raw_recipe_size_t recipe_size = sizeof(mach_voucher_attr_recipe_data_t) + recipe->content_size;
73*1b191cb5SApple OSS Distributions 	mach_voucher_attr_raw_recipe_array_t recipes = (mach_voucher_attr_raw_recipe_array_t)&voucher_buf[0];
74*1b191cb5SApple OSS Distributions 	mach_voucher_t mach_voucher = MACH_VOUCHER_NULL;
75*1b191cb5SApple OSS Distributions 	kr = host_create_mach_voucher(mach_host_self(), recipes, recipe_size, &mach_voucher);
76*1b191cb5SApple OSS Distributions 	if (kr != 0) {
77*1b191cb5SApple OSS Distributions 		return kr;
78*1b191cb5SApple OSS Distributions 	}
79*1b191cb5SApple OSS Distributions 	T_ASSERT_NE(mach_voucher, MACH_VOUCHER_NULL, NULL);
80*1b191cb5SApple OSS Distributions 
81*1b191cb5SApple OSS Distributions 	/* Verify that persona is set on the voucher */
82*1b191cb5SApple OSS Distributions 	uint32_t voucher_persona;
83*1b191cb5SApple OSS Distributions 	mach_voucher_attr_content_t content_out = (mach_voucher_attr_content_t)&voucher_persona;
84*1b191cb5SApple OSS Distributions 	mach_voucher_attr_content_size_t content_out_size = sizeof(voucher_persona);
85*1b191cb5SApple OSS Distributions 	kr = mach_voucher_attr_command(mach_voucher, MACH_VOUCHER_ATTR_KEY_BANK, BANK_PERSONA_ID, NULL, 0, content_out, &content_out_size);
86*1b191cb5SApple OSS Distributions 	if (kr != 0) {
87*1b191cb5SApple OSS Distributions 		return kr;
88*1b191cb5SApple OSS Distributions 	}
89*1b191cb5SApple OSS Distributions 	T_ASSERT_EQ(voucher_persona, persona_id, NULL);
90*1b191cb5SApple OSS Distributions 
91*1b191cb5SApple OSS Distributions 	kr = thread_set_mach_voucher(mach_thread_self(), mach_voucher);
92*1b191cb5SApple OSS Distributions 	return kr;
93*1b191cb5SApple OSS Distributions }
94*1b191cb5SApple OSS Distributions 
95*1b191cb5SApple OSS Distributions T_DECL(persona_with_matching_uid_can_be_adopted,
96*1b191cb5SApple OSS Distributions     "persona with UID matching at-spawn value can be adopted")
97*1b191cb5SApple OSS Distributions {
98*1b191cb5SApple OSS Distributions 	struct kpersona_info info = {
99*1b191cb5SApple OSS Distributions 		.persona_info_version = PERSONA_INFO_V2,
100*1b191cb5SApple OSS Distributions 	};
101*1b191cb5SApple OSS Distributions 	int error = kpersona_info(0, &info);
102*1b191cb5SApple OSS Distributions 	if (error != 0) {
103*1b191cb5SApple OSS Distributions 		T_SKIP("Test requrires to be running in a persona, skipping");
104*1b191cb5SApple OSS Distributions 	}
105*1b191cb5SApple OSS Distributions 
106*1b191cb5SApple OSS Distributions 	uid_t created_persona = _persona_create(PERSONA_MANAGED, info.persona_uid);
107*1b191cb5SApple OSS Distributions 	kern_return_t kr = _persona_try_adopting(created_persona);
108*1b191cb5SApple OSS Distributions 	T_ASSERT_EQ(kr, 0, NULL);
109*1b191cb5SApple OSS Distributions 
110*1b191cb5SApple OSS Distributions 	uid_t current_persona = PERSONA_ID_NONE;
111*1b191cb5SApple OSS Distributions 	T_ASSERT_EQ(kpersona_get(&current_persona), 0, NULL);
112*1b191cb5SApple OSS Distributions 	T_ASSERT_EQ(current_persona, created_persona, NULL);
113*1b191cb5SApple OSS Distributions 
114*1b191cb5SApple OSS Distributions 	T_ASSERT_EQ(kpersona_dealloc(created_persona), 0, NULL);
115*1b191cb5SApple OSS Distributions }
116*1b191cb5SApple OSS Distributions 
117*1b191cb5SApple OSS Distributions T_DECL(persona_with_mismatched_uid_cannot_be_adopted,
118*1b191cb5SApple OSS Distributions     "persona with UID that doesn't match at-spawn value cannot be adopted")
119*1b191cb5SApple OSS Distributions {
120*1b191cb5SApple OSS Distributions 	struct kpersona_info info = {
121*1b191cb5SApple OSS Distributions 		.persona_info_version = PERSONA_INFO_V2,
122*1b191cb5SApple OSS Distributions 	};
123*1b191cb5SApple OSS Distributions 	int error = kpersona_info(0, &info);
124*1b191cb5SApple OSS Distributions 	if (error != 0) {
125*1b191cb5SApple OSS Distributions 		T_SKIP("Test requrires to be running in a persona, skipping");
126*1b191cb5SApple OSS Distributions 	}
127*1b191cb5SApple OSS Distributions 
128*1b191cb5SApple OSS Distributions 	uid_t mismatched_uid = info.persona_uid + 1;
129*1b191cb5SApple OSS Distributions 	uid_t created_persona = _persona_create(PERSONA_MANAGED, mismatched_uid);
130*1b191cb5SApple OSS Distributions 	kern_return_t kr = _persona_try_adopting(created_persona);
131*1b191cb5SApple OSS Distributions 	T_ASSERT_NE(kr, 0, NULL);
132*1b191cb5SApple OSS Distributions 
133*1b191cb5SApple OSS Distributions 	uid_t current_persona = PERSONA_ID_NONE;
134*1b191cb5SApple OSS Distributions 	T_ASSERT_EQ(kpersona_get(&current_persona), 0, NULL);
135*1b191cb5SApple OSS Distributions 	T_ASSERT_EQ(current_persona, info.persona_id, NULL);
136*1b191cb5SApple OSS Distributions 
137*1b191cb5SApple OSS Distributions 	T_ASSERT_EQ(kpersona_dealloc(created_persona), 0, NULL);
138*1b191cb5SApple OSS Distributions }
139*1b191cb5SApple OSS Distributions 
140*1b191cb5SApple OSS Distributions #if !TARGET_OS_BRIDGE // PersonaEnterprise is not supported on bridgeOS
141*1b191cb5SApple OSS Distributions 
142*1b191cb5SApple OSS Distributions static uid_t _helper_persona = PERSONA_ID_NONE;
143*1b191cb5SApple OSS Distributions 
144*1b191cb5SApple OSS Distributions static void
_run_helper_in_persona_cleanup(void)145*1b191cb5SApple OSS Distributions _run_helper_in_persona_cleanup(void)
146*1b191cb5SApple OSS Distributions {
147*1b191cb5SApple OSS Distributions 	kpersona_dealloc(_helper_persona);
148*1b191cb5SApple OSS Distributions }
149*1b191cb5SApple OSS Distributions 
150*1b191cb5SApple OSS Distributions static void __attribute__((noreturn))
_run_helper_in_persona(const char * helper_name,int persona_type)151*1b191cb5SApple OSS Distributions _run_helper_in_persona(const char *helper_name, int persona_type)
152*1b191cb5SApple OSS Distributions {
153*1b191cb5SApple OSS Distributions 	struct kpersona_info info = {
154*1b191cb5SApple OSS Distributions 		.persona_info_version = PERSONA_INFO_V2,
155*1b191cb5SApple OSS Distributions 	};
156*1b191cb5SApple OSS Distributions 	int error = kpersona_info(0, &info);
157*1b191cb5SApple OSS Distributions 	uid_t persona_uid = (error == 0) ? info.persona_uid : geteuid();
158*1b191cb5SApple OSS Distributions 	_helper_persona = _persona_create(persona_type, persona_uid);
159*1b191cb5SApple OSS Distributions 	T_ATEND(_run_helper_in_persona_cleanup);
160*1b191cb5SApple OSS Distributions 
161*1b191cb5SApple OSS Distributions 	xpc_object_t plist = xpc_dictionary_create_empty();
162*1b191cb5SApple OSS Distributions 	xpc_dictionary_set_bool(plist, "RunAtLoad", true);
163*1b191cb5SApple OSS Distributions 	xpc_dictionary_set_int64(plist, "PersonaEnterprise", _helper_persona);
164*1b191cb5SApple OSS Distributions 	dt_helper_t helper = dt_launchd_helper_plist(plist, helper_name, LAUNCH_SYSTEM_DOMAIN, NULL, NULL);
165*1b191cb5SApple OSS Distributions 
166*1b191cb5SApple OSS Distributions 	dt_run_helpers(&helper, 1, 300);
167*1b191cb5SApple OSS Distributions }
168*1b191cb5SApple OSS Distributions 
169*1b191cb5SApple OSS Distributions T_HELPER_DECL(own_persona_can_be_adopted_impl,
170*1b191cb5SApple OSS Distributions     "own_persona_can_be_adopted helper spawned into persona type that prohibits adoption")
171*1b191cb5SApple OSS Distributions {
172*1b191cb5SApple OSS Distributions 	struct kpersona_info info = {
173*1b191cb5SApple OSS Distributions 		.persona_info_version = PERSONA_INFO_V2,
174*1b191cb5SApple OSS Distributions 	};
175*1b191cb5SApple OSS Distributions 	int error = kpersona_info(0, &info);
176*1b191cb5SApple OSS Distributions 	T_ASSERT_EQ(error, 0, NULL);
177*1b191cb5SApple OSS Distributions 
178*1b191cb5SApple OSS Distributions 	kern_return_t kr = _persona_try_adopting(info.persona_id);
179*1b191cb5SApple OSS Distributions 	T_ASSERT_EQ(kr, 0, NULL);
180*1b191cb5SApple OSS Distributions }
181*1b191cb5SApple OSS Distributions 
182*1b191cb5SApple OSS Distributions T_DECL(own_persona_can_be_adopted,
183*1b191cb5SApple OSS Distributions     "process spawned into a persona type that prohibits adoption can adopt own persona")
184*1b191cb5SApple OSS Distributions {
185*1b191cb5SApple OSS Distributions 	_run_helper_in_persona("own_persona_can_be_adopted_impl", PERSONA_MANAGED);
186*1b191cb5SApple OSS Distributions }
187*1b191cb5SApple OSS Distributions 
188*1b191cb5SApple OSS Distributions #endif // TARGET_OS_BRIDGE
189