key.c 29 KB

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