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, 0x01, /* REPORT_ID (1) */ 26 0x06, 0x00, 0xff, /* Usage Page (Vendor Defined Page 1) */ 27 0x19, 0x01, /* USAGE_MINIMUM (1) */ 28 0x29, 0x03, /* USAGE_MAXIMUM (3) */ 29 0x15, 0x00, /* LOGICAL_MINIMUM (0) */ 30 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */ 31 0x95, 0x03, /* REPORT_COUNT (3) */ 32 0x75, 0x01, /* REPORT_SIZE (1) */ 33 0x81, 0x02, /* INPUT (Data,Var,Abs) */ 34 0x95, 0x01, /* REPORT_COUNT (1) */ 35 0x75, 0x05, /* REPORT_SIZE (5) */ 36 0x81, 0x01, /* INPUT (Cnst,Var,Abs) */ 37 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */ 38 0x09, 0x30, /* USAGE (X) */ 39 0x09, 0x31, /* USAGE (Y) */ 40 0x15, 0x81, /* LOGICAL_MINIMUM (-127) */ 41 0x25, 0x7f, /* LOGICAL_MAXIMUM (127) */ 42 0x75, 0x10, /* REPORT_SIZE (16) */ 43 0x95, 0x02, /* REPORT_COUNT (2) */ 44 0x81, 0x06, /* INPUT (Data,Var,Rel) */ 45 46 0x06, 0x00, 0xff, /* Usage Page (Vendor Defined Page 1) */ 47 0x19, 0x01, /* USAGE_MINIMUM (1) */ 48 0x29, 0x03, /* USAGE_MAXIMUM (3) */ 49 0x15, 0x00, /* LOGICAL_MINIMUM (0) */ 50 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */ 51 0x95, 0x03, /* REPORT_COUNT (3) */ 52 0x75, 0x01, /* REPORT_SIZE (1) */ 53 0x91, 0x02, /* Output (Data,Var,Abs) */ 54 0x95, 0x01, /* REPORT_COUNT (1) */ 55 0x75, 0x05, /* REPORT_SIZE (5) */ 56 0x91, 0x01, /* Output (Cnst,Var,Abs) */ 57 58 0x06, 0x00, 0xff, /* Usage Page (Vendor Defined Page 1) */ 59 0x19, 0x06, /* USAGE_MINIMUM (6) */ 60 0x29, 0x08, /* USAGE_MAXIMUM (8) */ 61 0x15, 0x00, /* LOGICAL_MINIMUM (0) */ 62 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */ 63 0x95, 0x03, /* REPORT_COUNT (3) */ 64 0x75, 0x01, /* REPORT_SIZE (1) */ 65 0xb1, 0x02, /* Feature (Data,Var,Abs) */ 66 0x95, 0x01, /* REPORT_COUNT (1) */ 67 0x75, 0x05, /* REPORT_SIZE (5) */ 68 0x91, 0x01, /* Output (Cnst,Var,Abs) */ 69 70 0xc0, /* END_COLLECTION */ 71 0xc0, /* END_COLLECTION */ 72 }; 73 74 struct attach_prog_args { 75 int prog_fd; 76 unsigned int hid; 77 int retval; 78 }; 79 80 #define ASSERT_OK(data) ASSERT_FALSE(data) 81 #define ASSERT_OK_PTR(ptr) ASSERT_NE(NULL, ptr) 82 83 #define UHID_LOG(fmt, ...) do { \ 84 if (SHOW_UHID_DEBUG) \ 85 TH_LOG(fmt, ##__VA_ARGS__); \ 86 } while (0) 87 88 static pthread_mutex_t uhid_started_mtx = PTHREAD_MUTEX_INITIALIZER; 89 static pthread_cond_t uhid_started = PTHREAD_COND_INITIALIZER; 90 91 /* no need to protect uhid_stopped, only one thread accesses it */ 92 static bool uhid_stopped; 93 94 static int uhid_write(struct __test_metadata *_metadata, int fd, const struct uhid_event *ev) 95 { 96 ssize_t ret; 97 98 ret = write(fd, ev, sizeof(*ev)); 99 if (ret < 0) { 100 TH_LOG("Cannot write to uhid: %m"); 101 return -errno; 102 } else if (ret != sizeof(*ev)) { 103 TH_LOG("Wrong size written to uhid: %zd != %zu", 104 ret, sizeof(ev)); 105 return -EFAULT; 106 } else { 107 return 0; 108 } 109 } 110 111 static int uhid_create(struct __test_metadata *_metadata, int fd, int rand_nb) 112 { 113 struct uhid_event ev; 114 char buf[25]; 115 116 sprintf(buf, "test-uhid-device-%d", rand_nb); 117 118 memset(&ev, 0, sizeof(ev)); 119 ev.type = UHID_CREATE; 120 strcpy((char *)ev.u.create.name, buf); 121 ev.u.create.rd_data = rdesc; 122 ev.u.create.rd_size = sizeof(rdesc); 123 ev.u.create.bus = BUS_USB; 124 ev.u.create.vendor = 0x0001; 125 ev.u.create.product = 0x0a37; 126 ev.u.create.version = 0; 127 ev.u.create.country = 0; 128 129 sprintf(buf, "%d", rand_nb); 130 strcpy((char *)ev.u.create.phys, buf); 131 132 return uhid_write(_metadata, fd, &ev); 133 } 134 135 static void uhid_destroy(struct __test_metadata *_metadata, int fd) 136 { 137 struct uhid_event ev; 138 139 memset(&ev, 0, sizeof(ev)); 140 ev.type = UHID_DESTROY; 141 142 uhid_write(_metadata, fd, &ev); 143 } 144 145 static int uhid_event(struct __test_metadata *_metadata, int fd) 146 { 147 struct uhid_event ev; 148 ssize_t ret; 149 150 memset(&ev, 0, sizeof(ev)); 151 ret = read(fd, &ev, sizeof(ev)); 152 if (ret == 0) { 153 UHID_LOG("Read HUP on uhid-cdev"); 154 return -EFAULT; 155 } else if (ret < 0) { 156 UHID_LOG("Cannot read uhid-cdev: %m"); 157 return -errno; 158 } else if (ret != sizeof(ev)) { 159 UHID_LOG("Invalid size read from uhid-dev: %zd != %zu", 160 ret, sizeof(ev)); 161 return -EFAULT; 162 } 163 164 switch (ev.type) { 165 case UHID_START: 166 pthread_mutex_lock(&uhid_started_mtx); 167 pthread_cond_signal(&uhid_started); 168 pthread_mutex_unlock(&uhid_started_mtx); 169 170 UHID_LOG("UHID_START from uhid-dev"); 171 break; 172 case UHID_STOP: 173 uhid_stopped = true; 174 175 UHID_LOG("UHID_STOP from uhid-dev"); 176 break; 177 case UHID_OPEN: 178 UHID_LOG("UHID_OPEN from uhid-dev"); 179 break; 180 case UHID_CLOSE: 181 UHID_LOG("UHID_CLOSE from uhid-dev"); 182 break; 183 case UHID_OUTPUT: 184 UHID_LOG("UHID_OUTPUT from uhid-dev"); 185 break; 186 case UHID_GET_REPORT: 187 UHID_LOG("UHID_GET_REPORT from uhid-dev"); 188 break; 189 case UHID_SET_REPORT: 190 UHID_LOG("UHID_SET_REPORT from uhid-dev"); 191 break; 192 default: 193 TH_LOG("Invalid event from uhid-dev: %u", ev.type); 194 } 195 196 return 0; 197 } 198 199 struct uhid_thread_args { 200 int fd; 201 struct __test_metadata *_metadata; 202 }; 203 static void *uhid_read_events_thread(void *arg) 204 { 205 struct uhid_thread_args *args = (struct uhid_thread_args *)arg; 206 struct __test_metadata *_metadata = args->_metadata; 207 struct pollfd pfds[1]; 208 int fd = args->fd; 209 int ret = 0; 210 211 pfds[0].fd = fd; 212 pfds[0].events = POLLIN; 213 214 uhid_stopped = false; 215 216 while (!uhid_stopped) { 217 ret = poll(pfds, 1, 100); 218 if (ret < 0) { 219 TH_LOG("Cannot poll for fds: %m"); 220 break; 221 } 222 if (pfds[0].revents & POLLIN) { 223 ret = uhid_event(_metadata, fd); 224 if (ret) 225 break; 226 } 227 } 228 229 return (void *)(long)ret; 230 } 231 232 static int uhid_start_listener(struct __test_metadata *_metadata, pthread_t *tid, int uhid_fd) 233 { 234 struct uhid_thread_args args = { 235 .fd = uhid_fd, 236 ._metadata = _metadata, 237 }; 238 int err; 239 240 pthread_mutex_lock(&uhid_started_mtx); 241 err = pthread_create(tid, NULL, uhid_read_events_thread, (void *)&args); 242 ASSERT_EQ(0, err) { 243 TH_LOG("Could not start the uhid thread: %d", err); 244 pthread_mutex_unlock(&uhid_started_mtx); 245 close(uhid_fd); 246 return -EIO; 247 } 248 pthread_cond_wait(&uhid_started, &uhid_started_mtx); 249 pthread_mutex_unlock(&uhid_started_mtx); 250 251 return 0; 252 } 253 254 static int uhid_send_event(struct __test_metadata *_metadata, int fd, __u8 *buf, size_t size) 255 { 256 struct uhid_event ev; 257 258 if (size > sizeof(ev.u.input.data)) 259 return -E2BIG; 260 261 memset(&ev, 0, sizeof(ev)); 262 ev.type = UHID_INPUT2; 263 ev.u.input2.size = size; 264 265 memcpy(ev.u.input2.data, buf, size); 266 267 return uhid_write(_metadata, fd, &ev); 268 } 269 270 static int setup_uhid(struct __test_metadata *_metadata, int rand_nb) 271 { 272 int fd; 273 const char *path = "/dev/uhid"; 274 int ret; 275 276 fd = open(path, O_RDWR | O_CLOEXEC); 277 ASSERT_GE(fd, 0) TH_LOG("open uhid-cdev failed; %d", fd); 278 279 ret = uhid_create(_metadata, fd, rand_nb); 280 ASSERT_EQ(0, ret) { 281 TH_LOG("create uhid device failed: %d", ret); 282 close(fd); 283 } 284 285 return fd; 286 } 287 288 static bool match_sysfs_device(int dev_id, const char *workdir, struct dirent *dir) 289 { 290 const char *target = "0003:0001:0A37.*"; 291 char phys[512]; 292 char uevent[1024]; 293 char temp[512]; 294 int fd, nread; 295 bool found = false; 296 297 if (fnmatch(target, dir->d_name, 0)) 298 return false; 299 300 /* we found the correct VID/PID, now check for phys */ 301 sprintf(uevent, "%s/%s/uevent", workdir, dir->d_name); 302 303 fd = open(uevent, O_RDONLY | O_NONBLOCK); 304 if (fd < 0) 305 return false; 306 307 sprintf(phys, "PHYS=%d", dev_id); 308 309 nread = read(fd, temp, ARRAY_SIZE(temp)); 310 if (nread > 0 && (strstr(temp, phys)) != NULL) 311 found = true; 312 313 close(fd); 314 315 return found; 316 } 317 318 static int get_hid_id(int dev_id) 319 { 320 const char *workdir = "/sys/devices/virtual/misc/uhid"; 321 const char *str_id; 322 DIR *d; 323 struct dirent *dir; 324 int found = -1, attempts = 3; 325 326 /* it would be nice to be able to use nftw, but the no_alu32 target doesn't support it */ 327 328 while (found < 0 && attempts > 0) { 329 attempts--; 330 d = opendir(workdir); 331 if (d) { 332 while ((dir = readdir(d)) != NULL) { 333 if (!match_sysfs_device(dev_id, workdir, dir)) 334 continue; 335 336 str_id = dir->d_name + sizeof("0003:0001:0A37."); 337 found = (int)strtol(str_id, NULL, 16); 338 339 break; 340 } 341 closedir(d); 342 } 343 if (found < 0) 344 usleep(100000); 345 } 346 347 return found; 348 } 349 350 static int get_hidraw(int dev_id) 351 { 352 const char *workdir = "/sys/devices/virtual/misc/uhid"; 353 char sysfs[1024]; 354 DIR *d, *subd; 355 struct dirent *dir, *subdir; 356 int i, found = -1; 357 358 /* retry 5 times in case the system is loaded */ 359 for (i = 5; i > 0; i--) { 360 usleep(10); 361 d = opendir(workdir); 362 363 if (!d) 364 continue; 365 366 while ((dir = readdir(d)) != NULL) { 367 if (!match_sysfs_device(dev_id, workdir, dir)) 368 continue; 369 370 sprintf(sysfs, "%s/%s/hidraw", workdir, dir->d_name); 371 372 subd = opendir(sysfs); 373 if (!subd) 374 continue; 375 376 while ((subdir = readdir(subd)) != NULL) { 377 if (fnmatch("hidraw*", subdir->d_name, 0)) 378 continue; 379 380 found = atoi(subdir->d_name + strlen("hidraw")); 381 } 382 383 closedir(subd); 384 385 if (found > 0) 386 break; 387 } 388 closedir(d); 389 } 390 391 return found; 392 } 393 394 static int open_hidraw(int dev_id) 395 { 396 int hidraw_number; 397 char hidraw_path[64] = { 0 }; 398 399 hidraw_number = get_hidraw(dev_id); 400 if (hidraw_number < 0) 401 return hidraw_number; 402 403 /* open hidraw node to check the other side of the pipe */ 404 sprintf(hidraw_path, "/dev/hidraw%d", hidraw_number); 405 return open(hidraw_path, O_RDWR | O_NONBLOCK); 406 } 407 408 FIXTURE(hid_bpf) { 409 int dev_id; 410 int uhid_fd; 411 int hidraw_fd; 412 int hid_id; 413 pthread_t tid; 414 struct hid *skel; 415 }; 416 static void detach_bpf(FIXTURE_DATA(hid_bpf) * self) 417 { 418 if (self->hidraw_fd) 419 close(self->hidraw_fd); 420 self->hidraw_fd = 0; 421 422 hid__destroy(self->skel); 423 self->skel = NULL; 424 } 425 426 FIXTURE_TEARDOWN(hid_bpf) { 427 void *uhid_err; 428 429 uhid_destroy(_metadata, self->uhid_fd); 430 431 detach_bpf(self); 432 pthread_join(self->tid, &uhid_err); 433 } 434 #define TEARDOWN_LOG(fmt, ...) do { \ 435 TH_LOG(fmt, ##__VA_ARGS__); \ 436 hid_bpf_teardown(_metadata, self, variant); \ 437 } while (0) 438 439 FIXTURE_SETUP(hid_bpf) 440 { 441 time_t t; 442 int err; 443 444 /* initialize random number generator */ 445 srand((unsigned int)time(&t)); 446 447 self->dev_id = rand() % 1024; 448 449 self->uhid_fd = setup_uhid(_metadata, self->dev_id); 450 451 /* locate the uev, self, variant);ent file of the created device */ 452 self->hid_id = get_hid_id(self->dev_id); 453 ASSERT_GT(self->hid_id, 0) 454 TEARDOWN_LOG("Could not locate uhid device id: %d", self->hid_id); 455 456 err = uhid_start_listener(_metadata, &self->tid, self->uhid_fd); 457 ASSERT_EQ(0, err) TEARDOWN_LOG("could not start udev listener: %d", err); 458 } 459 460 struct test_program { 461 const char *name; 462 }; 463 #define LOAD_PROGRAMS(progs) \ 464 load_programs(progs, ARRAY_SIZE(progs), _metadata, self, variant) 465 #define LOAD_BPF \ 466 load_programs(NULL, 0, _metadata, self, variant) 467 static void load_programs(const struct test_program programs[], 468 const size_t progs_count, 469 struct __test_metadata *_metadata, 470 FIXTURE_DATA(hid_bpf) * self, 471 const FIXTURE_VARIANT(hid_bpf) * variant) 472 { 473 int attach_fd, err = -EINVAL; 474 struct attach_prog_args args = { 475 .retval = -1, 476 }; 477 DECLARE_LIBBPF_OPTS(bpf_test_run_opts, tattr, 478 .ctx_in = &args, 479 .ctx_size_in = sizeof(args), 480 ); 481 482 /* open the bpf file */ 483 self->skel = hid__open(); 484 ASSERT_OK_PTR(self->skel) TEARDOWN_LOG("Error while calling hid__open"); 485 486 for (int i = 0; i < progs_count; i++) { 487 struct bpf_program *prog; 488 489 prog = bpf_object__find_program_by_name(*self->skel->skeleton->obj, 490 programs[i].name); 491 ASSERT_OK_PTR(prog) TH_LOG("can not find program by name '%s'", programs[i].name); 492 493 bpf_program__set_autoload(prog, true); 494 } 495 496 err = hid__load(self->skel); 497 ASSERT_OK(err) TH_LOG("hid_skel_load failed: %d", err); 498 499 attach_fd = bpf_program__fd(self->skel->progs.attach_prog); 500 ASSERT_GE(attach_fd, 0) TH_LOG("locate attach_prog: %d", attach_fd); 501 502 for (int i = 0; i < progs_count; i++) { 503 struct bpf_program *prog; 504 505 prog = bpf_object__find_program_by_name(*self->skel->skeleton->obj, 506 programs[i].name); 507 ASSERT_OK_PTR(prog) TH_LOG("can not find program by name '%s'", programs[i].name); 508 509 args.prog_fd = bpf_program__fd(prog); 510 args.hid = self->hid_id; 511 err = bpf_prog_test_run_opts(attach_fd, &tattr); 512 ASSERT_OK(args.retval) TH_LOG("attach_hid(%s): %d", programs[i].name, args.retval); 513 } 514 515 self->hidraw_fd = open_hidraw(self->dev_id); 516 ASSERT_GE(self->hidraw_fd, 0) TH_LOG("open_hidraw"); 517 } 518 519 /* 520 * A simple test to see if the fixture is working fine. 521 * If this fails, none of the other tests will pass. 522 */ 523 TEST_F(hid_bpf, test_create_uhid) 524 { 525 } 526 527 /* 528 * Attach hid_first_event to the given uhid device, 529 * retrieve and open the matching hidraw node, 530 * inject one event in the uhid device, 531 * check that the program sees it and can change the data 532 */ 533 TEST_F(hid_bpf, raw_event) 534 { 535 const struct test_program progs[] = { 536 { .name = "hid_first_event" }, 537 }; 538 __u8 buf[10] = {0}; 539 int err; 540 541 LOAD_PROGRAMS(progs); 542 543 /* check that the program is correctly loaded */ 544 ASSERT_EQ(self->skel->data->callback_check, 52) TH_LOG("callback_check1"); 545 ASSERT_EQ(self->skel->data->callback2_check, 52) TH_LOG("callback2_check1"); 546 547 /* inject one event */ 548 buf[0] = 1; 549 buf[1] = 42; 550 uhid_send_event(_metadata, self->uhid_fd, buf, 6); 551 552 /* check that hid_first_event() was executed */ 553 ASSERT_EQ(self->skel->data->callback_check, 42) TH_LOG("callback_check1"); 554 555 /* read the data from hidraw */ 556 memset(buf, 0, sizeof(buf)); 557 err = read(self->hidraw_fd, buf, sizeof(buf)); 558 ASSERT_EQ(err, 6) TH_LOG("read_hidraw"); 559 ASSERT_EQ(buf[0], 1); 560 ASSERT_EQ(buf[2], 47); 561 562 /* inject another event */ 563 memset(buf, 0, sizeof(buf)); 564 buf[0] = 1; 565 buf[1] = 47; 566 uhid_send_event(_metadata, self->uhid_fd, buf, 6); 567 568 /* check that hid_first_event() was executed */ 569 ASSERT_EQ(self->skel->data->callback_check, 47) TH_LOG("callback_check1"); 570 571 /* read the data from hidraw */ 572 memset(buf, 0, sizeof(buf)); 573 err = read(self->hidraw_fd, buf, sizeof(buf)); 574 ASSERT_EQ(err, 6) TH_LOG("read_hidraw"); 575 ASSERT_EQ(buf[2], 52); 576 } 577 578 /* 579 * Ensures that we can attach/detach programs 580 */ 581 TEST_F(hid_bpf, test_attach_detach) 582 { 583 const struct test_program progs[] = { 584 { .name = "hid_first_event" }, 585 }; 586 __u8 buf[10] = {0}; 587 int err; 588 589 LOAD_PROGRAMS(progs); 590 591 /* inject one event */ 592 buf[0] = 1; 593 buf[1] = 42; 594 uhid_send_event(_metadata, self->uhid_fd, buf, 6); 595 596 /* read the data from hidraw */ 597 memset(buf, 0, sizeof(buf)); 598 err = read(self->hidraw_fd, buf, sizeof(buf)); 599 ASSERT_EQ(err, 6) TH_LOG("read_hidraw"); 600 ASSERT_EQ(buf[0], 1); 601 ASSERT_EQ(buf[2], 47); 602 603 /* pin the program and immediately unpin it */ 604 #define PIN_PATH "/sys/fs/bpf/hid_first_event" 605 bpf_program__pin(self->skel->progs.hid_first_event, PIN_PATH); 606 remove(PIN_PATH); 607 #undef PIN_PATH 608 usleep(100000); 609 610 /* detach the program */ 611 detach_bpf(self); 612 613 self->hidraw_fd = open_hidraw(self->dev_id); 614 ASSERT_GE(self->hidraw_fd, 0) TH_LOG("open_hidraw"); 615 616 /* inject another event */ 617 memset(buf, 0, sizeof(buf)); 618 buf[0] = 1; 619 buf[1] = 47; 620 uhid_send_event(_metadata, self->uhid_fd, buf, 6); 621 622 /* read the data from hidraw */ 623 memset(buf, 0, sizeof(buf)); 624 err = read(self->hidraw_fd, buf, sizeof(buf)); 625 ASSERT_EQ(err, 6) TH_LOG("read_hidraw_no_bpf"); 626 ASSERT_EQ(buf[0], 1); 627 ASSERT_EQ(buf[1], 47); 628 ASSERT_EQ(buf[2], 0); 629 630 /* re-attach our program */ 631 632 LOAD_PROGRAMS(progs); 633 634 /* inject one event */ 635 memset(buf, 0, sizeof(buf)); 636 buf[0] = 1; 637 buf[1] = 42; 638 uhid_send_event(_metadata, self->uhid_fd, buf, 6); 639 640 /* read the data from hidraw */ 641 memset(buf, 0, sizeof(buf)); 642 err = read(self->hidraw_fd, buf, sizeof(buf)); 643 ASSERT_EQ(err, 6) TH_LOG("read_hidraw"); 644 ASSERT_EQ(buf[0], 1); 645 ASSERT_EQ(buf[2], 47); 646 } 647 648 static int libbpf_print_fn(enum libbpf_print_level level, 649 const char *format, va_list args) 650 { 651 char buf[1024]; 652 653 if (level == LIBBPF_DEBUG) 654 return 0; 655 656 snprintf(buf, sizeof(buf), "# %s", format); 657 658 vfprintf(stdout, buf, args); 659 return 0; 660 } 661 662 static void __attribute__((constructor)) __constructor_order_last(void) 663 { 664 if (!__constructor_order) 665 __constructor_order = _CONSTRUCTOR_ORDER_BACKWARD; 666 } 667 668 int main(int argc, char **argv) 669 { 670 /* Use libbpf 1.0 API mode */ 671 libbpf_set_strict_mode(LIBBPF_STRICT_ALL); 672 libbpf_set_print(libbpf_print_fn); 673 674 return test_harness_run(argc, argv); 675 } 676