security.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. };
  22. int __init rxrpc_init_security(void)
  23. {
  24. int i, ret;
  25. for (i = 0; i < ARRAY_SIZE(rxrpc_security_types); i++) {
  26. if (rxrpc_security_types[i]) {
  27. ret = rxrpc_security_types[i]->init();
  28. if (ret < 0)
  29. goto failed;
  30. }
  31. }
  32. return 0;
  33. failed:
  34. for (i--; i >= 0; i--)
  35. if (rxrpc_security_types[i])
  36. rxrpc_security_types[i]->exit();
  37. return ret;
  38. }
  39. void rxrpc_exit_security(void)
  40. {
  41. int i;
  42. for (i = 0; i < ARRAY_SIZE(rxrpc_security_types); i++)
  43. if (rxrpc_security_types[i])
  44. rxrpc_security_types[i]->exit();
  45. }
  46. /*
  47. * look up an rxrpc security module
  48. */
  49. const struct rxrpc_security *rxrpc_security_lookup(u8 security_index)
  50. {
  51. if (security_index >= ARRAY_SIZE(rxrpc_security_types))
  52. return NULL;
  53. return rxrpc_security_types[security_index];
  54. }
  55. /*
  56. * Initialise the security on a client call.
  57. */
  58. int rxrpc_init_client_call_security(struct rxrpc_call *call)
  59. {
  60. const struct rxrpc_security *sec = &rxrpc_no_security;
  61. struct rxrpc_key_token *token;
  62. struct key *key = call->key;
  63. int ret;
  64. if (!key)
  65. goto found;
  66. ret = key_validate(key);
  67. if (ret < 0)
  68. return ret;
  69. for (token = key->payload.data[0]; token; token = token->next) {
  70. sec = rxrpc_security_lookup(token->security_index);
  71. if (sec)
  72. goto found;
  73. }
  74. return -EKEYREJECTED;
  75. found:
  76. call->security = sec;
  77. call->security_ix = sec->security_index;
  78. return 0;
  79. }
  80. /*
  81. * initialise the security on a client connection
  82. */
  83. int rxrpc_init_client_conn_security(struct rxrpc_connection *conn)
  84. {
  85. struct rxrpc_key_token *token;
  86. struct key *key = conn->key;
  87. int ret = 0;
  88. _enter("{%d},{%x}", conn->debug_id, key_serial(key));
  89. for (token = key->payload.data[0]; token; token = token->next) {
  90. if (token->security_index == conn->security->security_index)
  91. goto found;
  92. }
  93. return -EKEYREJECTED;
  94. found:
  95. mutex_lock(&conn->security_lock);
  96. if (conn->state == RXRPC_CONN_CLIENT_UNSECURED) {
  97. ret = conn->security->init_connection_security(conn, token);
  98. if (ret == 0) {
  99. spin_lock(&conn->state_lock);
  100. if (conn->state == RXRPC_CONN_CLIENT_UNSECURED)
  101. conn->state = RXRPC_CONN_CLIENT;
  102. spin_unlock(&conn->state_lock);
  103. }
  104. }
  105. mutex_unlock(&conn->security_lock);
  106. return ret;
  107. }
  108. /*
  109. * Set the ops a server connection.
  110. */
  111. const struct rxrpc_security *rxrpc_get_incoming_security(struct rxrpc_sock *rx,
  112. struct sk_buff *skb)
  113. {
  114. const struct rxrpc_security *sec;
  115. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  116. _enter("");
  117. sec = rxrpc_security_lookup(sp->hdr.securityIndex);
  118. if (!sec) {
  119. rxrpc_direct_abort(skb, rxrpc_abort_unsupported_security,
  120. RX_INVALID_OPERATION, -EKEYREJECTED);
  121. return NULL;
  122. }
  123. if (sp->hdr.securityIndex != RXRPC_SECURITY_NONE &&
  124. !rx->securities) {
  125. rxrpc_direct_abort(skb, rxrpc_abort_no_service_key,
  126. sec->no_key_abort, -EKEYREJECTED);
  127. return NULL;
  128. }
  129. return sec;
  130. }
  131. /*
  132. * Find the security key for a server connection.
  133. */
  134. struct key *rxrpc_look_up_server_security(struct rxrpc_connection *conn,
  135. struct sk_buff *skb,
  136. u32 kvno, u32 enctype)
  137. {
  138. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  139. struct rxrpc_sock *rx;
  140. struct key *key = ERR_PTR(-EKEYREJECTED);
  141. key_ref_t kref = NULL;
  142. char kdesc[5 + 1 + 3 + 1 + 12 + 1 + 12 + 1];
  143. int ret;
  144. _enter("");
  145. if (enctype)
  146. sprintf(kdesc, "%u:%u:%u:%u",
  147. sp->hdr.serviceId, sp->hdr.securityIndex, kvno, enctype);
  148. else if (kvno)
  149. sprintf(kdesc, "%u:%u:%u",
  150. sp->hdr.serviceId, sp->hdr.securityIndex, kvno);
  151. else
  152. sprintf(kdesc, "%u:%u",
  153. sp->hdr.serviceId, sp->hdr.securityIndex);
  154. read_lock(&conn->local->services_lock);
  155. rx = conn->local->service;
  156. if (!rx)
  157. goto out;
  158. /* look through the service's keyring */
  159. kref = keyring_search(make_key_ref(rx->securities, 1UL),
  160. &key_type_rxrpc_s, kdesc, true);
  161. if (IS_ERR(kref)) {
  162. key = ERR_CAST(kref);
  163. goto out;
  164. }
  165. key = key_ref_to_ptr(kref);
  166. ret = key_validate(key);
  167. if (ret < 0) {
  168. key_put(key);
  169. key = ERR_PTR(ret);
  170. goto out;
  171. }
  172. out:
  173. read_unlock(&conn->local->services_lock);
  174. return key;
  175. }