1*a1e26a70SApple OSS Distributions #include <stdio.h> 2*a1e26a70SApple OSS Distributions #include <mach/message.h> 3*a1e26a70SApple OSS Distributions #include <mach/mach_vm.h> 4*a1e26a70SApple OSS Distributions #include <mach/mach_port.h> 5*a1e26a70SApple OSS Distributions #include <mach/mach_error.h> 6*a1e26a70SApple OSS Distributions #include <sys/sysctl.h> 7*a1e26a70SApple OSS Distributions #include <sys/wait.h> 8*a1e26a70SApple OSS Distributions #include <unistd.h> 9*a1e26a70SApple OSS Distributions 10*a1e26a70SApple OSS Distributions #include <darwintest.h> 11*a1e26a70SApple OSS Distributions #include <darwintest_utils.h> 12*a1e26a70SApple OSS Distributions 13*a1e26a70SApple OSS Distributions #include <xpc/private.h> 14*a1e26a70SApple OSS Distributions 15*a1e26a70SApple OSS Distributions T_GLOBAL_META(T_META_RUN_CONCURRENTLY(true), 16*a1e26a70SApple OSS Distributions T_META_RADAR_COMPONENT_NAME("xnu"), 17*a1e26a70SApple OSS Distributions T_META_RADAR_COMPONENT_VERSION("IPC"), 18*a1e26a70SApple OSS Distributions T_META_NAMESPACE("xnu.ipc"), 19*a1e26a70SApple OSS Distributions T_META_TAG_VM_PREFERRED); 20*a1e26a70SApple OSS Distributions 21*a1e26a70SApple OSS Distributions struct test_msg { 22*a1e26a70SApple OSS Distributions mach_msg_header_t header; 23*a1e26a70SApple OSS Distributions mach_msg_trailer_t trailer; // subtract this when sending 24*a1e26a70SApple OSS Distributions }; 25*a1e26a70SApple OSS Distributions 26*a1e26a70SApple OSS Distributions T_DECL(bootstrap_mig_always_filtered, 27*a1e26a70SApple OSS Distributions "'MIG' messages to bootstrap ports from tasks with filtering should always be filtered", 28*a1e26a70SApple OSS Distributions T_META_ASROOT(true), T_META_REQUIRES_SYSCTL_EQ("kern.development", 1)) 29*a1e26a70SApple OSS Distributions { 30*a1e26a70SApple OSS Distributions int new_filter_flag = 1; 31*a1e26a70SApple OSS Distributions int rc = sysctlbyname("kern.task_set_filter_msg_flag", NULL, NULL, 32*a1e26a70SApple OSS Distributions &new_filter_flag, sizeof(new_filter_flag)); 33*a1e26a70SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(rc, "sysctlbyname"); 34*a1e26a70SApple OSS Distributions 35*a1e26a70SApple OSS Distributions struct mach_service_port_info mspi = { 36*a1e26a70SApple OSS Distributions .mspi_domain_type = XPC_DOMAIN_PORT, 37*a1e26a70SApple OSS Distributions }; 38*a1e26a70SApple OSS Distributions strlcpy(mspi.mspi_string_name, "com.apple.xnu.test_bootstrap_msgfilter", 39*a1e26a70SApple OSS Distributions sizeof(mspi.mspi_string_name)); 40*a1e26a70SApple OSS Distributions 41*a1e26a70SApple OSS Distributions mach_port_options_t port_opts = { 42*a1e26a70SApple OSS Distributions .flags = MPO_SERVICE_PORT | 43*a1e26a70SApple OSS Distributions MPO_INSERT_SEND_RIGHT | 44*a1e26a70SApple OSS Distributions MPO_CONTEXT_AS_GUARD | 45*a1e26a70SApple OSS Distributions MPO_STRICT, 46*a1e26a70SApple OSS Distributions .service_port_info = &mspi, 47*a1e26a70SApple OSS Distributions }; 48*a1e26a70SApple OSS Distributions 49*a1e26a70SApple OSS Distributions int ctxobj = 0; 50*a1e26a70SApple OSS Distributions 51*a1e26a70SApple OSS Distributions mach_port_t test_bootstrap_port; 52*a1e26a70SApple OSS Distributions kern_return_t kr = mach_port_construct(mach_task_self(), &port_opts, 53*a1e26a70SApple OSS Distributions (uintptr_t)&ctxobj, &test_bootstrap_port); 54*a1e26a70SApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "mach_port_construct"); 55*a1e26a70SApple OSS Distributions 56*a1e26a70SApple OSS Distributions // sending a valid 'XPC' msgid should succeed 57*a1e26a70SApple OSS Distributions 58*a1e26a70SApple OSS Distributions mach_msg_id_t permitted_xpc_msgid = 0x01000042; 59*a1e26a70SApple OSS Distributions 60*a1e26a70SApple OSS Distributions struct test_msg msg = { 61*a1e26a70SApple OSS Distributions .header = { 62*a1e26a70SApple OSS Distributions .msgh_bits = MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND, 0), 63*a1e26a70SApple OSS Distributions .msgh_size = offsetof(struct test_msg, trailer), 64*a1e26a70SApple OSS Distributions .msgh_remote_port = test_bootstrap_port, 65*a1e26a70SApple OSS Distributions .msgh_id = permitted_xpc_msgid, 66*a1e26a70SApple OSS Distributions }, 67*a1e26a70SApple OSS Distributions }; 68*a1e26a70SApple OSS Distributions 69*a1e26a70SApple OSS Distributions mach_msg_option_t msg_opts = MACH_SEND_MSG | MACH_RCV_MSG; 70*a1e26a70SApple OSS Distributions kr = mach_msg(&msg.header, msg_opts, msg.header.msgh_size, sizeof(msg), 71*a1e26a70SApple OSS Distributions test_bootstrap_port, MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL); 72*a1e26a70SApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "send message with valid (XPC) message ID"); 73*a1e26a70SApple OSS Distributions 74*a1e26a70SApple OSS Distributions // sending a 'MIG' msgid (0x00xxxxxx) should fail, non-fatally 75*a1e26a70SApple OSS Distributions 76*a1e26a70SApple OSS Distributions mach_msg_id_t disallowed_mig_msgid = 0x00000042; 77*a1e26a70SApple OSS Distributions msg_opts |= MACH_SEND_FILTER_NONFATAL; 78*a1e26a70SApple OSS Distributions 79*a1e26a70SApple OSS Distributions msg = (struct test_msg){ 80*a1e26a70SApple OSS Distributions .header = { 81*a1e26a70SApple OSS Distributions .msgh_bits = MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND, 0), 82*a1e26a70SApple OSS Distributions .msgh_size = offsetof(struct test_msg, trailer), 83*a1e26a70SApple OSS Distributions .msgh_remote_port = test_bootstrap_port, 84*a1e26a70SApple OSS Distributions .msgh_id = disallowed_mig_msgid, 85*a1e26a70SApple OSS Distributions }, 86*a1e26a70SApple OSS Distributions }; 87*a1e26a70SApple OSS Distributions kr = mach_msg(&msg.header, msg_opts, msg.header.msgh_size, sizeof(msg), 88*a1e26a70SApple OSS Distributions test_bootstrap_port, MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL); 89*a1e26a70SApple OSS Distributions T_ASSERT_EQ(kr, MACH_SEND_MSG_FILTERED, "message should be filtered"); 90*a1e26a70SApple OSS Distributions } 91