rxkad.c 27 KB

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