blob: 7d8de1c9a47816a05f230d417b249d9ea16abf25 [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)) {
437 key_ref = lookup_user_key(id, 0, 0);
438 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)) {
482 keyring_ref = lookup_user_key(ringid, 0, 0);
483 if (IS_ERR(keyring_ref))
484 goto error;
485 if (test_bit(KEY_FLAG_ROOT_CAN_CLEAR,
486 &key_ref_to_ptr(keyring_ref)->flags))
487 goto clear;
488 goto error_put;
489 }
490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 goto error;
492 }
493
David Howells700920e2012-01-18 15:31:45 +0000494clear:
Mimi Zohard3600bc2015-11-10 08:34:46 -0500495 keyring = key_ref_to_ptr(keyring_ref);
496 if (test_bit(KEY_FLAG_KEEP, &keyring->flags))
497 ret = -EPERM;
498 else
499 ret = keyring_clear(keyring);
David Howells700920e2012-01-18 15:31:45 +0000500error_put:
David Howells664cceb2005-09-28 17:03:15 +0100501 key_ref_put(keyring_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700502error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000504}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506/*
David Howells973c9f42011-01-20 16:38:33 +0000507 * Create a link from a keyring to a key if there's no matching key in the
508 * keyring, otherwise replace the link to the matching key with a link to the
509 * new key.
510 *
511 * The key must grant the caller Link permission and the the keyring must grant
512 * the caller Write permission. Furthermore, if an additional link is created,
513 * the keyring's quota will be extended.
514 *
515 * If successful, 0 will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 */
517long keyctl_keyring_link(key_serial_t id, key_serial_t ringid)
518{
David Howells664cceb2005-09-28 17:03:15 +0100519 key_ref_t keyring_ref, key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 long ret;
521
David Howellsf5895942014-03-14 17:44:49 +0000522 keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
David Howells664cceb2005-09-28 17:03:15 +0100523 if (IS_ERR(keyring_ref)) {
524 ret = PTR_ERR(keyring_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 goto error;
526 }
527
David Howellsf5895942014-03-14 17:44:49 +0000528 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE, KEY_NEED_LINK);
David Howells664cceb2005-09-28 17:03:15 +0100529 if (IS_ERR(key_ref)) {
530 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 goto error2;
532 }
533
David Howells664cceb2005-09-28 17:03:15 +0100534 ret = key_link(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
David Howells664cceb2005-09-28 17:03:15 +0100536 key_ref_put(key_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700537error2:
David Howells664cceb2005-09-28 17:03:15 +0100538 key_ref_put(keyring_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700539error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000541}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543/*
David Howells973c9f42011-01-20 16:38:33 +0000544 * Unlink a key from a keyring.
545 *
546 * The keyring must grant the caller Write permission for this to work; the key
547 * itself need not grant the caller anything. If the last link to a key is
548 * removed then that key will be scheduled for destruction.
549 *
Mimi Zohard3600bc2015-11-10 08:34:46 -0500550 * Keys or keyrings with KEY_FLAG_KEEP set should not be unlinked.
551 *
David Howells973c9f42011-01-20 16:38:33 +0000552 * If successful, 0 will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 */
554long keyctl_keyring_unlink(key_serial_t id, key_serial_t ringid)
555{
David Howells664cceb2005-09-28 17:03:15 +0100556 key_ref_t keyring_ref, key_ref;
Mimi Zohard3600bc2015-11-10 08:34:46 -0500557 struct key *keyring, *key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 long ret;
559
David Howellsf5895942014-03-14 17:44:49 +0000560 keyring_ref = lookup_user_key(ringid, 0, KEY_NEED_WRITE);
David Howells664cceb2005-09-28 17:03:15 +0100561 if (IS_ERR(keyring_ref)) {
562 ret = PTR_ERR(keyring_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 goto error;
564 }
565
David Howells55931222009-09-02 09:13:45 +0100566 key_ref = lookup_user_key(id, KEY_LOOKUP_FOR_UNLINK, 0);
David Howells664cceb2005-09-28 17:03:15 +0100567 if (IS_ERR(key_ref)) {
568 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 goto error2;
570 }
571
Mimi Zohard3600bc2015-11-10 08:34:46 -0500572 keyring = key_ref_to_ptr(keyring_ref);
573 key = key_ref_to_ptr(key_ref);
574 if (test_bit(KEY_FLAG_KEEP, &keyring->flags) &&
575 test_bit(KEY_FLAG_KEEP, &key->flags))
576 ret = -EPERM;
577 else
578 ret = key_unlink(keyring, key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
David Howells664cceb2005-09-28 17:03:15 +0100580 key_ref_put(key_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700581error2:
David Howells664cceb2005-09-28 17:03:15 +0100582 key_ref_put(keyring_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700583error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000585}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587/*
David Howellsed0ac5c2019-05-20 21:51:50 +0100588 * Move a link to a key from one keyring to another, displacing any matching
589 * key from the destination keyring.
590 *
591 * The key must grant the caller Link permission and both keyrings must grant
592 * the caller Write permission. There must also be a link in the from keyring
593 * to the key. If both keyrings are the same, nothing is done.
594 *
595 * If successful, 0 will be returned.
596 */
597long keyctl_keyring_move(key_serial_t id, key_serial_t from_ringid,
598 key_serial_t to_ringid, unsigned int flags)
599{
600 key_ref_t key_ref, from_ref, to_ref;
601 long ret;
602
603 if (flags & ~KEYCTL_MOVE_EXCL)
604 return -EINVAL;
605
606 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE, KEY_NEED_LINK);
607 if (IS_ERR(key_ref))
608 return PTR_ERR(key_ref);
609
610 from_ref = lookup_user_key(from_ringid, 0, KEY_NEED_WRITE);
611 if (IS_ERR(from_ref)) {
612 ret = PTR_ERR(from_ref);
613 goto error2;
614 }
615
616 to_ref = lookup_user_key(to_ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
617 if (IS_ERR(to_ref)) {
618 ret = PTR_ERR(to_ref);
619 goto error3;
620 }
621
622 ret = key_move(key_ref_to_ptr(key_ref), key_ref_to_ptr(from_ref),
623 key_ref_to_ptr(to_ref), flags);
624
625 key_ref_put(to_ref);
626error3:
627 key_ref_put(from_ref);
628error2:
629 key_ref_put(key_ref);
630 return ret;
631}
632
633/*
David Howells973c9f42011-01-20 16:38:33 +0000634 * Return a description of a key to userspace.
635 *
636 * The key must grant the caller View permission for this to work.
637 *
638 * If there's a buffer, we place up to buflen bytes of data into it formatted
639 * in the following way:
640 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 * type;uid;gid;perm;description<NUL>
David Howells973c9f42011-01-20 16:38:33 +0000642 *
643 * If successful, we return the amount of description available, irrespective
644 * of how much we may have copied into the buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 */
646long keyctl_describe_key(key_serial_t keyid,
647 char __user *buffer,
648 size_t buflen)
649{
David Howells3e301482005-06-23 22:00:56 -0700650 struct key *key, *instkey;
David Howells664cceb2005-09-28 17:03:15 +0100651 key_ref_t key_ref;
David Howellsaa9d4432014-12-01 22:52:45 +0000652 char *infobuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 long ret;
David Howellsaa9d4432014-12-01 22:52:45 +0000654 int desclen, infolen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
David Howellsf5895942014-03-14 17:44:49 +0000656 key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_NEED_VIEW);
David Howells664cceb2005-09-28 17:03:15 +0100657 if (IS_ERR(key_ref)) {
David Howells3e301482005-06-23 22:00:56 -0700658 /* viewing a key under construction is permitted if we have the
659 * authorisation token handy */
David Howells664cceb2005-09-28 17:03:15 +0100660 if (PTR_ERR(key_ref) == -EACCES) {
David Howells3e301482005-06-23 22:00:56 -0700661 instkey = key_get_instantiation_authkey(keyid);
662 if (!IS_ERR(instkey)) {
663 key_put(instkey);
David Howells8bbf49762008-11-14 10:39:14 +1100664 key_ref = lookup_user_key(keyid,
David Howells55931222009-09-02 09:13:45 +0100665 KEY_LOOKUP_PARTIAL,
666 0);
David Howells664cceb2005-09-28 17:03:15 +0100667 if (!IS_ERR(key_ref))
David Howells3e301482005-06-23 22:00:56 -0700668 goto okay;
669 }
670 }
671
David Howells664cceb2005-09-28 17:03:15 +0100672 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 goto error;
674 }
675
David Howells3e301482005-06-23 22:00:56 -0700676okay:
David Howells664cceb2005-09-28 17:03:15 +0100677 key = key_ref_to_ptr(key_ref);
David Howellsaa9d4432014-12-01 22:52:45 +0000678 desclen = strlen(key->description);
David Howells664cceb2005-09-28 17:03:15 +0100679
David Howellsaa9d4432014-12-01 22:52:45 +0000680 /* calculate how much information we're going to return */
681 ret = -ENOMEM;
682 infobuf = kasprintf(GFP_KERNEL,
683 "%s;%d;%d;%08x;",
684 key->type->name,
685 from_kuid_munged(current_user_ns(), key->uid),
686 from_kgid_munged(current_user_ns(), key->gid),
Linus Torvalds028db3e2019-07-10 18:43:43 -0700687 key->perm);
David Howellsaa9d4432014-12-01 22:52:45 +0000688 if (!infobuf)
689 goto error2;
690 infolen = strlen(infobuf);
691 ret = infolen + desclen + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
693 /* consider returning the data */
David Howellsaa9d4432014-12-01 22:52:45 +0000694 if (buffer && buflen >= ret) {
695 if (copy_to_user(buffer, infobuf, infolen) != 0 ||
696 copy_to_user(buffer + infolen, key->description,
697 desclen + 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 ret = -EFAULT;
699 }
700
David Howellsaa9d4432014-12-01 22:52:45 +0000701 kfree(infobuf);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700702error2:
David Howells664cceb2005-09-28 17:03:15 +0100703 key_ref_put(key_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700704error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000706}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708/*
David Howells973c9f42011-01-20 16:38:33 +0000709 * Search the specified keyring and any keyrings it links to for a matching
710 * key. Only keyrings that grant the caller Search permission will be searched
711 * (this includes the starting keyring). Only keys with Search permission can
712 * be found.
713 *
714 * If successful, the found key will be linked to the destination keyring if
715 * supplied and the key has Link permission, and the found key ID will be
716 * returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 */
718long keyctl_keyring_search(key_serial_t ringid,
719 const char __user *_type,
720 const char __user *_description,
721 key_serial_t destringid)
722{
723 struct key_type *ktype;
David Howells664cceb2005-09-28 17:03:15 +0100724 key_ref_t keyring_ref, key_ref, dest_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 char type[32], *description;
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800726 long ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
728 /* pull the type and description into kernel space */
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800729 ret = key_get_type_from_user(type, _type, sizeof(type));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 if (ret < 0)
731 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
David Howellsaa9d4432014-12-01 22:52:45 +0000733 description = strndup_user(_description, KEY_MAX_DESC_SIZE);
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800734 if (IS_ERR(description)) {
735 ret = PTR_ERR(description);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 goto error;
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800737 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
739 /* get the keyring at which to begin the search */
David Howellsf5895942014-03-14 17:44:49 +0000740 keyring_ref = lookup_user_key(ringid, 0, KEY_NEED_SEARCH);
David Howells664cceb2005-09-28 17:03:15 +0100741 if (IS_ERR(keyring_ref)) {
742 ret = PTR_ERR(keyring_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 goto error2;
744 }
745
746 /* get the destination keyring if specified */
David Howells664cceb2005-09-28 17:03:15 +0100747 dest_ref = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 if (destringid) {
David Howells55931222009-09-02 09:13:45 +0100749 dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE,
David Howellsf5895942014-03-14 17:44:49 +0000750 KEY_NEED_WRITE);
David Howells664cceb2005-09-28 17:03:15 +0100751 if (IS_ERR(dest_ref)) {
752 ret = PTR_ERR(dest_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 goto error3;
754 }
755 }
756
757 /* find the key type */
758 ktype = key_type_lookup(type);
759 if (IS_ERR(ktype)) {
760 ret = PTR_ERR(ktype);
761 goto error4;
762 }
763
764 /* do the search */
David Howellsdcf49db2019-06-26 21:02:32 +0100765 key_ref = keyring_search(keyring_ref, ktype, description, true);
David Howells664cceb2005-09-28 17:03:15 +0100766 if (IS_ERR(key_ref)) {
767 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
769 /* treat lack or presence of a negative key the same */
770 if (ret == -EAGAIN)
771 ret = -ENOKEY;
772 goto error5;
773 }
774
775 /* link the resulting key to the destination keyring if we can */
David Howells664cceb2005-09-28 17:03:15 +0100776 if (dest_ref) {
David Howellsf5895942014-03-14 17:44:49 +0000777 ret = key_permission(key_ref, KEY_NEED_LINK);
David Howells29db9192005-10-30 15:02:44 -0800778 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 goto error6;
780
David Howells664cceb2005-09-28 17:03:15 +0100781 ret = key_link(key_ref_to_ptr(dest_ref), key_ref_to_ptr(key_ref));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 if (ret < 0)
783 goto error6;
784 }
785
David Howells664cceb2005-09-28 17:03:15 +0100786 ret = key_ref_to_ptr(key_ref)->serial;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700788error6:
David Howells664cceb2005-09-28 17:03:15 +0100789 key_ref_put(key_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700790error5:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 key_type_put(ktype);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700792error4:
David Howells664cceb2005-09-28 17:03:15 +0100793 key_ref_put(dest_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700794error3:
David Howells664cceb2005-09-28 17:03:15 +0100795 key_ref_put(keyring_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700796error2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 kfree(description);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700798error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000800}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802/*
Waiman Longd3ec10a2020-03-21 21:11:24 -0400803 * Call the read method
804 */
805static long __keyctl_read_key(struct key *key, char *buffer, size_t buflen)
806{
807 long ret;
808
809 down_read(&key->sem);
810 ret = key_validate(key);
811 if (ret == 0)
812 ret = key->type->read(key, buffer, buflen);
813 up_read(&key->sem);
814 return ret;
815}
816
817/*
David Howells973c9f42011-01-20 16:38:33 +0000818 * Read a key's payload.
819 *
820 * The key must either grant the caller Read permission, or it must grant the
821 * caller Search permission when searched for from the process keyrings.
822 *
823 * If successful, we place up to buflen bytes of data into the buffer, if one
824 * is provided, and return the amount of data that is available in the key,
825 * irrespective of how much we copied into the buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 */
827long keyctl_read_key(key_serial_t keyid, char __user *buffer, size_t buflen)
828{
David Howells664cceb2005-09-28 17:03:15 +0100829 struct key *key;
830 key_ref_t key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 long ret;
Waiman Long4f088242020-03-21 21:11:25 -0400832 char *key_data = NULL;
833 size_t key_data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
835 /* find the key first */
David Howells55931222009-09-02 09:13:45 +0100836 key_ref = lookup_user_key(keyid, 0, 0);
David Howells664cceb2005-09-28 17:03:15 +0100837 if (IS_ERR(key_ref)) {
838 ret = -ENOKEY;
Waiman Longd3ec10a2020-03-21 21:11:24 -0400839 goto out;
David Howells664cceb2005-09-28 17:03:15 +0100840 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
David Howells664cceb2005-09-28 17:03:15 +0100842 key = key_ref_to_ptr(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
David Howells363b02d2017-10-04 16:43:25 +0100844 ret = key_read_state(key);
845 if (ret < 0)
Waiman Longd3ec10a2020-03-21 21:11:24 -0400846 goto key_put_out; /* Negatively instantiated */
Eric Biggers37863c42017-09-18 11:37:23 -0700847
David Howells664cceb2005-09-28 17:03:15 +0100848 /* see if we can read it directly */
David Howellsf5895942014-03-14 17:44:49 +0000849 ret = key_permission(key_ref, KEY_NEED_READ);
David Howells29db9192005-10-30 15:02:44 -0800850 if (ret == 0)
David Howells664cceb2005-09-28 17:03:15 +0100851 goto can_read_key;
David Howells29db9192005-10-30 15:02:44 -0800852 if (ret != -EACCES)
Waiman Longd3ec10a2020-03-21 21:11:24 -0400853 goto key_put_out;
David Howells664cceb2005-09-28 17:03:15 +0100854
855 /* we can't; see if it's searchable from this process's keyrings
856 * - we automatically take account of the fact that it may be
857 * dangling off an instantiation key
858 */
859 if (!is_key_possessed(key_ref)) {
860 ret = -EACCES;
Waiman Longd3ec10a2020-03-21 21:11:24 -0400861 goto key_put_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 }
863
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 /* the key is probably readable - now try to read it */
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700865can_read_key:
Waiman Longd3ec10a2020-03-21 21:11:24 -0400866 if (!key->type->read) {
867 ret = -EOPNOTSUPP;
868 goto key_put_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 }
870
Waiman Longd3ec10a2020-03-21 21:11:24 -0400871 if (!buffer || !buflen) {
872 /* Get the key length from the read method */
873 ret = __keyctl_read_key(key, NULL, 0);
874 goto key_put_out;
875 }
876
877 /*
878 * Read the data with the semaphore held (since we might sleep)
879 * to protect against the key being updated or revoked.
880 *
881 * Allocating a temporary buffer to hold the keys before
882 * transferring them to user buffer to avoid potential
883 * deadlock involving page fault and mmap_sem.
Waiman Long4f088242020-03-21 21:11:25 -0400884 *
885 * key_data_len = (buflen <= PAGE_SIZE)
886 * ? buflen : actual length of key data
887 *
888 * This prevents allocating arbitrary large buffer which can
889 * be much larger than the actual key length. In the latter case,
890 * at least 2 passes of this loop is required.
Waiman Longd3ec10a2020-03-21 21:11:24 -0400891 */
Waiman Long4f088242020-03-21 21:11:25 -0400892 key_data_len = (buflen <= PAGE_SIZE) ? buflen : 0;
893 for (;;) {
894 if (key_data_len) {
895 key_data = kvmalloc(key_data_len, GFP_KERNEL);
896 if (!key_data) {
897 ret = -ENOMEM;
898 goto key_put_out;
899 }
900 }
Waiman Longd3ec10a2020-03-21 21:11:24 -0400901
Waiman Long4f088242020-03-21 21:11:25 -0400902 ret = __keyctl_read_key(key, key_data, key_data_len);
Waiman Longd3ec10a2020-03-21 21:11:24 -0400903
Waiman Long4f088242020-03-21 21:11:25 -0400904 /*
905 * Read methods will just return the required length without
906 * any copying if the provided length isn't large enough.
907 */
908 if (ret <= 0 || ret > buflen)
909 break;
910
911 /*
912 * The key may change (unlikely) in between 2 consecutive
913 * __keyctl_read_key() calls. In this case, we reallocate
914 * a larger buffer and redo the key read when
915 * key_data_len < ret <= buflen.
916 */
917 if (ret > key_data_len) {
918 if (unlikely(key_data))
919 __kvzfree(key_data, key_data_len);
920 key_data_len = ret;
921 continue; /* Allocate buffer */
922 }
923
Waiman Longd3ec10a2020-03-21 21:11:24 -0400924 if (copy_to_user(buffer, key_data, ret))
925 ret = -EFAULT;
Waiman Long4f088242020-03-21 21:11:25 -0400926 break;
Waiman Longd3ec10a2020-03-21 21:11:24 -0400927 }
Waiman Long4f088242020-03-21 21:11:25 -0400928 __kvzfree(key_data, key_data_len);
Waiman Longd3ec10a2020-03-21 21:11:24 -0400929
930key_put_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 key_put(key);
Waiman Longd3ec10a2020-03-21 21:11:24 -0400932out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000934}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936/*
David Howells973c9f42011-01-20 16:38:33 +0000937 * Change the ownership of a key
938 *
939 * The key must grant the caller Setattr permission for this to work, though
940 * the key need not be fully instantiated yet. For the UID to be changed, or
941 * for the GID to be changed to a group the caller is not a member of, the
942 * caller must have sysadmin capability. If either uid or gid is -1 then that
943 * attribute is not changed.
944 *
945 * If the UID is to be changed, the new user must have sufficient quota to
946 * accept the key. The quota deduction will be removed from the old user to
947 * the new user should the attribute be changed.
948 *
949 * If successful, 0 will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 */
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -0800951long keyctl_chown_key(key_serial_t id, uid_t user, gid_t group)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952{
Fredrik Tolf58016492006-06-26 00:24:51 -0700953 struct key_user *newowner, *zapowner = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 struct key *key;
David Howells664cceb2005-09-28 17:03:15 +0100955 key_ref_t key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 long ret;
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -0800957 kuid_t uid;
958 kgid_t gid;
959
960 uid = make_kuid(current_user_ns(), user);
961 gid = make_kgid(current_user_ns(), group);
962 ret = -EINVAL;
963 if ((user != (uid_t) -1) && !uid_valid(uid))
964 goto error;
965 if ((group != (gid_t) -1) && !gid_valid(gid))
966 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
968 ret = 0;
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -0800969 if (user == (uid_t) -1 && group == (gid_t) -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 goto error;
971
David Howells55931222009-09-02 09:13:45 +0100972 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
Linus Torvalds028db3e2019-07-10 18:43:43 -0700973 KEY_NEED_SETATTR);
David Howells664cceb2005-09-28 17:03:15 +0100974 if (IS_ERR(key_ref)) {
975 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 goto error;
977 }
978
David Howells664cceb2005-09-28 17:03:15 +0100979 key = key_ref_to_ptr(key_ref);
980
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 /* make the changes with the locks held to prevent chown/chown races */
982 ret = -EACCES;
983 down_write(&key->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
985 if (!capable(CAP_SYS_ADMIN)) {
986 /* only the sysadmin can chown a key to some other UID */
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -0800987 if (user != (uid_t) -1 && !uid_eq(key->uid, uid))
Fredrik Tolf58016492006-06-26 00:24:51 -0700988 goto error_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
990 /* only the sysadmin can set the key's GID to a group other
991 * than one of those that the current process subscribes to */
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -0800992 if (group != (gid_t) -1 && !gid_eq(gid, key->gid) && !in_group_p(gid))
Fredrik Tolf58016492006-06-26 00:24:51 -0700993 goto error_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 }
995
Fredrik Tolf58016492006-06-26 00:24:51 -0700996 /* change the UID */
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -0800997 if (user != (uid_t) -1 && !uid_eq(uid, key->uid)) {
Fredrik Tolf58016492006-06-26 00:24:51 -0700998 ret = -ENOMEM;
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -0800999 newowner = key_user_lookup(uid);
Fredrik Tolf58016492006-06-26 00:24:51 -07001000 if (!newowner)
1001 goto error_put;
1002
1003 /* transfer the quota burden to the new user */
1004 if (test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) {
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -08001005 unsigned maxkeys = uid_eq(uid, GLOBAL_ROOT_UID) ?
David Howells0b77f5b2008-04-29 01:01:32 -07001006 key_quota_root_maxkeys : key_quota_maxkeys;
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -08001007 unsigned maxbytes = uid_eq(uid, GLOBAL_ROOT_UID) ?
David Howells0b77f5b2008-04-29 01:01:32 -07001008 key_quota_root_maxbytes : key_quota_maxbytes;
1009
Fredrik Tolf58016492006-06-26 00:24:51 -07001010 spin_lock(&newowner->lock);
Yang Xu2e356102020-02-28 12:41:51 +08001011 if (newowner->qnkeys + 1 > maxkeys ||
1012 newowner->qnbytes + key->quotalen > maxbytes ||
David Howells0b77f5b2008-04-29 01:01:32 -07001013 newowner->qnbytes + key->quotalen <
1014 newowner->qnbytes)
Fredrik Tolf58016492006-06-26 00:24:51 -07001015 goto quota_overrun;
1016
1017 newowner->qnkeys++;
1018 newowner->qnbytes += key->quotalen;
1019 spin_unlock(&newowner->lock);
1020
1021 spin_lock(&key->user->lock);
1022 key->user->qnkeys--;
1023 key->user->qnbytes -= key->quotalen;
1024 spin_unlock(&key->user->lock);
1025 }
1026
1027 atomic_dec(&key->user->nkeys);
1028 atomic_inc(&newowner->nkeys);
1029
David Howells363b02d2017-10-04 16:43:25 +01001030 if (key->state != KEY_IS_UNINSTANTIATED) {
Fredrik Tolf58016492006-06-26 00:24:51 -07001031 atomic_dec(&key->user->nikeys);
1032 atomic_inc(&newowner->nikeys);
1033 }
1034
1035 zapowner = key->user;
1036 key->user = newowner;
1037 key->uid = uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 }
1039
1040 /* change the GID */
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -08001041 if (group != (gid_t) -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 key->gid = gid;
1043
David Howellsf7e47672020-01-14 17:07:11 +00001044 notify_key(key, NOTIFY_KEY_SETATTR, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 ret = 0;
1046
Fredrik Tolf58016492006-06-26 00:24:51 -07001047error_put:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 up_write(&key->sem);
1049 key_put(key);
Fredrik Tolf58016492006-06-26 00:24:51 -07001050 if (zapowner)
1051 key_user_put(zapowner);
1052error:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 return ret;
1054
Fredrik Tolf58016492006-06-26 00:24:51 -07001055quota_overrun:
1056 spin_unlock(&newowner->lock);
1057 zapowner = newowner;
1058 ret = -EDQUOT;
1059 goto error_put;
David Howellsa8b17ed2011-01-20 16:38:27 +00001060}
Fredrik Tolf58016492006-06-26 00:24:51 -07001061
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062/*
David Howells973c9f42011-01-20 16:38:33 +00001063 * Change the permission mask on a key.
1064 *
1065 * The key must grant the caller Setattr permission for this to work, though
1066 * the key need not be fully instantiated yet. If the caller does not have
1067 * sysadmin capability, it may only change the permission on keys that it owns.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 */
Linus Torvalds028db3e2019-07-10 18:43:43 -07001069long keyctl_setperm_key(key_serial_t id, key_perm_t perm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070{
1071 struct key *key;
David Howells664cceb2005-09-28 17:03:15 +01001072 key_ref_t key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 long ret;
1074
Linus Torvalds028db3e2019-07-10 18:43:43 -07001075 ret = -EINVAL;
David Howells664cceb2005-09-28 17:03:15 +01001076 if (perm & ~(KEY_POS_ALL | KEY_USR_ALL | KEY_GRP_ALL | KEY_OTH_ALL))
Linus Torvalds028db3e2019-07-10 18:43:43 -07001077 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
David Howells55931222009-09-02 09:13:45 +01001079 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
Linus Torvalds028db3e2019-07-10 18:43:43 -07001080 KEY_NEED_SETATTR);
David Howells664cceb2005-09-28 17:03:15 +01001081 if (IS_ERR(key_ref)) {
1082 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 goto error;
1084 }
1085
David Howells664cceb2005-09-28 17:03:15 +01001086 key = key_ref_to_ptr(key_ref);
1087
Linus Torvalds028db3e2019-07-10 18:43:43 -07001088 /* make the changes with the locks held to prevent chown/chmod races */
1089 ret = -EACCES;
1090 down_write(&key->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
Linus Torvalds028db3e2019-07-10 18:43:43 -07001092 /* if we're not the sysadmin, we can only change a key that we own */
1093 if (capable(CAP_SYS_ADMIN) || uid_eq(key->uid, current_fsuid())) {
1094 key->perm = perm;
David Howellsf7e47672020-01-14 17:07:11 +00001095 notify_key(key, NOTIFY_KEY_SETATTR, 0);
Linus Torvalds028db3e2019-07-10 18:43:43 -07001096 ret = 0;
David Howells76d8aea2005-06-23 22:00:49 -07001097 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 up_write(&key->sem);
1100 key_put(key);
David Howells76d8aea2005-06-23 22:00:49 -07001101error:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +00001103}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
David Howells8bbf49762008-11-14 10:39:14 +11001105/*
David Howells973c9f42011-01-20 16:38:33 +00001106 * Get the destination keyring for instantiation and check that the caller has
1107 * Write permission on it.
David Howells8bbf49762008-11-14 10:39:14 +11001108 */
1109static long get_instantiation_keyring(key_serial_t ringid,
1110 struct request_key_auth *rka,
1111 struct key **_dest_keyring)
1112{
1113 key_ref_t dkref;
1114
David Howellseca1bf52008-12-29 00:41:51 +00001115 *_dest_keyring = NULL;
1116
David Howells8bbf49762008-11-14 10:39:14 +11001117 /* just return a NULL pointer if we weren't asked to make a link */
David Howellseca1bf52008-12-29 00:41:51 +00001118 if (ringid == 0)
David Howells8bbf49762008-11-14 10:39:14 +11001119 return 0;
David Howells8bbf49762008-11-14 10:39:14 +11001120
1121 /* if a specific keyring is nominated by ID, then use that */
1122 if (ringid > 0) {
David Howellsf5895942014-03-14 17:44:49 +00001123 dkref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
David Howells8bbf49762008-11-14 10:39:14 +11001124 if (IS_ERR(dkref))
1125 return PTR_ERR(dkref);
1126 *_dest_keyring = key_ref_to_ptr(dkref);
1127 return 0;
1128 }
1129
1130 if (ringid == KEY_SPEC_REQKEY_AUTH_KEY)
1131 return -EINVAL;
1132
1133 /* otherwise specify the destination keyring recorded in the
1134 * authorisation key (any KEY_SPEC_*_KEYRING) */
1135 if (ringid >= KEY_SPEC_REQUESTOR_KEYRING) {
David Howells21279cf2009-10-15 10:14:35 +01001136 *_dest_keyring = key_get(rka->dest_keyring);
David Howells8bbf49762008-11-14 10:39:14 +11001137 return 0;
1138 }
1139
1140 return -ENOKEY;
1141}
1142
David Howellsd84f4f92008-11-14 10:39:23 +11001143/*
David Howells973c9f42011-01-20 16:38:33 +00001144 * Change the request_key authorisation key on the current process.
David Howellsd84f4f92008-11-14 10:39:23 +11001145 */
1146static int keyctl_change_reqkey_auth(struct key *key)
1147{
1148 struct cred *new;
1149
1150 new = prepare_creds();
1151 if (!new)
1152 return -ENOMEM;
1153
1154 key_put(new->request_key_auth);
1155 new->request_key_auth = key_get(key);
1156
1157 return commit_creds(new);
1158}
1159
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160/*
David Howells973c9f42011-01-20 16:38:33 +00001161 * Instantiate a key with the specified payload and link the key into the
1162 * destination keyring if one is given.
1163 *
1164 * The caller must have the appropriate instantiation permit set for this to
1165 * work (see keyctl_assume_authority). No other permissions are required.
1166 *
1167 * If successful, 0 will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 */
David Howellsee009e4a02011-03-07 15:06:20 +00001169long keyctl_instantiate_key_common(key_serial_t id,
Al Virob353a1f2015-03-17 09:59:38 -04001170 struct iov_iter *from,
David Howellsee009e4a02011-03-07 15:06:20 +00001171 key_serial_t ringid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172{
David Howellsd84f4f92008-11-14 10:39:23 +11001173 const struct cred *cred = current_cred();
David Howells3e301482005-06-23 22:00:56 -07001174 struct request_key_auth *rka;
David Howells8bbf49762008-11-14 10:39:14 +11001175 struct key *instkey, *dest_keyring;
Al Virob353a1f2015-03-17 09:59:38 -04001176 size_t plen = from ? iov_iter_count(from) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 void *payload;
1178 long ret;
1179
David Howellsd84f4f92008-11-14 10:39:23 +11001180 kenter("%d,,%zu,%d", id, plen, ringid);
1181
Al Virob353a1f2015-03-17 09:59:38 -04001182 if (!plen)
1183 from = NULL;
1184
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 ret = -EINVAL;
David Howells38bbca62008-04-29 01:01:19 -07001186 if (plen > 1024 * 1024 - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 goto error;
1188
David Howellsb5f545c2006-01-08 01:02:47 -08001189 /* the appropriate instantiation authorisation key must have been
1190 * assumed before calling this */
1191 ret = -EPERM;
David Howellsd84f4f92008-11-14 10:39:23 +11001192 instkey = cred->request_key_auth;
David Howellsb5f545c2006-01-08 01:02:47 -08001193 if (!instkey)
1194 goto error;
1195
David Howells146aa8b2015-10-21 14:04:48 +01001196 rka = instkey->payload.data[0];
David Howellsb5f545c2006-01-08 01:02:47 -08001197 if (rka->target_key->serial != id)
1198 goto error;
1199
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 /* pull the payload in if one was supplied */
1201 payload = NULL;
1202
Al Virob353a1f2015-03-17 09:59:38 -04001203 if (from) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 ret = -ENOMEM;
Michal Hocko752ade62017-05-08 15:57:27 -07001205 payload = kvmalloc(plen, GFP_KERNEL);
1206 if (!payload)
1207 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
Al Virob353a1f2015-03-17 09:59:38 -04001209 ret = -EFAULT;
Al Virocbbd26b2016-11-01 22:09:04 -04001210 if (!copy_from_iter_full(payload, plen, from))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 goto error2;
1212 }
1213
David Howells3e301482005-06-23 22:00:56 -07001214 /* find the destination keyring amongst those belonging to the
1215 * requesting task */
David Howells8bbf49762008-11-14 10:39:14 +11001216 ret = get_instantiation_keyring(ringid, rka, &dest_keyring);
1217 if (ret < 0)
1218 goto error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
1220 /* instantiate the key and link it into a keyring */
David Howells3e301482005-06-23 22:00:56 -07001221 ret = key_instantiate_and_link(rka->target_key, payload, plen,
David Howells8bbf49762008-11-14 10:39:14 +11001222 dest_keyring, instkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
David Howells8bbf49762008-11-14 10:39:14 +11001224 key_put(dest_keyring);
David Howellsb5f545c2006-01-08 01:02:47 -08001225
1226 /* discard the assumed authority if it's just been disabled by
1227 * instantiation of the key */
David Howellsd84f4f92008-11-14 10:39:23 +11001228 if (ret == 0)
1229 keyctl_change_reqkey_auth(NULL);
David Howellsb5f545c2006-01-08 01:02:47 -08001230
1231error2:
Eric Biggers57070c82017-06-08 14:48:57 +01001232 if (payload) {
1233 memzero_explicit(payload, plen);
1234 kvfree(payload);
1235 }
David Howellsb5f545c2006-01-08 01:02:47 -08001236error:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +00001238}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240/*
David Howellsee009e4a02011-03-07 15:06:20 +00001241 * Instantiate a key with the specified payload and link the key into the
1242 * destination keyring if one is given.
1243 *
1244 * The caller must have the appropriate instantiation permit set for this to
1245 * work (see keyctl_assume_authority). No other permissions are required.
1246 *
1247 * If successful, 0 will be returned.
1248 */
1249long keyctl_instantiate_key(key_serial_t id,
1250 const void __user *_payload,
1251 size_t plen,
1252 key_serial_t ringid)
1253{
1254 if (_payload && plen) {
Al Virob353a1f2015-03-17 09:59:38 -04001255 struct iovec iov;
1256 struct iov_iter from;
1257 int ret;
David Howellsee009e4a02011-03-07 15:06:20 +00001258
Al Virob353a1f2015-03-17 09:59:38 -04001259 ret = import_single_range(WRITE, (void __user *)_payload, plen,
1260 &iov, &from);
1261 if (unlikely(ret))
1262 return ret;
1263
1264 return keyctl_instantiate_key_common(id, &from, ringid);
David Howellsee009e4a02011-03-07 15:06:20 +00001265 }
1266
Al Virob353a1f2015-03-17 09:59:38 -04001267 return keyctl_instantiate_key_common(id, NULL, ringid);
David Howellsee009e4a02011-03-07 15:06:20 +00001268}
1269
1270/*
1271 * Instantiate a key with the specified multipart payload and link the key into
1272 * the destination keyring if one is given.
1273 *
1274 * The caller must have the appropriate instantiation permit set for this to
1275 * work (see keyctl_assume_authority). No other permissions are required.
1276 *
1277 * If successful, 0 will be returned.
1278 */
1279long keyctl_instantiate_key_iov(key_serial_t id,
1280 const struct iovec __user *_payload_iov,
1281 unsigned ioc,
1282 key_serial_t ringid)
1283{
1284 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
Al Virob353a1f2015-03-17 09:59:38 -04001285 struct iov_iter from;
David Howellsee009e4a02011-03-07 15:06:20 +00001286 long ret;
1287
Al Virob353a1f2015-03-17 09:59:38 -04001288 if (!_payload_iov)
1289 ioc = 0;
David Howellsee009e4a02011-03-07 15:06:20 +00001290
Al Virob353a1f2015-03-17 09:59:38 -04001291 ret = import_iovec(WRITE, _payload_iov, ioc,
1292 ARRAY_SIZE(iovstack), &iov, &from);
David Howellsee009e4a02011-03-07 15:06:20 +00001293 if (ret < 0)
Al Virob353a1f2015-03-17 09:59:38 -04001294 return ret;
1295 ret = keyctl_instantiate_key_common(id, &from, ringid);
1296 kfree(iov);
David Howellsee009e4a02011-03-07 15:06:20 +00001297 return ret;
David Howellsee009e4a02011-03-07 15:06:20 +00001298}
1299
1300/*
David Howells973c9f42011-01-20 16:38:33 +00001301 * Negatively instantiate the key with the given timeout (in seconds) and link
1302 * the key into the destination keyring if one is given.
1303 *
1304 * The caller must have the appropriate instantiation permit set for this to
1305 * work (see keyctl_assume_authority). No other permissions are required.
1306 *
1307 * The key and any links to the key will be automatically garbage collected
1308 * after the timeout expires.
1309 *
1310 * Negative keys are used to rate limit repeated request_key() calls by causing
1311 * them to return -ENOKEY until the negative key expires.
1312 *
1313 * If successful, 0 will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 */
1315long keyctl_negate_key(key_serial_t id, unsigned timeout, key_serial_t ringid)
1316{
David Howellsfdd1b942011-03-07 15:06:09 +00001317 return keyctl_reject_key(id, timeout, ENOKEY, ringid);
1318}
1319
1320/*
1321 * Negatively instantiate the key with the given timeout (in seconds) and error
1322 * code and link the key into the destination keyring if one is given.
1323 *
1324 * The caller must have the appropriate instantiation permit set for this to
1325 * work (see keyctl_assume_authority). No other permissions are required.
1326 *
1327 * The key and any links to the key will be automatically garbage collected
1328 * after the timeout expires.
1329 *
1330 * Negative keys are used to rate limit repeated request_key() calls by causing
1331 * them to return the specified error code until the negative key expires.
1332 *
1333 * If successful, 0 will be returned.
1334 */
1335long keyctl_reject_key(key_serial_t id, unsigned timeout, unsigned error,
1336 key_serial_t ringid)
1337{
David Howellsd84f4f92008-11-14 10:39:23 +11001338 const struct cred *cred = current_cred();
David Howells3e301482005-06-23 22:00:56 -07001339 struct request_key_auth *rka;
David Howells8bbf49762008-11-14 10:39:14 +11001340 struct key *instkey, *dest_keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 long ret;
1342
David Howellsfdd1b942011-03-07 15:06:09 +00001343 kenter("%d,%u,%u,%d", id, timeout, error, ringid);
1344
1345 /* must be a valid error code and mustn't be a kernel special */
1346 if (error <= 0 ||
1347 error >= MAX_ERRNO ||
1348 error == ERESTARTSYS ||
1349 error == ERESTARTNOINTR ||
1350 error == ERESTARTNOHAND ||
1351 error == ERESTART_RESTARTBLOCK)
1352 return -EINVAL;
David Howellsd84f4f92008-11-14 10:39:23 +11001353
David Howellsb5f545c2006-01-08 01:02:47 -08001354 /* the appropriate instantiation authorisation key must have been
1355 * assumed before calling this */
1356 ret = -EPERM;
David Howellsd84f4f92008-11-14 10:39:23 +11001357 instkey = cred->request_key_auth;
David Howellsb5f545c2006-01-08 01:02:47 -08001358 if (!instkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
David Howells146aa8b2015-10-21 14:04:48 +01001361 rka = instkey->payload.data[0];
David Howellsb5f545c2006-01-08 01:02:47 -08001362 if (rka->target_key->serial != id)
1363 goto error;
David Howells3e301482005-06-23 22:00:56 -07001364
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 /* find the destination keyring if present (which must also be
1366 * writable) */
David Howells8bbf49762008-11-14 10:39:14 +11001367 ret = get_instantiation_keyring(ringid, rka, &dest_keyring);
1368 if (ret < 0)
1369 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
1371 /* instantiate the key and link it into a keyring */
David Howellsfdd1b942011-03-07 15:06:09 +00001372 ret = key_reject_and_link(rka->target_key, timeout, error,
David Howells8bbf49762008-11-14 10:39:14 +11001373 dest_keyring, instkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
David Howells8bbf49762008-11-14 10:39:14 +11001375 key_put(dest_keyring);
David Howellsb5f545c2006-01-08 01:02:47 -08001376
1377 /* discard the assumed authority if it's just been disabled by
1378 * instantiation of the key */
David Howellsd84f4f92008-11-14 10:39:23 +11001379 if (ret == 0)
1380 keyctl_change_reqkey_auth(NULL);
David Howellsb5f545c2006-01-08 01:02:47 -08001381
1382error:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +00001384}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386/*
David Howells973c9f42011-01-20 16:38:33 +00001387 * Read or set the default keyring in which request_key() will cache keys and
1388 * return the old setting.
1389 *
Eric Biggersc9f838d2017-04-18 15:31:09 +01001390 * If a thread or process keyring is specified then it will be created if it
1391 * doesn't yet exist. The old setting will be returned if successful.
David Howells3e301482005-06-23 22:00:56 -07001392 */
1393long keyctl_set_reqkey_keyring(int reqkey_defl)
1394{
David Howellsd84f4f92008-11-14 10:39:23 +11001395 struct cred *new;
1396 int ret, old_setting;
1397
1398 old_setting = current_cred_xxx(jit_keyring);
1399
1400 if (reqkey_defl == KEY_REQKEY_DEFL_NO_CHANGE)
1401 return old_setting;
1402
1403 new = prepare_creds();
1404 if (!new)
1405 return -ENOMEM;
David Howells3e301482005-06-23 22:00:56 -07001406
1407 switch (reqkey_defl) {
1408 case KEY_REQKEY_DEFL_THREAD_KEYRING:
David Howellsd84f4f92008-11-14 10:39:23 +11001409 ret = install_thread_keyring_to_cred(new);
David Howells3e301482005-06-23 22:00:56 -07001410 if (ret < 0)
David Howellsd84f4f92008-11-14 10:39:23 +11001411 goto error;
David Howells3e301482005-06-23 22:00:56 -07001412 goto set;
1413
1414 case KEY_REQKEY_DEFL_PROCESS_KEYRING:
David Howellsd84f4f92008-11-14 10:39:23 +11001415 ret = install_process_keyring_to_cred(new);
Eric Biggersc9f838d2017-04-18 15:31:09 +01001416 if (ret < 0)
1417 goto error;
David Howellsd84f4f92008-11-14 10:39:23 +11001418 goto set;
David Howells3e301482005-06-23 22:00:56 -07001419
1420 case KEY_REQKEY_DEFL_DEFAULT:
1421 case KEY_REQKEY_DEFL_SESSION_KEYRING:
1422 case KEY_REQKEY_DEFL_USER_KEYRING:
1423 case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
David Howellsd84f4f92008-11-14 10:39:23 +11001424 case KEY_REQKEY_DEFL_REQUESTOR_KEYRING:
1425 goto set;
David Howells3e301482005-06-23 22:00:56 -07001426
1427 case KEY_REQKEY_DEFL_NO_CHANGE:
David Howells3e301482005-06-23 22:00:56 -07001428 case KEY_REQKEY_DEFL_GROUP_KEYRING:
1429 default:
David Howellsd84f4f92008-11-14 10:39:23 +11001430 ret = -EINVAL;
1431 goto error;
David Howells3e301482005-06-23 22:00:56 -07001432 }
1433
David Howellsd84f4f92008-11-14 10:39:23 +11001434set:
1435 new->jit_keyring = reqkey_defl;
1436 commit_creds(new);
1437 return old_setting;
1438error:
1439 abort_creds(new);
Dan Carpenter4303ef12010-06-11 17:30:05 +01001440 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +00001441}
David Howellsd84f4f92008-11-14 10:39:23 +11001442
David Howells3e301482005-06-23 22:00:56 -07001443/*
David Howells973c9f42011-01-20 16:38:33 +00001444 * Set or clear the timeout on a key.
1445 *
1446 * Either the key must grant the caller Setattr permission or else the caller
1447 * must hold an instantiation authorisation token for the key.
1448 *
1449 * The timeout is either 0 to clear the timeout, or a number of seconds from
1450 * the current time. The key and any links to the key will be automatically
1451 * garbage collected after the timeout expires.
1452 *
Mimi Zohard3600bc2015-11-10 08:34:46 -05001453 * Keys with KEY_FLAG_KEEP set should not be timed out.
1454 *
David Howells973c9f42011-01-20 16:38:33 +00001455 * If successful, 0 is returned.
David Howells017679c2006-01-08 01:02:43 -08001456 */
1457long keyctl_set_timeout(key_serial_t id, unsigned timeout)
1458{
David Howells91562352010-06-11 17:31:05 +01001459 struct key *key, *instkey;
David Howells017679c2006-01-08 01:02:43 -08001460 key_ref_t key_ref;
David Howells017679c2006-01-08 01:02:43 -08001461 long ret;
1462
David Howells55931222009-09-02 09:13:45 +01001463 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
Linus Torvalds028db3e2019-07-10 18:43:43 -07001464 KEY_NEED_SETATTR);
David Howells017679c2006-01-08 01:02:43 -08001465 if (IS_ERR(key_ref)) {
David Howells91562352010-06-11 17:31:05 +01001466 /* setting the timeout on a key under construction is permitted
1467 * if we have the authorisation token handy */
1468 if (PTR_ERR(key_ref) == -EACCES) {
1469 instkey = key_get_instantiation_authkey(id);
1470 if (!IS_ERR(instkey)) {
1471 key_put(instkey);
1472 key_ref = lookup_user_key(id,
1473 KEY_LOOKUP_PARTIAL,
1474 0);
1475 if (!IS_ERR(key_ref))
1476 goto okay;
1477 }
1478 }
1479
David Howells017679c2006-01-08 01:02:43 -08001480 ret = PTR_ERR(key_ref);
1481 goto error;
1482 }
1483
David Howells91562352010-06-11 17:31:05 +01001484okay:
David Howells017679c2006-01-08 01:02:43 -08001485 key = key_ref_to_ptr(key_ref);
Mimi Zohar1d6d1672016-01-07 07:46:36 -05001486 ret = 0;
David Howellsf7e47672020-01-14 17:07:11 +00001487 if (test_bit(KEY_FLAG_KEEP, &key->flags)) {
Mimi Zohard3600bc2015-11-10 08:34:46 -05001488 ret = -EPERM;
David Howellsf7e47672020-01-14 17:07:11 +00001489 } else {
Mimi Zohard3600bc2015-11-10 08:34:46 -05001490 key_set_timeout(key, timeout);
David Howellsf7e47672020-01-14 17:07:11 +00001491 notify_key(key, NOTIFY_KEY_SETATTR, 0);
1492 }
David Howells017679c2006-01-08 01:02:43 -08001493 key_put(key);
1494
David Howells017679c2006-01-08 01:02:43 -08001495error:
1496 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +00001497}
David Howells017679c2006-01-08 01:02:43 -08001498
David Howells017679c2006-01-08 01:02:43 -08001499/*
David Howells973c9f42011-01-20 16:38:33 +00001500 * Assume (or clear) the authority to instantiate the specified key.
1501 *
1502 * This sets the authoritative token currently in force for key instantiation.
1503 * This must be done for a key to be instantiated. It has the effect of making
1504 * available all the keys from the caller of the request_key() that created a
1505 * key to request_key() calls made by the caller of this function.
1506 *
1507 * The caller must have the instantiation key in their process keyrings with a
1508 * Search permission grant available to the caller.
1509 *
1510 * If the ID given is 0, then the setting will be cleared and 0 returned.
1511 *
1512 * If the ID given has a matching an authorisation key, then that key will be
1513 * set and its ID will be returned. The authorisation key can be read to get
1514 * the callout information passed to request_key().
David Howellsb5f545c2006-01-08 01:02:47 -08001515 */
1516long keyctl_assume_authority(key_serial_t id)
1517{
1518 struct key *authkey;
1519 long ret;
1520
1521 /* special key IDs aren't permitted */
1522 ret = -EINVAL;
1523 if (id < 0)
1524 goto error;
1525
1526 /* we divest ourselves of authority if given an ID of 0 */
1527 if (id == 0) {
David Howellsd84f4f92008-11-14 10:39:23 +11001528 ret = keyctl_change_reqkey_auth(NULL);
David Howellsb5f545c2006-01-08 01:02:47 -08001529 goto error;
1530 }
1531
1532 /* attempt to assume the authority temporarily granted to us whilst we
1533 * instantiate the specified key
1534 * - the authorisation key must be in the current task's keyrings
1535 * somewhere
1536 */
1537 authkey = key_get_instantiation_authkey(id);
1538 if (IS_ERR(authkey)) {
1539 ret = PTR_ERR(authkey);
1540 goto error;
1541 }
1542
David Howellsd84f4f92008-11-14 10:39:23 +11001543 ret = keyctl_change_reqkey_auth(authkey);
Eric Biggers884bee02017-09-18 11:36:12 -07001544 if (ret == 0)
1545 ret = authkey->serial;
David Howellsd84f4f92008-11-14 10:39:23 +11001546 key_put(authkey);
David Howellsb5f545c2006-01-08 01:02:47 -08001547error:
1548 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +00001549}
David Howellsb5f545c2006-01-08 01:02:47 -08001550
David Howells70a5bb72008-04-29 01:01:26 -07001551/*
David Howells973c9f42011-01-20 16:38:33 +00001552 * Get a key's the LSM security label.
1553 *
1554 * The key must grant the caller View permission for this to work.
1555 *
1556 * If there's a buffer, then up to buflen bytes of data will be placed into it.
1557 *
1558 * If successful, the amount of information available will be returned,
1559 * irrespective of how much was copied (including the terminal NUL).
David Howells70a5bb72008-04-29 01:01:26 -07001560 */
1561long keyctl_get_security(key_serial_t keyid,
1562 char __user *buffer,
1563 size_t buflen)
1564{
1565 struct key *key, *instkey;
1566 key_ref_t key_ref;
1567 char *context;
1568 long ret;
1569
David Howellsf5895942014-03-14 17:44:49 +00001570 key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_NEED_VIEW);
David Howells70a5bb72008-04-29 01:01:26 -07001571 if (IS_ERR(key_ref)) {
1572 if (PTR_ERR(key_ref) != -EACCES)
1573 return PTR_ERR(key_ref);
1574
1575 /* viewing a key under construction is also permitted if we
1576 * have the authorisation token handy */
1577 instkey = key_get_instantiation_authkey(keyid);
1578 if (IS_ERR(instkey))
Roel Kluinfa1cc7b2009-12-15 15:05:12 -08001579 return PTR_ERR(instkey);
David Howells70a5bb72008-04-29 01:01:26 -07001580 key_put(instkey);
1581
David Howells55931222009-09-02 09:13:45 +01001582 key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, 0);
David Howells70a5bb72008-04-29 01:01:26 -07001583 if (IS_ERR(key_ref))
1584 return PTR_ERR(key_ref);
1585 }
1586
1587 key = key_ref_to_ptr(key_ref);
1588 ret = security_key_getsecurity(key, &context);
1589 if (ret == 0) {
1590 /* if no information was returned, give userspace an empty
1591 * string */
1592 ret = 1;
1593 if (buffer && buflen > 0 &&
1594 copy_to_user(buffer, "", 1) != 0)
1595 ret = -EFAULT;
1596 } else if (ret > 0) {
1597 /* return as much data as there's room for */
1598 if (buffer && buflen > 0) {
1599 if (buflen > ret)
1600 buflen = ret;
1601
1602 if (copy_to_user(buffer, context, buflen) != 0)
1603 ret = -EFAULT;
1604 }
1605
1606 kfree(context);
1607 }
1608
1609 key_ref_put(key_ref);
1610 return ret;
1611}
1612
David Howellsee18d642009-09-02 09:14:21 +01001613/*
David Howells973c9f42011-01-20 16:38:33 +00001614 * Attempt to install the calling process's session keyring on the process's
1615 * parent process.
1616 *
Linus Torvalds028db3e2019-07-10 18:43:43 -07001617 * The keyring must exist and must grant the caller LINK permission, and the
David Howells973c9f42011-01-20 16:38:33 +00001618 * parent process must be single-threaded and must have the same effective
1619 * ownership as this process and mustn't be SUID/SGID.
1620 *
1621 * The keyring will be emplaced on the parent when it next resumes userspace.
1622 *
1623 * If successful, 0 will be returned.
David Howellsee18d642009-09-02 09:14:21 +01001624 */
1625long keyctl_session_to_parent(void)
1626{
1627 struct task_struct *me, *parent;
1628 const struct cred *mycred, *pcred;
Al Viro67d12142012-06-27 11:07:19 +04001629 struct callback_head *newwork, *oldwork;
David Howellsee18d642009-09-02 09:14:21 +01001630 key_ref_t keyring_r;
Oleg Nesterov413cd3d2012-05-11 10:59:08 +10001631 struct cred *cred;
David Howellsee18d642009-09-02 09:14:21 +01001632 int ret;
1633
Linus Torvalds028db3e2019-07-10 18:43:43 -07001634 keyring_r = lookup_user_key(KEY_SPEC_SESSION_KEYRING, 0, KEY_NEED_LINK);
David Howellsee18d642009-09-02 09:14:21 +01001635 if (IS_ERR(keyring_r))
1636 return PTR_ERR(keyring_r);
1637
Oleg Nesterov413cd3d2012-05-11 10:59:08 +10001638 ret = -ENOMEM;
Oleg Nesterov413cd3d2012-05-11 10:59:08 +10001639
David Howellsee18d642009-09-02 09:14:21 +01001640 /* our parent is going to need a new cred struct, a new tgcred struct
1641 * and new security data, so we allocate them here to prevent ENOMEM in
1642 * our parent */
David Howellsee18d642009-09-02 09:14:21 +01001643 cred = cred_alloc_blank();
1644 if (!cred)
Al Viro67d12142012-06-27 11:07:19 +04001645 goto error_keyring;
1646 newwork = &cred->rcu;
David Howellsee18d642009-09-02 09:14:21 +01001647
David Howells3a505972012-10-02 19:24:29 +01001648 cred->session_keyring = key_ref_to_ptr(keyring_r);
1649 keyring_r = NULL;
Al Viro67d12142012-06-27 11:07:19 +04001650 init_task_work(newwork, key_change_session_keyring);
David Howellsee18d642009-09-02 09:14:21 +01001651
1652 me = current;
David Howells9d1ac652010-09-10 09:59:46 +01001653 rcu_read_lock();
David Howellsee18d642009-09-02 09:14:21 +01001654 write_lock_irq(&tasklist_lock);
1655
David Howellsee18d642009-09-02 09:14:21 +01001656 ret = -EPERM;
Oleg Nesterov413cd3d2012-05-11 10:59:08 +10001657 oldwork = NULL;
David Howells7936d162019-05-22 14:09:29 +01001658 parent = rcu_dereference_protected(me->real_parent,
1659 lockdep_is_held(&tasklist_lock));
David Howellsee18d642009-09-02 09:14:21 +01001660
1661 /* the parent mustn't be init and mustn't be a kernel thread */
1662 if (parent->pid <= 1 || !parent->mm)
Oleg Nesterov413cd3d2012-05-11 10:59:08 +10001663 goto unlock;
David Howellsee18d642009-09-02 09:14:21 +01001664
1665 /* the parent must be single threaded */
Oleg Nesterovdd98acf2010-05-26 14:43:23 -07001666 if (!thread_group_empty(parent))
Oleg Nesterov413cd3d2012-05-11 10:59:08 +10001667 goto unlock;
David Howellsee18d642009-09-02 09:14:21 +01001668
1669 /* the parent and the child must have different session keyrings or
1670 * there's no point */
1671 mycred = current_cred();
1672 pcred = __task_cred(parent);
1673 if (mycred == pcred ||
David Howells3a505972012-10-02 19:24:29 +01001674 mycred->session_keyring == pcred->session_keyring) {
Oleg Nesterov413cd3d2012-05-11 10:59:08 +10001675 ret = 0;
1676 goto unlock;
1677 }
David Howellsee18d642009-09-02 09:14:21 +01001678
1679 /* the parent must have the same effective ownership and mustn't be
1680 * SUID/SGID */
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -08001681 if (!uid_eq(pcred->uid, mycred->euid) ||
1682 !uid_eq(pcred->euid, mycred->euid) ||
1683 !uid_eq(pcred->suid, mycred->euid) ||
1684 !gid_eq(pcred->gid, mycred->egid) ||
1685 !gid_eq(pcred->egid, mycred->egid) ||
1686 !gid_eq(pcred->sgid, mycred->egid))
Oleg Nesterov413cd3d2012-05-11 10:59:08 +10001687 goto unlock;
David Howellsee18d642009-09-02 09:14:21 +01001688
1689 /* the keyrings must have the same UID */
David Howells3a505972012-10-02 19:24:29 +01001690 if ((pcred->session_keyring &&
Linus Torvalds2a74dbb2012-12-16 15:40:50 -08001691 !uid_eq(pcred->session_keyring->uid, mycred->euid)) ||
1692 !uid_eq(mycred->session_keyring->uid, mycred->euid))
Oleg Nesterov413cd3d2012-05-11 10:59:08 +10001693 goto unlock;
David Howellsee18d642009-09-02 09:14:21 +01001694
Oleg Nesterov413cd3d2012-05-11 10:59:08 +10001695 /* cancel an already pending keyring replacement */
1696 oldwork = task_work_cancel(parent, key_change_session_keyring);
David Howellsee18d642009-09-02 09:14:21 +01001697
1698 /* the replacement session keyring is applied just prior to userspace
1699 * restarting */
Al Viro67d12142012-06-27 11:07:19 +04001700 ret = task_work_add(parent, newwork, true);
Oleg Nesterov413cd3d2012-05-11 10:59:08 +10001701 if (!ret)
1702 newwork = NULL;
1703unlock:
David Howellsee18d642009-09-02 09:14:21 +01001704 write_unlock_irq(&tasklist_lock);
David Howells9d1ac652010-09-10 09:59:46 +01001705 rcu_read_unlock();
Al Viro67d12142012-06-27 11:07:19 +04001706 if (oldwork)
1707 put_cred(container_of(oldwork, struct cred, rcu));
1708 if (newwork)
1709 put_cred(cred);
David Howellsee18d642009-09-02 09:14:21 +01001710 return ret;
1711
1712error_keyring:
1713 key_ref_put(keyring_r);
1714 return ret;
1715}
1716
David Howellsb5f545c2006-01-08 01:02:47 -08001717/*
Mat Martineau6563c912017-03-01 16:44:09 -08001718 * Apply a restriction to a given keyring.
1719 *
1720 * The caller must have Setattr permission to change keyring restrictions.
1721 *
1722 * The requested type name may be a NULL pointer to reject all attempts
Eric Biggers18026d82017-12-08 15:13:29 +00001723 * to link to the keyring. In this case, _restriction must also be NULL.
1724 * Otherwise, both _type and _restriction must be non-NULL.
Mat Martineau6563c912017-03-01 16:44:09 -08001725 *
1726 * Returns 0 if successful.
1727 */
1728long keyctl_restrict_keyring(key_serial_t id, const char __user *_type,
1729 const char __user *_restriction)
1730{
1731 key_ref_t key_ref;
Mat Martineau6563c912017-03-01 16:44:09 -08001732 char type[32];
1733 char *restriction = NULL;
1734 long ret;
1735
Linus Torvalds028db3e2019-07-10 18:43:43 -07001736 key_ref = lookup_user_key(id, 0, KEY_NEED_SETATTR);
Mat Martineau6563c912017-03-01 16:44:09 -08001737 if (IS_ERR(key_ref))
1738 return PTR_ERR(key_ref);
1739
Eric Biggers18026d82017-12-08 15:13:29 +00001740 ret = -EINVAL;
Mat Martineau6563c912017-03-01 16:44:09 -08001741 if (_type) {
Eric Biggers18026d82017-12-08 15:13:29 +00001742 if (!_restriction)
1743 goto error;
1744
Mat Martineau6563c912017-03-01 16:44:09 -08001745 ret = key_get_type_from_user(type, _type, sizeof(type));
1746 if (ret < 0)
1747 goto error;
Mat Martineau6563c912017-03-01 16:44:09 -08001748
1749 restriction = strndup_user(_restriction, PAGE_SIZE);
1750 if (IS_ERR(restriction)) {
1751 ret = PTR_ERR(restriction);
1752 goto error;
1753 }
Eric Biggers18026d82017-12-08 15:13:29 +00001754 } else {
1755 if (_restriction)
1756 goto error;
Mat Martineau6563c912017-03-01 16:44:09 -08001757 }
1758
Eric Biggers18026d82017-12-08 15:13:29 +00001759 ret = keyring_restrict(key_ref, _type ? type : NULL, restriction);
Mat Martineau6563c912017-03-01 16:44:09 -08001760 kfree(restriction);
Mat Martineau6563c912017-03-01 16:44:09 -08001761error:
1762 key_ref_put(key_ref);
Mat Martineau6563c912017-03-01 16:44:09 -08001763 return ret;
1764}
1765
David Howellsf7e47672020-01-14 17:07:11 +00001766#ifdef CONFIG_KEY_NOTIFICATIONS
1767/*
1768 * Watch for changes to a key.
1769 *
1770 * The caller must have View permission to watch a key or keyring.
1771 */
1772long keyctl_watch_key(key_serial_t id, int watch_queue_fd, int watch_id)
1773{
1774 struct watch_queue *wqueue;
1775 struct watch_list *wlist = NULL;
1776 struct watch *watch = NULL;
1777 struct key *key;
1778 key_ref_t key_ref;
1779 long ret;
1780
1781 if (watch_id < -1 || watch_id > 0xff)
1782 return -EINVAL;
1783
1784 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE, KEY_NEED_VIEW);
1785 if (IS_ERR(key_ref))
1786 return PTR_ERR(key_ref);
1787 key = key_ref_to_ptr(key_ref);
1788
1789 wqueue = get_watch_queue(watch_queue_fd);
1790 if (IS_ERR(wqueue)) {
1791 ret = PTR_ERR(wqueue);
1792 goto err_key;
1793 }
1794
1795 if (watch_id >= 0) {
1796 ret = -ENOMEM;
1797 if (!key->watchers) {
1798 wlist = kzalloc(sizeof(*wlist), GFP_KERNEL);
1799 if (!wlist)
1800 goto err_wqueue;
1801 init_watch_list(wlist, NULL);
1802 }
1803
1804 watch = kzalloc(sizeof(*watch), GFP_KERNEL);
1805 if (!watch)
1806 goto err_wlist;
1807
1808 init_watch(watch, wqueue);
1809 watch->id = key->serial;
1810 watch->info_id = (u32)watch_id << WATCH_INFO_ID__SHIFT;
1811
1812 ret = security_watch_key(key);
1813 if (ret < 0)
1814 goto err_watch;
1815
1816 down_write(&key->sem);
1817 if (!key->watchers) {
1818 key->watchers = wlist;
1819 wlist = NULL;
1820 }
1821
1822 ret = add_watch_to_object(watch, key->watchers);
1823 up_write(&key->sem);
1824
1825 if (ret == 0)
1826 watch = NULL;
1827 } else {
1828 ret = -EBADSLT;
1829 if (key->watchers) {
1830 down_write(&key->sem);
1831 ret = remove_watch_from_object(key->watchers,
1832 wqueue, key_serial(key),
1833 false);
1834 up_write(&key->sem);
1835 }
1836 }
1837
1838err_watch:
1839 kfree(watch);
1840err_wlist:
1841 kfree(wlist);
1842err_wqueue:
1843 put_watch_queue(wqueue);
1844err_key:
1845 key_put(key);
1846 return ret;
1847}
1848#endif /* CONFIG_KEY_NOTIFICATIONS */
1849
Mat Martineau6563c912017-03-01 16:44:09 -08001850/*
David Howells45e0f302019-05-30 14:53:10 +01001851 * Get keyrings subsystem capabilities.
1852 */
1853long keyctl_capabilities(unsigned char __user *_buffer, size_t buflen)
1854{
1855 size_t size = buflen;
1856
1857 if (size > 0) {
1858 if (size > sizeof(keyrings_capabilities))
1859 size = sizeof(keyrings_capabilities);
1860 if (copy_to_user(_buffer, keyrings_capabilities, size) != 0)
1861 return -EFAULT;
1862 if (size < buflen &&
1863 clear_user(_buffer + size, buflen - size) != 0)
1864 return -EFAULT;
1865 }
1866
1867 return sizeof(keyrings_capabilities);
1868}
1869
1870/*
David Howells973c9f42011-01-20 16:38:33 +00001871 * The key control system call
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 */
Heiko Carstens938bb9f2009-01-14 14:14:30 +01001873SYSCALL_DEFINE5(keyctl, int, option, unsigned long, arg2, unsigned long, arg3,
1874 unsigned long, arg4, unsigned long, arg5)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875{
1876 switch (option) {
1877 case KEYCTL_GET_KEYRING_ID:
1878 return keyctl_get_keyring_ID((key_serial_t) arg2,
1879 (int) arg3);
1880
1881 case KEYCTL_JOIN_SESSION_KEYRING:
1882 return keyctl_join_session_keyring((const char __user *) arg2);
1883
1884 case KEYCTL_UPDATE:
1885 return keyctl_update_key((key_serial_t) arg2,
1886 (const void __user *) arg3,
1887 (size_t) arg4);
1888
1889 case KEYCTL_REVOKE:
1890 return keyctl_revoke_key((key_serial_t) arg2);
1891
1892 case KEYCTL_DESCRIBE:
1893 return keyctl_describe_key((key_serial_t) arg2,
1894 (char __user *) arg3,
1895 (unsigned) arg4);
1896
1897 case KEYCTL_CLEAR:
1898 return keyctl_keyring_clear((key_serial_t) arg2);
1899
1900 case KEYCTL_LINK:
1901 return keyctl_keyring_link((key_serial_t) arg2,
1902 (key_serial_t) arg3);
1903
1904 case KEYCTL_UNLINK:
1905 return keyctl_keyring_unlink((key_serial_t) arg2,
1906 (key_serial_t) arg3);
1907
1908 case KEYCTL_SEARCH:
1909 return keyctl_keyring_search((key_serial_t) arg2,
1910 (const char __user *) arg3,
1911 (const char __user *) arg4,
1912 (key_serial_t) arg5);
1913
1914 case KEYCTL_READ:
1915 return keyctl_read_key((key_serial_t) arg2,
1916 (char __user *) arg3,
1917 (size_t) arg4);
1918
1919 case KEYCTL_CHOWN:
1920 return keyctl_chown_key((key_serial_t) arg2,
1921 (uid_t) arg3,
1922 (gid_t) arg4);
1923
1924 case KEYCTL_SETPERM:
1925 return keyctl_setperm_key((key_serial_t) arg2,
Linus Torvalds028db3e2019-07-10 18:43:43 -07001926 (key_perm_t) arg3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927
1928 case KEYCTL_INSTANTIATE:
1929 return keyctl_instantiate_key((key_serial_t) arg2,
1930 (const void __user *) arg3,
1931 (size_t) arg4,
1932 (key_serial_t) arg5);
1933
1934 case KEYCTL_NEGATE:
1935 return keyctl_negate_key((key_serial_t) arg2,
1936 (unsigned) arg3,
1937 (key_serial_t) arg4);
1938
David Howells3e301482005-06-23 22:00:56 -07001939 case KEYCTL_SET_REQKEY_KEYRING:
1940 return keyctl_set_reqkey_keyring(arg2);
1941
David Howells017679c2006-01-08 01:02:43 -08001942 case KEYCTL_SET_TIMEOUT:
1943 return keyctl_set_timeout((key_serial_t) arg2,
1944 (unsigned) arg3);
1945
David Howellsb5f545c2006-01-08 01:02:47 -08001946 case KEYCTL_ASSUME_AUTHORITY:
1947 return keyctl_assume_authority((key_serial_t) arg2);
1948
David Howells70a5bb72008-04-29 01:01:26 -07001949 case KEYCTL_GET_SECURITY:
1950 return keyctl_get_security((key_serial_t) arg2,
James Morris90bd49a2008-12-29 14:35:35 +11001951 (char __user *) arg3,
David Howells70a5bb72008-04-29 01:01:26 -07001952 (size_t) arg4);
1953
David Howellsee18d642009-09-02 09:14:21 +01001954 case KEYCTL_SESSION_TO_PARENT:
1955 return keyctl_session_to_parent();
1956
David Howellsfdd1b942011-03-07 15:06:09 +00001957 case KEYCTL_REJECT:
1958 return keyctl_reject_key((key_serial_t) arg2,
1959 (unsigned) arg3,
1960 (unsigned) arg4,
1961 (key_serial_t) arg5);
1962
David Howellsee009e4a02011-03-07 15:06:20 +00001963 case KEYCTL_INSTANTIATE_IOV:
1964 return keyctl_instantiate_key_iov(
1965 (key_serial_t) arg2,
1966 (const struct iovec __user *) arg3,
1967 (unsigned) arg4,
1968 (key_serial_t) arg5);
1969
David Howellsfd758152012-05-11 10:56:56 +01001970 case KEYCTL_INVALIDATE:
1971 return keyctl_invalidate_key((key_serial_t) arg2);
1972
David Howellsf36f8c72013-09-24 10:35:19 +01001973 case KEYCTL_GET_PERSISTENT:
1974 return keyctl_get_persistent((uid_t)arg2, (key_serial_t)arg3);
1975
Mat Martineauddbb4112016-04-12 19:54:58 +01001976 case KEYCTL_DH_COMPUTE:
1977 return keyctl_dh_compute((struct keyctl_dh_params __user *) arg2,
Stephan Mueller4693fc72016-05-26 23:38:12 +02001978 (char __user *) arg3, (size_t) arg4,
Stephan Muellerf1c316a2016-08-19 20:39:09 +02001979 (struct keyctl_kdf_params __user *) arg5);
Mat Martineauddbb4112016-04-12 19:54:58 +01001980
Mat Martineau6563c912017-03-01 16:44:09 -08001981 case KEYCTL_RESTRICT_KEYRING:
1982 return keyctl_restrict_keyring((key_serial_t) arg2,
1983 (const char __user *) arg3,
1984 (const char __user *) arg4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
David Howells00d60fd2018-10-09 17:46:59 +01001986 case KEYCTL_PKEY_QUERY:
1987 if (arg3 != 0)
1988 return -EINVAL;
1989 return keyctl_pkey_query((key_serial_t)arg2,
1990 (const char __user *)arg4,
Ben Dooks468e91c2019-03-01 11:30:26 +00001991 (struct keyctl_pkey_query __user *)arg5);
David Howells00d60fd2018-10-09 17:46:59 +01001992
1993 case KEYCTL_PKEY_ENCRYPT:
1994 case KEYCTL_PKEY_DECRYPT:
1995 case KEYCTL_PKEY_SIGN:
1996 return keyctl_pkey_e_d_s(
1997 option,
1998 (const struct keyctl_pkey_params __user *)arg2,
1999 (const char __user *)arg3,
2000 (const void __user *)arg4,
2001 (void __user *)arg5);
2002
2003 case KEYCTL_PKEY_VERIFY:
2004 return keyctl_pkey_verify(
2005 (const struct keyctl_pkey_params __user *)arg2,
2006 (const char __user *)arg3,
2007 (const void __user *)arg4,
2008 (const void __user *)arg5);
2009
David Howellsed0ac5c2019-05-20 21:51:50 +01002010 case KEYCTL_MOVE:
2011 return keyctl_keyring_move((key_serial_t)arg2,
2012 (key_serial_t)arg3,
2013 (key_serial_t)arg4,
2014 (unsigned int)arg5);
2015
David Howells45e0f302019-05-30 14:53:10 +01002016 case KEYCTL_CAPABILITIES:
2017 return keyctl_capabilities((unsigned char __user *)arg2, (size_t)arg3);
2018
David Howellsf7e47672020-01-14 17:07:11 +00002019 case KEYCTL_WATCH_KEY:
2020 return keyctl_watch_key((key_serial_t)arg2, (int)arg3, (int)arg4);
2021
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 default:
2023 return -EOPNOTSUPP;
2024 }
2025}