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