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