rxkad.c 27 KB

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