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 int 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 close(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 attach_fd, err = -EINVAL; 531 struct attach_prog_args args = { 532 .retval = -1, 533 }; 534 DECLARE_LIBBPF_OPTS(bpf_test_run_opts, tattr, 535 .ctx_in = &args, 536 .ctx_size_in = sizeof(args), 537 ); 538 539 ASSERT_LE(progs_count, ARRAY_SIZE(self->hid_links)) 540 TH_LOG("too many programs are to be loaded"); 541 542 /* open the bpf file */ 543 self->skel = hid__open(); 544 ASSERT_OK_PTR(self->skel) TEARDOWN_LOG("Error while calling hid__open"); 545 546 for (int i = 0; i < progs_count; i++) { 547 struct bpf_program *prog; 548 549 prog = bpf_object__find_program_by_name(*self->skel->skeleton->obj, 550 programs[i].name); 551 ASSERT_OK_PTR(prog) TH_LOG("can not find program by name '%s'", programs[i].name); 552 553 bpf_program__set_autoload(prog, true); 554 } 555 556 err = hid__load(self->skel); 557 ASSERT_OK(err) TH_LOG("hid_skel_load failed: %d", err); 558 559 attach_fd = bpf_program__fd(self->skel->progs.attach_prog); 560 ASSERT_GE(attach_fd, 0) TH_LOG("locate attach_prog: %d", attach_fd); 561 562 for (int i = 0; i < progs_count; i++) { 563 struct bpf_program *prog; 564 565 prog = bpf_object__find_program_by_name(*self->skel->skeleton->obj, 566 programs[i].name); 567 ASSERT_OK_PTR(prog) TH_LOG("can not find program by name '%s'", programs[i].name); 568 569 args.prog_fd = bpf_program__fd(prog); 570 args.hid = self->hid_id; 571 args.insert_head = programs[i].insert_head; 572 err = bpf_prog_test_run_opts(attach_fd, &tattr); 573 ASSERT_GE(args.retval, 0) 574 TH_LOG("attach_hid(%s): %d", programs[i].name, args.retval); 575 576 self->hid_links[i] = args.retval; 577 } 578 579 self->hidraw_fd = open_hidraw(self->dev_id); 580 ASSERT_GE(self->hidraw_fd, 0) TH_LOG("open_hidraw"); 581 } 582 583 /* 584 * A simple test to see if the fixture is working fine. 585 * If this fails, none of the other tests will pass. 586 */ 587 TEST_F(hid_bpf, test_create_uhid) 588 { 589 } 590 591 /* 592 * Attach hid_first_event to the given uhid device, 593 * retrieve and open the matching hidraw node, 594 * inject one event in the uhid device, 595 * check that the program sees it and can change the data 596 */ 597 TEST_F(hid_bpf, raw_event) 598 { 599 const struct test_program progs[] = { 600 { .name = "hid_first_event" }, 601 }; 602 __u8 buf[10] = {0}; 603 int err; 604 605 LOAD_PROGRAMS(progs); 606 607 /* check that the program is correctly loaded */ 608 ASSERT_EQ(self->skel->data->callback_check, 52) TH_LOG("callback_check1"); 609 ASSERT_EQ(self->skel->data->callback2_check, 52) TH_LOG("callback2_check1"); 610 611 /* inject one event */ 612 buf[0] = 1; 613 buf[1] = 42; 614 uhid_send_event(_metadata, self->uhid_fd, buf, 6); 615 616 /* check that hid_first_event() was executed */ 617 ASSERT_EQ(self->skel->data->callback_check, 42) TH_LOG("callback_check1"); 618 619 /* read the data from hidraw */ 620 memset(buf, 0, sizeof(buf)); 621 err = read(self->hidraw_fd, buf, sizeof(buf)); 622 ASSERT_EQ(err, 6) TH_LOG("read_hidraw"); 623 ASSERT_EQ(buf[0], 1); 624 ASSERT_EQ(buf[2], 47); 625 626 /* inject another event */ 627 memset(buf, 0, sizeof(buf)); 628 buf[0] = 1; 629 buf[1] = 47; 630 uhid_send_event(_metadata, self->uhid_fd, buf, 6); 631 632 /* check that hid_first_event() was executed */ 633 ASSERT_EQ(self->skel->data->callback_check, 47) TH_LOG("callback_check1"); 634 635 /* read the data from hidraw */ 636 memset(buf, 0, sizeof(buf)); 637 err = read(self->hidraw_fd, buf, sizeof(buf)); 638 ASSERT_EQ(err, 6) TH_LOG("read_hidraw"); 639 ASSERT_EQ(buf[2], 52); 640 } 641 642 /* 643 * Ensures that we can attach/detach programs 644 */ 645 TEST_F(hid_bpf, test_attach_detach) 646 { 647 const struct test_program progs[] = { 648 { .name = "hid_first_event" }, 649 { .name = "hid_second_event" }, 650 }; 651 __u8 buf[10] = {0}; 652 int err, link; 653 654 LOAD_PROGRAMS(progs); 655 656 link = self->hid_links[0]; 657 ASSERT_GT(link, 0) TH_LOG("HID-BPF link not created"); 658 659 /* inject one event */ 660 buf[0] = 1; 661 buf[1] = 42; 662 uhid_send_event(_metadata, self->uhid_fd, buf, 6); 663 664 /* read the data from hidraw */ 665 memset(buf, 0, sizeof(buf)); 666 err = read(self->hidraw_fd, buf, sizeof(buf)); 667 ASSERT_EQ(err, 6) TH_LOG("read_hidraw"); 668 ASSERT_EQ(buf[0], 1); 669 ASSERT_EQ(buf[2], 47); 670 671 /* make sure both programs are run */ 672 ASSERT_EQ(buf[3], 52); 673 674 /* pin the first program and immediately unpin it */ 675 #define PIN_PATH "/sys/fs/bpf/hid_first_event" 676 err = bpf_obj_pin(link, PIN_PATH); 677 ASSERT_OK(err) TH_LOG("error while calling bpf_obj_pin"); 678 remove(PIN_PATH); 679 #undef PIN_PATH 680 usleep(100000); 681 682 /* detach the program */ 683 detach_bpf(self); 684 685 self->hidraw_fd = open_hidraw(self->dev_id); 686 ASSERT_GE(self->hidraw_fd, 0) TH_LOG("open_hidraw"); 687 688 /* inject another event */ 689 memset(buf, 0, sizeof(buf)); 690 buf[0] = 1; 691 buf[1] = 47; 692 uhid_send_event(_metadata, self->uhid_fd, buf, 6); 693 694 /* read the data from hidraw */ 695 memset(buf, 0, sizeof(buf)); 696 err = read(self->hidraw_fd, buf, sizeof(buf)); 697 ASSERT_EQ(err, 6) TH_LOG("read_hidraw_no_bpf"); 698 ASSERT_EQ(buf[0], 1); 699 ASSERT_EQ(buf[1], 47); 700 ASSERT_EQ(buf[2], 0); 701 ASSERT_EQ(buf[3], 0); 702 703 /* re-attach our program */ 704 705 LOAD_PROGRAMS(progs); 706 707 /* inject one event */ 708 memset(buf, 0, sizeof(buf)); 709 buf[0] = 1; 710 buf[1] = 42; 711 uhid_send_event(_metadata, self->uhid_fd, buf, 6); 712 713 /* read the data from hidraw */ 714 memset(buf, 0, sizeof(buf)); 715 err = read(self->hidraw_fd, buf, sizeof(buf)); 716 ASSERT_EQ(err, 6) TH_LOG("read_hidraw"); 717 ASSERT_EQ(buf[0], 1); 718 ASSERT_EQ(buf[2], 47); 719 ASSERT_EQ(buf[3], 52); 720 } 721 722 /* 723 * Attach hid_change_report_id to the given uhid device, 724 * retrieve and open the matching hidraw node, 725 * inject one event in the uhid device, 726 * check that the program sees it and can change the data 727 */ 728 TEST_F(hid_bpf, test_hid_change_report) 729 { 730 const struct test_program progs[] = { 731 { .name = "hid_change_report_id" }, 732 }; 733 __u8 buf[10] = {0}; 734 int err; 735 736 LOAD_PROGRAMS(progs); 737 738 /* inject one event */ 739 buf[0] = 1; 740 buf[1] = 42; 741 uhid_send_event(_metadata, self->uhid_fd, buf, 6); 742 743 /* read the data from hidraw */ 744 memset(buf, 0, sizeof(buf)); 745 err = read(self->hidraw_fd, buf, sizeof(buf)); 746 ASSERT_EQ(err, 9) TH_LOG("read_hidraw"); 747 ASSERT_EQ(buf[0], 2); 748 ASSERT_EQ(buf[1], 42); 749 ASSERT_EQ(buf[2], 0) TH_LOG("leftovers_from_previous_test"); 750 } 751 752 /* 753 * Call hid_bpf_input_report against the given uhid device, 754 * check that the program is called and does the expected. 755 */ 756 TEST_F(hid_bpf, test_hid_user_input_report_call) 757 { 758 struct hid_hw_request_syscall_args args = { 759 .retval = -1, 760 .size = 10, 761 }; 762 DECLARE_LIBBPF_OPTS(bpf_test_run_opts, tattrs, 763 .ctx_in = &args, 764 .ctx_size_in = sizeof(args), 765 ); 766 __u8 buf[10] = {0}; 767 int err, prog_fd; 768 769 LOAD_BPF; 770 771 args.hid = self->hid_id; 772 args.data[0] = 1; /* report ID */ 773 args.data[1] = 2; /* report ID */ 774 args.data[2] = 42; /* report ID */ 775 776 prog_fd = bpf_program__fd(self->skel->progs.hid_user_input_report); 777 778 /* check that there is no data to read from hidraw */ 779 memset(buf, 0, sizeof(buf)); 780 err = read(self->hidraw_fd, buf, sizeof(buf)); 781 ASSERT_EQ(err, -1) TH_LOG("read_hidraw"); 782 783 err = bpf_prog_test_run_opts(prog_fd, &tattrs); 784 785 ASSERT_OK(err) TH_LOG("error while calling bpf_prog_test_run_opts"); 786 787 ASSERT_EQ(args.retval, 0); 788 789 /* read the data from hidraw */ 790 memset(buf, 0, sizeof(buf)); 791 err = read(self->hidraw_fd, buf, sizeof(buf)); 792 ASSERT_EQ(err, 6) TH_LOG("read_hidraw"); 793 ASSERT_EQ(buf[0], 1); 794 ASSERT_EQ(buf[1], 2); 795 ASSERT_EQ(buf[2], 42); 796 } 797 798 /* 799 * Call hid_bpf_hw_output_report against the given uhid device, 800 * check that the program is called and does the expected. 801 */ 802 TEST_F(hid_bpf, test_hid_user_output_report_call) 803 { 804 struct hid_hw_request_syscall_args args = { 805 .retval = -1, 806 .size = 10, 807 }; 808 DECLARE_LIBBPF_OPTS(bpf_test_run_opts, tattrs, 809 .ctx_in = &args, 810 .ctx_size_in = sizeof(args), 811 ); 812 int err, cond_err, prog_fd; 813 struct timespec time_to_wait; 814 815 LOAD_BPF; 816 817 args.hid = self->hid_id; 818 args.data[0] = 1; /* report ID */ 819 args.data[1] = 2; /* report ID */ 820 args.data[2] = 42; /* report ID */ 821 822 prog_fd = bpf_program__fd(self->skel->progs.hid_user_output_report); 823 824 pthread_mutex_lock(&uhid_output_mtx); 825 826 memset(output_report, 0, sizeof(output_report)); 827 clock_gettime(CLOCK_REALTIME, &time_to_wait); 828 time_to_wait.tv_sec += 2; 829 830 err = bpf_prog_test_run_opts(prog_fd, &tattrs); 831 cond_err = pthread_cond_timedwait(&uhid_output_cond, &uhid_output_mtx, &time_to_wait); 832 833 ASSERT_OK(err) TH_LOG("error while calling bpf_prog_test_run_opts"); 834 ASSERT_OK(cond_err) TH_LOG("error while calling waiting for the condition"); 835 836 ASSERT_EQ(args.retval, 3); 837 838 ASSERT_EQ(output_report[0], 1); 839 ASSERT_EQ(output_report[1], 2); 840 ASSERT_EQ(output_report[2], 42); 841 842 pthread_mutex_unlock(&uhid_output_mtx); 843 } 844 845 /* 846 * Call hid_hw_raw_request against the given uhid device, 847 * check that the program is called and does the expected. 848 */ 849 TEST_F(hid_bpf, test_hid_user_raw_request_call) 850 { 851 struct hid_hw_request_syscall_args args = { 852 .retval = -1, 853 .type = HID_FEATURE_REPORT, 854 .request_type = HID_REQ_GET_REPORT, 855 .size = 10, 856 }; 857 DECLARE_LIBBPF_OPTS(bpf_test_run_opts, tattrs, 858 .ctx_in = &args, 859 .ctx_size_in = sizeof(args), 860 ); 861 int err, prog_fd; 862 863 LOAD_BPF; 864 865 args.hid = self->hid_id; 866 args.data[0] = 1; /* report ID */ 867 868 prog_fd = bpf_program__fd(self->skel->progs.hid_user_raw_request); 869 870 err = bpf_prog_test_run_opts(prog_fd, &tattrs); 871 ASSERT_OK(err) TH_LOG("error while calling bpf_prog_test_run_opts"); 872 873 ASSERT_EQ(args.retval, 2); 874 875 ASSERT_EQ(args.data[1], 2); 876 } 877 878 /* 879 * Attach hid_insert{0,1,2} to the given uhid device, 880 * retrieve and open the matching hidraw node, 881 * inject one event in the uhid device, 882 * check that the programs have been inserted in the correct order. 883 */ 884 TEST_F(hid_bpf, test_hid_attach_flags) 885 { 886 const struct test_program progs[] = { 887 { 888 .name = "hid_test_insert2", 889 .insert_head = 0, 890 }, 891 { 892 .name = "hid_test_insert1", 893 .insert_head = 1, 894 }, 895 { 896 .name = "hid_test_insert3", 897 .insert_head = 0, 898 }, 899 }; 900 __u8 buf[10] = {0}; 901 int err; 902 903 LOAD_PROGRAMS(progs); 904 905 /* inject one event */ 906 buf[0] = 1; 907 uhid_send_event(_metadata, self->uhid_fd, buf, 6); 908 909 /* read the data from hidraw */ 910 memset(buf, 0, sizeof(buf)); 911 err = read(self->hidraw_fd, buf, sizeof(buf)); 912 ASSERT_EQ(err, 6) TH_LOG("read_hidraw"); 913 ASSERT_EQ(buf[1], 1); 914 ASSERT_EQ(buf[2], 2); 915 ASSERT_EQ(buf[3], 3); 916 } 917 918 /* 919 * Attach hid_rdesc_fixup to the given uhid device, 920 * retrieve and open the matching hidraw node, 921 * check that the hidraw report descriptor has been updated. 922 */ 923 TEST_F(hid_bpf, test_rdesc_fixup) 924 { 925 struct hidraw_report_descriptor rpt_desc = {0}; 926 const struct test_program progs[] = { 927 { .name = "hid_rdesc_fixup" }, 928 }; 929 int err, desc_size; 930 931 LOAD_PROGRAMS(progs); 932 933 /* check that hid_rdesc_fixup() was executed */ 934 ASSERT_EQ(self->skel->data->callback2_check, 0x21); 935 936 /* read the exposed report descriptor from hidraw */ 937 err = ioctl(self->hidraw_fd, HIDIOCGRDESCSIZE, &desc_size); 938 ASSERT_GE(err, 0) TH_LOG("error while reading HIDIOCGRDESCSIZE: %d", err); 939 940 /* ensure the new size of the rdesc is bigger than the old one */ 941 ASSERT_GT(desc_size, sizeof(rdesc)); 942 943 rpt_desc.size = desc_size; 944 err = ioctl(self->hidraw_fd, HIDIOCGRDESC, &rpt_desc); 945 ASSERT_GE(err, 0) TH_LOG("error while reading HIDIOCGRDESC: %d", err); 946 947 ASSERT_EQ(rpt_desc.value[4], 0x42); 948 } 949 950 static int libbpf_print_fn(enum libbpf_print_level level, 951 const char *format, va_list args) 952 { 953 char buf[1024]; 954 955 if (level == LIBBPF_DEBUG) 956 return 0; 957 958 snprintf(buf, sizeof(buf), "# %s", format); 959 960 vfprintf(stdout, buf, args); 961 return 0; 962 } 963 964 static void __attribute__((constructor)) __constructor_order_last(void) 965 { 966 if (!__constructor_order) 967 __constructor_order = _CONSTRUCTOR_ORDER_BACKWARD; 968 } 969 970 int main(int argc, char **argv) 971 { 972 /* Use libbpf 1.0 API mode */ 973 libbpf_set_strict_mode(LIBBPF_STRICT_ALL); 974 libbpf_set_print(libbpf_print_fn); 975 976 return test_harness_run(argc, argv); 977 } 978