BPF List
 help / color / mirror / Atom feed
* [PATCH bpf] bpf: Fix a verifier bug due to incorrect branch offset comparison with cpu=v4
@ 2023-11-29  7:54 Yonghong Song
  2023-11-29 19:52 ` Stanislav Fomichev
  2023-11-29 22:51 ` Daniel Borkmann
  0 siblings, 2 replies; 5+ messages in thread
From: Yonghong Song @ 2023-11-29  7:54 UTC (permalink / raw)
  To: bpf
  Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann, kernel-team,
	Martin KaFai Lau

Bpf cpu=v4 support is introduced in [1] and Commit 4cd58e9af8b9
("bpf: Support new 32bit offset jmp instruction") added support for new
32bit offset jmp instruction. Unfortunately, in function
bpf_adj_delta_to_off(), for new branch insn with 32bit offset, the offset
(plus/minor a small delta) compares to 16-bit offset bound
[S16_MIN, S16_MAX], which caused the following verification failure:
  $ ./test_progs-cpuv4 -t verif_scale_pyperf180
  ...
  insn 10 cannot be patched due to 16-bit range
  ...
  libbpf: failed to load object 'pyperf180.bpf.o'
  scale_test:FAIL:expect_success unexpected error: -12 (errno 12)
  #405     verif_scale_pyperf180:FAIL

Note that due to recent llvm18 development, the patch [2] (already applied
in bpf-next) needs to be applied to bpf tree for testing purpose.

The fix is rather simple. For 32bit offset branch insn, the adjusted
offset compares to [S32_MIN, S32_MAX] and then verification succeeded.

  [1] https://lore.kernel.org/all/20230728011143.3710005-1-yonghong.song@linux.dev
  [2] https://lore.kernel.org/bpf/20231110193644.3130906-1-yonghong.song@linux.dev

Fixes: 4cd58e9af8b9 ("bpf: Support new 32bit offset jmp instruction")
Signed-off-by: Yonghong Song <yonghong.song@linux.dev>
---
 kernel/bpf/core.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c
index cd3afe57ece3..74f2fd48148c 100644
--- a/kernel/bpf/core.c
+++ b/kernel/bpf/core.c
@@ -371,14 +371,17 @@ static int bpf_adj_delta_to_imm(struct bpf_insn *insn, u32 pos, s32 end_old,
 static int bpf_adj_delta_to_off(struct bpf_insn *insn, u32 pos, s32 end_old,
 				s32 end_new, s32 curr, const bool probe_pass)
 {
-	const s32 off_min = S16_MIN, off_max = S16_MAX;
+	s32 off_min = S16_MIN, off_max = S16_MAX;
 	s32 delta = end_new - end_old;
 	s32 off;
 
-	if (insn->code == (BPF_JMP32 | BPF_JA))
+	if (insn->code == (BPF_JMP32 | BPF_JA)) {
 		off = insn->imm;
-	else
+		off_min = S32_MIN;
+		off_max = S32_MAX;
+	} else {
 		off = insn->off;
+	}
 
 	if (curr < pos && curr + off + 1 >= end_old)
 		off += delta;
-- 
2.34.1


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

* Re: [PATCH bpf] bpf: Fix a verifier bug due to incorrect branch offset comparison with cpu=v4
  2023-11-29  7:54 [PATCH bpf] bpf: Fix a verifier bug due to incorrect branch offset comparison with cpu=v4 Yonghong Song
@ 2023-11-29 19:52 ` Stanislav Fomichev
  2023-11-29 22:51 ` Daniel Borkmann
  1 sibling, 0 replies; 5+ messages in thread
From: Stanislav Fomichev @ 2023-11-29 19:52 UTC (permalink / raw)
  To: Yonghong Song
  Cc: bpf, Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
	kernel-team, Martin KaFai Lau

On 11/28, Yonghong Song wrote:
> Bpf cpu=v4 support is introduced in [1] and Commit 4cd58e9af8b9
> ("bpf: Support new 32bit offset jmp instruction") added support for new
> 32bit offset jmp instruction. Unfortunately, in function
> bpf_adj_delta_to_off(), for new branch insn with 32bit offset, the offset
> (plus/minor a small delta) compares to 16-bit offset bound
> [S16_MIN, S16_MAX], which caused the following verification failure:
>   $ ./test_progs-cpuv4 -t verif_scale_pyperf180
>   ...
>   insn 10 cannot be patched due to 16-bit range
>   ...
>   libbpf: failed to load object 'pyperf180.bpf.o'
>   scale_test:FAIL:expect_success unexpected error: -12 (errno 12)
>   #405     verif_scale_pyperf180:FAIL
> 
> Note that due to recent llvm18 development, the patch [2] (already applied
> in bpf-next) needs to be applied to bpf tree for testing purpose.
> 
> The fix is rather simple. For 32bit offset branch insn, the adjusted
> offset compares to [S32_MIN, S32_MAX] and then verification succeeded.
> 
>   [1] https://lore.kernel.org/all/20230728011143.3710005-1-yonghong.song@linux.dev
>   [2] https://lore.kernel.org/bpf/20231110193644.3130906-1-yonghong.song@linux.dev
> 
> Fixes: 4cd58e9af8b9 ("bpf: Support new 32bit offset jmp instruction")
> Signed-off-by: Yonghong Song <yonghong.song@linux.dev>

Acked-by: Stanislav Fomichev <sdf@google.com>

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

* Re: [PATCH bpf] bpf: Fix a verifier bug due to incorrect branch offset comparison with cpu=v4
  2023-11-29  7:54 [PATCH bpf] bpf: Fix a verifier bug due to incorrect branch offset comparison with cpu=v4 Yonghong Song
  2023-11-29 19:52 ` Stanislav Fomichev
@ 2023-11-29 22:51 ` Daniel Borkmann
  2023-11-29 23:42   ` Yonghong Song
  1 sibling, 1 reply; 5+ messages in thread
From: Daniel Borkmann @ 2023-11-29 22:51 UTC (permalink / raw)
  To: Yonghong Song, bpf
  Cc: Alexei Starovoitov, Andrii Nakryiko, kernel-team,
	Martin KaFai Lau

On 11/29/23 8:54 AM, Yonghong Song wrote:
> Bpf cpu=v4 support is introduced in [1] and Commit 4cd58e9af8b9
> ("bpf: Support new 32bit offset jmp instruction") added support for new
> 32bit offset jmp instruction. Unfortunately, in function
> bpf_adj_delta_to_off(), for new branch insn with 32bit offset, the offset
> (plus/minor a small delta) compares to 16-bit offset bound
> [S16_MIN, S16_MAX], which caused the following verification failure:
>    $ ./test_progs-cpuv4 -t verif_scale_pyperf180
>    ...
>    insn 10 cannot be patched due to 16-bit range
>    ...
>    libbpf: failed to load object 'pyperf180.bpf.o'
>    scale_test:FAIL:expect_success unexpected error: -12 (errno 12)
>    #405     verif_scale_pyperf180:FAIL
> 
> Note that due to recent llvm18 development, the patch [2] (already applied
> in bpf-next) needs to be applied to bpf tree for testing purpose.
> 
> The fix is rather simple. For 32bit offset branch insn, the adjusted
> offset compares to [S32_MIN, S32_MAX] and then verification succeeded.
> 
>    [1] https://lore.kernel.org/all/20230728011143.3710005-1-yonghong.song@linux.dev
>    [2] https://lore.kernel.org/bpf/20231110193644.3130906-1-yonghong.song@linux.dev
> 
> Fixes: 4cd58e9af8b9 ("bpf: Support new 32bit offset jmp instruction")
> Signed-off-by: Yonghong Song <yonghong.song@linux.dev>
> ---
>   kernel/bpf/core.c | 9 ++++++---
>   1 file changed, 6 insertions(+), 3 deletions(-)
> 
> diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c
> index cd3afe57ece3..74f2fd48148c 100644
> --- a/kernel/bpf/core.c
> +++ b/kernel/bpf/core.c
> @@ -371,14 +371,17 @@ static int bpf_adj_delta_to_imm(struct bpf_insn *insn, u32 pos, s32 end_old,
>   static int bpf_adj_delta_to_off(struct bpf_insn *insn, u32 pos, s32 end_old,
>   				s32 end_new, s32 curr, const bool probe_pass)
>   {
> -	const s32 off_min = S16_MIN, off_max = S16_MAX;
> +	s32 off_min = S16_MIN, off_max = S16_MAX;
>   	s32 delta = end_new - end_old;
>   	s32 off;

These should all be converted to s64, no? E.g. further below
the test will never trigger then for jmp32:

        if (off < off_min || off > off_max)
                 return -ERANGE;

> -	if (insn->code == (BPF_JMP32 | BPF_JA))
> +	if (insn->code == (BPF_JMP32 | BPF_JA)) {
>   		off = insn->imm;
> -	else
> +		off_min = S32_MIN;
> +		off_max = S32_MAX;
> +	} else {
>   		off = insn->off;
> +	}
>   
>   	if (curr < pos && curr + off + 1 >= end_old)
>   		off += delta;
> 


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

* Re: [PATCH bpf] bpf: Fix a verifier bug due to incorrect branch offset comparison with cpu=v4
  2023-11-29 22:51 ` Daniel Borkmann
@ 2023-11-29 23:42   ` Yonghong Song
  2023-11-30  0:19     ` Yonghong Song
  0 siblings, 1 reply; 5+ messages in thread
From: Yonghong Song @ 2023-11-29 23:42 UTC (permalink / raw)
  To: Daniel Borkmann, bpf
  Cc: Alexei Starovoitov, Andrii Nakryiko, kernel-team,
	Martin KaFai Lau


On 11/29/23 5:51 PM, Daniel Borkmann wrote:
> On 11/29/23 8:54 AM, Yonghong Song wrote:
>> Bpf cpu=v4 support is introduced in [1] and Commit 4cd58e9af8b9
>> ("bpf: Support new 32bit offset jmp instruction") added support for new
>> 32bit offset jmp instruction. Unfortunately, in function
>> bpf_adj_delta_to_off(), for new branch insn with 32bit offset, the 
>> offset
>> (plus/minor a small delta) compares to 16-bit offset bound
>> [S16_MIN, S16_MAX], which caused the following verification failure:
>>    $ ./test_progs-cpuv4 -t verif_scale_pyperf180
>>    ...
>>    insn 10 cannot be patched due to 16-bit range
>>    ...
>>    libbpf: failed to load object 'pyperf180.bpf.o'
>>    scale_test:FAIL:expect_success unexpected error: -12 (errno 12)
>>    #405     verif_scale_pyperf180:FAIL
>>
>> Note that due to recent llvm18 development, the patch [2] (already 
>> applied
>> in bpf-next) needs to be applied to bpf tree for testing purpose.
>>
>> The fix is rather simple. For 32bit offset branch insn, the adjusted
>> offset compares to [S32_MIN, S32_MAX] and then verification succeeded.
>>
>>    [1] 
>> https://lore.kernel.org/all/20230728011143.3710005-1-yonghong.song@linux.dev
>>    [2] 
>> https://lore.kernel.org/bpf/20231110193644.3130906-1-yonghong.song@linux.dev
>>
>> Fixes: 4cd58e9af8b9 ("bpf: Support new 32bit offset jmp instruction")
>> Signed-off-by: Yonghong Song <yonghong.song@linux.dev>
>> ---
>>   kernel/bpf/core.c | 9 ++++++---
>>   1 file changed, 6 insertions(+), 3 deletions(-)
>>
>> diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c
>> index cd3afe57ece3..74f2fd48148c 100644
>> --- a/kernel/bpf/core.c
>> +++ b/kernel/bpf/core.c
>> @@ -371,14 +371,17 @@ static int bpf_adj_delta_to_imm(struct bpf_insn 
>> *insn, u32 pos, s32 end_old,
>>   static int bpf_adj_delta_to_off(struct bpf_insn *insn, u32 pos, s32 
>> end_old,
>>                   s32 end_new, s32 curr, const bool probe_pass)
>>   {
>> -    const s32 off_min = S16_MIN, off_max = S16_MAX;
>> +    s32 off_min = S16_MIN, off_max = S16_MAX;
>>       s32 delta = end_new - end_old;
>>       s32 off;
>
> These should all be converted to s64, no? E.g. further below
> the test will never trigger then for jmp32:
>
>        if (off < off_min || off > off_max)
>                 return -ERANGE;


good point! Let us use s64 for potential overflows.
Will send v2 soon.

>
>> -    if (insn->code == (BPF_JMP32 | BPF_JA))
>> +    if (insn->code == (BPF_JMP32 | BPF_JA)) {
>>           off = insn->imm;
>> -    else
>> +        off_min = S32_MIN;
>> +        off_max = S32_MAX;
>> +    } else {
>>           off = insn->off;
>> +    }
>>         if (curr < pos && curr + off + 1 >= end_old)
>>           off += delta;
>>
>

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

* Re: [PATCH bpf] bpf: Fix a verifier bug due to incorrect branch offset comparison with cpu=v4
  2023-11-29 23:42   ` Yonghong Song
@ 2023-11-30  0:19     ` Yonghong Song
  0 siblings, 0 replies; 5+ messages in thread
From: Yonghong Song @ 2023-11-30  0:19 UTC (permalink / raw)
  To: Daniel Borkmann, bpf
  Cc: Alexei Starovoitov, Andrii Nakryiko, kernel-team,
	Martin KaFai Lau


On 11/29/23 6:42 PM, Yonghong Song wrote:
>
> On 11/29/23 5:51 PM, Daniel Borkmann wrote:
>> On 11/29/23 8:54 AM, Yonghong Song wrote:
>>> Bpf cpu=v4 support is introduced in [1] and Commit 4cd58e9af8b9
>>> ("bpf: Support new 32bit offset jmp instruction") added support for new
>>> 32bit offset jmp instruction. Unfortunately, in function
>>> bpf_adj_delta_to_off(), for new branch insn with 32bit offset, the 
>>> offset
>>> (plus/minor a small delta) compares to 16-bit offset bound
>>> [S16_MIN, S16_MAX], which caused the following verification failure:
>>>    $ ./test_progs-cpuv4 -t verif_scale_pyperf180
>>>    ...
>>>    insn 10 cannot be patched due to 16-bit range
>>>    ...
>>>    libbpf: failed to load object 'pyperf180.bpf.o'
>>>    scale_test:FAIL:expect_success unexpected error: -12 (errno 12)
>>>    #405     verif_scale_pyperf180:FAIL
>>>
>>> Note that due to recent llvm18 development, the patch [2] (already 
>>> applied
>>> in bpf-next) needs to be applied to bpf tree for testing purpose.
>>>
>>> The fix is rather simple. For 32bit offset branch insn, the adjusted
>>> offset compares to [S32_MIN, S32_MAX] and then verification succeeded.
>>>
>>>    [1] 
>>> https://lore.kernel.org/all/20230728011143.3710005-1-yonghong.song@linux.dev
>>>    [2] 
>>> https://lore.kernel.org/bpf/20231110193644.3130906-1-yonghong.song@linux.dev
>>>
>>> Fixes: 4cd58e9af8b9 ("bpf: Support new 32bit offset jmp instruction")
>>> Signed-off-by: Yonghong Song <yonghong.song@linux.dev>
>>> ---
>>>   kernel/bpf/core.c | 9 ++++++---
>>>   1 file changed, 6 insertions(+), 3 deletions(-)
>>>
>>> diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c
>>> index cd3afe57ece3..74f2fd48148c 100644
>>> --- a/kernel/bpf/core.c
>>> +++ b/kernel/bpf/core.c
>>> @@ -371,14 +371,17 @@ static int bpf_adj_delta_to_imm(struct 
>>> bpf_insn *insn, u32 pos, s32 end_old,
>>>   static int bpf_adj_delta_to_off(struct bpf_insn *insn, u32 pos, 
>>> s32 end_old,
>>>                   s32 end_new, s32 curr, const bool probe_pass)
>>>   {
>>> -    const s32 off_min = S16_MIN, off_max = S16_MAX;
>>> +    s32 off_min = S16_MIN, off_max = S16_MAX;
>>>       s32 delta = end_new - end_old;
>>>       s32 off;
>>
>> These should all be converted to s64, no? E.g. further below
>> the test will never trigger then for jmp32:
>>
>>        if (off < off_min || off > off_max)
>>                 return -ERANGE;
>
>
> good point! Let us use s64 for potential overflows.
> Will send v2 soon.

I didn't change 's32 delta' type to be consistent with
bpf_adj_delta_to_imm() such that the delta should be
within s32 range. Technically off_min/off_max can
remain as 's32' but I changed them to 's64' to be consistent
with bpf_adj_delta_to_imm().

>
>>
>>> -    if (insn->code == (BPF_JMP32 | BPF_JA))
>>> +    if (insn->code == (BPF_JMP32 | BPF_JA)) {
>>>           off = insn->imm;
>>> -    else
>>> +        off_min = S32_MIN;
>>> +        off_max = S32_MAX;
>>> +    } else {
>>>           off = insn->off;
>>> +    }
>>>         if (curr < pos && curr + off + 1 >= end_old)
>>>           off += delta;
>>>
>>
>

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

end of thread, other threads:[~2023-11-30  0:19 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-11-29  7:54 [PATCH bpf] bpf: Fix a verifier bug due to incorrect branch offset comparison with cpu=v4 Yonghong Song
2023-11-29 19:52 ` Stanislav Fomichev
2023-11-29 22:51 ` Daniel Borkmann
2023-11-29 23:42   ` Yonghong Song
2023-11-30  0:19     ` Yonghong Song

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox