blob: e282c6179b21d5753a289dee2509a140558e9b98 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
David Howells76181c12007-10-16 23:29:46 -07002/* Basic authentication token and access key management
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
David Howells69664cf2008-04-29 01:01:31 -07004 * Copyright (C) 2004-2008 Red Hat, Inc. All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Written by David Howells (dhowells@redhat.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
7
Paul Gortmaker876979c2018-12-09 15:36:29 -05008#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/init.h>
Randy Dunlapa7807a32006-06-27 02:53:54 -070010#include <linux/poison.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/sched.h>
12#include <linux/slab.h>
David Howells29db9192005-10-30 15:02:44 -080013#include <linux/security.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/workqueue.h>
Michael LeMaye51f6d32006-06-26 00:24:54 -070015#include <linux/random.h>
Lakshmi Ramasubramaniancb1aa382019-12-11 08:47:05 -080016#include <linux/ima.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/err.h>
18#include "internal.h"
19
David Howells8bc16de2011-08-22 14:09:11 +010020struct kmem_cache *key_jar;
Linus Torvalds1da177e2005-04-16 15:20:36 -070021struct rb_root key_serial_tree; /* tree of keys indexed by serial */
22DEFINE_SPINLOCK(key_serial_lock);
23
24struct rb_root key_user_tree; /* tree of quota records indexed by UID */
25DEFINE_SPINLOCK(key_user_lock);
26
Steve Dickson738c5d12014-09-02 13:52:05 +010027unsigned int key_quota_root_maxkeys = 1000000; /* root's key count quota */
28unsigned int key_quota_root_maxbytes = 25000000; /* root's key space quota */
David Howells0b77f5b2008-04-29 01:01:32 -070029unsigned int key_quota_maxkeys = 200; /* general key count quota */
30unsigned int key_quota_maxbytes = 20000; /* general key space quota */
31
Linus Torvalds1da177e2005-04-16 15:20:36 -070032static LIST_HEAD(key_types_list);
33static DECLARE_RWSEM(key_types_sem);
34
David Howells973c9f42011-01-20 16:38:33 +000035/* We serialise key instantiation and link */
David Howells76181c12007-10-16 23:29:46 -070036DEFINE_MUTEX(key_construction_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#ifdef KEY_DEBUGGING
39void __key_check(const struct key *key)
40{
41 printk("__key_check: key %p {%08x} should be {%08x}\n",
42 key, key->magic, KEY_DEBUG_MAGIC);
43 BUG();
44}
45#endif
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047/*
David Howells973c9f42011-01-20 16:38:33 +000048 * Get the key quota record for a user, allocating a new record if one doesn't
49 * already exist.
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 */
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -080051struct key_user *key_user_lookup(kuid_t uid)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
53 struct key_user *candidate = NULL, *user;
Eric Biggers8f674562017-09-18 11:37:39 -070054 struct rb_node *parent, **p;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
David Howells973c9f42011-01-20 16:38:33 +000056try_again:
Eric Biggers8f674562017-09-18 11:37:39 -070057 parent = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 p = &key_user_tree.rb_node;
59 spin_lock(&key_user_lock);
60
61 /* search the tree for a user record with a matching UID */
62 while (*p) {
63 parent = *p;
64 user = rb_entry(parent, struct key_user, node);
65
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -080066 if (uid_lt(uid, user->uid))
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 p = &(*p)->rb_left;
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -080068 else if (uid_gt(uid, user->uid))
Serge E. Hallyn1d1e9752009-02-26 18:27:38 -060069 p = &(*p)->rb_right;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 else
71 goto found;
72 }
73
74 /* if we get here, we failed to find a match in the tree */
75 if (!candidate) {
76 /* allocate a candidate user record if we don't already have
77 * one */
78 spin_unlock(&key_user_lock);
79
80 user = NULL;
81 candidate = kmalloc(sizeof(struct key_user), GFP_KERNEL);
82 if (unlikely(!candidate))
83 goto out;
84
85 /* the allocation may have scheduled, so we need to repeat the
86 * search lest someone else added the record whilst we were
87 * asleep */
88 goto try_again;
89 }
90
91 /* if we get here, then the user record still hadn't appeared on the
92 * second pass - so we use the candidate record */
Elena Reshetovaddb99e12017-03-31 15:20:49 +030093 refcount_set(&candidate->usage, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 atomic_set(&candidate->nkeys, 0);
95 atomic_set(&candidate->nikeys, 0);
96 candidate->uid = uid;
97 candidate->qnkeys = 0;
98 candidate->qnbytes = 0;
99 spin_lock_init(&candidate->lock);
David Howells76181c12007-10-16 23:29:46 -0700100 mutex_init(&candidate->cons_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102 rb_link_node(&candidate->node, parent, p);
103 rb_insert_color(&candidate->node, &key_user_tree);
104 spin_unlock(&key_user_lock);
105 user = candidate;
106 goto out;
107
108 /* okay - we found a user record for this UID */
David Howells973c9f42011-01-20 16:38:33 +0000109found:
Elena Reshetovaddb99e12017-03-31 15:20:49 +0300110 refcount_inc(&user->usage);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 spin_unlock(&key_user_lock);
Jesper Juhla7f988b2005-11-07 01:01:35 -0800112 kfree(candidate);
David Howells973c9f42011-01-20 16:38:33 +0000113out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 return user;
David Howellsa8b17ed2011-01-20 16:38:27 +0000115}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117/*
David Howells973c9f42011-01-20 16:38:33 +0000118 * Dispose of a user structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 */
120void key_user_put(struct key_user *user)
121{
Elena Reshetovaddb99e12017-03-31 15:20:49 +0300122 if (refcount_dec_and_lock(&user->usage, &key_user_lock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 rb_erase(&user->node, &key_user_tree);
124 spin_unlock(&key_user_lock);
125
126 kfree(user);
127 }
David Howellsa8b17ed2011-01-20 16:38:27 +0000128}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130/*
David Howells973c9f42011-01-20 16:38:33 +0000131 * Allocate a serial number for a key. These are assigned randomly to avoid
132 * security issues through covert channel problems.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 */
134static inline void key_alloc_serial(struct key *key)
135{
136 struct rb_node *parent, **p;
137 struct key *xkey;
138
Michael LeMaye51f6d32006-06-26 00:24:54 -0700139 /* propose a random serial number and look for a hole for it in the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 * serial number tree */
Michael LeMaye51f6d32006-06-26 00:24:54 -0700141 do {
142 get_random_bytes(&key->serial, sizeof(key->serial));
143
144 key->serial >>= 1; /* negative numbers are not permitted */
145 } while (key->serial < 3);
146
147 spin_lock(&key_serial_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
David Howells9ad08302007-02-06 13:45:51 +0000149attempt_insertion:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 parent = NULL;
151 p = &key_serial_tree.rb_node;
152
153 while (*p) {
154 parent = *p;
155 xkey = rb_entry(parent, struct key, serial_node);
156
157 if (key->serial < xkey->serial)
158 p = &(*p)->rb_left;
159 else if (key->serial > xkey->serial)
160 p = &(*p)->rb_right;
161 else
162 goto serial_exists;
163 }
David Howells9ad08302007-02-06 13:45:51 +0000164
165 /* we've found a suitable hole - arrange for this key to occupy it */
166 rb_link_node(&key->serial_node, parent, p);
167 rb_insert_color(&key->serial_node, &key_serial_tree);
168
169 spin_unlock(&key_serial_lock);
170 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172 /* we found a key with the proposed serial number - walk the tree from
173 * that point looking for the next unused serial number */
Michael LeMaye51f6d32006-06-26 00:24:54 -0700174serial_exists:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 for (;;) {
Michael LeMaye51f6d32006-06-26 00:24:54 -0700176 key->serial++;
David Howells9ad08302007-02-06 13:45:51 +0000177 if (key->serial < 3) {
178 key->serial = 3;
179 goto attempt_insertion;
180 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182 parent = rb_next(parent);
183 if (!parent)
David Howells9ad08302007-02-06 13:45:51 +0000184 goto attempt_insertion;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186 xkey = rb_entry(parent, struct key, serial_node);
187 if (key->serial < xkey->serial)
David Howells9ad08302007-02-06 13:45:51 +0000188 goto attempt_insertion;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 }
David Howellsa8b17ed2011-01-20 16:38:27 +0000190}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
David Howells973c9f42011-01-20 16:38:33 +0000192/**
193 * key_alloc - Allocate a key of the specified type.
194 * @type: The type of key to allocate.
195 * @desc: The key description to allow the key to be searched out.
196 * @uid: The owner of the new key.
197 * @gid: The group ID for the new key's group permissions.
198 * @cred: The credentials specifying UID namespace.
Linus Torvalds028db3e2019-07-10 18:43:43 -0700199 * @perm: The permissions mask of the new key.
David Howells973c9f42011-01-20 16:38:33 +0000200 * @flags: Flags specifying quota properties.
Mat Martineau2b6aa412016-08-31 16:05:43 -0700201 * @restrict_link: Optional link restriction for new keyrings.
David Howells973c9f42011-01-20 16:38:33 +0000202 *
203 * Allocate a key of the specified type with the attributes given. The key is
204 * returned in an uninstantiated state and the caller needs to instantiate the
205 * key before returning.
206 *
Mat Martineau2b6aa412016-08-31 16:05:43 -0700207 * The restrict_link structure (if not NULL) will be freed when the
208 * keyring is destroyed, so it must be dynamically allocated.
209 *
David Howells973c9f42011-01-20 16:38:33 +0000210 * The user's key count quota is updated to reflect the creation of the key and
211 * the user's key data quota has the default for the key type reserved. The
212 * instantiation function should amend this as necessary. If insufficient
213 * quota is available, -EDQUOT will be returned.
214 *
215 * The LSM security modules can prevent a key being created, in which case
216 * -EACCES will be returned.
217 *
218 * Returns a pointer to the new key if successful and an error code otherwise.
219 *
220 * Note that the caller needs to ensure the key type isn't uninstantiated.
221 * Internally this can be done by locking key_types_sem. Externally, this can
222 * be done by either never unregistering the key type, or making sure
223 * key_alloc() calls don't race with module unloading.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 */
225struct key *key_alloc(struct key_type *type, const char *desc,
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -0800226 kuid_t uid, kgid_t gid, const struct cred *cred,
Linus Torvalds028db3e2019-07-10 18:43:43 -0700227 key_perm_t perm, unsigned long flags,
Mat Martineau2b6aa412016-08-31 16:05:43 -0700228 struct key_restriction *restrict_link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
230 struct key_user *user = NULL;
231 struct key *key;
232 size_t desclen, quotalen;
David Howells29db9192005-10-30 15:02:44 -0800233 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
235 key = ERR_PTR(-EINVAL);
236 if (!desc || !*desc)
237 goto error;
238
David Howellsb9fffa32011-03-07 15:05:59 +0000239 if (type->vet_description) {
240 ret = type->vet_description(desc);
241 if (ret < 0) {
242 key = ERR_PTR(ret);
243 goto error;
244 }
245 }
246
David Howells16feef42013-09-24 10:35:15 +0100247 desclen = strlen(desc);
248 quotalen = desclen + 1 + type->def_datalen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250 /* get hold of the key tracking for this user */
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -0800251 user = key_user_lookup(uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 if (!user)
253 goto no_memory_1;
254
255 /* check that the user's quota permits allocation of another key and
256 * its description */
David Howells7e047ef2006-06-26 00:24:50 -0700257 if (!(flags & KEY_ALLOC_NOT_IN_QUOTA)) {
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -0800258 unsigned maxkeys = uid_eq(uid, GLOBAL_ROOT_UID) ?
David Howells0b77f5b2008-04-29 01:01:32 -0700259 key_quota_root_maxkeys : key_quota_maxkeys;
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -0800260 unsigned maxbytes = uid_eq(uid, GLOBAL_ROOT_UID) ?
David Howells0b77f5b2008-04-29 01:01:32 -0700261 key_quota_root_maxbytes : key_quota_maxbytes;
262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 spin_lock(&user->lock);
David Howells7e047ef2006-06-26 00:24:50 -0700264 if (!(flags & KEY_ALLOC_QUOTA_OVERRUN)) {
Eric Biggersa08bf912019-02-14 16:20:01 +0000265 if (user->qnkeys + 1 > maxkeys ||
266 user->qnbytes + quotalen > maxbytes ||
David Howells0b77f5b2008-04-29 01:01:32 -0700267 user->qnbytes + quotalen < user->qnbytes)
David Howells7e047ef2006-06-26 00:24:50 -0700268 goto no_quota;
269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
271 user->qnkeys++;
272 user->qnbytes += quotalen;
273 spin_unlock(&user->lock);
274 }
275
276 /* allocate and initialise the key and its description */
David Howells2480f572013-12-02 11:24:18 +0000277 key = kmem_cache_zalloc(key_jar, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 if (!key)
279 goto no_memory_2;
280
Dan Carpenter50579752014-12-11 19:59:38 +0000281 key->index_key.desc_len = desclen;
282 key->index_key.description = kmemdup(desc, desclen + 1, GFP_KERNEL);
Insu Yun27720e72015-10-21 14:04:47 +0100283 if (!key->index_key.description)
Dan Carpenter50579752014-12-11 19:59:38 +0000284 goto no_memory_3;
David Howells355ef8e2019-06-26 21:02:32 +0100285 key->index_key.type = type;
David Howellsf771fde2019-06-26 21:02:31 +0100286 key_set_index_key(&key->index_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Elena Reshetovafff29292017-03-31 15:20:48 +0300288 refcount_set(&key->usage, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 init_rwsem(&key->sem);
David Howells7845bc392011-11-16 11:15:54 +0000290 lockdep_set_class(&key->sem, &type->lock_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 key->user = user;
292 key->quotalen = quotalen;
293 key->datalen = type->def_datalen;
294 key->uid = uid;
295 key->gid = gid;
Linus Torvalds028db3e2019-07-10 18:43:43 -0700296 key->perm = perm;
David Howells5ac7eac2016-04-06 16:14:24 +0100297 key->restrict_link = restrict_link;
David Howells7c1857b2019-02-14 16:20:37 +0000298 key->last_used_at = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
David Howells7e047ef2006-06-26 00:24:50 -0700300 if (!(flags & KEY_ALLOC_NOT_IN_QUOTA))
David Howells76d8aea2005-06-23 22:00:49 -0700301 key->flags |= 1 << KEY_FLAG_IN_QUOTA;
David Howells5d2787c2016-02-09 16:40:46 +0000302 if (flags & KEY_ALLOC_BUILT_IN)
303 key->flags |= 1 << KEY_FLAG_BUILTIN;
Eric Biggers237bbd22017-09-18 11:37:03 -0700304 if (flags & KEY_ALLOC_UID_KEYRING)
305 key->flags |= 1 << KEY_FLAG_UID_KEYRING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307#ifdef KEY_DEBUGGING
308 key->magic = KEY_DEBUG_MAGIC;
309#endif
310
David Howells29db9192005-10-30 15:02:44 -0800311 /* let the security module know about the key */
David Howellsd84f4f92008-11-14 10:39:23 +1100312 ret = security_key_alloc(key, cred, flags);
David Howells29db9192005-10-30 15:02:44 -0800313 if (ret < 0)
314 goto security_error;
315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 /* publish the key by giving it a serial number */
David Howells3b6e4de2019-06-26 21:02:32 +0100317 refcount_inc(&key->domain_tag->usage);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 atomic_inc(&user->nkeys);
319 key_alloc_serial(key);
320
David Howells29db9192005-10-30 15:02:44 -0800321error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 return key;
323
David Howells29db9192005-10-30 15:02:44 -0800324security_error:
325 kfree(key->description);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 kmem_cache_free(key_jar, key);
David Howells7e047ef2006-06-26 00:24:50 -0700327 if (!(flags & KEY_ALLOC_NOT_IN_QUOTA)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 spin_lock(&user->lock);
329 user->qnkeys--;
330 user->qnbytes -= quotalen;
331 spin_unlock(&user->lock);
332 }
333 key_user_put(user);
David Howells29db9192005-10-30 15:02:44 -0800334 key = ERR_PTR(ret);
335 goto error;
336
337no_memory_3:
338 kmem_cache_free(key_jar, key);
339no_memory_2:
David Howells7e047ef2006-06-26 00:24:50 -0700340 if (!(flags & KEY_ALLOC_NOT_IN_QUOTA)) {
David Howells29db9192005-10-30 15:02:44 -0800341 spin_lock(&user->lock);
342 user->qnkeys--;
343 user->qnbytes -= quotalen;
344 spin_unlock(&user->lock);
345 }
346 key_user_put(user);
347no_memory_1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 key = ERR_PTR(-ENOMEM);
349 goto error;
350
David Howells29db9192005-10-30 15:02:44 -0800351no_quota:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 spin_unlock(&user->lock);
353 key_user_put(user);
354 key = ERR_PTR(-EDQUOT);
355 goto error;
David Howellsa8b17ed2011-01-20 16:38:27 +0000356}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357EXPORT_SYMBOL(key_alloc);
358
David Howells973c9f42011-01-20 16:38:33 +0000359/**
360 * key_payload_reserve - Adjust data quota reservation for the key's payload
361 * @key: The key to make the reservation for.
362 * @datalen: The amount of data payload the caller now wants.
363 *
364 * Adjust the amount of the owning user's key data quota that a key reserves.
365 * If the amount is increased, then -EDQUOT may be returned if there isn't
366 * enough free quota available.
367 *
368 * If successful, 0 is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 */
370int key_payload_reserve(struct key *key, size_t datalen)
371{
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700372 int delta = (int)datalen - key->datalen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 int ret = 0;
374
375 key_check(key);
376
377 /* contemplate the quota adjustment */
David Howells76d8aea2005-06-23 22:00:49 -0700378 if (delta != 0 && test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) {
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -0800379 unsigned maxbytes = uid_eq(key->user->uid, GLOBAL_ROOT_UID) ?
David Howells0b77f5b2008-04-29 01:01:32 -0700380 key_quota_root_maxbytes : key_quota_maxbytes;
381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 spin_lock(&key->user->lock);
383
384 if (delta > 0 &&
Yang Xu2e356102020-02-28 12:41:51 +0800385 (key->user->qnbytes + delta > maxbytes ||
David Howells0b77f5b2008-04-29 01:01:32 -0700386 key->user->qnbytes + delta < key->user->qnbytes)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 ret = -EDQUOT;
388 }
389 else {
390 key->user->qnbytes += delta;
391 key->quotalen += delta;
392 }
393 spin_unlock(&key->user->lock);
394 }
395
396 /* change the recorded data length if that didn't generate an error */
397 if (ret == 0)
398 key->datalen = datalen;
399
400 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000401}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402EXPORT_SYMBOL(key_payload_reserve);
403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404/*
David Howells363b02d2017-10-04 16:43:25 +0100405 * Change the key state to being instantiated.
406 */
407static void mark_key_instantiated(struct key *key, int reject_error)
408{
409 /* Commit the payload before setting the state; barrier versus
410 * key_read_state().
411 */
412 smp_store_release(&key->state,
413 (reject_error < 0) ? reject_error : KEY_IS_POSITIVE);
414}
415
416/*
David Howells973c9f42011-01-20 16:38:33 +0000417 * Instantiate a key and link it into the target keyring atomically. Must be
418 * called with the target keyring's semaphore writelocked. The target key's
419 * semaphore need not be locked as instantiation is serialised by
420 * key_construction_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 */
422static int __key_instantiate_and_link(struct key *key,
David Howellscf7f6012012-09-13 13:06:29 +0100423 struct key_preparsed_payload *prep,
David Howells3e301482005-06-23 22:00:56 -0700424 struct key *keyring,
David Howellsf70e2e02010-04-30 14:32:39 +0100425 struct key *authkey,
David Howellsb2a4df22013-09-24 10:35:18 +0100426 struct assoc_array_edit **_edit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427{
428 int ret, awaken;
429
430 key_check(key);
431 key_check(keyring);
432
433 awaken = 0;
434 ret = -EBUSY;
435
David Howells76181c12007-10-16 23:29:46 -0700436 mutex_lock(&key_construction_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438 /* can't instantiate twice */
David Howells363b02d2017-10-04 16:43:25 +0100439 if (key->state == KEY_IS_UNINSTANTIATED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 /* instantiate the key */
David Howellscf7f6012012-09-13 13:06:29 +0100441 ret = key->type->instantiate(key, prep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
443 if (ret == 0) {
444 /* mark the key as being instantiated */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 atomic_inc(&key->user->nikeys);
David Howells363b02d2017-10-04 16:43:25 +0100446 mark_key_instantiated(key, 0);
David Howellsf7e47672020-01-14 17:07:11 +0000447 notify_key(key, NOTIFY_KEY_INSTANTIATED, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
David Howells76d8aea2005-06-23 22:00:49 -0700449 if (test_and_clear_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 awaken = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
452 /* and link it into the destination keyring */
Mimi Zohard3600bc2015-11-10 08:34:46 -0500453 if (keyring) {
David Howellseee04502016-01-27 01:02:03 +0000454 if (test_bit(KEY_FLAG_KEEP, &keyring->flags))
455 set_bit(KEY_FLAG_KEEP, &key->flags);
Mimi Zohard3600bc2015-11-10 08:34:46 -0500456
David Howellsf7e47672020-01-14 17:07:11 +0000457 __key_link(keyring, key, _edit);
Mimi Zohard3600bc2015-11-10 08:34:46 -0500458 }
David Howells3e301482005-06-23 22:00:56 -0700459
460 /* disable the authorisation key */
David Howellsd84f4f92008-11-14 10:39:23 +1100461 if (authkey)
David Howellsa09003b2019-06-19 16:10:15 +0100462 key_invalidate(authkey);
David Howells7dfa0ca62014-07-18 18:56:34 +0100463
Baolin Wang0a9dd0e2017-11-15 16:38:45 +0000464 if (prep->expiry != TIME64_MAX) {
David Howells7dfa0ca62014-07-18 18:56:34 +0100465 key->expiry = prep->expiry;
466 key_schedule_gc(prep->expiry + key_gc_delay);
467 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 }
469 }
470
David Howells76181c12007-10-16 23:29:46 -0700471 mutex_unlock(&key_construction_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
473 /* wake up anyone waiting for a key to be constructed */
474 if (awaken)
David Howells76181c12007-10-16 23:29:46 -0700475 wake_up_bit(&key->flags, KEY_FLAG_USER_CONSTRUCT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
477 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000478}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
David Howells973c9f42011-01-20 16:38:33 +0000480/**
481 * key_instantiate_and_link - Instantiate a key and link it into the keyring.
482 * @key: The key to instantiate.
483 * @data: The data to use to instantiate the keyring.
484 * @datalen: The length of @data.
485 * @keyring: Keyring to create a link in on success (or NULL).
486 * @authkey: The authorisation token permitting instantiation.
487 *
488 * Instantiate a key that's in the uninstantiated state using the provided data
489 * and, if successful, link it in to the destination keyring if one is
490 * supplied.
491 *
492 * If successful, 0 is returned, the authorisation token is revoked and anyone
493 * waiting for the key is woken up. If the key was already instantiated,
494 * -EBUSY will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 */
496int key_instantiate_and_link(struct key *key,
497 const void *data,
498 size_t datalen,
David Howells3e301482005-06-23 22:00:56 -0700499 struct key *keyring,
David Howellsd84f4f92008-11-14 10:39:23 +1100500 struct key *authkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
David Howellscf7f6012012-09-13 13:06:29 +0100502 struct key_preparsed_payload prep;
David Howellsdf593ee2019-05-30 11:37:39 +0100503 struct assoc_array_edit *edit = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 int ret;
505
David Howellscf7f6012012-09-13 13:06:29 +0100506 memset(&prep, 0, sizeof(prep));
507 prep.data = data;
508 prep.datalen = datalen;
509 prep.quotalen = key->type->def_datalen;
Baolin Wang0a9dd0e2017-11-15 16:38:45 +0000510 prep.expiry = TIME64_MAX;
David Howellscf7f6012012-09-13 13:06:29 +0100511 if (key->type->preparse) {
512 ret = key->type->preparse(&prep);
513 if (ret < 0)
514 goto error;
515 }
516
David Howellsf70e2e02010-04-30 14:32:39 +0100517 if (keyring) {
David Howellsdf593ee2019-05-30 11:37:39 +0100518 ret = __key_link_lock(keyring, &key->index_key);
Mat Martineau4a420892016-10-04 16:27:32 -0700519 if (ret < 0)
520 goto error;
521
David Howellsdf593ee2019-05-30 11:37:39 +0100522 ret = __key_link_begin(keyring, &key->index_key, &edit);
523 if (ret < 0)
524 goto error_link_end;
525
Mat Martineau2b6aa412016-08-31 16:05:43 -0700526 if (keyring->restrict_link && keyring->restrict_link->check) {
527 struct key_restriction *keyres = keyring->restrict_link;
528
529 ret = keyres->check(keyring, key->type, &prep.payload,
530 keyres->key);
David Howells5ac7eac2016-04-06 16:14:24 +0100531 if (ret < 0)
Mat Martineau4a420892016-10-04 16:27:32 -0700532 goto error_link_end;
David Howells5ac7eac2016-04-06 16:14:24 +0100533 }
David Howellsf70e2e02010-04-30 14:32:39 +0100534 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
David Howellsb2a4df22013-09-24 10:35:18 +0100536 ret = __key_instantiate_and_link(key, &prep, keyring, authkey, &edit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Mat Martineau4a420892016-10-04 16:27:32 -0700538error_link_end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 if (keyring)
David Howellsb2a4df22013-09-24 10:35:18 +0100540 __key_link_end(keyring, &key->index_key, edit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
David Howells4d8c0252014-07-18 18:56:34 +0100542error:
David Howellscf7f6012012-09-13 13:06:29 +0100543 if (key->type->preparse)
544 key->type->free_preparse(&prep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000546}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
548EXPORT_SYMBOL(key_instantiate_and_link);
549
David Howells973c9f42011-01-20 16:38:33 +0000550/**
David Howellsfdd1b942011-03-07 15:06:09 +0000551 * key_reject_and_link - Negatively instantiate a key and link it into the keyring.
David Howells973c9f42011-01-20 16:38:33 +0000552 * @key: The key to instantiate.
553 * @timeout: The timeout on the negative key.
David Howellsfdd1b942011-03-07 15:06:09 +0000554 * @error: The error to return when the key is hit.
David Howells973c9f42011-01-20 16:38:33 +0000555 * @keyring: Keyring to create a link in on success (or NULL).
556 * @authkey: The authorisation token permitting instantiation.
557 *
558 * Negatively instantiate a key that's in the uninstantiated state and, if
David Howellsfdd1b942011-03-07 15:06:09 +0000559 * successful, set its timeout and stored error and link it in to the
560 * destination keyring if one is supplied. The key and any links to the key
561 * will be automatically garbage collected after the timeout expires.
David Howells973c9f42011-01-20 16:38:33 +0000562 *
563 * Negative keys are used to rate limit repeated request_key() calls by causing
David Howellsfdd1b942011-03-07 15:06:09 +0000564 * them to return the stored error code (typically ENOKEY) until the negative
565 * key expires.
David Howells973c9f42011-01-20 16:38:33 +0000566 *
567 * If successful, 0 is returned, the authorisation token is revoked and anyone
568 * waiting for the key is woken up. If the key was already instantiated,
569 * -EBUSY will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 */
David Howellsfdd1b942011-03-07 15:06:09 +0000571int key_reject_and_link(struct key *key,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 unsigned timeout,
David Howellsfdd1b942011-03-07 15:06:09 +0000573 unsigned error,
David Howells3e301482005-06-23 22:00:56 -0700574 struct key *keyring,
David Howellsd84f4f92008-11-14 10:39:23 +1100575 struct key *authkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576{
David Howellsdf593ee2019-05-30 11:37:39 +0100577 struct assoc_array_edit *edit = NULL;
David Howellsf70e2e02010-04-30 14:32:39 +0100578 int ret, awaken, link_ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580 key_check(key);
581 key_check(keyring);
582
583 awaken = 0;
584 ret = -EBUSY;
585
David Howells5ac7eac2016-04-06 16:14:24 +0100586 if (keyring) {
587 if (keyring->restrict_link)
588 return -EPERM;
589
David Howellsdf593ee2019-05-30 11:37:39 +0100590 link_ret = __key_link_lock(keyring, &key->index_key);
591 if (link_ret == 0) {
592 link_ret = __key_link_begin(keyring, &key->index_key, &edit);
593 if (link_ret < 0)
594 __key_link_end(keyring, &key->index_key, edit);
595 }
David Howells5ac7eac2016-04-06 16:14:24 +0100596 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
David Howells76181c12007-10-16 23:29:46 -0700598 mutex_lock(&key_construction_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
600 /* can't instantiate twice */
David Howells363b02d2017-10-04 16:43:25 +0100601 if (key->state == KEY_IS_UNINSTANTIATED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 /* mark the key as being negatively instantiated */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 atomic_inc(&key->user->nikeys);
David Howells363b02d2017-10-04 16:43:25 +0100604 mark_key_instantiated(key, -error);
David Howellsf7e47672020-01-14 17:07:11 +0000605 notify_key(key, NOTIFY_KEY_INSTANTIATED, -error);
Baolin Wang074d5892017-11-15 16:38:45 +0000606 key->expiry = ktime_get_real_seconds() + timeout;
David Howellsc08ef802009-09-14 17:26:13 +0100607 key_schedule_gc(key->expiry + key_gc_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
David Howells76d8aea2005-06-23 22:00:49 -0700609 if (test_and_clear_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 awaken = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 ret = 0;
613
614 /* and link it into the destination keyring */
David Howellsf70e2e02010-04-30 14:32:39 +0100615 if (keyring && link_ret == 0)
David Howellsf7e47672020-01-14 17:07:11 +0000616 __key_link(keyring, key, &edit);
David Howells3e301482005-06-23 22:00:56 -0700617
618 /* disable the authorisation key */
David Howellsd84f4f92008-11-14 10:39:23 +1100619 if (authkey)
David Howellsa09003b2019-06-19 16:10:15 +0100620 key_invalidate(authkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 }
622
David Howells76181c12007-10-16 23:29:46 -0700623 mutex_unlock(&key_construction_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Dan Carpenter38327422016-06-16 15:48:57 +0100625 if (keyring && link_ret == 0)
David Howellsb2a4df22013-09-24 10:35:18 +0100626 __key_link_end(keyring, &key->index_key, edit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
628 /* wake up anyone waiting for a key to be constructed */
629 if (awaken)
David Howells76181c12007-10-16 23:29:46 -0700630 wake_up_bit(&key->flags, KEY_FLAG_USER_CONSTRUCT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
David Howellsf70e2e02010-04-30 14:32:39 +0100632 return ret == 0 ? link_ret : ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000633}
David Howellsfdd1b942011-03-07 15:06:09 +0000634EXPORT_SYMBOL(key_reject_and_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
David Howells973c9f42011-01-20 16:38:33 +0000636/**
637 * key_put - Discard a reference to a key.
638 * @key: The key to discard a reference from.
639 *
640 * Discard a reference to a key, and when all the references are gone, we
641 * schedule the cleanup task to come and pull it out of the tree in process
642 * context at some later time.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 */
644void key_put(struct key *key)
645{
646 if (key) {
647 key_check(key);
648
Elena Reshetovafff29292017-03-31 15:20:48 +0300649 if (refcount_dec_and_test(&key->usage))
Tejun Heo3b07e9c2012-08-20 14:51:24 -0700650 schedule_work(&key_gc_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 }
David Howellsa8b17ed2011-01-20 16:38:27 +0000652}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653EXPORT_SYMBOL(key_put);
654
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655/*
David Howells973c9f42011-01-20 16:38:33 +0000656 * Find a key by its serial number.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 */
658struct key *key_lookup(key_serial_t id)
659{
660 struct rb_node *n;
661 struct key *key;
662
663 spin_lock(&key_serial_lock);
664
665 /* search the tree for the specified key */
666 n = key_serial_tree.rb_node;
667 while (n) {
668 key = rb_entry(n, struct key, serial_node);
669
670 if (id < key->serial)
671 n = n->rb_left;
672 else if (id > key->serial)
673 n = n->rb_right;
674 else
675 goto found;
676 }
677
David Howells973c9f42011-01-20 16:38:33 +0000678not_found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 key = ERR_PTR(-ENOKEY);
680 goto error;
681
David Howells973c9f42011-01-20 16:38:33 +0000682found:
Mark Rutland92347cf2017-06-08 14:47:41 +0100683 /* A key is allowed to be looked up only if someone still owns a
684 * reference to it - otherwise it's awaiting the gc.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 */
Mark Rutland92347cf2017-06-08 14:47:41 +0100686 if (!refcount_inc_not_zero(&key->usage))
687 goto not_found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
David Howells973c9f42011-01-20 16:38:33 +0000689error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 spin_unlock(&key_serial_lock);
691 return key;
David Howellsa8b17ed2011-01-20 16:38:27 +0000692}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694/*
David Howells973c9f42011-01-20 16:38:33 +0000695 * Find and lock the specified key type against removal.
696 *
697 * We return with the sem read-locked if successful. If the type wasn't
698 * available -ENOKEY is returned instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 */
700struct key_type *key_type_lookup(const char *type)
701{
702 struct key_type *ktype;
703
704 down_read(&key_types_sem);
705
706 /* look up the key type to see if it's one of the registered kernel
707 * types */
708 list_for_each_entry(ktype, &key_types_list, link) {
709 if (strcmp(ktype->name, type) == 0)
710 goto found_kernel_type;
711 }
712
713 up_read(&key_types_sem);
714 ktype = ERR_PTR(-ENOKEY);
715
David Howells973c9f42011-01-20 16:38:33 +0000716found_kernel_type:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 return ktype;
David Howellsa8b17ed2011-01-20 16:38:27 +0000718}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Bryan Schumaker59e6b9c2012-02-24 14:14:50 -0500720void key_set_timeout(struct key *key, unsigned timeout)
721{
Baolin Wang074d5892017-11-15 16:38:45 +0000722 time64_t expiry = 0;
Bryan Schumaker59e6b9c2012-02-24 14:14:50 -0500723
724 /* make the changes with the locks held to prevent races */
725 down_write(&key->sem);
726
Baolin Wang074d5892017-11-15 16:38:45 +0000727 if (timeout > 0)
728 expiry = ktime_get_real_seconds() + timeout;
Bryan Schumaker59e6b9c2012-02-24 14:14:50 -0500729
730 key->expiry = expiry;
731 key_schedule_gc(key->expiry + key_gc_delay);
732
733 up_write(&key->sem);
734}
735EXPORT_SYMBOL_GPL(key_set_timeout);
736
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737/*
David Howells973c9f42011-01-20 16:38:33 +0000738 * Unlock a key type locked by key_type_lookup().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 */
740void key_type_put(struct key_type *ktype)
741{
742 up_read(&key_types_sem);
David Howellsa8b17ed2011-01-20 16:38:27 +0000743}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745/*
David Howells973c9f42011-01-20 16:38:33 +0000746 * Attempt to update an existing key.
747 *
748 * The key is given to us with an incremented refcount that we need to discard
749 * if we get an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 */
David Howells664cceb2005-09-28 17:03:15 +0100751static inline key_ref_t __key_update(key_ref_t key_ref,
David Howellscf7f6012012-09-13 13:06:29 +0100752 struct key_preparsed_payload *prep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753{
David Howells664cceb2005-09-28 17:03:15 +0100754 struct key *key = key_ref_to_ptr(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 int ret;
756
757 /* need write permission on the key to update it */
David Howellsf5895942014-03-14 17:44:49 +0000758 ret = key_permission(key_ref, KEY_NEED_WRITE);
David Howells29db9192005-10-30 15:02:44 -0800759 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 goto error;
761
762 ret = -EEXIST;
763 if (!key->type->update)
764 goto error;
765
766 down_write(&key->sem);
767
David Howellscf7f6012012-09-13 13:06:29 +0100768 ret = key->type->update(key, prep);
David Howellsf7e47672020-01-14 17:07:11 +0000769 if (ret == 0) {
David Howells363b02d2017-10-04 16:43:25 +0100770 /* Updating a negative key positively instantiates it */
771 mark_key_instantiated(key, 0);
David Howellsf7e47672020-01-14 17:07:11 +0000772 notify_key(key, NOTIFY_KEY_UPDATED, 0);
773 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
775 up_write(&key->sem);
776
777 if (ret < 0)
778 goto error;
David Howells664cceb2005-09-28 17:03:15 +0100779out:
780 return key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
David Howells664cceb2005-09-28 17:03:15 +0100782error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 key_put(key);
David Howells664cceb2005-09-28 17:03:15 +0100784 key_ref = ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 goto out;
David Howellsa8b17ed2011-01-20 16:38:27 +0000786}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
David Howells973c9f42011-01-20 16:38:33 +0000788/**
789 * key_create_or_update - Update or create and instantiate a key.
790 * @keyring_ref: A pointer to the destination keyring with possession flag.
791 * @type: The type of key.
792 * @description: The searchable description for the key.
793 * @payload: The data to use to instantiate or update the key.
794 * @plen: The length of @payload.
Linus Torvalds028db3e2019-07-10 18:43:43 -0700795 * @perm: The permissions mask for a new key.
David Howells973c9f42011-01-20 16:38:33 +0000796 * @flags: The quota flags for a new key.
797 *
798 * Search the destination keyring for a key of the same description and if one
799 * is found, update it, otherwise create and instantiate a new one and create a
800 * link to it from that keyring.
801 *
802 * If perm is KEY_PERM_UNDEF then an appropriate key permissions mask will be
803 * concocted.
804 *
805 * Returns a pointer to the new key if successful, -ENODEV if the key type
806 * wasn't available, -ENOTDIR if the keyring wasn't a keyring, -EACCES if the
807 * caller isn't permitted to modify the keyring or the LSM did not permit
808 * creation of the key.
809 *
810 * On success, the possession flag from the keyring ref will be tacked on to
811 * the key ref before it is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 */
David Howells664cceb2005-09-28 17:03:15 +0100813key_ref_t key_create_or_update(key_ref_t keyring_ref,
814 const char *type,
815 const char *description,
816 const void *payload,
817 size_t plen,
Linus Torvalds028db3e2019-07-10 18:43:43 -0700818 key_perm_t perm,
David Howells7e047ef2006-06-26 00:24:50 -0700819 unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820{
David Howells16feef42013-09-24 10:35:15 +0100821 struct keyring_index_key index_key = {
822 .description = description,
823 };
David Howellscf7f6012012-09-13 13:06:29 +0100824 struct key_preparsed_payload prep;
David Howellsdf593ee2019-05-30 11:37:39 +0100825 struct assoc_array_edit *edit = NULL;
David Howellsd84f4f92008-11-14 10:39:23 +1100826 const struct cred *cred = current_cred();
David Howells664cceb2005-09-28 17:03:15 +0100827 struct key *keyring, *key = NULL;
David Howells664cceb2005-09-28 17:03:15 +0100828 key_ref_t key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 int ret;
Mat Martineau2b6aa412016-08-31 16:05:43 -0700830 struct key_restriction *restrict_link = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 /* look up the key type to see if it's one of the registered kernel
833 * types */
David Howells16feef42013-09-24 10:35:15 +0100834 index_key.type = key_type_lookup(type);
835 if (IS_ERR(index_key.type)) {
David Howells664cceb2005-09-28 17:03:15 +0100836 key_ref = ERR_PTR(-ENODEV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 goto error;
838 }
839
David Howells664cceb2005-09-28 17:03:15 +0100840 key_ref = ERR_PTR(-EINVAL);
David Howellsc06cfb02014-09-16 17:36:06 +0100841 if (!index_key.type->instantiate ||
David Howells16feef42013-09-24 10:35:15 +0100842 (!index_key.description && !index_key.type->preparse))
David Howellscf7f6012012-09-13 13:06:29 +0100843 goto error_put_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
David Howells664cceb2005-09-28 17:03:15 +0100845 keyring = key_ref_to_ptr(keyring_ref);
846
847 key_check(keyring);
848
David Howells5ac7eac2016-04-06 16:14:24 +0100849 if (!(flags & KEY_ALLOC_BYPASS_RESTRICTION))
850 restrict_link = keyring->restrict_link;
851
David Howellsc3a9d652006-04-10 15:15:21 +0100852 key_ref = ERR_PTR(-ENOTDIR);
853 if (keyring->type != &key_type_keyring)
David Howellscf7f6012012-09-13 13:06:29 +0100854 goto error_put_type;
855
856 memset(&prep, 0, sizeof(prep));
857 prep.data = payload;
858 prep.datalen = plen;
David Howells16feef42013-09-24 10:35:15 +0100859 prep.quotalen = index_key.type->def_datalen;
Baolin Wang0a9dd0e2017-11-15 16:38:45 +0000860 prep.expiry = TIME64_MAX;
David Howells16feef42013-09-24 10:35:15 +0100861 if (index_key.type->preparse) {
862 ret = index_key.type->preparse(&prep);
David Howellscf7f6012012-09-13 13:06:29 +0100863 if (ret < 0) {
864 key_ref = ERR_PTR(ret);
David Howells4d8c0252014-07-18 18:56:34 +0100865 goto error_free_prep;
David Howellscf7f6012012-09-13 13:06:29 +0100866 }
David Howells16feef42013-09-24 10:35:15 +0100867 if (!index_key.description)
868 index_key.description = prep.description;
David Howellscf7f6012012-09-13 13:06:29 +0100869 key_ref = ERR_PTR(-EINVAL);
David Howells16feef42013-09-24 10:35:15 +0100870 if (!index_key.description)
David Howellscf7f6012012-09-13 13:06:29 +0100871 goto error_free_prep;
872 }
David Howells16feef42013-09-24 10:35:15 +0100873 index_key.desc_len = strlen(index_key.description);
David Howellsf771fde2019-06-26 21:02:31 +0100874 key_set_index_key(&index_key);
David Howellsc3a9d652006-04-10 15:15:21 +0100875
David Howellsdf593ee2019-05-30 11:37:39 +0100876 ret = __key_link_lock(keyring, &index_key);
Mat Martineau4a420892016-10-04 16:27:32 -0700877 if (ret < 0) {
878 key_ref = ERR_PTR(ret);
879 goto error_free_prep;
880 }
881
David Howellsdf593ee2019-05-30 11:37:39 +0100882 ret = __key_link_begin(keyring, &index_key, &edit);
883 if (ret < 0) {
884 key_ref = ERR_PTR(ret);
885 goto error_link_end;
886 }
887
Mat Martineau2b6aa412016-08-31 16:05:43 -0700888 if (restrict_link && restrict_link->check) {
889 ret = restrict_link->check(keyring, index_key.type,
890 &prep.payload, restrict_link->key);
David Howells5ac7eac2016-04-06 16:14:24 +0100891 if (ret < 0) {
892 key_ref = ERR_PTR(ret);
Mat Martineau4a420892016-10-04 16:27:32 -0700893 goto error_link_end;
David Howells5ac7eac2016-04-06 16:14:24 +0100894 }
895 }
David Howells008643b2013-08-30 16:07:37 +0100896
David Howells664cceb2005-09-28 17:03:15 +0100897 /* if we're going to allocate a new key, we're going to have
898 * to modify the keyring */
David Howellsf5895942014-03-14 17:44:49 +0000899 ret = key_permission(keyring_ref, KEY_NEED_WRITE);
David Howells29db9192005-10-30 15:02:44 -0800900 if (ret < 0) {
901 key_ref = ERR_PTR(ret);
David Howellscf7f6012012-09-13 13:06:29 +0100902 goto error_link_end;
David Howells29db9192005-10-30 15:02:44 -0800903 }
David Howells664cceb2005-09-28 17:03:15 +0100904
David Howells1d9b7d92006-03-25 03:06:52 -0800905 /* if it's possible to update this type of key, search for an existing
906 * key of the same type and description in the destination keyring and
907 * update that instead if possible
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 */
David Howells16feef42013-09-24 10:35:15 +0100909 if (index_key.type->update) {
David Howellsb2a4df22013-09-24 10:35:18 +0100910 key_ref = find_key_to_update(keyring_ref, &index_key);
911 if (key_ref)
David Howells1d9b7d92006-03-25 03:06:52 -0800912 goto found_matching_key;
913 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Linus Torvalds028db3e2019-07-10 18:43:43 -0700915 /* if the client doesn't provide, decide on the permissions we want */
916 if (perm == KEY_PERM_UNDEF) {
917 perm = KEY_POS_VIEW | KEY_POS_SEARCH | KEY_POS_LINK | KEY_POS_SETATTR;
918 perm |= KEY_USR_VIEW;
919
920 if (index_key.type->read)
921 perm |= KEY_POS_READ;
922
923 if (index_key.type == &key_type_keyring ||
924 index_key.type->update)
925 perm |= KEY_POS_WRITE;
926 }
927
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 /* allocate a new key */
David Howells16feef42013-09-24 10:35:15 +0100929 key = key_alloc(index_key.type, index_key.description,
Linus Torvalds028db3e2019-07-10 18:43:43 -0700930 cred->fsuid, cred->fsgid, cred, perm, flags, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 if (IS_ERR(key)) {
David Howellse231c2e2008-02-07 00:15:26 -0800932 key_ref = ERR_CAST(key);
David Howellscf7f6012012-09-13 13:06:29 +0100933 goto error_link_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 }
935
936 /* instantiate it and link it into the target keyring */
David Howellsb2a4df22013-09-24 10:35:18 +0100937 ret = __key_instantiate_and_link(key, &prep, keyring, NULL, &edit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 if (ret < 0) {
939 key_put(key);
David Howells664cceb2005-09-28 17:03:15 +0100940 key_ref = ERR_PTR(ret);
David Howellscf7f6012012-09-13 13:06:29 +0100941 goto error_link_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 }
943
Lakshmi Ramasubramaniancb1aa382019-12-11 08:47:05 -0800944 ima_post_key_create_or_update(keyring, key, payload, plen,
945 flags, true);
946
David Howells664cceb2005-09-28 17:03:15 +0100947 key_ref = make_key_ref(key, is_key_possessed(keyring_ref));
948
David Howellscf7f6012012-09-13 13:06:29 +0100949error_link_end:
David Howellsb2a4df22013-09-24 10:35:18 +0100950 __key_link_end(keyring, &index_key, edit);
David Howellscf7f6012012-09-13 13:06:29 +0100951error_free_prep:
David Howells16feef42013-09-24 10:35:15 +0100952 if (index_key.type->preparse)
953 index_key.type->free_preparse(&prep);
David Howellscf7f6012012-09-13 13:06:29 +0100954error_put_type:
David Howells16feef42013-09-24 10:35:15 +0100955 key_type_put(index_key.type);
David Howellscf7f6012012-09-13 13:06:29 +0100956error:
David Howells664cceb2005-09-28 17:03:15 +0100957 return key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
959 found_matching_key:
960 /* we found a matching key, so we're going to try to update it
961 * - we can drop the locks first as we have the key pinned
962 */
David Howellsb2a4df22013-09-24 10:35:18 +0100963 __key_link_end(keyring, &index_key, edit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
David Howells60ff5b22017-10-12 16:00:41 +0100965 key = key_ref_to_ptr(key_ref);
966 if (test_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags)) {
967 ret = wait_for_key_construction(key, true);
968 if (ret < 0) {
969 key_ref_put(key_ref);
970 key_ref = ERR_PTR(ret);
971 goto error_free_prep;
972 }
973 }
974
David Howellscf7f6012012-09-13 13:06:29 +0100975 key_ref = __key_update(key_ref, &prep);
Lakshmi Ramasubramaniancb1aa382019-12-11 08:47:05 -0800976
977 if (!IS_ERR(key_ref))
978 ima_post_key_create_or_update(keyring, key,
979 payload, plen,
980 flags, false);
981
David Howellscf7f6012012-09-13 13:06:29 +0100982 goto error_free_prep;
David Howellsa8b17ed2011-01-20 16:38:27 +0000983}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984EXPORT_SYMBOL(key_create_or_update);
985
David Howells973c9f42011-01-20 16:38:33 +0000986/**
987 * key_update - Update a key's contents.
988 * @key_ref: The pointer (plus possession flag) to the key.
989 * @payload: The data to be used to update the key.
990 * @plen: The length of @payload.
991 *
992 * Attempt to update the contents of a key with the given payload data. The
993 * caller must be granted Write permission on the key. Negative keys can be
994 * instantiated by this method.
995 *
996 * Returns 0 on success, -EACCES if not permitted and -EOPNOTSUPP if the key
997 * type does not support updating. The key type may return other errors.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 */
David Howells664cceb2005-09-28 17:03:15 +0100999int key_update(key_ref_t key_ref, const void *payload, size_t plen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000{
David Howellscf7f6012012-09-13 13:06:29 +01001001 struct key_preparsed_payload prep;
David Howells664cceb2005-09-28 17:03:15 +01001002 struct key *key = key_ref_to_ptr(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 int ret;
1004
1005 key_check(key);
1006
1007 /* the key must be writable */
David Howellsf5895942014-03-14 17:44:49 +00001008 ret = key_permission(key_ref, KEY_NEED_WRITE);
David Howells29db9192005-10-30 15:02:44 -08001009 if (ret < 0)
Eric Biggers63a0b052017-06-08 14:48:47 +01001010 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
1012 /* attempt to update it if supported */
David Howellscf7f6012012-09-13 13:06:29 +01001013 if (!key->type->update)
Eric Biggers63a0b052017-06-08 14:48:47 +01001014 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
David Howellscf7f6012012-09-13 13:06:29 +01001016 memset(&prep, 0, sizeof(prep));
1017 prep.data = payload;
1018 prep.datalen = plen;
1019 prep.quotalen = key->type->def_datalen;
Baolin Wang0a9dd0e2017-11-15 16:38:45 +00001020 prep.expiry = TIME64_MAX;
David Howellscf7f6012012-09-13 13:06:29 +01001021 if (key->type->preparse) {
1022 ret = key->type->preparse(&prep);
1023 if (ret < 0)
1024 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 }
1026
David Howellscf7f6012012-09-13 13:06:29 +01001027 down_write(&key->sem);
1028
1029 ret = key->type->update(key, &prep);
David Howellsf7e47672020-01-14 17:07:11 +00001030 if (ret == 0) {
David Howells363b02d2017-10-04 16:43:25 +01001031 /* Updating a negative key positively instantiates it */
1032 mark_key_instantiated(key, 0);
David Howellsf7e47672020-01-14 17:07:11 +00001033 notify_key(key, NOTIFY_KEY_UPDATED, 0);
1034 }
David Howellscf7f6012012-09-13 13:06:29 +01001035
1036 up_write(&key->sem);
1037
David Howells4d8c0252014-07-18 18:56:34 +01001038error:
David Howellscf7f6012012-09-13 13:06:29 +01001039 if (key->type->preparse)
1040 key->type->free_preparse(&prep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +00001042}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043EXPORT_SYMBOL(key_update);
1044
David Howells973c9f42011-01-20 16:38:33 +00001045/**
1046 * key_revoke - Revoke a key.
1047 * @key: The key to be revoked.
1048 *
1049 * Mark a key as being revoked and ask the type to free up its resources. The
1050 * revocation timeout is set and the key and all its links will be
1051 * automatically garbage collected after key_gc_delay amount of time if they
1052 * are not manually dealt with first.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 */
1054void key_revoke(struct key *key)
1055{
Baolin Wang074d5892017-11-15 16:38:45 +00001056 time64_t time;
David Howells5d135442009-09-02 09:14:00 +01001057
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 key_check(key);
1059
David Howells76181c12007-10-16 23:29:46 -07001060 /* make sure no one's trying to change or use the key when we mark it
1061 * - we tell lockdep that we might nest because we might be revoking an
1062 * authorisation key whilst holding the sem on a key we've just
1063 * instantiated
1064 */
1065 down_write_nested(&key->sem, 1);
David Howellsf7e47672020-01-14 17:07:11 +00001066 if (!test_and_set_bit(KEY_FLAG_REVOKED, &key->flags)) {
1067 notify_key(key, NOTIFY_KEY_REVOKED, 0);
1068 if (key->type->revoke)
1069 key->type->revoke(key);
David Howells04c567d2006-06-22 14:47:18 -07001070
David Howellsf7e47672020-01-14 17:07:11 +00001071 /* set the death time to no more than the expiry time */
1072 time = ktime_get_real_seconds();
1073 if (key->revoked_at == 0 || key->revoked_at > time) {
1074 key->revoked_at = time;
1075 key_schedule_gc(key->revoked_at + key_gc_delay);
1076 }
David Howells5d135442009-09-02 09:14:00 +01001077 }
1078
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 up_write(&key->sem);
David Howellsa8b17ed2011-01-20 16:38:27 +00001080}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081EXPORT_SYMBOL(key_revoke);
1082
David Howells973c9f42011-01-20 16:38:33 +00001083/**
David Howellsfd758152012-05-11 10:56:56 +01001084 * key_invalidate - Invalidate a key.
1085 * @key: The key to be invalidated.
1086 *
1087 * Mark a key as being invalidated and have it cleaned up immediately. The key
1088 * is ignored by all searches and other operations from this point.
1089 */
1090void key_invalidate(struct key *key)
1091{
1092 kenter("%d", key_serial(key));
1093
1094 key_check(key);
1095
1096 if (!test_bit(KEY_FLAG_INVALIDATED, &key->flags)) {
1097 down_write_nested(&key->sem, 1);
David Howellsf7e47672020-01-14 17:07:11 +00001098 if (!test_and_set_bit(KEY_FLAG_INVALIDATED, &key->flags)) {
1099 notify_key(key, NOTIFY_KEY_INVALIDATED, 0);
David Howellsfd758152012-05-11 10:56:56 +01001100 key_schedule_gc_links();
David Howellsf7e47672020-01-14 17:07:11 +00001101 }
David Howellsfd758152012-05-11 10:56:56 +01001102 up_write(&key->sem);
1103 }
1104}
1105EXPORT_SYMBOL(key_invalidate);
1106
1107/**
David Howells6a09d172014-07-18 18:56:34 +01001108 * generic_key_instantiate - Simple instantiation of a key from preparsed data
1109 * @key: The key to be instantiated
1110 * @prep: The preparsed data to load.
1111 *
1112 * Instantiate a key from preparsed data. We assume we can just copy the data
1113 * in directly and clear the old pointers.
1114 *
1115 * This can be pointed to directly by the key type instantiate op pointer.
1116 */
1117int generic_key_instantiate(struct key *key, struct key_preparsed_payload *prep)
1118{
1119 int ret;
1120
1121 pr_devel("==>%s()\n", __func__);
1122
1123 ret = key_payload_reserve(key, prep->quotalen);
1124 if (ret == 0) {
David Howells146aa8b2015-10-21 14:04:48 +01001125 rcu_assign_keypointer(key, prep->payload.data[0]);
1126 key->payload.data[1] = prep->payload.data[1];
1127 key->payload.data[2] = prep->payload.data[2];
1128 key->payload.data[3] = prep->payload.data[3];
1129 prep->payload.data[0] = NULL;
1130 prep->payload.data[1] = NULL;
1131 prep->payload.data[2] = NULL;
1132 prep->payload.data[3] = NULL;
David Howells6a09d172014-07-18 18:56:34 +01001133 }
1134 pr_devel("<==%s() = %d\n", __func__, ret);
1135 return ret;
1136}
1137EXPORT_SYMBOL(generic_key_instantiate);
1138
1139/**
David Howells973c9f42011-01-20 16:38:33 +00001140 * register_key_type - Register a type of key.
1141 * @ktype: The new key type.
1142 *
1143 * Register a new key type.
1144 *
1145 * Returns 0 on success or -EEXIST if a type of this name already exists.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 */
1147int register_key_type(struct key_type *ktype)
1148{
1149 struct key_type *p;
1150 int ret;
1151
David Howells7845bc392011-11-16 11:15:54 +00001152 memset(&ktype->lock_class, 0, sizeof(ktype->lock_class));
1153
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 ret = -EEXIST;
1155 down_write(&key_types_sem);
1156
1157 /* disallow key types with the same name */
1158 list_for_each_entry(p, &key_types_list, link) {
1159 if (strcmp(p->name, ktype->name) == 0)
1160 goto out;
1161 }
1162
1163 /* store the type */
1164 list_add(&ktype->link, &key_types_list);
David Howells1eb1bcf2012-05-11 10:56:56 +01001165
1166 pr_notice("Key type %s registered\n", ktype->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 ret = 0;
1168
David Howells973c9f42011-01-20 16:38:33 +00001169out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 up_write(&key_types_sem);
1171 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +00001172}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173EXPORT_SYMBOL(register_key_type);
1174
David Howells973c9f42011-01-20 16:38:33 +00001175/**
1176 * unregister_key_type - Unregister a type of key.
1177 * @ktype: The key type.
1178 *
1179 * Unregister a key type and mark all the extant keys of this type as dead.
1180 * Those keys of this type are then destroyed to get rid of their payloads and
1181 * they and their links will be garbage collected as soon as possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 */
1183void unregister_key_type(struct key_type *ktype)
1184{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 down_write(&key_types_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 list_del_init(&ktype->link);
David Howells0c061b52011-08-22 14:09:36 +01001187 downgrade_write(&key_types_sem);
1188 key_gc_keytype(ktype);
David Howells1eb1bcf2012-05-11 10:56:56 +01001189 pr_notice("Key type %s unregistered\n", ktype->name);
David Howells0c061b52011-08-22 14:09:36 +01001190 up_read(&key_types_sem);
David Howellsa8b17ed2011-01-20 16:38:27 +00001191}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192EXPORT_SYMBOL(unregister_key_type);
1193
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194/*
David Howells973c9f42011-01-20 16:38:33 +00001195 * Initialise the key management state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 */
1197void __init key_init(void)
1198{
1199 /* allocate a slab in which we can store keys */
1200 key_jar = kmem_cache_create("key_jar", sizeof(struct key),
Paul Mundt20c2df82007-07-20 10:11:58 +09001201 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
1203 /* add the special key types */
1204 list_add_tail(&key_type_keyring.link, &key_types_list);
1205 list_add_tail(&key_type_dead.link, &key_types_list);
1206 list_add_tail(&key_type_user.link, &key_types_list);
Jeff Layton9f6ed2c2012-01-17 16:09:11 -05001207 list_add_tail(&key_type_logon.link, &key_types_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
1209 /* record the root user tracking */
1210 rb_link_node(&root_key_user.node,
1211 NULL,
1212 &key_user_tree.rb_node);
1213
1214 rb_insert_color(&root_key_user.node,
1215 &key_user_tree);
David Howellsa8b17ed2011-01-20 16:38:27 +00001216}