selftests/hid: Add a test to ensure we can write fields in hid_device

hid_device->{name,uniq,phys} are all writeable fields, we need to have
tests for them in case the verifier becomes too much strict.

Signed-off-by: Benjamin Tissoires <bentiss@kernel.org>
This commit is contained in:
Benjamin Tissoires 2026-08-25 11:55:12 +02:00
parent 67bfe48a29
commit 1fb68c2e76
3 changed files with 55 additions and 0 deletions

View File

@ -909,6 +909,32 @@ TEST_F(hid_bpf, test_rdesc_fixup_get_data_overflow)
ASSERT_EQ(self->skel->bss->get_data_overflow_check, 1);
}
TEST_F(hid_bpf, test_rdesc_fixup_change_uniq_name_phys)
{
const struct test_program progs[] = {
{ .name = "hid_rdesc_fixup_change_uniq_name_phys" },
};
char expected[256], buf[256] = {};
int err;
LOAD_PROGRAMS(progs);
err = ioctl(self->hidraw_fd, HIDIOCGRAWNAME(sizeof(buf)), buf);
ASSERT_GE(err, 0) TH_LOG("HIDIOCGRAWNAME");
ASSERT_STREQ("name coming from bpf", buf);
snprintf(expected, sizeof(expected), "%d phys:coming:from:bpf", self->hid.dev_id);
err = ioctl(self->hidraw_fd, HIDIOCGRAWPHYS(sizeof(buf)), buf);
ASSERT_GE(err, 0) TH_LOG("HIDIOCGRAWPHYS");
ASSERT_STREQ(expected, buf);
err = ioctl(self->hidraw_fd, HIDIOCGRAWUNIQ(sizeof(buf)), buf);
ASSERT_GE(err, 0) TH_LOG("HIDIOCGRAWUNIQ");
ASSERT_STREQ("uniq:coming:from:bpf", buf);
}
static int libbpf_print_fn(enum libbpf_print_level level,
const char *format, va_list args)
{

View File

@ -255,6 +255,32 @@ struct hid_bpf_ops rdesc_fixup_get_data_overflow = {
.hid_rdesc_fixup = (void *)hid_rdesc_fixup_get_data_overflow,
};
SEC("?struct_ops.s/hid_rdesc_fixup")
int BPF_PROG(hid_rdesc_fixup_change_uniq_name_phys, struct hid_bpf_ctx *hid_ctx)
{
#define HID_BPF_MEMCPY(target, str) \
__builtin_memcpy(target, str, sizeof(str))
HID_BPF_MEMCPY(hid_ctx->hid->name, "name coming from bpf");
HID_BPF_MEMCPY(hid_ctx->hid->uniq, "uniq:coming:from:bpf");
/* hid_bpf relies on a phys being a rand % 1024 */
for (int i = 0; i < 5; i++) {
if (!hid_ctx->hid->phys[i]) {
HID_BPF_MEMCPY(hid_ctx->hid->phys + i, " phys:coming:from:bpf");
break;
}
}
#undef HID_BPF_MEMCPY
return 0;
}
SEC(".struct_ops.link")
struct hid_bpf_ops rdesc_fixup_change_uniq_name_phys = {
.hid_rdesc_fixup = (void *)hid_rdesc_fixup_change_uniq_name_phys,
};
SEC("?struct_ops/hid_device_event")
int BPF_PROG(hid_test_insert1, struct hid_bpf_ctx *hid_ctx, enum hid_report_type type)
{

View File

@ -61,6 +61,9 @@ enum hid_report_type {
struct hid_device {
unsigned int id;
char name[128];
char phys[64];
char uniq[64];
} __attribute__((preserve_access_index));
struct bpf_wq {