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