From: Mathieu Desnoyers Date: Thu, 14 Feb 2019 21:14:42 +0000 (-0500) Subject: Fix: procname context async-signal safety X-Git-Tag: v2.12.0-rc1~56 X-Git-Url: https://git.lttng.org./?a=commitdiff_plain;h=74011e88983b12958dacd6621d60c18f6a84f59b;p=lttng-ust.git Fix: procname context async-signal safety Ensure an instrumented signal handler interrupting the procname context code will not write a partial process name string into the trace context. Signed-off-by: Mathieu Desnoyers --- diff --git a/liblttng-ust/lttng-context-procname.c b/liblttng-ust/lttng-context-procname.c index b6e69501..95d5f28d 100644 --- a/liblttng-ust/lttng-context-procname.c +++ b/liblttng-ust/lttng-context-procname.c @@ -28,6 +28,9 @@ #include #include "compat.h" +/* Maximum number of nesting levels for the procname cache. */ +#define PROCNAME_NESTING_MAX 2 + /* * We cache the result to ensure we don't trigger a system call for * each event. @@ -37,22 +40,34 @@ * be set for a thread before the first event is logged within this * thread. */ -typedef char procname_array[17]; +typedef char procname_array[PROCNAME_NESTING_MAX][17]; + static DEFINE_URCU_TLS(procname_array, cached_procname); +static DEFINE_URCU_TLS(int, procname_nesting); + static inline char *wrapper_getprocname(void) { - if (caa_unlikely(!URCU_TLS(cached_procname)[0])) { - lttng_ust_getprocname(URCU_TLS(cached_procname)); - URCU_TLS(cached_procname)[LTTNG_UST_PROCNAME_LEN - 1] = '\0'; + int nesting = URCU_TLS(procname_nesting); + + if (caa_unlikely(nesting >= PROCNAME_NESTING_MAX)) + return ""; + if (caa_unlikely(!URCU_TLS(cached_procname)[nesting][0])) { + CMM_STORE_SHARED(URCU_TLS(procname_nesting), nesting + 1); + lttng_ust_getprocname(URCU_TLS(cached_procname)[nesting]); + URCU_TLS(cached_procname)[nesting][LTTNG_UST_PROCNAME_LEN - 1] = '\0'; + CMM_STORE_SHARED(URCU_TLS(procname_nesting), nesting); } - return URCU_TLS(cached_procname); + return URCU_TLS(cached_procname)[nesting]; } void lttng_context_procname_reset(void) { - URCU_TLS(cached_procname)[0] = '\0'; + URCU_TLS(cached_procname)[1][0] = '\0'; + CMM_STORE_SHARED(URCU_TLS(procname_nesting), 1); + URCU_TLS(cached_procname)[0][0] = '\0'; + CMM_STORE_SHARED(URCU_TLS(procname_nesting), 0); } static