]> git.saurik.com Git - apple/xnu.git/blame - bsd/kern/kern_csr.c
xnu-3248.30.4.tar.gz
[apple/xnu.git] / bsd / kern / kern_csr.c
CommitLineData
fe8ab488
A
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
3e170ce0
A
36/* enable enforcement by default */
37static int csr_allow_all = 0;
fe8ab488
A
38
39void
40csr_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 }
fe8ab488
A
47}
48
49int
50csr_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 {
3e170ce0 56 *config = 0;
fe8ab488
A
57 }
58
59 return 0;
60}
61
62int
3e170ce0 63csr_check(csr_config_t mask)
fe8ab488
A
64{
65 boot_args *args = (boot_args *)PE_state.bootArgs;
3e170ce0
A
66 if ((mask & CSR_ALLOW_DEVICE_CONFIGURATION) && !(args->flags & kBootArgsFlagCSRConfigMode))
67 return EPERM;
fe8ab488 68
fe8ab488
A
69 if (csr_allow_all) {
70 return 0;
71 }
72
73 csr_config_t config;
74 int error = csr_get_active_config(&config);
75 if (error) {
76 return error;
77 }
78
fe8ab488
A
79 if (mask == 0) {
80 /* pass 0 to check if Rootless enforcement is active */
81 return -1;
82 }
83
84 error = (config & mask) ? 0 : EPERM;
85 return error;
86}
87
88void
89csr_set_allow_all(int value)
90{
91 csr_allow_all = !!value; // force value to 0 or 1
92}
3e170ce0
A
93
94/*
95 * Syscall stubs
96 */
97
98int syscall_csr_check(struct csrctl_args *args);
99int syscall_csr_get_active_config(struct csrctl_args *args);
100
101
102int
103syscall_csr_check(struct csrctl_args *args)
104{
105 csr_config_t mask = 0;
106 int error = 0;
107
108 if (args->useraddr == 0 || args->usersize != sizeof(mask))
109 return EINVAL;
110
111 error = copyin(args->useraddr, &mask, sizeof(mask));
112 if (error)
113 return error;
114
115 return csr_check(mask);
116}
117
118int
119syscall_csr_get_active_config(struct csrctl_args *args)
120{
121 csr_config_t config = 0;
122 int error = 0;
123
124 if (args->useraddr == 0 || args->usersize != sizeof(config))
125 return EINVAL;
126
127 error = csr_get_active_config(&config);
128 if (error)
129 return error;
130
131 return copyout(&config, args->useraddr, sizeof(config));
132}
133
134/*
135 * Syscall entrypoint
136 */
137
138int
139csrctl(__unused proc_t p, struct csrctl_args *args, __unused int32_t *retval)
140{
141 switch (args->op) {
142 case CSR_SYSCALL_CHECK:
143 return syscall_csr_check(args);
144 case CSR_SYSCALL_GET_ACTIVE_CONFIG:
145 return syscall_csr_get_active_config(args);
146 default:
147 return ENOSYS;
148 }
149}