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