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