blob: baaa9cfc0e0ac3082d59bf9deca6fa0f6f47b6d2 [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;
Eric Biggers64fa6cd2017-06-08 14:48:40 +010085 if (plen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 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
David Howells90396b22017-04-18 15:31:07 +0100263 * be skipped over. It is not permitted for userspace to create or join
264 * keyrings whose name begin with a dot.
David Howells973c9f42011-01-20 16:38:33 +0000265 *
266 * If successful, the ID of the joined session keyring will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 */
268long keyctl_join_session_keyring(const char __user *_name)
269{
270 char *name;
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800271 long ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
273 /* fetch the name from userspace */
274 name = NULL;
275 if (_name) {
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800276 name = strndup_user(_name, PAGE_SIZE);
277 if (IS_ERR(name)) {
278 ret = PTR_ERR(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 goto error;
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800280 }
David Howells90396b22017-04-18 15:31:07 +0100281
282 ret = -EPERM;
283 if (name[0] == '.')
284 goto error_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 }
286
287 /* join the session */
288 ret = join_session_keyring(name);
David Howells90396b22017-04-18 15:31:07 +0100289error_name:
Vegard Nossum0d54ee12009-01-17 17:45:45 +0100290 kfree(name);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700291error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000293}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295/*
David Howells973c9f42011-01-20 16:38:33 +0000296 * Update a key's data payload from the given data.
297 *
298 * The key must grant the caller Write permission and the key type must support
299 * updating for this to work. A negative key can be positively instantiated
300 * with this call.
301 *
302 * If successful, 0 will be returned. If the key type does not support
303 * updating, then -EOPNOTSUPP will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 */
305long keyctl_update_key(key_serial_t id,
306 const void __user *_payload,
307 size_t plen)
308{
David Howells664cceb2005-09-28 17:03:15 +0100309 key_ref_t key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 void *payload;
311 long ret;
312
313 ret = -EINVAL;
314 if (plen > PAGE_SIZE)
315 goto error;
316
317 /* pull the payload in if one was supplied */
318 payload = NULL;
Eric Biggers64fa6cd2017-06-08 14:48:40 +0100319 if (plen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 ret = -ENOMEM;
321 payload = kmalloc(plen, GFP_KERNEL);
322 if (!payload)
323 goto error;
324
325 ret = -EFAULT;
326 if (copy_from_user(payload, _payload, plen) != 0)
327 goto error2;
328 }
329
330 /* find the target key (which must be writable) */
David Howells55931222009-09-02 09:13:45 +0100331 key_ref = lookup_user_key(id, 0, KEY_WRITE);
David Howells664cceb2005-09-28 17:03:15 +0100332 if (IS_ERR(key_ref)) {
333 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 goto error2;
335 }
336
337 /* update the key */
David Howells664cceb2005-09-28 17:03:15 +0100338 ret = key_update(key_ref, payload, plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
David Howells664cceb2005-09-28 17:03:15 +0100340 key_ref_put(key_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700341error2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 kfree(payload);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700343error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000345}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347/*
David Howells973c9f42011-01-20 16:38:33 +0000348 * Revoke a key.
349 *
350 * The key must be grant the caller Write or Setattr permission for this to
351 * work. The key type should give up its quota claim when revoked. The key
352 * and any links to the key will be automatically garbage collected after a
353 * certain amount of time (/proc/sys/kernel/keys/gc_delay).
354 *
355 * If successful, 0 is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 */
357long keyctl_revoke_key(key_serial_t id)
358{
David Howells664cceb2005-09-28 17:03:15 +0100359 key_ref_t key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 long ret;
361
David Howells55931222009-09-02 09:13:45 +0100362 key_ref = lookup_user_key(id, 0, KEY_WRITE);
David Howells664cceb2005-09-28 17:03:15 +0100363 if (IS_ERR(key_ref)) {
364 ret = PTR_ERR(key_ref);
David Howells0c2c9a32009-09-02 09:13:50 +0100365 if (ret != -EACCES)
366 goto error;
367 key_ref = lookup_user_key(id, 0, KEY_SETATTR);
368 if (IS_ERR(key_ref)) {
369 ret = PTR_ERR(key_ref);
370 goto error;
371 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 }
373
David Howells664cceb2005-09-28 17:03:15 +0100374 key_revoke(key_ref_to_ptr(key_ref));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 ret = 0;
376
David Howells664cceb2005-09-28 17:03:15 +0100377 key_ref_put(key_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700378error:
David Howells1260f802005-08-04 11:50:01 +0100379 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000380}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382/*
David Howells973c9f42011-01-20 16:38:33 +0000383 * Clear the specified keyring, creating an empty process keyring if one of the
384 * special keyring IDs is used.
385 *
386 * The keyring must grant the caller Write permission for this to work. If
387 * successful, 0 will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 */
389long keyctl_keyring_clear(key_serial_t ringid)
390{
David Howells664cceb2005-09-28 17:03:15 +0100391 key_ref_t keyring_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 long ret;
393
David Howells55931222009-09-02 09:13:45 +0100394 keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_WRITE);
David Howells664cceb2005-09-28 17:03:15 +0100395 if (IS_ERR(keyring_ref)) {
396 ret = PTR_ERR(keyring_ref);
David Howells700920e2012-01-18 15:31:45 +0000397
398 /* Root is permitted to invalidate certain special keyrings */
399 if (capable(CAP_SYS_ADMIN)) {
400 keyring_ref = lookup_user_key(ringid, 0, 0);
401 if (IS_ERR(keyring_ref))
402 goto error;
403 if (test_bit(KEY_FLAG_ROOT_CAN_CLEAR,
404 &key_ref_to_ptr(keyring_ref)->flags))
405 goto clear;
406 goto error_put;
407 }
408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 goto error;
410 }
411
David Howells700920e2012-01-18 15:31:45 +0000412clear:
David Howells664cceb2005-09-28 17:03:15 +0100413 ret = keyring_clear(key_ref_to_ptr(keyring_ref));
David Howells700920e2012-01-18 15:31:45 +0000414error_put:
David Howells664cceb2005-09-28 17:03:15 +0100415 key_ref_put(keyring_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700416error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000418}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420/*
David Howells973c9f42011-01-20 16:38:33 +0000421 * Create a link from a keyring to a key if there's no matching key in the
422 * keyring, otherwise replace the link to the matching key with a link to the
423 * new key.
424 *
425 * The key must grant the caller Link permission and the the keyring must grant
426 * the caller Write permission. Furthermore, if an additional link is created,
427 * the keyring's quota will be extended.
428 *
429 * If successful, 0 will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 */
431long keyctl_keyring_link(key_serial_t id, key_serial_t ringid)
432{
David Howells664cceb2005-09-28 17:03:15 +0100433 key_ref_t keyring_ref, key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 long ret;
435
David Howells55931222009-09-02 09:13:45 +0100436 keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_WRITE);
David Howells664cceb2005-09-28 17:03:15 +0100437 if (IS_ERR(keyring_ref)) {
438 ret = PTR_ERR(keyring_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 goto error;
440 }
441
David Howells55931222009-09-02 09:13:45 +0100442 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE, KEY_LINK);
David Howells664cceb2005-09-28 17:03:15 +0100443 if (IS_ERR(key_ref)) {
444 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 goto error2;
446 }
447
David Howells664cceb2005-09-28 17:03:15 +0100448 ret = key_link(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
David Howells664cceb2005-09-28 17:03:15 +0100450 key_ref_put(key_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700451error2:
David Howells664cceb2005-09-28 17:03:15 +0100452 key_ref_put(keyring_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700453error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000455}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457/*
David Howells973c9f42011-01-20 16:38:33 +0000458 * Unlink a key from a keyring.
459 *
460 * The keyring must grant the caller Write permission for this to work; the key
461 * itself need not grant the caller anything. If the last link to a key is
462 * removed then that key will be scheduled for destruction.
463 *
464 * If successful, 0 will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 */
466long keyctl_keyring_unlink(key_serial_t id, key_serial_t ringid)
467{
David Howells664cceb2005-09-28 17:03:15 +0100468 key_ref_t keyring_ref, key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 long ret;
470
David Howells55931222009-09-02 09:13:45 +0100471 keyring_ref = lookup_user_key(ringid, 0, KEY_WRITE);
David Howells664cceb2005-09-28 17:03:15 +0100472 if (IS_ERR(keyring_ref)) {
473 ret = PTR_ERR(keyring_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 goto error;
475 }
476
David Howells55931222009-09-02 09:13:45 +0100477 key_ref = lookup_user_key(id, KEY_LOOKUP_FOR_UNLINK, 0);
David Howells664cceb2005-09-28 17:03:15 +0100478 if (IS_ERR(key_ref)) {
479 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 goto error2;
481 }
482
David Howells664cceb2005-09-28 17:03:15 +0100483 ret = key_unlink(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
David Howells664cceb2005-09-28 17:03:15 +0100485 key_ref_put(key_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700486error2:
David Howells664cceb2005-09-28 17:03:15 +0100487 key_ref_put(keyring_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700488error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000490}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492/*
David Howells973c9f42011-01-20 16:38:33 +0000493 * Return a description of a key to userspace.
494 *
495 * The key must grant the caller View permission for this to work.
496 *
497 * If there's a buffer, we place up to buflen bytes of data into it formatted
498 * in the following way:
499 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 * type;uid;gid;perm;description<NUL>
David Howells973c9f42011-01-20 16:38:33 +0000501 *
502 * If successful, we return the amount of description available, irrespective
503 * of how much we may have copied into the buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 */
505long keyctl_describe_key(key_serial_t keyid,
506 char __user *buffer,
507 size_t buflen)
508{
David Howells3e301482005-06-23 22:00:56 -0700509 struct key *key, *instkey;
David Howells664cceb2005-09-28 17:03:15 +0100510 key_ref_t key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 char *tmpbuf;
512 long ret;
513
David Howells55931222009-09-02 09:13:45 +0100514 key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_VIEW);
David Howells664cceb2005-09-28 17:03:15 +0100515 if (IS_ERR(key_ref)) {
David Howells3e301482005-06-23 22:00:56 -0700516 /* viewing a key under construction is permitted if we have the
517 * authorisation token handy */
David Howells664cceb2005-09-28 17:03:15 +0100518 if (PTR_ERR(key_ref) == -EACCES) {
David Howells3e301482005-06-23 22:00:56 -0700519 instkey = key_get_instantiation_authkey(keyid);
520 if (!IS_ERR(instkey)) {
521 key_put(instkey);
David Howells8bbf49762008-11-14 10:39:14 +1100522 key_ref = lookup_user_key(keyid,
David Howells55931222009-09-02 09:13:45 +0100523 KEY_LOOKUP_PARTIAL,
524 0);
David Howells664cceb2005-09-28 17:03:15 +0100525 if (!IS_ERR(key_ref))
David Howells3e301482005-06-23 22:00:56 -0700526 goto okay;
527 }
528 }
529
David Howells664cceb2005-09-28 17:03:15 +0100530 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 goto error;
532 }
533
David Howells3e301482005-06-23 22:00:56 -0700534okay:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 /* calculate how much description we're going to return */
536 ret = -ENOMEM;
537 tmpbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
538 if (!tmpbuf)
539 goto error2;
540
David Howells664cceb2005-09-28 17:03:15 +0100541 key = key_ref_to_ptr(key_ref);
542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 ret = snprintf(tmpbuf, PAGE_SIZE - 1,
David Howells664cceb2005-09-28 17:03:15 +0100544 "%s;%d;%d;%08x;%s",
David Howells94fd8402010-06-28 14:05:04 +0100545 key->type->name,
546 key->uid,
547 key->gid,
548 key->perm,
549 key->description ?: "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551 /* include a NUL char at the end of the data */
552 if (ret > PAGE_SIZE - 1)
553 ret = PAGE_SIZE - 1;
554 tmpbuf[ret] = 0;
555 ret++;
556
557 /* consider returning the data */
558 if (buffer && buflen > 0) {
559 if (buflen > ret)
560 buflen = ret;
561
562 if (copy_to_user(buffer, tmpbuf, buflen) != 0)
563 ret = -EFAULT;
564 }
565
566 kfree(tmpbuf);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700567error2:
David Howells664cceb2005-09-28 17:03:15 +0100568 key_ref_put(key_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700569error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000571}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573/*
David Howells973c9f42011-01-20 16:38:33 +0000574 * Search the specified keyring and any keyrings it links to for a matching
575 * key. Only keyrings that grant the caller Search permission will be searched
576 * (this includes the starting keyring). Only keys with Search permission can
577 * be found.
578 *
579 * If successful, the found key will be linked to the destination keyring if
580 * supplied and the key has Link permission, and the found key ID will be
581 * returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 */
583long keyctl_keyring_search(key_serial_t ringid,
584 const char __user *_type,
585 const char __user *_description,
586 key_serial_t destringid)
587{
588 struct key_type *ktype;
David Howells664cceb2005-09-28 17:03:15 +0100589 key_ref_t keyring_ref, key_ref, dest_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 char type[32], *description;
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800591 long ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
593 /* pull the type and description into kernel space */
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800594 ret = key_get_type_from_user(type, _type, sizeof(type));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 if (ret < 0)
596 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800598 description = strndup_user(_description, PAGE_SIZE);
599 if (IS_ERR(description)) {
600 ret = PTR_ERR(description);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 goto error;
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800602 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
604 /* get the keyring at which to begin the search */
David Howells55931222009-09-02 09:13:45 +0100605 keyring_ref = lookup_user_key(ringid, 0, KEY_SEARCH);
David Howells664cceb2005-09-28 17:03:15 +0100606 if (IS_ERR(keyring_ref)) {
607 ret = PTR_ERR(keyring_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 goto error2;
609 }
610
611 /* get the destination keyring if specified */
David Howells664cceb2005-09-28 17:03:15 +0100612 dest_ref = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 if (destringid) {
David Howells55931222009-09-02 09:13:45 +0100614 dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE,
615 KEY_WRITE);
David Howells664cceb2005-09-28 17:03:15 +0100616 if (IS_ERR(dest_ref)) {
617 ret = PTR_ERR(dest_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 goto error3;
619 }
620 }
621
622 /* find the key type */
623 ktype = key_type_lookup(type);
624 if (IS_ERR(ktype)) {
625 ret = PTR_ERR(ktype);
626 goto error4;
627 }
628
629 /* do the search */
David Howells664cceb2005-09-28 17:03:15 +0100630 key_ref = keyring_search(keyring_ref, ktype, description);
631 if (IS_ERR(key_ref)) {
632 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634 /* treat lack or presence of a negative key the same */
635 if (ret == -EAGAIN)
636 ret = -ENOKEY;
637 goto error5;
638 }
639
640 /* link the resulting key to the destination keyring if we can */
David Howells664cceb2005-09-28 17:03:15 +0100641 if (dest_ref) {
David Howells29db9192005-10-30 15:02:44 -0800642 ret = key_permission(key_ref, KEY_LINK);
643 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 goto error6;
645
David Howells664cceb2005-09-28 17:03:15 +0100646 ret = key_link(key_ref_to_ptr(dest_ref), key_ref_to_ptr(key_ref));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 if (ret < 0)
648 goto error6;
649 }
650
David Howells664cceb2005-09-28 17:03:15 +0100651 ret = key_ref_to_ptr(key_ref)->serial;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700653error6:
David Howells664cceb2005-09-28 17:03:15 +0100654 key_ref_put(key_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700655error5:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 key_type_put(ktype);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700657error4:
David Howells664cceb2005-09-28 17:03:15 +0100658 key_ref_put(dest_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700659error3:
David Howells664cceb2005-09-28 17:03:15 +0100660 key_ref_put(keyring_ref);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700661error2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 kfree(description);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700663error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000665}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667/*
David Howells973c9f42011-01-20 16:38:33 +0000668 * Read a key's payload.
669 *
670 * The key must either grant the caller Read permission, or it must grant the
671 * caller Search permission when searched for from the process keyrings.
672 *
673 * If successful, we place up to buflen bytes of data into the buffer, if one
674 * is provided, and return the amount of data that is available in the key,
675 * irrespective of how much we copied into the buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 */
677long keyctl_read_key(key_serial_t keyid, char __user *buffer, size_t buflen)
678{
David Howells664cceb2005-09-28 17:03:15 +0100679 struct key *key;
680 key_ref_t key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 long ret;
682
683 /* find the key first */
David Howells55931222009-09-02 09:13:45 +0100684 key_ref = lookup_user_key(keyid, 0, 0);
David Howells664cceb2005-09-28 17:03:15 +0100685 if (IS_ERR(key_ref)) {
686 ret = -ENOKEY;
687 goto error;
688 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
David Howells664cceb2005-09-28 17:03:15 +0100690 key = key_ref_to_ptr(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Eric Biggers75bdf912017-09-18 11:37:23 -0700692 if (test_bit(KEY_FLAG_NEGATIVE, &key->flags)) {
693 ret = -ENOKEY;
694 goto error2;
695 }
696
David Howells664cceb2005-09-28 17:03:15 +0100697 /* see if we can read it directly */
David Howells29db9192005-10-30 15:02:44 -0800698 ret = key_permission(key_ref, KEY_READ);
699 if (ret == 0)
David Howells664cceb2005-09-28 17:03:15 +0100700 goto can_read_key;
David Howells29db9192005-10-30 15:02:44 -0800701 if (ret != -EACCES)
702 goto error;
David Howells664cceb2005-09-28 17:03:15 +0100703
704 /* we can't; see if it's searchable from this process's keyrings
705 * - we automatically take account of the fact that it may be
706 * dangling off an instantiation key
707 */
708 if (!is_key_possessed(key_ref)) {
709 ret = -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 goto error2;
711 }
712
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 /* the key is probably readable - now try to read it */
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700714can_read_key:
David Howells7edb5e82015-12-18 01:34:26 +0000715 ret = -EOPNOTSUPP;
716 if (key->type->read) {
717 /* Read the data with the semaphore held (since we might sleep)
718 * to protect against the key being updated or revoked.
719 */
720 down_read(&key->sem);
721 ret = key_validate(key);
722 if (ret == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 ret = key->type->read(key, buffer, buflen);
David Howells7edb5e82015-12-18 01:34:26 +0000724 up_read(&key->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 }
726
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700727error2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 key_put(key);
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700729error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000731}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733/*
David Howells973c9f42011-01-20 16:38:33 +0000734 * Change the ownership of a key
735 *
736 * The key must grant the caller Setattr permission for this to work, though
737 * the key need not be fully instantiated yet. For the UID to be changed, or
738 * for the GID to be changed to a group the caller is not a member of, the
739 * caller must have sysadmin capability. If either uid or gid is -1 then that
740 * attribute is not changed.
741 *
742 * If the UID is to be changed, the new user must have sufficient quota to
743 * accept the key. The quota deduction will be removed from the old user to
744 * the new user should the attribute be changed.
745 *
746 * If successful, 0 will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 */
748long keyctl_chown_key(key_serial_t id, uid_t uid, gid_t gid)
749{
Fredrik Tolf58016492006-06-26 00:24:51 -0700750 struct key_user *newowner, *zapowner = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 struct key *key;
David Howells664cceb2005-09-28 17:03:15 +0100752 key_ref_t key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 long ret;
754
755 ret = 0;
756 if (uid == (uid_t) -1 && gid == (gid_t) -1)
757 goto error;
758
David Howells55931222009-09-02 09:13:45 +0100759 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
760 KEY_SETATTR);
David Howells664cceb2005-09-28 17:03:15 +0100761 if (IS_ERR(key_ref)) {
762 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 goto error;
764 }
765
David Howells664cceb2005-09-28 17:03:15 +0100766 key = key_ref_to_ptr(key_ref);
767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 /* make the changes with the locks held to prevent chown/chown races */
769 ret = -EACCES;
770 down_write(&key->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
772 if (!capable(CAP_SYS_ADMIN)) {
773 /* only the sysadmin can chown a key to some other UID */
774 if (uid != (uid_t) -1 && key->uid != uid)
Fredrik Tolf58016492006-06-26 00:24:51 -0700775 goto error_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
777 /* only the sysadmin can set the key's GID to a group other
778 * than one of those that the current process subscribes to */
779 if (gid != (gid_t) -1 && gid != key->gid && !in_group_p(gid))
Fredrik Tolf58016492006-06-26 00:24:51 -0700780 goto error_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 }
782
Fredrik Tolf58016492006-06-26 00:24:51 -0700783 /* change the UID */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 if (uid != (uid_t) -1 && uid != key->uid) {
Fredrik Tolf58016492006-06-26 00:24:51 -0700785 ret = -ENOMEM;
Serge E. Hallyn1d1e9752009-02-26 18:27:38 -0600786 newowner = key_user_lookup(uid, current_user_ns());
Fredrik Tolf58016492006-06-26 00:24:51 -0700787 if (!newowner)
788 goto error_put;
789
790 /* transfer the quota burden to the new user */
791 if (test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) {
David Howells0b77f5b2008-04-29 01:01:32 -0700792 unsigned maxkeys = (uid == 0) ?
793 key_quota_root_maxkeys : key_quota_maxkeys;
794 unsigned maxbytes = (uid == 0) ?
795 key_quota_root_maxbytes : key_quota_maxbytes;
796
Fredrik Tolf58016492006-06-26 00:24:51 -0700797 spin_lock(&newowner->lock);
David Howells0b77f5b2008-04-29 01:01:32 -0700798 if (newowner->qnkeys + 1 >= maxkeys ||
799 newowner->qnbytes + key->quotalen >= maxbytes ||
800 newowner->qnbytes + key->quotalen <
801 newowner->qnbytes)
Fredrik Tolf58016492006-06-26 00:24:51 -0700802 goto quota_overrun;
803
804 newowner->qnkeys++;
805 newowner->qnbytes += key->quotalen;
806 spin_unlock(&newowner->lock);
807
808 spin_lock(&key->user->lock);
809 key->user->qnkeys--;
810 key->user->qnbytes -= key->quotalen;
811 spin_unlock(&key->user->lock);
812 }
813
814 atomic_dec(&key->user->nkeys);
815 atomic_inc(&newowner->nkeys);
816
817 if (test_bit(KEY_FLAG_INSTANTIATED, &key->flags)) {
818 atomic_dec(&key->user->nikeys);
819 atomic_inc(&newowner->nikeys);
820 }
821
822 zapowner = key->user;
823 key->user = newowner;
824 key->uid = uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 }
826
827 /* change the GID */
828 if (gid != (gid_t) -1)
829 key->gid = gid;
830
831 ret = 0;
832
Fredrik Tolf58016492006-06-26 00:24:51 -0700833error_put:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 up_write(&key->sem);
835 key_put(key);
Fredrik Tolf58016492006-06-26 00:24:51 -0700836 if (zapowner)
837 key_user_put(zapowner);
838error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 return ret;
840
Fredrik Tolf58016492006-06-26 00:24:51 -0700841quota_overrun:
842 spin_unlock(&newowner->lock);
843 zapowner = newowner;
844 ret = -EDQUOT;
845 goto error_put;
David Howellsa8b17ed2011-01-20 16:38:27 +0000846}
Fredrik Tolf58016492006-06-26 00:24:51 -0700847
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848/*
David Howells973c9f42011-01-20 16:38:33 +0000849 * Change the permission mask on a key.
850 *
851 * The key must grant the caller Setattr permission for this to work, though
852 * the key need not be fully instantiated yet. If the caller does not have
853 * sysadmin capability, it may only change the permission on keys that it owns.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 */
855long keyctl_setperm_key(key_serial_t id, key_perm_t perm)
856{
857 struct key *key;
David Howells664cceb2005-09-28 17:03:15 +0100858 key_ref_t key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 long ret;
860
861 ret = -EINVAL;
David Howells664cceb2005-09-28 17:03:15 +0100862 if (perm & ~(KEY_POS_ALL | KEY_USR_ALL | KEY_GRP_ALL | KEY_OTH_ALL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 goto error;
864
David Howells55931222009-09-02 09:13:45 +0100865 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
866 KEY_SETATTR);
David Howells664cceb2005-09-28 17:03:15 +0100867 if (IS_ERR(key_ref)) {
868 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 goto error;
870 }
871
David Howells664cceb2005-09-28 17:03:15 +0100872 key = key_ref_to_ptr(key_ref);
873
David Howells76d8aea2005-06-23 22:00:49 -0700874 /* make the changes with the locks held to prevent chown/chmod races */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 ret = -EACCES;
876 down_write(&key->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
David Howells76d8aea2005-06-23 22:00:49 -0700878 /* if we're not the sysadmin, we can only change a key that we own */
David Howells47d804b2008-11-14 10:39:11 +1100879 if (capable(CAP_SYS_ADMIN) || key->uid == current_fsuid()) {
David Howells76d8aea2005-06-23 22:00:49 -0700880 key->perm = perm;
881 ret = 0;
882 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 up_write(&key->sem);
885 key_put(key);
David Howells76d8aea2005-06-23 22:00:49 -0700886error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000888}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
David Howells8bbf49762008-11-14 10:39:14 +1100890/*
David Howells973c9f42011-01-20 16:38:33 +0000891 * Get the destination keyring for instantiation and check that the caller has
892 * Write permission on it.
David Howells8bbf49762008-11-14 10:39:14 +1100893 */
894static long get_instantiation_keyring(key_serial_t ringid,
895 struct request_key_auth *rka,
896 struct key **_dest_keyring)
897{
898 key_ref_t dkref;
899
David Howellseca1bf52008-12-29 00:41:51 +0000900 *_dest_keyring = NULL;
901
David Howells8bbf49762008-11-14 10:39:14 +1100902 /* just return a NULL pointer if we weren't asked to make a link */
David Howellseca1bf52008-12-29 00:41:51 +0000903 if (ringid == 0)
David Howells8bbf49762008-11-14 10:39:14 +1100904 return 0;
David Howells8bbf49762008-11-14 10:39:14 +1100905
906 /* if a specific keyring is nominated by ID, then use that */
907 if (ringid > 0) {
David Howells55931222009-09-02 09:13:45 +0100908 dkref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_WRITE);
David Howells8bbf49762008-11-14 10:39:14 +1100909 if (IS_ERR(dkref))
910 return PTR_ERR(dkref);
911 *_dest_keyring = key_ref_to_ptr(dkref);
912 return 0;
913 }
914
915 if (ringid == KEY_SPEC_REQKEY_AUTH_KEY)
916 return -EINVAL;
917
918 /* otherwise specify the destination keyring recorded in the
919 * authorisation key (any KEY_SPEC_*_KEYRING) */
920 if (ringid >= KEY_SPEC_REQUESTOR_KEYRING) {
David Howells21279cf2009-10-15 10:14:35 +0100921 *_dest_keyring = key_get(rka->dest_keyring);
David Howells8bbf49762008-11-14 10:39:14 +1100922 return 0;
923 }
924
925 return -ENOKEY;
926}
927
David Howellsd84f4f92008-11-14 10:39:23 +1100928/*
David Howells973c9f42011-01-20 16:38:33 +0000929 * Change the request_key authorisation key on the current process.
David Howellsd84f4f92008-11-14 10:39:23 +1100930 */
931static int keyctl_change_reqkey_auth(struct key *key)
932{
933 struct cred *new;
934
935 new = prepare_creds();
936 if (!new)
937 return -ENOMEM;
938
939 key_put(new->request_key_auth);
940 new->request_key_auth = key_get(key);
941
942 return commit_creds(new);
943}
944
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945/*
David Howellsee009e42011-03-07 15:06:20 +0000946 * Copy the iovec data from userspace
947 */
948static long copy_from_user_iovec(void *buffer, const struct iovec *iov,
949 unsigned ioc)
950{
951 for (; ioc > 0; ioc--) {
952 if (copy_from_user(buffer, iov->iov_base, iov->iov_len) != 0)
953 return -EFAULT;
954 buffer += iov->iov_len;
955 iov++;
956 }
957 return 0;
958}
959
960/*
David Howells973c9f42011-01-20 16:38:33 +0000961 * Instantiate a key with the specified payload and link the key into the
962 * destination keyring if one is given.
963 *
964 * The caller must have the appropriate instantiation permit set for this to
965 * work (see keyctl_assume_authority). No other permissions are required.
966 *
967 * If successful, 0 will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 */
David Howellsee009e42011-03-07 15:06:20 +0000969long keyctl_instantiate_key_common(key_serial_t id,
970 const struct iovec *payload_iov,
971 unsigned ioc,
972 size_t plen,
973 key_serial_t ringid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974{
David Howellsd84f4f92008-11-14 10:39:23 +1100975 const struct cred *cred = current_cred();
David Howells3e301482005-06-23 22:00:56 -0700976 struct request_key_auth *rka;
David Howells8bbf49762008-11-14 10:39:14 +1100977 struct key *instkey, *dest_keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 void *payload;
979 long ret;
David Howells38bbca62008-04-29 01:01:19 -0700980 bool vm = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
David Howellsd84f4f92008-11-14 10:39:23 +1100982 kenter("%d,,%zu,%d", id, plen, ringid);
983
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 ret = -EINVAL;
David Howells38bbca62008-04-29 01:01:19 -0700985 if (plen > 1024 * 1024 - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 goto error;
987
David Howellsb5f545c2006-01-08 01:02:47 -0800988 /* the appropriate instantiation authorisation key must have been
989 * assumed before calling this */
990 ret = -EPERM;
David Howellsd84f4f92008-11-14 10:39:23 +1100991 instkey = cred->request_key_auth;
David Howellsb5f545c2006-01-08 01:02:47 -0800992 if (!instkey)
993 goto error;
994
995 rka = instkey->payload.data;
996 if (rka->target_key->serial != id)
997 goto error;
998
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 /* pull the payload in if one was supplied */
1000 payload = NULL;
1001
David Howellsee009e42011-03-07 15:06:20 +00001002 if (payload_iov) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 ret = -ENOMEM;
1004 payload = kmalloc(plen, GFP_KERNEL);
David Howells38bbca62008-04-29 01:01:19 -07001005 if (!payload) {
1006 if (plen <= PAGE_SIZE)
1007 goto error;
1008 vm = true;
1009 payload = vmalloc(plen);
1010 if (!payload)
1011 goto error;
1012 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
David Howellsee009e42011-03-07 15:06:20 +00001014 ret = copy_from_user_iovec(payload, payload_iov, ioc);
1015 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 goto error2;
1017 }
1018
David Howells3e301482005-06-23 22:00:56 -07001019 /* find the destination keyring amongst those belonging to the
1020 * requesting task */
David Howells8bbf49762008-11-14 10:39:14 +11001021 ret = get_instantiation_keyring(ringid, rka, &dest_keyring);
1022 if (ret < 0)
1023 goto error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
1025 /* instantiate the key and link it into a keyring */
David Howells3e301482005-06-23 22:00:56 -07001026 ret = key_instantiate_and_link(rka->target_key, payload, plen,
David Howells8bbf49762008-11-14 10:39:14 +11001027 dest_keyring, instkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
David Howells8bbf49762008-11-14 10:39:14 +11001029 key_put(dest_keyring);
David Howellsb5f545c2006-01-08 01:02:47 -08001030
1031 /* discard the assumed authority if it's just been disabled by
1032 * instantiation of the key */
David Howellsd84f4f92008-11-14 10:39:23 +11001033 if (ret == 0)
1034 keyctl_change_reqkey_auth(NULL);
David Howellsb5f545c2006-01-08 01:02:47 -08001035
1036error2:
David Howells38bbca62008-04-29 01:01:19 -07001037 if (!vm)
1038 kfree(payload);
1039 else
1040 vfree(payload);
David Howellsb5f545c2006-01-08 01:02:47 -08001041error:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +00001043}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045/*
David Howellsee009e42011-03-07 15:06:20 +00001046 * Instantiate a key with the specified payload and link the key into the
1047 * destination keyring if one is given.
1048 *
1049 * The caller must have the appropriate instantiation permit set for this to
1050 * work (see keyctl_assume_authority). No other permissions are required.
1051 *
1052 * If successful, 0 will be returned.
1053 */
1054long keyctl_instantiate_key(key_serial_t id,
1055 const void __user *_payload,
1056 size_t plen,
1057 key_serial_t ringid)
1058{
1059 if (_payload && plen) {
1060 struct iovec iov[1] = {
1061 [0].iov_base = (void __user *)_payload,
1062 [0].iov_len = plen
1063 };
1064
1065 return keyctl_instantiate_key_common(id, iov, 1, plen, ringid);
1066 }
1067
1068 return keyctl_instantiate_key_common(id, NULL, 0, 0, ringid);
1069}
1070
1071/*
1072 * Instantiate a key with the specified multipart payload and link the key into
1073 * the destination keyring if one is given.
1074 *
1075 * The caller must have the appropriate instantiation permit set for this to
1076 * work (see keyctl_assume_authority). No other permissions are required.
1077 *
1078 * If successful, 0 will be returned.
1079 */
1080long keyctl_instantiate_key_iov(key_serial_t id,
1081 const struct iovec __user *_payload_iov,
1082 unsigned ioc,
1083 key_serial_t ringid)
1084{
1085 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
1086 long ret;
1087
1088 if (_payload_iov == 0 || ioc == 0)
1089 goto no_payload;
1090
1091 ret = rw_copy_check_uvector(WRITE, _payload_iov, ioc,
Christopher Yeohfcf63402011-10-31 17:06:39 -07001092 ARRAY_SIZE(iovstack), iovstack, &iov, 1);
David Howellsee009e42011-03-07 15:06:20 +00001093 if (ret < 0)
Alan Cox829089b2012-09-28 12:20:02 +01001094 goto err;
David Howellsee009e42011-03-07 15:06:20 +00001095 if (ret == 0)
1096 goto no_payload_free;
1097
1098 ret = keyctl_instantiate_key_common(id, iov, ioc, ret, ringid);
Alan Cox829089b2012-09-28 12:20:02 +01001099err:
David Howellsee009e42011-03-07 15:06:20 +00001100 if (iov != iovstack)
1101 kfree(iov);
1102 return ret;
1103
1104no_payload_free:
1105 if (iov != iovstack)
1106 kfree(iov);
1107no_payload:
1108 return keyctl_instantiate_key_common(id, NULL, 0, 0, ringid);
1109}
1110
1111/*
David Howells973c9f42011-01-20 16:38:33 +00001112 * Negatively instantiate the key with the given timeout (in seconds) and link
1113 * the key into the destination keyring if one is given.
1114 *
1115 * The caller must have the appropriate instantiation permit set for this to
1116 * work (see keyctl_assume_authority). No other permissions are required.
1117 *
1118 * The key and any links to the key will be automatically garbage collected
1119 * after the timeout expires.
1120 *
1121 * Negative keys are used to rate limit repeated request_key() calls by causing
1122 * them to return -ENOKEY until the negative key expires.
1123 *
1124 * If successful, 0 will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 */
1126long keyctl_negate_key(key_serial_t id, unsigned timeout, key_serial_t ringid)
1127{
David Howellsfdd1b942011-03-07 15:06:09 +00001128 return keyctl_reject_key(id, timeout, ENOKEY, ringid);
1129}
1130
1131/*
1132 * Negatively instantiate the key with the given timeout (in seconds) and error
1133 * code and link the key into the destination keyring if one is given.
1134 *
1135 * The caller must have the appropriate instantiation permit set for this to
1136 * work (see keyctl_assume_authority). No other permissions are required.
1137 *
1138 * The key and any links to the key will be automatically garbage collected
1139 * after the timeout expires.
1140 *
1141 * Negative keys are used to rate limit repeated request_key() calls by causing
1142 * them to return the specified error code until the negative key expires.
1143 *
1144 * If successful, 0 will be returned.
1145 */
1146long keyctl_reject_key(key_serial_t id, unsigned timeout, unsigned error,
1147 key_serial_t ringid)
1148{
David Howellsd84f4f92008-11-14 10:39:23 +11001149 const struct cred *cred = current_cred();
David Howells3e301482005-06-23 22:00:56 -07001150 struct request_key_auth *rka;
David Howells8bbf49762008-11-14 10:39:14 +11001151 struct key *instkey, *dest_keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 long ret;
1153
David Howellsfdd1b942011-03-07 15:06:09 +00001154 kenter("%d,%u,%u,%d", id, timeout, error, ringid);
1155
1156 /* must be a valid error code and mustn't be a kernel special */
1157 if (error <= 0 ||
1158 error >= MAX_ERRNO ||
1159 error == ERESTARTSYS ||
1160 error == ERESTARTNOINTR ||
1161 error == ERESTARTNOHAND ||
1162 error == ERESTART_RESTARTBLOCK)
1163 return -EINVAL;
David Howellsd84f4f92008-11-14 10:39:23 +11001164
David Howellsb5f545c2006-01-08 01:02:47 -08001165 /* the appropriate instantiation authorisation key must have been
1166 * assumed before calling this */
1167 ret = -EPERM;
David Howellsd84f4f92008-11-14 10:39:23 +11001168 instkey = cred->request_key_auth;
David Howellsb5f545c2006-01-08 01:02:47 -08001169 if (!instkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
David Howells3e301482005-06-23 22:00:56 -07001172 rka = instkey->payload.data;
David Howellsb5f545c2006-01-08 01:02:47 -08001173 if (rka->target_key->serial != id)
1174 goto error;
David Howells3e301482005-06-23 22:00:56 -07001175
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 /* find the destination keyring if present (which must also be
1177 * writable) */
David Howells8bbf49762008-11-14 10:39:14 +11001178 ret = get_instantiation_keyring(ringid, rka, &dest_keyring);
1179 if (ret < 0)
1180 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
1182 /* instantiate the key and link it into a keyring */
David Howellsfdd1b942011-03-07 15:06:09 +00001183 ret = key_reject_and_link(rka->target_key, timeout, error,
David Howells8bbf49762008-11-14 10:39:14 +11001184 dest_keyring, instkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
David Howells8bbf49762008-11-14 10:39:14 +11001186 key_put(dest_keyring);
David Howellsb5f545c2006-01-08 01:02:47 -08001187
1188 /* discard the assumed authority if it's just been disabled by
1189 * instantiation of the key */
David Howellsd84f4f92008-11-14 10:39:23 +11001190 if (ret == 0)
1191 keyctl_change_reqkey_auth(NULL);
David Howellsb5f545c2006-01-08 01:02:47 -08001192
1193error:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +00001195}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197/*
David Howells973c9f42011-01-20 16:38:33 +00001198 * Read or set the default keyring in which request_key() will cache keys and
1199 * return the old setting.
1200 *
Eric Biggers379fa512017-04-18 15:31:09 +01001201 * If a thread or process keyring is specified then it will be created if it
1202 * doesn't yet exist. The old setting will be returned if successful.
David Howells3e301482005-06-23 22:00:56 -07001203 */
1204long keyctl_set_reqkey_keyring(int reqkey_defl)
1205{
David Howellsd84f4f92008-11-14 10:39:23 +11001206 struct cred *new;
1207 int ret, old_setting;
1208
1209 old_setting = current_cred_xxx(jit_keyring);
1210
1211 if (reqkey_defl == KEY_REQKEY_DEFL_NO_CHANGE)
1212 return old_setting;
1213
1214 new = prepare_creds();
1215 if (!new)
1216 return -ENOMEM;
David Howells3e301482005-06-23 22:00:56 -07001217
1218 switch (reqkey_defl) {
1219 case KEY_REQKEY_DEFL_THREAD_KEYRING:
David Howellsd84f4f92008-11-14 10:39:23 +11001220 ret = install_thread_keyring_to_cred(new);
David Howells3e301482005-06-23 22:00:56 -07001221 if (ret < 0)
David Howellsd84f4f92008-11-14 10:39:23 +11001222 goto error;
David Howells3e301482005-06-23 22:00:56 -07001223 goto set;
1224
1225 case KEY_REQKEY_DEFL_PROCESS_KEYRING:
David Howellsd84f4f92008-11-14 10:39:23 +11001226 ret = install_process_keyring_to_cred(new);
Eric Biggers379fa512017-04-18 15:31:09 +01001227 if (ret < 0)
1228 goto error;
David Howellsd84f4f92008-11-14 10:39:23 +11001229 goto set;
David Howells3e301482005-06-23 22:00:56 -07001230
1231 case KEY_REQKEY_DEFL_DEFAULT:
1232 case KEY_REQKEY_DEFL_SESSION_KEYRING:
1233 case KEY_REQKEY_DEFL_USER_KEYRING:
1234 case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
David Howellsd84f4f92008-11-14 10:39:23 +11001235 case KEY_REQKEY_DEFL_REQUESTOR_KEYRING:
1236 goto set;
David Howells3e301482005-06-23 22:00:56 -07001237
1238 case KEY_REQKEY_DEFL_NO_CHANGE:
David Howells3e301482005-06-23 22:00:56 -07001239 case KEY_REQKEY_DEFL_GROUP_KEYRING:
1240 default:
David Howellsd84f4f92008-11-14 10:39:23 +11001241 ret = -EINVAL;
1242 goto error;
David Howells3e301482005-06-23 22:00:56 -07001243 }
1244
David Howellsd84f4f92008-11-14 10:39:23 +11001245set:
1246 new->jit_keyring = reqkey_defl;
1247 commit_creds(new);
1248 return old_setting;
1249error:
1250 abort_creds(new);
Dan Carpenter4303ef12010-06-11 17:30:05 +01001251 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +00001252}
David Howellsd84f4f92008-11-14 10:39:23 +11001253
David Howells3e301482005-06-23 22:00:56 -07001254/*
David Howells973c9f42011-01-20 16:38:33 +00001255 * Set or clear the timeout on a key.
1256 *
1257 * Either the key must grant the caller Setattr permission or else the caller
1258 * must hold an instantiation authorisation token for the key.
1259 *
1260 * The timeout is either 0 to clear the timeout, or a number of seconds from
1261 * the current time. The key and any links to the key will be automatically
1262 * garbage collected after the timeout expires.
1263 *
1264 * If successful, 0 is returned.
David Howells017679c2006-01-08 01:02:43 -08001265 */
1266long keyctl_set_timeout(key_serial_t id, unsigned timeout)
1267{
David Howells91562352010-06-11 17:31:05 +01001268 struct key *key, *instkey;
David Howells017679c2006-01-08 01:02:43 -08001269 key_ref_t key_ref;
David Howells017679c2006-01-08 01:02:43 -08001270 long ret;
1271
David Howells55931222009-09-02 09:13:45 +01001272 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
1273 KEY_SETATTR);
David Howells017679c2006-01-08 01:02:43 -08001274 if (IS_ERR(key_ref)) {
David Howells91562352010-06-11 17:31:05 +01001275 /* setting the timeout on a key under construction is permitted
1276 * if we have the authorisation token handy */
1277 if (PTR_ERR(key_ref) == -EACCES) {
1278 instkey = key_get_instantiation_authkey(id);
1279 if (!IS_ERR(instkey)) {
1280 key_put(instkey);
1281 key_ref = lookup_user_key(id,
1282 KEY_LOOKUP_PARTIAL,
1283 0);
1284 if (!IS_ERR(key_ref))
1285 goto okay;
1286 }
1287 }
1288
David Howells017679c2006-01-08 01:02:43 -08001289 ret = PTR_ERR(key_ref);
1290 goto error;
1291 }
1292
David Howells91562352010-06-11 17:31:05 +01001293okay:
David Howells017679c2006-01-08 01:02:43 -08001294 key = key_ref_to_ptr(key_ref);
Bryan Schumaker59e6b9c2012-02-24 14:14:50 -05001295 key_set_timeout(key, timeout);
David Howells017679c2006-01-08 01:02:43 -08001296 key_put(key);
1297
1298 ret = 0;
1299error:
1300 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +00001301}
David Howells017679c2006-01-08 01:02:43 -08001302
David Howells017679c2006-01-08 01:02:43 -08001303/*
David Howells973c9f42011-01-20 16:38:33 +00001304 * Assume (or clear) the authority to instantiate the specified key.
1305 *
1306 * This sets the authoritative token currently in force for key instantiation.
1307 * This must be done for a key to be instantiated. It has the effect of making
1308 * available all the keys from the caller of the request_key() that created a
1309 * key to request_key() calls made by the caller of this function.
1310 *
1311 * The caller must have the instantiation key in their process keyrings with a
1312 * Search permission grant available to the caller.
1313 *
1314 * If the ID given is 0, then the setting will be cleared and 0 returned.
1315 *
1316 * If the ID given has a matching an authorisation key, then that key will be
1317 * set and its ID will be returned. The authorisation key can be read to get
1318 * the callout information passed to request_key().
David Howellsb5f545c2006-01-08 01:02:47 -08001319 */
1320long keyctl_assume_authority(key_serial_t id)
1321{
1322 struct key *authkey;
1323 long ret;
1324
1325 /* special key IDs aren't permitted */
1326 ret = -EINVAL;
1327 if (id < 0)
1328 goto error;
1329
1330 /* we divest ourselves of authority if given an ID of 0 */
1331 if (id == 0) {
David Howellsd84f4f92008-11-14 10:39:23 +11001332 ret = keyctl_change_reqkey_auth(NULL);
David Howellsb5f545c2006-01-08 01:02:47 -08001333 goto error;
1334 }
1335
1336 /* attempt to assume the authority temporarily granted to us whilst we
1337 * instantiate the specified key
1338 * - the authorisation key must be in the current task's keyrings
1339 * somewhere
1340 */
1341 authkey = key_get_instantiation_authkey(id);
1342 if (IS_ERR(authkey)) {
1343 ret = PTR_ERR(authkey);
1344 goto error;
1345 }
1346
David Howellsd84f4f92008-11-14 10:39:23 +11001347 ret = keyctl_change_reqkey_auth(authkey);
1348 if (ret < 0)
1349 goto error;
1350 key_put(authkey);
David Howellsb5f545c2006-01-08 01:02:47 -08001351
David Howellsd84f4f92008-11-14 10:39:23 +11001352 ret = authkey->serial;
David Howellsb5f545c2006-01-08 01:02:47 -08001353error:
1354 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +00001355}
David Howellsb5f545c2006-01-08 01:02:47 -08001356
David Howells70a5bb72008-04-29 01:01:26 -07001357/*
David Howells973c9f42011-01-20 16:38:33 +00001358 * Get a key's the LSM security label.
1359 *
1360 * The key must grant the caller View permission for this to work.
1361 *
1362 * If there's a buffer, then up to buflen bytes of data will be placed into it.
1363 *
1364 * If successful, the amount of information available will be returned,
1365 * irrespective of how much was copied (including the terminal NUL).
David Howells70a5bb72008-04-29 01:01:26 -07001366 */
1367long keyctl_get_security(key_serial_t keyid,
1368 char __user *buffer,
1369 size_t buflen)
1370{
1371 struct key *key, *instkey;
1372 key_ref_t key_ref;
1373 char *context;
1374 long ret;
1375
David Howells55931222009-09-02 09:13:45 +01001376 key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_VIEW);
David Howells70a5bb72008-04-29 01:01:26 -07001377 if (IS_ERR(key_ref)) {
1378 if (PTR_ERR(key_ref) != -EACCES)
1379 return PTR_ERR(key_ref);
1380
1381 /* viewing a key under construction is also permitted if we
1382 * have the authorisation token handy */
1383 instkey = key_get_instantiation_authkey(keyid);
1384 if (IS_ERR(instkey))
Roel Kluinfa1cc7b2009-12-15 15:05:12 -08001385 return PTR_ERR(instkey);
David Howells70a5bb72008-04-29 01:01:26 -07001386 key_put(instkey);
1387
David Howells55931222009-09-02 09:13:45 +01001388 key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, 0);
David Howells70a5bb72008-04-29 01:01:26 -07001389 if (IS_ERR(key_ref))
1390 return PTR_ERR(key_ref);
1391 }
1392
1393 key = key_ref_to_ptr(key_ref);
1394 ret = security_key_getsecurity(key, &context);
1395 if (ret == 0) {
1396 /* if no information was returned, give userspace an empty
1397 * string */
1398 ret = 1;
1399 if (buffer && buflen > 0 &&
1400 copy_to_user(buffer, "", 1) != 0)
1401 ret = -EFAULT;
1402 } else if (ret > 0) {
1403 /* return as much data as there's room for */
1404 if (buffer && buflen > 0) {
1405 if (buflen > ret)
1406 buflen = ret;
1407
1408 if (copy_to_user(buffer, context, buflen) != 0)
1409 ret = -EFAULT;
1410 }
1411
1412 kfree(context);
1413 }
1414
1415 key_ref_put(key_ref);
1416 return ret;
1417}
1418
David Howellsee18d642009-09-02 09:14:21 +01001419/*
David Howells973c9f42011-01-20 16:38:33 +00001420 * Attempt to install the calling process's session keyring on the process's
1421 * parent process.
1422 *
1423 * The keyring must exist and must grant the caller LINK permission, and the
1424 * parent process must be single-threaded and must have the same effective
1425 * ownership as this process and mustn't be SUID/SGID.
1426 *
1427 * The keyring will be emplaced on the parent when it next resumes userspace.
1428 *
1429 * If successful, 0 will be returned.
David Howellsee18d642009-09-02 09:14:21 +01001430 */
1431long keyctl_session_to_parent(void)
1432{
Geert Uytterhoevena00ae4d2009-12-13 20:21:34 +01001433#ifdef TIF_NOTIFY_RESUME
David Howellsee18d642009-09-02 09:14:21 +01001434 struct task_struct *me, *parent;
1435 const struct cred *mycred, *pcred;
1436 struct cred *cred, *oldcred;
1437 key_ref_t keyring_r;
1438 int ret;
1439
1440 keyring_r = lookup_user_key(KEY_SPEC_SESSION_KEYRING, 0, KEY_LINK);
1441 if (IS_ERR(keyring_r))
1442 return PTR_ERR(keyring_r);
1443
1444 /* our parent is going to need a new cred struct, a new tgcred struct
1445 * and new security data, so we allocate them here to prevent ENOMEM in
1446 * our parent */
1447 ret = -ENOMEM;
1448 cred = cred_alloc_blank();
1449 if (!cred)
1450 goto error_keyring;
1451
1452 cred->tgcred->session_keyring = key_ref_to_ptr(keyring_r);
1453 keyring_r = NULL;
1454
1455 me = current;
David Howells9d1ac652010-09-10 09:59:46 +01001456 rcu_read_lock();
David Howellsee18d642009-09-02 09:14:21 +01001457 write_lock_irq(&tasklist_lock);
1458
1459 parent = me->real_parent;
1460 ret = -EPERM;
1461
1462 /* the parent mustn't be init and mustn't be a kernel thread */
1463 if (parent->pid <= 1 || !parent->mm)
1464 goto not_permitted;
1465
1466 /* the parent must be single threaded */
Oleg Nesterovdd98acf2010-05-26 14:43:23 -07001467 if (!thread_group_empty(parent))
David Howellsee18d642009-09-02 09:14:21 +01001468 goto not_permitted;
1469
1470 /* the parent and the child must have different session keyrings or
1471 * there's no point */
1472 mycred = current_cred();
1473 pcred = __task_cred(parent);
1474 if (mycred == pcred ||
1475 mycred->tgcred->session_keyring == pcred->tgcred->session_keyring)
1476 goto already_same;
1477
1478 /* the parent must have the same effective ownership and mustn't be
1479 * SUID/SGID */
Justin P. Mattockc5b60b52010-04-21 00:02:11 -07001480 if (pcred->uid != mycred->euid ||
David Howellsee18d642009-09-02 09:14:21 +01001481 pcred->euid != mycred->euid ||
1482 pcred->suid != mycred->euid ||
Justin P. Mattockc5b60b52010-04-21 00:02:11 -07001483 pcred->gid != mycred->egid ||
David Howellsee18d642009-09-02 09:14:21 +01001484 pcred->egid != mycred->egid ||
1485 pcred->sgid != mycred->egid)
1486 goto not_permitted;
1487
1488 /* the keyrings must have the same UID */
David Howells3d964062010-09-10 09:59:51 +01001489 if ((pcred->tgcred->session_keyring &&
1490 pcred->tgcred->session_keyring->uid != mycred->euid) ||
David Howellsee18d642009-09-02 09:14:21 +01001491 mycred->tgcred->session_keyring->uid != mycred->euid)
1492 goto not_permitted;
1493
David Howellsee18d642009-09-02 09:14:21 +01001494 /* if there's an already pending keyring replacement, then we replace
1495 * that */
1496 oldcred = parent->replacement_session_keyring;
1497
1498 /* the replacement session keyring is applied just prior to userspace
1499 * restarting */
1500 parent->replacement_session_keyring = cred;
1501 cred = NULL;
1502 set_ti_thread_flag(task_thread_info(parent), TIF_NOTIFY_RESUME);
1503
1504 write_unlock_irq(&tasklist_lock);
David Howells9d1ac652010-09-10 09:59:46 +01001505 rcu_read_unlock();
David Howellsee18d642009-09-02 09:14:21 +01001506 if (oldcred)
1507 put_cred(oldcred);
1508 return 0;
1509
1510already_same:
1511 ret = 0;
1512not_permitted:
Marc Dionne5c843422009-09-14 12:46:23 +01001513 write_unlock_irq(&tasklist_lock);
David Howells9d1ac652010-09-10 09:59:46 +01001514 rcu_read_unlock();
David Howellsee18d642009-09-02 09:14:21 +01001515 put_cred(cred);
1516 return ret;
1517
1518error_keyring:
1519 key_ref_put(keyring_r);
1520 return ret;
Geert Uytterhoevena00ae4d2009-12-13 20:21:34 +01001521
1522#else /* !TIF_NOTIFY_RESUME */
1523 /*
1524 * To be removed when TIF_NOTIFY_RESUME has been implemented on
1525 * m68k/xtensa
1526 */
1527#warning TIF_NOTIFY_RESUME not implemented
1528 return -EOPNOTSUPP;
1529#endif /* !TIF_NOTIFY_RESUME */
David Howellsee18d642009-09-02 09:14:21 +01001530}
1531
David Howellsb5f545c2006-01-08 01:02:47 -08001532/*
David Howells973c9f42011-01-20 16:38:33 +00001533 * The key control system call
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 */
Heiko Carstens938bb9f2009-01-14 14:14:30 +01001535SYSCALL_DEFINE5(keyctl, int, option, unsigned long, arg2, unsigned long, arg3,
1536 unsigned long, arg4, unsigned long, arg5)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537{
1538 switch (option) {
1539 case KEYCTL_GET_KEYRING_ID:
1540 return keyctl_get_keyring_ID((key_serial_t) arg2,
1541 (int) arg3);
1542
1543 case KEYCTL_JOIN_SESSION_KEYRING:
1544 return keyctl_join_session_keyring((const char __user *) arg2);
1545
1546 case KEYCTL_UPDATE:
1547 return keyctl_update_key((key_serial_t) arg2,
1548 (const void __user *) arg3,
1549 (size_t) arg4);
1550
1551 case KEYCTL_REVOKE:
1552 return keyctl_revoke_key((key_serial_t) arg2);
1553
1554 case KEYCTL_DESCRIBE:
1555 return keyctl_describe_key((key_serial_t) arg2,
1556 (char __user *) arg3,
1557 (unsigned) arg4);
1558
1559 case KEYCTL_CLEAR:
1560 return keyctl_keyring_clear((key_serial_t) arg2);
1561
1562 case KEYCTL_LINK:
1563 return keyctl_keyring_link((key_serial_t) arg2,
1564 (key_serial_t) arg3);
1565
1566 case KEYCTL_UNLINK:
1567 return keyctl_keyring_unlink((key_serial_t) arg2,
1568 (key_serial_t) arg3);
1569
1570 case KEYCTL_SEARCH:
1571 return keyctl_keyring_search((key_serial_t) arg2,
1572 (const char __user *) arg3,
1573 (const char __user *) arg4,
1574 (key_serial_t) arg5);
1575
1576 case KEYCTL_READ:
1577 return keyctl_read_key((key_serial_t) arg2,
1578 (char __user *) arg3,
1579 (size_t) arg4);
1580
1581 case KEYCTL_CHOWN:
1582 return keyctl_chown_key((key_serial_t) arg2,
1583 (uid_t) arg3,
1584 (gid_t) arg4);
1585
1586 case KEYCTL_SETPERM:
1587 return keyctl_setperm_key((key_serial_t) arg2,
1588 (key_perm_t) arg3);
1589
1590 case KEYCTL_INSTANTIATE:
1591 return keyctl_instantiate_key((key_serial_t) arg2,
1592 (const void __user *) arg3,
1593 (size_t) arg4,
1594 (key_serial_t) arg5);
1595
1596 case KEYCTL_NEGATE:
1597 return keyctl_negate_key((key_serial_t) arg2,
1598 (unsigned) arg3,
1599 (key_serial_t) arg4);
1600
David Howells3e301482005-06-23 22:00:56 -07001601 case KEYCTL_SET_REQKEY_KEYRING:
1602 return keyctl_set_reqkey_keyring(arg2);
1603
David Howells017679c2006-01-08 01:02:43 -08001604 case KEYCTL_SET_TIMEOUT:
1605 return keyctl_set_timeout((key_serial_t) arg2,
1606 (unsigned) arg3);
1607
David Howellsb5f545c2006-01-08 01:02:47 -08001608 case KEYCTL_ASSUME_AUTHORITY:
1609 return keyctl_assume_authority((key_serial_t) arg2);
1610
David Howells70a5bb72008-04-29 01:01:26 -07001611 case KEYCTL_GET_SECURITY:
1612 return keyctl_get_security((key_serial_t) arg2,
James Morris90bd49a2008-12-29 14:35:35 +11001613 (char __user *) arg3,
David Howells70a5bb72008-04-29 01:01:26 -07001614 (size_t) arg4);
1615
David Howellsee18d642009-09-02 09:14:21 +01001616 case KEYCTL_SESSION_TO_PARENT:
1617 return keyctl_session_to_parent();
1618
David Howellsfdd1b942011-03-07 15:06:09 +00001619 case KEYCTL_REJECT:
1620 return keyctl_reject_key((key_serial_t) arg2,
1621 (unsigned) arg3,
1622 (unsigned) arg4,
1623 (key_serial_t) arg5);
1624
David Howellsee009e42011-03-07 15:06:20 +00001625 case KEYCTL_INSTANTIATE_IOV:
1626 return keyctl_instantiate_key_iov(
1627 (key_serial_t) arg2,
1628 (const struct iovec __user *) arg3,
1629 (unsigned) arg4,
1630 (key_serial_t) arg5);
1631
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 default:
1633 return -EOPNOTSUPP;
1634 }
David Howellsa8b17ed2011-01-20 16:38:27 +00001635}