[PATCH] fix array-index-out-of-bounds in bpf_prog_select_runtime

Camila Alvarez posted 1 patch 1 year, 10 months ago
There is a newer version of this series
kernel/bpf/core.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
[PATCH] fix array-index-out-of-bounds in bpf_prog_select_runtime
Posted by Camila Alvarez 1 year, 10 months ago
BPF documentation specifies that the maximum stack depth for a BPF
program is 512 bytes. This is not enforced when selecting a bpf
interpreter, thus casuing an index out of bounds error when trying to
obtain an interpreter with a bigger stack size.

This patch enforces the stack size to be not bigger than
512.

Reported-by: syzbot+d2a2c639d03ac200a4f1@syzkaller.appspotmail.com
Signed-off-by: Camila Alvarez <cam.alvarez.i@gmail.com>
---
 kernel/bpf/core.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c
index 696bc55de8e8..8167b3a721e9 100644
--- a/kernel/bpf/core.c
+++ b/kernel/bpf/core.c
@@ -2196,7 +2196,7 @@ static u64 ___bpf_prog_run(u64 *regs, const struct bpf_insn *insn)
 		BUG_ON(1);
 		return 0;
 }
-
+#define BPF_MAX_STACK_SIZE 512
 #define PROG_NAME(stack_size) __bpf_prog_run##stack_size
 #define DEFINE_BPF_PROG_RUN(stack_size) \
 static unsigned int PROG_NAME(stack_size)(const void *ctx, const struct bpf_insn *insn) \
@@ -2345,7 +2345,7 @@ static void bpf_prog_select_func(struct bpf_prog *fp)
 {
 #ifndef CONFIG_BPF_JIT_ALWAYS_ON
 	u32 stack_depth = max_t(u32, fp->aux->stack_depth, 1);
-
+	stack_depth = min_t(u32, stack_depth, BPF_MAX_STACK_SIZE);
 	fp->bpf_func = interpreters[(round_up(stack_depth, 32) / 32) - 1];
 #else
 	fp->bpf_func = __bpf_prog_ret0_warn;
-- 
2.34.1
Re: [PATCH] fix array-index-out-of-bounds in bpf_prog_select_runtime
Posted by Alexei Starovoitov 1 year, 10 months ago
On Mon, Mar 25, 2024 at 5:41 PM Camila Alvarez <cam.alvarez.i@gmail.com> wrote:
>
> BPF documentation specifies that the maximum stack depth for a BPF
> program is 512 bytes. This is not enforced when selecting a bpf
> interpreter, thus casuing an index out of bounds error when trying to
> obtain an interpreter with a bigger stack size.
>
> This patch enforces the stack size to be not bigger than
> 512.
>
> Reported-by: syzbot+d2a2c639d03ac200a4f1@syzkaller.appspotmail.com
> Signed-off-by: Camila Alvarez <cam.alvarez.i@gmail.com>
> ---
>  kernel/bpf/core.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c
> index 696bc55de8e8..8167b3a721e9 100644
> --- a/kernel/bpf/core.c
> +++ b/kernel/bpf/core.c
> @@ -2196,7 +2196,7 @@ static u64 ___bpf_prog_run(u64 *regs, const struct bpf_insn *insn)
>                 BUG_ON(1);
>                 return 0;
>  }
> -
> +#define BPF_MAX_STACK_SIZE 512
>  #define PROG_NAME(stack_size) __bpf_prog_run##stack_size
>  #define DEFINE_BPF_PROG_RUN(stack_size) \
>  static unsigned int PROG_NAME(stack_size)(const void *ctx, const struct bpf_insn *insn) \
> @@ -2345,7 +2345,7 @@ static void bpf_prog_select_func(struct bpf_prog *fp)
>  {
>  #ifndef CONFIG_BPF_JIT_ALWAYS_ON
>         u32 stack_depth = max_t(u32, fp->aux->stack_depth, 1);
> -
> +       stack_depth = min_t(u32, stack_depth, BPF_MAX_STACK_SIZE);
>         fp->bpf_func = interpreters[(round_up(stack_depth, 32) / 32) - 1];

That's not the root cause of the issue.
syzbot is saying: index 16 is out of range for type '<unknown> *[16]'

somehow stack depth got bigger than 512.
The bug is somewhere in the verifier.
Please debug it further.