blob: 8a8c23357291471a35a201ebfcde9f08c2e9b893 [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>
Davi Arnaut0cb409d2006-03-24 03:18:43 -080021#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/err.h>
David Howells38bbca62008-04-29 01:01:19 -070023#include <linux/vmalloc.h>
David Howells70a5bb72008-04-29 01:01:26 -070024#include <linux/security.h>
Kent Overstreeta27bb332013-05-07 16:19:08 -070025#include <linux/uio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <asm/uaccess.h>
27#include "internal.h"
28
Davi Arnaut0cb409d2006-03-24 03:18:43 -080029static int key_get_type_from_user(char *type,
30 const char __user *_type,
31 unsigned len)
32{
33 int ret;
34
35 ret = strncpy_from_user(type, _type, len);
Davi Arnaut0cb409d2006-03-24 03:18:43 -080036 if (ret < 0)
Dan Carpenter4303ef12010-06-11 17:30:05 +010037 return ret;
Davi Arnaut0cb409d2006-03-24 03:18:43 -080038 if (ret == 0 || ret >= len)
39 return -EINVAL;
Davi Arnaut0cb409d2006-03-24 03:18:43 -080040 type[len - 1] = '\0';
Davi Arnaut0cb409d2006-03-24 03:18:43 -080041 return 0;
42}
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044/*
David Howells973c9f42011-01-20 16:38:33 +000045 * Extract the description of a new key from userspace and either add it as a
46 * new key to the specified keyring or update a matching key in that keyring.
47 *
David Howellscf7f6012012-09-13 13:06:29 +010048 * If the description is NULL or an empty string, the key type is asked to
49 * generate one from the payload.
50 *
David Howells973c9f42011-01-20 16:38:33 +000051 * The keyring must be writable so that we can attach the key to it.
52 *
53 * If successful, the new key's serial number is returned, otherwise an error
54 * code is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 */
Heiko Carstens1e7bfb22009-01-14 14:14:29 +010056SYSCALL_DEFINE5(add_key, const char __user *, _type,
57 const char __user *, _description,
58 const void __user *, _payload,
59 size_t, plen,
60 key_serial_t, ringid)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
David Howells664cceb2005-09-28 17:03:15 +010062 key_ref_t keyring_ref, key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 char type[32], *description;
64 void *payload;
Davi Arnaut0cb409d2006-03-24 03:18:43 -080065 long ret;
David Howells38bbca62008-04-29 01:01:19 -070066 bool vm;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68 ret = -EINVAL;
David Howells38bbca62008-04-29 01:01:19 -070069 if (plen > 1024 * 1024 - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 goto error;
71
72 /* draw all the data into kernel space */
Davi Arnaut0cb409d2006-03-24 03:18:43 -080073 ret = key_get_type_from_user(type, _type, sizeof(type));
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 if (ret < 0)
75 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
David Howellscf7f6012012-09-13 13:06:29 +010077 description = NULL;
78 if (_description) {
79 description = strndup_user(_description, PAGE_SIZE);
80 if (IS_ERR(description)) {
81 ret = PTR_ERR(description);
82 goto error;
83 }
84 if (!*description) {
85 kfree(description);
86 description = NULL;
Mimi Zohara4e3b8d2014-05-22 14:02:23 -040087 } else if ((description[0] == '.') &&
88 (strncmp(type, "keyring", 7) == 0)) {
89 ret = -EPERM;
90 goto error2;
David Howellscf7f6012012-09-13 13:06:29 +010091 }
Davi Arnaut0cb409d2006-03-24 03:18:43 -080092 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94 /* pull the payload in if one was supplied */
95 payload = NULL;
96
David Howells38bbca62008-04-29 01:01:19 -070097 vm = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 if (_payload) {
99 ret = -ENOMEM;
Andrew Morton4f1c28d2012-05-31 16:26:02 -0700100 payload = kmalloc(plen, GFP_KERNEL | __GFP_NOWARN);
David Howells38bbca62008-04-29 01:01:19 -0700101 if (!payload) {
102 if (plen <= PAGE_SIZE)
103 goto error2;
104 vm = true;
105 payload = vmalloc(plen);
106 if (!payload)
107 goto error2;
108 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110 ret = -EFAULT;
111 if (copy_from_user(payload, _payload, plen) != 0)
112 goto error3;
113 }
114
115 /* find the target keyring (which must be writable) */
David Howellsf5895942014-03-14 17:44:49 +0000116 keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
David Howells664cceb2005-09-28 17:03:15 +0100117 if (IS_ERR(keyring_ref)) {
118 ret = PTR_ERR(keyring_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 goto error3;
120 }
121
122 /* create or update the requested key and add it to the target
123 * keyring */
David Howells664cceb2005-09-28 17:03:15 +0100124 key_ref = key_create_or_update(keyring_ref, type, description,
Arun Raghavan6b79ccb2008-04-29 01:01:28 -0700125 payload, plen, KEY_PERM_UNDEF,
126 KEY_ALLOC_IN_QUOTA);
David Howells664cceb2005-09-28 17:03:15 +0100127 if (!IS_ERR(key_ref)) {
128 ret = key_ref_to_ptr(key_ref)->serial;
129 key_ref_put(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 }
131 else {
David Howells664cceb2005-09-28 17:03:15 +0100132 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 }
134
David Howells664cceb2005-09-28 17:03:15 +0100135 key_ref_put(keyring_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 error3:
David Howells38bbca62008-04-29 01:01:19 -0700137 if (!vm)
138 kfree(payload);
139 else
140 vfree(payload);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 error2:
142 kfree(description);
143 error:
144 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000145}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147/*
David Howells973c9f42011-01-20 16:38:33 +0000148 * Search the process keyrings and keyring trees linked from those for a
149 * matching key. Keyrings must have appropriate Search permission to be
150 * searched.
151 *
152 * If a key is found, it will be attached to the destination keyring if there's
153 * one specified and the serial number of the key will be returned.
154 *
155 * If no key is found, /sbin/request-key will be invoked if _callout_info is
156 * non-NULL in an attempt to create a key. The _callout_info string will be
157 * passed to /sbin/request-key to aid with completing the request. If the
158 * _callout_info string is "" then it will be changed to "-".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 */
Heiko Carstens1e7bfb22009-01-14 14:14:29 +0100160SYSCALL_DEFINE4(request_key, const char __user *, _type,
161 const char __user *, _description,
162 const char __user *, _callout_info,
163 key_serial_t, destringid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
165 struct key_type *ktype;
David Howells664cceb2005-09-28 17:03:15 +0100166 struct key *key;
167 key_ref_t dest_ref;
David Howells4a38e122008-04-29 01:01:24 -0700168 size_t callout_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 char type[32], *description, *callout_info;
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800170 long ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172 /* pull the type into kernel space */
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800173 ret = key_get_type_from_user(type, _type, sizeof(type));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 if (ret < 0)
175 goto error;
David Howells1260f802005-08-04 11:50:01 +0100176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 /* pull the description into kernel space */
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800178 description = strndup_user(_description, PAGE_SIZE);
179 if (IS_ERR(description)) {
180 ret = PTR_ERR(description);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 goto error;
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800182 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184 /* pull the callout info into kernel space */
185 callout_info = NULL;
David Howells4a38e122008-04-29 01:01:24 -0700186 callout_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 if (_callout_info) {
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800188 callout_info = strndup_user(_callout_info, PAGE_SIZE);
189 if (IS_ERR(callout_info)) {
190 ret = PTR_ERR(callout_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 goto error2;
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800192 }
David Howells4a38e122008-04-29 01:01:24 -0700193 callout_len = strlen(callout_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 }
195
196 /* get the destination keyring if specified */
David Howells664cceb2005-09-28 17:03:15 +0100197 dest_ref = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 if (destringid) {
David Howells55931222009-09-02 09:13:45 +0100199 dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE,
David Howellsf5895942014-03-14 17:44:49 +0000200 KEY_NEED_WRITE);
David Howells664cceb2005-09-28 17:03:15 +0100201 if (IS_ERR(dest_ref)) {
202 ret = PTR_ERR(dest_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 goto error3;
204 }
205 }
206
207 /* find the key type */
208 ktype = key_type_lookup(type);
209 if (IS_ERR(ktype)) {
210 ret = PTR_ERR(ktype);
211 goto error4;
212 }
213
214 /* do the search */
David Howells4a38e122008-04-29 01:01:24 -0700215 key = request_key_and_link(ktype, description, callout_info,
216 callout_len, NULL, key_ref_to_ptr(dest_ref),
David Howells7e047ef2006-06-26 00:24:50 -0700217 KEY_ALLOC_IN_QUOTA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 if (IS_ERR(key)) {
219 ret = PTR_ERR(key);
220 goto error5;
221 }
222
David Howells4aab1e82011-03-11 17:57:33 +0000223 /* wait for the key to finish being constructed */
224 ret = wait_for_key_construction(key, 1);
225 if (ret < 0)
226 goto error6;
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 ret = key->serial;
229
David Howells4aab1e82011-03-11 17:57:33 +0000230error6:
David Howells3e301482005-06-23 22:00:56 -0700231 key_put(key);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700232error5:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 key_type_put(ktype);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700234error4:
David Howells664cceb2005-09-28 17:03:15 +0100235 key_ref_put(dest_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700236error3:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 kfree(callout_info);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700238error2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 kfree(description);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700240error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000242}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244/*
David Howells973c9f42011-01-20 16:38:33 +0000245 * Get the ID of the specified process keyring.
246 *
247 * The requested keyring must have search permission to be found.
248 *
249 * If successful, the ID of the requested keyring will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 */
251long keyctl_get_keyring_ID(key_serial_t id, int create)
252{
David Howells664cceb2005-09-28 17:03:15 +0100253 key_ref_t key_ref;
David Howells55931222009-09-02 09:13:45 +0100254 unsigned long lflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 long ret;
256
David Howells55931222009-09-02 09:13:45 +0100257 lflags = create ? KEY_LOOKUP_CREATE : 0;
David Howellsf5895942014-03-14 17:44:49 +0000258 key_ref = lookup_user_key(id, lflags, KEY_NEED_SEARCH);
David Howells664cceb2005-09-28 17:03:15 +0100259 if (IS_ERR(key_ref)) {
260 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 goto error;
262 }
263
David Howells664cceb2005-09-28 17:03:15 +0100264 ret = key_ref_to_ptr(key_ref)->serial;
265 key_ref_put(key_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700266error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 return ret;
David Howells973c9f42011-01-20 16:38:33 +0000268}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270/*
David Howells973c9f42011-01-20 16:38:33 +0000271 * Join a (named) session keyring.
272 *
273 * Create and join an anonymous session keyring or join a named session
274 * keyring, creating it if necessary. A named session keyring must have Search
275 * permission for it to be joined. Session keyrings without this permit will
276 * be skipped over.
277 *
278 * If successful, the ID of the joined session keyring will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 */
280long keyctl_join_session_keyring(const char __user *_name)
281{
282 char *name;
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800283 long ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 /* fetch the name from userspace */
286 name = NULL;
287 if (_name) {
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800288 name = strndup_user(_name, PAGE_SIZE);
289 if (IS_ERR(name)) {
290 ret = PTR_ERR(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 goto error;
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800292 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 }
294
295 /* join the session */
296 ret = join_session_keyring(name);
Vegard Nossum0d54ee12009-01-17 17:45:45 +0100297 kfree(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700299error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000301}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303/*
David Howells973c9f42011-01-20 16:38:33 +0000304 * Update a key's data payload from the given data.
305 *
306 * The key must grant the caller Write permission and the key type must support
307 * updating for this to work. A negative key can be positively instantiated
308 * with this call.
309 *
310 * If successful, 0 will be returned. If the key type does not support
311 * updating, then -EOPNOTSUPP will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 */
313long keyctl_update_key(key_serial_t id,
314 const void __user *_payload,
315 size_t plen)
316{
David Howells664cceb2005-09-28 17:03:15 +0100317 key_ref_t key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 void *payload;
319 long ret;
320
321 ret = -EINVAL;
322 if (plen > PAGE_SIZE)
323 goto error;
324
325 /* pull the payload in if one was supplied */
326 payload = NULL;
327 if (_payload) {
328 ret = -ENOMEM;
329 payload = kmalloc(plen, GFP_KERNEL);
330 if (!payload)
331 goto error;
332
333 ret = -EFAULT;
334 if (copy_from_user(payload, _payload, plen) != 0)
335 goto error2;
336 }
337
338 /* find the target key (which must be writable) */
David Howellsf5895942014-03-14 17:44:49 +0000339 key_ref = lookup_user_key(id, 0, KEY_NEED_WRITE);
David Howells664cceb2005-09-28 17:03:15 +0100340 if (IS_ERR(key_ref)) {
341 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 goto error2;
343 }
344
345 /* update the key */
David Howells664cceb2005-09-28 17:03:15 +0100346 ret = key_update(key_ref, payload, plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
David Howells664cceb2005-09-28 17:03:15 +0100348 key_ref_put(key_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700349error2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 kfree(payload);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700351error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000353}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355/*
David Howells973c9f42011-01-20 16:38:33 +0000356 * Revoke a key.
357 *
358 * The key must be grant the caller Write or Setattr permission for this to
359 * work. The key type should give up its quota claim when revoked. The key
360 * and any links to the key will be automatically garbage collected after a
361 * certain amount of time (/proc/sys/kernel/keys/gc_delay).
362 *
363 * If successful, 0 is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 */
365long keyctl_revoke_key(key_serial_t id)
366{
David Howells664cceb2005-09-28 17:03:15 +0100367 key_ref_t key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 long ret;
369
David Howellsf5895942014-03-14 17:44:49 +0000370 key_ref = lookup_user_key(id, 0, KEY_NEED_WRITE);
David Howells664cceb2005-09-28 17:03:15 +0100371 if (IS_ERR(key_ref)) {
372 ret = PTR_ERR(key_ref);
David Howells0c2c9a32009-09-02 09:13:50 +0100373 if (ret != -EACCES)
374 goto error;
David Howellsf5895942014-03-14 17:44:49 +0000375 key_ref = lookup_user_key(id, 0, KEY_NEED_SETATTR);
David Howells0c2c9a32009-09-02 09:13:50 +0100376 if (IS_ERR(key_ref)) {
377 ret = PTR_ERR(key_ref);
378 goto error;
379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 }
381
David Howells664cceb2005-09-28 17:03:15 +0100382 key_revoke(key_ref_to_ptr(key_ref));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 ret = 0;
384
David Howells664cceb2005-09-28 17:03:15 +0100385 key_ref_put(key_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700386error:
David Howells1260f802005-08-04 11:50:01 +0100387 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000388}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390/*
David Howellsfd758152012-05-11 10:56:56 +0100391 * Invalidate a key.
392 *
393 * The key must be grant the caller Invalidate permission for this to work.
394 * The key and any links to the key will be automatically garbage collected
395 * immediately.
396 *
397 * If successful, 0 is returned.
398 */
399long keyctl_invalidate_key(key_serial_t id)
400{
401 key_ref_t key_ref;
402 long ret;
403
404 kenter("%d", id);
405
David Howellsf5895942014-03-14 17:44:49 +0000406 key_ref = lookup_user_key(id, 0, KEY_NEED_SEARCH);
David Howellsfd758152012-05-11 10:56:56 +0100407 if (IS_ERR(key_ref)) {
408 ret = PTR_ERR(key_ref);
409 goto error;
410 }
411
412 key_invalidate(key_ref_to_ptr(key_ref));
413 ret = 0;
414
415 key_ref_put(key_ref);
416error:
417 kleave(" = %ld", ret);
418 return ret;
419}
420
421/*
David Howells973c9f42011-01-20 16:38:33 +0000422 * Clear the specified keyring, creating an empty process keyring if one of the
423 * special keyring IDs is used.
424 *
425 * The keyring must grant the caller Write permission for this to work. If
426 * successful, 0 will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 */
428long keyctl_keyring_clear(key_serial_t ringid)
429{
David Howells664cceb2005-09-28 17:03:15 +0100430 key_ref_t keyring_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 long ret;
432
David Howellsf5895942014-03-14 17:44:49 +0000433 keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
David Howells664cceb2005-09-28 17:03:15 +0100434 if (IS_ERR(keyring_ref)) {
435 ret = PTR_ERR(keyring_ref);
David Howells700920e2012-01-18 15:31:45 +0000436
437 /* Root is permitted to invalidate certain special keyrings */
438 if (capable(CAP_SYS_ADMIN)) {
439 keyring_ref = lookup_user_key(ringid, 0, 0);
440 if (IS_ERR(keyring_ref))
441 goto error;
442 if (test_bit(KEY_FLAG_ROOT_CAN_CLEAR,
443 &key_ref_to_ptr(keyring_ref)->flags))
444 goto clear;
445 goto error_put;
446 }
447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 goto error;
449 }
450
David Howells700920e2012-01-18 15:31:45 +0000451clear:
David Howells664cceb2005-09-28 17:03:15 +0100452 ret = keyring_clear(key_ref_to_ptr(keyring_ref));
David Howells700920e2012-01-18 15:31:45 +0000453error_put:
David Howells664cceb2005-09-28 17:03:15 +0100454 key_ref_put(keyring_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700455error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000457}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459/*
David Howells973c9f42011-01-20 16:38:33 +0000460 * Create a link from a keyring to a key if there's no matching key in the
461 * keyring, otherwise replace the link to the matching key with a link to the
462 * new key.
463 *
464 * The key must grant the caller Link permission and the the keyring must grant
465 * the caller Write permission. Furthermore, if an additional link is created,
466 * the keyring's quota will be extended.
467 *
468 * If successful, 0 will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 */
470long keyctl_keyring_link(key_serial_t id, key_serial_t ringid)
471{
David Howells664cceb2005-09-28 17:03:15 +0100472 key_ref_t keyring_ref, key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 long ret;
474
David Howellsf5895942014-03-14 17:44:49 +0000475 keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
David Howells664cceb2005-09-28 17:03:15 +0100476 if (IS_ERR(keyring_ref)) {
477 ret = PTR_ERR(keyring_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 goto error;
479 }
480
David Howellsf5895942014-03-14 17:44:49 +0000481 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE, KEY_NEED_LINK);
David Howells664cceb2005-09-28 17:03:15 +0100482 if (IS_ERR(key_ref)) {
483 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 goto error2;
485 }
486
David Howells664cceb2005-09-28 17:03:15 +0100487 ret = key_link(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
David Howells664cceb2005-09-28 17:03:15 +0100489 key_ref_put(key_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700490error2:
David Howells664cceb2005-09-28 17:03:15 +0100491 key_ref_put(keyring_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700492error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000494}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496/*
David Howells973c9f42011-01-20 16:38:33 +0000497 * Unlink a key from a keyring.
498 *
499 * The keyring must grant the caller Write permission for this to work; the key
500 * itself need not grant the caller anything. If the last link to a key is
501 * removed then that key will be scheduled for destruction.
502 *
503 * If successful, 0 will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 */
505long keyctl_keyring_unlink(key_serial_t id, key_serial_t ringid)
506{
David Howells664cceb2005-09-28 17:03:15 +0100507 key_ref_t keyring_ref, key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 long ret;
509
David Howellsf5895942014-03-14 17:44:49 +0000510 keyring_ref = lookup_user_key(ringid, 0, KEY_NEED_WRITE);
David Howells664cceb2005-09-28 17:03:15 +0100511 if (IS_ERR(keyring_ref)) {
512 ret = PTR_ERR(keyring_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 goto error;
514 }
515
David Howells55931222009-09-02 09:13:45 +0100516 key_ref = lookup_user_key(id, KEY_LOOKUP_FOR_UNLINK, 0);
David Howells664cceb2005-09-28 17:03:15 +0100517 if (IS_ERR(key_ref)) {
518 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 goto error2;
520 }
521
David Howells664cceb2005-09-28 17:03:15 +0100522 ret = key_unlink(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
David Howells664cceb2005-09-28 17:03:15 +0100524 key_ref_put(key_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700525error2:
David Howells664cceb2005-09-28 17:03:15 +0100526 key_ref_put(keyring_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700527error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000529}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531/*
David Howells973c9f42011-01-20 16:38:33 +0000532 * Return a description of a key to userspace.
533 *
534 * The key must grant the caller View permission for this to work.
535 *
536 * If there's a buffer, we place up to buflen bytes of data into it formatted
537 * in the following way:
538 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 * type;uid;gid;perm;description<NUL>
David Howells973c9f42011-01-20 16:38:33 +0000540 *
541 * If successful, we return the amount of description available, irrespective
542 * of how much we may have copied into the buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 */
544long keyctl_describe_key(key_serial_t keyid,
545 char __user *buffer,
546 size_t buflen)
547{
David Howells3e301482005-06-23 22:00:56 -0700548 struct key *key, *instkey;
David Howells664cceb2005-09-28 17:03:15 +0100549 key_ref_t key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 char *tmpbuf;
551 long ret;
552
David Howellsf5895942014-03-14 17:44:49 +0000553 key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_NEED_VIEW);
David Howells664cceb2005-09-28 17:03:15 +0100554 if (IS_ERR(key_ref)) {
David Howells3e301482005-06-23 22:00:56 -0700555 /* viewing a key under construction is permitted if we have the
556 * authorisation token handy */
David Howells664cceb2005-09-28 17:03:15 +0100557 if (PTR_ERR(key_ref) == -EACCES) {
David Howells3e301482005-06-23 22:00:56 -0700558 instkey = key_get_instantiation_authkey(keyid);
559 if (!IS_ERR(instkey)) {
560 key_put(instkey);
David Howells8bbf49762008-11-14 10:39:14 +1100561 key_ref = lookup_user_key(keyid,
David Howells55931222009-09-02 09:13:45 +0100562 KEY_LOOKUP_PARTIAL,
563 0);
David Howells664cceb2005-09-28 17:03:15 +0100564 if (!IS_ERR(key_ref))
David Howells3e301482005-06-23 22:00:56 -0700565 goto okay;
566 }
567 }
568
David Howells664cceb2005-09-28 17:03:15 +0100569 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 goto error;
571 }
572
David Howells3e301482005-06-23 22:00:56 -0700573okay:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 /* calculate how much description we're going to return */
575 ret = -ENOMEM;
576 tmpbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
577 if (!tmpbuf)
578 goto error2;
579
David Howells664cceb2005-09-28 17:03:15 +0100580 key = key_ref_to_ptr(key_ref);
581
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 ret = snprintf(tmpbuf, PAGE_SIZE - 1,
David Howells664cceb2005-09-28 17:03:15 +0100583 "%s;%d;%d;%08x;%s",
David Howells94fd8402010-06-28 14:05:04 +0100584 key->type->name,
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -0800585 from_kuid_munged(current_user_ns(), key->uid),
586 from_kgid_munged(current_user_ns(), key->gid),
David Howells94fd8402010-06-28 14:05:04 +0100587 key->perm,
588 key->description ?: "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
590 /* include a NUL char at the end of the data */
591 if (ret > PAGE_SIZE - 1)
592 ret = PAGE_SIZE - 1;
593 tmpbuf[ret] = 0;
594 ret++;
595
596 /* consider returning the data */
597 if (buffer && buflen > 0) {
598 if (buflen > ret)
599 buflen = ret;
600
601 if (copy_to_user(buffer, tmpbuf, buflen) != 0)
602 ret = -EFAULT;
603 }
604
605 kfree(tmpbuf);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700606error2:
David Howells664cceb2005-09-28 17:03:15 +0100607 key_ref_put(key_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700608error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000610}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612/*
David Howells973c9f42011-01-20 16:38:33 +0000613 * Search the specified keyring and any keyrings it links to for a matching
614 * key. Only keyrings that grant the caller Search permission will be searched
615 * (this includes the starting keyring). Only keys with Search permission can
616 * be found.
617 *
618 * If successful, the found key will be linked to the destination keyring if
619 * supplied and the key has Link permission, and the found key ID will be
620 * returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 */
622long keyctl_keyring_search(key_serial_t ringid,
623 const char __user *_type,
624 const char __user *_description,
625 key_serial_t destringid)
626{
627 struct key_type *ktype;
David Howells664cceb2005-09-28 17:03:15 +0100628 key_ref_t keyring_ref, key_ref, dest_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 char type[32], *description;
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800630 long ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
632 /* pull the type and description into kernel space */
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800633 ret = key_get_type_from_user(type, _type, sizeof(type));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 if (ret < 0)
635 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800637 description = strndup_user(_description, PAGE_SIZE);
638 if (IS_ERR(description)) {
639 ret = PTR_ERR(description);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 goto error;
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800641 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
643 /* get the keyring at which to begin the search */
David Howellsf5895942014-03-14 17:44:49 +0000644 keyring_ref = lookup_user_key(ringid, 0, KEY_NEED_SEARCH);
David Howells664cceb2005-09-28 17:03:15 +0100645 if (IS_ERR(keyring_ref)) {
646 ret = PTR_ERR(keyring_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 goto error2;
648 }
649
650 /* get the destination keyring if specified */
David Howells664cceb2005-09-28 17:03:15 +0100651 dest_ref = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 if (destringid) {
David Howells55931222009-09-02 09:13:45 +0100653 dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE,
David Howellsf5895942014-03-14 17:44:49 +0000654 KEY_NEED_WRITE);
David Howells664cceb2005-09-28 17:03:15 +0100655 if (IS_ERR(dest_ref)) {
656 ret = PTR_ERR(dest_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 goto error3;
658 }
659 }
660
661 /* find the key type */
662 ktype = key_type_lookup(type);
663 if (IS_ERR(ktype)) {
664 ret = PTR_ERR(ktype);
665 goto error4;
666 }
667
668 /* do the search */
David Howells664cceb2005-09-28 17:03:15 +0100669 key_ref = keyring_search(keyring_ref, ktype, description);
670 if (IS_ERR(key_ref)) {
671 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
673 /* treat lack or presence of a negative key the same */
674 if (ret == -EAGAIN)
675 ret = -ENOKEY;
676 goto error5;
677 }
678
679 /* link the resulting key to the destination keyring if we can */
David Howells664cceb2005-09-28 17:03:15 +0100680 if (dest_ref) {
David Howellsf5895942014-03-14 17:44:49 +0000681 ret = key_permission(key_ref, KEY_NEED_LINK);
David Howells29db9192005-10-30 15:02:44 -0800682 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 goto error6;
684
David Howells664cceb2005-09-28 17:03:15 +0100685 ret = key_link(key_ref_to_ptr(dest_ref), key_ref_to_ptr(key_ref));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 if (ret < 0)
687 goto error6;
688 }
689
David Howells664cceb2005-09-28 17:03:15 +0100690 ret = key_ref_to_ptr(key_ref)->serial;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700692error6:
David Howells664cceb2005-09-28 17:03:15 +0100693 key_ref_put(key_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700694error5:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 key_type_put(ktype);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700696error4:
David Howells664cceb2005-09-28 17:03:15 +0100697 key_ref_put(dest_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700698error3:
David Howells664cceb2005-09-28 17:03:15 +0100699 key_ref_put(keyring_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700700error2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 kfree(description);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700702error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000704}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706/*
David Howells973c9f42011-01-20 16:38:33 +0000707 * Read a key's payload.
708 *
709 * The key must either grant the caller Read permission, or it must grant the
710 * caller Search permission when searched for from the process keyrings.
711 *
712 * If successful, we place up to buflen bytes of data into the buffer, if one
713 * is provided, and return the amount of data that is available in the key,
714 * irrespective of how much we copied into the buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 */
716long keyctl_read_key(key_serial_t keyid, char __user *buffer, size_t buflen)
717{
David Howells664cceb2005-09-28 17:03:15 +0100718 struct key *key;
719 key_ref_t key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 long ret;
721
722 /* find the key first */
David Howells55931222009-09-02 09:13:45 +0100723 key_ref = lookup_user_key(keyid, 0, 0);
David Howells664cceb2005-09-28 17:03:15 +0100724 if (IS_ERR(key_ref)) {
725 ret = -ENOKEY;
726 goto error;
727 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
David Howells664cceb2005-09-28 17:03:15 +0100729 key = key_ref_to_ptr(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
David Howells664cceb2005-09-28 17:03:15 +0100731 /* see if we can read it directly */
David Howellsf5895942014-03-14 17:44:49 +0000732 ret = key_permission(key_ref, KEY_NEED_READ);
David Howells29db9192005-10-30 15:02:44 -0800733 if (ret == 0)
David Howells664cceb2005-09-28 17:03:15 +0100734 goto can_read_key;
David Howells29db9192005-10-30 15:02:44 -0800735 if (ret != -EACCES)
736 goto error;
David Howells664cceb2005-09-28 17:03:15 +0100737
738 /* we can't; see if it's searchable from this process's keyrings
739 * - we automatically take account of the fact that it may be
740 * dangling off an instantiation key
741 */
742 if (!is_key_possessed(key_ref)) {
743 ret = -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 goto error2;
745 }
746
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 /* the key is probably readable - now try to read it */
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700748can_read_key:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 ret = key_validate(key);
750 if (ret == 0) {
751 ret = -EOPNOTSUPP;
752 if (key->type->read) {
753 /* read the data with the semaphore held (since we
754 * might sleep) */
755 down_read(&key->sem);
756 ret = key->type->read(key, buffer, buflen);
757 up_read(&key->sem);
758 }
759 }
760
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700761error2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 key_put(key);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700763error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000765}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767/*
David Howells973c9f42011-01-20 16:38:33 +0000768 * Change the ownership of a key
769 *
770 * The key must grant the caller Setattr permission for this to work, though
771 * the key need not be fully instantiated yet. For the UID to be changed, or
772 * for the GID to be changed to a group the caller is not a member of, the
773 * caller must have sysadmin capability. If either uid or gid is -1 then that
774 * attribute is not changed.
775 *
776 * If the UID is to be changed, the new user must have sufficient quota to
777 * accept the key. The quota deduction will be removed from the old user to
778 * the new user should the attribute be changed.
779 *
780 * If successful, 0 will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 */
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -0800782long keyctl_chown_key(key_serial_t id, uid_t user, gid_t group)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783{
Fredrik Tolf58016492006-06-26 00:24:51 -0700784 struct key_user *newowner, *zapowner = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 struct key *key;
David Howells664cceb2005-09-28 17:03:15 +0100786 key_ref_t key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 long ret;
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -0800788 kuid_t uid;
789 kgid_t gid;
790
791 uid = make_kuid(current_user_ns(), user);
792 gid = make_kgid(current_user_ns(), group);
793 ret = -EINVAL;
794 if ((user != (uid_t) -1) && !uid_valid(uid))
795 goto error;
796 if ((group != (gid_t) -1) && !gid_valid(gid))
797 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
799 ret = 0;
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -0800800 if (user == (uid_t) -1 && group == (gid_t) -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 goto error;
802
David Howells55931222009-09-02 09:13:45 +0100803 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
David Howellsf5895942014-03-14 17:44:49 +0000804 KEY_NEED_SETATTR);
David Howells664cceb2005-09-28 17:03:15 +0100805 if (IS_ERR(key_ref)) {
806 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 goto error;
808 }
809
David Howells664cceb2005-09-28 17:03:15 +0100810 key = key_ref_to_ptr(key_ref);
811
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 /* make the changes with the locks held to prevent chown/chown races */
813 ret = -EACCES;
814 down_write(&key->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
816 if (!capable(CAP_SYS_ADMIN)) {
817 /* only the sysadmin can chown a key to some other UID */
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -0800818 if (user != (uid_t) -1 && !uid_eq(key->uid, uid))
Fredrik Tolf58016492006-06-26 00:24:51 -0700819 goto error_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
821 /* only the sysadmin can set the key's GID to a group other
822 * than one of those that the current process subscribes to */
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -0800823 if (group != (gid_t) -1 && !gid_eq(gid, key->gid) && !in_group_p(gid))
Fredrik Tolf58016492006-06-26 00:24:51 -0700824 goto error_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 }
826
Fredrik Tolf58016492006-06-26 00:24:51 -0700827 /* change the UID */
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -0800828 if (user != (uid_t) -1 && !uid_eq(uid, key->uid)) {
Fredrik Tolf58016492006-06-26 00:24:51 -0700829 ret = -ENOMEM;
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -0800830 newowner = key_user_lookup(uid);
Fredrik Tolf58016492006-06-26 00:24:51 -0700831 if (!newowner)
832 goto error_put;
833
834 /* transfer the quota burden to the new user */
835 if (test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) {
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -0800836 unsigned maxkeys = uid_eq(uid, GLOBAL_ROOT_UID) ?
David Howells0b77f5b2008-04-29 01:01:32 -0700837 key_quota_root_maxkeys : key_quota_maxkeys;
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -0800838 unsigned maxbytes = uid_eq(uid, GLOBAL_ROOT_UID) ?
David Howells0b77f5b2008-04-29 01:01:32 -0700839 key_quota_root_maxbytes : key_quota_maxbytes;
840
Fredrik Tolf58016492006-06-26 00:24:51 -0700841 spin_lock(&newowner->lock);
David Howells0b77f5b2008-04-29 01:01:32 -0700842 if (newowner->qnkeys + 1 >= maxkeys ||
843 newowner->qnbytes + key->quotalen >= maxbytes ||
844 newowner->qnbytes + key->quotalen <
845 newowner->qnbytes)
Fredrik Tolf58016492006-06-26 00:24:51 -0700846 goto quota_overrun;
847
848 newowner->qnkeys++;
849 newowner->qnbytes += key->quotalen;
850 spin_unlock(&newowner->lock);
851
852 spin_lock(&key->user->lock);
853 key->user->qnkeys--;
854 key->user->qnbytes -= key->quotalen;
855 spin_unlock(&key->user->lock);
856 }
857
858 atomic_dec(&key->user->nkeys);
859 atomic_inc(&newowner->nkeys);
860
861 if (test_bit(KEY_FLAG_INSTANTIATED, &key->flags)) {
862 atomic_dec(&key->user->nikeys);
863 atomic_inc(&newowner->nikeys);
864 }
865
866 zapowner = key->user;
867 key->user = newowner;
868 key->uid = uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 }
870
871 /* change the GID */
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -0800872 if (group != (gid_t) -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 key->gid = gid;
874
875 ret = 0;
876
Fredrik Tolf58016492006-06-26 00:24:51 -0700877error_put:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 up_write(&key->sem);
879 key_put(key);
Fredrik Tolf58016492006-06-26 00:24:51 -0700880 if (zapowner)
881 key_user_put(zapowner);
882error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 return ret;
884
Fredrik Tolf58016492006-06-26 00:24:51 -0700885quota_overrun:
886 spin_unlock(&newowner->lock);
887 zapowner = newowner;
888 ret = -EDQUOT;
889 goto error_put;
David Howellsa8b17ed2011-01-20 16:38:27 +0000890}
Fredrik Tolf58016492006-06-26 00:24:51 -0700891
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892/*
David Howells973c9f42011-01-20 16:38:33 +0000893 * Change the permission mask on a key.
894 *
895 * The key must grant the caller Setattr permission for this to work, though
896 * the key need not be fully instantiated yet. If the caller does not have
897 * sysadmin capability, it may only change the permission on keys that it owns.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 */
899long keyctl_setperm_key(key_serial_t id, key_perm_t perm)
900{
901 struct key *key;
David Howells664cceb2005-09-28 17:03:15 +0100902 key_ref_t key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 long ret;
904
905 ret = -EINVAL;
David Howells664cceb2005-09-28 17:03:15 +0100906 if (perm & ~(KEY_POS_ALL | KEY_USR_ALL | KEY_GRP_ALL | KEY_OTH_ALL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 goto error;
908
David Howells55931222009-09-02 09:13:45 +0100909 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
David Howellsf5895942014-03-14 17:44:49 +0000910 KEY_NEED_SETATTR);
David Howells664cceb2005-09-28 17:03:15 +0100911 if (IS_ERR(key_ref)) {
912 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 goto error;
914 }
915
David Howells664cceb2005-09-28 17:03:15 +0100916 key = key_ref_to_ptr(key_ref);
917
David Howells76d8aea2005-06-23 22:00:49 -0700918 /* make the changes with the locks held to prevent chown/chmod races */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 ret = -EACCES;
920 down_write(&key->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
David Howells76d8aea2005-06-23 22:00:49 -0700922 /* if we're not the sysadmin, we can only change a key that we own */
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -0800923 if (capable(CAP_SYS_ADMIN) || uid_eq(key->uid, current_fsuid())) {
David Howells76d8aea2005-06-23 22:00:49 -0700924 key->perm = perm;
925 ret = 0;
926 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 up_write(&key->sem);
929 key_put(key);
David Howells76d8aea2005-06-23 22:00:49 -0700930error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000932}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
David Howells8bbf49762008-11-14 10:39:14 +1100934/*
David Howells973c9f42011-01-20 16:38:33 +0000935 * Get the destination keyring for instantiation and check that the caller has
936 * Write permission on it.
David Howells8bbf49762008-11-14 10:39:14 +1100937 */
938static long get_instantiation_keyring(key_serial_t ringid,
939 struct request_key_auth *rka,
940 struct key **_dest_keyring)
941{
942 key_ref_t dkref;
943
David Howellseca1bf52008-12-29 00:41:51 +0000944 *_dest_keyring = NULL;
945
David Howells8bbf49762008-11-14 10:39:14 +1100946 /* just return a NULL pointer if we weren't asked to make a link */
David Howellseca1bf52008-12-29 00:41:51 +0000947 if (ringid == 0)
David Howells8bbf49762008-11-14 10:39:14 +1100948 return 0;
David Howells8bbf49762008-11-14 10:39:14 +1100949
950 /* if a specific keyring is nominated by ID, then use that */
951 if (ringid > 0) {
David Howellsf5895942014-03-14 17:44:49 +0000952 dkref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
David Howells8bbf49762008-11-14 10:39:14 +1100953 if (IS_ERR(dkref))
954 return PTR_ERR(dkref);
955 *_dest_keyring = key_ref_to_ptr(dkref);
956 return 0;
957 }
958
959 if (ringid == KEY_SPEC_REQKEY_AUTH_KEY)
960 return -EINVAL;
961
962 /* otherwise specify the destination keyring recorded in the
963 * authorisation key (any KEY_SPEC_*_KEYRING) */
964 if (ringid >= KEY_SPEC_REQUESTOR_KEYRING) {
David Howells21279cf2009-10-15 10:14:35 +0100965 *_dest_keyring = key_get(rka->dest_keyring);
David Howells8bbf49762008-11-14 10:39:14 +1100966 return 0;
967 }
968
969 return -ENOKEY;
970}
971
David Howellsd84f4f92008-11-14 10:39:23 +1100972/*
David Howells973c9f42011-01-20 16:38:33 +0000973 * Change the request_key authorisation key on the current process.
David Howellsd84f4f92008-11-14 10:39:23 +1100974 */
975static int keyctl_change_reqkey_auth(struct key *key)
976{
977 struct cred *new;
978
979 new = prepare_creds();
980 if (!new)
981 return -ENOMEM;
982
983 key_put(new->request_key_auth);
984 new->request_key_auth = key_get(key);
985
986 return commit_creds(new);
987}
988
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989/*
David Howellsee009e4a02011-03-07 15:06:20 +0000990 * Copy the iovec data from userspace
991 */
992static long copy_from_user_iovec(void *buffer, const struct iovec *iov,
993 unsigned ioc)
994{
995 for (; ioc > 0; ioc--) {
996 if (copy_from_user(buffer, iov->iov_base, iov->iov_len) != 0)
997 return -EFAULT;
998 buffer += iov->iov_len;
999 iov++;
1000 }
1001 return 0;
1002}
1003
1004/*
David Howells973c9f42011-01-20 16:38:33 +00001005 * Instantiate a key with the specified payload and link the key into the
1006 * destination keyring if one is given.
1007 *
1008 * The caller must have the appropriate instantiation permit set for this to
1009 * work (see keyctl_assume_authority). No other permissions are required.
1010 *
1011 * If successful, 0 will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 */
David Howellsee009e4a02011-03-07 15:06:20 +00001013long keyctl_instantiate_key_common(key_serial_t id,
1014 const struct iovec *payload_iov,
1015 unsigned ioc,
1016 size_t plen,
1017 key_serial_t ringid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018{
David Howellsd84f4f92008-11-14 10:39:23 +11001019 const struct cred *cred = current_cred();
David Howells3e301482005-06-23 22:00:56 -07001020 struct request_key_auth *rka;
David Howells8bbf49762008-11-14 10:39:14 +11001021 struct key *instkey, *dest_keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 void *payload;
1023 long ret;
David Howells38bbca62008-04-29 01:01:19 -07001024 bool vm = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
David Howellsd84f4f92008-11-14 10:39:23 +11001026 kenter("%d,,%zu,%d", id, plen, ringid);
1027
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 ret = -EINVAL;
David Howells38bbca62008-04-29 01:01:19 -07001029 if (plen > 1024 * 1024 - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 goto error;
1031
David Howellsb5f545c2006-01-08 01:02:47 -08001032 /* the appropriate instantiation authorisation key must have been
1033 * assumed before calling this */
1034 ret = -EPERM;
David Howellsd84f4f92008-11-14 10:39:23 +11001035 instkey = cred->request_key_auth;
David Howellsb5f545c2006-01-08 01:02:47 -08001036 if (!instkey)
1037 goto error;
1038
1039 rka = instkey->payload.data;
1040 if (rka->target_key->serial != id)
1041 goto error;
1042
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 /* pull the payload in if one was supplied */
1044 payload = NULL;
1045
David Howellsee009e4a02011-03-07 15:06:20 +00001046 if (payload_iov) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 ret = -ENOMEM;
1048 payload = kmalloc(plen, GFP_KERNEL);
David Howells38bbca62008-04-29 01:01:19 -07001049 if (!payload) {
1050 if (plen <= PAGE_SIZE)
1051 goto error;
1052 vm = true;
1053 payload = vmalloc(plen);
1054 if (!payload)
1055 goto error;
1056 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
David Howellsee009e4a02011-03-07 15:06:20 +00001058 ret = copy_from_user_iovec(payload, payload_iov, ioc);
1059 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 goto error2;
1061 }
1062
David Howells3e301482005-06-23 22:00:56 -07001063 /* find the destination keyring amongst those belonging to the
1064 * requesting task */
David Howells8bbf49762008-11-14 10:39:14 +11001065 ret = get_instantiation_keyring(ringid, rka, &dest_keyring);
1066 if (ret < 0)
1067 goto error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
1069 /* instantiate the key and link it into a keyring */
David Howells3e301482005-06-23 22:00:56 -07001070 ret = key_instantiate_and_link(rka->target_key, payload, plen,
David Howells8bbf49762008-11-14 10:39:14 +11001071 dest_keyring, instkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
David Howells8bbf49762008-11-14 10:39:14 +11001073 key_put(dest_keyring);
David Howellsb5f545c2006-01-08 01:02:47 -08001074
1075 /* discard the assumed authority if it's just been disabled by
1076 * instantiation of the key */
David Howellsd84f4f92008-11-14 10:39:23 +11001077 if (ret == 0)
1078 keyctl_change_reqkey_auth(NULL);
David Howellsb5f545c2006-01-08 01:02:47 -08001079
1080error2:
David Howells38bbca62008-04-29 01:01:19 -07001081 if (!vm)
1082 kfree(payload);
1083 else
1084 vfree(payload);
David Howellsb5f545c2006-01-08 01:02:47 -08001085error:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +00001087}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089/*
David Howellsee009e4a02011-03-07 15:06:20 +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.
1097 */
1098long keyctl_instantiate_key(key_serial_t id,
1099 const void __user *_payload,
1100 size_t plen,
1101 key_serial_t ringid)
1102{
1103 if (_payload && plen) {
1104 struct iovec iov[1] = {
1105 [0].iov_base = (void __user *)_payload,
1106 [0].iov_len = plen
1107 };
1108
1109 return keyctl_instantiate_key_common(id, iov, 1, plen, ringid);
1110 }
1111
1112 return keyctl_instantiate_key_common(id, NULL, 0, 0, ringid);
1113}
1114
1115/*
1116 * Instantiate a key with the specified multipart payload and link the key into
1117 * the destination keyring if one is given.
1118 *
1119 * The caller must have the appropriate instantiation permit set for this to
1120 * work (see keyctl_assume_authority). No other permissions are required.
1121 *
1122 * If successful, 0 will be returned.
1123 */
1124long keyctl_instantiate_key_iov(key_serial_t id,
1125 const struct iovec __user *_payload_iov,
1126 unsigned ioc,
1127 key_serial_t ringid)
1128{
1129 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
1130 long ret;
1131
David Howells423b97882012-05-21 12:32:13 +01001132 if (!_payload_iov || !ioc)
David Howellsee009e4a02011-03-07 15:06:20 +00001133 goto no_payload;
1134
1135 ret = rw_copy_check_uvector(WRITE, _payload_iov, ioc,
Christopher Yeohac34ebb2012-05-31 16:26:42 -07001136 ARRAY_SIZE(iovstack), iovstack, &iov);
David Howellsee009e4a02011-03-07 15:06:20 +00001137 if (ret < 0)
Alan Coxa84a9212012-09-28 12:20:02 +01001138 goto err;
David Howellsee009e4a02011-03-07 15:06:20 +00001139 if (ret == 0)
1140 goto no_payload_free;
1141
1142 ret = keyctl_instantiate_key_common(id, iov, ioc, ret, ringid);
Alan Coxa84a9212012-09-28 12:20:02 +01001143err:
David Howellsee009e4a02011-03-07 15:06:20 +00001144 if (iov != iovstack)
1145 kfree(iov);
1146 return ret;
1147
1148no_payload_free:
1149 if (iov != iovstack)
1150 kfree(iov);
1151no_payload:
1152 return keyctl_instantiate_key_common(id, NULL, 0, 0, ringid);
1153}
1154
1155/*
David Howells973c9f42011-01-20 16:38:33 +00001156 * Negatively instantiate the key with the given timeout (in seconds) and link
1157 * the key into the destination keyring if one is given.
1158 *
1159 * The caller must have the appropriate instantiation permit set for this to
1160 * work (see keyctl_assume_authority). No other permissions are required.
1161 *
1162 * The key and any links to the key will be automatically garbage collected
1163 * after the timeout expires.
1164 *
1165 * Negative keys are used to rate limit repeated request_key() calls by causing
1166 * them to return -ENOKEY until the negative key expires.
1167 *
1168 * If successful, 0 will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 */
1170long keyctl_negate_key(key_serial_t id, unsigned timeout, key_serial_t ringid)
1171{
David Howellsfdd1b942011-03-07 15:06:09 +00001172 return keyctl_reject_key(id, timeout, ENOKEY, ringid);
1173}
1174
1175/*
1176 * Negatively instantiate the key with the given timeout (in seconds) and error
1177 * code and link the key into the destination keyring if one is given.
1178 *
1179 * The caller must have the appropriate instantiation permit set for this to
1180 * work (see keyctl_assume_authority). No other permissions are required.
1181 *
1182 * The key and any links to the key will be automatically garbage collected
1183 * after the timeout expires.
1184 *
1185 * Negative keys are used to rate limit repeated request_key() calls by causing
1186 * them to return the specified error code until the negative key expires.
1187 *
1188 * If successful, 0 will be returned.
1189 */
1190long keyctl_reject_key(key_serial_t id, unsigned timeout, unsigned error,
1191 key_serial_t ringid)
1192{
David Howellsd84f4f92008-11-14 10:39:23 +11001193 const struct cred *cred = current_cred();
David Howells3e301482005-06-23 22:00:56 -07001194 struct request_key_auth *rka;
David Howells8bbf49762008-11-14 10:39:14 +11001195 struct key *instkey, *dest_keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 long ret;
1197
David Howellsfdd1b942011-03-07 15:06:09 +00001198 kenter("%d,%u,%u,%d", id, timeout, error, ringid);
1199
1200 /* must be a valid error code and mustn't be a kernel special */
1201 if (error <= 0 ||
1202 error >= MAX_ERRNO ||
1203 error == ERESTARTSYS ||
1204 error == ERESTARTNOINTR ||
1205 error == ERESTARTNOHAND ||
1206 error == ERESTART_RESTARTBLOCK)
1207 return -EINVAL;
David Howellsd84f4f92008-11-14 10:39:23 +11001208
David Howellsb5f545c2006-01-08 01:02:47 -08001209 /* the appropriate instantiation authorisation key must have been
1210 * assumed before calling this */
1211 ret = -EPERM;
David Howellsd84f4f92008-11-14 10:39:23 +11001212 instkey = cred->request_key_auth;
David Howellsb5f545c2006-01-08 01:02:47 -08001213 if (!instkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
David Howells3e301482005-06-23 22:00:56 -07001216 rka = instkey->payload.data;
David Howellsb5f545c2006-01-08 01:02:47 -08001217 if (rka->target_key->serial != id)
1218 goto error;
David Howells3e301482005-06-23 22:00:56 -07001219
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 /* find the destination keyring if present (which must also be
1221 * writable) */
David Howells8bbf49762008-11-14 10:39:14 +11001222 ret = get_instantiation_keyring(ringid, rka, &dest_keyring);
1223 if (ret < 0)
1224 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
1226 /* instantiate the key and link it into a keyring */
David Howellsfdd1b942011-03-07 15:06:09 +00001227 ret = key_reject_and_link(rka->target_key, timeout, error,
David Howells8bbf49762008-11-14 10:39:14 +11001228 dest_keyring, instkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
David Howells8bbf49762008-11-14 10:39:14 +11001230 key_put(dest_keyring);
David Howellsb5f545c2006-01-08 01:02:47 -08001231
1232 /* discard the assumed authority if it's just been disabled by
1233 * instantiation of the key */
David Howellsd84f4f92008-11-14 10:39:23 +11001234 if (ret == 0)
1235 keyctl_change_reqkey_auth(NULL);
David Howellsb5f545c2006-01-08 01:02:47 -08001236
1237error:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +00001239}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241/*
David Howells973c9f42011-01-20 16:38:33 +00001242 * Read or set the default keyring in which request_key() will cache keys and
1243 * return the old setting.
1244 *
1245 * If a process keyring is specified then this will be created if it doesn't
1246 * yet exist. The old setting will be returned if successful.
David Howells3e301482005-06-23 22:00:56 -07001247 */
1248long keyctl_set_reqkey_keyring(int reqkey_defl)
1249{
David Howellsd84f4f92008-11-14 10:39:23 +11001250 struct cred *new;
1251 int ret, old_setting;
1252
1253 old_setting = current_cred_xxx(jit_keyring);
1254
1255 if (reqkey_defl == KEY_REQKEY_DEFL_NO_CHANGE)
1256 return old_setting;
1257
1258 new = prepare_creds();
1259 if (!new)
1260 return -ENOMEM;
David Howells3e301482005-06-23 22:00:56 -07001261
1262 switch (reqkey_defl) {
1263 case KEY_REQKEY_DEFL_THREAD_KEYRING:
David Howellsd84f4f92008-11-14 10:39:23 +11001264 ret = install_thread_keyring_to_cred(new);
David Howells3e301482005-06-23 22:00:56 -07001265 if (ret < 0)
David Howellsd84f4f92008-11-14 10:39:23 +11001266 goto error;
David Howells3e301482005-06-23 22:00:56 -07001267 goto set;
1268
1269 case KEY_REQKEY_DEFL_PROCESS_KEYRING:
David Howellsd84f4f92008-11-14 10:39:23 +11001270 ret = install_process_keyring_to_cred(new);
1271 if (ret < 0) {
1272 if (ret != -EEXIST)
1273 goto error;
1274 ret = 0;
1275 }
1276 goto set;
David Howells3e301482005-06-23 22:00:56 -07001277
1278 case KEY_REQKEY_DEFL_DEFAULT:
1279 case KEY_REQKEY_DEFL_SESSION_KEYRING:
1280 case KEY_REQKEY_DEFL_USER_KEYRING:
1281 case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
David Howellsd84f4f92008-11-14 10:39:23 +11001282 case KEY_REQKEY_DEFL_REQUESTOR_KEYRING:
1283 goto set;
David Howells3e301482005-06-23 22:00:56 -07001284
1285 case KEY_REQKEY_DEFL_NO_CHANGE:
David Howells3e301482005-06-23 22:00:56 -07001286 case KEY_REQKEY_DEFL_GROUP_KEYRING:
1287 default:
David Howellsd84f4f92008-11-14 10:39:23 +11001288 ret = -EINVAL;
1289 goto error;
David Howells3e301482005-06-23 22:00:56 -07001290 }
1291
David Howellsd84f4f92008-11-14 10:39:23 +11001292set:
1293 new->jit_keyring = reqkey_defl;
1294 commit_creds(new);
1295 return old_setting;
1296error:
1297 abort_creds(new);
Dan Carpenter4303ef12010-06-11 17:30:05 +01001298 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +00001299}
David Howellsd84f4f92008-11-14 10:39:23 +11001300
David Howells3e301482005-06-23 22:00:56 -07001301/*
David Howells973c9f42011-01-20 16:38:33 +00001302 * Set or clear the timeout on a key.
1303 *
1304 * Either the key must grant the caller Setattr permission or else the caller
1305 * must hold an instantiation authorisation token for the key.
1306 *
1307 * The timeout is either 0 to clear the timeout, or a number of seconds from
1308 * the current time. The key and any links to the key will be automatically
1309 * garbage collected after the timeout expires.
1310 *
1311 * If successful, 0 is returned.
David Howells017679c2006-01-08 01:02:43 -08001312 */
1313long keyctl_set_timeout(key_serial_t id, unsigned timeout)
1314{
David Howells91562352010-06-11 17:31:05 +01001315 struct key *key, *instkey;
David Howells017679c2006-01-08 01:02:43 -08001316 key_ref_t key_ref;
David Howells017679c2006-01-08 01:02:43 -08001317 long ret;
1318
David Howells55931222009-09-02 09:13:45 +01001319 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
David Howellsf5895942014-03-14 17:44:49 +00001320 KEY_NEED_SETATTR);
David Howells017679c2006-01-08 01:02:43 -08001321 if (IS_ERR(key_ref)) {
David Howells91562352010-06-11 17:31:05 +01001322 /* setting the timeout on a key under construction is permitted
1323 * if we have the authorisation token handy */
1324 if (PTR_ERR(key_ref) == -EACCES) {
1325 instkey = key_get_instantiation_authkey(id);
1326 if (!IS_ERR(instkey)) {
1327 key_put(instkey);
1328 key_ref = lookup_user_key(id,
1329 KEY_LOOKUP_PARTIAL,
1330 0);
1331 if (!IS_ERR(key_ref))
1332 goto okay;
1333 }
1334 }
1335
David Howells017679c2006-01-08 01:02:43 -08001336 ret = PTR_ERR(key_ref);
1337 goto error;
1338 }
1339
David Howells91562352010-06-11 17:31:05 +01001340okay:
David Howells017679c2006-01-08 01:02:43 -08001341 key = key_ref_to_ptr(key_ref);
Bryan Schumaker59e6b9c2012-02-24 14:14:50 -05001342 key_set_timeout(key, timeout);
David Howells017679c2006-01-08 01:02:43 -08001343 key_put(key);
1344
1345 ret = 0;
1346error:
1347 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +00001348}
David Howells017679c2006-01-08 01:02:43 -08001349
David Howells017679c2006-01-08 01:02:43 -08001350/*
David Howells973c9f42011-01-20 16:38:33 +00001351 * Assume (or clear) the authority to instantiate the specified key.
1352 *
1353 * This sets the authoritative token currently in force for key instantiation.
1354 * This must be done for a key to be instantiated. It has the effect of making
1355 * available all the keys from the caller of the request_key() that created a
1356 * key to request_key() calls made by the caller of this function.
1357 *
1358 * The caller must have the instantiation key in their process keyrings with a
1359 * Search permission grant available to the caller.
1360 *
1361 * If the ID given is 0, then the setting will be cleared and 0 returned.
1362 *
1363 * If the ID given has a matching an authorisation key, then that key will be
1364 * set and its ID will be returned. The authorisation key can be read to get
1365 * the callout information passed to request_key().
David Howellsb5f545c2006-01-08 01:02:47 -08001366 */
1367long keyctl_assume_authority(key_serial_t id)
1368{
1369 struct key *authkey;
1370 long ret;
1371
1372 /* special key IDs aren't permitted */
1373 ret = -EINVAL;
1374 if (id < 0)
1375 goto error;
1376
1377 /* we divest ourselves of authority if given an ID of 0 */
1378 if (id == 0) {
David Howellsd84f4f92008-11-14 10:39:23 +11001379 ret = keyctl_change_reqkey_auth(NULL);
David Howellsb5f545c2006-01-08 01:02:47 -08001380 goto error;
1381 }
1382
1383 /* attempt to assume the authority temporarily granted to us whilst we
1384 * instantiate the specified key
1385 * - the authorisation key must be in the current task's keyrings
1386 * somewhere
1387 */
1388 authkey = key_get_instantiation_authkey(id);
1389 if (IS_ERR(authkey)) {
1390 ret = PTR_ERR(authkey);
1391 goto error;
1392 }
1393
David Howellsd84f4f92008-11-14 10:39:23 +11001394 ret = keyctl_change_reqkey_auth(authkey);
1395 if (ret < 0)
1396 goto error;
1397 key_put(authkey);
David Howellsb5f545c2006-01-08 01:02:47 -08001398
David Howellsd84f4f92008-11-14 10:39:23 +11001399 ret = authkey->serial;
David Howellsb5f545c2006-01-08 01:02:47 -08001400error:
1401 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +00001402}
David Howellsb5f545c2006-01-08 01:02:47 -08001403
David Howells70a5bb72008-04-29 01:01:26 -07001404/*
David Howells973c9f42011-01-20 16:38:33 +00001405 * Get a key's the LSM security label.
1406 *
1407 * The key must grant the caller View permission for this to work.
1408 *
1409 * If there's a buffer, then up to buflen bytes of data will be placed into it.
1410 *
1411 * If successful, the amount of information available will be returned,
1412 * irrespective of how much was copied (including the terminal NUL).
David Howells70a5bb72008-04-29 01:01:26 -07001413 */
1414long keyctl_get_security(key_serial_t keyid,
1415 char __user *buffer,
1416 size_t buflen)
1417{
1418 struct key *key, *instkey;
1419 key_ref_t key_ref;
1420 char *context;
1421 long ret;
1422
David Howellsf5895942014-03-14 17:44:49 +00001423 key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_NEED_VIEW);
David Howells70a5bb72008-04-29 01:01:26 -07001424 if (IS_ERR(key_ref)) {
1425 if (PTR_ERR(key_ref) != -EACCES)
1426 return PTR_ERR(key_ref);
1427
1428 /* viewing a key under construction is also permitted if we
1429 * have the authorisation token handy */
1430 instkey = key_get_instantiation_authkey(keyid);
1431 if (IS_ERR(instkey))
Roel Kluinfa1cc7b2009-12-15 15:05:12 -08001432 return PTR_ERR(instkey);
David Howells70a5bb72008-04-29 01:01:26 -07001433 key_put(instkey);
1434
David Howells55931222009-09-02 09:13:45 +01001435 key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, 0);
David Howells70a5bb72008-04-29 01:01:26 -07001436 if (IS_ERR(key_ref))
1437 return PTR_ERR(key_ref);
1438 }
1439
1440 key = key_ref_to_ptr(key_ref);
1441 ret = security_key_getsecurity(key, &context);
1442 if (ret == 0) {
1443 /* if no information was returned, give userspace an empty
1444 * string */
1445 ret = 1;
1446 if (buffer && buflen > 0 &&
1447 copy_to_user(buffer, "", 1) != 0)
1448 ret = -EFAULT;
1449 } else if (ret > 0) {
1450 /* return as much data as there's room for */
1451 if (buffer && buflen > 0) {
1452 if (buflen > ret)
1453 buflen = ret;
1454
1455 if (copy_to_user(buffer, context, buflen) != 0)
1456 ret = -EFAULT;
1457 }
1458
1459 kfree(context);
1460 }
1461
1462 key_ref_put(key_ref);
1463 return ret;
1464}
1465
David Howellsee18d642009-09-02 09:14:21 +01001466/*
David Howells973c9f42011-01-20 16:38:33 +00001467 * Attempt to install the calling process's session keyring on the process's
1468 * parent process.
1469 *
1470 * The keyring must exist and must grant the caller LINK permission, and the
1471 * parent process must be single-threaded and must have the same effective
1472 * ownership as this process and mustn't be SUID/SGID.
1473 *
1474 * The keyring will be emplaced on the parent when it next resumes userspace.
1475 *
1476 * If successful, 0 will be returned.
David Howellsee18d642009-09-02 09:14:21 +01001477 */
1478long keyctl_session_to_parent(void)
1479{
1480 struct task_struct *me, *parent;
1481 const struct cred *mycred, *pcred;
Al Viro67d12142012-06-27 11:07:19 +04001482 struct callback_head *newwork, *oldwork;
David Howellsee18d642009-09-02 09:14:21 +01001483 key_ref_t keyring_r;
Oleg Nesterov413cd3d2012-05-11 10:59:08 +10001484 struct cred *cred;
David Howellsee18d642009-09-02 09:14:21 +01001485 int ret;
1486
David Howellsf5895942014-03-14 17:44:49 +00001487 keyring_r = lookup_user_key(KEY_SPEC_SESSION_KEYRING, 0, KEY_NEED_LINK);
David Howellsee18d642009-09-02 09:14:21 +01001488 if (IS_ERR(keyring_r))
1489 return PTR_ERR(keyring_r);
1490
Oleg Nesterov413cd3d2012-05-11 10:59:08 +10001491 ret = -ENOMEM;
Oleg Nesterov413cd3d2012-05-11 10:59:08 +10001492
David Howellsee18d642009-09-02 09:14:21 +01001493 /* our parent is going to need a new cred struct, a new tgcred struct
1494 * and new security data, so we allocate them here to prevent ENOMEM in
1495 * our parent */
David Howellsee18d642009-09-02 09:14:21 +01001496 cred = cred_alloc_blank();
1497 if (!cred)
Al Viro67d12142012-06-27 11:07:19 +04001498 goto error_keyring;
1499 newwork = &cred->rcu;
David Howellsee18d642009-09-02 09:14:21 +01001500
David Howells3a505972012-10-02 19:24:29 +01001501 cred->session_keyring = key_ref_to_ptr(keyring_r);
1502 keyring_r = NULL;
Al Viro67d12142012-06-27 11:07:19 +04001503 init_task_work(newwork, key_change_session_keyring);
David Howellsee18d642009-09-02 09:14:21 +01001504
1505 me = current;
David Howells9d1ac652010-09-10 09:59:46 +01001506 rcu_read_lock();
David Howellsee18d642009-09-02 09:14:21 +01001507 write_lock_irq(&tasklist_lock);
1508
David Howellsee18d642009-09-02 09:14:21 +01001509 ret = -EPERM;
Oleg Nesterov413cd3d2012-05-11 10:59:08 +10001510 oldwork = NULL;
1511 parent = me->real_parent;
David Howellsee18d642009-09-02 09:14:21 +01001512
1513 /* the parent mustn't be init and mustn't be a kernel thread */
1514 if (parent->pid <= 1 || !parent->mm)
Oleg Nesterov413cd3d2012-05-11 10:59:08 +10001515 goto unlock;
David Howellsee18d642009-09-02 09:14:21 +01001516
1517 /* the parent must be single threaded */
Oleg Nesterovdd98acf2010-05-26 14:43:23 -07001518 if (!thread_group_empty(parent))
Oleg Nesterov413cd3d2012-05-11 10:59:08 +10001519 goto unlock;
David Howellsee18d642009-09-02 09:14:21 +01001520
1521 /* the parent and the child must have different session keyrings or
1522 * there's no point */
1523 mycred = current_cred();
1524 pcred = __task_cred(parent);
1525 if (mycred == pcred ||
David Howells3a505972012-10-02 19:24:29 +01001526 mycred->session_keyring == pcred->session_keyring) {
Oleg Nesterov413cd3d2012-05-11 10:59:08 +10001527 ret = 0;
1528 goto unlock;
1529 }
David Howellsee18d642009-09-02 09:14:21 +01001530
1531 /* the parent must have the same effective ownership and mustn't be
1532 * SUID/SGID */
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -08001533 if (!uid_eq(pcred->uid, mycred->euid) ||
1534 !uid_eq(pcred->euid, mycred->euid) ||
1535 !uid_eq(pcred->suid, mycred->euid) ||
1536 !gid_eq(pcred->gid, mycred->egid) ||
1537 !gid_eq(pcred->egid, mycred->egid) ||
1538 !gid_eq(pcred->sgid, mycred->egid))
Oleg Nesterov413cd3d2012-05-11 10:59:08 +10001539 goto unlock;
David Howellsee18d642009-09-02 09:14:21 +01001540
1541 /* the keyrings must have the same UID */
David Howells3a505972012-10-02 19:24:29 +01001542 if ((pcred->session_keyring &&
Linus Torvalds2a74dbb2012-12-16 15:40:50 -08001543 !uid_eq(pcred->session_keyring->uid, mycred->euid)) ||
1544 !uid_eq(mycred->session_keyring->uid, mycred->euid))
Oleg Nesterov413cd3d2012-05-11 10:59:08 +10001545 goto unlock;
David Howellsee18d642009-09-02 09:14:21 +01001546
Oleg Nesterov413cd3d2012-05-11 10:59:08 +10001547 /* cancel an already pending keyring replacement */
1548 oldwork = task_work_cancel(parent, key_change_session_keyring);
David Howellsee18d642009-09-02 09:14:21 +01001549
1550 /* the replacement session keyring is applied just prior to userspace
1551 * restarting */
Al Viro67d12142012-06-27 11:07:19 +04001552 ret = task_work_add(parent, newwork, true);
Oleg Nesterov413cd3d2012-05-11 10:59:08 +10001553 if (!ret)
1554 newwork = NULL;
1555unlock:
David Howellsee18d642009-09-02 09:14:21 +01001556 write_unlock_irq(&tasklist_lock);
David Howells9d1ac652010-09-10 09:59:46 +01001557 rcu_read_unlock();
Al Viro67d12142012-06-27 11:07:19 +04001558 if (oldwork)
1559 put_cred(container_of(oldwork, struct cred, rcu));
1560 if (newwork)
1561 put_cred(cred);
David Howellsee18d642009-09-02 09:14:21 +01001562 return ret;
1563
1564error_keyring:
1565 key_ref_put(keyring_r);
1566 return ret;
1567}
1568
David Howellsb5f545c2006-01-08 01:02:47 -08001569/*
David Howells973c9f42011-01-20 16:38:33 +00001570 * The key control system call
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 */
Heiko Carstens938bb9f2009-01-14 14:14:30 +01001572SYSCALL_DEFINE5(keyctl, int, option, unsigned long, arg2, unsigned long, arg3,
1573 unsigned long, arg4, unsigned long, arg5)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574{
1575 switch (option) {
1576 case KEYCTL_GET_KEYRING_ID:
1577 return keyctl_get_keyring_ID((key_serial_t) arg2,
1578 (int) arg3);
1579
1580 case KEYCTL_JOIN_SESSION_KEYRING:
1581 return keyctl_join_session_keyring((const char __user *) arg2);
1582
1583 case KEYCTL_UPDATE:
1584 return keyctl_update_key((key_serial_t) arg2,
1585 (const void __user *) arg3,
1586 (size_t) arg4);
1587
1588 case KEYCTL_REVOKE:
1589 return keyctl_revoke_key((key_serial_t) arg2);
1590
1591 case KEYCTL_DESCRIBE:
1592 return keyctl_describe_key((key_serial_t) arg2,
1593 (char __user *) arg3,
1594 (unsigned) arg4);
1595
1596 case KEYCTL_CLEAR:
1597 return keyctl_keyring_clear((key_serial_t) arg2);
1598
1599 case KEYCTL_LINK:
1600 return keyctl_keyring_link((key_serial_t) arg2,
1601 (key_serial_t) arg3);
1602
1603 case KEYCTL_UNLINK:
1604 return keyctl_keyring_unlink((key_serial_t) arg2,
1605 (key_serial_t) arg3);
1606
1607 case KEYCTL_SEARCH:
1608 return keyctl_keyring_search((key_serial_t) arg2,
1609 (const char __user *) arg3,
1610 (const char __user *) arg4,
1611 (key_serial_t) arg5);
1612
1613 case KEYCTL_READ:
1614 return keyctl_read_key((key_serial_t) arg2,
1615 (char __user *) arg3,
1616 (size_t) arg4);
1617
1618 case KEYCTL_CHOWN:
1619 return keyctl_chown_key((key_serial_t) arg2,
1620 (uid_t) arg3,
1621 (gid_t) arg4);
1622
1623 case KEYCTL_SETPERM:
1624 return keyctl_setperm_key((key_serial_t) arg2,
1625 (key_perm_t) arg3);
1626
1627 case KEYCTL_INSTANTIATE:
1628 return keyctl_instantiate_key((key_serial_t) arg2,
1629 (const void __user *) arg3,
1630 (size_t) arg4,
1631 (key_serial_t) arg5);
1632
1633 case KEYCTL_NEGATE:
1634 return keyctl_negate_key((key_serial_t) arg2,
1635 (unsigned) arg3,
1636 (key_serial_t) arg4);
1637
David Howells3e301482005-06-23 22:00:56 -07001638 case KEYCTL_SET_REQKEY_KEYRING:
1639 return keyctl_set_reqkey_keyring(arg2);
1640
David Howells017679c2006-01-08 01:02:43 -08001641 case KEYCTL_SET_TIMEOUT:
1642 return keyctl_set_timeout((key_serial_t) arg2,
1643 (unsigned) arg3);
1644
David Howellsb5f545c2006-01-08 01:02:47 -08001645 case KEYCTL_ASSUME_AUTHORITY:
1646 return keyctl_assume_authority((key_serial_t) arg2);
1647
David Howells70a5bb72008-04-29 01:01:26 -07001648 case KEYCTL_GET_SECURITY:
1649 return keyctl_get_security((key_serial_t) arg2,
James Morris90bd49a2008-12-29 14:35:35 +11001650 (char __user *) arg3,
David Howells70a5bb72008-04-29 01:01:26 -07001651 (size_t) arg4);
1652
David Howellsee18d642009-09-02 09:14:21 +01001653 case KEYCTL_SESSION_TO_PARENT:
1654 return keyctl_session_to_parent();
1655
David Howellsfdd1b942011-03-07 15:06:09 +00001656 case KEYCTL_REJECT:
1657 return keyctl_reject_key((key_serial_t) arg2,
1658 (unsigned) arg3,
1659 (unsigned) arg4,
1660 (key_serial_t) arg5);
1661
David Howellsee009e4a02011-03-07 15:06:20 +00001662 case KEYCTL_INSTANTIATE_IOV:
1663 return keyctl_instantiate_key_iov(
1664 (key_serial_t) arg2,
1665 (const struct iovec __user *) arg3,
1666 (unsigned) arg4,
1667 (key_serial_t) arg5);
1668
David Howellsfd758152012-05-11 10:56:56 +01001669 case KEYCTL_INVALIDATE:
1670 return keyctl_invalidate_key((key_serial_t) arg2);
1671
David Howellsf36f8c72013-09-24 10:35:19 +01001672 case KEYCTL_GET_PERSISTENT:
1673 return keyctl_get_persistent((uid_t)arg2, (key_serial_t)arg3);
1674
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 default:
1676 return -EOPNOTSUPP;
1677 }
David Howellsa8b17ed2011-01-20 16:38:27 +00001678}