From 9d68f7741f72f3becdc8ec05edf32b58aef24a5e Mon Sep 17 00:00:00 2001 From: Mateusz Guzik Date: Fri, 27 Apr 2018 15:16:34 +0000 Subject: [PATCH] systrace: track it like sdt probes While here predict false. Note the code is wrong (regardless of this change). Dereference of the pointer can race with module unload. A fix would set the probe to a nop stub instead of NULL. --- sys/cddl/dev/systrace/systrace.c | 8 ++++++++ sys/kern/kern_dtrace.c | 3 ++- sys/kern/subr_syscall.c | 6 ++++-- sys/sys/sysent.h | 1 + 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/sys/cddl/dev/systrace/systrace.c b/sys/cddl/dev/systrace/systrace.c index 02b089b4252..2ef74cb15e0 100644 --- a/sys/cddl/dev/systrace/systrace.c +++ b/sys/cddl/dev/systrace/systrace.c @@ -135,6 +135,8 @@ extern const char *freebsd32_syscallnames[]; #error 1 << SYSTRACE_SHIFT must exceed number of system calls #endif +static int systrace_enabled_count; + static void systrace_load(void *); static void systrace_unload(void *); @@ -315,6 +317,9 @@ systrace_enable(void *arg, dtrace_id_t id, void *parg) SYSENT[sysnum].sy_entry = id; else SYSENT[sysnum].sy_return = id; + systrace_enabled_count++; + if (systrace_enabled_count == 1) + systrace_enabled = true; } static void @@ -324,6 +329,9 @@ systrace_disable(void *arg, dtrace_id_t id, void *parg) SYSENT[sysnum].sy_entry = 0; SYSENT[sysnum].sy_return = 0; + systrace_enabled_count--; + if (systrace_enabled_count == 0) + systrace_enabled = false; } static void diff --git a/sys/kern/kern_dtrace.c b/sys/kern/kern_dtrace.c index 7b13a52b3f3..cbe6bdf15fd 100644 --- a/sys/kern/kern_dtrace.c +++ b/sys/kern/kern_dtrace.c @@ -56,7 +56,8 @@ dtrace_doubletrap_func_t dtrace_doubletrap_func; dtrace_pid_probe_ptr_t dtrace_pid_probe_ptr; dtrace_return_probe_ptr_t dtrace_return_probe_ptr; -systrace_probe_func_t __read_frequently systrace_probe_func; +bool __read_frequently systrace_enabled; +systrace_probe_func_t systrace_probe_func; /* Return the DTrace process data size compiled in the kernel hooks. */ size_t diff --git a/sys/kern/subr_syscall.c b/sys/kern/subr_syscall.c index bca47edaf61..6c70ddc9f78 100644 --- a/sys/kern/subr_syscall.c +++ b/sys/kern/subr_syscall.c @@ -126,7 +126,8 @@ syscallenter(struct thread *td) #ifdef KDTRACE_HOOKS /* Give the syscall:::entry DTrace probe a chance to fire. */ - if (systrace_probe_func != NULL && sa->callp->sy_entry != 0) + if (__predict_false(systrace_enabled && + sa->callp->sy_entry != 0)) (*systrace_probe_func)(sa, SYSTRACE_ENTRY, 0); #endif @@ -140,7 +141,8 @@ syscallenter(struct thread *td) #ifdef KDTRACE_HOOKS /* Give the syscall:::return DTrace probe a chance to fire. */ - if (systrace_probe_func != NULL && sa->callp->sy_return != 0) + if (__predict_false(systrace_enabled && + sa->callp->sy_return != 0)) (*systrace_probe_func)(sa, SYSTRACE_RETURN, error ? -1 : td->td_retval[0]); #endif diff --git a/sys/sys/sysent.h b/sys/sys/sysent.h index c811a4a7f3b..9d85728e159 100644 --- a/sys/sys/sysent.h +++ b/sys/sys/sysent.h @@ -53,6 +53,7 @@ typedef void (*systrace_probe_func_t)(struct syscall_args *, enum systrace_probe_t, int); typedef void (*systrace_args_func_t)(int, void *, uint64_t *, int *); +extern bool systrace_enabled; extern systrace_probe_func_t systrace_probe_func; struct sysent { /* system call table */