linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] tracing/kprobes: fix NULL pointer dereference in trace_kprobe_create()
@ 2019-01-11  6:01 Andrea Righi
  2019-01-11 13:45 ` Masami Hiramatsu
  2019-01-15 16:33 ` Steven Rostedt
  0 siblings, 2 replies; 4+ messages in thread
From: Andrea Righi @ 2019-01-11  6:01 UTC (permalink / raw)
  To: Steven Rostedt, Masami Hiramatsu; +Cc: Ingo Molnar, linux-kernel, stable

It is possible to trigger a NULL pointer dereference by writing an
incorrectly formatted string to krpobe_events (trying to create a
kretprobe omitting the symbol).

Example:

 echo "r:event_1 " >> /sys/kernel/debug/tracing/kprobe_events

That triggers this:

 BUG: unable to handle kernel NULL pointer dereference at 0000000000000000
 #PF error: [normal kernel read fault]
 PGD 0 P4D 0
 Oops: 0000 [#1] SMP PTI
 CPU: 6 PID: 1757 Comm: bash Not tainted 5.0.0-rc1+ #125
 Hardware name: Dell Inc. XPS 13 9370/0F6P3V, BIOS 1.5.1 08/09/2018
 RIP: 0010:kstrtoull+0x2/0x20
 Code: 28 00 00 00 75 17 48 83 c4 18 5b 41 5c 5d c3 b8 ea ff ff ff eb e1 b8 de ff ff ff eb da e8 d6 36 bb ff 66 0f 1f 44 00 00 31 c0 <80> 3f 2b 55 48 89 e5 0f 94 c0 48 01 c7 e8 5c ff ff ff 5d c3 66 2e
 RSP: 0018:ffffb5d482e57cb8 EFLAGS: 00010246
 RAX: 0000000000000000 RBX: 0000000000000001 RCX: ffffffff82b12720
 RDX: ffffb5d482e57cf8 RSI: 0000000000000000 RDI: 0000000000000000
 RBP: ffffb5d482e57d70 R08: ffffa0c05e5a7080 R09: ffffa0c05e003980
 R10: 0000000000000000 R11: 0000000040000000 R12: ffffa0c04fe87b08
 R13: 0000000000000001 R14: 000000000000000b R15: ffffa0c058d749e1
 FS:  00007f137c7f7740(0000) GS:ffffa0c05e580000(0000) knlGS:0000000000000000
 CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
 CR2: 0000000000000000 CR3: 0000000497d46004 CR4: 00000000003606e0
 Call Trace:
  ? trace_kprobe_create+0xb6/0x840
  ? _cond_resched+0x19/0x40
  ? _cond_resched+0x19/0x40
  ? __kmalloc+0x62/0x210
  ? argv_split+0x8f/0x140
  ? trace_kprobe_create+0x840/0x840
  ? trace_kprobe_create+0x840/0x840
  create_or_delete_trace_kprobe+0x11/0x30
  trace_run_command+0x50/0x90
  trace_parse_run_command+0xc1/0x160
  probes_write+0x10/0x20
  __vfs_write+0x3a/0x1b0
  ? apparmor_file_permission+0x1a/0x20
  ? security_file_permission+0x31/0xf0
  ? _cond_resched+0x19/0x40
  vfs_write+0xb1/0x1a0
  ksys_write+0x55/0xc0
  __x64_sys_write+0x1a/0x20
  do_syscall_64+0x5a/0x120
  entry_SYSCALL_64_after_hwframe+0x44/0xa9

Fix by doing the proper argument checks in trace_kprobe_create().

Link: https://lore.kernel.org/lkml/20190111095108.b79a2ee026185cbd62365977@kernel.org
Fixes: 6212dd29683e ("tracing/kprobes: Use dyn_event framework for kprobe events")
Cc: stable@vger.kernel.org
Signed-off-by: Andrea Righi <righi.andrea@gmail.com>
Signed-off-by: Masami Hiramatsu <mhiramat@kernel.org>
---
 v2: argument check refactoring

 kernel/trace/trace_kprobe.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/kernel/trace/trace_kprobe.c b/kernel/trace/trace_kprobe.c
index 5c19b8c41c7e..d5fb09ebba8b 100644
--- a/kernel/trace/trace_kprobe.c
+++ b/kernel/trace/trace_kprobe.c
@@ -607,11 +607,17 @@ static int trace_kprobe_create(int argc, const char *argv[])
 	char buf[MAX_EVENT_NAME_LEN];
 	unsigned int flags = TPARG_FL_KERNEL;
 
-	/* argc must be >= 1 */
-	if (argv[0][0] == 'r') {
+	switch (argv[0][0]) {
+	case 'r':
 		is_return = true;
 		flags |= TPARG_FL_RETURN;
-	} else if (argv[0][0] != 'p' || argc < 2)
+		break;
+	case 'p':
+		break;
+	default:
+		return -ECANCELED;
+	}
+	if (argc < 2)
 		return -ECANCELED;
 
 	event = strchr(&argv[0][1], ':');
-- 
2.17.1


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

* Re: [PATCH v2] tracing/kprobes: fix NULL pointer dereference in trace_kprobe_create()
  2019-01-11  6:01 [PATCH v2] tracing/kprobes: fix NULL pointer dereference in trace_kprobe_create() Andrea Righi
@ 2019-01-11 13:45 ` Masami Hiramatsu
  2019-01-14 15:42   ` Steven Rostedt
  2019-01-15 16:33 ` Steven Rostedt
  1 sibling, 1 reply; 4+ messages in thread
From: Masami Hiramatsu @ 2019-01-11 13:45 UTC (permalink / raw)
  To: Andrea Righi; +Cc: Steven Rostedt, Ingo Molnar, linux-kernel, stable

On Fri, 11 Jan 2019 07:01:13 +0100
Andrea Righi <righi.andrea@gmail.com> wrote:

> It is possible to trigger a NULL pointer dereference by writing an
> incorrectly formatted string to krpobe_events (trying to create a
> kretprobe omitting the symbol).
> 
> Example:
> 
>  echo "r:event_1 " >> /sys/kernel/debug/tracing/kprobe_events
> 
> That triggers this:
> 
>  BUG: unable to handle kernel NULL pointer dereference at 0000000000000000
>  #PF error: [normal kernel read fault]
>  PGD 0 P4D 0
>  Oops: 0000 [#1] SMP PTI
>  CPU: 6 PID: 1757 Comm: bash Not tainted 5.0.0-rc1+ #125
>  Hardware name: Dell Inc. XPS 13 9370/0F6P3V, BIOS 1.5.1 08/09/2018
>  RIP: 0010:kstrtoull+0x2/0x20
>  Code: 28 00 00 00 75 17 48 83 c4 18 5b 41 5c 5d c3 b8 ea ff ff ff eb e1 b8 de ff ff ff eb da e8 d6 36 bb ff 66 0f 1f 44 00 00 31 c0 <80> 3f 2b 55 48 89 e5 0f 94 c0 48 01 c7 e8 5c ff ff ff 5d c3 66 2e
>  RSP: 0018:ffffb5d482e57cb8 EFLAGS: 00010246
>  RAX: 0000000000000000 RBX: 0000000000000001 RCX: ffffffff82b12720
>  RDX: ffffb5d482e57cf8 RSI: 0000000000000000 RDI: 0000000000000000
>  RBP: ffffb5d482e57d70 R08: ffffa0c05e5a7080 R09: ffffa0c05e003980
>  R10: 0000000000000000 R11: 0000000040000000 R12: ffffa0c04fe87b08
>  R13: 0000000000000001 R14: 000000000000000b R15: ffffa0c058d749e1
>  FS:  00007f137c7f7740(0000) GS:ffffa0c05e580000(0000) knlGS:0000000000000000
>  CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
>  CR2: 0000000000000000 CR3: 0000000497d46004 CR4: 00000000003606e0
>  Call Trace:
>   ? trace_kprobe_create+0xb6/0x840
>   ? _cond_resched+0x19/0x40
>   ? _cond_resched+0x19/0x40
>   ? __kmalloc+0x62/0x210
>   ? argv_split+0x8f/0x140
>   ? trace_kprobe_create+0x840/0x840
>   ? trace_kprobe_create+0x840/0x840
>   create_or_delete_trace_kprobe+0x11/0x30
>   trace_run_command+0x50/0x90
>   trace_parse_run_command+0xc1/0x160
>   probes_write+0x10/0x20
>   __vfs_write+0x3a/0x1b0
>   ? apparmor_file_permission+0x1a/0x20
>   ? security_file_permission+0x31/0xf0
>   ? _cond_resched+0x19/0x40
>   vfs_write+0xb1/0x1a0
>   ksys_write+0x55/0xc0
>   __x64_sys_write+0x1a/0x20
>   do_syscall_64+0x5a/0x120
>   entry_SYSCALL_64_after_hwframe+0x44/0xa9
> 
> Fix by doing the proper argument checks in trace_kprobe_create().

Thank you Andrea!

Acked-by: Masami Hiramatsu <mhiramat@kernel.org>

> 
> Link: https://lore.kernel.org/lkml/20190111095108.b79a2ee026185cbd62365977@kernel.org
> Fixes: 6212dd29683e ("tracing/kprobes: Use dyn_event framework for kprobe events")
> Cc: stable@vger.kernel.org
> Signed-off-by: Andrea Righi <righi.andrea@gmail.com>
> Signed-off-by: Masami Hiramatsu <mhiramat@kernel.org>
> ---
>  v2: argument check refactoring
> 
>  kernel/trace/trace_kprobe.c | 12 +++++++++---
>  1 file changed, 9 insertions(+), 3 deletions(-)
> 
> diff --git a/kernel/trace/trace_kprobe.c b/kernel/trace/trace_kprobe.c
> index 5c19b8c41c7e..d5fb09ebba8b 100644
> --- a/kernel/trace/trace_kprobe.c
> +++ b/kernel/trace/trace_kprobe.c
> @@ -607,11 +607,17 @@ static int trace_kprobe_create(int argc, const char *argv[])
>  	char buf[MAX_EVENT_NAME_LEN];
>  	unsigned int flags = TPARG_FL_KERNEL;
>  
> -	/* argc must be >= 1 */
> -	if (argv[0][0] == 'r') {
> +	switch (argv[0][0]) {
> +	case 'r':
>  		is_return = true;
>  		flags |= TPARG_FL_RETURN;
> -	} else if (argv[0][0] != 'p' || argc < 2)
> +		break;
> +	case 'p':
> +		break;
> +	default:
> +		return -ECANCELED;
> +	}
> +	if (argc < 2)
>  		return -ECANCELED;
>  
>  	event = strchr(&argv[0][1], ':');
> -- 
> 2.17.1
> 


-- 
Masami Hiramatsu <mhiramat@kernel.org>

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

* Re: [PATCH v2] tracing/kprobes: fix NULL pointer dereference in trace_kprobe_create()
  2019-01-11 13:45 ` Masami Hiramatsu
@ 2019-01-14 15:42   ` Steven Rostedt
  0 siblings, 0 replies; 4+ messages in thread
From: Steven Rostedt @ 2019-01-14 15:42 UTC (permalink / raw)
  To: Masami Hiramatsu; +Cc: Andrea Righi, Ingo Molnar, linux-kernel, stable

On Fri, 11 Jan 2019 22:45:59 +0900
Masami Hiramatsu <mhiramat@kernel.org> wrote:

> On Fri, 11 Jan 2019 07:01:13 +0100
> Andrea Righi <righi.andrea@gmail.com> wrote:
> 
> > It is possible to trigger a NULL pointer dereference by writing an
> > incorrectly formatted string to krpobe_events (trying to create a
> > kretprobe omitting the symbol).
> > 
> > Example:
> > 
> >  echo "r:event_1 " >> /sys/kernel/debug/tracing/kprobe_events
> > 
> > That triggers this:
> > 
> >  BUG: unable to handle kernel NULL pointer dereference at 0000000000000000
> >  #PF error: [normal kernel read fault]
> >  PGD 0 P4D 0
> >  Oops: 0000 [#1] SMP PTI
> >  CPU: 6 PID: 1757 Comm: bash Not tainted 5.0.0-rc1+ #125
> >  Hardware name: Dell Inc. XPS 13 9370/0F6P3V, BIOS 1.5.1 08/09/2018
> >  RIP: 0010:kstrtoull+0x2/0x20
> >  Code: 28 00 00 00 75 17 48 83 c4 18 5b 41 5c 5d c3 b8 ea ff ff ff eb e1 b8 de ff ff ff eb da e8 d6 36 bb ff 66 0f 1f 44 00 00 31 c0 <80> 3f 2b 55 48 89 e5 0f 94 c0 48 01 c7 e8 5c ff ff ff 5d c3 66 2e
> >  RSP: 0018:ffffb5d482e57cb8 EFLAGS: 00010246
> >  RAX: 0000000000000000 RBX: 0000000000000001 RCX: ffffffff82b12720
> >  RDX: ffffb5d482e57cf8 RSI: 0000000000000000 RDI: 0000000000000000
> >  RBP: ffffb5d482e57d70 R08: ffffa0c05e5a7080 R09: ffffa0c05e003980
> >  R10: 0000000000000000 R11: 0000000040000000 R12: ffffa0c04fe87b08
> >  R13: 0000000000000001 R14: 000000000000000b R15: ffffa0c058d749e1
> >  FS:  00007f137c7f7740(0000) GS:ffffa0c05e580000(0000) knlGS:0000000000000000
> >  CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
> >  CR2: 0000000000000000 CR3: 0000000497d46004 CR4: 00000000003606e0
> >  Call Trace:
> >   ? trace_kprobe_create+0xb6/0x840
> >   ? _cond_resched+0x19/0x40
> >   ? _cond_resched+0x19/0x40
> >   ? __kmalloc+0x62/0x210
> >   ? argv_split+0x8f/0x140
> >   ? trace_kprobe_create+0x840/0x840
> >   ? trace_kprobe_create+0x840/0x840
> >   create_or_delete_trace_kprobe+0x11/0x30
> >   trace_run_command+0x50/0x90
> >   trace_parse_run_command+0xc1/0x160
> >   probes_write+0x10/0x20
> >   __vfs_write+0x3a/0x1b0
> >   ? apparmor_file_permission+0x1a/0x20
> >   ? security_file_permission+0x31/0xf0
> >   ? _cond_resched+0x19/0x40
> >   vfs_write+0xb1/0x1a0
> >   ksys_write+0x55/0xc0
> >   __x64_sys_write+0x1a/0x20
> >   do_syscall_64+0x5a/0x120
> >   entry_SYSCALL_64_after_hwframe+0x44/0xa9
> > 
> > Fix by doing the proper argument checks in trace_kprobe_create().  
> 
> Thank you Andrea!
> 
> Acked-by: Masami Hiramatsu <mhiramat@kernel.org>
> 

I've applied it and will be testing it now. Thanks!

-- Steve

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

* Re: [PATCH v2] tracing/kprobes: fix NULL pointer dereference in trace_kprobe_create()
  2019-01-11  6:01 [PATCH v2] tracing/kprobes: fix NULL pointer dereference in trace_kprobe_create() Andrea Righi
  2019-01-11 13:45 ` Masami Hiramatsu
@ 2019-01-15 16:33 ` Steven Rostedt
  1 sibling, 0 replies; 4+ messages in thread
From: Steven Rostedt @ 2019-01-15 16:33 UTC (permalink / raw)
  To: Andrea Righi; +Cc: Masami Hiramatsu, Ingo Molnar, linux-kernel, stable

On Fri, 11 Jan 2019 07:01:13 +0100
Andrea Righi <righi.andrea@gmail.com> wrote:

> It is possible to trigger a NULL pointer dereference by writing an
> incorrectly formatted string to krpobe_events (trying to create a
> kretprobe omitting the symbol).
> 
> Example:
> 
>  echo "r:event_1 " >> /sys/kernel/debug/tracing/kprobe_events
> 
> That triggers this:
> 
>  BUG: unable to handle kernel NULL pointer dereference at 0000000000000000
>  #PF error: [normal kernel read fault]
>  PGD 0 P4D 0
>  Oops: 0000 [#1] SMP PTI
>  CPU: 6 PID: 1757 Comm: bash Not tainted 5.0.0-rc1+ #125
>  Hardware name: Dell Inc. XPS 13 9370/0F6P3V, BIOS 1.5.1 08/09/2018
>  RIP: 0010:kstrtoull+0x2/0x20
>  Code: 28 00 00 00 75 17 48 83 c4 18 5b 41 5c 5d c3 b8 ea ff ff ff eb e1 b8 de ff ff ff eb da e8 d6 36 bb ff 66 0f 1f 44 00 00 31 c0 <80> 3f 2b 55 48 89 e5 0f 94 c0 48 01 c7 e8 5c ff ff ff 5d c3 66 2e
>  RSP: 0018:ffffb5d482e57cb8 EFLAGS: 00010246
>  RAX: 0000000000000000 RBX: 0000000000000001 RCX: ffffffff82b12720
>  RDX: ffffb5d482e57cf8 RSI: 0000000000000000 RDI: 0000000000000000
>  RBP: ffffb5d482e57d70 R08: ffffa0c05e5a7080 R09: ffffa0c05e003980
>  R10: 0000000000000000 R11: 0000000040000000 R12: ffffa0c04fe87b08
>  R13: 0000000000000001 R14: 000000000000000b R15: ffffa0c058d749e1
>  FS:  00007f137c7f7740(0000) GS:ffffa0c05e580000(0000) knlGS:0000000000000000
>  CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
>  CR2: 0000000000000000 CR3: 0000000497d46004 CR4: 00000000003606e0
>  Call Trace:
>   ? trace_kprobe_create+0xb6/0x840
>   ? _cond_resched+0x19/0x40
>   ? _cond_resched+0x19/0x40
>   ? __kmalloc+0x62/0x210
>   ? argv_split+0x8f/0x140
>   ? trace_kprobe_create+0x840/0x840
>   ? trace_kprobe_create+0x840/0x840
>   create_or_delete_trace_kprobe+0x11/0x30
>   trace_run_command+0x50/0x90
>   trace_parse_run_command+0xc1/0x160
>   probes_write+0x10/0x20
>   __vfs_write+0x3a/0x1b0
>   ? apparmor_file_permission+0x1a/0x20
>   ? security_file_permission+0x31/0xf0
>   ? _cond_resched+0x19/0x40
>   vfs_write+0xb1/0x1a0
>   ksys_write+0x55/0xc0
>   __x64_sys_write+0x1a/0x20
>   do_syscall_64+0x5a/0x120
>   entry_SYSCALL_64_after_hwframe+0x44/0xa9
> 
> Fix by doing the proper argument checks in trace_kprobe_create().
> 
> Link: https://lore.kernel.org/lkml/20190111095108.b79a2ee026185cbd62365977@kernel.org
> Fixes: 6212dd29683e ("tracing/kprobes: Use dyn_event framework for kprobe events")
> Cc: stable@vger.kernel.org

Dropping the stable tag because it appears that the Fixes commit went
into 5.0-rc1, which there's no stable kernel for.

-- Steve

> Signed-off-by: Andrea Righi <righi.andrea@gmail.com>
> Signed-off-by: Masami Hiramatsu <mhiramat@kernel.org>
> ---

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

end of thread, other threads:[~2019-01-15 16:33 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-01-11  6:01 [PATCH v2] tracing/kprobes: fix NULL pointer dereference in trace_kprobe_create() Andrea Righi
2019-01-11 13:45 ` Masami Hiramatsu
2019-01-14 15:42   ` Steven Rostedt
2019-01-15 16:33 ` Steven Rostedt

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