[2/5] perf/hw_breakpoint: Modify breakpoint even if the new attr has disabled set
diff mbox series

Message ID 20180810104730.3293-3-jolsa@kernel.org
State New
Headers show
Series
  • perf/hw_breakpoint: Fix breakpoint modify
Related show

Commit Message

Jiri Olsa Aug. 10, 2018, 10:47 a.m. UTC
We need to change the breakpoint even if the attr with
new fields has disabled set to true.

Current code prevents following user code to change
the breakpoint address:

  ptrace(PTRACE_POKEUSER, child, offsetof(struct user, u_debugreg[0]), addr_1)
  ptrace(PTRACE_POKEUSER, child, offsetof(struct user, u_debugreg[0]), addr_2)
  ptrace(PTRACE_POKEUSER, child, offsetof(struct user, u_debugreg[7]), dr7)

The first PTRACE_POKEUSER creates the breakpoint with
attr.disabled set to true:

  ptrace_set_breakpoint_addr(nr = 0)
    struct perf_event *bp = t->ptrace_bps[nr];

    ptrace_register_breakpoint(..., disabled = true)
      ptrace_fill_bp_fields(..., disabled)
      register_user_hw_breakpoint

So the second PTRACE_POKEUSER will be omitted:

  ptrace_set_breakpoint_addr(nr = 0)
    struct perf_event *bp = t->ptrace_bps[nr];
    struct perf_event_attr attr = bp->attr;

    modify_user_hw_breakpoint(bp, &attr)
      if (!attr->disabled)
        modify_user_hw_breakpoint_check

Acked-by: Oleg Nesterov <oleg@redhat.com>
Reported-by: Milind Chabbi <chabbi.milind@gmail.com>
Link: http://lkml.kernel.org/n/tip-yjhgplc28gk5gfzt7ceooe6z@git.kernel.org
Signed-off-by: Jiri Olsa <jolsa@kernel.org>
---
 kernel/events/hw_breakpoint.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

Comments

Frederic Weisbecker Aug. 17, 2018, 2:27 p.m. UTC | #1
On Fri, Aug 10, 2018 at 12:47:27PM +0200, Jiri Olsa wrote:
> We need to change the breakpoint even if the attr with
> new fields has disabled set to true.
> 
> Current code prevents following user code to change
> the breakpoint address:
> 
>   ptrace(PTRACE_POKEUSER, child, offsetof(struct user, u_debugreg[0]), addr_1)
>   ptrace(PTRACE_POKEUSER, child, offsetof(struct user, u_debugreg[0]), addr_2)
>   ptrace(PTRACE_POKEUSER, child, offsetof(struct user, u_debugreg[7]), dr7)
> 
> The first PTRACE_POKEUSER creates the breakpoint with
> attr.disabled set to true:
> 
>   ptrace_set_breakpoint_addr(nr = 0)
>     struct perf_event *bp = t->ptrace_bps[nr];
> 
>     ptrace_register_breakpoint(..., disabled = true)
>       ptrace_fill_bp_fields(..., disabled)
>       register_user_hw_breakpoint
> 
> So the second PTRACE_POKEUSER will be omitted:
> 
>   ptrace_set_breakpoint_addr(nr = 0)
>     struct perf_event *bp = t->ptrace_bps[nr];
>     struct perf_event_attr attr = bp->attr;
> 
>     modify_user_hw_breakpoint(bp, &attr)
>       if (!attr->disabled)
>         modify_user_hw_breakpoint_check
> 
> Acked-by: Oleg Nesterov <oleg@redhat.com>
> Reported-by: Milind Chabbi <chabbi.milind@gmail.com>
> Link: http://lkml.kernel.org/n/tip-yjhgplc28gk5gfzt7ceooe6z@git.kernel.org
> Signed-off-by: Jiri Olsa <jolsa@kernel.org>

Right, now that we fixed the breakpoint modification arch code, it should
be safe to do that.

Thanks!

Acked-by: Frederic Weisbecker <frederic@kernel.org>

Patch
diff mbox series

diff --git a/kernel/events/hw_breakpoint.c b/kernel/events/hw_breakpoint.c
index b3814fce5ecb..fb229d9c7f3c 100644
--- a/kernel/events/hw_breakpoint.c
+++ b/kernel/events/hw_breakpoint.c
@@ -509,6 +509,8 @@  modify_user_hw_breakpoint_check(struct perf_event *bp, struct perf_event_attr *a
  */
 int modify_user_hw_breakpoint(struct perf_event *bp, struct perf_event_attr *attr)
 {
+	int err;
+
 	/*
 	 * modify_user_hw_breakpoint can be invoked with IRQs disabled and hence it
 	 * will not be possible to raise IPIs that invoke __perf_event_disable.
@@ -520,11 +522,11 @@  int modify_user_hw_breakpoint(struct perf_event *bp, struct perf_event_attr *att
 	else
 		perf_event_disable(bp);
 
-	if (!attr->disabled) {
-		int err = modify_user_hw_breakpoint_check(bp, attr, false);
+	err = modify_user_hw_breakpoint_check(bp, attr, false);
+	if (err)
+		return err;
 
-		if (err)
-			return err;
+	if (!attr->disabled) {
 		perf_event_enable(bp);
 		bp->attr.disabled = 0;
 	}