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