blob: 6763ee45e04d1e2af0a3b81b8cbce1bc3f6d064b [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
David Howells973c9f42011-01-20 16:38:33 +00002/* Userspace key control operations
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
David Howells3e301482005-06-23 22:00:56 -07004 * Copyright (C) 2004-5 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
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/init.h>
9#include <linux/sched.h>
Ingo Molnar29930022017-02-08 18:51:36 +010010#include <linux/sched/task.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/slab.h>
12#include <linux/syscalls.h>
Bryan Schumaker59e6b9c2012-02-24 14:14:50 -050013#include <linux/key.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/keyctl.h>
15#include <linux/fs.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080016#include <linux/capability.h>
Ingo Molnar5b825c32017-02-02 17:54:15 +010017#include <linux/cred.h>
Davi Arnaut0cb409d2006-03-24 03:18:43 -080018#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/err.h>
David Howells38bbca62008-04-29 01:01:19 -070020#include <linux/vmalloc.h>
David Howells70a5bb72008-04-29 01:01:26 -070021#include <linux/security.h>
Kent Overstreeta27bb332013-05-07 16:19:08 -070022#include <linux/uio.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080023#include <linux/uaccess.h>
David Howells822ad642019-02-14 16:20:25 +000024#include <keys/request_key_auth-type.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "internal.h"
26
David Howellsaa9d4432014-12-01 22:52:45 +000027#define KEY_MAX_DESC_SIZE 4096
28
David Howellsb206f282019-06-26 21:02:32 +010029static const unsigned char keyrings_capabilities[2] = {
David Howells45e0f302019-05-30 14:53:10 +010030 [0] = (KEYCTL_CAPS0_CAPABILITIES |
31 (IS_ENABLED(CONFIG_PERSISTENT_KEYRINGS) ? KEYCTL_CAPS0_PERSISTENT_KEYRINGS : 0) |
32 (IS_ENABLED(CONFIG_KEY_DH_OPERATIONS) ? KEYCTL_CAPS0_DIFFIE_HELLMAN : 0) |
33 (IS_ENABLED(CONFIG_ASYMMETRIC_KEY_TYPE) ? KEYCTL_CAPS0_PUBLIC_KEY : 0) |
34 (IS_ENABLED(CONFIG_BIG_KEYS) ? KEYCTL_CAPS0_BIG_KEY : 0) |
35 KEYCTL_CAPS0_INVALIDATE |
36 KEYCTL_CAPS0_RESTRICT_KEYRING |
37 KEYCTL_CAPS0_MOVE
38 ),
David Howells3b6e4de2019-06-26 21:02:32 +010039 [1] = (KEYCTL_CAPS1_NS_KEYRING_NAME |
David Howellsf7e47672020-01-14 17:07:11 +000040 KEYCTL_CAPS1_NS_KEY_TAG |
41 (IS_ENABLED(CONFIG_KEY_NOTIFICATIONS) ? KEYCTL_CAPS1_NOTIFICATIONS : 0)
42 ),
David Howells45e0f302019-05-30 14:53:10 +010043};
44
Davi Arnaut0cb409d2006-03-24 03:18:43 -080045static int key_get_type_from_user(char *type,
46 const char __user *_type,
47 unsigned len)
48{
49 int ret;
50
51 ret = strncpy_from_user(type, _type, len);
Davi Arnaut0cb409d2006-03-24 03:18:43 -080052 if (ret < 0)
Dan Carpenter4303ef12010-06-11 17:30:05 +010053 return ret;
Davi Arnaut0cb409d2006-03-24 03:18:43 -080054 if (ret == 0 || ret >= len)
55 return -EINVAL;
David Howells54e2c2c2014-09-16 17:29:03 +010056 if (type[0] == '.')
57 return -EPERM;
Davi Arnaut0cb409d2006-03-24 03:18:43 -080058 type[len - 1] = '\0';
Davi Arnaut0cb409d2006-03-24 03:18:43 -080059 return 0;
60}
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062/*
David Howells973c9f42011-01-20 16:38:33 +000063 * Extract the description of a new key from userspace and either add it as a
64 * new key to the specified keyring or update a matching key in that keyring.
65 *
David Howellscf7f6012012-09-13 13:06:29 +010066 * If the description is NULL or an empty string, the key type is asked to
67 * generate one from the payload.
68 *
David Howells973c9f42011-01-20 16:38:33 +000069 * The keyring must be writable so that we can attach the key to it.
70 *
71 * If successful, the new key's serial number is returned, otherwise an error
72 * code is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 */
Heiko Carstens1e7bfb22009-01-14 14:14:29 +010074SYSCALL_DEFINE5(add_key, const char __user *, _type,
75 const char __user *, _description,
76 const void __user *, _payload,
77 size_t, plen,
78 key_serial_t, ringid)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
David Howells664cceb2005-09-28 17:03:15 +010080 key_ref_t keyring_ref, key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 char type[32], *description;
82 void *payload;
Davi Arnaut0cb409d2006-03-24 03:18:43 -080083 long ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85 ret = -EINVAL;
David Howells38bbca62008-04-29 01:01:19 -070086 if (plen > 1024 * 1024 - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 goto error;
88
89 /* draw all the data into kernel space */
Davi Arnaut0cb409d2006-03-24 03:18:43 -080090 ret = key_get_type_from_user(type, _type, sizeof(type));
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 if (ret < 0)
92 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
David Howellscf7f6012012-09-13 13:06:29 +010094 description = NULL;
95 if (_description) {
David Howellsaa9d4432014-12-01 22:52:45 +000096 description = strndup_user(_description, KEY_MAX_DESC_SIZE);
David Howellscf7f6012012-09-13 13:06:29 +010097 if (IS_ERR(description)) {
98 ret = PTR_ERR(description);
99 goto error;
100 }
101 if (!*description) {
102 kfree(description);
103 description = NULL;
Mimi Zohara4e3b8d2014-05-22 14:02:23 -0400104 } else if ((description[0] == '.') &&
105 (strncmp(type, "keyring", 7) == 0)) {
106 ret = -EPERM;
107 goto error2;
David Howellscf7f6012012-09-13 13:06:29 +0100108 }
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800109 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111 /* pull the payload in if one was supplied */
112 payload = NULL;
113
Eric Biggers56496452017-06-08 14:48:40 +0100114 if (plen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 ret = -ENOMEM;
Michal Hocko752ade62017-05-08 15:57:27 -0700116 payload = kvmalloc(plen, GFP_KERNEL);
117 if (!payload)
118 goto error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120 ret = -EFAULT;
121 if (copy_from_user(payload, _payload, plen) != 0)
122 goto error3;
123 }
124
125 /* find the target keyring (which must be writable) */
David Howellsf5895942014-03-14 17:44:49 +0000126 keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
David Howells664cceb2005-09-28 17:03:15 +0100127 if (IS_ERR(keyring_ref)) {
128 ret = PTR_ERR(keyring_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 goto error3;
130 }
131
132 /* create or update the requested key and add it to the target
133 * keyring */
David Howells664cceb2005-09-28 17:03:15 +0100134 key_ref = key_create_or_update(keyring_ref, type, description,
Linus Torvalds028db3e2019-07-10 18:43:43 -0700135 payload, plen, KEY_PERM_UNDEF,
136 KEY_ALLOC_IN_QUOTA);
David Howells664cceb2005-09-28 17:03:15 +0100137 if (!IS_ERR(key_ref)) {
138 ret = key_ref_to_ptr(key_ref)->serial;
139 key_ref_put(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 }
141 else {
David Howells664cceb2005-09-28 17:03:15 +0100142 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 }
144
David Howells664cceb2005-09-28 17:03:15 +0100145 key_ref_put(keyring_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 error3:
Eric Biggers57070c82017-06-08 14:48:57 +0100147 if (payload) {
148 memzero_explicit(payload, plen);
149 kvfree(payload);
150 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 error2:
152 kfree(description);
153 error:
154 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000155}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157/*
David Howells973c9f42011-01-20 16:38:33 +0000158 * Search the process keyrings and keyring trees linked from those for a
159 * matching key. Keyrings must have appropriate Search permission to be
160 * searched.
161 *
162 * If a key is found, it will be attached to the destination keyring if there's
163 * one specified and the serial number of the key will be returned.
164 *
165 * If no key is found, /sbin/request-key will be invoked if _callout_info is
166 * non-NULL in an attempt to create a key. The _callout_info string will be
167 * passed to /sbin/request-key to aid with completing the request. If the
168 * _callout_info string is "" then it will be changed to "-".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 */
Heiko Carstens1e7bfb22009-01-14 14:14:29 +0100170SYSCALL_DEFINE4(request_key, const char __user *, _type,
171 const char __user *, _description,
172 const char __user *, _callout_info,
173 key_serial_t, destringid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
175 struct key_type *ktype;
David Howells664cceb2005-09-28 17:03:15 +0100176 struct key *key;
177 key_ref_t dest_ref;
David Howells4a38e122008-04-29 01:01:24 -0700178 size_t callout_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 char type[32], *description, *callout_info;
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800180 long ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182 /* pull the type into kernel space */
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800183 ret = key_get_type_from_user(type, _type, sizeof(type));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 if (ret < 0)
185 goto error;
David Howells1260f802005-08-04 11:50:01 +0100186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 /* pull the description into kernel space */
David Howellsaa9d4432014-12-01 22:52:45 +0000188 description = strndup_user(_description, KEY_MAX_DESC_SIZE);
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800189 if (IS_ERR(description)) {
190 ret = PTR_ERR(description);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 goto error;
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194 /* pull the callout info into kernel space */
195 callout_info = NULL;
David Howells4a38e122008-04-29 01:01:24 -0700196 callout_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 if (_callout_info) {
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800198 callout_info = strndup_user(_callout_info, PAGE_SIZE);
199 if (IS_ERR(callout_info)) {
200 ret = PTR_ERR(callout_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 goto error2;
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800202 }
David Howells4a38e122008-04-29 01:01:24 -0700203 callout_len = strlen(callout_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 }
205
206 /* get the destination keyring if specified */
David Howells664cceb2005-09-28 17:03:15 +0100207 dest_ref = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 if (destringid) {
David Howells55931222009-09-02 09:13:45 +0100209 dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE,
David Howellsf5895942014-03-14 17:44:49 +0000210 KEY_NEED_WRITE);
David Howells664cceb2005-09-28 17:03:15 +0100211 if (IS_ERR(dest_ref)) {
212 ret = PTR_ERR(dest_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 goto error3;
214 }
215 }
216
217 /* find the key type */
218 ktype = key_type_lookup(type);
219 if (IS_ERR(ktype)) {
220 ret = PTR_ERR(ktype);
221 goto error4;
222 }
223
224 /* do the search */
David Howellsa58946c2019-06-26 21:02:33 +0100225 key = request_key_and_link(ktype, description, NULL, callout_info,
Linus Torvalds028db3e2019-07-10 18:43:43 -0700226 callout_len, NULL, key_ref_to_ptr(dest_ref),
David Howells7e047ef2006-06-26 00:24:50 -0700227 KEY_ALLOC_IN_QUOTA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 if (IS_ERR(key)) {
229 ret = PTR_ERR(key);
230 goto error5;
231 }
232
David Howells4aab1e82011-03-11 17:57:33 +0000233 /* wait for the key to finish being constructed */
234 ret = wait_for_key_construction(key, 1);
235 if (ret < 0)
236 goto error6;
237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 ret = key->serial;
239
David Howells4aab1e82011-03-11 17:57:33 +0000240error6:
David Howells3e301482005-06-23 22:00:56 -0700241 key_put(key);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700242error5:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 key_type_put(ktype);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700244error4:
David Howells664cceb2005-09-28 17:03:15 +0100245 key_ref_put(dest_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700246error3:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 kfree(callout_info);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700248error2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 kfree(description);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700250error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000252}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254/*
David Howells973c9f42011-01-20 16:38:33 +0000255 * Get the ID of the specified process keyring.
256 *
257 * The requested keyring must have search permission to be found.
258 *
259 * If successful, the ID of the requested keyring will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 */
261long keyctl_get_keyring_ID(key_serial_t id, int create)
262{
David Howells664cceb2005-09-28 17:03:15 +0100263 key_ref_t key_ref;
David Howells55931222009-09-02 09:13:45 +0100264 unsigned long lflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 long ret;
266
David Howells55931222009-09-02 09:13:45 +0100267 lflags = create ? KEY_LOOKUP_CREATE : 0;
David Howellsf5895942014-03-14 17:44:49 +0000268 key_ref = lookup_user_key(id, lflags, KEY_NEED_SEARCH);
David Howells664cceb2005-09-28 17:03:15 +0100269 if (IS_ERR(key_ref)) {
270 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 goto error;
272 }
273
David Howells664cceb2005-09-28 17:03:15 +0100274 ret = key_ref_to_ptr(key_ref)->serial;
275 key_ref_put(key_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700276error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 return ret;
David Howells973c9f42011-01-20 16:38:33 +0000278}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280/*
David Howells973c9f42011-01-20 16:38:33 +0000281 * Join a (named) session keyring.
282 *
283 * Create and join an anonymous session keyring or join a named session
284 * keyring, creating it if necessary. A named session keyring must have Search
285 * permission for it to be joined. Session keyrings without this permit will
David Howellsee8f8442017-04-18 15:31:07 +0100286 * be skipped over. It is not permitted for userspace to create or join
287 * keyrings whose name begin with a dot.
David Howells973c9f42011-01-20 16:38:33 +0000288 *
289 * If successful, the ID of the joined session keyring will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 */
291long keyctl_join_session_keyring(const char __user *_name)
292{
293 char *name;
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800294 long ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
296 /* fetch the name from userspace */
297 name = NULL;
298 if (_name) {
David Howellsaa9d4432014-12-01 22:52:45 +0000299 name = strndup_user(_name, KEY_MAX_DESC_SIZE);
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800300 if (IS_ERR(name)) {
301 ret = PTR_ERR(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 goto error;
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800303 }
David Howellsee8f8442017-04-18 15:31:07 +0100304
305 ret = -EPERM;
306 if (name[0] == '.')
307 goto error_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 }
309
310 /* join the session */
311 ret = join_session_keyring(name);
David Howellsee8f8442017-04-18 15:31:07 +0100312error_name:
Vegard Nossum0d54ee12009-01-17 17:45:45 +0100313 kfree(name);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700314error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000316}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318/*
David Howells973c9f42011-01-20 16:38:33 +0000319 * Update a key's data payload from the given data.
320 *
321 * The key must grant the caller Write permission and the key type must support
322 * updating for this to work. A negative key can be positively instantiated
323 * with this call.
324 *
325 * If successful, 0 will be returned. If the key type does not support
326 * updating, then -EOPNOTSUPP will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 */
328long keyctl_update_key(key_serial_t id,
329 const void __user *_payload,
330 size_t plen)
331{
David Howells664cceb2005-09-28 17:03:15 +0100332 key_ref_t key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 void *payload;
334 long ret;
335
336 ret = -EINVAL;
337 if (plen > PAGE_SIZE)
338 goto error;
339
340 /* pull the payload in if one was supplied */
341 payload = NULL;
Eric Biggers56496452017-06-08 14:48:40 +0100342 if (plen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 ret = -ENOMEM;
Waiman Long4f088242020-03-21 21:11:25 -0400344 payload = kvmalloc(plen, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 if (!payload)
346 goto error;
347
348 ret = -EFAULT;
349 if (copy_from_user(payload, _payload, plen) != 0)
350 goto error2;
351 }
352
353 /* find the target key (which must be writable) */
David Howellsf5895942014-03-14 17:44:49 +0000354 key_ref = lookup_user_key(id, 0, KEY_NEED_WRITE);
David Howells664cceb2005-09-28 17:03:15 +0100355 if (IS_ERR(key_ref)) {
356 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 goto error2;
358 }
359
360 /* update the key */
David Howells664cceb2005-09-28 17:03:15 +0100361 ret = key_update(key_ref, payload, plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
David Howells664cceb2005-09-28 17:03:15 +0100363 key_ref_put(key_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700364error2:
Waiman Long4f088242020-03-21 21:11:25 -0400365 __kvzfree(payload, plen);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700366error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000368}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370/*
David Howells973c9f42011-01-20 16:38:33 +0000371 * Revoke a key.
372 *
373 * The key must be grant the caller Write or Setattr permission for this to
374 * work. The key type should give up its quota claim when revoked. The key
375 * and any links to the key will be automatically garbage collected after a
376 * certain amount of time (/proc/sys/kernel/keys/gc_delay).
377 *
Mimi Zohard3600bc2015-11-10 08:34:46 -0500378 * Keys with KEY_FLAG_KEEP set should not be revoked.
379 *
David Howells973c9f42011-01-20 16:38:33 +0000380 * If successful, 0 is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 */
382long keyctl_revoke_key(key_serial_t id)
383{
David Howells664cceb2005-09-28 17:03:15 +0100384 key_ref_t key_ref;
Mimi Zohard3600bc2015-11-10 08:34:46 -0500385 struct key *key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 long ret;
387
Linus Torvalds028db3e2019-07-10 18:43:43 -0700388 key_ref = lookup_user_key(id, 0, KEY_NEED_WRITE);
David Howells664cceb2005-09-28 17:03:15 +0100389 if (IS_ERR(key_ref)) {
390 ret = PTR_ERR(key_ref);
Linus Torvalds028db3e2019-07-10 18:43:43 -0700391 if (ret != -EACCES)
392 goto error;
393 key_ref = lookup_user_key(id, 0, KEY_NEED_SETATTR);
394 if (IS_ERR(key_ref)) {
395 ret = PTR_ERR(key_ref);
396 goto error;
397 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 }
399
Mimi Zohard3600bc2015-11-10 08:34:46 -0500400 key = key_ref_to_ptr(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 ret = 0;
Mimi Zohard3600bc2015-11-10 08:34:46 -0500402 if (test_bit(KEY_FLAG_KEEP, &key->flags))
Mimi Zohar1d6d1672016-01-07 07:46:36 -0500403 ret = -EPERM;
404 else
Mimi Zohard3600bc2015-11-10 08:34:46 -0500405 key_revoke(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
David Howells664cceb2005-09-28 17:03:15 +0100407 key_ref_put(key_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700408error:
David Howells1260f802005-08-04 11:50:01 +0100409 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000410}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412/*
David Howellsfd758152012-05-11 10:56:56 +0100413 * Invalidate a key.
414 *
415 * The key must be grant the caller Invalidate permission for this to work.
416 * The key and any links to the key will be automatically garbage collected
417 * immediately.
418 *
Mimi Zohard3600bc2015-11-10 08:34:46 -0500419 * Keys with KEY_FLAG_KEEP set should not be invalidated.
420 *
David Howellsfd758152012-05-11 10:56:56 +0100421 * If successful, 0 is returned.
422 */
423long keyctl_invalidate_key(key_serial_t id)
424{
425 key_ref_t key_ref;
Mimi Zohard3600bc2015-11-10 08:34:46 -0500426 struct key *key;
David Howellsfd758152012-05-11 10:56:56 +0100427 long ret;
428
429 kenter("%d", id);
430
Linus Torvalds028db3e2019-07-10 18:43:43 -0700431 key_ref = lookup_user_key(id, 0, KEY_NEED_SEARCH);
David Howellsfd758152012-05-11 10:56:56 +0100432 if (IS_ERR(key_ref)) {
433 ret = PTR_ERR(key_ref);
David Howells0c7774a2014-07-17 20:45:08 +0100434
435 /* Root is permitted to invalidate certain special keys */
436 if (capable(CAP_SYS_ADMIN)) {
David Howells8c0637e2020-05-12 15:16:29 +0100437 key_ref = lookup_user_key(id, 0, KEY_SYSADMIN_OVERRIDE);
David Howells0c7774a2014-07-17 20:45:08 +0100438 if (IS_ERR(key_ref))
439 goto error;
440 if (test_bit(KEY_FLAG_ROOT_CAN_INVAL,
441 &key_ref_to_ptr(key_ref)->flags))
442 goto invalidate;
443 goto error_put;
444 }
445
David Howellsfd758152012-05-11 10:56:56 +0100446 goto error;
447 }
448
David Howells0c7774a2014-07-17 20:45:08 +0100449invalidate:
Mimi Zohard3600bc2015-11-10 08:34:46 -0500450 key = key_ref_to_ptr(key_ref);
David Howellsfd758152012-05-11 10:56:56 +0100451 ret = 0;
Mimi Zohard3600bc2015-11-10 08:34:46 -0500452 if (test_bit(KEY_FLAG_KEEP, &key->flags))
453 ret = -EPERM;
Mimi Zohar1d6d1672016-01-07 07:46:36 -0500454 else
Mimi Zohard3600bc2015-11-10 08:34:46 -0500455 key_invalidate(key);
David Howells0c7774a2014-07-17 20:45:08 +0100456error_put:
David Howellsfd758152012-05-11 10:56:56 +0100457 key_ref_put(key_ref);
458error:
459 kleave(" = %ld", ret);
460 return ret;
461}
462
463/*
David Howells973c9f42011-01-20 16:38:33 +0000464 * Clear the specified keyring, creating an empty process keyring if one of the
465 * special keyring IDs is used.
466 *
Mimi Zohard3600bc2015-11-10 08:34:46 -0500467 * The keyring must grant the caller Write permission and not have
468 * KEY_FLAG_KEEP set for this to work. If successful, 0 will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 */
470long keyctl_keyring_clear(key_serial_t ringid)
471{
David Howells664cceb2005-09-28 17:03:15 +0100472 key_ref_t keyring_ref;
Mimi Zohard3600bc2015-11-10 08:34:46 -0500473 struct key *keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 long ret;
475
Linus Torvalds028db3e2019-07-10 18:43:43 -0700476 keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
David Howells664cceb2005-09-28 17:03:15 +0100477 if (IS_ERR(keyring_ref)) {
478 ret = PTR_ERR(keyring_ref);
David Howells700920e2012-01-18 15:31:45 +0000479
480 /* Root is permitted to invalidate certain special keyrings */
481 if (capable(CAP_SYS_ADMIN)) {
David Howells8c0637e2020-05-12 15:16:29 +0100482 keyring_ref = lookup_user_key(ringid, 0,
483 KEY_SYSADMIN_OVERRIDE);
David Howells700920e2012-01-18 15:31:45 +0000484 if (IS_ERR(keyring_ref))
485 goto error;
486 if (test_bit(KEY_FLAG_ROOT_CAN_CLEAR,
487 &key_ref_to_ptr(keyring_ref)->flags))
488 goto clear;
489 goto error_put;
490 }
491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 goto error;
493 }
494
David Howells700920e2012-01-18 15:31:45 +0000495clear:
Mimi Zohard3600bc2015-11-10 08:34:46 -0500496 keyring = key_ref_to_ptr(keyring_ref);
497 if (test_bit(KEY_FLAG_KEEP, &keyring->flags))
498 ret = -EPERM;
499 else
500 ret = keyring_clear(keyring);
David Howells700920e2012-01-18 15:31:45 +0000501error_put:
David Howells664cceb2005-09-28 17:03:15 +0100502 key_ref_put(keyring_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700503error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000505}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507/*
David Howells973c9f42011-01-20 16:38:33 +0000508 * Create a link from a keyring to a key if there's no matching key in the
509 * keyring, otherwise replace the link to the matching key with a link to the
510 * new key.
511 *
512 * The key must grant the caller Link permission and the the keyring must grant
513 * the caller Write permission. Furthermore, if an additional link is created,
514 * the keyring's quota will be extended.
515 *
516 * If successful, 0 will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 */
518long keyctl_keyring_link(key_serial_t id, key_serial_t ringid)
519{
David Howells664cceb2005-09-28 17:03:15 +0100520 key_ref_t keyring_ref, key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 long ret;
522
David Howellsf5895942014-03-14 17:44:49 +0000523 keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
David Howells664cceb2005-09-28 17:03:15 +0100524 if (IS_ERR(keyring_ref)) {
525 ret = PTR_ERR(keyring_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 goto error;
527 }
528
David Howellsf5895942014-03-14 17:44:49 +0000529 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE, KEY_NEED_LINK);
David Howells664cceb2005-09-28 17:03:15 +0100530 if (IS_ERR(key_ref)) {
531 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 goto error2;
533 }
534
David Howells664cceb2005-09-28 17:03:15 +0100535 ret = key_link(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
David Howells664cceb2005-09-28 17:03:15 +0100537 key_ref_put(key_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700538error2:
David Howells664cceb2005-09-28 17:03:15 +0100539 key_ref_put(keyring_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700540error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000542}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544/*
David Howells973c9f42011-01-20 16:38:33 +0000545 * Unlink a key from a keyring.
546 *
547 * The keyring must grant the caller Write permission for this to work; the key
548 * itself need not grant the caller anything. If the last link to a key is
549 * removed then that key will be scheduled for destruction.
550 *
Mimi Zohard3600bc2015-11-10 08:34:46 -0500551 * Keys or keyrings with KEY_FLAG_KEEP set should not be unlinked.
552 *
David Howells973c9f42011-01-20 16:38:33 +0000553 * If successful, 0 will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 */
555long keyctl_keyring_unlink(key_serial_t id, key_serial_t ringid)
556{
David Howells664cceb2005-09-28 17:03:15 +0100557 key_ref_t keyring_ref, key_ref;
Mimi Zohard3600bc2015-11-10 08:34:46 -0500558 struct key *keyring, *key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 long ret;
560
David Howellsf5895942014-03-14 17:44:49 +0000561 keyring_ref = lookup_user_key(ringid, 0, KEY_NEED_WRITE);
David Howells664cceb2005-09-28 17:03:15 +0100562 if (IS_ERR(keyring_ref)) {
563 ret = PTR_ERR(keyring_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 goto error;
565 }
566
David Howells8c0637e2020-05-12 15:16:29 +0100567 key_ref = lookup_user_key(id, KEY_LOOKUP_PARTIAL, KEY_NEED_UNLINK);
David Howells664cceb2005-09-28 17:03:15 +0100568 if (IS_ERR(key_ref)) {
569 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 goto error2;
571 }
572
Mimi Zohard3600bc2015-11-10 08:34:46 -0500573 keyring = key_ref_to_ptr(keyring_ref);
574 key = key_ref_to_ptr(key_ref);
575 if (test_bit(KEY_FLAG_KEEP, &keyring->flags) &&
576 test_bit(KEY_FLAG_KEEP, &key->flags))
577 ret = -EPERM;
578 else
579 ret = key_unlink(keyring, key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
David Howells664cceb2005-09-28 17:03:15 +0100581 key_ref_put(key_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700582error2:
David Howells664cceb2005-09-28 17:03:15 +0100583 key_ref_put(keyring_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700584error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000586}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588/*
David Howellsed0ac5c2019-05-20 21:51:50 +0100589 * Move a link to a key from one keyring to another, displacing any matching
590 * key from the destination keyring.
591 *
592 * The key must grant the caller Link permission and both keyrings must grant
593 * the caller Write permission. There must also be a link in the from keyring
594 * to the key. If both keyrings are the same, nothing is done.
595 *
596 * If successful, 0 will be returned.
597 */
598long keyctl_keyring_move(key_serial_t id, key_serial_t from_ringid,
599 key_serial_t to_ringid, unsigned int flags)
600{
601 key_ref_t key_ref, from_ref, to_ref;
602 long ret;
603
604 if (flags & ~KEYCTL_MOVE_EXCL)
605 return -EINVAL;
606
607 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE, KEY_NEED_LINK);
608 if (IS_ERR(key_ref))
609 return PTR_ERR(key_ref);
610
611 from_ref = lookup_user_key(from_ringid, 0, KEY_NEED_WRITE);
612 if (IS_ERR(from_ref)) {
613 ret = PTR_ERR(from_ref);
614 goto error2;
615 }
616
617 to_ref = lookup_user_key(to_ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
618 if (IS_ERR(to_ref)) {
619 ret = PTR_ERR(to_ref);
620 goto error3;
621 }
622
623 ret = key_move(key_ref_to_ptr(key_ref), key_ref_to_ptr(from_ref),
624 key_ref_to_ptr(to_ref), flags);
625
626 key_ref_put(to_ref);
627error3:
628 key_ref_put(from_ref);
629error2:
630 key_ref_put(key_ref);
631 return ret;
632}
633
634/*
David Howells973c9f42011-01-20 16:38:33 +0000635 * Return a description of a key to userspace.
636 *
637 * The key must grant the caller View permission for this to work.
638 *
639 * If there's a buffer, we place up to buflen bytes of data into it formatted
640 * in the following way:
641 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 * type;uid;gid;perm;description<NUL>
David Howells973c9f42011-01-20 16:38:33 +0000643 *
644 * If successful, we return the amount of description available, irrespective
645 * of how much we may have copied into the buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 */
647long keyctl_describe_key(key_serial_t keyid,
648 char __user *buffer,
649 size_t buflen)
650{
David Howells3e301482005-06-23 22:00:56 -0700651 struct key *key, *instkey;
David Howells664cceb2005-09-28 17:03:15 +0100652 key_ref_t key_ref;
David Howellsaa9d4432014-12-01 22:52:45 +0000653 char *infobuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 long ret;
David Howellsaa9d4432014-12-01 22:52:45 +0000655 int desclen, infolen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
David Howellsf5895942014-03-14 17:44:49 +0000657 key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_NEED_VIEW);
David Howells664cceb2005-09-28 17:03:15 +0100658 if (IS_ERR(key_ref)) {
David Howells3e301482005-06-23 22:00:56 -0700659 /* viewing a key under construction is permitted if we have the
660 * authorisation token handy */
David Howells664cceb2005-09-28 17:03:15 +0100661 if (PTR_ERR(key_ref) == -EACCES) {
David Howells3e301482005-06-23 22:00:56 -0700662 instkey = key_get_instantiation_authkey(keyid);
663 if (!IS_ERR(instkey)) {
664 key_put(instkey);
David Howells8bbf49762008-11-14 10:39:14 +1100665 key_ref = lookup_user_key(keyid,
David Howells55931222009-09-02 09:13:45 +0100666 KEY_LOOKUP_PARTIAL,
David Howells8c0637e2020-05-12 15:16:29 +0100667 KEY_AUTHTOKEN_OVERRIDE);
David Howells664cceb2005-09-28 17:03:15 +0100668 if (!IS_ERR(key_ref))
David Howells3e301482005-06-23 22:00:56 -0700669 goto okay;
670 }
671 }
672
David Howells664cceb2005-09-28 17:03:15 +0100673 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 goto error;
675 }
676
David Howells3e301482005-06-23 22:00:56 -0700677okay:
David Howells664cceb2005-09-28 17:03:15 +0100678 key = key_ref_to_ptr(key_ref);
David Howellsaa9d4432014-12-01 22:52:45 +0000679 desclen = strlen(key->description);
David Howells664cceb2005-09-28 17:03:15 +0100680
David Howellsaa9d4432014-12-01 22:52:45 +0000681 /* calculate how much information we're going to return */
682 ret = -ENOMEM;
683 infobuf = kasprintf(GFP_KERNEL,
684 "%s;%d;%d;%08x;",
685 key->type->name,
686 from_kuid_munged(current_user_ns(), key->uid),
687 from_kgid_munged(current_user_ns(), key->gid),
Linus Torvalds028db3e2019-07-10 18:43:43 -0700688 key->perm);
David Howellsaa9d4432014-12-01 22:52:45 +0000689 if (!infobuf)
690 goto error2;
691 infolen = strlen(infobuf);
692 ret = infolen + desclen + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
694 /* consider returning the data */
David Howellsaa9d4432014-12-01 22:52:45 +0000695 if (buffer && buflen >= ret) {
696 if (copy_to_user(buffer, infobuf, infolen) != 0 ||
697 copy_to_user(buffer + infolen, key->description,
698 desclen + 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 ret = -EFAULT;
700 }
701
David Howellsaa9d4432014-12-01 22:52:45 +0000702 kfree(infobuf);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700703error2:
David Howells664cceb2005-09-28 17:03:15 +0100704 key_ref_put(key_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700705error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000707}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709/*
David Howells973c9f42011-01-20 16:38:33 +0000710 * Search the specified keyring and any keyrings it links to for a matching
711 * key. Only keyrings that grant the caller Search permission will be searched
712 * (this includes the starting keyring). Only keys with Search permission can
713 * be found.
714 *
715 * If successful, the found key will be linked to the destination keyring if
716 * supplied and the key has Link permission, and the found key ID will be
717 * returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 */
719long keyctl_keyring_search(key_serial_t ringid,
720 const char __user *_type,
721 const char __user *_description,
722 key_serial_t destringid)
723{
724 struct key_type *ktype;
David Howells664cceb2005-09-28 17:03:15 +0100725 key_ref_t keyring_ref, key_ref, dest_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 char type[32], *description;
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800727 long ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
729 /* pull the type and description into kernel space */
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800730 ret = key_get_type_from_user(type, _type, sizeof(type));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 if (ret < 0)
732 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
David Howellsaa9d4432014-12-01 22:52:45 +0000734 description = strndup_user(_description, KEY_MAX_DESC_SIZE);
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800735 if (IS_ERR(description)) {
736 ret = PTR_ERR(description);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 goto error;
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800738 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
740 /* get the keyring at which to begin the search */
David Howellsf5895942014-03-14 17:44:49 +0000741 keyring_ref = lookup_user_key(ringid, 0, KEY_NEED_SEARCH);
David Howells664cceb2005-09-28 17:03:15 +0100742 if (IS_ERR(keyring_ref)) {
743 ret = PTR_ERR(keyring_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 goto error2;
745 }
746
747 /* get the destination keyring if specified */
David Howells664cceb2005-09-28 17:03:15 +0100748 dest_ref = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 if (destringid) {
David Howells55931222009-09-02 09:13:45 +0100750 dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE,
David Howellsf5895942014-03-14 17:44:49 +0000751 KEY_NEED_WRITE);
David Howells664cceb2005-09-28 17:03:15 +0100752 if (IS_ERR(dest_ref)) {
753 ret = PTR_ERR(dest_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 goto error3;
755 }
756 }
757
758 /* find the key type */
759 ktype = key_type_lookup(type);
760 if (IS_ERR(ktype)) {
761 ret = PTR_ERR(ktype);
762 goto error4;
763 }
764
765 /* do the search */
David Howellsdcf49db2019-06-26 21:02:32 +0100766 key_ref = keyring_search(keyring_ref, ktype, description, true);
David Howells664cceb2005-09-28 17:03:15 +0100767 if (IS_ERR(key_ref)) {
768 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
770 /* treat lack or presence of a negative key the same */
771 if (ret == -EAGAIN)
772 ret = -ENOKEY;
773 goto error5;
774 }
775
776 /* link the resulting key to the destination keyring if we can */
David Howells664cceb2005-09-28 17:03:15 +0100777 if (dest_ref) {
David Howellsf5895942014-03-14 17:44:49 +0000778 ret = key_permission(key_ref, KEY_NEED_LINK);
David Howells29db9192005-10-30 15:02:44 -0800779 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 goto error6;
781
David Howells664cceb2005-09-28 17:03:15 +0100782 ret = key_link(key_ref_to_ptr(dest_ref), key_ref_to_ptr(key_ref));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 if (ret < 0)
784 goto error6;
785 }
786
David Howells664cceb2005-09-28 17:03:15 +0100787 ret = key_ref_to_ptr(key_ref)->serial;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700789error6:
David Howells664cceb2005-09-28 17:03:15 +0100790 key_ref_put(key_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700791error5:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 key_type_put(ktype);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700793error4:
David Howells664cceb2005-09-28 17:03:15 +0100794 key_ref_put(dest_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700795error3:
David Howells664cceb2005-09-28 17:03:15 +0100796 key_ref_put(keyring_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700797error2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 kfree(description);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700799error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000801}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803/*
Waiman Longd3ec10a2020-03-21 21:11:24 -0400804 * Call the read method
805 */
806static long __keyctl_read_key(struct key *key, char *buffer, size_t buflen)
807{
808 long ret;
809
810 down_read(&key->sem);
811 ret = key_validate(key);
812 if (ret == 0)
813 ret = key->type->read(key, buffer, buflen);
814 up_read(&key->sem);
815 return ret;
816}
817
818/*
David Howells973c9f42011-01-20 16:38:33 +0000819 * Read a key's payload.
820 *
821 * The key must either grant the caller Read permission, or it must grant the
822 * caller Search permission when searched for from the process keyrings.
823 *
824 * If successful, we place up to buflen bytes of data into the buffer, if one
825 * is provided, and return the amount of data that is available in the key,
826 * irrespective of how much we copied into the buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 */
828long keyctl_read_key(key_serial_t keyid, char __user *buffer, size_t buflen)
829{
David Howells664cceb2005-09-28 17:03:15 +0100830 struct key *key;
831 key_ref_t key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 long ret;
Waiman Long4f088242020-03-21 21:11:25 -0400833 char *key_data = NULL;
834 size_t key_data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
836 /* find the key first */
David Howells8c0637e2020-05-12 15:16:29 +0100837 key_ref = lookup_user_key(keyid, 0, KEY_DEFER_PERM_CHECK);
David Howells664cceb2005-09-28 17:03:15 +0100838 if (IS_ERR(key_ref)) {
839 ret = -ENOKEY;
Waiman Longd3ec10a2020-03-21 21:11:24 -0400840 goto out;
David Howells664cceb2005-09-28 17:03:15 +0100841 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
David Howells664cceb2005-09-28 17:03:15 +0100843 key = key_ref_to_ptr(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
David Howells363b02d2017-10-04 16:43:25 +0100845 ret = key_read_state(key);
846 if (ret < 0)
Waiman Longd3ec10a2020-03-21 21:11:24 -0400847 goto key_put_out; /* Negatively instantiated */
Eric Biggers37863c42017-09-18 11:37:23 -0700848
David Howells664cceb2005-09-28 17:03:15 +0100849 /* see if we can read it directly */
David Howellsf5895942014-03-14 17:44:49 +0000850 ret = key_permission(key_ref, KEY_NEED_READ);
David Howells29db9192005-10-30 15:02:44 -0800851 if (ret == 0)
David Howells664cceb2005-09-28 17:03:15 +0100852 goto can_read_key;
David Howells29db9192005-10-30 15:02:44 -0800853 if (ret != -EACCES)
Waiman Longd3ec10a2020-03-21 21:11:24 -0400854 goto key_put_out;
David Howells664cceb2005-09-28 17:03:15 +0100855
856 /* we can't; see if it's searchable from this process's keyrings
857 * - we automatically take account of the fact that it may be
858 * dangling off an instantiation key
859 */
860 if (!is_key_possessed(key_ref)) {
861 ret = -EACCES;
Waiman Longd3ec10a2020-03-21 21:11:24 -0400862 goto key_put_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 }
864
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 /* the key is probably readable - now try to read it */
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700866can_read_key:
Waiman Longd3ec10a2020-03-21 21:11:24 -0400867 if (!key->type->read) {
868 ret = -EOPNOTSUPP;
869 goto key_put_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 }
871
Waiman Longd3ec10a2020-03-21 21:11:24 -0400872 if (!buffer || !buflen) {
873 /* Get the key length from the read method */
874 ret = __keyctl_read_key(key, NULL, 0);
875 goto key_put_out;
876 }
877
878 /*
879 * Read the data with the semaphore held (since we might sleep)
880 * to protect against the key being updated or revoked.
881 *
882 * Allocating a temporary buffer to hold the keys before
883 * transferring them to user buffer to avoid potential
884 * deadlock involving page fault and mmap_sem.
Waiman Long4f088242020-03-21 21:11:25 -0400885 *
886 * key_data_len = (buflen <= PAGE_SIZE)
887 * ? buflen : actual length of key data
888 *
889 * This prevents allocating arbitrary large buffer which can
890 * be much larger than the actual key length. In the latter case,
891 * at least 2 passes of this loop is required.
Waiman Longd3ec10a2020-03-21 21:11:24 -0400892 */
Waiman Long4f088242020-03-21 21:11:25 -0400893 key_data_len = (buflen <= PAGE_SIZE) ? buflen : 0;
894 for (;;) {
895 if (key_data_len) {
896 key_data = kvmalloc(key_data_len, GFP_KERNEL);
897 if (!key_data) {
898 ret = -ENOMEM;
899 goto key_put_out;
900 }
901 }
Waiman Longd3ec10a2020-03-21 21:11:24 -0400902
Waiman Long4f088242020-03-21 21:11:25 -0400903 ret = __keyctl_read_key(key, key_data, key_data_len);
Waiman Longd3ec10a2020-03-21 21:11:24 -0400904
Waiman Long4f088242020-03-21 21:11:25 -0400905 /*
906 * Read methods will just return the required length without
907 * any copying if the provided length isn't large enough.
908 */
909 if (ret <= 0 || ret > buflen)
910 break;
911
912 /*
913 * The key may change (unlikely) in between 2 consecutive
914 * __keyctl_read_key() calls. In this case, we reallocate
915 * a larger buffer and redo the key read when
916 * key_data_len < ret <= buflen.
917 */
918 if (ret > key_data_len) {
919 if (unlikely(key_data))
920 __kvzfree(key_data, key_data_len);
921 key_data_len = ret;
922 continue; /* Allocate buffer */
923 }
924
Waiman Longd3ec10a2020-03-21 21:11:24 -0400925 if (copy_to_user(buffer, key_data, ret))
926 ret = -EFAULT;
Waiman Long4f088242020-03-21 21:11:25 -0400927 break;
Waiman Longd3ec10a2020-03-21 21:11:24 -0400928 }
Waiman Long4f088242020-03-21 21:11:25 -0400929 __kvzfree(key_data, key_data_len);
Waiman Longd3ec10a2020-03-21 21:11:24 -0400930
931key_put_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 key_put(key);
Waiman Longd3ec10a2020-03-21 21:11:24 -0400933out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000935}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937/*
David Howells973c9f42011-01-20 16:38:33 +0000938 * Change the ownership of a key
939 *
940 * The key must grant the caller Setattr permission for this to work, though
941 * the key need not be fully instantiated yet. For the UID to be changed, or
942 * for the GID to be changed to a group the caller is not a member of, the
943 * caller must have sysadmin capability. If either uid or gid is -1 then that
944 * attribute is not changed.
945 *
946 * If the UID is to be changed, the new user must have sufficient quota to
947 * accept the key. The quota deduction will be removed from the old user to
948 * the new user should the attribute be changed.
949 *
950 * If successful, 0 will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 */
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -0800952long keyctl_chown_key(key_serial_t id, uid_t user, gid_t group)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953{
Fredrik Tolf58016492006-06-26 00:24:51 -0700954 struct key_user *newowner, *zapowner = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 struct key *key;
David Howells664cceb2005-09-28 17:03:15 +0100956 key_ref_t key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 long ret;
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -0800958 kuid_t uid;
959 kgid_t gid;
960
961 uid = make_kuid(current_user_ns(), user);
962 gid = make_kgid(current_user_ns(), group);
963 ret = -EINVAL;
964 if ((user != (uid_t) -1) && !uid_valid(uid))
965 goto error;
966 if ((group != (gid_t) -1) && !gid_valid(gid))
967 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
969 ret = 0;
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -0800970 if (user == (uid_t) -1 && group == (gid_t) -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 goto error;
972
David Howells55931222009-09-02 09:13:45 +0100973 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
Linus Torvalds028db3e2019-07-10 18:43:43 -0700974 KEY_NEED_SETATTR);
David Howells664cceb2005-09-28 17:03:15 +0100975 if (IS_ERR(key_ref)) {
976 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 goto error;
978 }
979
David Howells664cceb2005-09-28 17:03:15 +0100980 key = key_ref_to_ptr(key_ref);
981
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 /* make the changes with the locks held to prevent chown/chown races */
983 ret = -EACCES;
984 down_write(&key->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
986 if (!capable(CAP_SYS_ADMIN)) {
987 /* only the sysadmin can chown a key to some other UID */
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -0800988 if (user != (uid_t) -1 && !uid_eq(key->uid, uid))
Fredrik Tolf58016492006-06-26 00:24:51 -0700989 goto error_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
991 /* only the sysadmin can set the key's GID to a group other
992 * than one of those that the current process subscribes to */
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -0800993 if (group != (gid_t) -1 && !gid_eq(gid, key->gid) && !in_group_p(gid))
Fredrik Tolf58016492006-06-26 00:24:51 -0700994 goto error_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 }
996
Fredrik Tolf58016492006-06-26 00:24:51 -0700997 /* change the UID */
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -0800998 if (user != (uid_t) -1 && !uid_eq(uid, key->uid)) {
Fredrik Tolf58016492006-06-26 00:24:51 -0700999 ret = -ENOMEM;
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -08001000 newowner = key_user_lookup(uid);
Fredrik Tolf58016492006-06-26 00:24:51 -07001001 if (!newowner)
1002 goto error_put;
1003
1004 /* transfer the quota burden to the new user */
1005 if (test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) {
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -08001006 unsigned maxkeys = uid_eq(uid, GLOBAL_ROOT_UID) ?
David Howells0b77f5b2008-04-29 01:01:32 -07001007 key_quota_root_maxkeys : key_quota_maxkeys;
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -08001008 unsigned maxbytes = uid_eq(uid, GLOBAL_ROOT_UID) ?
David Howells0b77f5b2008-04-29 01:01:32 -07001009 key_quota_root_maxbytes : key_quota_maxbytes;
1010
Fredrik Tolf58016492006-06-26 00:24:51 -07001011 spin_lock(&newowner->lock);
Yang Xu2e356102020-02-28 12:41:51 +08001012 if (newowner->qnkeys + 1 > maxkeys ||
1013 newowner->qnbytes + key->quotalen > maxbytes ||
David Howells0b77f5b2008-04-29 01:01:32 -07001014 newowner->qnbytes + key->quotalen <
1015 newowner->qnbytes)
Fredrik Tolf58016492006-06-26 00:24:51 -07001016 goto quota_overrun;
1017
1018 newowner->qnkeys++;
1019 newowner->qnbytes += key->quotalen;
1020 spin_unlock(&newowner->lock);
1021
1022 spin_lock(&key->user->lock);
1023 key->user->qnkeys--;
1024 key->user->qnbytes -= key->quotalen;
1025 spin_unlock(&key->user->lock);
1026 }
1027
1028 atomic_dec(&key->user->nkeys);
1029 atomic_inc(&newowner->nkeys);
1030
David Howells363b02d2017-10-04 16:43:25 +01001031 if (key->state != KEY_IS_UNINSTANTIATED) {
Fredrik Tolf58016492006-06-26 00:24:51 -07001032 atomic_dec(&key->user->nikeys);
1033 atomic_inc(&newowner->nikeys);
1034 }
1035
1036 zapowner = key->user;
1037 key->user = newowner;
1038 key->uid = uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 }
1040
1041 /* change the GID */
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -08001042 if (group != (gid_t) -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 key->gid = gid;
1044
David Howellsf7e47672020-01-14 17:07:11 +00001045 notify_key(key, NOTIFY_KEY_SETATTR, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 ret = 0;
1047
Fredrik Tolf58016492006-06-26 00:24:51 -07001048error_put:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 up_write(&key->sem);
1050 key_put(key);
Fredrik Tolf58016492006-06-26 00:24:51 -07001051 if (zapowner)
1052 key_user_put(zapowner);
1053error:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 return ret;
1055
Fredrik Tolf58016492006-06-26 00:24:51 -07001056quota_overrun:
1057 spin_unlock(&newowner->lock);
1058 zapowner = newowner;
1059 ret = -EDQUOT;
1060 goto error_put;
David Howellsa8b17ed2011-01-20 16:38:27 +00001061}
Fredrik Tolf58016492006-06-26 00:24:51 -07001062
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063/*
David Howells973c9f42011-01-20 16:38:33 +00001064 * Change the permission mask on a key.
1065 *
1066 * The key must grant the caller Setattr permission for this to work, though
1067 * the key need not be fully instantiated yet. If the caller does not have
1068 * sysadmin capability, it may only change the permission on keys that it owns.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 */
Linus Torvalds028db3e2019-07-10 18:43:43 -07001070long keyctl_setperm_key(key_serial_t id, key_perm_t perm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071{
1072 struct key *key;
David Howells664cceb2005-09-28 17:03:15 +01001073 key_ref_t key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 long ret;
1075
Linus Torvalds028db3e2019-07-10 18:43:43 -07001076 ret = -EINVAL;
David Howells664cceb2005-09-28 17:03:15 +01001077 if (perm & ~(KEY_POS_ALL | KEY_USR_ALL | KEY_GRP_ALL | KEY_OTH_ALL))
Linus Torvalds028db3e2019-07-10 18:43:43 -07001078 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
David Howells55931222009-09-02 09:13:45 +01001080 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
Linus Torvalds028db3e2019-07-10 18:43:43 -07001081 KEY_NEED_SETATTR);
David Howells664cceb2005-09-28 17:03:15 +01001082 if (IS_ERR(key_ref)) {
1083 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 goto error;
1085 }
1086
David Howells664cceb2005-09-28 17:03:15 +01001087 key = key_ref_to_ptr(key_ref);
1088
Linus Torvalds028db3e2019-07-10 18:43:43 -07001089 /* make the changes with the locks held to prevent chown/chmod races */
1090 ret = -EACCES;
1091 down_write(&key->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
Linus Torvalds028db3e2019-07-10 18:43:43 -07001093 /* if we're not the sysadmin, we can only change a key that we own */
1094 if (capable(CAP_SYS_ADMIN) || uid_eq(key->uid, current_fsuid())) {
1095 key->perm = perm;
David Howellsf7e47672020-01-14 17:07:11 +00001096 notify_key(key, NOTIFY_KEY_SETATTR, 0);
Linus Torvalds028db3e2019-07-10 18:43:43 -07001097 ret = 0;
David Howells76d8aea2005-06-23 22:00:49 -07001098 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 up_write(&key->sem);
1101 key_put(key);
David Howells76d8aea2005-06-23 22:00:49 -07001102error:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +00001104}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
David Howells8bbf49762008-11-14 10:39:14 +11001106/*
David Howells973c9f42011-01-20 16:38:33 +00001107 * Get the destination keyring for instantiation and check that the caller has
1108 * Write permission on it.
David Howells8bbf49762008-11-14 10:39:14 +11001109 */
1110static long get_instantiation_keyring(key_serial_t ringid,
1111 struct request_key_auth *rka,
1112 struct key **_dest_keyring)
1113{
1114 key_ref_t dkref;
1115
David Howellseca1bf52008-12-29 00:41:51 +00001116 *_dest_keyring = NULL;
1117
David Howells8bbf49762008-11-14 10:39:14 +11001118 /* just return a NULL pointer if we weren't asked to make a link */
David Howellseca1bf52008-12-29 00:41:51 +00001119 if (ringid == 0)
David Howells8bbf49762008-11-14 10:39:14 +11001120 return 0;
David Howells8bbf49762008-11-14 10:39:14 +11001121
1122 /* if a specific keyring is nominated by ID, then use that */
1123 if (ringid > 0) {
David Howellsf5895942014-03-14 17:44:49 +00001124 dkref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
David Howells8bbf49762008-11-14 10:39:14 +11001125 if (IS_ERR(dkref))
1126 return PTR_ERR(dkref);
1127 *_dest_keyring = key_ref_to_ptr(dkref);
1128 return 0;
1129 }
1130
1131 if (ringid == KEY_SPEC_REQKEY_AUTH_KEY)
1132 return -EINVAL;
1133
1134 /* otherwise specify the destination keyring recorded in the
1135 * authorisation key (any KEY_SPEC_*_KEYRING) */
1136 if (ringid >= KEY_SPEC_REQUESTOR_KEYRING) {
David Howells21279cf2009-10-15 10:14:35 +01001137 *_dest_keyring = key_get(rka->dest_keyring);
David Howells8bbf49762008-11-14 10:39:14 +11001138 return 0;
1139 }
1140
1141 return -ENOKEY;
1142}
1143
David Howellsd84f4f92008-11-14 10:39:23 +11001144/*
David Howells973c9f42011-01-20 16:38:33 +00001145 * Change the request_key authorisation key on the current process.
David Howellsd84f4f92008-11-14 10:39:23 +11001146 */
1147static int keyctl_change_reqkey_auth(struct key *key)
1148{
1149 struct cred *new;
1150
1151 new = prepare_creds();
1152 if (!new)
1153 return -ENOMEM;
1154
1155 key_put(new->request_key_auth);
1156 new->request_key_auth = key_get(key);
1157
1158 return commit_creds(new);
1159}
1160
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161/*
David Howells973c9f42011-01-20 16:38:33 +00001162 * Instantiate a key with the specified payload and link the key into the
1163 * destination keyring if one is given.
1164 *
1165 * The caller must have the appropriate instantiation permit set for this to
1166 * work (see keyctl_assume_authority). No other permissions are required.
1167 *
1168 * If successful, 0 will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 */
David Howellsee009e4a02011-03-07 15:06:20 +00001170long keyctl_instantiate_key_common(key_serial_t id,
Al Virob353a1f2015-03-17 09:59:38 -04001171 struct iov_iter *from,
David Howellsee009e4a02011-03-07 15:06:20 +00001172 key_serial_t ringid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173{
David Howellsd84f4f92008-11-14 10:39:23 +11001174 const struct cred *cred = current_cred();
David Howells3e301482005-06-23 22:00:56 -07001175 struct request_key_auth *rka;
David Howells8bbf49762008-11-14 10:39:14 +11001176 struct key *instkey, *dest_keyring;
Al Virob353a1f2015-03-17 09:59:38 -04001177 size_t plen = from ? iov_iter_count(from) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 void *payload;
1179 long ret;
1180
David Howellsd84f4f92008-11-14 10:39:23 +11001181 kenter("%d,,%zu,%d", id, plen, ringid);
1182
Al Virob353a1f2015-03-17 09:59:38 -04001183 if (!plen)
1184 from = NULL;
1185
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 ret = -EINVAL;
David Howells38bbca62008-04-29 01:01:19 -07001187 if (plen > 1024 * 1024 - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 goto error;
1189
David Howellsb5f545c2006-01-08 01:02:47 -08001190 /* the appropriate instantiation authorisation key must have been
1191 * assumed before calling this */
1192 ret = -EPERM;
David Howellsd84f4f92008-11-14 10:39:23 +11001193 instkey = cred->request_key_auth;
David Howellsb5f545c2006-01-08 01:02:47 -08001194 if (!instkey)
1195 goto error;
1196
David Howells146aa8b2015-10-21 14:04:48 +01001197 rka = instkey->payload.data[0];
David Howellsb5f545c2006-01-08 01:02:47 -08001198 if (rka->target_key->serial != id)
1199 goto error;
1200
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 /* pull the payload in if one was supplied */
1202 payload = NULL;
1203
Al Virob353a1f2015-03-17 09:59:38 -04001204 if (from) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 ret = -ENOMEM;
Michal Hocko752ade62017-05-08 15:57:27 -07001206 payload = kvmalloc(plen, GFP_KERNEL);
1207 if (!payload)
1208 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
Al Virob353a1f2015-03-17 09:59:38 -04001210 ret = -EFAULT;
Al Virocbbd26b2016-11-01 22:09:04 -04001211 if (!copy_from_iter_full(payload, plen, from))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 goto error2;
1213 }
1214
David Howells3e301482005-06-23 22:00:56 -07001215 /* find the destination keyring amongst those belonging to the
1216 * requesting task */
David Howells8bbf49762008-11-14 10:39:14 +11001217 ret = get_instantiation_keyring(ringid, rka, &dest_keyring);
1218 if (ret < 0)
1219 goto error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
1221 /* instantiate the key and link it into a keyring */
David Howells3e301482005-06-23 22:00:56 -07001222 ret = key_instantiate_and_link(rka->target_key, payload, plen,
David Howells8bbf49762008-11-14 10:39:14 +11001223 dest_keyring, instkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
David Howells8bbf49762008-11-14 10:39:14 +11001225 key_put(dest_keyring);
David Howellsb5f545c2006-01-08 01:02:47 -08001226
1227 /* discard the assumed authority if it's just been disabled by
1228 * instantiation of the key */
David Howellsd84f4f92008-11-14 10:39:23 +11001229 if (ret == 0)
1230 keyctl_change_reqkey_auth(NULL);
David Howellsb5f545c2006-01-08 01:02:47 -08001231
1232error2:
Eric Biggers57070c82017-06-08 14:48:57 +01001233 if (payload) {
1234 memzero_explicit(payload, plen);
1235 kvfree(payload);
1236 }
David Howellsb5f545c2006-01-08 01:02:47 -08001237error:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +00001239}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241/*
David Howellsee009e4a02011-03-07 15:06:20 +00001242 * Instantiate a key with the specified payload and link the key into the
1243 * destination keyring if one is given.
1244 *
1245 * The caller must have the appropriate instantiation permit set for this to
1246 * work (see keyctl_assume_authority). No other permissions are required.
1247 *
1248 * If successful, 0 will be returned.
1249 */
1250long keyctl_instantiate_key(key_serial_t id,
1251 const void __user *_payload,
1252 size_t plen,
1253 key_serial_t ringid)
1254{
1255 if (_payload && plen) {
Al Virob353a1f2015-03-17 09:59:38 -04001256 struct iovec iov;
1257 struct iov_iter from;
1258 int ret;
David Howellsee009e4a02011-03-07 15:06:20 +00001259
Al Virob353a1f2015-03-17 09:59:38 -04001260 ret = import_single_range(WRITE, (void __user *)_payload, plen,
1261 &iov, &from);
1262 if (unlikely(ret))
1263 return ret;
1264
1265 return keyctl_instantiate_key_common(id, &from, ringid);
David Howellsee009e4a02011-03-07 15:06:20 +00001266 }
1267
Al Virob353a1f2015-03-17 09:59:38 -04001268 return keyctl_instantiate_key_common(id, NULL, ringid);
David Howellsee009e4a02011-03-07 15:06:20 +00001269}
1270
1271/*
1272 * Instantiate a key with the specified multipart payload and link the key into
1273 * the destination keyring if one is given.
1274 *
1275 * The caller must have the appropriate instantiation permit set for this to
1276 * work (see keyctl_assume_authority). No other permissions are required.
1277 *
1278 * If successful, 0 will be returned.
1279 */
1280long keyctl_instantiate_key_iov(key_serial_t id,
1281 const struct iovec __user *_payload_iov,
1282 unsigned ioc,
1283 key_serial_t ringid)
1284{
1285 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
Al Virob353a1f2015-03-17 09:59:38 -04001286 struct iov_iter from;
David Howellsee009e4a02011-03-07 15:06:20 +00001287 long ret;
1288
Al Virob353a1f2015-03-17 09:59:38 -04001289 if (!_payload_iov)
1290 ioc = 0;
David Howellsee009e4a02011-03-07 15:06:20 +00001291
Al Virob353a1f2015-03-17 09:59:38 -04001292 ret = import_iovec(WRITE, _payload_iov, ioc,
1293 ARRAY_SIZE(iovstack), &iov, &from);
David Howellsee009e4a02011-03-07 15:06:20 +00001294 if (ret < 0)
Al Virob353a1f2015-03-17 09:59:38 -04001295 return ret;
1296 ret = keyctl_instantiate_key_common(id, &from, ringid);
1297 kfree(iov);
David Howellsee009e4a02011-03-07 15:06:20 +00001298 return ret;
David Howellsee009e4a02011-03-07 15:06:20 +00001299}
1300
1301/*
David Howells973c9f42011-01-20 16:38:33 +00001302 * Negatively instantiate the key with the given timeout (in seconds) and link
1303 * the key into the destination keyring if one is given.
1304 *
1305 * The caller must have the appropriate instantiation permit set for this to
1306 * work (see keyctl_assume_authority). No other permissions are required.
1307 *
1308 * The key and any links to the key will be automatically garbage collected
1309 * after the timeout expires.
1310 *
1311 * Negative keys are used to rate limit repeated request_key() calls by causing
1312 * them to return -ENOKEY until the negative key expires.
1313 *
1314 * If successful, 0 will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 */
1316long keyctl_negate_key(key_serial_t id, unsigned timeout, key_serial_t ringid)
1317{
David Howellsfdd1b942011-03-07 15:06:09 +00001318 return keyctl_reject_key(id, timeout, ENOKEY, ringid);
1319}
1320
1321/*
1322 * Negatively instantiate the key with the given timeout (in seconds) and error
1323 * code and link the key into the destination keyring if one is given.
1324 *
1325 * The caller must have the appropriate instantiation permit set for this to
1326 * work (see keyctl_assume_authority). No other permissions are required.
1327 *
1328 * The key and any links to the key will be automatically garbage collected
1329 * after the timeout expires.
1330 *
1331 * Negative keys are used to rate limit repeated request_key() calls by causing
1332 * them to return the specified error code until the negative key expires.
1333 *
1334 * If successful, 0 will be returned.
1335 */
1336long keyctl_reject_key(key_serial_t id, unsigned timeout, unsigned error,
1337 key_serial_t ringid)
1338{
David Howellsd84f4f92008-11-14 10:39:23 +11001339 const struct cred *cred = current_cred();
David Howells3e301482005-06-23 22:00:56 -07001340 struct request_key_auth *rka;
David Howells8bbf49762008-11-14 10:39:14 +11001341 struct key *instkey, *dest_keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 long ret;
1343
David Howellsfdd1b942011-03-07 15:06:09 +00001344 kenter("%d,%u,%u,%d", id, timeout, error, ringid);
1345
1346 /* must be a valid error code and mustn't be a kernel special */
1347 if (error <= 0 ||
1348 error >= MAX_ERRNO ||
1349 error == ERESTARTSYS ||
1350 error == ERESTARTNOINTR ||
1351 error == ERESTARTNOHAND ||
1352 error == ERESTART_RESTARTBLOCK)
1353 return -EINVAL;
David Howellsd84f4f92008-11-14 10:39:23 +11001354
David Howellsb5f545c2006-01-08 01:02:47 -08001355 /* the appropriate instantiation authorisation key must have been
1356 * assumed before calling this */
1357 ret = -EPERM;
David Howellsd84f4f92008-11-14 10:39:23 +11001358 instkey = cred->request_key_auth;
David Howellsb5f545c2006-01-08 01:02:47 -08001359 if (!instkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
David Howells146aa8b2015-10-21 14:04:48 +01001362 rka = instkey->payload.data[0];
David Howellsb5f545c2006-01-08 01:02:47 -08001363 if (rka->target_key->serial != id)
1364 goto error;
David Howells3e301482005-06-23 22:00:56 -07001365
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 /* find the destination keyring if present (which must also be
1367 * writable) */
David Howells8bbf49762008-11-14 10:39:14 +11001368 ret = get_instantiation_keyring(ringid, rka, &dest_keyring);
1369 if (ret < 0)
1370 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
1372 /* instantiate the key and link it into a keyring */
David Howellsfdd1b942011-03-07 15:06:09 +00001373 ret = key_reject_and_link(rka->target_key, timeout, error,
David Howells8bbf49762008-11-14 10:39:14 +11001374 dest_keyring, instkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
David Howells8bbf49762008-11-14 10:39:14 +11001376 key_put(dest_keyring);
David Howellsb5f545c2006-01-08 01:02:47 -08001377
1378 /* discard the assumed authority if it's just been disabled by
1379 * instantiation of the key */
David Howellsd84f4f92008-11-14 10:39:23 +11001380 if (ret == 0)
1381 keyctl_change_reqkey_auth(NULL);
David Howellsb5f545c2006-01-08 01:02:47 -08001382
1383error:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +00001385}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387/*
David Howells973c9f42011-01-20 16:38:33 +00001388 * Read or set the default keyring in which request_key() will cache keys and
1389 * return the old setting.
1390 *
Eric Biggersc9f838d2017-04-18 15:31:09 +01001391 * If a thread or process keyring is specified then it will be created if it
1392 * doesn't yet exist. The old setting will be returned if successful.
David Howells3e301482005-06-23 22:00:56 -07001393 */
1394long keyctl_set_reqkey_keyring(int reqkey_defl)
1395{
David Howellsd84f4f92008-11-14 10:39:23 +11001396 struct cred *new;
1397 int ret, old_setting;
1398
1399 old_setting = current_cred_xxx(jit_keyring);
1400
1401 if (reqkey_defl == KEY_REQKEY_DEFL_NO_CHANGE)
1402 return old_setting;
1403
1404 new = prepare_creds();
1405 if (!new)
1406 return -ENOMEM;
David Howells3e301482005-06-23 22:00:56 -07001407
1408 switch (reqkey_defl) {
1409 case KEY_REQKEY_DEFL_THREAD_KEYRING:
David Howellsd84f4f92008-11-14 10:39:23 +11001410 ret = install_thread_keyring_to_cred(new);
David Howells3e301482005-06-23 22:00:56 -07001411 if (ret < 0)
David Howellsd84f4f92008-11-14 10:39:23 +11001412 goto error;
David Howells3e301482005-06-23 22:00:56 -07001413 goto set;
1414
1415 case KEY_REQKEY_DEFL_PROCESS_KEYRING:
David Howellsd84f4f92008-11-14 10:39:23 +11001416 ret = install_process_keyring_to_cred(new);
Eric Biggersc9f838d2017-04-18 15:31:09 +01001417 if (ret < 0)
1418 goto error;
David Howellsd84f4f92008-11-14 10:39:23 +11001419 goto set;
David Howells3e301482005-06-23 22:00:56 -07001420
1421 case KEY_REQKEY_DEFL_DEFAULT:
1422 case KEY_REQKEY_DEFL_SESSION_KEYRING:
1423 case KEY_REQKEY_DEFL_USER_KEYRING:
1424 case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
David Howellsd84f4f92008-11-14 10:39:23 +11001425 case KEY_REQKEY_DEFL_REQUESTOR_KEYRING:
1426 goto set;
David Howells3e301482005-06-23 22:00:56 -07001427
1428 case KEY_REQKEY_DEFL_NO_CHANGE:
David Howells3e301482005-06-23 22:00:56 -07001429 case KEY_REQKEY_DEFL_GROUP_KEYRING:
1430 default:
David Howellsd84f4f92008-11-14 10:39:23 +11001431 ret = -EINVAL;
1432 goto error;
David Howells3e301482005-06-23 22:00:56 -07001433 }
1434
David Howellsd84f4f92008-11-14 10:39:23 +11001435set:
1436 new->jit_keyring = reqkey_defl;
1437 commit_creds(new);
1438 return old_setting;
1439error:
1440 abort_creds(new);
Dan Carpenter4303ef12010-06-11 17:30:05 +01001441 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +00001442}
David Howellsd84f4f92008-11-14 10:39:23 +11001443
David Howells3e301482005-06-23 22:00:56 -07001444/*
David Howells973c9f42011-01-20 16:38:33 +00001445 * Set or clear the timeout on a key.
1446 *
1447 * Either the key must grant the caller Setattr permission or else the caller
1448 * must hold an instantiation authorisation token for the key.
1449 *
1450 * The timeout is either 0 to clear the timeout, or a number of seconds from
1451 * the current time. The key and any links to the key will be automatically
1452 * garbage collected after the timeout expires.
1453 *
Mimi Zohard3600bc2015-11-10 08:34:46 -05001454 * Keys with KEY_FLAG_KEEP set should not be timed out.
1455 *
David Howells973c9f42011-01-20 16:38:33 +00001456 * If successful, 0 is returned.
David Howells017679c2006-01-08 01:02:43 -08001457 */
1458long keyctl_set_timeout(key_serial_t id, unsigned timeout)
1459{
David Howells91562352010-06-11 17:31:05 +01001460 struct key *key, *instkey;
David Howells017679c2006-01-08 01:02:43 -08001461 key_ref_t key_ref;
David Howells017679c2006-01-08 01:02:43 -08001462 long ret;
1463
David Howells55931222009-09-02 09:13:45 +01001464 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
Linus Torvalds028db3e2019-07-10 18:43:43 -07001465 KEY_NEED_SETATTR);
David Howells017679c2006-01-08 01:02:43 -08001466 if (IS_ERR(key_ref)) {
David Howells91562352010-06-11 17:31:05 +01001467 /* setting the timeout on a key under construction is permitted
1468 * if we have the authorisation token handy */
1469 if (PTR_ERR(key_ref) == -EACCES) {
1470 instkey = key_get_instantiation_authkey(id);
1471 if (!IS_ERR(instkey)) {
1472 key_put(instkey);
1473 key_ref = lookup_user_key(id,
1474 KEY_LOOKUP_PARTIAL,
David Howells8c0637e2020-05-12 15:16:29 +01001475 KEY_AUTHTOKEN_OVERRIDE);
David Howells91562352010-06-11 17:31:05 +01001476 if (!IS_ERR(key_ref))
1477 goto okay;
1478 }
1479 }
1480
David Howells017679c2006-01-08 01:02:43 -08001481 ret = PTR_ERR(key_ref);
1482 goto error;
1483 }
1484
David Howells91562352010-06-11 17:31:05 +01001485okay:
David Howells017679c2006-01-08 01:02:43 -08001486 key = key_ref_to_ptr(key_ref);
Mimi Zohar1d6d1672016-01-07 07:46:36 -05001487 ret = 0;
David Howellsf7e47672020-01-14 17:07:11 +00001488 if (test_bit(KEY_FLAG_KEEP, &key->flags)) {
Mimi Zohard3600bc2015-11-10 08:34:46 -05001489 ret = -EPERM;
David Howellsf7e47672020-01-14 17:07:11 +00001490 } else {
Mimi Zohard3600bc2015-11-10 08:34:46 -05001491 key_set_timeout(key, timeout);
David Howellsf7e47672020-01-14 17:07:11 +00001492 notify_key(key, NOTIFY_KEY_SETATTR, 0);
1493 }
David Howells017679c2006-01-08 01:02:43 -08001494 key_put(key);
1495
David Howells017679c2006-01-08 01:02:43 -08001496error:
1497 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +00001498}
David Howells017679c2006-01-08 01:02:43 -08001499
David Howells017679c2006-01-08 01:02:43 -08001500/*
David Howells973c9f42011-01-20 16:38:33 +00001501 * Assume (or clear) the authority to instantiate the specified key.
1502 *
1503 * This sets the authoritative token currently in force for key instantiation.
1504 * This must be done for a key to be instantiated. It has the effect of making
1505 * available all the keys from the caller of the request_key() that created a
1506 * key to request_key() calls made by the caller of this function.
1507 *
1508 * The caller must have the instantiation key in their process keyrings with a
1509 * Search permission grant available to the caller.
1510 *
1511 * If the ID given is 0, then the setting will be cleared and 0 returned.
1512 *
1513 * If the ID given has a matching an authorisation key, then that key will be
1514 * set and its ID will be returned. The authorisation key can be read to get
1515 * the callout information passed to request_key().
David Howellsb5f545c2006-01-08 01:02:47 -08001516 */
1517long keyctl_assume_authority(key_serial_t id)
1518{
1519 struct key *authkey;
1520 long ret;
1521
1522 /* special key IDs aren't permitted */
1523 ret = -EINVAL;
1524 if (id < 0)
1525 goto error;
1526
1527 /* we divest ourselves of authority if given an ID of 0 */
1528 if (id == 0) {
David Howellsd84f4f92008-11-14 10:39:23 +11001529 ret = keyctl_change_reqkey_auth(NULL);
David Howellsb5f545c2006-01-08 01:02:47 -08001530 goto error;
1531 }
1532
1533 /* attempt to assume the authority temporarily granted to us whilst we
1534 * instantiate the specified key
1535 * - the authorisation key must be in the current task's keyrings
1536 * somewhere
1537 */
1538 authkey = key_get_instantiation_authkey(id);
1539 if (IS_ERR(authkey)) {
1540 ret = PTR_ERR(authkey);
1541 goto error;
1542 }
1543
David Howellsd84f4f92008-11-14 10:39:23 +11001544 ret = keyctl_change_reqkey_auth(authkey);
Eric Biggers884bee02017-09-18 11:36:12 -07001545 if (ret == 0)
1546 ret = authkey->serial;
David Howellsd84f4f92008-11-14 10:39:23 +11001547 key_put(authkey);
David Howellsb5f545c2006-01-08 01:02:47 -08001548error:
1549 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +00001550}
David Howellsb5f545c2006-01-08 01:02:47 -08001551
David Howells70a5bb72008-04-29 01:01:26 -07001552/*
David Howells973c9f42011-01-20 16:38:33 +00001553 * Get a key's the LSM security label.
1554 *
1555 * The key must grant the caller View permission for this to work.
1556 *
1557 * If there's a buffer, then up to buflen bytes of data will be placed into it.
1558 *
1559 * If successful, the amount of information available will be returned,
1560 * irrespective of how much was copied (including the terminal NUL).
David Howells70a5bb72008-04-29 01:01:26 -07001561 */
1562long keyctl_get_security(key_serial_t keyid,
1563 char __user *buffer,
1564 size_t buflen)
1565{
1566 struct key *key, *instkey;
1567 key_ref_t key_ref;
1568 char *context;
1569 long ret;
1570
David Howellsf5895942014-03-14 17:44:49 +00001571 key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_NEED_VIEW);
David Howells70a5bb72008-04-29 01:01:26 -07001572 if (IS_ERR(key_ref)) {
1573 if (PTR_ERR(key_ref) != -EACCES)
1574 return PTR_ERR(key_ref);
1575
1576 /* viewing a key under construction is also permitted if we
1577 * have the authorisation token handy */
1578 instkey = key_get_instantiation_authkey(keyid);
1579 if (IS_ERR(instkey))
Roel Kluinfa1cc7b2009-12-15 15:05:12 -08001580 return PTR_ERR(instkey);
David Howells70a5bb72008-04-29 01:01:26 -07001581 key_put(instkey);
1582
David Howells8c0637e2020-05-12 15:16:29 +01001583 key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL,
1584 KEY_AUTHTOKEN_OVERRIDE);
David Howells70a5bb72008-04-29 01:01:26 -07001585 if (IS_ERR(key_ref))
1586 return PTR_ERR(key_ref);
1587 }
1588
1589 key = key_ref_to_ptr(key_ref);
1590 ret = security_key_getsecurity(key, &context);
1591 if (ret == 0) {
1592 /* if no information was returned, give userspace an empty
1593 * string */
1594 ret = 1;
1595 if (buffer && buflen > 0 &&
1596 copy_to_user(buffer, "", 1) != 0)
1597 ret = -EFAULT;
1598 } else if (ret > 0) {
1599 /* return as much data as there's room for */
1600 if (buffer && buflen > 0) {
1601 if (buflen > ret)
1602 buflen = ret;
1603
1604 if (copy_to_user(buffer, context, buflen) != 0)
1605 ret = -EFAULT;
1606 }
1607
1608 kfree(context);
1609 }
1610
1611 key_ref_put(key_ref);
1612 return ret;
1613}
1614
David Howellsee18d642009-09-02 09:14:21 +01001615/*
David Howells973c9f42011-01-20 16:38:33 +00001616 * Attempt to install the calling process's session keyring on the process's
1617 * parent process.
1618 *
Linus Torvalds028db3e2019-07-10 18:43:43 -07001619 * The keyring must exist and must grant the caller LINK permission, and the
David Howells973c9f42011-01-20 16:38:33 +00001620 * parent process must be single-threaded and must have the same effective
1621 * ownership as this process and mustn't be SUID/SGID.
1622 *
1623 * The keyring will be emplaced on the parent when it next resumes userspace.
1624 *
1625 * If successful, 0 will be returned.
David Howellsee18d642009-09-02 09:14:21 +01001626 */
1627long keyctl_session_to_parent(void)
1628{
1629 struct task_struct *me, *parent;
1630 const struct cred *mycred, *pcred;
Al Viro67d12142012-06-27 11:07:19 +04001631 struct callback_head *newwork, *oldwork;
David Howellsee18d642009-09-02 09:14:21 +01001632 key_ref_t keyring_r;
Oleg Nesterov413cd3d2012-05-11 10:59:08 +10001633 struct cred *cred;
David Howellsee18d642009-09-02 09:14:21 +01001634 int ret;
1635
Linus Torvalds028db3e2019-07-10 18:43:43 -07001636 keyring_r = lookup_user_key(KEY_SPEC_SESSION_KEYRING, 0, KEY_NEED_LINK);
David Howellsee18d642009-09-02 09:14:21 +01001637 if (IS_ERR(keyring_r))
1638 return PTR_ERR(keyring_r);
1639
Oleg Nesterov413cd3d2012-05-11 10:59:08 +10001640 ret = -ENOMEM;
Oleg Nesterov413cd3d2012-05-11 10:59:08 +10001641
David Howellsee18d642009-09-02 09:14:21 +01001642 /* our parent is going to need a new cred struct, a new tgcred struct
1643 * and new security data, so we allocate them here to prevent ENOMEM in
1644 * our parent */
David Howellsee18d642009-09-02 09:14:21 +01001645 cred = cred_alloc_blank();
1646 if (!cred)
Al Viro67d12142012-06-27 11:07:19 +04001647 goto error_keyring;
1648 newwork = &cred->rcu;
David Howellsee18d642009-09-02 09:14:21 +01001649
David Howells3a505972012-10-02 19:24:29 +01001650 cred->session_keyring = key_ref_to_ptr(keyring_r);
1651 keyring_r = NULL;
Al Viro67d12142012-06-27 11:07:19 +04001652 init_task_work(newwork, key_change_session_keyring);
David Howellsee18d642009-09-02 09:14:21 +01001653
1654 me = current;
David Howells9d1ac652010-09-10 09:59:46 +01001655 rcu_read_lock();
David Howellsee18d642009-09-02 09:14:21 +01001656 write_lock_irq(&tasklist_lock);
1657
David Howellsee18d642009-09-02 09:14:21 +01001658 ret = -EPERM;
Oleg Nesterov413cd3d2012-05-11 10:59:08 +10001659 oldwork = NULL;
David Howells7936d162019-05-22 14:09:29 +01001660 parent = rcu_dereference_protected(me->real_parent,
1661 lockdep_is_held(&tasklist_lock));
David Howellsee18d642009-09-02 09:14:21 +01001662
1663 /* the parent mustn't be init and mustn't be a kernel thread */
1664 if (parent->pid <= 1 || !parent->mm)
Oleg Nesterov413cd3d2012-05-11 10:59:08 +10001665 goto unlock;
David Howellsee18d642009-09-02 09:14:21 +01001666
1667 /* the parent must be single threaded */
Oleg Nesterovdd98acf2010-05-26 14:43:23 -07001668 if (!thread_group_empty(parent))
Oleg Nesterov413cd3d2012-05-11 10:59:08 +10001669 goto unlock;
David Howellsee18d642009-09-02 09:14:21 +01001670
1671 /* the parent and the child must have different session keyrings or
1672 * there's no point */
1673 mycred = current_cred();
1674 pcred = __task_cred(parent);
1675 if (mycred == pcred ||
David Howells3a505972012-10-02 19:24:29 +01001676 mycred->session_keyring == pcred->session_keyring) {
Oleg Nesterov413cd3d2012-05-11 10:59:08 +10001677 ret = 0;
1678 goto unlock;
1679 }
David Howellsee18d642009-09-02 09:14:21 +01001680
1681 /* the parent must have the same effective ownership and mustn't be
1682 * SUID/SGID */
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -08001683 if (!uid_eq(pcred->uid, mycred->euid) ||
1684 !uid_eq(pcred->euid, mycred->euid) ||
1685 !uid_eq(pcred->suid, mycred->euid) ||
1686 !gid_eq(pcred->gid, mycred->egid) ||
1687 !gid_eq(pcred->egid, mycred->egid) ||
1688 !gid_eq(pcred->sgid, mycred->egid))
Oleg Nesterov413cd3d2012-05-11 10:59:08 +10001689 goto unlock;
David Howellsee18d642009-09-02 09:14:21 +01001690
1691 /* the keyrings must have the same UID */
David Howells3a505972012-10-02 19:24:29 +01001692 if ((pcred->session_keyring &&
Linus Torvalds2a74dbb2012-12-16 15:40:50 -08001693 !uid_eq(pcred->session_keyring->uid, mycred->euid)) ||
1694 !uid_eq(mycred->session_keyring->uid, mycred->euid))
Oleg Nesterov413cd3d2012-05-11 10:59:08 +10001695 goto unlock;
David Howellsee18d642009-09-02 09:14:21 +01001696
Oleg Nesterov413cd3d2012-05-11 10:59:08 +10001697 /* cancel an already pending keyring replacement */
1698 oldwork = task_work_cancel(parent, key_change_session_keyring);
David Howellsee18d642009-09-02 09:14:21 +01001699
1700 /* the replacement session keyring is applied just prior to userspace
1701 * restarting */
Al Viro67d12142012-06-27 11:07:19 +04001702 ret = task_work_add(parent, newwork, true);
Oleg Nesterov413cd3d2012-05-11 10:59:08 +10001703 if (!ret)
1704 newwork = NULL;
1705unlock:
David Howellsee18d642009-09-02 09:14:21 +01001706 write_unlock_irq(&tasklist_lock);
David Howells9d1ac652010-09-10 09:59:46 +01001707 rcu_read_unlock();
Al Viro67d12142012-06-27 11:07:19 +04001708 if (oldwork)
1709 put_cred(container_of(oldwork, struct cred, rcu));
1710 if (newwork)
1711 put_cred(cred);
David Howellsee18d642009-09-02 09:14:21 +01001712 return ret;
1713
1714error_keyring:
1715 key_ref_put(keyring_r);
1716 return ret;
1717}
1718
David Howellsb5f545c2006-01-08 01:02:47 -08001719/*
Mat Martineau6563c912017-03-01 16:44:09 -08001720 * Apply a restriction to a given keyring.
1721 *
1722 * The caller must have Setattr permission to change keyring restrictions.
1723 *
1724 * The requested type name may be a NULL pointer to reject all attempts
Eric Biggers18026d82017-12-08 15:13:29 +00001725 * to link to the keyring. In this case, _restriction must also be NULL.
1726 * Otherwise, both _type and _restriction must be non-NULL.
Mat Martineau6563c912017-03-01 16:44:09 -08001727 *
1728 * Returns 0 if successful.
1729 */
1730long keyctl_restrict_keyring(key_serial_t id, const char __user *_type,
1731 const char __user *_restriction)
1732{
1733 key_ref_t key_ref;
Mat Martineau6563c912017-03-01 16:44:09 -08001734 char type[32];
1735 char *restriction = NULL;
1736 long ret;
1737
Linus Torvalds028db3e2019-07-10 18:43:43 -07001738 key_ref = lookup_user_key(id, 0, KEY_NEED_SETATTR);
Mat Martineau6563c912017-03-01 16:44:09 -08001739 if (IS_ERR(key_ref))
1740 return PTR_ERR(key_ref);
1741
Eric Biggers18026d82017-12-08 15:13:29 +00001742 ret = -EINVAL;
Mat Martineau6563c912017-03-01 16:44:09 -08001743 if (_type) {
Eric Biggers18026d82017-12-08 15:13:29 +00001744 if (!_restriction)
1745 goto error;
1746
Mat Martineau6563c912017-03-01 16:44:09 -08001747 ret = key_get_type_from_user(type, _type, sizeof(type));
1748 if (ret < 0)
1749 goto error;
Mat Martineau6563c912017-03-01 16:44:09 -08001750
1751 restriction = strndup_user(_restriction, PAGE_SIZE);
1752 if (IS_ERR(restriction)) {
1753 ret = PTR_ERR(restriction);
1754 goto error;
1755 }
Eric Biggers18026d82017-12-08 15:13:29 +00001756 } else {
1757 if (_restriction)
1758 goto error;
Mat Martineau6563c912017-03-01 16:44:09 -08001759 }
1760
Eric Biggers18026d82017-12-08 15:13:29 +00001761 ret = keyring_restrict(key_ref, _type ? type : NULL, restriction);
Mat Martineau6563c912017-03-01 16:44:09 -08001762 kfree(restriction);
Mat Martineau6563c912017-03-01 16:44:09 -08001763error:
1764 key_ref_put(key_ref);
Mat Martineau6563c912017-03-01 16:44:09 -08001765 return ret;
1766}
1767
David Howellsf7e47672020-01-14 17:07:11 +00001768#ifdef CONFIG_KEY_NOTIFICATIONS
1769/*
1770 * Watch for changes to a key.
1771 *
1772 * The caller must have View permission to watch a key or keyring.
1773 */
1774long keyctl_watch_key(key_serial_t id, int watch_queue_fd, int watch_id)
1775{
1776 struct watch_queue *wqueue;
1777 struct watch_list *wlist = NULL;
1778 struct watch *watch = NULL;
1779 struct key *key;
1780 key_ref_t key_ref;
1781 long ret;
1782
1783 if (watch_id < -1 || watch_id > 0xff)
1784 return -EINVAL;
1785
1786 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE, KEY_NEED_VIEW);
1787 if (IS_ERR(key_ref))
1788 return PTR_ERR(key_ref);
1789 key = key_ref_to_ptr(key_ref);
1790
1791 wqueue = get_watch_queue(watch_queue_fd);
1792 if (IS_ERR(wqueue)) {
1793 ret = PTR_ERR(wqueue);
1794 goto err_key;
1795 }
1796
1797 if (watch_id >= 0) {
1798 ret = -ENOMEM;
1799 if (!key->watchers) {
1800 wlist = kzalloc(sizeof(*wlist), GFP_KERNEL);
1801 if (!wlist)
1802 goto err_wqueue;
1803 init_watch_list(wlist, NULL);
1804 }
1805
1806 watch = kzalloc(sizeof(*watch), GFP_KERNEL);
1807 if (!watch)
1808 goto err_wlist;
1809
1810 init_watch(watch, wqueue);
1811 watch->id = key->serial;
1812 watch->info_id = (u32)watch_id << WATCH_INFO_ID__SHIFT;
1813
1814 ret = security_watch_key(key);
1815 if (ret < 0)
1816 goto err_watch;
1817
1818 down_write(&key->sem);
1819 if (!key->watchers) {
1820 key->watchers = wlist;
1821 wlist = NULL;
1822 }
1823
1824 ret = add_watch_to_object(watch, key->watchers);
1825 up_write(&key->sem);
1826
1827 if (ret == 0)
1828 watch = NULL;
1829 } else {
1830 ret = -EBADSLT;
1831 if (key->watchers) {
1832 down_write(&key->sem);
1833 ret = remove_watch_from_object(key->watchers,
1834 wqueue, key_serial(key),
1835 false);
1836 up_write(&key->sem);
1837 }
1838 }
1839
1840err_watch:
1841 kfree(watch);
1842err_wlist:
1843 kfree(wlist);
1844err_wqueue:
1845 put_watch_queue(wqueue);
1846err_key:
1847 key_put(key);
1848 return ret;
1849}
1850#endif /* CONFIG_KEY_NOTIFICATIONS */
1851
Mat Martineau6563c912017-03-01 16:44:09 -08001852/*
David Howells45e0f302019-05-30 14:53:10 +01001853 * Get keyrings subsystem capabilities.
1854 */
1855long keyctl_capabilities(unsigned char __user *_buffer, size_t buflen)
1856{
1857 size_t size = buflen;
1858
1859 if (size > 0) {
1860 if (size > sizeof(keyrings_capabilities))
1861 size = sizeof(keyrings_capabilities);
1862 if (copy_to_user(_buffer, keyrings_capabilities, size) != 0)
1863 return -EFAULT;
1864 if (size < buflen &&
1865 clear_user(_buffer + size, buflen - size) != 0)
1866 return -EFAULT;
1867 }
1868
1869 return sizeof(keyrings_capabilities);
1870}
1871
1872/*
David Howells973c9f42011-01-20 16:38:33 +00001873 * The key control system call
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 */
Heiko Carstens938bb9f2009-01-14 14:14:30 +01001875SYSCALL_DEFINE5(keyctl, int, option, unsigned long, arg2, unsigned long, arg3,
1876 unsigned long, arg4, unsigned long, arg5)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877{
1878 switch (option) {
1879 case KEYCTL_GET_KEYRING_ID:
1880 return keyctl_get_keyring_ID((key_serial_t) arg2,
1881 (int) arg3);
1882
1883 case KEYCTL_JOIN_SESSION_KEYRING:
1884 return keyctl_join_session_keyring((const char __user *) arg2);
1885
1886 case KEYCTL_UPDATE:
1887 return keyctl_update_key((key_serial_t) arg2,
1888 (const void __user *) arg3,
1889 (size_t) arg4);
1890
1891 case KEYCTL_REVOKE:
1892 return keyctl_revoke_key((key_serial_t) arg2);
1893
1894 case KEYCTL_DESCRIBE:
1895 return keyctl_describe_key((key_serial_t) arg2,
1896 (char __user *) arg3,
1897 (unsigned) arg4);
1898
1899 case KEYCTL_CLEAR:
1900 return keyctl_keyring_clear((key_serial_t) arg2);
1901
1902 case KEYCTL_LINK:
1903 return keyctl_keyring_link((key_serial_t) arg2,
1904 (key_serial_t) arg3);
1905
1906 case KEYCTL_UNLINK:
1907 return keyctl_keyring_unlink((key_serial_t) arg2,
1908 (key_serial_t) arg3);
1909
1910 case KEYCTL_SEARCH:
1911 return keyctl_keyring_search((key_serial_t) arg2,
1912 (const char __user *) arg3,
1913 (const char __user *) arg4,
1914 (key_serial_t) arg5);
1915
1916 case KEYCTL_READ:
1917 return keyctl_read_key((key_serial_t) arg2,
1918 (char __user *) arg3,
1919 (size_t) arg4);
1920
1921 case KEYCTL_CHOWN:
1922 return keyctl_chown_key((key_serial_t) arg2,
1923 (uid_t) arg3,
1924 (gid_t) arg4);
1925
1926 case KEYCTL_SETPERM:
1927 return keyctl_setperm_key((key_serial_t) arg2,
Linus Torvalds028db3e2019-07-10 18:43:43 -07001928 (key_perm_t) arg3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929
1930 case KEYCTL_INSTANTIATE:
1931 return keyctl_instantiate_key((key_serial_t) arg2,
1932 (const void __user *) arg3,
1933 (size_t) arg4,
1934 (key_serial_t) arg5);
1935
1936 case KEYCTL_NEGATE:
1937 return keyctl_negate_key((key_serial_t) arg2,
1938 (unsigned) arg3,
1939 (key_serial_t) arg4);
1940
David Howells3e301482005-06-23 22:00:56 -07001941 case KEYCTL_SET_REQKEY_KEYRING:
1942 return keyctl_set_reqkey_keyring(arg2);
1943
David Howells017679c2006-01-08 01:02:43 -08001944 case KEYCTL_SET_TIMEOUT:
1945 return keyctl_set_timeout((key_serial_t) arg2,
1946 (unsigned) arg3);
1947
David Howellsb5f545c2006-01-08 01:02:47 -08001948 case KEYCTL_ASSUME_AUTHORITY:
1949 return keyctl_assume_authority((key_serial_t) arg2);
1950
David Howells70a5bb72008-04-29 01:01:26 -07001951 case KEYCTL_GET_SECURITY:
1952 return keyctl_get_security((key_serial_t) arg2,
James Morris90bd49a2008-12-29 14:35:35 +11001953 (char __user *) arg3,
David Howells70a5bb72008-04-29 01:01:26 -07001954 (size_t) arg4);
1955
David Howellsee18d642009-09-02 09:14:21 +01001956 case KEYCTL_SESSION_TO_PARENT:
1957 return keyctl_session_to_parent();
1958
David Howellsfdd1b942011-03-07 15:06:09 +00001959 case KEYCTL_REJECT:
1960 return keyctl_reject_key((key_serial_t) arg2,
1961 (unsigned) arg3,
1962 (unsigned) arg4,
1963 (key_serial_t) arg5);
1964
David Howellsee009e4a02011-03-07 15:06:20 +00001965 case KEYCTL_INSTANTIATE_IOV:
1966 return keyctl_instantiate_key_iov(
1967 (key_serial_t) arg2,
1968 (const struct iovec __user *) arg3,
1969 (unsigned) arg4,
1970 (key_serial_t) arg5);
1971
David Howellsfd758152012-05-11 10:56:56 +01001972 case KEYCTL_INVALIDATE:
1973 return keyctl_invalidate_key((key_serial_t) arg2);
1974
David Howellsf36f8c72013-09-24 10:35:19 +01001975 case KEYCTL_GET_PERSISTENT:
1976 return keyctl_get_persistent((uid_t)arg2, (key_serial_t)arg3);
1977
Mat Martineauddbb4112016-04-12 19:54:58 +01001978 case KEYCTL_DH_COMPUTE:
1979 return keyctl_dh_compute((struct keyctl_dh_params __user *) arg2,
Stephan Mueller4693fc72016-05-26 23:38:12 +02001980 (char __user *) arg3, (size_t) arg4,
Stephan Muellerf1c316a2016-08-19 20:39:09 +02001981 (struct keyctl_kdf_params __user *) arg5);
Mat Martineauddbb4112016-04-12 19:54:58 +01001982
Mat Martineau6563c912017-03-01 16:44:09 -08001983 case KEYCTL_RESTRICT_KEYRING:
1984 return keyctl_restrict_keyring((key_serial_t) arg2,
1985 (const char __user *) arg3,
1986 (const char __user *) arg4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987
David Howells00d60fd2018-10-09 17:46:59 +01001988 case KEYCTL_PKEY_QUERY:
1989 if (arg3 != 0)
1990 return -EINVAL;
1991 return keyctl_pkey_query((key_serial_t)arg2,
1992 (const char __user *)arg4,
Ben Dooks468e91c2019-03-01 11:30:26 +00001993 (struct keyctl_pkey_query __user *)arg5);
David Howells00d60fd2018-10-09 17:46:59 +01001994
1995 case KEYCTL_PKEY_ENCRYPT:
1996 case KEYCTL_PKEY_DECRYPT:
1997 case KEYCTL_PKEY_SIGN:
1998 return keyctl_pkey_e_d_s(
1999 option,
2000 (const struct keyctl_pkey_params __user *)arg2,
2001 (const char __user *)arg3,
2002 (const void __user *)arg4,
2003 (void __user *)arg5);
2004
2005 case KEYCTL_PKEY_VERIFY:
2006 return keyctl_pkey_verify(
2007 (const struct keyctl_pkey_params __user *)arg2,
2008 (const char __user *)arg3,
2009 (const void __user *)arg4,
2010 (const void __user *)arg5);
2011
David Howellsed0ac5c2019-05-20 21:51:50 +01002012 case KEYCTL_MOVE:
2013 return keyctl_keyring_move((key_serial_t)arg2,
2014 (key_serial_t)arg3,
2015 (key_serial_t)arg4,
2016 (unsigned int)arg5);
2017
David Howells45e0f302019-05-30 14:53:10 +01002018 case KEYCTL_CAPABILITIES:
2019 return keyctl_capabilities((unsigned char __user *)arg2, (size_t)arg3);
2020
David Howellsf7e47672020-01-14 17:07:11 +00002021 case KEYCTL_WATCH_KEY:
2022 return keyctl_watch_key((key_serial_t)arg2, (int)arg3, (int)arg4);
2023
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 default:
2025 return -EOPNOTSUPP;
2026 }
2027}