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