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