Skip to content

Commit d564ffd

Browse files
committed
Merge branch 'bpf-allow-invoking-kfuncs-from-bpf_prog_type_syscall-progs'
David Vernet says: ==================== 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) ==================== Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Andrii Nakryiko <[email protected]>
2 parents 00d5d22 + 1bc724a commit d564ffd

File tree

6 files changed

+137
-2
lines changed

6 files changed

+137
-2
lines changed

kernel/bpf/cpumask.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,7 @@ static int __init cpumask_kfunc_init(void)
474474
ret = bpf_mem_alloc_init(&bpf_cpumask_ma, sizeof(struct bpf_cpumask), false);
475475
ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, &cpumask_kfunc_set);
476476
ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS, &cpumask_kfunc_set);
477+
ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SYSCALL, &cpumask_kfunc_set);
477478
return ret ?: register_btf_id_dtor_kfuncs(cpumask_dtors,
478479
ARRAY_SIZE(cpumask_dtors),
479480
THIS_MODULE);

kernel/bpf/helpers.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2653,6 +2653,7 @@ static int __init kfunc_init(void)
26532653
ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SCHED_CLS, &generic_kfunc_set);
26542654
ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_XDP, &generic_kfunc_set);
26552655
ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS, &generic_kfunc_set);
2656+
ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SYSCALL, &generic_kfunc_set);
26562657
ret = ret ?: register_btf_id_dtor_kfuncs(generic_dtors,
26572658
ARRAY_SIZE(generic_dtors),
26582659
THIS_MODULE);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright (c) 2024 Meta Platforms, Inc. and affiliates. */
3+
4+
#include <test_progs.h>
5+
6+
#include "verifier_kfunc_prog_types.skel.h"
7+
8+
void test_verifier_kfunc_prog_types(void)
9+
{
10+
RUN_TESTS(verifier_kfunc_prog_types);
11+
}

tools/testing/selftests/bpf/progs/cgrp_kfunc_common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ struct __cgrps_kfunc_map_value {
1313
struct cgroup __kptr * cgrp;
1414
};
1515

16-
struct hash_map {
16+
struct {
1717
__uint(type, BPF_MAP_TYPE_HASH);
1818
__type(key, int);
1919
__type(value, struct __cgrps_kfunc_map_value);

tools/testing/selftests/bpf/progs/task_kfunc_common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ struct __tasks_kfunc_map_value {
1313
struct task_struct __kptr * task;
1414
};
1515

16-
struct hash_map {
16+
struct {
1717
__uint(type, BPF_MAP_TYPE_HASH);
1818
__type(key, int);
1919
__type(value, struct __tasks_kfunc_map_value);
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright (c) 2024 Meta Platforms, Inc. and affiliates. */
3+
4+
#include <vmlinux.h>
5+
#include <bpf/bpf_tracing.h>
6+
#include <bpf/bpf_helpers.h>
7+
8+
#include "bpf_misc.h"
9+
#include "cgrp_kfunc_common.h"
10+
#include "cpumask_common.h"
11+
#include "task_kfunc_common.h"
12+
13+
char _license[] SEC("license") = "GPL";
14+
15+
/***************
16+
* Task kfuncs *
17+
***************/
18+
19+
static void task_kfunc_load_test(void)
20+
{
21+
struct task_struct *current, *ref_1, *ref_2;
22+
23+
current = bpf_get_current_task_btf();
24+
ref_1 = bpf_task_from_pid(current->pid);
25+
if (!ref_1)
26+
return;
27+
28+
ref_2 = bpf_task_acquire(ref_1);
29+
if (ref_2)
30+
bpf_task_release(ref_2);
31+
bpf_task_release(ref_1);
32+
}
33+
34+
SEC("raw_tp")
35+
__failure __msg("calling kernel function")
36+
int BPF_PROG(task_kfunc_raw_tp)
37+
{
38+
task_kfunc_load_test();
39+
return 0;
40+
}
41+
42+
SEC("syscall")
43+
__success
44+
int BPF_PROG(task_kfunc_syscall)
45+
{
46+
task_kfunc_load_test();
47+
return 0;
48+
}
49+
50+
/*****************
51+
* cgroup kfuncs *
52+
*****************/
53+
54+
static void cgrp_kfunc_load_test(void)
55+
{
56+
struct cgroup *cgrp, *ref;
57+
58+
cgrp = bpf_cgroup_from_id(0);
59+
if (!cgrp)
60+
return;
61+
62+
ref = bpf_cgroup_acquire(cgrp);
63+
if (!ref) {
64+
bpf_cgroup_release(cgrp);
65+
return;
66+
}
67+
68+
bpf_cgroup_release(ref);
69+
bpf_cgroup_release(cgrp);
70+
}
71+
72+
SEC("raw_tp")
73+
__failure __msg("calling kernel function")
74+
int BPF_PROG(cgrp_kfunc_raw_tp)
75+
{
76+
cgrp_kfunc_load_test();
77+
return 0;
78+
}
79+
80+
SEC("syscall")
81+
__success
82+
int BPF_PROG(cgrp_kfunc_syscall)
83+
{
84+
cgrp_kfunc_load_test();
85+
return 0;
86+
}
87+
88+
/******************
89+
* cpumask kfuncs *
90+
******************/
91+
92+
static void cpumask_kfunc_load_test(void)
93+
{
94+
struct bpf_cpumask *alloc, *ref;
95+
96+
alloc = bpf_cpumask_create();
97+
if (!alloc)
98+
return;
99+
100+
ref = bpf_cpumask_acquire(alloc);
101+
bpf_cpumask_set_cpu(0, alloc);
102+
bpf_cpumask_test_cpu(0, (const struct cpumask *)ref);
103+
104+
bpf_cpumask_release(ref);
105+
bpf_cpumask_release(alloc);
106+
}
107+
108+
SEC("raw_tp")
109+
__failure __msg("calling kernel function")
110+
int BPF_PROG(cpumask_kfunc_raw_tp)
111+
{
112+
cpumask_kfunc_load_test();
113+
return 0;
114+
}
115+
116+
SEC("syscall")
117+
__success
118+
int BPF_PROG(cpumask_kfunc_syscall)
119+
{
120+
cpumask_kfunc_load_test();
121+
return 0;
122+
}

0 commit comments

Comments
 (0)