Skip to contentSkip to conversation

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

PATCH Applied Yonghong Song 4 participants 5 messages Jul 31, 2023 – Aug 1, 2023

Subsystems (2)
lore
Share & tools
lore

Yonghong Song

author reviewer
#0 lore raw

PATCH 1/1 kernel/bpf: Fix an array-index-out-of-bounds issue in disasm.c

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 [email protected]
Fixes f835bb622299 ("bpf: Add kernel/bpftool asm support for new instructions")
---
 kernel/bpf/disasm.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/bpf/disasm.c +2 -1
index d7bff608f299..ef7c107c7b8f 100644
@@ -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,
Signature · 1 line
2.34.1
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 11 lines · Yonghong Song

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.

Quoted 6 lines

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

Reported-by [email protected]
Fixes f835bb622299 ("bpf: Add kernel/bpftool asm support for new instructions")
Quoted 18 lines
---
 kernel/bpf/disasm.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/bpf/disasm.c +2 -1
index d7bff608f299..ef7c107c7b8f 100644
@@ -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,
#2 lore raw
In reply to Yonghong Song
Quoted 2 lines · Yonghong Song

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.

author reviewer
#3 lore raw
Quoted 7 lines · Alexei Starovoitov
Quoted 2 lines · Yonghong Song

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

#4 lore raw
In reply to Yonghong Song
This patch was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:

Hello:

This patch was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:

Quoted 5 lines · 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().

Quoted 7 lines

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!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html