Skip to content

Commit 070110f

Browse files
kkdwvdtheihor
authored andcommitted
selftests/bpf: Exercise unsafe obj drops from tracing progs
Add task_kfunc failure cases for bpf_obj_drop() on local objects with referenced kptr fields from tracing and NMI tracing programs. These programs must be rejected because dropping the object would run full special-field destruction synchronously in an unsafe context. Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
1 parent 2c550f1 commit 070110f

1 file changed

Lines changed: 40 additions & 0 deletions

File tree

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <bpf/bpf_tracing.h>
66
#include <bpf/bpf_helpers.h>
77

8+
#include "../bpf_experimental.h"
89
#include "bpf_misc.h"
910
#include "task_kfunc_common.h"
1011

@@ -233,6 +234,45 @@ int BPF_PROG(task_kfunc_release_unacquired, struct task_struct *task, u64 clone_
233234
return 0;
234235
}
235236

237+
SEC("tp_btf/task_newtask")
238+
__failure __msg("bpf_obj_drop cannot be used in tracing programs on types with NMI unsafe fields")
239+
int BPF_PROG(task_kfunc_obj_drop_with_kptr, struct task_struct *task, u64 clone_flags)
240+
{
241+
struct __tasks_kfunc_map_value *local;
242+
243+
local = bpf_obj_new(typeof(*local));
244+
if (!local)
245+
return 0;
246+
247+
bpf_obj_drop(local);
248+
return 0;
249+
}
250+
251+
SEC("tp_btf/task_newtask")
252+
__failure __msg("bpf_obj_drop cannot be used in tracing programs on types with NMI unsafe fields")
253+
int BPF_PROG(task_kfunc_obj_drop_nmi_with_kptr, struct task_struct *task,
254+
u64 clone_flags)
255+
{
256+
struct __tasks_kfunc_map_value *local;
257+
struct task_struct *acquired, *old;
258+
259+
(void)clone_flags;
260+
261+
local = bpf_obj_new(typeof(*local));
262+
if (!local)
263+
return 0;
264+
265+
acquired = bpf_task_acquire(task);
266+
if (acquired) {
267+
old = bpf_kptr_xchg(&local->task, acquired);
268+
if (old)
269+
bpf_task_release(old);
270+
}
271+
272+
bpf_obj_drop(local);
273+
return 0;
274+
}
275+
236276
SEC("tp_btf/task_newtask")
237277
__failure __msg("Possibly NULL pointer passed to trusted R1")
238278
int BPF_PROG(task_kfunc_from_pid_no_null_check, struct task_struct *task, u64 clone_flags)

0 commit comments

Comments
 (0)