blob: 6523599e9ac0e08d7911c1fb007cc1971d1334a6 [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>
17#include <linux/keyctl.h>
18#include <linux/fs.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080019#include <linux/capability.h>
Davi Arnaut0cb409d2006-03-24 03:18:43 -080020#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/err.h>
David Howells38bbca62008-04-29 01:01:19 -070022#include <linux/vmalloc.h>
David Howells70a5bb72008-04-29 01:01:26 -070023#include <linux/security.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <asm/uaccess.h>
25#include "internal.h"
26
Davi Arnaut0cb409d2006-03-24 03:18:43 -080027static int key_get_type_from_user(char *type,
28 const char __user *_type,
29 unsigned len)
30{
31 int ret;
32
33 ret = strncpy_from_user(type, _type, len);
Davi Arnaut0cb409d2006-03-24 03:18:43 -080034 if (ret < 0)
Dan Carpenter4303ef12010-06-11 17:30:05 +010035 return ret;
Davi Arnaut0cb409d2006-03-24 03:18:43 -080036 if (ret == 0 || ret >= len)
37 return -EINVAL;
Davi Arnaut0cb409d2006-03-24 03:18:43 -080038 if (type[0] == '.')
39 return -EPERM;
Davi Arnaut0cb409d2006-03-24 03:18:43 -080040 type[len - 1] = '\0';
Davi Arnaut0cb409d2006-03-24 03:18:43 -080041 return 0;
42}
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044/*
David Howells973c9f42011-01-20 16:38:33 +000045 * Extract the description of a new key from userspace and either add it as a
46 * new key to the specified keyring or update a matching key in that keyring.
47 *
48 * The keyring must be writable so that we can attach the key to it.
49 *
50 * If successful, the new key's serial number is returned, otherwise an error
51 * code is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 */
Heiko Carstens1e7bfb22009-01-14 14:14:29 +010053SYSCALL_DEFINE5(add_key, const char __user *, _type,
54 const char __user *, _description,
55 const void __user *, _payload,
56 size_t, plen,
57 key_serial_t, ringid)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
David Howells664cceb2005-09-28 17:03:15 +010059 key_ref_t keyring_ref, key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 char type[32], *description;
61 void *payload;
Davi Arnaut0cb409d2006-03-24 03:18:43 -080062 long ret;
David Howells38bbca62008-04-29 01:01:19 -070063 bool vm;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65 ret = -EINVAL;
David Howells38bbca62008-04-29 01:01:19 -070066 if (plen > 1024 * 1024 - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 goto error;
68
69 /* draw all the data into kernel space */
Davi Arnaut0cb409d2006-03-24 03:18:43 -080070 ret = key_get_type_from_user(type, _type, sizeof(type));
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 if (ret < 0)
72 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Davi Arnaut0cb409d2006-03-24 03:18:43 -080074 description = strndup_user(_description, PAGE_SIZE);
75 if (IS_ERR(description)) {
76 ret = PTR_ERR(description);
David Howells3e301482005-06-23 22:00:56 -070077 goto error;
Davi Arnaut0cb409d2006-03-24 03:18:43 -080078 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80 /* pull the payload in if one was supplied */
81 payload = NULL;
82
David Howells38bbca62008-04-29 01:01:19 -070083 vm = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 if (_payload) {
85 ret = -ENOMEM;
86 payload = kmalloc(plen, GFP_KERNEL);
David Howells38bbca62008-04-29 01:01:19 -070087 if (!payload) {
88 if (plen <= PAGE_SIZE)
89 goto error2;
90 vm = true;
91 payload = vmalloc(plen);
92 if (!payload)
93 goto error2;
94 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96 ret = -EFAULT;
97 if (copy_from_user(payload, _payload, plen) != 0)
98 goto error3;
99 }
100
101 /* find the target keyring (which must be writable) */
David Howells55931222009-09-02 09:13:45 +0100102 keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_WRITE);
David Howells664cceb2005-09-28 17:03:15 +0100103 if (IS_ERR(keyring_ref)) {
104 ret = PTR_ERR(keyring_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 goto error3;
106 }
107
108 /* create or update the requested key and add it to the target
109 * keyring */
David Howells664cceb2005-09-28 17:03:15 +0100110 key_ref = key_create_or_update(keyring_ref, type, description,
Arun Raghavan6b79ccb2008-04-29 01:01:28 -0700111 payload, plen, KEY_PERM_UNDEF,
112 KEY_ALLOC_IN_QUOTA);
David Howells664cceb2005-09-28 17:03:15 +0100113 if (!IS_ERR(key_ref)) {
114 ret = key_ref_to_ptr(key_ref)->serial;
115 key_ref_put(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 }
117 else {
David Howells664cceb2005-09-28 17:03:15 +0100118 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 }
120
David Howells664cceb2005-09-28 17:03:15 +0100121 key_ref_put(keyring_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 error3:
David Howells38bbca62008-04-29 01:01:19 -0700123 if (!vm)
124 kfree(payload);
125 else
126 vfree(payload);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 error2:
128 kfree(description);
129 error:
130 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000131}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133/*
David Howells973c9f42011-01-20 16:38:33 +0000134 * Search the process keyrings and keyring trees linked from those for a
135 * matching key. Keyrings must have appropriate Search permission to be
136 * searched.
137 *
138 * If a key is found, it will be attached to the destination keyring if there's
139 * one specified and the serial number of the key will be returned.
140 *
141 * If no key is found, /sbin/request-key will be invoked if _callout_info is
142 * non-NULL in an attempt to create a key. The _callout_info string will be
143 * passed to /sbin/request-key to aid with completing the request. If the
144 * _callout_info string is "" then it will be changed to "-".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 */
Heiko Carstens1e7bfb22009-01-14 14:14:29 +0100146SYSCALL_DEFINE4(request_key, const char __user *, _type,
147 const char __user *, _description,
148 const char __user *, _callout_info,
149 key_serial_t, destringid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150{
151 struct key_type *ktype;
David Howells664cceb2005-09-28 17:03:15 +0100152 struct key *key;
153 key_ref_t dest_ref;
David Howells4a38e122008-04-29 01:01:24 -0700154 size_t callout_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 char type[32], *description, *callout_info;
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800156 long ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158 /* pull the type into kernel space */
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800159 ret = key_get_type_from_user(type, _type, sizeof(type));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 if (ret < 0)
161 goto error;
David Howells1260f802005-08-04 11:50:01 +0100162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 /* pull the description into kernel space */
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800164 description = strndup_user(_description, PAGE_SIZE);
165 if (IS_ERR(description)) {
166 ret = PTR_ERR(description);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 goto error;
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170 /* pull the callout info into kernel space */
171 callout_info = NULL;
David Howells4a38e122008-04-29 01:01:24 -0700172 callout_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 if (_callout_info) {
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800174 callout_info = strndup_user(_callout_info, PAGE_SIZE);
175 if (IS_ERR(callout_info)) {
176 ret = PTR_ERR(callout_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 goto error2;
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800178 }
David Howells4a38e122008-04-29 01:01:24 -0700179 callout_len = strlen(callout_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 }
181
182 /* get the destination keyring if specified */
David Howells664cceb2005-09-28 17:03:15 +0100183 dest_ref = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 if (destringid) {
David Howells55931222009-09-02 09:13:45 +0100185 dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE,
186 KEY_WRITE);
David Howells664cceb2005-09-28 17:03:15 +0100187 if (IS_ERR(dest_ref)) {
188 ret = PTR_ERR(dest_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 goto error3;
190 }
191 }
192
193 /* find the key type */
194 ktype = key_type_lookup(type);
195 if (IS_ERR(ktype)) {
196 ret = PTR_ERR(ktype);
197 goto error4;
198 }
199
200 /* do the search */
David Howells4a38e122008-04-29 01:01:24 -0700201 key = request_key_and_link(ktype, description, callout_info,
202 callout_len, NULL, key_ref_to_ptr(dest_ref),
David Howells7e047ef2006-06-26 00:24:50 -0700203 KEY_ALLOC_IN_QUOTA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 if (IS_ERR(key)) {
205 ret = PTR_ERR(key);
206 goto error5;
207 }
208
David Howells4aab1e82011-03-11 17:57:33 +0000209 /* wait for the key to finish being constructed */
210 ret = wait_for_key_construction(key, 1);
211 if (ret < 0)
212 goto error6;
213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 ret = key->serial;
215
David Howells4aab1e82011-03-11 17:57:33 +0000216error6:
David Howells3e301482005-06-23 22:00:56 -0700217 key_put(key);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700218error5:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 key_type_put(ktype);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700220error4:
David Howells664cceb2005-09-28 17:03:15 +0100221 key_ref_put(dest_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700222error3:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 kfree(callout_info);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700224error2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 kfree(description);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700226error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000228}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230/*
David Howells973c9f42011-01-20 16:38:33 +0000231 * Get the ID of the specified process keyring.
232 *
233 * The requested keyring must have search permission to be found.
234 *
235 * If successful, the ID of the requested keyring will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 */
237long keyctl_get_keyring_ID(key_serial_t id, int create)
238{
David Howells664cceb2005-09-28 17:03:15 +0100239 key_ref_t key_ref;
David Howells55931222009-09-02 09:13:45 +0100240 unsigned long lflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 long ret;
242
David Howells55931222009-09-02 09:13:45 +0100243 lflags = create ? KEY_LOOKUP_CREATE : 0;
244 key_ref = lookup_user_key(id, lflags, KEY_SEARCH);
David Howells664cceb2005-09-28 17:03:15 +0100245 if (IS_ERR(key_ref)) {
246 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 goto error;
248 }
249
David Howells664cceb2005-09-28 17:03:15 +0100250 ret = key_ref_to_ptr(key_ref)->serial;
251 key_ref_put(key_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700252error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 return ret;
David Howells973c9f42011-01-20 16:38:33 +0000254}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256/*
David Howells973c9f42011-01-20 16:38:33 +0000257 * Join a (named) session keyring.
258 *
259 * Create and join an anonymous session keyring or join a named session
260 * keyring, creating it if necessary. A named session keyring must have Search
261 * permission for it to be joined. Session keyrings without this permit will
262 * be skipped over.
263 *
264 * If successful, the ID of the joined session keyring will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 */
266long keyctl_join_session_keyring(const char __user *_name)
267{
268 char *name;
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800269 long ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
271 /* fetch the name from userspace */
272 name = NULL;
273 if (_name) {
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800274 name = strndup_user(_name, PAGE_SIZE);
275 if (IS_ERR(name)) {
276 ret = PTR_ERR(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 goto error;
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800278 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 }
280
281 /* join the session */
282 ret = join_session_keyring(name);
Vegard Nossum0d54ee12009-01-17 17:45:45 +0100283 kfree(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700285error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000287}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289/*
David Howells973c9f42011-01-20 16:38:33 +0000290 * Update a key's data payload from the given data.
291 *
292 * The key must grant the caller Write permission and the key type must support
293 * updating for this to work. A negative key can be positively instantiated
294 * with this call.
295 *
296 * If successful, 0 will be returned. If the key type does not support
297 * updating, then -EOPNOTSUPP will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 */
299long keyctl_update_key(key_serial_t id,
300 const void __user *_payload,
301 size_t plen)
302{
David Howells664cceb2005-09-28 17:03:15 +0100303 key_ref_t key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 void *payload;
305 long ret;
306
307 ret = -EINVAL;
308 if (plen > PAGE_SIZE)
309 goto error;
310
311 /* pull the payload in if one was supplied */
312 payload = NULL;
313 if (_payload) {
314 ret = -ENOMEM;
315 payload = kmalloc(plen, GFP_KERNEL);
316 if (!payload)
317 goto error;
318
319 ret = -EFAULT;
320 if (copy_from_user(payload, _payload, plen) != 0)
321 goto error2;
322 }
323
324 /* find the target key (which must be writable) */
David Howells55931222009-09-02 09:13:45 +0100325 key_ref = lookup_user_key(id, 0, KEY_WRITE);
David Howells664cceb2005-09-28 17:03:15 +0100326 if (IS_ERR(key_ref)) {
327 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 goto error2;
329 }
330
331 /* update the key */
David Howells664cceb2005-09-28 17:03:15 +0100332 ret = key_update(key_ref, payload, plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
David Howells664cceb2005-09-28 17:03:15 +0100334 key_ref_put(key_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700335error2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 kfree(payload);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700337error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000339}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341/*
David Howells973c9f42011-01-20 16:38:33 +0000342 * Revoke a key.
343 *
344 * The key must be grant the caller Write or Setattr permission for this to
345 * work. The key type should give up its quota claim when revoked. The key
346 * and any links to the key will be automatically garbage collected after a
347 * certain amount of time (/proc/sys/kernel/keys/gc_delay).
348 *
349 * If successful, 0 is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 */
351long keyctl_revoke_key(key_serial_t id)
352{
David Howells664cceb2005-09-28 17:03:15 +0100353 key_ref_t key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 long ret;
355
David Howells55931222009-09-02 09:13:45 +0100356 key_ref = lookup_user_key(id, 0, KEY_WRITE);
David Howells664cceb2005-09-28 17:03:15 +0100357 if (IS_ERR(key_ref)) {
358 ret = PTR_ERR(key_ref);
David Howells0c2c9a32009-09-02 09:13:50 +0100359 if (ret != -EACCES)
360 goto error;
361 key_ref = lookup_user_key(id, 0, KEY_SETATTR);
362 if (IS_ERR(key_ref)) {
363 ret = PTR_ERR(key_ref);
364 goto error;
365 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 }
367
David Howells664cceb2005-09-28 17:03:15 +0100368 key_revoke(key_ref_to_ptr(key_ref));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 ret = 0;
370
David Howells664cceb2005-09-28 17:03:15 +0100371 key_ref_put(key_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700372error:
David Howells1260f802005-08-04 11:50:01 +0100373 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000374}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376/*
David Howells973c9f42011-01-20 16:38:33 +0000377 * Clear the specified keyring, creating an empty process keyring if one of the
378 * special keyring IDs is used.
379 *
380 * The keyring must grant the caller Write permission for this to work. If
381 * successful, 0 will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 */
383long keyctl_keyring_clear(key_serial_t ringid)
384{
David Howells664cceb2005-09-28 17:03:15 +0100385 key_ref_t keyring_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 long ret;
387
David Howells55931222009-09-02 09:13:45 +0100388 keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_WRITE);
David Howells664cceb2005-09-28 17:03:15 +0100389 if (IS_ERR(keyring_ref)) {
390 ret = PTR_ERR(keyring_ref);
David Howells700920e2012-01-18 15:31:45 +0000391
392 /* Root is permitted to invalidate certain special keyrings */
393 if (capable(CAP_SYS_ADMIN)) {
394 keyring_ref = lookup_user_key(ringid, 0, 0);
395 if (IS_ERR(keyring_ref))
396 goto error;
397 if (test_bit(KEY_FLAG_ROOT_CAN_CLEAR,
398 &key_ref_to_ptr(keyring_ref)->flags))
399 goto clear;
400 goto error_put;
401 }
402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 goto error;
404 }
405
David Howells700920e2012-01-18 15:31:45 +0000406clear:
David Howells664cceb2005-09-28 17:03:15 +0100407 ret = keyring_clear(key_ref_to_ptr(keyring_ref));
David Howells700920e2012-01-18 15:31:45 +0000408error_put:
David Howells664cceb2005-09-28 17:03:15 +0100409 key_ref_put(keyring_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700410error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000412}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414/*
David Howells973c9f42011-01-20 16:38:33 +0000415 * Create a link from a keyring to a key if there's no matching key in the
416 * keyring, otherwise replace the link to the matching key with a link to the
417 * new key.
418 *
419 * The key must grant the caller Link permission and the the keyring must grant
420 * the caller Write permission. Furthermore, if an additional link is created,
421 * the keyring's quota will be extended.
422 *
423 * If successful, 0 will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 */
425long keyctl_keyring_link(key_serial_t id, key_serial_t ringid)
426{
David Howells664cceb2005-09-28 17:03:15 +0100427 key_ref_t keyring_ref, key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 long ret;
429
David Howells55931222009-09-02 09:13:45 +0100430 keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_WRITE);
David Howells664cceb2005-09-28 17:03:15 +0100431 if (IS_ERR(keyring_ref)) {
432 ret = PTR_ERR(keyring_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 goto error;
434 }
435
David Howells55931222009-09-02 09:13:45 +0100436 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE, KEY_LINK);
David Howells664cceb2005-09-28 17:03:15 +0100437 if (IS_ERR(key_ref)) {
438 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 goto error2;
440 }
441
David Howells664cceb2005-09-28 17:03:15 +0100442 ret = key_link(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
David Howells664cceb2005-09-28 17:03:15 +0100444 key_ref_put(key_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700445error2:
David Howells664cceb2005-09-28 17:03:15 +0100446 key_ref_put(keyring_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700447error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000449}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451/*
David Howells973c9f42011-01-20 16:38:33 +0000452 * Unlink a key from a keyring.
453 *
454 * The keyring must grant the caller Write permission for this to work; the key
455 * itself need not grant the caller anything. If the last link to a key is
456 * removed then that key will be scheduled for destruction.
457 *
458 * If successful, 0 will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 */
460long keyctl_keyring_unlink(key_serial_t id, key_serial_t ringid)
461{
David Howells664cceb2005-09-28 17:03:15 +0100462 key_ref_t keyring_ref, key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 long ret;
464
David Howells55931222009-09-02 09:13:45 +0100465 keyring_ref = lookup_user_key(ringid, 0, KEY_WRITE);
David Howells664cceb2005-09-28 17:03:15 +0100466 if (IS_ERR(keyring_ref)) {
467 ret = PTR_ERR(keyring_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 goto error;
469 }
470
David Howells55931222009-09-02 09:13:45 +0100471 key_ref = lookup_user_key(id, KEY_LOOKUP_FOR_UNLINK, 0);
David Howells664cceb2005-09-28 17:03:15 +0100472 if (IS_ERR(key_ref)) {
473 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 goto error2;
475 }
476
David Howells664cceb2005-09-28 17:03:15 +0100477 ret = key_unlink(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
David Howells664cceb2005-09-28 17:03:15 +0100479 key_ref_put(key_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700480error2:
David Howells664cceb2005-09-28 17:03:15 +0100481 key_ref_put(keyring_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700482error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000484}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486/*
David Howells973c9f42011-01-20 16:38:33 +0000487 * Return a description of a key to userspace.
488 *
489 * The key must grant the caller View permission for this to work.
490 *
491 * If there's a buffer, we place up to buflen bytes of data into it formatted
492 * in the following way:
493 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 * type;uid;gid;perm;description<NUL>
David Howells973c9f42011-01-20 16:38:33 +0000495 *
496 * If successful, we return the amount of description available, irrespective
497 * of how much we may have copied into the buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 */
499long keyctl_describe_key(key_serial_t keyid,
500 char __user *buffer,
501 size_t buflen)
502{
David Howells3e301482005-06-23 22:00:56 -0700503 struct key *key, *instkey;
David Howells664cceb2005-09-28 17:03:15 +0100504 key_ref_t key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 char *tmpbuf;
506 long ret;
507
David Howells55931222009-09-02 09:13:45 +0100508 key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_VIEW);
David Howells664cceb2005-09-28 17:03:15 +0100509 if (IS_ERR(key_ref)) {
David Howells3e301482005-06-23 22:00:56 -0700510 /* viewing a key under construction is permitted if we have the
511 * authorisation token handy */
David Howells664cceb2005-09-28 17:03:15 +0100512 if (PTR_ERR(key_ref) == -EACCES) {
David Howells3e301482005-06-23 22:00:56 -0700513 instkey = key_get_instantiation_authkey(keyid);
514 if (!IS_ERR(instkey)) {
515 key_put(instkey);
David Howells8bbf49762008-11-14 10:39:14 +1100516 key_ref = lookup_user_key(keyid,
David Howells55931222009-09-02 09:13:45 +0100517 KEY_LOOKUP_PARTIAL,
518 0);
David Howells664cceb2005-09-28 17:03:15 +0100519 if (!IS_ERR(key_ref))
David Howells3e301482005-06-23 22:00:56 -0700520 goto okay;
521 }
522 }
523
David Howells664cceb2005-09-28 17:03:15 +0100524 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 goto error;
526 }
527
David Howells3e301482005-06-23 22:00:56 -0700528okay:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 /* calculate how much description we're going to return */
530 ret = -ENOMEM;
531 tmpbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
532 if (!tmpbuf)
533 goto error2;
534
David Howells664cceb2005-09-28 17:03:15 +0100535 key = key_ref_to_ptr(key_ref);
536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 ret = snprintf(tmpbuf, PAGE_SIZE - 1,
David Howells664cceb2005-09-28 17:03:15 +0100538 "%s;%d;%d;%08x;%s",
David Howells94fd8402010-06-28 14:05:04 +0100539 key->type->name,
540 key->uid,
541 key->gid,
542 key->perm,
543 key->description ?: "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
545 /* include a NUL char at the end of the data */
546 if (ret > PAGE_SIZE - 1)
547 ret = PAGE_SIZE - 1;
548 tmpbuf[ret] = 0;
549 ret++;
550
551 /* consider returning the data */
552 if (buffer && buflen > 0) {
553 if (buflen > ret)
554 buflen = ret;
555
556 if (copy_to_user(buffer, tmpbuf, buflen) != 0)
557 ret = -EFAULT;
558 }
559
560 kfree(tmpbuf);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700561error2:
David Howells664cceb2005-09-28 17:03:15 +0100562 key_ref_put(key_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700563error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000565}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567/*
David Howells973c9f42011-01-20 16:38:33 +0000568 * Search the specified keyring and any keyrings it links to for a matching
569 * key. Only keyrings that grant the caller Search permission will be searched
570 * (this includes the starting keyring). Only keys with Search permission can
571 * be found.
572 *
573 * If successful, the found key will be linked to the destination keyring if
574 * supplied and the key has Link permission, and the found key ID will be
575 * returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 */
577long keyctl_keyring_search(key_serial_t ringid,
578 const char __user *_type,
579 const char __user *_description,
580 key_serial_t destringid)
581{
582 struct key_type *ktype;
David Howells664cceb2005-09-28 17:03:15 +0100583 key_ref_t keyring_ref, key_ref, dest_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 char type[32], *description;
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800585 long ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
587 /* pull the type and description into kernel space */
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800588 ret = key_get_type_from_user(type, _type, sizeof(type));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 if (ret < 0)
590 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800592 description = strndup_user(_description, PAGE_SIZE);
593 if (IS_ERR(description)) {
594 ret = PTR_ERR(description);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 goto error;
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800596 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
598 /* get the keyring at which to begin the search */
David Howells55931222009-09-02 09:13:45 +0100599 keyring_ref = lookup_user_key(ringid, 0, KEY_SEARCH);
David Howells664cceb2005-09-28 17:03:15 +0100600 if (IS_ERR(keyring_ref)) {
601 ret = PTR_ERR(keyring_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 goto error2;
603 }
604
605 /* get the destination keyring if specified */
David Howells664cceb2005-09-28 17:03:15 +0100606 dest_ref = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 if (destringid) {
David Howells55931222009-09-02 09:13:45 +0100608 dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE,
609 KEY_WRITE);
David Howells664cceb2005-09-28 17:03:15 +0100610 if (IS_ERR(dest_ref)) {
611 ret = PTR_ERR(dest_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 goto error3;
613 }
614 }
615
616 /* find the key type */
617 ktype = key_type_lookup(type);
618 if (IS_ERR(ktype)) {
619 ret = PTR_ERR(ktype);
620 goto error4;
621 }
622
623 /* do the search */
David Howells664cceb2005-09-28 17:03:15 +0100624 key_ref = keyring_search(keyring_ref, ktype, description);
625 if (IS_ERR(key_ref)) {
626 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
628 /* treat lack or presence of a negative key the same */
629 if (ret == -EAGAIN)
630 ret = -ENOKEY;
631 goto error5;
632 }
633
634 /* link the resulting key to the destination keyring if we can */
David Howells664cceb2005-09-28 17:03:15 +0100635 if (dest_ref) {
David Howells29db9192005-10-30 15:02:44 -0800636 ret = key_permission(key_ref, KEY_LINK);
637 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 goto error6;
639
David Howells664cceb2005-09-28 17:03:15 +0100640 ret = key_link(key_ref_to_ptr(dest_ref), key_ref_to_ptr(key_ref));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 if (ret < 0)
642 goto error6;
643 }
644
David Howells664cceb2005-09-28 17:03:15 +0100645 ret = key_ref_to_ptr(key_ref)->serial;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700647error6:
David Howells664cceb2005-09-28 17:03:15 +0100648 key_ref_put(key_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700649error5:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 key_type_put(ktype);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700651error4:
David Howells664cceb2005-09-28 17:03:15 +0100652 key_ref_put(dest_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700653error3:
David Howells664cceb2005-09-28 17:03:15 +0100654 key_ref_put(keyring_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700655error2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 kfree(description);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700657error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000659}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661/*
David Howells973c9f42011-01-20 16:38:33 +0000662 * Read a key's payload.
663 *
664 * The key must either grant the caller Read permission, or it must grant the
665 * caller Search permission when searched for from the process keyrings.
666 *
667 * If successful, we place up to buflen bytes of data into the buffer, if one
668 * is provided, and return the amount of data that is available in the key,
669 * irrespective of how much we copied into the buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 */
671long keyctl_read_key(key_serial_t keyid, char __user *buffer, size_t buflen)
672{
David Howells664cceb2005-09-28 17:03:15 +0100673 struct key *key;
674 key_ref_t key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 long ret;
676
677 /* find the key first */
David Howells55931222009-09-02 09:13:45 +0100678 key_ref = lookup_user_key(keyid, 0, 0);
David Howells664cceb2005-09-28 17:03:15 +0100679 if (IS_ERR(key_ref)) {
680 ret = -ENOKEY;
681 goto error;
682 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
David Howells664cceb2005-09-28 17:03:15 +0100684 key = key_ref_to_ptr(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
David Howells664cceb2005-09-28 17:03:15 +0100686 /* see if we can read it directly */
David Howells29db9192005-10-30 15:02:44 -0800687 ret = key_permission(key_ref, KEY_READ);
688 if (ret == 0)
David Howells664cceb2005-09-28 17:03:15 +0100689 goto can_read_key;
David Howells29db9192005-10-30 15:02:44 -0800690 if (ret != -EACCES)
691 goto error;
David Howells664cceb2005-09-28 17:03:15 +0100692
693 /* we can't; see if it's searchable from this process's keyrings
694 * - we automatically take account of the fact that it may be
695 * dangling off an instantiation key
696 */
697 if (!is_key_possessed(key_ref)) {
698 ret = -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 goto error2;
700 }
701
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 /* the key is probably readable - now try to read it */
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700703can_read_key:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 ret = key_validate(key);
705 if (ret == 0) {
706 ret = -EOPNOTSUPP;
707 if (key->type->read) {
708 /* read the data with the semaphore held (since we
709 * might sleep) */
710 down_read(&key->sem);
711 ret = key->type->read(key, buffer, buflen);
712 up_read(&key->sem);
713 }
714 }
715
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700716error2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 key_put(key);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700718error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000720}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722/*
David Howells973c9f42011-01-20 16:38:33 +0000723 * Change the ownership of a key
724 *
725 * The key must grant the caller Setattr permission for this to work, though
726 * the key need not be fully instantiated yet. For the UID to be changed, or
727 * for the GID to be changed to a group the caller is not a member of, the
728 * caller must have sysadmin capability. If either uid or gid is -1 then that
729 * attribute is not changed.
730 *
731 * If the UID is to be changed, the new user must have sufficient quota to
732 * accept the key. The quota deduction will be removed from the old user to
733 * the new user should the attribute be changed.
734 *
735 * If successful, 0 will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 */
737long keyctl_chown_key(key_serial_t id, uid_t uid, gid_t gid)
738{
Fredrik Tolf58016492006-06-26 00:24:51 -0700739 struct key_user *newowner, *zapowner = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 struct key *key;
David Howells664cceb2005-09-28 17:03:15 +0100741 key_ref_t key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 long ret;
743
744 ret = 0;
745 if (uid == (uid_t) -1 && gid == (gid_t) -1)
746 goto error;
747
David Howells55931222009-09-02 09:13:45 +0100748 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
749 KEY_SETATTR);
David Howells664cceb2005-09-28 17:03:15 +0100750 if (IS_ERR(key_ref)) {
751 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 goto error;
753 }
754
David Howells664cceb2005-09-28 17:03:15 +0100755 key = key_ref_to_ptr(key_ref);
756
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 /* make the changes with the locks held to prevent chown/chown races */
758 ret = -EACCES;
759 down_write(&key->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
761 if (!capable(CAP_SYS_ADMIN)) {
762 /* only the sysadmin can chown a key to some other UID */
763 if (uid != (uid_t) -1 && key->uid != uid)
Fredrik Tolf58016492006-06-26 00:24:51 -0700764 goto error_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
766 /* only the sysadmin can set the key's GID to a group other
767 * than one of those that the current process subscribes to */
768 if (gid != (gid_t) -1 && gid != key->gid && !in_group_p(gid))
Fredrik Tolf58016492006-06-26 00:24:51 -0700769 goto error_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 }
771
Fredrik Tolf58016492006-06-26 00:24:51 -0700772 /* change the UID */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 if (uid != (uid_t) -1 && uid != key->uid) {
Fredrik Tolf58016492006-06-26 00:24:51 -0700774 ret = -ENOMEM;
Serge E. Hallyn1d1e9752009-02-26 18:27:38 -0600775 newowner = key_user_lookup(uid, current_user_ns());
Fredrik Tolf58016492006-06-26 00:24:51 -0700776 if (!newowner)
777 goto error_put;
778
779 /* transfer the quota burden to the new user */
780 if (test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) {
David Howells0b77f5b2008-04-29 01:01:32 -0700781 unsigned maxkeys = (uid == 0) ?
782 key_quota_root_maxkeys : key_quota_maxkeys;
783 unsigned maxbytes = (uid == 0) ?
784 key_quota_root_maxbytes : key_quota_maxbytes;
785
Fredrik Tolf58016492006-06-26 00:24:51 -0700786 spin_lock(&newowner->lock);
David Howells0b77f5b2008-04-29 01:01:32 -0700787 if (newowner->qnkeys + 1 >= maxkeys ||
788 newowner->qnbytes + key->quotalen >= maxbytes ||
789 newowner->qnbytes + key->quotalen <
790 newowner->qnbytes)
Fredrik Tolf58016492006-06-26 00:24:51 -0700791 goto quota_overrun;
792
793 newowner->qnkeys++;
794 newowner->qnbytes += key->quotalen;
795 spin_unlock(&newowner->lock);
796
797 spin_lock(&key->user->lock);
798 key->user->qnkeys--;
799 key->user->qnbytes -= key->quotalen;
800 spin_unlock(&key->user->lock);
801 }
802
803 atomic_dec(&key->user->nkeys);
804 atomic_inc(&newowner->nkeys);
805
806 if (test_bit(KEY_FLAG_INSTANTIATED, &key->flags)) {
807 atomic_dec(&key->user->nikeys);
808 atomic_inc(&newowner->nikeys);
809 }
810
811 zapowner = key->user;
812 key->user = newowner;
813 key->uid = uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 }
815
816 /* change the GID */
817 if (gid != (gid_t) -1)
818 key->gid = gid;
819
820 ret = 0;
821
Fredrik Tolf58016492006-06-26 00:24:51 -0700822error_put:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 up_write(&key->sem);
824 key_put(key);
Fredrik Tolf58016492006-06-26 00:24:51 -0700825 if (zapowner)
826 key_user_put(zapowner);
827error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 return ret;
829
Fredrik Tolf58016492006-06-26 00:24:51 -0700830quota_overrun:
831 spin_unlock(&newowner->lock);
832 zapowner = newowner;
833 ret = -EDQUOT;
834 goto error_put;
David Howellsa8b17ed2011-01-20 16:38:27 +0000835}
Fredrik Tolf58016492006-06-26 00:24:51 -0700836
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837/*
David Howells973c9f42011-01-20 16:38:33 +0000838 * Change the permission mask on a key.
839 *
840 * The key must grant the caller Setattr permission for this to work, though
841 * the key need not be fully instantiated yet. If the caller does not have
842 * sysadmin capability, it may only change the permission on keys that it owns.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 */
844long keyctl_setperm_key(key_serial_t id, key_perm_t perm)
845{
846 struct key *key;
David Howells664cceb2005-09-28 17:03:15 +0100847 key_ref_t key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 long ret;
849
850 ret = -EINVAL;
David Howells664cceb2005-09-28 17:03:15 +0100851 if (perm & ~(KEY_POS_ALL | KEY_USR_ALL | KEY_GRP_ALL | KEY_OTH_ALL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 goto error;
853
David Howells55931222009-09-02 09:13:45 +0100854 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
855 KEY_SETATTR);
David Howells664cceb2005-09-28 17:03:15 +0100856 if (IS_ERR(key_ref)) {
857 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 goto error;
859 }
860
David Howells664cceb2005-09-28 17:03:15 +0100861 key = key_ref_to_ptr(key_ref);
862
David Howells76d8aea2005-06-23 22:00:49 -0700863 /* make the changes with the locks held to prevent chown/chmod races */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 ret = -EACCES;
865 down_write(&key->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
David Howells76d8aea2005-06-23 22:00:49 -0700867 /* if we're not the sysadmin, we can only change a key that we own */
David Howells47d804b2008-11-14 10:39:11 +1100868 if (capable(CAP_SYS_ADMIN) || key->uid == current_fsuid()) {
David Howells76d8aea2005-06-23 22:00:49 -0700869 key->perm = perm;
870 ret = 0;
871 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 up_write(&key->sem);
874 key_put(key);
David Howells76d8aea2005-06-23 22:00:49 -0700875error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000877}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
David Howells8bbf49762008-11-14 10:39:14 +1100879/*
David Howells973c9f42011-01-20 16:38:33 +0000880 * Get the destination keyring for instantiation and check that the caller has
881 * Write permission on it.
David Howells8bbf49762008-11-14 10:39:14 +1100882 */
883static long get_instantiation_keyring(key_serial_t ringid,
884 struct request_key_auth *rka,
885 struct key **_dest_keyring)
886{
887 key_ref_t dkref;
888
David Howellseca1bf52008-12-29 00:41:51 +0000889 *_dest_keyring = NULL;
890
David Howells8bbf49762008-11-14 10:39:14 +1100891 /* just return a NULL pointer if we weren't asked to make a link */
David Howellseca1bf52008-12-29 00:41:51 +0000892 if (ringid == 0)
David Howells8bbf49762008-11-14 10:39:14 +1100893 return 0;
David Howells8bbf49762008-11-14 10:39:14 +1100894
895 /* if a specific keyring is nominated by ID, then use that */
896 if (ringid > 0) {
David Howells55931222009-09-02 09:13:45 +0100897 dkref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_WRITE);
David Howells8bbf49762008-11-14 10:39:14 +1100898 if (IS_ERR(dkref))
899 return PTR_ERR(dkref);
900 *_dest_keyring = key_ref_to_ptr(dkref);
901 return 0;
902 }
903
904 if (ringid == KEY_SPEC_REQKEY_AUTH_KEY)
905 return -EINVAL;
906
907 /* otherwise specify the destination keyring recorded in the
908 * authorisation key (any KEY_SPEC_*_KEYRING) */
909 if (ringid >= KEY_SPEC_REQUESTOR_KEYRING) {
David Howells21279cf2009-10-15 10:14:35 +0100910 *_dest_keyring = key_get(rka->dest_keyring);
David Howells8bbf49762008-11-14 10:39:14 +1100911 return 0;
912 }
913
914 return -ENOKEY;
915}
916
David Howellsd84f4f92008-11-14 10:39:23 +1100917/*
David Howells973c9f42011-01-20 16:38:33 +0000918 * Change the request_key authorisation key on the current process.
David Howellsd84f4f92008-11-14 10:39:23 +1100919 */
920static int keyctl_change_reqkey_auth(struct key *key)
921{
922 struct cred *new;
923
924 new = prepare_creds();
925 if (!new)
926 return -ENOMEM;
927
928 key_put(new->request_key_auth);
929 new->request_key_auth = key_get(key);
930
931 return commit_creds(new);
932}
933
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934/*
David Howellsee009e4a02011-03-07 15:06:20 +0000935 * Copy the iovec data from userspace
936 */
937static long copy_from_user_iovec(void *buffer, const struct iovec *iov,
938 unsigned ioc)
939{
940 for (; ioc > 0; ioc--) {
941 if (copy_from_user(buffer, iov->iov_base, iov->iov_len) != 0)
942 return -EFAULT;
943 buffer += iov->iov_len;
944 iov++;
945 }
946 return 0;
947}
948
949/*
David Howells973c9f42011-01-20 16:38:33 +0000950 * Instantiate a key with the specified payload and link the key into the
951 * destination keyring if one is given.
952 *
953 * The caller must have the appropriate instantiation permit set for this to
954 * work (see keyctl_assume_authority). No other permissions are required.
955 *
956 * If successful, 0 will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 */
David Howellsee009e4a02011-03-07 15:06:20 +0000958long keyctl_instantiate_key_common(key_serial_t id,
959 const struct iovec *payload_iov,
960 unsigned ioc,
961 size_t plen,
962 key_serial_t ringid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963{
David Howellsd84f4f92008-11-14 10:39:23 +1100964 const struct cred *cred = current_cred();
David Howells3e301482005-06-23 22:00:56 -0700965 struct request_key_auth *rka;
David Howells8bbf49762008-11-14 10:39:14 +1100966 struct key *instkey, *dest_keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 void *payload;
968 long ret;
David Howells38bbca62008-04-29 01:01:19 -0700969 bool vm = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
David Howellsd84f4f92008-11-14 10:39:23 +1100971 kenter("%d,,%zu,%d", id, plen, ringid);
972
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 ret = -EINVAL;
David Howells38bbca62008-04-29 01:01:19 -0700974 if (plen > 1024 * 1024 - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 goto error;
976
David Howellsb5f545c2006-01-08 01:02:47 -0800977 /* the appropriate instantiation authorisation key must have been
978 * assumed before calling this */
979 ret = -EPERM;
David Howellsd84f4f92008-11-14 10:39:23 +1100980 instkey = cred->request_key_auth;
David Howellsb5f545c2006-01-08 01:02:47 -0800981 if (!instkey)
982 goto error;
983
984 rka = instkey->payload.data;
985 if (rka->target_key->serial != id)
986 goto error;
987
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 /* pull the payload in if one was supplied */
989 payload = NULL;
990
David Howellsee009e4a02011-03-07 15:06:20 +0000991 if (payload_iov) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 ret = -ENOMEM;
993 payload = kmalloc(plen, GFP_KERNEL);
David Howells38bbca62008-04-29 01:01:19 -0700994 if (!payload) {
995 if (plen <= PAGE_SIZE)
996 goto error;
997 vm = true;
998 payload = vmalloc(plen);
999 if (!payload)
1000 goto error;
1001 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
David Howellsee009e4a02011-03-07 15:06:20 +00001003 ret = copy_from_user_iovec(payload, payload_iov, ioc);
1004 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 goto error2;
1006 }
1007
David Howells3e301482005-06-23 22:00:56 -07001008 /* find the destination keyring amongst those belonging to the
1009 * requesting task */
David Howells8bbf49762008-11-14 10:39:14 +11001010 ret = get_instantiation_keyring(ringid, rka, &dest_keyring);
1011 if (ret < 0)
1012 goto error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
1014 /* instantiate the key and link it into a keyring */
David Howells3e301482005-06-23 22:00:56 -07001015 ret = key_instantiate_and_link(rka->target_key, payload, plen,
David Howells8bbf49762008-11-14 10:39:14 +11001016 dest_keyring, instkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
David Howells8bbf49762008-11-14 10:39:14 +11001018 key_put(dest_keyring);
David Howellsb5f545c2006-01-08 01:02:47 -08001019
1020 /* discard the assumed authority if it's just been disabled by
1021 * instantiation of the key */
David Howellsd84f4f92008-11-14 10:39:23 +11001022 if (ret == 0)
1023 keyctl_change_reqkey_auth(NULL);
David Howellsb5f545c2006-01-08 01:02:47 -08001024
1025error2:
David Howells38bbca62008-04-29 01:01:19 -07001026 if (!vm)
1027 kfree(payload);
1028 else
1029 vfree(payload);
David Howellsb5f545c2006-01-08 01:02:47 -08001030error:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +00001032}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034/*
David Howellsee009e4a02011-03-07 15:06:20 +00001035 * Instantiate a key with the specified payload and link the key into the
1036 * destination keyring if one is given.
1037 *
1038 * The caller must have the appropriate instantiation permit set for this to
1039 * work (see keyctl_assume_authority). No other permissions are required.
1040 *
1041 * If successful, 0 will be returned.
1042 */
1043long keyctl_instantiate_key(key_serial_t id,
1044 const void __user *_payload,
1045 size_t plen,
1046 key_serial_t ringid)
1047{
1048 if (_payload && plen) {
1049 struct iovec iov[1] = {
1050 [0].iov_base = (void __user *)_payload,
1051 [0].iov_len = plen
1052 };
1053
1054 return keyctl_instantiate_key_common(id, iov, 1, plen, ringid);
1055 }
1056
1057 return keyctl_instantiate_key_common(id, NULL, 0, 0, ringid);
1058}
1059
1060/*
1061 * Instantiate a key with the specified multipart payload and link the key into
1062 * the destination keyring if one is given.
1063 *
1064 * The caller must have the appropriate instantiation permit set for this to
1065 * work (see keyctl_assume_authority). No other permissions are required.
1066 *
1067 * If successful, 0 will be returned.
1068 */
1069long keyctl_instantiate_key_iov(key_serial_t id,
1070 const struct iovec __user *_payload_iov,
1071 unsigned ioc,
1072 key_serial_t ringid)
1073{
1074 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
1075 long ret;
1076
1077 if (_payload_iov == 0 || ioc == 0)
1078 goto no_payload;
1079
1080 ret = rw_copy_check_uvector(WRITE, _payload_iov, ioc,
Christopher Yeohfcf63402011-10-31 17:06:39 -07001081 ARRAY_SIZE(iovstack), iovstack, &iov, 1);
David Howellsee009e4a02011-03-07 15:06:20 +00001082 if (ret < 0)
1083 return ret;
1084 if (ret == 0)
1085 goto no_payload_free;
1086
1087 ret = keyctl_instantiate_key_common(id, iov, ioc, ret, ringid);
1088
1089 if (iov != iovstack)
1090 kfree(iov);
1091 return ret;
1092
1093no_payload_free:
1094 if (iov != iovstack)
1095 kfree(iov);
1096no_payload:
1097 return keyctl_instantiate_key_common(id, NULL, 0, 0, ringid);
1098}
1099
1100/*
David Howells973c9f42011-01-20 16:38:33 +00001101 * Negatively instantiate the key with the given timeout (in seconds) and link
1102 * the key into the destination keyring if one is given.
1103 *
1104 * The caller must have the appropriate instantiation permit set for this to
1105 * work (see keyctl_assume_authority). No other permissions are required.
1106 *
1107 * The key and any links to the key will be automatically garbage collected
1108 * after the timeout expires.
1109 *
1110 * Negative keys are used to rate limit repeated request_key() calls by causing
1111 * them to return -ENOKEY until the negative key expires.
1112 *
1113 * If successful, 0 will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 */
1115long keyctl_negate_key(key_serial_t id, unsigned timeout, key_serial_t ringid)
1116{
David Howellsfdd1b942011-03-07 15:06:09 +00001117 return keyctl_reject_key(id, timeout, ENOKEY, ringid);
1118}
1119
1120/*
1121 * Negatively instantiate the key with the given timeout (in seconds) and error
1122 * code and link the key into the destination keyring if one is given.
1123 *
1124 * The caller must have the appropriate instantiation permit set for this to
1125 * work (see keyctl_assume_authority). No other permissions are required.
1126 *
1127 * The key and any links to the key will be automatically garbage collected
1128 * after the timeout expires.
1129 *
1130 * Negative keys are used to rate limit repeated request_key() calls by causing
1131 * them to return the specified error code until the negative key expires.
1132 *
1133 * If successful, 0 will be returned.
1134 */
1135long keyctl_reject_key(key_serial_t id, unsigned timeout, unsigned error,
1136 key_serial_t ringid)
1137{
David Howellsd84f4f92008-11-14 10:39:23 +11001138 const struct cred *cred = current_cred();
David Howells3e301482005-06-23 22:00:56 -07001139 struct request_key_auth *rka;
David Howells8bbf49762008-11-14 10:39:14 +11001140 struct key *instkey, *dest_keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 long ret;
1142
David Howellsfdd1b942011-03-07 15:06:09 +00001143 kenter("%d,%u,%u,%d", id, timeout, error, ringid);
1144
1145 /* must be a valid error code and mustn't be a kernel special */
1146 if (error <= 0 ||
1147 error >= MAX_ERRNO ||
1148 error == ERESTARTSYS ||
1149 error == ERESTARTNOINTR ||
1150 error == ERESTARTNOHAND ||
1151 error == ERESTART_RESTARTBLOCK)
1152 return -EINVAL;
David Howellsd84f4f92008-11-14 10:39:23 +11001153
David Howellsb5f545c2006-01-08 01:02:47 -08001154 /* the appropriate instantiation authorisation key must have been
1155 * assumed before calling this */
1156 ret = -EPERM;
David Howellsd84f4f92008-11-14 10:39:23 +11001157 instkey = cred->request_key_auth;
David Howellsb5f545c2006-01-08 01:02:47 -08001158 if (!instkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
David Howells3e301482005-06-23 22:00:56 -07001161 rka = instkey->payload.data;
David Howellsb5f545c2006-01-08 01:02:47 -08001162 if (rka->target_key->serial != id)
1163 goto error;
David Howells3e301482005-06-23 22:00:56 -07001164
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 /* find the destination keyring if present (which must also be
1166 * writable) */
David Howells8bbf49762008-11-14 10:39:14 +11001167 ret = get_instantiation_keyring(ringid, rka, &dest_keyring);
1168 if (ret < 0)
1169 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
1171 /* instantiate the key and link it into a keyring */
David Howellsfdd1b942011-03-07 15:06:09 +00001172 ret = key_reject_and_link(rka->target_key, timeout, error,
David Howells8bbf49762008-11-14 10:39:14 +11001173 dest_keyring, instkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
David Howells8bbf49762008-11-14 10:39:14 +11001175 key_put(dest_keyring);
David Howellsb5f545c2006-01-08 01:02:47 -08001176
1177 /* discard the assumed authority if it's just been disabled by
1178 * instantiation of the key */
David Howellsd84f4f92008-11-14 10:39:23 +11001179 if (ret == 0)
1180 keyctl_change_reqkey_auth(NULL);
David Howellsb5f545c2006-01-08 01:02:47 -08001181
1182error:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +00001184}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186/*
David Howells973c9f42011-01-20 16:38:33 +00001187 * Read or set the default keyring in which request_key() will cache keys and
1188 * return the old setting.
1189 *
1190 * If a process keyring is specified then this will be created if it doesn't
1191 * yet exist. The old setting will be returned if successful.
David Howells3e301482005-06-23 22:00:56 -07001192 */
1193long keyctl_set_reqkey_keyring(int reqkey_defl)
1194{
David Howellsd84f4f92008-11-14 10:39:23 +11001195 struct cred *new;
1196 int ret, old_setting;
1197
1198 old_setting = current_cred_xxx(jit_keyring);
1199
1200 if (reqkey_defl == KEY_REQKEY_DEFL_NO_CHANGE)
1201 return old_setting;
1202
1203 new = prepare_creds();
1204 if (!new)
1205 return -ENOMEM;
David Howells3e301482005-06-23 22:00:56 -07001206
1207 switch (reqkey_defl) {
1208 case KEY_REQKEY_DEFL_THREAD_KEYRING:
David Howellsd84f4f92008-11-14 10:39:23 +11001209 ret = install_thread_keyring_to_cred(new);
David Howells3e301482005-06-23 22:00:56 -07001210 if (ret < 0)
David Howellsd84f4f92008-11-14 10:39:23 +11001211 goto error;
David Howells3e301482005-06-23 22:00:56 -07001212 goto set;
1213
1214 case KEY_REQKEY_DEFL_PROCESS_KEYRING:
David Howellsd84f4f92008-11-14 10:39:23 +11001215 ret = install_process_keyring_to_cred(new);
1216 if (ret < 0) {
1217 if (ret != -EEXIST)
1218 goto error;
1219 ret = 0;
1220 }
1221 goto set;
David Howells3e301482005-06-23 22:00:56 -07001222
1223 case KEY_REQKEY_DEFL_DEFAULT:
1224 case KEY_REQKEY_DEFL_SESSION_KEYRING:
1225 case KEY_REQKEY_DEFL_USER_KEYRING:
1226 case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
David Howellsd84f4f92008-11-14 10:39:23 +11001227 case KEY_REQKEY_DEFL_REQUESTOR_KEYRING:
1228 goto set;
David Howells3e301482005-06-23 22:00:56 -07001229
1230 case KEY_REQKEY_DEFL_NO_CHANGE:
David Howells3e301482005-06-23 22:00:56 -07001231 case KEY_REQKEY_DEFL_GROUP_KEYRING:
1232 default:
David Howellsd84f4f92008-11-14 10:39:23 +11001233 ret = -EINVAL;
1234 goto error;
David Howells3e301482005-06-23 22:00:56 -07001235 }
1236
David Howellsd84f4f92008-11-14 10:39:23 +11001237set:
1238 new->jit_keyring = reqkey_defl;
1239 commit_creds(new);
1240 return old_setting;
1241error:
1242 abort_creds(new);
Dan Carpenter4303ef12010-06-11 17:30:05 +01001243 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +00001244}
David Howellsd84f4f92008-11-14 10:39:23 +11001245
David Howells3e301482005-06-23 22:00:56 -07001246/*
David Howells973c9f42011-01-20 16:38:33 +00001247 * Set or clear the timeout on a key.
1248 *
1249 * Either the key must grant the caller Setattr permission or else the caller
1250 * must hold an instantiation authorisation token for the key.
1251 *
1252 * The timeout is either 0 to clear the timeout, or a number of seconds from
1253 * the current time. The key and any links to the key will be automatically
1254 * garbage collected after the timeout expires.
1255 *
1256 * If successful, 0 is returned.
David Howells017679c2006-01-08 01:02:43 -08001257 */
1258long keyctl_set_timeout(key_serial_t id, unsigned timeout)
1259{
1260 struct timespec now;
David Howells91562352010-06-11 17:31:05 +01001261 struct key *key, *instkey;
David Howells017679c2006-01-08 01:02:43 -08001262 key_ref_t key_ref;
1263 time_t expiry;
1264 long ret;
1265
David Howells55931222009-09-02 09:13:45 +01001266 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
1267 KEY_SETATTR);
David Howells017679c2006-01-08 01:02:43 -08001268 if (IS_ERR(key_ref)) {
David Howells91562352010-06-11 17:31:05 +01001269 /* setting the timeout on a key under construction is permitted
1270 * if we have the authorisation token handy */
1271 if (PTR_ERR(key_ref) == -EACCES) {
1272 instkey = key_get_instantiation_authkey(id);
1273 if (!IS_ERR(instkey)) {
1274 key_put(instkey);
1275 key_ref = lookup_user_key(id,
1276 KEY_LOOKUP_PARTIAL,
1277 0);
1278 if (!IS_ERR(key_ref))
1279 goto okay;
1280 }
1281 }
1282
David Howells017679c2006-01-08 01:02:43 -08001283 ret = PTR_ERR(key_ref);
1284 goto error;
1285 }
1286
David Howells91562352010-06-11 17:31:05 +01001287okay:
David Howells017679c2006-01-08 01:02:43 -08001288 key = key_ref_to_ptr(key_ref);
1289
1290 /* make the changes with the locks held to prevent races */
1291 down_write(&key->sem);
1292
1293 expiry = 0;
1294 if (timeout > 0) {
1295 now = current_kernel_time();
1296 expiry = now.tv_sec + timeout;
1297 }
1298
1299 key->expiry = expiry;
David Howellsc08ef802009-09-14 17:26:13 +01001300 key_schedule_gc(key->expiry + key_gc_delay);
David Howells017679c2006-01-08 01:02:43 -08001301
1302 up_write(&key->sem);
1303 key_put(key);
1304
1305 ret = 0;
1306error:
1307 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +00001308}
David Howells017679c2006-01-08 01:02:43 -08001309
David Howells017679c2006-01-08 01:02:43 -08001310/*
David Howells973c9f42011-01-20 16:38:33 +00001311 * Assume (or clear) the authority to instantiate the specified key.
1312 *
1313 * This sets the authoritative token currently in force for key instantiation.
1314 * This must be done for a key to be instantiated. It has the effect of making
1315 * available all the keys from the caller of the request_key() that created a
1316 * key to request_key() calls made by the caller of this function.
1317 *
1318 * The caller must have the instantiation key in their process keyrings with a
1319 * Search permission grant available to the caller.
1320 *
1321 * If the ID given is 0, then the setting will be cleared and 0 returned.
1322 *
1323 * If the ID given has a matching an authorisation key, then that key will be
1324 * set and its ID will be returned. The authorisation key can be read to get
1325 * the callout information passed to request_key().
David Howellsb5f545c2006-01-08 01:02:47 -08001326 */
1327long keyctl_assume_authority(key_serial_t id)
1328{
1329 struct key *authkey;
1330 long ret;
1331
1332 /* special key IDs aren't permitted */
1333 ret = -EINVAL;
1334 if (id < 0)
1335 goto error;
1336
1337 /* we divest ourselves of authority if given an ID of 0 */
1338 if (id == 0) {
David Howellsd84f4f92008-11-14 10:39:23 +11001339 ret = keyctl_change_reqkey_auth(NULL);
David Howellsb5f545c2006-01-08 01:02:47 -08001340 goto error;
1341 }
1342
1343 /* attempt to assume the authority temporarily granted to us whilst we
1344 * instantiate the specified key
1345 * - the authorisation key must be in the current task's keyrings
1346 * somewhere
1347 */
1348 authkey = key_get_instantiation_authkey(id);
1349 if (IS_ERR(authkey)) {
1350 ret = PTR_ERR(authkey);
1351 goto error;
1352 }
1353
David Howellsd84f4f92008-11-14 10:39:23 +11001354 ret = keyctl_change_reqkey_auth(authkey);
1355 if (ret < 0)
1356 goto error;
1357 key_put(authkey);
David Howellsb5f545c2006-01-08 01:02:47 -08001358
David Howellsd84f4f92008-11-14 10:39:23 +11001359 ret = authkey->serial;
David Howellsb5f545c2006-01-08 01:02:47 -08001360error:
1361 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +00001362}
David Howellsb5f545c2006-01-08 01:02:47 -08001363
David Howells70a5bb72008-04-29 01:01:26 -07001364/*
David Howells973c9f42011-01-20 16:38:33 +00001365 * Get a key's the LSM security label.
1366 *
1367 * The key must grant the caller View permission for this to work.
1368 *
1369 * If there's a buffer, then up to buflen bytes of data will be placed into it.
1370 *
1371 * If successful, the amount of information available will be returned,
1372 * irrespective of how much was copied (including the terminal NUL).
David Howells70a5bb72008-04-29 01:01:26 -07001373 */
1374long keyctl_get_security(key_serial_t keyid,
1375 char __user *buffer,
1376 size_t buflen)
1377{
1378 struct key *key, *instkey;
1379 key_ref_t key_ref;
1380 char *context;
1381 long ret;
1382
David Howells55931222009-09-02 09:13:45 +01001383 key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_VIEW);
David Howells70a5bb72008-04-29 01:01:26 -07001384 if (IS_ERR(key_ref)) {
1385 if (PTR_ERR(key_ref) != -EACCES)
1386 return PTR_ERR(key_ref);
1387
1388 /* viewing a key under construction is also permitted if we
1389 * have the authorisation token handy */
1390 instkey = key_get_instantiation_authkey(keyid);
1391 if (IS_ERR(instkey))
Roel Kluinfa1cc7b2009-12-15 15:05:12 -08001392 return PTR_ERR(instkey);
David Howells70a5bb72008-04-29 01:01:26 -07001393 key_put(instkey);
1394
David Howells55931222009-09-02 09:13:45 +01001395 key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, 0);
David Howells70a5bb72008-04-29 01:01:26 -07001396 if (IS_ERR(key_ref))
1397 return PTR_ERR(key_ref);
1398 }
1399
1400 key = key_ref_to_ptr(key_ref);
1401 ret = security_key_getsecurity(key, &context);
1402 if (ret == 0) {
1403 /* if no information was returned, give userspace an empty
1404 * string */
1405 ret = 1;
1406 if (buffer && buflen > 0 &&
1407 copy_to_user(buffer, "", 1) != 0)
1408 ret = -EFAULT;
1409 } else if (ret > 0) {
1410 /* return as much data as there's room for */
1411 if (buffer && buflen > 0) {
1412 if (buflen > ret)
1413 buflen = ret;
1414
1415 if (copy_to_user(buffer, context, buflen) != 0)
1416 ret = -EFAULT;
1417 }
1418
1419 kfree(context);
1420 }
1421
1422 key_ref_put(key_ref);
1423 return ret;
1424}
1425
David Howellsee18d642009-09-02 09:14:21 +01001426/*
David Howells973c9f42011-01-20 16:38:33 +00001427 * Attempt to install the calling process's session keyring on the process's
1428 * parent process.
1429 *
1430 * The keyring must exist and must grant the caller LINK permission, and the
1431 * parent process must be single-threaded and must have the same effective
1432 * ownership as this process and mustn't be SUID/SGID.
1433 *
1434 * The keyring will be emplaced on the parent when it next resumes userspace.
1435 *
1436 * If successful, 0 will be returned.
David Howellsee18d642009-09-02 09:14:21 +01001437 */
1438long keyctl_session_to_parent(void)
1439{
Geert Uytterhoevena00ae4d2009-12-13 20:21:34 +01001440#ifdef TIF_NOTIFY_RESUME
David Howellsee18d642009-09-02 09:14:21 +01001441 struct task_struct *me, *parent;
1442 const struct cred *mycred, *pcred;
1443 struct cred *cred, *oldcred;
1444 key_ref_t keyring_r;
1445 int ret;
1446
1447 keyring_r = lookup_user_key(KEY_SPEC_SESSION_KEYRING, 0, KEY_LINK);
1448 if (IS_ERR(keyring_r))
1449 return PTR_ERR(keyring_r);
1450
1451 /* our parent is going to need a new cred struct, a new tgcred struct
1452 * and new security data, so we allocate them here to prevent ENOMEM in
1453 * our parent */
1454 ret = -ENOMEM;
1455 cred = cred_alloc_blank();
1456 if (!cred)
1457 goto error_keyring;
1458
1459 cred->tgcred->session_keyring = key_ref_to_ptr(keyring_r);
1460 keyring_r = NULL;
1461
1462 me = current;
David Howells9d1ac652010-09-10 09:59:46 +01001463 rcu_read_lock();
David Howellsee18d642009-09-02 09:14:21 +01001464 write_lock_irq(&tasklist_lock);
1465
1466 parent = me->real_parent;
1467 ret = -EPERM;
1468
1469 /* the parent mustn't be init and mustn't be a kernel thread */
1470 if (parent->pid <= 1 || !parent->mm)
1471 goto not_permitted;
1472
1473 /* the parent must be single threaded */
Oleg Nesterovdd98acf2010-05-26 14:43:23 -07001474 if (!thread_group_empty(parent))
David Howellsee18d642009-09-02 09:14:21 +01001475 goto not_permitted;
1476
1477 /* the parent and the child must have different session keyrings or
1478 * there's no point */
1479 mycred = current_cred();
1480 pcred = __task_cred(parent);
1481 if (mycred == pcred ||
1482 mycred->tgcred->session_keyring == pcred->tgcred->session_keyring)
1483 goto already_same;
1484
1485 /* the parent must have the same effective ownership and mustn't be
1486 * SUID/SGID */
Justin P. Mattockc5b60b52010-04-21 00:02:11 -07001487 if (pcred->uid != mycred->euid ||
David Howellsee18d642009-09-02 09:14:21 +01001488 pcred->euid != mycred->euid ||
1489 pcred->suid != mycred->euid ||
Justin P. Mattockc5b60b52010-04-21 00:02:11 -07001490 pcred->gid != mycred->egid ||
David Howellsee18d642009-09-02 09:14:21 +01001491 pcred->egid != mycred->egid ||
1492 pcred->sgid != mycred->egid)
1493 goto not_permitted;
1494
1495 /* the keyrings must have the same UID */
David Howells3d964062010-09-10 09:59:51 +01001496 if ((pcred->tgcred->session_keyring &&
1497 pcred->tgcred->session_keyring->uid != mycred->euid) ||
David Howellsee18d642009-09-02 09:14:21 +01001498 mycred->tgcred->session_keyring->uid != mycred->euid)
1499 goto not_permitted;
1500
David Howellsee18d642009-09-02 09:14:21 +01001501 /* if there's an already pending keyring replacement, then we replace
1502 * that */
1503 oldcred = parent->replacement_session_keyring;
1504
1505 /* the replacement session keyring is applied just prior to userspace
1506 * restarting */
1507 parent->replacement_session_keyring = cred;
1508 cred = NULL;
1509 set_ti_thread_flag(task_thread_info(parent), TIF_NOTIFY_RESUME);
1510
1511 write_unlock_irq(&tasklist_lock);
David Howells9d1ac652010-09-10 09:59:46 +01001512 rcu_read_unlock();
David Howellsee18d642009-09-02 09:14:21 +01001513 if (oldcred)
1514 put_cred(oldcred);
1515 return 0;
1516
1517already_same:
1518 ret = 0;
1519not_permitted:
Marc Dionne5c843422009-09-14 12:46:23 +01001520 write_unlock_irq(&tasklist_lock);
David Howells9d1ac652010-09-10 09:59:46 +01001521 rcu_read_unlock();
David Howellsee18d642009-09-02 09:14:21 +01001522 put_cred(cred);
1523 return ret;
1524
1525error_keyring:
1526 key_ref_put(keyring_r);
1527 return ret;
Geert Uytterhoevena00ae4d2009-12-13 20:21:34 +01001528
1529#else /* !TIF_NOTIFY_RESUME */
1530 /*
1531 * To be removed when TIF_NOTIFY_RESUME has been implemented on
1532 * m68k/xtensa
1533 */
1534#warning TIF_NOTIFY_RESUME not implemented
1535 return -EOPNOTSUPP;
1536#endif /* !TIF_NOTIFY_RESUME */
David Howellsee18d642009-09-02 09:14:21 +01001537}
1538
David Howellsb5f545c2006-01-08 01:02:47 -08001539/*
David Howells973c9f42011-01-20 16:38:33 +00001540 * The key control system call
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 */
Heiko Carstens938bb9f2009-01-14 14:14:30 +01001542SYSCALL_DEFINE5(keyctl, int, option, unsigned long, arg2, unsigned long, arg3,
1543 unsigned long, arg4, unsigned long, arg5)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544{
1545 switch (option) {
1546 case KEYCTL_GET_KEYRING_ID:
1547 return keyctl_get_keyring_ID((key_serial_t) arg2,
1548 (int) arg3);
1549
1550 case KEYCTL_JOIN_SESSION_KEYRING:
1551 return keyctl_join_session_keyring((const char __user *) arg2);
1552
1553 case KEYCTL_UPDATE:
1554 return keyctl_update_key((key_serial_t) arg2,
1555 (const void __user *) arg3,
1556 (size_t) arg4);
1557
1558 case KEYCTL_REVOKE:
1559 return keyctl_revoke_key((key_serial_t) arg2);
1560
1561 case KEYCTL_DESCRIBE:
1562 return keyctl_describe_key((key_serial_t) arg2,
1563 (char __user *) arg3,
1564 (unsigned) arg4);
1565
1566 case KEYCTL_CLEAR:
1567 return keyctl_keyring_clear((key_serial_t) arg2);
1568
1569 case KEYCTL_LINK:
1570 return keyctl_keyring_link((key_serial_t) arg2,
1571 (key_serial_t) arg3);
1572
1573 case KEYCTL_UNLINK:
1574 return keyctl_keyring_unlink((key_serial_t) arg2,
1575 (key_serial_t) arg3);
1576
1577 case KEYCTL_SEARCH:
1578 return keyctl_keyring_search((key_serial_t) arg2,
1579 (const char __user *) arg3,
1580 (const char __user *) arg4,
1581 (key_serial_t) arg5);
1582
1583 case KEYCTL_READ:
1584 return keyctl_read_key((key_serial_t) arg2,
1585 (char __user *) arg3,
1586 (size_t) arg4);
1587
1588 case KEYCTL_CHOWN:
1589 return keyctl_chown_key((key_serial_t) arg2,
1590 (uid_t) arg3,
1591 (gid_t) arg4);
1592
1593 case KEYCTL_SETPERM:
1594 return keyctl_setperm_key((key_serial_t) arg2,
1595 (key_perm_t) arg3);
1596
1597 case KEYCTL_INSTANTIATE:
1598 return keyctl_instantiate_key((key_serial_t) arg2,
1599 (const void __user *) arg3,
1600 (size_t) arg4,
1601 (key_serial_t) arg5);
1602
1603 case KEYCTL_NEGATE:
1604 return keyctl_negate_key((key_serial_t) arg2,
1605 (unsigned) arg3,
1606 (key_serial_t) arg4);
1607
David Howells3e301482005-06-23 22:00:56 -07001608 case KEYCTL_SET_REQKEY_KEYRING:
1609 return keyctl_set_reqkey_keyring(arg2);
1610
David Howells017679c2006-01-08 01:02:43 -08001611 case KEYCTL_SET_TIMEOUT:
1612 return keyctl_set_timeout((key_serial_t) arg2,
1613 (unsigned) arg3);
1614
David Howellsb5f545c2006-01-08 01:02:47 -08001615 case KEYCTL_ASSUME_AUTHORITY:
1616 return keyctl_assume_authority((key_serial_t) arg2);
1617
David Howells70a5bb72008-04-29 01:01:26 -07001618 case KEYCTL_GET_SECURITY:
1619 return keyctl_get_security((key_serial_t) arg2,
James Morris90bd49a2008-12-29 14:35:35 +11001620 (char __user *) arg3,
David Howells70a5bb72008-04-29 01:01:26 -07001621 (size_t) arg4);
1622
David Howellsee18d642009-09-02 09:14:21 +01001623 case KEYCTL_SESSION_TO_PARENT:
1624 return keyctl_session_to_parent();
1625
David Howellsfdd1b942011-03-07 15:06:09 +00001626 case KEYCTL_REJECT:
1627 return keyctl_reject_key((key_serial_t) arg2,
1628 (unsigned) arg3,
1629 (unsigned) arg4,
1630 (key_serial_t) arg5);
1631
David Howellsee009e4a02011-03-07 15:06:20 +00001632 case KEYCTL_INSTANTIATE_IOV:
1633 return keyctl_instantiate_key_iov(
1634 (key_serial_t) arg2,
1635 (const struct iovec __user *) arg3,
1636 (unsigned) arg4,
1637 (key_serial_t) arg5);
1638
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 default:
1640 return -EOPNOTSUPP;
1641 }
David Howellsa8b17ed2011-01-20 16:38:27 +00001642}