David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1 | /* RxRPC security handling |
| 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 | |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/net.h> |
| 14 | #include <linux/skbuff.h> |
| 15 | #include <linux/udp.h> |
| 16 | #include <linux/crypto.h> |
| 17 | #include <net/sock.h> |
| 18 | #include <net/af_rxrpc.h> |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 19 | #include <keys/rxrpc-type.h> |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 20 | #include "ar-internal.h" |
| 21 | |
David Howells | 648af7f | 2016-04-07 17:23:51 +0100 | [diff] [blame] | 22 | static const struct rxrpc_security *rxrpc_security_types[] = { |
David Howells | e0e4d82 | 2016-04-07 17:23:58 +0100 | [diff] [blame] | 23 | [RXRPC_SECURITY_NONE] = &rxrpc_no_security, |
David Howells | 648af7f | 2016-04-07 17:23:51 +0100 | [diff] [blame] | 24 | #ifdef CONFIG_RXKAD |
| 25 | [RXRPC_SECURITY_RXKAD] = &rxkad, |
| 26 | #endif |
| 27 | }; |
| 28 | |
| 29 | int __init rxrpc_init_security(void) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 30 | { |
David Howells | 648af7f | 2016-04-07 17:23:51 +0100 | [diff] [blame] | 31 | int i, ret; |
| 32 | |
| 33 | for (i = 0; i < ARRAY_SIZE(rxrpc_security_types); i++) { |
| 34 | if (rxrpc_security_types[i]) { |
| 35 | ret = rxrpc_security_types[i]->init(); |
| 36 | if (ret < 0) |
| 37 | goto failed; |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | return 0; |
| 42 | |
| 43 | failed: |
| 44 | for (i--; i >= 0; i--) |
| 45 | if (rxrpc_security_types[i]) |
| 46 | rxrpc_security_types[i]->exit(); |
| 47 | return ret; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 48 | } |
| 49 | |
David Howells | 648af7f | 2016-04-07 17:23:51 +0100 | [diff] [blame] | 50 | void rxrpc_exit_security(void) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 51 | { |
David Howells | 648af7f | 2016-04-07 17:23:51 +0100 | [diff] [blame] | 52 | int i; |
| 53 | |
| 54 | for (i = 0; i < ARRAY_SIZE(rxrpc_security_types); i++) |
| 55 | if (rxrpc_security_types[i]) |
| 56 | rxrpc_security_types[i]->exit(); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | /* |
| 60 | * look up an rxrpc security module |
| 61 | */ |
David Howells | 648af7f | 2016-04-07 17:23:51 +0100 | [diff] [blame] | 62 | static const struct rxrpc_security *rxrpc_security_lookup(u8 security_index) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 63 | { |
David Howells | 648af7f | 2016-04-07 17:23:51 +0100 | [diff] [blame] | 64 | if (security_index >= ARRAY_SIZE(rxrpc_security_types)) |
| 65 | return NULL; |
| 66 | return rxrpc_security_types[security_index]; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 67 | } |
| 68 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 69 | /* |
| 70 | * initialise the security on a client connection |
| 71 | */ |
| 72 | int rxrpc_init_client_conn_security(struct rxrpc_connection *conn) |
| 73 | { |
David Howells | 648af7f | 2016-04-07 17:23:51 +0100 | [diff] [blame] | 74 | const struct rxrpc_security *sec; |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 75 | struct rxrpc_key_token *token; |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 76 | struct key *key = conn->params.key; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 77 | int ret; |
| 78 | |
| 79 | _enter("{%d},{%x}", conn->debug_id, key_serial(key)); |
| 80 | |
| 81 | if (!key) |
| 82 | return 0; |
| 83 | |
| 84 | ret = key_validate(key); |
| 85 | if (ret < 0) |
| 86 | return ret; |
| 87 | |
David Howells | 146aa8b | 2015-10-21 14:04:48 +0100 | [diff] [blame] | 88 | token = key->payload.data[0]; |
| 89 | if (!token) |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 90 | return -EKEYREJECTED; |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 91 | |
| 92 | sec = rxrpc_security_lookup(token->security_index); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 93 | if (!sec) |
| 94 | return -EKEYREJECTED; |
| 95 | conn->security = sec; |
| 96 | |
| 97 | ret = conn->security->init_connection_security(conn); |
| 98 | if (ret < 0) { |
David Howells | e0e4d82 | 2016-04-07 17:23:58 +0100 | [diff] [blame] | 99 | conn->security = &rxrpc_no_security; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 100 | return ret; |
| 101 | } |
| 102 | |
| 103 | _leave(" = 0"); |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | /* |
| 108 | * initialise the security on a server connection |
| 109 | */ |
| 110 | int rxrpc_init_server_conn_security(struct rxrpc_connection *conn) |
| 111 | { |
David Howells | 648af7f | 2016-04-07 17:23:51 +0100 | [diff] [blame] | 112 | const struct rxrpc_security *sec; |
David Howells | 85f3227 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 113 | struct rxrpc_local *local = conn->params.local; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 114 | struct rxrpc_sock *rx; |
| 115 | struct key *key; |
| 116 | key_ref_t kref; |
David Howells | b4f1342 | 2016-03-04 15:56:19 +0000 | [diff] [blame] | 117 | char kdesc[5 + 1 + 3 + 1]; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 118 | |
| 119 | _enter(""); |
| 120 | |
David Howells | 68d6d1a | 2017-06-05 14:30:49 +0100 | [diff] [blame] | 121 | sprintf(kdesc, "%u:%u", conn->service_id, conn->security_ix); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 122 | |
| 123 | sec = rxrpc_security_lookup(conn->security_ix); |
| 124 | if (!sec) { |
| 125 | _leave(" = -ENOKEY [lookup]"); |
| 126 | return -ENOKEY; |
| 127 | } |
| 128 | |
| 129 | /* find the service */ |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 130 | read_lock(&local->services_lock); |
David Howells | 1e9e5c9 | 2016-09-29 22:37:15 +0100 | [diff] [blame] | 131 | rx = rcu_dereference_protected(local->service, |
| 132 | lockdep_is_held(&local->services_lock)); |
David Howells | 28036f4 | 2017-06-05 14:30:49 +0100 | [diff] [blame] | 133 | if (rx && (rx->srx.srx_service == conn->service_id || |
| 134 | rx->second_service == conn->service_id)) |
David Howells | 1e9e5c9 | 2016-09-29 22:37:15 +0100 | [diff] [blame] | 135 | goto found_service; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 136 | |
| 137 | /* the service appears to have died */ |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 138 | read_unlock(&local->services_lock); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 139 | _leave(" = -ENOENT"); |
| 140 | return -ENOENT; |
| 141 | |
| 142 | found_service: |
| 143 | if (!rx->securities) { |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 144 | read_unlock(&local->services_lock); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 145 | _leave(" = -ENOKEY"); |
| 146 | return -ENOKEY; |
| 147 | } |
| 148 | |
| 149 | /* look through the service's keyring */ |
| 150 | kref = keyring_search(make_key_ref(rx->securities, 1UL), |
| 151 | &key_type_rxrpc_s, kdesc); |
| 152 | if (IS_ERR(kref)) { |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 153 | read_unlock(&local->services_lock); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 154 | _leave(" = %ld [search]", PTR_ERR(kref)); |
| 155 | return PTR_ERR(kref); |
| 156 | } |
| 157 | |
| 158 | key = key_ref_to_ptr(kref); |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 159 | read_unlock(&local->services_lock); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 160 | |
| 161 | conn->server_key = key; |
| 162 | conn->security = sec; |
| 163 | |
| 164 | _leave(" = 0"); |
| 165 | return 0; |
| 166 | } |