server_key.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* RxRPC key management
  3. *
  4. * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
  5. * Written by David Howells (dhowells@redhat.com)
  6. *
  7. * RxRPC keys should have a description of describing their purpose:
  8. * "afs@CAMBRIDGE.REDHAT.COM>
  9. */
  10. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  11. #include <crypto/skcipher.h>
  12. #include <linux/module.h>
  13. #include <linux/net.h>
  14. #include <linux/skbuff.h>
  15. #include <linux/key-type.h>
  16. #include <linux/ctype.h>
  17. #include <linux/slab.h>
  18. #include <net/sock.h>
  19. #include <net/af_rxrpc.h>
  20. #include <keys/rxrpc-type.h>
  21. #include <keys/user-type.h>
  22. #include "ar-internal.h"
  23. static int rxrpc_vet_description_s(const char *);
  24. static int rxrpc_preparse_s(struct key_preparsed_payload *);
  25. static void rxrpc_free_preparse_s(struct key_preparsed_payload *);
  26. static void rxrpc_destroy_s(struct key *);
  27. static void rxrpc_describe_s(const struct key *, struct seq_file *);
  28. /*
  29. * rxrpc server keys take "<serviceId>:<securityIndex>[:<sec-specific>]" as the
  30. * description and the key material as the payload.
  31. */
  32. struct key_type key_type_rxrpc_s = {
  33. .name = "rxrpc_s",
  34. .flags = KEY_TYPE_NET_DOMAIN,
  35. .vet_description = rxrpc_vet_description_s,
  36. .preparse = rxrpc_preparse_s,
  37. .free_preparse = rxrpc_free_preparse_s,
  38. .instantiate = generic_key_instantiate,
  39. .destroy = rxrpc_destroy_s,
  40. .describe = rxrpc_describe_s,
  41. };
  42. /*
  43. * Vet the description for an RxRPC server key.
  44. */
  45. static int rxrpc_vet_description_s(const char *desc)
  46. {
  47. unsigned long service, sec_class;
  48. char *p;
  49. service = simple_strtoul(desc, &p, 10);
  50. if (*p != ':' || service > 65535)
  51. return -EINVAL;
  52. sec_class = simple_strtoul(p + 1, &p, 10);
  53. if ((*p && *p != ':') || sec_class < 1 || sec_class > 255)
  54. return -EINVAL;
  55. return 0;
  56. }
  57. /*
  58. * Preparse a server secret key.
  59. */
  60. static int rxrpc_preparse_s(struct key_preparsed_payload *prep)
  61. {
  62. const struct rxrpc_security *sec;
  63. unsigned int service, sec_class;
  64. int n;
  65. _enter("%zu", prep->datalen);
  66. if (!prep->orig_description)
  67. return -EINVAL;
  68. if (sscanf(prep->orig_description, "%u:%u%n", &service, &sec_class, &n) != 2)
  69. return -EINVAL;
  70. sec = rxrpc_security_lookup(sec_class);
  71. if (!sec)
  72. return -ENOPKG;
  73. prep->payload.data[1] = (struct rxrpc_security *)sec;
  74. if (!sec->preparse_server_key)
  75. return -EINVAL;
  76. return sec->preparse_server_key(prep);
  77. }
  78. static void rxrpc_free_preparse_s(struct key_preparsed_payload *prep)
  79. {
  80. const struct rxrpc_security *sec = prep->payload.data[1];
  81. if (sec && sec->free_preparse_server_key)
  82. sec->free_preparse_server_key(prep);
  83. }
  84. static void rxrpc_destroy_s(struct key *key)
  85. {
  86. const struct rxrpc_security *sec = key->payload.data[1];
  87. if (sec && sec->destroy_server_key)
  88. sec->destroy_server_key(key);
  89. }
  90. static void rxrpc_describe_s(const struct key *key, struct seq_file *m)
  91. {
  92. const struct rxrpc_security *sec = key->payload.data[1];
  93. seq_puts(m, key->description);
  94. if (sec && sec->describe_server_key)
  95. sec->describe_server_key(key, m);
  96. }
  97. /*
  98. * grab the security keyring for a server socket
  99. */
  100. int rxrpc_server_keyring(struct rxrpc_sock *rx, sockptr_t optval, int optlen)
  101. {
  102. struct key *key;
  103. char *description;
  104. _enter("");
  105. if (rx->securities)
  106. return -EINVAL;
  107. if (optlen <= 0 || optlen > PAGE_SIZE - 1)
  108. return -EINVAL;
  109. description = memdup_sockptr_nul(optval, optlen);
  110. if (IS_ERR(description))
  111. return PTR_ERR(description);
  112. key = request_key(&key_type_keyring, description, NULL);
  113. if (IS_ERR(key)) {
  114. kfree(description);
  115. _leave(" = %ld", PTR_ERR(key));
  116. return PTR_ERR(key);
  117. }
  118. rx->securities = key;
  119. kfree(description);
  120. _leave(" = 0 [key %x]", key->serial);
  121. return 0;
  122. }
  123. /**
  124. * rxrpc_sock_set_security_keyring - Set the security keyring for a kernel service
  125. * @sk: The socket to set the keyring on
  126. * @keyring: The keyring to set
  127. *
  128. * Set the server security keyring on an rxrpc socket. This is used to provide
  129. * the encryption keys for a kernel service.
  130. *
  131. * Return: %0 if successful and a negative error code otherwise.
  132. */
  133. int rxrpc_sock_set_security_keyring(struct sock *sk, struct key *keyring)
  134. {
  135. struct rxrpc_sock *rx = rxrpc_sk(sk);
  136. int ret = 0;
  137. lock_sock(sk);
  138. if (rx->securities)
  139. ret = -EINVAL;
  140. else if (rx->sk.sk_state != RXRPC_UNBOUND)
  141. ret = -EISCONN;
  142. else
  143. rx->securities = key_get(keyring);
  144. release_sock(sk);
  145. return ret;
  146. }
  147. EXPORT_SYMBOL(rxrpc_sock_set_security_keyring);
  148. /**
  149. * rxrpc_sock_set_manage_response - Set the manage-response flag for a kernel service
  150. * @sk: The socket to set the keyring on
  151. * @set: True to set, false to clear the flag
  152. *
  153. * Set the flag on an rxrpc socket to say that the caller wants to manage the
  154. * RESPONSE packet and the user-defined data it may contain. Setting this
  155. * means that recvmsg() will return messages with RXRPC_CHALLENGED in the
  156. * control message buffer containing information about the challenge.
  157. *
  158. * The user should respond to the challenge by passing RXRPC_RESPOND or
  159. * RXRPC_RESPOND_ABORT control messages with sendmsg() to the same call.
  160. * Supplementary control messages, such as RXRPC_RESP_RXGK_APPDATA, may be
  161. * included to indicate the parts the user wants to supply.
  162. *
  163. * The server will be passed the response data with a RXRPC_RESPONDED control
  164. * message when it gets the first data from each call.
  165. *
  166. * Note that this is only honoured by security classes that need auxiliary data
  167. * (e.g. RxGK). Those that don't offer the facility (e.g. RxKAD) respond
  168. * without consulting userspace.
  169. *
  170. * Return: The previous setting.
  171. */
  172. int rxrpc_sock_set_manage_response(struct sock *sk, bool set)
  173. {
  174. struct rxrpc_sock *rx = rxrpc_sk(sk);
  175. int ret;
  176. lock_sock(sk);
  177. ret = !!test_bit(RXRPC_SOCK_MANAGE_RESPONSE, &rx->flags);
  178. if (set)
  179. set_bit(RXRPC_SOCK_MANAGE_RESPONSE, &rx->flags);
  180. else
  181. clear_bit(RXRPC_SOCK_MANAGE_RESPONSE, &rx->flags);
  182. release_sock(sk);
  183. return ret;
  184. }
  185. EXPORT_SYMBOL(rxrpc_sock_set_manage_response);