rxkad.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162
  1. /* Kerberos-based RxRPC security
  2. *
  3. * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/net.h>
  13. #include <linux/skbuff.h>
  14. #include <linux/udp.h>
  15. #include <linux/crypto.h>
  16. #include <linux/scatterlist.h>
  17. #include <linux/ctype.h>
  18. #include <linux/slab.h>
  19. #include <net/sock.h>
  20. #include <net/af_rxrpc.h>
  21. #include <keys/rxrpc-type.h>
  22. #define rxrpc_debug rxkad_debug
  23. #include "ar-internal.h"
  24. #define RXKAD_VERSION 2
  25. #define MAXKRB5TICKETLEN 1024
  26. #define RXKAD_TKT_TYPE_KERBEROS_V5 256
  27. #define ANAME_SZ 40 /* size of authentication name */
  28. #define INST_SZ 40 /* size of principal's instance */
  29. #define REALM_SZ 40 /* size of principal's auth domain */
  30. #define SNAME_SZ 40 /* size of service name */
  31. unsigned int rxrpc_debug;
  32. module_param_named(debug, rxrpc_debug, uint, S_IWUSR | S_IRUGO);
  33. MODULE_PARM_DESC(debug, "rxkad debugging mask");
  34. struct rxkad_level1_hdr {
  35. __be32 data_size; /* true data size (excluding padding) */
  36. };
  37. struct rxkad_level2_hdr {
  38. __be32 data_size; /* true data size (excluding padding) */
  39. __be32 checksum; /* decrypted data checksum */
  40. };
  41. MODULE_DESCRIPTION("RxRPC network protocol type-2 security (Kerberos 4)");
  42. MODULE_AUTHOR("Red Hat, Inc.");
  43. MODULE_LICENSE("GPL");
  44. /*
  45. * this holds a pinned cipher so that keventd doesn't get called by the cipher
  46. * alloc routine, but since we have it to hand, we use it to decrypt RESPONSE
  47. * packets
  48. */
  49. static struct crypto_blkcipher *rxkad_ci;
  50. static DEFINE_MUTEX(rxkad_ci_mutex);
  51. /*
  52. * initialise connection security
  53. */
  54. static int rxkad_init_connection_security(struct rxrpc_connection *conn)
  55. {
  56. struct crypto_blkcipher *ci;
  57. struct rxrpc_key_token *token;
  58. int ret;
  59. _enter("{%d},{%x}", conn->debug_id, key_serial(conn->key));
  60. token = conn->key->payload.data;
  61. conn->security_ix = token->security_index;
  62. ci = crypto_alloc_blkcipher("pcbc(fcrypt)", 0, CRYPTO_ALG_ASYNC);
  63. if (IS_ERR(ci)) {
  64. _debug("no cipher");
  65. ret = PTR_ERR(ci);
  66. goto error;
  67. }
  68. if (crypto_blkcipher_setkey(ci, token->kad->session_key,
  69. sizeof(token->kad->session_key)) < 0)
  70. BUG();
  71. switch (conn->security_level) {
  72. case RXRPC_SECURITY_PLAIN:
  73. break;
  74. case RXRPC_SECURITY_AUTH:
  75. conn->size_align = 8;
  76. conn->security_size = sizeof(struct rxkad_level1_hdr);
  77. conn->header_size += sizeof(struct rxkad_level1_hdr);
  78. break;
  79. case RXRPC_SECURITY_ENCRYPT:
  80. conn->size_align = 8;
  81. conn->security_size = sizeof(struct rxkad_level2_hdr);
  82. conn->header_size += sizeof(struct rxkad_level2_hdr);
  83. break;
  84. default:
  85. ret = -EKEYREJECTED;
  86. goto error;
  87. }
  88. conn->cipher = ci;
  89. ret = 0;
  90. error:
  91. _leave(" = %d", ret);
  92. return ret;
  93. }
  94. /*
  95. * prime the encryption state with the invariant parts of a connection's
  96. * description
  97. */
  98. static void rxkad_prime_packet_security(struct rxrpc_connection *conn)
  99. {
  100. struct rxrpc_key_token *token;
  101. struct blkcipher_desc desc;
  102. struct scatterlist sg[2];
  103. struct rxrpc_crypt iv;
  104. struct {
  105. __be32 x[4];
  106. } tmpbuf __attribute__((aligned(16))); /* must all be in same page */
  107. _enter("");
  108. if (!conn->key)
  109. return;
  110. token = conn->key->payload.data;
  111. memcpy(&iv, token->kad->session_key, sizeof(iv));
  112. desc.tfm = conn->cipher;
  113. desc.info = iv.x;
  114. desc.flags = 0;
  115. tmpbuf.x[0] = conn->epoch;
  116. tmpbuf.x[1] = conn->cid;
  117. tmpbuf.x[2] = 0;
  118. tmpbuf.x[3] = htonl(conn->security_ix);
  119. sg_init_one(&sg[0], &tmpbuf, sizeof(tmpbuf));
  120. sg_init_one(&sg[1], &tmpbuf, sizeof(tmpbuf));
  121. crypto_blkcipher_encrypt_iv(&desc, &sg[0], &sg[1], sizeof(tmpbuf));
  122. memcpy(&conn->csum_iv, &tmpbuf.x[2], sizeof(conn->csum_iv));
  123. ASSERTCMP(conn->csum_iv.n[0], ==, tmpbuf.x[2]);
  124. _leave("");
  125. }
  126. /*
  127. * partially encrypt a packet (level 1 security)
  128. */
  129. static int rxkad_secure_packet_auth(const struct rxrpc_call *call,
  130. struct sk_buff *skb,
  131. u32 data_size,
  132. void *sechdr)
  133. {
  134. struct rxrpc_skb_priv *sp;
  135. struct blkcipher_desc desc;
  136. struct rxrpc_crypt iv;
  137. struct scatterlist sg[2];
  138. struct {
  139. struct rxkad_level1_hdr hdr;
  140. __be32 first; /* first four bytes of data and padding */
  141. } tmpbuf __attribute__((aligned(8))); /* must all be in same page */
  142. u16 check;
  143. sp = rxrpc_skb(skb);
  144. _enter("");
  145. check = ntohl(sp->hdr.seq ^ sp->hdr.callNumber);
  146. data_size |= (u32) check << 16;
  147. tmpbuf.hdr.data_size = htonl(data_size);
  148. memcpy(&tmpbuf.first, sechdr + 4, sizeof(tmpbuf.first));
  149. /* start the encryption afresh */
  150. memset(&iv, 0, sizeof(iv));
  151. desc.tfm = call->conn->cipher;
  152. desc.info = iv.x;
  153. desc.flags = 0;
  154. sg_init_one(&sg[0], &tmpbuf, sizeof(tmpbuf));
  155. sg_init_one(&sg[1], &tmpbuf, sizeof(tmpbuf));
  156. crypto_blkcipher_encrypt_iv(&desc, &sg[0], &sg[1], sizeof(tmpbuf));
  157. memcpy(sechdr, &tmpbuf, sizeof(tmpbuf));
  158. _leave(" = 0");
  159. return 0;
  160. }
  161. /*
  162. * wholly encrypt a packet (level 2 security)
  163. */
  164. static int rxkad_secure_packet_encrypt(const struct rxrpc_call *call,
  165. struct sk_buff *skb,
  166. u32 data_size,
  167. void *sechdr)
  168. {
  169. const struct rxrpc_key_token *token;
  170. struct rxkad_level2_hdr rxkhdr
  171. __attribute__((aligned(8))); /* must be all on one page */
  172. struct rxrpc_skb_priv *sp;
  173. struct blkcipher_desc desc;
  174. struct rxrpc_crypt iv;
  175. struct scatterlist sg[16];
  176. struct sk_buff *trailer;
  177. unsigned int len;
  178. u16 check;
  179. int nsg;
  180. sp = rxrpc_skb(skb);
  181. _enter("");
  182. check = ntohl(sp->hdr.seq ^ sp->hdr.callNumber);
  183. rxkhdr.data_size = htonl(data_size | (u32) check << 16);
  184. rxkhdr.checksum = 0;
  185. /* encrypt from the session key */
  186. token = call->conn->key->payload.data;
  187. memcpy(&iv, token->kad->session_key, sizeof(iv));
  188. desc.tfm = call->conn->cipher;
  189. desc.info = iv.x;
  190. desc.flags = 0;
  191. sg_init_one(&sg[0], sechdr, sizeof(rxkhdr));
  192. sg_init_one(&sg[1], &rxkhdr, sizeof(rxkhdr));
  193. crypto_blkcipher_encrypt_iv(&desc, &sg[0], &sg[1], sizeof(rxkhdr));
  194. /* we want to encrypt the skbuff in-place */
  195. nsg = skb_cow_data(skb, 0, &trailer);
  196. if (nsg < 0 || nsg > 16)
  197. return -ENOMEM;
  198. len = data_size + call->conn->size_align - 1;
  199. len &= ~(call->conn->size_align - 1);
  200. sg_init_table(sg, nsg);
  201. skb_to_sgvec(skb, sg, 0, len);
  202. crypto_blkcipher_encrypt_iv(&desc, sg, sg, len);
  203. _leave(" = 0");
  204. return 0;
  205. }
  206. /*
  207. * checksum an RxRPC packet header
  208. */
  209. static int rxkad_secure_packet(const struct rxrpc_call *call,
  210. struct sk_buff *skb,
  211. size_t data_size,
  212. void *sechdr)
  213. {
  214. struct rxrpc_skb_priv *sp;
  215. struct blkcipher_desc desc;
  216. struct rxrpc_crypt iv;
  217. struct scatterlist sg[2];
  218. struct {
  219. __be32 x[2];
  220. } tmpbuf __attribute__((aligned(8))); /* must all be in same page */
  221. __be32 x;
  222. u32 y;
  223. int ret;
  224. sp = rxrpc_skb(skb);
  225. _enter("{%d{%x}},{#%u},%zu,",
  226. call->debug_id, key_serial(call->conn->key), ntohl(sp->hdr.seq),
  227. data_size);
  228. if (!call->conn->cipher)
  229. return 0;
  230. ret = key_validate(call->conn->key);
  231. if (ret < 0)
  232. return ret;
  233. /* continue encrypting from where we left off */
  234. memcpy(&iv, call->conn->csum_iv.x, sizeof(iv));
  235. desc.tfm = call->conn->cipher;
  236. desc.info = iv.x;
  237. desc.flags = 0;
  238. /* calculate the security checksum */
  239. x = htonl(call->channel << (32 - RXRPC_CIDSHIFT));
  240. x |= sp->hdr.seq & cpu_to_be32(0x3fffffff);
  241. tmpbuf.x[0] = sp->hdr.callNumber;
  242. tmpbuf.x[1] = x;
  243. sg_init_one(&sg[0], &tmpbuf, sizeof(tmpbuf));
  244. sg_init_one(&sg[1], &tmpbuf, sizeof(tmpbuf));
  245. crypto_blkcipher_encrypt_iv(&desc, &sg[0], &sg[1], sizeof(tmpbuf));
  246. y = ntohl(tmpbuf.x[1]);
  247. y = (y >> 16) & 0xffff;
  248. if (y == 0)
  249. y = 1; /* zero checksums are not permitted */
  250. sp->hdr.cksum = htons(y);
  251. switch (call->conn->security_level) {
  252. case RXRPC_SECURITY_PLAIN:
  253. ret = 0;
  254. break;
  255. case RXRPC_SECURITY_AUTH:
  256. ret = rxkad_secure_packet_auth(call, skb, data_size, sechdr);
  257. break;
  258. case RXRPC_SECURITY_ENCRYPT:
  259. ret = rxkad_secure_packet_encrypt(call, skb, data_size,
  260. sechdr);
  261. break;
  262. default:
  263. ret = -EPERM;
  264. break;
  265. }
  266. _leave(" = %d [set %hx]", ret, y);
  267. return ret;
  268. }
  269. /*
  270. * decrypt partial encryption on a packet (level 1 security)
  271. */
  272. static int rxkad_verify_packet_auth(const struct rxrpc_call *call,
  273. struct sk_buff *skb,
  274. u32 *_abort_code)
  275. {
  276. struct rxkad_level1_hdr sechdr;
  277. struct rxrpc_skb_priv *sp;
  278. struct blkcipher_desc desc;
  279. struct rxrpc_crypt iv;
  280. struct scatterlist sg[16];
  281. struct sk_buff *trailer;
  282. u32 data_size, buf;
  283. u16 check;
  284. int nsg;
  285. _enter("");
  286. sp = rxrpc_skb(skb);
  287. /* we want to decrypt the skbuff in-place */
  288. nsg = skb_cow_data(skb, 0, &trailer);
  289. if (nsg < 0 || nsg > 16)
  290. goto nomem;
  291. sg_init_table(sg, nsg);
  292. skb_to_sgvec(skb, sg, 0, 8);
  293. /* start the decryption afresh */
  294. memset(&iv, 0, sizeof(iv));
  295. desc.tfm = call->conn->cipher;
  296. desc.info = iv.x;
  297. desc.flags = 0;
  298. crypto_blkcipher_decrypt_iv(&desc, sg, sg, 8);
  299. /* remove the decrypted packet length */
  300. if (skb_copy_bits(skb, 0, &sechdr, sizeof(sechdr)) < 0)
  301. goto datalen_error;
  302. if (!skb_pull(skb, sizeof(sechdr)))
  303. BUG();
  304. buf = ntohl(sechdr.data_size);
  305. data_size = buf & 0xffff;
  306. check = buf >> 16;
  307. check ^= ntohl(sp->hdr.seq ^ sp->hdr.callNumber);
  308. check &= 0xffff;
  309. if (check != 0) {
  310. *_abort_code = RXKADSEALEDINCON;
  311. goto protocol_error;
  312. }
  313. /* shorten the packet to remove the padding */
  314. if (data_size > skb->len)
  315. goto datalen_error;
  316. else if (data_size < skb->len)
  317. skb->len = data_size;
  318. _leave(" = 0 [dlen=%x]", data_size);
  319. return 0;
  320. datalen_error:
  321. *_abort_code = RXKADDATALEN;
  322. protocol_error:
  323. _leave(" = -EPROTO");
  324. return -EPROTO;
  325. nomem:
  326. _leave(" = -ENOMEM");
  327. return -ENOMEM;
  328. }
  329. /*
  330. * wholly decrypt a packet (level 2 security)
  331. */
  332. static int rxkad_verify_packet_encrypt(const struct rxrpc_call *call,
  333. struct sk_buff *skb,
  334. u32 *_abort_code)
  335. {
  336. const struct rxrpc_key_token *token;
  337. struct rxkad_level2_hdr sechdr;
  338. struct rxrpc_skb_priv *sp;
  339. struct blkcipher_desc desc;
  340. struct rxrpc_crypt iv;
  341. struct scatterlist _sg[4], *sg;
  342. struct sk_buff *trailer;
  343. u32 data_size, buf;
  344. u16 check;
  345. int nsg;
  346. _enter(",{%d}", skb->len);
  347. sp = rxrpc_skb(skb);
  348. /* we want to decrypt the skbuff in-place */
  349. nsg = skb_cow_data(skb, 0, &trailer);
  350. if (nsg < 0)
  351. goto nomem;
  352. sg = _sg;
  353. if (unlikely(nsg > 4)) {
  354. sg = kmalloc(sizeof(*sg) * nsg, GFP_NOIO);
  355. if (!sg)
  356. goto nomem;
  357. }
  358. sg_init_table(sg, nsg);
  359. skb_to_sgvec(skb, sg, 0, skb->len);
  360. /* decrypt from the session key */
  361. token = call->conn->key->payload.data;
  362. memcpy(&iv, token->kad->session_key, sizeof(iv));
  363. desc.tfm = call->conn->cipher;
  364. desc.info = iv.x;
  365. desc.flags = 0;
  366. crypto_blkcipher_decrypt_iv(&desc, sg, sg, skb->len);
  367. if (sg != _sg)
  368. kfree(sg);
  369. /* remove the decrypted packet length */
  370. if (skb_copy_bits(skb, 0, &sechdr, sizeof(sechdr)) < 0)
  371. goto datalen_error;
  372. if (!skb_pull(skb, sizeof(sechdr)))
  373. BUG();
  374. buf = ntohl(sechdr.data_size);
  375. data_size = buf & 0xffff;
  376. check = buf >> 16;
  377. check ^= ntohl(sp->hdr.seq ^ sp->hdr.callNumber);
  378. check &= 0xffff;
  379. if (check != 0) {
  380. *_abort_code = RXKADSEALEDINCON;
  381. goto protocol_error;
  382. }
  383. /* shorten the packet to remove the padding */
  384. if (data_size > skb->len)
  385. goto datalen_error;
  386. else if (data_size < skb->len)
  387. skb->len = data_size;
  388. _leave(" = 0 [dlen=%x]", data_size);
  389. return 0;
  390. datalen_error:
  391. *_abort_code = RXKADDATALEN;
  392. protocol_error:
  393. _leave(" = -EPROTO");
  394. return -EPROTO;
  395. nomem:
  396. _leave(" = -ENOMEM");
  397. return -ENOMEM;
  398. }
  399. /*
  400. * verify the security on a received packet
  401. */
  402. static int rxkad_verify_packet(const struct rxrpc_call *call,
  403. struct sk_buff *skb,
  404. u32 *_abort_code)
  405. {
  406. struct blkcipher_desc desc;
  407. struct rxrpc_skb_priv *sp;
  408. struct rxrpc_crypt iv;
  409. struct scatterlist sg[2];
  410. struct {
  411. __be32 x[2];
  412. } tmpbuf __attribute__((aligned(8))); /* must all be in same page */
  413. __be32 x;
  414. __be16 cksum;
  415. u32 y;
  416. int ret;
  417. sp = rxrpc_skb(skb);
  418. _enter("{%d{%x}},{#%u}",
  419. call->debug_id, key_serial(call->conn->key),
  420. ntohl(sp->hdr.seq));
  421. if (!call->conn->cipher)
  422. return 0;
  423. if (sp->hdr.securityIndex != RXRPC_SECURITY_RXKAD) {
  424. *_abort_code = RXKADINCONSISTENCY;
  425. _leave(" = -EPROTO [not rxkad]");
  426. return -EPROTO;
  427. }
  428. /* continue encrypting from where we left off */
  429. memcpy(&iv, call->conn->csum_iv.x, sizeof(iv));
  430. desc.tfm = call->conn->cipher;
  431. desc.info = iv.x;
  432. desc.flags = 0;
  433. /* validate the security checksum */
  434. x = htonl(call->channel << (32 - RXRPC_CIDSHIFT));
  435. x |= sp->hdr.seq & cpu_to_be32(0x3fffffff);
  436. tmpbuf.x[0] = call->call_id;
  437. tmpbuf.x[1] = x;
  438. sg_init_one(&sg[0], &tmpbuf, sizeof(tmpbuf));
  439. sg_init_one(&sg[1], &tmpbuf, sizeof(tmpbuf));
  440. crypto_blkcipher_encrypt_iv(&desc, &sg[0], &sg[1], sizeof(tmpbuf));
  441. y = ntohl(tmpbuf.x[1]);
  442. y = (y >> 16) & 0xffff;
  443. if (y == 0)
  444. y = 1; /* zero checksums are not permitted */
  445. cksum = htons(y);
  446. if (sp->hdr.cksum != cksum) {
  447. *_abort_code = RXKADSEALEDINCON;
  448. _leave(" = -EPROTO [csum failed]");
  449. return -EPROTO;
  450. }
  451. switch (call->conn->security_level) {
  452. case RXRPC_SECURITY_PLAIN:
  453. ret = 0;
  454. break;
  455. case RXRPC_SECURITY_AUTH:
  456. ret = rxkad_verify_packet_auth(call, skb, _abort_code);
  457. break;
  458. case RXRPC_SECURITY_ENCRYPT:
  459. ret = rxkad_verify_packet_encrypt(call, skb, _abort_code);
  460. break;
  461. default:
  462. ret = -ENOANO;
  463. break;
  464. }
  465. _leave(" = %d", ret);
  466. return ret;
  467. }
  468. /*
  469. * issue a challenge
  470. */
  471. static int rxkad_issue_challenge(struct rxrpc_connection *conn)
  472. {
  473. struct rxkad_challenge challenge;
  474. struct rxrpc_header hdr;
  475. struct msghdr msg;
  476. struct kvec iov[2];
  477. size_t len;
  478. int ret;
  479. _enter("{%d,%x}", conn->debug_id, key_serial(conn->key));
  480. ret = key_validate(conn->key);
  481. if (ret < 0)
  482. return ret;
  483. get_random_bytes(&conn->security_nonce, sizeof(conn->security_nonce));
  484. challenge.version = htonl(2);
  485. challenge.nonce = htonl(conn->security_nonce);
  486. challenge.min_level = htonl(0);
  487. challenge.__padding = 0;
  488. msg.msg_name = &conn->trans->peer->srx.transport.sin;
  489. msg.msg_namelen = sizeof(conn->trans->peer->srx.transport.sin);
  490. msg.msg_control = NULL;
  491. msg.msg_controllen = 0;
  492. msg.msg_flags = 0;
  493. hdr.epoch = conn->epoch;
  494. hdr.cid = conn->cid;
  495. hdr.callNumber = 0;
  496. hdr.seq = 0;
  497. hdr.type = RXRPC_PACKET_TYPE_CHALLENGE;
  498. hdr.flags = conn->out_clientflag;
  499. hdr.userStatus = 0;
  500. hdr.securityIndex = conn->security_ix;
  501. hdr._rsvd = 0;
  502. hdr.serviceId = conn->service_id;
  503. iov[0].iov_base = &hdr;
  504. iov[0].iov_len = sizeof(hdr);
  505. iov[1].iov_base = &challenge;
  506. iov[1].iov_len = sizeof(challenge);
  507. len = iov[0].iov_len + iov[1].iov_len;
  508. hdr.serial = htonl(atomic_inc_return(&conn->serial));
  509. _proto("Tx CHALLENGE %%%u", ntohl(hdr.serial));
  510. ret = kernel_sendmsg(conn->trans->local->socket, &msg, iov, 2, len);
  511. if (ret < 0) {
  512. _debug("sendmsg failed: %d", ret);
  513. return -EAGAIN;
  514. }
  515. _leave(" = 0");
  516. return 0;
  517. }
  518. /*
  519. * send a Kerberos security response
  520. */
  521. static int rxkad_send_response(struct rxrpc_connection *conn,
  522. struct rxrpc_header *hdr,
  523. struct rxkad_response *resp,
  524. const struct rxkad_key *s2)
  525. {
  526. struct msghdr msg;
  527. struct kvec iov[3];
  528. size_t len;
  529. int ret;
  530. _enter("");
  531. msg.msg_name = &conn->trans->peer->srx.transport.sin;
  532. msg.msg_namelen = sizeof(conn->trans->peer->srx.transport.sin);
  533. msg.msg_control = NULL;
  534. msg.msg_controllen = 0;
  535. msg.msg_flags = 0;
  536. hdr->epoch = conn->epoch;
  537. hdr->seq = 0;
  538. hdr->type = RXRPC_PACKET_TYPE_RESPONSE;
  539. hdr->flags = conn->out_clientflag;
  540. hdr->userStatus = 0;
  541. hdr->_rsvd = 0;
  542. iov[0].iov_base = hdr;
  543. iov[0].iov_len = sizeof(*hdr);
  544. iov[1].iov_base = resp;
  545. iov[1].iov_len = sizeof(*resp);
  546. iov[2].iov_base = (void *) s2->ticket;
  547. iov[2].iov_len = s2->ticket_len;
  548. len = iov[0].iov_len + iov[1].iov_len + iov[2].iov_len;
  549. hdr->serial = htonl(atomic_inc_return(&conn->serial));
  550. _proto("Tx RESPONSE %%%u", ntohl(hdr->serial));
  551. ret = kernel_sendmsg(conn->trans->local->socket, &msg, iov, 3, len);
  552. if (ret < 0) {
  553. _debug("sendmsg failed: %d", ret);
  554. return -EAGAIN;
  555. }
  556. _leave(" = 0");
  557. return 0;
  558. }
  559. /*
  560. * calculate the response checksum
  561. */
  562. static void rxkad_calc_response_checksum(struct rxkad_response *response)
  563. {
  564. u32 csum = 1000003;
  565. int loop;
  566. u8 *p = (u8 *) response;
  567. for (loop = sizeof(*response); loop > 0; loop--)
  568. csum = csum * 0x10204081 + *p++;
  569. response->encrypted.checksum = htonl(csum);
  570. }
  571. /*
  572. * load a scatterlist with a potentially split-page buffer
  573. */
  574. static void rxkad_sg_set_buf2(struct scatterlist sg[2],
  575. void *buf, size_t buflen)
  576. {
  577. int nsg = 1;
  578. sg_init_table(sg, 2);
  579. sg_set_buf(&sg[0], buf, buflen);
  580. if (sg[0].offset + buflen > PAGE_SIZE) {
  581. /* the buffer was split over two pages */
  582. sg[0].length = PAGE_SIZE - sg[0].offset;
  583. sg_set_buf(&sg[1], buf + sg[0].length, buflen - sg[0].length);
  584. nsg++;
  585. }
  586. sg_mark_end(&sg[nsg - 1]);
  587. ASSERTCMP(sg[0].length + sg[1].length, ==, buflen);
  588. }
  589. /*
  590. * encrypt the response packet
  591. */
  592. static void rxkad_encrypt_response(struct rxrpc_connection *conn,
  593. struct rxkad_response *resp,
  594. const struct rxkad_key *s2)
  595. {
  596. struct blkcipher_desc desc;
  597. struct rxrpc_crypt iv;
  598. struct scatterlist sg[2];
  599. /* continue encrypting from where we left off */
  600. memcpy(&iv, s2->session_key, sizeof(iv));
  601. desc.tfm = conn->cipher;
  602. desc.info = iv.x;
  603. desc.flags = 0;
  604. rxkad_sg_set_buf2(sg, &resp->encrypted, sizeof(resp->encrypted));
  605. crypto_blkcipher_encrypt_iv(&desc, sg, sg, sizeof(resp->encrypted));
  606. }
  607. /*
  608. * respond to a challenge packet
  609. */
  610. static int rxkad_respond_to_challenge(struct rxrpc_connection *conn,
  611. struct sk_buff *skb,
  612. u32 *_abort_code)
  613. {
  614. const struct rxrpc_key_token *token;
  615. struct rxkad_challenge challenge;
  616. struct rxkad_response resp
  617. __attribute__((aligned(8))); /* must be aligned for crypto */
  618. struct rxrpc_skb_priv *sp;
  619. u32 version, nonce, min_level, abort_code;
  620. int ret;
  621. _enter("{%d,%x}", conn->debug_id, key_serial(conn->key));
  622. if (!conn->key) {
  623. _leave(" = -EPROTO [no key]");
  624. return -EPROTO;
  625. }
  626. ret = key_validate(conn->key);
  627. if (ret < 0) {
  628. *_abort_code = RXKADEXPIRED;
  629. return ret;
  630. }
  631. abort_code = RXKADPACKETSHORT;
  632. sp = rxrpc_skb(skb);
  633. if (skb_copy_bits(skb, 0, &challenge, sizeof(challenge)) < 0)
  634. goto protocol_error;
  635. version = ntohl(challenge.version);
  636. nonce = ntohl(challenge.nonce);
  637. min_level = ntohl(challenge.min_level);
  638. _proto("Rx CHALLENGE %%%u { v=%u n=%u ml=%u }",
  639. ntohl(sp->hdr.serial), version, nonce, min_level);
  640. abort_code = RXKADINCONSISTENCY;
  641. if (version != RXKAD_VERSION)
  642. goto protocol_error;
  643. abort_code = RXKADLEVELFAIL;
  644. if (conn->security_level < min_level)
  645. goto protocol_error;
  646. token = conn->key->payload.data;
  647. /* build the response packet */
  648. memset(&resp, 0, sizeof(resp));
  649. resp.version = RXKAD_VERSION;
  650. resp.encrypted.epoch = conn->epoch;
  651. resp.encrypted.cid = conn->cid;
  652. resp.encrypted.securityIndex = htonl(conn->security_ix);
  653. resp.encrypted.call_id[0] =
  654. (conn->channels[0] ? conn->channels[0]->call_id : 0);
  655. resp.encrypted.call_id[1] =
  656. (conn->channels[1] ? conn->channels[1]->call_id : 0);
  657. resp.encrypted.call_id[2] =
  658. (conn->channels[2] ? conn->channels[2]->call_id : 0);
  659. resp.encrypted.call_id[3] =
  660. (conn->channels[3] ? conn->channels[3]->call_id : 0);
  661. resp.encrypted.inc_nonce = htonl(nonce + 1);
  662. resp.encrypted.level = htonl(conn->security_level);
  663. resp.kvno = htonl(token->kad->kvno);
  664. resp.ticket_len = htonl(token->kad->ticket_len);
  665. /* calculate the response checksum and then do the encryption */
  666. rxkad_calc_response_checksum(&resp);
  667. rxkad_encrypt_response(conn, &resp, token->kad);
  668. return rxkad_send_response(conn, &sp->hdr, &resp, token->kad);
  669. protocol_error:
  670. *_abort_code = abort_code;
  671. _leave(" = -EPROTO [%d]", abort_code);
  672. return -EPROTO;
  673. }
  674. /*
  675. * decrypt the kerberos IV ticket in the response
  676. */
  677. static int rxkad_decrypt_ticket(struct rxrpc_connection *conn,
  678. void *ticket, size_t ticket_len,
  679. struct rxrpc_crypt *_session_key,
  680. time_t *_expiry,
  681. u32 *_abort_code)
  682. {
  683. struct blkcipher_desc desc;
  684. struct rxrpc_crypt iv, key;
  685. struct scatterlist sg[1];
  686. struct in_addr addr;
  687. unsigned int life;
  688. time_t issue, now;
  689. bool little_endian;
  690. int ret;
  691. u8 *p, *q, *name, *end;
  692. _enter("{%d},{%x}", conn->debug_id, key_serial(conn->server_key));
  693. *_expiry = 0;
  694. ret = key_validate(conn->server_key);
  695. if (ret < 0) {
  696. switch (ret) {
  697. case -EKEYEXPIRED:
  698. *_abort_code = RXKADEXPIRED;
  699. goto error;
  700. default:
  701. *_abort_code = RXKADNOAUTH;
  702. goto error;
  703. }
  704. }
  705. ASSERT(conn->server_key->payload.data != NULL);
  706. ASSERTCMP((unsigned long) ticket & 7UL, ==, 0);
  707. memcpy(&iv, &conn->server_key->type_data, sizeof(iv));
  708. desc.tfm = conn->server_key->payload.data;
  709. desc.info = iv.x;
  710. desc.flags = 0;
  711. sg_init_one(&sg[0], ticket, ticket_len);
  712. crypto_blkcipher_decrypt_iv(&desc, sg, sg, ticket_len);
  713. p = ticket;
  714. end = p + ticket_len;
  715. #define Z(size) \
  716. ({ \
  717. u8 *__str = p; \
  718. q = memchr(p, 0, end - p); \
  719. if (!q || q - p > (size)) \
  720. goto bad_ticket; \
  721. for (; p < q; p++) \
  722. if (!isprint(*p)) \
  723. goto bad_ticket; \
  724. p++; \
  725. __str; \
  726. })
  727. /* extract the ticket flags */
  728. _debug("KIV FLAGS: %x", *p);
  729. little_endian = *p & 1;
  730. p++;
  731. /* extract the authentication name */
  732. name = Z(ANAME_SZ);
  733. _debug("KIV ANAME: %s", name);
  734. /* extract the principal's instance */
  735. name = Z(INST_SZ);
  736. _debug("KIV INST : %s", name);
  737. /* extract the principal's authentication domain */
  738. name = Z(REALM_SZ);
  739. _debug("KIV REALM: %s", name);
  740. if (end - p < 4 + 8 + 4 + 2)
  741. goto bad_ticket;
  742. /* get the IPv4 address of the entity that requested the ticket */
  743. memcpy(&addr, p, sizeof(addr));
  744. p += 4;
  745. _debug("KIV ADDR : %pI4", &addr);
  746. /* get the session key from the ticket */
  747. memcpy(&key, p, sizeof(key));
  748. p += 8;
  749. _debug("KIV KEY : %08x %08x", ntohl(key.n[0]), ntohl(key.n[1]));
  750. memcpy(_session_key, &key, sizeof(key));
  751. /* get the ticket's lifetime */
  752. life = *p++ * 5 * 60;
  753. _debug("KIV LIFE : %u", life);
  754. /* get the issue time of the ticket */
  755. if (little_endian) {
  756. __le32 stamp;
  757. memcpy(&stamp, p, 4);
  758. issue = le32_to_cpu(stamp);
  759. } else {
  760. __be32 stamp;
  761. memcpy(&stamp, p, 4);
  762. issue = be32_to_cpu(stamp);
  763. }
  764. p += 4;
  765. now = get_seconds();
  766. _debug("KIV ISSUE: %lx [%lx]", issue, now);
  767. /* check the ticket is in date */
  768. if (issue > now) {
  769. *_abort_code = RXKADNOAUTH;
  770. ret = -EKEYREJECTED;
  771. goto error;
  772. }
  773. if (issue < now - life) {
  774. *_abort_code = RXKADEXPIRED;
  775. ret = -EKEYEXPIRED;
  776. goto error;
  777. }
  778. *_expiry = issue + life;
  779. /* get the service name */
  780. name = Z(SNAME_SZ);
  781. _debug("KIV SNAME: %s", name);
  782. /* get the service instance name */
  783. name = Z(INST_SZ);
  784. _debug("KIV SINST: %s", name);
  785. ret = 0;
  786. error:
  787. _leave(" = %d", ret);
  788. return ret;
  789. bad_ticket:
  790. *_abort_code = RXKADBADTICKET;
  791. ret = -EBADMSG;
  792. goto error;
  793. }
  794. /*
  795. * decrypt the response packet
  796. */
  797. static void rxkad_decrypt_response(struct rxrpc_connection *conn,
  798. struct rxkad_response *resp,
  799. const struct rxrpc_crypt *session_key)
  800. {
  801. struct blkcipher_desc desc;
  802. struct scatterlist sg[2];
  803. struct rxrpc_crypt iv;
  804. _enter(",,%08x%08x",
  805. ntohl(session_key->n[0]), ntohl(session_key->n[1]));
  806. ASSERT(rxkad_ci != NULL);
  807. mutex_lock(&rxkad_ci_mutex);
  808. if (crypto_blkcipher_setkey(rxkad_ci, session_key->x,
  809. sizeof(*session_key)) < 0)
  810. BUG();
  811. memcpy(&iv, session_key, sizeof(iv));
  812. desc.tfm = rxkad_ci;
  813. desc.info = iv.x;
  814. desc.flags = 0;
  815. rxkad_sg_set_buf2(sg, &resp->encrypted, sizeof(resp->encrypted));
  816. crypto_blkcipher_decrypt_iv(&desc, sg, sg, sizeof(resp->encrypted));
  817. mutex_unlock(&rxkad_ci_mutex);
  818. _leave("");
  819. }
  820. /*
  821. * verify a response
  822. */
  823. static int rxkad_verify_response(struct rxrpc_connection *conn,
  824. struct sk_buff *skb,
  825. u32 *_abort_code)
  826. {
  827. struct rxkad_response response
  828. __attribute__((aligned(8))); /* must be aligned for crypto */
  829. struct rxrpc_skb_priv *sp;
  830. struct rxrpc_crypt session_key;
  831. time_t expiry;
  832. void *ticket;
  833. u32 abort_code, version, kvno, ticket_len, level;
  834. __be32 csum;
  835. int ret;
  836. _enter("{%d,%x}", conn->debug_id, key_serial(conn->server_key));
  837. abort_code = RXKADPACKETSHORT;
  838. if (skb_copy_bits(skb, 0, &response, sizeof(response)) < 0)
  839. goto protocol_error;
  840. if (!pskb_pull(skb, sizeof(response)))
  841. BUG();
  842. version = ntohl(response.version);
  843. ticket_len = ntohl(response.ticket_len);
  844. kvno = ntohl(response.kvno);
  845. sp = rxrpc_skb(skb);
  846. _proto("Rx RESPONSE %%%u { v=%u kv=%u tl=%u }",
  847. ntohl(sp->hdr.serial), version, kvno, ticket_len);
  848. abort_code = RXKADINCONSISTENCY;
  849. if (version != RXKAD_VERSION)
  850. goto protocol_error;
  851. abort_code = RXKADTICKETLEN;
  852. if (ticket_len < 4 || ticket_len > MAXKRB5TICKETLEN)
  853. goto protocol_error;
  854. abort_code = RXKADUNKNOWNKEY;
  855. if (kvno >= RXKAD_TKT_TYPE_KERBEROS_V5)
  856. goto protocol_error;
  857. /* extract the kerberos ticket and decrypt and decode it */
  858. ticket = kmalloc(ticket_len, GFP_NOFS);
  859. if (!ticket)
  860. return -ENOMEM;
  861. abort_code = RXKADPACKETSHORT;
  862. if (skb_copy_bits(skb, 0, ticket, ticket_len) < 0)
  863. goto protocol_error_free;
  864. ret = rxkad_decrypt_ticket(conn, ticket, ticket_len, &session_key,
  865. &expiry, &abort_code);
  866. if (ret < 0) {
  867. *_abort_code = abort_code;
  868. kfree(ticket);
  869. return ret;
  870. }
  871. /* use the session key from inside the ticket to decrypt the
  872. * response */
  873. rxkad_decrypt_response(conn, &response, &session_key);
  874. abort_code = RXKADSEALEDINCON;
  875. if (response.encrypted.epoch != conn->epoch)
  876. goto protocol_error_free;
  877. if (response.encrypted.cid != conn->cid)
  878. goto protocol_error_free;
  879. if (ntohl(response.encrypted.securityIndex) != conn->security_ix)
  880. goto protocol_error_free;
  881. csum = response.encrypted.checksum;
  882. response.encrypted.checksum = 0;
  883. rxkad_calc_response_checksum(&response);
  884. if (response.encrypted.checksum != csum)
  885. goto protocol_error_free;
  886. if (ntohl(response.encrypted.call_id[0]) > INT_MAX ||
  887. ntohl(response.encrypted.call_id[1]) > INT_MAX ||
  888. ntohl(response.encrypted.call_id[2]) > INT_MAX ||
  889. ntohl(response.encrypted.call_id[3]) > INT_MAX)
  890. goto protocol_error_free;
  891. abort_code = RXKADOUTOFSEQUENCE;
  892. if (response.encrypted.inc_nonce != htonl(conn->security_nonce + 1))
  893. goto protocol_error_free;
  894. abort_code = RXKADLEVELFAIL;
  895. level = ntohl(response.encrypted.level);
  896. if (level > RXRPC_SECURITY_ENCRYPT)
  897. goto protocol_error_free;
  898. conn->security_level = level;
  899. /* create a key to hold the security data and expiration time - after
  900. * this the connection security can be handled in exactly the same way
  901. * as for a client connection */
  902. ret = rxrpc_get_server_data_key(conn, &session_key, expiry, kvno);
  903. if (ret < 0) {
  904. kfree(ticket);
  905. return ret;
  906. }
  907. kfree(ticket);
  908. _leave(" = 0");
  909. return 0;
  910. protocol_error_free:
  911. kfree(ticket);
  912. protocol_error:
  913. *_abort_code = abort_code;
  914. _leave(" = -EPROTO [%d]", abort_code);
  915. return -EPROTO;
  916. }
  917. /*
  918. * clear the connection security
  919. */
  920. static void rxkad_clear(struct rxrpc_connection *conn)
  921. {
  922. _enter("");
  923. if (conn->cipher)
  924. crypto_free_blkcipher(conn->cipher);
  925. }
  926. /*
  927. * RxRPC Kerberos-based security
  928. */
  929. static struct rxrpc_security rxkad = {
  930. .owner = THIS_MODULE,
  931. .name = "rxkad",
  932. .security_index = RXRPC_SECURITY_RXKAD,
  933. .init_connection_security = rxkad_init_connection_security,
  934. .prime_packet_security = rxkad_prime_packet_security,
  935. .secure_packet = rxkad_secure_packet,
  936. .verify_packet = rxkad_verify_packet,
  937. .issue_challenge = rxkad_issue_challenge,
  938. .respond_to_challenge = rxkad_respond_to_challenge,
  939. .verify_response = rxkad_verify_response,
  940. .clear = rxkad_clear,
  941. };
  942. static __init int rxkad_init(void)
  943. {
  944. _enter("");
  945. /* pin the cipher we need so that the crypto layer doesn't invoke
  946. * keventd to go get it */
  947. rxkad_ci = crypto_alloc_blkcipher("pcbc(fcrypt)", 0, CRYPTO_ALG_ASYNC);
  948. if (IS_ERR(rxkad_ci))
  949. return PTR_ERR(rxkad_ci);
  950. return rxrpc_register_security(&rxkad);
  951. }
  952. module_init(rxkad_init);
  953. static __exit void rxkad_exit(void)
  954. {
  955. _enter("");
  956. rxrpc_unregister_security(&rxkad);
  957. crypto_free_blkcipher(rxkad_ci);
  958. }
  959. module_exit(rxkad_exit);