rxkad.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154
  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. memset(sg, 0, sizeof(sg));
  118. sg_set_buf(&sg[0], &tmpbuf, sizeof(tmpbuf));
  119. sg_set_buf(&sg[1], &tmpbuf, sizeof(tmpbuf));
  120. crypto_blkcipher_encrypt_iv(&desc, &sg[0], &sg[1], sizeof(tmpbuf));
  121. memcpy(&conn->csum_iv, &tmpbuf.x[2], sizeof(conn->csum_iv));
  122. ASSERTCMP(conn->csum_iv.n[0], ==, tmpbuf.x[2]);
  123. _leave("");
  124. }
  125. /*
  126. * partially encrypt a packet (level 1 security)
  127. */
  128. static int rxkad_secure_packet_auth(const struct rxrpc_call *call,
  129. struct sk_buff *skb,
  130. u32 data_size,
  131. void *sechdr)
  132. {
  133. struct rxrpc_skb_priv *sp;
  134. struct blkcipher_desc desc;
  135. struct rxrpc_crypt iv;
  136. struct scatterlist sg[2];
  137. struct {
  138. struct rxkad_level1_hdr hdr;
  139. __be32 first; /* first four bytes of data and padding */
  140. } tmpbuf __attribute__((aligned(8))); /* must all be in same page */
  141. u16 check;
  142. sp = rxrpc_skb(skb);
  143. _enter("");
  144. check = ntohl(sp->hdr.seq ^ sp->hdr.callNumber);
  145. data_size |= (u32) check << 16;
  146. tmpbuf.hdr.data_size = htonl(data_size);
  147. memcpy(&tmpbuf.first, sechdr + 4, sizeof(tmpbuf.first));
  148. /* start the encryption afresh */
  149. memset(&iv, 0, sizeof(iv));
  150. desc.tfm = call->conn->cipher;
  151. desc.info = iv.x;
  152. desc.flags = 0;
  153. memset(sg, 0, sizeof(sg));
  154. sg_set_buf(&sg[0], &tmpbuf, sizeof(tmpbuf));
  155. sg_set_buf(&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_payload *payload;
  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 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. payload = call->conn->key->payload.data;
  187. memcpy(&iv, payload->k.session_key, sizeof(iv));
  188. desc.tfm = call->conn->cipher;
  189. desc.info = iv.x;
  190. desc.flags = 0;
  191. memset(sg, 0, sizeof(sg[0]) * 2);
  192. sg_set_buf(&sg[0], sechdr, sizeof(rxkhdr));
  193. sg_set_buf(&sg[1], &rxkhdr, sizeof(rxkhdr));
  194. crypto_blkcipher_encrypt_iv(&desc, &sg[0], &sg[1], sizeof(rxkhdr));
  195. /* we want to encrypt the skbuff in-place */
  196. nsg = skb_cow_data(skb, 0, &trailer);
  197. if (nsg < 0 || nsg > 16)
  198. return -ENOMEM;
  199. len = data_size + call->conn->size_align - 1;
  200. len &= ~(call->conn->size_align - 1);
  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. int ret;
  223. sp = rxrpc_skb(skb);
  224. _enter("{%d{%x}},{#%u},%zu,",
  225. call->debug_id, key_serial(call->conn->key), ntohl(sp->hdr.seq),
  226. data_size);
  227. if (!call->conn->cipher)
  228. return 0;
  229. ret = key_validate(call->conn->key);
  230. if (ret < 0)
  231. return ret;
  232. /* continue encrypting from where we left off */
  233. memcpy(&iv, call->conn->csum_iv.x, sizeof(iv));
  234. desc.tfm = call->conn->cipher;
  235. desc.info = iv.x;
  236. desc.flags = 0;
  237. /* calculate the security checksum */
  238. x = htonl(call->channel << (32 - RXRPC_CIDSHIFT));
  239. x |= sp->hdr.seq & __constant_cpu_to_be32(0x3fffffff);
  240. tmpbuf.x[0] = sp->hdr.callNumber;
  241. tmpbuf.x[1] = x;
  242. memset(&sg, 0, sizeof(sg));
  243. sg_set_buf(&sg[0], &tmpbuf, sizeof(tmpbuf));
  244. sg_set_buf(&sg[1], &tmpbuf, sizeof(tmpbuf));
  245. crypto_blkcipher_encrypt_iv(&desc, &sg[0], &sg[1], sizeof(tmpbuf));
  246. x = ntohl(tmpbuf.x[1]);
  247. x = (x >> 16) & 0xffff;
  248. if (x == 0)
  249. x = 1; /* zero checksums are not permitted */
  250. sp->hdr.cksum = htons(x);
  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, x);
  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[2];
  281. struct sk_buff *trailer;
  282. u32 data_size, buf;
  283. u16 check;
  284. _enter("");
  285. sp = rxrpc_skb(skb);
  286. /* we want to decrypt the skbuff in-place */
  287. if (skb_cow_data(skb, 0, &trailer) < 0)
  288. goto nomem;
  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. skb_to_sgvec(skb, sg, 0, skb->len);
  356. /* decrypt from the session key */
  357. payload = call->conn->key->payload.data;
  358. memcpy(&iv, payload->k.session_key, sizeof(iv));
  359. desc.tfm = call->conn->cipher;
  360. desc.info = iv.x;
  361. desc.flags = 0;
  362. crypto_blkcipher_decrypt_iv(&desc, sg, sg, skb->len);
  363. if (sg != _sg)
  364. kfree(sg);
  365. /* remove the decrypted packet length */
  366. if (skb_copy_bits(skb, 0, &sechdr, sizeof(sechdr)) < 0)
  367. goto datalen_error;
  368. if (!skb_pull(skb, sizeof(sechdr)))
  369. BUG();
  370. buf = ntohl(sechdr.data_size);
  371. data_size = buf & 0xffff;
  372. check = buf >> 16;
  373. check ^= ntohl(sp->hdr.seq ^ sp->hdr.callNumber);
  374. check &= 0xffff;
  375. if (check != 0) {
  376. *_abort_code = RXKADSEALEDINCON;
  377. goto protocol_error;
  378. }
  379. /* shorten the packet to remove the padding */
  380. if (data_size > skb->len)
  381. goto datalen_error;
  382. else if (data_size < skb->len)
  383. skb->len = data_size;
  384. _leave(" = 0 [dlen=%x]", data_size);
  385. return 0;
  386. datalen_error:
  387. *_abort_code = RXKADDATALEN;
  388. protocol_error:
  389. _leave(" = -EPROTO");
  390. return -EPROTO;
  391. nomem:
  392. _leave(" = -ENOMEM");
  393. return -ENOMEM;
  394. }
  395. /*
  396. * verify the security on a received packet
  397. */
  398. static int rxkad_verify_packet(const struct rxrpc_call *call,
  399. struct sk_buff *skb,
  400. u32 *_abort_code)
  401. {
  402. struct blkcipher_desc desc;
  403. struct rxrpc_skb_priv *sp;
  404. struct rxrpc_crypt iv;
  405. struct scatterlist sg[2];
  406. struct {
  407. __be32 x[2];
  408. } tmpbuf __attribute__((aligned(8))); /* must all be in same page */
  409. __be32 x;
  410. __be16 cksum;
  411. int ret;
  412. sp = rxrpc_skb(skb);
  413. _enter("{%d{%x}},{#%u}",
  414. call->debug_id, key_serial(call->conn->key),
  415. ntohl(sp->hdr.seq));
  416. if (!call->conn->cipher)
  417. return 0;
  418. if (sp->hdr.securityIndex != 2) {
  419. *_abort_code = RXKADINCONSISTENCY;
  420. _leave(" = -EPROTO [not rxkad]");
  421. return -EPROTO;
  422. }
  423. /* continue encrypting from where we left off */
  424. memcpy(&iv, call->conn->csum_iv.x, sizeof(iv));
  425. desc.tfm = call->conn->cipher;
  426. desc.info = iv.x;
  427. desc.flags = 0;
  428. /* validate the security checksum */
  429. x = htonl(call->channel << (32 - RXRPC_CIDSHIFT));
  430. x |= sp->hdr.seq & __constant_cpu_to_be32(0x3fffffff);
  431. tmpbuf.x[0] = call->call_id;
  432. tmpbuf.x[1] = x;
  433. memset(&sg, 0, sizeof(sg));
  434. sg_set_buf(&sg[0], &tmpbuf, sizeof(tmpbuf));
  435. sg_set_buf(&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. memset(sg, 0, sizeof(sg));
  574. sg_set_buf(&sg[0], buf, buflen);
  575. if (sg[0].offset + buflen > PAGE_SIZE) {
  576. /* the buffer was split over two pages */
  577. sg[0].length = PAGE_SIZE - sg[0].offset;
  578. sg_set_buf(&sg[1], buf + sg[0].length, buflen - sg[0].length);
  579. }
  580. ASSERTCMP(sg[0].length + sg[1].length, ==, buflen);
  581. }
  582. /*
  583. * encrypt the response packet
  584. */
  585. static void rxkad_encrypt_response(struct rxrpc_connection *conn,
  586. struct rxkad_response *resp,
  587. const struct rxkad_key *s2)
  588. {
  589. struct blkcipher_desc desc;
  590. struct rxrpc_crypt iv;
  591. struct scatterlist ssg[2], dsg[2];
  592. /* continue encrypting from where we left off */
  593. memcpy(&iv, s2->session_key, sizeof(iv));
  594. desc.tfm = conn->cipher;
  595. desc.info = iv.x;
  596. desc.flags = 0;
  597. rxkad_sg_set_buf2(ssg, &resp->encrypted, sizeof(resp->encrypted));
  598. memcpy(dsg, ssg, sizeof(dsg));
  599. crypto_blkcipher_encrypt_iv(&desc, dsg, ssg, sizeof(resp->encrypted));
  600. }
  601. /*
  602. * respond to a challenge packet
  603. */
  604. static int rxkad_respond_to_challenge(struct rxrpc_connection *conn,
  605. struct sk_buff *skb,
  606. u32 *_abort_code)
  607. {
  608. const struct rxrpc_key_payload *payload;
  609. struct rxkad_challenge challenge;
  610. struct rxkad_response resp
  611. __attribute__((aligned(8))); /* must be aligned for crypto */
  612. struct rxrpc_skb_priv *sp;
  613. u32 version, nonce, min_level, abort_code;
  614. int ret;
  615. _enter("{%d,%x}", conn->debug_id, key_serial(conn->key));
  616. if (!conn->key) {
  617. _leave(" = -EPROTO [no key]");
  618. return -EPROTO;
  619. }
  620. ret = key_validate(conn->key);
  621. if (ret < 0) {
  622. *_abort_code = RXKADEXPIRED;
  623. return ret;
  624. }
  625. abort_code = RXKADPACKETSHORT;
  626. sp = rxrpc_skb(skb);
  627. if (skb_copy_bits(skb, 0, &challenge, sizeof(challenge)) < 0)
  628. goto protocol_error;
  629. version = ntohl(challenge.version);
  630. nonce = ntohl(challenge.nonce);
  631. min_level = ntohl(challenge.min_level);
  632. _proto("Rx CHALLENGE %%%u { v=%u n=%u ml=%u }",
  633. ntohl(sp->hdr.serial), version, nonce, min_level);
  634. abort_code = RXKADINCONSISTENCY;
  635. if (version != RXKAD_VERSION)
  636. goto protocol_error;
  637. abort_code = RXKADLEVELFAIL;
  638. if (conn->security_level < min_level)
  639. goto protocol_error;
  640. payload = conn->key->payload.data;
  641. /* build the response packet */
  642. memset(&resp, 0, sizeof(resp));
  643. resp.version = RXKAD_VERSION;
  644. resp.encrypted.epoch = conn->epoch;
  645. resp.encrypted.cid = conn->cid;
  646. resp.encrypted.securityIndex = htonl(conn->security_ix);
  647. resp.encrypted.call_id[0] =
  648. (conn->channels[0] ? conn->channels[0]->call_id : 0);
  649. resp.encrypted.call_id[1] =
  650. (conn->channels[1] ? conn->channels[1]->call_id : 0);
  651. resp.encrypted.call_id[2] =
  652. (conn->channels[2] ? conn->channels[2]->call_id : 0);
  653. resp.encrypted.call_id[3] =
  654. (conn->channels[3] ? conn->channels[3]->call_id : 0);
  655. resp.encrypted.inc_nonce = htonl(nonce + 1);
  656. resp.encrypted.level = htonl(conn->security_level);
  657. resp.kvno = htonl(payload->k.kvno);
  658. resp.ticket_len = htonl(payload->k.ticket_len);
  659. /* calculate the response checksum and then do the encryption */
  660. rxkad_calc_response_checksum(&resp);
  661. rxkad_encrypt_response(conn, &resp, &payload->k);
  662. return rxkad_send_response(conn, &sp->hdr, &resp, &payload->k);
  663. protocol_error:
  664. *_abort_code = abort_code;
  665. _leave(" = -EPROTO [%d]", abort_code);
  666. return -EPROTO;
  667. }
  668. /*
  669. * decrypt the kerberos IV ticket in the response
  670. */
  671. static int rxkad_decrypt_ticket(struct rxrpc_connection *conn,
  672. void *ticket, size_t ticket_len,
  673. struct rxrpc_crypt *_session_key,
  674. time_t *_expiry,
  675. u32 *_abort_code)
  676. {
  677. struct blkcipher_desc desc;
  678. struct rxrpc_crypt iv, key;
  679. struct scatterlist ssg[1], dsg[1];
  680. struct in_addr addr;
  681. unsigned life;
  682. time_t issue, now;
  683. bool little_endian;
  684. int ret;
  685. u8 *p, *q, *name, *end;
  686. _enter("{%d},{%x}", conn->debug_id, key_serial(conn->server_key));
  687. *_expiry = 0;
  688. ret = key_validate(conn->server_key);
  689. if (ret < 0) {
  690. switch (ret) {
  691. case -EKEYEXPIRED:
  692. *_abort_code = RXKADEXPIRED;
  693. goto error;
  694. default:
  695. *_abort_code = RXKADNOAUTH;
  696. goto error;
  697. }
  698. }
  699. ASSERT(conn->server_key->payload.data != NULL);
  700. ASSERTCMP((unsigned long) ticket & 7UL, ==, 0);
  701. memcpy(&iv, &conn->server_key->type_data, sizeof(iv));
  702. desc.tfm = conn->server_key->payload.data;
  703. desc.info = iv.x;
  704. desc.flags = 0;
  705. sg_init_one(&ssg[0], ticket, ticket_len);
  706. memcpy(dsg, ssg, sizeof(dsg));
  707. crypto_blkcipher_decrypt_iv(&desc, dsg, ssg, ticket_len);
  708. p = ticket;
  709. end = p + ticket_len;
  710. #define Z(size) \
  711. ({ \
  712. u8 *__str = p; \
  713. q = memchr(p, 0, end - p); \
  714. if (!q || q - p > (size)) \
  715. goto bad_ticket; \
  716. for (; p < q; p++) \
  717. if (!isprint(*p)) \
  718. goto bad_ticket; \
  719. p++; \
  720. __str; \
  721. })
  722. /* extract the ticket flags */
  723. _debug("KIV FLAGS: %x", *p);
  724. little_endian = *p & 1;
  725. p++;
  726. /* extract the authentication name */
  727. name = Z(ANAME_SZ);
  728. _debug("KIV ANAME: %s", name);
  729. /* extract the principal's instance */
  730. name = Z(INST_SZ);
  731. _debug("KIV INST : %s", name);
  732. /* extract the principal's authentication domain */
  733. name = Z(REALM_SZ);
  734. _debug("KIV REALM: %s", name);
  735. if (end - p < 4 + 8 + 4 + 2)
  736. goto bad_ticket;
  737. /* get the IPv4 address of the entity that requested the ticket */
  738. memcpy(&addr, p, sizeof(addr));
  739. p += 4;
  740. _debug("KIV ADDR : "NIPQUAD_FMT, NIPQUAD(addr));
  741. /* get the session key from the ticket */
  742. memcpy(&key, p, sizeof(key));
  743. p += 8;
  744. _debug("KIV KEY : %08x %08x", ntohl(key.n[0]), ntohl(key.n[1]));
  745. memcpy(_session_key, &key, sizeof(key));
  746. /* get the ticket's lifetime */
  747. life = *p++ * 5 * 60;
  748. _debug("KIV LIFE : %u", life);
  749. /* get the issue time of the ticket */
  750. if (little_endian) {
  751. __le32 stamp;
  752. memcpy(&stamp, p, 4);
  753. issue = le32_to_cpu(stamp);
  754. } else {
  755. __be32 stamp;
  756. memcpy(&stamp, p, 4);
  757. issue = be32_to_cpu(stamp);
  758. }
  759. p += 4;
  760. now = get_seconds();
  761. _debug("KIV ISSUE: %lx [%lx]", issue, now);
  762. /* check the ticket is in date */
  763. if (issue > now) {
  764. *_abort_code = RXKADNOAUTH;
  765. ret = -EKEYREJECTED;
  766. goto error;
  767. }
  768. if (issue < now - life) {
  769. *_abort_code = RXKADEXPIRED;
  770. ret = -EKEYEXPIRED;
  771. goto error;
  772. }
  773. *_expiry = issue + life;
  774. /* get the service name */
  775. name = Z(SNAME_SZ);
  776. _debug("KIV SNAME: %s", name);
  777. /* get the service instance name */
  778. name = Z(INST_SZ);
  779. _debug("KIV SINST: %s", name);
  780. ret = 0;
  781. error:
  782. _leave(" = %d", ret);
  783. return ret;
  784. bad_ticket:
  785. *_abort_code = RXKADBADTICKET;
  786. ret = -EBADMSG;
  787. goto error;
  788. }
  789. /*
  790. * decrypt the response packet
  791. */
  792. static void rxkad_decrypt_response(struct rxrpc_connection *conn,
  793. struct rxkad_response *resp,
  794. const struct rxrpc_crypt *session_key)
  795. {
  796. struct blkcipher_desc desc;
  797. struct scatterlist ssg[2], dsg[2];
  798. struct rxrpc_crypt iv;
  799. _enter(",,%08x%08x",
  800. ntohl(session_key->n[0]), ntohl(session_key->n[1]));
  801. ASSERT(rxkad_ci != NULL);
  802. mutex_lock(&rxkad_ci_mutex);
  803. if (crypto_blkcipher_setkey(rxkad_ci, session_key->x,
  804. sizeof(*session_key)) < 0)
  805. BUG();
  806. memcpy(&iv, session_key, sizeof(iv));
  807. desc.tfm = rxkad_ci;
  808. desc.info = iv.x;
  809. desc.flags = 0;
  810. rxkad_sg_set_buf2(ssg, &resp->encrypted, sizeof(resp->encrypted));
  811. memcpy(dsg, ssg, sizeof(dsg));
  812. crypto_blkcipher_decrypt_iv(&desc, dsg, ssg, 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. abort_code = RXKADTICKETLEN;
  846. if (ticket_len < 4 || ticket_len > MAXKRB5TICKETLEN)
  847. goto protocol_error;
  848. abort_code = RXKADUNKNOWNKEY;
  849. if (kvno >= RXKAD_TKT_TYPE_KERBEROS_V5)
  850. goto protocol_error;
  851. /* extract the kerberos ticket and decrypt and decode it */
  852. ticket = kmalloc(ticket_len, GFP_NOFS);
  853. if (!ticket)
  854. return -ENOMEM;
  855. abort_code = RXKADPACKETSHORT;
  856. if (skb_copy_bits(skb, 0, ticket, ticket_len) < 0)
  857. goto protocol_error_free;
  858. ret = rxkad_decrypt_ticket(conn, ticket, ticket_len, &session_key,
  859. &expiry, &abort_code);
  860. if (ret < 0) {
  861. *_abort_code = abort_code;
  862. kfree(ticket);
  863. return ret;
  864. }
  865. /* use the session key from inside the ticket to decrypt the
  866. * response */
  867. rxkad_decrypt_response(conn, &response, &session_key);
  868. abort_code = RXKADSEALEDINCON;
  869. if (response.encrypted.epoch != conn->epoch)
  870. goto protocol_error_free;
  871. if (response.encrypted.cid != conn->cid)
  872. goto protocol_error_free;
  873. if (ntohl(response.encrypted.securityIndex) != conn->security_ix)
  874. goto protocol_error_free;
  875. csum = response.encrypted.checksum;
  876. response.encrypted.checksum = 0;
  877. rxkad_calc_response_checksum(&response);
  878. if (response.encrypted.checksum != csum)
  879. goto protocol_error_free;
  880. if (ntohl(response.encrypted.call_id[0]) > INT_MAX ||
  881. ntohl(response.encrypted.call_id[1]) > INT_MAX ||
  882. ntohl(response.encrypted.call_id[2]) > INT_MAX ||
  883. ntohl(response.encrypted.call_id[3]) > INT_MAX)
  884. goto protocol_error_free;
  885. abort_code = RXKADOUTOFSEQUENCE;
  886. if (response.encrypted.inc_nonce != htonl(conn->security_nonce + 1))
  887. goto protocol_error_free;
  888. abort_code = RXKADLEVELFAIL;
  889. level = ntohl(response.encrypted.level);
  890. if (level > RXRPC_SECURITY_ENCRYPT)
  891. goto protocol_error_free;
  892. conn->security_level = level;
  893. /* create a key to hold the security data and expiration time - after
  894. * this the connection security can be handled in exactly the same way
  895. * as for a client connection */
  896. ret = rxrpc_get_server_data_key(conn, &session_key, expiry, kvno);
  897. if (ret < 0) {
  898. kfree(ticket);
  899. return ret;
  900. }
  901. kfree(ticket);
  902. _leave(" = 0");
  903. return 0;
  904. protocol_error_free:
  905. kfree(ticket);
  906. protocol_error:
  907. *_abort_code = abort_code;
  908. _leave(" = -EPROTO [%d]", abort_code);
  909. return -EPROTO;
  910. }
  911. /*
  912. * clear the connection security
  913. */
  914. static void rxkad_clear(struct rxrpc_connection *conn)
  915. {
  916. _enter("");
  917. if (conn->cipher)
  918. crypto_free_blkcipher(conn->cipher);
  919. }
  920. /*
  921. * RxRPC Kerberos-based security
  922. */
  923. static struct rxrpc_security rxkad = {
  924. .owner = THIS_MODULE,
  925. .name = "rxkad",
  926. .security_index = RXKAD_VERSION,
  927. .init_connection_security = rxkad_init_connection_security,
  928. .prime_packet_security = rxkad_prime_packet_security,
  929. .secure_packet = rxkad_secure_packet,
  930. .verify_packet = rxkad_verify_packet,
  931. .issue_challenge = rxkad_issue_challenge,
  932. .respond_to_challenge = rxkad_respond_to_challenge,
  933. .verify_response = rxkad_verify_response,
  934. .clear = rxkad_clear,
  935. };
  936. static __init int rxkad_init(void)
  937. {
  938. _enter("");
  939. /* pin the cipher we need so that the crypto layer doesn't invoke
  940. * keventd to go get it */
  941. rxkad_ci = crypto_alloc_blkcipher("pcbc(fcrypt)", 0, CRYPTO_ALG_ASYNC);
  942. if (IS_ERR(rxkad_ci))
  943. return PTR_ERR(rxkad_ci);
  944. return rxrpc_register_security(&rxkad);
  945. }
  946. module_init(rxkad_init);
  947. static __exit void rxkad_exit(void)
  948. {
  949. _enter("");
  950. rxrpc_unregister_security(&rxkad);
  951. crypto_free_blkcipher(rxkad_ci);
  952. }
  953. module_exit(rxkad_exit);