blob: eff88a5f5d40da17611d381bcf4e219a90bcc972 [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;
David Howells54e2c2c2014-09-16 17:29:03 +010040 if (type[0] == '.')
41 return -EPERM;
Davi Arnaut0cb409d2006-03-24 03:18:43 -080042 type[len - 1] = '\0';
Davi Arnaut0cb409d2006-03-24 03:18:43 -080043 return 0;
44}
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046/*
David Howells973c9f42011-01-20 16:38:33 +000047 * Extract the description of a new key from userspace and either add it as a
48 * new key to the specified keyring or update a matching key in that keyring.
49 *
David Howellscf7f6012012-09-13 13:06:29 +010050 * If the description is NULL or an empty string, the key type is asked to
51 * generate one from the payload.
52 *
David Howells973c9f42011-01-20 16:38:33 +000053 * The keyring must be writable so that we can attach the key to it.
54 *
55 * If successful, the new key's serial number is returned, otherwise an error
56 * code is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 */
Heiko Carstens1e7bfb22009-01-14 14:14:29 +010058SYSCALL_DEFINE5(add_key, const char __user *, _type,
59 const char __user *, _description,
60 const void __user *, _payload,
61 size_t, plen,
62 key_serial_t, ringid)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
David Howells664cceb2005-09-28 17:03:15 +010064 key_ref_t keyring_ref, key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 char type[32], *description;
66 void *payload;
Davi Arnaut0cb409d2006-03-24 03:18:43 -080067 long ret;
David Howells38bbca62008-04-29 01:01:19 -070068 bool vm;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70 ret = -EINVAL;
David Howells38bbca62008-04-29 01:01:19 -070071 if (plen > 1024 * 1024 - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 goto error;
73
74 /* draw all the data into kernel space */
Davi Arnaut0cb409d2006-03-24 03:18:43 -080075 ret = key_get_type_from_user(type, _type, sizeof(type));
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 if (ret < 0)
77 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
David Howellscf7f6012012-09-13 13:06:29 +010079 description = NULL;
80 if (_description) {
81 description = strndup_user(_description, PAGE_SIZE);
82 if (IS_ERR(description)) {
83 ret = PTR_ERR(description);
84 goto error;
85 }
86 if (!*description) {
87 kfree(description);
88 description = NULL;
Mimi Zohara4e3b8d2014-05-22 14:02:23 -040089 } else if ((description[0] == '.') &&
90 (strncmp(type, "keyring", 7) == 0)) {
91 ret = -EPERM;
92 goto error2;
David Howellscf7f6012012-09-13 13:06:29 +010093 }
Davi Arnaut0cb409d2006-03-24 03:18:43 -080094 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96 /* pull the payload in if one was supplied */
97 payload = NULL;
98
David Howells38bbca62008-04-29 01:01:19 -070099 vm = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 if (_payload) {
101 ret = -ENOMEM;
Andrew Morton4f1c28d2012-05-31 16:26:02 -0700102 payload = kmalloc(plen, GFP_KERNEL | __GFP_NOWARN);
David Howells38bbca62008-04-29 01:01:19 -0700103 if (!payload) {
104 if (plen <= PAGE_SIZE)
105 goto error2;
106 vm = true;
107 payload = vmalloc(plen);
108 if (!payload)
109 goto error2;
110 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112 ret = -EFAULT;
113 if (copy_from_user(payload, _payload, plen) != 0)
114 goto error3;
115 }
116
117 /* find the target keyring (which must be writable) */
David Howellsf5895942014-03-14 17:44:49 +0000118 keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
David Howells664cceb2005-09-28 17:03:15 +0100119 if (IS_ERR(keyring_ref)) {
120 ret = PTR_ERR(keyring_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 goto error3;
122 }
123
124 /* create or update the requested key and add it to the target
125 * keyring */
David Howells664cceb2005-09-28 17:03:15 +0100126 key_ref = key_create_or_update(keyring_ref, type, description,
Arun Raghavan6b79ccb2008-04-29 01:01:28 -0700127 payload, plen, KEY_PERM_UNDEF,
128 KEY_ALLOC_IN_QUOTA);
David Howells664cceb2005-09-28 17:03:15 +0100129 if (!IS_ERR(key_ref)) {
130 ret = key_ref_to_ptr(key_ref)->serial;
131 key_ref_put(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 }
133 else {
David Howells664cceb2005-09-28 17:03:15 +0100134 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 }
136
David Howells664cceb2005-09-28 17:03:15 +0100137 key_ref_put(keyring_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 error3:
David Howells38bbca62008-04-29 01:01:19 -0700139 if (!vm)
140 kfree(payload);
141 else
142 vfree(payload);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 error2:
144 kfree(description);
145 error:
146 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000147}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149/*
David Howells973c9f42011-01-20 16:38:33 +0000150 * Search the process keyrings and keyring trees linked from those for a
151 * matching key. Keyrings must have appropriate Search permission to be
152 * searched.
153 *
154 * If a key is found, it will be attached to the destination keyring if there's
155 * one specified and the serial number of the key will be returned.
156 *
157 * If no key is found, /sbin/request-key will be invoked if _callout_info is
158 * non-NULL in an attempt to create a key. The _callout_info string will be
159 * passed to /sbin/request-key to aid with completing the request. If the
160 * _callout_info string is "" then it will be changed to "-".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 */
Heiko Carstens1e7bfb22009-01-14 14:14:29 +0100162SYSCALL_DEFINE4(request_key, const char __user *, _type,
163 const char __user *, _description,
164 const char __user *, _callout_info,
165 key_serial_t, destringid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
167 struct key_type *ktype;
David Howells664cceb2005-09-28 17:03:15 +0100168 struct key *key;
169 key_ref_t dest_ref;
David Howells4a38e122008-04-29 01:01:24 -0700170 size_t callout_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 char type[32], *description, *callout_info;
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800172 long ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174 /* pull the type into kernel space */
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800175 ret = key_get_type_from_user(type, _type, sizeof(type));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 if (ret < 0)
177 goto error;
David Howells1260f802005-08-04 11:50:01 +0100178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 /* pull the description into kernel space */
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800180 description = strndup_user(_description, PAGE_SIZE);
181 if (IS_ERR(description)) {
182 ret = PTR_ERR(description);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 goto error;
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186 /* pull the callout info into kernel space */
187 callout_info = NULL;
David Howells4a38e122008-04-29 01:01:24 -0700188 callout_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 if (_callout_info) {
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800190 callout_info = strndup_user(_callout_info, PAGE_SIZE);
191 if (IS_ERR(callout_info)) {
192 ret = PTR_ERR(callout_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 goto error2;
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800194 }
David Howells4a38e122008-04-29 01:01:24 -0700195 callout_len = strlen(callout_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 }
197
198 /* get the destination keyring if specified */
David Howells664cceb2005-09-28 17:03:15 +0100199 dest_ref = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 if (destringid) {
David Howells55931222009-09-02 09:13:45 +0100201 dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE,
David Howellsf5895942014-03-14 17:44:49 +0000202 KEY_NEED_WRITE);
David Howells664cceb2005-09-28 17:03:15 +0100203 if (IS_ERR(dest_ref)) {
204 ret = PTR_ERR(dest_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 goto error3;
206 }
207 }
208
209 /* find the key type */
210 ktype = key_type_lookup(type);
211 if (IS_ERR(ktype)) {
212 ret = PTR_ERR(ktype);
213 goto error4;
214 }
215
216 /* do the search */
David Howells4a38e122008-04-29 01:01:24 -0700217 key = request_key_and_link(ktype, description, callout_info,
218 callout_len, NULL, key_ref_to_ptr(dest_ref),
David Howells7e047ef2006-06-26 00:24:50 -0700219 KEY_ALLOC_IN_QUOTA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 if (IS_ERR(key)) {
221 ret = PTR_ERR(key);
222 goto error5;
223 }
224
David Howells4aab1e82011-03-11 17:57:33 +0000225 /* wait for the key to finish being constructed */
226 ret = wait_for_key_construction(key, 1);
227 if (ret < 0)
228 goto error6;
229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 ret = key->serial;
231
David Howells4aab1e82011-03-11 17:57:33 +0000232error6:
David Howells3e301482005-06-23 22:00:56 -0700233 key_put(key);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700234error5:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 key_type_put(ktype);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700236error4:
David Howells664cceb2005-09-28 17:03:15 +0100237 key_ref_put(dest_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700238error3:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 kfree(callout_info);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700240error2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 kfree(description);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700242error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000244}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246/*
David Howells973c9f42011-01-20 16:38:33 +0000247 * Get the ID of the specified process keyring.
248 *
249 * The requested keyring must have search permission to be found.
250 *
251 * If successful, the ID of the requested keyring will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 */
253long keyctl_get_keyring_ID(key_serial_t id, int create)
254{
David Howells664cceb2005-09-28 17:03:15 +0100255 key_ref_t key_ref;
David Howells55931222009-09-02 09:13:45 +0100256 unsigned long lflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 long ret;
258
David Howells55931222009-09-02 09:13:45 +0100259 lflags = create ? KEY_LOOKUP_CREATE : 0;
David Howellsf5895942014-03-14 17:44:49 +0000260 key_ref = lookup_user_key(id, lflags, KEY_NEED_SEARCH);
David Howells664cceb2005-09-28 17:03:15 +0100261 if (IS_ERR(key_ref)) {
262 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 goto error;
264 }
265
David Howells664cceb2005-09-28 17:03:15 +0100266 ret = key_ref_to_ptr(key_ref)->serial;
267 key_ref_put(key_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700268error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 return ret;
David Howells973c9f42011-01-20 16:38:33 +0000270}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272/*
David Howells973c9f42011-01-20 16:38:33 +0000273 * Join a (named) session keyring.
274 *
275 * Create and join an anonymous session keyring or join a named session
276 * keyring, creating it if necessary. A named session keyring must have Search
277 * permission for it to be joined. Session keyrings without this permit will
278 * be skipped over.
279 *
280 * If successful, the ID of the joined session keyring will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 */
282long keyctl_join_session_keyring(const char __user *_name)
283{
284 char *name;
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800285 long ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287 /* fetch the name from userspace */
288 name = NULL;
289 if (_name) {
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800290 name = strndup_user(_name, PAGE_SIZE);
291 if (IS_ERR(name)) {
292 ret = PTR_ERR(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 goto error;
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 }
296
297 /* join the session */
298 ret = join_session_keyring(name);
Vegard Nossum0d54ee12009-01-17 17:45:45 +0100299 kfree(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700301error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000303}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305/*
David Howells973c9f42011-01-20 16:38:33 +0000306 * Update a key's data payload from the given data.
307 *
308 * The key must grant the caller Write permission and the key type must support
309 * updating for this to work. A negative key can be positively instantiated
310 * with this call.
311 *
312 * If successful, 0 will be returned. If the key type does not support
313 * updating, then -EOPNOTSUPP will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 */
315long keyctl_update_key(key_serial_t id,
316 const void __user *_payload,
317 size_t plen)
318{
David Howells664cceb2005-09-28 17:03:15 +0100319 key_ref_t key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 void *payload;
321 long ret;
322
323 ret = -EINVAL;
324 if (plen > PAGE_SIZE)
325 goto error;
326
327 /* pull the payload in if one was supplied */
328 payload = NULL;
329 if (_payload) {
330 ret = -ENOMEM;
331 payload = kmalloc(plen, GFP_KERNEL);
332 if (!payload)
333 goto error;
334
335 ret = -EFAULT;
336 if (copy_from_user(payload, _payload, plen) != 0)
337 goto error2;
338 }
339
340 /* find the target key (which must be writable) */
David Howellsf5895942014-03-14 17:44:49 +0000341 key_ref = lookup_user_key(id, 0, KEY_NEED_WRITE);
David Howells664cceb2005-09-28 17:03:15 +0100342 if (IS_ERR(key_ref)) {
343 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 goto error2;
345 }
346
347 /* update the key */
David Howells664cceb2005-09-28 17:03:15 +0100348 ret = key_update(key_ref, payload, plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
David Howells664cceb2005-09-28 17:03:15 +0100350 key_ref_put(key_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700351error2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 kfree(payload);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700353error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000355}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357/*
David Howells973c9f42011-01-20 16:38:33 +0000358 * Revoke a key.
359 *
360 * The key must be grant the caller Write or Setattr permission for this to
361 * work. The key type should give up its quota claim when revoked. The key
362 * and any links to the key will be automatically garbage collected after a
363 * certain amount of time (/proc/sys/kernel/keys/gc_delay).
364 *
365 * If successful, 0 is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 */
367long keyctl_revoke_key(key_serial_t id)
368{
David Howells664cceb2005-09-28 17:03:15 +0100369 key_ref_t key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 long ret;
371
David Howellsf5895942014-03-14 17:44:49 +0000372 key_ref = lookup_user_key(id, 0, KEY_NEED_WRITE);
David Howells664cceb2005-09-28 17:03:15 +0100373 if (IS_ERR(key_ref)) {
374 ret = PTR_ERR(key_ref);
David Howells0c2c9a32009-09-02 09:13:50 +0100375 if (ret != -EACCES)
376 goto error;
David Howellsf5895942014-03-14 17:44:49 +0000377 key_ref = lookup_user_key(id, 0, KEY_NEED_SETATTR);
David Howells0c2c9a32009-09-02 09:13:50 +0100378 if (IS_ERR(key_ref)) {
379 ret = PTR_ERR(key_ref);
380 goto error;
381 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 }
383
David Howells664cceb2005-09-28 17:03:15 +0100384 key_revoke(key_ref_to_ptr(key_ref));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 ret = 0;
386
David Howells664cceb2005-09-28 17:03:15 +0100387 key_ref_put(key_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700388error:
David Howells1260f802005-08-04 11:50:01 +0100389 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000390}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392/*
David Howellsfd758152012-05-11 10:56:56 +0100393 * Invalidate a key.
394 *
395 * The key must be grant the caller Invalidate permission for this to work.
396 * The key and any links to the key will be automatically garbage collected
397 * immediately.
398 *
399 * If successful, 0 is returned.
400 */
401long keyctl_invalidate_key(key_serial_t id)
402{
403 key_ref_t key_ref;
404 long ret;
405
406 kenter("%d", id);
407
David Howellsf5895942014-03-14 17:44:49 +0000408 key_ref = lookup_user_key(id, 0, KEY_NEED_SEARCH);
David Howellsfd758152012-05-11 10:56:56 +0100409 if (IS_ERR(key_ref)) {
410 ret = PTR_ERR(key_ref);
David Howells0c7774a2014-07-17 20:45:08 +0100411
412 /* Root is permitted to invalidate certain special keys */
413 if (capable(CAP_SYS_ADMIN)) {
414 key_ref = lookup_user_key(id, 0, 0);
415 if (IS_ERR(key_ref))
416 goto error;
417 if (test_bit(KEY_FLAG_ROOT_CAN_INVAL,
418 &key_ref_to_ptr(key_ref)->flags))
419 goto invalidate;
420 goto error_put;
421 }
422
David Howellsfd758152012-05-11 10:56:56 +0100423 goto error;
424 }
425
David Howells0c7774a2014-07-17 20:45:08 +0100426invalidate:
David Howellsfd758152012-05-11 10:56:56 +0100427 key_invalidate(key_ref_to_ptr(key_ref));
428 ret = 0;
David Howells0c7774a2014-07-17 20:45:08 +0100429error_put:
David Howellsfd758152012-05-11 10:56:56 +0100430 key_ref_put(key_ref);
431error:
432 kleave(" = %ld", ret);
433 return ret;
434}
435
436/*
David Howells973c9f42011-01-20 16:38:33 +0000437 * Clear the specified keyring, creating an empty process keyring if one of the
438 * special keyring IDs is used.
439 *
440 * The keyring must grant the caller Write permission for this to work. If
441 * successful, 0 will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 */
443long keyctl_keyring_clear(key_serial_t ringid)
444{
David Howells664cceb2005-09-28 17:03:15 +0100445 key_ref_t keyring_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 long ret;
447
David Howellsf5895942014-03-14 17:44:49 +0000448 keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
David Howells664cceb2005-09-28 17:03:15 +0100449 if (IS_ERR(keyring_ref)) {
450 ret = PTR_ERR(keyring_ref);
David Howells700920e2012-01-18 15:31:45 +0000451
452 /* Root is permitted to invalidate certain special keyrings */
453 if (capable(CAP_SYS_ADMIN)) {
454 keyring_ref = lookup_user_key(ringid, 0, 0);
455 if (IS_ERR(keyring_ref))
456 goto error;
457 if (test_bit(KEY_FLAG_ROOT_CAN_CLEAR,
458 &key_ref_to_ptr(keyring_ref)->flags))
459 goto clear;
460 goto error_put;
461 }
462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 goto error;
464 }
465
David Howells700920e2012-01-18 15:31:45 +0000466clear:
David Howells664cceb2005-09-28 17:03:15 +0100467 ret = keyring_clear(key_ref_to_ptr(keyring_ref));
David Howells700920e2012-01-18 15:31:45 +0000468error_put:
David Howells664cceb2005-09-28 17:03:15 +0100469 key_ref_put(keyring_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700470error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000472}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474/*
David Howells973c9f42011-01-20 16:38:33 +0000475 * Create a link from a keyring to a key if there's no matching key in the
476 * keyring, otherwise replace the link to the matching key with a link to the
477 * new key.
478 *
479 * The key must grant the caller Link permission and the the keyring must grant
480 * the caller Write permission. Furthermore, if an additional link is created,
481 * the keyring's quota will be extended.
482 *
483 * If successful, 0 will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 */
485long keyctl_keyring_link(key_serial_t id, key_serial_t ringid)
486{
David Howells664cceb2005-09-28 17:03:15 +0100487 key_ref_t keyring_ref, key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 long ret;
489
David Howellsf5895942014-03-14 17:44:49 +0000490 keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
David Howells664cceb2005-09-28 17:03:15 +0100491 if (IS_ERR(keyring_ref)) {
492 ret = PTR_ERR(keyring_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 goto error;
494 }
495
David Howellsf5895942014-03-14 17:44:49 +0000496 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE, KEY_NEED_LINK);
David Howells664cceb2005-09-28 17:03:15 +0100497 if (IS_ERR(key_ref)) {
498 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 goto error2;
500 }
501
David Howells664cceb2005-09-28 17:03:15 +0100502 ret = key_link(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
David Howells664cceb2005-09-28 17:03:15 +0100504 key_ref_put(key_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700505error2:
David Howells664cceb2005-09-28 17:03:15 +0100506 key_ref_put(keyring_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700507error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000509}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511/*
David Howells973c9f42011-01-20 16:38:33 +0000512 * Unlink a key from a keyring.
513 *
514 * The keyring must grant the caller Write permission for this to work; the key
515 * itself need not grant the caller anything. If the last link to a key is
516 * removed then that key will be scheduled for destruction.
517 *
518 * If successful, 0 will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 */
520long keyctl_keyring_unlink(key_serial_t id, key_serial_t ringid)
521{
David Howells664cceb2005-09-28 17:03:15 +0100522 key_ref_t keyring_ref, key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 long ret;
524
David Howellsf5895942014-03-14 17:44:49 +0000525 keyring_ref = lookup_user_key(ringid, 0, KEY_NEED_WRITE);
David Howells664cceb2005-09-28 17:03:15 +0100526 if (IS_ERR(keyring_ref)) {
527 ret = PTR_ERR(keyring_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 goto error;
529 }
530
David Howells55931222009-09-02 09:13:45 +0100531 key_ref = lookup_user_key(id, KEY_LOOKUP_FOR_UNLINK, 0);
David Howells664cceb2005-09-28 17:03:15 +0100532 if (IS_ERR(key_ref)) {
533 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 goto error2;
535 }
536
David Howells664cceb2005-09-28 17:03:15 +0100537 ret = key_unlink(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
David Howells664cceb2005-09-28 17:03:15 +0100539 key_ref_put(key_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700540error2:
David Howells664cceb2005-09-28 17:03:15 +0100541 key_ref_put(keyring_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700542error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000544}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546/*
David Howells973c9f42011-01-20 16:38:33 +0000547 * Return a description of a key to userspace.
548 *
549 * The key must grant the caller View permission for this to work.
550 *
551 * If there's a buffer, we place up to buflen bytes of data into it formatted
552 * in the following way:
553 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 * type;uid;gid;perm;description<NUL>
David Howells973c9f42011-01-20 16:38:33 +0000555 *
556 * If successful, we return the amount of description available, irrespective
557 * of how much we may have copied into the buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 */
559long keyctl_describe_key(key_serial_t keyid,
560 char __user *buffer,
561 size_t buflen)
562{
David Howells3e301482005-06-23 22:00:56 -0700563 struct key *key, *instkey;
David Howells664cceb2005-09-28 17:03:15 +0100564 key_ref_t key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 char *tmpbuf;
566 long ret;
567
David Howellsf5895942014-03-14 17:44:49 +0000568 key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_NEED_VIEW);
David Howells664cceb2005-09-28 17:03:15 +0100569 if (IS_ERR(key_ref)) {
David Howells3e301482005-06-23 22:00:56 -0700570 /* viewing a key under construction is permitted if we have the
571 * authorisation token handy */
David Howells664cceb2005-09-28 17:03:15 +0100572 if (PTR_ERR(key_ref) == -EACCES) {
David Howells3e301482005-06-23 22:00:56 -0700573 instkey = key_get_instantiation_authkey(keyid);
574 if (!IS_ERR(instkey)) {
575 key_put(instkey);
David Howells8bbf49762008-11-14 10:39:14 +1100576 key_ref = lookup_user_key(keyid,
David Howells55931222009-09-02 09:13:45 +0100577 KEY_LOOKUP_PARTIAL,
578 0);
David Howells664cceb2005-09-28 17:03:15 +0100579 if (!IS_ERR(key_ref))
David Howells3e301482005-06-23 22:00:56 -0700580 goto okay;
581 }
582 }
583
David Howells664cceb2005-09-28 17:03:15 +0100584 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 goto error;
586 }
587
David Howells3e301482005-06-23 22:00:56 -0700588okay:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 /* calculate how much description we're going to return */
590 ret = -ENOMEM;
591 tmpbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
592 if (!tmpbuf)
593 goto error2;
594
David Howells664cceb2005-09-28 17:03:15 +0100595 key = key_ref_to_ptr(key_ref);
596
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 ret = snprintf(tmpbuf, PAGE_SIZE - 1,
David Howells664cceb2005-09-28 17:03:15 +0100598 "%s;%d;%d;%08x;%s",
David Howells94fd8402010-06-28 14:05:04 +0100599 key->type->name,
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -0800600 from_kuid_munged(current_user_ns(), key->uid),
601 from_kgid_munged(current_user_ns(), key->gid),
David Howells94fd8402010-06-28 14:05:04 +0100602 key->perm,
603 key->description ?: "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
605 /* include a NUL char at the end of the data */
606 if (ret > PAGE_SIZE - 1)
607 ret = PAGE_SIZE - 1;
608 tmpbuf[ret] = 0;
609 ret++;
610
611 /* consider returning the data */
612 if (buffer && buflen > 0) {
613 if (buflen > ret)
614 buflen = ret;
615
616 if (copy_to_user(buffer, tmpbuf, buflen) != 0)
617 ret = -EFAULT;
618 }
619
620 kfree(tmpbuf);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700621error2:
David Howells664cceb2005-09-28 17:03:15 +0100622 key_ref_put(key_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700623error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000625}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627/*
David Howells973c9f42011-01-20 16:38:33 +0000628 * Search the specified keyring and any keyrings it links to for a matching
629 * key. Only keyrings that grant the caller Search permission will be searched
630 * (this includes the starting keyring). Only keys with Search permission can
631 * be found.
632 *
633 * If successful, the found key will be linked to the destination keyring if
634 * supplied and the key has Link permission, and the found key ID will be
635 * returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 */
637long keyctl_keyring_search(key_serial_t ringid,
638 const char __user *_type,
639 const char __user *_description,
640 key_serial_t destringid)
641{
642 struct key_type *ktype;
David Howells664cceb2005-09-28 17:03:15 +0100643 key_ref_t keyring_ref, key_ref, dest_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 char type[32], *description;
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800645 long ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
647 /* pull the type and description into kernel space */
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800648 ret = key_get_type_from_user(type, _type, sizeof(type));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 if (ret < 0)
650 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800652 description = strndup_user(_description, PAGE_SIZE);
653 if (IS_ERR(description)) {
654 ret = PTR_ERR(description);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 goto error;
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800656 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
658 /* get the keyring at which to begin the search */
David Howellsf5895942014-03-14 17:44:49 +0000659 keyring_ref = lookup_user_key(ringid, 0, KEY_NEED_SEARCH);
David Howells664cceb2005-09-28 17:03:15 +0100660 if (IS_ERR(keyring_ref)) {
661 ret = PTR_ERR(keyring_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 goto error2;
663 }
664
665 /* get the destination keyring if specified */
David Howells664cceb2005-09-28 17:03:15 +0100666 dest_ref = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 if (destringid) {
David Howells55931222009-09-02 09:13:45 +0100668 dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE,
David Howellsf5895942014-03-14 17:44:49 +0000669 KEY_NEED_WRITE);
David Howells664cceb2005-09-28 17:03:15 +0100670 if (IS_ERR(dest_ref)) {
671 ret = PTR_ERR(dest_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 goto error3;
673 }
674 }
675
676 /* find the key type */
677 ktype = key_type_lookup(type);
678 if (IS_ERR(ktype)) {
679 ret = PTR_ERR(ktype);
680 goto error4;
681 }
682
683 /* do the search */
David Howells664cceb2005-09-28 17:03:15 +0100684 key_ref = keyring_search(keyring_ref, ktype, description);
685 if (IS_ERR(key_ref)) {
686 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
688 /* treat lack or presence of a negative key the same */
689 if (ret == -EAGAIN)
690 ret = -ENOKEY;
691 goto error5;
692 }
693
694 /* link the resulting key to the destination keyring if we can */
David Howells664cceb2005-09-28 17:03:15 +0100695 if (dest_ref) {
David Howellsf5895942014-03-14 17:44:49 +0000696 ret = key_permission(key_ref, KEY_NEED_LINK);
David Howells29db9192005-10-30 15:02:44 -0800697 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 goto error6;
699
David Howells664cceb2005-09-28 17:03:15 +0100700 ret = key_link(key_ref_to_ptr(dest_ref), key_ref_to_ptr(key_ref));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 if (ret < 0)
702 goto error6;
703 }
704
David Howells664cceb2005-09-28 17:03:15 +0100705 ret = key_ref_to_ptr(key_ref)->serial;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700707error6:
David Howells664cceb2005-09-28 17:03:15 +0100708 key_ref_put(key_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700709error5:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 key_type_put(ktype);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700711error4:
David Howells664cceb2005-09-28 17:03:15 +0100712 key_ref_put(dest_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700713error3:
David Howells664cceb2005-09-28 17:03:15 +0100714 key_ref_put(keyring_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700715error2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 kfree(description);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700717error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000719}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721/*
David Howells973c9f42011-01-20 16:38:33 +0000722 * Read a key's payload.
723 *
724 * The key must either grant the caller Read permission, or it must grant the
725 * caller Search permission when searched for from the process keyrings.
726 *
727 * If successful, we place up to buflen bytes of data into the buffer, if one
728 * is provided, and return the amount of data that is available in the key,
729 * irrespective of how much we copied into the buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 */
731long keyctl_read_key(key_serial_t keyid, char __user *buffer, size_t buflen)
732{
David Howells664cceb2005-09-28 17:03:15 +0100733 struct key *key;
734 key_ref_t key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 long ret;
736
737 /* find the key first */
David Howells55931222009-09-02 09:13:45 +0100738 key_ref = lookup_user_key(keyid, 0, 0);
David Howells664cceb2005-09-28 17:03:15 +0100739 if (IS_ERR(key_ref)) {
740 ret = -ENOKEY;
741 goto error;
742 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
David Howells664cceb2005-09-28 17:03:15 +0100744 key = key_ref_to_ptr(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
David Howells664cceb2005-09-28 17:03:15 +0100746 /* see if we can read it directly */
David Howellsf5895942014-03-14 17:44:49 +0000747 ret = key_permission(key_ref, KEY_NEED_READ);
David Howells29db9192005-10-30 15:02:44 -0800748 if (ret == 0)
David Howells664cceb2005-09-28 17:03:15 +0100749 goto can_read_key;
David Howells29db9192005-10-30 15:02:44 -0800750 if (ret != -EACCES)
751 goto error;
David Howells664cceb2005-09-28 17:03:15 +0100752
753 /* we can't; see if it's searchable from this process's keyrings
754 * - we automatically take account of the fact that it may be
755 * dangling off an instantiation key
756 */
757 if (!is_key_possessed(key_ref)) {
758 ret = -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 goto error2;
760 }
761
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 /* the key is probably readable - now try to read it */
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700763can_read_key:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 ret = key_validate(key);
765 if (ret == 0) {
766 ret = -EOPNOTSUPP;
767 if (key->type->read) {
768 /* read the data with the semaphore held (since we
769 * might sleep) */
770 down_read(&key->sem);
771 ret = key->type->read(key, buffer, buflen);
772 up_read(&key->sem);
773 }
774 }
775
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700776error2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 key_put(key);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700778error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000780}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782/*
David Howells973c9f42011-01-20 16:38:33 +0000783 * Change the ownership of a key
784 *
785 * The key must grant the caller Setattr permission for this to work, though
786 * the key need not be fully instantiated yet. For the UID to be changed, or
787 * for the GID to be changed to a group the caller is not a member of, the
788 * caller must have sysadmin capability. If either uid or gid is -1 then that
789 * attribute is not changed.
790 *
791 * If the UID is to be changed, the new user must have sufficient quota to
792 * accept the key. The quota deduction will be removed from the old user to
793 * the new user should the attribute be changed.
794 *
795 * If successful, 0 will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 */
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -0800797long keyctl_chown_key(key_serial_t id, uid_t user, gid_t group)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798{
Fredrik Tolf58016492006-06-26 00:24:51 -0700799 struct key_user *newowner, *zapowner = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 struct key *key;
David Howells664cceb2005-09-28 17:03:15 +0100801 key_ref_t key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 long ret;
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -0800803 kuid_t uid;
804 kgid_t gid;
805
806 uid = make_kuid(current_user_ns(), user);
807 gid = make_kgid(current_user_ns(), group);
808 ret = -EINVAL;
809 if ((user != (uid_t) -1) && !uid_valid(uid))
810 goto error;
811 if ((group != (gid_t) -1) && !gid_valid(gid))
812 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
814 ret = 0;
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -0800815 if (user == (uid_t) -1 && group == (gid_t) -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 goto error;
817
David Howells55931222009-09-02 09:13:45 +0100818 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
David Howellsf5895942014-03-14 17:44:49 +0000819 KEY_NEED_SETATTR);
David Howells664cceb2005-09-28 17:03:15 +0100820 if (IS_ERR(key_ref)) {
821 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 goto error;
823 }
824
David Howells664cceb2005-09-28 17:03:15 +0100825 key = key_ref_to_ptr(key_ref);
826
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 /* make the changes with the locks held to prevent chown/chown races */
828 ret = -EACCES;
829 down_write(&key->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
831 if (!capable(CAP_SYS_ADMIN)) {
832 /* only the sysadmin can chown a key to some other UID */
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -0800833 if (user != (uid_t) -1 && !uid_eq(key->uid, uid))
Fredrik Tolf58016492006-06-26 00:24:51 -0700834 goto error_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
836 /* only the sysadmin can set the key's GID to a group other
837 * than one of those that the current process subscribes to */
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -0800838 if (group != (gid_t) -1 && !gid_eq(gid, key->gid) && !in_group_p(gid))
Fredrik Tolf58016492006-06-26 00:24:51 -0700839 goto error_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 }
841
Fredrik Tolf58016492006-06-26 00:24:51 -0700842 /* change the UID */
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -0800843 if (user != (uid_t) -1 && !uid_eq(uid, key->uid)) {
Fredrik Tolf58016492006-06-26 00:24:51 -0700844 ret = -ENOMEM;
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -0800845 newowner = key_user_lookup(uid);
Fredrik Tolf58016492006-06-26 00:24:51 -0700846 if (!newowner)
847 goto error_put;
848
849 /* transfer the quota burden to the new user */
850 if (test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) {
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -0800851 unsigned maxkeys = uid_eq(uid, GLOBAL_ROOT_UID) ?
David Howells0b77f5b2008-04-29 01:01:32 -0700852 key_quota_root_maxkeys : key_quota_maxkeys;
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -0800853 unsigned maxbytes = uid_eq(uid, GLOBAL_ROOT_UID) ?
David Howells0b77f5b2008-04-29 01:01:32 -0700854 key_quota_root_maxbytes : key_quota_maxbytes;
855
Fredrik Tolf58016492006-06-26 00:24:51 -0700856 spin_lock(&newowner->lock);
David Howells0b77f5b2008-04-29 01:01:32 -0700857 if (newowner->qnkeys + 1 >= maxkeys ||
858 newowner->qnbytes + key->quotalen >= maxbytes ||
859 newowner->qnbytes + key->quotalen <
860 newowner->qnbytes)
Fredrik Tolf58016492006-06-26 00:24:51 -0700861 goto quota_overrun;
862
863 newowner->qnkeys++;
864 newowner->qnbytes += key->quotalen;
865 spin_unlock(&newowner->lock);
866
867 spin_lock(&key->user->lock);
868 key->user->qnkeys--;
869 key->user->qnbytes -= key->quotalen;
870 spin_unlock(&key->user->lock);
871 }
872
873 atomic_dec(&key->user->nkeys);
874 atomic_inc(&newowner->nkeys);
875
876 if (test_bit(KEY_FLAG_INSTANTIATED, &key->flags)) {
877 atomic_dec(&key->user->nikeys);
878 atomic_inc(&newowner->nikeys);
879 }
880
881 zapowner = key->user;
882 key->user = newowner;
883 key->uid = uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 }
885
886 /* change the GID */
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -0800887 if (group != (gid_t) -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 key->gid = gid;
889
890 ret = 0;
891
Fredrik Tolf58016492006-06-26 00:24:51 -0700892error_put:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 up_write(&key->sem);
894 key_put(key);
Fredrik Tolf58016492006-06-26 00:24:51 -0700895 if (zapowner)
896 key_user_put(zapowner);
897error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 return ret;
899
Fredrik Tolf58016492006-06-26 00:24:51 -0700900quota_overrun:
901 spin_unlock(&newowner->lock);
902 zapowner = newowner;
903 ret = -EDQUOT;
904 goto error_put;
David Howellsa8b17ed2011-01-20 16:38:27 +0000905}
Fredrik Tolf58016492006-06-26 00:24:51 -0700906
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907/*
David Howells973c9f42011-01-20 16:38:33 +0000908 * Change the permission mask on a key.
909 *
910 * The key must grant the caller Setattr permission for this to work, though
911 * the key need not be fully instantiated yet. If the caller does not have
912 * sysadmin capability, it may only change the permission on keys that it owns.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 */
914long keyctl_setperm_key(key_serial_t id, key_perm_t perm)
915{
916 struct key *key;
David Howells664cceb2005-09-28 17:03:15 +0100917 key_ref_t key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 long ret;
919
920 ret = -EINVAL;
David Howells664cceb2005-09-28 17:03:15 +0100921 if (perm & ~(KEY_POS_ALL | KEY_USR_ALL | KEY_GRP_ALL | KEY_OTH_ALL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 goto error;
923
David Howells55931222009-09-02 09:13:45 +0100924 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
David Howellsf5895942014-03-14 17:44:49 +0000925 KEY_NEED_SETATTR);
David Howells664cceb2005-09-28 17:03:15 +0100926 if (IS_ERR(key_ref)) {
927 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 goto error;
929 }
930
David Howells664cceb2005-09-28 17:03:15 +0100931 key = key_ref_to_ptr(key_ref);
932
David Howells76d8aea2005-06-23 22:00:49 -0700933 /* make the changes with the locks held to prevent chown/chmod races */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 ret = -EACCES;
935 down_write(&key->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
David Howells76d8aea2005-06-23 22:00:49 -0700937 /* if we're not the sysadmin, we can only change a key that we own */
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -0800938 if (capable(CAP_SYS_ADMIN) || uid_eq(key->uid, current_fsuid())) {
David Howells76d8aea2005-06-23 22:00:49 -0700939 key->perm = perm;
940 ret = 0;
941 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 up_write(&key->sem);
944 key_put(key);
David Howells76d8aea2005-06-23 22:00:49 -0700945error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000947}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
David Howells8bbf49762008-11-14 10:39:14 +1100949/*
David Howells973c9f42011-01-20 16:38:33 +0000950 * Get the destination keyring for instantiation and check that the caller has
951 * Write permission on it.
David Howells8bbf49762008-11-14 10:39:14 +1100952 */
953static long get_instantiation_keyring(key_serial_t ringid,
954 struct request_key_auth *rka,
955 struct key **_dest_keyring)
956{
957 key_ref_t dkref;
958
David Howellseca1bf52008-12-29 00:41:51 +0000959 *_dest_keyring = NULL;
960
David Howells8bbf49762008-11-14 10:39:14 +1100961 /* just return a NULL pointer if we weren't asked to make a link */
David Howellseca1bf52008-12-29 00:41:51 +0000962 if (ringid == 0)
David Howells8bbf49762008-11-14 10:39:14 +1100963 return 0;
David Howells8bbf49762008-11-14 10:39:14 +1100964
965 /* if a specific keyring is nominated by ID, then use that */
966 if (ringid > 0) {
David Howellsf5895942014-03-14 17:44:49 +0000967 dkref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
David Howells8bbf49762008-11-14 10:39:14 +1100968 if (IS_ERR(dkref))
969 return PTR_ERR(dkref);
970 *_dest_keyring = key_ref_to_ptr(dkref);
971 return 0;
972 }
973
974 if (ringid == KEY_SPEC_REQKEY_AUTH_KEY)
975 return -EINVAL;
976
977 /* otherwise specify the destination keyring recorded in the
978 * authorisation key (any KEY_SPEC_*_KEYRING) */
979 if (ringid >= KEY_SPEC_REQUESTOR_KEYRING) {
David Howells21279cf2009-10-15 10:14:35 +0100980 *_dest_keyring = key_get(rka->dest_keyring);
David Howells8bbf49762008-11-14 10:39:14 +1100981 return 0;
982 }
983
984 return -ENOKEY;
985}
986
David Howellsd84f4f92008-11-14 10:39:23 +1100987/*
David Howells973c9f42011-01-20 16:38:33 +0000988 * Change the request_key authorisation key on the current process.
David Howellsd84f4f92008-11-14 10:39:23 +1100989 */
990static int keyctl_change_reqkey_auth(struct key *key)
991{
992 struct cred *new;
993
994 new = prepare_creds();
995 if (!new)
996 return -ENOMEM;
997
998 key_put(new->request_key_auth);
999 new->request_key_auth = key_get(key);
1000
1001 return commit_creds(new);
1002}
1003
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004/*
David Howellsee009e4a02011-03-07 15:06:20 +00001005 * Copy the iovec data from userspace
1006 */
1007static long copy_from_user_iovec(void *buffer, const struct iovec *iov,
1008 unsigned ioc)
1009{
1010 for (; ioc > 0; ioc--) {
1011 if (copy_from_user(buffer, iov->iov_base, iov->iov_len) != 0)
1012 return -EFAULT;
1013 buffer += iov->iov_len;
1014 iov++;
1015 }
1016 return 0;
1017}
1018
1019/*
David Howells973c9f42011-01-20 16:38:33 +00001020 * Instantiate a key with the specified payload and link the key into the
1021 * destination keyring if one is given.
1022 *
1023 * The caller must have the appropriate instantiation permit set for this to
1024 * work (see keyctl_assume_authority). No other permissions are required.
1025 *
1026 * If successful, 0 will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 */
David Howellsee009e4a02011-03-07 15:06:20 +00001028long keyctl_instantiate_key_common(key_serial_t id,
1029 const struct iovec *payload_iov,
1030 unsigned ioc,
1031 size_t plen,
1032 key_serial_t ringid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033{
David Howellsd84f4f92008-11-14 10:39:23 +11001034 const struct cred *cred = current_cred();
David Howells3e301482005-06-23 22:00:56 -07001035 struct request_key_auth *rka;
David Howells8bbf49762008-11-14 10:39:14 +11001036 struct key *instkey, *dest_keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 void *payload;
1038 long ret;
David Howells38bbca62008-04-29 01:01:19 -07001039 bool vm = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
David Howellsd84f4f92008-11-14 10:39:23 +11001041 kenter("%d,,%zu,%d", id, plen, ringid);
1042
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 ret = -EINVAL;
David Howells38bbca62008-04-29 01:01:19 -07001044 if (plen > 1024 * 1024 - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 goto error;
1046
David Howellsb5f545c2006-01-08 01:02:47 -08001047 /* the appropriate instantiation authorisation key must have been
1048 * assumed before calling this */
1049 ret = -EPERM;
David Howellsd84f4f92008-11-14 10:39:23 +11001050 instkey = cred->request_key_auth;
David Howellsb5f545c2006-01-08 01:02:47 -08001051 if (!instkey)
1052 goto error;
1053
1054 rka = instkey->payload.data;
1055 if (rka->target_key->serial != id)
1056 goto error;
1057
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 /* pull the payload in if one was supplied */
1059 payload = NULL;
1060
David Howellsee009e4a02011-03-07 15:06:20 +00001061 if (payload_iov) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 ret = -ENOMEM;
1063 payload = kmalloc(plen, GFP_KERNEL);
David Howells38bbca62008-04-29 01:01:19 -07001064 if (!payload) {
1065 if (plen <= PAGE_SIZE)
1066 goto error;
1067 vm = true;
1068 payload = vmalloc(plen);
1069 if (!payload)
1070 goto error;
1071 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
David Howellsee009e4a02011-03-07 15:06:20 +00001073 ret = copy_from_user_iovec(payload, payload_iov, ioc);
1074 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 goto error2;
1076 }
1077
David Howells3e301482005-06-23 22:00:56 -07001078 /* find the destination keyring amongst those belonging to the
1079 * requesting task */
David Howells8bbf49762008-11-14 10:39:14 +11001080 ret = get_instantiation_keyring(ringid, rka, &dest_keyring);
1081 if (ret < 0)
1082 goto error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
1084 /* instantiate the key and link it into a keyring */
David Howells3e301482005-06-23 22:00:56 -07001085 ret = key_instantiate_and_link(rka->target_key, payload, plen,
David Howells8bbf49762008-11-14 10:39:14 +11001086 dest_keyring, instkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
David Howells8bbf49762008-11-14 10:39:14 +11001088 key_put(dest_keyring);
David Howellsb5f545c2006-01-08 01:02:47 -08001089
1090 /* discard the assumed authority if it's just been disabled by
1091 * instantiation of the key */
David Howellsd84f4f92008-11-14 10:39:23 +11001092 if (ret == 0)
1093 keyctl_change_reqkey_auth(NULL);
David Howellsb5f545c2006-01-08 01:02:47 -08001094
1095error2:
David Howells38bbca62008-04-29 01:01:19 -07001096 if (!vm)
1097 kfree(payload);
1098 else
1099 vfree(payload);
David Howellsb5f545c2006-01-08 01:02:47 -08001100error:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +00001102}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104/*
David Howellsee009e4a02011-03-07 15:06:20 +00001105 * Instantiate a key with the specified payload and link the key into the
1106 * destination keyring if one is given.
1107 *
1108 * The caller must have the appropriate instantiation permit set for this to
1109 * work (see keyctl_assume_authority). No other permissions are required.
1110 *
1111 * If successful, 0 will be returned.
1112 */
1113long keyctl_instantiate_key(key_serial_t id,
1114 const void __user *_payload,
1115 size_t plen,
1116 key_serial_t ringid)
1117{
1118 if (_payload && plen) {
1119 struct iovec iov[1] = {
1120 [0].iov_base = (void __user *)_payload,
1121 [0].iov_len = plen
1122 };
1123
1124 return keyctl_instantiate_key_common(id, iov, 1, plen, ringid);
1125 }
1126
1127 return keyctl_instantiate_key_common(id, NULL, 0, 0, ringid);
1128}
1129
1130/*
1131 * Instantiate a key with the specified multipart payload and link the key into
1132 * the destination keyring if one is given.
1133 *
1134 * The caller must have the appropriate instantiation permit set for this to
1135 * work (see keyctl_assume_authority). No other permissions are required.
1136 *
1137 * If successful, 0 will be returned.
1138 */
1139long keyctl_instantiate_key_iov(key_serial_t id,
1140 const struct iovec __user *_payload_iov,
1141 unsigned ioc,
1142 key_serial_t ringid)
1143{
1144 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
1145 long ret;
1146
David Howells423b97882012-05-21 12:32:13 +01001147 if (!_payload_iov || !ioc)
David Howellsee009e4a02011-03-07 15:06:20 +00001148 goto no_payload;
1149
1150 ret = rw_copy_check_uvector(WRITE, _payload_iov, ioc,
Christopher Yeohac34ebb2012-05-31 16:26:42 -07001151 ARRAY_SIZE(iovstack), iovstack, &iov);
David Howellsee009e4a02011-03-07 15:06:20 +00001152 if (ret < 0)
Alan Coxa84a9212012-09-28 12:20:02 +01001153 goto err;
David Howellsee009e4a02011-03-07 15:06:20 +00001154 if (ret == 0)
1155 goto no_payload_free;
1156
1157 ret = keyctl_instantiate_key_common(id, iov, ioc, ret, ringid);
Alan Coxa84a9212012-09-28 12:20:02 +01001158err:
David Howellsee009e4a02011-03-07 15:06:20 +00001159 if (iov != iovstack)
1160 kfree(iov);
1161 return ret;
1162
1163no_payload_free:
1164 if (iov != iovstack)
1165 kfree(iov);
1166no_payload:
1167 return keyctl_instantiate_key_common(id, NULL, 0, 0, ringid);
1168}
1169
1170/*
David Howells973c9f42011-01-20 16:38:33 +00001171 * Negatively instantiate the key with the given timeout (in seconds) and link
1172 * the key into the destination keyring if one is given.
1173 *
1174 * The caller must have the appropriate instantiation permit set for this to
1175 * work (see keyctl_assume_authority). No other permissions are required.
1176 *
1177 * The key and any links to the key will be automatically garbage collected
1178 * after the timeout expires.
1179 *
1180 * Negative keys are used to rate limit repeated request_key() calls by causing
1181 * them to return -ENOKEY until the negative key expires.
1182 *
1183 * If successful, 0 will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 */
1185long keyctl_negate_key(key_serial_t id, unsigned timeout, key_serial_t ringid)
1186{
David Howellsfdd1b942011-03-07 15:06:09 +00001187 return keyctl_reject_key(id, timeout, ENOKEY, ringid);
1188}
1189
1190/*
1191 * Negatively instantiate the key with the given timeout (in seconds) and error
1192 * code and link the key into the destination keyring if one is given.
1193 *
1194 * The caller must have the appropriate instantiation permit set for this to
1195 * work (see keyctl_assume_authority). No other permissions are required.
1196 *
1197 * The key and any links to the key will be automatically garbage collected
1198 * after the timeout expires.
1199 *
1200 * Negative keys are used to rate limit repeated request_key() calls by causing
1201 * them to return the specified error code until the negative key expires.
1202 *
1203 * If successful, 0 will be returned.
1204 */
1205long keyctl_reject_key(key_serial_t id, unsigned timeout, unsigned error,
1206 key_serial_t ringid)
1207{
David Howellsd84f4f92008-11-14 10:39:23 +11001208 const struct cred *cred = current_cred();
David Howells3e301482005-06-23 22:00:56 -07001209 struct request_key_auth *rka;
David Howells8bbf49762008-11-14 10:39:14 +11001210 struct key *instkey, *dest_keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 long ret;
1212
David Howellsfdd1b942011-03-07 15:06:09 +00001213 kenter("%d,%u,%u,%d", id, timeout, error, ringid);
1214
1215 /* must be a valid error code and mustn't be a kernel special */
1216 if (error <= 0 ||
1217 error >= MAX_ERRNO ||
1218 error == ERESTARTSYS ||
1219 error == ERESTARTNOINTR ||
1220 error == ERESTARTNOHAND ||
1221 error == ERESTART_RESTARTBLOCK)
1222 return -EINVAL;
David Howellsd84f4f92008-11-14 10:39:23 +11001223
David Howellsb5f545c2006-01-08 01:02:47 -08001224 /* the appropriate instantiation authorisation key must have been
1225 * assumed before calling this */
1226 ret = -EPERM;
David Howellsd84f4f92008-11-14 10:39:23 +11001227 instkey = cred->request_key_auth;
David Howellsb5f545c2006-01-08 01:02:47 -08001228 if (!instkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
David Howells3e301482005-06-23 22:00:56 -07001231 rka = instkey->payload.data;
David Howellsb5f545c2006-01-08 01:02:47 -08001232 if (rka->target_key->serial != id)
1233 goto error;
David Howells3e301482005-06-23 22:00:56 -07001234
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 /* find the destination keyring if present (which must also be
1236 * writable) */
David Howells8bbf49762008-11-14 10:39:14 +11001237 ret = get_instantiation_keyring(ringid, rka, &dest_keyring);
1238 if (ret < 0)
1239 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
1241 /* instantiate the key and link it into a keyring */
David Howellsfdd1b942011-03-07 15:06:09 +00001242 ret = key_reject_and_link(rka->target_key, timeout, error,
David Howells8bbf49762008-11-14 10:39:14 +11001243 dest_keyring, instkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
David Howells8bbf49762008-11-14 10:39:14 +11001245 key_put(dest_keyring);
David Howellsb5f545c2006-01-08 01:02:47 -08001246
1247 /* discard the assumed authority if it's just been disabled by
1248 * instantiation of the key */
David Howellsd84f4f92008-11-14 10:39:23 +11001249 if (ret == 0)
1250 keyctl_change_reqkey_auth(NULL);
David Howellsb5f545c2006-01-08 01:02:47 -08001251
1252error:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +00001254}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256/*
David Howells973c9f42011-01-20 16:38:33 +00001257 * Read or set the default keyring in which request_key() will cache keys and
1258 * return the old setting.
1259 *
1260 * If a process keyring is specified then this will be created if it doesn't
1261 * yet exist. The old setting will be returned if successful.
David Howells3e301482005-06-23 22:00:56 -07001262 */
1263long keyctl_set_reqkey_keyring(int reqkey_defl)
1264{
David Howellsd84f4f92008-11-14 10:39:23 +11001265 struct cred *new;
1266 int ret, old_setting;
1267
1268 old_setting = current_cred_xxx(jit_keyring);
1269
1270 if (reqkey_defl == KEY_REQKEY_DEFL_NO_CHANGE)
1271 return old_setting;
1272
1273 new = prepare_creds();
1274 if (!new)
1275 return -ENOMEM;
David Howells3e301482005-06-23 22:00:56 -07001276
1277 switch (reqkey_defl) {
1278 case KEY_REQKEY_DEFL_THREAD_KEYRING:
David Howellsd84f4f92008-11-14 10:39:23 +11001279 ret = install_thread_keyring_to_cred(new);
David Howells3e301482005-06-23 22:00:56 -07001280 if (ret < 0)
David Howellsd84f4f92008-11-14 10:39:23 +11001281 goto error;
David Howells3e301482005-06-23 22:00:56 -07001282 goto set;
1283
1284 case KEY_REQKEY_DEFL_PROCESS_KEYRING:
David Howellsd84f4f92008-11-14 10:39:23 +11001285 ret = install_process_keyring_to_cred(new);
1286 if (ret < 0) {
1287 if (ret != -EEXIST)
1288 goto error;
1289 ret = 0;
1290 }
1291 goto set;
David Howells3e301482005-06-23 22:00:56 -07001292
1293 case KEY_REQKEY_DEFL_DEFAULT:
1294 case KEY_REQKEY_DEFL_SESSION_KEYRING:
1295 case KEY_REQKEY_DEFL_USER_KEYRING:
1296 case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
David Howellsd84f4f92008-11-14 10:39:23 +11001297 case KEY_REQKEY_DEFL_REQUESTOR_KEYRING:
1298 goto set;
David Howells3e301482005-06-23 22:00:56 -07001299
1300 case KEY_REQKEY_DEFL_NO_CHANGE:
David Howells3e301482005-06-23 22:00:56 -07001301 case KEY_REQKEY_DEFL_GROUP_KEYRING:
1302 default:
David Howellsd84f4f92008-11-14 10:39:23 +11001303 ret = -EINVAL;
1304 goto error;
David Howells3e301482005-06-23 22:00:56 -07001305 }
1306
David Howellsd84f4f92008-11-14 10:39:23 +11001307set:
1308 new->jit_keyring = reqkey_defl;
1309 commit_creds(new);
1310 return old_setting;
1311error:
1312 abort_creds(new);
Dan Carpenter4303ef12010-06-11 17:30:05 +01001313 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +00001314}
David Howellsd84f4f92008-11-14 10:39:23 +11001315
David Howells3e301482005-06-23 22:00:56 -07001316/*
David Howells973c9f42011-01-20 16:38:33 +00001317 * Set or clear the timeout on a key.
1318 *
1319 * Either the key must grant the caller Setattr permission or else the caller
1320 * must hold an instantiation authorisation token for the key.
1321 *
1322 * The timeout is either 0 to clear the timeout, or a number of seconds from
1323 * the current time. The key and any links to the key will be automatically
1324 * garbage collected after the timeout expires.
1325 *
1326 * If successful, 0 is returned.
David Howells017679c2006-01-08 01:02:43 -08001327 */
1328long keyctl_set_timeout(key_serial_t id, unsigned timeout)
1329{
David Howells91562352010-06-11 17:31:05 +01001330 struct key *key, *instkey;
David Howells017679c2006-01-08 01:02:43 -08001331 key_ref_t key_ref;
David Howells017679c2006-01-08 01:02:43 -08001332 long ret;
1333
David Howells55931222009-09-02 09:13:45 +01001334 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
David Howellsf5895942014-03-14 17:44:49 +00001335 KEY_NEED_SETATTR);
David Howells017679c2006-01-08 01:02:43 -08001336 if (IS_ERR(key_ref)) {
David Howells91562352010-06-11 17:31:05 +01001337 /* setting the timeout on a key under construction is permitted
1338 * if we have the authorisation token handy */
1339 if (PTR_ERR(key_ref) == -EACCES) {
1340 instkey = key_get_instantiation_authkey(id);
1341 if (!IS_ERR(instkey)) {
1342 key_put(instkey);
1343 key_ref = lookup_user_key(id,
1344 KEY_LOOKUP_PARTIAL,
1345 0);
1346 if (!IS_ERR(key_ref))
1347 goto okay;
1348 }
1349 }
1350
David Howells017679c2006-01-08 01:02:43 -08001351 ret = PTR_ERR(key_ref);
1352 goto error;
1353 }
1354
David Howells91562352010-06-11 17:31:05 +01001355okay:
David Howells017679c2006-01-08 01:02:43 -08001356 key = key_ref_to_ptr(key_ref);
Bryan Schumaker59e6b9c2012-02-24 14:14:50 -05001357 key_set_timeout(key, timeout);
David Howells017679c2006-01-08 01:02:43 -08001358 key_put(key);
1359
1360 ret = 0;
1361error:
1362 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +00001363}
David Howells017679c2006-01-08 01:02:43 -08001364
David Howells017679c2006-01-08 01:02:43 -08001365/*
David Howells973c9f42011-01-20 16:38:33 +00001366 * Assume (or clear) the authority to instantiate the specified key.
1367 *
1368 * This sets the authoritative token currently in force for key instantiation.
1369 * This must be done for a key to be instantiated. It has the effect of making
1370 * available all the keys from the caller of the request_key() that created a
1371 * key to request_key() calls made by the caller of this function.
1372 *
1373 * The caller must have the instantiation key in their process keyrings with a
1374 * Search permission grant available to the caller.
1375 *
1376 * If the ID given is 0, then the setting will be cleared and 0 returned.
1377 *
1378 * If the ID given has a matching an authorisation key, then that key will be
1379 * set and its ID will be returned. The authorisation key can be read to get
1380 * the callout information passed to request_key().
David Howellsb5f545c2006-01-08 01:02:47 -08001381 */
1382long keyctl_assume_authority(key_serial_t id)
1383{
1384 struct key *authkey;
1385 long ret;
1386
1387 /* special key IDs aren't permitted */
1388 ret = -EINVAL;
1389 if (id < 0)
1390 goto error;
1391
1392 /* we divest ourselves of authority if given an ID of 0 */
1393 if (id == 0) {
David Howellsd84f4f92008-11-14 10:39:23 +11001394 ret = keyctl_change_reqkey_auth(NULL);
David Howellsb5f545c2006-01-08 01:02:47 -08001395 goto error;
1396 }
1397
1398 /* attempt to assume the authority temporarily granted to us whilst we
1399 * instantiate the specified key
1400 * - the authorisation key must be in the current task's keyrings
1401 * somewhere
1402 */
1403 authkey = key_get_instantiation_authkey(id);
1404 if (IS_ERR(authkey)) {
1405 ret = PTR_ERR(authkey);
1406 goto error;
1407 }
1408
David Howellsd84f4f92008-11-14 10:39:23 +11001409 ret = keyctl_change_reqkey_auth(authkey);
1410 if (ret < 0)
1411 goto error;
1412 key_put(authkey);
David Howellsb5f545c2006-01-08 01:02:47 -08001413
David Howellsd84f4f92008-11-14 10:39:23 +11001414 ret = authkey->serial;
David Howellsb5f545c2006-01-08 01:02:47 -08001415error:
1416 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +00001417}
David Howellsb5f545c2006-01-08 01:02:47 -08001418
David Howells70a5bb72008-04-29 01:01:26 -07001419/*
David Howells973c9f42011-01-20 16:38:33 +00001420 * Get a key's the LSM security label.
1421 *
1422 * The key must grant the caller View permission for this to work.
1423 *
1424 * If there's a buffer, then up to buflen bytes of data will be placed into it.
1425 *
1426 * If successful, the amount of information available will be returned,
1427 * irrespective of how much was copied (including the terminal NUL).
David Howells70a5bb72008-04-29 01:01:26 -07001428 */
1429long keyctl_get_security(key_serial_t keyid,
1430 char __user *buffer,
1431 size_t buflen)
1432{
1433 struct key *key, *instkey;
1434 key_ref_t key_ref;
1435 char *context;
1436 long ret;
1437
David Howellsf5895942014-03-14 17:44:49 +00001438 key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_NEED_VIEW);
David Howells70a5bb72008-04-29 01:01:26 -07001439 if (IS_ERR(key_ref)) {
1440 if (PTR_ERR(key_ref) != -EACCES)
1441 return PTR_ERR(key_ref);
1442
1443 /* viewing a key under construction is also permitted if we
1444 * have the authorisation token handy */
1445 instkey = key_get_instantiation_authkey(keyid);
1446 if (IS_ERR(instkey))
Roel Kluinfa1cc7b2009-12-15 15:05:12 -08001447 return PTR_ERR(instkey);
David Howells70a5bb72008-04-29 01:01:26 -07001448 key_put(instkey);
1449
David Howells55931222009-09-02 09:13:45 +01001450 key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, 0);
David Howells70a5bb72008-04-29 01:01:26 -07001451 if (IS_ERR(key_ref))
1452 return PTR_ERR(key_ref);
1453 }
1454
1455 key = key_ref_to_ptr(key_ref);
1456 ret = security_key_getsecurity(key, &context);
1457 if (ret == 0) {
1458 /* if no information was returned, give userspace an empty
1459 * string */
1460 ret = 1;
1461 if (buffer && buflen > 0 &&
1462 copy_to_user(buffer, "", 1) != 0)
1463 ret = -EFAULT;
1464 } else if (ret > 0) {
1465 /* return as much data as there's room for */
1466 if (buffer && buflen > 0) {
1467 if (buflen > ret)
1468 buflen = ret;
1469
1470 if (copy_to_user(buffer, context, buflen) != 0)
1471 ret = -EFAULT;
1472 }
1473
1474 kfree(context);
1475 }
1476
1477 key_ref_put(key_ref);
1478 return ret;
1479}
1480
David Howellsee18d642009-09-02 09:14:21 +01001481/*
David Howells973c9f42011-01-20 16:38:33 +00001482 * Attempt to install the calling process's session keyring on the process's
1483 * parent process.
1484 *
1485 * The keyring must exist and must grant the caller LINK permission, and the
1486 * parent process must be single-threaded and must have the same effective
1487 * ownership as this process and mustn't be SUID/SGID.
1488 *
1489 * The keyring will be emplaced on the parent when it next resumes userspace.
1490 *
1491 * If successful, 0 will be returned.
David Howellsee18d642009-09-02 09:14:21 +01001492 */
1493long keyctl_session_to_parent(void)
1494{
1495 struct task_struct *me, *parent;
1496 const struct cred *mycred, *pcred;
Al Viro67d12142012-06-27 11:07:19 +04001497 struct callback_head *newwork, *oldwork;
David Howellsee18d642009-09-02 09:14:21 +01001498 key_ref_t keyring_r;
Oleg Nesterov413cd3d2012-05-11 10:59:08 +10001499 struct cred *cred;
David Howellsee18d642009-09-02 09:14:21 +01001500 int ret;
1501
David Howellsf5895942014-03-14 17:44:49 +00001502 keyring_r = lookup_user_key(KEY_SPEC_SESSION_KEYRING, 0, KEY_NEED_LINK);
David Howellsee18d642009-09-02 09:14:21 +01001503 if (IS_ERR(keyring_r))
1504 return PTR_ERR(keyring_r);
1505
Oleg Nesterov413cd3d2012-05-11 10:59:08 +10001506 ret = -ENOMEM;
Oleg Nesterov413cd3d2012-05-11 10:59:08 +10001507
David Howellsee18d642009-09-02 09:14:21 +01001508 /* our parent is going to need a new cred struct, a new tgcred struct
1509 * and new security data, so we allocate them here to prevent ENOMEM in
1510 * our parent */
David Howellsee18d642009-09-02 09:14:21 +01001511 cred = cred_alloc_blank();
1512 if (!cred)
Al Viro67d12142012-06-27 11:07:19 +04001513 goto error_keyring;
1514 newwork = &cred->rcu;
David Howellsee18d642009-09-02 09:14:21 +01001515
David Howells3a505972012-10-02 19:24:29 +01001516 cred->session_keyring = key_ref_to_ptr(keyring_r);
1517 keyring_r = NULL;
Al Viro67d12142012-06-27 11:07:19 +04001518 init_task_work(newwork, key_change_session_keyring);
David Howellsee18d642009-09-02 09:14:21 +01001519
1520 me = current;
David Howells9d1ac652010-09-10 09:59:46 +01001521 rcu_read_lock();
David Howellsee18d642009-09-02 09:14:21 +01001522 write_lock_irq(&tasklist_lock);
1523
David Howellsee18d642009-09-02 09:14:21 +01001524 ret = -EPERM;
Oleg Nesterov413cd3d2012-05-11 10:59:08 +10001525 oldwork = NULL;
1526 parent = me->real_parent;
David Howellsee18d642009-09-02 09:14:21 +01001527
1528 /* the parent mustn't be init and mustn't be a kernel thread */
1529 if (parent->pid <= 1 || !parent->mm)
Oleg Nesterov413cd3d2012-05-11 10:59:08 +10001530 goto unlock;
David Howellsee18d642009-09-02 09:14:21 +01001531
1532 /* the parent must be single threaded */
Oleg Nesterovdd98acf2010-05-26 14:43:23 -07001533 if (!thread_group_empty(parent))
Oleg Nesterov413cd3d2012-05-11 10:59:08 +10001534 goto unlock;
David Howellsee18d642009-09-02 09:14:21 +01001535
1536 /* the parent and the child must have different session keyrings or
1537 * there's no point */
1538 mycred = current_cred();
1539 pcred = __task_cred(parent);
1540 if (mycred == pcred ||
David Howells3a505972012-10-02 19:24:29 +01001541 mycred->session_keyring == pcred->session_keyring) {
Oleg Nesterov413cd3d2012-05-11 10:59:08 +10001542 ret = 0;
1543 goto unlock;
1544 }
David Howellsee18d642009-09-02 09:14:21 +01001545
1546 /* the parent must have the same effective ownership and mustn't be
1547 * SUID/SGID */
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -08001548 if (!uid_eq(pcred->uid, mycred->euid) ||
1549 !uid_eq(pcred->euid, mycred->euid) ||
1550 !uid_eq(pcred->suid, mycred->euid) ||
1551 !gid_eq(pcred->gid, mycred->egid) ||
1552 !gid_eq(pcred->egid, mycred->egid) ||
1553 !gid_eq(pcred->sgid, mycred->egid))
Oleg Nesterov413cd3d2012-05-11 10:59:08 +10001554 goto unlock;
David Howellsee18d642009-09-02 09:14:21 +01001555
1556 /* the keyrings must have the same UID */
David Howells3a505972012-10-02 19:24:29 +01001557 if ((pcred->session_keyring &&
Linus Torvalds2a74dbb2012-12-16 15:40:50 -08001558 !uid_eq(pcred->session_keyring->uid, mycred->euid)) ||
1559 !uid_eq(mycred->session_keyring->uid, mycred->euid))
Oleg Nesterov413cd3d2012-05-11 10:59:08 +10001560 goto unlock;
David Howellsee18d642009-09-02 09:14:21 +01001561
Oleg Nesterov413cd3d2012-05-11 10:59:08 +10001562 /* cancel an already pending keyring replacement */
1563 oldwork = task_work_cancel(parent, key_change_session_keyring);
David Howellsee18d642009-09-02 09:14:21 +01001564
1565 /* the replacement session keyring is applied just prior to userspace
1566 * restarting */
Al Viro67d12142012-06-27 11:07:19 +04001567 ret = task_work_add(parent, newwork, true);
Oleg Nesterov413cd3d2012-05-11 10:59:08 +10001568 if (!ret)
1569 newwork = NULL;
1570unlock:
David Howellsee18d642009-09-02 09:14:21 +01001571 write_unlock_irq(&tasklist_lock);
David Howells9d1ac652010-09-10 09:59:46 +01001572 rcu_read_unlock();
Al Viro67d12142012-06-27 11:07:19 +04001573 if (oldwork)
1574 put_cred(container_of(oldwork, struct cred, rcu));
1575 if (newwork)
1576 put_cred(cred);
David Howellsee18d642009-09-02 09:14:21 +01001577 return ret;
1578
1579error_keyring:
1580 key_ref_put(keyring_r);
1581 return ret;
1582}
1583
David Howellsb5f545c2006-01-08 01:02:47 -08001584/*
David Howells973c9f42011-01-20 16:38:33 +00001585 * The key control system call
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 */
Heiko Carstens938bb9f2009-01-14 14:14:30 +01001587SYSCALL_DEFINE5(keyctl, int, option, unsigned long, arg2, unsigned long, arg3,
1588 unsigned long, arg4, unsigned long, arg5)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589{
1590 switch (option) {
1591 case KEYCTL_GET_KEYRING_ID:
1592 return keyctl_get_keyring_ID((key_serial_t) arg2,
1593 (int) arg3);
1594
1595 case KEYCTL_JOIN_SESSION_KEYRING:
1596 return keyctl_join_session_keyring((const char __user *) arg2);
1597
1598 case KEYCTL_UPDATE:
1599 return keyctl_update_key((key_serial_t) arg2,
1600 (const void __user *) arg3,
1601 (size_t) arg4);
1602
1603 case KEYCTL_REVOKE:
1604 return keyctl_revoke_key((key_serial_t) arg2);
1605
1606 case KEYCTL_DESCRIBE:
1607 return keyctl_describe_key((key_serial_t) arg2,
1608 (char __user *) arg3,
1609 (unsigned) arg4);
1610
1611 case KEYCTL_CLEAR:
1612 return keyctl_keyring_clear((key_serial_t) arg2);
1613
1614 case KEYCTL_LINK:
1615 return keyctl_keyring_link((key_serial_t) arg2,
1616 (key_serial_t) arg3);
1617
1618 case KEYCTL_UNLINK:
1619 return keyctl_keyring_unlink((key_serial_t) arg2,
1620 (key_serial_t) arg3);
1621
1622 case KEYCTL_SEARCH:
1623 return keyctl_keyring_search((key_serial_t) arg2,
1624 (const char __user *) arg3,
1625 (const char __user *) arg4,
1626 (key_serial_t) arg5);
1627
1628 case KEYCTL_READ:
1629 return keyctl_read_key((key_serial_t) arg2,
1630 (char __user *) arg3,
1631 (size_t) arg4);
1632
1633 case KEYCTL_CHOWN:
1634 return keyctl_chown_key((key_serial_t) arg2,
1635 (uid_t) arg3,
1636 (gid_t) arg4);
1637
1638 case KEYCTL_SETPERM:
1639 return keyctl_setperm_key((key_serial_t) arg2,
1640 (key_perm_t) arg3);
1641
1642 case KEYCTL_INSTANTIATE:
1643 return keyctl_instantiate_key((key_serial_t) arg2,
1644 (const void __user *) arg3,
1645 (size_t) arg4,
1646 (key_serial_t) arg5);
1647
1648 case KEYCTL_NEGATE:
1649 return keyctl_negate_key((key_serial_t) arg2,
1650 (unsigned) arg3,
1651 (key_serial_t) arg4);
1652
David Howells3e301482005-06-23 22:00:56 -07001653 case KEYCTL_SET_REQKEY_KEYRING:
1654 return keyctl_set_reqkey_keyring(arg2);
1655
David Howells017679c2006-01-08 01:02:43 -08001656 case KEYCTL_SET_TIMEOUT:
1657 return keyctl_set_timeout((key_serial_t) arg2,
1658 (unsigned) arg3);
1659
David Howellsb5f545c2006-01-08 01:02:47 -08001660 case KEYCTL_ASSUME_AUTHORITY:
1661 return keyctl_assume_authority((key_serial_t) arg2);
1662
David Howells70a5bb72008-04-29 01:01:26 -07001663 case KEYCTL_GET_SECURITY:
1664 return keyctl_get_security((key_serial_t) arg2,
James Morris90bd49a2008-12-29 14:35:35 +11001665 (char __user *) arg3,
David Howells70a5bb72008-04-29 01:01:26 -07001666 (size_t) arg4);
1667
David Howellsee18d642009-09-02 09:14:21 +01001668 case KEYCTL_SESSION_TO_PARENT:
1669 return keyctl_session_to_parent();
1670
David Howellsfdd1b942011-03-07 15:06:09 +00001671 case KEYCTL_REJECT:
1672 return keyctl_reject_key((key_serial_t) arg2,
1673 (unsigned) arg3,
1674 (unsigned) arg4,
1675 (key_serial_t) arg5);
1676
David Howellsee009e4a02011-03-07 15:06:20 +00001677 case KEYCTL_INSTANTIATE_IOV:
1678 return keyctl_instantiate_key_iov(
1679 (key_serial_t) arg2,
1680 (const struct iovec __user *) arg3,
1681 (unsigned) arg4,
1682 (key_serial_t) arg5);
1683
David Howellsfd758152012-05-11 10:56:56 +01001684 case KEYCTL_INVALIDATE:
1685 return keyctl_invalidate_key((key_serial_t) arg2);
1686
David Howellsf36f8c72013-09-24 10:35:19 +01001687 case KEYCTL_GET_PERSISTENT:
1688 return keyctl_get_persistent((uid_t)arg2, (key_serial_t)arg3);
1689
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 default:
1691 return -EOPNOTSUPP;
1692 }
David Howellsa8b17ed2011-01-20 16:38:27 +00001693}