xref: /linux/security/keys/request_key_auth.c (revision 114f00d738f15dd8c7318369edcdc53dd6d08763)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Request key authorisation token key definition.
3  *
4  * Copyright (C) 2005 Red Hat, Inc. All Rights Reserved.
5  * Written by David Howells (dhowells@redhat.com)
6  *
7  * See Documentation/security/keys/request-key.rst
8  */
9 
10 #include <linux/sched.h>
11 #include <linux/err.h>
12 #include <linux/pid.h>
13 #include <linux/proc_fs.h>
14 #include <linux/seq_file.h>
15 #include <linux/slab.h>
16 #include <linux/uaccess.h>
17 #include "internal.h"
18 #include <keys/request_key_auth-type.h>
19 
20 static int request_key_auth_preparse(struct key_preparsed_payload *);
21 static void request_key_auth_free_preparse(struct key_preparsed_payload *);
22 static int request_key_auth_instantiate(struct key *,
23 					struct key_preparsed_payload *);
24 static void request_key_auth_describe(const struct key *, struct seq_file *);
25 static void request_key_auth_revoke(struct key *);
26 static void request_key_auth_destroy(struct key *);
27 static long request_key_auth_read(const struct key *, char *, size_t);
28 static void request_key_auth_rcu_disposal(struct rcu_head *);
29 
30 /*
31  * The request-key authorisation key type definition.
32  */
33 struct key_type key_type_request_key_auth = {
34 	.name		= ".request_key_auth",
35 	.def_datalen	= sizeof(struct request_key_auth),
36 	.preparse	= request_key_auth_preparse,
37 	.free_preparse	= request_key_auth_free_preparse,
38 	.instantiate	= request_key_auth_instantiate,
39 	.describe	= request_key_auth_describe,
40 	.revoke		= request_key_auth_revoke,
41 	.destroy	= request_key_auth_destroy,
42 	.read		= request_key_auth_read,
43 };
44 
45 static int request_key_auth_preparse(struct key_preparsed_payload *prep)
46 {
47 	return 0;
48 }
49 
50 static void request_key_auth_free_preparse(struct key_preparsed_payload *prep)
51 {
52 }
53 
54 /*
55  * Instantiate a request-key authorisation key.
56  */
57 static int request_key_auth_instantiate(struct key *key,
58 					struct key_preparsed_payload *prep)
59 {
60 	rcu_assign_keypointer(key, (struct request_key_auth *)prep->data);
61 	return 0;
62 }
63 
64 /*
65  * Describe an authorisation token.
66  */
67 static void request_key_auth_describe(const struct key *key,
68 				      struct seq_file *m)
69 {
70 	struct request_key_auth *rka = dereference_key_rcu(key);
71 
72 	if (!rka)
73 		return;
74 
75 	seq_puts(m, "key:");
76 	seq_puts(m, key->description);
77 	if (key_is_positive(key))
78 		seq_printf(m, " pid:%d ci:%zu",
79 			   pid_nr_ns(rka->pid,
80 				     proc_pid_ns(file_inode(m->file)->i_sb)),
81 			   rka->callout_len);
82 }
83 
84 /*
85  * Read the callout_info data (retrieves the callout information).
86  * - the key's semaphore is read-locked
87  */
88 static long request_key_auth_read(const struct key *key,
89 				  char *buffer, size_t buflen)
90 {
91 	struct request_key_auth *rka = dereference_key_locked(key);
92 	size_t datalen;
93 	long ret;
94 
95 	if (!rka)
96 		return -EKEYREVOKED;
97 
98 	datalen = rka->callout_len;
99 	ret = datalen;
100 
101 	/* we can return the data as is */
102 	if (buffer && buflen > 0) {
103 		if (buflen > datalen)
104 			buflen = datalen;
105 
106 		memcpy(buffer, rka->callout_info, buflen);
107 	}
108 
109 	return ret;
110 }
111 
112 static void free_request_key_auth(struct request_key_auth *rka)
113 {
114 	if (!rka)
115 		return;
116 	key_put(rka->target_key);
117 	key_put(rka->dest_keyring);
118 	if (rka->cred)
119 		put_cred(rka->cred);
120 	kfree(rka->callout_info);
121 	put_pid(rka->pid);
122 	kfree(rka);
123 }
124 
125 /*
126  * Take a reference to the request-key authorisation payload so callers can
127  * drop authkey->sem before doing operations that may sleep.
128  */
129 struct request_key_auth *request_key_auth_get(struct key *authkey)
130 {
131 	struct request_key_auth *rka;
132 
133 	down_read(&authkey->sem);
134 	rka = dereference_key_locked(authkey);
135 	if (rka && !test_bit(KEY_FLAG_REVOKED, &authkey->flags))
136 		refcount_inc(&rka->usage);
137 	else
138 		rka = NULL;
139 	up_read(&authkey->sem);
140 
141 	return rka;
142 }
143 
144 void request_key_auth_put(struct request_key_auth *rka)
145 {
146 	if (rka && refcount_dec_and_test(&rka->usage))
147 		call_rcu(&rka->rcu, request_key_auth_rcu_disposal);
148 }
149 
150 /*
151  * Dispose of the request_key_auth record under RCU conditions
152  */
153 static void request_key_auth_rcu_disposal(struct rcu_head *rcu)
154 {
155 	struct request_key_auth *rka =
156 		container_of(rcu, struct request_key_auth, rcu);
157 
158 	free_request_key_auth(rka);
159 }
160 
161 /*
162  * Handle revocation of an authorisation token key.
163  *
164  * Called with the key sem write-locked.
165  */
166 static void request_key_auth_revoke(struct key *key)
167 {
168 	struct request_key_auth *rka = dereference_key_locked(key);
169 
170 	kenter("{%d}", key->serial);
171 	if (!rka)
172 		return;
173 	rcu_assign_keypointer(key, NULL);
174 	request_key_auth_put(rka);
175 }
176 
177 /*
178  * Destroy an instantiation authorisation token key.
179  */
180 static void request_key_auth_destroy(struct key *key)
181 {
182 	struct request_key_auth *rka = rcu_access_pointer(key->payload.rcu_data0);
183 
184 	kenter("{%d}", key->serial);
185 	if (rka) {
186 		rcu_assign_keypointer(key, NULL);
187 		request_key_auth_put(rka);
188 	}
189 }
190 
191 /*
192  * Create an authorisation token for /sbin/request-key or whoever to gain
193  * access to the caller's security data.
194  */
195 struct key *request_key_auth_new(struct key *target, const char *op,
196 				 const void *callout_info, size_t callout_len,
197 				 struct key *dest_keyring)
198 {
199 	struct request_key_auth *rka, *irka;
200 	const struct cred *cred = current_cred();
201 	struct key *authkey = NULL;
202 	char desc[20];
203 	int ret = -ENOMEM;
204 
205 	kenter("%d,", target->serial);
206 
207 	/* allocate a auth record */
208 	rka = kzalloc_obj(*rka);
209 	if (!rka)
210 		goto error;
211 	refcount_set(&rka->usage, 1);
212 	rka->callout_info = kmemdup(callout_info, callout_len, GFP_KERNEL);
213 	if (!rka->callout_info)
214 		goto error_free_rka;
215 	rka->callout_len = callout_len;
216 	strscpy(rka->op, op, sizeof(rka->op));
217 
218 	/* see if the calling process is already servicing the key request of
219 	 * another process */
220 	if (cred->request_key_auth) {
221 		/* it is - use that instantiation context here too */
222 		down_read(&cred->request_key_auth->sem);
223 
224 		/* if the auth key has been revoked, then the key we're
225 		 * servicing is already instantiated */
226 		if (test_bit(KEY_FLAG_REVOKED,
227 			     &cred->request_key_auth->flags)) {
228 			up_read(&cred->request_key_auth->sem);
229 			ret = -EKEYREVOKED;
230 			goto error_free_rka;
231 		}
232 
233 		irka = cred->request_key_auth->payload.data[0];
234 		rka->cred = get_cred(irka->cred);
235 		rka->pid = get_pid(irka->pid);
236 
237 		up_read(&cred->request_key_auth->sem);
238 	}
239 	else {
240 		/* it isn't - use this process as the context */
241 		rka->cred = get_cred(cred);
242 		rka->pid = get_pid(task_pid(current));
243 	}
244 
245 	rka->target_key = key_get(target);
246 	rka->dest_keyring = key_get(dest_keyring);
247 
248 	/* allocate the auth key */
249 	sprintf(desc, "%x", target->serial);
250 
251 	authkey = key_alloc(&key_type_request_key_auth, desc,
252 			    cred->fsuid, cred->fsgid, cred,
253 			    KEY_POS_VIEW | KEY_POS_READ | KEY_POS_SEARCH | KEY_POS_LINK |
254 			    KEY_USR_VIEW, KEY_ALLOC_NOT_IN_QUOTA, NULL);
255 	if (IS_ERR(authkey)) {
256 		ret = PTR_ERR(authkey);
257 		goto error_free_rka;
258 	}
259 
260 	/* construct the auth key */
261 	ret = key_instantiate_and_link(authkey, rka, 0, NULL, NULL);
262 	if (ret < 0)
263 		goto error_put_authkey;
264 
265 	kleave(" = {%d,%d}", authkey->serial, refcount_read(&authkey->usage));
266 	return authkey;
267 
268 error_put_authkey:
269 	key_put(authkey);
270 error_free_rka:
271 	free_request_key_auth(rka);
272 error:
273 	kleave("= %d", ret);
274 	return ERR_PTR(ret);
275 }
276 
277 /*
278  * Search the current process's keyrings for the authorisation key for
279  * instantiation of a key.
280  */
281 struct key *key_get_instantiation_authkey(key_serial_t target_id)
282 {
283 	char description[16];
284 	struct keyring_search_context ctx = {
285 		.index_key.type		= &key_type_request_key_auth,
286 		.index_key.description	= description,
287 		.cred			= current_cred(),
288 		.match_data.cmp		= key_default_cmp,
289 		.match_data.raw_data	= description,
290 		.match_data.lookup_type	= KEYRING_SEARCH_LOOKUP_DIRECT,
291 		.flags			= (KEYRING_SEARCH_DO_STATE_CHECK |
292 					   KEYRING_SEARCH_RECURSE),
293 	};
294 	struct key *authkey;
295 	key_ref_t authkey_ref;
296 
297 	ctx.index_key.desc_len = sprintf(description, "%x", target_id);
298 
299 	rcu_read_lock();
300 	authkey_ref = search_process_keyrings_rcu(&ctx);
301 	rcu_read_unlock();
302 
303 	if (IS_ERR(authkey_ref)) {
304 		authkey = ERR_CAST(authkey_ref);
305 		if (authkey == ERR_PTR(-EAGAIN))
306 			authkey = ERR_PTR(-ENOKEY);
307 		goto error;
308 	}
309 
310 	authkey = key_ref_to_ptr(authkey_ref);
311 	if (test_bit(KEY_FLAG_REVOKED, &authkey->flags)) {
312 		key_put(authkey);
313 		authkey = ERR_PTR(-EKEYREVOKED);
314 	}
315 
316 error:
317 	return authkey;
318 }
319