tls_strp.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2016 Tom Herbert <[email protected]> */
  3. #include <linux/skbuff.h>
  4. #include <linux/workqueue.h>
  5. #include <net/strparser.h>
  6. #include <net/tcp.h>
  7. #include <net/sock.h>
  8. #include <net/tls.h>
  9. #include "tls.h"
  10. static struct workqueue_struct *tls_strp_wq;
  11. static void tls_strp_abort_strp(struct tls_strparser *strp, int err)
  12. {
  13. if (strp->stopped)
  14. return;
  15. strp->stopped = 1;
  16. /* Report an error on the lower socket */
  17. WRITE_ONCE(strp->sk->sk_err, -err);
  18. /* Paired with smp_rmb() in tcp_poll() */
  19. smp_wmb();
  20. sk_error_report(strp->sk);
  21. }
  22. static void tls_strp_anchor_free(struct tls_strparser *strp)
  23. {
  24. struct skb_shared_info *shinfo = skb_shinfo(strp->anchor);
  25. DEBUG_NET_WARN_ON_ONCE(atomic_read(&shinfo->dataref) != 1);
  26. if (!strp->copy_mode)
  27. shinfo->frag_list = NULL;
  28. consume_skb(strp->anchor);
  29. strp->anchor = NULL;
  30. }
  31. static struct sk_buff *
  32. tls_strp_skb_copy(struct tls_strparser *strp, struct sk_buff *in_skb,
  33. int offset, int len)
  34. {
  35. struct sk_buff *skb;
  36. int i, err;
  37. skb = alloc_skb_with_frags(0, len, TLS_PAGE_ORDER,
  38. &err, strp->sk->sk_allocation);
  39. if (!skb)
  40. return NULL;
  41. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  42. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  43. WARN_ON_ONCE(skb_copy_bits(in_skb, offset,
  44. skb_frag_address(frag),
  45. skb_frag_size(frag)));
  46. offset += skb_frag_size(frag);
  47. }
  48. skb->len = len;
  49. skb->data_len = len;
  50. skb_copy_header(skb, in_skb);
  51. return skb;
  52. }
  53. /* Create a new skb with the contents of input copied to its page frags */
  54. static struct sk_buff *tls_strp_msg_make_copy(struct tls_strparser *strp)
  55. {
  56. struct strp_msg *rxm;
  57. struct sk_buff *skb;
  58. skb = tls_strp_skb_copy(strp, strp->anchor, strp->stm.offset,
  59. strp->stm.full_len);
  60. if (!skb)
  61. return NULL;
  62. rxm = strp_msg(skb);
  63. rxm->offset = 0;
  64. return skb;
  65. }
  66. /* Steal the input skb, input msg is invalid after calling this function */
  67. struct sk_buff *tls_strp_msg_detach(struct tls_sw_context_rx *ctx)
  68. {
  69. struct tls_strparser *strp = &ctx->strp;
  70. #ifdef CONFIG_TLS_DEVICE
  71. DEBUG_NET_WARN_ON_ONCE(!strp->anchor->decrypted);
  72. #else
  73. /* This function turns an input into an output,
  74. * that can only happen if we have offload.
  75. */
  76. WARN_ON(1);
  77. #endif
  78. if (strp->copy_mode) {
  79. struct sk_buff *skb;
  80. /* Replace anchor with an empty skb, this is a little
  81. * dangerous but __tls_cur_msg() warns on empty skbs
  82. * so hopefully we'll catch abuses.
  83. */
  84. skb = alloc_skb(0, strp->sk->sk_allocation);
  85. if (!skb)
  86. return NULL;
  87. swap(strp->anchor, skb);
  88. return skb;
  89. }
  90. return tls_strp_msg_make_copy(strp);
  91. }
  92. /* Force the input skb to be in copy mode. The data ownership remains
  93. * with the input skb itself (meaning unpause will wipe it) but it can
  94. * be modified.
  95. */
  96. int tls_strp_msg_cow(struct tls_sw_context_rx *ctx)
  97. {
  98. struct tls_strparser *strp = &ctx->strp;
  99. struct sk_buff *skb;
  100. if (strp->copy_mode)
  101. return 0;
  102. skb = tls_strp_msg_make_copy(strp);
  103. if (!skb)
  104. return -ENOMEM;
  105. tls_strp_anchor_free(strp);
  106. strp->anchor = skb;
  107. tcp_read_done(strp->sk, strp->stm.full_len);
  108. strp->copy_mode = 1;
  109. return 0;
  110. }
  111. /* Make a clone (in the skb sense) of the input msg to keep a reference
  112. * to the underlying data. The reference-holding skbs get placed on
  113. * @dst.
  114. */
  115. int tls_strp_msg_hold(struct tls_strparser *strp, struct sk_buff_head *dst)
  116. {
  117. struct skb_shared_info *shinfo = skb_shinfo(strp->anchor);
  118. if (strp->copy_mode) {
  119. struct sk_buff *skb;
  120. WARN_ON_ONCE(!shinfo->nr_frags);
  121. /* We can't skb_clone() the anchor, it gets wiped by unpause */
  122. skb = alloc_skb(0, strp->sk->sk_allocation);
  123. if (!skb)
  124. return -ENOMEM;
  125. __skb_queue_tail(dst, strp->anchor);
  126. strp->anchor = skb;
  127. } else {
  128. struct sk_buff *iter, *clone;
  129. int chunk, len, offset;
  130. offset = strp->stm.offset;
  131. len = strp->stm.full_len;
  132. iter = shinfo->frag_list;
  133. while (len > 0) {
  134. if (iter->len <= offset) {
  135. offset -= iter->len;
  136. goto next;
  137. }
  138. chunk = iter->len - offset;
  139. offset = 0;
  140. clone = skb_clone(iter, strp->sk->sk_allocation);
  141. if (!clone)
  142. return -ENOMEM;
  143. __skb_queue_tail(dst, clone);
  144. len -= chunk;
  145. next:
  146. iter = iter->next;
  147. }
  148. }
  149. return 0;
  150. }
  151. static void tls_strp_flush_anchor_copy(struct tls_strparser *strp)
  152. {
  153. struct skb_shared_info *shinfo = skb_shinfo(strp->anchor);
  154. int i;
  155. DEBUG_NET_WARN_ON_ONCE(atomic_read(&shinfo->dataref) != 1);
  156. for (i = 0; i < shinfo->nr_frags; i++)
  157. __skb_frag_unref(&shinfo->frags[i], false);
  158. shinfo->nr_frags = 0;
  159. if (strp->copy_mode) {
  160. kfree_skb_list(shinfo->frag_list);
  161. shinfo->frag_list = NULL;
  162. }
  163. strp->copy_mode = 0;
  164. strp->mixed_decrypted = 0;
  165. }
  166. static int tls_strp_copyin_frag(struct tls_strparser *strp, struct sk_buff *skb,
  167. struct sk_buff *in_skb, unsigned int offset,
  168. size_t in_len)
  169. {
  170. size_t len, chunk;
  171. skb_frag_t *frag;
  172. int sz;
  173. frag = &skb_shinfo(skb)->frags[skb->len / PAGE_SIZE];
  174. len = in_len;
  175. /* First make sure we got the header */
  176. if (!strp->stm.full_len) {
  177. /* Assume one page is more than enough for headers */
  178. chunk = min_t(size_t, len, PAGE_SIZE - skb_frag_size(frag));
  179. WARN_ON_ONCE(skb_copy_bits(in_skb, offset,
  180. skb_frag_address(frag) +
  181. skb_frag_size(frag),
  182. chunk));
  183. skb->len += chunk;
  184. skb->data_len += chunk;
  185. skb_frag_size_add(frag, chunk);
  186. sz = tls_rx_msg_size(strp, skb);
  187. if (sz < 0)
  188. return sz;
  189. /* We may have over-read, sz == 0 is guaranteed under-read */
  190. if (unlikely(sz && sz < skb->len)) {
  191. int over = skb->len - sz;
  192. WARN_ON_ONCE(over > chunk);
  193. skb->len -= over;
  194. skb->data_len -= over;
  195. skb_frag_size_add(frag, -over);
  196. chunk -= over;
  197. }
  198. frag++;
  199. len -= chunk;
  200. offset += chunk;
  201. strp->stm.full_len = sz;
  202. if (!strp->stm.full_len)
  203. goto read_done;
  204. }
  205. /* Load up more data */
  206. while (len && strp->stm.full_len > skb->len) {
  207. chunk = min_t(size_t, len, strp->stm.full_len - skb->len);
  208. chunk = min_t(size_t, chunk, PAGE_SIZE - skb_frag_size(frag));
  209. WARN_ON_ONCE(skb_copy_bits(in_skb, offset,
  210. skb_frag_address(frag) +
  211. skb_frag_size(frag),
  212. chunk));
  213. skb->len += chunk;
  214. skb->data_len += chunk;
  215. skb_frag_size_add(frag, chunk);
  216. frag++;
  217. len -= chunk;
  218. offset += chunk;
  219. }
  220. read_done:
  221. return in_len - len;
  222. }
  223. static int tls_strp_copyin_skb(struct tls_strparser *strp, struct sk_buff *skb,
  224. struct sk_buff *in_skb, unsigned int offset,
  225. size_t in_len)
  226. {
  227. struct sk_buff *nskb, *first, *last;
  228. struct skb_shared_info *shinfo;
  229. size_t chunk;
  230. int sz;
  231. if (strp->stm.full_len)
  232. chunk = strp->stm.full_len - skb->len;
  233. else
  234. chunk = TLS_MAX_PAYLOAD_SIZE + PAGE_SIZE;
  235. chunk = min(chunk, in_len);
  236. nskb = tls_strp_skb_copy(strp, in_skb, offset, chunk);
  237. if (!nskb)
  238. return -ENOMEM;
  239. shinfo = skb_shinfo(skb);
  240. if (!shinfo->frag_list) {
  241. shinfo->frag_list = nskb;
  242. nskb->prev = nskb;
  243. } else {
  244. first = shinfo->frag_list;
  245. last = first->prev;
  246. last->next = nskb;
  247. first->prev = nskb;
  248. }
  249. skb->len += chunk;
  250. skb->data_len += chunk;
  251. if (!strp->stm.full_len) {
  252. sz = tls_rx_msg_size(strp, skb);
  253. if (sz < 0)
  254. return sz;
  255. /* We may have over-read, sz == 0 is guaranteed under-read */
  256. if (unlikely(sz && sz < skb->len)) {
  257. int over = skb->len - sz;
  258. WARN_ON_ONCE(over > chunk);
  259. skb->len -= over;
  260. skb->data_len -= over;
  261. __pskb_trim(nskb, nskb->len - over);
  262. chunk -= over;
  263. }
  264. strp->stm.full_len = sz;
  265. }
  266. return chunk;
  267. }
  268. static int tls_strp_copyin(read_descriptor_t *desc, struct sk_buff *in_skb,
  269. unsigned int offset, size_t in_len)
  270. {
  271. struct tls_strparser *strp = (struct tls_strparser *)desc->arg.data;
  272. struct sk_buff *skb;
  273. int ret;
  274. if (strp->msg_ready)
  275. return 0;
  276. skb = strp->anchor;
  277. if (!skb->len)
  278. skb_copy_decrypted(skb, in_skb);
  279. else
  280. strp->mixed_decrypted |= !!skb_cmp_decrypted(skb, in_skb);
  281. if (IS_ENABLED(CONFIG_TLS_DEVICE) && strp->mixed_decrypted)
  282. ret = tls_strp_copyin_skb(strp, skb, in_skb, offset, in_len);
  283. else
  284. ret = tls_strp_copyin_frag(strp, skb, in_skb, offset, in_len);
  285. if (ret < 0) {
  286. desc->error = ret;
  287. ret = 0;
  288. }
  289. if (strp->stm.full_len && strp->stm.full_len == skb->len) {
  290. desc->count = 0;
  291. strp->msg_ready = 1;
  292. tls_rx_msg_ready(strp);
  293. }
  294. return ret;
  295. }
  296. static int tls_strp_read_copyin(struct tls_strparser *strp)
  297. {
  298. struct socket *sock = strp->sk->sk_socket;
  299. read_descriptor_t desc;
  300. desc.arg.data = strp;
  301. desc.error = 0;
  302. desc.count = 1; /* give more than one skb per call */
  303. /* sk should be locked here, so okay to do read_sock */
  304. sock->ops->read_sock(strp->sk, &desc, tls_strp_copyin);
  305. return desc.error;
  306. }
  307. static int tls_strp_read_copy(struct tls_strparser *strp, bool qshort)
  308. {
  309. struct skb_shared_info *shinfo;
  310. struct page *page;
  311. int need_spc, len;
  312. /* If the rbuf is small or rcv window has collapsed to 0 we need
  313. * to read the data out. Otherwise the connection will stall.
  314. * Without pressure threshold of INT_MAX will never be ready.
  315. */
  316. if (likely(qshort && !tcp_epollin_ready(strp->sk, INT_MAX)))
  317. return 0;
  318. shinfo = skb_shinfo(strp->anchor);
  319. shinfo->frag_list = NULL;
  320. /* If we don't know the length go max plus page for cipher overhead */
  321. need_spc = strp->stm.full_len ?: TLS_MAX_PAYLOAD_SIZE + PAGE_SIZE;
  322. for (len = need_spc; len > 0; len -= PAGE_SIZE) {
  323. page = alloc_page(strp->sk->sk_allocation);
  324. if (!page) {
  325. tls_strp_flush_anchor_copy(strp);
  326. return -ENOMEM;
  327. }
  328. skb_fill_page_desc(strp->anchor, shinfo->nr_frags++,
  329. page, 0, 0);
  330. }
  331. strp->copy_mode = 1;
  332. strp->stm.offset = 0;
  333. strp->anchor->len = 0;
  334. strp->anchor->data_len = 0;
  335. strp->anchor->truesize = round_up(need_spc, PAGE_SIZE);
  336. tls_strp_read_copyin(strp);
  337. return 0;
  338. }
  339. static bool tls_strp_check_queue_ok(struct tls_strparser *strp)
  340. {
  341. unsigned int len = strp->stm.offset + strp->stm.full_len;
  342. struct sk_buff *first, *skb;
  343. u32 seq;
  344. first = skb_shinfo(strp->anchor)->frag_list;
  345. skb = first;
  346. seq = TCP_SKB_CB(first)->seq;
  347. /* Make sure there's no duplicate data in the queue,
  348. * and the decrypted status matches.
  349. */
  350. while (skb->len < len) {
  351. seq += skb->len;
  352. len -= skb->len;
  353. skb = skb->next;
  354. if (TCP_SKB_CB(skb)->seq != seq)
  355. return false;
  356. if (skb_cmp_decrypted(first, skb))
  357. return false;
  358. }
  359. return true;
  360. }
  361. static void tls_strp_load_anchor_with_queue(struct tls_strparser *strp, int len)
  362. {
  363. struct tcp_sock *tp = tcp_sk(strp->sk);
  364. struct sk_buff *first;
  365. u32 offset;
  366. first = tcp_recv_skb(strp->sk, tp->copied_seq, &offset);
  367. if (WARN_ON_ONCE(!first))
  368. return;
  369. /* Bestow the state onto the anchor */
  370. strp->anchor->len = offset + len;
  371. strp->anchor->data_len = offset + len;
  372. strp->anchor->truesize = offset + len;
  373. skb_shinfo(strp->anchor)->frag_list = first;
  374. skb_copy_header(strp->anchor, first);
  375. strp->anchor->destructor = NULL;
  376. strp->stm.offset = offset;
  377. }
  378. void tls_strp_msg_load(struct tls_strparser *strp, bool force_refresh)
  379. {
  380. struct strp_msg *rxm;
  381. struct tls_msg *tlm;
  382. DEBUG_NET_WARN_ON_ONCE(!strp->msg_ready);
  383. DEBUG_NET_WARN_ON_ONCE(!strp->stm.full_len);
  384. if (!strp->copy_mode && force_refresh) {
  385. if (WARN_ON(tcp_inq(strp->sk) < strp->stm.full_len))
  386. return;
  387. tls_strp_load_anchor_with_queue(strp, strp->stm.full_len);
  388. }
  389. rxm = strp_msg(strp->anchor);
  390. rxm->full_len = strp->stm.full_len;
  391. rxm->offset = strp->stm.offset;
  392. tlm = tls_msg(strp->anchor);
  393. tlm->control = strp->mark;
  394. }
  395. /* Called with lock held on lower socket */
  396. static int tls_strp_read_sock(struct tls_strparser *strp)
  397. {
  398. int sz, inq;
  399. inq = tcp_inq(strp->sk);
  400. if (inq < 1)
  401. return 0;
  402. if (unlikely(strp->copy_mode))
  403. return tls_strp_read_copyin(strp);
  404. if (inq < strp->stm.full_len)
  405. return tls_strp_read_copy(strp, true);
  406. if (!strp->stm.full_len) {
  407. tls_strp_load_anchor_with_queue(strp, inq);
  408. sz = tls_rx_msg_size(strp, strp->anchor);
  409. if (sz < 0) {
  410. tls_strp_abort_strp(strp, sz);
  411. return sz;
  412. }
  413. strp->stm.full_len = sz;
  414. if (!strp->stm.full_len || inq < strp->stm.full_len)
  415. return tls_strp_read_copy(strp, true);
  416. }
  417. if (!tls_strp_check_queue_ok(strp))
  418. return tls_strp_read_copy(strp, false);
  419. strp->msg_ready = 1;
  420. tls_rx_msg_ready(strp);
  421. return 0;
  422. }
  423. void tls_strp_check_rcv(struct tls_strparser *strp)
  424. {
  425. if (unlikely(strp->stopped) || strp->msg_ready)
  426. return;
  427. if (tls_strp_read_sock(strp) == -ENOMEM)
  428. queue_work(tls_strp_wq, &strp->work);
  429. }
  430. /* Lower sock lock held */
  431. void tls_strp_data_ready(struct tls_strparser *strp)
  432. {
  433. /* This check is needed to synchronize with do_tls_strp_work.
  434. * do_tls_strp_work acquires a process lock (lock_sock) whereas
  435. * the lock held here is bh_lock_sock. The two locks can be
  436. * held by different threads at the same time, but bh_lock_sock
  437. * allows a thread in BH context to safely check if the process
  438. * lock is held. In this case, if the lock is held, queue work.
  439. */
  440. if (sock_owned_by_user_nocheck(strp->sk)) {
  441. queue_work(tls_strp_wq, &strp->work);
  442. return;
  443. }
  444. tls_strp_check_rcv(strp);
  445. }
  446. static void tls_strp_work(struct work_struct *w)
  447. {
  448. struct tls_strparser *strp =
  449. container_of(w, struct tls_strparser, work);
  450. lock_sock(strp->sk);
  451. tls_strp_check_rcv(strp);
  452. release_sock(strp->sk);
  453. }
  454. void tls_strp_msg_done(struct tls_strparser *strp)
  455. {
  456. WARN_ON(!strp->stm.full_len);
  457. if (likely(!strp->copy_mode))
  458. tcp_read_done(strp->sk, strp->stm.full_len);
  459. else
  460. tls_strp_flush_anchor_copy(strp);
  461. strp->msg_ready = 0;
  462. memset(&strp->stm, 0, sizeof(strp->stm));
  463. tls_strp_check_rcv(strp);
  464. }
  465. void tls_strp_stop(struct tls_strparser *strp)
  466. {
  467. strp->stopped = 1;
  468. }
  469. int tls_strp_init(struct tls_strparser *strp, struct sock *sk)
  470. {
  471. memset(strp, 0, sizeof(*strp));
  472. strp->sk = sk;
  473. strp->anchor = alloc_skb(0, GFP_KERNEL);
  474. if (!strp->anchor)
  475. return -ENOMEM;
  476. INIT_WORK(&strp->work, tls_strp_work);
  477. return 0;
  478. }
  479. /* strp must already be stopped so that tls_strp_recv will no longer be called.
  480. * Note that tls_strp_done is not called with the lower socket held.
  481. */
  482. void tls_strp_done(struct tls_strparser *strp)
  483. {
  484. WARN_ON(!strp->stopped);
  485. cancel_work_sync(&strp->work);
  486. tls_strp_anchor_free(strp);
  487. }
  488. int __init tls_strp_dev_init(void)
  489. {
  490. tls_strp_wq = create_workqueue("tls-strp");
  491. if (unlikely(!tls_strp_wq))
  492. return -ENOMEM;
  493. return 0;
  494. }
  495. void tls_strp_dev_exit(void)
  496. {
  497. destroy_workqueue(tls_strp_wq);
  498. }