xref: /openbmc/linux/net/rxrpc/security.c (revision 65844828)
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 
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>
15 #include <keys/rxrpc-type.h>
16 #include "ar-internal.h"
17 
18 static const struct rxrpc_security *rxrpc_security_types[] = {
19 	[RXRPC_SECURITY_NONE]	= &rxrpc_no_security,
20 #ifdef CONFIG_RXKAD
21 	[RXRPC_SECURITY_RXKAD]	= &rxkad,
22 #endif
23 };
24 
25 int __init rxrpc_init_security(void)
26 {
27 	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 
39 failed:
40 	for (i--; i >= 0; i--)
41 		if (rxrpc_security_types[i])
42 			rxrpc_security_types[i]->exit();
43 	return ret;
44 }
45 
46 void rxrpc_exit_security(void)
47 {
48 	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();
53 }
54 
55 /*
56  * look up an rxrpc security module
57  */
58 const struct rxrpc_security *rxrpc_security_lookup(u8 security_index)
59 {
60 	if (security_index >= ARRAY_SIZE(rxrpc_security_types))
61 		return NULL;
62 	return rxrpc_security_types[security_index];
63 }
64 
65 /*
66  * Initialise the security on a client call.
67  */
68 int rxrpc_init_client_call_security(struct rxrpc_call *call)
69 {
70 	const struct rxrpc_security *sec;
71 	struct rxrpc_key_token *token;
72 	struct key *key = call->key;
73 	int ret;
74 
75 	if (!key)
76 		return 0;
77 
78 	ret = key_validate(key);
79 	if (ret < 0)
80 		return ret;
81 
82 	for (token = key->payload.data[0]; token; token = token->next) {
83 		sec = rxrpc_security_lookup(token->security_index);
84 		if (sec)
85 			goto found;
86 	}
87 	return -EKEYREJECTED;
88 
89 found:
90 	call->security = sec;
91 	_leave(" = 0");
92 	return 0;
93 }
94 
95 /*
96  * initialise the security on a client connection
97  */
98 int rxrpc_init_client_conn_security(struct rxrpc_connection *conn)
99 {
100 	const struct rxrpc_security *sec;
101 	struct rxrpc_key_token *token;
102 	struct key *key = conn->key;
103 	int ret;
104 
105 	_enter("{%d},{%x}", conn->debug_id, key_serial(key));
106 
107 	if (!key)
108 		return 0;
109 
110 	ret = key_validate(key);
111 	if (ret < 0)
112 		return ret;
113 
114 	for (token = key->payload.data[0]; token; token = token->next) {
115 		sec = rxrpc_security_lookup(token->security_index);
116 		if (sec)
117 			goto found;
118 	}
119 	return -EKEYREJECTED;
120 
121 found:
122 	conn->security = sec;
123 
124 	ret = conn->security->init_connection_security(conn, token);
125 	if (ret < 0) {
126 		conn->security = &rxrpc_no_security;
127 		return ret;
128 	}
129 
130 	_leave(" = 0");
131 	return 0;
132 }
133 
134 /*
135  * Set the ops a server connection.
136  */
137 const struct rxrpc_security *rxrpc_get_incoming_security(struct rxrpc_sock *rx,
138 							 struct sk_buff *skb)
139 {
140 	const struct rxrpc_security *sec;
141 	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
142 
143 	_enter("");
144 
145 	sec = rxrpc_security_lookup(sp->hdr.securityIndex);
146 	if (!sec) {
147 		trace_rxrpc_abort(0, "SVS",
148 				  sp->hdr.cid, sp->hdr.callNumber, sp->hdr.seq,
149 				  RX_INVALID_OPERATION, EKEYREJECTED);
150 		skb->mark = RXRPC_SKB_MARK_REJECT_ABORT;
151 		skb->priority = RX_INVALID_OPERATION;
152 		return NULL;
153 	}
154 
155 	if (sp->hdr.securityIndex != RXRPC_SECURITY_NONE &&
156 	    !rx->securities) {
157 		trace_rxrpc_abort(0, "SVR",
158 				  sp->hdr.cid, sp->hdr.callNumber, sp->hdr.seq,
159 				  RX_INVALID_OPERATION, EKEYREJECTED);
160 		skb->mark = RXRPC_SKB_MARK_REJECT_ABORT;
161 		skb->priority = sec->no_key_abort;
162 		return NULL;
163 	}
164 
165 	return sec;
166 }
167 
168 /*
169  * Find the security key for a server connection.
170  */
171 struct key *rxrpc_look_up_server_security(struct rxrpc_connection *conn,
172 					  struct sk_buff *skb,
173 					  u32 kvno, u32 enctype)
174 {
175 	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
176 	struct rxrpc_sock *rx;
177 	struct key *key = ERR_PTR(-EKEYREJECTED);
178 	key_ref_t kref = NULL;
179 	char kdesc[5 + 1 + 3 + 1 + 12 + 1 + 12 + 1];
180 	int ret;
181 
182 	_enter("");
183 
184 	if (enctype)
185 		sprintf(kdesc, "%u:%u:%u:%u",
186 			sp->hdr.serviceId, sp->hdr.securityIndex, kvno, enctype);
187 	else if (kvno)
188 		sprintf(kdesc, "%u:%u:%u",
189 			sp->hdr.serviceId, sp->hdr.securityIndex, kvno);
190 	else
191 		sprintf(kdesc, "%u:%u",
192 			sp->hdr.serviceId, sp->hdr.securityIndex);
193 
194 	rcu_read_lock();
195 
196 	rx = rcu_dereference(conn->local->service);
197 	if (!rx)
198 		goto out;
199 
200 	/* look through the service's keyring */
201 	kref = keyring_search(make_key_ref(rx->securities, 1UL),
202 			      &key_type_rxrpc_s, kdesc, true);
203 	if (IS_ERR(kref)) {
204 		key = ERR_CAST(kref);
205 		goto out;
206 	}
207 
208 	key = key_ref_to_ptr(kref);
209 
210 	ret = key_validate(key);
211 	if (ret < 0) {
212 		key_put(key);
213 		key = ERR_PTR(ret);
214 		goto out;
215 	}
216 
217 out:
218 	rcu_read_unlock();
219 	return key;
220 }
221