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