blob: bcb0b597c39187b1906fc732b612234c0a23397a [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
12#include <linux/module.h>
13#include <linux/init.h>
14#include <linux/sched.h>
15#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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "internal.h"
29
David Howellsaa9d4432014-12-01 22:52:45 +000030#define KEY_MAX_DESC_SIZE 4096
31
Davi Arnaut0cb409d2006-03-24 03:18:43 -080032static int key_get_type_from_user(char *type,
33 const char __user *_type,
34 unsigned len)
35{
36 int ret;
37
38 ret = strncpy_from_user(type, _type, len);
Davi Arnaut0cb409d2006-03-24 03:18:43 -080039 if (ret < 0)
Dan Carpenter4303ef12010-06-11 17:30:05 +010040 return ret;
Davi Arnaut0cb409d2006-03-24 03:18:43 -080041 if (ret == 0 || ret >= len)
42 return -EINVAL;
David Howells54e2c2c2014-09-16 17:29:03 +010043 if (type[0] == '.')
44 return -EPERM;
Davi Arnaut0cb409d2006-03-24 03:18:43 -080045 type[len - 1] = '\0';
Davi Arnaut0cb409d2006-03-24 03:18:43 -080046 return 0;
47}
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049/*
David Howells973c9f42011-01-20 16:38:33 +000050 * Extract the description of a new key from userspace and either add it as a
51 * new key to the specified keyring or update a matching key in that keyring.
52 *
David Howellscf7f6012012-09-13 13:06:29 +010053 * If the description is NULL or an empty string, the key type is asked to
54 * generate one from the payload.
55 *
David Howells973c9f42011-01-20 16:38:33 +000056 * The keyring must be writable so that we can attach the key to it.
57 *
58 * If successful, the new key's serial number is returned, otherwise an error
59 * code is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 */
Heiko Carstens1e7bfb22009-01-14 14:14:29 +010061SYSCALL_DEFINE5(add_key, const char __user *, _type,
62 const char __user *, _description,
63 const void __user *, _payload,
64 size_t, plen,
65 key_serial_t, ringid)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
David Howells664cceb2005-09-28 17:03:15 +010067 key_ref_t keyring_ref, key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 char type[32], *description;
69 void *payload;
Davi Arnaut0cb409d2006-03-24 03:18:43 -080070 long ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72 ret = -EINVAL;
David Howells38bbca62008-04-29 01:01:19 -070073 if (plen > 1024 * 1024 - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 goto error;
75
76 /* draw all the data into kernel space */
Davi Arnaut0cb409d2006-03-24 03:18:43 -080077 ret = key_get_type_from_user(type, _type, sizeof(type));
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 if (ret < 0)
79 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
David Howellscf7f6012012-09-13 13:06:29 +010081 description = NULL;
82 if (_description) {
David Howellsaa9d4432014-12-01 22:52:45 +000083 description = strndup_user(_description, KEY_MAX_DESC_SIZE);
David Howellscf7f6012012-09-13 13:06:29 +010084 if (IS_ERR(description)) {
85 ret = PTR_ERR(description);
86 goto error;
87 }
88 if (!*description) {
89 kfree(description);
90 description = NULL;
Mimi Zohara4e3b8d2014-05-22 14:02:23 -040091 } else if ((description[0] == '.') &&
92 (strncmp(type, "keyring", 7) == 0)) {
93 ret = -EPERM;
94 goto error2;
David Howellscf7f6012012-09-13 13:06:29 +010095 }
Davi Arnaut0cb409d2006-03-24 03:18:43 -080096 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98 /* pull the payload in if one was supplied */
99 payload = NULL;
100
101 if (_payload) {
102 ret = -ENOMEM;
Andrew Morton4f1c28d2012-05-31 16:26:02 -0700103 payload = kmalloc(plen, GFP_KERNEL | __GFP_NOWARN);
David Howells38bbca62008-04-29 01:01:19 -0700104 if (!payload) {
105 if (plen <= PAGE_SIZE)
106 goto error2;
David Howells38bbca62008-04-29 01:01:19 -0700107 payload = vmalloc(plen);
108 if (!payload)
109 goto error2;
110 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112 ret = -EFAULT;
113 if (copy_from_user(payload, _payload, plen) != 0)
114 goto error3;
115 }
116
117 /* find the target keyring (which must be writable) */
David Howellsf5895942014-03-14 17:44:49 +0000118 keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
David Howells664cceb2005-09-28 17:03:15 +0100119 if (IS_ERR(keyring_ref)) {
120 ret = PTR_ERR(keyring_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 goto error3;
122 }
123
124 /* create or update the requested key and add it to the target
125 * keyring */
David Howells664cceb2005-09-28 17:03:15 +0100126 key_ref = key_create_or_update(keyring_ref, type, description,
Arun Raghavan6b79ccb2008-04-29 01:01:28 -0700127 payload, plen, KEY_PERM_UNDEF,
128 KEY_ALLOC_IN_QUOTA);
David Howells664cceb2005-09-28 17:03:15 +0100129 if (!IS_ERR(key_ref)) {
130 ret = key_ref_to_ptr(key_ref)->serial;
131 key_ref_put(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 }
133 else {
David Howells664cceb2005-09-28 17:03:15 +0100134 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 }
136
David Howells664cceb2005-09-28 17:03:15 +0100137 key_ref_put(keyring_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 error3:
Geliang Tangd0e0eba2015-10-21 14:04:46 +0100139 kvfree(payload);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 error2:
141 kfree(description);
142 error:
143 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000144}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146/*
David Howells973c9f42011-01-20 16:38:33 +0000147 * Search the process keyrings and keyring trees linked from those for a
148 * matching key. Keyrings must have appropriate Search permission to be
149 * searched.
150 *
151 * If a key is found, it will be attached to the destination keyring if there's
152 * one specified and the serial number of the key will be returned.
153 *
154 * If no key is found, /sbin/request-key will be invoked if _callout_info is
155 * non-NULL in an attempt to create a key. The _callout_info string will be
156 * passed to /sbin/request-key to aid with completing the request. If the
157 * _callout_info string is "" then it will be changed to "-".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 */
Heiko Carstens1e7bfb22009-01-14 14:14:29 +0100159SYSCALL_DEFINE4(request_key, const char __user *, _type,
160 const char __user *, _description,
161 const char __user *, _callout_info,
162 key_serial_t, destringid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
164 struct key_type *ktype;
David Howells664cceb2005-09-28 17:03:15 +0100165 struct key *key;
166 key_ref_t dest_ref;
David Howells4a38e122008-04-29 01:01:24 -0700167 size_t callout_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 char type[32], *description, *callout_info;
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800169 long ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171 /* pull the type into kernel space */
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800172 ret = key_get_type_from_user(type, _type, sizeof(type));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 if (ret < 0)
174 goto error;
David Howells1260f802005-08-04 11:50:01 +0100175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 /* pull the description into kernel space */
David Howellsaa9d4432014-12-01 22:52:45 +0000177 description = strndup_user(_description, KEY_MAX_DESC_SIZE);
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800178 if (IS_ERR(description)) {
179 ret = PTR_ERR(description);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 goto error;
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800181 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183 /* pull the callout info into kernel space */
184 callout_info = NULL;
David Howells4a38e122008-04-29 01:01:24 -0700185 callout_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 if (_callout_info) {
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800187 callout_info = strndup_user(_callout_info, PAGE_SIZE);
188 if (IS_ERR(callout_info)) {
189 ret = PTR_ERR(callout_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 goto error2;
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800191 }
David Howells4a38e122008-04-29 01:01:24 -0700192 callout_len = strlen(callout_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 }
194
195 /* get the destination keyring if specified */
David Howells664cceb2005-09-28 17:03:15 +0100196 dest_ref = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 if (destringid) {
David Howells55931222009-09-02 09:13:45 +0100198 dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE,
David Howellsf5895942014-03-14 17:44:49 +0000199 KEY_NEED_WRITE);
David Howells664cceb2005-09-28 17:03:15 +0100200 if (IS_ERR(dest_ref)) {
201 ret = PTR_ERR(dest_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 goto error3;
203 }
204 }
205
206 /* find the key type */
207 ktype = key_type_lookup(type);
208 if (IS_ERR(ktype)) {
209 ret = PTR_ERR(ktype);
210 goto error4;
211 }
212
213 /* do the search */
David Howells4a38e122008-04-29 01:01:24 -0700214 key = request_key_and_link(ktype, description, callout_info,
215 callout_len, NULL, key_ref_to_ptr(dest_ref),
David Howells7e047ef2006-06-26 00:24:50 -0700216 KEY_ALLOC_IN_QUOTA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 if (IS_ERR(key)) {
218 ret = PTR_ERR(key);
219 goto error5;
220 }
221
David Howells4aab1e82011-03-11 17:57:33 +0000222 /* wait for the key to finish being constructed */
223 ret = wait_for_key_construction(key, 1);
224 if (ret < 0)
225 goto error6;
226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 ret = key->serial;
228
David Howells4aab1e82011-03-11 17:57:33 +0000229error6:
David Howells3e301482005-06-23 22:00:56 -0700230 key_put(key);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700231error5:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 key_type_put(ktype);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700233error4:
David Howells664cceb2005-09-28 17:03:15 +0100234 key_ref_put(dest_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700235error3:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 kfree(callout_info);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700237error2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 kfree(description);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700239error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000241}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243/*
David Howells973c9f42011-01-20 16:38:33 +0000244 * Get the ID of the specified process keyring.
245 *
246 * The requested keyring must have search permission to be found.
247 *
248 * If successful, the ID of the requested keyring will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 */
250long keyctl_get_keyring_ID(key_serial_t id, int create)
251{
David Howells664cceb2005-09-28 17:03:15 +0100252 key_ref_t key_ref;
David Howells55931222009-09-02 09:13:45 +0100253 unsigned long lflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 long ret;
255
David Howells55931222009-09-02 09:13:45 +0100256 lflags = create ? KEY_LOOKUP_CREATE : 0;
David Howellsf5895942014-03-14 17:44:49 +0000257 key_ref = lookup_user_key(id, lflags, KEY_NEED_SEARCH);
David Howells664cceb2005-09-28 17:03:15 +0100258 if (IS_ERR(key_ref)) {
259 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 goto error;
261 }
262
David Howells664cceb2005-09-28 17:03:15 +0100263 ret = key_ref_to_ptr(key_ref)->serial;
264 key_ref_put(key_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700265error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 return ret;
David Howells973c9f42011-01-20 16:38:33 +0000267}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269/*
David Howells973c9f42011-01-20 16:38:33 +0000270 * Join a (named) session keyring.
271 *
272 * Create and join an anonymous session keyring or join a named session
273 * keyring, creating it if necessary. A named session keyring must have Search
274 * permission for it to be joined. Session keyrings without this permit will
275 * be skipped over.
276 *
277 * If successful, the ID of the joined session keyring will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 */
279long keyctl_join_session_keyring(const char __user *_name)
280{
281 char *name;
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800282 long ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284 /* fetch the name from userspace */
285 name = NULL;
286 if (_name) {
David Howellsaa9d4432014-12-01 22:52:45 +0000287 name = strndup_user(_name, KEY_MAX_DESC_SIZE);
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800288 if (IS_ERR(name)) {
289 ret = PTR_ERR(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 goto error;
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800291 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 }
293
294 /* join the session */
295 ret = join_session_keyring(name);
Vegard Nossum0d54ee12009-01-17 17:45:45 +0100296 kfree(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700298error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000300}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302/*
David Howells973c9f42011-01-20 16:38:33 +0000303 * Update a key's data payload from the given data.
304 *
305 * The key must grant the caller Write permission and the key type must support
306 * updating for this to work. A negative key can be positively instantiated
307 * with this call.
308 *
309 * If successful, 0 will be returned. If the key type does not support
310 * updating, then -EOPNOTSUPP will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 */
312long keyctl_update_key(key_serial_t id,
313 const void __user *_payload,
314 size_t plen)
315{
David Howells664cceb2005-09-28 17:03:15 +0100316 key_ref_t key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 void *payload;
318 long ret;
319
320 ret = -EINVAL;
321 if (plen > PAGE_SIZE)
322 goto error;
323
324 /* pull the payload in if one was supplied */
325 payload = NULL;
326 if (_payload) {
327 ret = -ENOMEM;
328 payload = kmalloc(plen, GFP_KERNEL);
329 if (!payload)
330 goto error;
331
332 ret = -EFAULT;
333 if (copy_from_user(payload, _payload, plen) != 0)
334 goto error2;
335 }
336
337 /* find the target key (which must be writable) */
David Howellsf5895942014-03-14 17:44:49 +0000338 key_ref = lookup_user_key(id, 0, KEY_NEED_WRITE);
David Howells664cceb2005-09-28 17:03:15 +0100339 if (IS_ERR(key_ref)) {
340 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 goto error2;
342 }
343
344 /* update the key */
David Howells664cceb2005-09-28 17:03:15 +0100345 ret = key_update(key_ref, payload, plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
David Howells664cceb2005-09-28 17:03:15 +0100347 key_ref_put(key_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700348error2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 kfree(payload);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700350error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000352}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354/*
David Howells973c9f42011-01-20 16:38:33 +0000355 * Revoke a key.
356 *
357 * The key must be grant the caller Write or Setattr permission for this to
358 * work. The key type should give up its quota claim when revoked. The key
359 * and any links to the key will be automatically garbage collected after a
360 * certain amount of time (/proc/sys/kernel/keys/gc_delay).
361 *
Mimi Zohard3600bc2015-11-10 08:34:46 -0500362 * Keys with KEY_FLAG_KEEP set should not be revoked.
363 *
David Howells973c9f42011-01-20 16:38:33 +0000364 * If successful, 0 is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 */
366long keyctl_revoke_key(key_serial_t id)
367{
David Howells664cceb2005-09-28 17:03:15 +0100368 key_ref_t key_ref;
Mimi Zohard3600bc2015-11-10 08:34:46 -0500369 struct key *key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 long ret;
371
David Howellsf5895942014-03-14 17:44:49 +0000372 key_ref = lookup_user_key(id, 0, KEY_NEED_WRITE);
David Howells664cceb2005-09-28 17:03:15 +0100373 if (IS_ERR(key_ref)) {
374 ret = PTR_ERR(key_ref);
David Howells0c2c9a32009-09-02 09:13:50 +0100375 if (ret != -EACCES)
376 goto error;
David Howellsf5895942014-03-14 17:44:49 +0000377 key_ref = lookup_user_key(id, 0, KEY_NEED_SETATTR);
David Howells0c2c9a32009-09-02 09:13:50 +0100378 if (IS_ERR(key_ref)) {
379 ret = PTR_ERR(key_ref);
380 goto error;
381 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 }
383
Mimi Zohard3600bc2015-11-10 08:34:46 -0500384 key = key_ref_to_ptr(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 ret = 0;
Mimi Zohard3600bc2015-11-10 08:34:46 -0500386 if (test_bit(KEY_FLAG_KEEP, &key->flags))
Mimi Zohar1d6d1672016-01-07 07:46:36 -0500387 ret = -EPERM;
388 else
Mimi Zohard3600bc2015-11-10 08:34:46 -0500389 key_revoke(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
David Howells664cceb2005-09-28 17:03:15 +0100391 key_ref_put(key_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700392error:
David Howells1260f802005-08-04 11:50:01 +0100393 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000394}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396/*
David Howellsfd758152012-05-11 10:56:56 +0100397 * Invalidate a key.
398 *
399 * The key must be grant the caller Invalidate permission for this to work.
400 * The key and any links to the key will be automatically garbage collected
401 * immediately.
402 *
Mimi Zohard3600bc2015-11-10 08:34:46 -0500403 * Keys with KEY_FLAG_KEEP set should not be invalidated.
404 *
David Howellsfd758152012-05-11 10:56:56 +0100405 * If successful, 0 is returned.
406 */
407long keyctl_invalidate_key(key_serial_t id)
408{
409 key_ref_t key_ref;
Mimi Zohard3600bc2015-11-10 08:34:46 -0500410 struct key *key;
David Howellsfd758152012-05-11 10:56:56 +0100411 long ret;
412
413 kenter("%d", id);
414
David Howellsf5895942014-03-14 17:44:49 +0000415 key_ref = lookup_user_key(id, 0, KEY_NEED_SEARCH);
David Howellsfd758152012-05-11 10:56:56 +0100416 if (IS_ERR(key_ref)) {
417 ret = PTR_ERR(key_ref);
David Howells0c7774a2014-07-17 20:45:08 +0100418
419 /* Root is permitted to invalidate certain special keys */
420 if (capable(CAP_SYS_ADMIN)) {
421 key_ref = lookup_user_key(id, 0, 0);
422 if (IS_ERR(key_ref))
423 goto error;
424 if (test_bit(KEY_FLAG_ROOT_CAN_INVAL,
425 &key_ref_to_ptr(key_ref)->flags))
426 goto invalidate;
427 goto error_put;
428 }
429
David Howellsfd758152012-05-11 10:56:56 +0100430 goto error;
431 }
432
David Howells0c7774a2014-07-17 20:45:08 +0100433invalidate:
Mimi Zohard3600bc2015-11-10 08:34:46 -0500434 key = key_ref_to_ptr(key_ref);
David Howellsfd758152012-05-11 10:56:56 +0100435 ret = 0;
Mimi Zohard3600bc2015-11-10 08:34:46 -0500436 if (test_bit(KEY_FLAG_KEEP, &key->flags))
437 ret = -EPERM;
Mimi Zohar1d6d1672016-01-07 07:46:36 -0500438 else
Mimi Zohard3600bc2015-11-10 08:34:46 -0500439 key_invalidate(key);
David Howells0c7774a2014-07-17 20:45:08 +0100440error_put:
David Howellsfd758152012-05-11 10:56:56 +0100441 key_ref_put(key_ref);
442error:
443 kleave(" = %ld", ret);
444 return ret;
445}
446
447/*
David Howells973c9f42011-01-20 16:38:33 +0000448 * Clear the specified keyring, creating an empty process keyring if one of the
449 * special keyring IDs is used.
450 *
Mimi Zohard3600bc2015-11-10 08:34:46 -0500451 * The keyring must grant the caller Write permission and not have
452 * KEY_FLAG_KEEP set for this to work. If successful, 0 will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 */
454long keyctl_keyring_clear(key_serial_t ringid)
455{
David Howells664cceb2005-09-28 17:03:15 +0100456 key_ref_t keyring_ref;
Mimi Zohard3600bc2015-11-10 08:34:46 -0500457 struct key *keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 long ret;
459
David Howellsf5895942014-03-14 17:44:49 +0000460 keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
David Howells664cceb2005-09-28 17:03:15 +0100461 if (IS_ERR(keyring_ref)) {
462 ret = PTR_ERR(keyring_ref);
David Howells700920e2012-01-18 15:31:45 +0000463
464 /* Root is permitted to invalidate certain special keyrings */
465 if (capable(CAP_SYS_ADMIN)) {
466 keyring_ref = lookup_user_key(ringid, 0, 0);
467 if (IS_ERR(keyring_ref))
468 goto error;
469 if (test_bit(KEY_FLAG_ROOT_CAN_CLEAR,
470 &key_ref_to_ptr(keyring_ref)->flags))
471 goto clear;
472 goto error_put;
473 }
474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 goto error;
476 }
477
David Howells700920e2012-01-18 15:31:45 +0000478clear:
Mimi Zohard3600bc2015-11-10 08:34:46 -0500479 keyring = key_ref_to_ptr(keyring_ref);
480 if (test_bit(KEY_FLAG_KEEP, &keyring->flags))
481 ret = -EPERM;
482 else
483 ret = keyring_clear(keyring);
David Howells700920e2012-01-18 15:31:45 +0000484error_put:
David Howells664cceb2005-09-28 17:03:15 +0100485 key_ref_put(keyring_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700486error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000488}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490/*
David Howells973c9f42011-01-20 16:38:33 +0000491 * Create a link from a keyring to a key if there's no matching key in the
492 * keyring, otherwise replace the link to the matching key with a link to the
493 * new key.
494 *
495 * The key must grant the caller Link permission and the the keyring must grant
496 * the caller Write permission. Furthermore, if an additional link is created,
497 * the keyring's quota will be extended.
498 *
499 * If successful, 0 will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 */
501long keyctl_keyring_link(key_serial_t id, key_serial_t ringid)
502{
David Howells664cceb2005-09-28 17:03:15 +0100503 key_ref_t keyring_ref, key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 long ret;
505
David Howellsf5895942014-03-14 17:44:49 +0000506 keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
David Howells664cceb2005-09-28 17:03:15 +0100507 if (IS_ERR(keyring_ref)) {
508 ret = PTR_ERR(keyring_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 goto error;
510 }
511
David Howellsf5895942014-03-14 17:44:49 +0000512 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE, KEY_NEED_LINK);
David Howells664cceb2005-09-28 17:03:15 +0100513 if (IS_ERR(key_ref)) {
514 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 goto error2;
516 }
517
David Howells664cceb2005-09-28 17:03:15 +0100518 ret = key_link(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
David Howells664cceb2005-09-28 17:03:15 +0100520 key_ref_put(key_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700521error2:
David Howells664cceb2005-09-28 17:03:15 +0100522 key_ref_put(keyring_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700523error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000525}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527/*
David Howells973c9f42011-01-20 16:38:33 +0000528 * Unlink a key from a keyring.
529 *
530 * The keyring must grant the caller Write permission for this to work; the key
531 * itself need not grant the caller anything. If the last link to a key is
532 * removed then that key will be scheduled for destruction.
533 *
Mimi Zohard3600bc2015-11-10 08:34:46 -0500534 * Keys or keyrings with KEY_FLAG_KEEP set should not be unlinked.
535 *
David Howells973c9f42011-01-20 16:38:33 +0000536 * If successful, 0 will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 */
538long keyctl_keyring_unlink(key_serial_t id, key_serial_t ringid)
539{
David Howells664cceb2005-09-28 17:03:15 +0100540 key_ref_t keyring_ref, key_ref;
Mimi Zohard3600bc2015-11-10 08:34:46 -0500541 struct key *keyring, *key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 long ret;
543
David Howellsf5895942014-03-14 17:44:49 +0000544 keyring_ref = lookup_user_key(ringid, 0, KEY_NEED_WRITE);
David Howells664cceb2005-09-28 17:03:15 +0100545 if (IS_ERR(keyring_ref)) {
546 ret = PTR_ERR(keyring_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 goto error;
548 }
549
David Howells55931222009-09-02 09:13:45 +0100550 key_ref = lookup_user_key(id, KEY_LOOKUP_FOR_UNLINK, 0);
David Howells664cceb2005-09-28 17:03:15 +0100551 if (IS_ERR(key_ref)) {
552 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 goto error2;
554 }
555
Mimi Zohard3600bc2015-11-10 08:34:46 -0500556 keyring = key_ref_to_ptr(keyring_ref);
557 key = key_ref_to_ptr(key_ref);
558 if (test_bit(KEY_FLAG_KEEP, &keyring->flags) &&
559 test_bit(KEY_FLAG_KEEP, &key->flags))
560 ret = -EPERM;
561 else
562 ret = key_unlink(keyring, key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
David Howells664cceb2005-09-28 17:03:15 +0100564 key_ref_put(key_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700565error2:
David Howells664cceb2005-09-28 17:03:15 +0100566 key_ref_put(keyring_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700567error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000569}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571/*
David Howells973c9f42011-01-20 16:38:33 +0000572 * Return a description of a key to userspace.
573 *
574 * The key must grant the caller View permission for this to work.
575 *
576 * If there's a buffer, we place up to buflen bytes of data into it formatted
577 * in the following way:
578 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 * type;uid;gid;perm;description<NUL>
David Howells973c9f42011-01-20 16:38:33 +0000580 *
581 * If successful, we return the amount of description available, irrespective
582 * of how much we may have copied into the buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 */
584long keyctl_describe_key(key_serial_t keyid,
585 char __user *buffer,
586 size_t buflen)
587{
David Howells3e301482005-06-23 22:00:56 -0700588 struct key *key, *instkey;
David Howells664cceb2005-09-28 17:03:15 +0100589 key_ref_t key_ref;
David Howellsaa9d4432014-12-01 22:52:45 +0000590 char *infobuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 long ret;
David Howellsaa9d4432014-12-01 22:52:45 +0000592 int desclen, infolen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
David Howellsf5895942014-03-14 17:44:49 +0000594 key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_NEED_VIEW);
David Howells664cceb2005-09-28 17:03:15 +0100595 if (IS_ERR(key_ref)) {
David Howells3e301482005-06-23 22:00:56 -0700596 /* viewing a key under construction is permitted if we have the
597 * authorisation token handy */
David Howells664cceb2005-09-28 17:03:15 +0100598 if (PTR_ERR(key_ref) == -EACCES) {
David Howells3e301482005-06-23 22:00:56 -0700599 instkey = key_get_instantiation_authkey(keyid);
600 if (!IS_ERR(instkey)) {
601 key_put(instkey);
David Howells8bbf49762008-11-14 10:39:14 +1100602 key_ref = lookup_user_key(keyid,
David Howells55931222009-09-02 09:13:45 +0100603 KEY_LOOKUP_PARTIAL,
604 0);
David Howells664cceb2005-09-28 17:03:15 +0100605 if (!IS_ERR(key_ref))
David Howells3e301482005-06-23 22:00:56 -0700606 goto okay;
607 }
608 }
609
David Howells664cceb2005-09-28 17:03:15 +0100610 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 goto error;
612 }
613
David Howells3e301482005-06-23 22:00:56 -0700614okay:
David Howells664cceb2005-09-28 17:03:15 +0100615 key = key_ref_to_ptr(key_ref);
David Howellsaa9d4432014-12-01 22:52:45 +0000616 desclen = strlen(key->description);
David Howells664cceb2005-09-28 17:03:15 +0100617
David Howellsaa9d4432014-12-01 22:52:45 +0000618 /* calculate how much information we're going to return */
619 ret = -ENOMEM;
620 infobuf = kasprintf(GFP_KERNEL,
621 "%s;%d;%d;%08x;",
622 key->type->name,
623 from_kuid_munged(current_user_ns(), key->uid),
624 from_kgid_munged(current_user_ns(), key->gid),
625 key->perm);
626 if (!infobuf)
627 goto error2;
628 infolen = strlen(infobuf);
629 ret = infolen + desclen + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
631 /* consider returning the data */
David Howellsaa9d4432014-12-01 22:52:45 +0000632 if (buffer && buflen >= ret) {
633 if (copy_to_user(buffer, infobuf, infolen) != 0 ||
634 copy_to_user(buffer + infolen, key->description,
635 desclen + 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 ret = -EFAULT;
637 }
638
David Howellsaa9d4432014-12-01 22:52:45 +0000639 kfree(infobuf);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700640error2:
David Howells664cceb2005-09-28 17:03:15 +0100641 key_ref_put(key_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700642error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000644}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646/*
David Howells973c9f42011-01-20 16:38:33 +0000647 * Search the specified keyring and any keyrings it links to for a matching
648 * key. Only keyrings that grant the caller Search permission will be searched
649 * (this includes the starting keyring). Only keys with Search permission can
650 * be found.
651 *
652 * If successful, the found key will be linked to the destination keyring if
653 * supplied and the key has Link permission, and the found key ID will be
654 * returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 */
656long keyctl_keyring_search(key_serial_t ringid,
657 const char __user *_type,
658 const char __user *_description,
659 key_serial_t destringid)
660{
661 struct key_type *ktype;
David Howells664cceb2005-09-28 17:03:15 +0100662 key_ref_t keyring_ref, key_ref, dest_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 char type[32], *description;
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800664 long ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
666 /* pull the type and description into kernel space */
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800667 ret = key_get_type_from_user(type, _type, sizeof(type));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 if (ret < 0)
669 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
David Howellsaa9d4432014-12-01 22:52:45 +0000671 description = strndup_user(_description, KEY_MAX_DESC_SIZE);
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800672 if (IS_ERR(description)) {
673 ret = PTR_ERR(description);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 goto error;
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800675 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
677 /* get the keyring at which to begin the search */
David Howellsf5895942014-03-14 17:44:49 +0000678 keyring_ref = lookup_user_key(ringid, 0, KEY_NEED_SEARCH);
David Howells664cceb2005-09-28 17:03:15 +0100679 if (IS_ERR(keyring_ref)) {
680 ret = PTR_ERR(keyring_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 goto error2;
682 }
683
684 /* get the destination keyring if specified */
David Howells664cceb2005-09-28 17:03:15 +0100685 dest_ref = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 if (destringid) {
David Howells55931222009-09-02 09:13:45 +0100687 dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE,
David Howellsf5895942014-03-14 17:44:49 +0000688 KEY_NEED_WRITE);
David Howells664cceb2005-09-28 17:03:15 +0100689 if (IS_ERR(dest_ref)) {
690 ret = PTR_ERR(dest_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 goto error3;
692 }
693 }
694
695 /* find the key type */
696 ktype = key_type_lookup(type);
697 if (IS_ERR(ktype)) {
698 ret = PTR_ERR(ktype);
699 goto error4;
700 }
701
702 /* do the search */
David Howells664cceb2005-09-28 17:03:15 +0100703 key_ref = keyring_search(keyring_ref, ktype, description);
704 if (IS_ERR(key_ref)) {
705 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
707 /* treat lack or presence of a negative key the same */
708 if (ret == -EAGAIN)
709 ret = -ENOKEY;
710 goto error5;
711 }
712
713 /* link the resulting key to the destination keyring if we can */
David Howells664cceb2005-09-28 17:03:15 +0100714 if (dest_ref) {
David Howellsf5895942014-03-14 17:44:49 +0000715 ret = key_permission(key_ref, KEY_NEED_LINK);
David Howells29db9192005-10-30 15:02:44 -0800716 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 goto error6;
718
David Howells664cceb2005-09-28 17:03:15 +0100719 ret = key_link(key_ref_to_ptr(dest_ref), key_ref_to_ptr(key_ref));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 if (ret < 0)
721 goto error6;
722 }
723
David Howells664cceb2005-09-28 17:03:15 +0100724 ret = key_ref_to_ptr(key_ref)->serial;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700726error6:
David Howells664cceb2005-09-28 17:03:15 +0100727 key_ref_put(key_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700728error5:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 key_type_put(ktype);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700730error4:
David Howells664cceb2005-09-28 17:03:15 +0100731 key_ref_put(dest_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700732error3:
David Howells664cceb2005-09-28 17:03:15 +0100733 key_ref_put(keyring_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700734error2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 kfree(description);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700736error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000738}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740/*
David Howells973c9f42011-01-20 16:38:33 +0000741 * Read a key's payload.
742 *
743 * The key must either grant the caller Read permission, or it must grant the
744 * caller Search permission when searched for from the process keyrings.
745 *
746 * If successful, we place up to buflen bytes of data into the buffer, if one
747 * is provided, and return the amount of data that is available in the key,
748 * irrespective of how much we copied into the buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 */
750long keyctl_read_key(key_serial_t keyid, char __user *buffer, size_t buflen)
751{
David Howells664cceb2005-09-28 17:03:15 +0100752 struct key *key;
753 key_ref_t key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 long ret;
755
756 /* find the key first */
David Howells55931222009-09-02 09:13:45 +0100757 key_ref = lookup_user_key(keyid, 0, 0);
David Howells664cceb2005-09-28 17:03:15 +0100758 if (IS_ERR(key_ref)) {
759 ret = -ENOKEY;
760 goto error;
761 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
David Howells664cceb2005-09-28 17:03:15 +0100763 key = key_ref_to_ptr(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
David Howells664cceb2005-09-28 17:03:15 +0100765 /* see if we can read it directly */
David Howellsf5895942014-03-14 17:44:49 +0000766 ret = key_permission(key_ref, KEY_NEED_READ);
David Howells29db9192005-10-30 15:02:44 -0800767 if (ret == 0)
David Howells664cceb2005-09-28 17:03:15 +0100768 goto can_read_key;
David Howells29db9192005-10-30 15:02:44 -0800769 if (ret != -EACCES)
770 goto error;
David Howells664cceb2005-09-28 17:03:15 +0100771
772 /* we can't; see if it's searchable from this process's keyrings
773 * - we automatically take account of the fact that it may be
774 * dangling off an instantiation key
775 */
776 if (!is_key_possessed(key_ref)) {
777 ret = -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 goto error2;
779 }
780
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 /* the key is probably readable - now try to read it */
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700782can_read_key:
David Howellsb4a1b4f2015-12-18 01:34:26 +0000783 ret = -EOPNOTSUPP;
784 if (key->type->read) {
785 /* Read the data with the semaphore held (since we might sleep)
786 * to protect against the key being updated or revoked.
787 */
788 down_read(&key->sem);
789 ret = key_validate(key);
790 if (ret == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 ret = key->type->read(key, buffer, buflen);
David Howellsb4a1b4f2015-12-18 01:34:26 +0000792 up_read(&key->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 }
794
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700795error2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 key_put(key);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700797error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000799}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801/*
David Howells973c9f42011-01-20 16:38:33 +0000802 * Change the ownership of a key
803 *
804 * The key must grant the caller Setattr permission for this to work, though
805 * the key need not be fully instantiated yet. For the UID to be changed, or
806 * for the GID to be changed to a group the caller is not a member of, the
807 * caller must have sysadmin capability. If either uid or gid is -1 then that
808 * attribute is not changed.
809 *
810 * If the UID is to be changed, the new user must have sufficient quota to
811 * accept the key. The quota deduction will be removed from the old user to
812 * the new user should the attribute be changed.
813 *
814 * If successful, 0 will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 */
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -0800816long keyctl_chown_key(key_serial_t id, uid_t user, gid_t group)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817{
Fredrik Tolf58016492006-06-26 00:24:51 -0700818 struct key_user *newowner, *zapowner = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 struct key *key;
David Howells664cceb2005-09-28 17:03:15 +0100820 key_ref_t key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 long ret;
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -0800822 kuid_t uid;
823 kgid_t gid;
824
825 uid = make_kuid(current_user_ns(), user);
826 gid = make_kgid(current_user_ns(), group);
827 ret = -EINVAL;
828 if ((user != (uid_t) -1) && !uid_valid(uid))
829 goto error;
830 if ((group != (gid_t) -1) && !gid_valid(gid))
831 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
833 ret = 0;
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -0800834 if (user == (uid_t) -1 && group == (gid_t) -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 goto error;
836
David Howells55931222009-09-02 09:13:45 +0100837 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
David Howellsf5895942014-03-14 17:44:49 +0000838 KEY_NEED_SETATTR);
David Howells664cceb2005-09-28 17:03:15 +0100839 if (IS_ERR(key_ref)) {
840 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 goto error;
842 }
843
David Howells664cceb2005-09-28 17:03:15 +0100844 key = key_ref_to_ptr(key_ref);
845
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 /* make the changes with the locks held to prevent chown/chown races */
847 ret = -EACCES;
848 down_write(&key->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
850 if (!capable(CAP_SYS_ADMIN)) {
851 /* only the sysadmin can chown a key to some other UID */
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -0800852 if (user != (uid_t) -1 && !uid_eq(key->uid, uid))
Fredrik Tolf58016492006-06-26 00:24:51 -0700853 goto error_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
855 /* only the sysadmin can set the key's GID to a group other
856 * than one of those that the current process subscribes to */
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -0800857 if (group != (gid_t) -1 && !gid_eq(gid, key->gid) && !in_group_p(gid))
Fredrik Tolf58016492006-06-26 00:24:51 -0700858 goto error_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 }
860
Fredrik Tolf58016492006-06-26 00:24:51 -0700861 /* change the UID */
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -0800862 if (user != (uid_t) -1 && !uid_eq(uid, key->uid)) {
Fredrik Tolf58016492006-06-26 00:24:51 -0700863 ret = -ENOMEM;
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -0800864 newowner = key_user_lookup(uid);
Fredrik Tolf58016492006-06-26 00:24:51 -0700865 if (!newowner)
866 goto error_put;
867
868 /* transfer the quota burden to the new user */
869 if (test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) {
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -0800870 unsigned maxkeys = uid_eq(uid, GLOBAL_ROOT_UID) ?
David Howells0b77f5b2008-04-29 01:01:32 -0700871 key_quota_root_maxkeys : key_quota_maxkeys;
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -0800872 unsigned maxbytes = uid_eq(uid, GLOBAL_ROOT_UID) ?
David Howells0b77f5b2008-04-29 01:01:32 -0700873 key_quota_root_maxbytes : key_quota_maxbytes;
874
Fredrik Tolf58016492006-06-26 00:24:51 -0700875 spin_lock(&newowner->lock);
David Howells0b77f5b2008-04-29 01:01:32 -0700876 if (newowner->qnkeys + 1 >= maxkeys ||
877 newowner->qnbytes + key->quotalen >= maxbytes ||
878 newowner->qnbytes + key->quotalen <
879 newowner->qnbytes)
Fredrik Tolf58016492006-06-26 00:24:51 -0700880 goto quota_overrun;
881
882 newowner->qnkeys++;
883 newowner->qnbytes += key->quotalen;
884 spin_unlock(&newowner->lock);
885
886 spin_lock(&key->user->lock);
887 key->user->qnkeys--;
888 key->user->qnbytes -= key->quotalen;
889 spin_unlock(&key->user->lock);
890 }
891
892 atomic_dec(&key->user->nkeys);
893 atomic_inc(&newowner->nkeys);
894
895 if (test_bit(KEY_FLAG_INSTANTIATED, &key->flags)) {
896 atomic_dec(&key->user->nikeys);
897 atomic_inc(&newowner->nikeys);
898 }
899
900 zapowner = key->user;
901 key->user = newowner;
902 key->uid = uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 }
904
905 /* change the GID */
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -0800906 if (group != (gid_t) -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 key->gid = gid;
908
909 ret = 0;
910
Fredrik Tolf58016492006-06-26 00:24:51 -0700911error_put:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 up_write(&key->sem);
913 key_put(key);
Fredrik Tolf58016492006-06-26 00:24:51 -0700914 if (zapowner)
915 key_user_put(zapowner);
916error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 return ret;
918
Fredrik Tolf58016492006-06-26 00:24:51 -0700919quota_overrun:
920 spin_unlock(&newowner->lock);
921 zapowner = newowner;
922 ret = -EDQUOT;
923 goto error_put;
David Howellsa8b17ed2011-01-20 16:38:27 +0000924}
Fredrik Tolf58016492006-06-26 00:24:51 -0700925
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926/*
David Howells973c9f42011-01-20 16:38:33 +0000927 * Change the permission mask on a key.
928 *
929 * The key must grant the caller Setattr permission for this to work, though
930 * the key need not be fully instantiated yet. If the caller does not have
931 * sysadmin capability, it may only change the permission on keys that it owns.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 */
933long keyctl_setperm_key(key_serial_t id, key_perm_t perm)
934{
935 struct key *key;
David Howells664cceb2005-09-28 17:03:15 +0100936 key_ref_t key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 long ret;
938
939 ret = -EINVAL;
David Howells664cceb2005-09-28 17:03:15 +0100940 if (perm & ~(KEY_POS_ALL | KEY_USR_ALL | KEY_GRP_ALL | KEY_OTH_ALL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 goto error;
942
David Howells55931222009-09-02 09:13:45 +0100943 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
David Howellsf5895942014-03-14 17:44:49 +0000944 KEY_NEED_SETATTR);
David Howells664cceb2005-09-28 17:03:15 +0100945 if (IS_ERR(key_ref)) {
946 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 goto error;
948 }
949
David Howells664cceb2005-09-28 17:03:15 +0100950 key = key_ref_to_ptr(key_ref);
951
David Howells76d8aea2005-06-23 22:00:49 -0700952 /* make the changes with the locks held to prevent chown/chmod races */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 ret = -EACCES;
954 down_write(&key->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
David Howells76d8aea2005-06-23 22:00:49 -0700956 /* if we're not the sysadmin, we can only change a key that we own */
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -0800957 if (capable(CAP_SYS_ADMIN) || uid_eq(key->uid, current_fsuid())) {
David Howells76d8aea2005-06-23 22:00:49 -0700958 key->perm = perm;
959 ret = 0;
960 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 up_write(&key->sem);
963 key_put(key);
David Howells76d8aea2005-06-23 22:00:49 -0700964error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000966}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
David Howells8bbf49762008-11-14 10:39:14 +1100968/*
David Howells973c9f42011-01-20 16:38:33 +0000969 * Get the destination keyring for instantiation and check that the caller has
970 * Write permission on it.
David Howells8bbf49762008-11-14 10:39:14 +1100971 */
972static long get_instantiation_keyring(key_serial_t ringid,
973 struct request_key_auth *rka,
974 struct key **_dest_keyring)
975{
976 key_ref_t dkref;
977
David Howellseca1bf52008-12-29 00:41:51 +0000978 *_dest_keyring = NULL;
979
David Howells8bbf49762008-11-14 10:39:14 +1100980 /* just return a NULL pointer if we weren't asked to make a link */
David Howellseca1bf52008-12-29 00:41:51 +0000981 if (ringid == 0)
David Howells8bbf49762008-11-14 10:39:14 +1100982 return 0;
David Howells8bbf49762008-11-14 10:39:14 +1100983
984 /* if a specific keyring is nominated by ID, then use that */
985 if (ringid > 0) {
David Howellsf5895942014-03-14 17:44:49 +0000986 dkref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
David Howells8bbf49762008-11-14 10:39:14 +1100987 if (IS_ERR(dkref))
988 return PTR_ERR(dkref);
989 *_dest_keyring = key_ref_to_ptr(dkref);
990 return 0;
991 }
992
993 if (ringid == KEY_SPEC_REQKEY_AUTH_KEY)
994 return -EINVAL;
995
996 /* otherwise specify the destination keyring recorded in the
997 * authorisation key (any KEY_SPEC_*_KEYRING) */
998 if (ringid >= KEY_SPEC_REQUESTOR_KEYRING) {
David Howells21279cf2009-10-15 10:14:35 +0100999 *_dest_keyring = key_get(rka->dest_keyring);
David Howells8bbf49762008-11-14 10:39:14 +11001000 return 0;
1001 }
1002
1003 return -ENOKEY;
1004}
1005
David Howellsd84f4f92008-11-14 10:39:23 +11001006/*
David Howells973c9f42011-01-20 16:38:33 +00001007 * Change the request_key authorisation key on the current process.
David Howellsd84f4f92008-11-14 10:39:23 +11001008 */
1009static int keyctl_change_reqkey_auth(struct key *key)
1010{
1011 struct cred *new;
1012
1013 new = prepare_creds();
1014 if (!new)
1015 return -ENOMEM;
1016
1017 key_put(new->request_key_auth);
1018 new->request_key_auth = key_get(key);
1019
1020 return commit_creds(new);
1021}
1022
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023/*
David Howells973c9f42011-01-20 16:38:33 +00001024 * Instantiate a key with the specified payload and link the key into the
1025 * destination keyring if one is given.
1026 *
1027 * The caller must have the appropriate instantiation permit set for this to
1028 * work (see keyctl_assume_authority). No other permissions are required.
1029 *
1030 * If successful, 0 will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 */
David Howellsee009e4a02011-03-07 15:06:20 +00001032long keyctl_instantiate_key_common(key_serial_t id,
Al Virob353a1f2015-03-17 09:59:38 -04001033 struct iov_iter *from,
David Howellsee009e4a02011-03-07 15:06:20 +00001034 key_serial_t ringid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035{
David Howellsd84f4f92008-11-14 10:39:23 +11001036 const struct cred *cred = current_cred();
David Howells3e301482005-06-23 22:00:56 -07001037 struct request_key_auth *rka;
David Howells8bbf49762008-11-14 10:39:14 +11001038 struct key *instkey, *dest_keyring;
Al Virob353a1f2015-03-17 09:59:38 -04001039 size_t plen = from ? iov_iter_count(from) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 void *payload;
1041 long ret;
1042
David Howellsd84f4f92008-11-14 10:39:23 +11001043 kenter("%d,,%zu,%d", id, plen, ringid);
1044
Al Virob353a1f2015-03-17 09:59:38 -04001045 if (!plen)
1046 from = NULL;
1047
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 ret = -EINVAL;
David Howells38bbca62008-04-29 01:01:19 -07001049 if (plen > 1024 * 1024 - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 goto error;
1051
David Howellsb5f545c2006-01-08 01:02:47 -08001052 /* the appropriate instantiation authorisation key must have been
1053 * assumed before calling this */
1054 ret = -EPERM;
David Howellsd84f4f92008-11-14 10:39:23 +11001055 instkey = cred->request_key_auth;
David Howellsb5f545c2006-01-08 01:02:47 -08001056 if (!instkey)
1057 goto error;
1058
David Howells146aa8b2015-10-21 14:04:48 +01001059 rka = instkey->payload.data[0];
David Howellsb5f545c2006-01-08 01:02:47 -08001060 if (rka->target_key->serial != id)
1061 goto error;
1062
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 /* pull the payload in if one was supplied */
1064 payload = NULL;
1065
Al Virob353a1f2015-03-17 09:59:38 -04001066 if (from) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 ret = -ENOMEM;
1068 payload = kmalloc(plen, GFP_KERNEL);
David Howells38bbca62008-04-29 01:01:19 -07001069 if (!payload) {
1070 if (plen <= PAGE_SIZE)
1071 goto error;
David Howells38bbca62008-04-29 01:01:19 -07001072 payload = vmalloc(plen);
1073 if (!payload)
1074 goto error;
1075 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
Al Virob353a1f2015-03-17 09:59:38 -04001077 ret = -EFAULT;
Al Virocbbd26b2016-11-01 22:09:04 -04001078 if (!copy_from_iter_full(payload, plen, from))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 goto error2;
1080 }
1081
David Howells3e301482005-06-23 22:00:56 -07001082 /* find the destination keyring amongst those belonging to the
1083 * requesting task */
David Howells8bbf49762008-11-14 10:39:14 +11001084 ret = get_instantiation_keyring(ringid, rka, &dest_keyring);
1085 if (ret < 0)
1086 goto error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
1088 /* instantiate the key and link it into a keyring */
David Howells3e301482005-06-23 22:00:56 -07001089 ret = key_instantiate_and_link(rka->target_key, payload, plen,
David Howells8bbf49762008-11-14 10:39:14 +11001090 dest_keyring, instkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
David Howells8bbf49762008-11-14 10:39:14 +11001092 key_put(dest_keyring);
David Howellsb5f545c2006-01-08 01:02:47 -08001093
1094 /* discard the assumed authority if it's just been disabled by
1095 * instantiation of the key */
David Howellsd84f4f92008-11-14 10:39:23 +11001096 if (ret == 0)
1097 keyctl_change_reqkey_auth(NULL);
David Howellsb5f545c2006-01-08 01:02:47 -08001098
1099error2:
Al Virob353a1f2015-03-17 09:59:38 -04001100 kvfree(payload);
David Howellsb5f545c2006-01-08 01:02:47 -08001101error:
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
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105/*
David Howellsee009e4a02011-03-07 15:06:20 +00001106 * Instantiate a key with the specified payload and link the key into the
1107 * destination keyring if one is given.
1108 *
1109 * The caller must have the appropriate instantiation permit set for this to
1110 * work (see keyctl_assume_authority). No other permissions are required.
1111 *
1112 * If successful, 0 will be returned.
1113 */
1114long keyctl_instantiate_key(key_serial_t id,
1115 const void __user *_payload,
1116 size_t plen,
1117 key_serial_t ringid)
1118{
1119 if (_payload && plen) {
Al Virob353a1f2015-03-17 09:59:38 -04001120 struct iovec iov;
1121 struct iov_iter from;
1122 int ret;
David Howellsee009e4a02011-03-07 15:06:20 +00001123
Al Virob353a1f2015-03-17 09:59:38 -04001124 ret = import_single_range(WRITE, (void __user *)_payload, plen,
1125 &iov, &from);
1126 if (unlikely(ret))
1127 return ret;
1128
1129 return keyctl_instantiate_key_common(id, &from, ringid);
David Howellsee009e4a02011-03-07 15:06:20 +00001130 }
1131
Al Virob353a1f2015-03-17 09:59:38 -04001132 return keyctl_instantiate_key_common(id, NULL, ringid);
David Howellsee009e4a02011-03-07 15:06:20 +00001133}
1134
1135/*
1136 * Instantiate a key with the specified multipart payload and link the key into
1137 * the destination keyring if one is given.
1138 *
1139 * The caller must have the appropriate instantiation permit set for this to
1140 * work (see keyctl_assume_authority). No other permissions are required.
1141 *
1142 * If successful, 0 will be returned.
1143 */
1144long keyctl_instantiate_key_iov(key_serial_t id,
1145 const struct iovec __user *_payload_iov,
1146 unsigned ioc,
1147 key_serial_t ringid)
1148{
1149 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
Al Virob353a1f2015-03-17 09:59:38 -04001150 struct iov_iter from;
David Howellsee009e4a02011-03-07 15:06:20 +00001151 long ret;
1152
Al Virob353a1f2015-03-17 09:59:38 -04001153 if (!_payload_iov)
1154 ioc = 0;
David Howellsee009e4a02011-03-07 15:06:20 +00001155
Al Virob353a1f2015-03-17 09:59:38 -04001156 ret = import_iovec(WRITE, _payload_iov, ioc,
1157 ARRAY_SIZE(iovstack), &iov, &from);
David Howellsee009e4a02011-03-07 15:06:20 +00001158 if (ret < 0)
Al Virob353a1f2015-03-17 09:59:38 -04001159 return ret;
1160 ret = keyctl_instantiate_key_common(id, &from, ringid);
1161 kfree(iov);
David Howellsee009e4a02011-03-07 15:06:20 +00001162 return ret;
David Howellsee009e4a02011-03-07 15:06:20 +00001163}
1164
1165/*
David Howells973c9f42011-01-20 16:38:33 +00001166 * Negatively instantiate the key with the given timeout (in seconds) and link
1167 * the key into the destination keyring if one is given.
1168 *
1169 * The caller must have the appropriate instantiation permit set for this to
1170 * work (see keyctl_assume_authority). No other permissions are required.
1171 *
1172 * The key and any links to the key will be automatically garbage collected
1173 * after the timeout expires.
1174 *
1175 * Negative keys are used to rate limit repeated request_key() calls by causing
1176 * them to return -ENOKEY until the negative key expires.
1177 *
1178 * If successful, 0 will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 */
1180long keyctl_negate_key(key_serial_t id, unsigned timeout, key_serial_t ringid)
1181{
David Howellsfdd1b942011-03-07 15:06:09 +00001182 return keyctl_reject_key(id, timeout, ENOKEY, ringid);
1183}
1184
1185/*
1186 * Negatively instantiate the key with the given timeout (in seconds) and error
1187 * code and link the key into the destination keyring if one is given.
1188 *
1189 * The caller must have the appropriate instantiation permit set for this to
1190 * work (see keyctl_assume_authority). No other permissions are required.
1191 *
1192 * The key and any links to the key will be automatically garbage collected
1193 * after the timeout expires.
1194 *
1195 * Negative keys are used to rate limit repeated request_key() calls by causing
1196 * them to return the specified error code until the negative key expires.
1197 *
1198 * If successful, 0 will be returned.
1199 */
1200long keyctl_reject_key(key_serial_t id, unsigned timeout, unsigned error,
1201 key_serial_t ringid)
1202{
David Howellsd84f4f92008-11-14 10:39:23 +11001203 const struct cred *cred = current_cred();
David Howells3e301482005-06-23 22:00:56 -07001204 struct request_key_auth *rka;
David Howells8bbf49762008-11-14 10:39:14 +11001205 struct key *instkey, *dest_keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 long ret;
1207
David Howellsfdd1b942011-03-07 15:06:09 +00001208 kenter("%d,%u,%u,%d", id, timeout, error, ringid);
1209
1210 /* must be a valid error code and mustn't be a kernel special */
1211 if (error <= 0 ||
1212 error >= MAX_ERRNO ||
1213 error == ERESTARTSYS ||
1214 error == ERESTARTNOINTR ||
1215 error == ERESTARTNOHAND ||
1216 error == ERESTART_RESTARTBLOCK)
1217 return -EINVAL;
David Howellsd84f4f92008-11-14 10:39:23 +11001218
David Howellsb5f545c2006-01-08 01:02:47 -08001219 /* the appropriate instantiation authorisation key must have been
1220 * assumed before calling this */
1221 ret = -EPERM;
David Howellsd84f4f92008-11-14 10:39:23 +11001222 instkey = cred->request_key_auth;
David Howellsb5f545c2006-01-08 01:02:47 -08001223 if (!instkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
David Howells146aa8b2015-10-21 14:04:48 +01001226 rka = instkey->payload.data[0];
David Howellsb5f545c2006-01-08 01:02:47 -08001227 if (rka->target_key->serial != id)
1228 goto error;
David Howells3e301482005-06-23 22:00:56 -07001229
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 /* find the destination keyring if present (which must also be
1231 * writable) */
David Howells8bbf49762008-11-14 10:39:14 +11001232 ret = get_instantiation_keyring(ringid, rka, &dest_keyring);
1233 if (ret < 0)
1234 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
1236 /* instantiate the key and link it into a keyring */
David Howellsfdd1b942011-03-07 15:06:09 +00001237 ret = key_reject_and_link(rka->target_key, timeout, error,
David Howells8bbf49762008-11-14 10:39:14 +11001238 dest_keyring, instkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
David Howells8bbf49762008-11-14 10:39:14 +11001240 key_put(dest_keyring);
David Howellsb5f545c2006-01-08 01:02:47 -08001241
1242 /* discard the assumed authority if it's just been disabled by
1243 * instantiation of the key */
David Howellsd84f4f92008-11-14 10:39:23 +11001244 if (ret == 0)
1245 keyctl_change_reqkey_auth(NULL);
David Howellsb5f545c2006-01-08 01:02:47 -08001246
1247error:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +00001249}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251/*
David Howells973c9f42011-01-20 16:38:33 +00001252 * Read or set the default keyring in which request_key() will cache keys and
1253 * return the old setting.
1254 *
1255 * If a process keyring is specified then this will be created if it doesn't
1256 * yet exist. The old setting will be returned if successful.
David Howells3e301482005-06-23 22:00:56 -07001257 */
1258long keyctl_set_reqkey_keyring(int reqkey_defl)
1259{
David Howellsd84f4f92008-11-14 10:39:23 +11001260 struct cred *new;
1261 int ret, old_setting;
1262
1263 old_setting = current_cred_xxx(jit_keyring);
1264
1265 if (reqkey_defl == KEY_REQKEY_DEFL_NO_CHANGE)
1266 return old_setting;
1267
1268 new = prepare_creds();
1269 if (!new)
1270 return -ENOMEM;
David Howells3e301482005-06-23 22:00:56 -07001271
1272 switch (reqkey_defl) {
1273 case KEY_REQKEY_DEFL_THREAD_KEYRING:
David Howellsd84f4f92008-11-14 10:39:23 +11001274 ret = install_thread_keyring_to_cred(new);
David Howells3e301482005-06-23 22:00:56 -07001275 if (ret < 0)
David Howellsd84f4f92008-11-14 10:39:23 +11001276 goto error;
David Howells3e301482005-06-23 22:00:56 -07001277 goto set;
1278
1279 case KEY_REQKEY_DEFL_PROCESS_KEYRING:
David Howellsd84f4f92008-11-14 10:39:23 +11001280 ret = install_process_keyring_to_cred(new);
1281 if (ret < 0) {
1282 if (ret != -EEXIST)
1283 goto error;
1284 ret = 0;
1285 }
1286 goto set;
David Howells3e301482005-06-23 22:00:56 -07001287
1288 case KEY_REQKEY_DEFL_DEFAULT:
1289 case KEY_REQKEY_DEFL_SESSION_KEYRING:
1290 case KEY_REQKEY_DEFL_USER_KEYRING:
1291 case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
David Howellsd84f4f92008-11-14 10:39:23 +11001292 case KEY_REQKEY_DEFL_REQUESTOR_KEYRING:
1293 goto set;
David Howells3e301482005-06-23 22:00:56 -07001294
1295 case KEY_REQKEY_DEFL_NO_CHANGE:
David Howells3e301482005-06-23 22:00:56 -07001296 case KEY_REQKEY_DEFL_GROUP_KEYRING:
1297 default:
David Howellsd84f4f92008-11-14 10:39:23 +11001298 ret = -EINVAL;
1299 goto error;
David Howells3e301482005-06-23 22:00:56 -07001300 }
1301
David Howellsd84f4f92008-11-14 10:39:23 +11001302set:
1303 new->jit_keyring = reqkey_defl;
1304 commit_creds(new);
1305 return old_setting;
1306error:
1307 abort_creds(new);
Dan Carpenter4303ef12010-06-11 17:30:05 +01001308 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +00001309}
David Howellsd84f4f92008-11-14 10:39:23 +11001310
David Howells3e301482005-06-23 22:00:56 -07001311/*
David Howells973c9f42011-01-20 16:38:33 +00001312 * Set or clear the timeout on a key.
1313 *
1314 * Either the key must grant the caller Setattr permission or else the caller
1315 * must hold an instantiation authorisation token for the key.
1316 *
1317 * The timeout is either 0 to clear the timeout, or a number of seconds from
1318 * the current time. The key and any links to the key will be automatically
1319 * garbage collected after the timeout expires.
1320 *
Mimi Zohard3600bc2015-11-10 08:34:46 -05001321 * Keys with KEY_FLAG_KEEP set should not be timed out.
1322 *
David Howells973c9f42011-01-20 16:38:33 +00001323 * If successful, 0 is returned.
David Howells017679c2006-01-08 01:02:43 -08001324 */
1325long keyctl_set_timeout(key_serial_t id, unsigned timeout)
1326{
David Howells91562352010-06-11 17:31:05 +01001327 struct key *key, *instkey;
David Howells017679c2006-01-08 01:02:43 -08001328 key_ref_t key_ref;
David Howells017679c2006-01-08 01:02:43 -08001329 long ret;
1330
David Howells55931222009-09-02 09:13:45 +01001331 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
David Howellsf5895942014-03-14 17:44:49 +00001332 KEY_NEED_SETATTR);
David Howells017679c2006-01-08 01:02:43 -08001333 if (IS_ERR(key_ref)) {
David Howells91562352010-06-11 17:31:05 +01001334 /* setting the timeout on a key under construction is permitted
1335 * if we have the authorisation token handy */
1336 if (PTR_ERR(key_ref) == -EACCES) {
1337 instkey = key_get_instantiation_authkey(id);
1338 if (!IS_ERR(instkey)) {
1339 key_put(instkey);
1340 key_ref = lookup_user_key(id,
1341 KEY_LOOKUP_PARTIAL,
1342 0);
1343 if (!IS_ERR(key_ref))
1344 goto okay;
1345 }
1346 }
1347
David Howells017679c2006-01-08 01:02:43 -08001348 ret = PTR_ERR(key_ref);
1349 goto error;
1350 }
1351
David Howells91562352010-06-11 17:31:05 +01001352okay:
David Howells017679c2006-01-08 01:02:43 -08001353 key = key_ref_to_ptr(key_ref);
Mimi Zohar1d6d1672016-01-07 07:46:36 -05001354 ret = 0;
Mimi Zohard3600bc2015-11-10 08:34:46 -05001355 if (test_bit(KEY_FLAG_KEEP, &key->flags))
1356 ret = -EPERM;
Mimi Zohar1d6d1672016-01-07 07:46:36 -05001357 else
Mimi Zohard3600bc2015-11-10 08:34:46 -05001358 key_set_timeout(key, timeout);
David Howells017679c2006-01-08 01:02:43 -08001359 key_put(key);
1360
David Howells017679c2006-01-08 01:02:43 -08001361error:
1362 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +00001363}
David Howells017679c2006-01-08 01:02:43 -08001364
David Howells017679c2006-01-08 01:02:43 -08001365/*
David Howells973c9f42011-01-20 16:38:33 +00001366 * Assume (or clear) the authority to instantiate the specified key.
1367 *
1368 * This sets the authoritative token currently in force for key instantiation.
1369 * This must be done for a key to be instantiated. It has the effect of making
1370 * available all the keys from the caller of the request_key() that created a
1371 * key to request_key() calls made by the caller of this function.
1372 *
1373 * The caller must have the instantiation key in their process keyrings with a
1374 * Search permission grant available to the caller.
1375 *
1376 * If the ID given is 0, then the setting will be cleared and 0 returned.
1377 *
1378 * If the ID given has a matching an authorisation key, then that key will be
1379 * set and its ID will be returned. The authorisation key can be read to get
1380 * the callout information passed to request_key().
David Howellsb5f545c2006-01-08 01:02:47 -08001381 */
1382long keyctl_assume_authority(key_serial_t id)
1383{
1384 struct key *authkey;
1385 long ret;
1386
1387 /* special key IDs aren't permitted */
1388 ret = -EINVAL;
1389 if (id < 0)
1390 goto error;
1391
1392 /* we divest ourselves of authority if given an ID of 0 */
1393 if (id == 0) {
David Howellsd84f4f92008-11-14 10:39:23 +11001394 ret = keyctl_change_reqkey_auth(NULL);
David Howellsb5f545c2006-01-08 01:02:47 -08001395 goto error;
1396 }
1397
1398 /* attempt to assume the authority temporarily granted to us whilst we
1399 * instantiate the specified key
1400 * - the authorisation key must be in the current task's keyrings
1401 * somewhere
1402 */
1403 authkey = key_get_instantiation_authkey(id);
1404 if (IS_ERR(authkey)) {
1405 ret = PTR_ERR(authkey);
1406 goto error;
1407 }
1408
David Howellsd84f4f92008-11-14 10:39:23 +11001409 ret = keyctl_change_reqkey_auth(authkey);
1410 if (ret < 0)
1411 goto error;
1412 key_put(authkey);
David Howellsb5f545c2006-01-08 01:02:47 -08001413
David Howellsd84f4f92008-11-14 10:39:23 +11001414 ret = authkey->serial;
David Howellsb5f545c2006-01-08 01:02:47 -08001415error:
1416 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +00001417}
David Howellsb5f545c2006-01-08 01:02:47 -08001418
David Howells70a5bb72008-04-29 01:01:26 -07001419/*
David Howells973c9f42011-01-20 16:38:33 +00001420 * Get a key's the LSM security label.
1421 *
1422 * The key must grant the caller View permission for this to work.
1423 *
1424 * If there's a buffer, then up to buflen bytes of data will be placed into it.
1425 *
1426 * If successful, the amount of information available will be returned,
1427 * irrespective of how much was copied (including the terminal NUL).
David Howells70a5bb72008-04-29 01:01:26 -07001428 */
1429long keyctl_get_security(key_serial_t keyid,
1430 char __user *buffer,
1431 size_t buflen)
1432{
1433 struct key *key, *instkey;
1434 key_ref_t key_ref;
1435 char *context;
1436 long ret;
1437
David Howellsf5895942014-03-14 17:44:49 +00001438 key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_NEED_VIEW);
David Howells70a5bb72008-04-29 01:01:26 -07001439 if (IS_ERR(key_ref)) {
1440 if (PTR_ERR(key_ref) != -EACCES)
1441 return PTR_ERR(key_ref);
1442
1443 /* viewing a key under construction is also permitted if we
1444 * have the authorisation token handy */
1445 instkey = key_get_instantiation_authkey(keyid);
1446 if (IS_ERR(instkey))
Roel Kluinfa1cc7b2009-12-15 15:05:12 -08001447 return PTR_ERR(instkey);
David Howells70a5bb72008-04-29 01:01:26 -07001448 key_put(instkey);
1449
David Howells55931222009-09-02 09:13:45 +01001450 key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, 0);
David Howells70a5bb72008-04-29 01:01:26 -07001451 if (IS_ERR(key_ref))
1452 return PTR_ERR(key_ref);
1453 }
1454
1455 key = key_ref_to_ptr(key_ref);
1456 ret = security_key_getsecurity(key, &context);
1457 if (ret == 0) {
1458 /* if no information was returned, give userspace an empty
1459 * string */
1460 ret = 1;
1461 if (buffer && buflen > 0 &&
1462 copy_to_user(buffer, "", 1) != 0)
1463 ret = -EFAULT;
1464 } else if (ret > 0) {
1465 /* return as much data as there's room for */
1466 if (buffer && buflen > 0) {
1467 if (buflen > ret)
1468 buflen = ret;
1469
1470 if (copy_to_user(buffer, context, buflen) != 0)
1471 ret = -EFAULT;
1472 }
1473
1474 kfree(context);
1475 }
1476
1477 key_ref_put(key_ref);
1478 return ret;
1479}
1480
David Howellsee18d642009-09-02 09:14:21 +01001481/*
David Howells973c9f42011-01-20 16:38:33 +00001482 * Attempt to install the calling process's session keyring on the process's
1483 * parent process.
1484 *
1485 * The keyring must exist and must grant the caller LINK permission, and the
1486 * parent process must be single-threaded and must have the same effective
1487 * ownership as this process and mustn't be SUID/SGID.
1488 *
1489 * The keyring will be emplaced on the parent when it next resumes userspace.
1490 *
1491 * If successful, 0 will be returned.
David Howellsee18d642009-09-02 09:14:21 +01001492 */
1493long keyctl_session_to_parent(void)
1494{
1495 struct task_struct *me, *parent;
1496 const struct cred *mycred, *pcred;
Al Viro67d12142012-06-27 11:07:19 +04001497 struct callback_head *newwork, *oldwork;
David Howellsee18d642009-09-02 09:14:21 +01001498 key_ref_t keyring_r;
Oleg Nesterov413cd3d2012-05-11 10:59:08 +10001499 struct cred *cred;
David Howellsee18d642009-09-02 09:14:21 +01001500 int ret;
1501
David Howellsf5895942014-03-14 17:44:49 +00001502 keyring_r = lookup_user_key(KEY_SPEC_SESSION_KEYRING, 0, KEY_NEED_LINK);
David Howellsee18d642009-09-02 09:14:21 +01001503 if (IS_ERR(keyring_r))
1504 return PTR_ERR(keyring_r);
1505
Oleg Nesterov413cd3d2012-05-11 10:59:08 +10001506 ret = -ENOMEM;
Oleg Nesterov413cd3d2012-05-11 10:59:08 +10001507
David Howellsee18d642009-09-02 09:14:21 +01001508 /* our parent is going to need a new cred struct, a new tgcred struct
1509 * and new security data, so we allocate them here to prevent ENOMEM in
1510 * our parent */
David Howellsee18d642009-09-02 09:14:21 +01001511 cred = cred_alloc_blank();
1512 if (!cred)
Al Viro67d12142012-06-27 11:07:19 +04001513 goto error_keyring;
1514 newwork = &cred->rcu;
David Howellsee18d642009-09-02 09:14:21 +01001515
David Howells3a505972012-10-02 19:24:29 +01001516 cred->session_keyring = key_ref_to_ptr(keyring_r);
1517 keyring_r = NULL;
Al Viro67d12142012-06-27 11:07:19 +04001518 init_task_work(newwork, key_change_session_keyring);
David Howellsee18d642009-09-02 09:14:21 +01001519
1520 me = current;
David Howells9d1ac652010-09-10 09:59:46 +01001521 rcu_read_lock();
David Howellsee18d642009-09-02 09:14:21 +01001522 write_lock_irq(&tasklist_lock);
1523
David Howellsee18d642009-09-02 09:14:21 +01001524 ret = -EPERM;
Oleg Nesterov413cd3d2012-05-11 10:59:08 +10001525 oldwork = NULL;
1526 parent = me->real_parent;
David Howellsee18d642009-09-02 09:14:21 +01001527
1528 /* the parent mustn't be init and mustn't be a kernel thread */
1529 if (parent->pid <= 1 || !parent->mm)
Oleg Nesterov413cd3d2012-05-11 10:59:08 +10001530 goto unlock;
David Howellsee18d642009-09-02 09:14:21 +01001531
1532 /* the parent must be single threaded */
Oleg Nesterovdd98acf2010-05-26 14:43:23 -07001533 if (!thread_group_empty(parent))
Oleg Nesterov413cd3d2012-05-11 10:59:08 +10001534 goto unlock;
David Howellsee18d642009-09-02 09:14:21 +01001535
1536 /* the parent and the child must have different session keyrings or
1537 * there's no point */
1538 mycred = current_cred();
1539 pcred = __task_cred(parent);
1540 if (mycred == pcred ||
David Howells3a505972012-10-02 19:24:29 +01001541 mycred->session_keyring == pcred->session_keyring) {
Oleg Nesterov413cd3d2012-05-11 10:59:08 +10001542 ret = 0;
1543 goto unlock;
1544 }
David Howellsee18d642009-09-02 09:14:21 +01001545
1546 /* the parent must have the same effective ownership and mustn't be
1547 * SUID/SGID */
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -08001548 if (!uid_eq(pcred->uid, mycred->euid) ||
1549 !uid_eq(pcred->euid, mycred->euid) ||
1550 !uid_eq(pcred->suid, mycred->euid) ||
1551 !gid_eq(pcred->gid, mycred->egid) ||
1552 !gid_eq(pcred->egid, mycred->egid) ||
1553 !gid_eq(pcred->sgid, mycred->egid))
Oleg Nesterov413cd3d2012-05-11 10:59:08 +10001554 goto unlock;
David Howellsee18d642009-09-02 09:14:21 +01001555
1556 /* the keyrings must have the same UID */
David Howells3a505972012-10-02 19:24:29 +01001557 if ((pcred->session_keyring &&
Linus Torvalds2a74dbb2012-12-16 15:40:50 -08001558 !uid_eq(pcred->session_keyring->uid, mycred->euid)) ||
1559 !uid_eq(mycred->session_keyring->uid, mycred->euid))
Oleg Nesterov413cd3d2012-05-11 10:59:08 +10001560 goto unlock;
David Howellsee18d642009-09-02 09:14:21 +01001561
Oleg Nesterov413cd3d2012-05-11 10:59:08 +10001562 /* cancel an already pending keyring replacement */
1563 oldwork = task_work_cancel(parent, key_change_session_keyring);
David Howellsee18d642009-09-02 09:14:21 +01001564
1565 /* the replacement session keyring is applied just prior to userspace
1566 * restarting */
Al Viro67d12142012-06-27 11:07:19 +04001567 ret = task_work_add(parent, newwork, true);
Oleg Nesterov413cd3d2012-05-11 10:59:08 +10001568 if (!ret)
1569 newwork = NULL;
1570unlock:
David Howellsee18d642009-09-02 09:14:21 +01001571 write_unlock_irq(&tasklist_lock);
David Howells9d1ac652010-09-10 09:59:46 +01001572 rcu_read_unlock();
Al Viro67d12142012-06-27 11:07:19 +04001573 if (oldwork)
1574 put_cred(container_of(oldwork, struct cred, rcu));
1575 if (newwork)
1576 put_cred(cred);
David Howellsee18d642009-09-02 09:14:21 +01001577 return ret;
1578
1579error_keyring:
1580 key_ref_put(keyring_r);
1581 return ret;
1582}
1583
David Howellsb5f545c2006-01-08 01:02:47 -08001584/*
David Howells973c9f42011-01-20 16:38:33 +00001585 * The key control system call
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 */
Heiko Carstens938bb9f2009-01-14 14:14:30 +01001587SYSCALL_DEFINE5(keyctl, int, option, unsigned long, arg2, unsigned long, arg3,
1588 unsigned long, arg4, unsigned long, arg5)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589{
1590 switch (option) {
1591 case KEYCTL_GET_KEYRING_ID:
1592 return keyctl_get_keyring_ID((key_serial_t) arg2,
1593 (int) arg3);
1594
1595 case KEYCTL_JOIN_SESSION_KEYRING:
1596 return keyctl_join_session_keyring((const char __user *) arg2);
1597
1598 case KEYCTL_UPDATE:
1599 return keyctl_update_key((key_serial_t) arg2,
1600 (const void __user *) arg3,
1601 (size_t) arg4);
1602
1603 case KEYCTL_REVOKE:
1604 return keyctl_revoke_key((key_serial_t) arg2);
1605
1606 case KEYCTL_DESCRIBE:
1607 return keyctl_describe_key((key_serial_t) arg2,
1608 (char __user *) arg3,
1609 (unsigned) arg4);
1610
1611 case KEYCTL_CLEAR:
1612 return keyctl_keyring_clear((key_serial_t) arg2);
1613
1614 case KEYCTL_LINK:
1615 return keyctl_keyring_link((key_serial_t) arg2,
1616 (key_serial_t) arg3);
1617
1618 case KEYCTL_UNLINK:
1619 return keyctl_keyring_unlink((key_serial_t) arg2,
1620 (key_serial_t) arg3);
1621
1622 case KEYCTL_SEARCH:
1623 return keyctl_keyring_search((key_serial_t) arg2,
1624 (const char __user *) arg3,
1625 (const char __user *) arg4,
1626 (key_serial_t) arg5);
1627
1628 case KEYCTL_READ:
1629 return keyctl_read_key((key_serial_t) arg2,
1630 (char __user *) arg3,
1631 (size_t) arg4);
1632
1633 case KEYCTL_CHOWN:
1634 return keyctl_chown_key((key_serial_t) arg2,
1635 (uid_t) arg3,
1636 (gid_t) arg4);
1637
1638 case KEYCTL_SETPERM:
1639 return keyctl_setperm_key((key_serial_t) arg2,
1640 (key_perm_t) arg3);
1641
1642 case KEYCTL_INSTANTIATE:
1643 return keyctl_instantiate_key((key_serial_t) arg2,
1644 (const void __user *) arg3,
1645 (size_t) arg4,
1646 (key_serial_t) arg5);
1647
1648 case KEYCTL_NEGATE:
1649 return keyctl_negate_key((key_serial_t) arg2,
1650 (unsigned) arg3,
1651 (key_serial_t) arg4);
1652
David Howells3e301482005-06-23 22:00:56 -07001653 case KEYCTL_SET_REQKEY_KEYRING:
1654 return keyctl_set_reqkey_keyring(arg2);
1655
David Howells017679c2006-01-08 01:02:43 -08001656 case KEYCTL_SET_TIMEOUT:
1657 return keyctl_set_timeout((key_serial_t) arg2,
1658 (unsigned) arg3);
1659
David Howellsb5f545c2006-01-08 01:02:47 -08001660 case KEYCTL_ASSUME_AUTHORITY:
1661 return keyctl_assume_authority((key_serial_t) arg2);
1662
David Howells70a5bb72008-04-29 01:01:26 -07001663 case KEYCTL_GET_SECURITY:
1664 return keyctl_get_security((key_serial_t) arg2,
James Morris90bd49a2008-12-29 14:35:35 +11001665 (char __user *) arg3,
David Howells70a5bb72008-04-29 01:01:26 -07001666 (size_t) arg4);
1667
David Howellsee18d642009-09-02 09:14:21 +01001668 case KEYCTL_SESSION_TO_PARENT:
1669 return keyctl_session_to_parent();
1670
David Howellsfdd1b942011-03-07 15:06:09 +00001671 case KEYCTL_REJECT:
1672 return keyctl_reject_key((key_serial_t) arg2,
1673 (unsigned) arg3,
1674 (unsigned) arg4,
1675 (key_serial_t) arg5);
1676
David Howellsee009e4a02011-03-07 15:06:20 +00001677 case KEYCTL_INSTANTIATE_IOV:
1678 return keyctl_instantiate_key_iov(
1679 (key_serial_t) arg2,
1680 (const struct iovec __user *) arg3,
1681 (unsigned) arg4,
1682 (key_serial_t) arg5);
1683
David Howellsfd758152012-05-11 10:56:56 +01001684 case KEYCTL_INVALIDATE:
1685 return keyctl_invalidate_key((key_serial_t) arg2);
1686
David Howellsf36f8c72013-09-24 10:35:19 +01001687 case KEYCTL_GET_PERSISTENT:
1688 return keyctl_get_persistent((uid_t)arg2, (key_serial_t)arg3);
1689
Mat Martineauddbb4112016-04-12 19:54:58 +01001690 case KEYCTL_DH_COMPUTE:
1691 return keyctl_dh_compute((struct keyctl_dh_params __user *) arg2,
Stephan Mueller4693fc72016-05-26 23:38:12 +02001692 (char __user *) arg3, (size_t) arg4,
1693 (void __user *) arg5);
Mat Martineauddbb4112016-04-12 19:54:58 +01001694
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 default:
1696 return -EOPNOTSUPP;
1697 }
David Howellsa8b17ed2011-01-20 16:38:27 +00001698}