ar-key.c 29 KB

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