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