mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
synced 2026-05-13 07:08:21 -04:00
The tests are pretty basic: - create a virtual uhid device that no userspace will like (to not mess up the running system) - attach a BPF prog to it - open the matching hidraw node - inject one event and check: * that the BPF program can do something on the event stream * can modify the event stream - add another test where we attach/detach BPF programs to see if we get errors Note: the Makefile is extracted from selftests/bpf so we can rebuild the libbpf and bpftool components from the current kernel tree without relying on system installed components. Signed-off-by: Benjamin Tissoires <benjamin.tissoires@redhat.com> Signed-off-by: Jiri Kosina <jkosina@suse.cz>
42 lines
800 B
C
42 lines
800 B
C
// SPDX-License-Identifier: GPL-2.0
|
|
/* Copyright (c) 2022 Red hat */
|
|
#include "vmlinux.h"
|
|
#include <bpf/bpf_helpers.h>
|
|
#include <bpf/bpf_tracing.h>
|
|
#include "hid_bpf_helpers.h"
|
|
|
|
char _license[] SEC("license") = "GPL";
|
|
|
|
struct attach_prog_args {
|
|
int prog_fd;
|
|
unsigned int hid;
|
|
int retval;
|
|
};
|
|
|
|
__u64 callback_check = 52;
|
|
__u64 callback2_check = 52;
|
|
|
|
SEC("?fmod_ret/hid_bpf_device_event")
|
|
int BPF_PROG(hid_first_event, struct hid_bpf_ctx *hid_ctx)
|
|
{
|
|
__u8 *rw_data = hid_bpf_get_data(hid_ctx, 0 /* offset */, 3 /* size */);
|
|
|
|
if (!rw_data)
|
|
return 0; /* EPERM check */
|
|
|
|
callback_check = rw_data[1];
|
|
|
|
rw_data[2] = rw_data[1] + 5;
|
|
|
|
return 0;
|
|
}
|
|
|
|
SEC("syscall")
|
|
int attach_prog(struct attach_prog_args *ctx)
|
|
{
|
|
ctx->retval = hid_bpf_attach_prog(ctx->hid,
|
|
ctx->prog_fd,
|
|
0);
|
|
return 0;
|
|
}
|