diff mbox series

[bpf] bpf: Fix a verifier bug due to incorrect branch offset comparison with cpu=v4

Message ID 20231129075409.2709587-1-yonghong.song@linux.dev (mailing list archive)
State Superseded
Delegated to: BPF
Headers show
Series [bpf] bpf: Fix a verifier bug due to incorrect branch offset comparison with cpu=v4 | expand

Checks

Context Check Description
bpf/vmtest-bpf-PR success PR summary
bpf/vmtest-bpf-VM_Test-9 success Logs for s390x-gcc / build / build for s390x with gcc
bpf/vmtest-bpf-VM_Test-27 success Logs for x86_64-llvm-16 / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with llvm-16
bpf/vmtest-bpf-VM_Test-25 success Logs for x86_64-llvm-16 / test (test_maps, false, 360) / test_maps on x86_64 with llvm-16
bpf/vmtest-bpf-VM_Test-26 success Logs for x86_64-llvm-16 / test (test_progs, false, 360) / test_progs on x86_64 with llvm-16
bpf/vmtest-bpf-VM_Test-29 success Logs for x86_64-llvm-16 / veristat
bpf/vmtest-bpf-VM_Test-28 success Logs for x86_64-llvm-16 / test (test_verifier, false, 360) / test_verifier on x86_64 with llvm-16
netdev/series_format success Single patches do not need cover letters
netdev/codegen success Generated files up to date
netdev/tree_selection success Clearly marked for bpf
netdev/fixes_present success Fixes tag present in non-next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 1124 this patch: 1124
netdev/cc_maintainers fail 1 blamed authors not CCed: eddyz87@gmail.com; 8 maintainers not CCed: song@kernel.org martin.lau@linux.dev kpsingh@kernel.org john.fastabend@gmail.com sdf@google.com haoluo@google.com jolsa@kernel.org eddyz87@gmail.com
netdev/build_clang success Errors and warnings before: 1143 this patch: 1143
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success Fixes tag looks correct
netdev/build_allmodconfig_warn success Errors and warnings before: 1151 this patch: 1151
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 20 lines checked
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-VM_Test-1 success Logs for ShellCheck
bpf/vmtest-bpf-VM_Test-0 success Logs for Lint
bpf/vmtest-bpf-VM_Test-2 success Logs for Validate matrix.py
bpf/vmtest-bpf-VM_Test-3 success Logs for aarch64-gcc / build / build for aarch64 with gcc
bpf/vmtest-bpf-VM_Test-8 success Logs for aarch64-gcc / veristat
bpf/vmtest-bpf-VM_Test-11 success Logs for x86_64-gcc / build / build for x86_64 with gcc
bpf/vmtest-bpf-VM_Test-10 success Logs for set-matrix
bpf/vmtest-bpf-VM_Test-19 success Logs for x86_64-llvm-16 / build / build for x86_64 with llvm-16
bpf/vmtest-bpf-VM_Test-24 success Logs for x86_64-llvm-16 / veristat
bpf/vmtest-bpf-VM_Test-4 success Logs for aarch64-gcc / test (test_maps, false, 360) / test_maps on aarch64 with gcc
bpf/vmtest-bpf-VM_Test-7 success Logs for aarch64-gcc / test (test_verifier, false, 360) / test_verifier on aarch64 with gcc
bpf/vmtest-bpf-VM_Test-14 success Logs for x86_64-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-12 success Logs for x86_64-gcc / test (test_maps, false, 360) / test_maps on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-13 success Logs for x86_64-gcc / test (test_progs, false, 360) / test_progs on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-17 success Logs for x86_64-gcc / test (test_verifier, false, 360) / test_verifier on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-18 success Logs for x86_64-gcc / veristat / veristat on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-20 success Logs for x86_64-llvm-16 / test (test_maps, false, 360) / test_maps on x86_64 with llvm-16
bpf/vmtest-bpf-VM_Test-22 success Logs for x86_64-llvm-16 / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with llvm-16
bpf/vmtest-bpf-VM_Test-21 fail Logs for x86_64-llvm-16 / test (test_progs, false, 360) / test_progs on x86_64 with llvm-16
bpf/vmtest-bpf-VM_Test-23 success Logs for x86_64-llvm-16 / test (test_verifier, false, 360) / test_verifier on x86_64 with llvm-16
bpf/vmtest-bpf-VM_Test-5 success Logs for aarch64-gcc / test (test_progs, false, 360) / test_progs on aarch64 with gcc
bpf/vmtest-bpf-VM_Test-6 success Logs for aarch64-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on aarch64 with gcc
bpf/vmtest-bpf-VM_Test-15 success Logs for x86_64-gcc / test (test_progs_no_alu32_parallel, true, 30) / test_progs_no_alu32_parallel on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-16 success Logs for x86_64-gcc / test (test_progs_parallel, true, 30) / test_progs_parallel on x86_64 with gcc

Commit Message

Yonghong Song Nov. 29, 2023, 7:54 a.m. UTC
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(-)

Comments

Stanislav Fomichev Nov. 29, 2023, 7:52 p.m. UTC | #1
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>
Daniel Borkmann Nov. 29, 2023, 10:51 p.m. UTC | #2
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;
>
Yonghong Song Nov. 29, 2023, 11:42 p.m. UTC | #3
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;
>>
>
Yonghong Song Nov. 30, 2023, 12:19 a.m. UTC | #4
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;
>>>
>>
>
diff mbox series

Patch

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;