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