xref: /xnu-10002.41.9/tests/cpu_counters/cpc_security_tests.c (revision 699cd48037512bf4380799317ca44ca453c82f57)
1 // Copyright 2023 (c) Apple Inc.  All rights reserved.
2 
3 #include <darwintest.h>
4 #include <darwintest_utils.h>
5 #include <dirent.h>
6 #include <kperf/kpc.h>
7 #include <kperfdata/kpep.h>
8 #include <stdarg.h>
9 #include <stdbool.h>
10 #include <string.h>
11 #include <sys/guarded.h>
12 #include <sys/ioctl.h>
13 #include <sys/monotonic.h>
14 
15 #include "test_utils.h"
16 
17 #if __arm64__
18 #define HAS_CPC_SECURITY true
19 #else // __arm64__
20 #define HAS_CPC_SECURITY false
21 #endif // !__arm64__
22 
23 #define _T_META_REQUIRES_CPC_SUPPORT \
24 	T_META_REQUIRES_SYSCTL_EQ("kern.monotonic.supported", "1")
25 
26 T_GLOBAL_META(
27 	T_META_NAMESPACE("xnu.cpc"),
28 	T_META_RADAR_COMPONENT_NAME("xnu"),
29 	T_META_RADAR_COMPONENT_VERSION("cpu counters"),
30 	T_META_OWNER("mwidmann"),
31 	T_META_CHECK_LEAKS(false),
32 	XNU_T_META_SOC_SPECIFIC,
33 	T_META_ENABLED(HAS_CPC_SECURITY),
34 	_T_META_REQUIRES_CPC_SUPPORT);
35 
36 // Several of these tests have two variants to support running on development and release kernels.
37 // Tests prefixed with `secure_` put the development kernel into a secure CPC mode while tests prefixed with `release_` can run on the RELEASE build variant.
38 
39 // Metadata for running on a development kernel in CPC secure mode.
40 #define _T_META_CPC_SECURE_ON_DEV \
41 	XNU_T_META_REQUIRES_DEVELOPMENT_KERNEL, T_META_SYSCTL_INT("kern.cpc.secure=1")
42 
43 static void
_assert_kpep_ok(int kpep_err,const char * fmt,...)44 _assert_kpep_ok(int kpep_err, const char *fmt, ...)
45 {
46 	char msg[1024] = "";
47 	va_list args;
48 	va_start(args, fmt);
49 	vsnprintf(msg, sizeof(msg), fmt, args);
50 	va_end(args);
51 	T_QUIET;
52 	T_ASSERT_EQ(kpep_err, KPEP_ERR_NONE, "%s: %s", msg, kpep_strerror(kpep_err));
53 }
54 
55 static void
_skip_for_db(const char * kind,int kpep_err)56 _skip_for_db(const char *kind, int kpep_err)
57 {
58 	const char * const public_kpep_path = "/usr/share/kpep";
59 	const char * const internal_kpep_path = "/usr/local/share/kpep";
60 	const char * const paths[2] = { public_kpep_path, internal_kpep_path, };
61 	for (int i = 0; i < 2; i++) {
62 		const char * const path = paths[i];
63 		T_LOG("contents of %s:", path);
64 		DIR *dir = opendir(path);
65 		if (dir) {
66 			struct dirent *entry = NULL;
67 			while ((entry = readdir(dir)) != NULL) {
68 				T_LOG("    %s", entry->d_name);
69 			}
70 			(void)closedir(dir);
71 		} else {
72 			T_LOG("failed to open directory: %s", strerror(errno));
73 		}
74 	}
75 	int cpu_family = 0;
76 	size_t family_size = sizeof(cpu_family);
77 	int ret = sysctlbyname("hw.cpufamily", &cpu_family, &family_size, NULL, 0);
78 	if (ret != 0) {
79 		T_LOG("HW CPU family: 0x%8x", cpu_family);
80 	} else {
81 		T_LOG("failed to get hw.cpufamily: %s", strerror(errno));
82 	}
83 	T_SKIP("cannot open %s event database: %s", kind, kpep_strerror(kpep_err));
84 }
85 
86 // Check that a secure kernel disallows restricted events.
87 
88 static void
check_secure_cpmu(void)89 check_secure_cpmu(void)
90 {
91 	kpep_db_t public_db = NULL;
92 	int ret = kpep_db_createx(NULL, KPEP_DB_FLAG_PUBLIC_ONLY, &public_db);
93 	if (ret != KPEP_ERR_NONE) {
94 		_skip_for_db("public", ret);
95 	}
96 	kpep_db_t internal_db = NULL;
97 	ret = kpep_db_createx(NULL, KPEP_DB_FLAG_INTERNAL_ONLY, &internal_db);
98 	if (ret != KPEP_ERR_NONE) {
99 		_skip_for_db("internal", ret);
100 	}
101 	const char *na = NULL;
102 	kpep_db_name(public_db, &na);
103 
104 	size_t internal_event_count = 0;
105 	ret = kpep_db_events_count(internal_db, &internal_event_count);
106 	_assert_kpep_ok(ret, "getting internal event count");
107 
108 	kpep_event_t *internal_events = calloc(internal_event_count,
109 	    sizeof(internal_events[0]));
110 	T_QUIET; T_WITH_ERRNO;
111 	T_ASSERT_NOTNULL(internal_events, "allocate space for internal events");
112 
113 	ret = kpep_db_events(internal_db, internal_events,
114 	    internal_event_count * sizeof(internal_events[0]));
115 	_assert_kpep_ok(ret, "getting internal events");
116 
117 	kpep_config_t config = NULL;
118 	ret = kpep_config_create(internal_db, &config);
119 	_assert_kpep_ok(ret, "creating event configuration");
120 	ret = kpep_config_force_counters(config);
121 	_assert_kpep_ok(ret, "forcing counters with configuration");
122 
123 	unsigned int tested = 0;
124 	unsigned int filtered = 0;
125 	unsigned int public_tested = 0;
126 	for (size_t i = 0; i < internal_event_count; i++) {
127 		kpep_event_t event = internal_events[i];
128 		const char *name = NULL;
129 		ret = kpep_event_alias(event, &name);
130 		if (!name) {
131 			ret = kpep_event_name(event, &name);
132 		}
133 		_assert_kpep_ok(ret, "getting event name");
134 		if (strncmp(name, "FIXED", strlen("FIXED")) == 0) {
135 			T_LOG("skipping non-configurable %s event", name);
136 			continue;
137 		}
138 		bool empty_event = strcmp(name, "NO_EVNT") == 0;
139 		if (empty_event) {
140 			continue;
141 		}
142 
143 		kpep_event_t public_event = NULL;
144 		ret = kpep_db_event(public_db, name, &public_event);
145 		bool internal_only = ret == KPEP_ERR_EVENT_NOT_FOUND;
146 		ret = kpep_config_add_event(config, &event, 0, NULL);
147 		_assert_kpep_ok(ret, "adding event %s to configuration", name);
148 
149 		ret = kpep_config_apply(config);
150 		bool not_permitted = ret == KPEP_ERR_ERRNO && errno == EPERM;
151 		if (not_permitted) {
152 			if (!internal_only) {
153 				T_LOG("failed to configure public event %s", name);
154 			}
155 			filtered++;
156 		} else if (internal_only) {
157 			T_FAIL("configured internal-only event %s with secure CPC", name);
158 		} else {
159 			public_tested++;
160 		}
161 		ret = kpep_config_remove_event(config, 0);
162 		_assert_kpep_ok(ret, "removing event %s from configuration", name);
163 		tested++;
164 	}
165 
166 	T_LOG("tested %u internal/public events", tested);
167 	T_LOG("correctly permitted to configure %u public events", public_tested);
168 	T_LOG("correctly not permitted to configure %u internal-only events",
169 	    filtered);
170 	kpep_config_free(config);
171 	kpep_db_free(public_db);
172 	kpep_db_free(internal_db);
173 }
174 
175 T_DECL(secure_cpmu_event_restrictions, "secured CPMU should be restricted to known events",
176     _T_META_CPC_SECURE_ON_DEV)
177 {
178 	check_secure_cpmu();
179 }
180 
181 T_DECL(release_cpmu_event_restrictions, "release CPMU should be restricted to known events",
182     XNU_T_META_REQUIRES_RELEASE_KERNEL)
183 {
184 	check_secure_cpmu();
185 }
186 
187 #define UNCORE_DEV_PATH "/dev/monotonic/uncore"
188 #define UPMU_REF_CYCLES 0x02
189 
190 static void
check_secure_upmu(void)191 check_secure_upmu(void)
192 {
193 	guardid_t guard;
194 	int fd;
195 
196 	guard = 0xa5adcafe;
197 
198 	T_SETUPBEGIN;
199 
200 	fd = guarded_open_np(UNCORE_DEV_PATH, &guard,
201 	    GUARD_CLOSE | GUARD_DUP | GUARD_WRITE, O_CLOEXEC | O_EXCL);
202 	if (fd < 0 && errno == ENOENT) {
203 		T_SKIP("uncore counters are unsupported");
204 	}
205 
206 	union monotonic_ctl_add add_ctl = {
207 		.in.config.event = UPMU_REF_CYCLES,
208 		.in.config.allowed_ctr_mask = 0xffff,
209 	};
210 
211 	T_SETUPEND;
212 
213 	int ret = ioctl(fd, MT_IOC_ADD, &add_ctl);
214 	T_EXPECT_POSIX_FAILURE(ret, EPERM,
215 	    "should not be allowed to count any events on UPMU");
216 }
217 
218 T_DECL(secure_upmu_event_restrictions, "secured UPMU should be restricted to no events",
219     _T_META_CPC_SECURE_ON_DEV)
220 {
221 	check_secure_upmu();
222 }
223 
224 T_DECL(release_upmu_event_restrictions, "release UPMU should be restricted to no events",
225     XNU_T_META_REQUIRES_RELEASE_KERNEL)
226 {
227 	check_secure_upmu();
228 }
229 
230 // Check that events which are exposed publicly are allowed to be configured.
231 
232 static void
check_event_coverage(kpep_db_flags_t flag,const char * kind)233 check_event_coverage(kpep_db_flags_t flag, const char *kind)
234 {
235 	kpep_db_t db = NULL;
236 	int ret = kpep_db_createx(NULL, flag, &db);
237 	_assert_kpep_ok(ret, "creating %s event database", kind);
238 
239 	size_t event_count = 0;
240 	ret = kpep_db_events_count(db, &event_count);
241 	_assert_kpep_ok(ret, "getting %s event count", kind);
242 
243 	kpep_event_t *events = calloc(event_count, sizeof(events[0]));
244 	T_QUIET; T_WITH_ERRNO;
245 	T_ASSERT_NOTNULL(events, "allocate space for events");
246 
247 	ret = kpep_db_events(db, events, event_count * sizeof(events[0]));
248 	_assert_kpep_ok(ret, "getting public events");
249 
250 	kpep_config_t config = NULL;
251 	ret = kpep_config_create(db, &config);
252 	_assert_kpep_ok(ret, "creating event configuration");
253 	ret = kpep_config_force_counters(config);
254 	_assert_kpep_ok(ret, "forcing counters with configuration");
255 
256 	unsigned int tested = 0;
257 	for (size_t i = 0; i < event_count; i++) {
258 		kpep_event_t event = events[i];
259 		const char *name = NULL;
260 		ret = kpep_event_name(event, &name);
261 		_assert_kpep_ok(ret, "getting event name");
262 		if (strncmp(name, "FIXED", strlen("FIXED")) == 0) {
263 			T_LOG("skipping non-configurable %s event", name);
264 			continue;
265 		}
266 
267 		ret = kpep_config_add_event(config, &event, 0, NULL);
268 		_assert_kpep_ok(ret, "adding event %s to configuration", name);
269 
270 		ret = kpep_config_apply(config);
271 		if (ret == KPEP_ERR_ERRNO && errno == EPERM) {
272 			T_FAIL("failed to configure %s event %s with secure CPC", kind, name);
273 		} else {
274 			_assert_kpep_ok(ret, "applying configuration with event %s", name);
275 		}
276 		ret = kpep_config_remove_event(config, 0);
277 		_assert_kpep_ok(ret, "removing event %s from configuration", name);
278 		tested++;
279 	}
280 
281 	T_LOG("successfully configured %u %s events", tested, kind);
282 	kpep_config_free(config);
283 	kpep_db_free(db);
284 }
285 
286 T_DECL(secure_public_event_coverage, "all public events in kpep should be allowed",
287     _T_META_CPC_SECURE_ON_DEV)
288 {
289 	check_event_coverage(KPEP_DB_FLAG_PUBLIC_ONLY, "public");
290 }
291 
292 T_DECL(release_public_event_coverage, "all public events in kpep should be allowed",
293     XNU_T_META_REQUIRES_RELEASE_KERNEL)
294 {
295 	check_event_coverage(KPEP_DB_FLAG_PUBLIC_ONLY, "public");
296 }
297 
298 // Check for internal development behaviors.
299 
300 T_DECL(insecure_cpmu_unrestricted, "insecure CPMU should be unrestricted",
301     XNU_T_META_REQUIRES_DEVELOPMENT_KERNEL, T_META_SYSCTL_INT("kern.cpc.secure=0"))
302 {
303 	check_event_coverage(KPEP_DB_FLAG_INTERNAL_ONLY, "internal");
304 }
305