xref: /xnu-8020.121.3/tests/immovable_rights.c (revision fdd8201d7b966f0c3ea610489d29bd841d358941)
1*fdd8201dSApple OSS Distributions #include <darwintest.h>
2*fdd8201dSApple OSS Distributions #include <servers/bootstrap.h>
3*fdd8201dSApple OSS Distributions #include <mach/mach.h>
4*fdd8201dSApple OSS Distributions #include <mach/message.h>
5*fdd8201dSApple OSS Distributions #include <stdlib.h>
6*fdd8201dSApple OSS Distributions #include <sys/sysctl.h>
7*fdd8201dSApple OSS Distributions #include <unistd.h>
8*fdd8201dSApple OSS Distributions #include <mach/port.h>
9*fdd8201dSApple OSS Distributions #include <mach/mach_port.h>
10*fdd8201dSApple OSS Distributions 
11*fdd8201dSApple OSS Distributions T_GLOBAL_META(T_META_RUN_CONCURRENTLY(true));
12*fdd8201dSApple OSS Distributions 
13*fdd8201dSApple OSS Distributions T_DECL(immovable_rights, "Create a port with immovable receive rights") {
14*fdd8201dSApple OSS Distributions 	mach_port_t imm_port;
15*fdd8201dSApple OSS Distributions 	mach_port_options_t opts = {
16*fdd8201dSApple OSS Distributions 		.flags = MPO_CONTEXT_AS_GUARD | MPO_IMMOVABLE_RECEIVE
17*fdd8201dSApple OSS Distributions 	};
18*fdd8201dSApple OSS Distributions 	kern_return_t kr;
19*fdd8201dSApple OSS Distributions 
20*fdd8201dSApple OSS Distributions 	kr = mach_port_construct(mach_task_self(), &opts, 0x10, &imm_port);
21*fdd8201dSApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "mach_port_construct");
22*fdd8201dSApple OSS Distributions 
23*fdd8201dSApple OSS Distributions 	mach_port_status_t status;
24*fdd8201dSApple OSS Distributions 	mach_msg_type_number_t status_size = MACH_PORT_RECEIVE_STATUS_COUNT;
25*fdd8201dSApple OSS Distributions 	kr = mach_port_get_attributes(mach_task_self(), imm_port,
26*fdd8201dSApple OSS Distributions 	    MACH_PORT_RECEIVE_STATUS, (mach_port_info_t)&status, &status_size);
27*fdd8201dSApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "mach_port_get_attributes");
28*fdd8201dSApple OSS Distributions 	T_LOG("Status flags %d", status.mps_flags);
29*fdd8201dSApple OSS Distributions 	T_ASSERT_NE(0, (status.mps_flags & MACH_PORT_STATUS_FLAG_GUARD_IMMOVABLE_RECEIVE), "Imm rcv bit is set");
30*fdd8201dSApple OSS Distributions 
31*fdd8201dSApple OSS Distributions 	mach_port_t imm_port2;
32*fdd8201dSApple OSS Distributions 	mach_port_options_t opts2 = {};
33*fdd8201dSApple OSS Distributions 
34*fdd8201dSApple OSS Distributions 	kr = mach_port_construct(mach_task_self(), &opts2, 0, &imm_port2);
35*fdd8201dSApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "mach_port_construct");
36*fdd8201dSApple OSS Distributions 
37*fdd8201dSApple OSS Distributions 	kr = mach_port_guard_with_flags(mach_task_self(), imm_port2, 0x11, (uint64_t)MPG_IMMOVABLE_RECEIVE);
38*fdd8201dSApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "mach_port_guard_with_flags");
39*fdd8201dSApple OSS Distributions 
40*fdd8201dSApple OSS Distributions 	kr = mach_port_get_attributes(mach_task_self(), imm_port2,
41*fdd8201dSApple OSS Distributions 	    MACH_PORT_RECEIVE_STATUS, (mach_port_info_t)&status, &status_size);
42*fdd8201dSApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "mach_port_get_attributes");
43*fdd8201dSApple OSS Distributions 	T_LOG("Status flags %d", status.mps_flags);
44*fdd8201dSApple OSS Distributions 	T_ASSERT_NE(0, (status.mps_flags & MACH_PORT_STATUS_FLAG_GUARD_IMMOVABLE_RECEIVE), "Imm rcv bit is set");
45*fdd8201dSApple OSS Distributions 
46*fdd8201dSApple OSS Distributions 	kr = mach_port_swap_guard(mach_task_self(), imm_port2, 0x11, 0xde18);
47*fdd8201dSApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "mach_port_swap_guard");
48*fdd8201dSApple OSS Distributions 
49*fdd8201dSApple OSS Distributions 	kr = mach_port_unguard(mach_task_self(), imm_port2, 0xde18);
50*fdd8201dSApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "mach_port_unguard");
51*fdd8201dSApple OSS Distributions }
52