All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3] kprobes: Forbid probing on trampoline and bpf prog
@ 2022-08-01  3:37 Chen Zhongjin
  2022-08-01 20:41 ` Jiri Olsa
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Chen Zhongjin @ 2022-08-01  3:37 UTC (permalink / raw)
  To: linux-kernel, bpf
  Cc: naveen.n.rao, anil.s.keshavamurthy, davem, mhiramat, peterz,
	mingo, ast, daniel, chenzhongjin

kernel_text_address returns ftrace_trampoline, kprobe_insn_slot
and bpf_text_address as kprobe legal address.

These text are removable and changeable without any notifier to
kprobes. Probing on them can trigger some unexpected behavior[1].

Considering that jump_label and static_call text are already be
forbiden to probe, kernel_text_address should be replaced with
core_kernel_text and is_module_text_address to check other text
which is unsafe to kprobe.

[1] https://lkml.org/lkml/2022/7/26/1148

Fixes: 5b485629ba0d ("kprobes, extable: Identify kprobes trampolines as kernel text area")
Fixes: 74451e66d516 ("bpf: make jited programs visible in traces")
Signed-off-by: Chen Zhongjin <chenzhongjin@huawei.com>
---
v2 -> v3:
Remove '-next' carelessly added in title.

v1 -> v2:
Check core_kernel_text and is_module_text_address rather than
only kprobe_insn.
Also fix title and commit message for this. See old patch at [1].
---
 kernel/kprobes.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/kernel/kprobes.c b/kernel/kprobes.c
index f214f8c088ed..80697e5e03e4 100644
--- a/kernel/kprobes.c
+++ b/kernel/kprobes.c
@@ -1560,7 +1560,8 @@ static int check_kprobe_address_safe(struct kprobe *p,
 	preempt_disable();
 
 	/* Ensure it is not in reserved area nor out of text */
-	if (!kernel_text_address((unsigned long) p->addr) ||
+	if (!(core_kernel_text((unsigned long) p->addr) ||
+	    is_module_text_address((unsigned long) p->addr)) ||
 	    within_kprobe_blacklist((unsigned long) p->addr) ||
 	    jump_label_text_reserved(p->addr, p->addr) ||
 	    static_call_text_reserved(p->addr, p->addr) ||
-- 
2.17.1


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

* Re: [PATCH v3] kprobes: Forbid probing on trampoline and bpf prog
  2022-08-01  3:37 [PATCH v3] kprobes: Forbid probing on trampoline and bpf prog Chen Zhongjin
@ 2022-08-01 20:41 ` Jiri Olsa
  2022-08-01 20:51   ` Steven Rostedt
  2022-08-01 23:28 ` Masami Hiramatsu
  2022-08-02  9:55 ` [tip: perf/urgent] kprobes: Forbid probing on trampoline and BPF code areas tip-bot2 for Chen Zhongjin
  2 siblings, 1 reply; 8+ messages in thread
From: Jiri Olsa @ 2022-08-01 20:41 UTC (permalink / raw)
  To: Chen Zhongjin
  Cc: linux-kernel, bpf, naveen.n.rao, anil.s.keshavamurthy, davem,
	mhiramat, peterz, mingo, ast, daniel, Steven Rostedt

On Mon, Aug 01, 2022 at 11:37:19AM +0800, Chen Zhongjin wrote:
> kernel_text_address returns ftrace_trampoline, kprobe_insn_slot
> and bpf_text_address as kprobe legal address.
> 
> These text are removable and changeable without any notifier to
> kprobes. Probing on them can trigger some unexpected behavior[1].
> 
> Considering that jump_label and static_call text are already be
> forbiden to probe, kernel_text_address should be replaced with
> core_kernel_text and is_module_text_address to check other text
> which is unsafe to kprobe.
> 
> [1] https://lkml.org/lkml/2022/7/26/1148
> 
> Fixes: 5b485629ba0d ("kprobes, extable: Identify kprobes trampolines as kernel text area")
> Fixes: 74451e66d516 ("bpf: make jited programs visible in traces")
> Signed-off-by: Chen Zhongjin <chenzhongjin@huawei.com>
> ---
> v2 -> v3:
> Remove '-next' carelessly added in title.

LGTM cc-ing Steven because it affects ftrace as well

jirka

> 
> v1 -> v2:
> Check core_kernel_text and is_module_text_address rather than
> only kprobe_insn.
> Also fix title and commit message for this. See old patch at [1].
> ---
>  kernel/kprobes.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/kernel/kprobes.c b/kernel/kprobes.c
> index f214f8c088ed..80697e5e03e4 100644
> --- a/kernel/kprobes.c
> +++ b/kernel/kprobes.c
> @@ -1560,7 +1560,8 @@ static int check_kprobe_address_safe(struct kprobe *p,
>  	preempt_disable();
>  
>  	/* Ensure it is not in reserved area nor out of text */
> -	if (!kernel_text_address((unsigned long) p->addr) ||
> +	if (!(core_kernel_text((unsigned long) p->addr) ||
> +	    is_module_text_address((unsigned long) p->addr)) ||
>  	    within_kprobe_blacklist((unsigned long) p->addr) ||
>  	    jump_label_text_reserved(p->addr, p->addr) ||
>  	    static_call_text_reserved(p->addr, p->addr) ||
> -- 
> 2.17.1
> 

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

* Re: [PATCH v3] kprobes: Forbid probing on trampoline and bpf prog
  2022-08-01 20:41 ` Jiri Olsa
@ 2022-08-01 20:51   ` Steven Rostedt
  2022-08-01 23:29     ` Masami Hiramatsu
  2022-08-02  9:06     ` Jiri Olsa
  0 siblings, 2 replies; 8+ messages in thread
From: Steven Rostedt @ 2022-08-01 20:51 UTC (permalink / raw)
  To: Jiri Olsa
  Cc: Chen Zhongjin, linux-kernel, bpf, naveen.n.rao,
	anil.s.keshavamurthy, davem, mhiramat, peterz, mingo, ast,
	daniel

On Mon, 1 Aug 2022 22:41:19 +0200
Jiri Olsa <olsajiri@gmail.com> wrote:

> LGTM cc-ing Steven because it affects ftrace as well

Thanks for the Cc, but I don't quite see how it affects ftrace.

Unless you are just saying how it can affect kprobe_events?

-- Steve


> 
> jirka
> 
> > 
> > v1 -> v2:
> > Check core_kernel_text and is_module_text_address rather than
> > only kprobe_insn.
> > Also fix title and commit message for this. See old patch at [1].
> > ---
> >  kernel/kprobes.c | 3 ++-
> >  1 file changed, 2 insertions(+), 1 deletion(-)
> > 
> > diff --git a/kernel/kprobes.c b/kernel/kprobes.c
> > index f214f8c088ed..80697e5e03e4 100644
> > --- a/kernel/kprobes.c
> > +++ b/kernel/kprobes.c
> > @@ -1560,7 +1560,8 @@ static int check_kprobe_address_safe(struct kprobe *p,
> >  	preempt_disable();
> >  
> >  	/* Ensure it is not in reserved area nor out of text */
> > -	if (!kernel_text_address((unsigned long) p->addr) ||
> > +	if (!(core_kernel_text((unsigned long) p->addr) ||
> > +	    is_module_text_address((unsigned long) p->addr)) ||
> >  	    within_kprobe_blacklist((unsigned long) p->addr) ||
> >  	    jump_label_text_reserved(p->addr, p->addr) ||
> >  	    static_call_text_reserved(p->addr, p->addr) ||
> > -- 
> > 2.17.1
> >   


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

* Re: [PATCH v3] kprobes: Forbid probing on trampoline and bpf prog
  2022-08-01  3:37 [PATCH v3] kprobes: Forbid probing on trampoline and bpf prog Chen Zhongjin
  2022-08-01 20:41 ` Jiri Olsa
@ 2022-08-01 23:28 ` Masami Hiramatsu
  2022-08-02  9:55 ` [tip: perf/urgent] kprobes: Forbid probing on trampoline and BPF code areas tip-bot2 for Chen Zhongjin
  2 siblings, 0 replies; 8+ messages in thread
From: Masami Hiramatsu @ 2022-08-01 23:28 UTC (permalink / raw)
  To: Chen Zhongjin
  Cc: linux-kernel, bpf, naveen.n.rao, anil.s.keshavamurthy, davem,
	mhiramat, peterz, mingo, ast, daniel

On Mon, 1 Aug 2022 11:37:19 +0800
Chen Zhongjin <chenzhongjin@huawei.com> wrote:

> kernel_text_address returns ftrace_trampoline, kprobe_insn_slot
> and bpf_text_address as kprobe legal address.
> 
> These text are removable and changeable without any notifier to
> kprobes. Probing on them can trigger some unexpected behavior[1].
> 
> Considering that jump_label and static_call text are already be
> forbiden to probe, kernel_text_address should be replaced with
> core_kernel_text and is_module_text_address to check other text
> which is unsafe to kprobe.
> 
> [1] https://lkml.org/lkml/2022/7/26/1148
> 
> Fixes: 5b485629ba0d ("kprobes, extable: Identify kprobes trampolines as kernel text area")
> Fixes: 74451e66d516 ("bpf: make jited programs visible in traces")
> Signed-off-by: Chen Zhongjin <chenzhongjin@huawei.com>

Thanks! this looks good to me.

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


> ---
> v2 -> v3:
> Remove '-next' carelessly added in title.
> 
> v1 -> v2:
> Check core_kernel_text and is_module_text_address rather than
> only kprobe_insn.
> Also fix title and commit message for this. See old patch at [1].
> ---
>  kernel/kprobes.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/kernel/kprobes.c b/kernel/kprobes.c
> index f214f8c088ed..80697e5e03e4 100644
> --- a/kernel/kprobes.c
> +++ b/kernel/kprobes.c
> @@ -1560,7 +1560,8 @@ static int check_kprobe_address_safe(struct kprobe *p,
>  	preempt_disable();
>  
>  	/* Ensure it is not in reserved area nor out of text */
> -	if (!kernel_text_address((unsigned long) p->addr) ||
> +	if (!(core_kernel_text((unsigned long) p->addr) ||
> +	    is_module_text_address((unsigned long) p->addr)) ||
>  	    within_kprobe_blacklist((unsigned long) p->addr) ||
>  	    jump_label_text_reserved(p->addr, p->addr) ||
>  	    static_call_text_reserved(p->addr, p->addr) ||
> -- 
> 2.17.1
> 


-- 
Masami Hiramatsu (Google) <mhiramat@kernel.org>

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

* Re: [PATCH v3] kprobes: Forbid probing on trampoline and bpf prog
  2022-08-01 20:51   ` Steven Rostedt
@ 2022-08-01 23:29     ` Masami Hiramatsu
  2022-08-02  9:06     ` Jiri Olsa
  1 sibling, 0 replies; 8+ messages in thread
From: Masami Hiramatsu @ 2022-08-01 23:29 UTC (permalink / raw)
  To: Steven Rostedt
  Cc: Jiri Olsa, Chen Zhongjin, linux-kernel, bpf, naveen.n.rao,
	anil.s.keshavamurthy, davem, mhiramat, peterz, mingo, ast,
	daniel

On Mon, 1 Aug 2022 16:51:46 -0400
Steven Rostedt <rostedt@goodmis.org> wrote:

> On Mon, 1 Aug 2022 22:41:19 +0200
> Jiri Olsa <olsajiri@gmail.com> wrote:
> 
> > LGTM cc-ing Steven because it affects ftrace as well
> 
> Thanks for the Cc, but I don't quite see how it affects ftrace.
> 
> Unless you are just saying how it can affect kprobe_events?

Maybe kprobe_events can probe the ftrace trampoline buffer if
CONFIG_KPROBE_EVENTS_ON_NOTRACE=y.

> 
> -- Steve
> 
> 
> > 
> > jirka
> > 
> > > 
> > > v1 -> v2:
> > > Check core_kernel_text and is_module_text_address rather than
> > > only kprobe_insn.
> > > Also fix title and commit message for this. See old patch at [1].
> > > ---
> > >  kernel/kprobes.c | 3 ++-
> > >  1 file changed, 2 insertions(+), 1 deletion(-)
> > > 
> > > diff --git a/kernel/kprobes.c b/kernel/kprobes.c
> > > index f214f8c088ed..80697e5e03e4 100644
> > > --- a/kernel/kprobes.c
> > > +++ b/kernel/kprobes.c
> > > @@ -1560,7 +1560,8 @@ static int check_kprobe_address_safe(struct kprobe *p,
> > >  	preempt_disable();
> > >  
> > >  	/* Ensure it is not in reserved area nor out of text */
> > > -	if (!kernel_text_address((unsigned long) p->addr) ||
> > > +	if (!(core_kernel_text((unsigned long) p->addr) ||
> > > +	    is_module_text_address((unsigned long) p->addr)) ||
> > >  	    within_kprobe_blacklist((unsigned long) p->addr) ||
> > >  	    jump_label_text_reserved(p->addr, p->addr) ||
> > >  	    static_call_text_reserved(p->addr, p->addr) ||
> > > -- 
> > > 2.17.1
> > >   
> 


-- 
Masami Hiramatsu (Google) <mhiramat@kernel.org>

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

* Re: [PATCH v3] kprobes: Forbid probing on trampoline and bpf prog
  2022-08-01 20:51   ` Steven Rostedt
  2022-08-01 23:29     ` Masami Hiramatsu
@ 2022-08-02  9:06     ` Jiri Olsa
  2022-08-02 12:28       ` Chen Zhongjin
  1 sibling, 1 reply; 8+ messages in thread
From: Jiri Olsa @ 2022-08-02  9:06 UTC (permalink / raw)
  To: Steven Rostedt
  Cc: Jiri Olsa, Chen Zhongjin, linux-kernel, bpf, naveen.n.rao,
	anil.s.keshavamurthy, davem, mhiramat, peterz, mingo, ast,
	daniel

On Mon, Aug 01, 2022 at 04:51:46PM -0400, Steven Rostedt wrote:
> On Mon, 1 Aug 2022 22:41:19 +0200
> Jiri Olsa <olsajiri@gmail.com> wrote:
> 
> > LGTM cc-ing Steven because it affects ftrace as well
> 
> Thanks for the Cc, but I don't quite see how it affects ftrace.
> 
> Unless you are just saying how it can affect kprobe_events?

nope, I just saw the 'ftrace' in changelog ;-)

anyway the patch makes check_kprobe_address_safe to fail
on ftrace trampoline address.. but not sure you could make
kprobe on ftrace trampoline before, probably not

jirka

> 
> -- Steve
> 
> 
> > 
> > jirka
> > 
> > > 
> > > v1 -> v2:
> > > Check core_kernel_text and is_module_text_address rather than
> > > only kprobe_insn.
> > > Also fix title and commit message for this. See old patch at [1].
> > > ---
> > >  kernel/kprobes.c | 3 ++-
> > >  1 file changed, 2 insertions(+), 1 deletion(-)
> > > 
> > > diff --git a/kernel/kprobes.c b/kernel/kprobes.c
> > > index f214f8c088ed..80697e5e03e4 100644
> > > --- a/kernel/kprobes.c
> > > +++ b/kernel/kprobes.c
> > > @@ -1560,7 +1560,8 @@ static int check_kprobe_address_safe(struct kprobe *p,
> > >  	preempt_disable();
> > >  
> > >  	/* Ensure it is not in reserved area nor out of text */
> > > -	if (!kernel_text_address((unsigned long) p->addr) ||
> > > +	if (!(core_kernel_text((unsigned long) p->addr) ||
> > > +	    is_module_text_address((unsigned long) p->addr)) ||
> > >  	    within_kprobe_blacklist((unsigned long) p->addr) ||
> > >  	    jump_label_text_reserved(p->addr, p->addr) ||
> > >  	    static_call_text_reserved(p->addr, p->addr) ||
> > > -- 
> > > 2.17.1
> > >   
> 

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

* [tip: perf/urgent] kprobes: Forbid probing on trampoline and BPF code areas
  2022-08-01  3:37 [PATCH v3] kprobes: Forbid probing on trampoline and bpf prog Chen Zhongjin
  2022-08-01 20:41 ` Jiri Olsa
  2022-08-01 23:28 ` Masami Hiramatsu
@ 2022-08-02  9:55 ` tip-bot2 for Chen Zhongjin
  2 siblings, 0 replies; 8+ messages in thread
From: tip-bot2 for Chen Zhongjin @ 2022-08-02  9:55 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: Chen Zhongjin, Ingo Molnar, Masami Hiramatsu (Google), x86, linux-kernel

The following commit has been merged into the perf/urgent branch of tip:

Commit-ID:     28f6c37a2910f565b4f5960df52b2eccae28c891
Gitweb:        https://git.kernel.org/tip/28f6c37a2910f565b4f5960df52b2eccae28c891
Author:        Chen Zhongjin <chenzhongjin@huawei.com>
AuthorDate:    Mon, 01 Aug 2022 11:37:19 +08:00
Committer:     Ingo Molnar <mingo@kernel.org>
CommitterDate: Tue, 02 Aug 2022 11:47:29 +02:00

kprobes: Forbid probing on trampoline and BPF code areas

kernel_text_address() treats ftrace_trampoline, kprobe_insn_slot
and bpf_text_address as valid kprobe addresses - which is not ideal.

These text areas are removable and changeable without any notification
to kprobes, and probing on them can trigger unexpected behavior:

  https://lkml.org/lkml/2022/7/26/1148

Considering that jump_label and static_call text are already
forbiden to probe, kernel_text_address() should be replaced with
core_kernel_text() and is_module_text_address() to check other text
areas which are unsafe to kprobe.

[ mingo: Rewrote the changelog. ]

Fixes: 5b485629ba0d ("kprobes, extable: Identify kprobes trampolines as kernel text area")
Fixes: 74451e66d516 ("bpf: make jited programs visible in traces")
Signed-off-by: Chen Zhongjin <chenzhongjin@huawei.com>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Acked-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
Link: https://lore.kernel.org/r/20220801033719.228248-1-chenzhongjin@huawei.com
---
 kernel/kprobes.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/kernel/kprobes.c b/kernel/kprobes.c
index f214f8c..80697e5 100644
--- a/kernel/kprobes.c
+++ b/kernel/kprobes.c
@@ -1560,7 +1560,8 @@ static int check_kprobe_address_safe(struct kprobe *p,
 	preempt_disable();
 
 	/* Ensure it is not in reserved area nor out of text */
-	if (!kernel_text_address((unsigned long) p->addr) ||
+	if (!(core_kernel_text((unsigned long) p->addr) ||
+	    is_module_text_address((unsigned long) p->addr)) ||
 	    within_kprobe_blacklist((unsigned long) p->addr) ||
 	    jump_label_text_reserved(p->addr, p->addr) ||
 	    static_call_text_reserved(p->addr, p->addr) ||

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

* Re: [PATCH v3] kprobes: Forbid probing on trampoline and bpf prog
  2022-08-02  9:06     ` Jiri Olsa
@ 2022-08-02 12:28       ` Chen Zhongjin
  0 siblings, 0 replies; 8+ messages in thread
From: Chen Zhongjin @ 2022-08-02 12:28 UTC (permalink / raw)
  To: Jiri Olsa, Steven Rostedt
  Cc: linux-kernel, bpf, naveen.n.rao, anil.s.keshavamurthy, davem,
	mhiramat, peterz, mingo, ast, daniel


On 2022/8/2 17:06, Jiri Olsa wrote:
> On Mon, Aug 01, 2022 at 04:51:46PM -0400, Steven Rostedt wrote:
>> On Mon, 1 Aug 2022 22:41:19 +0200
>> Jiri Olsa <olsajiri@gmail.com> wrote:
>>
>>> LGTM cc-ing Steven because it affects ftrace as well
>> Thanks for the Cc, but I don't quite see how it affects ftrace.
>>
>> Unless you are just saying how it can affect kprobe_events?
> nope, I just saw the 'ftrace' in changelog ;-)
>
> anyway the patch makes check_kprobe_address_safe to fail
> on ftrace trampoline address.. but not sure you could make
> kprobe on ftrace trampoline before, probably not
>
> jirka

In fact with CONFIG_KPROBE_EVENTS_ON_NOTRACE=y it can happen.

But I think ftrace has no responsibility to promise the address safety 
when this option open.


Best,

Chen

>> -- Steve
>>
>>
>>> jirka
>>>
>>>> v1 -> v2:
>>>> Check core_kernel_text and is_module_text_address rather than
>>>> only kprobe_insn.
>>>> Also fix title and commit message for this. See old patch at [1].
>>>> ---
>>>>   kernel/kprobes.c | 3 ++-
>>>>   1 file changed, 2 insertions(+), 1 deletion(-)
>>>>
>>>> diff --git a/kernel/kprobes.c b/kernel/kprobes.c
>>>> index f214f8c088ed..80697e5e03e4 100644
>>>> --- a/kernel/kprobes.c
>>>> +++ b/kernel/kprobes.c
>>>> @@ -1560,7 +1560,8 @@ static int check_kprobe_address_safe(struct kprobe *p,
>>>>   	preempt_disable();
>>>>   
>>>>   	/* Ensure it is not in reserved area nor out of text */
>>>> -	if (!kernel_text_address((unsigned long) p->addr) ||
>>>> +	if (!(core_kernel_text((unsigned long) p->addr) ||
>>>> +	    is_module_text_address((unsigned long) p->addr)) ||
>>>>   	    within_kprobe_blacklist((unsigned long) p->addr) ||
>>>>   	    jump_label_text_reserved(p->addr, p->addr) ||
>>>>   	    static_call_text_reserved(p->addr, p->addr) ||
>>>> -- 
>>>> 2.17.1
>>>>    


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

end of thread, other threads:[~2022-08-02 12:28 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-01  3:37 [PATCH v3] kprobes: Forbid probing on trampoline and bpf prog Chen Zhongjin
2022-08-01 20:41 ` Jiri Olsa
2022-08-01 20:51   ` Steven Rostedt
2022-08-01 23:29     ` Masami Hiramatsu
2022-08-02  9:06     ` Jiri Olsa
2022-08-02 12:28       ` Chen Zhongjin
2022-08-01 23:28 ` Masami Hiramatsu
2022-08-02  9:55 ` [tip: perf/urgent] kprobes: Forbid probing on trampoline and BPF code areas tip-bot2 for Chen Zhongjin

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.