blob: a4c47d2b705478eabf87b699da6852207f525f56 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
David Howells17926a72007-04-26 15:48:28 -07002/* RxRPC security handling
3 *
4 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
David Howells17926a72007-04-26 15:48:28 -07006 */
7
8#include <linux/module.h>
9#include <linux/net.h>
10#include <linux/skbuff.h>
11#include <linux/udp.h>
12#include <linux/crypto.h>
13#include <net/sock.h>
14#include <net/af_rxrpc.h>
David Howells33941282009-09-14 01:17:35 +000015#include <keys/rxrpc-type.h>
David Howells17926a72007-04-26 15:48:28 -070016#include "ar-internal.h"
17
David Howells648af7f2016-04-07 17:23:51 +010018static const struct rxrpc_security *rxrpc_security_types[] = {
David Howellse0e4d822016-04-07 17:23:58 +010019 [RXRPC_SECURITY_NONE] = &rxrpc_no_security,
David Howells648af7f2016-04-07 17:23:51 +010020#ifdef CONFIG_RXKAD
21 [RXRPC_SECURITY_RXKAD] = &rxkad,
22#endif
23};
24
25int __init rxrpc_init_security(void)
David Howells17926a72007-04-26 15:48:28 -070026{
David Howells648af7f2016-04-07 17:23:51 +010027 int i, ret;
28
29 for (i = 0; i < ARRAY_SIZE(rxrpc_security_types); i++) {
30 if (rxrpc_security_types[i]) {
31 ret = rxrpc_security_types[i]->init();
32 if (ret < 0)
33 goto failed;
34 }
35 }
36
37 return 0;
38
39failed:
40 for (i--; i >= 0; i--)
41 if (rxrpc_security_types[i])
42 rxrpc_security_types[i]->exit();
43 return ret;
David Howells17926a72007-04-26 15:48:28 -070044}
45
David Howells648af7f2016-04-07 17:23:51 +010046void rxrpc_exit_security(void)
David Howells17926a72007-04-26 15:48:28 -070047{
David Howells648af7f2016-04-07 17:23:51 +010048 int i;
49
50 for (i = 0; i < ARRAY_SIZE(rxrpc_security_types); i++)
51 if (rxrpc_security_types[i])
52 rxrpc_security_types[i]->exit();
David Howells17926a72007-04-26 15:48:28 -070053}
54
55/*
56 * look up an rxrpc security module
57 */
David Howells648af7f2016-04-07 17:23:51 +010058static const struct rxrpc_security *rxrpc_security_lookup(u8 security_index)
David Howells17926a72007-04-26 15:48:28 -070059{
David Howells648af7f2016-04-07 17:23:51 +010060 if (security_index >= ARRAY_SIZE(rxrpc_security_types))
61 return NULL;
62 return rxrpc_security_types[security_index];
David Howells17926a72007-04-26 15:48:28 -070063}
64
David Howells17926a72007-04-26 15:48:28 -070065/*
66 * initialise the security on a client connection
67 */
68int rxrpc_init_client_conn_security(struct rxrpc_connection *conn)
69{
David Howells648af7f2016-04-07 17:23:51 +010070 const struct rxrpc_security *sec;
David Howells33941282009-09-14 01:17:35 +000071 struct rxrpc_key_token *token;
David Howells19ffa012016-04-04 14:00:36 +010072 struct key *key = conn->params.key;
David Howells17926a72007-04-26 15:48:28 -070073 int ret;
74
75 _enter("{%d},{%x}", conn->debug_id, key_serial(key));
76
77 if (!key)
78 return 0;
79
80 ret = key_validate(key);
81 if (ret < 0)
82 return ret;
83
David Howells146aa8b2015-10-21 14:04:48 +010084 token = key->payload.data[0];
85 if (!token)
David Howells33941282009-09-14 01:17:35 +000086 return -EKEYREJECTED;
David Howells33941282009-09-14 01:17:35 +000087
88 sec = rxrpc_security_lookup(token->security_index);
David Howells17926a72007-04-26 15:48:28 -070089 if (!sec)
90 return -EKEYREJECTED;
91 conn->security = sec;
92
93 ret = conn->security->init_connection_security(conn);
94 if (ret < 0) {
David Howellse0e4d822016-04-07 17:23:58 +010095 conn->security = &rxrpc_no_security;
David Howells17926a72007-04-26 15:48:28 -070096 return ret;
97 }
98
99 _leave(" = 0");
100 return 0;
101}
102
103/*
104 * initialise the security on a server connection
105 */
106int rxrpc_init_server_conn_security(struct rxrpc_connection *conn)
107{
David Howells648af7f2016-04-07 17:23:51 +0100108 const struct rxrpc_security *sec;
David Howells85f32272016-04-04 14:00:36 +0100109 struct rxrpc_local *local = conn->params.local;
David Howells17926a72007-04-26 15:48:28 -0700110 struct rxrpc_sock *rx;
111 struct key *key;
112 key_ref_t kref;
David Howellsb4f13422016-03-04 15:56:19 +0000113 char kdesc[5 + 1 + 3 + 1];
David Howells17926a72007-04-26 15:48:28 -0700114
115 _enter("");
116
David Howells68d6d1a2017-06-05 14:30:49 +0100117 sprintf(kdesc, "%u:%u", conn->service_id, conn->security_ix);
David Howells17926a72007-04-26 15:48:28 -0700118
119 sec = rxrpc_security_lookup(conn->security_ix);
120 if (!sec) {
121 _leave(" = -ENOKEY [lookup]");
122 return -ENOKEY;
123 }
124
125 /* find the service */
David Howells248f2192016-09-08 11:10:12 +0100126 read_lock(&local->services_lock);
David Howells1e9e5c92016-09-29 22:37:15 +0100127 rx = rcu_dereference_protected(local->service,
128 lockdep_is_held(&local->services_lock));
David Howells28036f42017-06-05 14:30:49 +0100129 if (rx && (rx->srx.srx_service == conn->service_id ||
130 rx->second_service == conn->service_id))
David Howells1e9e5c92016-09-29 22:37:15 +0100131 goto found_service;
David Howells17926a72007-04-26 15:48:28 -0700132
133 /* the service appears to have died */
David Howells248f2192016-09-08 11:10:12 +0100134 read_unlock(&local->services_lock);
David Howells17926a72007-04-26 15:48:28 -0700135 _leave(" = -ENOENT");
136 return -ENOENT;
137
138found_service:
139 if (!rx->securities) {
David Howells248f2192016-09-08 11:10:12 +0100140 read_unlock(&local->services_lock);
David Howells17926a72007-04-26 15:48:28 -0700141 _leave(" = -ENOKEY");
142 return -ENOKEY;
143 }
144
145 /* look through the service's keyring */
146 kref = keyring_search(make_key_ref(rx->securities, 1UL),
David Howellsdcf49db2019-06-26 21:02:32 +0100147 &key_type_rxrpc_s, kdesc, true);
David Howells17926a72007-04-26 15:48:28 -0700148 if (IS_ERR(kref)) {
David Howells248f2192016-09-08 11:10:12 +0100149 read_unlock(&local->services_lock);
David Howells17926a72007-04-26 15:48:28 -0700150 _leave(" = %ld [search]", PTR_ERR(kref));
151 return PTR_ERR(kref);
152 }
153
154 key = key_ref_to_ptr(kref);
David Howells248f2192016-09-08 11:10:12 +0100155 read_unlock(&local->services_lock);
David Howells17926a72007-04-26 15:48:28 -0700156
157 conn->server_key = key;
158 conn->security = sec;
159
160 _leave(" = 0");
161 return 0;
162}