xref: /xnu-8019.80.24/libsyscall/wrappers/kern_debug.c (revision a325d9c4a84054e40bbe985afedcb50ab80993ea)
1*a325d9c4SApple OSS Distributions /*
2*a325d9c4SApple OSS Distributions  * Copyright (c) 2021 Apple Inc. All rights reserved.
3*a325d9c4SApple OSS Distributions  *
4*a325d9c4SApple OSS Distributions  * @APPLE_LICENSE_HEADER_START@
5*a325d9c4SApple OSS Distributions  *
6*a325d9c4SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*a325d9c4SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*a325d9c4SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*a325d9c4SApple OSS Distributions  * compliance with the License. Please obtain a copy of the License at
10*a325d9c4SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this
11*a325d9c4SApple OSS Distributions  * file.
12*a325d9c4SApple OSS Distributions  *
13*a325d9c4SApple OSS Distributions  * The Original Code and all software distributed under the License are
14*a325d9c4SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15*a325d9c4SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16*a325d9c4SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17*a325d9c4SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18*a325d9c4SApple OSS Distributions  * Please see the License for the specific language governing rights and
19*a325d9c4SApple OSS Distributions  * limitations under the License.
20*a325d9c4SApple OSS Distributions  *
21*a325d9c4SApple OSS Distributions  * @APPLE_LICENSE_HEADER_END@
22*a325d9c4SApple OSS Distributions  */
23*a325d9c4SApple OSS Distributions #include <stdbool.h>
24*a325d9c4SApple OSS Distributions 
25*a325d9c4SApple OSS Distributions #include <sys/errno.h>
26*a325d9c4SApple OSS Distributions 
27*a325d9c4SApple OSS Distributions #include <sys/kern_debug.h>
28*a325d9c4SApple OSS Distributions 
29*a325d9c4SApple OSS Distributions /* Syscall entry points */
30*a325d9c4SApple OSS Distributions int __debug_syscall_reject(uint64_t packed_selectors);
31*a325d9c4SApple OSS Distributions 
32*a325d9c4SApple OSS Distributions static bool supported = true;
33*a325d9c4SApple OSS Distributions 
34*a325d9c4SApple OSS Distributions int
debug_syscall_reject(const syscall_rejection_selector_t * selectors,size_t len)35*a325d9c4SApple OSS Distributions debug_syscall_reject(const syscall_rejection_selector_t *selectors, size_t len)
36*a325d9c4SApple OSS Distributions {
37*a325d9c4SApple OSS Distributions 	_Static_assert(sizeof(syscall_rejection_selector_t) == 1, "selector size is not 1 byte");
38*a325d9c4SApple OSS Distributions 
39*a325d9c4SApple OSS Distributions 	if (!supported) {
40*a325d9c4SApple OSS Distributions 		/* Gracefully ignored if unsupported (e.g. if compiled out of RELEASE). */
41*a325d9c4SApple OSS Distributions 		return 0;
42*a325d9c4SApple OSS Distributions 	}
43*a325d9c4SApple OSS Distributions 
44*a325d9c4SApple OSS Distributions 	if (len > 8) {
45*a325d9c4SApple OSS Distributions 		/* selectors are packed one per byte into an uint64_t */
46*a325d9c4SApple OSS Distributions 		errno = E2BIG;
47*a325d9c4SApple OSS Distributions 		return -1;
48*a325d9c4SApple OSS Distributions 	}
49*a325d9c4SApple OSS Distributions 
50*a325d9c4SApple OSS Distributions 	/*
51*a325d9c4SApple OSS Distributions 	 * The masks to apply are passed to the kernel as packed selectors,
52*a325d9c4SApple OSS Distributions 	 * which are just however many of the selector data type fit into one
53*a325d9c4SApple OSS Distributions 	 * (or more) fields of the natural word size (i.e. a register). This
54*a325d9c4SApple OSS Distributions 	 * avoids copying from user space.
55*a325d9c4SApple OSS Distributions 	 *
56*a325d9c4SApple OSS Distributions 	 * More specifically, at the time of this writing, a selector is 1
57*a325d9c4SApple OSS Distributions 	 * byte wide, and there is only one uint64_t argument
58*a325d9c4SApple OSS Distributions 	 * (args->packed_selectors), so up to 8 selectors can be specified,
59*a325d9c4SApple OSS Distributions 	 * which are then stuffed into the 64 bits of the argument. If less
60*a325d9c4SApple OSS Distributions 	 * than 8 masks are requested to be applied, the remaining selectors
61*a325d9c4SApple OSS Distributions 	 * will just be left as 0, which naturally resolves as the "empty" or
62*a325d9c4SApple OSS Distributions 	 * "NULL" mask that changes nothing.
63*a325d9c4SApple OSS Distributions 	 *
64*a325d9c4SApple OSS Distributions 	 * This libsyscall wrapper provides a more convenient interface where
65*a325d9c4SApple OSS Distributions 	 * an array (up to 8 elements long) and its length are passed in,
66*a325d9c4SApple OSS Distributions 	 * which the wrapper then packs into packed_selectors of the actual
67*a325d9c4SApple OSS Distributions 	 * system call.
68*a325d9c4SApple OSS Distributions 	 */
69*a325d9c4SApple OSS Distributions 
70*a325d9c4SApple OSS Distributions 
71*a325d9c4SApple OSS Distributions 	uint64_t packed_selectors = 0;
72*a325d9c4SApple OSS Distributions 	int shift = 0;
73*a325d9c4SApple OSS Distributions 
74*a325d9c4SApple OSS Distributions 	for (int i = 0; i < len; i++, shift += 8) {
75*a325d9c4SApple OSS Distributions 		packed_selectors |= ((uint64_t)(selectors[i]) & 0xff) << shift;
76*a325d9c4SApple OSS Distributions 	}
77*a325d9c4SApple OSS Distributions 
78*a325d9c4SApple OSS Distributions 	int ret = __debug_syscall_reject(packed_selectors);
79*a325d9c4SApple OSS Distributions 
80*a325d9c4SApple OSS Distributions 	if (ret == -1 && errno == ENOTSUP) {
81*a325d9c4SApple OSS Distributions 		errno = 0;
82*a325d9c4SApple OSS Distributions 		supported = false;
83*a325d9c4SApple OSS Distributions 		return 0;
84*a325d9c4SApple OSS Distributions 	}
85*a325d9c4SApple OSS Distributions 
86*a325d9c4SApple OSS Distributions 	return ret;
87*a325d9c4SApple OSS Distributions }
88