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