hid_bpf.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (c) 2022-2024 Red Hat */
  3. #include "hid.skel.h"
  4. #include "hid_common.h"
  5. #include <bpf/bpf.h>
  6. struct attach_prog_args {
  7. int prog_fd;
  8. unsigned int hid;
  9. int retval;
  10. int insert_head;
  11. };
  12. struct hid_hw_request_syscall_args {
  13. __u8 data[10];
  14. unsigned int hid;
  15. int retval;
  16. size_t size;
  17. enum hid_report_type type;
  18. __u8 request_type;
  19. };
  20. FIXTURE(hid_bpf) {
  21. int dev_id;
  22. int uhid_fd;
  23. int hidraw_fd;
  24. int hid_id;
  25. pthread_t tid;
  26. struct hid *skel;
  27. struct bpf_link *hid_links[3]; /* max number of programs loaded in a single test */
  28. };
  29. static void detach_bpf(FIXTURE_DATA(hid_bpf) * self)
  30. {
  31. int i;
  32. if (self->hidraw_fd)
  33. close(self->hidraw_fd);
  34. self->hidraw_fd = 0;
  35. if (!self->skel)
  36. return;
  37. hid__detach(self->skel);
  38. for (i = 0; i < ARRAY_SIZE(self->hid_links); i++) {
  39. if (self->hid_links[i])
  40. bpf_link__destroy(self->hid_links[i]);
  41. }
  42. hid__destroy(self->skel);
  43. self->skel = NULL;
  44. }
  45. FIXTURE_TEARDOWN(hid_bpf) {
  46. void *uhid_err;
  47. uhid_destroy(_metadata, self->uhid_fd);
  48. detach_bpf(self);
  49. pthread_join(self->tid, &uhid_err);
  50. }
  51. #define TEARDOWN_LOG(fmt, ...) do { \
  52. TH_LOG(fmt, ##__VA_ARGS__); \
  53. hid_bpf_teardown(_metadata, self, variant); \
  54. } while (0)
  55. FIXTURE_SETUP(hid_bpf)
  56. {
  57. time_t t;
  58. int err;
  59. /* initialize random number generator */
  60. srand((unsigned int)time(&t));
  61. self->dev_id = rand() % 1024;
  62. self->uhid_fd = setup_uhid(_metadata, self->dev_id);
  63. /* locate the uev, self, variant);ent file of the created device */
  64. self->hid_id = get_hid_id(self->dev_id);
  65. ASSERT_GT(self->hid_id, 0)
  66. TEARDOWN_LOG("Could not locate uhid device id: %d", self->hid_id);
  67. err = uhid_start_listener(_metadata, &self->tid, self->uhid_fd);
  68. ASSERT_EQ(0, err) TEARDOWN_LOG("could not start udev listener: %d", err);
  69. }
  70. struct test_program {
  71. const char *name;
  72. int insert_head;
  73. };
  74. #define LOAD_PROGRAMS(progs) \
  75. load_programs(progs, ARRAY_SIZE(progs), _metadata, self, variant)
  76. #define LOAD_BPF \
  77. load_programs(NULL, 0, _metadata, self, variant)
  78. static void load_programs(const struct test_program programs[],
  79. const size_t progs_count,
  80. struct __test_metadata *_metadata,
  81. FIXTURE_DATA(hid_bpf) * self,
  82. const FIXTURE_VARIANT(hid_bpf) * variant)
  83. {
  84. struct bpf_map *iter_map;
  85. int err = -EINVAL;
  86. ASSERT_LE(progs_count, ARRAY_SIZE(self->hid_links))
  87. TH_LOG("too many programs are to be loaded");
  88. /* open the bpf file */
  89. self->skel = hid__open();
  90. ASSERT_OK_PTR(self->skel) TEARDOWN_LOG("Error while calling hid__open");
  91. for (int i = 0; i < progs_count; i++) {
  92. struct bpf_program *prog;
  93. struct bpf_map *map;
  94. int *ops_hid_id;
  95. prog = bpf_object__find_program_by_name(*self->skel->skeleton->obj,
  96. programs[i].name);
  97. ASSERT_OK_PTR(prog) TH_LOG("can not find program by name '%s'", programs[i].name);
  98. bpf_program__set_autoload(prog, true);
  99. map = bpf_object__find_map_by_name(*self->skel->skeleton->obj,
  100. programs[i].name + 4);
  101. ASSERT_OK_PTR(map) TH_LOG("can not find struct_ops by name '%s'",
  102. programs[i].name + 4);
  103. /* hid_id is the first field of struct hid_bpf_ops */
  104. ops_hid_id = bpf_map__initial_value(map, NULL);
  105. ASSERT_OK_PTR(ops_hid_id) TH_LOG("unable to retrieve struct_ops data");
  106. *ops_hid_id = self->hid_id;
  107. }
  108. /* we disable the auto-attach feature of all maps because we
  109. * only want the tested one to be manually attached in the next
  110. * call to bpf_map__attach_struct_ops()
  111. */
  112. bpf_object__for_each_map(iter_map, *self->skel->skeleton->obj)
  113. bpf_map__set_autoattach(iter_map, false);
  114. err = hid__load(self->skel);
  115. ASSERT_OK(err) TH_LOG("hid_skel_load failed: %d", err);
  116. for (int i = 0; i < progs_count; i++) {
  117. struct bpf_map *map;
  118. map = bpf_object__find_map_by_name(*self->skel->skeleton->obj,
  119. programs[i].name + 4);
  120. ASSERT_OK_PTR(map) TH_LOG("can not find struct_ops by name '%s'",
  121. programs[i].name + 4);
  122. self->hid_links[i] = bpf_map__attach_struct_ops(map);
  123. ASSERT_OK_PTR(self->hid_links[i]) TH_LOG("failed to attach struct ops '%s'",
  124. programs[i].name + 4);
  125. }
  126. hid__attach(self->skel);
  127. self->hidraw_fd = open_hidraw(self->dev_id);
  128. ASSERT_GE(self->hidraw_fd, 0) TH_LOG("open_hidraw");
  129. }
  130. /*
  131. * A simple test to see if the fixture is working fine.
  132. * If this fails, none of the other tests will pass.
  133. */
  134. TEST_F(hid_bpf, test_create_uhid)
  135. {
  136. }
  137. /*
  138. * Attach hid_first_event to the given uhid device,
  139. * retrieve and open the matching hidraw node,
  140. * inject one event in the uhid device,
  141. * check that the program sees it and can change the data
  142. */
  143. TEST_F(hid_bpf, raw_event)
  144. {
  145. const struct test_program progs[] = {
  146. { .name = "hid_first_event" },
  147. };
  148. __u8 buf[10] = {0};
  149. int err;
  150. LOAD_PROGRAMS(progs);
  151. /* check that the program is correctly loaded */
  152. ASSERT_EQ(self->skel->data->callback_check, 52) TH_LOG("callback_check1");
  153. ASSERT_EQ(self->skel->data->callback2_check, 52) TH_LOG("callback2_check1");
  154. /* inject one event */
  155. buf[0] = 1;
  156. buf[1] = 42;
  157. uhid_send_event(_metadata, self->uhid_fd, buf, 6);
  158. /* check that hid_first_event() was executed */
  159. ASSERT_EQ(self->skel->data->callback_check, 42) TH_LOG("callback_check1");
  160. /* read the data from hidraw */
  161. memset(buf, 0, sizeof(buf));
  162. err = read(self->hidraw_fd, buf, sizeof(buf));
  163. ASSERT_EQ(err, 6) TH_LOG("read_hidraw");
  164. ASSERT_EQ(buf[0], 1);
  165. ASSERT_EQ(buf[2], 47);
  166. /* inject another event */
  167. memset(buf, 0, sizeof(buf));
  168. buf[0] = 1;
  169. buf[1] = 47;
  170. uhid_send_event(_metadata, self->uhid_fd, buf, 6);
  171. /* check that hid_first_event() was executed */
  172. ASSERT_EQ(self->skel->data->callback_check, 47) TH_LOG("callback_check1");
  173. /* read the data from hidraw */
  174. memset(buf, 0, sizeof(buf));
  175. err = read(self->hidraw_fd, buf, sizeof(buf));
  176. ASSERT_EQ(err, 6) TH_LOG("read_hidraw");
  177. ASSERT_EQ(buf[2], 52);
  178. }
  179. /*
  180. * Attach hid_first_event to the given uhid device,
  181. * retrieve and open the matching hidraw node,
  182. * inject one event in the uhid device,
  183. * check that the program sees it and can change the data
  184. */
  185. TEST_F(hid_bpf, subprog_raw_event)
  186. {
  187. const struct test_program progs[] = {
  188. { .name = "hid_subprog_first_event" },
  189. };
  190. __u8 buf[10] = {0};
  191. int err;
  192. LOAD_PROGRAMS(progs);
  193. /* inject one event */
  194. buf[0] = 1;
  195. buf[1] = 42;
  196. uhid_send_event(_metadata, self->uhid_fd, buf, 6);
  197. /* read the data from hidraw */
  198. memset(buf, 0, sizeof(buf));
  199. err = read(self->hidraw_fd, buf, sizeof(buf));
  200. ASSERT_EQ(err, 6) TH_LOG("read_hidraw");
  201. ASSERT_EQ(buf[0], 1);
  202. ASSERT_EQ(buf[2], 47);
  203. /* inject another event */
  204. memset(buf, 0, sizeof(buf));
  205. buf[0] = 1;
  206. buf[1] = 47;
  207. uhid_send_event(_metadata, self->uhid_fd, buf, 6);
  208. /* read the data from hidraw */
  209. memset(buf, 0, sizeof(buf));
  210. err = read(self->hidraw_fd, buf, sizeof(buf));
  211. ASSERT_EQ(err, 6) TH_LOG("read_hidraw");
  212. ASSERT_EQ(buf[2], 52);
  213. }
  214. /*
  215. * Attach hid_first_event to the given uhid device,
  216. * attempt at re-attaching it, we should not lock and
  217. * return an invalid struct bpf_link
  218. */
  219. TEST_F(hid_bpf, multiple_attach)
  220. {
  221. const struct test_program progs[] = {
  222. { .name = "hid_first_event" },
  223. };
  224. struct bpf_link *link;
  225. LOAD_PROGRAMS(progs);
  226. link = bpf_map__attach_struct_ops(self->skel->maps.first_event);
  227. ASSERT_NULL(link) TH_LOG("unexpected return value when re-attaching the struct_ops");
  228. }
  229. /*
  230. * Ensures that we can attach/detach programs
  231. */
  232. TEST_F(hid_bpf, test_attach_detach)
  233. {
  234. const struct test_program progs[] = {
  235. { .name = "hid_first_event" },
  236. { .name = "hid_second_event" },
  237. };
  238. struct bpf_link *link;
  239. __u8 buf[10] = {0};
  240. int err, link_fd;
  241. LOAD_PROGRAMS(progs);
  242. link = self->hid_links[0];
  243. ASSERT_OK_PTR(link) TH_LOG("HID-BPF link not created");
  244. link_fd = bpf_link__fd(link);
  245. ASSERT_GE(link_fd, 0) TH_LOG("HID-BPF link FD not valid");
  246. /* inject one event */
  247. buf[0] = 1;
  248. buf[1] = 42;
  249. uhid_send_event(_metadata, self->uhid_fd, buf, 6);
  250. /* read the data from hidraw */
  251. memset(buf, 0, sizeof(buf));
  252. err = read(self->hidraw_fd, buf, sizeof(buf));
  253. ASSERT_EQ(err, 6) TH_LOG("read_hidraw");
  254. ASSERT_EQ(buf[0], 1);
  255. ASSERT_EQ(buf[2], 47);
  256. /* make sure both programs are run */
  257. ASSERT_EQ(buf[3], 52);
  258. /* pin the first program and immediately unpin it */
  259. #define PIN_PATH "/sys/fs/bpf/hid_first_event"
  260. err = bpf_obj_pin(link_fd, PIN_PATH);
  261. ASSERT_OK(err) TH_LOG("error while calling bpf_obj_pin");
  262. remove(PIN_PATH);
  263. #undef PIN_PATH
  264. usleep(100000);
  265. /* detach the program */
  266. detach_bpf(self);
  267. self->hidraw_fd = open_hidraw(self->dev_id);
  268. ASSERT_GE(self->hidraw_fd, 0) TH_LOG("open_hidraw");
  269. /* inject another event */
  270. memset(buf, 0, sizeof(buf));
  271. buf[0] = 1;
  272. buf[1] = 47;
  273. uhid_send_event(_metadata, self->uhid_fd, buf, 6);
  274. /* read the data from hidraw */
  275. memset(buf, 0, sizeof(buf));
  276. err = read(self->hidraw_fd, buf, sizeof(buf));
  277. ASSERT_EQ(err, 6) TH_LOG("read_hidraw_no_bpf");
  278. ASSERT_EQ(buf[0], 1);
  279. ASSERT_EQ(buf[1], 47);
  280. ASSERT_EQ(buf[2], 0);
  281. ASSERT_EQ(buf[3], 0);
  282. /* re-attach our program */
  283. LOAD_PROGRAMS(progs);
  284. /* inject one event */
  285. memset(buf, 0, sizeof(buf));
  286. buf[0] = 1;
  287. buf[1] = 42;
  288. uhid_send_event(_metadata, self->uhid_fd, buf, 6);
  289. /* read the data from hidraw */
  290. memset(buf, 0, sizeof(buf));
  291. err = read(self->hidraw_fd, buf, sizeof(buf));
  292. ASSERT_EQ(err, 6) TH_LOG("read_hidraw");
  293. ASSERT_EQ(buf[0], 1);
  294. ASSERT_EQ(buf[2], 47);
  295. ASSERT_EQ(buf[3], 52);
  296. }
  297. /*
  298. * Attach hid_change_report_id to the given uhid device,
  299. * retrieve and open the matching hidraw node,
  300. * inject one event in the uhid device,
  301. * check that the program sees it and can change the data
  302. */
  303. TEST_F(hid_bpf, test_hid_change_report)
  304. {
  305. const struct test_program progs[] = {
  306. { .name = "hid_change_report_id" },
  307. };
  308. __u8 buf[10] = {0};
  309. int err;
  310. LOAD_PROGRAMS(progs);
  311. /* inject one event */
  312. buf[0] = 1;
  313. buf[1] = 42;
  314. uhid_send_event(_metadata, self->uhid_fd, buf, 6);
  315. /* read the data from hidraw */
  316. memset(buf, 0, sizeof(buf));
  317. err = read(self->hidraw_fd, buf, sizeof(buf));
  318. ASSERT_EQ(err, 9) TH_LOG("read_hidraw");
  319. ASSERT_EQ(buf[0], 2);
  320. ASSERT_EQ(buf[1], 42);
  321. ASSERT_EQ(buf[2], 0) TH_LOG("leftovers_from_previous_test");
  322. }
  323. /*
  324. * Call hid_bpf_input_report against the given uhid device,
  325. * check that the program is called and does the expected.
  326. */
  327. TEST_F(hid_bpf, test_hid_user_input_report_call)
  328. {
  329. struct hid_hw_request_syscall_args args = {
  330. .retval = -1,
  331. .size = 10,
  332. };
  333. DECLARE_LIBBPF_OPTS(bpf_test_run_opts, tattrs,
  334. .ctx_in = &args,
  335. .ctx_size_in = sizeof(args),
  336. );
  337. __u8 buf[10] = {0};
  338. int err, prog_fd;
  339. LOAD_BPF;
  340. args.hid = self->hid_id;
  341. args.data[0] = 1; /* report ID */
  342. args.data[1] = 2; /* report ID */
  343. args.data[2] = 42; /* report ID */
  344. prog_fd = bpf_program__fd(self->skel->progs.hid_user_input_report);
  345. /* check that there is no data to read from hidraw */
  346. memset(buf, 0, sizeof(buf));
  347. err = read(self->hidraw_fd, buf, sizeof(buf));
  348. ASSERT_EQ(err, -1) TH_LOG("read_hidraw");
  349. err = bpf_prog_test_run_opts(prog_fd, &tattrs);
  350. ASSERT_OK(err) TH_LOG("error while calling bpf_prog_test_run_opts");
  351. ASSERT_EQ(args.retval, 0);
  352. /* read the data from hidraw */
  353. memset(buf, 0, sizeof(buf));
  354. err = read(self->hidraw_fd, buf, sizeof(buf));
  355. ASSERT_EQ(err, 6) TH_LOG("read_hidraw");
  356. ASSERT_EQ(buf[0], 1);
  357. ASSERT_EQ(buf[1], 2);
  358. ASSERT_EQ(buf[2], 42);
  359. }
  360. /*
  361. * Call hid_bpf_hw_output_report against the given uhid device,
  362. * check that the program is called and does the expected.
  363. */
  364. TEST_F(hid_bpf, test_hid_user_output_report_call)
  365. {
  366. struct hid_hw_request_syscall_args args = {
  367. .retval = -1,
  368. .size = 10,
  369. };
  370. DECLARE_LIBBPF_OPTS(bpf_test_run_opts, tattrs,
  371. .ctx_in = &args,
  372. .ctx_size_in = sizeof(args),
  373. );
  374. int err, cond_err, prog_fd;
  375. struct timespec time_to_wait;
  376. LOAD_BPF;
  377. args.hid = self->hid_id;
  378. args.data[0] = 1; /* report ID */
  379. args.data[1] = 2; /* report ID */
  380. args.data[2] = 42; /* report ID */
  381. prog_fd = bpf_program__fd(self->skel->progs.hid_user_output_report);
  382. pthread_mutex_lock(&uhid_output_mtx);
  383. memset(output_report, 0, sizeof(output_report));
  384. clock_gettime(CLOCK_REALTIME, &time_to_wait);
  385. time_to_wait.tv_sec += 2;
  386. err = bpf_prog_test_run_opts(prog_fd, &tattrs);
  387. cond_err = pthread_cond_timedwait(&uhid_output_cond, &uhid_output_mtx, &time_to_wait);
  388. ASSERT_OK(err) TH_LOG("error while calling bpf_prog_test_run_opts");
  389. ASSERT_OK(cond_err) TH_LOG("error while calling waiting for the condition");
  390. ASSERT_EQ(args.retval, 3);
  391. ASSERT_EQ(output_report[0], 1);
  392. ASSERT_EQ(output_report[1], 2);
  393. ASSERT_EQ(output_report[2], 42);
  394. pthread_mutex_unlock(&uhid_output_mtx);
  395. }
  396. /*
  397. * Call hid_hw_raw_request against the given uhid device,
  398. * check that the program is called and does the expected.
  399. */
  400. TEST_F(hid_bpf, test_hid_user_raw_request_call)
  401. {
  402. struct hid_hw_request_syscall_args args = {
  403. .retval = -1,
  404. .type = HID_FEATURE_REPORT,
  405. .request_type = HID_REQ_GET_REPORT,
  406. .size = 10,
  407. };
  408. DECLARE_LIBBPF_OPTS(bpf_test_run_opts, tattrs,
  409. .ctx_in = &args,
  410. .ctx_size_in = sizeof(args),
  411. );
  412. int err, prog_fd;
  413. LOAD_BPF;
  414. args.hid = self->hid_id;
  415. args.data[0] = 1; /* report ID */
  416. prog_fd = bpf_program__fd(self->skel->progs.hid_user_raw_request);
  417. err = bpf_prog_test_run_opts(prog_fd, &tattrs);
  418. ASSERT_OK(err) TH_LOG("error while calling bpf_prog_test_run_opts");
  419. ASSERT_EQ(args.retval, 2);
  420. ASSERT_EQ(args.data[1], 2);
  421. }
  422. /*
  423. * Call hid_hw_raw_request against the given uhid device,
  424. * check that the program is called and prevents the
  425. * call to uhid.
  426. */
  427. TEST_F(hid_bpf, test_hid_filter_raw_request_call)
  428. {
  429. const struct test_program progs[] = {
  430. { .name = "hid_test_filter_raw_request" },
  431. };
  432. __u8 buf[10] = {0};
  433. int err;
  434. LOAD_PROGRAMS(progs);
  435. /* first check that we did not attach to device_event */
  436. /* inject one event */
  437. buf[0] = 1;
  438. buf[1] = 42;
  439. uhid_send_event(_metadata, self->uhid_fd, buf, 6);
  440. /* read the data from hidraw */
  441. memset(buf, 0, sizeof(buf));
  442. err = read(self->hidraw_fd, buf, sizeof(buf));
  443. ASSERT_EQ(err, 6) TH_LOG("read_hidraw");
  444. ASSERT_EQ(buf[0], 1);
  445. ASSERT_EQ(buf[1], 42);
  446. ASSERT_EQ(buf[2], 0) TH_LOG("leftovers_from_previous_test");
  447. /* now check that our program is preventing hid_hw_raw_request() */
  448. /* emit hid_hw_raw_request from hidraw */
  449. /* Get Feature */
  450. memset(buf, 0, sizeof(buf));
  451. buf[0] = 0x1; /* Report Number */
  452. err = ioctl(self->hidraw_fd, HIDIOCGFEATURE(sizeof(buf)), buf);
  453. ASSERT_LT(err, 0) TH_LOG("unexpected success while reading HIDIOCGFEATURE: %d", err);
  454. ASSERT_EQ(errno, 20) TH_LOG("unexpected error code while reading HIDIOCGFEATURE: %d",
  455. errno);
  456. /* remove our bpf program and check that we can now emit commands */
  457. /* detach the program */
  458. detach_bpf(self);
  459. self->hidraw_fd = open_hidraw(self->dev_id);
  460. ASSERT_GE(self->hidraw_fd, 0) TH_LOG("open_hidraw");
  461. err = ioctl(self->hidraw_fd, HIDIOCGFEATURE(sizeof(buf)), buf);
  462. ASSERT_GE(err, 0) TH_LOG("error while reading HIDIOCGFEATURE: %d", err);
  463. }
  464. /*
  465. * Call hid_hw_raw_request against the given uhid device,
  466. * check that the program is called and can issue the call
  467. * to uhid and transform the answer.
  468. */
  469. TEST_F(hid_bpf, test_hid_change_raw_request_call)
  470. {
  471. const struct test_program progs[] = {
  472. { .name = "hid_test_hidraw_raw_request" },
  473. };
  474. __u8 buf[10] = {0};
  475. int err;
  476. LOAD_PROGRAMS(progs);
  477. /* emit hid_hw_raw_request from hidraw */
  478. /* Get Feature */
  479. memset(buf, 0, sizeof(buf));
  480. buf[0] = 0x1; /* Report Number */
  481. err = ioctl(self->hidraw_fd, HIDIOCGFEATURE(sizeof(buf)), buf);
  482. ASSERT_EQ(err, 3) TH_LOG("unexpected returned size while reading HIDIOCGFEATURE: %d", err);
  483. ASSERT_EQ(buf[0], 2);
  484. ASSERT_EQ(buf[1], 3);
  485. ASSERT_EQ(buf[2], 4);
  486. }
  487. /*
  488. * Call hid_hw_raw_request against the given uhid device,
  489. * check that the program is not making infinite loops.
  490. */
  491. TEST_F(hid_bpf, test_hid_infinite_loop_raw_request_call)
  492. {
  493. const struct test_program progs[] = {
  494. { .name = "hid_test_infinite_loop_raw_request" },
  495. };
  496. __u8 buf[10] = {0};
  497. int err;
  498. LOAD_PROGRAMS(progs);
  499. /* emit hid_hw_raw_request from hidraw */
  500. /* Get Feature */
  501. memset(buf, 0, sizeof(buf));
  502. buf[0] = 0x1; /* Report Number */
  503. err = ioctl(self->hidraw_fd, HIDIOCGFEATURE(sizeof(buf)), buf);
  504. ASSERT_EQ(err, 3) TH_LOG("unexpected returned size while reading HIDIOCGFEATURE: %d", err);
  505. }
  506. /*
  507. * Call hid_hw_output_report against the given uhid device,
  508. * check that the program is called and prevents the
  509. * call to uhid.
  510. */
  511. TEST_F(hid_bpf, test_hid_filter_output_report_call)
  512. {
  513. const struct test_program progs[] = {
  514. { .name = "hid_test_filter_output_report" },
  515. };
  516. __u8 buf[10] = {0};
  517. int err;
  518. LOAD_PROGRAMS(progs);
  519. /* first check that we did not attach to device_event */
  520. /* inject one event */
  521. buf[0] = 1;
  522. buf[1] = 42;
  523. uhid_send_event(_metadata, self->uhid_fd, buf, 6);
  524. /* read the data from hidraw */
  525. memset(buf, 0, sizeof(buf));
  526. err = read(self->hidraw_fd, buf, sizeof(buf));
  527. ASSERT_EQ(err, 6) TH_LOG("read_hidraw");
  528. ASSERT_EQ(buf[0], 1);
  529. ASSERT_EQ(buf[1], 42);
  530. ASSERT_EQ(buf[2], 0) TH_LOG("leftovers_from_previous_test");
  531. /* now check that our program is preventing hid_hw_output_report() */
  532. buf[0] = 1; /* report ID */
  533. buf[1] = 2;
  534. buf[2] = 42;
  535. err = write(self->hidraw_fd, buf, 3);
  536. ASSERT_LT(err, 0) TH_LOG("unexpected success while sending hid_hw_output_report: %d", err);
  537. ASSERT_EQ(errno, 25) TH_LOG("unexpected error code while sending hid_hw_output_report: %d",
  538. errno);
  539. /* remove our bpf program and check that we can now emit commands */
  540. /* detach the program */
  541. detach_bpf(self);
  542. self->hidraw_fd = open_hidraw(self->dev_id);
  543. ASSERT_GE(self->hidraw_fd, 0) TH_LOG("open_hidraw");
  544. err = write(self->hidraw_fd, buf, 3);
  545. ASSERT_GE(err, 0) TH_LOG("error while sending hid_hw_output_report: %d", err);
  546. }
  547. /*
  548. * Call hid_hw_output_report against the given uhid device,
  549. * check that the program is called and can issue the call
  550. * to uhid and transform the answer.
  551. */
  552. TEST_F(hid_bpf, test_hid_change_output_report_call)
  553. {
  554. const struct test_program progs[] = {
  555. { .name = "hid_test_hidraw_output_report" },
  556. };
  557. __u8 buf[10] = {0};
  558. int err;
  559. LOAD_PROGRAMS(progs);
  560. /* emit hid_hw_output_report from hidraw */
  561. buf[0] = 1; /* report ID */
  562. buf[1] = 2;
  563. buf[2] = 42;
  564. err = write(self->hidraw_fd, buf, 10);
  565. ASSERT_EQ(err, 2) TH_LOG("unexpected returned size while sending hid_hw_output_report: %d",
  566. err);
  567. }
  568. /*
  569. * Call hid_hw_output_report against the given uhid device,
  570. * check that the program is not making infinite loops.
  571. */
  572. TEST_F(hid_bpf, test_hid_infinite_loop_output_report_call)
  573. {
  574. const struct test_program progs[] = {
  575. { .name = "hid_test_infinite_loop_output_report" },
  576. };
  577. __u8 buf[10] = {0};
  578. int err;
  579. LOAD_PROGRAMS(progs);
  580. /* emit hid_hw_output_report from hidraw */
  581. buf[0] = 1; /* report ID */
  582. buf[1] = 2;
  583. buf[2] = 42;
  584. err = write(self->hidraw_fd, buf, 8);
  585. ASSERT_EQ(err, 2) TH_LOG("unexpected returned size while sending hid_hw_output_report: %d",
  586. err);
  587. }
  588. /*
  589. * Attach hid_multiply_event_wq to the given uhid device,
  590. * retrieve and open the matching hidraw node,
  591. * inject one event in the uhid device,
  592. * check that the program sees it and can add extra data
  593. */
  594. TEST_F(hid_bpf, test_multiply_events_wq)
  595. {
  596. const struct test_program progs[] = {
  597. { .name = "hid_test_multiply_events_wq" },
  598. };
  599. __u8 buf[10] = {0};
  600. int err;
  601. LOAD_PROGRAMS(progs);
  602. /* inject one event */
  603. buf[0] = 1;
  604. buf[1] = 42;
  605. uhid_send_event(_metadata, self->uhid_fd, buf, 6);
  606. /* read the data from hidraw */
  607. memset(buf, 0, sizeof(buf));
  608. err = read(self->hidraw_fd, buf, sizeof(buf));
  609. ASSERT_EQ(err, 6) TH_LOG("read_hidraw");
  610. ASSERT_EQ(buf[0], 1);
  611. ASSERT_EQ(buf[1], 47);
  612. usleep(100000);
  613. /* read the data from hidraw */
  614. memset(buf, 0, sizeof(buf));
  615. err = read(self->hidraw_fd, buf, sizeof(buf));
  616. ASSERT_EQ(err, 9) TH_LOG("read_hidraw");
  617. ASSERT_EQ(buf[0], 2);
  618. ASSERT_EQ(buf[1], 3);
  619. }
  620. /*
  621. * Attach hid_multiply_event to the given uhid device,
  622. * retrieve and open the matching hidraw node,
  623. * inject one event in the uhid device,
  624. * check that the program sees it and can add extra data
  625. */
  626. TEST_F(hid_bpf, test_multiply_events)
  627. {
  628. const struct test_program progs[] = {
  629. { .name = "hid_test_multiply_events" },
  630. };
  631. __u8 buf[10] = {0};
  632. int err;
  633. LOAD_PROGRAMS(progs);
  634. /* inject one event */
  635. buf[0] = 1;
  636. buf[1] = 42;
  637. uhid_send_event(_metadata, self->uhid_fd, buf, 6);
  638. /* read the data from hidraw */
  639. memset(buf, 0, sizeof(buf));
  640. err = read(self->hidraw_fd, buf, sizeof(buf));
  641. ASSERT_EQ(err, 9) TH_LOG("read_hidraw");
  642. ASSERT_EQ(buf[0], 2);
  643. ASSERT_EQ(buf[1], 47);
  644. /* read the data from hidraw */
  645. memset(buf, 0, sizeof(buf));
  646. err = read(self->hidraw_fd, buf, sizeof(buf));
  647. ASSERT_EQ(err, 9) TH_LOG("read_hidraw");
  648. ASSERT_EQ(buf[0], 2);
  649. ASSERT_EQ(buf[1], 52);
  650. }
  651. /*
  652. * Call hid_bpf_input_report against the given uhid device,
  653. * check that the program is not making infinite loops.
  654. */
  655. TEST_F(hid_bpf, test_hid_infinite_loop_input_report_call)
  656. {
  657. const struct test_program progs[] = {
  658. { .name = "hid_test_infinite_loop_input_report" },
  659. };
  660. __u8 buf[10] = {0};
  661. int err;
  662. LOAD_PROGRAMS(progs);
  663. /* emit hid_hw_output_report from hidraw */
  664. buf[0] = 1; /* report ID */
  665. buf[1] = 2;
  666. buf[2] = 42;
  667. uhid_send_event(_metadata, self->uhid_fd, buf, 6);
  668. /* read the data from hidraw */
  669. memset(buf, 0, sizeof(buf));
  670. err = read(self->hidraw_fd, buf, sizeof(buf));
  671. ASSERT_EQ(err, 6) TH_LOG("read_hidraw");
  672. ASSERT_EQ(buf[0], 1);
  673. ASSERT_EQ(buf[1], 3);
  674. /* read the data from hidraw: hid_bpf_try_input_report should work exactly one time */
  675. memset(buf, 0, sizeof(buf));
  676. err = read(self->hidraw_fd, buf, sizeof(buf));
  677. ASSERT_EQ(err, 6) TH_LOG("read_hidraw");
  678. ASSERT_EQ(buf[0], 1);
  679. ASSERT_EQ(buf[1], 4);
  680. /* read the data from hidraw: there should be none */
  681. memset(buf, 0, sizeof(buf));
  682. err = read(self->hidraw_fd, buf, sizeof(buf));
  683. ASSERT_EQ(err, -1) TH_LOG("read_hidraw");
  684. }
  685. /*
  686. * Attach hid_insert{0,1,2} to the given uhid device,
  687. * retrieve and open the matching hidraw node,
  688. * inject one event in the uhid device,
  689. * check that the programs have been inserted in the correct order.
  690. */
  691. TEST_F(hid_bpf, test_hid_attach_flags)
  692. {
  693. const struct test_program progs[] = {
  694. {
  695. .name = "hid_test_insert2",
  696. .insert_head = 0,
  697. },
  698. {
  699. .name = "hid_test_insert1",
  700. .insert_head = 1,
  701. },
  702. {
  703. .name = "hid_test_insert3",
  704. .insert_head = 0,
  705. },
  706. };
  707. __u8 buf[10] = {0};
  708. int err;
  709. LOAD_PROGRAMS(progs);
  710. /* inject one event */
  711. buf[0] = 1;
  712. uhid_send_event(_metadata, self->uhid_fd, buf, 6);
  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[1], 1);
  718. ASSERT_EQ(buf[2], 2);
  719. ASSERT_EQ(buf[3], 3);
  720. }
  721. /*
  722. * Attach hid_rdesc_fixup to the given uhid device,
  723. * retrieve and open the matching hidraw node,
  724. * check that the hidraw report descriptor has been updated.
  725. */
  726. TEST_F(hid_bpf, test_rdesc_fixup)
  727. {
  728. struct hidraw_report_descriptor rpt_desc = {0};
  729. const struct test_program progs[] = {
  730. { .name = "hid_rdesc_fixup" },
  731. };
  732. int err, desc_size;
  733. LOAD_PROGRAMS(progs);
  734. /* check that hid_rdesc_fixup() was executed */
  735. ASSERT_EQ(self->skel->data->callback2_check, 0x21);
  736. /* read the exposed report descriptor from hidraw */
  737. err = ioctl(self->hidraw_fd, HIDIOCGRDESCSIZE, &desc_size);
  738. ASSERT_GE(err, 0) TH_LOG("error while reading HIDIOCGRDESCSIZE: %d", err);
  739. /* ensure the new size of the rdesc is bigger than the old one */
  740. ASSERT_GT(desc_size, sizeof(rdesc));
  741. rpt_desc.size = desc_size;
  742. err = ioctl(self->hidraw_fd, HIDIOCGRDESC, &rpt_desc);
  743. ASSERT_GE(err, 0) TH_LOG("error while reading HIDIOCGRDESC: %d", err);
  744. ASSERT_EQ(rpt_desc.value[4], 0x42);
  745. }
  746. static int libbpf_print_fn(enum libbpf_print_level level,
  747. const char *format, va_list args)
  748. {
  749. char buf[1024];
  750. if (level == LIBBPF_DEBUG)
  751. return 0;
  752. snprintf(buf, sizeof(buf), "# %s", format);
  753. vfprintf(stdout, buf, args);
  754. return 0;
  755. }
  756. int main(int argc, char **argv)
  757. {
  758. /* Use libbpf 1.0 API mode */
  759. libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
  760. libbpf_set_print(libbpf_print_fn);
  761. return test_harness_run(argc, argv);
  762. }