mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/herbert/cryptodev-2.6.git
synced 2026-04-28 21:46:02 -04:00
selftests/bpf: Add selftests validating the user ringbuf
This change includes selftests that validate the expected behavior and APIs of the new BPF_MAP_TYPE_USER_RINGBUF map type. Signed-off-by: David Vernet <void@manifault.com> Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Link: https://lore.kernel.org/bpf/20220920000100.477320-5-void@manifault.com
This commit is contained in:
committed by
Andrii Nakryiko
parent
b66ccae01f
commit
e5a9df51c7
177
tools/testing/selftests/bpf/progs/user_ringbuf_fail.c
Normal file
177
tools/testing/selftests/bpf/progs/user_ringbuf_fail.c
Normal file
@@ -0,0 +1,177 @@
|
||||
// SPDX-License-Identifier: GPL-2.0
|
||||
/* Copyright (c) 2022 Meta Platforms, Inc. and affiliates. */
|
||||
|
||||
#include <linux/bpf.h>
|
||||
#include <bpf/bpf_helpers.h>
|
||||
#include "bpf_misc.h"
|
||||
|
||||
char _license[] SEC("license") = "GPL";
|
||||
|
||||
struct sample {
|
||||
int pid;
|
||||
int seq;
|
||||
long value;
|
||||
char comm[16];
|
||||
};
|
||||
|
||||
struct {
|
||||
__uint(type, BPF_MAP_TYPE_USER_RINGBUF);
|
||||
} user_ringbuf SEC(".maps");
|
||||
|
||||
static long
|
||||
bad_access1(struct bpf_dynptr *dynptr, void *context)
|
||||
{
|
||||
const struct sample *sample;
|
||||
|
||||
sample = bpf_dynptr_data(dynptr - 1, 0, sizeof(*sample));
|
||||
bpf_printk("Was able to pass bad pointer %lx\n", (__u64)dynptr - 1);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* A callback that accesses a dynptr in a bpf_user_ringbuf_drain callback should
|
||||
* not be able to read before the pointer.
|
||||
*/
|
||||
SEC("?raw_tp/sys_nanosleep")
|
||||
int user_ringbuf_callback_bad_access1(void *ctx)
|
||||
{
|
||||
bpf_user_ringbuf_drain(&user_ringbuf, bad_access1, NULL, 0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static long
|
||||
bad_access2(struct bpf_dynptr *dynptr, void *context)
|
||||
{
|
||||
const struct sample *sample;
|
||||
|
||||
sample = bpf_dynptr_data(dynptr + 1, 0, sizeof(*sample));
|
||||
bpf_printk("Was able to pass bad pointer %lx\n", (__u64)dynptr + 1);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* A callback that accesses a dynptr in a bpf_user_ringbuf_drain callback should
|
||||
* not be able to read past the end of the pointer.
|
||||
*/
|
||||
SEC("?raw_tp/sys_nanosleep")
|
||||
int user_ringbuf_callback_bad_access2(void *ctx)
|
||||
{
|
||||
bpf_user_ringbuf_drain(&user_ringbuf, bad_access2, NULL, 0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static long
|
||||
write_forbidden(struct bpf_dynptr *dynptr, void *context)
|
||||
{
|
||||
*((long *)dynptr) = 0;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* A callback that accesses a dynptr in a bpf_user_ringbuf_drain callback should
|
||||
* not be able to write to that pointer.
|
||||
*/
|
||||
SEC("?raw_tp/sys_nanosleep")
|
||||
int user_ringbuf_callback_write_forbidden(void *ctx)
|
||||
{
|
||||
bpf_user_ringbuf_drain(&user_ringbuf, write_forbidden, NULL, 0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static long
|
||||
null_context_write(struct bpf_dynptr *dynptr, void *context)
|
||||
{
|
||||
*((__u64 *)context) = 0;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* A callback that accesses a dynptr in a bpf_user_ringbuf_drain callback should
|
||||
* not be able to write to that pointer.
|
||||
*/
|
||||
SEC("?raw_tp/sys_nanosleep")
|
||||
int user_ringbuf_callback_null_context_write(void *ctx)
|
||||
{
|
||||
bpf_user_ringbuf_drain(&user_ringbuf, null_context_write, NULL, 0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static long
|
||||
null_context_read(struct bpf_dynptr *dynptr, void *context)
|
||||
{
|
||||
__u64 id = *((__u64 *)context);
|
||||
|
||||
bpf_printk("Read id %lu\n", id);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* A callback that accesses a dynptr in a bpf_user_ringbuf_drain callback should
|
||||
* not be able to write to that pointer.
|
||||
*/
|
||||
SEC("?raw_tp/sys_nanosleep")
|
||||
int user_ringbuf_callback_null_context_read(void *ctx)
|
||||
{
|
||||
bpf_user_ringbuf_drain(&user_ringbuf, null_context_read, NULL, 0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static long
|
||||
try_discard_dynptr(struct bpf_dynptr *dynptr, void *context)
|
||||
{
|
||||
bpf_ringbuf_discard_dynptr(dynptr, 0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* A callback that accesses a dynptr in a bpf_user_ringbuf_drain callback should
|
||||
* not be able to read past the end of the pointer.
|
||||
*/
|
||||
SEC("?raw_tp/sys_nanosleep")
|
||||
int user_ringbuf_callback_discard_dynptr(void *ctx)
|
||||
{
|
||||
bpf_user_ringbuf_drain(&user_ringbuf, try_discard_dynptr, NULL, 0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static long
|
||||
try_submit_dynptr(struct bpf_dynptr *dynptr, void *context)
|
||||
{
|
||||
bpf_ringbuf_submit_dynptr(dynptr, 0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* A callback that accesses a dynptr in a bpf_user_ringbuf_drain callback should
|
||||
* not be able to read past the end of the pointer.
|
||||
*/
|
||||
SEC("?raw_tp/sys_nanosleep")
|
||||
int user_ringbuf_callback_submit_dynptr(void *ctx)
|
||||
{
|
||||
bpf_user_ringbuf_drain(&user_ringbuf, try_submit_dynptr, NULL, 0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static long
|
||||
invalid_drain_callback_return(struct bpf_dynptr *dynptr, void *context)
|
||||
{
|
||||
return 2;
|
||||
}
|
||||
|
||||
/* A callback that accesses a dynptr in a bpf_user_ringbuf_drain callback should
|
||||
* not be able to write to that pointer.
|
||||
*/
|
||||
SEC("?raw_tp/sys_nanosleep")
|
||||
int user_ringbuf_callback_invalid_return(void *ctx)
|
||||
{
|
||||
bpf_user_ringbuf_drain(&user_ringbuf, invalid_drain_callback_return, NULL, 0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user