xref: /xnu-10002.81.5/libsyscall/mach/mach_right.c (revision 5e3eaea39dcf651e66cb99ba7d70e32cc4a99587)
1*5e3eaea3SApple OSS Distributions /*
2*5e3eaea3SApple OSS Distributions  * Copyright (c) 2018 Apple Inc. All rights reserved.
3*5e3eaea3SApple OSS Distributions  *
4*5e3eaea3SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*5e3eaea3SApple OSS Distributions  *
6*5e3eaea3SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*5e3eaea3SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*5e3eaea3SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*5e3eaea3SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*5e3eaea3SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*5e3eaea3SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*5e3eaea3SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*5e3eaea3SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*5e3eaea3SApple OSS Distributions  *
15*5e3eaea3SApple OSS Distributions  * Please obtain a copy of the License at
16*5e3eaea3SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*5e3eaea3SApple OSS Distributions  *
18*5e3eaea3SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*5e3eaea3SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*5e3eaea3SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*5e3eaea3SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*5e3eaea3SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*5e3eaea3SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*5e3eaea3SApple OSS Distributions  * limitations under the License.
25*5e3eaea3SApple OSS Distributions  *
26*5e3eaea3SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*5e3eaea3SApple OSS Distributions  */
28*5e3eaea3SApple OSS Distributions #include <mach/mach.h>
29*5e3eaea3SApple OSS Distributions #include <mach/mach_traps.h>
30*5e3eaea3SApple OSS Distributions #include <mach/mach_port.h>
31*5e3eaea3SApple OSS Distributions #include <mach/mach_right.h>
32*5e3eaea3SApple OSS Distributions 
33*5e3eaea3SApple OSS Distributions 
34*5e3eaea3SApple OSS Distributions #pragma mark Utilities
35*5e3eaea3SApple OSS Distributions #define _mach_assert(__op, __kr) \
36*5e3eaea3SApple OSS Distributions 	do { \
37*5e3eaea3SApple OSS Distributions 	        if (kr != KERN_SUCCESS) { \
38*5e3eaea3SApple OSS Distributions 	                __builtin_trap(); \
39*5e3eaea3SApple OSS Distributions 	        } \
40*5e3eaea3SApple OSS Distributions 	} while (0)
41*5e3eaea3SApple OSS Distributions 
42*5e3eaea3SApple OSS Distributions #pragma mark API
43*5e3eaea3SApple OSS Distributions mach_right_recv_t
mach_right_recv_construct(mach_right_flags_t flags,mach_right_send_t * _Nullable sr,uintptr_t ctx)44*5e3eaea3SApple OSS Distributions mach_right_recv_construct(mach_right_flags_t flags,
45*5e3eaea3SApple OSS Distributions     mach_right_send_t *_Nullable sr, uintptr_t ctx)
46*5e3eaea3SApple OSS Distributions {
47*5e3eaea3SApple OSS Distributions 	kern_return_t kr = KERN_FAILURE;
48*5e3eaea3SApple OSS Distributions 	mach_port_t p = MACH_PORT_NULL;
49*5e3eaea3SApple OSS Distributions 	mach_port_options_t opts = {
50*5e3eaea3SApple OSS Distributions 		.flags = MPO_CONTEXT_AS_GUARD,
51*5e3eaea3SApple OSS Distributions 		.mpl = {
52*5e3eaea3SApple OSS Distributions 			.mpl_qlimit = MACH_PORT_QLIMIT_BASIC,
53*5e3eaea3SApple OSS Distributions 		},
54*5e3eaea3SApple OSS Distributions 	};
55*5e3eaea3SApple OSS Distributions 
56*5e3eaea3SApple OSS Distributions 	if (flags & MACH_RIGHT_RECV_FLAG_UNGUARDED) {
57*5e3eaea3SApple OSS Distributions 		opts.flags &= (~MPO_CONTEXT_AS_GUARD);
58*5e3eaea3SApple OSS Distributions 	}
59*5e3eaea3SApple OSS Distributions 	if (sr) {
60*5e3eaea3SApple OSS Distributions 		opts.flags |= MPO_INSERT_SEND_RIGHT;
61*5e3eaea3SApple OSS Distributions 	}
62*5e3eaea3SApple OSS Distributions 
63*5e3eaea3SApple OSS Distributions 	kr = mach_port_construct(mach_task_self(), &opts, ctx, &p);
64*5e3eaea3SApple OSS Distributions 	_mach_assert("construct recv right", kr);
65*5e3eaea3SApple OSS Distributions 
66*5e3eaea3SApple OSS Distributions 	if (sr) {
67*5e3eaea3SApple OSS Distributions 		sr->mrs_name = p;
68*5e3eaea3SApple OSS Distributions 	}
69*5e3eaea3SApple OSS Distributions 
70*5e3eaea3SApple OSS Distributions 	return mach_right_recv(p);
71*5e3eaea3SApple OSS Distributions }
72*5e3eaea3SApple OSS Distributions 
73*5e3eaea3SApple OSS Distributions void
mach_right_recv_destruct(mach_right_recv_t r,mach_right_send_t * s,uintptr_t ctx)74*5e3eaea3SApple OSS Distributions mach_right_recv_destruct(mach_right_recv_t r, mach_right_send_t *s,
75*5e3eaea3SApple OSS Distributions     uintptr_t ctx)
76*5e3eaea3SApple OSS Distributions {
77*5e3eaea3SApple OSS Distributions 	kern_return_t kr = KERN_FAILURE;
78*5e3eaea3SApple OSS Distributions 	mach_port_delta_t srd = 0;
79*5e3eaea3SApple OSS Distributions 
80*5e3eaea3SApple OSS Distributions 	if (s) {
81*5e3eaea3SApple OSS Distributions 		if (r.mrr_name != s->mrs_name) {
82*5e3eaea3SApple OSS Distributions 			__builtin_trap();
83*5e3eaea3SApple OSS Distributions 		}
84*5e3eaea3SApple OSS Distributions 
85*5e3eaea3SApple OSS Distributions 		srd = -1;
86*5e3eaea3SApple OSS Distributions 	}
87*5e3eaea3SApple OSS Distributions 
88*5e3eaea3SApple OSS Distributions 	kr = mach_port_destruct(mach_task_self(), r.mrr_name, srd, ctx);
89*5e3eaea3SApple OSS Distributions 	_mach_assert("destruct recv right", kr);
90*5e3eaea3SApple OSS Distributions }
91*5e3eaea3SApple OSS Distributions 
92*5e3eaea3SApple OSS Distributions mach_right_send_t
mach_right_send_create(mach_right_recv_t r)93*5e3eaea3SApple OSS Distributions mach_right_send_create(mach_right_recv_t r)
94*5e3eaea3SApple OSS Distributions {
95*5e3eaea3SApple OSS Distributions 	kern_return_t kr = KERN_FAILURE;
96*5e3eaea3SApple OSS Distributions 
97*5e3eaea3SApple OSS Distributions 	kr = mach_port_insert_right(mach_task_self(), r.mrr_name, r.mrr_name,
98*5e3eaea3SApple OSS Distributions 	    MACH_MSG_TYPE_MAKE_SEND);
99*5e3eaea3SApple OSS Distributions 	_mach_assert("create send right", kr);
100*5e3eaea3SApple OSS Distributions 
101*5e3eaea3SApple OSS Distributions 	return mach_right_send(r.mrr_name);
102*5e3eaea3SApple OSS Distributions }
103*5e3eaea3SApple OSS Distributions 
104*5e3eaea3SApple OSS Distributions mach_right_send_t
mach_right_send_retain(mach_right_send_t s)105*5e3eaea3SApple OSS Distributions mach_right_send_retain(mach_right_send_t s)
106*5e3eaea3SApple OSS Distributions {
107*5e3eaea3SApple OSS Distributions 	kern_return_t kr = KERN_FAILURE;
108*5e3eaea3SApple OSS Distributions 	mach_right_send_t rs = MACH_RIGHT_SEND_NULL;
109*5e3eaea3SApple OSS Distributions 
110*5e3eaea3SApple OSS Distributions 	kr = mach_port_mod_refs(mach_task_self(), s.mrs_name,
111*5e3eaea3SApple OSS Distributions 	    MACH_PORT_RIGHT_SEND, 1);
112*5e3eaea3SApple OSS Distributions 	switch (kr) {
113*5e3eaea3SApple OSS Distributions 	case 0:
114*5e3eaea3SApple OSS Distributions 		rs = s;
115*5e3eaea3SApple OSS Distributions 		break;
116*5e3eaea3SApple OSS Distributions 	case KERN_INVALID_RIGHT:
117*5e3eaea3SApple OSS Distributions 		rs.mrs_name = MACH_PORT_DEAD;
118*5e3eaea3SApple OSS Distributions 		break;
119*5e3eaea3SApple OSS Distributions 	case KERN_INVALID_NAME:
120*5e3eaea3SApple OSS Distributions 	// mach_port_mod_refs() will return success when given either
121*5e3eaea3SApple OSS Distributions 	// MACH_PORT_DEAD or MACH_PORT_NULL with send or send-once right
122*5e3eaea3SApple OSS Distributions 	// operations, so this is always fatal.
123*5e3eaea3SApple OSS Distributions 	default:
124*5e3eaea3SApple OSS Distributions 		_mach_assert("retain send right", kr);
125*5e3eaea3SApple OSS Distributions 	}
126*5e3eaea3SApple OSS Distributions 
127*5e3eaea3SApple OSS Distributions 	return rs;
128*5e3eaea3SApple OSS Distributions }
129*5e3eaea3SApple OSS Distributions 
130*5e3eaea3SApple OSS Distributions void
mach_right_send_release(mach_right_send_t s)131*5e3eaea3SApple OSS Distributions mach_right_send_release(mach_right_send_t s)
132*5e3eaea3SApple OSS Distributions {
133*5e3eaea3SApple OSS Distributions 	kern_return_t kr = KERN_FAILURE;
134*5e3eaea3SApple OSS Distributions 
135*5e3eaea3SApple OSS Distributions 	kr = mach_port_mod_refs(mach_task_self(), s.mrs_name,
136*5e3eaea3SApple OSS Distributions 	    MACH_PORT_RIGHT_SEND, -1);
137*5e3eaea3SApple OSS Distributions 	switch (kr) {
138*5e3eaea3SApple OSS Distributions 	case 0:
139*5e3eaea3SApple OSS Distributions 		break;
140*5e3eaea3SApple OSS Distributions 	case KERN_INVALID_RIGHT:
141*5e3eaea3SApple OSS Distributions 		kr = mach_port_mod_refs(mach_task_self(), s.mrs_name,
142*5e3eaea3SApple OSS Distributions 		    MACH_PORT_RIGHT_DEAD_NAME, -1);
143*5e3eaea3SApple OSS Distributions 		_mach_assert("release dead name", kr);
144*5e3eaea3SApple OSS Distributions 		break;
145*5e3eaea3SApple OSS Distributions 	default:
146*5e3eaea3SApple OSS Distributions 		_mach_assert("release send right", kr);
147*5e3eaea3SApple OSS Distributions 	}
148*5e3eaea3SApple OSS Distributions }
149*5e3eaea3SApple OSS Distributions 
150*5e3eaea3SApple OSS Distributions mach_right_send_once_t
mach_right_send_once_create(mach_right_recv_t r)151*5e3eaea3SApple OSS Distributions mach_right_send_once_create(mach_right_recv_t r)
152*5e3eaea3SApple OSS Distributions {
153*5e3eaea3SApple OSS Distributions 	mach_msg_type_name_t right = 0;
154*5e3eaea3SApple OSS Distributions 	mach_port_t so = MACH_PORT_NULL;
155*5e3eaea3SApple OSS Distributions 	kern_return_t kr = mach_port_extract_right(mach_task_self(), r.mrr_name,
156*5e3eaea3SApple OSS Distributions 	    MACH_MSG_TYPE_MAKE_SEND_ONCE, &so, &right);
157*5e3eaea3SApple OSS Distributions 	_mach_assert("create send-once right", kr);
158*5e3eaea3SApple OSS Distributions 
159*5e3eaea3SApple OSS Distributions 	return mach_right_send_once(so);
160*5e3eaea3SApple OSS Distributions }
161*5e3eaea3SApple OSS Distributions 
162*5e3eaea3SApple OSS Distributions void
mach_right_send_once_consume(mach_right_send_once_t so)163*5e3eaea3SApple OSS Distributions mach_right_send_once_consume(mach_right_send_once_t so)
164*5e3eaea3SApple OSS Distributions {
165*5e3eaea3SApple OSS Distributions 	kern_return_t kr = KERN_FAILURE;
166*5e3eaea3SApple OSS Distributions 
167*5e3eaea3SApple OSS Distributions 	kr = mach_port_mod_refs(mach_task_self(), so.mrso_name,
168*5e3eaea3SApple OSS Distributions 	    MACH_PORT_RIGHT_SEND_ONCE, -1);
169*5e3eaea3SApple OSS Distributions 	switch (kr) {
170*5e3eaea3SApple OSS Distributions 	case 0:
171*5e3eaea3SApple OSS Distributions 		break;
172*5e3eaea3SApple OSS Distributions 	case KERN_INVALID_RIGHT:
173*5e3eaea3SApple OSS Distributions 		kr = mach_port_mod_refs(mach_task_self(), so.mrso_name,
174*5e3eaea3SApple OSS Distributions 		    MACH_PORT_RIGHT_DEAD_NAME, -1);
175*5e3eaea3SApple OSS Distributions 		_mach_assert("release dead name", kr);
176*5e3eaea3SApple OSS Distributions 		break;
177*5e3eaea3SApple OSS Distributions 	default:
178*5e3eaea3SApple OSS Distributions 		_mach_assert("consume send-once right", kr);
179*5e3eaea3SApple OSS Distributions 	}
180*5e3eaea3SApple OSS Distributions }
181