xref: /xnu-8020.121.3/bsd/dev/dtrace/fbt_blacklist.c (revision fdd8201d7b966f0c3ea610489d29bd841d358941)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 #include <sys/dtrace_impl.h>
23 #include <sys/fbt.h>
24 #include <sys/sysctl.h>
25 
26 #define CLOSURE(s) #s,
27 #define CRITICAL(s) #s,
28 
29 #if KASAN
30 #define KASAN_ONLY(s) #s,
31 #else
32 #define KASAN_ONLY(s)
33 #endif /* KASAN */
34 
35 #if CONFIG_UBSAN_MINIMAL
36 #define UBSAN_MINIMAL_ONLY(s) #s,
37 #else
38 #define UBSAN_MINIMAL_ONLY(s)
39 #endif
40 
41 #if defined(__arm__) || defined(__arm64__)
42 #define ARM_ONLY(s) #s,
43 #else
44 #define ARM_ONLY(s)
45 #endif /* defined(__arm__) || defined(__arm64__) */
46 #if defined(__x86_64__)
47 #define X86_ONLY(s) #s,
48 #else
49 #define X86_ONLY(s)
50 #endif /* defined(__x86_64__) */
51 
52 /*
53  * Routine prefixes that must not be probed, either because they are used in
54  * the exception path, by dtrace code in probe context, or are general
55  * critical routines that must never be probed.
56  *
57  * All routines whose name start with one of these will be ignored.
58  *
59  * This must be kept in asciibetical order for purposes of bsearch().
60  */
61 const char * fbt_blacklist[] =
62 {
63 	CRITICAL(Call_DebuggerC)
64 	CLOSURE(ClearIdlePop)
65 	CLOSURE(Debugger)
66 	CRITICAL(IOCPURunPlatformPanicActions)
67 	CLOSURE(IS_64BIT_PROCESS)
68 	CRITICAL(OSAdd)
69 	CRITICAL(OSBit)
70 	CLOSURE(OSCompareAndSwap)
71 	CRITICAL(OSDecrement)
72 	CRITICAL(OSIncrement)
73 	CRITICAL(PEARMDebugPanicHook)
74 	CRITICAL(PEHaltRestart)
75 	CRITICAL(PE_)
76 	CRITICAL(SavePanicInfo)
77 	CLOSURE(SetIdlePop)
78 	CRITICAL(SysChoked)
79 	CRITICAL(_ZN15OSMetaClassBase12safeMetaCastEPKS_PK11OSMetaClass) /* OSMetaClassBase::safeMetaCast */
80 	CRITICAL(_ZN16IOPlatformExpert11haltRestartEj) /* IOPlatformExpert::haltRestart */
81 	CRITICAL(_ZN18IODTPlatformExpert11haltRestartEj) /* IODTPlatformExpert::haltRestart */
82 	ARM_ONLY(_ZN8ASPNVRAM4syncEv) /* ASPNVRAM::sync */
83 	CRITICAL(_ZN9IODTNVRAM13savePanicInfoEPhy) /* IODTNVRAM::savePanicInfo */
84 	CRITICAL(_ZN9IOService14newTemperatureElPS_) /* IOService::newTemperature */
85 	CRITICAL(_ZN9IOService26temperatureCriticalForZoneEPS_) /* IOService::temperatureCriticalForZone */
86 	CRITICAL(_ZNK11OSMetaClass13checkMetaCastEPK15OSMetaClassBase) /* OSMetaClass::checkMetaCast */
87 	CRITICAL(_ZNK15OSMetaClassBase8metaCastEPK11OSMetaClass) /* OSMetaClassBase::metaCast */
88 	CRITICAL(_ZNK6OSData14getBytesNoCopyEv) /* Data::getBytesNoCopy, IOHibernateSystemWake path */
89 	KASAN_ONLY(__asan)
90 	ARM_ONLY(__div)
91 	CLOSURE(__dtrace_probe)
92 	KASAN_ONLY(__kasan)
93 	ARM_ONLY(__ml)
94 	ARM_ONLY(__mod)
95 	CRITICAL(__strlcpy_chk)
96 	CLOSURE(__thread_ro_circularity_panic)
97 	UBSAN_MINIMAL_ONLY(__ubsan)
98 	ARM_ONLY(__udiv)
99 	ARM_ONLY(__umod)
100 	CLOSURE(_copyin)
101 	CLOSURE(_copyout)
102 	CRITICAL(_disable_preemption)
103 	CRITICAL(_enable_preemption)
104 	CLOSURE(absolutetime_to_microtime)
105 	X86_ONLY(acpi_)
106 	X86_ONLY(act_machine)
107 	CLOSURE(act_set_astbsd)
108 	ARM_ONLY(alternate_debugger_enter)
109 	ARM_ONLY(arm_init_idle_cpu)
110 	CLOSURE(ast_dtrace_on)
111 	CLOSURE(ast_pending)
112 	CRITICAL(backtrace_)
113 	CRITICAL(bcopy)
114 	CLOSURE(clean_dcache)
115 	CLOSURE(clean_mmu_dcache)
116 	CRITICAL(clock_)
117 	X86_ONLY(commpage_)
118 	CLOSURE(copyin)
119 	CLOSURE(copyout)
120 	CRITICAL(cpu_)
121 	CLOSURE(current_act)
122 	CLOSURE(current_percpu_base)
123 	CLOSURE(current_proc)
124 	CLOSURE(current_processor)
125 	CLOSURE(current_task)
126 	CLOSURE(current_task_early)
127 	CLOSURE(current_thread)
128 	CLOSURE(current_thread_ro)
129 	CLOSURE(current_thread_ro_unchecked)
130 	CLOSURE(current_uthread)
131 	CLOSURE(debug_)
132 	X86_ONLY(dsmos_)
133 	CLOSURE(dtrace_)
134 	CRITICAL(enter_lohandler)
135 	CRITICAL(fasttrap_)
136 	CRITICAL(fbt_invop)
137 	CRITICAL(fbt_perfCallback)
138 	CLOSURE(find_user_regs)
139 	ARM_ONLY(fleh_)
140 	CLOSURE(flush_dcache)
141 	ARM_ONLY(flush_mmu_tlb_)
142 	CLOSURE(flush_tlb64)
143 	CRITICAL(fuword)
144 	X86_ONLY(get_active_thread)
145 	CLOSURE(get_bsdtask_info)
146 	CLOSURE(get_bsdthread_info)
147 	CLOSURE(get_machthread)
148 	CRITICAL(get_preemption_level)
149 	CRITICAL(get_thread_ro)
150 	CRITICAL(get_thread_ro_unchecked)
151 	CRITICAL(get_threadtask)
152 	CRITICAL(get_threadtask_early)
153 	ARM_ONLY(get_vfp_enabled)
154 	CRITICAL(getminor)
155 	CRITICAL(handle_pending_TLB_flushes)
156 	CRITICAL(hibernate_)
157 	X86_ONLY(hndl_)
158 	CRITICAL(hw_)
159 	X86_ONLY(idt64)
160 	CRITICAL(interrupt)
161 	CRITICAL(invalidate_mmu_icache)
162 	CRITICAL(is_saved_state32)
163 	KASAN_ONLY(kasan)
164 	CLOSURE(kauth_cred_get)
165 	CLOSURE(kauth_getgid)
166 	CLOSURE(kauth_getuid)
167 	CRITICAL(kdb_)
168 	CRITICAL(kdp_)
169 	CRITICAL(kernel_preempt_check)
170 	CRITICAL(kernel_trap)
171 	CRITICAL(kprintf)
172 	CRITICAL(ks_)
173 	CLOSURE(kvtophys)
174 	X86_ONLY(lapic_)
175 	CRITICAL(lo_alltraps)
176 	CRITICAL(lock_debugger)
177 	CLOSURE(mach_absolute_time)
178 	CRITICAL(machine_)
179 	X86_ONLY(mapping_)
180 	CRITICAL(mca_cpu_alloc)
181 	CRITICAL(mca_cpu_init)
182 	CLOSURE(memcpy)
183 	CLOSURE(memmove)
184 	CRITICAL(ml_)
185 	CLOSURE(mt_core_snap)
186 	CLOSURE(mt_cur_cpu_cycles)
187 	CLOSURE(mt_cur_cpu_instrs)
188 	CLOSURE(mt_cur_thread_cycles)
189 	CLOSURE(mt_cur_thread_instrs)
190 	CLOSURE(mt_fixed_counts)
191 	CLOSURE(mt_fixed_counts_internal)
192 	CLOSURE(mt_mtc_update_count)
193 	CLOSURE(mt_update_thread)
194 	CRITICAL(nanoseconds_to_absolutetime)
195 	CRITICAL(nanotime_to_absolutetime)
196 	CRITICAL(no_asts)
197 	CLOSURE(other_percpu_base)
198 	CRITICAL(ovbcopy)
199 	CRITICAL(packA)
200 	X86_ONLY(pal_)
201 	CLOSURE(panic)
202 	CRITICAL(phystokv)
203 	CRITICAL(platform_)
204 	X86_ONLY(pltrace)
205 	X86_ONLY(pmCPU)
206 	X86_ONLY(pmKextRegister)
207 	X86_ONLY(pmMarkAllCPUsOff)
208 	X86_ONLY(pmSafeMode)
209 	X86_ONLY(pmTimerRestore)
210 	X86_ONLY(pmTimerSave)
211 	X86_ONLY(pmUnRegister)
212 	X86_ONLY(pmap64_pdpt)
213 	CLOSURE(pmap_find_pa)
214 	CLOSURE(pmap_find_phys)
215 	ARM_ONLY(pmap_get_cpu_data)
216 	CLOSURE(pmap_get_mapwindow)
217 	CLOSURE(pmap_pde)
218 	CLOSURE(pmap_pde_internal0)
219 	CLOSURE(pmap_pde_internal1)
220 	CLOSURE(pmap_pte)
221 	CLOSURE(pmap_pte_internal)
222 	CLOSURE(pmap_put_mapwindow)
223 	CLOSURE(pmap_valid_page)
224 	CLOSURE(pmap_vtophys)
225 	X86_ONLY(pms)
226 	CRITICAL(power_management_init)
227 	CRITICAL(preemption_underflow_panic)
228 	CLOSURE(prf)
229 	CLOSURE(proc_best_name)
230 	CLOSURE(proc_is64bit)
231 	CLOSURE(proc_require)
232 	CRITICAL(rbtrace_bt)
233 	CRITICAL(register_cpu_setup_func)
234 	CRITICAL(ret64_iret)
235 	CRITICAL(ret_to_user)
236 	CRITICAL(return_to_kernel)
237 	CRITICAL(return_to_user)
238 	CRITICAL(rtc_)
239 	CRITICAL(rtclock_)
240 	CRITICAL(saved_state64)
241 	CLOSURE(sdt_getargdesc)
242 	CRITICAL(sdt_invop)
243 	CLOSURE(setPop)
244 	ARM_ONLY(sleh_)
245 	CRITICAL(sprlock)
246 	CRITICAL(sprunlock)
247 	CLOSURE(strlcpy)
248 	CRITICAL(strlen)
249 	CRITICAL(strncmp)
250 	CRITICAL(suword)
251 	X86_ONLY(sync_iss_to_iks_unconditionally)
252 	CLOSURE(systrace_stub)
253 	CRITICAL(t_invop)
254 	CLOSURE(thread_tid)
255 	CLOSURE(timer_grab)
256 	ARM_ONLY(timer_state_event)
257 	CRITICAL(tmrCvt)
258 	CRITICAL(trap_from_kernel)
259 	CRITICAL(traptrace_)
260 	CRITICAL(tsc_)
261 	CRITICAL(uart_putc)
262 	CRITICAL(unlock_debugger)
263 	CRITICAL(unpackA)
264 	CRITICAL(unregister_cpu_setup_func)
265 	CRITICAL(uread)
266 	CLOSURE(uthread_is64bit)
267 	CRITICAL(uwrite)
268 	CRITICAL(vstart)
269 	CLOSURE(zone_has_index)
270 	CLOSURE(zone_id_require)
271 	CLOSURE(zone_id_require_panic)
272 	CLOSURE(zone_range_contains)
273 	CLOSURE(zone_require_panic)
274 	CLOSURE(zone_require_ro)
275 	CLOSURE(zpercpu_count)
276 };
277 #define BLACKLIST_COUNT (sizeof(fbt_blacklist)/sizeof(fbt_blacklist[0]))
278 
279 /*
280  * Modules that should not be probed.
281  *
282  * This must be kept in asciibetical order for purposes of bsearch().
283  */
284 static const char* fbt_module_blacklist[] = {
285 	X86_ONLY(com.apple.driver.AppleACPIEC)
286 	X86_ONLY(com.apple.driver.AppleACPIPlatform)
287 	ARM_ONLY(com.apple.driver.AppleARMPlatform)
288 	X86_ONLY(com.apple.driver.AppleEFI)
289 	X86_ONLY(com.apple.driver.AppleIntelCPUPowerManagement)
290 	ARM_ONLY(com.apple.driver.AppleInterruptController)
291 	X86_ONLY(com.apple.driver.AppleRTC)
292 	X86_ONLY(com.apple.iokit.IOACPIFamily)
293 };
294 #define MODULE_BLACKLIST_COUNT (sizeof(fbt_module_blacklist)/sizeof(fbt_module_blacklist[0]))
295 
296 int ignore_fbt_blacklist = 0;
297 extern int dtrace_kernel_symbol_mode;
298 
299 #pragma clang diagnostic push
300 #pragma clang diagnostic ignored "-Wcast-qual"
301 static int
_cmp(const void * a,const void * b)302 _cmp(const void *a, const void *b)
303 {
304 	const char *v = *(const char **)b;
305 	return strncmp((const char *)a, v, strlen(v));
306 }
307 
308 
309 #pragma clang diagnostic pop
310 /*
311  * Module validation
312  */
313 bool
fbt_module_excluded(struct modctl * ctl)314 fbt_module_excluded(struct modctl* ctl)
315 {
316 	const char *excluded;
317 
318 	ASSERT(!MOD_FBT_DONE(ctl));
319 
320 	if (ctl->mod_address == 0 || ctl->mod_size == 0 || !ctl->mod_loaded) {
321 		return true;
322 	}
323 
324 	if (ignore_fbt_blacklist) {
325 		return false;
326 	}
327 
328 	excluded = bsearch(ctl->mod_modname, fbt_module_blacklist,
329 	    MODULE_BLACKLIST_COUNT, sizeof(fbt_module_blacklist[0]), _cmp);
330 	return excluded;
331 }
332 
333 /*
334  * FBT probe name validation
335  */
336 bool
fbt_excluded(const char * name)337 fbt_excluded(const char* name)
338 {
339 	const char *excluded;
340 
341 	if (ignore_fbt_blacklist) {
342 		return false;
343 	}
344 
345 	excluded = bsearch(name, fbt_blacklist, BLACKLIST_COUNT, sizeof(name),
346 	    _cmp );
347 	return excluded;
348 }
349 
350 SYSCTL_DECL(_kern_dtrace);
351 
352 static int
353 sysctl_dtrace_ignore_fbt_blacklist SYSCTL_HANDLER_ARGS
354 {
355 #pragma unused(oidp, arg2)
356 	int err;
357 	int value = *(int*)arg1;
358 
359 	err = sysctl_io_number(req, value, sizeof(value), &value, NULL);
360 	if (err) {
361 		return err;
362 	}
363 	if (req->newptr) {
364 		if (!(value == 0 || value == 1)) {
365 			return ERANGE;
366 		}
367 
368 		/*
369 		 * We do not allow setting the blacklist back to on, as we have no way
370 		 * of knowing if those unsafe probes are still used.
371 		 *
372 		 * If we are using kernel symbols, we also do not allow any change,
373 		 * since the symbols are jettison'd after the first pass.
374 		 *
375 		 * We do not need to take any locks here because those symbol modes
376 		 * are permanent and do not change after boot.
377 		 */
378 		if (value != 1 || dtrace_kernel_symbol_mode == DTRACE_KERNEL_SYMBOLS_NEVER ||
379 		    dtrace_kernel_symbol_mode == DTRACE_KERNEL_SYMBOLS_ALWAYS_FROM_KERNEL) {
380 			return EPERM;
381 		}
382 
383 		ignore_fbt_blacklist = 1;
384 	}
385 
386 	return 0;
387 }
388 
389 SYSCTL_PROC(_kern_dtrace, OID_AUTO, ignore_fbt_blacklist,
390     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
391     &ignore_fbt_blacklist, 0,
392     sysctl_dtrace_ignore_fbt_blacklist, "I", "fbt provider ignore blacklist");
393 
394 void
fbt_blacklist_init(void)395 fbt_blacklist_init(void)
396 {
397 	PE_parse_boot_argn("IgnoreFBTBlacklist", &ignore_fbt_blacklist, sizeof(ignore_fbt_blacklist));
398 #if DEBUG || DEVELOPMENT
399 	for (size_t i = 1; i < BLACKLIST_COUNT; i++) {
400 		if (strcmp(fbt_blacklist[i - 1], fbt_blacklist[i]) > 0) {
401 			panic("unordered fbt blacklist %s > %s", fbt_blacklist[i - 1], fbt_blacklist[i]);
402 		}
403 	}
404 #endif /* DEBUG || DEVELOPMENT */
405 }
406