rxkad.c 27 KB

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