blob: 169409b611b0189cd2eeafc7a63eca5a0adb6b01 [file] [log] [blame]
David Howells973c9f42011-01-20 16:38:33 +00001/* Userspace key control operations
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
David Howells3e301482005-06-23 22:00:56 -07003 * Copyright (C) 2004-5 Red Hat, Inc. All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Written by David Howells (dhowells@redhat.com)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
11
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/init.h>
13#include <linux/sched.h>
Ingo Molnar29930022017-02-08 18:51:36 +010014#include <linux/sched/task.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/slab.h>
16#include <linux/syscalls.h>
Bryan Schumaker59e6b9c2012-02-24 14:14:50 -050017#include <linux/key.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/keyctl.h>
19#include <linux/fs.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080020#include <linux/capability.h>
Ingo Molnar5b825c32017-02-02 17:54:15 +010021#include <linux/cred.h>
Davi Arnaut0cb409d2006-03-24 03:18:43 -080022#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/err.h>
David Howells38bbca62008-04-29 01:01:19 -070024#include <linux/vmalloc.h>
David Howells70a5bb72008-04-29 01:01:26 -070025#include <linux/security.h>
Kent Overstreeta27bb332013-05-07 16:19:08 -070026#include <linux/uio.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080027#include <linux/uaccess.h>
David Howells822ad642019-02-14 16:20:25 +000028#include <keys/request_key_auth-type.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "internal.h"
30
David Howellsaa9d4432014-12-01 22:52:45 +000031#define KEY_MAX_DESC_SIZE 4096
32
David Howells45e0f302019-05-30 14:53:10 +010033static const unsigned char keyrings_capabilities[1] = {
34 [0] = (KEYCTL_CAPS0_CAPABILITIES |
35 (IS_ENABLED(CONFIG_PERSISTENT_KEYRINGS) ? KEYCTL_CAPS0_PERSISTENT_KEYRINGS : 0) |
36 (IS_ENABLED(CONFIG_KEY_DH_OPERATIONS) ? KEYCTL_CAPS0_DIFFIE_HELLMAN : 0) |
37 (IS_ENABLED(CONFIG_ASYMMETRIC_KEY_TYPE) ? KEYCTL_CAPS0_PUBLIC_KEY : 0) |
38 (IS_ENABLED(CONFIG_BIG_KEYS) ? KEYCTL_CAPS0_BIG_KEY : 0) |
39 KEYCTL_CAPS0_INVALIDATE |
40 KEYCTL_CAPS0_RESTRICT_KEYRING |
41 KEYCTL_CAPS0_MOVE
42 ),
43};
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,
Arun Raghavan6b79ccb2008-04-29 01:01:28 -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 Howells4a38e122008-04-29 01:01:24 -0700225 key = request_key_and_link(ktype, description, callout_info,
226 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;
344 payload = kmalloc(plen, GFP_KERNEL);
345 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:
Eric Biggers57070c82017-06-08 14:48:57 +0100365 kzfree(payload);
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
David Howellsf5895942014-03-14 17:44:49 +0000388 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);
David Howells0c2c9a32009-09-02 09:13:50 +0100391 if (ret != -EACCES)
392 goto error;
David Howellsf5895942014-03-14 17:44:49 +0000393 key_ref = lookup_user_key(id, 0, KEY_NEED_SETATTR);
David Howells0c2c9a32009-09-02 09:13:50 +0100394 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
David Howellsf5895942014-03-14 17:44:49 +0000431 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
David Howellsf5895942014-03-14 17:44:49 +0000476 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),
687 key->perm);
688 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/*
David Howells973c9f42011-01-20 16:38:33 +0000803 * Read a key's payload.
804 *
805 * The key must either grant the caller Read permission, or it must grant the
806 * caller Search permission when searched for from the process keyrings.
807 *
808 * If successful, we place up to buflen bytes of data into the buffer, if one
809 * is provided, and return the amount of data that is available in the key,
810 * irrespective of how much we copied into the buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 */
812long keyctl_read_key(key_serial_t keyid, char __user *buffer, size_t buflen)
813{
David Howells664cceb2005-09-28 17:03:15 +0100814 struct key *key;
815 key_ref_t key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 long ret;
817
818 /* find the key first */
David Howells55931222009-09-02 09:13:45 +0100819 key_ref = lookup_user_key(keyid, 0, 0);
David Howells664cceb2005-09-28 17:03:15 +0100820 if (IS_ERR(key_ref)) {
821 ret = -ENOKEY;
822 goto error;
823 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
David Howells664cceb2005-09-28 17:03:15 +0100825 key = key_ref_to_ptr(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
David Howells363b02d2017-10-04 16:43:25 +0100827 ret = key_read_state(key);
828 if (ret < 0)
829 goto error2; /* Negatively instantiated */
Eric Biggers37863c42017-09-18 11:37:23 -0700830
David Howells664cceb2005-09-28 17:03:15 +0100831 /* see if we can read it directly */
David Howellsf5895942014-03-14 17:44:49 +0000832 ret = key_permission(key_ref, KEY_NEED_READ);
David Howells29db9192005-10-30 15:02:44 -0800833 if (ret == 0)
David Howells664cceb2005-09-28 17:03:15 +0100834 goto can_read_key;
David Howells29db9192005-10-30 15:02:44 -0800835 if (ret != -EACCES)
Eric Biggers7fc07862017-09-18 11:36:31 -0700836 goto error2;
David Howells664cceb2005-09-28 17:03:15 +0100837
838 /* we can't; see if it's searchable from this process's keyrings
839 * - we automatically take account of the fact that it may be
840 * dangling off an instantiation key
841 */
842 if (!is_key_possessed(key_ref)) {
843 ret = -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 goto error2;
845 }
846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 /* the key is probably readable - now try to read it */
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700848can_read_key:
David Howellsb4a1b4f2015-12-18 01:34:26 +0000849 ret = -EOPNOTSUPP;
850 if (key->type->read) {
851 /* Read the data with the semaphore held (since we might sleep)
852 * to protect against the key being updated or revoked.
853 */
854 down_read(&key->sem);
855 ret = key_validate(key);
856 if (ret == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 ret = key->type->read(key, buffer, buflen);
David Howellsb4a1b4f2015-12-18 01:34:26 +0000858 up_read(&key->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 }
860
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700861error2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 key_put(key);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700863error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000865}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867/*
David Howells973c9f42011-01-20 16:38:33 +0000868 * Change the ownership of a key
869 *
870 * The key must grant the caller Setattr permission for this to work, though
871 * the key need not be fully instantiated yet. For the UID to be changed, or
872 * for the GID to be changed to a group the caller is not a member of, the
873 * caller must have sysadmin capability. If either uid or gid is -1 then that
874 * attribute is not changed.
875 *
876 * If the UID is to be changed, the new user must have sufficient quota to
877 * accept the key. The quota deduction will be removed from the old user to
878 * the new user should the attribute be changed.
879 *
880 * If successful, 0 will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 */
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -0800882long keyctl_chown_key(key_serial_t id, uid_t user, gid_t group)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883{
Fredrik Tolf58016492006-06-26 00:24:51 -0700884 struct key_user *newowner, *zapowner = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 struct key *key;
David Howells664cceb2005-09-28 17:03:15 +0100886 key_ref_t key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 long ret;
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -0800888 kuid_t uid;
889 kgid_t gid;
890
891 uid = make_kuid(current_user_ns(), user);
892 gid = make_kgid(current_user_ns(), group);
893 ret = -EINVAL;
894 if ((user != (uid_t) -1) && !uid_valid(uid))
895 goto error;
896 if ((group != (gid_t) -1) && !gid_valid(gid))
897 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
899 ret = 0;
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -0800900 if (user == (uid_t) -1 && group == (gid_t) -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 goto error;
902
David Howells55931222009-09-02 09:13:45 +0100903 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
David Howellsf5895942014-03-14 17:44:49 +0000904 KEY_NEED_SETATTR);
David Howells664cceb2005-09-28 17:03:15 +0100905 if (IS_ERR(key_ref)) {
906 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 goto error;
908 }
909
David Howells664cceb2005-09-28 17:03:15 +0100910 key = key_ref_to_ptr(key_ref);
911
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 /* make the changes with the locks held to prevent chown/chown races */
913 ret = -EACCES;
914 down_write(&key->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
916 if (!capable(CAP_SYS_ADMIN)) {
917 /* only the sysadmin can chown a key to some other UID */
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -0800918 if (user != (uid_t) -1 && !uid_eq(key->uid, uid))
Fredrik Tolf58016492006-06-26 00:24:51 -0700919 goto error_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
921 /* only the sysadmin can set the key's GID to a group other
922 * than one of those that the current process subscribes to */
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -0800923 if (group != (gid_t) -1 && !gid_eq(gid, key->gid) && !in_group_p(gid))
Fredrik Tolf58016492006-06-26 00:24:51 -0700924 goto error_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 }
926
Fredrik Tolf58016492006-06-26 00:24:51 -0700927 /* change the UID */
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -0800928 if (user != (uid_t) -1 && !uid_eq(uid, key->uid)) {
Fredrik Tolf58016492006-06-26 00:24:51 -0700929 ret = -ENOMEM;
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -0800930 newowner = key_user_lookup(uid);
Fredrik Tolf58016492006-06-26 00:24:51 -0700931 if (!newowner)
932 goto error_put;
933
934 /* transfer the quota burden to the new user */
935 if (test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) {
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -0800936 unsigned maxkeys = uid_eq(uid, GLOBAL_ROOT_UID) ?
David Howells0b77f5b2008-04-29 01:01:32 -0700937 key_quota_root_maxkeys : key_quota_maxkeys;
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -0800938 unsigned maxbytes = uid_eq(uid, GLOBAL_ROOT_UID) ?
David Howells0b77f5b2008-04-29 01:01:32 -0700939 key_quota_root_maxbytes : key_quota_maxbytes;
940
Fredrik Tolf58016492006-06-26 00:24:51 -0700941 spin_lock(&newowner->lock);
David Howells0b77f5b2008-04-29 01:01:32 -0700942 if (newowner->qnkeys + 1 >= maxkeys ||
943 newowner->qnbytes + key->quotalen >= maxbytes ||
944 newowner->qnbytes + key->quotalen <
945 newowner->qnbytes)
Fredrik Tolf58016492006-06-26 00:24:51 -0700946 goto quota_overrun;
947
948 newowner->qnkeys++;
949 newowner->qnbytes += key->quotalen;
950 spin_unlock(&newowner->lock);
951
952 spin_lock(&key->user->lock);
953 key->user->qnkeys--;
954 key->user->qnbytes -= key->quotalen;
955 spin_unlock(&key->user->lock);
956 }
957
958 atomic_dec(&key->user->nkeys);
959 atomic_inc(&newowner->nkeys);
960
David Howells363b02d2017-10-04 16:43:25 +0100961 if (key->state != KEY_IS_UNINSTANTIATED) {
Fredrik Tolf58016492006-06-26 00:24:51 -0700962 atomic_dec(&key->user->nikeys);
963 atomic_inc(&newowner->nikeys);
964 }
965
966 zapowner = key->user;
967 key->user = newowner;
968 key->uid = uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 }
970
971 /* change the GID */
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -0800972 if (group != (gid_t) -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 key->gid = gid;
974
975 ret = 0;
976
Fredrik Tolf58016492006-06-26 00:24:51 -0700977error_put:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 up_write(&key->sem);
979 key_put(key);
Fredrik Tolf58016492006-06-26 00:24:51 -0700980 if (zapowner)
981 key_user_put(zapowner);
982error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 return ret;
984
Fredrik Tolf58016492006-06-26 00:24:51 -0700985quota_overrun:
986 spin_unlock(&newowner->lock);
987 zapowner = newowner;
988 ret = -EDQUOT;
989 goto error_put;
David Howellsa8b17ed2011-01-20 16:38:27 +0000990}
Fredrik Tolf58016492006-06-26 00:24:51 -0700991
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992/*
David Howells973c9f42011-01-20 16:38:33 +0000993 * Change the permission mask on a key.
994 *
995 * The key must grant the caller Setattr permission for this to work, though
996 * the key need not be fully instantiated yet. If the caller does not have
997 * sysadmin capability, it may only change the permission on keys that it owns.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 */
999long keyctl_setperm_key(key_serial_t id, key_perm_t perm)
1000{
1001 struct key *key;
David Howells664cceb2005-09-28 17:03:15 +01001002 key_ref_t key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 long ret;
1004
1005 ret = -EINVAL;
David Howells664cceb2005-09-28 17:03:15 +01001006 if (perm & ~(KEY_POS_ALL | KEY_USR_ALL | KEY_GRP_ALL | KEY_OTH_ALL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 goto error;
1008
David Howells55931222009-09-02 09:13:45 +01001009 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
David Howellsf5895942014-03-14 17:44:49 +00001010 KEY_NEED_SETATTR);
David Howells664cceb2005-09-28 17:03:15 +01001011 if (IS_ERR(key_ref)) {
1012 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 goto error;
1014 }
1015
David Howells664cceb2005-09-28 17:03:15 +01001016 key = key_ref_to_ptr(key_ref);
1017
David Howells76d8aea2005-06-23 22:00:49 -07001018 /* make the changes with the locks held to prevent chown/chmod races */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 ret = -EACCES;
1020 down_write(&key->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
David Howells76d8aea2005-06-23 22:00:49 -07001022 /* if we're not the sysadmin, we can only change a key that we own */
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -08001023 if (capable(CAP_SYS_ADMIN) || uid_eq(key->uid, current_fsuid())) {
David Howells76d8aea2005-06-23 22:00:49 -07001024 key->perm = perm;
1025 ret = 0;
1026 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 up_write(&key->sem);
1029 key_put(key);
David Howells76d8aea2005-06-23 22:00:49 -07001030error:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +00001032}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
David Howells8bbf49762008-11-14 10:39:14 +11001034/*
David Howells973c9f42011-01-20 16:38:33 +00001035 * Get the destination keyring for instantiation and check that the caller has
1036 * Write permission on it.
David Howells8bbf49762008-11-14 10:39:14 +11001037 */
1038static long get_instantiation_keyring(key_serial_t ringid,
1039 struct request_key_auth *rka,
1040 struct key **_dest_keyring)
1041{
1042 key_ref_t dkref;
1043
David Howellseca1bf52008-12-29 00:41:51 +00001044 *_dest_keyring = NULL;
1045
David Howells8bbf49762008-11-14 10:39:14 +11001046 /* just return a NULL pointer if we weren't asked to make a link */
David Howellseca1bf52008-12-29 00:41:51 +00001047 if (ringid == 0)
David Howells8bbf49762008-11-14 10:39:14 +11001048 return 0;
David Howells8bbf49762008-11-14 10:39:14 +11001049
1050 /* if a specific keyring is nominated by ID, then use that */
1051 if (ringid > 0) {
David Howellsf5895942014-03-14 17:44:49 +00001052 dkref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
David Howells8bbf49762008-11-14 10:39:14 +11001053 if (IS_ERR(dkref))
1054 return PTR_ERR(dkref);
1055 *_dest_keyring = key_ref_to_ptr(dkref);
1056 return 0;
1057 }
1058
1059 if (ringid == KEY_SPEC_REQKEY_AUTH_KEY)
1060 return -EINVAL;
1061
1062 /* otherwise specify the destination keyring recorded in the
1063 * authorisation key (any KEY_SPEC_*_KEYRING) */
1064 if (ringid >= KEY_SPEC_REQUESTOR_KEYRING) {
David Howells21279cf2009-10-15 10:14:35 +01001065 *_dest_keyring = key_get(rka->dest_keyring);
David Howells8bbf49762008-11-14 10:39:14 +11001066 return 0;
1067 }
1068
1069 return -ENOKEY;
1070}
1071
David Howellsd84f4f92008-11-14 10:39:23 +11001072/*
David Howells973c9f42011-01-20 16:38:33 +00001073 * Change the request_key authorisation key on the current process.
David Howellsd84f4f92008-11-14 10:39:23 +11001074 */
1075static int keyctl_change_reqkey_auth(struct key *key)
1076{
1077 struct cred *new;
1078
1079 new = prepare_creds();
1080 if (!new)
1081 return -ENOMEM;
1082
1083 key_put(new->request_key_auth);
1084 new->request_key_auth = key_get(key);
1085
1086 return commit_creds(new);
1087}
1088
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089/*
David Howells973c9f42011-01-20 16:38:33 +00001090 * Instantiate a key with the specified payload and link the key into the
1091 * destination keyring if one is given.
1092 *
1093 * The caller must have the appropriate instantiation permit set for this to
1094 * work (see keyctl_assume_authority). No other permissions are required.
1095 *
1096 * If successful, 0 will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 */
David Howellsee009e4a02011-03-07 15:06:20 +00001098long keyctl_instantiate_key_common(key_serial_t id,
Al Virob353a1f2015-03-17 09:59:38 -04001099 struct iov_iter *from,
David Howellsee009e4a02011-03-07 15:06:20 +00001100 key_serial_t ringid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101{
David Howellsd84f4f92008-11-14 10:39:23 +11001102 const struct cred *cred = current_cred();
David Howells3e301482005-06-23 22:00:56 -07001103 struct request_key_auth *rka;
David Howells8bbf49762008-11-14 10:39:14 +11001104 struct key *instkey, *dest_keyring;
Al Virob353a1f2015-03-17 09:59:38 -04001105 size_t plen = from ? iov_iter_count(from) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 void *payload;
1107 long ret;
1108
David Howellsd84f4f92008-11-14 10:39:23 +11001109 kenter("%d,,%zu,%d", id, plen, ringid);
1110
Al Virob353a1f2015-03-17 09:59:38 -04001111 if (!plen)
1112 from = NULL;
1113
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 ret = -EINVAL;
David Howells38bbca62008-04-29 01:01:19 -07001115 if (plen > 1024 * 1024 - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 goto error;
1117
David Howellsb5f545c2006-01-08 01:02:47 -08001118 /* the appropriate instantiation authorisation key must have been
1119 * assumed before calling this */
1120 ret = -EPERM;
David Howellsd84f4f92008-11-14 10:39:23 +11001121 instkey = cred->request_key_auth;
David Howellsb5f545c2006-01-08 01:02:47 -08001122 if (!instkey)
1123 goto error;
1124
David Howells146aa8b2015-10-21 14:04:48 +01001125 rka = instkey->payload.data[0];
David Howellsb5f545c2006-01-08 01:02:47 -08001126 if (rka->target_key->serial != id)
1127 goto error;
1128
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 /* pull the payload in if one was supplied */
1130 payload = NULL;
1131
Al Virob353a1f2015-03-17 09:59:38 -04001132 if (from) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 ret = -ENOMEM;
Michal Hocko752ade62017-05-08 15:57:27 -07001134 payload = kvmalloc(plen, GFP_KERNEL);
1135 if (!payload)
1136 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
Al Virob353a1f2015-03-17 09:59:38 -04001138 ret = -EFAULT;
Al Virocbbd26b2016-11-01 22:09:04 -04001139 if (!copy_from_iter_full(payload, plen, from))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 goto error2;
1141 }
1142
David Howells3e301482005-06-23 22:00:56 -07001143 /* find the destination keyring amongst those belonging to the
1144 * requesting task */
David Howells8bbf49762008-11-14 10:39:14 +11001145 ret = get_instantiation_keyring(ringid, rka, &dest_keyring);
1146 if (ret < 0)
1147 goto error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
1149 /* instantiate the key and link it into a keyring */
David Howells3e301482005-06-23 22:00:56 -07001150 ret = key_instantiate_and_link(rka->target_key, payload, plen,
David Howells8bbf49762008-11-14 10:39:14 +11001151 dest_keyring, instkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
David Howells8bbf49762008-11-14 10:39:14 +11001153 key_put(dest_keyring);
David Howellsb5f545c2006-01-08 01:02:47 -08001154
1155 /* discard the assumed authority if it's just been disabled by
1156 * instantiation of the key */
David Howellsd84f4f92008-11-14 10:39:23 +11001157 if (ret == 0)
1158 keyctl_change_reqkey_auth(NULL);
David Howellsb5f545c2006-01-08 01:02:47 -08001159
1160error2:
Eric Biggers57070c82017-06-08 14:48:57 +01001161 if (payload) {
1162 memzero_explicit(payload, plen);
1163 kvfree(payload);
1164 }
David Howellsb5f545c2006-01-08 01:02:47 -08001165error:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +00001167}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169/*
David Howellsee009e4a02011-03-07 15:06:20 +00001170 * Instantiate a key with the specified payload and link the key into the
1171 * destination keyring if one is given.
1172 *
1173 * The caller must have the appropriate instantiation permit set for this to
1174 * work (see keyctl_assume_authority). No other permissions are required.
1175 *
1176 * If successful, 0 will be returned.
1177 */
1178long keyctl_instantiate_key(key_serial_t id,
1179 const void __user *_payload,
1180 size_t plen,
1181 key_serial_t ringid)
1182{
1183 if (_payload && plen) {
Al Virob353a1f2015-03-17 09:59:38 -04001184 struct iovec iov;
1185 struct iov_iter from;
1186 int ret;
David Howellsee009e4a02011-03-07 15:06:20 +00001187
Al Virob353a1f2015-03-17 09:59:38 -04001188 ret = import_single_range(WRITE, (void __user *)_payload, plen,
1189 &iov, &from);
1190 if (unlikely(ret))
1191 return ret;
1192
1193 return keyctl_instantiate_key_common(id, &from, ringid);
David Howellsee009e4a02011-03-07 15:06:20 +00001194 }
1195
Al Virob353a1f2015-03-17 09:59:38 -04001196 return keyctl_instantiate_key_common(id, NULL, ringid);
David Howellsee009e4a02011-03-07 15:06:20 +00001197}
1198
1199/*
1200 * Instantiate a key with the specified multipart payload and link the key into
1201 * the destination keyring if one is given.
1202 *
1203 * The caller must have the appropriate instantiation permit set for this to
1204 * work (see keyctl_assume_authority). No other permissions are required.
1205 *
1206 * If successful, 0 will be returned.
1207 */
1208long keyctl_instantiate_key_iov(key_serial_t id,
1209 const struct iovec __user *_payload_iov,
1210 unsigned ioc,
1211 key_serial_t ringid)
1212{
1213 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
Al Virob353a1f2015-03-17 09:59:38 -04001214 struct iov_iter from;
David Howellsee009e4a02011-03-07 15:06:20 +00001215 long ret;
1216
Al Virob353a1f2015-03-17 09:59:38 -04001217 if (!_payload_iov)
1218 ioc = 0;
David Howellsee009e4a02011-03-07 15:06:20 +00001219
Al Virob353a1f2015-03-17 09:59:38 -04001220 ret = import_iovec(WRITE, _payload_iov, ioc,
1221 ARRAY_SIZE(iovstack), &iov, &from);
David Howellsee009e4a02011-03-07 15:06:20 +00001222 if (ret < 0)
Al Virob353a1f2015-03-17 09:59:38 -04001223 return ret;
1224 ret = keyctl_instantiate_key_common(id, &from, ringid);
1225 kfree(iov);
David Howellsee009e4a02011-03-07 15:06:20 +00001226 return ret;
David Howellsee009e4a02011-03-07 15:06:20 +00001227}
1228
1229/*
David Howells973c9f42011-01-20 16:38:33 +00001230 * Negatively instantiate the key with the given timeout (in seconds) and link
1231 * the key into the destination keyring if one is given.
1232 *
1233 * The caller must have the appropriate instantiation permit set for this to
1234 * work (see keyctl_assume_authority). No other permissions are required.
1235 *
1236 * The key and any links to the key will be automatically garbage collected
1237 * after the timeout expires.
1238 *
1239 * Negative keys are used to rate limit repeated request_key() calls by causing
1240 * them to return -ENOKEY until the negative key expires.
1241 *
1242 * If successful, 0 will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 */
1244long keyctl_negate_key(key_serial_t id, unsigned timeout, key_serial_t ringid)
1245{
David Howellsfdd1b942011-03-07 15:06:09 +00001246 return keyctl_reject_key(id, timeout, ENOKEY, ringid);
1247}
1248
1249/*
1250 * Negatively instantiate the key with the given timeout (in seconds) and error
1251 * code and link the key into the destination keyring if one is given.
1252 *
1253 * The caller must have the appropriate instantiation permit set for this to
1254 * work (see keyctl_assume_authority). No other permissions are required.
1255 *
1256 * The key and any links to the key will be automatically garbage collected
1257 * after the timeout expires.
1258 *
1259 * Negative keys are used to rate limit repeated request_key() calls by causing
1260 * them to return the specified error code until the negative key expires.
1261 *
1262 * If successful, 0 will be returned.
1263 */
1264long keyctl_reject_key(key_serial_t id, unsigned timeout, unsigned error,
1265 key_serial_t ringid)
1266{
David Howellsd84f4f92008-11-14 10:39:23 +11001267 const struct cred *cred = current_cred();
David Howells3e301482005-06-23 22:00:56 -07001268 struct request_key_auth *rka;
David Howells8bbf49762008-11-14 10:39:14 +11001269 struct key *instkey, *dest_keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 long ret;
1271
David Howellsfdd1b942011-03-07 15:06:09 +00001272 kenter("%d,%u,%u,%d", id, timeout, error, ringid);
1273
1274 /* must be a valid error code and mustn't be a kernel special */
1275 if (error <= 0 ||
1276 error >= MAX_ERRNO ||
1277 error == ERESTARTSYS ||
1278 error == ERESTARTNOINTR ||
1279 error == ERESTARTNOHAND ||
1280 error == ERESTART_RESTARTBLOCK)
1281 return -EINVAL;
David Howellsd84f4f92008-11-14 10:39:23 +11001282
David Howellsb5f545c2006-01-08 01:02:47 -08001283 /* the appropriate instantiation authorisation key must have been
1284 * assumed before calling this */
1285 ret = -EPERM;
David Howellsd84f4f92008-11-14 10:39:23 +11001286 instkey = cred->request_key_auth;
David Howellsb5f545c2006-01-08 01:02:47 -08001287 if (!instkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
David Howells146aa8b2015-10-21 14:04:48 +01001290 rka = instkey->payload.data[0];
David Howellsb5f545c2006-01-08 01:02:47 -08001291 if (rka->target_key->serial != id)
1292 goto error;
David Howells3e301482005-06-23 22:00:56 -07001293
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 /* find the destination keyring if present (which must also be
1295 * writable) */
David Howells8bbf49762008-11-14 10:39:14 +11001296 ret = get_instantiation_keyring(ringid, rka, &dest_keyring);
1297 if (ret < 0)
1298 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
1300 /* instantiate the key and link it into a keyring */
David Howellsfdd1b942011-03-07 15:06:09 +00001301 ret = key_reject_and_link(rka->target_key, timeout, error,
David Howells8bbf49762008-11-14 10:39:14 +11001302 dest_keyring, instkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
David Howells8bbf49762008-11-14 10:39:14 +11001304 key_put(dest_keyring);
David Howellsb5f545c2006-01-08 01:02:47 -08001305
1306 /* discard the assumed authority if it's just been disabled by
1307 * instantiation of the key */
David Howellsd84f4f92008-11-14 10:39:23 +11001308 if (ret == 0)
1309 keyctl_change_reqkey_auth(NULL);
David Howellsb5f545c2006-01-08 01:02:47 -08001310
1311error:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +00001313}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315/*
David Howells973c9f42011-01-20 16:38:33 +00001316 * Read or set the default keyring in which request_key() will cache keys and
1317 * return the old setting.
1318 *
Eric Biggersc9f838d2017-04-18 15:31:09 +01001319 * If a thread or process keyring is specified then it will be created if it
1320 * doesn't yet exist. The old setting will be returned if successful.
David Howells3e301482005-06-23 22:00:56 -07001321 */
1322long keyctl_set_reqkey_keyring(int reqkey_defl)
1323{
David Howellsd84f4f92008-11-14 10:39:23 +11001324 struct cred *new;
1325 int ret, old_setting;
1326
1327 old_setting = current_cred_xxx(jit_keyring);
1328
1329 if (reqkey_defl == KEY_REQKEY_DEFL_NO_CHANGE)
1330 return old_setting;
1331
1332 new = prepare_creds();
1333 if (!new)
1334 return -ENOMEM;
David Howells3e301482005-06-23 22:00:56 -07001335
1336 switch (reqkey_defl) {
1337 case KEY_REQKEY_DEFL_THREAD_KEYRING:
David Howellsd84f4f92008-11-14 10:39:23 +11001338 ret = install_thread_keyring_to_cred(new);
David Howells3e301482005-06-23 22:00:56 -07001339 if (ret < 0)
David Howellsd84f4f92008-11-14 10:39:23 +11001340 goto error;
David Howells3e301482005-06-23 22:00:56 -07001341 goto set;
1342
1343 case KEY_REQKEY_DEFL_PROCESS_KEYRING:
David Howellsd84f4f92008-11-14 10:39:23 +11001344 ret = install_process_keyring_to_cred(new);
Eric Biggersc9f838d2017-04-18 15:31:09 +01001345 if (ret < 0)
1346 goto error;
David Howellsd84f4f92008-11-14 10:39:23 +11001347 goto set;
David Howells3e301482005-06-23 22:00:56 -07001348
1349 case KEY_REQKEY_DEFL_DEFAULT:
1350 case KEY_REQKEY_DEFL_SESSION_KEYRING:
1351 case KEY_REQKEY_DEFL_USER_KEYRING:
1352 case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
David Howellsd84f4f92008-11-14 10:39:23 +11001353 case KEY_REQKEY_DEFL_REQUESTOR_KEYRING:
1354 goto set;
David Howells3e301482005-06-23 22:00:56 -07001355
1356 case KEY_REQKEY_DEFL_NO_CHANGE:
David Howells3e301482005-06-23 22:00:56 -07001357 case KEY_REQKEY_DEFL_GROUP_KEYRING:
1358 default:
David Howellsd84f4f92008-11-14 10:39:23 +11001359 ret = -EINVAL;
1360 goto error;
David Howells3e301482005-06-23 22:00:56 -07001361 }
1362
David Howellsd84f4f92008-11-14 10:39:23 +11001363set:
1364 new->jit_keyring = reqkey_defl;
1365 commit_creds(new);
1366 return old_setting;
1367error:
1368 abort_creds(new);
Dan Carpenter4303ef12010-06-11 17:30:05 +01001369 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +00001370}
David Howellsd84f4f92008-11-14 10:39:23 +11001371
David Howells3e301482005-06-23 22:00:56 -07001372/*
David Howells973c9f42011-01-20 16:38:33 +00001373 * Set or clear the timeout on a key.
1374 *
1375 * Either the key must grant the caller Setattr permission or else the caller
1376 * must hold an instantiation authorisation token for the key.
1377 *
1378 * The timeout is either 0 to clear the timeout, or a number of seconds from
1379 * the current time. The key and any links to the key will be automatically
1380 * garbage collected after the timeout expires.
1381 *
Mimi Zohard3600bc2015-11-10 08:34:46 -05001382 * Keys with KEY_FLAG_KEEP set should not be timed out.
1383 *
David Howells973c9f42011-01-20 16:38:33 +00001384 * If successful, 0 is returned.
David Howells017679c2006-01-08 01:02:43 -08001385 */
1386long keyctl_set_timeout(key_serial_t id, unsigned timeout)
1387{
David Howells91562352010-06-11 17:31:05 +01001388 struct key *key, *instkey;
David Howells017679c2006-01-08 01:02:43 -08001389 key_ref_t key_ref;
David Howells017679c2006-01-08 01:02:43 -08001390 long ret;
1391
David Howells55931222009-09-02 09:13:45 +01001392 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
David Howellsf5895942014-03-14 17:44:49 +00001393 KEY_NEED_SETATTR);
David Howells017679c2006-01-08 01:02:43 -08001394 if (IS_ERR(key_ref)) {
David Howells91562352010-06-11 17:31:05 +01001395 /* setting the timeout on a key under construction is permitted
1396 * if we have the authorisation token handy */
1397 if (PTR_ERR(key_ref) == -EACCES) {
1398 instkey = key_get_instantiation_authkey(id);
1399 if (!IS_ERR(instkey)) {
1400 key_put(instkey);
1401 key_ref = lookup_user_key(id,
1402 KEY_LOOKUP_PARTIAL,
1403 0);
1404 if (!IS_ERR(key_ref))
1405 goto okay;
1406 }
1407 }
1408
David Howells017679c2006-01-08 01:02:43 -08001409 ret = PTR_ERR(key_ref);
1410 goto error;
1411 }
1412
David Howells91562352010-06-11 17:31:05 +01001413okay:
David Howells017679c2006-01-08 01:02:43 -08001414 key = key_ref_to_ptr(key_ref);
Mimi Zohar1d6d1672016-01-07 07:46:36 -05001415 ret = 0;
Mimi Zohard3600bc2015-11-10 08:34:46 -05001416 if (test_bit(KEY_FLAG_KEEP, &key->flags))
1417 ret = -EPERM;
Mimi Zohar1d6d1672016-01-07 07:46:36 -05001418 else
Mimi Zohard3600bc2015-11-10 08:34:46 -05001419 key_set_timeout(key, timeout);
David Howells017679c2006-01-08 01:02:43 -08001420 key_put(key);
1421
David Howells017679c2006-01-08 01:02:43 -08001422error:
1423 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +00001424}
David Howells017679c2006-01-08 01:02:43 -08001425
David Howells017679c2006-01-08 01:02:43 -08001426/*
David Howells973c9f42011-01-20 16:38:33 +00001427 * Assume (or clear) the authority to instantiate the specified key.
1428 *
1429 * This sets the authoritative token currently in force for key instantiation.
1430 * This must be done for a key to be instantiated. It has the effect of making
1431 * available all the keys from the caller of the request_key() that created a
1432 * key to request_key() calls made by the caller of this function.
1433 *
1434 * The caller must have the instantiation key in their process keyrings with a
1435 * Search permission grant available to the caller.
1436 *
1437 * If the ID given is 0, then the setting will be cleared and 0 returned.
1438 *
1439 * If the ID given has a matching an authorisation key, then that key will be
1440 * set and its ID will be returned. The authorisation key can be read to get
1441 * the callout information passed to request_key().
David Howellsb5f545c2006-01-08 01:02:47 -08001442 */
1443long keyctl_assume_authority(key_serial_t id)
1444{
1445 struct key *authkey;
1446 long ret;
1447
1448 /* special key IDs aren't permitted */
1449 ret = -EINVAL;
1450 if (id < 0)
1451 goto error;
1452
1453 /* we divest ourselves of authority if given an ID of 0 */
1454 if (id == 0) {
David Howellsd84f4f92008-11-14 10:39:23 +11001455 ret = keyctl_change_reqkey_auth(NULL);
David Howellsb5f545c2006-01-08 01:02:47 -08001456 goto error;
1457 }
1458
1459 /* attempt to assume the authority temporarily granted to us whilst we
1460 * instantiate the specified key
1461 * - the authorisation key must be in the current task's keyrings
1462 * somewhere
1463 */
1464 authkey = key_get_instantiation_authkey(id);
1465 if (IS_ERR(authkey)) {
1466 ret = PTR_ERR(authkey);
1467 goto error;
1468 }
1469
David Howellsd84f4f92008-11-14 10:39:23 +11001470 ret = keyctl_change_reqkey_auth(authkey);
Eric Biggers884bee02017-09-18 11:36:12 -07001471 if (ret == 0)
1472 ret = authkey->serial;
David Howellsd84f4f92008-11-14 10:39:23 +11001473 key_put(authkey);
David Howellsb5f545c2006-01-08 01:02:47 -08001474error:
1475 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +00001476}
David Howellsb5f545c2006-01-08 01:02:47 -08001477
David Howells70a5bb72008-04-29 01:01:26 -07001478/*
David Howells973c9f42011-01-20 16:38:33 +00001479 * Get a key's the LSM security label.
1480 *
1481 * The key must grant the caller View permission for this to work.
1482 *
1483 * If there's a buffer, then up to buflen bytes of data will be placed into it.
1484 *
1485 * If successful, the amount of information available will be returned,
1486 * irrespective of how much was copied (including the terminal NUL).
David Howells70a5bb72008-04-29 01:01:26 -07001487 */
1488long keyctl_get_security(key_serial_t keyid,
1489 char __user *buffer,
1490 size_t buflen)
1491{
1492 struct key *key, *instkey;
1493 key_ref_t key_ref;
1494 char *context;
1495 long ret;
1496
David Howellsf5895942014-03-14 17:44:49 +00001497 key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_NEED_VIEW);
David Howells70a5bb72008-04-29 01:01:26 -07001498 if (IS_ERR(key_ref)) {
1499 if (PTR_ERR(key_ref) != -EACCES)
1500 return PTR_ERR(key_ref);
1501
1502 /* viewing a key under construction is also permitted if we
1503 * have the authorisation token handy */
1504 instkey = key_get_instantiation_authkey(keyid);
1505 if (IS_ERR(instkey))
Roel Kluinfa1cc7b2009-12-15 15:05:12 -08001506 return PTR_ERR(instkey);
David Howells70a5bb72008-04-29 01:01:26 -07001507 key_put(instkey);
1508
David Howells55931222009-09-02 09:13:45 +01001509 key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, 0);
David Howells70a5bb72008-04-29 01:01:26 -07001510 if (IS_ERR(key_ref))
1511 return PTR_ERR(key_ref);
1512 }
1513
1514 key = key_ref_to_ptr(key_ref);
1515 ret = security_key_getsecurity(key, &context);
1516 if (ret == 0) {
1517 /* if no information was returned, give userspace an empty
1518 * string */
1519 ret = 1;
1520 if (buffer && buflen > 0 &&
1521 copy_to_user(buffer, "", 1) != 0)
1522 ret = -EFAULT;
1523 } else if (ret > 0) {
1524 /* return as much data as there's room for */
1525 if (buffer && buflen > 0) {
1526 if (buflen > ret)
1527 buflen = ret;
1528
1529 if (copy_to_user(buffer, context, buflen) != 0)
1530 ret = -EFAULT;
1531 }
1532
1533 kfree(context);
1534 }
1535
1536 key_ref_put(key_ref);
1537 return ret;
1538}
1539
David Howellsee18d642009-09-02 09:14:21 +01001540/*
David Howells973c9f42011-01-20 16:38:33 +00001541 * Attempt to install the calling process's session keyring on the process's
1542 * parent process.
1543 *
1544 * The keyring must exist and must grant the caller LINK permission, and the
1545 * parent process must be single-threaded and must have the same effective
1546 * ownership as this process and mustn't be SUID/SGID.
1547 *
1548 * The keyring will be emplaced on the parent when it next resumes userspace.
1549 *
1550 * If successful, 0 will be returned.
David Howellsee18d642009-09-02 09:14:21 +01001551 */
1552long keyctl_session_to_parent(void)
1553{
1554 struct task_struct *me, *parent;
1555 const struct cred *mycred, *pcred;
Al Viro67d12142012-06-27 11:07:19 +04001556 struct callback_head *newwork, *oldwork;
David Howellsee18d642009-09-02 09:14:21 +01001557 key_ref_t keyring_r;
Oleg Nesterov413cd3d2012-05-11 10:59:08 +10001558 struct cred *cred;
David Howellsee18d642009-09-02 09:14:21 +01001559 int ret;
1560
David Howellsf5895942014-03-14 17:44:49 +00001561 keyring_r = lookup_user_key(KEY_SPEC_SESSION_KEYRING, 0, KEY_NEED_LINK);
David Howellsee18d642009-09-02 09:14:21 +01001562 if (IS_ERR(keyring_r))
1563 return PTR_ERR(keyring_r);
1564
Oleg Nesterov413cd3d2012-05-11 10:59:08 +10001565 ret = -ENOMEM;
Oleg Nesterov413cd3d2012-05-11 10:59:08 +10001566
David Howellsee18d642009-09-02 09:14:21 +01001567 /* our parent is going to need a new cred struct, a new tgcred struct
1568 * and new security data, so we allocate them here to prevent ENOMEM in
1569 * our parent */
David Howellsee18d642009-09-02 09:14:21 +01001570 cred = cred_alloc_blank();
1571 if (!cred)
Al Viro67d12142012-06-27 11:07:19 +04001572 goto error_keyring;
1573 newwork = &cred->rcu;
David Howellsee18d642009-09-02 09:14:21 +01001574
David Howells3a505972012-10-02 19:24:29 +01001575 cred->session_keyring = key_ref_to_ptr(keyring_r);
1576 keyring_r = NULL;
Al Viro67d12142012-06-27 11:07:19 +04001577 init_task_work(newwork, key_change_session_keyring);
David Howellsee18d642009-09-02 09:14:21 +01001578
1579 me = current;
David Howells9d1ac652010-09-10 09:59:46 +01001580 rcu_read_lock();
David Howellsee18d642009-09-02 09:14:21 +01001581 write_lock_irq(&tasklist_lock);
1582
David Howellsee18d642009-09-02 09:14:21 +01001583 ret = -EPERM;
Oleg Nesterov413cd3d2012-05-11 10:59:08 +10001584 oldwork = NULL;
David Howells7936d162019-05-22 14:09:29 +01001585 parent = rcu_dereference_protected(me->real_parent,
1586 lockdep_is_held(&tasklist_lock));
David Howellsee18d642009-09-02 09:14:21 +01001587
1588 /* the parent mustn't be init and mustn't be a kernel thread */
1589 if (parent->pid <= 1 || !parent->mm)
Oleg Nesterov413cd3d2012-05-11 10:59:08 +10001590 goto unlock;
David Howellsee18d642009-09-02 09:14:21 +01001591
1592 /* the parent must be single threaded */
Oleg Nesterovdd98acf2010-05-26 14:43:23 -07001593 if (!thread_group_empty(parent))
Oleg Nesterov413cd3d2012-05-11 10:59:08 +10001594 goto unlock;
David Howellsee18d642009-09-02 09:14:21 +01001595
1596 /* the parent and the child must have different session keyrings or
1597 * there's no point */
1598 mycred = current_cred();
1599 pcred = __task_cred(parent);
1600 if (mycred == pcred ||
David Howells3a505972012-10-02 19:24:29 +01001601 mycred->session_keyring == pcred->session_keyring) {
Oleg Nesterov413cd3d2012-05-11 10:59:08 +10001602 ret = 0;
1603 goto unlock;
1604 }
David Howellsee18d642009-09-02 09:14:21 +01001605
1606 /* the parent must have the same effective ownership and mustn't be
1607 * SUID/SGID */
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -08001608 if (!uid_eq(pcred->uid, mycred->euid) ||
1609 !uid_eq(pcred->euid, mycred->euid) ||
1610 !uid_eq(pcred->suid, mycred->euid) ||
1611 !gid_eq(pcred->gid, mycred->egid) ||
1612 !gid_eq(pcred->egid, mycred->egid) ||
1613 !gid_eq(pcred->sgid, mycred->egid))
Oleg Nesterov413cd3d2012-05-11 10:59:08 +10001614 goto unlock;
David Howellsee18d642009-09-02 09:14:21 +01001615
1616 /* the keyrings must have the same UID */
David Howells3a505972012-10-02 19:24:29 +01001617 if ((pcred->session_keyring &&
Linus Torvalds2a74dbb2012-12-16 15:40:50 -08001618 !uid_eq(pcred->session_keyring->uid, mycred->euid)) ||
1619 !uid_eq(mycred->session_keyring->uid, mycred->euid))
Oleg Nesterov413cd3d2012-05-11 10:59:08 +10001620 goto unlock;
David Howellsee18d642009-09-02 09:14:21 +01001621
Oleg Nesterov413cd3d2012-05-11 10:59:08 +10001622 /* cancel an already pending keyring replacement */
1623 oldwork = task_work_cancel(parent, key_change_session_keyring);
David Howellsee18d642009-09-02 09:14:21 +01001624
1625 /* the replacement session keyring is applied just prior to userspace
1626 * restarting */
Al Viro67d12142012-06-27 11:07:19 +04001627 ret = task_work_add(parent, newwork, true);
Oleg Nesterov413cd3d2012-05-11 10:59:08 +10001628 if (!ret)
1629 newwork = NULL;
1630unlock:
David Howellsee18d642009-09-02 09:14:21 +01001631 write_unlock_irq(&tasklist_lock);
David Howells9d1ac652010-09-10 09:59:46 +01001632 rcu_read_unlock();
Al Viro67d12142012-06-27 11:07:19 +04001633 if (oldwork)
1634 put_cred(container_of(oldwork, struct cred, rcu));
1635 if (newwork)
1636 put_cred(cred);
David Howellsee18d642009-09-02 09:14:21 +01001637 return ret;
1638
1639error_keyring:
1640 key_ref_put(keyring_r);
1641 return ret;
1642}
1643
David Howellsb5f545c2006-01-08 01:02:47 -08001644/*
Mat Martineau6563c912017-03-01 16:44:09 -08001645 * Apply a restriction to a given keyring.
1646 *
1647 * The caller must have Setattr permission to change keyring restrictions.
1648 *
1649 * The requested type name may be a NULL pointer to reject all attempts
Eric Biggers18026d82017-12-08 15:13:29 +00001650 * to link to the keyring. In this case, _restriction must also be NULL.
1651 * Otherwise, both _type and _restriction must be non-NULL.
Mat Martineau6563c912017-03-01 16:44:09 -08001652 *
1653 * Returns 0 if successful.
1654 */
1655long keyctl_restrict_keyring(key_serial_t id, const char __user *_type,
1656 const char __user *_restriction)
1657{
1658 key_ref_t key_ref;
Mat Martineau6563c912017-03-01 16:44:09 -08001659 char type[32];
1660 char *restriction = NULL;
1661 long ret;
1662
1663 key_ref = lookup_user_key(id, 0, KEY_NEED_SETATTR);
1664 if (IS_ERR(key_ref))
1665 return PTR_ERR(key_ref);
1666
Eric Biggers18026d82017-12-08 15:13:29 +00001667 ret = -EINVAL;
Mat Martineau6563c912017-03-01 16:44:09 -08001668 if (_type) {
Eric Biggers18026d82017-12-08 15:13:29 +00001669 if (!_restriction)
1670 goto error;
1671
Mat Martineau6563c912017-03-01 16:44:09 -08001672 ret = key_get_type_from_user(type, _type, sizeof(type));
1673 if (ret < 0)
1674 goto error;
Mat Martineau6563c912017-03-01 16:44:09 -08001675
1676 restriction = strndup_user(_restriction, PAGE_SIZE);
1677 if (IS_ERR(restriction)) {
1678 ret = PTR_ERR(restriction);
1679 goto error;
1680 }
Eric Biggers18026d82017-12-08 15:13:29 +00001681 } else {
1682 if (_restriction)
1683 goto error;
Mat Martineau6563c912017-03-01 16:44:09 -08001684 }
1685
Eric Biggers18026d82017-12-08 15:13:29 +00001686 ret = keyring_restrict(key_ref, _type ? type : NULL, restriction);
Mat Martineau6563c912017-03-01 16:44:09 -08001687 kfree(restriction);
Mat Martineau6563c912017-03-01 16:44:09 -08001688error:
1689 key_ref_put(key_ref);
Mat Martineau6563c912017-03-01 16:44:09 -08001690 return ret;
1691}
1692
1693/*
David Howells45e0f302019-05-30 14:53:10 +01001694 * Get keyrings subsystem capabilities.
1695 */
1696long keyctl_capabilities(unsigned char __user *_buffer, size_t buflen)
1697{
1698 size_t size = buflen;
1699
1700 if (size > 0) {
1701 if (size > sizeof(keyrings_capabilities))
1702 size = sizeof(keyrings_capabilities);
1703 if (copy_to_user(_buffer, keyrings_capabilities, size) != 0)
1704 return -EFAULT;
1705 if (size < buflen &&
1706 clear_user(_buffer + size, buflen - size) != 0)
1707 return -EFAULT;
1708 }
1709
1710 return sizeof(keyrings_capabilities);
1711}
1712
1713/*
David Howells973c9f42011-01-20 16:38:33 +00001714 * The key control system call
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 */
Heiko Carstens938bb9f2009-01-14 14:14:30 +01001716SYSCALL_DEFINE5(keyctl, int, option, unsigned long, arg2, unsigned long, arg3,
1717 unsigned long, arg4, unsigned long, arg5)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718{
1719 switch (option) {
1720 case KEYCTL_GET_KEYRING_ID:
1721 return keyctl_get_keyring_ID((key_serial_t) arg2,
1722 (int) arg3);
1723
1724 case KEYCTL_JOIN_SESSION_KEYRING:
1725 return keyctl_join_session_keyring((const char __user *) arg2);
1726
1727 case KEYCTL_UPDATE:
1728 return keyctl_update_key((key_serial_t) arg2,
1729 (const void __user *) arg3,
1730 (size_t) arg4);
1731
1732 case KEYCTL_REVOKE:
1733 return keyctl_revoke_key((key_serial_t) arg2);
1734
1735 case KEYCTL_DESCRIBE:
1736 return keyctl_describe_key((key_serial_t) arg2,
1737 (char __user *) arg3,
1738 (unsigned) arg4);
1739
1740 case KEYCTL_CLEAR:
1741 return keyctl_keyring_clear((key_serial_t) arg2);
1742
1743 case KEYCTL_LINK:
1744 return keyctl_keyring_link((key_serial_t) arg2,
1745 (key_serial_t) arg3);
1746
1747 case KEYCTL_UNLINK:
1748 return keyctl_keyring_unlink((key_serial_t) arg2,
1749 (key_serial_t) arg3);
1750
1751 case KEYCTL_SEARCH:
1752 return keyctl_keyring_search((key_serial_t) arg2,
1753 (const char __user *) arg3,
1754 (const char __user *) arg4,
1755 (key_serial_t) arg5);
1756
1757 case KEYCTL_READ:
1758 return keyctl_read_key((key_serial_t) arg2,
1759 (char __user *) arg3,
1760 (size_t) arg4);
1761
1762 case KEYCTL_CHOWN:
1763 return keyctl_chown_key((key_serial_t) arg2,
1764 (uid_t) arg3,
1765 (gid_t) arg4);
1766
1767 case KEYCTL_SETPERM:
1768 return keyctl_setperm_key((key_serial_t) arg2,
1769 (key_perm_t) arg3);
1770
1771 case KEYCTL_INSTANTIATE:
1772 return keyctl_instantiate_key((key_serial_t) arg2,
1773 (const void __user *) arg3,
1774 (size_t) arg4,
1775 (key_serial_t) arg5);
1776
1777 case KEYCTL_NEGATE:
1778 return keyctl_negate_key((key_serial_t) arg2,
1779 (unsigned) arg3,
1780 (key_serial_t) arg4);
1781
David Howells3e301482005-06-23 22:00:56 -07001782 case KEYCTL_SET_REQKEY_KEYRING:
1783 return keyctl_set_reqkey_keyring(arg2);
1784
David Howells017679c2006-01-08 01:02:43 -08001785 case KEYCTL_SET_TIMEOUT:
1786 return keyctl_set_timeout((key_serial_t) arg2,
1787 (unsigned) arg3);
1788
David Howellsb5f545c2006-01-08 01:02:47 -08001789 case KEYCTL_ASSUME_AUTHORITY:
1790 return keyctl_assume_authority((key_serial_t) arg2);
1791
David Howells70a5bb72008-04-29 01:01:26 -07001792 case KEYCTL_GET_SECURITY:
1793 return keyctl_get_security((key_serial_t) arg2,
James Morris90bd49a2008-12-29 14:35:35 +11001794 (char __user *) arg3,
David Howells70a5bb72008-04-29 01:01:26 -07001795 (size_t) arg4);
1796
David Howellsee18d642009-09-02 09:14:21 +01001797 case KEYCTL_SESSION_TO_PARENT:
1798 return keyctl_session_to_parent();
1799
David Howellsfdd1b942011-03-07 15:06:09 +00001800 case KEYCTL_REJECT:
1801 return keyctl_reject_key((key_serial_t) arg2,
1802 (unsigned) arg3,
1803 (unsigned) arg4,
1804 (key_serial_t) arg5);
1805
David Howellsee009e4a02011-03-07 15:06:20 +00001806 case KEYCTL_INSTANTIATE_IOV:
1807 return keyctl_instantiate_key_iov(
1808 (key_serial_t) arg2,
1809 (const struct iovec __user *) arg3,
1810 (unsigned) arg4,
1811 (key_serial_t) arg5);
1812
David Howellsfd758152012-05-11 10:56:56 +01001813 case KEYCTL_INVALIDATE:
1814 return keyctl_invalidate_key((key_serial_t) arg2);
1815
David Howellsf36f8c72013-09-24 10:35:19 +01001816 case KEYCTL_GET_PERSISTENT:
1817 return keyctl_get_persistent((uid_t)arg2, (key_serial_t)arg3);
1818
Mat Martineauddbb4112016-04-12 19:54:58 +01001819 case KEYCTL_DH_COMPUTE:
1820 return keyctl_dh_compute((struct keyctl_dh_params __user *) arg2,
Stephan Mueller4693fc72016-05-26 23:38:12 +02001821 (char __user *) arg3, (size_t) arg4,
Stephan Muellerf1c316a2016-08-19 20:39:09 +02001822 (struct keyctl_kdf_params __user *) arg5);
Mat Martineauddbb4112016-04-12 19:54:58 +01001823
Mat Martineau6563c912017-03-01 16:44:09 -08001824 case KEYCTL_RESTRICT_KEYRING:
1825 return keyctl_restrict_keyring((key_serial_t) arg2,
1826 (const char __user *) arg3,
1827 (const char __user *) arg4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828
David Howells00d60fd2018-10-09 17:46:59 +01001829 case KEYCTL_PKEY_QUERY:
1830 if (arg3 != 0)
1831 return -EINVAL;
1832 return keyctl_pkey_query((key_serial_t)arg2,
1833 (const char __user *)arg4,
Ben Dooks468e91c2019-03-01 11:30:26 +00001834 (struct keyctl_pkey_query __user *)arg5);
David Howells00d60fd2018-10-09 17:46:59 +01001835
1836 case KEYCTL_PKEY_ENCRYPT:
1837 case KEYCTL_PKEY_DECRYPT:
1838 case KEYCTL_PKEY_SIGN:
1839 return keyctl_pkey_e_d_s(
1840 option,
1841 (const struct keyctl_pkey_params __user *)arg2,
1842 (const char __user *)arg3,
1843 (const void __user *)arg4,
1844 (void __user *)arg5);
1845
1846 case KEYCTL_PKEY_VERIFY:
1847 return keyctl_pkey_verify(
1848 (const struct keyctl_pkey_params __user *)arg2,
1849 (const char __user *)arg3,
1850 (const void __user *)arg4,
1851 (const void __user *)arg5);
1852
David Howellsed0ac5c2019-05-20 21:51:50 +01001853 case KEYCTL_MOVE:
1854 return keyctl_keyring_move((key_serial_t)arg2,
1855 (key_serial_t)arg3,
1856 (key_serial_t)arg4,
1857 (unsigned int)arg5);
1858
David Howells45e0f302019-05-30 14:53:10 +01001859 case KEYCTL_CAPABILITIES:
1860 return keyctl_capabilities((unsigned char __user *)arg2, (size_t)arg3);
1861
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 default:
1863 return -EOPNOTSUPP;
1864 }
1865}