blob: e26f860e5f2ec5e88e77c797da6c8642b1291697 [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);
David Howells0c7774a2014-07-17 20:45:08 +0100409
410 /* Root is permitted to invalidate certain special keys */
411 if (capable(CAP_SYS_ADMIN)) {
412 key_ref = lookup_user_key(id, 0, 0);
413 if (IS_ERR(key_ref))
414 goto error;
415 if (test_bit(KEY_FLAG_ROOT_CAN_INVAL,
416 &key_ref_to_ptr(key_ref)->flags))
417 goto invalidate;
418 goto error_put;
419 }
420
David Howellsfd758152012-05-11 10:56:56 +0100421 goto error;
422 }
423
David Howells0c7774a2014-07-17 20:45:08 +0100424invalidate:
David Howellsfd758152012-05-11 10:56:56 +0100425 key_invalidate(key_ref_to_ptr(key_ref));
426 ret = 0;
David Howells0c7774a2014-07-17 20:45:08 +0100427error_put:
David Howellsfd758152012-05-11 10:56:56 +0100428 key_ref_put(key_ref);
429error:
430 kleave(" = %ld", ret);
431 return ret;
432}
433
434/*
David Howells973c9f42011-01-20 16:38:33 +0000435 * Clear the specified keyring, creating an empty process keyring if one of the
436 * special keyring IDs is used.
437 *
438 * The keyring must grant the caller Write permission for this to work. If
439 * successful, 0 will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 */
441long keyctl_keyring_clear(key_serial_t ringid)
442{
David Howells664cceb2005-09-28 17:03:15 +0100443 key_ref_t keyring_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 long ret;
445
David Howellsf5895942014-03-14 17:44:49 +0000446 keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
David Howells664cceb2005-09-28 17:03:15 +0100447 if (IS_ERR(keyring_ref)) {
448 ret = PTR_ERR(keyring_ref);
David Howells700920e2012-01-18 15:31:45 +0000449
450 /* Root is permitted to invalidate certain special keyrings */
451 if (capable(CAP_SYS_ADMIN)) {
452 keyring_ref = lookup_user_key(ringid, 0, 0);
453 if (IS_ERR(keyring_ref))
454 goto error;
455 if (test_bit(KEY_FLAG_ROOT_CAN_CLEAR,
456 &key_ref_to_ptr(keyring_ref)->flags))
457 goto clear;
458 goto error_put;
459 }
460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 goto error;
462 }
463
David Howells700920e2012-01-18 15:31:45 +0000464clear:
David Howells664cceb2005-09-28 17:03:15 +0100465 ret = keyring_clear(key_ref_to_ptr(keyring_ref));
David Howells700920e2012-01-18 15:31:45 +0000466error_put:
David Howells664cceb2005-09-28 17:03:15 +0100467 key_ref_put(keyring_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700468error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000470}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472/*
David Howells973c9f42011-01-20 16:38:33 +0000473 * Create a link from a keyring to a key if there's no matching key in the
474 * keyring, otherwise replace the link to the matching key with a link to the
475 * new key.
476 *
477 * The key must grant the caller Link permission and the the keyring must grant
478 * the caller Write permission. Furthermore, if an additional link is created,
479 * the keyring's quota will be extended.
480 *
481 * If successful, 0 will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 */
483long keyctl_keyring_link(key_serial_t id, key_serial_t ringid)
484{
David Howells664cceb2005-09-28 17:03:15 +0100485 key_ref_t keyring_ref, key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 long ret;
487
David Howellsf5895942014-03-14 17:44:49 +0000488 keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
David Howells664cceb2005-09-28 17:03:15 +0100489 if (IS_ERR(keyring_ref)) {
490 ret = PTR_ERR(keyring_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 goto error;
492 }
493
David Howellsf5895942014-03-14 17:44:49 +0000494 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE, KEY_NEED_LINK);
David Howells664cceb2005-09-28 17:03:15 +0100495 if (IS_ERR(key_ref)) {
496 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 goto error2;
498 }
499
David Howells664cceb2005-09-28 17:03:15 +0100500 ret = key_link(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
David Howells664cceb2005-09-28 17:03:15 +0100502 key_ref_put(key_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700503error2:
David Howells664cceb2005-09-28 17:03:15 +0100504 key_ref_put(keyring_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700505error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000507}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509/*
David Howells973c9f42011-01-20 16:38:33 +0000510 * Unlink a key from a keyring.
511 *
512 * The keyring must grant the caller Write permission for this to work; the key
513 * itself need not grant the caller anything. If the last link to a key is
514 * removed then that key will be scheduled for destruction.
515 *
516 * If successful, 0 will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 */
518long keyctl_keyring_unlink(key_serial_t id, key_serial_t ringid)
519{
David Howells664cceb2005-09-28 17:03:15 +0100520 key_ref_t keyring_ref, key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 long ret;
522
David Howellsf5895942014-03-14 17:44:49 +0000523 keyring_ref = lookup_user_key(ringid, 0, KEY_NEED_WRITE);
David Howells664cceb2005-09-28 17:03:15 +0100524 if (IS_ERR(keyring_ref)) {
525 ret = PTR_ERR(keyring_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 goto error;
527 }
528
David Howells55931222009-09-02 09:13:45 +0100529 key_ref = lookup_user_key(id, KEY_LOOKUP_FOR_UNLINK, 0);
David Howells664cceb2005-09-28 17:03:15 +0100530 if (IS_ERR(key_ref)) {
531 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 goto error2;
533 }
534
David Howells664cceb2005-09-28 17:03:15 +0100535 ret = key_unlink(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
David Howells664cceb2005-09-28 17:03:15 +0100537 key_ref_put(key_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700538error2:
David Howells664cceb2005-09-28 17:03:15 +0100539 key_ref_put(keyring_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700540error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000542}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544/*
David Howells973c9f42011-01-20 16:38:33 +0000545 * Return a description of a key to userspace.
546 *
547 * The key must grant the caller View permission for this to work.
548 *
549 * If there's a buffer, we place up to buflen bytes of data into it formatted
550 * in the following way:
551 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 * type;uid;gid;perm;description<NUL>
David Howells973c9f42011-01-20 16:38:33 +0000553 *
554 * If successful, we return the amount of description available, irrespective
555 * of how much we may have copied into the buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 */
557long keyctl_describe_key(key_serial_t keyid,
558 char __user *buffer,
559 size_t buflen)
560{
David Howells3e301482005-06-23 22:00:56 -0700561 struct key *key, *instkey;
David Howells664cceb2005-09-28 17:03:15 +0100562 key_ref_t key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 char *tmpbuf;
564 long ret;
565
David Howellsf5895942014-03-14 17:44:49 +0000566 key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_NEED_VIEW);
David Howells664cceb2005-09-28 17:03:15 +0100567 if (IS_ERR(key_ref)) {
David Howells3e301482005-06-23 22:00:56 -0700568 /* viewing a key under construction is permitted if we have the
569 * authorisation token handy */
David Howells664cceb2005-09-28 17:03:15 +0100570 if (PTR_ERR(key_ref) == -EACCES) {
David Howells3e301482005-06-23 22:00:56 -0700571 instkey = key_get_instantiation_authkey(keyid);
572 if (!IS_ERR(instkey)) {
573 key_put(instkey);
David Howells8bbf49762008-11-14 10:39:14 +1100574 key_ref = lookup_user_key(keyid,
David Howells55931222009-09-02 09:13:45 +0100575 KEY_LOOKUP_PARTIAL,
576 0);
David Howells664cceb2005-09-28 17:03:15 +0100577 if (!IS_ERR(key_ref))
David Howells3e301482005-06-23 22:00:56 -0700578 goto okay;
579 }
580 }
581
David Howells664cceb2005-09-28 17:03:15 +0100582 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 goto error;
584 }
585
David Howells3e301482005-06-23 22:00:56 -0700586okay:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 /* calculate how much description we're going to return */
588 ret = -ENOMEM;
589 tmpbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
590 if (!tmpbuf)
591 goto error2;
592
David Howells664cceb2005-09-28 17:03:15 +0100593 key = key_ref_to_ptr(key_ref);
594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 ret = snprintf(tmpbuf, PAGE_SIZE - 1,
David Howells664cceb2005-09-28 17:03:15 +0100596 "%s;%d;%d;%08x;%s",
David Howells94fd8402010-06-28 14:05:04 +0100597 key->type->name,
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -0800598 from_kuid_munged(current_user_ns(), key->uid),
599 from_kgid_munged(current_user_ns(), key->gid),
David Howells94fd8402010-06-28 14:05:04 +0100600 key->perm,
601 key->description ?: "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
603 /* include a NUL char at the end of the data */
604 if (ret > PAGE_SIZE - 1)
605 ret = PAGE_SIZE - 1;
606 tmpbuf[ret] = 0;
607 ret++;
608
609 /* consider returning the data */
610 if (buffer && buflen > 0) {
611 if (buflen > ret)
612 buflen = ret;
613
614 if (copy_to_user(buffer, tmpbuf, buflen) != 0)
615 ret = -EFAULT;
616 }
617
618 kfree(tmpbuf);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700619error2:
David Howells664cceb2005-09-28 17:03:15 +0100620 key_ref_put(key_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700621error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000623}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625/*
David Howells973c9f42011-01-20 16:38:33 +0000626 * Search the specified keyring and any keyrings it links to for a matching
627 * key. Only keyrings that grant the caller Search permission will be searched
628 * (this includes the starting keyring). Only keys with Search permission can
629 * be found.
630 *
631 * If successful, the found key will be linked to the destination keyring if
632 * supplied and the key has Link permission, and the found key ID will be
633 * returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 */
635long keyctl_keyring_search(key_serial_t ringid,
636 const char __user *_type,
637 const char __user *_description,
638 key_serial_t destringid)
639{
640 struct key_type *ktype;
David Howells664cceb2005-09-28 17:03:15 +0100641 key_ref_t keyring_ref, key_ref, dest_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 char type[32], *description;
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800643 long ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
645 /* pull the type and description into kernel space */
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800646 ret = key_get_type_from_user(type, _type, sizeof(type));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 if (ret < 0)
648 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800650 description = strndup_user(_description, PAGE_SIZE);
651 if (IS_ERR(description)) {
652 ret = PTR_ERR(description);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 goto error;
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800654 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
656 /* get the keyring at which to begin the search */
David Howellsf5895942014-03-14 17:44:49 +0000657 keyring_ref = lookup_user_key(ringid, 0, KEY_NEED_SEARCH);
David Howells664cceb2005-09-28 17:03:15 +0100658 if (IS_ERR(keyring_ref)) {
659 ret = PTR_ERR(keyring_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 goto error2;
661 }
662
663 /* get the destination keyring if specified */
David Howells664cceb2005-09-28 17:03:15 +0100664 dest_ref = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 if (destringid) {
David Howells55931222009-09-02 09:13:45 +0100666 dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE,
David Howellsf5895942014-03-14 17:44:49 +0000667 KEY_NEED_WRITE);
David Howells664cceb2005-09-28 17:03:15 +0100668 if (IS_ERR(dest_ref)) {
669 ret = PTR_ERR(dest_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 goto error3;
671 }
672 }
673
674 /* find the key type */
675 ktype = key_type_lookup(type);
676 if (IS_ERR(ktype)) {
677 ret = PTR_ERR(ktype);
678 goto error4;
679 }
680
681 /* do the search */
David Howells664cceb2005-09-28 17:03:15 +0100682 key_ref = keyring_search(keyring_ref, ktype, description);
683 if (IS_ERR(key_ref)) {
684 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
686 /* treat lack or presence of a negative key the same */
687 if (ret == -EAGAIN)
688 ret = -ENOKEY;
689 goto error5;
690 }
691
692 /* link the resulting key to the destination keyring if we can */
David Howells664cceb2005-09-28 17:03:15 +0100693 if (dest_ref) {
David Howellsf5895942014-03-14 17:44:49 +0000694 ret = key_permission(key_ref, KEY_NEED_LINK);
David Howells29db9192005-10-30 15:02:44 -0800695 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 goto error6;
697
David Howells664cceb2005-09-28 17:03:15 +0100698 ret = key_link(key_ref_to_ptr(dest_ref), key_ref_to_ptr(key_ref));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 if (ret < 0)
700 goto error6;
701 }
702
David Howells664cceb2005-09-28 17:03:15 +0100703 ret = key_ref_to_ptr(key_ref)->serial;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700705error6:
David Howells664cceb2005-09-28 17:03:15 +0100706 key_ref_put(key_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700707error5:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 key_type_put(ktype);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700709error4:
David Howells664cceb2005-09-28 17:03:15 +0100710 key_ref_put(dest_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700711error3:
David Howells664cceb2005-09-28 17:03:15 +0100712 key_ref_put(keyring_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700713error2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 kfree(description);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700715error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000717}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719/*
David Howells973c9f42011-01-20 16:38:33 +0000720 * Read a key's payload.
721 *
722 * The key must either grant the caller Read permission, or it must grant the
723 * caller Search permission when searched for from the process keyrings.
724 *
725 * If successful, we place up to buflen bytes of data into the buffer, if one
726 * is provided, and return the amount of data that is available in the key,
727 * irrespective of how much we copied into the buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 */
729long keyctl_read_key(key_serial_t keyid, char __user *buffer, size_t buflen)
730{
David Howells664cceb2005-09-28 17:03:15 +0100731 struct key *key;
732 key_ref_t key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 long ret;
734
735 /* find the key first */
David Howells55931222009-09-02 09:13:45 +0100736 key_ref = lookup_user_key(keyid, 0, 0);
David Howells664cceb2005-09-28 17:03:15 +0100737 if (IS_ERR(key_ref)) {
738 ret = -ENOKEY;
739 goto error;
740 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
David Howells664cceb2005-09-28 17:03:15 +0100742 key = key_ref_to_ptr(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
David Howells664cceb2005-09-28 17:03:15 +0100744 /* see if we can read it directly */
David Howellsf5895942014-03-14 17:44:49 +0000745 ret = key_permission(key_ref, KEY_NEED_READ);
David Howells29db9192005-10-30 15:02:44 -0800746 if (ret == 0)
David Howells664cceb2005-09-28 17:03:15 +0100747 goto can_read_key;
David Howells29db9192005-10-30 15:02:44 -0800748 if (ret != -EACCES)
749 goto error;
David Howells664cceb2005-09-28 17:03:15 +0100750
751 /* we can't; see if it's searchable from this process's keyrings
752 * - we automatically take account of the fact that it may be
753 * dangling off an instantiation key
754 */
755 if (!is_key_possessed(key_ref)) {
756 ret = -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 goto error2;
758 }
759
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 /* the key is probably readable - now try to read it */
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700761can_read_key:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 ret = key_validate(key);
763 if (ret == 0) {
764 ret = -EOPNOTSUPP;
765 if (key->type->read) {
766 /* read the data with the semaphore held (since we
767 * might sleep) */
768 down_read(&key->sem);
769 ret = key->type->read(key, buffer, buflen);
770 up_read(&key->sem);
771 }
772 }
773
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700774error2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 key_put(key);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700776error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000778}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780/*
David Howells973c9f42011-01-20 16:38:33 +0000781 * Change the ownership of a key
782 *
783 * The key must grant the caller Setattr permission for this to work, though
784 * the key need not be fully instantiated yet. For the UID to be changed, or
785 * for the GID to be changed to a group the caller is not a member of, the
786 * caller must have sysadmin capability. If either uid or gid is -1 then that
787 * attribute is not changed.
788 *
789 * If the UID is to be changed, the new user must have sufficient quota to
790 * accept the key. The quota deduction will be removed from the old user to
791 * the new user should the attribute be changed.
792 *
793 * If successful, 0 will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 */
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -0800795long keyctl_chown_key(key_serial_t id, uid_t user, gid_t group)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796{
Fredrik Tolf58016492006-06-26 00:24:51 -0700797 struct key_user *newowner, *zapowner = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 struct key *key;
David Howells664cceb2005-09-28 17:03:15 +0100799 key_ref_t key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 long ret;
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -0800801 kuid_t uid;
802 kgid_t gid;
803
804 uid = make_kuid(current_user_ns(), user);
805 gid = make_kgid(current_user_ns(), group);
806 ret = -EINVAL;
807 if ((user != (uid_t) -1) && !uid_valid(uid))
808 goto error;
809 if ((group != (gid_t) -1) && !gid_valid(gid))
810 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
812 ret = 0;
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -0800813 if (user == (uid_t) -1 && group == (gid_t) -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 goto error;
815
David Howells55931222009-09-02 09:13:45 +0100816 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
David Howellsf5895942014-03-14 17:44:49 +0000817 KEY_NEED_SETATTR);
David Howells664cceb2005-09-28 17:03:15 +0100818 if (IS_ERR(key_ref)) {
819 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 goto error;
821 }
822
David Howells664cceb2005-09-28 17:03:15 +0100823 key = key_ref_to_ptr(key_ref);
824
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 /* make the changes with the locks held to prevent chown/chown races */
826 ret = -EACCES;
827 down_write(&key->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
829 if (!capable(CAP_SYS_ADMIN)) {
830 /* only the sysadmin can chown a key to some other UID */
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -0800831 if (user != (uid_t) -1 && !uid_eq(key->uid, uid))
Fredrik Tolf58016492006-06-26 00:24:51 -0700832 goto error_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
834 /* only the sysadmin can set the key's GID to a group other
835 * than one of those that the current process subscribes to */
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -0800836 if (group != (gid_t) -1 && !gid_eq(gid, key->gid) && !in_group_p(gid))
Fredrik Tolf58016492006-06-26 00:24:51 -0700837 goto error_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 }
839
Fredrik Tolf58016492006-06-26 00:24:51 -0700840 /* change the UID */
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -0800841 if (user != (uid_t) -1 && !uid_eq(uid, key->uid)) {
Fredrik Tolf58016492006-06-26 00:24:51 -0700842 ret = -ENOMEM;
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -0800843 newowner = key_user_lookup(uid);
Fredrik Tolf58016492006-06-26 00:24:51 -0700844 if (!newowner)
845 goto error_put;
846
847 /* transfer the quota burden to the new user */
848 if (test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) {
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -0800849 unsigned maxkeys = uid_eq(uid, GLOBAL_ROOT_UID) ?
David Howells0b77f5b2008-04-29 01:01:32 -0700850 key_quota_root_maxkeys : key_quota_maxkeys;
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -0800851 unsigned maxbytes = uid_eq(uid, GLOBAL_ROOT_UID) ?
David Howells0b77f5b2008-04-29 01:01:32 -0700852 key_quota_root_maxbytes : key_quota_maxbytes;
853
Fredrik Tolf58016492006-06-26 00:24:51 -0700854 spin_lock(&newowner->lock);
David Howells0b77f5b2008-04-29 01:01:32 -0700855 if (newowner->qnkeys + 1 >= maxkeys ||
856 newowner->qnbytes + key->quotalen >= maxbytes ||
857 newowner->qnbytes + key->quotalen <
858 newowner->qnbytes)
Fredrik Tolf58016492006-06-26 00:24:51 -0700859 goto quota_overrun;
860
861 newowner->qnkeys++;
862 newowner->qnbytes += key->quotalen;
863 spin_unlock(&newowner->lock);
864
865 spin_lock(&key->user->lock);
866 key->user->qnkeys--;
867 key->user->qnbytes -= key->quotalen;
868 spin_unlock(&key->user->lock);
869 }
870
871 atomic_dec(&key->user->nkeys);
872 atomic_inc(&newowner->nkeys);
873
874 if (test_bit(KEY_FLAG_INSTANTIATED, &key->flags)) {
875 atomic_dec(&key->user->nikeys);
876 atomic_inc(&newowner->nikeys);
877 }
878
879 zapowner = key->user;
880 key->user = newowner;
881 key->uid = uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 }
883
884 /* change the GID */
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -0800885 if (group != (gid_t) -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 key->gid = gid;
887
888 ret = 0;
889
Fredrik Tolf58016492006-06-26 00:24:51 -0700890error_put:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 up_write(&key->sem);
892 key_put(key);
Fredrik Tolf58016492006-06-26 00:24:51 -0700893 if (zapowner)
894 key_user_put(zapowner);
895error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 return ret;
897
Fredrik Tolf58016492006-06-26 00:24:51 -0700898quota_overrun:
899 spin_unlock(&newowner->lock);
900 zapowner = newowner;
901 ret = -EDQUOT;
902 goto error_put;
David Howellsa8b17ed2011-01-20 16:38:27 +0000903}
Fredrik Tolf58016492006-06-26 00:24:51 -0700904
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905/*
David Howells973c9f42011-01-20 16:38:33 +0000906 * Change the permission mask on a key.
907 *
908 * The key must grant the caller Setattr permission for this to work, though
909 * the key need not be fully instantiated yet. If the caller does not have
910 * sysadmin capability, it may only change the permission on keys that it owns.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 */
912long keyctl_setperm_key(key_serial_t id, key_perm_t perm)
913{
914 struct key *key;
David Howells664cceb2005-09-28 17:03:15 +0100915 key_ref_t key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 long ret;
917
918 ret = -EINVAL;
David Howells664cceb2005-09-28 17:03:15 +0100919 if (perm & ~(KEY_POS_ALL | KEY_USR_ALL | KEY_GRP_ALL | KEY_OTH_ALL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 goto error;
921
David Howells55931222009-09-02 09:13:45 +0100922 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
David Howellsf5895942014-03-14 17:44:49 +0000923 KEY_NEED_SETATTR);
David Howells664cceb2005-09-28 17:03:15 +0100924 if (IS_ERR(key_ref)) {
925 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 goto error;
927 }
928
David Howells664cceb2005-09-28 17:03:15 +0100929 key = key_ref_to_ptr(key_ref);
930
David Howells76d8aea2005-06-23 22:00:49 -0700931 /* make the changes with the locks held to prevent chown/chmod races */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 ret = -EACCES;
933 down_write(&key->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
David Howells76d8aea2005-06-23 22:00:49 -0700935 /* if we're not the sysadmin, we can only change a key that we own */
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -0800936 if (capable(CAP_SYS_ADMIN) || uid_eq(key->uid, current_fsuid())) {
David Howells76d8aea2005-06-23 22:00:49 -0700937 key->perm = perm;
938 ret = 0;
939 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 up_write(&key->sem);
942 key_put(key);
David Howells76d8aea2005-06-23 22:00:49 -0700943error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000945}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
David Howells8bbf49762008-11-14 10:39:14 +1100947/*
David Howells973c9f42011-01-20 16:38:33 +0000948 * Get the destination keyring for instantiation and check that the caller has
949 * Write permission on it.
David Howells8bbf49762008-11-14 10:39:14 +1100950 */
951static long get_instantiation_keyring(key_serial_t ringid,
952 struct request_key_auth *rka,
953 struct key **_dest_keyring)
954{
955 key_ref_t dkref;
956
David Howellseca1bf52008-12-29 00:41:51 +0000957 *_dest_keyring = NULL;
958
David Howells8bbf49762008-11-14 10:39:14 +1100959 /* just return a NULL pointer if we weren't asked to make a link */
David Howellseca1bf52008-12-29 00:41:51 +0000960 if (ringid == 0)
David Howells8bbf49762008-11-14 10:39:14 +1100961 return 0;
David Howells8bbf49762008-11-14 10:39:14 +1100962
963 /* if a specific keyring is nominated by ID, then use that */
964 if (ringid > 0) {
David Howellsf5895942014-03-14 17:44:49 +0000965 dkref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
David Howells8bbf49762008-11-14 10:39:14 +1100966 if (IS_ERR(dkref))
967 return PTR_ERR(dkref);
968 *_dest_keyring = key_ref_to_ptr(dkref);
969 return 0;
970 }
971
972 if (ringid == KEY_SPEC_REQKEY_AUTH_KEY)
973 return -EINVAL;
974
975 /* otherwise specify the destination keyring recorded in the
976 * authorisation key (any KEY_SPEC_*_KEYRING) */
977 if (ringid >= KEY_SPEC_REQUESTOR_KEYRING) {
David Howells21279cf2009-10-15 10:14:35 +0100978 *_dest_keyring = key_get(rka->dest_keyring);
David Howells8bbf49762008-11-14 10:39:14 +1100979 return 0;
980 }
981
982 return -ENOKEY;
983}
984
David Howellsd84f4f92008-11-14 10:39:23 +1100985/*
David Howells973c9f42011-01-20 16:38:33 +0000986 * Change the request_key authorisation key on the current process.
David Howellsd84f4f92008-11-14 10:39:23 +1100987 */
988static int keyctl_change_reqkey_auth(struct key *key)
989{
990 struct cred *new;
991
992 new = prepare_creds();
993 if (!new)
994 return -ENOMEM;
995
996 key_put(new->request_key_auth);
997 new->request_key_auth = key_get(key);
998
999 return commit_creds(new);
1000}
1001
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002/*
David Howellsee009e4a02011-03-07 15:06:20 +00001003 * Copy the iovec data from userspace
1004 */
1005static long copy_from_user_iovec(void *buffer, const struct iovec *iov,
1006 unsigned ioc)
1007{
1008 for (; ioc > 0; ioc--) {
1009 if (copy_from_user(buffer, iov->iov_base, iov->iov_len) != 0)
1010 return -EFAULT;
1011 buffer += iov->iov_len;
1012 iov++;
1013 }
1014 return 0;
1015}
1016
1017/*
David Howells973c9f42011-01-20 16:38:33 +00001018 * Instantiate a key with the specified payload and link the key into the
1019 * destination keyring if one is given.
1020 *
1021 * The caller must have the appropriate instantiation permit set for this to
1022 * work (see keyctl_assume_authority). No other permissions are required.
1023 *
1024 * If successful, 0 will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 */
David Howellsee009e4a02011-03-07 15:06:20 +00001026long keyctl_instantiate_key_common(key_serial_t id,
1027 const struct iovec *payload_iov,
1028 unsigned ioc,
1029 size_t plen,
1030 key_serial_t ringid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031{
David Howellsd84f4f92008-11-14 10:39:23 +11001032 const struct cred *cred = current_cred();
David Howells3e301482005-06-23 22:00:56 -07001033 struct request_key_auth *rka;
David Howells8bbf49762008-11-14 10:39:14 +11001034 struct key *instkey, *dest_keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 void *payload;
1036 long ret;
David Howells38bbca62008-04-29 01:01:19 -07001037 bool vm = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
David Howellsd84f4f92008-11-14 10:39:23 +11001039 kenter("%d,,%zu,%d", id, plen, ringid);
1040
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 ret = -EINVAL;
David Howells38bbca62008-04-29 01:01:19 -07001042 if (plen > 1024 * 1024 - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 goto error;
1044
David Howellsb5f545c2006-01-08 01:02:47 -08001045 /* the appropriate instantiation authorisation key must have been
1046 * assumed before calling this */
1047 ret = -EPERM;
David Howellsd84f4f92008-11-14 10:39:23 +11001048 instkey = cred->request_key_auth;
David Howellsb5f545c2006-01-08 01:02:47 -08001049 if (!instkey)
1050 goto error;
1051
1052 rka = instkey->payload.data;
1053 if (rka->target_key->serial != id)
1054 goto error;
1055
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 /* pull the payload in if one was supplied */
1057 payload = NULL;
1058
David Howellsee009e4a02011-03-07 15:06:20 +00001059 if (payload_iov) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 ret = -ENOMEM;
1061 payload = kmalloc(plen, GFP_KERNEL);
David Howells38bbca62008-04-29 01:01:19 -07001062 if (!payload) {
1063 if (plen <= PAGE_SIZE)
1064 goto error;
1065 vm = true;
1066 payload = vmalloc(plen);
1067 if (!payload)
1068 goto error;
1069 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
David Howellsee009e4a02011-03-07 15:06:20 +00001071 ret = copy_from_user_iovec(payload, payload_iov, ioc);
1072 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 goto error2;
1074 }
1075
David Howells3e301482005-06-23 22:00:56 -07001076 /* find the destination keyring amongst those belonging to the
1077 * requesting task */
David Howells8bbf49762008-11-14 10:39:14 +11001078 ret = get_instantiation_keyring(ringid, rka, &dest_keyring);
1079 if (ret < 0)
1080 goto error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
1082 /* instantiate the key and link it into a keyring */
David Howells3e301482005-06-23 22:00:56 -07001083 ret = key_instantiate_and_link(rka->target_key, payload, plen,
David Howells8bbf49762008-11-14 10:39:14 +11001084 dest_keyring, instkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
David Howells8bbf49762008-11-14 10:39:14 +11001086 key_put(dest_keyring);
David Howellsb5f545c2006-01-08 01:02:47 -08001087
1088 /* discard the assumed authority if it's just been disabled by
1089 * instantiation of the key */
David Howellsd84f4f92008-11-14 10:39:23 +11001090 if (ret == 0)
1091 keyctl_change_reqkey_auth(NULL);
David Howellsb5f545c2006-01-08 01:02:47 -08001092
1093error2:
David Howells38bbca62008-04-29 01:01:19 -07001094 if (!vm)
1095 kfree(payload);
1096 else
1097 vfree(payload);
David Howellsb5f545c2006-01-08 01:02:47 -08001098error:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +00001100}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102/*
David Howellsee009e4a02011-03-07 15:06:20 +00001103 * Instantiate a key with the specified payload and link the key into the
1104 * destination keyring if one is given.
1105 *
1106 * The caller must have the appropriate instantiation permit set for this to
1107 * work (see keyctl_assume_authority). No other permissions are required.
1108 *
1109 * If successful, 0 will be returned.
1110 */
1111long keyctl_instantiate_key(key_serial_t id,
1112 const void __user *_payload,
1113 size_t plen,
1114 key_serial_t ringid)
1115{
1116 if (_payload && plen) {
1117 struct iovec iov[1] = {
1118 [0].iov_base = (void __user *)_payload,
1119 [0].iov_len = plen
1120 };
1121
1122 return keyctl_instantiate_key_common(id, iov, 1, plen, ringid);
1123 }
1124
1125 return keyctl_instantiate_key_common(id, NULL, 0, 0, ringid);
1126}
1127
1128/*
1129 * Instantiate a key with the specified multipart payload and link the key into
1130 * the destination keyring if one is given.
1131 *
1132 * The caller must have the appropriate instantiation permit set for this to
1133 * work (see keyctl_assume_authority). No other permissions are required.
1134 *
1135 * If successful, 0 will be returned.
1136 */
1137long keyctl_instantiate_key_iov(key_serial_t id,
1138 const struct iovec __user *_payload_iov,
1139 unsigned ioc,
1140 key_serial_t ringid)
1141{
1142 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
1143 long ret;
1144
David Howells423b97882012-05-21 12:32:13 +01001145 if (!_payload_iov || !ioc)
David Howellsee009e4a02011-03-07 15:06:20 +00001146 goto no_payload;
1147
1148 ret = rw_copy_check_uvector(WRITE, _payload_iov, ioc,
Christopher Yeohac34ebb2012-05-31 16:26:42 -07001149 ARRAY_SIZE(iovstack), iovstack, &iov);
David Howellsee009e4a02011-03-07 15:06:20 +00001150 if (ret < 0)
Alan Coxa84a9212012-09-28 12:20:02 +01001151 goto err;
David Howellsee009e4a02011-03-07 15:06:20 +00001152 if (ret == 0)
1153 goto no_payload_free;
1154
1155 ret = keyctl_instantiate_key_common(id, iov, ioc, ret, ringid);
Alan Coxa84a9212012-09-28 12:20:02 +01001156err:
David Howellsee009e4a02011-03-07 15:06:20 +00001157 if (iov != iovstack)
1158 kfree(iov);
1159 return ret;
1160
1161no_payload_free:
1162 if (iov != iovstack)
1163 kfree(iov);
1164no_payload:
1165 return keyctl_instantiate_key_common(id, NULL, 0, 0, ringid);
1166}
1167
1168/*
David Howells973c9f42011-01-20 16:38:33 +00001169 * Negatively instantiate the key with the given timeout (in seconds) and link
1170 * the key into the destination keyring if one is given.
1171 *
1172 * The caller must have the appropriate instantiation permit set for this to
1173 * work (see keyctl_assume_authority). No other permissions are required.
1174 *
1175 * The key and any links to the key will be automatically garbage collected
1176 * after the timeout expires.
1177 *
1178 * Negative keys are used to rate limit repeated request_key() calls by causing
1179 * them to return -ENOKEY until the negative key expires.
1180 *
1181 * If successful, 0 will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 */
1183long keyctl_negate_key(key_serial_t id, unsigned timeout, key_serial_t ringid)
1184{
David Howellsfdd1b942011-03-07 15:06:09 +00001185 return keyctl_reject_key(id, timeout, ENOKEY, ringid);
1186}
1187
1188/*
1189 * Negatively instantiate the key with the given timeout (in seconds) and error
1190 * code and link the key into the destination keyring if one is given.
1191 *
1192 * The caller must have the appropriate instantiation permit set for this to
1193 * work (see keyctl_assume_authority). No other permissions are required.
1194 *
1195 * The key and any links to the key will be automatically garbage collected
1196 * after the timeout expires.
1197 *
1198 * Negative keys are used to rate limit repeated request_key() calls by causing
1199 * them to return the specified error code until the negative key expires.
1200 *
1201 * If successful, 0 will be returned.
1202 */
1203long keyctl_reject_key(key_serial_t id, unsigned timeout, unsigned error,
1204 key_serial_t ringid)
1205{
David Howellsd84f4f92008-11-14 10:39:23 +11001206 const struct cred *cred = current_cred();
David Howells3e301482005-06-23 22:00:56 -07001207 struct request_key_auth *rka;
David Howells8bbf49762008-11-14 10:39:14 +11001208 struct key *instkey, *dest_keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 long ret;
1210
David Howellsfdd1b942011-03-07 15:06:09 +00001211 kenter("%d,%u,%u,%d", id, timeout, error, ringid);
1212
1213 /* must be a valid error code and mustn't be a kernel special */
1214 if (error <= 0 ||
1215 error >= MAX_ERRNO ||
1216 error == ERESTARTSYS ||
1217 error == ERESTARTNOINTR ||
1218 error == ERESTARTNOHAND ||
1219 error == ERESTART_RESTARTBLOCK)
1220 return -EINVAL;
David Howellsd84f4f92008-11-14 10:39:23 +11001221
David Howellsb5f545c2006-01-08 01:02:47 -08001222 /* the appropriate instantiation authorisation key must have been
1223 * assumed before calling this */
1224 ret = -EPERM;
David Howellsd84f4f92008-11-14 10:39:23 +11001225 instkey = cred->request_key_auth;
David Howellsb5f545c2006-01-08 01:02:47 -08001226 if (!instkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
David Howells3e301482005-06-23 22:00:56 -07001229 rka = instkey->payload.data;
David Howellsb5f545c2006-01-08 01:02:47 -08001230 if (rka->target_key->serial != id)
1231 goto error;
David Howells3e301482005-06-23 22:00:56 -07001232
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 /* find the destination keyring if present (which must also be
1234 * writable) */
David Howells8bbf49762008-11-14 10:39:14 +11001235 ret = get_instantiation_keyring(ringid, rka, &dest_keyring);
1236 if (ret < 0)
1237 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
1239 /* instantiate the key and link it into a keyring */
David Howellsfdd1b942011-03-07 15:06:09 +00001240 ret = key_reject_and_link(rka->target_key, timeout, error,
David Howells8bbf49762008-11-14 10:39:14 +11001241 dest_keyring, instkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
David Howells8bbf49762008-11-14 10:39:14 +11001243 key_put(dest_keyring);
David Howellsb5f545c2006-01-08 01:02:47 -08001244
1245 /* discard the assumed authority if it's just been disabled by
1246 * instantiation of the key */
David Howellsd84f4f92008-11-14 10:39:23 +11001247 if (ret == 0)
1248 keyctl_change_reqkey_auth(NULL);
David Howellsb5f545c2006-01-08 01:02:47 -08001249
1250error:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +00001252}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254/*
David Howells973c9f42011-01-20 16:38:33 +00001255 * Read or set the default keyring in which request_key() will cache keys and
1256 * return the old setting.
1257 *
1258 * If a process keyring is specified then this will be created if it doesn't
1259 * yet exist. The old setting will be returned if successful.
David Howells3e301482005-06-23 22:00:56 -07001260 */
1261long keyctl_set_reqkey_keyring(int reqkey_defl)
1262{
David Howellsd84f4f92008-11-14 10:39:23 +11001263 struct cred *new;
1264 int ret, old_setting;
1265
1266 old_setting = current_cred_xxx(jit_keyring);
1267
1268 if (reqkey_defl == KEY_REQKEY_DEFL_NO_CHANGE)
1269 return old_setting;
1270
1271 new = prepare_creds();
1272 if (!new)
1273 return -ENOMEM;
David Howells3e301482005-06-23 22:00:56 -07001274
1275 switch (reqkey_defl) {
1276 case KEY_REQKEY_DEFL_THREAD_KEYRING:
David Howellsd84f4f92008-11-14 10:39:23 +11001277 ret = install_thread_keyring_to_cred(new);
David Howells3e301482005-06-23 22:00:56 -07001278 if (ret < 0)
David Howellsd84f4f92008-11-14 10:39:23 +11001279 goto error;
David Howells3e301482005-06-23 22:00:56 -07001280 goto set;
1281
1282 case KEY_REQKEY_DEFL_PROCESS_KEYRING:
David Howellsd84f4f92008-11-14 10:39:23 +11001283 ret = install_process_keyring_to_cred(new);
1284 if (ret < 0) {
1285 if (ret != -EEXIST)
1286 goto error;
1287 ret = 0;
1288 }
1289 goto set;
David Howells3e301482005-06-23 22:00:56 -07001290
1291 case KEY_REQKEY_DEFL_DEFAULT:
1292 case KEY_REQKEY_DEFL_SESSION_KEYRING:
1293 case KEY_REQKEY_DEFL_USER_KEYRING:
1294 case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
David Howellsd84f4f92008-11-14 10:39:23 +11001295 case KEY_REQKEY_DEFL_REQUESTOR_KEYRING:
1296 goto set;
David Howells3e301482005-06-23 22:00:56 -07001297
1298 case KEY_REQKEY_DEFL_NO_CHANGE:
David Howells3e301482005-06-23 22:00:56 -07001299 case KEY_REQKEY_DEFL_GROUP_KEYRING:
1300 default:
David Howellsd84f4f92008-11-14 10:39:23 +11001301 ret = -EINVAL;
1302 goto error;
David Howells3e301482005-06-23 22:00:56 -07001303 }
1304
David Howellsd84f4f92008-11-14 10:39:23 +11001305set:
1306 new->jit_keyring = reqkey_defl;
1307 commit_creds(new);
1308 return old_setting;
1309error:
1310 abort_creds(new);
Dan Carpenter4303ef12010-06-11 17:30:05 +01001311 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +00001312}
David Howellsd84f4f92008-11-14 10:39:23 +11001313
David Howells3e301482005-06-23 22:00:56 -07001314/*
David Howells973c9f42011-01-20 16:38:33 +00001315 * Set or clear the timeout on a key.
1316 *
1317 * Either the key must grant the caller Setattr permission or else the caller
1318 * must hold an instantiation authorisation token for the key.
1319 *
1320 * The timeout is either 0 to clear the timeout, or a number of seconds from
1321 * the current time. The key and any links to the key will be automatically
1322 * garbage collected after the timeout expires.
1323 *
1324 * If successful, 0 is returned.
David Howells017679c2006-01-08 01:02:43 -08001325 */
1326long keyctl_set_timeout(key_serial_t id, unsigned timeout)
1327{
David Howells91562352010-06-11 17:31:05 +01001328 struct key *key, *instkey;
David Howells017679c2006-01-08 01:02:43 -08001329 key_ref_t key_ref;
David Howells017679c2006-01-08 01:02:43 -08001330 long ret;
1331
David Howells55931222009-09-02 09:13:45 +01001332 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
David Howellsf5895942014-03-14 17:44:49 +00001333 KEY_NEED_SETATTR);
David Howells017679c2006-01-08 01:02:43 -08001334 if (IS_ERR(key_ref)) {
David Howells91562352010-06-11 17:31:05 +01001335 /* setting the timeout on a key under construction is permitted
1336 * if we have the authorisation token handy */
1337 if (PTR_ERR(key_ref) == -EACCES) {
1338 instkey = key_get_instantiation_authkey(id);
1339 if (!IS_ERR(instkey)) {
1340 key_put(instkey);
1341 key_ref = lookup_user_key(id,
1342 KEY_LOOKUP_PARTIAL,
1343 0);
1344 if (!IS_ERR(key_ref))
1345 goto okay;
1346 }
1347 }
1348
David Howells017679c2006-01-08 01:02:43 -08001349 ret = PTR_ERR(key_ref);
1350 goto error;
1351 }
1352
David Howells91562352010-06-11 17:31:05 +01001353okay:
David Howells017679c2006-01-08 01:02:43 -08001354 key = key_ref_to_ptr(key_ref);
Bryan Schumaker59e6b9c2012-02-24 14:14:50 -05001355 key_set_timeout(key, timeout);
David Howells017679c2006-01-08 01:02:43 -08001356 key_put(key);
1357
1358 ret = 0;
1359error:
1360 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +00001361}
David Howells017679c2006-01-08 01:02:43 -08001362
David Howells017679c2006-01-08 01:02:43 -08001363/*
David Howells973c9f42011-01-20 16:38:33 +00001364 * Assume (or clear) the authority to instantiate the specified key.
1365 *
1366 * This sets the authoritative token currently in force for key instantiation.
1367 * This must be done for a key to be instantiated. It has the effect of making
1368 * available all the keys from the caller of the request_key() that created a
1369 * key to request_key() calls made by the caller of this function.
1370 *
1371 * The caller must have the instantiation key in their process keyrings with a
1372 * Search permission grant available to the caller.
1373 *
1374 * If the ID given is 0, then the setting will be cleared and 0 returned.
1375 *
1376 * If the ID given has a matching an authorisation key, then that key will be
1377 * set and its ID will be returned. The authorisation key can be read to get
1378 * the callout information passed to request_key().
David Howellsb5f545c2006-01-08 01:02:47 -08001379 */
1380long keyctl_assume_authority(key_serial_t id)
1381{
1382 struct key *authkey;
1383 long ret;
1384
1385 /* special key IDs aren't permitted */
1386 ret = -EINVAL;
1387 if (id < 0)
1388 goto error;
1389
1390 /* we divest ourselves of authority if given an ID of 0 */
1391 if (id == 0) {
David Howellsd84f4f92008-11-14 10:39:23 +11001392 ret = keyctl_change_reqkey_auth(NULL);
David Howellsb5f545c2006-01-08 01:02:47 -08001393 goto error;
1394 }
1395
1396 /* attempt to assume the authority temporarily granted to us whilst we
1397 * instantiate the specified key
1398 * - the authorisation key must be in the current task's keyrings
1399 * somewhere
1400 */
1401 authkey = key_get_instantiation_authkey(id);
1402 if (IS_ERR(authkey)) {
1403 ret = PTR_ERR(authkey);
1404 goto error;
1405 }
1406
David Howellsd84f4f92008-11-14 10:39:23 +11001407 ret = keyctl_change_reqkey_auth(authkey);
1408 if (ret < 0)
1409 goto error;
1410 key_put(authkey);
David Howellsb5f545c2006-01-08 01:02:47 -08001411
David Howellsd84f4f92008-11-14 10:39:23 +11001412 ret = authkey->serial;
David Howellsb5f545c2006-01-08 01:02:47 -08001413error:
1414 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +00001415}
David Howellsb5f545c2006-01-08 01:02:47 -08001416
David Howells70a5bb72008-04-29 01:01:26 -07001417/*
David Howells973c9f42011-01-20 16:38:33 +00001418 * Get a key's the LSM security label.
1419 *
1420 * The key must grant the caller View permission for this to work.
1421 *
1422 * If there's a buffer, then up to buflen bytes of data will be placed into it.
1423 *
1424 * If successful, the amount of information available will be returned,
1425 * irrespective of how much was copied (including the terminal NUL).
David Howells70a5bb72008-04-29 01:01:26 -07001426 */
1427long keyctl_get_security(key_serial_t keyid,
1428 char __user *buffer,
1429 size_t buflen)
1430{
1431 struct key *key, *instkey;
1432 key_ref_t key_ref;
1433 char *context;
1434 long ret;
1435
David Howellsf5895942014-03-14 17:44:49 +00001436 key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_NEED_VIEW);
David Howells70a5bb72008-04-29 01:01:26 -07001437 if (IS_ERR(key_ref)) {
1438 if (PTR_ERR(key_ref) != -EACCES)
1439 return PTR_ERR(key_ref);
1440
1441 /* viewing a key under construction is also permitted if we
1442 * have the authorisation token handy */
1443 instkey = key_get_instantiation_authkey(keyid);
1444 if (IS_ERR(instkey))
Roel Kluinfa1cc7b2009-12-15 15:05:12 -08001445 return PTR_ERR(instkey);
David Howells70a5bb72008-04-29 01:01:26 -07001446 key_put(instkey);
1447
David Howells55931222009-09-02 09:13:45 +01001448 key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, 0);
David Howells70a5bb72008-04-29 01:01:26 -07001449 if (IS_ERR(key_ref))
1450 return PTR_ERR(key_ref);
1451 }
1452
1453 key = key_ref_to_ptr(key_ref);
1454 ret = security_key_getsecurity(key, &context);
1455 if (ret == 0) {
1456 /* if no information was returned, give userspace an empty
1457 * string */
1458 ret = 1;
1459 if (buffer && buflen > 0 &&
1460 copy_to_user(buffer, "", 1) != 0)
1461 ret = -EFAULT;
1462 } else if (ret > 0) {
1463 /* return as much data as there's room for */
1464 if (buffer && buflen > 0) {
1465 if (buflen > ret)
1466 buflen = ret;
1467
1468 if (copy_to_user(buffer, context, buflen) != 0)
1469 ret = -EFAULT;
1470 }
1471
1472 kfree(context);
1473 }
1474
1475 key_ref_put(key_ref);
1476 return ret;
1477}
1478
David Howellsee18d642009-09-02 09:14:21 +01001479/*
David Howells973c9f42011-01-20 16:38:33 +00001480 * Attempt to install the calling process's session keyring on the process's
1481 * parent process.
1482 *
1483 * The keyring must exist and must grant the caller LINK permission, and the
1484 * parent process must be single-threaded and must have the same effective
1485 * ownership as this process and mustn't be SUID/SGID.
1486 *
1487 * The keyring will be emplaced on the parent when it next resumes userspace.
1488 *
1489 * If successful, 0 will be returned.
David Howellsee18d642009-09-02 09:14:21 +01001490 */
1491long keyctl_session_to_parent(void)
1492{
1493 struct task_struct *me, *parent;
1494 const struct cred *mycred, *pcred;
Al Viro67d12142012-06-27 11:07:19 +04001495 struct callback_head *newwork, *oldwork;
David Howellsee18d642009-09-02 09:14:21 +01001496 key_ref_t keyring_r;
Oleg Nesterov413cd3d2012-05-11 10:59:08 +10001497 struct cred *cred;
David Howellsee18d642009-09-02 09:14:21 +01001498 int ret;
1499
David Howellsf5895942014-03-14 17:44:49 +00001500 keyring_r = lookup_user_key(KEY_SPEC_SESSION_KEYRING, 0, KEY_NEED_LINK);
David Howellsee18d642009-09-02 09:14:21 +01001501 if (IS_ERR(keyring_r))
1502 return PTR_ERR(keyring_r);
1503
Oleg Nesterov413cd3d2012-05-11 10:59:08 +10001504 ret = -ENOMEM;
Oleg Nesterov413cd3d2012-05-11 10:59:08 +10001505
David Howellsee18d642009-09-02 09:14:21 +01001506 /* our parent is going to need a new cred struct, a new tgcred struct
1507 * and new security data, so we allocate them here to prevent ENOMEM in
1508 * our parent */
David Howellsee18d642009-09-02 09:14:21 +01001509 cred = cred_alloc_blank();
1510 if (!cred)
Al Viro67d12142012-06-27 11:07:19 +04001511 goto error_keyring;
1512 newwork = &cred->rcu;
David Howellsee18d642009-09-02 09:14:21 +01001513
David Howells3a505972012-10-02 19:24:29 +01001514 cred->session_keyring = key_ref_to_ptr(keyring_r);
1515 keyring_r = NULL;
Al Viro67d12142012-06-27 11:07:19 +04001516 init_task_work(newwork, key_change_session_keyring);
David Howellsee18d642009-09-02 09:14:21 +01001517
1518 me = current;
David Howells9d1ac652010-09-10 09:59:46 +01001519 rcu_read_lock();
David Howellsee18d642009-09-02 09:14:21 +01001520 write_lock_irq(&tasklist_lock);
1521
David Howellsee18d642009-09-02 09:14:21 +01001522 ret = -EPERM;
Oleg Nesterov413cd3d2012-05-11 10:59:08 +10001523 oldwork = NULL;
1524 parent = me->real_parent;
David Howellsee18d642009-09-02 09:14:21 +01001525
1526 /* the parent mustn't be init and mustn't be a kernel thread */
1527 if (parent->pid <= 1 || !parent->mm)
Oleg Nesterov413cd3d2012-05-11 10:59:08 +10001528 goto unlock;
David Howellsee18d642009-09-02 09:14:21 +01001529
1530 /* the parent must be single threaded */
Oleg Nesterovdd98acf2010-05-26 14:43:23 -07001531 if (!thread_group_empty(parent))
Oleg Nesterov413cd3d2012-05-11 10:59:08 +10001532 goto unlock;
David Howellsee18d642009-09-02 09:14:21 +01001533
1534 /* the parent and the child must have different session keyrings or
1535 * there's no point */
1536 mycred = current_cred();
1537 pcred = __task_cred(parent);
1538 if (mycred == pcred ||
David Howells3a505972012-10-02 19:24:29 +01001539 mycred->session_keyring == pcred->session_keyring) {
Oleg Nesterov413cd3d2012-05-11 10:59:08 +10001540 ret = 0;
1541 goto unlock;
1542 }
David Howellsee18d642009-09-02 09:14:21 +01001543
1544 /* the parent must have the same effective ownership and mustn't be
1545 * SUID/SGID */
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -08001546 if (!uid_eq(pcred->uid, mycred->euid) ||
1547 !uid_eq(pcred->euid, mycred->euid) ||
1548 !uid_eq(pcred->suid, mycred->euid) ||
1549 !gid_eq(pcred->gid, mycred->egid) ||
1550 !gid_eq(pcred->egid, mycred->egid) ||
1551 !gid_eq(pcred->sgid, mycred->egid))
Oleg Nesterov413cd3d2012-05-11 10:59:08 +10001552 goto unlock;
David Howellsee18d642009-09-02 09:14:21 +01001553
1554 /* the keyrings must have the same UID */
David Howells3a505972012-10-02 19:24:29 +01001555 if ((pcred->session_keyring &&
Linus Torvalds2a74dbb2012-12-16 15:40:50 -08001556 !uid_eq(pcred->session_keyring->uid, mycred->euid)) ||
1557 !uid_eq(mycred->session_keyring->uid, mycred->euid))
Oleg Nesterov413cd3d2012-05-11 10:59:08 +10001558 goto unlock;
David Howellsee18d642009-09-02 09:14:21 +01001559
Oleg Nesterov413cd3d2012-05-11 10:59:08 +10001560 /* cancel an already pending keyring replacement */
1561 oldwork = task_work_cancel(parent, key_change_session_keyring);
David Howellsee18d642009-09-02 09:14:21 +01001562
1563 /* the replacement session keyring is applied just prior to userspace
1564 * restarting */
Al Viro67d12142012-06-27 11:07:19 +04001565 ret = task_work_add(parent, newwork, true);
Oleg Nesterov413cd3d2012-05-11 10:59:08 +10001566 if (!ret)
1567 newwork = NULL;
1568unlock:
David Howellsee18d642009-09-02 09:14:21 +01001569 write_unlock_irq(&tasklist_lock);
David Howells9d1ac652010-09-10 09:59:46 +01001570 rcu_read_unlock();
Al Viro67d12142012-06-27 11:07:19 +04001571 if (oldwork)
1572 put_cred(container_of(oldwork, struct cred, rcu));
1573 if (newwork)
1574 put_cred(cred);
David Howellsee18d642009-09-02 09:14:21 +01001575 return ret;
1576
1577error_keyring:
1578 key_ref_put(keyring_r);
1579 return ret;
1580}
1581
David Howellsb5f545c2006-01-08 01:02:47 -08001582/*
David Howells973c9f42011-01-20 16:38:33 +00001583 * The key control system call
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 */
Heiko Carstens938bb9f2009-01-14 14:14:30 +01001585SYSCALL_DEFINE5(keyctl, int, option, unsigned long, arg2, unsigned long, arg3,
1586 unsigned long, arg4, unsigned long, arg5)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587{
1588 switch (option) {
1589 case KEYCTL_GET_KEYRING_ID:
1590 return keyctl_get_keyring_ID((key_serial_t) arg2,
1591 (int) arg3);
1592
1593 case KEYCTL_JOIN_SESSION_KEYRING:
1594 return keyctl_join_session_keyring((const char __user *) arg2);
1595
1596 case KEYCTL_UPDATE:
1597 return keyctl_update_key((key_serial_t) arg2,
1598 (const void __user *) arg3,
1599 (size_t) arg4);
1600
1601 case KEYCTL_REVOKE:
1602 return keyctl_revoke_key((key_serial_t) arg2);
1603
1604 case KEYCTL_DESCRIBE:
1605 return keyctl_describe_key((key_serial_t) arg2,
1606 (char __user *) arg3,
1607 (unsigned) arg4);
1608
1609 case KEYCTL_CLEAR:
1610 return keyctl_keyring_clear((key_serial_t) arg2);
1611
1612 case KEYCTL_LINK:
1613 return keyctl_keyring_link((key_serial_t) arg2,
1614 (key_serial_t) arg3);
1615
1616 case KEYCTL_UNLINK:
1617 return keyctl_keyring_unlink((key_serial_t) arg2,
1618 (key_serial_t) arg3);
1619
1620 case KEYCTL_SEARCH:
1621 return keyctl_keyring_search((key_serial_t) arg2,
1622 (const char __user *) arg3,
1623 (const char __user *) arg4,
1624 (key_serial_t) arg5);
1625
1626 case KEYCTL_READ:
1627 return keyctl_read_key((key_serial_t) arg2,
1628 (char __user *) arg3,
1629 (size_t) arg4);
1630
1631 case KEYCTL_CHOWN:
1632 return keyctl_chown_key((key_serial_t) arg2,
1633 (uid_t) arg3,
1634 (gid_t) arg4);
1635
1636 case KEYCTL_SETPERM:
1637 return keyctl_setperm_key((key_serial_t) arg2,
1638 (key_perm_t) arg3);
1639
1640 case KEYCTL_INSTANTIATE:
1641 return keyctl_instantiate_key((key_serial_t) arg2,
1642 (const void __user *) arg3,
1643 (size_t) arg4,
1644 (key_serial_t) arg5);
1645
1646 case KEYCTL_NEGATE:
1647 return keyctl_negate_key((key_serial_t) arg2,
1648 (unsigned) arg3,
1649 (key_serial_t) arg4);
1650
David Howells3e301482005-06-23 22:00:56 -07001651 case KEYCTL_SET_REQKEY_KEYRING:
1652 return keyctl_set_reqkey_keyring(arg2);
1653
David Howells017679c2006-01-08 01:02:43 -08001654 case KEYCTL_SET_TIMEOUT:
1655 return keyctl_set_timeout((key_serial_t) arg2,
1656 (unsigned) arg3);
1657
David Howellsb5f545c2006-01-08 01:02:47 -08001658 case KEYCTL_ASSUME_AUTHORITY:
1659 return keyctl_assume_authority((key_serial_t) arg2);
1660
David Howells70a5bb72008-04-29 01:01:26 -07001661 case KEYCTL_GET_SECURITY:
1662 return keyctl_get_security((key_serial_t) arg2,
James Morris90bd49a2008-12-29 14:35:35 +11001663 (char __user *) arg3,
David Howells70a5bb72008-04-29 01:01:26 -07001664 (size_t) arg4);
1665
David Howellsee18d642009-09-02 09:14:21 +01001666 case KEYCTL_SESSION_TO_PARENT:
1667 return keyctl_session_to_parent();
1668
David Howellsfdd1b942011-03-07 15:06:09 +00001669 case KEYCTL_REJECT:
1670 return keyctl_reject_key((key_serial_t) arg2,
1671 (unsigned) arg3,
1672 (unsigned) arg4,
1673 (key_serial_t) arg5);
1674
David Howellsee009e4a02011-03-07 15:06:20 +00001675 case KEYCTL_INSTANTIATE_IOV:
1676 return keyctl_instantiate_key_iov(
1677 (key_serial_t) arg2,
1678 (const struct iovec __user *) arg3,
1679 (unsigned) arg4,
1680 (key_serial_t) arg5);
1681
David Howellsfd758152012-05-11 10:56:56 +01001682 case KEYCTL_INVALIDATE:
1683 return keyctl_invalidate_key((key_serial_t) arg2);
1684
David Howellsf36f8c72013-09-24 10:35:19 +01001685 case KEYCTL_GET_PERSISTENT:
1686 return keyctl_get_persistent((uid_t)arg2, (key_serial_t)arg3);
1687
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 default:
1689 return -EOPNOTSUPP;
1690 }
David Howellsa8b17ed2011-01-20 16:38:27 +00001691}