Skip to contentSkip to conversation

tracepoint: Fix race between tracing and removing tracepoint

PATCH In review Alexey Kardashevskiy 2 participants 2 messages Feb 2, 2021 Tracing

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tracepoint: Fix race between tracing and removing tracepoint

When executing a tracepoint, the tracepoint's func is dereferenced twice - in __DO_TRACE() (where the returned pointer is checked) and later on in __traceiter_##_name where the returned pointer is dereferenced without checking which leads to races against tracepoint_removal_sync() and crashes.

This adds a check before referencing the pointer in tracepoint_ptr_deref.

Fixes d25e37d89dd2f ("tracepoint: Optimize using static_call()")

---

This is in reply to https://lkml.org/lkml/2021/2/1/868

Feel free to change the commit log. Thanks!

Fixing it properly is rather scary :)
I tried passing it_func_ptr to it_func but this change triggered way too many prototypes changes such as __bpf_trace_##call().

---
 include/linux/tracepoint.h | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
include/linux/tracepoint.h +7 -5
index 0f21617f1a66..966ed8980327 100644
@@ -307,11 +307,13 @@ static inline struct tracepoint *tracepoint_ptr_deref(tracepoint_ptr_t *p)
 									\
 		it_func_ptr =						\
 			rcu_dereference_raw((&__tracepoint_##_name)->funcs); \
-		do {							\
-			it_func = (it_func_ptr)->func;			\
-			__data = (it_func_ptr)->data;			\
-			((void(*)(void *, proto))(it_func))(__data, args); \
-		} while ((++it_func_ptr)->func);			\
+		if (it_func_ptr) {					\
+			do {						\
+				it_func = (it_func_ptr)->func;		\
+				__data = (it_func_ptr)->data;		\
+				((void(*)(void *, proto))(it_func))(__data, args); \
+			} while ((++it_func_ptr)->func);		\
+		}							\
 		return 0;						\
 	}								\
 	DEFINE_STATIC_CALL(tp_func_##_name, __traceiter_##_name);
Signature · 1 line
2.17.1
Acked-by on v1 1/1
#1 lore raw
Quoted 7 lines · Alexey Kardashevskiy

When executing a tracepoint, the tracepoint's func is dereferenced twice - in __DO_TRACE() (where the returned pointer is checked) and later on in __traceiter_##_name where the returned pointer is dereferenced without checking which leads to races against tracepoint_removal_sync() and crashes.

This adds a check before referencing the pointer in tracepoint_ptr_deref.

Agreed, a reload got added and it doesn't check the value again.

Quoted 4 lines
Fixes d25e37d89dd2f ("tracepoint: Optimize using static_call()")
include/linux/tracepoint.h +0 -0
 		it_func_ptr =						\

include/linux/tracepoint.h static inline struct tracepoint *tracepoint_ptr_deref(tracepoint_ptr_t *p) · line 317

Quoted 10 lines
include/linux/tracepoint.h +7 -0
 			rcu_dereference_raw((&__tracepoint_##_name)->funcs); \
 
+		if (it_func_ptr) {					\
+			do {						\
+				it_func = (it_func_ptr)->func;		\
+				__data = (it_func_ptr)->data;		\
+				((void(*)(void *, proto))(it_func))(__data, args); \
+			} while ((++it_func_ptr)->func);		\
+		}							\
 		return 0;						\