Faultable syscall tracepoints updates
Mathieu Desnoyers
reviewerv5 cover Faultable syscall tracepoints updates
This series addresses use-after-free of faultable syscall tracepoints reported by test bots using Syzkaller.
This applies on linux-next 20241022.
Thanks,
Mathieu
Mathieu Desnoyers (4):
tracing: Introduce tracepoint extended structure
tracing: Introduce tracepoint_is_faultable()
tracing: Fix syscall tracepoint use-after-free
tracing: Add might_fault() check in __DECLARE_TRACE_SYSCALL
include/linux/tracepoint-defs.h | 10 ++++++-- include/linux/tracepoint.h | 44 ++++++++++++++++++++++++++++----- include/trace/define_trace.h | 2 +- kernel/tracepoint.c | 20 ++++++++------- 4 files changed, 58 insertions(+), 18 deletions(-)
Signature · 1 line
2.39.5
Mathieu Desnoyers
reviewerPATCH v5 1/4 tracing: Introduce tracepoint extended structure
Shrink the struct tracepoint size from 80 bytes to 72 bytes on x86-64 by moving the (typically NULL) regfunc/unregfunc pointers to an extended structure.
---
Changes since v4:
- Add static to DEFINE_TRACE_FN.
--- include/linux/tracepoint-defs.h | 8 ++++++-- include/linux/tracepoint.h | 19 +++++++++++++------ kernel/tracepoint.c | 9 ++++----- 3 files changed, 23 insertions(+), 13 deletions(-)
include/linux/tracepoint-defs.h +6 -2
@@ -29,6 +29,11 @@ struct tracepoint_func {int prio; }; +struct tracepoint_ext { +int (*regfunc)(void ); +void (*unregfunc)(void ); +}; +struct tracepoint {const char *name;/* Tracepoint name */ struct static_key_false key;
@@ -36,9 +41,8 @@ struct tracepoint {void *static_call_tramp;void *iterator;void *probestub; -int (*regfunc)(void ); -void (*unregfunc)(void );struct tracepoint_func __rcu *funcs; +struct tracepoint_ext *ext; };#ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
include/linux/tracepoint.h +13 -6
@@ -302,7 +302,7 @@ static inline struct tracepoint *tracepoint_ptr_deref(tracepoint_ptr_t *p)* structures, so we create an array of pointers that will be used for iteration * on the tracepoints. */ -#define DEFINE_TRACE_FN (_name, _reg, _unreg, proto, args) \ +#define __DEFINE_TRACE_EXT (_name, _ext, proto, args) \ static const char __tpstrtab_##_name[] \ __section("__tracepoints_strings") = #_name; \ extern struct static_call_key STATIC_CALL_KEY(tp_func_##_name); \
@@ -316,9 +316,9 @@ static inline struct tracepoint *tracepoint_ptr_deref(tracepoint_ptr_t *p) .static_call_tramp = STATIC_CALL_TRAMP_ADDR(tp_func_##_name), \ .iterator = &__traceiter_##_name, \ .probestub = &__probestub_##_name, \ - .regfunc = _reg, \ - .unregfunc = _unreg, \ - .funcs = NULL }; \ + .funcs = NULL, \ + .ext = _ext, \ + }; \__TRACEPOINT_ENTRY (_name); \ int __traceiter_##_name(void *__data, proto) \ { \
@@ -341,8 +341,15 @@ static inline struct tracepoint *tracepoint_ptr_deref(tracepoint_ptr_t *p) } \ DEFINE_STATIC_CALL(tp_func_##_name, __traceiter_##_name); -#define DEFINE_TRACE (name, proto, args) \ - DEFINE_TRACE_FN(name, NULL, NULL, PARAMS(proto), PARAMS(args)); +#define DEFINE_TRACE_FN (_name, _reg, _unreg, _proto, _args) \ + static struct tracepoint_ext __tracepoint_ext_##_name = { \ + .regfunc = _reg, \ + .unregfunc = _unreg, \ + }; \ + __DEFINE_TRACE_EXT(_name, &__tracepoint_ext_##_name, PARAMS(_proto), PARAMS(_args)); + +#define DEFINE_TRACE (_name, _proto, _args) \ + __DEFINE_TRACE_EXT(_name, NULL, PARAMS(_proto), PARAMS(_args));#define EXPORT_TRACEPOINT_SYMBOL_GPL (name) \ EXPORT_SYMBOL_GPL(__tracepoint_##name); \
kernel/tracepoint.c +4 -5
@@ -278,8 +278,8 @@ static int tracepoint_add_func(struct tracepoint *tp,struct tracepoint_func *old, *tp_funcs;int ret; -if (tp->regfunc && !static_key_enabled (&tp->key)) { - ret = tp->regfunc (); +if (tp->ext && tp->ext->regfunc && !static_key_enabled (&tp->key)) { + ret = tp->ext->regfunc ();if (ret <0 )return ret; }
@@ -362,9 +362,8 @@ static int tracepoint_remove_func(struct tracepoint *tp,switch (nr_func_state (tp_funcs)) {case TP_FUNC_0 :/* 1->0 */ /* Removed last function */ -if (tp->unregfunc &&static_key_enabled (&tp->key)) - tp->unregfunc (); - +if (tp->ext && tp->ext->unregfunc &&static_key_enabled (&tp->key)) + tp->ext->unregfunc ();static_branch_disable (&tp->key);/* Set iterator static call */ tracepoint_update_call (tp, tp_funcs);
Signature · 1 line
2.39.5
Mathieu Desnoyers
reviewerPATCH v5 2/4 tracing: Introduce tracepoint_is_faultable()
In reply to Mathieu DesnoyersIntroduce a "faultable" flag within the extended structure to know whether a tracepoint needs rcu tasks trace grace period before reclaim. This can be queried using tracepoint_is_faultable().
---
Changes since v4:
- Add static to DEFINE_TRACE_SYSCALL.
--- include/linux/tracepoint-defs.h | 2 ++ include/linux/tracepoint.h | 24 ++++++++++++++++++++++++ include/trace/define_trace.h | 2 +- 3 files changed, 27 insertions(+), 1 deletion(-)
include/linux/tracepoint-defs.h +2 -0
@@ -32,6 +32,8 @@ struct tracepoint_func {struct tracepoint_ext {int (*regfunc)(void );void (*unregfunc)(void ); +/* Flags. */ +unsigned int faultable:1 ; };struct tracepoint {
include/linux/tracepoint.h +24 -0
@@ -104,6 +104,12 @@ void for_each_tracepoint_in_module(struct module *mod,* tracepoint_synchronize_unregister must be called between the last tracepoint * probe unregistration and the end of module exit to make sure there is no * caller executing a probe when it is freed. +* +* An alternative is to use the following for batch reclaim associated +* with a given tracepoint: +* +* - tracepoint_is_faultable() == false: call_rcu() +* - tracepoint_is_faultable() == true: call_rcu_tasks_trace() */ #ifdef CONFIG_TRACEPOINTS static inline void tracepoint_synchronize_unregister (void )
@@ -111,9 +117,17 @@ static inline void tracepoint_synchronize_unregister(void)synchronize_rcu_tasks_trace (); synchronize_rcu(); } +static inline bool tracepoint_is_faultable (struct tracepoint *tp) +{ +return tp->ext && tp->ext->faultable; +}#else static inline void tracepoint_synchronize_unregister (void ) { } +static inline bool tracepoint_is_faultable (struct tracepoint *tp) +{ +return false; +}#endif #ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS
@@ -345,6 +359,15 @@ static inline struct tracepoint *tracepoint_ptr_deref(tracepoint_ptr_t *p) static struct tracepoint_ext __tracepoint_ext_##_name = { \ .regfunc = _reg, \ .unregfunc = _unreg, \ + .faultable = false, \ + }; \ + __DEFINE_TRACE_EXT(_name, &__tracepoint_ext_##_name, PARAMS(_proto), PARAMS(_args)); + +#define DEFINE_TRACE_SYSCALL (_name, _reg, _unreg, _proto, _args) \ + static struct tracepoint_ext __tracepoint_ext_##_name = { \ + .regfunc = _reg, \ + .unregfunc = _unreg, \ + .faultable = true, \ }; \ __DEFINE_TRACE_EXT(_name, &__tracepoint_ext_##_name, PARAMS(_proto), PARAMS(_args));
@@ -389,6 +412,7 @@ static inline struct tracepoint *tracepoint_ptr_deref(tracepoint_ptr_t *p)#define __DECLARE_TRACE_SYSCALL __DECLARE_TRACE#define DEFINE_TRACE_FN (name, reg, unreg, proto, args) +#define DEFINE_TRACE_SYSCALL (name, reg, unreg, proto, args)#define DEFINE_TRACE (name, proto, args)#define EXPORT_TRACEPOINT_SYMBOL_GPL (name)#define EXPORT_TRACEPOINT_SYMBOL (name)
include/trace/define_trace.h +1 -1
@@ -48,7 +48,7 @@#undef TRACE_EVENT_SYSCALL#define TRACE_EVENT_SYSCALL (name, proto, args, struct, assign, print, reg, unreg) \ - DEFINE_TRACE_FN(name, reg, unreg, PARAMS(proto), PARAMS(args)) + DEFINE_TRACE_SYSCALL(name, reg, unreg, PARAMS(proto), PARAMS(args))#undef TRACE_EVENT_NOP#define TRACE_EVENT_NOP (name, proto, args, struct, assign, print)
Signature · 1 line
2.39.5
Mathieu Desnoyers
reviewerPATCH v5 3/4 tracing: Fix syscall tracepoint use-after-free
In reply to Mathieu DesnoyersThe grace period used internally within tracepoint.c:release_probes() uses call_rcu() to batch waiting for quiescence of old probe arrays, rather than using the tracepoint_synchronize_unregister() which blocks while waiting for quiescence.
With the introduction of faultable syscall tracepoints, this causes use-after-free issues reproduced with syzkaller.
Fix this by using the appropriate call_rcu() or call_rcu_tasks_trace() before invoking the rcu_free_old_probes callback. This can be chosen using the tracepoint_is_faultable() API.
A similar issue exists in bpf use of call_rcu(). Fixing this is left to a separate change.
---
Changes since v0:
- Introduce tracepoint_call_rcu(),
- Fix bpf_link_free() use of call_rcu as well.
Changes since v1:
- Use tracepoint_call_rcu() for bpf_prog_put as well.
Changes since v2:
- Do not cover bpf changes in the same commit, let bpf developers implement it.
Changes since v3:
- Use tracepoint_is_faultable().
--- kernel/tracepoint.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-)
kernel/tracepoint.c +7 -4
@@ -106,13 +106,16 @@ static void rcu_free_old_probes(struct rcu_head *head)kfree (container_of(head ,struct tp_probes ,rcu )); } -static inline void release_probes (struct tracepoint_func *old) +static inline void release_probes (struct tracepoint *tp,struct tracepoint_func *old) {if (old) {struct tp_probes *tp_probes =container_of (old, struct tp_probes, probes[0]); -call_rcu (&tp_probes->rcu, rcu_free_old_probes); +if (tracepoint_is_faultable (tp)) +call_rcu_tasks_trace (&tp_probes->rcu, rcu_free_old_probes); +else +call_rcu (&tp_probes->rcu, rcu_free_old_probes); } }
@@ -334,7 +337,7 @@ static int tracepoint_add_func(struct tracepoint *tp,break ; } -release_probes (old); +release_probes (tp, old);return 0 ; }
@@ -405,7 +408,7 @@ static int tracepoint_remove_func(struct tracepoint *tp,WARN_ON_ONCE (1 );break ; } -release_probes (old); +release_probes (tp, old);return 0 ; }
Signature · 1 line
2.39.5
Mathieu Desnoyers
reviewerPATCH v5 4/4 tracing: Add might_fault() check in __DECLARE_TRACE_SYSCALL
In reply to Mathieu DesnoyersCatch incorrect use of syscall tracepoints even if no probes are registered by adding a might_fault() check in trace_##name() emitted by __DECLARE_TRACE_SYSCALL.
---
Changes since v4:
- Move might_fault() to trace_##name() emitted by __DECLARE_TRACE_SYSCALL so it is validated even when the tracepoint is disabled.
--- include/linux/tracepoint.h | 1 + 1 file changed, 1 insertion(+)
include/linux/tracepoint.h +1 -0
@@ -301,6 +301,7 @@ static inline struct tracepoint *tracepoint_ptr_deref(tracepoint_ptr_t *p) __DECLARE_TRACE_COMMON(name, PARAMS(proto), PARAMS(args), cond, PARAMS(data_proto)) \ static inline void trace_##name(proto) \ { \ + might_fault(); \ if (static_branch_unlikely(&__tracepoint_##name.key)) \ __DO_TRACE(name, \ TP_ARGS(args), \
Signature · 1 line
2.39.5
Steven Rostedt
maintainerQuoted 4 lines · On Wed, 30 Oct 2024 10:46:30 -0400 Mathieu Desnoyers…
This series addresses use-after-free of faultable syscall tracepoints reported by test bots using Syzkaller.
This applies on linux-next 20241022.
Please Cc, [email protected] so that your patches appear in patchwork. Otherwise they will not be seen. I can pull them from my "INBOX patchwork" but that is outside the workflow of the tracing subsystem.
-- Steve