]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - security/keys/request_key_auth.c
Revert "Merge tag 'keys-acl-20190703' of git://git.kernel.org/pub/scm/linux/kernel...
[mirror_ubuntu-jammy-kernel.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 *);
b5f545c8 25static long request_key_auth_read(const struct key *, char __user *, 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
DH
68
69 seq_puts(m, "key:");
70 seq_puts(m, key->description);
363b02da 71 if (key_is_positive(key))
78b7280c 72 seq_printf(m, " pid:%d ci:%zu", rka->pid, rka->callout_len);
a8b17ed0 73}
3e30148c 74
b5f545c8 75/*
973c9f4f 76 * Read the callout_info data (retrieves the callout information).
b5f545c8
DH
77 * - the key's semaphore is read-locked
78 */
79static long request_key_auth_read(const struct key *key,
80 char __user *buffer, size_t buflen)
81{
e59428f7 82 struct request_key_auth *rka = dereference_key_locked(key);
b5f545c8
DH
83 size_t datalen;
84 long ret;
85
4a38e122 86 datalen = rka->callout_len;
b5f545c8
DH
87 ret = datalen;
88
89 /* we can return the data as is */
90 if (buffer && buflen > 0) {
91 if (buflen > datalen)
92 buflen = datalen;
93
94 if (copy_to_user(buffer, rka->callout_info, buflen) != 0)
95 ret = -EFAULT;
96 }
97
98 return ret;
a8b17ed0 99}
b5f545c8 100
44d81433
EB
101static void free_request_key_auth(struct request_key_auth *rka)
102{
103 if (!rka)
104 return;
105 key_put(rka->target_key);
106 key_put(rka->dest_keyring);
107 if (rka->cred)
108 put_cred(rka->cred);
109 kfree(rka->callout_info);
110 kfree(rka);
111}
112
e59428f7
DH
113/*
114 * Dispose of the request_key_auth record under RCU conditions
115 */
116static void request_key_auth_rcu_disposal(struct rcu_head *rcu)
117{
118 struct request_key_auth *rka =
119 container_of(rcu, struct request_key_auth, rcu);
120
121 free_request_key_auth(rka);
122}
123
124/*
125 * Handle revocation of an authorisation token key.
126 *
127 * Called with the key sem write-locked.
128 */
129static void request_key_auth_revoke(struct key *key)
130{
131 struct request_key_auth *rka = dereference_key_locked(key);
132
133 kenter("{%d}", key->serial);
134 rcu_assign_keypointer(key, NULL);
135 call_rcu(&rka->rcu, request_key_auth_rcu_disposal);
136}
137
3e30148c 138/*
973c9f4f 139 * Destroy an instantiation authorisation token key.
3e30148c
DH
140 */
141static void request_key_auth_destroy(struct key *key)
142{
e59428f7 143 struct request_key_auth *rka = rcu_access_pointer(key->payload.rcu_data0);
3e30148c
DH
144
145 kenter("{%d}", key->serial);
e59428f7
DH
146 if (rka) {
147 rcu_assign_keypointer(key, NULL);
148 call_rcu(&rka->rcu, request_key_auth_rcu_disposal);
149 }
a8b17ed0 150}
3e30148c 151
3e30148c 152/*
973c9f4f
DH
153 * Create an authorisation token for /sbin/request-key or whoever to gain
154 * access to the caller's security data.
3e30148c 155 */
822ad64d
DH
156struct key *request_key_auth_new(struct key *target, const char *op,
157 const void *callout_info, size_t callout_len,
158 struct key *dest_keyring)
3e30148c 159{
b5f545c8 160 struct request_key_auth *rka, *irka;
7936d16d 161 const struct cred *cred = current_cred();
b5f545c8 162 struct key *authkey = NULL;
3e30148c 163 char desc[20];
44d81433 164 int ret = -ENOMEM;
3e30148c
DH
165
166 kenter("%d,", target->serial);
167
b5f545c8 168 /* allocate a auth record */
44d81433
EB
169 rka = kzalloc(sizeof(*rka), GFP_KERNEL);
170 if (!rka)
171 goto error;
e007ce9c 172 rka->callout_info = kmemdup(callout_info, callout_len, GFP_KERNEL);
44d81433
EB
173 if (!rka->callout_info)
174 goto error_free_rka;
e007ce9c 175 rka->callout_len = callout_len;
822ad64d 176 strlcpy(rka->op, op, sizeof(rka->op));
3e30148c 177
b5f545c8
DH
178 /* see if the calling process is already servicing the key request of
179 * another process */
d84f4f99 180 if (cred->request_key_auth) {
b5f545c8 181 /* it is - use that instantiation context here too */
d84f4f99 182 down_read(&cred->request_key_auth->sem);
04c567d9
DH
183
184 /* if the auth key has been revoked, then the key we're
185 * servicing is already instantiated */
44d81433
EB
186 if (test_bit(KEY_FLAG_REVOKED,
187 &cred->request_key_auth->flags)) {
188 up_read(&cred->request_key_auth->sem);
189 ret = -EKEYREVOKED;
190 goto error_free_rka;
191 }
04c567d9 192
146aa8b1 193 irka = cred->request_key_auth->payload.data[0];
d84f4f99 194 rka->cred = get_cred(irka->cred);
b5f545c8 195 rka->pid = irka->pid;
04c567d9 196
d84f4f99 197 up_read(&cred->request_key_auth->sem);
3e30148c 198 }
b5f545c8
DH
199 else {
200 /* it isn't - use this process as the context */
d84f4f99 201 rka->cred = get_cred(cred);
b5f545c8
DH
202 rka->pid = current->pid;
203 }
204
205 rka->target_key = key_get(target);
8bbf4976 206 rka->dest_keyring = key_get(dest_keyring);
3e30148c
DH
207
208 /* allocate the auth key */
209 sprintf(desc, "%x", target->serial);
210
b5f545c8 211 authkey = key_alloc(&key_type_request_key_auth, desc,
d84f4f99 212 cred->fsuid, cred->fsgid, cred,
028db3e2
LT
213 KEY_POS_VIEW | KEY_POS_READ | KEY_POS_SEARCH | KEY_POS_LINK |
214 KEY_USR_VIEW, KEY_ALLOC_NOT_IN_QUOTA, NULL);
b5f545c8
DH
215 if (IS_ERR(authkey)) {
216 ret = PTR_ERR(authkey);
44d81433 217 goto error_free_rka;
3e30148c
DH
218 }
219
d84f4f99 220 /* construct the auth key */
b5f545c8
DH
221 ret = key_instantiate_and_link(authkey, rka, 0, NULL, NULL);
222 if (ret < 0)
44d81433 223 goto error_put_authkey;
3e30148c 224
fff29291 225 kleave(" = {%d,%d}", authkey->serial, refcount_read(&authkey->usage));
b5f545c8
DH
226 return authkey;
227
44d81433 228error_put_authkey:
b5f545c8 229 key_put(authkey);
44d81433
EB
230error_free_rka:
231 free_request_key_auth(rka);
232error:
b5f545c8
DH
233 kleave("= %d", ret);
234 return ERR_PTR(ret);
a8b17ed0 235}
3e30148c 236
3e30148c 237/*
973c9f4f
DH
238 * Search the current process's keyrings for the authorisation key for
239 * instantiation of a key.
3e30148c
DH
240 */
241struct key *key_get_instantiation_authkey(key_serial_t target_id)
242{
d0a059ca 243 char description[16];
4bdf0bc3
DH
244 struct keyring_search_context ctx = {
245 .index_key.type = &key_type_request_key_auth,
d0a059ca 246 .index_key.description = description,
4bdf0bc3 247 .cred = current_cred(),
c06cfb08 248 .match_data.cmp = key_default_cmp,
46291959
DH
249 .match_data.raw_data = description,
250 .match_data.lookup_type = KEYRING_SEARCH_LOOKUP_DIRECT,
dcf49dbc
DH
251 .flags = (KEYRING_SEARCH_DO_STATE_CHECK |
252 KEYRING_SEARCH_RECURSE),
4bdf0bc3 253 };
b5f545c8
DH
254 struct key *authkey;
255 key_ref_t authkey_ref;
256
ede0fa98 257 ctx.index_key.desc_len = sprintf(description, "%x", target_id);
d0a059ca 258
e59428f7
DH
259 rcu_read_lock();
260 authkey_ref = search_process_keyrings_rcu(&ctx);
261 rcu_read_unlock();
b5f545c8
DH
262
263 if (IS_ERR(authkey_ref)) {
e231c2ee 264 authkey = ERR_CAST(authkey_ref);
4d67431f
DH
265 if (authkey == ERR_PTR(-EAGAIN))
266 authkey = ERR_PTR(-ENOKEY);
b5f545c8
DH
267 goto error;
268 }
3e30148c 269
b5f545c8
DH
270 authkey = key_ref_to_ptr(authkey_ref);
271 if (test_bit(KEY_FLAG_REVOKED, &authkey->flags)) {
272 key_put(authkey);
273 authkey = ERR_PTR(-EKEYREVOKED);
274 }
3e30148c 275
b5f545c8
DH
276error:
277 return authkey;
a8b17ed0 278}