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