ar-key.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219
  1. /* RxRPC key management
  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. * RxRPC keys should have a description of describing their purpose:
  12. * "afs@CAMBRIDGE.REDHAT.COM>
  13. */
  14. #include <linux/module.h>
  15. #include <linux/net.h>
  16. #include <linux/skbuff.h>
  17. #include <linux/key-type.h>
  18. #include <linux/crypto.h>
  19. #include <linux/ctype.h>
  20. #include <linux/slab.h>
  21. #include <net/sock.h>
  22. #include <net/af_rxrpc.h>
  23. #include <keys/rxrpc-type.h>
  24. #include <keys/user-type.h>
  25. #include "ar-internal.h"
  26. static int rxrpc_vet_description_s(const char *);
  27. static int rxrpc_instantiate(struct key *, const void *, size_t);
  28. static int rxrpc_instantiate_s(struct key *, const void *, size_t);
  29. static void rxrpc_destroy(struct key *);
  30. static void rxrpc_destroy_s(struct key *);
  31. static void rxrpc_describe(const struct key *, struct seq_file *);
  32. static long rxrpc_read(const struct key *, char __user *, size_t);
  33. /*
  34. * rxrpc defined keys take an arbitrary string as the description and an
  35. * arbitrary blob of data as the payload
  36. */
  37. struct key_type key_type_rxrpc = {
  38. .name = "rxrpc",
  39. .instantiate = rxrpc_instantiate,
  40. .match = user_match,
  41. .destroy = rxrpc_destroy,
  42. .describe = rxrpc_describe,
  43. .read = rxrpc_read,
  44. };
  45. EXPORT_SYMBOL(key_type_rxrpc);
  46. /*
  47. * rxrpc server defined keys take "<serviceId>:<securityIndex>" as the
  48. * description and an 8-byte decryption key as the payload
  49. */
  50. struct key_type key_type_rxrpc_s = {
  51. .name = "rxrpc_s",
  52. .vet_description = rxrpc_vet_description_s,
  53. .instantiate = rxrpc_instantiate_s,
  54. .match = user_match,
  55. .destroy = rxrpc_destroy_s,
  56. .describe = rxrpc_describe,
  57. };
  58. /*
  59. * Vet the description for an RxRPC server key
  60. */
  61. static int rxrpc_vet_description_s(const char *desc)
  62. {
  63. unsigned long num;
  64. char *p;
  65. num = simple_strtoul(desc, &p, 10);
  66. if (*p != ':' || num > 65535)
  67. return -EINVAL;
  68. num = simple_strtoul(p + 1, &p, 10);
  69. if (*p || num < 1 || num > 255)
  70. return -EINVAL;
  71. return 0;
  72. }
  73. /*
  74. * parse an RxKAD type XDR format token
  75. * - the caller guarantees we have at least 4 words
  76. */
  77. static int rxrpc_instantiate_xdr_rxkad(struct key *key, const __be32 *xdr,
  78. unsigned int toklen)
  79. {
  80. struct rxrpc_key_token *token, **pptoken;
  81. size_t plen;
  82. u32 tktlen;
  83. int ret;
  84. _enter(",{%x,%x,%x,%x},%u",
  85. ntohl(xdr[0]), ntohl(xdr[1]), ntohl(xdr[2]), ntohl(xdr[3]),
  86. toklen);
  87. if (toklen <= 8 * 4)
  88. return -EKEYREJECTED;
  89. tktlen = ntohl(xdr[7]);
  90. _debug("tktlen: %x", tktlen);
  91. if (tktlen > AFSTOKEN_RK_TIX_MAX)
  92. return -EKEYREJECTED;
  93. if (8 * 4 + tktlen != toklen)
  94. return -EKEYREJECTED;
  95. plen = sizeof(*token) + sizeof(*token->kad) + tktlen;
  96. ret = key_payload_reserve(key, key->datalen + plen);
  97. if (ret < 0)
  98. return ret;
  99. plen -= sizeof(*token);
  100. token = kzalloc(sizeof(*token), GFP_KERNEL);
  101. if (!token)
  102. return -ENOMEM;
  103. token->kad = kzalloc(plen, GFP_KERNEL);
  104. if (!token->kad) {
  105. kfree(token);
  106. return -ENOMEM;
  107. }
  108. token->security_index = RXRPC_SECURITY_RXKAD;
  109. token->kad->ticket_len = tktlen;
  110. token->kad->vice_id = ntohl(xdr[0]);
  111. token->kad->kvno = ntohl(xdr[1]);
  112. token->kad->start = ntohl(xdr[4]);
  113. token->kad->expiry = ntohl(xdr[5]);
  114. token->kad->primary_flag = ntohl(xdr[6]);
  115. memcpy(&token->kad->session_key, &xdr[2], 8);
  116. memcpy(&token->kad->ticket, &xdr[8], tktlen);
  117. _debug("SCIX: %u", token->security_index);
  118. _debug("TLEN: %u", token->kad->ticket_len);
  119. _debug("EXPY: %x", token->kad->expiry);
  120. _debug("KVNO: %u", token->kad->kvno);
  121. _debug("PRIM: %u", token->kad->primary_flag);
  122. _debug("SKEY: %02x%02x%02x%02x%02x%02x%02x%02x",
  123. token->kad->session_key[0], token->kad->session_key[1],
  124. token->kad->session_key[2], token->kad->session_key[3],
  125. token->kad->session_key[4], token->kad->session_key[5],
  126. token->kad->session_key[6], token->kad->session_key[7]);
  127. if (token->kad->ticket_len >= 8)
  128. _debug("TCKT: %02x%02x%02x%02x%02x%02x%02x%02x",
  129. token->kad->ticket[0], token->kad->ticket[1],
  130. token->kad->ticket[2], token->kad->ticket[3],
  131. token->kad->ticket[4], token->kad->ticket[5],
  132. token->kad->ticket[6], token->kad->ticket[7]);
  133. /* count the number of tokens attached */
  134. key->type_data.x[0]++;
  135. /* attach the data */
  136. for (pptoken = (struct rxrpc_key_token **)&key->payload.data;
  137. *pptoken;
  138. pptoken = &(*pptoken)->next)
  139. continue;
  140. *pptoken = token;
  141. if (token->kad->expiry < key->expiry)
  142. key->expiry = token->kad->expiry;
  143. _leave(" = 0");
  144. return 0;
  145. }
  146. static void rxrpc_free_krb5_principal(struct krb5_principal *princ)
  147. {
  148. int loop;
  149. if (princ->name_parts) {
  150. for (loop = princ->n_name_parts - 1; loop >= 0; loop--)
  151. kfree(princ->name_parts[loop]);
  152. kfree(princ->name_parts);
  153. }
  154. kfree(princ->realm);
  155. }
  156. static void rxrpc_free_krb5_tagged(struct krb5_tagged_data *td)
  157. {
  158. kfree(td->data);
  159. }
  160. /*
  161. * free up an RxK5 token
  162. */
  163. static void rxrpc_rxk5_free(struct rxk5_key *rxk5)
  164. {
  165. int loop;
  166. rxrpc_free_krb5_principal(&rxk5->client);
  167. rxrpc_free_krb5_principal(&rxk5->server);
  168. rxrpc_free_krb5_tagged(&rxk5->session);
  169. if (rxk5->addresses) {
  170. for (loop = rxk5->n_addresses - 1; loop >= 0; loop--)
  171. rxrpc_free_krb5_tagged(&rxk5->addresses[loop]);
  172. kfree(rxk5->addresses);
  173. }
  174. if (rxk5->authdata) {
  175. for (loop = rxk5->n_authdata - 1; loop >= 0; loop--)
  176. rxrpc_free_krb5_tagged(&rxk5->authdata[loop]);
  177. kfree(rxk5->authdata);
  178. }
  179. kfree(rxk5->ticket);
  180. kfree(rxk5->ticket2);
  181. kfree(rxk5);
  182. }
  183. /*
  184. * extract a krb5 principal
  185. */
  186. static int rxrpc_krb5_decode_principal(struct krb5_principal *princ,
  187. const __be32 **_xdr,
  188. unsigned int *_toklen)
  189. {
  190. const __be32 *xdr = *_xdr;
  191. unsigned int toklen = *_toklen, n_parts, loop, tmp;
  192. /* there must be at least one name, and at least #names+1 length
  193. * words */
  194. if (toklen <= 12)
  195. return -EINVAL;
  196. _enter(",{%x,%x,%x},%u",
  197. ntohl(xdr[0]), ntohl(xdr[1]), ntohl(xdr[2]), toklen);
  198. n_parts = ntohl(*xdr++);
  199. toklen -= 4;
  200. if (n_parts <= 0 || n_parts > AFSTOKEN_K5_COMPONENTS_MAX)
  201. return -EINVAL;
  202. princ->n_name_parts = n_parts;
  203. if (toklen <= (n_parts + 1) * 4)
  204. return -EINVAL;
  205. princ->name_parts = kcalloc(n_parts, sizeof(char *), GFP_KERNEL);
  206. if (!princ->name_parts)
  207. return -ENOMEM;
  208. for (loop = 0; loop < n_parts; loop++) {
  209. if (toklen < 4)
  210. return -EINVAL;
  211. tmp = ntohl(*xdr++);
  212. toklen -= 4;
  213. if (tmp <= 0 || tmp > AFSTOKEN_STRING_MAX)
  214. return -EINVAL;
  215. if (tmp > toklen)
  216. return -EINVAL;
  217. princ->name_parts[loop] = kmalloc(tmp + 1, GFP_KERNEL);
  218. if (!princ->name_parts[loop])
  219. return -ENOMEM;
  220. memcpy(princ->name_parts[loop], xdr, tmp);
  221. princ->name_parts[loop][tmp] = 0;
  222. tmp = (tmp + 3) & ~3;
  223. toklen -= tmp;
  224. xdr += tmp >> 2;
  225. }
  226. if (toklen < 4)
  227. return -EINVAL;
  228. tmp = ntohl(*xdr++);
  229. toklen -= 4;
  230. if (tmp <= 0 || tmp > AFSTOKEN_K5_REALM_MAX)
  231. return -EINVAL;
  232. if (tmp > toklen)
  233. return -EINVAL;
  234. princ->realm = kmalloc(tmp + 1, GFP_KERNEL);
  235. if (!princ->realm)
  236. return -ENOMEM;
  237. memcpy(princ->realm, xdr, tmp);
  238. princ->realm[tmp] = 0;
  239. tmp = (tmp + 3) & ~3;
  240. toklen -= tmp;
  241. xdr += tmp >> 2;
  242. _debug("%s/...@%s", princ->name_parts[0], princ->realm);
  243. *_xdr = xdr;
  244. *_toklen = toklen;
  245. _leave(" = 0 [toklen=%u]", toklen);
  246. return 0;
  247. }
  248. /*
  249. * extract a piece of krb5 tagged data
  250. */
  251. static int rxrpc_krb5_decode_tagged_data(struct krb5_tagged_data *td,
  252. size_t max_data_size,
  253. const __be32 **_xdr,
  254. unsigned int *_toklen)
  255. {
  256. const __be32 *xdr = *_xdr;
  257. unsigned int toklen = *_toklen, len;
  258. /* there must be at least one tag and one length word */
  259. if (toklen <= 8)
  260. return -EINVAL;
  261. _enter(",%zu,{%x,%x},%u",
  262. max_data_size, ntohl(xdr[0]), ntohl(xdr[1]), toklen);
  263. td->tag = ntohl(*xdr++);
  264. len = ntohl(*xdr++);
  265. toklen -= 8;
  266. if (len > max_data_size)
  267. return -EINVAL;
  268. td->data_len = len;
  269. if (len > 0) {
  270. td->data = kmemdup(xdr, len, GFP_KERNEL);
  271. if (!td->data)
  272. return -ENOMEM;
  273. len = (len + 3) & ~3;
  274. toklen -= len;
  275. xdr += len >> 2;
  276. }
  277. _debug("tag %x len %x", td->tag, td->data_len);
  278. *_xdr = xdr;
  279. *_toklen = toklen;
  280. _leave(" = 0 [toklen=%u]", toklen);
  281. return 0;
  282. }
  283. /*
  284. * extract an array of tagged data
  285. */
  286. static int rxrpc_krb5_decode_tagged_array(struct krb5_tagged_data **_td,
  287. u8 *_n_elem,
  288. u8 max_n_elem,
  289. size_t max_elem_size,
  290. const __be32 **_xdr,
  291. unsigned int *_toklen)
  292. {
  293. struct krb5_tagged_data *td;
  294. const __be32 *xdr = *_xdr;
  295. unsigned int toklen = *_toklen, n_elem, loop;
  296. int ret;
  297. /* there must be at least one count */
  298. if (toklen < 4)
  299. return -EINVAL;
  300. _enter(",,%u,%zu,{%x},%u",
  301. max_n_elem, max_elem_size, ntohl(xdr[0]), toklen);
  302. n_elem = ntohl(*xdr++);
  303. toklen -= 4;
  304. if (n_elem < 0 || n_elem > max_n_elem)
  305. return -EINVAL;
  306. *_n_elem = n_elem;
  307. if (n_elem > 0) {
  308. if (toklen <= (n_elem + 1) * 4)
  309. return -EINVAL;
  310. _debug("n_elem %d", n_elem);
  311. td = kcalloc(n_elem, sizeof(struct krb5_tagged_data),
  312. GFP_KERNEL);
  313. if (!td)
  314. return -ENOMEM;
  315. *_td = td;
  316. for (loop = 0; loop < n_elem; loop++) {
  317. ret = rxrpc_krb5_decode_tagged_data(&td[loop],
  318. max_elem_size,
  319. &xdr, &toklen);
  320. if (ret < 0)
  321. return ret;
  322. }
  323. }
  324. *_xdr = xdr;
  325. *_toklen = toklen;
  326. _leave(" = 0 [toklen=%u]", toklen);
  327. return 0;
  328. }
  329. /*
  330. * extract a krb5 ticket
  331. */
  332. static int rxrpc_krb5_decode_ticket(u8 **_ticket, u16 *_tktlen,
  333. const __be32 **_xdr, unsigned int *_toklen)
  334. {
  335. const __be32 *xdr = *_xdr;
  336. unsigned int toklen = *_toklen, len;
  337. /* there must be at least one length word */
  338. if (toklen <= 4)
  339. return -EINVAL;
  340. _enter(",{%x},%u", ntohl(xdr[0]), toklen);
  341. len = ntohl(*xdr++);
  342. toklen -= 4;
  343. if (len > AFSTOKEN_K5_TIX_MAX)
  344. return -EINVAL;
  345. *_tktlen = len;
  346. _debug("ticket len %u", len);
  347. if (len > 0) {
  348. *_ticket = kmemdup(xdr, len, GFP_KERNEL);
  349. if (!*_ticket)
  350. return -ENOMEM;
  351. len = (len + 3) & ~3;
  352. toklen -= len;
  353. xdr += len >> 2;
  354. }
  355. *_xdr = xdr;
  356. *_toklen = toklen;
  357. _leave(" = 0 [toklen=%u]", toklen);
  358. return 0;
  359. }
  360. /*
  361. * parse an RxK5 type XDR format token
  362. * - the caller guarantees we have at least 4 words
  363. */
  364. static int rxrpc_instantiate_xdr_rxk5(struct key *key, const __be32 *xdr,
  365. unsigned int toklen)
  366. {
  367. struct rxrpc_key_token *token, **pptoken;
  368. struct rxk5_key *rxk5;
  369. const __be32 *end_xdr = xdr + (toklen >> 2);
  370. int ret;
  371. _enter(",{%x,%x,%x,%x},%u",
  372. ntohl(xdr[0]), ntohl(xdr[1]), ntohl(xdr[2]), ntohl(xdr[3]),
  373. toklen);
  374. /* reserve some payload space for this subkey - the length of the token
  375. * is a reasonable approximation */
  376. ret = key_payload_reserve(key, key->datalen + toklen);
  377. if (ret < 0)
  378. return ret;
  379. token = kzalloc(sizeof(*token), GFP_KERNEL);
  380. if (!token)
  381. return -ENOMEM;
  382. rxk5 = kzalloc(sizeof(*rxk5), GFP_KERNEL);
  383. if (!rxk5) {
  384. kfree(token);
  385. return -ENOMEM;
  386. }
  387. token->security_index = RXRPC_SECURITY_RXK5;
  388. token->k5 = rxk5;
  389. /* extract the principals */
  390. ret = rxrpc_krb5_decode_principal(&rxk5->client, &xdr, &toklen);
  391. if (ret < 0)
  392. goto error;
  393. ret = rxrpc_krb5_decode_principal(&rxk5->server, &xdr, &toklen);
  394. if (ret < 0)
  395. goto error;
  396. /* extract the session key and the encoding type (the tag field ->
  397. * ENCTYPE_xxx) */
  398. ret = rxrpc_krb5_decode_tagged_data(&rxk5->session, AFSTOKEN_DATA_MAX,
  399. &xdr, &toklen);
  400. if (ret < 0)
  401. goto error;
  402. if (toklen < 4 * 8 + 2 * 4)
  403. goto inval;
  404. rxk5->authtime = be64_to_cpup((const __be64 *) xdr);
  405. xdr += 2;
  406. rxk5->starttime = be64_to_cpup((const __be64 *) xdr);
  407. xdr += 2;
  408. rxk5->endtime = be64_to_cpup((const __be64 *) xdr);
  409. xdr += 2;
  410. rxk5->renew_till = be64_to_cpup((const __be64 *) xdr);
  411. xdr += 2;
  412. rxk5->is_skey = ntohl(*xdr++);
  413. rxk5->flags = ntohl(*xdr++);
  414. toklen -= 4 * 8 + 2 * 4;
  415. _debug("times: a=%llx s=%llx e=%llx rt=%llx",
  416. rxk5->authtime, rxk5->starttime, rxk5->endtime,
  417. rxk5->renew_till);
  418. _debug("is_skey=%x flags=%x", rxk5->is_skey, rxk5->flags);
  419. /* extract the permitted client addresses */
  420. ret = rxrpc_krb5_decode_tagged_array(&rxk5->addresses,
  421. &rxk5->n_addresses,
  422. AFSTOKEN_K5_ADDRESSES_MAX,
  423. AFSTOKEN_DATA_MAX,
  424. &xdr, &toklen);
  425. if (ret < 0)
  426. goto error;
  427. ASSERTCMP((end_xdr - xdr) << 2, ==, toklen);
  428. /* extract the tickets */
  429. ret = rxrpc_krb5_decode_ticket(&rxk5->ticket, &rxk5->ticket_len,
  430. &xdr, &toklen);
  431. if (ret < 0)
  432. goto error;
  433. ret = rxrpc_krb5_decode_ticket(&rxk5->ticket2, &rxk5->ticket2_len,
  434. &xdr, &toklen);
  435. if (ret < 0)
  436. goto error;
  437. ASSERTCMP((end_xdr - xdr) << 2, ==, toklen);
  438. /* extract the typed auth data */
  439. ret = rxrpc_krb5_decode_tagged_array(&rxk5->authdata,
  440. &rxk5->n_authdata,
  441. AFSTOKEN_K5_AUTHDATA_MAX,
  442. AFSTOKEN_BDATALN_MAX,
  443. &xdr, &toklen);
  444. if (ret < 0)
  445. goto error;
  446. ASSERTCMP((end_xdr - xdr) << 2, ==, toklen);
  447. if (toklen != 0)
  448. goto inval;
  449. /* attach the payload to the key */
  450. for (pptoken = (struct rxrpc_key_token **)&key->payload.data;
  451. *pptoken;
  452. pptoken = &(*pptoken)->next)
  453. continue;
  454. *pptoken = token;
  455. if (token->kad->expiry < key->expiry)
  456. key->expiry = token->kad->expiry;
  457. _leave(" = 0");
  458. return 0;
  459. inval:
  460. ret = -EINVAL;
  461. error:
  462. rxrpc_rxk5_free(rxk5);
  463. kfree(token);
  464. _leave(" = %d", ret);
  465. return ret;
  466. }
  467. /*
  468. * attempt to parse the data as the XDR format
  469. * - the caller guarantees we have more than 7 words
  470. */
  471. static int rxrpc_instantiate_xdr(struct key *key, const void *data, size_t datalen)
  472. {
  473. const __be32 *xdr = data, *token;
  474. const char *cp;
  475. unsigned int len, tmp, loop, ntoken, toklen, sec_ix;
  476. int ret;
  477. _enter(",{%x,%x,%x,%x},%zu",
  478. ntohl(xdr[0]), ntohl(xdr[1]), ntohl(xdr[2]), ntohl(xdr[3]),
  479. datalen);
  480. if (datalen > AFSTOKEN_LENGTH_MAX)
  481. goto not_xdr;
  482. /* XDR is an array of __be32's */
  483. if (datalen & 3)
  484. goto not_xdr;
  485. /* the flags should be 0 (the setpag bit must be handled by
  486. * userspace) */
  487. if (ntohl(*xdr++) != 0)
  488. goto not_xdr;
  489. datalen -= 4;
  490. /* check the cell name */
  491. len = ntohl(*xdr++);
  492. if (len < 1 || len > AFSTOKEN_CELL_MAX)
  493. goto not_xdr;
  494. datalen -= 4;
  495. tmp = (len + 3) & ~3;
  496. if (tmp > datalen)
  497. goto not_xdr;
  498. cp = (const char *) xdr;
  499. for (loop = 0; loop < len; loop++)
  500. if (!isprint(cp[loop]))
  501. goto not_xdr;
  502. if (len < tmp)
  503. for (; loop < tmp; loop++)
  504. if (cp[loop])
  505. goto not_xdr;
  506. _debug("cellname: [%u/%u] '%*.*s'",
  507. len, tmp, len, len, (const char *) xdr);
  508. datalen -= tmp;
  509. xdr += tmp >> 2;
  510. /* get the token count */
  511. if (datalen < 12)
  512. goto not_xdr;
  513. ntoken = ntohl(*xdr++);
  514. datalen -= 4;
  515. _debug("ntoken: %x", ntoken);
  516. if (ntoken < 1 || ntoken > AFSTOKEN_MAX)
  517. goto not_xdr;
  518. /* check each token wrapper */
  519. token = xdr;
  520. loop = ntoken;
  521. do {
  522. if (datalen < 8)
  523. goto not_xdr;
  524. toklen = ntohl(*xdr++);
  525. sec_ix = ntohl(*xdr);
  526. datalen -= 4;
  527. _debug("token: [%x/%zx] %x", toklen, datalen, sec_ix);
  528. if (toklen < 20 || toklen > datalen)
  529. goto not_xdr;
  530. datalen -= (toklen + 3) & ~3;
  531. xdr += (toklen + 3) >> 2;
  532. } while (--loop > 0);
  533. _debug("remainder: %zu", datalen);
  534. if (datalen != 0)
  535. goto not_xdr;
  536. /* okay: we're going to assume it's valid XDR format
  537. * - we ignore the cellname, relying on the key to be correctly named
  538. */
  539. do {
  540. xdr = token;
  541. toklen = ntohl(*xdr++);
  542. token = xdr + ((toklen + 3) >> 2);
  543. sec_ix = ntohl(*xdr++);
  544. toklen -= 4;
  545. _debug("TOKEN type=%u [%p-%p]", sec_ix, xdr, token);
  546. switch (sec_ix) {
  547. case RXRPC_SECURITY_RXKAD:
  548. ret = rxrpc_instantiate_xdr_rxkad(key, xdr, toklen);
  549. if (ret != 0)
  550. goto error;
  551. break;
  552. case RXRPC_SECURITY_RXK5:
  553. ret = rxrpc_instantiate_xdr_rxk5(key, xdr, toklen);
  554. if (ret != 0)
  555. goto error;
  556. break;
  557. default:
  558. ret = -EPROTONOSUPPORT;
  559. goto error;
  560. }
  561. } while (--ntoken > 0);
  562. _leave(" = 0");
  563. return 0;
  564. not_xdr:
  565. _leave(" = -EPROTO");
  566. return -EPROTO;
  567. error:
  568. _leave(" = %d", ret);
  569. return ret;
  570. }
  571. /*
  572. * instantiate an rxrpc defined key
  573. * data should be of the form:
  574. * OFFSET LEN CONTENT
  575. * 0 4 key interface version number
  576. * 4 2 security index (type)
  577. * 6 2 ticket length
  578. * 8 4 key expiry time (time_t)
  579. * 12 4 kvno
  580. * 16 8 session key
  581. * 24 [len] ticket
  582. *
  583. * if no data is provided, then a no-security key is made
  584. */
  585. static int rxrpc_instantiate(struct key *key, const void *data, size_t datalen)
  586. {
  587. const struct rxrpc_key_data_v1 *v1;
  588. struct rxrpc_key_token *token, **pp;
  589. size_t plen;
  590. u32 kver;
  591. int ret;
  592. _enter("{%x},,%zu", key_serial(key), datalen);
  593. /* handle a no-security key */
  594. if (!data && datalen == 0)
  595. return 0;
  596. /* determine if the XDR payload format is being used */
  597. if (datalen > 7 * 4) {
  598. ret = rxrpc_instantiate_xdr(key, data, datalen);
  599. if (ret != -EPROTO)
  600. return ret;
  601. }
  602. /* get the key interface version number */
  603. ret = -EINVAL;
  604. if (datalen <= 4 || !data)
  605. goto error;
  606. memcpy(&kver, data, sizeof(kver));
  607. data += sizeof(kver);
  608. datalen -= sizeof(kver);
  609. _debug("KEY I/F VERSION: %u", kver);
  610. ret = -EKEYREJECTED;
  611. if (kver != 1)
  612. goto error;
  613. /* deal with a version 1 key */
  614. ret = -EINVAL;
  615. if (datalen < sizeof(*v1))
  616. goto error;
  617. v1 = data;
  618. if (datalen != sizeof(*v1) + v1->ticket_length)
  619. goto error;
  620. _debug("SCIX: %u", v1->security_index);
  621. _debug("TLEN: %u", v1->ticket_length);
  622. _debug("EXPY: %x", v1->expiry);
  623. _debug("KVNO: %u", v1->kvno);
  624. _debug("SKEY: %02x%02x%02x%02x%02x%02x%02x%02x",
  625. v1->session_key[0], v1->session_key[1],
  626. v1->session_key[2], v1->session_key[3],
  627. v1->session_key[4], v1->session_key[5],
  628. v1->session_key[6], v1->session_key[7]);
  629. if (v1->ticket_length >= 8)
  630. _debug("TCKT: %02x%02x%02x%02x%02x%02x%02x%02x",
  631. v1->ticket[0], v1->ticket[1],
  632. v1->ticket[2], v1->ticket[3],
  633. v1->ticket[4], v1->ticket[5],
  634. v1->ticket[6], v1->ticket[7]);
  635. ret = -EPROTONOSUPPORT;
  636. if (v1->security_index != RXRPC_SECURITY_RXKAD)
  637. goto error;
  638. plen = sizeof(*token->kad) + v1->ticket_length;
  639. ret = key_payload_reserve(key, plen + sizeof(*token));
  640. if (ret < 0)
  641. goto error;
  642. ret = -ENOMEM;
  643. token = kzalloc(sizeof(*token), GFP_KERNEL);
  644. if (!token)
  645. goto error;
  646. token->kad = kzalloc(plen, GFP_KERNEL);
  647. if (!token->kad)
  648. goto error_free;
  649. token->security_index = RXRPC_SECURITY_RXKAD;
  650. token->kad->ticket_len = v1->ticket_length;
  651. token->kad->expiry = v1->expiry;
  652. token->kad->kvno = v1->kvno;
  653. memcpy(&token->kad->session_key, &v1->session_key, 8);
  654. memcpy(&token->kad->ticket, v1->ticket, v1->ticket_length);
  655. /* attach the data */
  656. key->type_data.x[0]++;
  657. pp = (struct rxrpc_key_token **)&key->payload.data;
  658. while (*pp)
  659. pp = &(*pp)->next;
  660. *pp = token;
  661. if (token->kad->expiry < key->expiry)
  662. key->expiry = token->kad->expiry;
  663. token = NULL;
  664. ret = 0;
  665. error_free:
  666. kfree(token);
  667. error:
  668. return ret;
  669. }
  670. /*
  671. * instantiate a server secret key
  672. * data should be a pointer to the 8-byte secret key
  673. */
  674. static int rxrpc_instantiate_s(struct key *key, const void *data,
  675. size_t datalen)
  676. {
  677. struct crypto_blkcipher *ci;
  678. _enter("{%x},,%zu", key_serial(key), datalen);
  679. if (datalen != 8)
  680. return -EINVAL;
  681. memcpy(&key->type_data, data, 8);
  682. ci = crypto_alloc_blkcipher("pcbc(des)", 0, CRYPTO_ALG_ASYNC);
  683. if (IS_ERR(ci)) {
  684. _leave(" = %ld", PTR_ERR(ci));
  685. return PTR_ERR(ci);
  686. }
  687. if (crypto_blkcipher_setkey(ci, data, 8) < 0)
  688. BUG();
  689. key->payload.data = ci;
  690. _leave(" = 0");
  691. return 0;
  692. }
  693. /*
  694. * dispose of the data dangling from the corpse of a rxrpc key
  695. */
  696. static void rxrpc_destroy(struct key *key)
  697. {
  698. struct rxrpc_key_token *token;
  699. while ((token = key->payload.data)) {
  700. key->payload.data = token->next;
  701. switch (token->security_index) {
  702. case RXRPC_SECURITY_RXKAD:
  703. kfree(token->kad);
  704. break;
  705. case RXRPC_SECURITY_RXK5:
  706. if (token->k5)
  707. rxrpc_rxk5_free(token->k5);
  708. break;
  709. default:
  710. printk(KERN_ERR "Unknown token type %x on rxrpc key\n",
  711. token->security_index);
  712. BUG();
  713. }
  714. kfree(token);
  715. }
  716. }
  717. /*
  718. * dispose of the data dangling from the corpse of a rxrpc key
  719. */
  720. static void rxrpc_destroy_s(struct key *key)
  721. {
  722. if (key->payload.data) {
  723. crypto_free_blkcipher(key->payload.data);
  724. key->payload.data = NULL;
  725. }
  726. }
  727. /*
  728. * describe the rxrpc key
  729. */
  730. static void rxrpc_describe(const struct key *key, struct seq_file *m)
  731. {
  732. seq_puts(m, key->description);
  733. }
  734. /*
  735. * grab the security key for a socket
  736. */
  737. int rxrpc_request_key(struct rxrpc_sock *rx, char __user *optval, int optlen)
  738. {
  739. struct key *key;
  740. char *description;
  741. _enter("");
  742. if (optlen <= 0 || optlen > PAGE_SIZE - 1)
  743. return -EINVAL;
  744. description = kmalloc(optlen + 1, GFP_KERNEL);
  745. if (!description)
  746. return -ENOMEM;
  747. if (copy_from_user(description, optval, optlen)) {
  748. kfree(description);
  749. return -EFAULT;
  750. }
  751. description[optlen] = 0;
  752. key = request_key(&key_type_rxrpc, description, NULL);
  753. if (IS_ERR(key)) {
  754. kfree(description);
  755. _leave(" = %ld", PTR_ERR(key));
  756. return PTR_ERR(key);
  757. }
  758. rx->key = key;
  759. kfree(description);
  760. _leave(" = 0 [key %x]", key->serial);
  761. return 0;
  762. }
  763. /*
  764. * grab the security keyring for a server socket
  765. */
  766. int rxrpc_server_keyring(struct rxrpc_sock *rx, char __user *optval,
  767. int optlen)
  768. {
  769. struct key *key;
  770. char *description;
  771. _enter("");
  772. if (optlen <= 0 || optlen > PAGE_SIZE - 1)
  773. return -EINVAL;
  774. description = kmalloc(optlen + 1, GFP_KERNEL);
  775. if (!description)
  776. return -ENOMEM;
  777. if (copy_from_user(description, optval, optlen)) {
  778. kfree(description);
  779. return -EFAULT;
  780. }
  781. description[optlen] = 0;
  782. key = request_key(&key_type_keyring, description, NULL);
  783. if (IS_ERR(key)) {
  784. kfree(description);
  785. _leave(" = %ld", PTR_ERR(key));
  786. return PTR_ERR(key);
  787. }
  788. rx->securities = key;
  789. kfree(description);
  790. _leave(" = 0 [key %x]", key->serial);
  791. return 0;
  792. }
  793. /*
  794. * generate a server data key
  795. */
  796. int rxrpc_get_server_data_key(struct rxrpc_connection *conn,
  797. const void *session_key,
  798. time_t expiry,
  799. u32 kvno)
  800. {
  801. const struct cred *cred = current_cred();
  802. struct key *key;
  803. int ret;
  804. struct {
  805. u32 kver;
  806. struct rxrpc_key_data_v1 v1;
  807. } data;
  808. _enter("");
  809. key = key_alloc(&key_type_rxrpc, "x", 0, 0, cred, 0,
  810. KEY_ALLOC_NOT_IN_QUOTA);
  811. if (IS_ERR(key)) {
  812. _leave(" = -ENOMEM [alloc %ld]", PTR_ERR(key));
  813. return -ENOMEM;
  814. }
  815. _debug("key %d", key_serial(key));
  816. data.kver = 1;
  817. data.v1.security_index = RXRPC_SECURITY_RXKAD;
  818. data.v1.ticket_length = 0;
  819. data.v1.expiry = expiry;
  820. data.v1.kvno = 0;
  821. memcpy(&data.v1.session_key, session_key, sizeof(data.v1.session_key));
  822. ret = key_instantiate_and_link(key, &data, sizeof(data), NULL, NULL);
  823. if (ret < 0)
  824. goto error;
  825. conn->key = key;
  826. _leave(" = 0 [%d]", key_serial(key));
  827. return 0;
  828. error:
  829. key_revoke(key);
  830. key_put(key);
  831. _leave(" = -ENOMEM [ins %d]", ret);
  832. return -ENOMEM;
  833. }
  834. EXPORT_SYMBOL(rxrpc_get_server_data_key);
  835. /**
  836. * rxrpc_get_null_key - Generate a null RxRPC key
  837. * @keyname: The name to give the key.
  838. *
  839. * Generate a null RxRPC key that can be used to indicate anonymous security is
  840. * required for a particular domain.
  841. */
  842. struct key *rxrpc_get_null_key(const char *keyname)
  843. {
  844. const struct cred *cred = current_cred();
  845. struct key *key;
  846. int ret;
  847. key = key_alloc(&key_type_rxrpc, keyname, 0, 0, cred,
  848. KEY_POS_SEARCH, KEY_ALLOC_NOT_IN_QUOTA);
  849. if (IS_ERR(key))
  850. return key;
  851. ret = key_instantiate_and_link(key, NULL, 0, NULL, NULL);
  852. if (ret < 0) {
  853. key_revoke(key);
  854. key_put(key);
  855. return ERR_PTR(ret);
  856. }
  857. return key;
  858. }
  859. EXPORT_SYMBOL(rxrpc_get_null_key);
  860. /*
  861. * read the contents of an rxrpc key
  862. * - this returns the result in XDR form
  863. */
  864. static long rxrpc_read(const struct key *key,
  865. char __user *buffer, size_t buflen)
  866. {
  867. const struct rxrpc_key_token *token;
  868. const struct krb5_principal *princ;
  869. size_t size;
  870. __be32 __user *xdr, *oldxdr;
  871. u32 cnlen, toksize, ntoks, tok, zero;
  872. u16 toksizes[AFSTOKEN_MAX];
  873. int loop;
  874. _enter("");
  875. /* we don't know what form we should return non-AFS keys in */
  876. if (memcmp(key->description, "afs@", 4) != 0)
  877. return -EOPNOTSUPP;
  878. cnlen = strlen(key->description + 4);
  879. #define RND(X) (((X) + 3) & ~3)
  880. /* AFS keys we return in XDR form, so we need to work out the size of
  881. * the XDR */
  882. size = 2 * 4; /* flags, cellname len */
  883. size += RND(cnlen); /* cellname */
  884. size += 1 * 4; /* token count */
  885. ntoks = 0;
  886. for (token = key->payload.data; token; token = token->next) {
  887. toksize = 4; /* sec index */
  888. switch (token->security_index) {
  889. case RXRPC_SECURITY_RXKAD:
  890. toksize += 8 * 4; /* viceid, kvno, key*2, begin,
  891. * end, primary, tktlen */
  892. toksize += RND(token->kad->ticket_len);
  893. break;
  894. case RXRPC_SECURITY_RXK5:
  895. princ = &token->k5->client;
  896. toksize += 4 + princ->n_name_parts * 4;
  897. for (loop = 0; loop < princ->n_name_parts; loop++)
  898. toksize += RND(strlen(princ->name_parts[loop]));
  899. toksize += 4 + RND(strlen(princ->realm));
  900. princ = &token->k5->server;
  901. toksize += 4 + princ->n_name_parts * 4;
  902. for (loop = 0; loop < princ->n_name_parts; loop++)
  903. toksize += RND(strlen(princ->name_parts[loop]));
  904. toksize += 4 + RND(strlen(princ->realm));
  905. toksize += 8 + RND(token->k5->session.data_len);
  906. toksize += 4 * 8 + 2 * 4;
  907. toksize += 4 + token->k5->n_addresses * 8;
  908. for (loop = 0; loop < token->k5->n_addresses; loop++)
  909. toksize += RND(token->k5->addresses[loop].data_len);
  910. toksize += 4 + RND(token->k5->ticket_len);
  911. toksize += 4 + RND(token->k5->ticket2_len);
  912. toksize += 4 + token->k5->n_authdata * 8;
  913. for (loop = 0; loop < token->k5->n_authdata; loop++)
  914. toksize += RND(token->k5->authdata[loop].data_len);
  915. break;
  916. default: /* we have a ticket we can't encode */
  917. BUG();
  918. continue;
  919. }
  920. _debug("token[%u]: toksize=%u", ntoks, toksize);
  921. ASSERTCMP(toksize, <=, AFSTOKEN_LENGTH_MAX);
  922. toksizes[ntoks++] = toksize;
  923. size += toksize + 4; /* each token has a length word */
  924. }
  925. #undef RND
  926. if (!buffer || buflen < size)
  927. return size;
  928. xdr = (__be32 __user *) buffer;
  929. zero = 0;
  930. #define ENCODE(x) \
  931. do { \
  932. __be32 y = htonl(x); \
  933. if (put_user(y, xdr++) < 0) \
  934. goto fault; \
  935. } while(0)
  936. #define ENCODE_DATA(l, s) \
  937. do { \
  938. u32 _l = (l); \
  939. ENCODE(l); \
  940. if (copy_to_user(xdr, (s), _l) != 0) \
  941. goto fault; \
  942. if (_l & 3 && \
  943. copy_to_user((u8 *)xdr + _l, &zero, 4 - (_l & 3)) != 0) \
  944. goto fault; \
  945. xdr += (_l + 3) >> 2; \
  946. } while(0)
  947. #define ENCODE64(x) \
  948. do { \
  949. __be64 y = cpu_to_be64(x); \
  950. if (copy_to_user(xdr, &y, 8) != 0) \
  951. goto fault; \
  952. xdr += 8 >> 2; \
  953. } while(0)
  954. #define ENCODE_STR(s) \
  955. do { \
  956. const char *_s = (s); \
  957. ENCODE_DATA(strlen(_s), _s); \
  958. } while(0)
  959. ENCODE(0); /* flags */
  960. ENCODE_DATA(cnlen, key->description + 4); /* cellname */
  961. ENCODE(ntoks);
  962. tok = 0;
  963. for (token = key->payload.data; token; token = token->next) {
  964. toksize = toksizes[tok++];
  965. ENCODE(toksize);
  966. oldxdr = xdr;
  967. ENCODE(token->security_index);
  968. switch (token->security_index) {
  969. case RXRPC_SECURITY_RXKAD:
  970. ENCODE(token->kad->vice_id);
  971. ENCODE(token->kad->kvno);
  972. ENCODE_DATA(8, token->kad->session_key);
  973. ENCODE(token->kad->start);
  974. ENCODE(token->kad->expiry);
  975. ENCODE(token->kad->primary_flag);
  976. ENCODE_DATA(token->kad->ticket_len, token->kad->ticket);
  977. break;
  978. case RXRPC_SECURITY_RXK5:
  979. princ = &token->k5->client;
  980. ENCODE(princ->n_name_parts);
  981. for (loop = 0; loop < princ->n_name_parts; loop++)
  982. ENCODE_STR(princ->name_parts[loop]);
  983. ENCODE_STR(princ->realm);
  984. princ = &token->k5->server;
  985. ENCODE(princ->n_name_parts);
  986. for (loop = 0; loop < princ->n_name_parts; loop++)
  987. ENCODE_STR(princ->name_parts[loop]);
  988. ENCODE_STR(princ->realm);
  989. ENCODE(token->k5->session.tag);
  990. ENCODE_DATA(token->k5->session.data_len,
  991. token->k5->session.data);
  992. ENCODE64(token->k5->authtime);
  993. ENCODE64(token->k5->starttime);
  994. ENCODE64(token->k5->endtime);
  995. ENCODE64(token->k5->renew_till);
  996. ENCODE(token->k5->is_skey);
  997. ENCODE(token->k5->flags);
  998. ENCODE(token->k5->n_addresses);
  999. for (loop = 0; loop < token->k5->n_addresses; loop++) {
  1000. ENCODE(token->k5->addresses[loop].tag);
  1001. ENCODE_DATA(token->k5->addresses[loop].data_len,
  1002. token->k5->addresses[loop].data);
  1003. }
  1004. ENCODE_DATA(token->k5->ticket_len, token->k5->ticket);
  1005. ENCODE_DATA(token->k5->ticket2_len, token->k5->ticket2);
  1006. ENCODE(token->k5->n_authdata);
  1007. for (loop = 0; loop < token->k5->n_authdata; loop++) {
  1008. ENCODE(token->k5->authdata[loop].tag);
  1009. ENCODE_DATA(token->k5->authdata[loop].data_len,
  1010. token->k5->authdata[loop].data);
  1011. }
  1012. break;
  1013. default:
  1014. BUG();
  1015. break;
  1016. }
  1017. ASSERTCMP((unsigned long)xdr - (unsigned long)oldxdr, ==,
  1018. toksize);
  1019. }
  1020. #undef ENCODE_STR
  1021. #undef ENCODE_DATA
  1022. #undef ENCODE64
  1023. #undef ENCODE
  1024. ASSERTCMP(tok, ==, ntoks);
  1025. ASSERTCMP((char __user *) xdr - buffer, ==, size);
  1026. _leave(" = %zu", size);
  1027. return size;
  1028. fault:
  1029. _leave(" = -EFAULT");
  1030. return -EFAULT;
  1031. }