rxkad.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* Kerberos-based RxRPC security
  3. *
  4. * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
  5. * Written by David Howells ([email protected])
  6. */
  7. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  8. #include <crypto/skcipher.h>
  9. #include <linux/module.h>
  10. #include <linux/net.h>
  11. #include <linux/skbuff.h>
  12. #include <linux/udp.h>
  13. #include <linux/scatterlist.h>
  14. #include <linux/ctype.h>
  15. #include <linux/slab.h>
  16. #include <linux/key-type.h>
  17. #include <net/sock.h>
  18. #include <net/af_rxrpc.h>
  19. #include <keys/rxrpc-type.h>
  20. #include "ar-internal.h"
  21. #define RXKAD_VERSION 2
  22. #define MAXKRB5TICKETLEN 1024
  23. #define RXKAD_TKT_TYPE_KERBEROS_V5 256
  24. #define ANAME_SZ 40 /* size of authentication name */
  25. #define INST_SZ 40 /* size of principal's instance */
  26. #define REALM_SZ 40 /* size of principal's auth domain */
  27. #define SNAME_SZ 40 /* size of service name */
  28. #define RXKAD_ALIGN 8
  29. struct rxkad_level1_hdr {
  30. __be32 data_size; /* true data size (excluding padding) */
  31. };
  32. struct rxkad_level2_hdr {
  33. __be32 data_size; /* true data size (excluding padding) */
  34. __be32 checksum; /* decrypted data checksum */
  35. };
  36. static int rxkad_prime_packet_security(struct rxrpc_connection *conn,
  37. struct crypto_sync_skcipher *ci);
  38. /*
  39. * this holds a pinned cipher so that keventd doesn't get called by the cipher
  40. * alloc routine, but since we have it to hand, we use it to decrypt RESPONSE
  41. * packets
  42. */
  43. static struct crypto_sync_skcipher *rxkad_ci;
  44. static struct skcipher_request *rxkad_ci_req;
  45. static DEFINE_MUTEX(rxkad_ci_mutex);
  46. /*
  47. * Parse the information from a server key
  48. *
  49. * The data should be the 8-byte secret key.
  50. */
  51. static int rxkad_preparse_server_key(struct key_preparsed_payload *prep)
  52. {
  53. struct crypto_skcipher *ci;
  54. if (prep->datalen != 8)
  55. return -EINVAL;
  56. memcpy(&prep->payload.data[2], prep->data, 8);
  57. ci = crypto_alloc_skcipher("pcbc(des)", 0, CRYPTO_ALG_ASYNC);
  58. if (IS_ERR(ci)) {
  59. _leave(" = %ld", PTR_ERR(ci));
  60. return PTR_ERR(ci);
  61. }
  62. if (crypto_skcipher_setkey(ci, prep->data, 8) < 0)
  63. BUG();
  64. prep->payload.data[0] = ci;
  65. _leave(" = 0");
  66. return 0;
  67. }
  68. static void rxkad_free_preparse_server_key(struct key_preparsed_payload *prep)
  69. {
  70. if (prep->payload.data[0])
  71. crypto_free_skcipher(prep->payload.data[0]);
  72. }
  73. static void rxkad_destroy_server_key(struct key *key)
  74. {
  75. if (key->payload.data[0]) {
  76. crypto_free_skcipher(key->payload.data[0]);
  77. key->payload.data[0] = NULL;
  78. }
  79. }
  80. /*
  81. * initialise connection security
  82. */
  83. static int rxkad_init_connection_security(struct rxrpc_connection *conn,
  84. struct rxrpc_key_token *token)
  85. {
  86. struct crypto_sync_skcipher *ci;
  87. int ret;
  88. _enter("{%d},{%x}", conn->debug_id, key_serial(conn->params.key));
  89. conn->security_ix = token->security_index;
  90. ci = crypto_alloc_sync_skcipher("pcbc(fcrypt)", 0, 0);
  91. if (IS_ERR(ci)) {
  92. _debug("no cipher");
  93. ret = PTR_ERR(ci);
  94. goto error;
  95. }
  96. if (crypto_sync_skcipher_setkey(ci, token->kad->session_key,
  97. sizeof(token->kad->session_key)) < 0)
  98. BUG();
  99. switch (conn->params.security_level) {
  100. case RXRPC_SECURITY_PLAIN:
  101. case RXRPC_SECURITY_AUTH:
  102. case RXRPC_SECURITY_ENCRYPT:
  103. break;
  104. default:
  105. ret = -EKEYREJECTED;
  106. goto error;
  107. }
  108. ret = rxkad_prime_packet_security(conn, ci);
  109. if (ret < 0)
  110. goto error_ci;
  111. conn->rxkad.cipher = ci;
  112. return 0;
  113. error_ci:
  114. crypto_free_sync_skcipher(ci);
  115. error:
  116. _leave(" = %d", ret);
  117. return ret;
  118. }
  119. /*
  120. * Work out how much data we can put in a packet.
  121. */
  122. static int rxkad_how_much_data(struct rxrpc_call *call, size_t remain,
  123. size_t *_buf_size, size_t *_data_size, size_t *_offset)
  124. {
  125. size_t shdr, buf_size, chunk;
  126. switch (call->conn->params.security_level) {
  127. default:
  128. buf_size = chunk = min_t(size_t, remain, RXRPC_JUMBO_DATALEN);
  129. shdr = 0;
  130. goto out;
  131. case RXRPC_SECURITY_AUTH:
  132. shdr = sizeof(struct rxkad_level1_hdr);
  133. break;
  134. case RXRPC_SECURITY_ENCRYPT:
  135. shdr = sizeof(struct rxkad_level2_hdr);
  136. break;
  137. }
  138. buf_size = round_down(RXRPC_JUMBO_DATALEN, RXKAD_ALIGN);
  139. chunk = buf_size - shdr;
  140. if (remain < chunk)
  141. buf_size = round_up(shdr + remain, RXKAD_ALIGN);
  142. out:
  143. *_buf_size = buf_size;
  144. *_data_size = chunk;
  145. *_offset = shdr;
  146. return 0;
  147. }
  148. /*
  149. * prime the encryption state with the invariant parts of a connection's
  150. * description
  151. */
  152. static int rxkad_prime_packet_security(struct rxrpc_connection *conn,
  153. struct crypto_sync_skcipher *ci)
  154. {
  155. struct skcipher_request *req;
  156. struct rxrpc_key_token *token;
  157. struct scatterlist sg;
  158. struct rxrpc_crypt iv;
  159. __be32 *tmpbuf;
  160. size_t tmpsize = 4 * sizeof(__be32);
  161. _enter("");
  162. if (!conn->params.key)
  163. return 0;
  164. tmpbuf = kmalloc(tmpsize, GFP_KERNEL);
  165. if (!tmpbuf)
  166. return -ENOMEM;
  167. req = skcipher_request_alloc(&ci->base, GFP_NOFS);
  168. if (!req) {
  169. kfree(tmpbuf);
  170. return -ENOMEM;
  171. }
  172. token = conn->params.key->payload.data[0];
  173. memcpy(&iv, token->kad->session_key, sizeof(iv));
  174. tmpbuf[0] = htonl(conn->proto.epoch);
  175. tmpbuf[1] = htonl(conn->proto.cid);
  176. tmpbuf[2] = 0;
  177. tmpbuf[3] = htonl(conn->security_ix);
  178. sg_init_one(&sg, tmpbuf, tmpsize);
  179. skcipher_request_set_sync_tfm(req, ci);
  180. skcipher_request_set_callback(req, 0, NULL, NULL);
  181. skcipher_request_set_crypt(req, &sg, &sg, tmpsize, iv.x);
  182. crypto_skcipher_encrypt(req);
  183. skcipher_request_free(req);
  184. memcpy(&conn->rxkad.csum_iv, tmpbuf + 2, sizeof(conn->rxkad.csum_iv));
  185. kfree(tmpbuf);
  186. _leave(" = 0");
  187. return 0;
  188. }
  189. /*
  190. * Allocate and prepare the crypto request on a call. For any particular call,
  191. * this is called serially for the packets, so no lock should be necessary.
  192. */
  193. static struct skcipher_request *rxkad_get_call_crypto(struct rxrpc_call *call)
  194. {
  195. struct crypto_skcipher *tfm = &call->conn->rxkad.cipher->base;
  196. struct skcipher_request *cipher_req = call->cipher_req;
  197. if (!cipher_req) {
  198. cipher_req = skcipher_request_alloc(tfm, GFP_NOFS);
  199. if (!cipher_req)
  200. return NULL;
  201. call->cipher_req = cipher_req;
  202. }
  203. return cipher_req;
  204. }
  205. /*
  206. * Clean up the crypto on a call.
  207. */
  208. static void rxkad_free_call_crypto(struct rxrpc_call *call)
  209. {
  210. if (call->cipher_req)
  211. skcipher_request_free(call->cipher_req);
  212. call->cipher_req = NULL;
  213. }
  214. /*
  215. * partially encrypt a packet (level 1 security)
  216. */
  217. static int rxkad_secure_packet_auth(const struct rxrpc_call *call,
  218. struct sk_buff *skb, u32 data_size,
  219. struct skcipher_request *req)
  220. {
  221. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  222. struct rxkad_level1_hdr hdr;
  223. struct rxrpc_crypt iv;
  224. struct scatterlist sg;
  225. size_t pad;
  226. u16 check;
  227. _enter("");
  228. check = sp->hdr.seq ^ call->call_id;
  229. data_size |= (u32)check << 16;
  230. hdr.data_size = htonl(data_size);
  231. memcpy(skb->head, &hdr, sizeof(hdr));
  232. pad = sizeof(struct rxkad_level1_hdr) + data_size;
  233. pad = RXKAD_ALIGN - pad;
  234. pad &= RXKAD_ALIGN - 1;
  235. if (pad)
  236. skb_put_zero(skb, pad);
  237. /* start the encryption afresh */
  238. memset(&iv, 0, sizeof(iv));
  239. sg_init_one(&sg, skb->head, 8);
  240. skcipher_request_set_sync_tfm(req, call->conn->rxkad.cipher);
  241. skcipher_request_set_callback(req, 0, NULL, NULL);
  242. skcipher_request_set_crypt(req, &sg, &sg, 8, iv.x);
  243. crypto_skcipher_encrypt(req);
  244. skcipher_request_zero(req);
  245. _leave(" = 0");
  246. return 0;
  247. }
  248. /*
  249. * wholly encrypt a packet (level 2 security)
  250. */
  251. static int rxkad_secure_packet_encrypt(const struct rxrpc_call *call,
  252. struct sk_buff *skb,
  253. u32 data_size,
  254. struct skcipher_request *req)
  255. {
  256. const struct rxrpc_key_token *token;
  257. struct rxkad_level2_hdr rxkhdr;
  258. struct rxrpc_skb_priv *sp;
  259. struct rxrpc_crypt iv;
  260. struct scatterlist sg[16];
  261. unsigned int len;
  262. size_t pad;
  263. u16 check;
  264. int err;
  265. sp = rxrpc_skb(skb);
  266. _enter("");
  267. check = sp->hdr.seq ^ call->call_id;
  268. rxkhdr.data_size = htonl(data_size | (u32)check << 16);
  269. rxkhdr.checksum = 0;
  270. memcpy(skb->head, &rxkhdr, sizeof(rxkhdr));
  271. pad = sizeof(struct rxkad_level2_hdr) + data_size;
  272. pad = RXKAD_ALIGN - pad;
  273. pad &= RXKAD_ALIGN - 1;
  274. if (pad)
  275. skb_put_zero(skb, pad);
  276. /* encrypt from the session key */
  277. token = call->conn->params.key->payload.data[0];
  278. memcpy(&iv, token->kad->session_key, sizeof(iv));
  279. sg_init_one(&sg[0], skb->head, sizeof(rxkhdr));
  280. skcipher_request_set_sync_tfm(req, call->conn->rxkad.cipher);
  281. skcipher_request_set_callback(req, 0, NULL, NULL);
  282. skcipher_request_set_crypt(req, &sg[0], &sg[0], sizeof(rxkhdr), iv.x);
  283. crypto_skcipher_encrypt(req);
  284. /* we want to encrypt the skbuff in-place */
  285. err = -EMSGSIZE;
  286. if (skb_shinfo(skb)->nr_frags > 16)
  287. goto out;
  288. len = round_up(data_size, RXKAD_ALIGN);
  289. sg_init_table(sg, ARRAY_SIZE(sg));
  290. err = skb_to_sgvec(skb, sg, 8, len);
  291. if (unlikely(err < 0))
  292. goto out;
  293. skcipher_request_set_crypt(req, sg, sg, len, iv.x);
  294. crypto_skcipher_encrypt(req);
  295. _leave(" = 0");
  296. err = 0;
  297. out:
  298. skcipher_request_zero(req);
  299. return err;
  300. }
  301. /*
  302. * checksum an RxRPC packet header
  303. */
  304. static int rxkad_secure_packet(struct rxrpc_call *call,
  305. struct sk_buff *skb,
  306. size_t data_size)
  307. {
  308. struct rxrpc_skb_priv *sp;
  309. struct skcipher_request *req;
  310. struct rxrpc_crypt iv;
  311. struct scatterlist sg;
  312. u32 x, y;
  313. int ret;
  314. sp = rxrpc_skb(skb);
  315. _enter("{%d{%x}},{#%u},%zu,",
  316. call->debug_id, key_serial(call->conn->params.key),
  317. sp->hdr.seq, data_size);
  318. if (!call->conn->rxkad.cipher)
  319. return 0;
  320. ret = key_validate(call->conn->params.key);
  321. if (ret < 0)
  322. return ret;
  323. req = rxkad_get_call_crypto(call);
  324. if (!req)
  325. return -ENOMEM;
  326. /* continue encrypting from where we left off */
  327. memcpy(&iv, call->conn->rxkad.csum_iv.x, sizeof(iv));
  328. /* calculate the security checksum */
  329. x = (call->cid & RXRPC_CHANNELMASK) << (32 - RXRPC_CIDSHIFT);
  330. x |= sp->hdr.seq & 0x3fffffff;
  331. call->crypto_buf[0] = htonl(call->call_id);
  332. call->crypto_buf[1] = htonl(x);
  333. sg_init_one(&sg, call->crypto_buf, 8);
  334. skcipher_request_set_sync_tfm(req, call->conn->rxkad.cipher);
  335. skcipher_request_set_callback(req, 0, NULL, NULL);
  336. skcipher_request_set_crypt(req, &sg, &sg, 8, iv.x);
  337. crypto_skcipher_encrypt(req);
  338. skcipher_request_zero(req);
  339. y = ntohl(call->crypto_buf[1]);
  340. y = (y >> 16) & 0xffff;
  341. if (y == 0)
  342. y = 1; /* zero checksums are not permitted */
  343. sp->hdr.cksum = y;
  344. switch (call->conn->params.security_level) {
  345. case RXRPC_SECURITY_PLAIN:
  346. ret = 0;
  347. break;
  348. case RXRPC_SECURITY_AUTH:
  349. ret = rxkad_secure_packet_auth(call, skb, data_size, req);
  350. break;
  351. case RXRPC_SECURITY_ENCRYPT:
  352. ret = rxkad_secure_packet_encrypt(call, skb, data_size, req);
  353. break;
  354. default:
  355. ret = -EPERM;
  356. break;
  357. }
  358. _leave(" = %d [set %x]", ret, y);
  359. return ret;
  360. }
  361. /*
  362. * decrypt partial encryption on a packet (level 1 security)
  363. */
  364. static int rxkad_verify_packet_1(struct rxrpc_call *call, struct sk_buff *skb,
  365. unsigned int offset, unsigned int len,
  366. rxrpc_seq_t seq,
  367. struct skcipher_request *req)
  368. {
  369. struct rxkad_level1_hdr sechdr;
  370. struct rxrpc_crypt iv;
  371. struct scatterlist sg[16];
  372. bool aborted;
  373. u32 data_size, buf;
  374. u16 check;
  375. int ret;
  376. _enter("");
  377. if (len < 8) {
  378. aborted = rxrpc_abort_eproto(call, skb, "rxkad_1_hdr", "V1H",
  379. RXKADSEALEDINCON);
  380. goto protocol_error;
  381. }
  382. /* Decrypt the skbuff in-place. TODO: We really want to decrypt
  383. * directly into the target buffer.
  384. */
  385. sg_init_table(sg, ARRAY_SIZE(sg));
  386. ret = skb_to_sgvec(skb, sg, offset, 8);
  387. if (unlikely(ret < 0))
  388. return ret;
  389. /* start the decryption afresh */
  390. memset(&iv, 0, sizeof(iv));
  391. skcipher_request_set_sync_tfm(req, call->conn->rxkad.cipher);
  392. skcipher_request_set_callback(req, 0, NULL, NULL);
  393. skcipher_request_set_crypt(req, sg, sg, 8, iv.x);
  394. crypto_skcipher_decrypt(req);
  395. skcipher_request_zero(req);
  396. /* Extract the decrypted packet length */
  397. if (skb_copy_bits(skb, offset, &sechdr, sizeof(sechdr)) < 0) {
  398. aborted = rxrpc_abort_eproto(call, skb, "rxkad_1_len", "XV1",
  399. RXKADDATALEN);
  400. goto protocol_error;
  401. }
  402. len -= sizeof(sechdr);
  403. buf = ntohl(sechdr.data_size);
  404. data_size = buf & 0xffff;
  405. check = buf >> 16;
  406. check ^= seq ^ call->call_id;
  407. check &= 0xffff;
  408. if (check != 0) {
  409. aborted = rxrpc_abort_eproto(call, skb, "rxkad_1_check", "V1C",
  410. RXKADSEALEDINCON);
  411. goto protocol_error;
  412. }
  413. if (data_size > len) {
  414. aborted = rxrpc_abort_eproto(call, skb, "rxkad_1_datalen", "V1L",
  415. RXKADDATALEN);
  416. goto protocol_error;
  417. }
  418. _leave(" = 0 [dlen=%x]", data_size);
  419. return 0;
  420. protocol_error:
  421. if (aborted)
  422. rxrpc_send_abort_packet(call);
  423. return -EPROTO;
  424. }
  425. /*
  426. * wholly decrypt a packet (level 2 security)
  427. */
  428. static int rxkad_verify_packet_2(struct rxrpc_call *call, struct sk_buff *skb,
  429. unsigned int offset, unsigned int len,
  430. rxrpc_seq_t seq,
  431. struct skcipher_request *req)
  432. {
  433. const struct rxrpc_key_token *token;
  434. struct rxkad_level2_hdr sechdr;
  435. struct rxrpc_crypt iv;
  436. struct scatterlist _sg[4], *sg;
  437. bool aborted;
  438. u32 data_size, buf;
  439. u16 check;
  440. int nsg, ret;
  441. _enter(",{%d}", skb->len);
  442. if (len < 8) {
  443. aborted = rxrpc_abort_eproto(call, skb, "rxkad_2_hdr", "V2H",
  444. RXKADSEALEDINCON);
  445. goto protocol_error;
  446. }
  447. /* Decrypt the skbuff in-place. TODO: We really want to decrypt
  448. * directly into the target buffer.
  449. */
  450. sg = _sg;
  451. nsg = skb_shinfo(skb)->nr_frags + 1;
  452. if (nsg <= 4) {
  453. nsg = 4;
  454. } else {
  455. sg = kmalloc_array(nsg, sizeof(*sg), GFP_NOIO);
  456. if (!sg)
  457. goto nomem;
  458. }
  459. sg_init_table(sg, nsg);
  460. ret = skb_to_sgvec(skb, sg, offset, len);
  461. if (unlikely(ret < 0)) {
  462. if (sg != _sg)
  463. kfree(sg);
  464. return ret;
  465. }
  466. /* decrypt from the session key */
  467. token = call->conn->params.key->payload.data[0];
  468. memcpy(&iv, token->kad->session_key, sizeof(iv));
  469. skcipher_request_set_sync_tfm(req, call->conn->rxkad.cipher);
  470. skcipher_request_set_callback(req, 0, NULL, NULL);
  471. skcipher_request_set_crypt(req, sg, sg, len, iv.x);
  472. crypto_skcipher_decrypt(req);
  473. skcipher_request_zero(req);
  474. if (sg != _sg)
  475. kfree(sg);
  476. /* Extract the decrypted packet length */
  477. if (skb_copy_bits(skb, offset, &sechdr, sizeof(sechdr)) < 0) {
  478. aborted = rxrpc_abort_eproto(call, skb, "rxkad_2_len", "XV2",
  479. RXKADDATALEN);
  480. goto protocol_error;
  481. }
  482. len -= sizeof(sechdr);
  483. buf = ntohl(sechdr.data_size);
  484. data_size = buf & 0xffff;
  485. check = buf >> 16;
  486. check ^= seq ^ call->call_id;
  487. check &= 0xffff;
  488. if (check != 0) {
  489. aborted = rxrpc_abort_eproto(call, skb, "rxkad_2_check", "V2C",
  490. RXKADSEALEDINCON);
  491. goto protocol_error;
  492. }
  493. if (data_size > len) {
  494. aborted = rxrpc_abort_eproto(call, skb, "rxkad_2_datalen", "V2L",
  495. RXKADDATALEN);
  496. goto protocol_error;
  497. }
  498. _leave(" = 0 [dlen=%x]", data_size);
  499. return 0;
  500. protocol_error:
  501. if (aborted)
  502. rxrpc_send_abort_packet(call);
  503. return -EPROTO;
  504. nomem:
  505. _leave(" = -ENOMEM");
  506. return -ENOMEM;
  507. }
  508. /*
  509. * Verify the security on a received packet or subpacket (if part of a
  510. * jumbo packet).
  511. */
  512. static int rxkad_verify_packet(struct rxrpc_call *call, struct sk_buff *skb,
  513. unsigned int offset, unsigned int len,
  514. rxrpc_seq_t seq, u16 expected_cksum)
  515. {
  516. struct skcipher_request *req;
  517. struct rxrpc_crypt iv;
  518. struct scatterlist sg;
  519. bool aborted;
  520. u16 cksum;
  521. u32 x, y;
  522. _enter("{%d{%x}},{#%u}",
  523. call->debug_id, key_serial(call->conn->params.key), seq);
  524. if (!call->conn->rxkad.cipher)
  525. return 0;
  526. req = rxkad_get_call_crypto(call);
  527. if (!req)
  528. return -ENOMEM;
  529. /* continue encrypting from where we left off */
  530. memcpy(&iv, call->conn->rxkad.csum_iv.x, sizeof(iv));
  531. /* validate the security checksum */
  532. x = (call->cid & RXRPC_CHANNELMASK) << (32 - RXRPC_CIDSHIFT);
  533. x |= seq & 0x3fffffff;
  534. call->crypto_buf[0] = htonl(call->call_id);
  535. call->crypto_buf[1] = htonl(x);
  536. sg_init_one(&sg, call->crypto_buf, 8);
  537. skcipher_request_set_sync_tfm(req, call->conn->rxkad.cipher);
  538. skcipher_request_set_callback(req, 0, NULL, NULL);
  539. skcipher_request_set_crypt(req, &sg, &sg, 8, iv.x);
  540. crypto_skcipher_encrypt(req);
  541. skcipher_request_zero(req);
  542. y = ntohl(call->crypto_buf[1]);
  543. cksum = (y >> 16) & 0xffff;
  544. if (cksum == 0)
  545. cksum = 1; /* zero checksums are not permitted */
  546. if (cksum != expected_cksum) {
  547. aborted = rxrpc_abort_eproto(call, skb, "rxkad_csum", "VCK",
  548. RXKADSEALEDINCON);
  549. goto protocol_error;
  550. }
  551. switch (call->conn->params.security_level) {
  552. case RXRPC_SECURITY_PLAIN:
  553. return 0;
  554. case RXRPC_SECURITY_AUTH:
  555. return rxkad_verify_packet_1(call, skb, offset, len, seq, req);
  556. case RXRPC_SECURITY_ENCRYPT:
  557. return rxkad_verify_packet_2(call, skb, offset, len, seq, req);
  558. default:
  559. return -ENOANO;
  560. }
  561. protocol_error:
  562. if (aborted)
  563. rxrpc_send_abort_packet(call);
  564. return -EPROTO;
  565. }
  566. /*
  567. * Locate the data contained in a packet that was partially encrypted.
  568. */
  569. static void rxkad_locate_data_1(struct rxrpc_call *call, struct sk_buff *skb,
  570. unsigned int *_offset, unsigned int *_len)
  571. {
  572. struct rxkad_level1_hdr sechdr;
  573. if (skb_copy_bits(skb, *_offset, &sechdr, sizeof(sechdr)) < 0)
  574. BUG();
  575. *_offset += sizeof(sechdr);
  576. *_len = ntohl(sechdr.data_size) & 0xffff;
  577. }
  578. /*
  579. * Locate the data contained in a packet that was completely encrypted.
  580. */
  581. static void rxkad_locate_data_2(struct rxrpc_call *call, struct sk_buff *skb,
  582. unsigned int *_offset, unsigned int *_len)
  583. {
  584. struct rxkad_level2_hdr sechdr;
  585. if (skb_copy_bits(skb, *_offset, &sechdr, sizeof(sechdr)) < 0)
  586. BUG();
  587. *_offset += sizeof(sechdr);
  588. *_len = ntohl(sechdr.data_size) & 0xffff;
  589. }
  590. /*
  591. * Locate the data contained in an already decrypted packet.
  592. */
  593. static void rxkad_locate_data(struct rxrpc_call *call, struct sk_buff *skb,
  594. unsigned int *_offset, unsigned int *_len)
  595. {
  596. switch (call->conn->params.security_level) {
  597. case RXRPC_SECURITY_AUTH:
  598. rxkad_locate_data_1(call, skb, _offset, _len);
  599. return;
  600. case RXRPC_SECURITY_ENCRYPT:
  601. rxkad_locate_data_2(call, skb, _offset, _len);
  602. return;
  603. default:
  604. return;
  605. }
  606. }
  607. /*
  608. * issue a challenge
  609. */
  610. static int rxkad_issue_challenge(struct rxrpc_connection *conn)
  611. {
  612. struct rxkad_challenge challenge;
  613. struct rxrpc_wire_header whdr;
  614. struct msghdr msg;
  615. struct kvec iov[2];
  616. size_t len;
  617. u32 serial;
  618. int ret;
  619. _enter("{%d}", conn->debug_id);
  620. get_random_bytes(&conn->rxkad.nonce, sizeof(conn->rxkad.nonce));
  621. challenge.version = htonl(2);
  622. challenge.nonce = htonl(conn->rxkad.nonce);
  623. challenge.min_level = htonl(0);
  624. challenge.__padding = 0;
  625. msg.msg_name = &conn->params.peer->srx.transport;
  626. msg.msg_namelen = conn->params.peer->srx.transport_len;
  627. msg.msg_control = NULL;
  628. msg.msg_controllen = 0;
  629. msg.msg_flags = 0;
  630. whdr.epoch = htonl(conn->proto.epoch);
  631. whdr.cid = htonl(conn->proto.cid);
  632. whdr.callNumber = 0;
  633. whdr.seq = 0;
  634. whdr.type = RXRPC_PACKET_TYPE_CHALLENGE;
  635. whdr.flags = conn->out_clientflag;
  636. whdr.userStatus = 0;
  637. whdr.securityIndex = conn->security_ix;
  638. whdr._rsvd = 0;
  639. whdr.serviceId = htons(conn->service_id);
  640. iov[0].iov_base = &whdr;
  641. iov[0].iov_len = sizeof(whdr);
  642. iov[1].iov_base = &challenge;
  643. iov[1].iov_len = sizeof(challenge);
  644. len = iov[0].iov_len + iov[1].iov_len;
  645. serial = atomic_inc_return(&conn->serial);
  646. whdr.serial = htonl(serial);
  647. _proto("Tx CHALLENGE %%%u", serial);
  648. ret = kernel_sendmsg(conn->params.local->socket, &msg, iov, 2, len);
  649. if (ret < 0) {
  650. trace_rxrpc_tx_fail(conn->debug_id, serial, ret,
  651. rxrpc_tx_point_rxkad_challenge);
  652. return -EAGAIN;
  653. }
  654. conn->params.peer->last_tx_at = ktime_get_seconds();
  655. trace_rxrpc_tx_packet(conn->debug_id, &whdr,
  656. rxrpc_tx_point_rxkad_challenge);
  657. _leave(" = 0");
  658. return 0;
  659. }
  660. /*
  661. * send a Kerberos security response
  662. */
  663. static int rxkad_send_response(struct rxrpc_connection *conn,
  664. struct rxrpc_host_header *hdr,
  665. struct rxkad_response *resp,
  666. const struct rxkad_key *s2)
  667. {
  668. struct rxrpc_wire_header whdr;
  669. struct msghdr msg;
  670. struct kvec iov[3];
  671. size_t len;
  672. u32 serial;
  673. int ret;
  674. _enter("");
  675. msg.msg_name = &conn->params.peer->srx.transport;
  676. msg.msg_namelen = conn->params.peer->srx.transport_len;
  677. msg.msg_control = NULL;
  678. msg.msg_controllen = 0;
  679. msg.msg_flags = 0;
  680. memset(&whdr, 0, sizeof(whdr));
  681. whdr.epoch = htonl(hdr->epoch);
  682. whdr.cid = htonl(hdr->cid);
  683. whdr.type = RXRPC_PACKET_TYPE_RESPONSE;
  684. whdr.flags = conn->out_clientflag;
  685. whdr.securityIndex = hdr->securityIndex;
  686. whdr.serviceId = htons(hdr->serviceId);
  687. iov[0].iov_base = &whdr;
  688. iov[0].iov_len = sizeof(whdr);
  689. iov[1].iov_base = resp;
  690. iov[1].iov_len = sizeof(*resp);
  691. iov[2].iov_base = (void *)s2->ticket;
  692. iov[2].iov_len = s2->ticket_len;
  693. len = iov[0].iov_len + iov[1].iov_len + iov[2].iov_len;
  694. serial = atomic_inc_return(&conn->serial);
  695. whdr.serial = htonl(serial);
  696. _proto("Tx RESPONSE %%%u", serial);
  697. ret = kernel_sendmsg(conn->params.local->socket, &msg, iov, 3, len);
  698. if (ret < 0) {
  699. trace_rxrpc_tx_fail(conn->debug_id, serial, ret,
  700. rxrpc_tx_point_rxkad_response);
  701. return -EAGAIN;
  702. }
  703. conn->params.peer->last_tx_at = ktime_get_seconds();
  704. _leave(" = 0");
  705. return 0;
  706. }
  707. /*
  708. * calculate the response checksum
  709. */
  710. static void rxkad_calc_response_checksum(struct rxkad_response *response)
  711. {
  712. u32 csum = 1000003;
  713. int loop;
  714. u8 *p = (u8 *) response;
  715. for (loop = sizeof(*response); loop > 0; loop--)
  716. csum = csum * 0x10204081 + *p++;
  717. response->encrypted.checksum = htonl(csum);
  718. }
  719. /*
  720. * encrypt the response packet
  721. */
  722. static int rxkad_encrypt_response(struct rxrpc_connection *conn,
  723. struct rxkad_response *resp,
  724. const struct rxkad_key *s2)
  725. {
  726. struct skcipher_request *req;
  727. struct rxrpc_crypt iv;
  728. struct scatterlist sg[1];
  729. req = skcipher_request_alloc(&conn->rxkad.cipher->base, GFP_NOFS);
  730. if (!req)
  731. return -ENOMEM;
  732. /* continue encrypting from where we left off */
  733. memcpy(&iv, s2->session_key, sizeof(iv));
  734. sg_init_table(sg, 1);
  735. sg_set_buf(sg, &resp->encrypted, sizeof(resp->encrypted));
  736. skcipher_request_set_sync_tfm(req, conn->rxkad.cipher);
  737. skcipher_request_set_callback(req, 0, NULL, NULL);
  738. skcipher_request_set_crypt(req, sg, sg, sizeof(resp->encrypted), iv.x);
  739. crypto_skcipher_encrypt(req);
  740. skcipher_request_free(req);
  741. return 0;
  742. }
  743. /*
  744. * respond to a challenge packet
  745. */
  746. static int rxkad_respond_to_challenge(struct rxrpc_connection *conn,
  747. struct sk_buff *skb,
  748. u32 *_abort_code)
  749. {
  750. const struct rxrpc_key_token *token;
  751. struct rxkad_challenge challenge;
  752. struct rxkad_response *resp;
  753. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  754. const char *eproto;
  755. u32 version, nonce, min_level, abort_code;
  756. int ret;
  757. _enter("{%d,%x}", conn->debug_id, key_serial(conn->params.key));
  758. eproto = tracepoint_string("chall_no_key");
  759. abort_code = RX_PROTOCOL_ERROR;
  760. if (!conn->params.key)
  761. goto protocol_error;
  762. abort_code = RXKADEXPIRED;
  763. ret = key_validate(conn->params.key);
  764. if (ret < 0)
  765. goto other_error;
  766. eproto = tracepoint_string("chall_short");
  767. abort_code = RXKADPACKETSHORT;
  768. if (skb_copy_bits(skb, sizeof(struct rxrpc_wire_header),
  769. &challenge, sizeof(challenge)) < 0)
  770. goto protocol_error;
  771. version = ntohl(challenge.version);
  772. nonce = ntohl(challenge.nonce);
  773. min_level = ntohl(challenge.min_level);
  774. _proto("Rx CHALLENGE %%%u { v=%u n=%u ml=%u }",
  775. sp->hdr.serial, version, nonce, min_level);
  776. eproto = tracepoint_string("chall_ver");
  777. abort_code = RXKADINCONSISTENCY;
  778. if (version != RXKAD_VERSION)
  779. goto protocol_error;
  780. abort_code = RXKADLEVELFAIL;
  781. ret = -EACCES;
  782. if (conn->params.security_level < min_level)
  783. goto other_error;
  784. token = conn->params.key->payload.data[0];
  785. /* build the response packet */
  786. resp = kzalloc(sizeof(struct rxkad_response), GFP_NOFS);
  787. if (!resp)
  788. return -ENOMEM;
  789. resp->version = htonl(RXKAD_VERSION);
  790. resp->encrypted.epoch = htonl(conn->proto.epoch);
  791. resp->encrypted.cid = htonl(conn->proto.cid);
  792. resp->encrypted.securityIndex = htonl(conn->security_ix);
  793. resp->encrypted.inc_nonce = htonl(nonce + 1);
  794. resp->encrypted.level = htonl(conn->params.security_level);
  795. resp->kvno = htonl(token->kad->kvno);
  796. resp->ticket_len = htonl(token->kad->ticket_len);
  797. resp->encrypted.call_id[0] = htonl(conn->channels[0].call_counter);
  798. resp->encrypted.call_id[1] = htonl(conn->channels[1].call_counter);
  799. resp->encrypted.call_id[2] = htonl(conn->channels[2].call_counter);
  800. resp->encrypted.call_id[3] = htonl(conn->channels[3].call_counter);
  801. /* calculate the response checksum and then do the encryption */
  802. rxkad_calc_response_checksum(resp);
  803. ret = rxkad_encrypt_response(conn, resp, token->kad);
  804. if (ret == 0)
  805. ret = rxkad_send_response(conn, &sp->hdr, resp, token->kad);
  806. kfree(resp);
  807. return ret;
  808. protocol_error:
  809. trace_rxrpc_rx_eproto(NULL, sp->hdr.serial, eproto);
  810. ret = -EPROTO;
  811. other_error:
  812. *_abort_code = abort_code;
  813. return ret;
  814. }
  815. /*
  816. * decrypt the kerberos IV ticket in the response
  817. */
  818. static int rxkad_decrypt_ticket(struct rxrpc_connection *conn,
  819. struct key *server_key,
  820. struct sk_buff *skb,
  821. void *ticket, size_t ticket_len,
  822. struct rxrpc_crypt *_session_key,
  823. time64_t *_expiry,
  824. u32 *_abort_code)
  825. {
  826. struct skcipher_request *req;
  827. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  828. struct rxrpc_crypt iv, key;
  829. struct scatterlist sg[1];
  830. struct in_addr addr;
  831. unsigned int life;
  832. const char *eproto;
  833. time64_t issue, now;
  834. bool little_endian;
  835. int ret;
  836. u32 abort_code;
  837. u8 *p, *q, *name, *end;
  838. _enter("{%d},{%x}", conn->debug_id, key_serial(server_key));
  839. *_expiry = 0;
  840. ASSERT(server_key->payload.data[0] != NULL);
  841. ASSERTCMP((unsigned long) ticket & 7UL, ==, 0);
  842. memcpy(&iv, &server_key->payload.data[2], sizeof(iv));
  843. ret = -ENOMEM;
  844. req = skcipher_request_alloc(server_key->payload.data[0], GFP_NOFS);
  845. if (!req)
  846. goto temporary_error;
  847. sg_init_one(&sg[0], ticket, ticket_len);
  848. skcipher_request_set_callback(req, 0, NULL, NULL);
  849. skcipher_request_set_crypt(req, sg, sg, ticket_len, iv.x);
  850. crypto_skcipher_decrypt(req);
  851. skcipher_request_free(req);
  852. p = ticket;
  853. end = p + ticket_len;
  854. #define Z(field) \
  855. ({ \
  856. u8 *__str = p; \
  857. eproto = tracepoint_string("rxkad_bad_"#field); \
  858. q = memchr(p, 0, end - p); \
  859. if (!q || q - p > (field##_SZ)) \
  860. goto bad_ticket; \
  861. for (; p < q; p++) \
  862. if (!isprint(*p)) \
  863. goto bad_ticket; \
  864. p++; \
  865. __str; \
  866. })
  867. /* extract the ticket flags */
  868. _debug("KIV FLAGS: %x", *p);
  869. little_endian = *p & 1;
  870. p++;
  871. /* extract the authentication name */
  872. name = Z(ANAME);
  873. _debug("KIV ANAME: %s", name);
  874. /* extract the principal's instance */
  875. name = Z(INST);
  876. _debug("KIV INST : %s", name);
  877. /* extract the principal's authentication domain */
  878. name = Z(REALM);
  879. _debug("KIV REALM: %s", name);
  880. eproto = tracepoint_string("rxkad_bad_len");
  881. if (end - p < 4 + 8 + 4 + 2)
  882. goto bad_ticket;
  883. /* get the IPv4 address of the entity that requested the ticket */
  884. memcpy(&addr, p, sizeof(addr));
  885. p += 4;
  886. _debug("KIV ADDR : %pI4", &addr);
  887. /* get the session key from the ticket */
  888. memcpy(&key, p, sizeof(key));
  889. p += 8;
  890. _debug("KIV KEY : %08x %08x", ntohl(key.n[0]), ntohl(key.n[1]));
  891. memcpy(_session_key, &key, sizeof(key));
  892. /* get the ticket's lifetime */
  893. life = *p++ * 5 * 60;
  894. _debug("KIV LIFE : %u", life);
  895. /* get the issue time of the ticket */
  896. if (little_endian) {
  897. __le32 stamp;
  898. memcpy(&stamp, p, 4);
  899. issue = rxrpc_u32_to_time64(le32_to_cpu(stamp));
  900. } else {
  901. __be32 stamp;
  902. memcpy(&stamp, p, 4);
  903. issue = rxrpc_u32_to_time64(be32_to_cpu(stamp));
  904. }
  905. p += 4;
  906. now = ktime_get_real_seconds();
  907. _debug("KIV ISSUE: %llx [%llx]", issue, now);
  908. /* check the ticket is in date */
  909. if (issue > now) {
  910. abort_code = RXKADNOAUTH;
  911. ret = -EKEYREJECTED;
  912. goto other_error;
  913. }
  914. if (issue < now - life) {
  915. abort_code = RXKADEXPIRED;
  916. ret = -EKEYEXPIRED;
  917. goto other_error;
  918. }
  919. *_expiry = issue + life;
  920. /* get the service name */
  921. name = Z(SNAME);
  922. _debug("KIV SNAME: %s", name);
  923. /* get the service instance name */
  924. name = Z(INST);
  925. _debug("KIV SINST: %s", name);
  926. return 0;
  927. bad_ticket:
  928. trace_rxrpc_rx_eproto(NULL, sp->hdr.serial, eproto);
  929. abort_code = RXKADBADTICKET;
  930. ret = -EPROTO;
  931. other_error:
  932. *_abort_code = abort_code;
  933. return ret;
  934. temporary_error:
  935. return ret;
  936. }
  937. /*
  938. * decrypt the response packet
  939. */
  940. static void rxkad_decrypt_response(struct rxrpc_connection *conn,
  941. struct rxkad_response *resp,
  942. const struct rxrpc_crypt *session_key)
  943. {
  944. struct skcipher_request *req = rxkad_ci_req;
  945. struct scatterlist sg[1];
  946. struct rxrpc_crypt iv;
  947. _enter(",,%08x%08x",
  948. ntohl(session_key->n[0]), ntohl(session_key->n[1]));
  949. mutex_lock(&rxkad_ci_mutex);
  950. if (crypto_sync_skcipher_setkey(rxkad_ci, session_key->x,
  951. sizeof(*session_key)) < 0)
  952. BUG();
  953. memcpy(&iv, session_key, sizeof(iv));
  954. sg_init_table(sg, 1);
  955. sg_set_buf(sg, &resp->encrypted, sizeof(resp->encrypted));
  956. skcipher_request_set_sync_tfm(req, rxkad_ci);
  957. skcipher_request_set_callback(req, 0, NULL, NULL);
  958. skcipher_request_set_crypt(req, sg, sg, sizeof(resp->encrypted), iv.x);
  959. crypto_skcipher_decrypt(req);
  960. skcipher_request_zero(req);
  961. mutex_unlock(&rxkad_ci_mutex);
  962. _leave("");
  963. }
  964. /*
  965. * verify a response
  966. */
  967. static int rxkad_verify_response(struct rxrpc_connection *conn,
  968. struct sk_buff *skb,
  969. u32 *_abort_code)
  970. {
  971. struct rxkad_response *response;
  972. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  973. struct rxrpc_crypt session_key;
  974. struct key *server_key;
  975. const char *eproto;
  976. time64_t expiry;
  977. void *ticket;
  978. u32 abort_code, version, kvno, ticket_len, level;
  979. __be32 csum;
  980. int ret, i;
  981. _enter("{%d}", conn->debug_id);
  982. server_key = rxrpc_look_up_server_security(conn, skb, 0, 0);
  983. if (IS_ERR(server_key)) {
  984. switch (PTR_ERR(server_key)) {
  985. case -ENOKEY:
  986. abort_code = RXKADUNKNOWNKEY;
  987. break;
  988. case -EKEYEXPIRED:
  989. abort_code = RXKADEXPIRED;
  990. break;
  991. default:
  992. abort_code = RXKADNOAUTH;
  993. break;
  994. }
  995. trace_rxrpc_abort(0, "SVK",
  996. sp->hdr.cid, sp->hdr.callNumber, sp->hdr.seq,
  997. abort_code, PTR_ERR(server_key));
  998. *_abort_code = abort_code;
  999. return -EPROTO;
  1000. }
  1001. ret = -ENOMEM;
  1002. response = kzalloc(sizeof(struct rxkad_response), GFP_NOFS);
  1003. if (!response)
  1004. goto temporary_error;
  1005. eproto = tracepoint_string("rxkad_rsp_short");
  1006. abort_code = RXKADPACKETSHORT;
  1007. if (skb_copy_bits(skb, sizeof(struct rxrpc_wire_header),
  1008. response, sizeof(*response)) < 0)
  1009. goto protocol_error;
  1010. version = ntohl(response->version);
  1011. ticket_len = ntohl(response->ticket_len);
  1012. kvno = ntohl(response->kvno);
  1013. _proto("Rx RESPONSE %%%u { v=%u kv=%u tl=%u }",
  1014. sp->hdr.serial, version, kvno, ticket_len);
  1015. eproto = tracepoint_string("rxkad_rsp_ver");
  1016. abort_code = RXKADINCONSISTENCY;
  1017. if (version != RXKAD_VERSION)
  1018. goto protocol_error;
  1019. eproto = tracepoint_string("rxkad_rsp_tktlen");
  1020. abort_code = RXKADTICKETLEN;
  1021. if (ticket_len < 4 || ticket_len > MAXKRB5TICKETLEN)
  1022. goto protocol_error;
  1023. eproto = tracepoint_string("rxkad_rsp_unkkey");
  1024. abort_code = RXKADUNKNOWNKEY;
  1025. if (kvno >= RXKAD_TKT_TYPE_KERBEROS_V5)
  1026. goto protocol_error;
  1027. /* extract the kerberos ticket and decrypt and decode it */
  1028. ret = -ENOMEM;
  1029. ticket = kmalloc(ticket_len, GFP_NOFS);
  1030. if (!ticket)
  1031. goto temporary_error_free_resp;
  1032. eproto = tracepoint_string("rxkad_tkt_short");
  1033. abort_code = RXKADPACKETSHORT;
  1034. if (skb_copy_bits(skb, sizeof(struct rxrpc_wire_header) + sizeof(*response),
  1035. ticket, ticket_len) < 0)
  1036. goto protocol_error_free;
  1037. ret = rxkad_decrypt_ticket(conn, server_key, skb, ticket, ticket_len,
  1038. &session_key, &expiry, _abort_code);
  1039. if (ret < 0)
  1040. goto temporary_error_free_ticket;
  1041. /* use the session key from inside the ticket to decrypt the
  1042. * response */
  1043. rxkad_decrypt_response(conn, response, &session_key);
  1044. eproto = tracepoint_string("rxkad_rsp_param");
  1045. abort_code = RXKADSEALEDINCON;
  1046. if (ntohl(response->encrypted.epoch) != conn->proto.epoch)
  1047. goto protocol_error_free;
  1048. if (ntohl(response->encrypted.cid) != conn->proto.cid)
  1049. goto protocol_error_free;
  1050. if (ntohl(response->encrypted.securityIndex) != conn->security_ix)
  1051. goto protocol_error_free;
  1052. csum = response->encrypted.checksum;
  1053. response->encrypted.checksum = 0;
  1054. rxkad_calc_response_checksum(response);
  1055. eproto = tracepoint_string("rxkad_rsp_csum");
  1056. if (response->encrypted.checksum != csum)
  1057. goto protocol_error_free;
  1058. spin_lock(&conn->bundle->channel_lock);
  1059. for (i = 0; i < RXRPC_MAXCALLS; i++) {
  1060. struct rxrpc_call *call;
  1061. u32 call_id = ntohl(response->encrypted.call_id[i]);
  1062. eproto = tracepoint_string("rxkad_rsp_callid");
  1063. if (call_id > INT_MAX)
  1064. goto protocol_error_unlock;
  1065. eproto = tracepoint_string("rxkad_rsp_callctr");
  1066. if (call_id < conn->channels[i].call_counter)
  1067. goto protocol_error_unlock;
  1068. eproto = tracepoint_string("rxkad_rsp_callst");
  1069. if (call_id > conn->channels[i].call_counter) {
  1070. call = rcu_dereference_protected(
  1071. conn->channels[i].call,
  1072. lockdep_is_held(&conn->bundle->channel_lock));
  1073. if (call && call->state < RXRPC_CALL_COMPLETE)
  1074. goto protocol_error_unlock;
  1075. conn->channels[i].call_counter = call_id;
  1076. }
  1077. }
  1078. spin_unlock(&conn->bundle->channel_lock);
  1079. eproto = tracepoint_string("rxkad_rsp_seq");
  1080. abort_code = RXKADOUTOFSEQUENCE;
  1081. if (ntohl(response->encrypted.inc_nonce) != conn->rxkad.nonce + 1)
  1082. goto protocol_error_free;
  1083. eproto = tracepoint_string("rxkad_rsp_level");
  1084. abort_code = RXKADLEVELFAIL;
  1085. level = ntohl(response->encrypted.level);
  1086. if (level > RXRPC_SECURITY_ENCRYPT)
  1087. goto protocol_error_free;
  1088. conn->params.security_level = level;
  1089. /* create a key to hold the security data and expiration time - after
  1090. * this the connection security can be handled in exactly the same way
  1091. * as for a client connection */
  1092. ret = rxrpc_get_server_data_key(conn, &session_key, expiry, kvno);
  1093. if (ret < 0)
  1094. goto temporary_error_free_ticket;
  1095. kfree(ticket);
  1096. kfree(response);
  1097. _leave(" = 0");
  1098. return 0;
  1099. protocol_error_unlock:
  1100. spin_unlock(&conn->bundle->channel_lock);
  1101. protocol_error_free:
  1102. kfree(ticket);
  1103. protocol_error:
  1104. kfree(response);
  1105. trace_rxrpc_rx_eproto(NULL, sp->hdr.serial, eproto);
  1106. key_put(server_key);
  1107. *_abort_code = abort_code;
  1108. return -EPROTO;
  1109. temporary_error_free_ticket:
  1110. kfree(ticket);
  1111. temporary_error_free_resp:
  1112. kfree(response);
  1113. temporary_error:
  1114. /* Ignore the response packet if we got a temporary error such as
  1115. * ENOMEM. We just want to send the challenge again. Note that we
  1116. * also come out this way if the ticket decryption fails.
  1117. */
  1118. key_put(server_key);
  1119. return ret;
  1120. }
  1121. /*
  1122. * clear the connection security
  1123. */
  1124. static void rxkad_clear(struct rxrpc_connection *conn)
  1125. {
  1126. _enter("");
  1127. if (conn->rxkad.cipher)
  1128. crypto_free_sync_skcipher(conn->rxkad.cipher);
  1129. }
  1130. /*
  1131. * Initialise the rxkad security service.
  1132. */
  1133. static int rxkad_init(void)
  1134. {
  1135. struct crypto_sync_skcipher *tfm;
  1136. struct skcipher_request *req;
  1137. /* pin the cipher we need so that the crypto layer doesn't invoke
  1138. * keventd to go get it */
  1139. tfm = crypto_alloc_sync_skcipher("pcbc(fcrypt)", 0, 0);
  1140. if (IS_ERR(tfm))
  1141. return PTR_ERR(tfm);
  1142. req = skcipher_request_alloc(&tfm->base, GFP_KERNEL);
  1143. if (!req)
  1144. goto nomem_tfm;
  1145. rxkad_ci_req = req;
  1146. rxkad_ci = tfm;
  1147. return 0;
  1148. nomem_tfm:
  1149. crypto_free_sync_skcipher(tfm);
  1150. return -ENOMEM;
  1151. }
  1152. /*
  1153. * Clean up the rxkad security service.
  1154. */
  1155. static void rxkad_exit(void)
  1156. {
  1157. crypto_free_sync_skcipher(rxkad_ci);
  1158. skcipher_request_free(rxkad_ci_req);
  1159. }
  1160. /*
  1161. * RxRPC Kerberos-based security
  1162. */
  1163. const struct rxrpc_security rxkad = {
  1164. .name = "rxkad",
  1165. .security_index = RXRPC_SECURITY_RXKAD,
  1166. .no_key_abort = RXKADUNKNOWNKEY,
  1167. .init = rxkad_init,
  1168. .exit = rxkad_exit,
  1169. .preparse_server_key = rxkad_preparse_server_key,
  1170. .free_preparse_server_key = rxkad_free_preparse_server_key,
  1171. .destroy_server_key = rxkad_destroy_server_key,
  1172. .init_connection_security = rxkad_init_connection_security,
  1173. .how_much_data = rxkad_how_much_data,
  1174. .secure_packet = rxkad_secure_packet,
  1175. .verify_packet = rxkad_verify_packet,
  1176. .free_call_crypto = rxkad_free_call_crypto,
  1177. .locate_data = rxkad_locate_data,
  1178. .issue_challenge = rxkad_issue_challenge,
  1179. .respond_to_challenge = rxkad_respond_to_challenge,
  1180. .verify_response = rxkad_verify_response,
  1181. .clear = rxkad_clear,
  1182. };