]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/kern_csr.c
xnu-4570.41.2.tar.gz
[apple/xnu.git] / bsd / kern / kern_csr.c
1 /*
2 * Copyright (c) 2014 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
29 #include <pexpert/pexpert.h>
30 #include <sys/csr.h>
31 #include <sys/errno.h>
32 #include <sys/sysproto.h>
33 #include <sys/systm.h>
34 #include <sys/types.h>
35
36 /* enable enforcement by default */
37 static int csr_allow_all = 0;
38
39 void
40 csr_init(void)
41 {
42 boot_args *args = (boot_args *)PE_state.bootArgs;
43 if (args->flags & kBootArgsFlagCSRBoot) {
44 /* special booter; allow everything */
45 csr_allow_all = 1;
46 }
47 }
48
49 int
50 csr_get_active_config(csr_config_t *config)
51 {
52 boot_args *args = (boot_args *)PE_state.bootArgs;
53 if (args->flags & kBootArgsFlagCSRActiveConfig) {
54 *config = args->csrActiveConfig & CSR_VALID_FLAGS;
55 } else {
56 *config = 0;
57 }
58
59 return 0;
60 }
61
62 int
63 csr_check(csr_config_t mask)
64 {
65 boot_args *args = (boot_args *)PE_state.bootArgs;
66 if (mask & CSR_ALLOW_DEVICE_CONFIGURATION)
67 return (args->flags & kBootArgsFlagCSRConfigMode) ? 0 : EPERM;
68
69 csr_config_t config;
70 int ret = csr_get_active_config(&config);
71 if (ret) {
72 return ret;
73 }
74
75 // CSR_ALLOW_KERNEL_DEBUGGER needs to be allowed when SIP is disabled
76 // to allow 3rd-party developers to debug their kexts. Use
77 // CSR_ALLOW_UNTRUSTED_KEXTS as a proxy for "SIP is disabled" on the
78 // grounds that you can do the same damage with a kernel debugger as
79 // you can with an untrusted kext.
80 if ((config & (CSR_ALLOW_UNTRUSTED_KEXTS|CSR_ALLOW_APPLE_INTERNAL)) != 0)
81 config |= CSR_ALLOW_KERNEL_DEBUGGER;
82
83 ret = ((config & mask) == mask) ? 0 : EPERM;
84 if (ret == EPERM) {
85 // Override the return value if booted from the BaseSystem and the mask does not contain any flag that should always be enforced.
86 if (csr_allow_all && (mask & CSR_ALWAYS_ENFORCED_FLAGS) == 0)
87 ret = 0;
88 }
89
90 return ret;
91 }
92
93 /*
94 * Syscall stubs
95 */
96
97 int syscall_csr_check(struct csrctl_args *args);
98 int syscall_csr_get_active_config(struct csrctl_args *args);
99
100
101 int
102 syscall_csr_check(struct csrctl_args *args)
103 {
104 csr_config_t mask = 0;
105 int error = 0;
106
107 if (args->useraddr == 0 || args->usersize != sizeof(mask))
108 return EINVAL;
109
110 error = copyin(args->useraddr, &mask, sizeof(mask));
111 if (error)
112 return error;
113
114 return csr_check(mask);
115 }
116
117 int
118 syscall_csr_get_active_config(struct csrctl_args *args)
119 {
120 csr_config_t config = 0;
121 int error = 0;
122
123 if (args->useraddr == 0 || args->usersize != sizeof(config))
124 return EINVAL;
125
126 error = csr_get_active_config(&config);
127 if (error)
128 return error;
129
130 return copyout(&config, args->useraddr, sizeof(config));
131 }
132
133 /*
134 * Syscall entrypoint
135 */
136
137 int
138 csrctl(__unused proc_t p, struct csrctl_args *args, __unused int32_t *retval)
139 {
140 switch (args->op) {
141 case CSR_SYSCALL_CHECK:
142 return syscall_csr_check(args);
143 case CSR_SYSCALL_GET_ACTIVE_CONFIG:
144 return syscall_csr_get_active_config(args);
145 default:
146 return ENOSYS;
147 }
148 }