blob: 0a4a21d73f6a44086026008f910f37106f5772f1 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <asm/uaccess.h>
26#include "internal.h"
27
Davi Arnaut0cb409d2006-03-24 03:18:43 -080028static int key_get_type_from_user(char *type,
29 const char __user *_type,
30 unsigned len)
31{
32 int ret;
33
34 ret = strncpy_from_user(type, _type, len);
Davi Arnaut0cb409d2006-03-24 03:18:43 -080035 if (ret < 0)
Dan Carpenter4303ef12010-06-11 17:30:05 +010036 return ret;
Davi Arnaut0cb409d2006-03-24 03:18:43 -080037 if (ret == 0 || ret >= len)
38 return -EINVAL;
Davi Arnaut0cb409d2006-03-24 03:18:43 -080039 if (type[0] == '.')
40 return -EPERM;
Davi Arnaut0cb409d2006-03-24 03:18:43 -080041 type[len - 1] = '\0';
Davi Arnaut0cb409d2006-03-24 03:18:43 -080042 return 0;
43}
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045/*
David Howells973c9f42011-01-20 16:38:33 +000046 * Extract the description of a new key from userspace and either add it as a
47 * new key to the specified keyring or update a matching key in that keyring.
48 *
49 * The keyring must be writable so that we can attach the key to it.
50 *
51 * If successful, the new key's serial number is returned, otherwise an error
52 * code is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 */
Heiko Carstens1e7bfb22009-01-14 14:14:29 +010054SYSCALL_DEFINE5(add_key, const char __user *, _type,
55 const char __user *, _description,
56 const void __user *, _payload,
57 size_t, plen,
58 key_serial_t, ringid)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059{
David Howells664cceb2005-09-28 17:03:15 +010060 key_ref_t keyring_ref, key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 char type[32], *description;
62 void *payload;
Davi Arnaut0cb409d2006-03-24 03:18:43 -080063 long ret;
David Howells38bbca62008-04-29 01:01:19 -070064 bool vm;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66 ret = -EINVAL;
David Howells38bbca62008-04-29 01:01:19 -070067 if (plen > 1024 * 1024 - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 goto error;
69
70 /* draw all the data into kernel space */
Davi Arnaut0cb409d2006-03-24 03:18:43 -080071 ret = key_get_type_from_user(type, _type, sizeof(type));
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 if (ret < 0)
73 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Davi Arnaut0cb409d2006-03-24 03:18:43 -080075 description = strndup_user(_description, PAGE_SIZE);
76 if (IS_ERR(description)) {
77 ret = PTR_ERR(description);
David Howells3e301482005-06-23 22:00:56 -070078 goto error;
Davi Arnaut0cb409d2006-03-24 03:18:43 -080079 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81 /* pull the payload in if one was supplied */
82 payload = NULL;
83
David Howells38bbca62008-04-29 01:01:19 -070084 vm = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 if (_payload) {
86 ret = -ENOMEM;
87 payload = kmalloc(plen, GFP_KERNEL);
David Howells38bbca62008-04-29 01:01:19 -070088 if (!payload) {
89 if (plen <= PAGE_SIZE)
90 goto error2;
91 vm = true;
92 payload = vmalloc(plen);
93 if (!payload)
94 goto error2;
95 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97 ret = -EFAULT;
98 if (copy_from_user(payload, _payload, plen) != 0)
99 goto error3;
100 }
101
102 /* find the target keyring (which must be writable) */
David Howells55931222009-09-02 09:13:45 +0100103 keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_WRITE);
David Howells664cceb2005-09-28 17:03:15 +0100104 if (IS_ERR(keyring_ref)) {
105 ret = PTR_ERR(keyring_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 goto error3;
107 }
108
109 /* create or update the requested key and add it to the target
110 * keyring */
David Howells664cceb2005-09-28 17:03:15 +0100111 key_ref = key_create_or_update(keyring_ref, type, description,
Arun Raghavan6b79ccb2008-04-29 01:01:28 -0700112 payload, plen, KEY_PERM_UNDEF,
113 KEY_ALLOC_IN_QUOTA);
David Howells664cceb2005-09-28 17:03:15 +0100114 if (!IS_ERR(key_ref)) {
115 ret = key_ref_to_ptr(key_ref)->serial;
116 key_ref_put(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 }
118 else {
David Howells664cceb2005-09-28 17:03:15 +0100119 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 }
121
David Howells664cceb2005-09-28 17:03:15 +0100122 key_ref_put(keyring_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 error3:
David Howells38bbca62008-04-29 01:01:19 -0700124 if (!vm)
125 kfree(payload);
126 else
127 vfree(payload);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 error2:
129 kfree(description);
130 error:
131 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000132}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134/*
David Howells973c9f42011-01-20 16:38:33 +0000135 * Search the process keyrings and keyring trees linked from those for a
136 * matching key. Keyrings must have appropriate Search permission to be
137 * searched.
138 *
139 * If a key is found, it will be attached to the destination keyring if there's
140 * one specified and the serial number of the key will be returned.
141 *
142 * If no key is found, /sbin/request-key will be invoked if _callout_info is
143 * non-NULL in an attempt to create a key. The _callout_info string will be
144 * passed to /sbin/request-key to aid with completing the request. If the
145 * _callout_info string is "" then it will be changed to "-".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 */
Heiko Carstens1e7bfb22009-01-14 14:14:29 +0100147SYSCALL_DEFINE4(request_key, const char __user *, _type,
148 const char __user *, _description,
149 const char __user *, _callout_info,
150 key_serial_t, destringid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
152 struct key_type *ktype;
David Howells664cceb2005-09-28 17:03:15 +0100153 struct key *key;
154 key_ref_t dest_ref;
David Howells4a38e122008-04-29 01:01:24 -0700155 size_t callout_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 char type[32], *description, *callout_info;
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800157 long ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159 /* pull the type into kernel space */
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800160 ret = key_get_type_from_user(type, _type, sizeof(type));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 if (ret < 0)
162 goto error;
David Howells1260f802005-08-04 11:50:01 +0100163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 /* pull the description into kernel space */
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800165 description = strndup_user(_description, PAGE_SIZE);
166 if (IS_ERR(description)) {
167 ret = PTR_ERR(description);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 goto error;
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171 /* pull the callout info into kernel space */
172 callout_info = NULL;
David Howells4a38e122008-04-29 01:01:24 -0700173 callout_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 if (_callout_info) {
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800175 callout_info = strndup_user(_callout_info, PAGE_SIZE);
176 if (IS_ERR(callout_info)) {
177 ret = PTR_ERR(callout_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 goto error2;
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800179 }
David Howells4a38e122008-04-29 01:01:24 -0700180 callout_len = strlen(callout_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 }
182
183 /* get the destination keyring if specified */
David Howells664cceb2005-09-28 17:03:15 +0100184 dest_ref = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 if (destringid) {
David Howells55931222009-09-02 09:13:45 +0100186 dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE,
187 KEY_WRITE);
David Howells664cceb2005-09-28 17:03:15 +0100188 if (IS_ERR(dest_ref)) {
189 ret = PTR_ERR(dest_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 goto error3;
191 }
192 }
193
194 /* find the key type */
195 ktype = key_type_lookup(type);
196 if (IS_ERR(ktype)) {
197 ret = PTR_ERR(ktype);
198 goto error4;
199 }
200
201 /* do the search */
David Howells4a38e122008-04-29 01:01:24 -0700202 key = request_key_and_link(ktype, description, callout_info,
203 callout_len, NULL, key_ref_to_ptr(dest_ref),
David Howells7e047ef2006-06-26 00:24:50 -0700204 KEY_ALLOC_IN_QUOTA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 if (IS_ERR(key)) {
206 ret = PTR_ERR(key);
207 goto error5;
208 }
209
David Howells4aab1e82011-03-11 17:57:33 +0000210 /* wait for the key to finish being constructed */
211 ret = wait_for_key_construction(key, 1);
212 if (ret < 0)
213 goto error6;
214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 ret = key->serial;
216
David Howells4aab1e82011-03-11 17:57:33 +0000217error6:
David Howells3e301482005-06-23 22:00:56 -0700218 key_put(key);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700219error5:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 key_type_put(ktype);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700221error4:
David Howells664cceb2005-09-28 17:03:15 +0100222 key_ref_put(dest_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700223error3:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 kfree(callout_info);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700225error2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 kfree(description);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700227error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000229}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231/*
David Howells973c9f42011-01-20 16:38:33 +0000232 * Get the ID of the specified process keyring.
233 *
234 * The requested keyring must have search permission to be found.
235 *
236 * If successful, the ID of the requested keyring will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 */
238long keyctl_get_keyring_ID(key_serial_t id, int create)
239{
David Howells664cceb2005-09-28 17:03:15 +0100240 key_ref_t key_ref;
David Howells55931222009-09-02 09:13:45 +0100241 unsigned long lflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 long ret;
243
David Howells55931222009-09-02 09:13:45 +0100244 lflags = create ? KEY_LOOKUP_CREATE : 0;
245 key_ref = lookup_user_key(id, lflags, KEY_SEARCH);
David Howells664cceb2005-09-28 17:03:15 +0100246 if (IS_ERR(key_ref)) {
247 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 goto error;
249 }
250
David Howells664cceb2005-09-28 17:03:15 +0100251 ret = key_ref_to_ptr(key_ref)->serial;
252 key_ref_put(key_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700253error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 return ret;
David Howells973c9f42011-01-20 16:38:33 +0000255}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257/*
David Howells973c9f42011-01-20 16:38:33 +0000258 * Join a (named) session keyring.
259 *
260 * Create and join an anonymous session keyring or join a named session
261 * keyring, creating it if necessary. A named session keyring must have Search
262 * permission for it to be joined. Session keyrings without this permit will
263 * be skipped over.
264 *
265 * If successful, the ID of the joined session keyring will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 */
267long keyctl_join_session_keyring(const char __user *_name)
268{
269 char *name;
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800270 long ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 /* fetch the name from userspace */
273 name = NULL;
274 if (_name) {
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800275 name = strndup_user(_name, PAGE_SIZE);
276 if (IS_ERR(name)) {
277 ret = PTR_ERR(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 goto error;
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 }
281
282 /* join the session */
283 ret = join_session_keyring(name);
Vegard Nossum0d54ee12009-01-17 17:45:45 +0100284 kfree(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700286error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000288}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290/*
David Howells973c9f42011-01-20 16:38:33 +0000291 * Update a key's data payload from the given data.
292 *
293 * The key must grant the caller Write permission and the key type must support
294 * updating for this to work. A negative key can be positively instantiated
295 * with this call.
296 *
297 * If successful, 0 will be returned. If the key type does not support
298 * updating, then -EOPNOTSUPP will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 */
300long keyctl_update_key(key_serial_t id,
301 const void __user *_payload,
302 size_t plen)
303{
David Howells664cceb2005-09-28 17:03:15 +0100304 key_ref_t key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 void *payload;
306 long ret;
307
308 ret = -EINVAL;
309 if (plen > PAGE_SIZE)
310 goto error;
311
312 /* pull the payload in if one was supplied */
313 payload = NULL;
314 if (_payload) {
315 ret = -ENOMEM;
316 payload = kmalloc(plen, GFP_KERNEL);
317 if (!payload)
318 goto error;
319
320 ret = -EFAULT;
321 if (copy_from_user(payload, _payload, plen) != 0)
322 goto error2;
323 }
324
325 /* find the target key (which must be writable) */
David Howells55931222009-09-02 09:13:45 +0100326 key_ref = lookup_user_key(id, 0, KEY_WRITE);
David Howells664cceb2005-09-28 17:03:15 +0100327 if (IS_ERR(key_ref)) {
328 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 goto error2;
330 }
331
332 /* update the key */
David Howells664cceb2005-09-28 17:03:15 +0100333 ret = key_update(key_ref, payload, plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
David Howells664cceb2005-09-28 17:03:15 +0100335 key_ref_put(key_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700336error2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 kfree(payload);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700338error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000340}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342/*
David Howells973c9f42011-01-20 16:38:33 +0000343 * Revoke a key.
344 *
345 * The key must be grant the caller Write or Setattr permission for this to
346 * work. The key type should give up its quota claim when revoked. The key
347 * and any links to the key will be automatically garbage collected after a
348 * certain amount of time (/proc/sys/kernel/keys/gc_delay).
349 *
350 * If successful, 0 is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 */
352long keyctl_revoke_key(key_serial_t id)
353{
David Howells664cceb2005-09-28 17:03:15 +0100354 key_ref_t key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 long ret;
356
David Howells55931222009-09-02 09:13:45 +0100357 key_ref = lookup_user_key(id, 0, KEY_WRITE);
David Howells664cceb2005-09-28 17:03:15 +0100358 if (IS_ERR(key_ref)) {
359 ret = PTR_ERR(key_ref);
David Howells0c2c9a32009-09-02 09:13:50 +0100360 if (ret != -EACCES)
361 goto error;
362 key_ref = lookup_user_key(id, 0, KEY_SETATTR);
363 if (IS_ERR(key_ref)) {
364 ret = PTR_ERR(key_ref);
365 goto error;
366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 }
368
David Howells664cceb2005-09-28 17:03:15 +0100369 key_revoke(key_ref_to_ptr(key_ref));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 ret = 0;
371
David Howells664cceb2005-09-28 17:03:15 +0100372 key_ref_put(key_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700373error:
David Howells1260f802005-08-04 11:50:01 +0100374 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000375}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377/*
David Howells973c9f42011-01-20 16:38:33 +0000378 * Clear the specified keyring, creating an empty process keyring if one of the
379 * special keyring IDs is used.
380 *
381 * The keyring must grant the caller Write permission for this to work. If
382 * successful, 0 will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 */
384long keyctl_keyring_clear(key_serial_t ringid)
385{
David Howells664cceb2005-09-28 17:03:15 +0100386 key_ref_t keyring_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 long ret;
388
David Howells55931222009-09-02 09:13:45 +0100389 keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_WRITE);
David Howells664cceb2005-09-28 17:03:15 +0100390 if (IS_ERR(keyring_ref)) {
391 ret = PTR_ERR(keyring_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 goto error;
393 }
394
David Howells664cceb2005-09-28 17:03:15 +0100395 ret = keyring_clear(key_ref_to_ptr(keyring_ref));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
David Howells664cceb2005-09-28 17:03:15 +0100397 key_ref_put(keyring_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700398error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000400}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402/*
David Howells973c9f42011-01-20 16:38:33 +0000403 * Create a link from a keyring to a key if there's no matching key in the
404 * keyring, otherwise replace the link to the matching key with a link to the
405 * new key.
406 *
407 * The key must grant the caller Link permission and the the keyring must grant
408 * the caller Write permission. Furthermore, if an additional link is created,
409 * the keyring's quota will be extended.
410 *
411 * If successful, 0 will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 */
413long keyctl_keyring_link(key_serial_t id, key_serial_t ringid)
414{
David Howells664cceb2005-09-28 17:03:15 +0100415 key_ref_t keyring_ref, key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 long ret;
417
David Howells55931222009-09-02 09:13:45 +0100418 keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_WRITE);
David Howells664cceb2005-09-28 17:03:15 +0100419 if (IS_ERR(keyring_ref)) {
420 ret = PTR_ERR(keyring_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 goto error;
422 }
423
David Howells55931222009-09-02 09:13:45 +0100424 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE, KEY_LINK);
David Howells664cceb2005-09-28 17:03:15 +0100425 if (IS_ERR(key_ref)) {
426 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 goto error2;
428 }
429
David Howells664cceb2005-09-28 17:03:15 +0100430 ret = key_link(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
David Howells664cceb2005-09-28 17:03:15 +0100432 key_ref_put(key_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700433error2:
David Howells664cceb2005-09-28 17:03:15 +0100434 key_ref_put(keyring_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700435error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000437}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439/*
David Howells973c9f42011-01-20 16:38:33 +0000440 * Unlink a key from a keyring.
441 *
442 * The keyring must grant the caller Write permission for this to work; the key
443 * itself need not grant the caller anything. If the last link to a key is
444 * removed then that key will be scheduled for destruction.
445 *
446 * If successful, 0 will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 */
448long keyctl_keyring_unlink(key_serial_t id, key_serial_t ringid)
449{
David Howells664cceb2005-09-28 17:03:15 +0100450 key_ref_t keyring_ref, key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 long ret;
452
David Howells55931222009-09-02 09:13:45 +0100453 keyring_ref = lookup_user_key(ringid, 0, KEY_WRITE);
David Howells664cceb2005-09-28 17:03:15 +0100454 if (IS_ERR(keyring_ref)) {
455 ret = PTR_ERR(keyring_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 goto error;
457 }
458
David Howells55931222009-09-02 09:13:45 +0100459 key_ref = lookup_user_key(id, KEY_LOOKUP_FOR_UNLINK, 0);
David Howells664cceb2005-09-28 17:03:15 +0100460 if (IS_ERR(key_ref)) {
461 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 goto error2;
463 }
464
David Howells664cceb2005-09-28 17:03:15 +0100465 ret = key_unlink(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
David Howells664cceb2005-09-28 17:03:15 +0100467 key_ref_put(key_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700468error2:
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 * Return a description of a key to userspace.
476 *
477 * The key must grant the caller View permission for this to work.
478 *
479 * If there's a buffer, we place up to buflen bytes of data into it formatted
480 * in the following way:
481 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 * type;uid;gid;perm;description<NUL>
David Howells973c9f42011-01-20 16:38:33 +0000483 *
484 * If successful, we return the amount of description available, irrespective
485 * of how much we may have copied into the buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 */
487long keyctl_describe_key(key_serial_t keyid,
488 char __user *buffer,
489 size_t buflen)
490{
David Howells3e301482005-06-23 22:00:56 -0700491 struct key *key, *instkey;
David Howells664cceb2005-09-28 17:03:15 +0100492 key_ref_t key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 char *tmpbuf;
494 long ret;
495
David Howells55931222009-09-02 09:13:45 +0100496 key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_VIEW);
David Howells664cceb2005-09-28 17:03:15 +0100497 if (IS_ERR(key_ref)) {
David Howells3e301482005-06-23 22:00:56 -0700498 /* viewing a key under construction is permitted if we have the
499 * authorisation token handy */
David Howells664cceb2005-09-28 17:03:15 +0100500 if (PTR_ERR(key_ref) == -EACCES) {
David Howells3e301482005-06-23 22:00:56 -0700501 instkey = key_get_instantiation_authkey(keyid);
502 if (!IS_ERR(instkey)) {
503 key_put(instkey);
David Howells8bbf49762008-11-14 10:39:14 +1100504 key_ref = lookup_user_key(keyid,
David Howells55931222009-09-02 09:13:45 +0100505 KEY_LOOKUP_PARTIAL,
506 0);
David Howells664cceb2005-09-28 17:03:15 +0100507 if (!IS_ERR(key_ref))
David Howells3e301482005-06-23 22:00:56 -0700508 goto okay;
509 }
510 }
511
David Howells664cceb2005-09-28 17:03:15 +0100512 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 goto error;
514 }
515
David Howells3e301482005-06-23 22:00:56 -0700516okay:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 /* calculate how much description we're going to return */
518 ret = -ENOMEM;
519 tmpbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
520 if (!tmpbuf)
521 goto error2;
522
David Howells664cceb2005-09-28 17:03:15 +0100523 key = key_ref_to_ptr(key_ref);
524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 ret = snprintf(tmpbuf, PAGE_SIZE - 1,
David Howells664cceb2005-09-28 17:03:15 +0100526 "%s;%d;%d;%08x;%s",
David Howells94fd8402010-06-28 14:05:04 +0100527 key->type->name,
528 key->uid,
529 key->gid,
530 key->perm,
531 key->description ?: "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
533 /* include a NUL char at the end of the data */
534 if (ret > PAGE_SIZE - 1)
535 ret = PAGE_SIZE - 1;
536 tmpbuf[ret] = 0;
537 ret++;
538
539 /* consider returning the data */
540 if (buffer && buflen > 0) {
541 if (buflen > ret)
542 buflen = ret;
543
544 if (copy_to_user(buffer, tmpbuf, buflen) != 0)
545 ret = -EFAULT;
546 }
547
548 kfree(tmpbuf);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700549error2:
David Howells664cceb2005-09-28 17:03:15 +0100550 key_ref_put(key_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700551error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000553}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555/*
David Howells973c9f42011-01-20 16:38:33 +0000556 * Search the specified keyring and any keyrings it links to for a matching
557 * key. Only keyrings that grant the caller Search permission will be searched
558 * (this includes the starting keyring). Only keys with Search permission can
559 * be found.
560 *
561 * If successful, the found key will be linked to the destination keyring if
562 * supplied and the key has Link permission, and the found key ID will be
563 * returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 */
565long keyctl_keyring_search(key_serial_t ringid,
566 const char __user *_type,
567 const char __user *_description,
568 key_serial_t destringid)
569{
570 struct key_type *ktype;
David Howells664cceb2005-09-28 17:03:15 +0100571 key_ref_t keyring_ref, key_ref, dest_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 char type[32], *description;
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800573 long ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
575 /* pull the type and description into kernel space */
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800576 ret = key_get_type_from_user(type, _type, sizeof(type));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 if (ret < 0)
578 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800580 description = strndup_user(_description, PAGE_SIZE);
581 if (IS_ERR(description)) {
582 ret = PTR_ERR(description);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 goto error;
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
586 /* get the keyring at which to begin the search */
David Howells55931222009-09-02 09:13:45 +0100587 keyring_ref = lookup_user_key(ringid, 0, KEY_SEARCH);
David Howells664cceb2005-09-28 17:03:15 +0100588 if (IS_ERR(keyring_ref)) {
589 ret = PTR_ERR(keyring_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 goto error2;
591 }
592
593 /* get the destination keyring if specified */
David Howells664cceb2005-09-28 17:03:15 +0100594 dest_ref = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 if (destringid) {
David Howells55931222009-09-02 09:13:45 +0100596 dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE,
597 KEY_WRITE);
David Howells664cceb2005-09-28 17:03:15 +0100598 if (IS_ERR(dest_ref)) {
599 ret = PTR_ERR(dest_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 goto error3;
601 }
602 }
603
604 /* find the key type */
605 ktype = key_type_lookup(type);
606 if (IS_ERR(ktype)) {
607 ret = PTR_ERR(ktype);
608 goto error4;
609 }
610
611 /* do the search */
David Howells664cceb2005-09-28 17:03:15 +0100612 key_ref = keyring_search(keyring_ref, ktype, description);
613 if (IS_ERR(key_ref)) {
614 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616 /* treat lack or presence of a negative key the same */
617 if (ret == -EAGAIN)
618 ret = -ENOKEY;
619 goto error5;
620 }
621
622 /* link the resulting key to the destination keyring if we can */
David Howells664cceb2005-09-28 17:03:15 +0100623 if (dest_ref) {
David Howells29db9192005-10-30 15:02:44 -0800624 ret = key_permission(key_ref, KEY_LINK);
625 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 goto error6;
627
David Howells664cceb2005-09-28 17:03:15 +0100628 ret = key_link(key_ref_to_ptr(dest_ref), key_ref_to_ptr(key_ref));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 if (ret < 0)
630 goto error6;
631 }
632
David Howells664cceb2005-09-28 17:03:15 +0100633 ret = key_ref_to_ptr(key_ref)->serial;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700635error6:
David Howells664cceb2005-09-28 17:03:15 +0100636 key_ref_put(key_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700637error5:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 key_type_put(ktype);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700639error4:
David Howells664cceb2005-09-28 17:03:15 +0100640 key_ref_put(dest_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700641error3:
David Howells664cceb2005-09-28 17:03:15 +0100642 key_ref_put(keyring_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700643error2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 kfree(description);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700645error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000647}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649/*
David Howells973c9f42011-01-20 16:38:33 +0000650 * Read a key's payload.
651 *
652 * The key must either grant the caller Read permission, or it must grant the
653 * caller Search permission when searched for from the process keyrings.
654 *
655 * If successful, we place up to buflen bytes of data into the buffer, if one
656 * is provided, and return the amount of data that is available in the key,
657 * irrespective of how much we copied into the buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 */
659long keyctl_read_key(key_serial_t keyid, char __user *buffer, size_t buflen)
660{
David Howells664cceb2005-09-28 17:03:15 +0100661 struct key *key;
662 key_ref_t key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 long ret;
664
665 /* find the key first */
David Howells55931222009-09-02 09:13:45 +0100666 key_ref = lookup_user_key(keyid, 0, 0);
David Howells664cceb2005-09-28 17:03:15 +0100667 if (IS_ERR(key_ref)) {
668 ret = -ENOKEY;
669 goto error;
670 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
David Howells664cceb2005-09-28 17:03:15 +0100672 key = key_ref_to_ptr(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
David Howells664cceb2005-09-28 17:03:15 +0100674 /* see if we can read it directly */
David Howells29db9192005-10-30 15:02:44 -0800675 ret = key_permission(key_ref, KEY_READ);
676 if (ret == 0)
David Howells664cceb2005-09-28 17:03:15 +0100677 goto can_read_key;
David Howells29db9192005-10-30 15:02:44 -0800678 if (ret != -EACCES)
679 goto error;
David Howells664cceb2005-09-28 17:03:15 +0100680
681 /* we can't; see if it's searchable from this process's keyrings
682 * - we automatically take account of the fact that it may be
683 * dangling off an instantiation key
684 */
685 if (!is_key_possessed(key_ref)) {
686 ret = -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 goto error2;
688 }
689
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 /* the key is probably readable - now try to read it */
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700691can_read_key:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 ret = key_validate(key);
693 if (ret == 0) {
694 ret = -EOPNOTSUPP;
695 if (key->type->read) {
696 /* read the data with the semaphore held (since we
697 * might sleep) */
698 down_read(&key->sem);
699 ret = key->type->read(key, buffer, buflen);
700 up_read(&key->sem);
701 }
702 }
703
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700704error2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 key_put(key);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700706error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000708}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710/*
David Howells973c9f42011-01-20 16:38:33 +0000711 * Change the ownership of a key
712 *
713 * The key must grant the caller Setattr permission for this to work, though
714 * the key need not be fully instantiated yet. For the UID to be changed, or
715 * for the GID to be changed to a group the caller is not a member of, the
716 * caller must have sysadmin capability. If either uid or gid is -1 then that
717 * attribute is not changed.
718 *
719 * If the UID is to be changed, the new user must have sufficient quota to
720 * accept the key. The quota deduction will be removed from the old user to
721 * the new user should the attribute be changed.
722 *
723 * If successful, 0 will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 */
725long keyctl_chown_key(key_serial_t id, uid_t uid, gid_t gid)
726{
Fredrik Tolf58016492006-06-26 00:24:51 -0700727 struct key_user *newowner, *zapowner = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 struct key *key;
David Howells664cceb2005-09-28 17:03:15 +0100729 key_ref_t key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 long ret;
731
732 ret = 0;
733 if (uid == (uid_t) -1 && gid == (gid_t) -1)
734 goto error;
735
David Howells55931222009-09-02 09:13:45 +0100736 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
737 KEY_SETATTR);
David Howells664cceb2005-09-28 17:03:15 +0100738 if (IS_ERR(key_ref)) {
739 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 goto error;
741 }
742
David Howells664cceb2005-09-28 17:03:15 +0100743 key = key_ref_to_ptr(key_ref);
744
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 /* make the changes with the locks held to prevent chown/chown races */
746 ret = -EACCES;
747 down_write(&key->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
749 if (!capable(CAP_SYS_ADMIN)) {
750 /* only the sysadmin can chown a key to some other UID */
751 if (uid != (uid_t) -1 && key->uid != uid)
Fredrik Tolf58016492006-06-26 00:24:51 -0700752 goto error_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
754 /* only the sysadmin can set the key's GID to a group other
755 * than one of those that the current process subscribes to */
756 if (gid != (gid_t) -1 && gid != key->gid && !in_group_p(gid))
Fredrik Tolf58016492006-06-26 00:24:51 -0700757 goto error_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 }
759
Fredrik Tolf58016492006-06-26 00:24:51 -0700760 /* change the UID */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 if (uid != (uid_t) -1 && uid != key->uid) {
Fredrik Tolf58016492006-06-26 00:24:51 -0700762 ret = -ENOMEM;
Serge E. Hallyn1d1e9752009-02-26 18:27:38 -0600763 newowner = key_user_lookup(uid, current_user_ns());
Fredrik Tolf58016492006-06-26 00:24:51 -0700764 if (!newowner)
765 goto error_put;
766
767 /* transfer the quota burden to the new user */
768 if (test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) {
David Howells0b77f5b2008-04-29 01:01:32 -0700769 unsigned maxkeys = (uid == 0) ?
770 key_quota_root_maxkeys : key_quota_maxkeys;
771 unsigned maxbytes = (uid == 0) ?
772 key_quota_root_maxbytes : key_quota_maxbytes;
773
Fredrik Tolf58016492006-06-26 00:24:51 -0700774 spin_lock(&newowner->lock);
David Howells0b77f5b2008-04-29 01:01:32 -0700775 if (newowner->qnkeys + 1 >= maxkeys ||
776 newowner->qnbytes + key->quotalen >= maxbytes ||
777 newowner->qnbytes + key->quotalen <
778 newowner->qnbytes)
Fredrik Tolf58016492006-06-26 00:24:51 -0700779 goto quota_overrun;
780
781 newowner->qnkeys++;
782 newowner->qnbytes += key->quotalen;
783 spin_unlock(&newowner->lock);
784
785 spin_lock(&key->user->lock);
786 key->user->qnkeys--;
787 key->user->qnbytes -= key->quotalen;
788 spin_unlock(&key->user->lock);
789 }
790
791 atomic_dec(&key->user->nkeys);
792 atomic_inc(&newowner->nkeys);
793
794 if (test_bit(KEY_FLAG_INSTANTIATED, &key->flags)) {
795 atomic_dec(&key->user->nikeys);
796 atomic_inc(&newowner->nikeys);
797 }
798
799 zapowner = key->user;
800 key->user = newowner;
801 key->uid = uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 }
803
804 /* change the GID */
805 if (gid != (gid_t) -1)
806 key->gid = gid;
807
808 ret = 0;
809
Fredrik Tolf58016492006-06-26 00:24:51 -0700810error_put:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 up_write(&key->sem);
812 key_put(key);
Fredrik Tolf58016492006-06-26 00:24:51 -0700813 if (zapowner)
814 key_user_put(zapowner);
815error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 return ret;
817
Fredrik Tolf58016492006-06-26 00:24:51 -0700818quota_overrun:
819 spin_unlock(&newowner->lock);
820 zapowner = newowner;
821 ret = -EDQUOT;
822 goto error_put;
David Howellsa8b17ed2011-01-20 16:38:27 +0000823}
Fredrik Tolf58016492006-06-26 00:24:51 -0700824
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825/*
David Howells973c9f42011-01-20 16:38:33 +0000826 * Change the permission mask on a key.
827 *
828 * The key must grant the caller Setattr permission for this to work, though
829 * the key need not be fully instantiated yet. If the caller does not have
830 * sysadmin capability, it may only change the permission on keys that it owns.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 */
832long keyctl_setperm_key(key_serial_t id, key_perm_t perm)
833{
834 struct key *key;
David Howells664cceb2005-09-28 17:03:15 +0100835 key_ref_t key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 long ret;
837
838 ret = -EINVAL;
David Howells664cceb2005-09-28 17:03:15 +0100839 if (perm & ~(KEY_POS_ALL | KEY_USR_ALL | KEY_GRP_ALL | KEY_OTH_ALL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 goto error;
841
David Howells55931222009-09-02 09:13:45 +0100842 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
843 KEY_SETATTR);
David Howells664cceb2005-09-28 17:03:15 +0100844 if (IS_ERR(key_ref)) {
845 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 goto error;
847 }
848
David Howells664cceb2005-09-28 17:03:15 +0100849 key = key_ref_to_ptr(key_ref);
850
David Howells76d8aea2005-06-23 22:00:49 -0700851 /* make the changes with the locks held to prevent chown/chmod races */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 ret = -EACCES;
853 down_write(&key->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
David Howells76d8aea2005-06-23 22:00:49 -0700855 /* if we're not the sysadmin, we can only change a key that we own */
David Howells47d804b2008-11-14 10:39:11 +1100856 if (capable(CAP_SYS_ADMIN) || key->uid == current_fsuid()) {
David Howells76d8aea2005-06-23 22:00:49 -0700857 key->perm = perm;
858 ret = 0;
859 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 up_write(&key->sem);
862 key_put(key);
David Howells76d8aea2005-06-23 22:00:49 -0700863error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000865}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
David Howells8bbf49762008-11-14 10:39:14 +1100867/*
David Howells973c9f42011-01-20 16:38:33 +0000868 * Get the destination keyring for instantiation and check that the caller has
869 * Write permission on it.
David Howells8bbf49762008-11-14 10:39:14 +1100870 */
871static long get_instantiation_keyring(key_serial_t ringid,
872 struct request_key_auth *rka,
873 struct key **_dest_keyring)
874{
875 key_ref_t dkref;
876
David Howellseca1bf52008-12-29 00:41:51 +0000877 *_dest_keyring = NULL;
878
David Howells8bbf49762008-11-14 10:39:14 +1100879 /* just return a NULL pointer if we weren't asked to make a link */
David Howellseca1bf52008-12-29 00:41:51 +0000880 if (ringid == 0)
David Howells8bbf49762008-11-14 10:39:14 +1100881 return 0;
David Howells8bbf49762008-11-14 10:39:14 +1100882
883 /* if a specific keyring is nominated by ID, then use that */
884 if (ringid > 0) {
David Howells55931222009-09-02 09:13:45 +0100885 dkref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_WRITE);
David Howells8bbf49762008-11-14 10:39:14 +1100886 if (IS_ERR(dkref))
887 return PTR_ERR(dkref);
888 *_dest_keyring = key_ref_to_ptr(dkref);
889 return 0;
890 }
891
892 if (ringid == KEY_SPEC_REQKEY_AUTH_KEY)
893 return -EINVAL;
894
895 /* otherwise specify the destination keyring recorded in the
896 * authorisation key (any KEY_SPEC_*_KEYRING) */
897 if (ringid >= KEY_SPEC_REQUESTOR_KEYRING) {
David Howells21279cf2009-10-15 10:14:35 +0100898 *_dest_keyring = key_get(rka->dest_keyring);
David Howells8bbf49762008-11-14 10:39:14 +1100899 return 0;
900 }
901
902 return -ENOKEY;
903}
904
David Howellsd84f4f92008-11-14 10:39:23 +1100905/*
David Howells973c9f42011-01-20 16:38:33 +0000906 * Change the request_key authorisation key on the current process.
David Howellsd84f4f92008-11-14 10:39:23 +1100907 */
908static int keyctl_change_reqkey_auth(struct key *key)
909{
910 struct cred *new;
911
912 new = prepare_creds();
913 if (!new)
914 return -ENOMEM;
915
916 key_put(new->request_key_auth);
917 new->request_key_auth = key_get(key);
918
919 return commit_creds(new);
920}
921
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922/*
David Howellsee009e4a02011-03-07 15:06:20 +0000923 * Copy the iovec data from userspace
924 */
925static long copy_from_user_iovec(void *buffer, const struct iovec *iov,
926 unsigned ioc)
927{
928 for (; ioc > 0; ioc--) {
929 if (copy_from_user(buffer, iov->iov_base, iov->iov_len) != 0)
930 return -EFAULT;
931 buffer += iov->iov_len;
932 iov++;
933 }
934 return 0;
935}
936
937/*
David Howells973c9f42011-01-20 16:38:33 +0000938 * Instantiate a key with the specified payload and link the key into the
939 * destination keyring if one is given.
940 *
941 * The caller must have the appropriate instantiation permit set for this to
942 * work (see keyctl_assume_authority). No other permissions are required.
943 *
944 * If successful, 0 will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 */
David Howellsee009e4a02011-03-07 15:06:20 +0000946long keyctl_instantiate_key_common(key_serial_t id,
947 const struct iovec *payload_iov,
948 unsigned ioc,
949 size_t plen,
950 key_serial_t ringid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951{
David Howellsd84f4f92008-11-14 10:39:23 +1100952 const struct cred *cred = current_cred();
David Howells3e301482005-06-23 22:00:56 -0700953 struct request_key_auth *rka;
David Howells8bbf49762008-11-14 10:39:14 +1100954 struct key *instkey, *dest_keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 void *payload;
956 long ret;
David Howells38bbca62008-04-29 01:01:19 -0700957 bool vm = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
David Howellsd84f4f92008-11-14 10:39:23 +1100959 kenter("%d,,%zu,%d", id, plen, ringid);
960
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 ret = -EINVAL;
David Howells38bbca62008-04-29 01:01:19 -0700962 if (plen > 1024 * 1024 - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 goto error;
964
David Howellsb5f545c2006-01-08 01:02:47 -0800965 /* the appropriate instantiation authorisation key must have been
966 * assumed before calling this */
967 ret = -EPERM;
David Howellsd84f4f92008-11-14 10:39:23 +1100968 instkey = cred->request_key_auth;
David Howellsb5f545c2006-01-08 01:02:47 -0800969 if (!instkey)
970 goto error;
971
972 rka = instkey->payload.data;
973 if (rka->target_key->serial != id)
974 goto error;
975
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 /* pull the payload in if one was supplied */
977 payload = NULL;
978
David Howellsee009e4a02011-03-07 15:06:20 +0000979 if (payload_iov) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 ret = -ENOMEM;
981 payload = kmalloc(plen, GFP_KERNEL);
David Howells38bbca62008-04-29 01:01:19 -0700982 if (!payload) {
983 if (plen <= PAGE_SIZE)
984 goto error;
985 vm = true;
986 payload = vmalloc(plen);
987 if (!payload)
988 goto error;
989 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
David Howellsee009e4a02011-03-07 15:06:20 +0000991 ret = copy_from_user_iovec(payload, payload_iov, ioc);
992 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 goto error2;
994 }
995
David Howells3e301482005-06-23 22:00:56 -0700996 /* find the destination keyring amongst those belonging to the
997 * requesting task */
David Howells8bbf49762008-11-14 10:39:14 +1100998 ret = get_instantiation_keyring(ringid, rka, &dest_keyring);
999 if (ret < 0)
1000 goto error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
1002 /* instantiate the key and link it into a keyring */
David Howells3e301482005-06-23 22:00:56 -07001003 ret = key_instantiate_and_link(rka->target_key, payload, plen,
David Howells8bbf49762008-11-14 10:39:14 +11001004 dest_keyring, instkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
David Howells8bbf49762008-11-14 10:39:14 +11001006 key_put(dest_keyring);
David Howellsb5f545c2006-01-08 01:02:47 -08001007
1008 /* discard the assumed authority if it's just been disabled by
1009 * instantiation of the key */
David Howellsd84f4f92008-11-14 10:39:23 +11001010 if (ret == 0)
1011 keyctl_change_reqkey_auth(NULL);
David Howellsb5f545c2006-01-08 01:02:47 -08001012
1013error2:
David Howells38bbca62008-04-29 01:01:19 -07001014 if (!vm)
1015 kfree(payload);
1016 else
1017 vfree(payload);
David Howellsb5f545c2006-01-08 01:02:47 -08001018error:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +00001020}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022/*
David Howellsee009e4a02011-03-07 15:06:20 +00001023 * Instantiate a key with the specified payload and link the key into the
1024 * destination keyring if one is given.
1025 *
1026 * The caller must have the appropriate instantiation permit set for this to
1027 * work (see keyctl_assume_authority). No other permissions are required.
1028 *
1029 * If successful, 0 will be returned.
1030 */
1031long keyctl_instantiate_key(key_serial_t id,
1032 const void __user *_payload,
1033 size_t plen,
1034 key_serial_t ringid)
1035{
1036 if (_payload && plen) {
1037 struct iovec iov[1] = {
1038 [0].iov_base = (void __user *)_payload,
1039 [0].iov_len = plen
1040 };
1041
1042 return keyctl_instantiate_key_common(id, iov, 1, plen, ringid);
1043 }
1044
1045 return keyctl_instantiate_key_common(id, NULL, 0, 0, ringid);
1046}
1047
1048/*
1049 * Instantiate a key with the specified multipart payload and link the key into
1050 * the destination keyring if one is given.
1051 *
1052 * The caller must have the appropriate instantiation permit set for this to
1053 * work (see keyctl_assume_authority). No other permissions are required.
1054 *
1055 * If successful, 0 will be returned.
1056 */
1057long keyctl_instantiate_key_iov(key_serial_t id,
1058 const struct iovec __user *_payload_iov,
1059 unsigned ioc,
1060 key_serial_t ringid)
1061{
1062 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
1063 long ret;
1064
1065 if (_payload_iov == 0 || ioc == 0)
1066 goto no_payload;
1067
1068 ret = rw_copy_check_uvector(WRITE, _payload_iov, ioc,
Christopher Yeohfcf63402011-10-31 17:06:39 -07001069 ARRAY_SIZE(iovstack), iovstack, &iov, 1);
David Howellsee009e4a02011-03-07 15:06:20 +00001070 if (ret < 0)
1071 return ret;
1072 if (ret == 0)
1073 goto no_payload_free;
1074
1075 ret = keyctl_instantiate_key_common(id, iov, ioc, ret, ringid);
1076
1077 if (iov != iovstack)
1078 kfree(iov);
1079 return ret;
1080
1081no_payload_free:
1082 if (iov != iovstack)
1083 kfree(iov);
1084no_payload:
1085 return keyctl_instantiate_key_common(id, NULL, 0, 0, ringid);
1086}
1087
1088/*
David Howells973c9f42011-01-20 16:38:33 +00001089 * Negatively instantiate the key with the given timeout (in seconds) and link
1090 * the key into the destination keyring if one is given.
1091 *
1092 * The caller must have the appropriate instantiation permit set for this to
1093 * work (see keyctl_assume_authority). No other permissions are required.
1094 *
1095 * The key and any links to the key will be automatically garbage collected
1096 * after the timeout expires.
1097 *
1098 * Negative keys are used to rate limit repeated request_key() calls by causing
1099 * them to return -ENOKEY until the negative key expires.
1100 *
1101 * If successful, 0 will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 */
1103long keyctl_negate_key(key_serial_t id, unsigned timeout, key_serial_t ringid)
1104{
David Howellsfdd1b942011-03-07 15:06:09 +00001105 return keyctl_reject_key(id, timeout, ENOKEY, ringid);
1106}
1107
1108/*
1109 * Negatively instantiate the key with the given timeout (in seconds) and error
1110 * code and link the key into the destination keyring if one is given.
1111 *
1112 * The caller must have the appropriate instantiation permit set for this to
1113 * work (see keyctl_assume_authority). No other permissions are required.
1114 *
1115 * The key and any links to the key will be automatically garbage collected
1116 * after the timeout expires.
1117 *
1118 * Negative keys are used to rate limit repeated request_key() calls by causing
1119 * them to return the specified error code until the negative key expires.
1120 *
1121 * If successful, 0 will be returned.
1122 */
1123long keyctl_reject_key(key_serial_t id, unsigned timeout, unsigned error,
1124 key_serial_t ringid)
1125{
David Howellsd84f4f92008-11-14 10:39:23 +11001126 const struct cred *cred = current_cred();
David Howells3e301482005-06-23 22:00:56 -07001127 struct request_key_auth *rka;
David Howells8bbf49762008-11-14 10:39:14 +11001128 struct key *instkey, *dest_keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 long ret;
1130
David Howellsfdd1b942011-03-07 15:06:09 +00001131 kenter("%d,%u,%u,%d", id, timeout, error, ringid);
1132
1133 /* must be a valid error code and mustn't be a kernel special */
1134 if (error <= 0 ||
1135 error >= MAX_ERRNO ||
1136 error == ERESTARTSYS ||
1137 error == ERESTARTNOINTR ||
1138 error == ERESTARTNOHAND ||
1139 error == ERESTART_RESTARTBLOCK)
1140 return -EINVAL;
David Howellsd84f4f92008-11-14 10:39:23 +11001141
David Howellsb5f545c2006-01-08 01:02:47 -08001142 /* the appropriate instantiation authorisation key must have been
1143 * assumed before calling this */
1144 ret = -EPERM;
David Howellsd84f4f92008-11-14 10:39:23 +11001145 instkey = cred->request_key_auth;
David Howellsb5f545c2006-01-08 01:02:47 -08001146 if (!instkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
David Howells3e301482005-06-23 22:00:56 -07001149 rka = instkey->payload.data;
David Howellsb5f545c2006-01-08 01:02:47 -08001150 if (rka->target_key->serial != id)
1151 goto error;
David Howells3e301482005-06-23 22:00:56 -07001152
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 /* find the destination keyring if present (which must also be
1154 * writable) */
David Howells8bbf49762008-11-14 10:39:14 +11001155 ret = get_instantiation_keyring(ringid, rka, &dest_keyring);
1156 if (ret < 0)
1157 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
1159 /* instantiate the key and link it into a keyring */
David Howellsfdd1b942011-03-07 15:06:09 +00001160 ret = key_reject_and_link(rka->target_key, timeout, error,
David Howells8bbf49762008-11-14 10:39:14 +11001161 dest_keyring, instkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
David Howells8bbf49762008-11-14 10:39:14 +11001163 key_put(dest_keyring);
David Howellsb5f545c2006-01-08 01:02:47 -08001164
1165 /* discard the assumed authority if it's just been disabled by
1166 * instantiation of the key */
David Howellsd84f4f92008-11-14 10:39:23 +11001167 if (ret == 0)
1168 keyctl_change_reqkey_auth(NULL);
David Howellsb5f545c2006-01-08 01:02:47 -08001169
1170error:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +00001172}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174/*
David Howells973c9f42011-01-20 16:38:33 +00001175 * Read or set the default keyring in which request_key() will cache keys and
1176 * return the old setting.
1177 *
1178 * If a process keyring is specified then this will be created if it doesn't
1179 * yet exist. The old setting will be returned if successful.
David Howells3e301482005-06-23 22:00:56 -07001180 */
1181long keyctl_set_reqkey_keyring(int reqkey_defl)
1182{
David Howellsd84f4f92008-11-14 10:39:23 +11001183 struct cred *new;
1184 int ret, old_setting;
1185
1186 old_setting = current_cred_xxx(jit_keyring);
1187
1188 if (reqkey_defl == KEY_REQKEY_DEFL_NO_CHANGE)
1189 return old_setting;
1190
1191 new = prepare_creds();
1192 if (!new)
1193 return -ENOMEM;
David Howells3e301482005-06-23 22:00:56 -07001194
1195 switch (reqkey_defl) {
1196 case KEY_REQKEY_DEFL_THREAD_KEYRING:
David Howellsd84f4f92008-11-14 10:39:23 +11001197 ret = install_thread_keyring_to_cred(new);
David Howells3e301482005-06-23 22:00:56 -07001198 if (ret < 0)
David Howellsd84f4f92008-11-14 10:39:23 +11001199 goto error;
David Howells3e301482005-06-23 22:00:56 -07001200 goto set;
1201
1202 case KEY_REQKEY_DEFL_PROCESS_KEYRING:
David Howellsd84f4f92008-11-14 10:39:23 +11001203 ret = install_process_keyring_to_cred(new);
1204 if (ret < 0) {
1205 if (ret != -EEXIST)
1206 goto error;
1207 ret = 0;
1208 }
1209 goto set;
David Howells3e301482005-06-23 22:00:56 -07001210
1211 case KEY_REQKEY_DEFL_DEFAULT:
1212 case KEY_REQKEY_DEFL_SESSION_KEYRING:
1213 case KEY_REQKEY_DEFL_USER_KEYRING:
1214 case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
David Howellsd84f4f92008-11-14 10:39:23 +11001215 case KEY_REQKEY_DEFL_REQUESTOR_KEYRING:
1216 goto set;
David Howells3e301482005-06-23 22:00:56 -07001217
1218 case KEY_REQKEY_DEFL_NO_CHANGE:
David Howells3e301482005-06-23 22:00:56 -07001219 case KEY_REQKEY_DEFL_GROUP_KEYRING:
1220 default:
David Howellsd84f4f92008-11-14 10:39:23 +11001221 ret = -EINVAL;
1222 goto error;
David Howells3e301482005-06-23 22:00:56 -07001223 }
1224
David Howellsd84f4f92008-11-14 10:39:23 +11001225set:
1226 new->jit_keyring = reqkey_defl;
1227 commit_creds(new);
1228 return old_setting;
1229error:
1230 abort_creds(new);
Dan Carpenter4303ef12010-06-11 17:30:05 +01001231 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +00001232}
David Howellsd84f4f92008-11-14 10:39:23 +11001233
David Howells3e301482005-06-23 22:00:56 -07001234/*
David Howells973c9f42011-01-20 16:38:33 +00001235 * Set or clear the timeout on a key.
1236 *
1237 * Either the key must grant the caller Setattr permission or else the caller
1238 * must hold an instantiation authorisation token for the key.
1239 *
1240 * The timeout is either 0 to clear the timeout, or a number of seconds from
1241 * the current time. The key and any links to the key will be automatically
1242 * garbage collected after the timeout expires.
1243 *
1244 * If successful, 0 is returned.
David Howells017679c2006-01-08 01:02:43 -08001245 */
1246long keyctl_set_timeout(key_serial_t id, unsigned timeout)
1247{
David Howells91562352010-06-11 17:31:05 +01001248 struct key *key, *instkey;
David Howells017679c2006-01-08 01:02:43 -08001249 key_ref_t key_ref;
David Howells017679c2006-01-08 01:02:43 -08001250 long ret;
1251
David Howells55931222009-09-02 09:13:45 +01001252 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
1253 KEY_SETATTR);
David Howells017679c2006-01-08 01:02:43 -08001254 if (IS_ERR(key_ref)) {
David Howells91562352010-06-11 17:31:05 +01001255 /* setting the timeout on a key under construction is permitted
1256 * if we have the authorisation token handy */
1257 if (PTR_ERR(key_ref) == -EACCES) {
1258 instkey = key_get_instantiation_authkey(id);
1259 if (!IS_ERR(instkey)) {
1260 key_put(instkey);
1261 key_ref = lookup_user_key(id,
1262 KEY_LOOKUP_PARTIAL,
1263 0);
1264 if (!IS_ERR(key_ref))
1265 goto okay;
1266 }
1267 }
1268
David Howells017679c2006-01-08 01:02:43 -08001269 ret = PTR_ERR(key_ref);
1270 goto error;
1271 }
1272
David Howells91562352010-06-11 17:31:05 +01001273okay:
David Howells017679c2006-01-08 01:02:43 -08001274 key = key_ref_to_ptr(key_ref);
Bryan Schumaker59e6b9c2012-02-24 14:14:50 -05001275 key_set_timeout(key, timeout);
David Howells017679c2006-01-08 01:02:43 -08001276 key_put(key);
1277
1278 ret = 0;
1279error:
1280 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +00001281}
David Howells017679c2006-01-08 01:02:43 -08001282
David Howells017679c2006-01-08 01:02:43 -08001283/*
David Howells973c9f42011-01-20 16:38:33 +00001284 * Assume (or clear) the authority to instantiate the specified key.
1285 *
1286 * This sets the authoritative token currently in force for key instantiation.
1287 * This must be done for a key to be instantiated. It has the effect of making
1288 * available all the keys from the caller of the request_key() that created a
1289 * key to request_key() calls made by the caller of this function.
1290 *
1291 * The caller must have the instantiation key in their process keyrings with a
1292 * Search permission grant available to the caller.
1293 *
1294 * If the ID given is 0, then the setting will be cleared and 0 returned.
1295 *
1296 * If the ID given has a matching an authorisation key, then that key will be
1297 * set and its ID will be returned. The authorisation key can be read to get
1298 * the callout information passed to request_key().
David Howellsb5f545c2006-01-08 01:02:47 -08001299 */
1300long keyctl_assume_authority(key_serial_t id)
1301{
1302 struct key *authkey;
1303 long ret;
1304
1305 /* special key IDs aren't permitted */
1306 ret = -EINVAL;
1307 if (id < 0)
1308 goto error;
1309
1310 /* we divest ourselves of authority if given an ID of 0 */
1311 if (id == 0) {
David Howellsd84f4f92008-11-14 10:39:23 +11001312 ret = keyctl_change_reqkey_auth(NULL);
David Howellsb5f545c2006-01-08 01:02:47 -08001313 goto error;
1314 }
1315
1316 /* attempt to assume the authority temporarily granted to us whilst we
1317 * instantiate the specified key
1318 * - the authorisation key must be in the current task's keyrings
1319 * somewhere
1320 */
1321 authkey = key_get_instantiation_authkey(id);
1322 if (IS_ERR(authkey)) {
1323 ret = PTR_ERR(authkey);
1324 goto error;
1325 }
1326
David Howellsd84f4f92008-11-14 10:39:23 +11001327 ret = keyctl_change_reqkey_auth(authkey);
1328 if (ret < 0)
1329 goto error;
1330 key_put(authkey);
David Howellsb5f545c2006-01-08 01:02:47 -08001331
David Howellsd84f4f92008-11-14 10:39:23 +11001332 ret = authkey->serial;
David Howellsb5f545c2006-01-08 01:02:47 -08001333error:
1334 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +00001335}
David Howellsb5f545c2006-01-08 01:02:47 -08001336
David Howells70a5bb72008-04-29 01:01:26 -07001337/*
David Howells973c9f42011-01-20 16:38:33 +00001338 * Get a key's the LSM security label.
1339 *
1340 * The key must grant the caller View permission for this to work.
1341 *
1342 * If there's a buffer, then up to buflen bytes of data will be placed into it.
1343 *
1344 * If successful, the amount of information available will be returned,
1345 * irrespective of how much was copied (including the terminal NUL).
David Howells70a5bb72008-04-29 01:01:26 -07001346 */
1347long keyctl_get_security(key_serial_t keyid,
1348 char __user *buffer,
1349 size_t buflen)
1350{
1351 struct key *key, *instkey;
1352 key_ref_t key_ref;
1353 char *context;
1354 long ret;
1355
David Howells55931222009-09-02 09:13:45 +01001356 key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_VIEW);
David Howells70a5bb72008-04-29 01:01:26 -07001357 if (IS_ERR(key_ref)) {
1358 if (PTR_ERR(key_ref) != -EACCES)
1359 return PTR_ERR(key_ref);
1360
1361 /* viewing a key under construction is also permitted if we
1362 * have the authorisation token handy */
1363 instkey = key_get_instantiation_authkey(keyid);
1364 if (IS_ERR(instkey))
Roel Kluinfa1cc7b2009-12-15 15:05:12 -08001365 return PTR_ERR(instkey);
David Howells70a5bb72008-04-29 01:01:26 -07001366 key_put(instkey);
1367
David Howells55931222009-09-02 09:13:45 +01001368 key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, 0);
David Howells70a5bb72008-04-29 01:01:26 -07001369 if (IS_ERR(key_ref))
1370 return PTR_ERR(key_ref);
1371 }
1372
1373 key = key_ref_to_ptr(key_ref);
1374 ret = security_key_getsecurity(key, &context);
1375 if (ret == 0) {
1376 /* if no information was returned, give userspace an empty
1377 * string */
1378 ret = 1;
1379 if (buffer && buflen > 0 &&
1380 copy_to_user(buffer, "", 1) != 0)
1381 ret = -EFAULT;
1382 } else if (ret > 0) {
1383 /* return as much data as there's room for */
1384 if (buffer && buflen > 0) {
1385 if (buflen > ret)
1386 buflen = ret;
1387
1388 if (copy_to_user(buffer, context, buflen) != 0)
1389 ret = -EFAULT;
1390 }
1391
1392 kfree(context);
1393 }
1394
1395 key_ref_put(key_ref);
1396 return ret;
1397}
1398
David Howellsee18d642009-09-02 09:14:21 +01001399/*
David Howells973c9f42011-01-20 16:38:33 +00001400 * Attempt to install the calling process's session keyring on the process's
1401 * parent process.
1402 *
1403 * The keyring must exist and must grant the caller LINK permission, and the
1404 * parent process must be single-threaded and must have the same effective
1405 * ownership as this process and mustn't be SUID/SGID.
1406 *
1407 * The keyring will be emplaced on the parent when it next resumes userspace.
1408 *
1409 * If successful, 0 will be returned.
David Howellsee18d642009-09-02 09:14:21 +01001410 */
1411long keyctl_session_to_parent(void)
1412{
Geert Uytterhoevena00ae4d2009-12-13 20:21:34 +01001413#ifdef TIF_NOTIFY_RESUME
David Howellsee18d642009-09-02 09:14:21 +01001414 struct task_struct *me, *parent;
1415 const struct cred *mycred, *pcred;
1416 struct cred *cred, *oldcred;
1417 key_ref_t keyring_r;
1418 int ret;
1419
1420 keyring_r = lookup_user_key(KEY_SPEC_SESSION_KEYRING, 0, KEY_LINK);
1421 if (IS_ERR(keyring_r))
1422 return PTR_ERR(keyring_r);
1423
1424 /* our parent is going to need a new cred struct, a new tgcred struct
1425 * and new security data, so we allocate them here to prevent ENOMEM in
1426 * our parent */
1427 ret = -ENOMEM;
1428 cred = cred_alloc_blank();
1429 if (!cred)
1430 goto error_keyring;
1431
1432 cred->tgcred->session_keyring = key_ref_to_ptr(keyring_r);
1433 keyring_r = NULL;
1434
1435 me = current;
David Howells9d1ac652010-09-10 09:59:46 +01001436 rcu_read_lock();
David Howellsee18d642009-09-02 09:14:21 +01001437 write_lock_irq(&tasklist_lock);
1438
1439 parent = me->real_parent;
1440 ret = -EPERM;
1441
1442 /* the parent mustn't be init and mustn't be a kernel thread */
1443 if (parent->pid <= 1 || !parent->mm)
1444 goto not_permitted;
1445
1446 /* the parent must be single threaded */
Oleg Nesterovdd98acf2010-05-26 14:43:23 -07001447 if (!thread_group_empty(parent))
David Howellsee18d642009-09-02 09:14:21 +01001448 goto not_permitted;
1449
1450 /* the parent and the child must have different session keyrings or
1451 * there's no point */
1452 mycred = current_cred();
1453 pcred = __task_cred(parent);
1454 if (mycred == pcred ||
1455 mycred->tgcred->session_keyring == pcred->tgcred->session_keyring)
1456 goto already_same;
1457
1458 /* the parent must have the same effective ownership and mustn't be
1459 * SUID/SGID */
Justin P. Mattockc5b60b52010-04-21 00:02:11 -07001460 if (pcred->uid != mycred->euid ||
David Howellsee18d642009-09-02 09:14:21 +01001461 pcred->euid != mycred->euid ||
1462 pcred->suid != mycred->euid ||
Justin P. Mattockc5b60b52010-04-21 00:02:11 -07001463 pcred->gid != mycred->egid ||
David Howellsee18d642009-09-02 09:14:21 +01001464 pcred->egid != mycred->egid ||
1465 pcred->sgid != mycred->egid)
1466 goto not_permitted;
1467
1468 /* the keyrings must have the same UID */
David Howells3d964062010-09-10 09:59:51 +01001469 if ((pcred->tgcred->session_keyring &&
1470 pcred->tgcred->session_keyring->uid != mycred->euid) ||
David Howellsee18d642009-09-02 09:14:21 +01001471 mycred->tgcred->session_keyring->uid != mycred->euid)
1472 goto not_permitted;
1473
David Howellsee18d642009-09-02 09:14:21 +01001474 /* if there's an already pending keyring replacement, then we replace
1475 * that */
1476 oldcred = parent->replacement_session_keyring;
1477
1478 /* the replacement session keyring is applied just prior to userspace
1479 * restarting */
1480 parent->replacement_session_keyring = cred;
1481 cred = NULL;
1482 set_ti_thread_flag(task_thread_info(parent), TIF_NOTIFY_RESUME);
1483
1484 write_unlock_irq(&tasklist_lock);
David Howells9d1ac652010-09-10 09:59:46 +01001485 rcu_read_unlock();
David Howellsee18d642009-09-02 09:14:21 +01001486 if (oldcred)
1487 put_cred(oldcred);
1488 return 0;
1489
1490already_same:
1491 ret = 0;
1492not_permitted:
Marc Dionne5c843422009-09-14 12:46:23 +01001493 write_unlock_irq(&tasklist_lock);
David Howells9d1ac652010-09-10 09:59:46 +01001494 rcu_read_unlock();
David Howellsee18d642009-09-02 09:14:21 +01001495 put_cred(cred);
1496 return ret;
1497
1498error_keyring:
1499 key_ref_put(keyring_r);
1500 return ret;
Geert Uytterhoevena00ae4d2009-12-13 20:21:34 +01001501
1502#else /* !TIF_NOTIFY_RESUME */
1503 /*
1504 * To be removed when TIF_NOTIFY_RESUME has been implemented on
1505 * m68k/xtensa
1506 */
1507#warning TIF_NOTIFY_RESUME not implemented
1508 return -EOPNOTSUPP;
1509#endif /* !TIF_NOTIFY_RESUME */
David Howellsee18d642009-09-02 09:14:21 +01001510}
1511
David Howellsb5f545c2006-01-08 01:02:47 -08001512/*
David Howells973c9f42011-01-20 16:38:33 +00001513 * The key control system call
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 */
Heiko Carstens938bb9f2009-01-14 14:14:30 +01001515SYSCALL_DEFINE5(keyctl, int, option, unsigned long, arg2, unsigned long, arg3,
1516 unsigned long, arg4, unsigned long, arg5)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517{
1518 switch (option) {
1519 case KEYCTL_GET_KEYRING_ID:
1520 return keyctl_get_keyring_ID((key_serial_t) arg2,
1521 (int) arg3);
1522
1523 case KEYCTL_JOIN_SESSION_KEYRING:
1524 return keyctl_join_session_keyring((const char __user *) arg2);
1525
1526 case KEYCTL_UPDATE:
1527 return keyctl_update_key((key_serial_t) arg2,
1528 (const void __user *) arg3,
1529 (size_t) arg4);
1530
1531 case KEYCTL_REVOKE:
1532 return keyctl_revoke_key((key_serial_t) arg2);
1533
1534 case KEYCTL_DESCRIBE:
1535 return keyctl_describe_key((key_serial_t) arg2,
1536 (char __user *) arg3,
1537 (unsigned) arg4);
1538
1539 case KEYCTL_CLEAR:
1540 return keyctl_keyring_clear((key_serial_t) arg2);
1541
1542 case KEYCTL_LINK:
1543 return keyctl_keyring_link((key_serial_t) arg2,
1544 (key_serial_t) arg3);
1545
1546 case KEYCTL_UNLINK:
1547 return keyctl_keyring_unlink((key_serial_t) arg2,
1548 (key_serial_t) arg3);
1549
1550 case KEYCTL_SEARCH:
1551 return keyctl_keyring_search((key_serial_t) arg2,
1552 (const char __user *) arg3,
1553 (const char __user *) arg4,
1554 (key_serial_t) arg5);
1555
1556 case KEYCTL_READ:
1557 return keyctl_read_key((key_serial_t) arg2,
1558 (char __user *) arg3,
1559 (size_t) arg4);
1560
1561 case KEYCTL_CHOWN:
1562 return keyctl_chown_key((key_serial_t) arg2,
1563 (uid_t) arg3,
1564 (gid_t) arg4);
1565
1566 case KEYCTL_SETPERM:
1567 return keyctl_setperm_key((key_serial_t) arg2,
1568 (key_perm_t) arg3);
1569
1570 case KEYCTL_INSTANTIATE:
1571 return keyctl_instantiate_key((key_serial_t) arg2,
1572 (const void __user *) arg3,
1573 (size_t) arg4,
1574 (key_serial_t) arg5);
1575
1576 case KEYCTL_NEGATE:
1577 return keyctl_negate_key((key_serial_t) arg2,
1578 (unsigned) arg3,
1579 (key_serial_t) arg4);
1580
David Howells3e301482005-06-23 22:00:56 -07001581 case KEYCTL_SET_REQKEY_KEYRING:
1582 return keyctl_set_reqkey_keyring(arg2);
1583
David Howells017679c2006-01-08 01:02:43 -08001584 case KEYCTL_SET_TIMEOUT:
1585 return keyctl_set_timeout((key_serial_t) arg2,
1586 (unsigned) arg3);
1587
David Howellsb5f545c2006-01-08 01:02:47 -08001588 case KEYCTL_ASSUME_AUTHORITY:
1589 return keyctl_assume_authority((key_serial_t) arg2);
1590
David Howells70a5bb72008-04-29 01:01:26 -07001591 case KEYCTL_GET_SECURITY:
1592 return keyctl_get_security((key_serial_t) arg2,
James Morris90bd49a2008-12-29 14:35:35 +11001593 (char __user *) arg3,
David Howells70a5bb72008-04-29 01:01:26 -07001594 (size_t) arg4);
1595
David Howellsee18d642009-09-02 09:14:21 +01001596 case KEYCTL_SESSION_TO_PARENT:
1597 return keyctl_session_to_parent();
1598
David Howellsfdd1b942011-03-07 15:06:09 +00001599 case KEYCTL_REJECT:
1600 return keyctl_reject_key((key_serial_t) arg2,
1601 (unsigned) arg3,
1602 (unsigned) arg4,
1603 (key_serial_t) arg5);
1604
David Howellsee009e4a02011-03-07 15:06:20 +00001605 case KEYCTL_INSTANTIATE_IOV:
1606 return keyctl_instantiate_key_iov(
1607 (key_serial_t) arg2,
1608 (const struct iovec __user *) arg3,
1609 (unsigned) arg4,
1610 (key_serial_t) arg5);
1611
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 default:
1613 return -EOPNOTSUPP;
1614 }
David Howellsa8b17ed2011-01-20 16:38:27 +00001615}