tls_strp.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2016 Tom Herbert <tom@herbertland.com> */
  3. #include <linux/skbuff.h>
  4. #include <linux/skbuff_ref.h>
  5. #include <linux/workqueue.h>
  6. #include <net/strparser.h>
  7. #include <net/tcp.h>
  8. #include <net/sock.h>
  9. #include <net/tls.h>
  10. #include "tls.h"
  11. static struct workqueue_struct *tls_strp_wq;
  12. static void tls_strp_abort_strp(struct tls_strparser *strp, int err)
  13. {
  14. if (strp->stopped)
  15. return;
  16. strp->stopped = 1;
  17. /* Report an error on the lower socket */
  18. WRITE_ONCE(strp->sk->sk_err, -err);
  19. /* Paired with smp_rmb() in tcp_poll() */
  20. smp_wmb();
  21. sk_error_report(strp->sk);
  22. }
  23. static void tls_strp_anchor_free(struct tls_strparser *strp)
  24. {
  25. struct skb_shared_info *shinfo = skb_shinfo(strp->anchor);
  26. DEBUG_NET_WARN_ON_ONCE(atomic_read(&shinfo->dataref) != 1);
  27. if (!strp->copy_mode)
  28. shinfo->frag_list = NULL;
  29. consume_skb(strp->anchor);
  30. strp->anchor = NULL;
  31. }
  32. static struct sk_buff *
  33. tls_strp_skb_copy(struct tls_strparser *strp, struct sk_buff *in_skb,
  34. int offset, int len)
  35. {
  36. struct sk_buff *skb;
  37. int i, err;
  38. skb = alloc_skb_with_frags(0, len, TLS_PAGE_ORDER,
  39. &err, strp->sk->sk_allocation);
  40. if (!skb)
  41. return NULL;
  42. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  43. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  44. WARN_ON_ONCE(skb_copy_bits(in_skb, offset,
  45. skb_frag_address(frag),
  46. skb_frag_size(frag)));
  47. offset += skb_frag_size(frag);
  48. }
  49. skb->len = len;
  50. skb->data_len = len;
  51. skb_copy_header(skb, in_skb);
  52. return skb;
  53. }
  54. /* Create a new skb with the contents of input copied to its page frags */
  55. static struct sk_buff *tls_strp_msg_make_copy(struct tls_strparser *strp)
  56. {
  57. struct strp_msg *rxm;
  58. struct sk_buff *skb;
  59. skb = tls_strp_skb_copy(strp, strp->anchor, strp->stm.offset,
  60. strp->stm.full_len);
  61. if (!skb)
  62. return NULL;
  63. rxm = strp_msg(skb);
  64. rxm->offset = 0;
  65. return skb;
  66. }
  67. /* Steal the input skb, input msg is invalid after calling this function */
  68. struct sk_buff *tls_strp_msg_detach(struct tls_sw_context_rx *ctx)
  69. {
  70. struct tls_strparser *strp = &ctx->strp;
  71. #ifdef CONFIG_TLS_DEVICE
  72. DEBUG_NET_WARN_ON_ONCE(!strp->anchor->decrypted);
  73. #else
  74. /* This function turns an input into an output,
  75. * that can only happen if we have offload.
  76. */
  77. WARN_ON(1);
  78. #endif
  79. if (strp->copy_mode) {
  80. struct sk_buff *skb;
  81. /* Replace anchor with an empty skb, this is a little
  82. * dangerous but __tls_cur_msg() warns on empty skbs
  83. * so hopefully we'll catch abuses.
  84. */
  85. skb = alloc_skb(0, strp->sk->sk_allocation);
  86. if (!skb)
  87. return NULL;
  88. swap(strp->anchor, skb);
  89. return skb;
  90. }
  91. return tls_strp_msg_make_copy(strp);
  92. }
  93. /* Force the input skb to be in copy mode. The data ownership remains
  94. * with the input skb itself (meaning unpause will wipe it) but it can
  95. * be modified.
  96. */
  97. int tls_strp_msg_cow(struct tls_sw_context_rx *ctx)
  98. {
  99. struct tls_strparser *strp = &ctx->strp;
  100. struct sk_buff *skb;
  101. if (strp->copy_mode)
  102. return 0;
  103. skb = tls_strp_msg_make_copy(strp);
  104. if (!skb)
  105. return -ENOMEM;
  106. tls_strp_anchor_free(strp);
  107. strp->anchor = skb;
  108. tcp_read_done(strp->sk, strp->stm.full_len);
  109. strp->copy_mode = 1;
  110. return 0;
  111. }
  112. /* Make a clone (in the skb sense) of the input msg to keep a reference
  113. * to the underlying data. The reference-holding skbs get placed on
  114. * @dst.
  115. */
  116. int tls_strp_msg_hold(struct tls_strparser *strp, struct sk_buff_head *dst)
  117. {
  118. struct skb_shared_info *shinfo = skb_shinfo(strp->anchor);
  119. if (strp->copy_mode) {
  120. struct sk_buff *skb;
  121. WARN_ON_ONCE(!shinfo->nr_frags);
  122. /* We can't skb_clone() the anchor, it gets wiped by unpause */
  123. skb = alloc_skb(0, strp->sk->sk_allocation);
  124. if (!skb)
  125. return -ENOMEM;
  126. __skb_queue_tail(dst, strp->anchor);
  127. strp->anchor = skb;
  128. } else {
  129. struct sk_buff *iter, *clone;
  130. int chunk, len, offset;
  131. offset = strp->stm.offset;
  132. len = strp->stm.full_len;
  133. iter = shinfo->frag_list;
  134. while (len > 0) {
  135. if (iter->len <= offset) {
  136. offset -= iter->len;
  137. goto next;
  138. }
  139. chunk = iter->len - offset;
  140. offset = 0;
  141. clone = skb_clone(iter, strp->sk->sk_allocation);
  142. if (!clone)
  143. return -ENOMEM;
  144. __skb_queue_tail(dst, clone);
  145. len -= chunk;
  146. next:
  147. iter = iter->next;
  148. }
  149. }
  150. return 0;
  151. }
  152. static void tls_strp_flush_anchor_copy(struct tls_strparser *strp)
  153. {
  154. struct skb_shared_info *shinfo = skb_shinfo(strp->anchor);
  155. int i;
  156. DEBUG_NET_WARN_ON_ONCE(atomic_read(&shinfo->dataref) != 1);
  157. for (i = 0; i < shinfo->nr_frags; i++)
  158. __skb_frag_unref(&shinfo->frags[i], false);
  159. shinfo->nr_frags = 0;
  160. if (strp->copy_mode) {
  161. kfree_skb_list(shinfo->frag_list);
  162. shinfo->frag_list = NULL;
  163. }
  164. strp->copy_mode = 0;
  165. strp->mixed_decrypted = 0;
  166. }
  167. static int tls_strp_copyin_frag(struct tls_strparser *strp, struct sk_buff *skb,
  168. struct sk_buff *in_skb, unsigned int offset,
  169. size_t in_len)
  170. {
  171. size_t len, chunk;
  172. skb_frag_t *frag;
  173. int sz;
  174. frag = &skb_shinfo(skb)->frags[skb->len / PAGE_SIZE];
  175. len = in_len;
  176. /* First make sure we got the header */
  177. if (!strp->stm.full_len) {
  178. /* Assume one page is more than enough for headers */
  179. chunk = min_t(size_t, len, PAGE_SIZE - skb_frag_size(frag));
  180. WARN_ON_ONCE(skb_copy_bits(in_skb, offset,
  181. skb_frag_address(frag) +
  182. skb_frag_size(frag),
  183. chunk));
  184. skb->len += chunk;
  185. skb->data_len += chunk;
  186. skb_frag_size_add(frag, chunk);
  187. sz = tls_rx_msg_size(strp, skb);
  188. if (sz < 0)
  189. return sz;
  190. /* We may have over-read, sz == 0 is guaranteed under-read */
  191. if (unlikely(sz && sz < skb->len)) {
  192. int over = skb->len - sz;
  193. WARN_ON_ONCE(over > chunk);
  194. skb->len -= over;
  195. skb->data_len -= over;
  196. skb_frag_size_add(frag, -over);
  197. chunk -= over;
  198. }
  199. frag++;
  200. len -= chunk;
  201. offset += chunk;
  202. strp->stm.full_len = sz;
  203. if (!strp->stm.full_len)
  204. goto read_done;
  205. }
  206. /* Load up more data */
  207. while (len && strp->stm.full_len > skb->len) {
  208. chunk = min_t(size_t, len, strp->stm.full_len - skb->len);
  209. chunk = min_t(size_t, chunk, PAGE_SIZE - skb_frag_size(frag));
  210. WARN_ON_ONCE(skb_copy_bits(in_skb, offset,
  211. skb_frag_address(frag) +
  212. skb_frag_size(frag),
  213. chunk));
  214. skb->len += chunk;
  215. skb->data_len += chunk;
  216. skb_frag_size_add(frag, chunk);
  217. frag++;
  218. len -= chunk;
  219. offset += chunk;
  220. }
  221. read_done:
  222. return in_len - len;
  223. }
  224. static int tls_strp_copyin_skb(struct tls_strparser *strp, struct sk_buff *skb,
  225. struct sk_buff *in_skb, unsigned int offset,
  226. size_t in_len)
  227. {
  228. struct sk_buff *nskb, *first, *last;
  229. struct skb_shared_info *shinfo;
  230. size_t chunk;
  231. int sz;
  232. if (strp->stm.full_len)
  233. chunk = strp->stm.full_len - skb->len;
  234. else
  235. chunk = TLS_MAX_PAYLOAD_SIZE + PAGE_SIZE;
  236. chunk = min(chunk, in_len);
  237. nskb = tls_strp_skb_copy(strp, in_skb, offset, chunk);
  238. if (!nskb)
  239. return -ENOMEM;
  240. shinfo = skb_shinfo(skb);
  241. if (!shinfo->frag_list) {
  242. shinfo->frag_list = nskb;
  243. nskb->prev = nskb;
  244. } else {
  245. first = shinfo->frag_list;
  246. last = first->prev;
  247. last->next = nskb;
  248. first->prev = nskb;
  249. }
  250. skb->len += chunk;
  251. skb->data_len += chunk;
  252. if (!strp->stm.full_len) {
  253. sz = tls_rx_msg_size(strp, skb);
  254. if (sz < 0)
  255. return sz;
  256. /* We may have over-read, sz == 0 is guaranteed under-read */
  257. if (unlikely(sz && sz < skb->len)) {
  258. int over = skb->len - sz;
  259. WARN_ON_ONCE(over > chunk);
  260. skb->len -= over;
  261. skb->data_len -= over;
  262. __pskb_trim(nskb, nskb->len - over);
  263. chunk -= over;
  264. }
  265. strp->stm.full_len = sz;
  266. }
  267. return chunk;
  268. }
  269. static int tls_strp_copyin(read_descriptor_t *desc, struct sk_buff *in_skb,
  270. unsigned int offset, size_t in_len)
  271. {
  272. struct tls_strparser *strp = (struct tls_strparser *)desc->arg.data;
  273. struct sk_buff *skb;
  274. int ret;
  275. if (strp->msg_ready)
  276. return 0;
  277. skb = strp->anchor;
  278. if (!skb->len)
  279. skb_copy_decrypted(skb, in_skb);
  280. else
  281. strp->mixed_decrypted |= !!skb_cmp_decrypted(skb, in_skb);
  282. if (IS_ENABLED(CONFIG_TLS_DEVICE) && strp->mixed_decrypted)
  283. ret = tls_strp_copyin_skb(strp, skb, in_skb, offset, in_len);
  284. else
  285. ret = tls_strp_copyin_frag(strp, skb, in_skb, offset, in_len);
  286. if (ret < 0) {
  287. desc->error = ret;
  288. ret = 0;
  289. }
  290. if (strp->stm.full_len && strp->stm.full_len == skb->len) {
  291. desc->count = 0;
  292. WRITE_ONCE(strp->msg_ready, 1);
  293. tls_rx_msg_ready(strp);
  294. }
  295. return ret;
  296. }
  297. static int tls_strp_read_copyin(struct tls_strparser *strp)
  298. {
  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. tcp_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. WRITE_ONCE(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. WRITE_ONCE(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. }