bpf: Allow invoking kfuncs from BPF_PROG_TYPE_SYSCALL progs
Subsystems (3)
v2 cover bpf: Allow invoking kfuncs from BPF_PROG_TYPE_SYSCALL progs
Currently, a set of core BPF kfuncs (e.g. bpf_task_*, bpf_cgroup_*, bpf_cpumask_*, etc) cannot be invoked from BPF_PROG_TYPE_SYSCALL programs. The whitelist approach taken for enabling kfuncs makes sense:
it not safe to call these kfuncs from every program type. For example, it may not be safe to call bpf_task_acquire() in an fentry to free_task().
BPF_PROG_TYPE_SYSCALL, on the other hand, is a perfectly safe program type from which to invoke these kfuncs, as it's a very controlled environment, and we should never be able to run into any of the typical problems such as recursive invoations, acquiring references on freeing kptrs, etc. Being able to invoke these kfuncs would be useful, as BPF_PROG_TYPE_SYSCALL can be invoked with BPF_PROG_RUN, and would therefore enable user space programs to synchronously call into BPF to manipulate these kptrs.
---
v1: https://lore.kernel.org/all/[email protected]/
v1 -> v2:
- Create new verifier_kfunc_prog_types testcase meant to specifically validate calling core kfuncs from various program types. Remove the macros and testcases that had been added to the task, cgrp, and cpumask kfunc testcases (Andrii and Yonghong)
David Vernet (2):
bpf: Allow invoking kfuncs from BPF_PROG_TYPE_SYSCALL progs selftests/bpf: Verify calling core kfuncs from BPF_PROG_TYPE_SYCALL
kernel/bpf/cpumask.c | 1 + kernel/bpf/helpers.c | 1 + .../prog_tests/verifier_kfunc_prog_types.c | 11 ++ .../selftests/bpf/progs/cgrp_kfunc_common.h | 2 +- .../selftests/bpf/progs/task_kfunc_common.h | 2 +- .../bpf/progs/verifier_kfunc_prog_types.c | 119 ++++++++++++++++++ 6 files changed, 134 insertions(+), 2 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/verifier_kfunc_prog_types.c create mode 100644 tools/testing/selftests/bpf/progs/verifier_kfunc_prog_types.c
Signature · 1 line
2.44.0
PATCH v2 2/2 selftests/bpf: Verify calling core kfuncs from BPF_PROG_TYPE_SYCALL
Now that we can call some kfuncs from BPF_PROG_TYPE_SYSCALL progs, let's add some selftests that verify as much. As a bonus, let's also verify that we can't call the progs from raw tracepoints. Do do this, we add a new selftest suite called verifier_kfunc_prog_types.
--- .../prog_tests/verifier_kfunc_prog_types.c | 11 ++ .../selftests/bpf/progs/cgrp_kfunc_common.h | 2 +- .../selftests/bpf/progs/task_kfunc_common.h | 2 +- .../bpf/progs/verifier_kfunc_prog_types.c | 119 ++++++++++++++++++ 4 files changed, 132 insertions(+), 2 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/verifier_kfunc_prog_types.c create mode 100644 tools/testing/selftests/bpf/progs/verifier_kfunc_prog_types.c
tools/testing/selftests/bpf/prog_tests/verifier_kfunc_prog_types.c new file +11 -0
@@ -0,0 +1,11 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2024 Meta Platforms, Inc. and affiliates. */ + +#include <test_progs.h> + +#include "verifier_kfunc_prog_types.skel.h" + +void test_verifier_kfunc_prog_types (void ) +{ +RUN_TESTS (verifier_kfunc_prog_types); +}
tools/testing/selftests/bpf/progs/cgrp_kfunc_common.h +1 -1
@@ -13,7 +13,7 @@ struct __cgrps_kfunc_map_value { struct cgroup __kptr * cgrp; }; -struct hash_map { +struct { __uint(type, BPF_MAP_TYPE_HASH); __type(key, int); __type(value, struct __cgrps_kfunc_map_value);
tools/testing/selftests/bpf/progs/task_kfunc_common.h +1 -1
@@ -13,7 +13,7 @@ struct __tasks_kfunc_map_value { struct task_struct __kptr * task; }; -struct hash_map { +struct { __uint(type, BPF_MAP_TYPE_HASH); __type(key, int); __type(value, struct __tasks_kfunc_map_value);
tools/testing/selftests/bpf/progs/verifier_kfunc_prog_types.c new file +119 -0
119 lines not on this page · raw message
Signature · 1 line
2.44.0
PATCH v2 1/2 bpf: Allow invoking kfuncs from BPF_PROG_TYPE_SYSCALL progs
In reply to David VernetCurrently, a set of core BPF kfuncs (e.g. bpf_task_*, bpf_cgroup_*, bpf_cpumask_*, etc) cannot be invoked from BPF_PROG_TYPE_SYSCALL programs. The whitelist approach taken for enabling kfuncs makes sense:
it not safe to call these kfuncs from every program type. For example, it may not be safe to call bpf_task_acquire() in an fentry to free_task().
BPF_PROG_TYPE_SYSCALL, on the other hand, is a perfectly safe program type from which to invoke these kfuncs, as it's a very controlled environment, and we should never be able to run into any of the typical problems such as recursive invoations, acquiring references on freeing kptrs, etc. Being able to invoke these kfuncs would be useful, as BPF_PROG_TYPE_SYSCALL can be invoked with BPF_PROG_RUN, and would therefore enable user space programs to synchronously call into BPF to manipulate these kptrs.
This patch therefore enables invoking the aforementioned core kfuncs from BPF_PROG_TYPE_SYSCALL progs.
--- kernel/bpf/cpumask.c | 1 + kernel/bpf/helpers.c | 1 + 2 files changed, 2 insertions(+)
kernel/bpf/cpumask.c +1 -0
@@ -474,6 +474,7 @@ static int __init cpumask_kfunc_init(void) ret =bpf_mem_alloc_init (&bpf_cpumask_ma,sizeof (struct bpf_cpumask ), false); ret = ret ?:register_btf_kfunc_id_set (BPF_PROG_TYPE_TRACING , &cpumask_kfunc_set); ret = ret ?:register_btf_kfunc_id_set (BPF_PROG_TYPE_STRUCT_OPS , &cpumask_kfunc_set); + ret = ret ?:register_btf_kfunc_id_set (BPF_PROG_TYPE_SYSCALL , &cpumask_kfunc_set);return ret ?:register_btf_id_dtor_kfuncs (cpumask_dtors,ARRAY_SIZE (cpumask_dtors),THIS_MODULE );
kernel/bpf/helpers.c +1 -0
@@ -2653,6 +2653,7 @@ static int __init kfunc_init(void) ret = ret ?:register_btf_kfunc_id_set (BPF_PROG_TYPE_SCHED_CLS , &generic_kfunc_set); ret = ret ?:register_btf_kfunc_id_set (BPF_PROG_TYPE_XDP , &generic_kfunc_set); ret = ret ?:register_btf_kfunc_id_set (BPF_PROG_TYPE_STRUCT_OPS , &generic_kfunc_set); + ret = ret ?:register_btf_kfunc_id_set (BPF_PROG_TYPE_SYSCALL , &generic_kfunc_set); ret = ret ?:register_btf_id_dtor_kfuncs (generic_dtors,ARRAY_SIZE (generic_dtors),THIS_MODULE );
Signature · 1 line
2.44.0
This series was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:
Hello:
This series was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:
Quoted 8 lines · On Fri, 5 Apr 2024 09:30:39 -0500 you wrote:
Currently, a set of core BPF kfuncs (e.g. bpf_task_*, bpf_cgroup_*, bpf_cpumask_*, etc) cannot be invoked from
BPF_PROG_TYPE_SYSCALLprograms. The whitelist approach taken for enabling kfuncs makes sense:
it not safe to call these kfuncs from every program type. For example, it may not be safe to callbpf_task_acquire()in an fentry tofree_task().[...]
Here is the summary with links:
- [bpf-next,v2,1/2] bpf: Allow invoking kfuncs from BPF_PROG_TYPE_SYSCALL progs
https://git.kernel.org/bpf/bpf-next/c/a8e03b6bbb2c - [bpf-next,v2,2/2] selftests/bpf: Verify calling core kfuncs from BPF_PROG_TYPE_SYCALL https://git.kernel.org/bpf/bpf-next/c/1bc724af00cc
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html