* [PATCH bpf] bpf: add missing check_func_arg_reg_off() to prevent out-of-bounds memory accesses
@ 2024-06-19 8:29 Matt Bobrowski
2024-06-19 8:51 ` Kumar Kartikeya Dwivedi
2024-06-19 22:53 ` Eduard Zingerman
0 siblings, 2 replies; 3+ messages in thread
From: Matt Bobrowski @ 2024-06-19 8:29 UTC (permalink / raw)
To: bpf
Cc: ast, daniel, andrii, eddyz87, kpsingh, sdf, jolsa, Matt Bobrowski,
Kumar Kartikeya Dwivedi
Currently, it's possible to pass in a modified CONST_PTR_TO_DYNPTR to
a global function as an argument. The adverse effects of this is that
BPF helpers can continue to make use of this modified
CONST_PTR_TO_DYNPTR from within the context of the global function,
which can unintentionally result in out-of-bounds memory accesses and
therefore compromise overall system stability i.e.
[ 244.157771] BUG: KASAN: slab-out-of-bounds in bpf_dynptr_data+0x137/0x140
[ 244.161345] Read of size 8 at addr ffff88810914be68 by task test_progs/302
[ 244.167151] CPU: 0 PID: 302 Comm: test_progs Tainted: G O E 6.10.0-rc3-00131-g66b586715063 #533
[ 244.174318] Call Trace:
[ 244.175787] <TASK>
[ 244.177356] dump_stack_lvl+0x66/0xa0
[ 244.179531] print_report+0xce/0x670
[ 244.182314] ? __virt_addr_valid+0x200/0x3e0
[ 244.184908] kasan_report+0xd7/0x110
[ 244.187408] ? bpf_dynptr_data+0x137/0x140
[ 244.189714] ? bpf_dynptr_data+0x137/0x140
[ 244.192020] bpf_dynptr_data+0x137/0x140
[ 244.194264] bpf_prog_b02a02fdd2bdc5fa_global_call_bpf_dynptr_data+0x22/0x26
[ 244.198044] bpf_prog_b0fe7b9d7dc3abde_callback_adjust_bpf_dynptr_reg_off+0x1f/0x23
[ 244.202136] bpf_user_ringbuf_drain+0x2c7/0x570
[ 244.204744] ? 0xffffffffc0009e58
[ 244.206593] ? __pfx_bpf_user_ringbuf_drain+0x10/0x10
[ 244.209795] bpf_prog_33ab33f6a804ba2d_user_ringbuf_callback_const_ptr_to_dynptr_reg_off+0x47/0x4b
[ 244.215922] bpf_trampoline_6442502480+0x43/0xe3
[ 244.218691] __x64_sys_prlimit64+0x9/0xf0
[ 244.220912] do_syscall_64+0xc1/0x1d0
[ 244.223043] entry_SYSCALL_64_after_hwframe+0x77/0x7f
[ 244.226458] RIP: 0033:0x7ffa3eb8f059
[ 244.228582] Code: 08 89 e8 5b 5d c3 66 2e 0f 1f 84 00 00 00 00 00 90 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 8b 0d 8f 1d 0d 00 f7 d8 64 89 01 48
[ 244.241307] RSP: 002b:00007ffa3e9c6eb8 EFLAGS: 00000206 ORIG_RAX: 000000000000012e
[ 244.246474] RAX: ffffffffffffffda RBX: 00007ffa3e9c7cdc RCX: 00007ffa3eb8f059
[ 244.250478] RDX: 00007ffa3eb162b4 RSI: 0000000000000000 RDI: 00007ffa3e9c7fb0
[ 244.255396] RBP: 00007ffa3e9c6ed0 R08: 00007ffa3e9c76c0 R09: 0000000000000000
[ 244.260195] R10: 0000000000000000 R11: 0000000000000206 R12: ffffffffffffff80
[ 244.264201] R13: 000000000000001c R14: 00007ffc5d6b4260 R15: 00007ffa3e1c7000
[ 244.268303] </TASK>
Add a check_func_arg_reg_off() to the path in which the BPF verifier
verifies the arguments of global function arguments, specifically
those which take an argument of type ARG_PTR_TO_DYNPTR |
MEM_RDONLY. Also, process_dynptr_func() doesn't appear to perform any
explicit and strict type matching on the supplied register type, so
let's also enforce that a register either type PTR_TO_STACK or
CONST_PTR_TO_DYNPTR is by the caller. Lastly, we also add a new
negative test which catches this regression.
Reported-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
Signed-off-by: Matt Bobrowski <mattbobrowski@google.com>
---
kernel/bpf/verifier.c | 27 ++++++++++++-------
.../bpf/progs/test_kfunc_dynptr_param.c | 2 +-
.../selftests/bpf/progs/user_ringbuf_fail.c | 22 +++++++++++++++
3 files changed, 41 insertions(+), 10 deletions(-)
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index e0a398a97d32..6dd0a0b7db8e 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -7715,6 +7715,13 @@ static int process_dynptr_func(struct bpf_verifier_env *env, int regno, int insn
struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno];
int err;
+ if (reg->type != PTR_TO_STACK && reg->type != CONST_PTR_TO_DYNPTR) {
+ verbose(env,
+ "arg#%d expected pointer to stack or const struct bpf_dynptr\n",
+ regno);
+ return -EINVAL;
+ }
+
/* MEM_UNINIT and MEM_RDONLY are exclusive, when applied to an
* ARG_PTR_TO_DYNPTR (or ARG_PTR_TO_DYNPTR | DYNPTR_TYPE_*):
*/
@@ -9464,7 +9471,13 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env, int subprog,
return -EINVAL;
}
} else if (arg->arg_type == (ARG_PTR_TO_DYNPTR | MEM_RDONLY)) {
- ret = process_dynptr_func(env, regno, -1, arg->arg_type, 0);
+ ret = check_func_arg_reg_off(env, reg, regno,
+ ARG_PTR_TO_DYNPTR);
+ if (ret)
+ return ret;
+
+ ret = process_dynptr_func(env, regno, -1, arg->arg_type,
+ 0);
if (ret)
return ret;
} else if (base_type(arg->arg_type) == ARG_PTR_TO_BTF_ID) {
@@ -11954,14 +11967,8 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_
break;
case KF_ARG_PTR_TO_DYNPTR:
{
- enum bpf_arg_type dynptr_arg_type = ARG_PTR_TO_DYNPTR;
int clone_ref_obj_id = 0;
-
- if (reg->type != PTR_TO_STACK &&
- reg->type != CONST_PTR_TO_DYNPTR) {
- verbose(env, "arg#%d expected pointer to stack or dynptr_ptr\n", i);
- return -EINVAL;
- }
+ enum bpf_arg_type dynptr_arg_type = ARG_PTR_TO_DYNPTR;
if (reg->type == CONST_PTR_TO_DYNPTR)
dynptr_arg_type |= MEM_RDONLY;
@@ -11990,7 +11997,9 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_
}
}
- ret = process_dynptr_func(env, regno, insn_idx, dynptr_arg_type, clone_ref_obj_id);
+ ret = process_dynptr_func(env, regno, insn_idx,
+ dynptr_arg_type,
+ clone_ref_obj_id);
if (ret < 0)
return ret;
diff --git a/tools/testing/selftests/bpf/progs/test_kfunc_dynptr_param.c b/tools/testing/selftests/bpf/progs/test_kfunc_dynptr_param.c
index 2dde8e3fe4c9..e68667aec6a6 100644
--- a/tools/testing/selftests/bpf/progs/test_kfunc_dynptr_param.c
+++ b/tools/testing/selftests/bpf/progs/test_kfunc_dynptr_param.c
@@ -45,7 +45,7 @@ int BPF_PROG(not_valid_dynptr, int cmd, union bpf_attr *attr, unsigned int size)
}
SEC("?lsm.s/bpf")
-__failure __msg("arg#0 expected pointer to stack or dynptr_ptr")
+__failure __msg("arg#1 expected pointer to stack or const struct bpf_dynptr")
int BPF_PROG(not_ptr_to_stack, int cmd, union bpf_attr *attr, unsigned int size)
{
unsigned long val = 0;
diff --git a/tools/testing/selftests/bpf/progs/user_ringbuf_fail.c b/tools/testing/selftests/bpf/progs/user_ringbuf_fail.c
index 11ab25c42c36..54de0389f878 100644
--- a/tools/testing/selftests/bpf/progs/user_ringbuf_fail.c
+++ b/tools/testing/selftests/bpf/progs/user_ringbuf_fail.c
@@ -221,3 +221,25 @@ int user_ringbuf_callback_reinit_dynptr_ringbuf(void *ctx)
bpf_user_ringbuf_drain(&user_ringbuf, try_reinit_dynptr_ringbuf, NULL, 0);
return 0;
}
+
+__noinline long global_call_bpf_dynptr_data(struct bpf_dynptr *dynptr)
+{
+ bpf_dynptr_data(dynptr, 0xA, 0xA);
+ return 0;
+}
+
+static long callback_adjust_bpf_dynptr_reg_off(struct bpf_dynptr *dynptr,
+ void *ctx)
+{
+ global_call_bpf_dynptr_data(dynptr += 1024);
+ return 0;
+}
+
+SEC("?raw_tp")
+__failure __msg("dereference of modified dynptr_ptr ptr R1 off=16384 disallowed")
+int user_ringbuf_callback_const_ptr_to_dynptr_reg_off(void *ctx)
+{
+ bpf_user_ringbuf_drain(&user_ringbuf,
+ callback_adjust_bpf_dynptr_reg_off, NULL, 0);
+ return 0;
+}
--
2.45.2.627.g7a2c4fd464-goog
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH bpf] bpf: add missing check_func_arg_reg_off() to prevent out-of-bounds memory accesses
2024-06-19 8:29 [PATCH bpf] bpf: add missing check_func_arg_reg_off() to prevent out-of-bounds memory accesses Matt Bobrowski
@ 2024-06-19 8:51 ` Kumar Kartikeya Dwivedi
2024-06-19 22:53 ` Eduard Zingerman
1 sibling, 0 replies; 3+ messages in thread
From: Kumar Kartikeya Dwivedi @ 2024-06-19 8:51 UTC (permalink / raw)
To: Matt Bobrowski; +Cc: bpf, ast, daniel, andrii, eddyz87, kpsingh, sdf, jolsa
On Wed, 19 Jun 2024 at 10:29, Matt Bobrowski <mattbobrowski@google.com> wrote:
>
> Currently, it's possible to pass in a modified CONST_PTR_TO_DYNPTR to
> a global function as an argument. The adverse effects of this is that
> BPF helpers can continue to make use of this modified
> CONST_PTR_TO_DYNPTR from within the context of the global function,
> which can unintentionally result in out-of-bounds memory accesses and
> therefore compromise overall system stability i.e.
>
> [ 244.157771] BUG: KASAN: slab-out-of-bounds in bpf_dynptr_data+0x137/0x140
> [ 244.161345] Read of size 8 at addr ffff88810914be68 by task test_progs/302
> [ 244.167151] CPU: 0 PID: 302 Comm: test_progs Tainted: G O E 6.10.0-rc3-00131-g66b586715063 #533
> [ 244.174318] Call Trace:
> [ 244.175787] <TASK>
> [ 244.177356] dump_stack_lvl+0x66/0xa0
> [ 244.179531] print_report+0xce/0x670
> [ 244.182314] ? __virt_addr_valid+0x200/0x3e0
> [ 244.184908] kasan_report+0xd7/0x110
> [ 244.187408] ? bpf_dynptr_data+0x137/0x140
> [ 244.189714] ? bpf_dynptr_data+0x137/0x140
> [ 244.192020] bpf_dynptr_data+0x137/0x140
> [ 244.194264] bpf_prog_b02a02fdd2bdc5fa_global_call_bpf_dynptr_data+0x22/0x26
> [ 244.198044] bpf_prog_b0fe7b9d7dc3abde_callback_adjust_bpf_dynptr_reg_off+0x1f/0x23
> [ 244.202136] bpf_user_ringbuf_drain+0x2c7/0x570
> [ 244.204744] ? 0xffffffffc0009e58
> [ 244.206593] ? __pfx_bpf_user_ringbuf_drain+0x10/0x10
> [ 244.209795] bpf_prog_33ab33f6a804ba2d_user_ringbuf_callback_const_ptr_to_dynptr_reg_off+0x47/0x4b
> [ 244.215922] bpf_trampoline_6442502480+0x43/0xe3
> [ 244.218691] __x64_sys_prlimit64+0x9/0xf0
> [ 244.220912] do_syscall_64+0xc1/0x1d0
> [ 244.223043] entry_SYSCALL_64_after_hwframe+0x77/0x7f
> [ 244.226458] RIP: 0033:0x7ffa3eb8f059
> [ 244.228582] Code: 08 89 e8 5b 5d c3 66 2e 0f 1f 84 00 00 00 00 00 90 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 8b 0d 8f 1d 0d 00 f7 d8 64 89 01 48
> [ 244.241307] RSP: 002b:00007ffa3e9c6eb8 EFLAGS: 00000206 ORIG_RAX: 000000000000012e
> [ 244.246474] RAX: ffffffffffffffda RBX: 00007ffa3e9c7cdc RCX: 00007ffa3eb8f059
> [ 244.250478] RDX: 00007ffa3eb162b4 RSI: 0000000000000000 RDI: 00007ffa3e9c7fb0
> [ 244.255396] RBP: 00007ffa3e9c6ed0 R08: 00007ffa3e9c76c0 R09: 0000000000000000
> [ 244.260195] R10: 0000000000000000 R11: 0000000000000206 R12: ffffffffffffff80
> [ 244.264201] R13: 000000000000001c R14: 00007ffc5d6b4260 R15: 00007ffa3e1c7000
> [ 244.268303] </TASK>
>
> Add a check_func_arg_reg_off() to the path in which the BPF verifier
> verifies the arguments of global function arguments, specifically
> those which take an argument of type ARG_PTR_TO_DYNPTR |
> MEM_RDONLY. Also, process_dynptr_func() doesn't appear to perform any
> explicit and strict type matching on the supplied register type, so
> let's also enforce that a register either type PTR_TO_STACK or
> CONST_PTR_TO_DYNPTR is by the caller. Lastly, we also add a new
> negative test which catches this regression.
>
> Reported-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
> Signed-off-by: Matt Bobrowski <mattbobrowski@google.com>
> ---
Acked-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
As we discussed offline, once you respin, it'd be best to split
selftests and the fix into two, and probably add one more for the lack
of reg->type check (if it's possible to trigger without this fix), and
extend the commit log accordingly.
Apart from that, fix & test lgtm!
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH bpf] bpf: add missing check_func_arg_reg_off() to prevent out-of-bounds memory accesses
2024-06-19 8:29 [PATCH bpf] bpf: add missing check_func_arg_reg_off() to prevent out-of-bounds memory accesses Matt Bobrowski
2024-06-19 8:51 ` Kumar Kartikeya Dwivedi
@ 2024-06-19 22:53 ` Eduard Zingerman
1 sibling, 0 replies; 3+ messages in thread
From: Eduard Zingerman @ 2024-06-19 22:53 UTC (permalink / raw)
To: Matt Bobrowski, bpf
Cc: ast, daniel, andrii, kpsingh, sdf, jolsa, Kumar Kartikeya Dwivedi
On Wed, 2024-06-19 at 08:29 +0000, Matt Bobrowski wrote:
> Currently, it's possible to pass in a modified CONST_PTR_TO_DYNPTR to
> a global function as an argument. The adverse effects of this is that
> BPF helpers can continue to make use of this modified
> CONST_PTR_TO_DYNPTR from within the context of the global function,
> which can unintentionally result in out-of-bounds memory accesses and
> therefore compromise overall system stability i.e.
[...]
> Reported-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
> Signed-off-by: Matt Bobrowski <mattbobrowski@google.com>
> ---
Hi Matt,
Thank you for fixing this bug. Overall looks good,
I second the requests from Kumar + one nit from me.
Also, note that kfunc_param_nullable/kfunc_dynptr_nullable_test3
needs an update.
Acked-by: Eduard Zingerman <eddyz87@gmail.com>
[...]
> @@ -9464,7 +9471,13 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env, int subprog,
> return -EINVAL;
> }
> } else if (arg->arg_type == (ARG_PTR_TO_DYNPTR | MEM_RDONLY)) {
> - ret = process_dynptr_func(env, regno, -1, arg->arg_type, 0);
> + ret = check_func_arg_reg_off(env, reg, regno,
> + ARG_PTR_TO_DYNPTR);
Nit: Please avoid splitting lines too often, this line should be:
ret = check_func_arg_reg_off(env, reg, regno, ARG_PTR_TO_DYNPTR);
the limit for line length is 100 characters.
There are a few more such places.
> + if (ret)
> + return ret;
> +
> + ret = process_dynptr_func(env, regno, -1, arg->arg_type,
> + 0);
> if (ret)
> return ret;
> } else if (base_type(arg->arg_type) == ARG_PTR_TO_BTF_ID) {
[...]
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2024-06-19 22:53 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-06-19 8:29 [PATCH bpf] bpf: add missing check_func_arg_reg_off() to prevent out-of-bounds memory accesses Matt Bobrowski
2024-06-19 8:51 ` Kumar Kartikeya Dwivedi
2024-06-19 22:53 ` Eduard Zingerman
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox