rxkad.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244
  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. break;
  72. case RXRPC_SECURITY_ENCRYPT:
  73. conn->size_align = 8;
  74. conn->security_size = sizeof(struct rxkad_level2_hdr);
  75. break;
  76. default:
  77. ret = -EKEYREJECTED;
  78. goto error;
  79. }
  80. conn->cipher = ci;
  81. ret = 0;
  82. error:
  83. _leave(" = %d", ret);
  84. return ret;
  85. }
  86. /*
  87. * prime the encryption state with the invariant parts of a connection's
  88. * description
  89. */
  90. static int rxkad_prime_packet_security(struct rxrpc_connection *conn)
  91. {
  92. struct rxrpc_key_token *token;
  93. SKCIPHER_REQUEST_ON_STACK(req, conn->cipher);
  94. struct scatterlist sg;
  95. struct rxrpc_crypt iv;
  96. __be32 *tmpbuf;
  97. size_t tmpsize = 4 * sizeof(__be32);
  98. _enter("");
  99. if (!conn->params.key)
  100. return 0;
  101. tmpbuf = kmalloc(tmpsize, GFP_KERNEL);
  102. if (!tmpbuf)
  103. return -ENOMEM;
  104. token = conn->params.key->payload.data[0];
  105. memcpy(&iv, token->kad->session_key, sizeof(iv));
  106. tmpbuf[0] = htonl(conn->proto.epoch);
  107. tmpbuf[1] = htonl(conn->proto.cid);
  108. tmpbuf[2] = 0;
  109. tmpbuf[3] = htonl(conn->security_ix);
  110. sg_init_one(&sg, tmpbuf, tmpsize);
  111. skcipher_request_set_tfm(req, conn->cipher);
  112. skcipher_request_set_callback(req, 0, NULL, NULL);
  113. skcipher_request_set_crypt(req, &sg, &sg, tmpsize, iv.x);
  114. crypto_skcipher_encrypt(req);
  115. skcipher_request_zero(req);
  116. memcpy(&conn->csum_iv, tmpbuf + 2, sizeof(conn->csum_iv));
  117. kfree(tmpbuf);
  118. _leave(" = 0");
  119. return 0;
  120. }
  121. /*
  122. * partially encrypt a packet (level 1 security)
  123. */
  124. static int rxkad_secure_packet_auth(const struct rxrpc_call *call,
  125. struct sk_buff *skb,
  126. u32 data_size,
  127. void *sechdr)
  128. {
  129. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  130. SKCIPHER_REQUEST_ON_STACK(req, call->conn->cipher);
  131. struct rxkad_level1_hdr hdr;
  132. struct rxrpc_crypt iv;
  133. struct scatterlist sg;
  134. u16 check;
  135. _enter("");
  136. check = sp->hdr.seq ^ call->call_id;
  137. data_size |= (u32)check << 16;
  138. hdr.data_size = htonl(data_size);
  139. memcpy(sechdr, &hdr, sizeof(hdr));
  140. /* start the encryption afresh */
  141. memset(&iv, 0, sizeof(iv));
  142. sg_init_one(&sg, sechdr, 8);
  143. skcipher_request_set_tfm(req, call->conn->cipher);
  144. skcipher_request_set_callback(req, 0, NULL, NULL);
  145. skcipher_request_set_crypt(req, &sg, &sg, 8, iv.x);
  146. crypto_skcipher_encrypt(req);
  147. skcipher_request_zero(req);
  148. _leave(" = 0");
  149. return 0;
  150. }
  151. /*
  152. * wholly encrypt a packet (level 2 security)
  153. */
  154. static int rxkad_secure_packet_encrypt(const struct rxrpc_call *call,
  155. struct sk_buff *skb,
  156. u32 data_size,
  157. void *sechdr)
  158. {
  159. const struct rxrpc_key_token *token;
  160. struct rxkad_level2_hdr rxkhdr;
  161. struct rxrpc_skb_priv *sp;
  162. SKCIPHER_REQUEST_ON_STACK(req, call->conn->cipher);
  163. struct rxrpc_crypt iv;
  164. struct scatterlist sg[16];
  165. struct sk_buff *trailer;
  166. unsigned int len;
  167. u16 check;
  168. int nsg;
  169. int err;
  170. sp = rxrpc_skb(skb);
  171. _enter("");
  172. check = sp->hdr.seq ^ call->call_id;
  173. rxkhdr.data_size = htonl(data_size | (u32)check << 16);
  174. rxkhdr.checksum = 0;
  175. memcpy(sechdr, &rxkhdr, sizeof(rxkhdr));
  176. /* encrypt from the session key */
  177. token = call->conn->params.key->payload.data[0];
  178. memcpy(&iv, token->kad->session_key, sizeof(iv));
  179. sg_init_one(&sg[0], sechdr, sizeof(rxkhdr));
  180. skcipher_request_set_tfm(req, call->conn->cipher);
  181. skcipher_request_set_callback(req, 0, NULL, NULL);
  182. skcipher_request_set_crypt(req, &sg[0], &sg[0], sizeof(rxkhdr), iv.x);
  183. crypto_skcipher_encrypt(req);
  184. /* we want to encrypt the skbuff in-place */
  185. nsg = skb_cow_data(skb, 0, &trailer);
  186. err = -ENOMEM;
  187. if (nsg < 0 || nsg > 16)
  188. goto out;
  189. len = data_size + call->conn->size_align - 1;
  190. len &= ~(call->conn->size_align - 1);
  191. sg_init_table(sg, nsg);
  192. err = skb_to_sgvec(skb, sg, 0, len);
  193. if (unlikely(err < 0))
  194. goto out;
  195. skcipher_request_set_crypt(req, sg, sg, len, iv.x);
  196. crypto_skcipher_encrypt(req);
  197. _leave(" = 0");
  198. err = 0;
  199. out:
  200. skcipher_request_zero(req);
  201. return err;
  202. }
  203. /*
  204. * checksum an RxRPC packet header
  205. */
  206. static int rxkad_secure_packet(struct rxrpc_call *call,
  207. struct sk_buff *skb,
  208. size_t data_size,
  209. void *sechdr)
  210. {
  211. struct rxrpc_skb_priv *sp;
  212. SKCIPHER_REQUEST_ON_STACK(req, call->conn->cipher);
  213. struct rxrpc_crypt iv;
  214. struct scatterlist sg;
  215. u32 x, y;
  216. int ret;
  217. sp = rxrpc_skb(skb);
  218. _enter("{%d{%x}},{#%u},%zu,",
  219. call->debug_id, key_serial(call->conn->params.key),
  220. sp->hdr.seq, data_size);
  221. if (!call->conn->cipher)
  222. return 0;
  223. ret = key_validate(call->conn->params.key);
  224. if (ret < 0)
  225. return ret;
  226. /* continue encrypting from where we left off */
  227. memcpy(&iv, call->conn->csum_iv.x, sizeof(iv));
  228. /* calculate the security checksum */
  229. x = (call->cid & RXRPC_CHANNELMASK) << (32 - RXRPC_CIDSHIFT);
  230. x |= sp->hdr.seq & 0x3fffffff;
  231. call->crypto_buf[0] = htonl(call->call_id);
  232. call->crypto_buf[1] = htonl(x);
  233. sg_init_one(&sg, call->crypto_buf, 8);
  234. skcipher_request_set_tfm(req, call->conn->cipher);
  235. skcipher_request_set_callback(req, 0, NULL, NULL);
  236. skcipher_request_set_crypt(req, &sg, &sg, 8, iv.x);
  237. crypto_skcipher_encrypt(req);
  238. skcipher_request_zero(req);
  239. y = ntohl(call->crypto_buf[1]);
  240. y = (y >> 16) & 0xffff;
  241. if (y == 0)
  242. y = 1; /* zero checksums are not permitted */
  243. sp->hdr.cksum = y;
  244. switch (call->conn->params.security_level) {
  245. case RXRPC_SECURITY_PLAIN:
  246. ret = 0;
  247. break;
  248. case RXRPC_SECURITY_AUTH:
  249. ret = rxkad_secure_packet_auth(call, skb, data_size, sechdr);
  250. break;
  251. case RXRPC_SECURITY_ENCRYPT:
  252. ret = rxkad_secure_packet_encrypt(call, skb, data_size,
  253. sechdr);
  254. break;
  255. default:
  256. ret = -EPERM;
  257. break;
  258. }
  259. _leave(" = %d [set %hx]", ret, y);
  260. return ret;
  261. }
  262. /*
  263. * decrypt partial encryption on a packet (level 1 security)
  264. */
  265. static int rxkad_verify_packet_1(struct rxrpc_call *call, struct sk_buff *skb,
  266. unsigned int offset, unsigned int len,
  267. rxrpc_seq_t seq)
  268. {
  269. struct rxkad_level1_hdr sechdr;
  270. SKCIPHER_REQUEST_ON_STACK(req, call->conn->cipher);
  271. struct rxrpc_crypt iv;
  272. struct scatterlist sg[16];
  273. struct sk_buff *trailer;
  274. bool aborted;
  275. u32 data_size, buf;
  276. u16 check;
  277. int nsg, ret;
  278. _enter("");
  279. if (len < 8) {
  280. aborted = rxrpc_abort_eproto(call, skb, "rxkad_1_hdr", "V1H",
  281. RXKADSEALEDINCON);
  282. goto protocol_error;
  283. }
  284. /* Decrypt the skbuff in-place. TODO: We really want to decrypt
  285. * directly into the target buffer.
  286. */
  287. nsg = skb_cow_data(skb, 0, &trailer);
  288. if (nsg < 0 || nsg > 16)
  289. goto nomem;
  290. sg_init_table(sg, nsg);
  291. ret = skb_to_sgvec(skb, sg, offset, 8);
  292. if (unlikely(ret < 0))
  293. return ret;
  294. /* start the decryption afresh */
  295. memset(&iv, 0, sizeof(iv));
  296. skcipher_request_set_tfm(req, call->conn->cipher);
  297. skcipher_request_set_callback(req, 0, NULL, NULL);
  298. skcipher_request_set_crypt(req, sg, sg, 8, iv.x);
  299. crypto_skcipher_decrypt(req);
  300. skcipher_request_zero(req);
  301. /* Extract the decrypted packet length */
  302. if (skb_copy_bits(skb, offset, &sechdr, sizeof(sechdr)) < 0) {
  303. aborted = rxrpc_abort_eproto(call, skb, "rxkad_1_len", "XV1",
  304. RXKADDATALEN);
  305. goto protocol_error;
  306. }
  307. offset += sizeof(sechdr);
  308. len -= sizeof(sechdr);
  309. buf = ntohl(sechdr.data_size);
  310. data_size = buf & 0xffff;
  311. check = buf >> 16;
  312. check ^= seq ^ call->call_id;
  313. check &= 0xffff;
  314. if (check != 0) {
  315. aborted = rxrpc_abort_eproto(call, skb, "rxkad_1_check", "V1C",
  316. RXKADSEALEDINCON);
  317. goto protocol_error;
  318. }
  319. if (data_size > len) {
  320. aborted = rxrpc_abort_eproto(call, skb, "rxkad_1_datalen", "V1L",
  321. RXKADDATALEN);
  322. goto protocol_error;
  323. }
  324. _leave(" = 0 [dlen=%x]", data_size);
  325. return 0;
  326. protocol_error:
  327. if (aborted)
  328. rxrpc_send_abort_packet(call);
  329. return -EPROTO;
  330. nomem:
  331. _leave(" = -ENOMEM");
  332. return -ENOMEM;
  333. }
  334. /*
  335. * wholly decrypt a packet (level 2 security)
  336. */
  337. static int rxkad_verify_packet_2(struct rxrpc_call *call, struct sk_buff *skb,
  338. unsigned int offset, unsigned int len,
  339. rxrpc_seq_t seq)
  340. {
  341. const struct rxrpc_key_token *token;
  342. struct rxkad_level2_hdr sechdr;
  343. SKCIPHER_REQUEST_ON_STACK(req, call->conn->cipher);
  344. struct rxrpc_crypt iv;
  345. struct scatterlist _sg[4], *sg;
  346. struct sk_buff *trailer;
  347. bool aborted;
  348. u32 data_size, buf;
  349. u16 check;
  350. int nsg, ret;
  351. _enter(",{%d}", skb->len);
  352. if (len < 8) {
  353. aborted = rxrpc_abort_eproto(call, skb, "rxkad_2_hdr", "V2H",
  354. RXKADSEALEDINCON);
  355. goto protocol_error;
  356. }
  357. /* Decrypt the skbuff in-place. TODO: We really want to decrypt
  358. * directly into the target buffer.
  359. */
  360. nsg = skb_cow_data(skb, 0, &trailer);
  361. if (nsg < 0)
  362. goto nomem;
  363. sg = _sg;
  364. if (unlikely(nsg > 4)) {
  365. sg = kmalloc(sizeof(*sg) * nsg, GFP_NOIO);
  366. if (!sg)
  367. goto nomem;
  368. }
  369. sg_init_table(sg, nsg);
  370. ret = skb_to_sgvec(skb, sg, offset, len);
  371. if (unlikely(ret < 0)) {
  372. if (sg != _sg)
  373. kfree(sg);
  374. return ret;
  375. }
  376. /* decrypt from the session key */
  377. token = call->conn->params.key->payload.data[0];
  378. memcpy(&iv, token->kad->session_key, sizeof(iv));
  379. skcipher_request_set_tfm(req, call->conn->cipher);
  380. skcipher_request_set_callback(req, 0, NULL, NULL);
  381. skcipher_request_set_crypt(req, sg, sg, len, iv.x);
  382. crypto_skcipher_decrypt(req);
  383. skcipher_request_zero(req);
  384. if (sg != _sg)
  385. kfree(sg);
  386. /* Extract the decrypted packet length */
  387. if (skb_copy_bits(skb, offset, &sechdr, sizeof(sechdr)) < 0) {
  388. aborted = rxrpc_abort_eproto(call, skb, "rxkad_2_len", "XV2",
  389. RXKADDATALEN);
  390. goto protocol_error;
  391. }
  392. offset += sizeof(sechdr);
  393. len -= sizeof(sechdr);
  394. buf = ntohl(sechdr.data_size);
  395. data_size = buf & 0xffff;
  396. check = buf >> 16;
  397. check ^= seq ^ call->call_id;
  398. check &= 0xffff;
  399. if (check != 0) {
  400. aborted = rxrpc_abort_eproto(call, skb, "rxkad_2_check", "V2C",
  401. RXKADSEALEDINCON);
  402. goto protocol_error;
  403. }
  404. if (data_size > len) {
  405. aborted = rxrpc_abort_eproto(call, skb, "rxkad_2_datalen", "V2L",
  406. RXKADDATALEN);
  407. goto protocol_error;
  408. }
  409. _leave(" = 0 [dlen=%x]", data_size);
  410. return 0;
  411. protocol_error:
  412. if (aborted)
  413. rxrpc_send_abort_packet(call);
  414. return -EPROTO;
  415. nomem:
  416. _leave(" = -ENOMEM");
  417. return -ENOMEM;
  418. }
  419. /*
  420. * Verify the security on a received packet or subpacket (if part of a
  421. * jumbo packet).
  422. */
  423. static int rxkad_verify_packet(struct rxrpc_call *call, struct sk_buff *skb,
  424. unsigned int offset, unsigned int len,
  425. rxrpc_seq_t seq, u16 expected_cksum)
  426. {
  427. SKCIPHER_REQUEST_ON_STACK(req, call->conn->cipher);
  428. struct rxrpc_crypt iv;
  429. struct scatterlist sg;
  430. bool aborted;
  431. u16 cksum;
  432. u32 x, y;
  433. _enter("{%d{%x}},{#%u}",
  434. call->debug_id, key_serial(call->conn->params.key), seq);
  435. if (!call->conn->cipher)
  436. return 0;
  437. /* continue encrypting from where we left off */
  438. memcpy(&iv, call->conn->csum_iv.x, sizeof(iv));
  439. /* validate the security checksum */
  440. x = (call->cid & RXRPC_CHANNELMASK) << (32 - RXRPC_CIDSHIFT);
  441. x |= seq & 0x3fffffff;
  442. call->crypto_buf[0] = htonl(call->call_id);
  443. call->crypto_buf[1] = htonl(x);
  444. sg_init_one(&sg, call->crypto_buf, 8);
  445. skcipher_request_set_tfm(req, call->conn->cipher);
  446. skcipher_request_set_callback(req, 0, NULL, NULL);
  447. skcipher_request_set_crypt(req, &sg, &sg, 8, iv.x);
  448. crypto_skcipher_encrypt(req);
  449. skcipher_request_zero(req);
  450. y = ntohl(call->crypto_buf[1]);
  451. cksum = (y >> 16) & 0xffff;
  452. if (cksum == 0)
  453. cksum = 1; /* zero checksums are not permitted */
  454. if (cksum != expected_cksum) {
  455. aborted = rxrpc_abort_eproto(call, skb, "rxkad_csum", "VCK",
  456. RXKADSEALEDINCON);
  457. goto protocol_error;
  458. }
  459. switch (call->conn->params.security_level) {
  460. case RXRPC_SECURITY_PLAIN:
  461. return 0;
  462. case RXRPC_SECURITY_AUTH:
  463. return rxkad_verify_packet_1(call, skb, offset, len, seq);
  464. case RXRPC_SECURITY_ENCRYPT:
  465. return rxkad_verify_packet_2(call, skb, offset, len, seq);
  466. default:
  467. return -ENOANO;
  468. }
  469. protocol_error:
  470. if (aborted)
  471. rxrpc_send_abort_packet(call);
  472. return -EPROTO;
  473. }
  474. /*
  475. * Locate the data contained in a packet that was partially encrypted.
  476. */
  477. static void rxkad_locate_data_1(struct rxrpc_call *call, struct sk_buff *skb,
  478. unsigned int *_offset, unsigned int *_len)
  479. {
  480. struct rxkad_level1_hdr sechdr;
  481. if (skb_copy_bits(skb, *_offset, &sechdr, sizeof(sechdr)) < 0)
  482. BUG();
  483. *_offset += sizeof(sechdr);
  484. *_len = ntohl(sechdr.data_size) & 0xffff;
  485. }
  486. /*
  487. * Locate the data contained in a packet that was completely encrypted.
  488. */
  489. static void rxkad_locate_data_2(struct rxrpc_call *call, struct sk_buff *skb,
  490. unsigned int *_offset, unsigned int *_len)
  491. {
  492. struct rxkad_level2_hdr sechdr;
  493. if (skb_copy_bits(skb, *_offset, &sechdr, sizeof(sechdr)) < 0)
  494. BUG();
  495. *_offset += sizeof(sechdr);
  496. *_len = ntohl(sechdr.data_size) & 0xffff;
  497. }
  498. /*
  499. * Locate the data contained in an already decrypted packet.
  500. */
  501. static void rxkad_locate_data(struct rxrpc_call *call, struct sk_buff *skb,
  502. unsigned int *_offset, unsigned int *_len)
  503. {
  504. switch (call->conn->params.security_level) {
  505. case RXRPC_SECURITY_AUTH:
  506. rxkad_locate_data_1(call, skb, _offset, _len);
  507. return;
  508. case RXRPC_SECURITY_ENCRYPT:
  509. rxkad_locate_data_2(call, skb, _offset, _len);
  510. return;
  511. default:
  512. return;
  513. }
  514. }
  515. /*
  516. * issue a challenge
  517. */
  518. static int rxkad_issue_challenge(struct rxrpc_connection *conn)
  519. {
  520. struct rxkad_challenge challenge;
  521. struct rxrpc_wire_header whdr;
  522. struct msghdr msg;
  523. struct kvec iov[2];
  524. size_t len;
  525. u32 serial;
  526. int ret;
  527. _enter("{%d,%x}", conn->debug_id, key_serial(conn->params.key));
  528. ret = key_validate(conn->params.key);
  529. if (ret < 0)
  530. return ret;
  531. get_random_bytes(&conn->security_nonce, sizeof(conn->security_nonce));
  532. challenge.version = htonl(2);
  533. challenge.nonce = htonl(conn->security_nonce);
  534. challenge.min_level = htonl(0);
  535. challenge.__padding = 0;
  536. msg.msg_name = &conn->params.peer->srx.transport.sin;
  537. msg.msg_namelen = sizeof(conn->params.peer->srx.transport.sin);
  538. msg.msg_control = NULL;
  539. msg.msg_controllen = 0;
  540. msg.msg_flags = 0;
  541. whdr.epoch = htonl(conn->proto.epoch);
  542. whdr.cid = htonl(conn->proto.cid);
  543. whdr.callNumber = 0;
  544. whdr.seq = 0;
  545. whdr.type = RXRPC_PACKET_TYPE_CHALLENGE;
  546. whdr.flags = conn->out_clientflag;
  547. whdr.userStatus = 0;
  548. whdr.securityIndex = conn->security_ix;
  549. whdr._rsvd = 0;
  550. whdr.serviceId = htons(conn->service_id);
  551. iov[0].iov_base = &whdr;
  552. iov[0].iov_len = sizeof(whdr);
  553. iov[1].iov_base = &challenge;
  554. iov[1].iov_len = sizeof(challenge);
  555. len = iov[0].iov_len + iov[1].iov_len;
  556. serial = atomic_inc_return(&conn->serial);
  557. whdr.serial = htonl(serial);
  558. _proto("Tx CHALLENGE %%%u", serial);
  559. ret = kernel_sendmsg(conn->params.local->socket, &msg, iov, 2, len);
  560. if (ret < 0) {
  561. _debug("sendmsg failed: %d", ret);
  562. return -EAGAIN;
  563. }
  564. _leave(" = 0");
  565. return 0;
  566. }
  567. /*
  568. * send a Kerberos security response
  569. */
  570. static int rxkad_send_response(struct rxrpc_connection *conn,
  571. struct rxrpc_host_header *hdr,
  572. struct rxkad_response *resp,
  573. const struct rxkad_key *s2)
  574. {
  575. struct rxrpc_wire_header whdr;
  576. struct msghdr msg;
  577. struct kvec iov[3];
  578. size_t len;
  579. u32 serial;
  580. int ret;
  581. _enter("");
  582. msg.msg_name = &conn->params.peer->srx.transport.sin;
  583. msg.msg_namelen = sizeof(conn->params.peer->srx.transport.sin);
  584. msg.msg_control = NULL;
  585. msg.msg_controllen = 0;
  586. msg.msg_flags = 0;
  587. memset(&whdr, 0, sizeof(whdr));
  588. whdr.epoch = htonl(hdr->epoch);
  589. whdr.cid = htonl(hdr->cid);
  590. whdr.type = RXRPC_PACKET_TYPE_RESPONSE;
  591. whdr.flags = conn->out_clientflag;
  592. whdr.securityIndex = hdr->securityIndex;
  593. whdr.serviceId = htons(hdr->serviceId);
  594. iov[0].iov_base = &whdr;
  595. iov[0].iov_len = sizeof(whdr);
  596. iov[1].iov_base = resp;
  597. iov[1].iov_len = sizeof(*resp);
  598. iov[2].iov_base = (void *)s2->ticket;
  599. iov[2].iov_len = s2->ticket_len;
  600. len = iov[0].iov_len + iov[1].iov_len + iov[2].iov_len;
  601. serial = atomic_inc_return(&conn->serial);
  602. whdr.serial = htonl(serial);
  603. _proto("Tx RESPONSE %%%u", serial);
  604. ret = kernel_sendmsg(conn->params.local->socket, &msg, iov, 3, len);
  605. if (ret < 0) {
  606. _debug("sendmsg failed: %d", ret);
  607. return -EAGAIN;
  608. }
  609. _leave(" = 0");
  610. return 0;
  611. }
  612. /*
  613. * calculate the response checksum
  614. */
  615. static void rxkad_calc_response_checksum(struct rxkad_response *response)
  616. {
  617. u32 csum = 1000003;
  618. int loop;
  619. u8 *p = (u8 *) response;
  620. for (loop = sizeof(*response); loop > 0; loop--)
  621. csum = csum * 0x10204081 + *p++;
  622. response->encrypted.checksum = htonl(csum);
  623. }
  624. /*
  625. * encrypt the response packet
  626. */
  627. static void rxkad_encrypt_response(struct rxrpc_connection *conn,
  628. struct rxkad_response *resp,
  629. const struct rxkad_key *s2)
  630. {
  631. SKCIPHER_REQUEST_ON_STACK(req, conn->cipher);
  632. struct rxrpc_crypt iv;
  633. struct scatterlist sg[1];
  634. /* continue encrypting from where we left off */
  635. memcpy(&iv, s2->session_key, sizeof(iv));
  636. sg_init_table(sg, 1);
  637. sg_set_buf(sg, &resp->encrypted, sizeof(resp->encrypted));
  638. skcipher_request_set_tfm(req, conn->cipher);
  639. skcipher_request_set_callback(req, 0, NULL, NULL);
  640. skcipher_request_set_crypt(req, sg, sg, sizeof(resp->encrypted), iv.x);
  641. crypto_skcipher_encrypt(req);
  642. skcipher_request_zero(req);
  643. }
  644. /*
  645. * respond to a challenge packet
  646. */
  647. static int rxkad_respond_to_challenge(struct rxrpc_connection *conn,
  648. struct sk_buff *skb,
  649. u32 *_abort_code)
  650. {
  651. const struct rxrpc_key_token *token;
  652. struct rxkad_challenge challenge;
  653. struct rxkad_response resp
  654. __attribute__((aligned(8))); /* must be aligned for crypto */
  655. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  656. const char *eproto;
  657. u32 version, nonce, min_level, abort_code;
  658. int ret;
  659. _enter("{%d,%x}", conn->debug_id, key_serial(conn->params.key));
  660. eproto = tracepoint_string("chall_no_key");
  661. abort_code = RX_PROTOCOL_ERROR;
  662. if (!conn->params.key)
  663. goto protocol_error;
  664. abort_code = RXKADEXPIRED;
  665. ret = key_validate(conn->params.key);
  666. if (ret < 0)
  667. goto other_error;
  668. eproto = tracepoint_string("chall_short");
  669. abort_code = RXKADPACKETSHORT;
  670. if (skb_copy_bits(skb, sizeof(struct rxrpc_wire_header),
  671. &challenge, sizeof(challenge)) < 0)
  672. goto protocol_error;
  673. version = ntohl(challenge.version);
  674. nonce = ntohl(challenge.nonce);
  675. min_level = ntohl(challenge.min_level);
  676. _proto("Rx CHALLENGE %%%u { v=%u n=%u ml=%u }",
  677. sp->hdr.serial, version, nonce, min_level);
  678. eproto = tracepoint_string("chall_ver");
  679. abort_code = RXKADINCONSISTENCY;
  680. if (version != RXKAD_VERSION)
  681. goto protocol_error;
  682. abort_code = RXKADLEVELFAIL;
  683. ret = -EACCES;
  684. if (conn->params.security_level < min_level)
  685. goto other_error;
  686. token = conn->params.key->payload.data[0];
  687. /* build the response packet */
  688. memset(&resp, 0, sizeof(resp));
  689. resp.version = htonl(RXKAD_VERSION);
  690. resp.encrypted.epoch = htonl(conn->proto.epoch);
  691. resp.encrypted.cid = htonl(conn->proto.cid);
  692. resp.encrypted.securityIndex = htonl(conn->security_ix);
  693. resp.encrypted.inc_nonce = htonl(nonce + 1);
  694. resp.encrypted.level = htonl(conn->params.security_level);
  695. resp.kvno = htonl(token->kad->kvno);
  696. resp.ticket_len = htonl(token->kad->ticket_len);
  697. resp.encrypted.call_id[0] = htonl(conn->channels[0].call_counter);
  698. resp.encrypted.call_id[1] = htonl(conn->channels[1].call_counter);
  699. resp.encrypted.call_id[2] = htonl(conn->channels[2].call_counter);
  700. resp.encrypted.call_id[3] = htonl(conn->channels[3].call_counter);
  701. /* calculate the response checksum and then do the encryption */
  702. rxkad_calc_response_checksum(&resp);
  703. rxkad_encrypt_response(conn, &resp, token->kad);
  704. return rxkad_send_response(conn, &sp->hdr, &resp, token->kad);
  705. protocol_error:
  706. trace_rxrpc_rx_eproto(NULL, sp->hdr.serial, eproto);
  707. ret = -EPROTO;
  708. other_error:
  709. *_abort_code = abort_code;
  710. return ret;
  711. }
  712. /*
  713. * decrypt the kerberos IV ticket in the response
  714. */
  715. static int rxkad_decrypt_ticket(struct rxrpc_connection *conn,
  716. struct sk_buff *skb,
  717. void *ticket, size_t ticket_len,
  718. struct rxrpc_crypt *_session_key,
  719. time_t *_expiry,
  720. u32 *_abort_code)
  721. {
  722. struct skcipher_request *req;
  723. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  724. struct rxrpc_crypt iv, key;
  725. struct scatterlist sg[1];
  726. struct in_addr addr;
  727. unsigned int life;
  728. const char *eproto;
  729. time_t issue, now;
  730. bool little_endian;
  731. int ret;
  732. u32 abort_code;
  733. u8 *p, *q, *name, *end;
  734. _enter("{%d},{%x}", conn->debug_id, key_serial(conn->server_key));
  735. *_expiry = 0;
  736. ret = key_validate(conn->server_key);
  737. if (ret < 0) {
  738. switch (ret) {
  739. case -EKEYEXPIRED:
  740. abort_code = RXKADEXPIRED;
  741. goto other_error;
  742. default:
  743. abort_code = RXKADNOAUTH;
  744. goto other_error;
  745. }
  746. }
  747. ASSERT(conn->server_key->payload.data[0] != NULL);
  748. ASSERTCMP((unsigned long) ticket & 7UL, ==, 0);
  749. memcpy(&iv, &conn->server_key->payload.data[2], sizeof(iv));
  750. ret = -ENOMEM;
  751. req = skcipher_request_alloc(conn->server_key->payload.data[0],
  752. GFP_NOFS);
  753. if (!req)
  754. goto temporary_error;
  755. sg_init_one(&sg[0], ticket, ticket_len);
  756. skcipher_request_set_callback(req, 0, NULL, NULL);
  757. skcipher_request_set_crypt(req, sg, sg, ticket_len, iv.x);
  758. crypto_skcipher_decrypt(req);
  759. skcipher_request_free(req);
  760. p = ticket;
  761. end = p + ticket_len;
  762. #define Z(field) \
  763. ({ \
  764. u8 *__str = p; \
  765. eproto = tracepoint_string("rxkad_bad_"#field); \
  766. q = memchr(p, 0, end - p); \
  767. if (!q || q - p > (field##_SZ)) \
  768. goto bad_ticket; \
  769. for (; p < q; p++) \
  770. if (!isprint(*p)) \
  771. goto bad_ticket; \
  772. p++; \
  773. __str; \
  774. })
  775. /* extract the ticket flags */
  776. _debug("KIV FLAGS: %x", *p);
  777. little_endian = *p & 1;
  778. p++;
  779. /* extract the authentication name */
  780. name = Z(ANAME);
  781. _debug("KIV ANAME: %s", name);
  782. /* extract the principal's instance */
  783. name = Z(INST);
  784. _debug("KIV INST : %s", name);
  785. /* extract the principal's authentication domain */
  786. name = Z(REALM);
  787. _debug("KIV REALM: %s", name);
  788. eproto = tracepoint_string("rxkad_bad_len");
  789. if (end - p < 4 + 8 + 4 + 2)
  790. goto bad_ticket;
  791. /* get the IPv4 address of the entity that requested the ticket */
  792. memcpy(&addr, p, sizeof(addr));
  793. p += 4;
  794. _debug("KIV ADDR : %pI4", &addr);
  795. /* get the session key from the ticket */
  796. memcpy(&key, p, sizeof(key));
  797. p += 8;
  798. _debug("KIV KEY : %08x %08x", ntohl(key.n[0]), ntohl(key.n[1]));
  799. memcpy(_session_key, &key, sizeof(key));
  800. /* get the ticket's lifetime */
  801. life = *p++ * 5 * 60;
  802. _debug("KIV LIFE : %u", life);
  803. /* get the issue time of the ticket */
  804. if (little_endian) {
  805. __le32 stamp;
  806. memcpy(&stamp, p, 4);
  807. issue = le32_to_cpu(stamp);
  808. } else {
  809. __be32 stamp;
  810. memcpy(&stamp, p, 4);
  811. issue = be32_to_cpu(stamp);
  812. }
  813. p += 4;
  814. now = get_seconds();
  815. _debug("KIV ISSUE: %lx [%lx]", issue, now);
  816. /* check the ticket is in date */
  817. if (issue > now) {
  818. abort_code = RXKADNOAUTH;
  819. ret = -EKEYREJECTED;
  820. goto other_error;
  821. }
  822. if (issue < now - life) {
  823. abort_code = RXKADEXPIRED;
  824. ret = -EKEYEXPIRED;
  825. goto other_error;
  826. }
  827. *_expiry = issue + life;
  828. /* get the service name */
  829. name = Z(SNAME);
  830. _debug("KIV SNAME: %s", name);
  831. /* get the service instance name */
  832. name = Z(INST);
  833. _debug("KIV SINST: %s", name);
  834. return 0;
  835. bad_ticket:
  836. trace_rxrpc_rx_eproto(NULL, sp->hdr.serial, eproto);
  837. abort_code = RXKADBADTICKET;
  838. ret = -EPROTO;
  839. other_error:
  840. *_abort_code = abort_code;
  841. return ret;
  842. temporary_error:
  843. return ret;
  844. }
  845. /*
  846. * decrypt the response packet
  847. */
  848. static void rxkad_decrypt_response(struct rxrpc_connection *conn,
  849. struct rxkad_response *resp,
  850. const struct rxrpc_crypt *session_key)
  851. {
  852. SKCIPHER_REQUEST_ON_STACK(req, rxkad_ci);
  853. struct scatterlist sg[1];
  854. struct rxrpc_crypt iv;
  855. _enter(",,%08x%08x",
  856. ntohl(session_key->n[0]), ntohl(session_key->n[1]));
  857. ASSERT(rxkad_ci != NULL);
  858. mutex_lock(&rxkad_ci_mutex);
  859. if (crypto_skcipher_setkey(rxkad_ci, session_key->x,
  860. sizeof(*session_key)) < 0)
  861. BUG();
  862. memcpy(&iv, session_key, sizeof(iv));
  863. sg_init_table(sg, 1);
  864. sg_set_buf(sg, &resp->encrypted, sizeof(resp->encrypted));
  865. skcipher_request_set_tfm(req, rxkad_ci);
  866. skcipher_request_set_callback(req, 0, NULL, NULL);
  867. skcipher_request_set_crypt(req, sg, sg, sizeof(resp->encrypted), iv.x);
  868. crypto_skcipher_decrypt(req);
  869. skcipher_request_zero(req);
  870. mutex_unlock(&rxkad_ci_mutex);
  871. _leave("");
  872. }
  873. /*
  874. * verify a response
  875. */
  876. static int rxkad_verify_response(struct rxrpc_connection *conn,
  877. struct sk_buff *skb,
  878. u32 *_abort_code)
  879. {
  880. struct rxkad_response response
  881. __attribute__((aligned(8))); /* must be aligned for crypto */
  882. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  883. struct rxrpc_crypt session_key;
  884. const char *eproto;
  885. time_t expiry;
  886. void *ticket;
  887. u32 abort_code, version, kvno, ticket_len, level;
  888. __be32 csum;
  889. int ret, i;
  890. _enter("{%d,%x}", conn->debug_id, key_serial(conn->server_key));
  891. eproto = tracepoint_string("rxkad_rsp_short");
  892. abort_code = RXKADPACKETSHORT;
  893. if (skb_copy_bits(skb, sizeof(struct rxrpc_wire_header),
  894. &response, sizeof(response)) < 0)
  895. goto protocol_error;
  896. if (!pskb_pull(skb, sizeof(response)))
  897. BUG();
  898. version = ntohl(response.version);
  899. ticket_len = ntohl(response.ticket_len);
  900. kvno = ntohl(response.kvno);
  901. _proto("Rx RESPONSE %%%u { v=%u kv=%u tl=%u }",
  902. sp->hdr.serial, version, kvno, ticket_len);
  903. eproto = tracepoint_string("rxkad_rsp_ver");
  904. abort_code = RXKADINCONSISTENCY;
  905. if (version != RXKAD_VERSION)
  906. goto protocol_error;
  907. eproto = tracepoint_string("rxkad_rsp_tktlen");
  908. abort_code = RXKADTICKETLEN;
  909. if (ticket_len < 4 || ticket_len > MAXKRB5TICKETLEN)
  910. goto protocol_error;
  911. eproto = tracepoint_string("rxkad_rsp_unkkey");
  912. abort_code = RXKADUNKNOWNKEY;
  913. if (kvno >= RXKAD_TKT_TYPE_KERBEROS_V5)
  914. goto protocol_error;
  915. /* extract the kerberos ticket and decrypt and decode it */
  916. ret = -ENOMEM;
  917. ticket = kmalloc(ticket_len, GFP_NOFS);
  918. if (!ticket)
  919. goto temporary_error;
  920. eproto = tracepoint_string("rxkad_tkt_short");
  921. abort_code = RXKADPACKETSHORT;
  922. if (skb_copy_bits(skb, sizeof(struct rxrpc_wire_header),
  923. ticket, ticket_len) < 0)
  924. goto protocol_error_free;
  925. ret = rxkad_decrypt_ticket(conn, skb, ticket, ticket_len, &session_key,
  926. &expiry, _abort_code);
  927. if (ret < 0)
  928. goto temporary_error_free;
  929. /* use the session key from inside the ticket to decrypt the
  930. * response */
  931. rxkad_decrypt_response(conn, &response, &session_key);
  932. eproto = tracepoint_string("rxkad_rsp_param");
  933. abort_code = RXKADSEALEDINCON;
  934. if (ntohl(response.encrypted.epoch) != conn->proto.epoch)
  935. goto protocol_error_free;
  936. if (ntohl(response.encrypted.cid) != conn->proto.cid)
  937. goto protocol_error_free;
  938. if (ntohl(response.encrypted.securityIndex) != conn->security_ix)
  939. goto protocol_error_free;
  940. csum = response.encrypted.checksum;
  941. response.encrypted.checksum = 0;
  942. rxkad_calc_response_checksum(&response);
  943. eproto = tracepoint_string("rxkad_rsp_csum");
  944. if (response.encrypted.checksum != csum)
  945. goto protocol_error_free;
  946. spin_lock(&conn->channel_lock);
  947. for (i = 0; i < RXRPC_MAXCALLS; i++) {
  948. struct rxrpc_call *call;
  949. u32 call_id = ntohl(response.encrypted.call_id[i]);
  950. eproto = tracepoint_string("rxkad_rsp_callid");
  951. if (call_id > INT_MAX)
  952. goto protocol_error_unlock;
  953. eproto = tracepoint_string("rxkad_rsp_callctr");
  954. if (call_id < conn->channels[i].call_counter)
  955. goto protocol_error_unlock;
  956. eproto = tracepoint_string("rxkad_rsp_callst");
  957. if (call_id > conn->channels[i].call_counter) {
  958. call = rcu_dereference_protected(
  959. conn->channels[i].call,
  960. lockdep_is_held(&conn->channel_lock));
  961. if (call && call->state < RXRPC_CALL_COMPLETE)
  962. goto protocol_error_unlock;
  963. conn->channels[i].call_counter = call_id;
  964. }
  965. }
  966. spin_unlock(&conn->channel_lock);
  967. eproto = tracepoint_string("rxkad_rsp_seq");
  968. abort_code = RXKADOUTOFSEQUENCE;
  969. if (ntohl(response.encrypted.inc_nonce) != conn->security_nonce + 1)
  970. goto protocol_error_free;
  971. eproto = tracepoint_string("rxkad_rsp_level");
  972. abort_code = RXKADLEVELFAIL;
  973. level = ntohl(response.encrypted.level);
  974. if (level > RXRPC_SECURITY_ENCRYPT)
  975. goto protocol_error_free;
  976. conn->params.security_level = level;
  977. /* create a key to hold the security data and expiration time - after
  978. * this the connection security can be handled in exactly the same way
  979. * as for a client connection */
  980. ret = rxrpc_get_server_data_key(conn, &session_key, expiry, kvno);
  981. if (ret < 0)
  982. goto temporary_error_free;
  983. kfree(ticket);
  984. _leave(" = 0");
  985. return 0;
  986. protocol_error_unlock:
  987. spin_unlock(&conn->channel_lock);
  988. protocol_error_free:
  989. kfree(ticket);
  990. protocol_error:
  991. trace_rxrpc_rx_eproto(NULL, sp->hdr.serial, eproto);
  992. *_abort_code = abort_code;
  993. return -EPROTO;
  994. temporary_error_free:
  995. kfree(ticket);
  996. temporary_error:
  997. /* Ignore the response packet if we got a temporary error such as
  998. * ENOMEM. We just want to send the challenge again. Note that we
  999. * also come out this way if the ticket decryption fails.
  1000. */
  1001. return ret;
  1002. }
  1003. /*
  1004. * clear the connection security
  1005. */
  1006. static void rxkad_clear(struct rxrpc_connection *conn)
  1007. {
  1008. _enter("");
  1009. if (conn->cipher)
  1010. crypto_free_skcipher(conn->cipher);
  1011. }
  1012. /*
  1013. * Initialise the rxkad security service.
  1014. */
  1015. static int rxkad_init(void)
  1016. {
  1017. /* pin the cipher we need so that the crypto layer doesn't invoke
  1018. * keventd to go get it */
  1019. rxkad_ci = crypto_alloc_skcipher("pcbc(fcrypt)", 0, CRYPTO_ALG_ASYNC);
  1020. return PTR_ERR_OR_ZERO(rxkad_ci);
  1021. }
  1022. /*
  1023. * Clean up the rxkad security service.
  1024. */
  1025. static void rxkad_exit(void)
  1026. {
  1027. if (rxkad_ci)
  1028. crypto_free_skcipher(rxkad_ci);
  1029. }
  1030. /*
  1031. * RxRPC Kerberos-based security
  1032. */
  1033. const struct rxrpc_security rxkad = {
  1034. .name = "rxkad",
  1035. .security_index = RXRPC_SECURITY_RXKAD,
  1036. .init = rxkad_init,
  1037. .exit = rxkad_exit,
  1038. .init_connection_security = rxkad_init_connection_security,
  1039. .prime_packet_security = rxkad_prime_packet_security,
  1040. .secure_packet = rxkad_secure_packet,
  1041. .verify_packet = rxkad_verify_packet,
  1042. .locate_data = rxkad_locate_data,
  1043. .issue_challenge = rxkad_issue_challenge,
  1044. .respond_to_challenge = rxkad_respond_to_challenge,
  1045. .verify_response = rxkad_verify_response,
  1046. .clear = rxkad_clear,
  1047. };