diff mbox series

[bpf-next] kernel/bpf: Fix an array-index-out-of-bounds issue in disasm.c

Message ID 20230731204534.1975311-1-yonghong.song@linux.dev (mailing list archive)
State Accepted
Commit e99688eba2e90a600956e936bc335ece902a5d7f
Delegated to: BPF
Headers show
Series [bpf-next] kernel/bpf: Fix an array-index-out-of-bounds issue in disasm.c | expand

Checks

Context Check Description
bpf/vmtest-bpf-next-PR pending PR summary
bpf/vmtest-bpf-next-VM_Test-1 success Logs for ShellCheck
bpf/vmtest-bpf-next-VM_Test-2 success Logs for build for aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-4 success Logs for build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-5 success Logs for build for x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-6 success Logs for set-matrix
bpf/vmtest-bpf-next-VM_Test-3 success Logs for build for s390x with gcc
bpf/vmtest-bpf-next-VM_Test-8 pending Logs for test_maps on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-12 pending Logs for test_progs on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-16 pending Logs for test_progs_no_alu32 on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-25 success Logs for test_verifier on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-26 pending Logs for test_verifier on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-7 success Logs for test_maps on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-9 success Logs for test_maps on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-10 success Logs for test_maps on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-11 success Logs for test_progs on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-13 success Logs for test_progs on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-14 success Logs for test_progs on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-15 success Logs for test_progs_no_alu32 on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-17 success Logs for test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-18 success Logs for test_progs_no_alu32 on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-19 success Logs for test_progs_no_alu32_parallel on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-20 success Logs for test_progs_no_alu32_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-21 success Logs for test_progs_no_alu32_parallel on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-22 success Logs for test_progs_parallel on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-23 success Logs for test_progs_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-24 success Logs for test_progs_parallel on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-27 success Logs for test_verifier on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-28 success Logs for test_verifier on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-29 success Logs for veristat
netdev/series_format success Single patches do not need cover letters
netdev/tree_selection success Clearly marked for bpf-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 1346 this patch: 1346
netdev/cc_maintainers fail 2 blamed authors not CCed: quentin@isovalent.com eddyz87@gmail.com; 9 maintainers not CCed: eddyz87@gmail.com kpsingh@kernel.org martin.lau@linux.dev john.fastabend@gmail.com song@kernel.org sdf@google.com quentin@isovalent.com jolsa@kernel.org haoluo@google.com
netdev/build_clang success Errors and warnings before: 1365 this patch: 1365
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: 1369 this patch: 1369
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 9 lines checked
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Yonghong Song July 31, 2023, 8:45 p.m. UTC
syzbot reported an array-index-out-of-bounds when printing out bpf
insns. Further investigation shows the insn is illegal but
is printed out due to log level 1 or 2 before actual insn verification
in do_check().

This particular illegal insn is a MOVSX insn with offset value 2.
The legal offset value for MOVSX should be 8, 16 and 32.
The disasm sign-extension-size array index is calculated as
 (insn->off / 8) - 1
and offset value 2 gives an out-of-bound index -1.

Tighten the checking for MOVSX insn in disasm.c to avoid
array-index-out-of-bounds issue.

Reported-by: syzbot+3758842a6c01012aa73b@syzkaller.appspotmail.com
Fixes: f835bb622299 ("bpf: Add kernel/bpftool asm support for new instructions")
Signed-off-by: Yonghong Song <yonghong.song@linux.dev>
---
 kernel/bpf/disasm.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

Comments

Eduard Zingerman July 31, 2023, 10:03 p.m. UTC | #1
On Mon, 2023-07-31 at 13:45 -0700, Yonghong Song wrote:
> syzbot reported an array-index-out-of-bounds when printing out bpf
> insns. Further investigation shows the insn is illegal but
> is printed out due to log level 1 or 2 before actual insn verification
> in do_check().
> 
> This particular illegal insn is a MOVSX insn with offset value 2.
> The legal offset value for MOVSX should be 8, 16 and 32.
> The disasm sign-extension-size array index is calculated as
>  (insn->off / 8) - 1
> and offset value 2 gives an out-of-bound index -1.
> 
> Tighten the checking for MOVSX insn in disasm.c to avoid
> array-index-out-of-bounds issue.
> 
> Reported-by: syzbot+3758842a6c01012aa73b@syzkaller.appspotmail.com
> Fixes: f835bb622299 ("bpf: Add kernel/bpftool asm support for new instructions")
> Signed-off-by: Yonghong Song <yonghong.song@linux.dev>

Acked-by: Eduard Zingerman <eddyz87@gmail.com>

> ---
>  kernel/bpf/disasm.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/kernel/bpf/disasm.c b/kernel/bpf/disasm.c
> index d7bff608f299..ef7c107c7b8f 100644
> --- a/kernel/bpf/disasm.c
> +++ b/kernel/bpf/disasm.c
> @@ -162,7 +162,8 @@ static bool is_sdiv_smod(const struct bpf_insn *insn)
>  
>  static bool is_movsx(const struct bpf_insn *insn)
>  {
> -	return BPF_OP(insn->code) == BPF_MOV && insn->off != 0;
> +	return BPF_OP(insn->code) == BPF_MOV &&
> +	       (insn->off == 8 || insn->off == 16 || insn->off == 32);
>  }
>  
>  void print_bpf_insn(const struct bpf_insn_cbs *cbs,
Alexei Starovoitov Aug. 1, 2023, 12:37 a.m. UTC | #2
On Mon, Jul 31, 2023 at 1:45 PM Yonghong Song <yonghong.song@linux.dev> wrote:
>
> syzbot reported an array-index-out-of-bounds when printing out bpf

Applied and fixed subject.
"kernel/bpf:" is an usual prefix. Not sure why you used it.
Just "bpf:" is enough.
Yonghong Song Aug. 1, 2023, 12:39 a.m. UTC | #3
On 7/31/23 5:37 PM, Alexei Starovoitov wrote:
> On Mon, Jul 31, 2023 at 1:45 PM Yonghong Song <yonghong.song@linux.dev> wrote:
>>
>> syzbot reported an array-index-out-of-bounds when printing out bpf
> 
> Applied and fixed subject.
> "kernel/bpf:" is an usual prefix. Not sure why you used it.
> Just "bpf:" is enough.

I am not sure either :-( Maybe sleepy at that moment :-)
patchwork-bot+netdevbpf@kernel.org Aug. 1, 2023, 12:40 a.m. UTC | #4
Hello:

This patch was applied to bpf/bpf-next.git (master)
by Alexei Starovoitov <ast@kernel.org>:

On Mon, 31 Jul 2023 13:45:34 -0700 you wrote:
> syzbot reported an array-index-out-of-bounds when printing out bpf
> insns. Further investigation shows the insn is illegal but
> is printed out due to log level 1 or 2 before actual insn verification
> in do_check().
> 
> This particular illegal insn is a MOVSX insn with offset value 2.
> The legal offset value for MOVSX should be 8, 16 and 32.
> The disasm sign-extension-size array index is calculated as
>  (insn->off / 8) - 1
> and offset value 2 gives an out-of-bound index -1.
> 
> [...]

Here is the summary with links:
  - [bpf-next] kernel/bpf: Fix an array-index-out-of-bounds issue in disasm.c
    https://git.kernel.org/bpf/bpf-next/c/e99688eba2e9

You are awesome, thank you!
diff mbox series

Patch

diff --git a/kernel/bpf/disasm.c b/kernel/bpf/disasm.c
index d7bff608f299..ef7c107c7b8f 100644
--- a/kernel/bpf/disasm.c
+++ b/kernel/bpf/disasm.c
@@ -162,7 +162,8 @@  static bool is_sdiv_smod(const struct bpf_insn *insn)
 
 static bool is_movsx(const struct bpf_insn *insn)
 {
-	return BPF_OP(insn->code) == BPF_MOV && insn->off != 0;
+	return BPF_OP(insn->code) == BPF_MOV &&
+	       (insn->off == 8 || insn->off == 16 || insn->off == 32);
 }
 
 void print_bpf_insn(const struct bpf_insn_cbs *cbs,