1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright (c) 2022 Red Hat */ 3 #include "hid.skel.h" 4 5 #include "../kselftest_harness.h" 6 7 #include <bpf/bpf.h> 8 #include <fcntl.h> 9 #include <fnmatch.h> 10 #include <dirent.h> 11 #include <poll.h> 12 #include <pthread.h> 13 #include <stdbool.h> 14 #include <linux/hidraw.h> 15 #include <linux/uhid.h> 16 17 #define SHOW_UHID_DEBUG 0 18 19 static unsigned char rdesc[] = { 20 0x06, 0x00, 0xff, /* Usage Page (Vendor Defined Page 1) */ 21 0x09, 0x21, /* Usage (Vendor Usage 0x21) */ 22 0xa1, 0x01, /* COLLECTION (Application) */ 23 0x09, 0x01, /* Usage (Vendor Usage 0x01) */ 24 0xa1, 0x00, /* COLLECTION (Physical) */ 25 0x85, 0x02, /* REPORT_ID (2) */ 26 0x19, 0x01, /* USAGE_MINIMUM (1) */ 27 0x29, 0x08, /* USAGE_MAXIMUM (3) */ 28 0x15, 0x00, /* LOGICAL_MINIMUM (0) */ 29 0x25, 0xff, /* LOGICAL_MAXIMUM (255) */ 30 0x95, 0x08, /* REPORT_COUNT (8) */ 31 0x75, 0x08, /* REPORT_SIZE (8) */ 32 0x81, 0x02, /* INPUT (Data,Var,Abs) */ 33 0xc0, /* END_COLLECTION */ 34 0x09, 0x01, /* Usage (Vendor Usage 0x01) */ 35 0xa1, 0x00, /* COLLECTION (Physical) */ 36 0x85, 0x01, /* REPORT_ID (1) */ 37 0x06, 0x00, 0xff, /* Usage Page (Vendor Defined Page 1) */ 38 0x19, 0x01, /* USAGE_MINIMUM (1) */ 39 0x29, 0x03, /* USAGE_MAXIMUM (3) */ 40 0x15, 0x00, /* LOGICAL_MINIMUM (0) */ 41 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */ 42 0x95, 0x03, /* REPORT_COUNT (3) */ 43 0x75, 0x01, /* REPORT_SIZE (1) */ 44 0x81, 0x02, /* INPUT (Data,Var,Abs) */ 45 0x95, 0x01, /* REPORT_COUNT (1) */ 46 0x75, 0x05, /* REPORT_SIZE (5) */ 47 0x81, 0x01, /* INPUT (Cnst,Var,Abs) */ 48 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */ 49 0x09, 0x30, /* USAGE (X) */ 50 0x09, 0x31, /* USAGE (Y) */ 51 0x15, 0x81, /* LOGICAL_MINIMUM (-127) */ 52 0x25, 0x7f, /* LOGICAL_MAXIMUM (127) */ 53 0x75, 0x10, /* REPORT_SIZE (16) */ 54 0x95, 0x02, /* REPORT_COUNT (2) */ 55 0x81, 0x06, /* INPUT (Data,Var,Rel) */ 56 57 0x06, 0x00, 0xff, /* Usage Page (Vendor Defined Page 1) */ 58 0x19, 0x01, /* USAGE_MINIMUM (1) */ 59 0x29, 0x03, /* USAGE_MAXIMUM (3) */ 60 0x15, 0x00, /* LOGICAL_MINIMUM (0) */ 61 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */ 62 0x95, 0x03, /* REPORT_COUNT (3) */ 63 0x75, 0x01, /* REPORT_SIZE (1) */ 64 0x91, 0x02, /* Output (Data,Var,Abs) */ 65 0x95, 0x01, /* REPORT_COUNT (1) */ 66 0x75, 0x05, /* REPORT_SIZE (5) */ 67 0x91, 0x01, /* Output (Cnst,Var,Abs) */ 68 69 0x06, 0x00, 0xff, /* Usage Page (Vendor Defined Page 1) */ 70 0x19, 0x06, /* USAGE_MINIMUM (6) */ 71 0x29, 0x08, /* USAGE_MAXIMUM (8) */ 72 0x15, 0x00, /* LOGICAL_MINIMUM (0) */ 73 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */ 74 0x95, 0x03, /* REPORT_COUNT (3) */ 75 0x75, 0x01, /* REPORT_SIZE (1) */ 76 0xb1, 0x02, /* Feature (Data,Var,Abs) */ 77 0x95, 0x01, /* REPORT_COUNT (1) */ 78 0x75, 0x05, /* REPORT_SIZE (5) */ 79 0x91, 0x01, /* Output (Cnst,Var,Abs) */ 80 81 0xc0, /* END_COLLECTION */ 82 0xc0, /* END_COLLECTION */ 83 }; 84 85 static __u8 feature_data[] = { 1, 2 }; 86 87 struct attach_prog_args { 88 int prog_fd; 89 unsigned int hid; 90 int retval; 91 int insert_head; 92 }; 93 94 struct hid_hw_request_syscall_args { 95 __u8 data[10]; 96 unsigned int hid; 97 int retval; 98 size_t size; 99 enum hid_report_type type; 100 __u8 request_type; 101 }; 102 103 #define ASSERT_OK(data) ASSERT_FALSE(data) 104 #define ASSERT_OK_PTR(ptr) ASSERT_NE(NULL, ptr) 105 106 #define UHID_LOG(fmt, ...) do { \ 107 if (SHOW_UHID_DEBUG) \ 108 TH_LOG(fmt, ##__VA_ARGS__); \ 109 } while (0) 110 111 static pthread_mutex_t uhid_started_mtx = PTHREAD_MUTEX_INITIALIZER; 112 static pthread_cond_t uhid_started = PTHREAD_COND_INITIALIZER; 113 114 /* no need to protect uhid_stopped, only one thread accesses it */ 115 static bool uhid_stopped; 116 117 static int uhid_write(struct __test_metadata *_metadata, int fd, const struct uhid_event *ev) 118 { 119 ssize_t ret; 120 121 ret = write(fd, ev, sizeof(*ev)); 122 if (ret < 0) { 123 TH_LOG("Cannot write to uhid: %m"); 124 return -errno; 125 } else if (ret != sizeof(*ev)) { 126 TH_LOG("Wrong size written to uhid: %zd != %zu", 127 ret, sizeof(ev)); 128 return -EFAULT; 129 } else { 130 return 0; 131 } 132 } 133 134 static int uhid_create(struct __test_metadata *_metadata, int fd, int rand_nb) 135 { 136 struct uhid_event ev; 137 char buf[25]; 138 139 sprintf(buf, "test-uhid-device-%d", rand_nb); 140 141 memset(&ev, 0, sizeof(ev)); 142 ev.type = UHID_CREATE; 143 strcpy((char *)ev.u.create.name, buf); 144 ev.u.create.rd_data = rdesc; 145 ev.u.create.rd_size = sizeof(rdesc); 146 ev.u.create.bus = BUS_USB; 147 ev.u.create.vendor = 0x0001; 148 ev.u.create.product = 0x0a37; 149 ev.u.create.version = 0; 150 ev.u.create.country = 0; 151 152 sprintf(buf, "%d", rand_nb); 153 strcpy((char *)ev.u.create.phys, buf); 154 155 return uhid_write(_metadata, fd, &ev); 156 } 157 158 static void uhid_destroy(struct __test_metadata *_metadata, int fd) 159 { 160 struct uhid_event ev; 161 162 memset(&ev, 0, sizeof(ev)); 163 ev.type = UHID_DESTROY; 164 165 uhid_write(_metadata, fd, &ev); 166 } 167 168 static int uhid_event(struct __test_metadata *_metadata, int fd) 169 { 170 struct uhid_event ev, answer; 171 ssize_t ret; 172 173 memset(&ev, 0, sizeof(ev)); 174 ret = read(fd, &ev, sizeof(ev)); 175 if (ret == 0) { 176 UHID_LOG("Read HUP on uhid-cdev"); 177 return -EFAULT; 178 } else if (ret < 0) { 179 UHID_LOG("Cannot read uhid-cdev: %m"); 180 return -errno; 181 } else if (ret != sizeof(ev)) { 182 UHID_LOG("Invalid size read from uhid-dev: %zd != %zu", 183 ret, sizeof(ev)); 184 return -EFAULT; 185 } 186 187 switch (ev.type) { 188 case UHID_START: 189 pthread_mutex_lock(&uhid_started_mtx); 190 pthread_cond_signal(&uhid_started); 191 pthread_mutex_unlock(&uhid_started_mtx); 192 193 UHID_LOG("UHID_START from uhid-dev"); 194 break; 195 case UHID_STOP: 196 uhid_stopped = true; 197 198 UHID_LOG("UHID_STOP from uhid-dev"); 199 break; 200 case UHID_OPEN: 201 UHID_LOG("UHID_OPEN from uhid-dev"); 202 break; 203 case UHID_CLOSE: 204 UHID_LOG("UHID_CLOSE from uhid-dev"); 205 break; 206 case UHID_OUTPUT: 207 UHID_LOG("UHID_OUTPUT from uhid-dev"); 208 break; 209 case UHID_GET_REPORT: 210 UHID_LOG("UHID_GET_REPORT from uhid-dev"); 211 212 answer.type = UHID_GET_REPORT_REPLY; 213 answer.u.get_report_reply.id = ev.u.get_report.id; 214 answer.u.get_report_reply.err = ev.u.get_report.rnum == 1 ? 0 : -EIO; 215 answer.u.get_report_reply.size = sizeof(feature_data); 216 memcpy(answer.u.get_report_reply.data, feature_data, sizeof(feature_data)); 217 218 uhid_write(_metadata, fd, &answer); 219 220 break; 221 case UHID_SET_REPORT: 222 UHID_LOG("UHID_SET_REPORT from uhid-dev"); 223 break; 224 default: 225 TH_LOG("Invalid event from uhid-dev: %u", ev.type); 226 } 227 228 return 0; 229 } 230 231 struct uhid_thread_args { 232 int fd; 233 struct __test_metadata *_metadata; 234 }; 235 static void *uhid_read_events_thread(void *arg) 236 { 237 struct uhid_thread_args *args = (struct uhid_thread_args *)arg; 238 struct __test_metadata *_metadata = args->_metadata; 239 struct pollfd pfds[1]; 240 int fd = args->fd; 241 int ret = 0; 242 243 pfds[0].fd = fd; 244 pfds[0].events = POLLIN; 245 246 uhid_stopped = false; 247 248 while (!uhid_stopped) { 249 ret = poll(pfds, 1, 100); 250 if (ret < 0) { 251 TH_LOG("Cannot poll for fds: %m"); 252 break; 253 } 254 if (pfds[0].revents & POLLIN) { 255 ret = uhid_event(_metadata, fd); 256 if (ret) 257 break; 258 } 259 } 260 261 return (void *)(long)ret; 262 } 263 264 static int uhid_start_listener(struct __test_metadata *_metadata, pthread_t *tid, int uhid_fd) 265 { 266 struct uhid_thread_args args = { 267 .fd = uhid_fd, 268 ._metadata = _metadata, 269 }; 270 int err; 271 272 pthread_mutex_lock(&uhid_started_mtx); 273 err = pthread_create(tid, NULL, uhid_read_events_thread, (void *)&args); 274 ASSERT_EQ(0, err) { 275 TH_LOG("Could not start the uhid thread: %d", err); 276 pthread_mutex_unlock(&uhid_started_mtx); 277 close(uhid_fd); 278 return -EIO; 279 } 280 pthread_cond_wait(&uhid_started, &uhid_started_mtx); 281 pthread_mutex_unlock(&uhid_started_mtx); 282 283 return 0; 284 } 285 286 static int uhid_send_event(struct __test_metadata *_metadata, int fd, __u8 *buf, size_t size) 287 { 288 struct uhid_event ev; 289 290 if (size > sizeof(ev.u.input.data)) 291 return -E2BIG; 292 293 memset(&ev, 0, sizeof(ev)); 294 ev.type = UHID_INPUT2; 295 ev.u.input2.size = size; 296 297 memcpy(ev.u.input2.data, buf, size); 298 299 return uhid_write(_metadata, fd, &ev); 300 } 301 302 static int setup_uhid(struct __test_metadata *_metadata, int rand_nb) 303 { 304 int fd; 305 const char *path = "/dev/uhid"; 306 int ret; 307 308 fd = open(path, O_RDWR | O_CLOEXEC); 309 ASSERT_GE(fd, 0) TH_LOG("open uhid-cdev failed; %d", fd); 310 311 ret = uhid_create(_metadata, fd, rand_nb); 312 ASSERT_EQ(0, ret) { 313 TH_LOG("create uhid device failed: %d", ret); 314 close(fd); 315 } 316 317 return fd; 318 } 319 320 static bool match_sysfs_device(int dev_id, const char *workdir, struct dirent *dir) 321 { 322 const char *target = "0003:0001:0A37.*"; 323 char phys[512]; 324 char uevent[1024]; 325 char temp[512]; 326 int fd, nread; 327 bool found = false; 328 329 if (fnmatch(target, dir->d_name, 0)) 330 return false; 331 332 /* we found the correct VID/PID, now check for phys */ 333 sprintf(uevent, "%s/%s/uevent", workdir, dir->d_name); 334 335 fd = open(uevent, O_RDONLY | O_NONBLOCK); 336 if (fd < 0) 337 return false; 338 339 sprintf(phys, "PHYS=%d", dev_id); 340 341 nread = read(fd, temp, ARRAY_SIZE(temp)); 342 if (nread > 0 && (strstr(temp, phys)) != NULL) 343 found = true; 344 345 close(fd); 346 347 return found; 348 } 349 350 static int get_hid_id(int dev_id) 351 { 352 const char *workdir = "/sys/devices/virtual/misc/uhid"; 353 const char *str_id; 354 DIR *d; 355 struct dirent *dir; 356 int found = -1, attempts = 3; 357 358 /* it would be nice to be able to use nftw, but the no_alu32 target doesn't support it */ 359 360 while (found < 0 && attempts > 0) { 361 attempts--; 362 d = opendir(workdir); 363 if (d) { 364 while ((dir = readdir(d)) != NULL) { 365 if (!match_sysfs_device(dev_id, workdir, dir)) 366 continue; 367 368 str_id = dir->d_name + sizeof("0003:0001:0A37."); 369 found = (int)strtol(str_id, NULL, 16); 370 371 break; 372 } 373 closedir(d); 374 } 375 if (found < 0) 376 usleep(100000); 377 } 378 379 return found; 380 } 381 382 static int get_hidraw(int dev_id) 383 { 384 const char *workdir = "/sys/devices/virtual/misc/uhid"; 385 char sysfs[1024]; 386 DIR *d, *subd; 387 struct dirent *dir, *subdir; 388 int i, found = -1; 389 390 /* retry 5 times in case the system is loaded */ 391 for (i = 5; i > 0; i--) { 392 usleep(10); 393 d = opendir(workdir); 394 395 if (!d) 396 continue; 397 398 while ((dir = readdir(d)) != NULL) { 399 if (!match_sysfs_device(dev_id, workdir, dir)) 400 continue; 401 402 sprintf(sysfs, "%s/%s/hidraw", workdir, dir->d_name); 403 404 subd = opendir(sysfs); 405 if (!subd) 406 continue; 407 408 while ((subdir = readdir(subd)) != NULL) { 409 if (fnmatch("hidraw*", subdir->d_name, 0)) 410 continue; 411 412 found = atoi(subdir->d_name + strlen("hidraw")); 413 } 414 415 closedir(subd); 416 417 if (found > 0) 418 break; 419 } 420 closedir(d); 421 } 422 423 return found; 424 } 425 426 static int open_hidraw(int dev_id) 427 { 428 int hidraw_number; 429 char hidraw_path[64] = { 0 }; 430 431 hidraw_number = get_hidraw(dev_id); 432 if (hidraw_number < 0) 433 return hidraw_number; 434 435 /* open hidraw node to check the other side of the pipe */ 436 sprintf(hidraw_path, "/dev/hidraw%d", hidraw_number); 437 return open(hidraw_path, O_RDWR | O_NONBLOCK); 438 } 439 440 FIXTURE(hid_bpf) { 441 int dev_id; 442 int uhid_fd; 443 int hidraw_fd; 444 int hid_id; 445 pthread_t tid; 446 struct hid *skel; 447 }; 448 static void detach_bpf(FIXTURE_DATA(hid_bpf) * self) 449 { 450 if (self->hidraw_fd) 451 close(self->hidraw_fd); 452 self->hidraw_fd = 0; 453 454 hid__destroy(self->skel); 455 self->skel = NULL; 456 } 457 458 FIXTURE_TEARDOWN(hid_bpf) { 459 void *uhid_err; 460 461 uhid_destroy(_metadata, self->uhid_fd); 462 463 detach_bpf(self); 464 pthread_join(self->tid, &uhid_err); 465 } 466 #define TEARDOWN_LOG(fmt, ...) do { \ 467 TH_LOG(fmt, ##__VA_ARGS__); \ 468 hid_bpf_teardown(_metadata, self, variant); \ 469 } while (0) 470 471 FIXTURE_SETUP(hid_bpf) 472 { 473 time_t t; 474 int err; 475 476 /* initialize random number generator */ 477 srand((unsigned int)time(&t)); 478 479 self->dev_id = rand() % 1024; 480 481 self->uhid_fd = setup_uhid(_metadata, self->dev_id); 482 483 /* locate the uev, self, variant);ent file of the created device */ 484 self->hid_id = get_hid_id(self->dev_id); 485 ASSERT_GT(self->hid_id, 0) 486 TEARDOWN_LOG("Could not locate uhid device id: %d", self->hid_id); 487 488 err = uhid_start_listener(_metadata, &self->tid, self->uhid_fd); 489 ASSERT_EQ(0, err) TEARDOWN_LOG("could not start udev listener: %d", err); 490 } 491 492 struct test_program { 493 const char *name; 494 int insert_head; 495 }; 496 #define LOAD_PROGRAMS(progs) \ 497 load_programs(progs, ARRAY_SIZE(progs), _metadata, self, variant) 498 #define LOAD_BPF \ 499 load_programs(NULL, 0, _metadata, self, variant) 500 static void load_programs(const struct test_program programs[], 501 const size_t progs_count, 502 struct __test_metadata *_metadata, 503 FIXTURE_DATA(hid_bpf) * self, 504 const FIXTURE_VARIANT(hid_bpf) * variant) 505 { 506 int attach_fd, err = -EINVAL; 507 struct attach_prog_args args = { 508 .retval = -1, 509 }; 510 DECLARE_LIBBPF_OPTS(bpf_test_run_opts, tattr, 511 .ctx_in = &args, 512 .ctx_size_in = sizeof(args), 513 ); 514 515 /* open the bpf file */ 516 self->skel = hid__open(); 517 ASSERT_OK_PTR(self->skel) TEARDOWN_LOG("Error while calling hid__open"); 518 519 for (int i = 0; i < progs_count; i++) { 520 struct bpf_program *prog; 521 522 prog = bpf_object__find_program_by_name(*self->skel->skeleton->obj, 523 programs[i].name); 524 ASSERT_OK_PTR(prog) TH_LOG("can not find program by name '%s'", programs[i].name); 525 526 bpf_program__set_autoload(prog, true); 527 } 528 529 err = hid__load(self->skel); 530 ASSERT_OK(err) TH_LOG("hid_skel_load failed: %d", err); 531 532 attach_fd = bpf_program__fd(self->skel->progs.attach_prog); 533 ASSERT_GE(attach_fd, 0) TH_LOG("locate attach_prog: %d", attach_fd); 534 535 for (int i = 0; i < progs_count; i++) { 536 struct bpf_program *prog; 537 538 prog = bpf_object__find_program_by_name(*self->skel->skeleton->obj, 539 programs[i].name); 540 ASSERT_OK_PTR(prog) TH_LOG("can not find program by name '%s'", programs[i].name); 541 542 args.prog_fd = bpf_program__fd(prog); 543 args.hid = self->hid_id; 544 args.insert_head = programs[i].insert_head; 545 err = bpf_prog_test_run_opts(attach_fd, &tattr); 546 ASSERT_OK(args.retval) TH_LOG("attach_hid(%s): %d", programs[i].name, args.retval); 547 } 548 549 self->hidraw_fd = open_hidraw(self->dev_id); 550 ASSERT_GE(self->hidraw_fd, 0) TH_LOG("open_hidraw"); 551 } 552 553 /* 554 * A simple test to see if the fixture is working fine. 555 * If this fails, none of the other tests will pass. 556 */ 557 TEST_F(hid_bpf, test_create_uhid) 558 { 559 } 560 561 /* 562 * Attach hid_first_event to the given uhid device, 563 * retrieve and open the matching hidraw node, 564 * inject one event in the uhid device, 565 * check that the program sees it and can change the data 566 */ 567 TEST_F(hid_bpf, raw_event) 568 { 569 const struct test_program progs[] = { 570 { .name = "hid_first_event" }, 571 }; 572 __u8 buf[10] = {0}; 573 int err; 574 575 LOAD_PROGRAMS(progs); 576 577 /* check that the program is correctly loaded */ 578 ASSERT_EQ(self->skel->data->callback_check, 52) TH_LOG("callback_check1"); 579 ASSERT_EQ(self->skel->data->callback2_check, 52) TH_LOG("callback2_check1"); 580 581 /* inject one event */ 582 buf[0] = 1; 583 buf[1] = 42; 584 uhid_send_event(_metadata, self->uhid_fd, buf, 6); 585 586 /* check that hid_first_event() was executed */ 587 ASSERT_EQ(self->skel->data->callback_check, 42) TH_LOG("callback_check1"); 588 589 /* read the data from hidraw */ 590 memset(buf, 0, sizeof(buf)); 591 err = read(self->hidraw_fd, buf, sizeof(buf)); 592 ASSERT_EQ(err, 6) TH_LOG("read_hidraw"); 593 ASSERT_EQ(buf[0], 1); 594 ASSERT_EQ(buf[2], 47); 595 596 /* inject another event */ 597 memset(buf, 0, sizeof(buf)); 598 buf[0] = 1; 599 buf[1] = 47; 600 uhid_send_event(_metadata, self->uhid_fd, buf, 6); 601 602 /* check that hid_first_event() was executed */ 603 ASSERT_EQ(self->skel->data->callback_check, 47) TH_LOG("callback_check1"); 604 605 /* read the data from hidraw */ 606 memset(buf, 0, sizeof(buf)); 607 err = read(self->hidraw_fd, buf, sizeof(buf)); 608 ASSERT_EQ(err, 6) TH_LOG("read_hidraw"); 609 ASSERT_EQ(buf[2], 52); 610 } 611 612 /* 613 * Ensures that we can attach/detach programs 614 */ 615 TEST_F(hid_bpf, test_attach_detach) 616 { 617 const struct test_program progs[] = { 618 { .name = "hid_first_event" }, 619 { .name = "hid_second_event" }, 620 }; 621 __u8 buf[10] = {0}; 622 int err; 623 624 LOAD_PROGRAMS(progs); 625 626 /* inject one event */ 627 buf[0] = 1; 628 buf[1] = 42; 629 uhid_send_event(_metadata, self->uhid_fd, buf, 6); 630 631 /* read the data from hidraw */ 632 memset(buf, 0, sizeof(buf)); 633 err = read(self->hidraw_fd, buf, sizeof(buf)); 634 ASSERT_EQ(err, 6) TH_LOG("read_hidraw"); 635 ASSERT_EQ(buf[0], 1); 636 ASSERT_EQ(buf[2], 47); 637 638 /* make sure both programs are run */ 639 ASSERT_EQ(buf[3], 52); 640 641 /* pin the first program and immediately unpin it */ 642 #define PIN_PATH "/sys/fs/bpf/hid_first_event" 643 bpf_program__pin(self->skel->progs.hid_first_event, PIN_PATH); 644 remove(PIN_PATH); 645 #undef PIN_PATH 646 usleep(100000); 647 648 /* detach the program */ 649 detach_bpf(self); 650 651 self->hidraw_fd = open_hidraw(self->dev_id); 652 ASSERT_GE(self->hidraw_fd, 0) TH_LOG("open_hidraw"); 653 654 /* inject another event */ 655 memset(buf, 0, sizeof(buf)); 656 buf[0] = 1; 657 buf[1] = 47; 658 uhid_send_event(_metadata, self->uhid_fd, buf, 6); 659 660 /* read the data from hidraw */ 661 memset(buf, 0, sizeof(buf)); 662 err = read(self->hidraw_fd, buf, sizeof(buf)); 663 ASSERT_EQ(err, 6) TH_LOG("read_hidraw_no_bpf"); 664 ASSERT_EQ(buf[0], 1); 665 ASSERT_EQ(buf[1], 47); 666 ASSERT_EQ(buf[2], 0); 667 ASSERT_EQ(buf[3], 0); 668 669 /* re-attach our program */ 670 671 LOAD_PROGRAMS(progs); 672 673 /* inject one event */ 674 memset(buf, 0, sizeof(buf)); 675 buf[0] = 1; 676 buf[1] = 42; 677 uhid_send_event(_metadata, self->uhid_fd, buf, 6); 678 679 /* read the data from hidraw */ 680 memset(buf, 0, sizeof(buf)); 681 err = read(self->hidraw_fd, buf, sizeof(buf)); 682 ASSERT_EQ(err, 6) TH_LOG("read_hidraw"); 683 ASSERT_EQ(buf[0], 1); 684 ASSERT_EQ(buf[2], 47); 685 ASSERT_EQ(buf[3], 52); 686 } 687 688 /* 689 * Attach hid_change_report_id to the given uhid device, 690 * retrieve and open the matching hidraw node, 691 * inject one event in the uhid device, 692 * check that the program sees it and can change the data 693 */ 694 TEST_F(hid_bpf, test_hid_change_report) 695 { 696 const struct test_program progs[] = { 697 { .name = "hid_change_report_id" }, 698 }; 699 __u8 buf[10] = {0}; 700 int err; 701 702 LOAD_PROGRAMS(progs); 703 704 /* inject one event */ 705 buf[0] = 1; 706 buf[1] = 42; 707 uhid_send_event(_metadata, self->uhid_fd, buf, 6); 708 709 /* read the data from hidraw */ 710 memset(buf, 0, sizeof(buf)); 711 err = read(self->hidraw_fd, buf, sizeof(buf)); 712 ASSERT_EQ(err, 9) TH_LOG("read_hidraw"); 713 ASSERT_EQ(buf[0], 2); 714 ASSERT_EQ(buf[1], 42); 715 ASSERT_EQ(buf[2], 0) TH_LOG("leftovers_from_previous_test"); 716 } 717 718 /* 719 * Attach hid_user_raw_request to the given uhid device, 720 * call the bpf program from userspace 721 * check that the program is called and does the expected. 722 */ 723 TEST_F(hid_bpf, test_hid_user_raw_request_call) 724 { 725 struct hid_hw_request_syscall_args args = { 726 .retval = -1, 727 .type = HID_FEATURE_REPORT, 728 .request_type = HID_REQ_GET_REPORT, 729 .size = 10, 730 }; 731 DECLARE_LIBBPF_OPTS(bpf_test_run_opts, tattrs, 732 .ctx_in = &args, 733 .ctx_size_in = sizeof(args), 734 ); 735 int err, prog_fd; 736 737 LOAD_BPF; 738 739 args.hid = self->hid_id; 740 args.data[0] = 1; /* report ID */ 741 742 prog_fd = bpf_program__fd(self->skel->progs.hid_user_raw_request); 743 744 err = bpf_prog_test_run_opts(prog_fd, &tattrs); 745 ASSERT_OK(err) TH_LOG("error while calling bpf_prog_test_run_opts"); 746 747 ASSERT_EQ(args.retval, 2); 748 749 ASSERT_EQ(args.data[1], 2); 750 } 751 752 /* 753 * Attach hid_insert{0,1,2} to the given uhid device, 754 * retrieve and open the matching hidraw node, 755 * inject one event in the uhid device, 756 * check that the programs have been inserted in the correct order. 757 */ 758 TEST_F(hid_bpf, test_hid_attach_flags) 759 { 760 const struct test_program progs[] = { 761 { 762 .name = "hid_test_insert2", 763 .insert_head = 0, 764 }, 765 { 766 .name = "hid_test_insert1", 767 .insert_head = 1, 768 }, 769 { 770 .name = "hid_test_insert3", 771 .insert_head = 0, 772 }, 773 }; 774 __u8 buf[10] = {0}; 775 int err; 776 777 LOAD_PROGRAMS(progs); 778 779 /* inject one event */ 780 buf[0] = 1; 781 uhid_send_event(_metadata, self->uhid_fd, buf, 6); 782 783 /* read the data from hidraw */ 784 memset(buf, 0, sizeof(buf)); 785 err = read(self->hidraw_fd, buf, sizeof(buf)); 786 ASSERT_EQ(err, 6) TH_LOG("read_hidraw"); 787 ASSERT_EQ(buf[1], 1); 788 ASSERT_EQ(buf[2], 2); 789 ASSERT_EQ(buf[3], 3); 790 } 791 792 /* 793 * Attach hid_rdesc_fixup to the given uhid device, 794 * retrieve and open the matching hidraw node, 795 * check that the hidraw report descriptor has been updated. 796 */ 797 TEST_F(hid_bpf, test_rdesc_fixup) 798 { 799 struct hidraw_report_descriptor rpt_desc = {0}; 800 const struct test_program progs[] = { 801 { .name = "hid_rdesc_fixup" }, 802 }; 803 int err, desc_size; 804 805 LOAD_PROGRAMS(progs); 806 807 /* check that hid_rdesc_fixup() was executed */ 808 ASSERT_EQ(self->skel->data->callback2_check, 0x21); 809 810 /* read the exposed report descriptor from hidraw */ 811 err = ioctl(self->hidraw_fd, HIDIOCGRDESCSIZE, &desc_size); 812 ASSERT_GE(err, 0) TH_LOG("error while reading HIDIOCGRDESCSIZE: %d", err); 813 814 /* ensure the new size of the rdesc is bigger than the old one */ 815 ASSERT_GT(desc_size, sizeof(rdesc)); 816 817 rpt_desc.size = desc_size; 818 err = ioctl(self->hidraw_fd, HIDIOCGRDESC, &rpt_desc); 819 ASSERT_GE(err, 0) TH_LOG("error while reading HIDIOCGRDESC: %d", err); 820 821 ASSERT_EQ(rpt_desc.value[4], 0x42); 822 } 823 824 static int libbpf_print_fn(enum libbpf_print_level level, 825 const char *format, va_list args) 826 { 827 char buf[1024]; 828 829 if (level == LIBBPF_DEBUG) 830 return 0; 831 832 snprintf(buf, sizeof(buf), "# %s", format); 833 834 vfprintf(stdout, buf, args); 835 return 0; 836 } 837 838 static void __attribute__((constructor)) __constructor_order_last(void) 839 { 840 if (!__constructor_order) 841 __constructor_order = _CONSTRUCTOR_ORDER_BACKWARD; 842 } 843 844 int main(int argc, char **argv) 845 { 846 /* Use libbpf 1.0 API mode */ 847 libbpf_set_strict_mode(LIBBPF_STRICT_ALL); 848 libbpf_set_print(libbpf_print_fn); 849 850 return test_harness_run(argc, argv); 851 } 852