1*1031c584SApple OSS Distributions #include <stdlib.h>
2*1031c584SApple OSS Distributions #include <stdio.h>
3*1031c584SApple OSS Distributions #include <pthread.h>
4*1031c584SApple OSS Distributions #include <unistd.h>
5*1031c584SApple OSS Distributions #include <mach/mach.h>
6*1031c584SApple OSS Distributions #include <mach/task.h>
7*1031c584SApple OSS Distributions #include <assert.h>
8*1031c584SApple OSS Distributions
9*1031c584SApple OSS Distributions #define MAX_TEST_NUM 5
10*1031c584SApple OSS Distributions
11*1031c584SApple OSS Distributions static mach_port_t
alloc_server_port(void)12*1031c584SApple OSS Distributions alloc_server_port(void)
13*1031c584SApple OSS Distributions {
14*1031c584SApple OSS Distributions mach_port_t server_port = MACH_PORT_NULL;
15*1031c584SApple OSS Distributions kern_return_t kr;
16*1031c584SApple OSS Distributions
17*1031c584SApple OSS Distributions kr = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &server_port);
18*1031c584SApple OSS Distributions assert(kr == 0);
19*1031c584SApple OSS Distributions
20*1031c584SApple OSS Distributions kr = mach_port_insert_right(mach_task_self(), server_port, server_port, MACH_MSG_TYPE_MAKE_SEND);
21*1031c584SApple OSS Distributions assert(kr == 0);
22*1031c584SApple OSS Distributions
23*1031c584SApple OSS Distributions return server_port;
24*1031c584SApple OSS Distributions }
25*1031c584SApple OSS Distributions
26*1031c584SApple OSS Distributions static mach_port_t
alloc_provisional_reply_port()27*1031c584SApple OSS Distributions alloc_provisional_reply_port()
28*1031c584SApple OSS Distributions {
29*1031c584SApple OSS Distributions kern_return_t kr;
30*1031c584SApple OSS Distributions mach_port_t reply_port = MACH_PORT_NULL;
31*1031c584SApple OSS Distributions mach_port_t task = mach_task_self();
32*1031c584SApple OSS Distributions
33*1031c584SApple OSS Distributions mach_port_options_t opts = {
34*1031c584SApple OSS Distributions .flags = MPO_PROVISIONAL_REPLY_PORT | MPO_INSERT_SEND_RIGHT,
35*1031c584SApple OSS Distributions };
36*1031c584SApple OSS Distributions
37*1031c584SApple OSS Distributions kr = mach_port_construct(mach_task_self(), &opts, 0, &reply_port);
38*1031c584SApple OSS Distributions assert(kr == 0);
39*1031c584SApple OSS Distributions
40*1031c584SApple OSS Distributions return reply_port;
41*1031c584SApple OSS Distributions }
42*1031c584SApple OSS Distributions
43*1031c584SApple OSS Distributions static mach_port_t
alloc_reply_port()44*1031c584SApple OSS Distributions alloc_reply_port()
45*1031c584SApple OSS Distributions {
46*1031c584SApple OSS Distributions kern_return_t kr;
47*1031c584SApple OSS Distributions mach_port_t reply_port = MACH_PORT_NULL;
48*1031c584SApple OSS Distributions mach_port_t task = mach_task_self();
49*1031c584SApple OSS Distributions
50*1031c584SApple OSS Distributions mach_port_options_t opts = {
51*1031c584SApple OSS Distributions .flags = MPO_REPLY_PORT | MPO_INSERT_SEND_RIGHT,
52*1031c584SApple OSS Distributions };
53*1031c584SApple OSS Distributions
54*1031c584SApple OSS Distributions kr = mach_port_construct(mach_task_self(), &opts, 0, &reply_port);
55*1031c584SApple OSS Distributions assert(kr == 0);
56*1031c584SApple OSS Distributions
57*1031c584SApple OSS Distributions return reply_port;
58*1031c584SApple OSS Distributions }
59*1031c584SApple OSS Distributions
60*1031c584SApple OSS Distributions /* The rcv right of the port would be marked immovable. */
61*1031c584SApple OSS Distributions static void
test_immovable_receive_right(void)62*1031c584SApple OSS Distributions test_immovable_receive_right(void)
63*1031c584SApple OSS Distributions {
64*1031c584SApple OSS Distributions kern_return_t kr;
65*1031c584SApple OSS Distributions mach_port_t server_port = MACH_PORT_NULL, reply_port = MACH_PORT_NULL;
66*1031c584SApple OSS Distributions struct {
67*1031c584SApple OSS Distributions mach_msg_header_t header;
68*1031c584SApple OSS Distributions mach_msg_body_t body;
69*1031c584SApple OSS Distributions mach_msg_port_descriptor_t desc;
70*1031c584SApple OSS Distributions } msg;
71*1031c584SApple OSS Distributions
72*1031c584SApple OSS Distributions server_port = alloc_server_port();
73*1031c584SApple OSS Distributions reply_port = alloc_reply_port();
74*1031c584SApple OSS Distributions
75*1031c584SApple OSS Distributions msg.header.msgh_remote_port = server_port;
76*1031c584SApple OSS Distributions msg.header.msgh_local_port = MACH_PORT_NULL;
77*1031c584SApple OSS Distributions msg.header.msgh_bits = MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND, 0) | MACH_MSGH_BITS_COMPLEX;
78*1031c584SApple OSS Distributions msg.header.msgh_size = sizeof msg;
79*1031c584SApple OSS Distributions
80*1031c584SApple OSS Distributions msg.body.msgh_descriptor_count = 1;
81*1031c584SApple OSS Distributions
82*1031c584SApple OSS Distributions msg.desc.name = reply_port;
83*1031c584SApple OSS Distributions msg.desc.disposition = MACH_MSG_TYPE_MOVE_RECEIVE;
84*1031c584SApple OSS Distributions msg.desc.type = MACH_MSG_PORT_DESCRIPTOR;
85*1031c584SApple OSS Distributions
86*1031c584SApple OSS Distributions kr = mach_msg_send(&msg.header);
87*1031c584SApple OSS Distributions
88*1031c584SApple OSS Distributions printf("[reply_port_defense_client test_immovable_receive_right]: mach_msg2() returned %d\n", kr);
89*1031c584SApple OSS Distributions }
90*1031c584SApple OSS Distributions
91*1031c584SApple OSS Distributions /* The only way you could create a send once right is when you send the port in local port of a mach msg with MAKE_SEND_ONCE disposition. */
92*1031c584SApple OSS Distributions static void
test_make_send_once_right(void)93*1031c584SApple OSS Distributions test_make_send_once_right(void)
94*1031c584SApple OSS Distributions {
95*1031c584SApple OSS Distributions kern_return_t kr;
96*1031c584SApple OSS Distributions mach_port_t reply_port = alloc_reply_port();
97*1031c584SApple OSS Distributions kr = mach_port_insert_right(mach_task_self(), reply_port, reply_port, MACH_MSG_TYPE_MAKE_SEND_ONCE);
98*1031c584SApple OSS Distributions printf("[reply_port_defense_client test_make_send_once_right]: mach_port_insert_right() returned %d\n", kr);
99*1031c584SApple OSS Distributions }
100*1031c584SApple OSS Distributions
101*1031c584SApple OSS Distributions /* The send right of the port would only used for guarding a name in ipc space, it would not allow to send a message. */
102*1031c584SApple OSS Distributions static void
test_using_send_right(void)103*1031c584SApple OSS Distributions test_using_send_right(void)
104*1031c584SApple OSS Distributions {
105*1031c584SApple OSS Distributions kern_return_t kr;
106*1031c584SApple OSS Distributions mach_port_t reply_port = alloc_reply_port();
107*1031c584SApple OSS Distributions struct {
108*1031c584SApple OSS Distributions mach_msg_header_t header;
109*1031c584SApple OSS Distributions mach_msg_body_t body;
110*1031c584SApple OSS Distributions } msg;
111*1031c584SApple OSS Distributions
112*1031c584SApple OSS Distributions msg.header.msgh_remote_port = reply_port;
113*1031c584SApple OSS Distributions msg.header.msgh_local_port = MACH_PORT_NULL;
114*1031c584SApple OSS Distributions msg.header.msgh_bits = MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND, 0);
115*1031c584SApple OSS Distributions msg.header.msgh_size = sizeof msg;
116*1031c584SApple OSS Distributions
117*1031c584SApple OSS Distributions kr = mach_msg_send(&msg.header);
118*1031c584SApple OSS Distributions printf("[reply_port_defense_client test_using_send_right]: mach_msg2() returned %d\n", kr);
119*1031c584SApple OSS Distributions }
120*1031c584SApple OSS Distributions
121*1031c584SApple OSS Distributions /* The send right of the port would only used for guarding a name in ipc space, it would not allowed to get moved. */
122*1031c584SApple OSS Distributions static void
test_move_send_right(void)123*1031c584SApple OSS Distributions test_move_send_right(void)
124*1031c584SApple OSS Distributions {
125*1031c584SApple OSS Distributions kern_return_t kr;
126*1031c584SApple OSS Distributions mach_port_t server_port = MACH_PORT_NULL, reply_port = MACH_PORT_NULL;
127*1031c584SApple OSS Distributions struct {
128*1031c584SApple OSS Distributions mach_msg_header_t header;
129*1031c584SApple OSS Distributions mach_msg_body_t body;
130*1031c584SApple OSS Distributions mach_msg_port_descriptor_t desc;
131*1031c584SApple OSS Distributions } msg;
132*1031c584SApple OSS Distributions
133*1031c584SApple OSS Distributions server_port = alloc_server_port();
134*1031c584SApple OSS Distributions reply_port = alloc_reply_port();
135*1031c584SApple OSS Distributions
136*1031c584SApple OSS Distributions msg.header.msgh_remote_port = server_port;
137*1031c584SApple OSS Distributions msg.header.msgh_local_port = MACH_PORT_NULL;
138*1031c584SApple OSS Distributions msg.header.msgh_bits = MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND, 0) | MACH_MSGH_BITS_COMPLEX;
139*1031c584SApple OSS Distributions msg.header.msgh_size = sizeof msg;
140*1031c584SApple OSS Distributions
141*1031c584SApple OSS Distributions msg.body.msgh_descriptor_count = 1;
142*1031c584SApple OSS Distributions
143*1031c584SApple OSS Distributions msg.desc.name = reply_port;
144*1031c584SApple OSS Distributions msg.desc.disposition = MACH_MSG_TYPE_MOVE_SEND;
145*1031c584SApple OSS Distributions msg.desc.type = MACH_MSG_PORT_DESCRIPTOR;
146*1031c584SApple OSS Distributions
147*1031c584SApple OSS Distributions kr = mach_msg_send(&msg.header);
148*1031c584SApple OSS Distributions printf("[reply_port_defense_client test_move_send_right]: mach_msg2() returned %d\n", kr);
149*1031c584SApple OSS Distributions }
150*1031c584SApple OSS Distributions
151*1031c584SApple OSS Distributions static void
test_move_provisional_reply_port(void)152*1031c584SApple OSS Distributions test_move_provisional_reply_port(void)
153*1031c584SApple OSS Distributions {
154*1031c584SApple OSS Distributions kern_return_t kr;
155*1031c584SApple OSS Distributions mach_port_t server_port = MACH_PORT_NULL, reply_port = MACH_PORT_NULL;
156*1031c584SApple OSS Distributions struct {
157*1031c584SApple OSS Distributions mach_msg_header_t header;
158*1031c584SApple OSS Distributions mach_msg_body_t body;
159*1031c584SApple OSS Distributions mach_msg_port_descriptor_t desc;
160*1031c584SApple OSS Distributions } msg;
161*1031c584SApple OSS Distributions
162*1031c584SApple OSS Distributions server_port = alloc_server_port();
163*1031c584SApple OSS Distributions reply_port = alloc_provisional_reply_port();
164*1031c584SApple OSS Distributions
165*1031c584SApple OSS Distributions msg.header.msgh_remote_port = server_port;
166*1031c584SApple OSS Distributions msg.header.msgh_local_port = MACH_PORT_NULL;
167*1031c584SApple OSS Distributions msg.header.msgh_bits = MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND, 0) | MACH_MSGH_BITS_COMPLEX;
168*1031c584SApple OSS Distributions msg.header.msgh_size = sizeof msg;
169*1031c584SApple OSS Distributions
170*1031c584SApple OSS Distributions msg.body.msgh_descriptor_count = 1;
171*1031c584SApple OSS Distributions
172*1031c584SApple OSS Distributions msg.desc.name = reply_port;
173*1031c584SApple OSS Distributions msg.desc.disposition = MACH_MSG_TYPE_MOVE_RECEIVE;
174*1031c584SApple OSS Distributions msg.desc.type = MACH_MSG_PORT_DESCRIPTOR;
175*1031c584SApple OSS Distributions
176*1031c584SApple OSS Distributions kr = mach_msg_send(&msg.header);
177*1031c584SApple OSS Distributions
178*1031c584SApple OSS Distributions printf("[reply_port_defense_client test_immovable_receive_right]: mach_msg2() returned %d\n", kr);
179*1031c584SApple OSS Distributions }
180*1031c584SApple OSS Distributions
181*1031c584SApple OSS Distributions int
main(int argc,char * argv[])182*1031c584SApple OSS Distributions main(int argc, char *argv[])
183*1031c584SApple OSS Distributions {
184*1031c584SApple OSS Distributions printf("[reply_port_defense_client]: My Pid: %d\n", getpid());
185*1031c584SApple OSS Distributions
186*1031c584SApple OSS Distributions void (*tests[MAX_TEST_NUM])(void) = {
187*1031c584SApple OSS Distributions test_immovable_receive_right,
188*1031c584SApple OSS Distributions test_make_send_once_right,
189*1031c584SApple OSS Distributions test_using_send_right,
190*1031c584SApple OSS Distributions test_move_send_right,
191*1031c584SApple OSS Distributions test_move_provisional_reply_port
192*1031c584SApple OSS Distributions };
193*1031c584SApple OSS Distributions
194*1031c584SApple OSS Distributions if (argc < 2) {
195*1031c584SApple OSS Distributions printf("[reply_port_defense_client]: Specify a test to run.");
196*1031c584SApple OSS Distributions exit(-1);
197*1031c584SApple OSS Distributions }
198*1031c584SApple OSS Distributions
199*1031c584SApple OSS Distributions int test_num = atoi(argv[1]);
200*1031c584SApple OSS Distributions if (test_num >= 0 && test_num < MAX_TEST_NUM) {
201*1031c584SApple OSS Distributions (*tests[test_num])();
202*1031c584SApple OSS Distributions } else {
203*1031c584SApple OSS Distributions printf("[reply_port_defense_client]: Invalid test num. Exiting...\n");
204*1031c584SApple OSS Distributions exit(-1);
205*1031c584SApple OSS Distributions }
206*1031c584SApple OSS Distributions exit(0);
207*1031c584SApple OSS Distributions }
208