security.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* RxRPC security handling
  3. *
  4. * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
  5. * Written by David Howells (dhowells@redhat.com)
  6. */
  7. #include <linux/module.h>
  8. #include <linux/net.h>
  9. #include <linux/skbuff.h>
  10. #include <linux/udp.h>
  11. #include <linux/crypto.h>
  12. #include <net/sock.h>
  13. #include <net/af_rxrpc.h>
  14. #include <keys/rxrpc-type.h>
  15. #include "ar-internal.h"
  16. static const struct rxrpc_security *rxrpc_security_types[] = {
  17. [RXRPC_SECURITY_NONE] = &rxrpc_no_security,
  18. #ifdef CONFIG_RXKAD
  19. [RXRPC_SECURITY_RXKAD] = &rxkad,
  20. #endif
  21. #ifdef CONFIG_RXGK
  22. [RXRPC_SECURITY_YFS_RXGK] = &rxgk_yfs,
  23. #endif
  24. };
  25. int __init rxrpc_init_security(void)
  26. {
  27. int i, ret;
  28. for (i = 0; i < ARRAY_SIZE(rxrpc_security_types); i++) {
  29. if (rxrpc_security_types[i]) {
  30. ret = rxrpc_security_types[i]->init();
  31. if (ret < 0)
  32. goto failed;
  33. }
  34. }
  35. return 0;
  36. failed:
  37. for (i--; i >= 0; i--)
  38. if (rxrpc_security_types[i])
  39. rxrpc_security_types[i]->exit();
  40. return ret;
  41. }
  42. void rxrpc_exit_security(void)
  43. {
  44. int i;
  45. for (i = 0; i < ARRAY_SIZE(rxrpc_security_types); i++)
  46. if (rxrpc_security_types[i])
  47. rxrpc_security_types[i]->exit();
  48. }
  49. /*
  50. * look up an rxrpc security module
  51. */
  52. const struct rxrpc_security *rxrpc_security_lookup(u8 security_index)
  53. {
  54. if (security_index >= ARRAY_SIZE(rxrpc_security_types))
  55. return NULL;
  56. return rxrpc_security_types[security_index];
  57. }
  58. /*
  59. * Initialise the security on a client call.
  60. */
  61. int rxrpc_init_client_call_security(struct rxrpc_call *call)
  62. {
  63. const struct rxrpc_security *sec = &rxrpc_no_security;
  64. struct rxrpc_key_token *token;
  65. struct key *key = call->key;
  66. int ret;
  67. if (!key)
  68. goto found;
  69. ret = key_validate(key);
  70. if (ret < 0)
  71. return ret;
  72. for (token = key->payload.data[0]; token; token = token->next) {
  73. sec = rxrpc_security_lookup(token->security_index);
  74. if (sec)
  75. goto found;
  76. }
  77. return -EKEYREJECTED;
  78. found:
  79. call->security = sec;
  80. call->security_ix = sec->security_index;
  81. return 0;
  82. }
  83. /*
  84. * initialise the security on a client connection
  85. */
  86. int rxrpc_init_client_conn_security(struct rxrpc_connection *conn)
  87. {
  88. struct rxrpc_key_token *token;
  89. struct key *key = conn->key;
  90. int ret = 0;
  91. _enter("{%d},{%x}", conn->debug_id, key_serial(key));
  92. for (token = key->payload.data[0]; token; token = token->next) {
  93. if (token->security_index == conn->security->security_index)
  94. goto found;
  95. }
  96. return -EKEYREJECTED;
  97. found:
  98. mutex_lock(&conn->security_lock);
  99. if (conn->state == RXRPC_CONN_CLIENT_UNSECURED) {
  100. ret = conn->security->init_connection_security(conn, token);
  101. if (ret == 0) {
  102. spin_lock_irq(&conn->state_lock);
  103. if (conn->state == RXRPC_CONN_CLIENT_UNSECURED)
  104. conn->state = RXRPC_CONN_CLIENT;
  105. spin_unlock_irq(&conn->state_lock);
  106. }
  107. }
  108. mutex_unlock(&conn->security_lock);
  109. return ret;
  110. }
  111. /*
  112. * Set the ops a server connection.
  113. */
  114. const struct rxrpc_security *rxrpc_get_incoming_security(struct rxrpc_sock *rx,
  115. struct sk_buff *skb)
  116. {
  117. const struct rxrpc_security *sec;
  118. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  119. _enter("");
  120. sec = rxrpc_security_lookup(sp->hdr.securityIndex);
  121. if (!sec) {
  122. rxrpc_direct_conn_abort(skb, rxrpc_abort_unsupported_security,
  123. RX_INVALID_OPERATION, -EKEYREJECTED);
  124. return NULL;
  125. }
  126. if (sp->hdr.securityIndex != RXRPC_SECURITY_NONE &&
  127. !rx->securities) {
  128. rxrpc_direct_conn_abort(skb, rxrpc_abort_no_service_key,
  129. sec->no_key_abort, -EKEYREJECTED);
  130. return NULL;
  131. }
  132. return sec;
  133. }
  134. /*
  135. * Find the security key for a server connection.
  136. */
  137. struct key *rxrpc_look_up_server_security(struct rxrpc_connection *conn,
  138. struct sk_buff *skb,
  139. u32 kvno, u32 enctype)
  140. {
  141. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  142. struct rxrpc_sock *rx;
  143. struct key *key = ERR_PTR(-EKEYREJECTED);
  144. key_ref_t kref = NULL;
  145. char kdesc[5 + 1 + 3 + 1 + 12 + 1 + 12 + 1];
  146. int ret;
  147. _enter("");
  148. if (enctype)
  149. sprintf(kdesc, "%u:%u:%u:%u",
  150. sp->hdr.serviceId, sp->hdr.securityIndex, kvno, enctype);
  151. else if (kvno)
  152. sprintf(kdesc, "%u:%u:%u",
  153. sp->hdr.serviceId, sp->hdr.securityIndex, kvno);
  154. else
  155. sprintf(kdesc, "%u:%u",
  156. sp->hdr.serviceId, sp->hdr.securityIndex);
  157. read_lock(&conn->local->services_lock);
  158. rx = conn->local->service;
  159. if (!rx)
  160. goto out;
  161. /* look through the service's keyring */
  162. kref = keyring_search(make_key_ref(rx->securities, 1UL),
  163. &key_type_rxrpc_s, kdesc, true);
  164. if (IS_ERR(kref)) {
  165. key = ERR_CAST(kref);
  166. goto out;
  167. }
  168. key = key_ref_to_ptr(kref);
  169. ret = key_validate(key);
  170. if (ret < 0) {
  171. key_put(key);
  172. key = ERR_PTR(ret);
  173. goto out;
  174. }
  175. out:
  176. read_unlock(&conn->local->services_lock);
  177. return key;
  178. }