rxkad.c 27 KB

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