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 | |
| 22 | static LIST_HEAD(rxrpc_security_methods); |
| 23 | static DECLARE_RWSEM(rxrpc_security_sem); |
| 24 | |
David Howells | 648af7f | 2016-04-07 17:23:51 +0100 | [diff] [blame^] | 25 | static const struct rxrpc_security *rxrpc_security_types[] = { |
| 26 | #ifdef CONFIG_RXKAD |
| 27 | [RXRPC_SECURITY_RXKAD] = &rxkad, |
| 28 | #endif |
| 29 | }; |
| 30 | |
| 31 | int __init rxrpc_init_security(void) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 32 | { |
David Howells | 648af7f | 2016-04-07 17:23:51 +0100 | [diff] [blame^] | 33 | int i, ret; |
| 34 | |
| 35 | for (i = 0; i < ARRAY_SIZE(rxrpc_security_types); i++) { |
| 36 | if (rxrpc_security_types[i]) { |
| 37 | ret = rxrpc_security_types[i]->init(); |
| 38 | if (ret < 0) |
| 39 | goto failed; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | return 0; |
| 44 | |
| 45 | failed: |
| 46 | for (i--; i >= 0; i--) |
| 47 | if (rxrpc_security_types[i]) |
| 48 | rxrpc_security_types[i]->exit(); |
| 49 | return ret; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 50 | } |
| 51 | |
David Howells | 648af7f | 2016-04-07 17:23:51 +0100 | [diff] [blame^] | 52 | void rxrpc_exit_security(void) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 53 | { |
David Howells | 648af7f | 2016-04-07 17:23:51 +0100 | [diff] [blame^] | 54 | int i; |
| 55 | |
| 56 | for (i = 0; i < ARRAY_SIZE(rxrpc_security_types); i++) |
| 57 | if (rxrpc_security_types[i]) |
| 58 | rxrpc_security_types[i]->exit(); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | /* |
| 62 | * look up an rxrpc security module |
| 63 | */ |
David Howells | 648af7f | 2016-04-07 17:23:51 +0100 | [diff] [blame^] | 64 | static const struct rxrpc_security *rxrpc_security_lookup(u8 security_index) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 65 | { |
David Howells | 648af7f | 2016-04-07 17:23:51 +0100 | [diff] [blame^] | 66 | if (security_index >= ARRAY_SIZE(rxrpc_security_types)) |
| 67 | return NULL; |
| 68 | return rxrpc_security_types[security_index]; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 69 | } |
| 70 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 71 | /* |
| 72 | * initialise the security on a client connection |
| 73 | */ |
| 74 | int rxrpc_init_client_conn_security(struct rxrpc_connection *conn) |
| 75 | { |
David Howells | 648af7f | 2016-04-07 17:23:51 +0100 | [diff] [blame^] | 76 | const struct rxrpc_security *sec; |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 77 | struct rxrpc_key_token *token; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 78 | struct key *key = conn->key; |
| 79 | int ret; |
| 80 | |
| 81 | _enter("{%d},{%x}", conn->debug_id, key_serial(key)); |
| 82 | |
| 83 | if (!key) |
| 84 | return 0; |
| 85 | |
| 86 | ret = key_validate(key); |
| 87 | if (ret < 0) |
| 88 | return ret; |
| 89 | |
David Howells | 146aa8b | 2015-10-21 14:04:48 +0100 | [diff] [blame] | 90 | token = key->payload.data[0]; |
| 91 | if (!token) |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 92 | return -EKEYREJECTED; |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 93 | |
| 94 | sec = rxrpc_security_lookup(token->security_index); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 95 | if (!sec) |
| 96 | return -EKEYREJECTED; |
| 97 | conn->security = sec; |
| 98 | |
| 99 | ret = conn->security->init_connection_security(conn); |
| 100 | if (ret < 0) { |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 101 | conn->security = NULL; |
| 102 | return ret; |
| 103 | } |
| 104 | |
| 105 | _leave(" = 0"); |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | /* |
| 110 | * initialise the security on a server connection |
| 111 | */ |
| 112 | int rxrpc_init_server_conn_security(struct rxrpc_connection *conn) |
| 113 | { |
David Howells | 648af7f | 2016-04-07 17:23:51 +0100 | [diff] [blame^] | 114 | const struct rxrpc_security *sec; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 115 | struct rxrpc_local *local = conn->trans->local; |
| 116 | struct rxrpc_sock *rx; |
| 117 | struct key *key; |
| 118 | key_ref_t kref; |
David Howells | b4f1342 | 2016-03-04 15:56:19 +0000 | [diff] [blame] | 119 | char kdesc[5 + 1 + 3 + 1]; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 120 | |
| 121 | _enter(""); |
| 122 | |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 123 | sprintf(kdesc, "%u:%u", conn->service_id, conn->security_ix); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 124 | |
| 125 | sec = rxrpc_security_lookup(conn->security_ix); |
| 126 | if (!sec) { |
| 127 | _leave(" = -ENOKEY [lookup]"); |
| 128 | return -ENOKEY; |
| 129 | } |
| 130 | |
| 131 | /* find the service */ |
| 132 | read_lock_bh(&local->services_lock); |
| 133 | list_for_each_entry(rx, &local->services, listen_link) { |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 134 | if (rx->srx.srx_service == conn->service_id) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 135 | goto found_service; |
| 136 | } |
| 137 | |
| 138 | /* the service appears to have died */ |
| 139 | read_unlock_bh(&local->services_lock); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 140 | _leave(" = -ENOENT"); |
| 141 | return -ENOENT; |
| 142 | |
| 143 | found_service: |
| 144 | if (!rx->securities) { |
| 145 | read_unlock_bh(&local->services_lock); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 146 | _leave(" = -ENOKEY"); |
| 147 | return -ENOKEY; |
| 148 | } |
| 149 | |
| 150 | /* look through the service's keyring */ |
| 151 | kref = keyring_search(make_key_ref(rx->securities, 1UL), |
| 152 | &key_type_rxrpc_s, kdesc); |
| 153 | if (IS_ERR(kref)) { |
| 154 | read_unlock_bh(&local->services_lock); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 155 | _leave(" = %ld [search]", PTR_ERR(kref)); |
| 156 | return PTR_ERR(kref); |
| 157 | } |
| 158 | |
| 159 | key = key_ref_to_ptr(kref); |
| 160 | read_unlock_bh(&local->services_lock); |
| 161 | |
| 162 | conn->server_key = key; |
| 163 | conn->security = sec; |
| 164 | |
| 165 | _leave(" = 0"); |
| 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | /* |
| 170 | * secure a packet prior to transmission |
| 171 | */ |
| 172 | int rxrpc_secure_packet(const struct rxrpc_call *call, |
| 173 | struct sk_buff *skb, |
| 174 | size_t data_size, |
| 175 | void *sechdr) |
| 176 | { |
| 177 | if (call->conn->security) |
| 178 | return call->conn->security->secure_packet( |
| 179 | call, skb, data_size, sechdr); |
| 180 | return 0; |
| 181 | } |
| 182 | |
| 183 | /* |
| 184 | * secure a packet prior to transmission |
| 185 | */ |
| 186 | int rxrpc_verify_packet(const struct rxrpc_call *call, struct sk_buff *skb, |
| 187 | u32 *_abort_code) |
| 188 | { |
| 189 | if (call->conn->security) |
| 190 | return call->conn->security->verify_packet( |
| 191 | call, skb, _abort_code); |
| 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | /* |
| 196 | * clear connection security |
| 197 | */ |
| 198 | void rxrpc_clear_conn_security(struct rxrpc_connection *conn) |
| 199 | { |
| 200 | _enter("{%d}", conn->debug_id); |
| 201 | |
David Howells | 648af7f | 2016-04-07 17:23:51 +0100 | [diff] [blame^] | 202 | if (conn->security) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 203 | conn->security->clear(conn); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 204 | |
| 205 | key_put(conn->key); |
| 206 | key_put(conn->server_key); |
| 207 | } |