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(__arm64__)
42 #define ARM_ONLY(s) #s,
43 #else
44 #define ARM_ONLY(s)
45 #endif /* 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 #if SCHED_HYGIENE_DEBUG
105 CLOSURE(abandon_preemption_disable_measurement)
106 #endif
107 CLOSURE(absolutetime_to_microtime)
108 X86_ONLY(acpi_)
109 X86_ONLY(act_machine)
110 CLOSURE(act_set_astbsd)
111 ARM_ONLY(alternate_debugger_enter)
112 ARM_ONLY(arm_init_idle_cpu)
113 CLOSURE(ast_dtrace_on)
114 CLOSURE(ast_pending)
115 CRITICAL(backtrace_)
116 CRITICAL(bcopy)
117 CLOSURE(clean_dcache)
118 CLOSURE(clean_mmu_dcache)
119 CRITICAL(clock_)
120 X86_ONLY(commpage_)
121 CLOSURE(copyin)
122 CLOSURE(copyout)
123 CRITICAL(cpu_)
124 CLOSURE(current_act)
125 CLOSURE(current_percpu_base)
126 CLOSURE(current_proc)
127 CLOSURE(current_processor)
128 CLOSURE(current_task)
129 CLOSURE(current_task_early)
130 CLOSURE(current_thread)
131 CLOSURE(current_thread_ro)
132 CLOSURE(current_thread_ro_unchecked)
133 CLOSURE(current_uthread)
134 CLOSURE(debug_)
135 X86_ONLY(dsmos_)
136 CLOSURE(dtrace_)
137 CRITICAL(enter_lohandler)
138 CRITICAL(fasttrap_)
139 CRITICAL(fbt_invop)
140 CRITICAL(fbt_perfCallback)
141 CLOSURE(find_user_regs)
142 ARM_ONLY(fleh_)
143 CLOSURE(flush_dcache)
144 ARM_ONLY(flush_mmu_tlb_)
145 CLOSURE(flush_tlb64)
146 CRITICAL(fuword)
147 X86_ONLY(get_active_thread)
148 CLOSURE(get_bsdtask_info)
149 CLOSURE(get_bsdthread_info)
150 CLOSURE(get_machthread)
151 CRITICAL(get_preemption_level)
152 CRITICAL(get_thread_ro)
153 CRITICAL(get_thread_ro_unchecked)
154 CRITICAL(get_threadtask)
155 CRITICAL(get_threadtask_early)
156 ARM_ONLY(get_vfp_enabled)
157 CRITICAL(getminor)
158 CRITICAL(handle_pending_TLB_flushes)
159 CRITICAL(hibernate_)
160 X86_ONLY(hndl_)
161 CRITICAL(hw_)
162 X86_ONLY(idt64)
163 CRITICAL(interrupt)
164 CRITICAL(invalidate_mmu_icache)
165 CRITICAL(is_saved_state32)
166 KASAN_ONLY(kasan)
167 CLOSURE(kauth_cred_get)
168 CLOSURE(kauth_getgid)
169 CLOSURE(kauth_getuid)
170 CRITICAL(kdb_)
171 CRITICAL(kdp_)
172 CRITICAL(kernel_preempt_check)
173 CRITICAL(kernel_trap)
174 CRITICAL(kprintf)
175 CRITICAL(ks_)
176 CLOSURE(kvtophys)
177 X86_ONLY(lapic_)
178 CRITICAL(lo_alltraps)
179 CRITICAL(lock_debugger)
180 CLOSURE(mach_absolute_time)
181 CRITICAL(machine_)
182 X86_ONLY(mapping_)
183 CRITICAL(mca_cpu_alloc)
184 CRITICAL(mca_cpu_init)
185 CLOSURE(memcpy)
186 CLOSURE(memmove)
187 CRITICAL(ml_)
188 CLOSURE(mt_core_snap)
189 CLOSURE(mt_cur_cpu)
190 CLOSURE(mt_fixed_counts)
191 CLOSURE(mt_fixed_counts_internal)
192 CLOSURE(mt_mtc_update_count)
193 CLOSURE(mt_mtc_update_fixed_counts)
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_get_task_raw)
231 CLOSURE(proc_is64bit)
232 CLOSURE(proc_require)
233 CLOSURE(proc_task)
234 CRITICAL(rbtrace_bt)
235 CLOSURE(recount_)
236 CRITICAL(register_cpu_setup_func)
237 CRITICAL(ret64_iret)
238 CRITICAL(ret_to_user)
239 CRITICAL(return_to_kernel)
240 CRITICAL(return_to_user)
241 CRITICAL(rtc_)
242 CRITICAL(rtclock_)
243 CRITICAL(saved_state64)
244 CLOSURE(sdt_getargdesc)
245 CRITICAL(sdt_invop)
246 CLOSURE(setPop)
247 ARM_ONLY(sleh_)
248 CRITICAL(sprlock)
249 CRITICAL(sprunlock)
250 CLOSURE(strlcpy)
251 CRITICAL(strlen)
252 CRITICAL(strncmp)
253 CRITICAL(suword)
254 X86_ONLY(sync_iss_to_iks_unconditionally)
255 CLOSURE(systrace_stub)
256 CRITICAL(t_invop)
257 CLOSURE(task_get_proc_raw)
258 CLOSURE(thread_tid)
259 CLOSURE(timer_grab)
260 ARM_ONLY(timer_state_event)
261 CRITICAL(tmrCvt)
262 CRITICAL(trap_from_kernel)
263 CRITICAL(traptrace_)
264 CRITICAL(tsc_)
265 CRITICAL(uart_putc)
266 CRITICAL(unlock_debugger)
267 CRITICAL(unpackA)
268 CRITICAL(unregister_cpu_setup_func)
269 CRITICAL(uread)
270 CLOSURE(uthread_is64bit)
271 CRITICAL(uwrite)
272 CRITICAL(vstart)
273 CLOSURE(zone_has_index)
274 CLOSURE(zone_id_require)
275 CLOSURE(zone_id_require_panic)
276 CLOSURE(zone_range_contains)
277 CLOSURE(zone_require_panic)
278 CLOSURE(zone_require_ro)
279 CLOSURE(zpercpu_count)
280 };
281 #define BLACKLIST_COUNT (sizeof(fbt_blacklist)/sizeof(fbt_blacklist[0]))
282
283 /*
284 * Modules that should not be probed.
285 *
286 * This must be kept in asciibetical order for purposes of bsearch().
287 */
288 static const char* fbt_module_blacklist[] = {
289 X86_ONLY(com.apple.driver.AppleACPIEC)
290 X86_ONLY(com.apple.driver.AppleACPIPlatform)
291 ARM_ONLY(com.apple.driver.AppleARMPlatform)
292 X86_ONLY(com.apple.driver.AppleEFI)
293 X86_ONLY(com.apple.driver.AppleIntelCPUPowerManagement)
294 ARM_ONLY(com.apple.driver.AppleInterruptController)
295 X86_ONLY(com.apple.driver.AppleRTC)
296 X86_ONLY(com.apple.iokit.IOACPIFamily)
297 };
298 #define MODULE_BLACKLIST_COUNT (sizeof(fbt_module_blacklist)/sizeof(fbt_module_blacklist[0]))
299
300 int ignore_fbt_blacklist = 0;
301 extern int dtrace_kernel_symbol_mode;
302
303 #pragma clang diagnostic push
304 #pragma clang diagnostic ignored "-Wcast-qual"
305 static int
_cmp(const void * a,const void * b)306 _cmp(const void *a, const void *b)
307 {
308 const char *v = *(const char **)b;
309 return strncmp((const char *)a, v, strlen(v));
310 }
311
312
313 #pragma clang diagnostic pop
314 /*
315 * Module validation
316 */
317 bool
fbt_module_excluded(struct modctl * ctl)318 fbt_module_excluded(struct modctl* ctl)
319 {
320 const char *excluded;
321
322 ASSERT(!MOD_FBT_DONE(ctl));
323
324 if (ctl->mod_address == 0 || ctl->mod_size == 0 || !ctl->mod_loaded) {
325 return true;
326 }
327
328 if (ignore_fbt_blacklist) {
329 return false;
330 }
331
332 excluded = bsearch(ctl->mod_modname, fbt_module_blacklist,
333 MODULE_BLACKLIST_COUNT, sizeof(fbt_module_blacklist[0]), _cmp);
334 return excluded;
335 }
336
337 /*
338 * FBT probe name validation
339 */
340 bool
fbt_excluded(const char * name)341 fbt_excluded(const char* name)
342 {
343 const char *excluded;
344
345 if (ignore_fbt_blacklist) {
346 return false;
347 }
348
349 excluded = bsearch(name, fbt_blacklist, BLACKLIST_COUNT, sizeof(name),
350 _cmp );
351 return excluded;
352 }
353
354 SYSCTL_DECL(_kern_dtrace);
355
356 static int
357 sysctl_dtrace_ignore_fbt_blacklist SYSCTL_HANDLER_ARGS
358 {
359 #pragma unused(oidp, arg2)
360 int err;
361 int value = *(int*)arg1;
362
363 err = sysctl_io_number(req, value, sizeof(value), &value, NULL);
364 if (err) {
365 return err;
366 }
367 if (req->newptr) {
368 if (!(value == 0 || value == 1)) {
369 return ERANGE;
370 }
371
372 /*
373 * We do not allow setting the blacklist back to on, as we have no way
374 * of knowing if those unsafe probes are still used.
375 *
376 * If we are using kernel symbols, we also do not allow any change,
377 * since the symbols are jettison'd after the first pass.
378 *
379 * We do not need to take any locks here because those symbol modes
380 * are permanent and do not change after boot.
381 */
382 if (value != 1 || dtrace_kernel_symbol_mode == DTRACE_KERNEL_SYMBOLS_NEVER ||
383 dtrace_kernel_symbol_mode == DTRACE_KERNEL_SYMBOLS_ALWAYS_FROM_KERNEL) {
384 return EPERM;
385 }
386
387 ignore_fbt_blacklist = 1;
388 }
389
390 return 0;
391 }
392
393 SYSCTL_PROC(_kern_dtrace, OID_AUTO, ignore_fbt_blacklist,
394 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
395 &ignore_fbt_blacklist, 0,
396 sysctl_dtrace_ignore_fbt_blacklist, "I", "fbt provider ignore blacklist");
397
398 void
fbt_blacklist_init(void)399 fbt_blacklist_init(void)
400 {
401 PE_parse_boot_argn("IgnoreFBTBlacklist", &ignore_fbt_blacklist, sizeof(ignore_fbt_blacklist));
402 #if DEBUG || DEVELOPMENT
403 for (size_t i = 1; i < BLACKLIST_COUNT; i++) {
404 if (strcmp(fbt_blacklist[i - 1], fbt_blacklist[i]) > 0) {
405 panic("unordered fbt blacklist %s > %s", fbt_blacklist[i - 1], fbt_blacklist[i]);
406 }
407 }
408 #endif /* DEBUG || DEVELOPMENT */
409 }
410