Skip to content

Commit caf713c

Browse files
Byte-LabAlexei Starovoitov
authored andcommitted
bpf: Disallow NULLable pointers for trusted kfuncs
KF_TRUSTED_ARGS kfuncs currently have a subtle and insidious bug in validating pointers to scalars. Say that you have a kfunc like the following, which takes an array as the first argument: bool bpf_cpumask_empty(const struct cpumask *cpumask) { return cpumask_empty(cpumask); } ... BTF_ID_FLAGS(func, bpf_cpumask_empty, KF_TRUSTED_ARGS) ... If a BPF program were to invoke the kfunc with a NULL argument, it would crash the kernel. The reason is that struct cpumask is defined as a bitmap, which is itself defined as an array, and is accessed as a memory address by bitmap operations. So when the verifier analyzes the register, it interprets it as a pointer to a scalar struct, which is an array of size 8. check_mem_reg() then sees that the register is NULL and returns 0, and the kfunc crashes when it passes it down to the cpumask wrappers. To fix this, this patch adds a check for KF_ARG_PTR_TO_MEM which verifies that the register doesn't contain a possibly-NULL pointer if the kfunc is KF_TRUSTED_ARGS. Signed-off-by: David Vernet <void@manifault.com> Link: https://lore.kernel.org/r/20230125143816.721952-2-void@manifault.com Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent b613d33 commit caf713c

File tree

3 files changed

+10
-4
lines changed

3 files changed

+10
-4
lines changed

kernel/bpf/verifier.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9194,6 +9194,12 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_
91949194
return -EINVAL;
91959195
}
91969196

9197+
if (is_kfunc_trusted_args(meta) &&
9198+
(register_is_null(reg) || type_may_be_null(reg->type))) {
9199+
verbose(env, "Possibly NULL pointer passed to trusted arg%d\n", i);
9200+
return -EACCES;
9201+
}
9202+
91979203
if (reg->ref_obj_id) {
91989204
if (is_kfunc_release(meta) && meta->ref_obj_id) {
91999205
verbose(env, "verifier internal error: more than one arg with ref_obj_id R%d %u %u\n",

tools/testing/selftests/bpf/prog_tests/cgrp_kfunc.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,11 @@ static struct {
9393
const char *prog_name;
9494
const char *expected_err_msg;
9595
} failure_tests[] = {
96-
{"cgrp_kfunc_acquire_untrusted", "R1 must be referenced or trusted"},
96+
{"cgrp_kfunc_acquire_untrusted", "Possibly NULL pointer passed to trusted arg0"},
9797
{"cgrp_kfunc_acquire_fp", "arg#0 pointer type STRUCT cgroup must point"},
9898
{"cgrp_kfunc_acquire_unsafe_kretprobe", "reg type unsupported for arg#0 function"},
9999
{"cgrp_kfunc_acquire_trusted_walked", "R1 must be referenced or trusted"},
100-
{"cgrp_kfunc_acquire_null", "arg#0 pointer type STRUCT cgroup must point"},
100+
{"cgrp_kfunc_acquire_null", "Possibly NULL pointer passed to trusted arg0"},
101101
{"cgrp_kfunc_acquire_unreleased", "Unreleased reference"},
102102
{"cgrp_kfunc_get_non_kptr_param", "arg#0 expected pointer to map value"},
103103
{"cgrp_kfunc_get_non_kptr_acquired", "arg#0 expected pointer to map value"},

tools/testing/selftests/bpf/progs/task_kfunc_failure.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ static struct __tasks_kfunc_map_value *insert_lookup_task(struct task_struct *ta
2828
}
2929

3030
SEC("tp_btf/task_newtask")
31-
__failure __msg("R1 must be referenced or trusted")
31+
__failure __msg("Possibly NULL pointer passed to trusted arg0")
3232
int BPF_PROG(task_kfunc_acquire_untrusted, struct task_struct *task, u64 clone_flags)
3333
{
3434
struct task_struct *acquired;
@@ -86,7 +86,7 @@ int BPF_PROG(task_kfunc_acquire_trusted_walked, struct task_struct *task, u64 cl
8686

8787

8888
SEC("tp_btf/task_newtask")
89-
__failure __msg("arg#0 pointer type STRUCT task_struct must point")
89+
__failure __msg("Possibly NULL pointer passed to trusted arg0")
9090
int BPF_PROG(task_kfunc_acquire_null, struct task_struct *task, u64 clone_flags)
9191
{
9292
struct task_struct *acquired;

0 commit comments

Comments
 (0)