xref: /linux/net/rxrpc/server_key.c (revision 3e9201e4fe8bd78f4601a51212562505bbb60e3a)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* RxRPC key management
3  *
4  * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
5  * Written by David Howells (dhowells@redhat.com)
6  *
7  * RxRPC keys should have a description of describing their purpose:
8  *	"afs@CAMBRIDGE.REDHAT.COM>
9  */
10 
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 
13 #include <linux/module.h>
14 #include <linux/net.h>
15 #include <linux/skbuff.h>
16 #include <linux/key-type.h>
17 #include <linux/ctype.h>
18 #include <linux/slab.h>
19 #include <net/sock.h>
20 #include <net/af_rxrpc.h>
21 #include <keys/rxrpc-type.h>
22 #include <keys/user-type.h>
23 #include "ar-internal.h"
24 
25 static int rxrpc_vet_description_s(const char *);
26 static int rxrpc_preparse_s(struct key_preparsed_payload *);
27 static void rxrpc_free_preparse_s(struct key_preparsed_payload *);
28 static void rxrpc_destroy_s(struct key *);
29 static void rxrpc_describe_s(const struct key *, struct seq_file *);
30 
31 /*
32  * rxrpc server keys take "<serviceId>:<securityIndex>[:<sec-specific>]" as the
33  * description and the key material as the payload.
34  */
35 struct key_type key_type_rxrpc_s = {
36 	.name		= "rxrpc_s",
37 	.flags		= KEY_TYPE_NET_DOMAIN,
38 	.vet_description = rxrpc_vet_description_s,
39 	.preparse	= rxrpc_preparse_s,
40 	.free_preparse	= rxrpc_free_preparse_s,
41 	.instantiate	= generic_key_instantiate,
42 	.destroy	= rxrpc_destroy_s,
43 	.describe	= rxrpc_describe_s,
44 };
45 
46 /*
47  * Vet the description for an RxRPC server key.
48  */
49 static int rxrpc_vet_description_s(const char *desc)
50 {
51 	unsigned long service, sec_class;
52 	char *p;
53 
54 	service = simple_strtoul(desc, &p, 10);
55 	if (*p != ':' || service > 65535)
56 		return -EINVAL;
57 	sec_class = simple_strtoul(p + 1, &p, 10);
58 	if ((*p && *p != ':') || sec_class < 1 || sec_class > 255)
59 		return -EINVAL;
60 	return 0;
61 }
62 
63 /*
64  * Preparse a server secret key.
65  */
66 static int rxrpc_preparse_s(struct key_preparsed_payload *prep)
67 {
68 	const struct rxrpc_security *sec;
69 	unsigned int service, sec_class;
70 	int n;
71 
72 	_enter("%zu", prep->datalen);
73 
74 	if (!prep->orig_description)
75 		return -EINVAL;
76 
77 	if (sscanf(prep->orig_description, "%u:%u%n", &service, &sec_class, &n) != 2)
78 		return -EINVAL;
79 
80 	sec = rxrpc_security_lookup(sec_class);
81 	if (!sec)
82 		return -ENOPKG;
83 
84 	prep->payload.data[1] = (struct rxrpc_security *)sec;
85 
86 	if (!sec->preparse_server_key)
87 		return -EINVAL;
88 
89 	return sec->preparse_server_key(prep);
90 }
91 
92 static void rxrpc_free_preparse_s(struct key_preparsed_payload *prep)
93 {
94 	const struct rxrpc_security *sec = prep->payload.data[1];
95 
96 	if (sec && sec->free_preparse_server_key)
97 		sec->free_preparse_server_key(prep);
98 }
99 
100 static void rxrpc_destroy_s(struct key *key)
101 {
102 	const struct rxrpc_security *sec = key->payload.data[1];
103 
104 	if (sec && sec->destroy_server_key)
105 		sec->destroy_server_key(key);
106 }
107 
108 static void rxrpc_describe_s(const struct key *key, struct seq_file *m)
109 {
110 	const struct rxrpc_security *sec = key->payload.data[1];
111 
112 	seq_puts(m, key->description);
113 	if (sec && sec->describe_server_key)
114 		sec->describe_server_key(key, m);
115 }
116 
117 /*
118  * grab the security keyring for a server socket
119  */
120 int rxrpc_server_keyring(struct rxrpc_sock *rx, sockptr_t optval, int optlen)
121 {
122 	struct key *key;
123 	char *description;
124 
125 	_enter("");
126 
127 	if (rx->securities)
128 		return -EINVAL;
129 
130 	if (optlen <= 0 || optlen > PAGE_SIZE - 1)
131 		return -EINVAL;
132 
133 	description = memdup_sockptr_nul(optval, optlen);
134 	if (IS_ERR(description))
135 		return PTR_ERR(description);
136 
137 	key = request_key(&key_type_keyring, description, NULL);
138 	if (IS_ERR(key)) {
139 		kfree(description);
140 		_leave(" = %ld", PTR_ERR(key));
141 		return PTR_ERR(key);
142 	}
143 
144 	rx->securities = key;
145 	kfree(description);
146 	_leave(" = 0 [key %x]", key->serial);
147 	return 0;
148 }
149 
150 /**
151  * rxrpc_sock_set_security_keyring - Set the security keyring for a kernel service
152  * @sk: The socket to set the keyring on
153  * @keyring: The keyring to set
154  *
155  * Set the server security keyring on an rxrpc socket.  This is used to provide
156  * the encryption keys for a kernel service.
157  *
158  * Return: %0 if successful and a negative error code otherwise.
159  */
160 int rxrpc_sock_set_security_keyring(struct sock *sk, struct key *keyring)
161 {
162 	struct rxrpc_sock *rx = rxrpc_sk(sk);
163 	int ret = 0;
164 
165 	lock_sock(sk);
166 	if (rx->securities)
167 		ret = -EINVAL;
168 	else if (rx->sk.sk_state != RXRPC_UNBOUND)
169 		ret = -EISCONN;
170 	else
171 		rx->securities = key_get(keyring);
172 	release_sock(sk);
173 	return ret;
174 }
175 EXPORT_SYMBOL(rxrpc_sock_set_security_keyring);
176 
177 /**
178  * rxrpc_sock_set_manage_response - Set the manage-response flag for a kernel service
179  * @sk: The socket to set the keyring on
180  * @set: True to set, false to clear the flag
181  *
182  * Set the flag on an rxrpc socket to say that the caller wants to manage the
183  * RESPONSE packet and the user-defined data it may contain.  Setting this
184  * means that recvmsg() will return messages with RXRPC_CHALLENGED in the
185  * control message buffer containing information about the challenge.
186  *
187  * The user should respond to the challenge by passing RXRPC_RESPOND or
188  * RXRPC_RESPOND_ABORT control messages with sendmsg() to the same call.
189  * Supplementary control messages, such as RXRPC_RESP_RXGK_APPDATA, may be
190  * included to indicate the parts the user wants to supply.
191  *
192  * The server will be passed the response data with a RXRPC_RESPONDED control
193  * message when it gets the first data from each call.
194  *
195  * Note that this is only honoured by security classes that need auxiliary data
196  * (e.g. RxGK).  Those that don't offer the facility (e.g. RxKAD) respond
197  * without consulting userspace.
198  *
199  * Return: The previous setting.
200  */
201 int rxrpc_sock_set_manage_response(struct sock *sk, bool set)
202 {
203 	struct rxrpc_sock *rx = rxrpc_sk(sk);
204 	int ret;
205 
206 	lock_sock(sk);
207 	ret = !!test_bit(RXRPC_SOCK_MANAGE_RESPONSE, &rx->flags);
208 	if (set)
209 		set_bit(RXRPC_SOCK_MANAGE_RESPONSE, &rx->flags);
210 	else
211 		clear_bit(RXRPC_SOCK_MANAGE_RESPONSE, &rx->flags);
212 	release_sock(sk);
213 	return ret;
214 }
215 EXPORT_SYMBOL(rxrpc_sock_set_manage_response);
216