handshake-test.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2023 Oracle and/or its affiliates.
  4. *
  5. * KUnit test of the handshake upcall mechanism.
  6. */
  7. #include <kunit/test.h>
  8. #include <kunit/visibility.h>
  9. #include <linux/kernel.h>
  10. #include <net/sock.h>
  11. #include <net/genetlink.h>
  12. #include <net/netns/generic.h>
  13. #include <uapi/linux/handshake.h>
  14. #include "handshake.h"
  15. MODULE_IMPORT_NS(EXPORTED_FOR_KUNIT_TESTING);
  16. static int test_accept_func(struct handshake_req *req, struct genl_info *info,
  17. int fd)
  18. {
  19. return 0;
  20. }
  21. static void test_done_func(struct handshake_req *req, unsigned int status,
  22. struct genl_info *info)
  23. {
  24. }
  25. struct handshake_req_alloc_test_param {
  26. const char *desc;
  27. struct handshake_proto *proto;
  28. gfp_t gfp;
  29. bool expect_success;
  30. };
  31. static struct handshake_proto handshake_req_alloc_proto_2 = {
  32. .hp_handler_class = HANDSHAKE_HANDLER_CLASS_NONE,
  33. };
  34. static struct handshake_proto handshake_req_alloc_proto_3 = {
  35. .hp_handler_class = HANDSHAKE_HANDLER_CLASS_MAX,
  36. };
  37. static struct handshake_proto handshake_req_alloc_proto_4 = {
  38. .hp_handler_class = HANDSHAKE_HANDLER_CLASS_TLSHD,
  39. };
  40. static struct handshake_proto handshake_req_alloc_proto_5 = {
  41. .hp_handler_class = HANDSHAKE_HANDLER_CLASS_TLSHD,
  42. .hp_accept = test_accept_func,
  43. };
  44. static struct handshake_proto handshake_req_alloc_proto_6 = {
  45. .hp_handler_class = HANDSHAKE_HANDLER_CLASS_TLSHD,
  46. .hp_privsize = UINT_MAX,
  47. .hp_accept = test_accept_func,
  48. .hp_done = test_done_func,
  49. };
  50. static struct handshake_proto handshake_req_alloc_proto_good = {
  51. .hp_handler_class = HANDSHAKE_HANDLER_CLASS_TLSHD,
  52. .hp_accept = test_accept_func,
  53. .hp_done = test_done_func,
  54. };
  55. static const
  56. struct handshake_req_alloc_test_param handshake_req_alloc_params[] = {
  57. {
  58. .desc = "handshake_req_alloc NULL proto",
  59. .proto = NULL,
  60. .gfp = GFP_KERNEL,
  61. .expect_success = false,
  62. },
  63. {
  64. .desc = "handshake_req_alloc CLASS_NONE",
  65. .proto = &handshake_req_alloc_proto_2,
  66. .gfp = GFP_KERNEL,
  67. .expect_success = false,
  68. },
  69. {
  70. .desc = "handshake_req_alloc CLASS_MAX",
  71. .proto = &handshake_req_alloc_proto_3,
  72. .gfp = GFP_KERNEL,
  73. .expect_success = false,
  74. },
  75. {
  76. .desc = "handshake_req_alloc no callbacks",
  77. .proto = &handshake_req_alloc_proto_4,
  78. .gfp = GFP_KERNEL,
  79. .expect_success = false,
  80. },
  81. {
  82. .desc = "handshake_req_alloc no done callback",
  83. .proto = &handshake_req_alloc_proto_5,
  84. .gfp = GFP_KERNEL,
  85. .expect_success = false,
  86. },
  87. {
  88. .desc = "handshake_req_alloc excessive privsize",
  89. .proto = &handshake_req_alloc_proto_6,
  90. .gfp = GFP_KERNEL | __GFP_NOWARN,
  91. .expect_success = false,
  92. },
  93. {
  94. .desc = "handshake_req_alloc all good",
  95. .proto = &handshake_req_alloc_proto_good,
  96. .gfp = GFP_KERNEL,
  97. .expect_success = true,
  98. },
  99. };
  100. static void
  101. handshake_req_alloc_get_desc(const struct handshake_req_alloc_test_param *param,
  102. char *desc)
  103. {
  104. strscpy(desc, param->desc, KUNIT_PARAM_DESC_SIZE);
  105. }
  106. /* Creates the function handshake_req_alloc_gen_params */
  107. KUNIT_ARRAY_PARAM(handshake_req_alloc, handshake_req_alloc_params,
  108. handshake_req_alloc_get_desc);
  109. static void handshake_req_alloc_case(struct kunit *test)
  110. {
  111. const struct handshake_req_alloc_test_param *param = test->param_value;
  112. struct handshake_req *result;
  113. /* Arrange */
  114. /* Act */
  115. result = handshake_req_alloc(param->proto, param->gfp);
  116. /* Assert */
  117. if (param->expect_success)
  118. KUNIT_EXPECT_NOT_NULL(test, result);
  119. else
  120. KUNIT_EXPECT_NULL(test, result);
  121. kfree(result);
  122. }
  123. static void handshake_req_submit_test1(struct kunit *test)
  124. {
  125. struct socket *sock;
  126. int err, result;
  127. /* Arrange */
  128. err = __sock_create(&init_net, PF_INET, SOCK_STREAM, IPPROTO_TCP,
  129. &sock, 1);
  130. KUNIT_ASSERT_EQ(test, err, 0);
  131. /* Act */
  132. result = handshake_req_submit(sock, NULL, GFP_KERNEL);
  133. /* Assert */
  134. KUNIT_EXPECT_EQ(test, result, -EINVAL);
  135. sock_release(sock);
  136. }
  137. static void handshake_req_submit_test2(struct kunit *test)
  138. {
  139. struct handshake_req *req;
  140. int result;
  141. /* Arrange */
  142. req = handshake_req_alloc(&handshake_req_alloc_proto_good, GFP_KERNEL);
  143. KUNIT_ASSERT_NOT_NULL(test, req);
  144. /* Act */
  145. result = handshake_req_submit(NULL, req, GFP_KERNEL);
  146. /* Assert */
  147. KUNIT_EXPECT_EQ(test, result, -EINVAL);
  148. /* handshake_req_submit() destroys @req on error */
  149. }
  150. static void handshake_req_submit_test3(struct kunit *test)
  151. {
  152. struct handshake_req *req;
  153. struct socket *sock;
  154. int err, result;
  155. /* Arrange */
  156. req = handshake_req_alloc(&handshake_req_alloc_proto_good, GFP_KERNEL);
  157. KUNIT_ASSERT_NOT_NULL(test, req);
  158. err = __sock_create(&init_net, PF_INET, SOCK_STREAM, IPPROTO_TCP,
  159. &sock, 1);
  160. KUNIT_ASSERT_EQ(test, err, 0);
  161. sock->file = NULL;
  162. /* Act */
  163. result = handshake_req_submit(sock, req, GFP_KERNEL);
  164. /* Assert */
  165. KUNIT_EXPECT_EQ(test, result, -EINVAL);
  166. /* handshake_req_submit() destroys @req on error */
  167. sock_release(sock);
  168. }
  169. static void handshake_req_submit_test4(struct kunit *test)
  170. {
  171. struct handshake_req *req, *result;
  172. struct socket *sock;
  173. struct file *filp;
  174. int err;
  175. /* Arrange */
  176. req = handshake_req_alloc(&handshake_req_alloc_proto_good, GFP_KERNEL);
  177. KUNIT_ASSERT_NOT_NULL(test, req);
  178. err = __sock_create(&init_net, PF_INET, SOCK_STREAM, IPPROTO_TCP,
  179. &sock, 1);
  180. KUNIT_ASSERT_EQ(test, err, 0);
  181. filp = sock_alloc_file(sock, O_NONBLOCK, NULL);
  182. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, filp);
  183. KUNIT_ASSERT_NOT_NULL(test, sock->sk);
  184. sock->file = filp;
  185. err = handshake_req_submit(sock, req, GFP_KERNEL);
  186. KUNIT_ASSERT_EQ(test, err, 0);
  187. /* Act */
  188. result = handshake_req_hash_lookup(sock->sk);
  189. /* Assert */
  190. KUNIT_EXPECT_NOT_NULL(test, result);
  191. KUNIT_EXPECT_PTR_EQ(test, req, result);
  192. handshake_req_cancel(sock->sk);
  193. fput(filp);
  194. }
  195. static void handshake_req_submit_test5(struct kunit *test)
  196. {
  197. struct handshake_req *req;
  198. struct handshake_net *hn;
  199. struct socket *sock;
  200. struct file *filp;
  201. struct net *net;
  202. int saved, err;
  203. /* Arrange */
  204. req = handshake_req_alloc(&handshake_req_alloc_proto_good, GFP_KERNEL);
  205. KUNIT_ASSERT_NOT_NULL(test, req);
  206. err = __sock_create(&init_net, PF_INET, SOCK_STREAM, IPPROTO_TCP,
  207. &sock, 1);
  208. KUNIT_ASSERT_EQ(test, err, 0);
  209. filp = sock_alloc_file(sock, O_NONBLOCK, NULL);
  210. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, filp);
  211. KUNIT_ASSERT_NOT_NULL(test, sock->sk);
  212. sock->file = filp;
  213. net = sock_net(sock->sk);
  214. hn = handshake_pernet(net);
  215. KUNIT_ASSERT_NOT_NULL(test, hn);
  216. saved = hn->hn_pending;
  217. hn->hn_pending = hn->hn_pending_max + 1;
  218. /* Act */
  219. err = handshake_req_submit(sock, req, GFP_KERNEL);
  220. /* Assert */
  221. KUNIT_EXPECT_EQ(test, err, -EAGAIN);
  222. fput(filp);
  223. hn->hn_pending = saved;
  224. }
  225. static void handshake_req_submit_test6(struct kunit *test)
  226. {
  227. struct handshake_req *req1, *req2;
  228. struct socket *sock;
  229. struct file *filp;
  230. int err;
  231. /* Arrange */
  232. req1 = handshake_req_alloc(&handshake_req_alloc_proto_good, GFP_KERNEL);
  233. KUNIT_ASSERT_NOT_NULL(test, req1);
  234. req2 = handshake_req_alloc(&handshake_req_alloc_proto_good, GFP_KERNEL);
  235. KUNIT_ASSERT_NOT_NULL(test, req2);
  236. err = __sock_create(&init_net, PF_INET, SOCK_STREAM, IPPROTO_TCP,
  237. &sock, 1);
  238. KUNIT_ASSERT_EQ(test, err, 0);
  239. filp = sock_alloc_file(sock, O_NONBLOCK, NULL);
  240. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, filp);
  241. KUNIT_ASSERT_NOT_NULL(test, sock->sk);
  242. sock->file = filp;
  243. /* Act */
  244. err = handshake_req_submit(sock, req1, GFP_KERNEL);
  245. KUNIT_ASSERT_EQ(test, err, 0);
  246. err = handshake_req_submit(sock, req2, GFP_KERNEL);
  247. /* Assert */
  248. KUNIT_EXPECT_EQ(test, err, -EBUSY);
  249. handshake_req_cancel(sock->sk);
  250. fput(filp);
  251. }
  252. static void handshake_req_cancel_test1(struct kunit *test)
  253. {
  254. struct handshake_req *req;
  255. struct socket *sock;
  256. struct file *filp;
  257. bool result;
  258. int err;
  259. /* Arrange */
  260. req = handshake_req_alloc(&handshake_req_alloc_proto_good, GFP_KERNEL);
  261. KUNIT_ASSERT_NOT_NULL(test, req);
  262. err = __sock_create(&init_net, PF_INET, SOCK_STREAM, IPPROTO_TCP,
  263. &sock, 1);
  264. KUNIT_ASSERT_EQ(test, err, 0);
  265. filp = sock_alloc_file(sock, O_NONBLOCK, NULL);
  266. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, filp);
  267. sock->file = filp;
  268. err = handshake_req_submit(sock, req, GFP_KERNEL);
  269. KUNIT_ASSERT_EQ(test, err, 0);
  270. /* NB: handshake_req hasn't been accepted */
  271. /* Act */
  272. result = handshake_req_cancel(sock->sk);
  273. /* Assert */
  274. KUNIT_EXPECT_TRUE(test, result);
  275. fput(filp);
  276. }
  277. static void handshake_req_cancel_test2(struct kunit *test)
  278. {
  279. struct handshake_req *req, *next;
  280. struct handshake_net *hn;
  281. struct socket *sock;
  282. struct file *filp;
  283. struct net *net;
  284. bool result;
  285. int err;
  286. /* Arrange */
  287. req = handshake_req_alloc(&handshake_req_alloc_proto_good, GFP_KERNEL);
  288. KUNIT_ASSERT_NOT_NULL(test, req);
  289. err = __sock_create(&init_net, PF_INET, SOCK_STREAM, IPPROTO_TCP,
  290. &sock, 1);
  291. KUNIT_ASSERT_EQ(test, err, 0);
  292. filp = sock_alloc_file(sock, O_NONBLOCK, NULL);
  293. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, filp);
  294. sock->file = filp;
  295. err = handshake_req_submit(sock, req, GFP_KERNEL);
  296. KUNIT_ASSERT_EQ(test, err, 0);
  297. net = sock_net(sock->sk);
  298. hn = handshake_pernet(net);
  299. KUNIT_ASSERT_NOT_NULL(test, hn);
  300. /* Pretend to accept this request */
  301. next = handshake_req_next(hn, HANDSHAKE_HANDLER_CLASS_TLSHD);
  302. KUNIT_ASSERT_PTR_EQ(test, req, next);
  303. /* Act */
  304. result = handshake_req_cancel(sock->sk);
  305. /* Assert */
  306. KUNIT_EXPECT_TRUE(test, result);
  307. fput(filp);
  308. }
  309. static void handshake_req_cancel_test3(struct kunit *test)
  310. {
  311. struct handshake_req *req, *next;
  312. struct handshake_net *hn;
  313. struct socket *sock;
  314. struct file *filp;
  315. struct net *net;
  316. bool result;
  317. int err;
  318. /* Arrange */
  319. req = handshake_req_alloc(&handshake_req_alloc_proto_good, GFP_KERNEL);
  320. KUNIT_ASSERT_NOT_NULL(test, req);
  321. err = __sock_create(&init_net, PF_INET, SOCK_STREAM, IPPROTO_TCP,
  322. &sock, 1);
  323. KUNIT_ASSERT_EQ(test, err, 0);
  324. filp = sock_alloc_file(sock, O_NONBLOCK, NULL);
  325. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, filp);
  326. sock->file = filp;
  327. err = handshake_req_submit(sock, req, GFP_KERNEL);
  328. KUNIT_ASSERT_EQ(test, err, 0);
  329. net = sock_net(sock->sk);
  330. hn = handshake_pernet(net);
  331. KUNIT_ASSERT_NOT_NULL(test, hn);
  332. /* Pretend to accept this request */
  333. next = handshake_req_next(hn, HANDSHAKE_HANDLER_CLASS_TLSHD);
  334. KUNIT_ASSERT_PTR_EQ(test, req, next);
  335. /* Pretend to complete this request */
  336. handshake_complete(next, -ETIMEDOUT, NULL);
  337. /* Act */
  338. result = handshake_req_cancel(sock->sk);
  339. /* Assert */
  340. KUNIT_EXPECT_FALSE(test, result);
  341. fput(filp);
  342. }
  343. static struct handshake_req *handshake_req_destroy_test;
  344. static void test_destroy_func(struct handshake_req *req)
  345. {
  346. handshake_req_destroy_test = req;
  347. }
  348. static struct handshake_proto handshake_req_alloc_proto_destroy = {
  349. .hp_handler_class = HANDSHAKE_HANDLER_CLASS_TLSHD,
  350. .hp_accept = test_accept_func,
  351. .hp_done = test_done_func,
  352. .hp_destroy = test_destroy_func,
  353. };
  354. static void handshake_req_destroy_test1(struct kunit *test)
  355. {
  356. struct handshake_req *req;
  357. struct socket *sock;
  358. struct file *filp;
  359. int err;
  360. /* Arrange */
  361. handshake_req_destroy_test = NULL;
  362. req = handshake_req_alloc(&handshake_req_alloc_proto_destroy, GFP_KERNEL);
  363. KUNIT_ASSERT_NOT_NULL(test, req);
  364. err = __sock_create(&init_net, PF_INET, SOCK_STREAM, IPPROTO_TCP,
  365. &sock, 1);
  366. KUNIT_ASSERT_EQ(test, err, 0);
  367. filp = sock_alloc_file(sock, O_NONBLOCK, NULL);
  368. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, filp);
  369. sock->file = filp;
  370. err = handshake_req_submit(sock, req, GFP_KERNEL);
  371. KUNIT_ASSERT_EQ(test, err, 0);
  372. handshake_req_cancel(sock->sk);
  373. /* Act */
  374. /* Ensure the close/release/put process has run to
  375. * completion before checking the result.
  376. */
  377. __fput_sync(filp);
  378. /* Assert */
  379. KUNIT_EXPECT_PTR_EQ(test, handshake_req_destroy_test, req);
  380. }
  381. static struct kunit_case handshake_api_test_cases[] = {
  382. {
  383. .name = "req_alloc API fuzzing",
  384. .run_case = handshake_req_alloc_case,
  385. .generate_params = handshake_req_alloc_gen_params,
  386. },
  387. {
  388. .name = "req_submit NULL req arg",
  389. .run_case = handshake_req_submit_test1,
  390. },
  391. {
  392. .name = "req_submit NULL sock arg",
  393. .run_case = handshake_req_submit_test2,
  394. },
  395. {
  396. .name = "req_submit NULL sock->file",
  397. .run_case = handshake_req_submit_test3,
  398. },
  399. {
  400. .name = "req_lookup works",
  401. .run_case = handshake_req_submit_test4,
  402. },
  403. {
  404. .name = "req_submit max pending",
  405. .run_case = handshake_req_submit_test5,
  406. },
  407. {
  408. .name = "req_submit multiple",
  409. .run_case = handshake_req_submit_test6,
  410. },
  411. {
  412. .name = "req_cancel before accept",
  413. .run_case = handshake_req_cancel_test1,
  414. },
  415. {
  416. .name = "req_cancel after accept",
  417. .run_case = handshake_req_cancel_test2,
  418. },
  419. {
  420. .name = "req_cancel after done",
  421. .run_case = handshake_req_cancel_test3,
  422. },
  423. {
  424. .name = "req_destroy works",
  425. .run_case = handshake_req_destroy_test1,
  426. },
  427. {}
  428. };
  429. static struct kunit_suite handshake_api_suite = {
  430. .name = "Handshake API tests",
  431. .test_cases = handshake_api_test_cases,
  432. };
  433. kunit_test_suites(&handshake_api_suite);
  434. MODULE_DESCRIPTION("Test handshake upcall API functions");
  435. MODULE_LICENSE("GPL");