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