ar-key.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  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 <net/sock.h>
  21. #include <net/af_rxrpc.h>
  22. #include <keys/rxrpc-type.h>
  23. #include <keys/user-type.h>
  24. #include "ar-internal.h"
  25. static int rxrpc_instantiate(struct key *, const void *, size_t);
  26. static int rxrpc_instantiate_s(struct key *, const void *, size_t);
  27. static void rxrpc_destroy(struct key *);
  28. static void rxrpc_destroy_s(struct key *);
  29. static void rxrpc_describe(const struct key *, struct seq_file *);
  30. static long rxrpc_read(const struct key *, char __user *, size_t);
  31. /*
  32. * rxrpc defined keys take an arbitrary string as the description and an
  33. * arbitrary blob of data as the payload
  34. */
  35. struct key_type key_type_rxrpc = {
  36. .name = "rxrpc",
  37. .instantiate = rxrpc_instantiate,
  38. .match = user_match,
  39. .destroy = rxrpc_destroy,
  40. .describe = rxrpc_describe,
  41. .read = rxrpc_read,
  42. };
  43. EXPORT_SYMBOL(key_type_rxrpc);
  44. /*
  45. * rxrpc server defined keys take "<serviceId>:<securityIndex>" as the
  46. * description and an 8-byte decryption key as the payload
  47. */
  48. struct key_type key_type_rxrpc_s = {
  49. .name = "rxrpc_s",
  50. .instantiate = rxrpc_instantiate_s,
  51. .match = user_match,
  52. .destroy = rxrpc_destroy_s,
  53. .describe = rxrpc_describe,
  54. };
  55. /*
  56. * parse an RxKAD type XDR format token
  57. * - the caller guarantees we have at least 4 words
  58. */
  59. static int rxrpc_instantiate_xdr_rxkad(struct key *key, const __be32 *xdr,
  60. unsigned toklen)
  61. {
  62. struct rxrpc_key_token *token;
  63. size_t plen;
  64. u32 tktlen;
  65. int ret;
  66. _enter(",{%x,%x,%x,%x},%u",
  67. ntohl(xdr[0]), ntohl(xdr[1]), ntohl(xdr[2]), ntohl(xdr[3]),
  68. toklen);
  69. if (toklen <= 8 * 4)
  70. return -EKEYREJECTED;
  71. tktlen = ntohl(xdr[7]);
  72. _debug("tktlen: %x", tktlen);
  73. if (tktlen > AFSTOKEN_RK_TIX_MAX)
  74. return -EKEYREJECTED;
  75. if (8 * 4 + tktlen != toklen)
  76. return -EKEYREJECTED;
  77. plen = sizeof(*token) + sizeof(*token->kad) + tktlen;
  78. ret = key_payload_reserve(key, key->datalen + plen);
  79. if (ret < 0)
  80. return ret;
  81. plen -= sizeof(*token);
  82. token = kmalloc(sizeof(*token), GFP_KERNEL);
  83. if (!token)
  84. return -ENOMEM;
  85. token->kad = kmalloc(plen, GFP_KERNEL);
  86. if (!token->kad) {
  87. kfree(token);
  88. return -ENOMEM;
  89. }
  90. token->security_index = RXRPC_SECURITY_RXKAD;
  91. token->kad->ticket_len = tktlen;
  92. token->kad->vice_id = ntohl(xdr[0]);
  93. token->kad->kvno = ntohl(xdr[1]);
  94. token->kad->start = ntohl(xdr[4]);
  95. token->kad->expiry = ntohl(xdr[5]);
  96. token->kad->primary_flag = ntohl(xdr[6]);
  97. memcpy(&token->kad->session_key, &xdr[2], 8);
  98. memcpy(&token->kad->ticket, &xdr[8], tktlen);
  99. _debug("SCIX: %u", token->security_index);
  100. _debug("TLEN: %u", token->kad->ticket_len);
  101. _debug("EXPY: %x", token->kad->expiry);
  102. _debug("KVNO: %u", token->kad->kvno);
  103. _debug("PRIM: %u", token->kad->primary_flag);
  104. _debug("SKEY: %02x%02x%02x%02x%02x%02x%02x%02x",
  105. token->kad->session_key[0], token->kad->session_key[1],
  106. token->kad->session_key[2], token->kad->session_key[3],
  107. token->kad->session_key[4], token->kad->session_key[5],
  108. token->kad->session_key[6], token->kad->session_key[7]);
  109. if (token->kad->ticket_len >= 8)
  110. _debug("TCKT: %02x%02x%02x%02x%02x%02x%02x%02x",
  111. token->kad->ticket[0], token->kad->ticket[1],
  112. token->kad->ticket[2], token->kad->ticket[3],
  113. token->kad->ticket[4], token->kad->ticket[5],
  114. token->kad->ticket[6], token->kad->ticket[7]);
  115. /* count the number of tokens attached */
  116. key->type_data.x[0]++;
  117. /* attach the data */
  118. token->next = key->payload.data;
  119. key->payload.data = token;
  120. if (token->kad->expiry < key->expiry)
  121. key->expiry = token->kad->expiry;
  122. _leave(" = 0");
  123. return 0;
  124. }
  125. /*
  126. * attempt to parse the data as the XDR format
  127. * - the caller guarantees we have more than 7 words
  128. */
  129. static int rxrpc_instantiate_xdr(struct key *key, const void *data, size_t datalen)
  130. {
  131. const __be32 *xdr = data, *token;
  132. const char *cp;
  133. unsigned len, tmp, loop, ntoken, toklen, sec_ix;
  134. int ret;
  135. _enter(",{%x,%x,%x,%x},%zu",
  136. ntohl(xdr[0]), ntohl(xdr[1]), ntohl(xdr[2]), ntohl(xdr[3]),
  137. datalen);
  138. if (datalen > AFSTOKEN_LENGTH_MAX)
  139. goto not_xdr;
  140. /* XDR is an array of __be32's */
  141. if (datalen & 3)
  142. goto not_xdr;
  143. /* the flags should be 0 (the setpag bit must be handled by
  144. * userspace) */
  145. if (ntohl(*xdr++) != 0)
  146. goto not_xdr;
  147. datalen -= 4;
  148. /* check the cell name */
  149. len = ntohl(*xdr++);
  150. if (len < 1 || len > AFSTOKEN_CELL_MAX)
  151. goto not_xdr;
  152. datalen -= 4;
  153. tmp = (len + 3) & ~3;
  154. if (tmp > datalen)
  155. goto not_xdr;
  156. cp = (const char *) xdr;
  157. for (loop = 0; loop < len; loop++)
  158. if (!isprint(cp[loop]))
  159. goto not_xdr;
  160. if (len < tmp)
  161. for (; loop < tmp; loop++)
  162. if (cp[loop])
  163. goto not_xdr;
  164. _debug("cellname: [%u/%u] '%*.*s'",
  165. len, tmp, len, len, (const char *) xdr);
  166. datalen -= tmp;
  167. xdr += tmp >> 2;
  168. /* get the token count */
  169. if (datalen < 12)
  170. goto not_xdr;
  171. ntoken = ntohl(*xdr++);
  172. datalen -= 4;
  173. _debug("ntoken: %x", ntoken);
  174. if (ntoken < 1 || ntoken > AFSTOKEN_MAX)
  175. goto not_xdr;
  176. /* check each token wrapper */
  177. token = xdr;
  178. loop = ntoken;
  179. do {
  180. if (datalen < 8)
  181. goto not_xdr;
  182. toklen = ntohl(*xdr++);
  183. sec_ix = ntohl(*xdr);
  184. datalen -= 4;
  185. _debug("token: [%x/%zx] %x", toklen, datalen, sec_ix);
  186. if (toklen < 20 || toklen > datalen)
  187. goto not_xdr;
  188. datalen -= (toklen + 3) & ~3;
  189. xdr += (toklen + 3) >> 2;
  190. } while (--loop > 0);
  191. _debug("remainder: %zu", datalen);
  192. if (datalen != 0)
  193. goto not_xdr;
  194. /* okay: we're going to assume it's valid XDR format
  195. * - we ignore the cellname, relying on the key to be correctly named
  196. */
  197. do {
  198. xdr = token;
  199. toklen = ntohl(*xdr++);
  200. token = xdr + ((toklen + 3) >> 2);
  201. sec_ix = ntohl(*xdr++);
  202. toklen -= 4;
  203. switch (sec_ix) {
  204. case RXRPC_SECURITY_RXKAD:
  205. ret = rxrpc_instantiate_xdr_rxkad(key, xdr, toklen);
  206. if (ret != 0)
  207. goto error;
  208. break;
  209. default:
  210. ret = -EPROTONOSUPPORT;
  211. goto error;
  212. }
  213. } while (--ntoken > 0);
  214. _leave(" = 0");
  215. return 0;
  216. not_xdr:
  217. _leave(" = -EPROTO");
  218. return -EPROTO;
  219. error:
  220. _leave(" = %d", ret);
  221. return ret;
  222. }
  223. /*
  224. * instantiate an rxrpc defined key
  225. * data should be of the form:
  226. * OFFSET LEN CONTENT
  227. * 0 4 key interface version number
  228. * 4 2 security index (type)
  229. * 6 2 ticket length
  230. * 8 4 key expiry time (time_t)
  231. * 12 4 kvno
  232. * 16 8 session key
  233. * 24 [len] ticket
  234. *
  235. * if no data is provided, then a no-security key is made
  236. */
  237. static int rxrpc_instantiate(struct key *key, const void *data, size_t datalen)
  238. {
  239. const struct rxrpc_key_data_v1 *v1;
  240. struct rxrpc_key_token *token, **pp;
  241. size_t plen;
  242. u32 kver;
  243. int ret;
  244. _enter("{%x},,%zu", key_serial(key), datalen);
  245. /* handle a no-security key */
  246. if (!data && datalen == 0)
  247. return 0;
  248. /* determine if the XDR payload format is being used */
  249. if (datalen > 7 * 4) {
  250. ret = rxrpc_instantiate_xdr(key, data, datalen);
  251. if (ret != -EPROTO)
  252. return ret;
  253. }
  254. /* get the key interface version number */
  255. ret = -EINVAL;
  256. if (datalen <= 4 || !data)
  257. goto error;
  258. memcpy(&kver, data, sizeof(kver));
  259. data += sizeof(kver);
  260. datalen -= sizeof(kver);
  261. _debug("KEY I/F VERSION: %u", kver);
  262. ret = -EKEYREJECTED;
  263. if (kver != 1)
  264. goto error;
  265. /* deal with a version 1 key */
  266. ret = -EINVAL;
  267. if (datalen < sizeof(*v1))
  268. goto error;
  269. v1 = data;
  270. if (datalen != sizeof(*v1) + v1->ticket_length)
  271. goto error;
  272. _debug("SCIX: %u", v1->security_index);
  273. _debug("TLEN: %u", v1->ticket_length);
  274. _debug("EXPY: %x", v1->expiry);
  275. _debug("KVNO: %u", v1->kvno);
  276. _debug("SKEY: %02x%02x%02x%02x%02x%02x%02x%02x",
  277. v1->session_key[0], v1->session_key[1],
  278. v1->session_key[2], v1->session_key[3],
  279. v1->session_key[4], v1->session_key[5],
  280. v1->session_key[6], v1->session_key[7]);
  281. if (v1->ticket_length >= 8)
  282. _debug("TCKT: %02x%02x%02x%02x%02x%02x%02x%02x",
  283. v1->ticket[0], v1->ticket[1],
  284. v1->ticket[2], v1->ticket[3],
  285. v1->ticket[4], v1->ticket[5],
  286. v1->ticket[6], v1->ticket[7]);
  287. ret = -EPROTONOSUPPORT;
  288. if (v1->security_index != RXRPC_SECURITY_RXKAD)
  289. goto error;
  290. plen = sizeof(*token->kad) + v1->ticket_length;
  291. ret = key_payload_reserve(key, plen + sizeof(*token));
  292. if (ret < 0)
  293. goto error;
  294. ret = -ENOMEM;
  295. token = kmalloc(sizeof(*token), GFP_KERNEL);
  296. if (!token)
  297. goto error;
  298. token->kad = kmalloc(plen, GFP_KERNEL);
  299. if (!token->kad)
  300. goto error_free;
  301. token->security_index = RXRPC_SECURITY_RXKAD;
  302. token->kad->ticket_len = v1->ticket_length;
  303. token->kad->expiry = v1->expiry;
  304. token->kad->kvno = v1->kvno;
  305. memcpy(&token->kad->session_key, &v1->session_key, 8);
  306. memcpy(&token->kad->ticket, v1->ticket, v1->ticket_length);
  307. /* attach the data */
  308. key->type_data.x[0]++;
  309. pp = (struct rxrpc_key_token **)&key->payload.data;
  310. while (*pp)
  311. pp = &(*pp)->next;
  312. *pp = token;
  313. if (token->kad->expiry < key->expiry)
  314. key->expiry = token->kad->expiry;
  315. token = NULL;
  316. ret = 0;
  317. error_free:
  318. kfree(token);
  319. error:
  320. return ret;
  321. }
  322. /*
  323. * instantiate a server secret key
  324. * data should be a pointer to the 8-byte secret key
  325. */
  326. static int rxrpc_instantiate_s(struct key *key, const void *data,
  327. size_t datalen)
  328. {
  329. struct crypto_blkcipher *ci;
  330. _enter("{%x},,%zu", key_serial(key), datalen);
  331. if (datalen != 8)
  332. return -EINVAL;
  333. memcpy(&key->type_data, data, 8);
  334. ci = crypto_alloc_blkcipher("pcbc(des)", 0, CRYPTO_ALG_ASYNC);
  335. if (IS_ERR(ci)) {
  336. _leave(" = %ld", PTR_ERR(ci));
  337. return PTR_ERR(ci);
  338. }
  339. if (crypto_blkcipher_setkey(ci, data, 8) < 0)
  340. BUG();
  341. key->payload.data = ci;
  342. _leave(" = 0");
  343. return 0;
  344. }
  345. /*
  346. * dispose of the data dangling from the corpse of a rxrpc key
  347. */
  348. static void rxrpc_destroy(struct key *key)
  349. {
  350. struct rxrpc_key_token *token;
  351. while ((token = key->payload.data)) {
  352. key->payload.data = token->next;
  353. switch (token->security_index) {
  354. case RXRPC_SECURITY_RXKAD:
  355. kfree(token->kad);
  356. break;
  357. default:
  358. printk(KERN_ERR "Unknown token type %x on rxrpc key\n",
  359. token->security_index);
  360. BUG();
  361. }
  362. kfree(token);
  363. }
  364. }
  365. /*
  366. * dispose of the data dangling from the corpse of a rxrpc key
  367. */
  368. static void rxrpc_destroy_s(struct key *key)
  369. {
  370. if (key->payload.data) {
  371. crypto_free_blkcipher(key->payload.data);
  372. key->payload.data = NULL;
  373. }
  374. }
  375. /*
  376. * describe the rxrpc key
  377. */
  378. static void rxrpc_describe(const struct key *key, struct seq_file *m)
  379. {
  380. seq_puts(m, key->description);
  381. }
  382. /*
  383. * grab the security key for a socket
  384. */
  385. int rxrpc_request_key(struct rxrpc_sock *rx, char __user *optval, int optlen)
  386. {
  387. struct key *key;
  388. char *description;
  389. _enter("");
  390. if (optlen <= 0 || optlen > PAGE_SIZE - 1)
  391. return -EINVAL;
  392. description = kmalloc(optlen + 1, GFP_KERNEL);
  393. if (!description)
  394. return -ENOMEM;
  395. if (copy_from_user(description, optval, optlen)) {
  396. kfree(description);
  397. return -EFAULT;
  398. }
  399. description[optlen] = 0;
  400. key = request_key(&key_type_rxrpc, description, NULL);
  401. if (IS_ERR(key)) {
  402. kfree(description);
  403. _leave(" = %ld", PTR_ERR(key));
  404. return PTR_ERR(key);
  405. }
  406. rx->key = key;
  407. kfree(description);
  408. _leave(" = 0 [key %x]", key->serial);
  409. return 0;
  410. }
  411. /*
  412. * grab the security keyring for a server socket
  413. */
  414. int rxrpc_server_keyring(struct rxrpc_sock *rx, char __user *optval,
  415. int optlen)
  416. {
  417. struct key *key;
  418. char *description;
  419. _enter("");
  420. if (optlen <= 0 || optlen > PAGE_SIZE - 1)
  421. return -EINVAL;
  422. description = kmalloc(optlen + 1, GFP_KERNEL);
  423. if (!description)
  424. return -ENOMEM;
  425. if (copy_from_user(description, optval, optlen)) {
  426. kfree(description);
  427. return -EFAULT;
  428. }
  429. description[optlen] = 0;
  430. key = request_key(&key_type_keyring, description, NULL);
  431. if (IS_ERR(key)) {
  432. kfree(description);
  433. _leave(" = %ld", PTR_ERR(key));
  434. return PTR_ERR(key);
  435. }
  436. rx->securities = key;
  437. kfree(description);
  438. _leave(" = 0 [key %x]", key->serial);
  439. return 0;
  440. }
  441. /*
  442. * generate a server data key
  443. */
  444. int rxrpc_get_server_data_key(struct rxrpc_connection *conn,
  445. const void *session_key,
  446. time_t expiry,
  447. u32 kvno)
  448. {
  449. const struct cred *cred = current_cred();
  450. struct key *key;
  451. int ret;
  452. struct {
  453. u32 kver;
  454. struct rxrpc_key_data_v1 v1;
  455. } data;
  456. _enter("");
  457. key = key_alloc(&key_type_rxrpc, "x", 0, 0, cred, 0,
  458. KEY_ALLOC_NOT_IN_QUOTA);
  459. if (IS_ERR(key)) {
  460. _leave(" = -ENOMEM [alloc %ld]", PTR_ERR(key));
  461. return -ENOMEM;
  462. }
  463. _debug("key %d", key_serial(key));
  464. data.kver = 1;
  465. data.v1.security_index = RXRPC_SECURITY_RXKAD;
  466. data.v1.ticket_length = 0;
  467. data.v1.expiry = expiry;
  468. data.v1.kvno = 0;
  469. memcpy(&data.v1.session_key, session_key, sizeof(data.v1.session_key));
  470. ret = key_instantiate_and_link(key, &data, sizeof(data), NULL, NULL);
  471. if (ret < 0)
  472. goto error;
  473. conn->key = key;
  474. _leave(" = 0 [%d]", key_serial(key));
  475. return 0;
  476. error:
  477. key_revoke(key);
  478. key_put(key);
  479. _leave(" = -ENOMEM [ins %d]", ret);
  480. return -ENOMEM;
  481. }
  482. EXPORT_SYMBOL(rxrpc_get_server_data_key);
  483. /**
  484. * rxrpc_get_null_key - Generate a null RxRPC key
  485. * @keyname: The name to give the key.
  486. *
  487. * Generate a null RxRPC key that can be used to indicate anonymous security is
  488. * required for a particular domain.
  489. */
  490. struct key *rxrpc_get_null_key(const char *keyname)
  491. {
  492. const struct cred *cred = current_cred();
  493. struct key *key;
  494. int ret;
  495. key = key_alloc(&key_type_rxrpc, keyname, 0, 0, cred,
  496. KEY_POS_SEARCH, KEY_ALLOC_NOT_IN_QUOTA);
  497. if (IS_ERR(key))
  498. return key;
  499. ret = key_instantiate_and_link(key, NULL, 0, NULL, NULL);
  500. if (ret < 0) {
  501. key_revoke(key);
  502. key_put(key);
  503. return ERR_PTR(ret);
  504. }
  505. return key;
  506. }
  507. EXPORT_SYMBOL(rxrpc_get_null_key);
  508. /*
  509. * read the contents of an rxrpc key
  510. * - this returns the result in XDR form
  511. */
  512. static long rxrpc_read(const struct key *key,
  513. char __user *buffer, size_t buflen)
  514. {
  515. struct rxrpc_key_token *token;
  516. size_t size, toksize;
  517. __be32 __user *xdr;
  518. u32 cnlen, tktlen, ntoks, zero;
  519. _enter("");
  520. /* we don't know what form we should return non-AFS keys in */
  521. if (memcmp(key->description, "afs@", 4) != 0)
  522. return -EOPNOTSUPP;
  523. cnlen = strlen(key->description + 4);
  524. /* AFS keys we return in XDR form, so we need to work out the size of
  525. * the XDR */
  526. size = 2 * 4; /* flags, cellname len */
  527. size += (cnlen + 3) & ~3; /* cellname */
  528. size += 1 * 4; /* token count */
  529. ntoks = 0;
  530. for (token = key->payload.data; token; token = token->next) {
  531. switch (token->security_index) {
  532. case RXRPC_SECURITY_RXKAD:
  533. size += 2 * 4; /* length, security index (switch ID) */
  534. size += 8 * 4; /* viceid, kvno, key*2, begin, end,
  535. * primary, tktlen */
  536. size += (token->kad->ticket_len + 3) & ~3; /* ticket */
  537. ntoks++;
  538. break;
  539. default: /* can't encode */
  540. break;
  541. }
  542. }
  543. if (!buffer || buflen < size)
  544. return size;
  545. xdr = (__be32 __user *) buffer;
  546. zero = 0;
  547. #define ENCODE(x) \
  548. do { \
  549. __be32 y = htonl(x); \
  550. if (put_user(y, xdr++) < 0) \
  551. goto fault; \
  552. } while(0)
  553. ENCODE(0); /* flags */
  554. ENCODE(cnlen); /* cellname length */
  555. if (copy_to_user(xdr, key->description + 4, cnlen) != 0)
  556. goto fault;
  557. if (cnlen & 3 &&
  558. copy_to_user((u8 *)xdr + cnlen, &zero, 4 - (cnlen & 3)) != 0)
  559. goto fault;
  560. xdr += (cnlen + 3) >> 2;
  561. ENCODE(ntoks); /* token count */
  562. for (token = key->payload.data; token; token = token->next) {
  563. toksize = 1 * 4; /* sec index */
  564. switch (token->security_index) {
  565. case RXRPC_SECURITY_RXKAD:
  566. toksize += 8 * 4;
  567. toksize += (token->kad->ticket_len + 3) & ~3;
  568. ENCODE(toksize);
  569. ENCODE(token->security_index);
  570. ENCODE(token->kad->vice_id);
  571. ENCODE(token->kad->kvno);
  572. if (copy_to_user(xdr, token->kad->session_key, 8) != 0)
  573. goto fault;
  574. xdr += 8 >> 2;
  575. ENCODE(token->kad->start);
  576. ENCODE(token->kad->expiry);
  577. ENCODE(token->kad->primary_flag);
  578. tktlen = token->kad->ticket_len;
  579. ENCODE(tktlen);
  580. if (copy_to_user(xdr, token->kad->ticket, tktlen) != 0)
  581. goto fault;
  582. if (tktlen & 3 &&
  583. copy_to_user((u8 *)xdr + tktlen, &zero,
  584. 4 - (tktlen & 3)) != 0)
  585. goto fault;
  586. xdr += (tktlen + 3) >> 2;
  587. break;
  588. default:
  589. break;
  590. }
  591. }
  592. #undef ENCODE
  593. ASSERTCMP((char __user *) xdr - buffer, ==, size);
  594. _leave(" = %zu", size);
  595. return size;
  596. fault:
  597. _leave(" = -EFAULT");
  598. return -EFAULT;
  599. }