linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] x86 bpf: Fix extable offset calculation
       [not found] <CAADnVQJux+8n-vpuK9FqTLuj4cXrp04pGkpvKaUdAPXLQ4c-PQ@mail.gmail.com>
@ 2021-06-22 11:00 ` Ravi Bangoria
  2021-06-25  4:01   ` Alexei Starovoitov
  0 siblings, 1 reply; 4+ messages in thread
From: Ravi Bangoria @ 2021-06-22 11:00 UTC (permalink / raw)
  To: ast
  Cc: ravi.bangoria, davem, yoshfuji, dsahern, daniel, tglx, mingo, bp,
	x86, hpa, andrii, kafai, songliubraving, yhs, john.fastabend,
	kpsingh, netdev, bpf, linux-kernel, naveen.n.rao

commit 4c5de127598e1 ("bpf: Emit explicit NULL pointer checks
for PROBE_LDX instructions.") is emitting couple of instructions
before actual load. Consider those additional instructions while
calculating extable offset.

Fixes: 4c5de127598e1 ("bpf: Emit explicit NULL pointer checks for PROBE_LDX instructions.")
Signed-off-by: Ravi Bangoria <ravi.bangoria@linux.ibm.com>
---
 arch/x86/net/bpf_jit_comp.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/x86/net/bpf_jit_comp.c b/arch/x86/net/bpf_jit_comp.c
index 2a2e290fa5d8..231a8178cc11 100644
--- a/arch/x86/net/bpf_jit_comp.c
+++ b/arch/x86/net/bpf_jit_comp.c
@@ -1297,7 +1297,7 @@ st:			if (is_imm8(insn->off))
 			emit_ldx(&prog, BPF_SIZE(insn->code), dst_reg, src_reg, insn->off);
 			if (BPF_MODE(insn->code) == BPF_PROBE_MEM) {
 				struct exception_table_entry *ex;
-				u8 *_insn = image + proglen;
+				u8 *_insn = image + proglen + (u8)(start_of_ldx - temp);
 				s64 delta;
 
 				/* populate jmp_offset for JMP above */
-- 
2.30.2


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

* Re: [PATCH] x86 bpf: Fix extable offset calculation
  2021-06-22 11:00 ` [PATCH] x86 bpf: Fix extable offset calculation Ravi Bangoria
@ 2021-06-25  4:01   ` Alexei Starovoitov
  2021-06-25  6:22     ` Ravi Bangoria
  0 siblings, 1 reply; 4+ messages in thread
From: Alexei Starovoitov @ 2021-06-25  4:01 UTC (permalink / raw)
  To: Ravi Bangoria
  Cc: Alexei Starovoitov, David S. Miller, Hideaki YOSHIFUJI,
	David Ahern, Daniel Borkmann, Thomas Gleixner, Ingo Molnar,
	Borislav Petkov, X86 ML, H. Peter Anvin, Andrii Nakryiko,
	Martin KaFai Lau, Song Liu, Yonghong Song, John Fastabend,
	KP Singh, Network Development, bpf, LKML, Naveen N. Rao

On Tue, Jun 22, 2021 at 4:01 AM Ravi Bangoria
<ravi.bangoria@linux.ibm.com> wrote:
>
> commit 4c5de127598e1 ("bpf: Emit explicit NULL pointer checks
> for PROBE_LDX instructions.") is emitting couple of instructions
> before actual load. Consider those additional instructions while
> calculating extable offset.
>
> Fixes: 4c5de127598e1 ("bpf: Emit explicit NULL pointer checks for PROBE_LDX instructions.")
> Signed-off-by: Ravi Bangoria <ravi.bangoria@linux.ibm.com>
> ---
>  arch/x86/net/bpf_jit_comp.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/arch/x86/net/bpf_jit_comp.c b/arch/x86/net/bpf_jit_comp.c
> index 2a2e290fa5d8..231a8178cc11 100644
> --- a/arch/x86/net/bpf_jit_comp.c
> +++ b/arch/x86/net/bpf_jit_comp.c
> @@ -1297,7 +1297,7 @@ st:                       if (is_imm8(insn->off))
>                         emit_ldx(&prog, BPF_SIZE(insn->code), dst_reg, src_reg, insn->off);
>                         if (BPF_MODE(insn->code) == BPF_PROBE_MEM) {
>                                 struct exception_table_entry *ex;
> -                               u8 *_insn = image + proglen;
> +                               u8 *_insn = image + proglen + (u8)(start_of_ldx - temp);

Great debugging and the fix. Thanks a lot.
I've dropped (u8) cast, kept (), and applied to bpf tree.
I think it looks cleaner without that cast.
Could you send a followup patch with a selftest, so I don't make
the same mistake again ? ;)

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

* Re: [PATCH] x86 bpf: Fix extable offset calculation
  2021-06-25  4:01   ` Alexei Starovoitov
@ 2021-06-25  6:22     ` Ravi Bangoria
  2021-06-25 15:07       ` Alexei Starovoitov
  0 siblings, 1 reply; 4+ messages in thread
From: Ravi Bangoria @ 2021-06-25  6:22 UTC (permalink / raw)
  To: Alexei Starovoitov
  Cc: Alexei Starovoitov, David S. Miller, Hideaki YOSHIFUJI,
	David Ahern, Daniel Borkmann, Thomas Gleixner, Ingo Molnar,
	Borislav Petkov, X86 ML, H. Peter Anvin, Andrii Nakryiko,
	Martin KaFai Lau, Song Liu, Yonghong Song, John Fastabend,
	KP Singh, Network Development, bpf, LKML, Naveen N. Rao,
	Ravi Bangoria



On 6/25/21 9:31 AM, Alexei Starovoitov wrote:
> On Tue, Jun 22, 2021 at 4:01 AM Ravi Bangoria
> <ravi.bangoria@linux.ibm.com> wrote:
>>
>> commit 4c5de127598e1 ("bpf: Emit explicit NULL pointer checks
>> for PROBE_LDX instructions.") is emitting couple of instructions
>> before actual load. Consider those additional instructions while
>> calculating extable offset.
>>
>> Fixes: 4c5de127598e1 ("bpf: Emit explicit NULL pointer checks for PROBE_LDX instructions.")
>> Signed-off-by: Ravi Bangoria <ravi.bangoria@linux.ibm.com>
>> ---
>>   arch/x86/net/bpf_jit_comp.c | 2 +-
>>   1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/arch/x86/net/bpf_jit_comp.c b/arch/x86/net/bpf_jit_comp.c
>> index 2a2e290fa5d8..231a8178cc11 100644
>> --- a/arch/x86/net/bpf_jit_comp.c
>> +++ b/arch/x86/net/bpf_jit_comp.c
>> @@ -1297,7 +1297,7 @@ st:                       if (is_imm8(insn->off))
>>                          emit_ldx(&prog, BPF_SIZE(insn->code), dst_reg, src_reg, insn->off);
>>                          if (BPF_MODE(insn->code) == BPF_PROBE_MEM) {
>>                                  struct exception_table_entry *ex;
>> -                               u8 *_insn = image + proglen;
>> +                               u8 *_insn = image + proglen + (u8)(start_of_ldx - temp);
> 
> Great debugging and the fix. Thanks a lot.
> I've dropped (u8) cast, kept (), and applied to bpf tree.
> I think it looks cleaner without that cast.

Thanks.

> Could you send a followup patch with a selftest, so I don't make
> the same mistake again ? ;)

Unfortunately extable gets involved only for bad kernel pointers and
ideally there should not be any bad pointer in kernel. So there is no
easy way to create a proper selftest for this.

Ravi

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

* Re: [PATCH] x86 bpf: Fix extable offset calculation
  2021-06-25  6:22     ` Ravi Bangoria
@ 2021-06-25 15:07       ` Alexei Starovoitov
  0 siblings, 0 replies; 4+ messages in thread
From: Alexei Starovoitov @ 2021-06-25 15:07 UTC (permalink / raw)
  To: Ravi Bangoria
  Cc: Alexei Starovoitov, David S. Miller, Hideaki YOSHIFUJI,
	David Ahern, Daniel Borkmann, Thomas Gleixner, Ingo Molnar,
	Borislav Petkov, X86 ML, H. Peter Anvin, Andrii Nakryiko,
	Martin KaFai Lau, Song Liu, Yonghong Song, John Fastabend,
	KP Singh, Network Development, bpf, LKML, Naveen N. Rao

On Thu, Jun 24, 2021 at 11:22 PM Ravi Bangoria
<ravi.bangoria@linux.ibm.com> wrote:
> > Could you send a followup patch with a selftest, so I don't make
> > the same mistake again ? ;)
>
> Unfortunately extable gets involved only for bad kernel pointers and
> ideally there should not be any bad pointer in kernel. So there is no
> easy way to create a proper selftest for this.

Right. We have lib/test_bpf.c kernel module for such cases.

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

end of thread, other threads:[~2021-06-25 15:07 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <CAADnVQJux+8n-vpuK9FqTLuj4cXrp04pGkpvKaUdAPXLQ4c-PQ@mail.gmail.com>
2021-06-22 11:00 ` [PATCH] x86 bpf: Fix extable offset calculation Ravi Bangoria
2021-06-25  4:01   ` Alexei Starovoitov
2021-06-25  6:22     ` Ravi Bangoria
2021-06-25 15:07       ` Alexei Starovoitov

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