1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Request key authorisation token key definition.
4 * Copyright (C) 2005 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
7 * See Documentation/security/keys/request-key.rst
10 #include <linux/sched.h>
11 #include <linux/err.h>
12 #include <linux/seq_file.h>
13 #include <linux/slab.h>
14 #include <linux/uaccess.h>
16 #include <keys/request_key_auth-type.h>
18 static int request_key_auth_preparse(struct key_preparsed_payload *);
19 static void request_key_auth_free_preparse(struct key_preparsed_payload *);
20 static int request_key_auth_instantiate(struct key *,
21 struct key_preparsed_payload *);
22 static void request_key_auth_describe(const struct key *, struct seq_file *);
23 static void request_key_auth_revoke(struct key *);
24 static void request_key_auth_destroy(struct key *);
25 static long request_key_auth_read(const struct key *, char __user *, size_t);
27 static struct key_acl request_key_auth_acl = {
28 .usage = REFCOUNT_INIT(1),
30 .possessor_viewable = true,
32 KEY_POSSESSOR_ACE(KEY_ACE_VIEW | KEY_ACE_READ | KEY_ACE_SEARCH |
34 KEY_OWNER_ACE(KEY_ACE_VIEW),
39 * The request-key authorisation key type definition.
41 struct key_type key_type_request_key_auth = {
42 .name = ".request_key_auth",
43 .def_datalen = sizeof(struct request_key_auth),
44 .preparse = request_key_auth_preparse,
45 .free_preparse = request_key_auth_free_preparse,
46 .instantiate = request_key_auth_instantiate,
47 .describe = request_key_auth_describe,
48 .revoke = request_key_auth_revoke,
49 .destroy = request_key_auth_destroy,
50 .read = request_key_auth_read,
53 static int request_key_auth_preparse(struct key_preparsed_payload *prep)
58 static void request_key_auth_free_preparse(struct key_preparsed_payload *prep)
63 * Instantiate a request-key authorisation key.
65 static int request_key_auth_instantiate(struct key *key,
66 struct key_preparsed_payload *prep)
68 rcu_assign_keypointer(key, (struct request_key_auth *)prep->data);
73 * Describe an authorisation token.
75 static void request_key_auth_describe(const struct key *key,
78 struct request_key_auth *rka = dereference_key_rcu(key);
81 seq_puts(m, key->description);
82 if (key_is_positive(key))
83 seq_printf(m, " pid:%d ci:%zu", rka->pid, rka->callout_len);
87 * Read the callout_info data (retrieves the callout information).
88 * - the key's semaphore is read-locked
90 static long request_key_auth_read(const struct key *key,
91 char __user *buffer, size_t buflen)
93 struct request_key_auth *rka = dereference_key_locked(key);
97 datalen = rka->callout_len;
100 /* we can return the data as is */
101 if (buffer && buflen > 0) {
102 if (buflen > datalen)
105 if (copy_to_user(buffer, rka->callout_info, buflen) != 0)
112 static void free_request_key_auth(struct request_key_auth *rka)
116 key_put(rka->target_key);
117 key_put(rka->dest_keyring);
120 kfree(rka->callout_info);
125 * Dispose of the request_key_auth record under RCU conditions
127 static void request_key_auth_rcu_disposal(struct rcu_head *rcu)
129 struct request_key_auth *rka =
130 container_of(rcu, struct request_key_auth, rcu);
132 free_request_key_auth(rka);
136 * Handle revocation of an authorisation token key.
138 * Called with the key sem write-locked.
140 static void request_key_auth_revoke(struct key *key)
142 struct request_key_auth *rka = dereference_key_locked(key);
144 kenter("{%d}", key->serial);
145 rcu_assign_keypointer(key, NULL);
146 call_rcu(&rka->rcu, request_key_auth_rcu_disposal);
150 * Destroy an instantiation authorisation token key.
152 static void request_key_auth_destroy(struct key *key)
154 struct request_key_auth *rka = rcu_access_pointer(key->payload.rcu_data0);
156 kenter("{%d}", key->serial);
158 rcu_assign_keypointer(key, NULL);
159 call_rcu(&rka->rcu, request_key_auth_rcu_disposal);
164 * Create an authorisation token for /sbin/request-key or whoever to gain
165 * access to the caller's security data.
167 struct key *request_key_auth_new(struct key *target, const char *op,
168 const void *callout_info, size_t callout_len,
169 struct key *dest_keyring)
171 struct request_key_auth *rka, *irka;
172 const struct cred *cred = current_cred();
173 struct key *authkey = NULL;
177 kenter("%d,", target->serial);
179 /* allocate a auth record */
180 rka = kzalloc(sizeof(*rka), GFP_KERNEL);
183 rka->callout_info = kmemdup(callout_info, callout_len, GFP_KERNEL);
184 if (!rka->callout_info)
186 rka->callout_len = callout_len;
187 strlcpy(rka->op, op, sizeof(rka->op));
189 /* see if the calling process is already servicing the key request of
191 if (cred->request_key_auth) {
192 /* it is - use that instantiation context here too */
193 down_read(&cred->request_key_auth->sem);
195 /* if the auth key has been revoked, then the key we're
196 * servicing is already instantiated */
197 if (test_bit(KEY_FLAG_REVOKED,
198 &cred->request_key_auth->flags)) {
199 up_read(&cred->request_key_auth->sem);
204 irka = cred->request_key_auth->payload.data[0];
205 rka->cred = get_cred(irka->cred);
206 rka->pid = irka->pid;
208 up_read(&cred->request_key_auth->sem);
211 /* it isn't - use this process as the context */
212 rka->cred = get_cred(cred);
213 rka->pid = current->pid;
216 rka->target_key = key_get(target);
217 rka->dest_keyring = key_get(dest_keyring);
219 /* allocate the auth key */
220 sprintf(desc, "%x", target->serial);
222 authkey = key_alloc(&key_type_request_key_auth, desc,
223 cred->fsuid, cred->fsgid, cred,
224 &request_key_auth_acl,
225 KEY_ALLOC_NOT_IN_QUOTA, NULL);
226 if (IS_ERR(authkey)) {
227 ret = PTR_ERR(authkey);
231 /* construct the auth key */
232 ret = key_instantiate_and_link(authkey, rka, 0, NULL, NULL);
234 goto error_put_authkey;
236 kleave(" = {%d,%d}", authkey->serial, refcount_read(&authkey->usage));
242 free_request_key_auth(rka);
249 * Search the current process's keyrings for the authorisation key for
250 * instantiation of a key.
252 struct key *key_get_instantiation_authkey(key_serial_t target_id)
254 char description[16];
255 struct keyring_search_context ctx = {
256 .index_key.type = &key_type_request_key_auth,
257 .index_key.description = description,
258 .cred = current_cred(),
259 .match_data.cmp = key_default_cmp,
260 .match_data.raw_data = description,
261 .match_data.lookup_type = KEYRING_SEARCH_LOOKUP_DIRECT,
262 .flags = (KEYRING_SEARCH_DO_STATE_CHECK |
263 KEYRING_SEARCH_RECURSE),
266 key_ref_t authkey_ref;
268 ctx.index_key.desc_len = sprintf(description, "%x", target_id);
271 authkey_ref = search_process_keyrings_rcu(&ctx);
274 if (IS_ERR(authkey_ref)) {
275 authkey = ERR_CAST(authkey_ref);
276 if (authkey == ERR_PTR(-EAGAIN))
277 authkey = ERR_PTR(-ENOKEY);
281 authkey = key_ref_to_ptr(authkey_ref);
282 if (test_bit(KEY_FLAG_REVOKED, &authkey->flags)) {
284 authkey = ERR_PTR(-EKEYREVOKED);