mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH kernel] tracepoint: Fix race between tracing and removing tracepoint
@ 2021-02-02  7:23 Alexey Kardashevskiy
  2021-02-02 10:08 ` Peter Zijlstra
  0 siblings, 1 reply; 2+ messages in thread
From: Alexey Kardashevskiy @ 2021-02-02  7:23 UTC (permalink / raw)
  To: Steven Rostedt
  Cc: Alexey Kardashevskiy, Peter Zijlstra (Intel),
	Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
	Ingo Molnar, John Fastabend, KP Singh, Martin KaFai Lau,
	Song Liu, Yonghong Song, linux-kernel

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()")
Signed-off-by: Alexey Kardashevskiy <aik@ozlabs.ru>
---

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(-)

diff --git a/include/linux/tracepoint.h b/include/linux/tracepoint.h
index 0f21617f1a66..966ed8980327 100644
--- a/include/linux/tracepoint.h
+++ b/include/linux/tracepoint.h
@@ -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);
-- 
2.17.1


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH kernel] tracepoint: Fix race between tracing and removing tracepoint
  2021-02-02  7:23 [PATCH kernel] tracepoint: Fix race between tracing and removing tracepoint Alexey Kardashevskiy
@ 2021-02-02 10:08 ` Peter Zijlstra
  0 siblings, 0 replies; 2+ messages in thread
From: Peter Zijlstra @ 2021-02-02 10:08 UTC (permalink / raw)
  To: Alexey Kardashevskiy
  Cc: Steven Rostedt, Alexei Starovoitov, Andrii Nakryiko,
	Daniel Borkmann, Ingo Molnar, John Fastabend, KP Singh,
	Martin KaFai Lau, Song Liu, Yonghong Song, linux-kernel

On Tue, Feb 02, 2021 at 06:23:26PM +1100, Alexey Kardashevskiy wrote:
> 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.

> Fixes: d25e37d89dd2f ("tracepoint: Optimize using static_call()")
> Signed-off-by: Alexey Kardashevskiy <aik@ozlabs.ru>

>  		it_func_ptr =						\
>  			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;						\


Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org>

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-02-02 10:10 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-02-02  7:23 [PATCH kernel] tracepoint: Fix race between tracing and removing tracepoint Alexey Kardashevskiy
2021-02-02 10:08 ` Peter Zijlstra

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®