linux-trace-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v3 1/2] tracing: Add task_prctl_unknown tracepoint
@ 2024-11-08 11:34 Marco Elver
  2024-11-08 11:34 ` [PATCH v3 2/2] tracing: Remove pid in task_rename tracing output Marco Elver
                   ` (3 more replies)
  0 siblings, 4 replies; 8+ messages in thread
From: Marco Elver @ 2024-11-08 11:34 UTC (permalink / raw)
  To: elver, Steven Rostedt, Kees Cook
  Cc: Masami Hiramatsu, Mathieu Desnoyers, Andrew Morton, Oleg Nesterov,
	linux-kernel, linux-trace-kernel, Dmitry Vyukov, kasan-dev

prctl() is a complex syscall which multiplexes its functionality based
on a large set of PR_* options. Currently we count 64 such options. The
return value of unknown options is -EINVAL, and doesn't distinguish from
known options that were passed invalid args that also return -EINVAL.

To understand if programs are attempting to use prctl() options not yet
available on the running kernel, provide the task_prctl_unknown
tracepoint.

Note, this tracepoint is in an unlikely cold path, and would therefore
be suitable for continuous monitoring (e.g. via perf_event_open).

While the above is likely the simplest usecase, additionally this
tracepoint can help unlock some testing scenarios (where probing
sys_enter or sys_exit causes undesirable performance overheads):

  a. unprivileged triggering of a test module: test modules may register a
     probe to be called back on task_prctl_unknown, and pick a very large
     unknown prctl() option upon which they perform a test function for an
     unprivileged user;

  b. unprivileged triggering of an eBPF program function: similar
     as idea (a).

Example trace_pipe output:

  test-380     [001] .....    78.142904: task_prctl_unknown: option=1234 arg2=101 arg3=102 arg4=103 arg5=104

Signed-off-by: Marco Elver <elver@google.com>
---
v3:
* Remove "comm".

v2:
* Remove "pid" in trace output (suggested by Steven).
---
 include/trace/events/task.h | 37 +++++++++++++++++++++++++++++++++++++
 kernel/sys.c                |  3 +++
 2 files changed, 40 insertions(+)

diff --git a/include/trace/events/task.h b/include/trace/events/task.h
index 47b527464d1a..209d315852fb 100644
--- a/include/trace/events/task.h
+++ b/include/trace/events/task.h
@@ -56,6 +56,43 @@ TRACE_EVENT(task_rename,
 		__entry->newcomm, __entry->oom_score_adj)
 );
 
+/**
+ * task_prctl_unknown - called on unknown prctl() option
+ * @option:	option passed
+ * @arg2:	arg2 passed
+ * @arg3:	arg3 passed
+ * @arg4:	arg4 passed
+ * @arg5:	arg5 passed
+ *
+ * Called on an unknown prctl() option.
+ */
+TRACE_EVENT(task_prctl_unknown,
+
+	TP_PROTO(int option, unsigned long arg2, unsigned long arg3,
+		 unsigned long arg4, unsigned long arg5),
+
+	TP_ARGS(option, arg2, arg3, arg4, arg5),
+
+	TP_STRUCT__entry(
+		__field(	int,		option)
+		__field(	unsigned long,	arg2)
+		__field(	unsigned long,	arg3)
+		__field(	unsigned long,	arg4)
+		__field(	unsigned long,	arg5)
+	),
+
+	TP_fast_assign(
+		__entry->option = option;
+		__entry->arg2 = arg2;
+		__entry->arg3 = arg3;
+		__entry->arg4 = arg4;
+		__entry->arg5 = arg5;
+	),
+
+	TP_printk("option=%d arg2=%ld arg3=%ld arg4=%ld arg5=%ld",
+		  __entry->option, __entry->arg2, __entry->arg3, __entry->arg4, __entry->arg5)
+);
+
 #endif
 
 /* This part must be outside protection */
diff --git a/kernel/sys.c b/kernel/sys.c
index 4da31f28fda8..b366cef102ec 100644
--- a/kernel/sys.c
+++ b/kernel/sys.c
@@ -75,6 +75,8 @@
 #include <asm/io.h>
 #include <asm/unistd.h>
 
+#include <trace/events/task.h>
+
 #include "uid16.h"
 
 #ifndef SET_UNALIGN_CTL
@@ -2785,6 +2787,7 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
 		error = RISCV_SET_ICACHE_FLUSH_CTX(arg2, arg3);
 		break;
 	default:
+		trace_task_prctl_unknown(option, arg2, arg3, arg4, arg5);
 		error = -EINVAL;
 		break;
 	}
-- 
2.47.0.277.g8800431eea-goog


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

* [PATCH v3 2/2] tracing: Remove pid in task_rename tracing output
  2024-11-08 11:34 [PATCH v3 1/2] tracing: Add task_prctl_unknown tracepoint Marco Elver
@ 2024-11-08 11:34 ` Marco Elver
  2024-11-15 12:00 ` [PATCH v3 1/2] tracing: Add task_prctl_unknown tracepoint Marco Elver
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 8+ messages in thread
From: Marco Elver @ 2024-11-08 11:34 UTC (permalink / raw)
  To: elver, Steven Rostedt, Kees Cook
  Cc: Masami Hiramatsu, Mathieu Desnoyers, Andrew Morton, Oleg Nesterov,
	linux-kernel, linux-trace-kernel, Dmitry Vyukov, kasan-dev

Remove pid in task_rename tracepoint output, since that tracepoint only
deals with the current task, and is printed by default. This also saves
some space in the entry and avoids wasted padding.

Link: https://lkml.kernel.org/r/20241105120247.596a0dc9@gandalf.local.home
Suggested-by: Steven Rostedt <rostedt@goodmis.org>
Signed-off-by: Marco Elver <elver@google.com>
---
v2:
* New patch
---
 include/trace/events/task.h | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)

diff --git a/include/trace/events/task.h b/include/trace/events/task.h
index 209d315852fb..af535b053033 100644
--- a/include/trace/events/task.h
+++ b/include/trace/events/task.h
@@ -38,22 +38,19 @@ TRACE_EVENT(task_rename,
 	TP_ARGS(task, comm),
 
 	TP_STRUCT__entry(
-		__field(	pid_t,	pid)
 		__array(	char, oldcomm,  TASK_COMM_LEN)
 		__array(	char, newcomm,  TASK_COMM_LEN)
 		__field(	short,	oom_score_adj)
 	),
 
 	TP_fast_assign(
-		__entry->pid = task->pid;
 		memcpy(entry->oldcomm, task->comm, TASK_COMM_LEN);
 		strscpy(entry->newcomm, comm, TASK_COMM_LEN);
 		__entry->oom_score_adj = task->signal->oom_score_adj;
 	),
 
-	TP_printk("pid=%d oldcomm=%s newcomm=%s oom_score_adj=%hd",
-		__entry->pid, __entry->oldcomm,
-		__entry->newcomm, __entry->oom_score_adj)
+	TP_printk("oldcomm=%s newcomm=%s oom_score_adj=%hd",
+		  __entry->oldcomm, __entry->newcomm, __entry->oom_score_adj)
 );
 
 /**
-- 
2.47.0.277.g8800431eea-goog


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

* Re: [PATCH v3 1/2] tracing: Add task_prctl_unknown tracepoint
  2024-11-08 11:34 [PATCH v3 1/2] tracing: Add task_prctl_unknown tracepoint Marco Elver
  2024-11-08 11:34 ` [PATCH v3 2/2] tracing: Remove pid in task_rename tracing output Marco Elver
@ 2024-11-15 12:00 ` Marco Elver
  2024-11-15 13:27   ` Steven Rostedt
  2024-12-12 10:05 ` Alexander Potapenko
  2024-12-17  0:38 ` Kees Cook
  3 siblings, 1 reply; 8+ messages in thread
From: Marco Elver @ 2024-11-15 12:00 UTC (permalink / raw)
  To: elver, Steven Rostedt, Kees Cook
  Cc: Masami Hiramatsu, Mathieu Desnoyers, Andrew Morton, Oleg Nesterov,
	linux-kernel, linux-trace-kernel, Dmitry Vyukov, kasan-dev

On Fri, 8 Nov 2024 at 12:35, Marco Elver <elver@google.com> wrote:
>
> prctl() is a complex syscall which multiplexes its functionality based
> on a large set of PR_* options. Currently we count 64 such options. The
> return value of unknown options is -EINVAL, and doesn't distinguish from
> known options that were passed invalid args that also return -EINVAL.
>
> To understand if programs are attempting to use prctl() options not yet
> available on the running kernel, provide the task_prctl_unknown
> tracepoint.
>
> Note, this tracepoint is in an unlikely cold path, and would therefore
> be suitable for continuous monitoring (e.g. via perf_event_open).
>
> While the above is likely the simplest usecase, additionally this
> tracepoint can help unlock some testing scenarios (where probing
> sys_enter or sys_exit causes undesirable performance overheads):
>
>   a. unprivileged triggering of a test module: test modules may register a
>      probe to be called back on task_prctl_unknown, and pick a very large
>      unknown prctl() option upon which they perform a test function for an
>      unprivileged user;
>
>   b. unprivileged triggering of an eBPF program function: similar
>      as idea (a).
>
> Example trace_pipe output:
>
>   test-380     [001] .....    78.142904: task_prctl_unknown: option=1234 arg2=101 arg3=102 arg4=103 arg5=104
>
> Signed-off-by: Marco Elver <elver@google.com>

Steven, unless there are any further objections, would you be able to
take this through the tracing tree?

Many thanks!

> ---
> v3:
> * Remove "comm".
>
> v2:
> * Remove "pid" in trace output (suggested by Steven).
> ---
>  include/trace/events/task.h | 37 +++++++++++++++++++++++++++++++++++++
>  kernel/sys.c                |  3 +++
>  2 files changed, 40 insertions(+)
>
> diff --git a/include/trace/events/task.h b/include/trace/events/task.h
> index 47b527464d1a..209d315852fb 100644
> --- a/include/trace/events/task.h
> +++ b/include/trace/events/task.h
> @@ -56,6 +56,43 @@ TRACE_EVENT(task_rename,
>                 __entry->newcomm, __entry->oom_score_adj)
>  );
>
> +/**
> + * task_prctl_unknown - called on unknown prctl() option
> + * @option:    option passed
> + * @arg2:      arg2 passed
> + * @arg3:      arg3 passed
> + * @arg4:      arg4 passed
> + * @arg5:      arg5 passed
> + *
> + * Called on an unknown prctl() option.
> + */
> +TRACE_EVENT(task_prctl_unknown,
> +
> +       TP_PROTO(int option, unsigned long arg2, unsigned long arg3,
> +                unsigned long arg4, unsigned long arg5),
> +
> +       TP_ARGS(option, arg2, arg3, arg4, arg5),
> +
> +       TP_STRUCT__entry(
> +               __field(        int,            option)
> +               __field(        unsigned long,  arg2)
> +               __field(        unsigned long,  arg3)
> +               __field(        unsigned long,  arg4)
> +               __field(        unsigned long,  arg5)
> +       ),
> +
> +       TP_fast_assign(
> +               __entry->option = option;
> +               __entry->arg2 = arg2;
> +               __entry->arg3 = arg3;
> +               __entry->arg4 = arg4;
> +               __entry->arg5 = arg5;
> +       ),
> +
> +       TP_printk("option=%d arg2=%ld arg3=%ld arg4=%ld arg5=%ld",
> +                 __entry->option, __entry->arg2, __entry->arg3, __entry->arg4, __entry->arg5)
> +);
> +
>  #endif
>
>  /* This part must be outside protection */
> diff --git a/kernel/sys.c b/kernel/sys.c
> index 4da31f28fda8..b366cef102ec 100644
> --- a/kernel/sys.c
> +++ b/kernel/sys.c
> @@ -75,6 +75,8 @@
>  #include <asm/io.h>
>  #include <asm/unistd.h>
>
> +#include <trace/events/task.h>
> +
>  #include "uid16.h"
>
>  #ifndef SET_UNALIGN_CTL
> @@ -2785,6 +2787,7 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
>                 error = RISCV_SET_ICACHE_FLUSH_CTX(arg2, arg3);
>                 break;
>         default:
> +               trace_task_prctl_unknown(option, arg2, arg3, arg4, arg5);
>                 error = -EINVAL;
>                 break;
>         }
> --
> 2.47.0.277.g8800431eea-goog
>

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

* Re: [PATCH v3 1/2] tracing: Add task_prctl_unknown tracepoint
  2024-11-15 12:00 ` [PATCH v3 1/2] tracing: Add task_prctl_unknown tracepoint Marco Elver
@ 2024-11-15 13:27   ` Steven Rostedt
  2024-11-15 15:06     ` Marco Elver
  0 siblings, 1 reply; 8+ messages in thread
From: Steven Rostedt @ 2024-11-15 13:27 UTC (permalink / raw)
  To: Marco Elver
  Cc: Kees Cook, Masami Hiramatsu, Mathieu Desnoyers, Andrew Morton,
	Oleg Nesterov, linux-kernel, linux-trace-kernel, Dmitry Vyukov,
	kasan-dev

On Fri, 15 Nov 2024 13:00:00 +0100
Marco Elver <elver@google.com> wrote:

> Steven, unless there are any further objections, would you be able to
> take this through the tracing tree?
> 
> Many thanks!

This isn't my file. Trace events usually belong to the subsystems that
use them. As this adds an event to kernel/sys.c which doesn't really have
an owner, then I would ask Andrew Morton to take it.

-- Steve

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

* Re: [PATCH v3 1/2] tracing: Add task_prctl_unknown tracepoint
  2024-11-15 13:27   ` Steven Rostedt
@ 2024-11-15 15:06     ` Marco Elver
  2024-12-11 14:09       ` Marco Elver
  0 siblings, 1 reply; 8+ messages in thread
From: Marco Elver @ 2024-11-15 15:06 UTC (permalink / raw)
  To: Steven Rostedt, Andrew Morton
  Cc: Kees Cook, Masami Hiramatsu, Mathieu Desnoyers, Oleg Nesterov,
	linux-kernel, linux-trace-kernel, Dmitry Vyukov, kasan-dev

On Fri, 15 Nov 2024 at 14:27, Steven Rostedt <rostedt@goodmis.org> wrote:
>
> On Fri, 15 Nov 2024 13:00:00 +0100
> Marco Elver <elver@google.com> wrote:
>
> > Steven, unless there are any further objections, would you be able to
> > take this through the tracing tree?
> >
> > Many thanks!
>
> This isn't my file. Trace events usually belong to the subsystems that
> use them. As this adds an event to kernel/sys.c which doesn't really have
> an owner, then I would ask Andrew Morton to take it.

Got it.

Andrew, can you pick this up?

Thanks,
-- Marco

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

* Re: [PATCH v3 1/2] tracing: Add task_prctl_unknown tracepoint
  2024-11-15 15:06     ` Marco Elver
@ 2024-12-11 14:09       ` Marco Elver
  0 siblings, 0 replies; 8+ messages in thread
From: Marco Elver @ 2024-12-11 14:09 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Kees Cook, Steven Rostedt, Masami Hiramatsu, Mathieu Desnoyers,
	Oleg Nesterov, linux-kernel, linux-trace-kernel, Dmitry Vyukov,
	kasan-dev

On Fri, 15 Nov 2024 at 16:06, Marco Elver <elver@google.com> wrote:
>
> On Fri, 15 Nov 2024 at 14:27, Steven Rostedt <rostedt@goodmis.org> wrote:
> >
> > On Fri, 15 Nov 2024 13:00:00 +0100
> > Marco Elver <elver@google.com> wrote:
> >
> > > Steven, unless there are any further objections, would you be able to
> > > take this through the tracing tree?
> > >
> > > Many thanks!
> >
> > This isn't my file. Trace events usually belong to the subsystems that
> > use them. As this adds an event to kernel/sys.c which doesn't really have
> > an owner, then I would ask Andrew Morton to take it.
>
> Got it.
>
> Andrew, can you pick this up?

Gentle ping - many thanks,
-- Marco

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

* Re: [PATCH v3 1/2] tracing: Add task_prctl_unknown tracepoint
  2024-11-08 11:34 [PATCH v3 1/2] tracing: Add task_prctl_unknown tracepoint Marco Elver
  2024-11-08 11:34 ` [PATCH v3 2/2] tracing: Remove pid in task_rename tracing output Marco Elver
  2024-11-15 12:00 ` [PATCH v3 1/2] tracing: Add task_prctl_unknown tracepoint Marco Elver
@ 2024-12-12 10:05 ` Alexander Potapenko
  2024-12-17  0:38 ` Kees Cook
  3 siblings, 0 replies; 8+ messages in thread
From: Alexander Potapenko @ 2024-12-12 10:05 UTC (permalink / raw)
  To: Marco Elver
  Cc: Steven Rostedt, Kees Cook, Masami Hiramatsu, Mathieu Desnoyers,
	Andrew Morton, Oleg Nesterov, linux-kernel, linux-trace-kernel,
	Dmitry Vyukov, kasan-dev

On Fri, Nov 8, 2024 at 12:35 PM 'Marco Elver' via kasan-dev
<kasan-dev@googlegroups.com> wrote:
>
> prctl() is a complex syscall which multiplexes its functionality based
> on a large set of PR_* options. Currently we count 64 such options. The
> return value of unknown options is -EINVAL, and doesn't distinguish from
> known options that were passed invalid args that also return -EINVAL.
>
> To understand if programs are attempting to use prctl() options not yet
> available on the running kernel, provide the task_prctl_unknown
> tracepoint.
>
> Note, this tracepoint is in an unlikely cold path, and would therefore
> be suitable for continuous monitoring (e.g. via perf_event_open).
>
> While the above is likely the simplest usecase, additionally this
> tracepoint can help unlock some testing scenarios (where probing
> sys_enter or sys_exit causes undesirable performance overheads):
>
>   a. unprivileged triggering of a test module: test modules may register a
>      probe to be called back on task_prctl_unknown, and pick a very large
>      unknown prctl() option upon which they perform a test function for an
>      unprivileged user;
>
>   b. unprivileged triggering of an eBPF program function: similar
>      as idea (a).
>
> Example trace_pipe output:
>
>   test-380     [001] .....    78.142904: task_prctl_unknown: option=1234 arg2=101 arg3=102 arg4=103 arg5=104

For what it's worth:

> Signed-off-by: Marco Elver <elver@google.com>
Reviewed-by: Alexander Potapenko <glider@google.com>

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

* Re: [PATCH v3 1/2] tracing: Add task_prctl_unknown tracepoint
  2024-11-08 11:34 [PATCH v3 1/2] tracing: Add task_prctl_unknown tracepoint Marco Elver
                   ` (2 preceding siblings ...)
  2024-12-12 10:05 ` Alexander Potapenko
@ 2024-12-17  0:38 ` Kees Cook
  3 siblings, 0 replies; 8+ messages in thread
From: Kees Cook @ 2024-12-17  0:38 UTC (permalink / raw)
  To: Steven Rostedt, Kees Cook, Marco Elver
  Cc: Masami Hiramatsu, Mathieu Desnoyers, Andrew Morton, Oleg Nesterov,
	linux-kernel, linux-trace-kernel, Dmitry Vyukov, kasan-dev

On Fri, 08 Nov 2024 12:34:24 +0100, Marco Elver wrote:
> prctl() is a complex syscall which multiplexes its functionality based
> on a large set of PR_* options. Currently we count 64 such options. The
> return value of unknown options is -EINVAL, and doesn't distinguish from
> known options that were passed invalid args that also return -EINVAL.
> 
> To understand if programs are attempting to use prctl() options not yet
> available on the running kernel, provide the task_prctl_unknown
> tracepoint.
> 
> [...]

Applied to for-next/hardening, thanks!

[1/2] tracing: Add task_prctl_unknown tracepoint
      https://git.kernel.org/kees/c/57a6baf3a3ea
[2/2] tracing: Remove pid in task_rename tracing output
      https://git.kernel.org/kees/c/a6115cceb1dd

Take care,

-- 
Kees Cook


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

end of thread, other threads:[~2024-12-17  0:38 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-11-08 11:34 [PATCH v3 1/2] tracing: Add task_prctl_unknown tracepoint Marco Elver
2024-11-08 11:34 ` [PATCH v3 2/2] tracing: Remove pid in task_rename tracing output Marco Elver
2024-11-15 12:00 ` [PATCH v3 1/2] tracing: Add task_prctl_unknown tracepoint Marco Elver
2024-11-15 13:27   ` Steven Rostedt
2024-11-15 15:06     ` Marco Elver
2024-12-11 14:09       ` Marco Elver
2024-12-12 10:05 ` Alexander Potapenko
2024-12-17  0:38 ` Kees Cook

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).