1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /* Userspace key control operations
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Copyright (C) 2004-5 Red Hat, Inc. All Rights Reserved.
5*4882a593Smuzhiyun * Written by David Howells (dhowells@redhat.com)
6*4882a593Smuzhiyun */
7*4882a593Smuzhiyun
8*4882a593Smuzhiyun #include <linux/init.h>
9*4882a593Smuzhiyun #include <linux/sched.h>
10*4882a593Smuzhiyun #include <linux/sched/task.h>
11*4882a593Smuzhiyun #include <linux/slab.h>
12*4882a593Smuzhiyun #include <linux/syscalls.h>
13*4882a593Smuzhiyun #include <linux/key.h>
14*4882a593Smuzhiyun #include <linux/keyctl.h>
15*4882a593Smuzhiyun #include <linux/fs.h>
16*4882a593Smuzhiyun #include <linux/capability.h>
17*4882a593Smuzhiyun #include <linux/cred.h>
18*4882a593Smuzhiyun #include <linux/string.h>
19*4882a593Smuzhiyun #include <linux/err.h>
20*4882a593Smuzhiyun #include <linux/vmalloc.h>
21*4882a593Smuzhiyun #include <linux/security.h>
22*4882a593Smuzhiyun #include <linux/uio.h>
23*4882a593Smuzhiyun #include <linux/uaccess.h>
24*4882a593Smuzhiyun #include <keys/request_key_auth-type.h>
25*4882a593Smuzhiyun #include "internal.h"
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun #define KEY_MAX_DESC_SIZE 4096
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun static const unsigned char keyrings_capabilities[2] = {
30*4882a593Smuzhiyun [0] = (KEYCTL_CAPS0_CAPABILITIES |
31*4882a593Smuzhiyun (IS_ENABLED(CONFIG_PERSISTENT_KEYRINGS) ? KEYCTL_CAPS0_PERSISTENT_KEYRINGS : 0) |
32*4882a593Smuzhiyun (IS_ENABLED(CONFIG_KEY_DH_OPERATIONS) ? KEYCTL_CAPS0_DIFFIE_HELLMAN : 0) |
33*4882a593Smuzhiyun (IS_ENABLED(CONFIG_ASYMMETRIC_KEY_TYPE) ? KEYCTL_CAPS0_PUBLIC_KEY : 0) |
34*4882a593Smuzhiyun (IS_ENABLED(CONFIG_BIG_KEYS) ? KEYCTL_CAPS0_BIG_KEY : 0) |
35*4882a593Smuzhiyun KEYCTL_CAPS0_INVALIDATE |
36*4882a593Smuzhiyun KEYCTL_CAPS0_RESTRICT_KEYRING |
37*4882a593Smuzhiyun KEYCTL_CAPS0_MOVE
38*4882a593Smuzhiyun ),
39*4882a593Smuzhiyun [1] = (KEYCTL_CAPS1_NS_KEYRING_NAME |
40*4882a593Smuzhiyun KEYCTL_CAPS1_NS_KEY_TAG |
41*4882a593Smuzhiyun (IS_ENABLED(CONFIG_KEY_NOTIFICATIONS) ? KEYCTL_CAPS1_NOTIFICATIONS : 0)
42*4882a593Smuzhiyun ),
43*4882a593Smuzhiyun };
44*4882a593Smuzhiyun
key_get_type_from_user(char * type,const char __user * _type,unsigned len)45*4882a593Smuzhiyun static int key_get_type_from_user(char *type,
46*4882a593Smuzhiyun const char __user *_type,
47*4882a593Smuzhiyun unsigned len)
48*4882a593Smuzhiyun {
49*4882a593Smuzhiyun int ret;
50*4882a593Smuzhiyun
51*4882a593Smuzhiyun ret = strncpy_from_user(type, _type, len);
52*4882a593Smuzhiyun if (ret < 0)
53*4882a593Smuzhiyun return ret;
54*4882a593Smuzhiyun if (ret == 0 || ret >= len)
55*4882a593Smuzhiyun return -EINVAL;
56*4882a593Smuzhiyun if (type[0] == '.')
57*4882a593Smuzhiyun return -EPERM;
58*4882a593Smuzhiyun type[len - 1] = '\0';
59*4882a593Smuzhiyun return 0;
60*4882a593Smuzhiyun }
61*4882a593Smuzhiyun
62*4882a593Smuzhiyun /*
63*4882a593Smuzhiyun * Extract the description of a new key from userspace and either add it as a
64*4882a593Smuzhiyun * new key to the specified keyring or update a matching key in that keyring.
65*4882a593Smuzhiyun *
66*4882a593Smuzhiyun * If the description is NULL or an empty string, the key type is asked to
67*4882a593Smuzhiyun * generate one from the payload.
68*4882a593Smuzhiyun *
69*4882a593Smuzhiyun * The keyring must be writable so that we can attach the key to it.
70*4882a593Smuzhiyun *
71*4882a593Smuzhiyun * If successful, the new key's serial number is returned, otherwise an error
72*4882a593Smuzhiyun * code is returned.
73*4882a593Smuzhiyun */
SYSCALL_DEFINE5(add_key,const char __user *,_type,const char __user *,_description,const void __user *,_payload,size_t,plen,key_serial_t,ringid)74*4882a593Smuzhiyun SYSCALL_DEFINE5(add_key, const char __user *, _type,
75*4882a593Smuzhiyun const char __user *, _description,
76*4882a593Smuzhiyun const void __user *, _payload,
77*4882a593Smuzhiyun size_t, plen,
78*4882a593Smuzhiyun key_serial_t, ringid)
79*4882a593Smuzhiyun {
80*4882a593Smuzhiyun key_ref_t keyring_ref, key_ref;
81*4882a593Smuzhiyun char type[32], *description;
82*4882a593Smuzhiyun void *payload;
83*4882a593Smuzhiyun long ret;
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun ret = -EINVAL;
86*4882a593Smuzhiyun if (plen > 1024 * 1024 - 1)
87*4882a593Smuzhiyun goto error;
88*4882a593Smuzhiyun
89*4882a593Smuzhiyun /* draw all the data into kernel space */
90*4882a593Smuzhiyun ret = key_get_type_from_user(type, _type, sizeof(type));
91*4882a593Smuzhiyun if (ret < 0)
92*4882a593Smuzhiyun goto error;
93*4882a593Smuzhiyun
94*4882a593Smuzhiyun description = NULL;
95*4882a593Smuzhiyun if (_description) {
96*4882a593Smuzhiyun description = strndup_user(_description, KEY_MAX_DESC_SIZE);
97*4882a593Smuzhiyun if (IS_ERR(description)) {
98*4882a593Smuzhiyun ret = PTR_ERR(description);
99*4882a593Smuzhiyun goto error;
100*4882a593Smuzhiyun }
101*4882a593Smuzhiyun if (!*description) {
102*4882a593Smuzhiyun kfree(description);
103*4882a593Smuzhiyun description = NULL;
104*4882a593Smuzhiyun } else if ((description[0] == '.') &&
105*4882a593Smuzhiyun (strncmp(type, "keyring", 7) == 0)) {
106*4882a593Smuzhiyun ret = -EPERM;
107*4882a593Smuzhiyun goto error2;
108*4882a593Smuzhiyun }
109*4882a593Smuzhiyun }
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun /* pull the payload in if one was supplied */
112*4882a593Smuzhiyun payload = NULL;
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun if (plen) {
115*4882a593Smuzhiyun ret = -ENOMEM;
116*4882a593Smuzhiyun payload = kvmalloc(plen, GFP_KERNEL);
117*4882a593Smuzhiyun if (!payload)
118*4882a593Smuzhiyun goto error2;
119*4882a593Smuzhiyun
120*4882a593Smuzhiyun ret = -EFAULT;
121*4882a593Smuzhiyun if (copy_from_user(payload, _payload, plen) != 0)
122*4882a593Smuzhiyun goto error3;
123*4882a593Smuzhiyun }
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun /* find the target keyring (which must be writable) */
126*4882a593Smuzhiyun keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
127*4882a593Smuzhiyun if (IS_ERR(keyring_ref)) {
128*4882a593Smuzhiyun ret = PTR_ERR(keyring_ref);
129*4882a593Smuzhiyun goto error3;
130*4882a593Smuzhiyun }
131*4882a593Smuzhiyun
132*4882a593Smuzhiyun /* create or update the requested key and add it to the target
133*4882a593Smuzhiyun * keyring */
134*4882a593Smuzhiyun key_ref = key_create_or_update(keyring_ref, type, description,
135*4882a593Smuzhiyun payload, plen, KEY_PERM_UNDEF,
136*4882a593Smuzhiyun KEY_ALLOC_IN_QUOTA);
137*4882a593Smuzhiyun if (!IS_ERR(key_ref)) {
138*4882a593Smuzhiyun ret = key_ref_to_ptr(key_ref)->serial;
139*4882a593Smuzhiyun key_ref_put(key_ref);
140*4882a593Smuzhiyun }
141*4882a593Smuzhiyun else {
142*4882a593Smuzhiyun ret = PTR_ERR(key_ref);
143*4882a593Smuzhiyun }
144*4882a593Smuzhiyun
145*4882a593Smuzhiyun key_ref_put(keyring_ref);
146*4882a593Smuzhiyun error3:
147*4882a593Smuzhiyun kvfree_sensitive(payload, plen);
148*4882a593Smuzhiyun error2:
149*4882a593Smuzhiyun kfree(description);
150*4882a593Smuzhiyun error:
151*4882a593Smuzhiyun return ret;
152*4882a593Smuzhiyun }
153*4882a593Smuzhiyun
154*4882a593Smuzhiyun /*
155*4882a593Smuzhiyun * Search the process keyrings and keyring trees linked from those for a
156*4882a593Smuzhiyun * matching key. Keyrings must have appropriate Search permission to be
157*4882a593Smuzhiyun * searched.
158*4882a593Smuzhiyun *
159*4882a593Smuzhiyun * If a key is found, it will be attached to the destination keyring if there's
160*4882a593Smuzhiyun * one specified and the serial number of the key will be returned.
161*4882a593Smuzhiyun *
162*4882a593Smuzhiyun * If no key is found, /sbin/request-key will be invoked if _callout_info is
163*4882a593Smuzhiyun * non-NULL in an attempt to create a key. The _callout_info string will be
164*4882a593Smuzhiyun * passed to /sbin/request-key to aid with completing the request. If the
165*4882a593Smuzhiyun * _callout_info string is "" then it will be changed to "-".
166*4882a593Smuzhiyun */
SYSCALL_DEFINE4(request_key,const char __user *,_type,const char __user *,_description,const char __user *,_callout_info,key_serial_t,destringid)167*4882a593Smuzhiyun SYSCALL_DEFINE4(request_key, const char __user *, _type,
168*4882a593Smuzhiyun const char __user *, _description,
169*4882a593Smuzhiyun const char __user *, _callout_info,
170*4882a593Smuzhiyun key_serial_t, destringid)
171*4882a593Smuzhiyun {
172*4882a593Smuzhiyun struct key_type *ktype;
173*4882a593Smuzhiyun struct key *key;
174*4882a593Smuzhiyun key_ref_t dest_ref;
175*4882a593Smuzhiyun size_t callout_len;
176*4882a593Smuzhiyun char type[32], *description, *callout_info;
177*4882a593Smuzhiyun long ret;
178*4882a593Smuzhiyun
179*4882a593Smuzhiyun /* pull the type into kernel space */
180*4882a593Smuzhiyun ret = key_get_type_from_user(type, _type, sizeof(type));
181*4882a593Smuzhiyun if (ret < 0)
182*4882a593Smuzhiyun goto error;
183*4882a593Smuzhiyun
184*4882a593Smuzhiyun /* pull the description into kernel space */
185*4882a593Smuzhiyun description = strndup_user(_description, KEY_MAX_DESC_SIZE);
186*4882a593Smuzhiyun if (IS_ERR(description)) {
187*4882a593Smuzhiyun ret = PTR_ERR(description);
188*4882a593Smuzhiyun goto error;
189*4882a593Smuzhiyun }
190*4882a593Smuzhiyun
191*4882a593Smuzhiyun /* pull the callout info into kernel space */
192*4882a593Smuzhiyun callout_info = NULL;
193*4882a593Smuzhiyun callout_len = 0;
194*4882a593Smuzhiyun if (_callout_info) {
195*4882a593Smuzhiyun callout_info = strndup_user(_callout_info, PAGE_SIZE);
196*4882a593Smuzhiyun if (IS_ERR(callout_info)) {
197*4882a593Smuzhiyun ret = PTR_ERR(callout_info);
198*4882a593Smuzhiyun goto error2;
199*4882a593Smuzhiyun }
200*4882a593Smuzhiyun callout_len = strlen(callout_info);
201*4882a593Smuzhiyun }
202*4882a593Smuzhiyun
203*4882a593Smuzhiyun /* get the destination keyring if specified */
204*4882a593Smuzhiyun dest_ref = NULL;
205*4882a593Smuzhiyun if (destringid) {
206*4882a593Smuzhiyun dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE,
207*4882a593Smuzhiyun KEY_NEED_WRITE);
208*4882a593Smuzhiyun if (IS_ERR(dest_ref)) {
209*4882a593Smuzhiyun ret = PTR_ERR(dest_ref);
210*4882a593Smuzhiyun goto error3;
211*4882a593Smuzhiyun }
212*4882a593Smuzhiyun }
213*4882a593Smuzhiyun
214*4882a593Smuzhiyun /* find the key type */
215*4882a593Smuzhiyun ktype = key_type_lookup(type);
216*4882a593Smuzhiyun if (IS_ERR(ktype)) {
217*4882a593Smuzhiyun ret = PTR_ERR(ktype);
218*4882a593Smuzhiyun goto error4;
219*4882a593Smuzhiyun }
220*4882a593Smuzhiyun
221*4882a593Smuzhiyun /* do the search */
222*4882a593Smuzhiyun key = request_key_and_link(ktype, description, NULL, callout_info,
223*4882a593Smuzhiyun callout_len, NULL, key_ref_to_ptr(dest_ref),
224*4882a593Smuzhiyun KEY_ALLOC_IN_QUOTA);
225*4882a593Smuzhiyun if (IS_ERR(key)) {
226*4882a593Smuzhiyun ret = PTR_ERR(key);
227*4882a593Smuzhiyun goto error5;
228*4882a593Smuzhiyun }
229*4882a593Smuzhiyun
230*4882a593Smuzhiyun /* wait for the key to finish being constructed */
231*4882a593Smuzhiyun ret = wait_for_key_construction(key, 1);
232*4882a593Smuzhiyun if (ret < 0)
233*4882a593Smuzhiyun goto error6;
234*4882a593Smuzhiyun
235*4882a593Smuzhiyun ret = key->serial;
236*4882a593Smuzhiyun
237*4882a593Smuzhiyun error6:
238*4882a593Smuzhiyun key_put(key);
239*4882a593Smuzhiyun error5:
240*4882a593Smuzhiyun key_type_put(ktype);
241*4882a593Smuzhiyun error4:
242*4882a593Smuzhiyun key_ref_put(dest_ref);
243*4882a593Smuzhiyun error3:
244*4882a593Smuzhiyun kfree(callout_info);
245*4882a593Smuzhiyun error2:
246*4882a593Smuzhiyun kfree(description);
247*4882a593Smuzhiyun error:
248*4882a593Smuzhiyun return ret;
249*4882a593Smuzhiyun }
250*4882a593Smuzhiyun
251*4882a593Smuzhiyun /*
252*4882a593Smuzhiyun * Get the ID of the specified process keyring.
253*4882a593Smuzhiyun *
254*4882a593Smuzhiyun * The requested keyring must have search permission to be found.
255*4882a593Smuzhiyun *
256*4882a593Smuzhiyun * If successful, the ID of the requested keyring will be returned.
257*4882a593Smuzhiyun */
keyctl_get_keyring_ID(key_serial_t id,int create)258*4882a593Smuzhiyun long keyctl_get_keyring_ID(key_serial_t id, int create)
259*4882a593Smuzhiyun {
260*4882a593Smuzhiyun key_ref_t key_ref;
261*4882a593Smuzhiyun unsigned long lflags;
262*4882a593Smuzhiyun long ret;
263*4882a593Smuzhiyun
264*4882a593Smuzhiyun lflags = create ? KEY_LOOKUP_CREATE : 0;
265*4882a593Smuzhiyun key_ref = lookup_user_key(id, lflags, KEY_NEED_SEARCH);
266*4882a593Smuzhiyun if (IS_ERR(key_ref)) {
267*4882a593Smuzhiyun ret = PTR_ERR(key_ref);
268*4882a593Smuzhiyun goto error;
269*4882a593Smuzhiyun }
270*4882a593Smuzhiyun
271*4882a593Smuzhiyun ret = key_ref_to_ptr(key_ref)->serial;
272*4882a593Smuzhiyun key_ref_put(key_ref);
273*4882a593Smuzhiyun error:
274*4882a593Smuzhiyun return ret;
275*4882a593Smuzhiyun }
276*4882a593Smuzhiyun
277*4882a593Smuzhiyun /*
278*4882a593Smuzhiyun * Join a (named) session keyring.
279*4882a593Smuzhiyun *
280*4882a593Smuzhiyun * Create and join an anonymous session keyring or join a named session
281*4882a593Smuzhiyun * keyring, creating it if necessary. A named session keyring must have Search
282*4882a593Smuzhiyun * permission for it to be joined. Session keyrings without this permit will
283*4882a593Smuzhiyun * be skipped over. It is not permitted for userspace to create or join
284*4882a593Smuzhiyun * keyrings whose name begin with a dot.
285*4882a593Smuzhiyun *
286*4882a593Smuzhiyun * If successful, the ID of the joined session keyring will be returned.
287*4882a593Smuzhiyun */
keyctl_join_session_keyring(const char __user * _name)288*4882a593Smuzhiyun long keyctl_join_session_keyring(const char __user *_name)
289*4882a593Smuzhiyun {
290*4882a593Smuzhiyun char *name;
291*4882a593Smuzhiyun long ret;
292*4882a593Smuzhiyun
293*4882a593Smuzhiyun /* fetch the name from userspace */
294*4882a593Smuzhiyun name = NULL;
295*4882a593Smuzhiyun if (_name) {
296*4882a593Smuzhiyun name = strndup_user(_name, KEY_MAX_DESC_SIZE);
297*4882a593Smuzhiyun if (IS_ERR(name)) {
298*4882a593Smuzhiyun ret = PTR_ERR(name);
299*4882a593Smuzhiyun goto error;
300*4882a593Smuzhiyun }
301*4882a593Smuzhiyun
302*4882a593Smuzhiyun ret = -EPERM;
303*4882a593Smuzhiyun if (name[0] == '.')
304*4882a593Smuzhiyun goto error_name;
305*4882a593Smuzhiyun }
306*4882a593Smuzhiyun
307*4882a593Smuzhiyun /* join the session */
308*4882a593Smuzhiyun ret = join_session_keyring(name);
309*4882a593Smuzhiyun error_name:
310*4882a593Smuzhiyun kfree(name);
311*4882a593Smuzhiyun error:
312*4882a593Smuzhiyun return ret;
313*4882a593Smuzhiyun }
314*4882a593Smuzhiyun
315*4882a593Smuzhiyun /*
316*4882a593Smuzhiyun * Update a key's data payload from the given data.
317*4882a593Smuzhiyun *
318*4882a593Smuzhiyun * The key must grant the caller Write permission and the key type must support
319*4882a593Smuzhiyun * updating for this to work. A negative key can be positively instantiated
320*4882a593Smuzhiyun * with this call.
321*4882a593Smuzhiyun *
322*4882a593Smuzhiyun * If successful, 0 will be returned. If the key type does not support
323*4882a593Smuzhiyun * updating, then -EOPNOTSUPP will be returned.
324*4882a593Smuzhiyun */
keyctl_update_key(key_serial_t id,const void __user * _payload,size_t plen)325*4882a593Smuzhiyun long keyctl_update_key(key_serial_t id,
326*4882a593Smuzhiyun const void __user *_payload,
327*4882a593Smuzhiyun size_t plen)
328*4882a593Smuzhiyun {
329*4882a593Smuzhiyun key_ref_t key_ref;
330*4882a593Smuzhiyun void *payload;
331*4882a593Smuzhiyun long ret;
332*4882a593Smuzhiyun
333*4882a593Smuzhiyun ret = -EINVAL;
334*4882a593Smuzhiyun if (plen > PAGE_SIZE)
335*4882a593Smuzhiyun goto error;
336*4882a593Smuzhiyun
337*4882a593Smuzhiyun /* pull the payload in if one was supplied */
338*4882a593Smuzhiyun payload = NULL;
339*4882a593Smuzhiyun if (plen) {
340*4882a593Smuzhiyun ret = -ENOMEM;
341*4882a593Smuzhiyun payload = kvmalloc(plen, GFP_KERNEL);
342*4882a593Smuzhiyun if (!payload)
343*4882a593Smuzhiyun goto error;
344*4882a593Smuzhiyun
345*4882a593Smuzhiyun ret = -EFAULT;
346*4882a593Smuzhiyun if (copy_from_user(payload, _payload, plen) != 0)
347*4882a593Smuzhiyun goto error2;
348*4882a593Smuzhiyun }
349*4882a593Smuzhiyun
350*4882a593Smuzhiyun /* find the target key (which must be writable) */
351*4882a593Smuzhiyun key_ref = lookup_user_key(id, 0, KEY_NEED_WRITE);
352*4882a593Smuzhiyun if (IS_ERR(key_ref)) {
353*4882a593Smuzhiyun ret = PTR_ERR(key_ref);
354*4882a593Smuzhiyun goto error2;
355*4882a593Smuzhiyun }
356*4882a593Smuzhiyun
357*4882a593Smuzhiyun /* update the key */
358*4882a593Smuzhiyun ret = key_update(key_ref, payload, plen);
359*4882a593Smuzhiyun
360*4882a593Smuzhiyun key_ref_put(key_ref);
361*4882a593Smuzhiyun error2:
362*4882a593Smuzhiyun kvfree_sensitive(payload, plen);
363*4882a593Smuzhiyun error:
364*4882a593Smuzhiyun return ret;
365*4882a593Smuzhiyun }
366*4882a593Smuzhiyun
367*4882a593Smuzhiyun /*
368*4882a593Smuzhiyun * Revoke a key.
369*4882a593Smuzhiyun *
370*4882a593Smuzhiyun * The key must be grant the caller Write or Setattr permission for this to
371*4882a593Smuzhiyun * work. The key type should give up its quota claim when revoked. The key
372*4882a593Smuzhiyun * and any links to the key will be automatically garbage collected after a
373*4882a593Smuzhiyun * certain amount of time (/proc/sys/kernel/keys/gc_delay).
374*4882a593Smuzhiyun *
375*4882a593Smuzhiyun * Keys with KEY_FLAG_KEEP set should not be revoked.
376*4882a593Smuzhiyun *
377*4882a593Smuzhiyun * If successful, 0 is returned.
378*4882a593Smuzhiyun */
keyctl_revoke_key(key_serial_t id)379*4882a593Smuzhiyun long keyctl_revoke_key(key_serial_t id)
380*4882a593Smuzhiyun {
381*4882a593Smuzhiyun key_ref_t key_ref;
382*4882a593Smuzhiyun struct key *key;
383*4882a593Smuzhiyun long ret;
384*4882a593Smuzhiyun
385*4882a593Smuzhiyun key_ref = lookup_user_key(id, 0, KEY_NEED_WRITE);
386*4882a593Smuzhiyun if (IS_ERR(key_ref)) {
387*4882a593Smuzhiyun ret = PTR_ERR(key_ref);
388*4882a593Smuzhiyun if (ret != -EACCES)
389*4882a593Smuzhiyun goto error;
390*4882a593Smuzhiyun key_ref = lookup_user_key(id, 0, KEY_NEED_SETATTR);
391*4882a593Smuzhiyun if (IS_ERR(key_ref)) {
392*4882a593Smuzhiyun ret = PTR_ERR(key_ref);
393*4882a593Smuzhiyun goto error;
394*4882a593Smuzhiyun }
395*4882a593Smuzhiyun }
396*4882a593Smuzhiyun
397*4882a593Smuzhiyun key = key_ref_to_ptr(key_ref);
398*4882a593Smuzhiyun ret = 0;
399*4882a593Smuzhiyun if (test_bit(KEY_FLAG_KEEP, &key->flags))
400*4882a593Smuzhiyun ret = -EPERM;
401*4882a593Smuzhiyun else
402*4882a593Smuzhiyun key_revoke(key);
403*4882a593Smuzhiyun
404*4882a593Smuzhiyun key_ref_put(key_ref);
405*4882a593Smuzhiyun error:
406*4882a593Smuzhiyun return ret;
407*4882a593Smuzhiyun }
408*4882a593Smuzhiyun
409*4882a593Smuzhiyun /*
410*4882a593Smuzhiyun * Invalidate a key.
411*4882a593Smuzhiyun *
412*4882a593Smuzhiyun * The key must be grant the caller Invalidate permission for this to work.
413*4882a593Smuzhiyun * The key and any links to the key will be automatically garbage collected
414*4882a593Smuzhiyun * immediately.
415*4882a593Smuzhiyun *
416*4882a593Smuzhiyun * Keys with KEY_FLAG_KEEP set should not be invalidated.
417*4882a593Smuzhiyun *
418*4882a593Smuzhiyun * If successful, 0 is returned.
419*4882a593Smuzhiyun */
keyctl_invalidate_key(key_serial_t id)420*4882a593Smuzhiyun long keyctl_invalidate_key(key_serial_t id)
421*4882a593Smuzhiyun {
422*4882a593Smuzhiyun key_ref_t key_ref;
423*4882a593Smuzhiyun struct key *key;
424*4882a593Smuzhiyun long ret;
425*4882a593Smuzhiyun
426*4882a593Smuzhiyun kenter("%d", id);
427*4882a593Smuzhiyun
428*4882a593Smuzhiyun key_ref = lookup_user_key(id, 0, KEY_NEED_SEARCH);
429*4882a593Smuzhiyun if (IS_ERR(key_ref)) {
430*4882a593Smuzhiyun ret = PTR_ERR(key_ref);
431*4882a593Smuzhiyun
432*4882a593Smuzhiyun /* Root is permitted to invalidate certain special keys */
433*4882a593Smuzhiyun if (capable(CAP_SYS_ADMIN)) {
434*4882a593Smuzhiyun key_ref = lookup_user_key(id, 0, KEY_SYSADMIN_OVERRIDE);
435*4882a593Smuzhiyun if (IS_ERR(key_ref))
436*4882a593Smuzhiyun goto error;
437*4882a593Smuzhiyun if (test_bit(KEY_FLAG_ROOT_CAN_INVAL,
438*4882a593Smuzhiyun &key_ref_to_ptr(key_ref)->flags))
439*4882a593Smuzhiyun goto invalidate;
440*4882a593Smuzhiyun goto error_put;
441*4882a593Smuzhiyun }
442*4882a593Smuzhiyun
443*4882a593Smuzhiyun goto error;
444*4882a593Smuzhiyun }
445*4882a593Smuzhiyun
446*4882a593Smuzhiyun invalidate:
447*4882a593Smuzhiyun key = key_ref_to_ptr(key_ref);
448*4882a593Smuzhiyun ret = 0;
449*4882a593Smuzhiyun if (test_bit(KEY_FLAG_KEEP, &key->flags))
450*4882a593Smuzhiyun ret = -EPERM;
451*4882a593Smuzhiyun else
452*4882a593Smuzhiyun key_invalidate(key);
453*4882a593Smuzhiyun error_put:
454*4882a593Smuzhiyun key_ref_put(key_ref);
455*4882a593Smuzhiyun error:
456*4882a593Smuzhiyun kleave(" = %ld", ret);
457*4882a593Smuzhiyun return ret;
458*4882a593Smuzhiyun }
459*4882a593Smuzhiyun
460*4882a593Smuzhiyun /*
461*4882a593Smuzhiyun * Clear the specified keyring, creating an empty process keyring if one of the
462*4882a593Smuzhiyun * special keyring IDs is used.
463*4882a593Smuzhiyun *
464*4882a593Smuzhiyun * The keyring must grant the caller Write permission and not have
465*4882a593Smuzhiyun * KEY_FLAG_KEEP set for this to work. If successful, 0 will be returned.
466*4882a593Smuzhiyun */
keyctl_keyring_clear(key_serial_t ringid)467*4882a593Smuzhiyun long keyctl_keyring_clear(key_serial_t ringid)
468*4882a593Smuzhiyun {
469*4882a593Smuzhiyun key_ref_t keyring_ref;
470*4882a593Smuzhiyun struct key *keyring;
471*4882a593Smuzhiyun long ret;
472*4882a593Smuzhiyun
473*4882a593Smuzhiyun keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
474*4882a593Smuzhiyun if (IS_ERR(keyring_ref)) {
475*4882a593Smuzhiyun ret = PTR_ERR(keyring_ref);
476*4882a593Smuzhiyun
477*4882a593Smuzhiyun /* Root is permitted to invalidate certain special keyrings */
478*4882a593Smuzhiyun if (capable(CAP_SYS_ADMIN)) {
479*4882a593Smuzhiyun keyring_ref = lookup_user_key(ringid, 0,
480*4882a593Smuzhiyun KEY_SYSADMIN_OVERRIDE);
481*4882a593Smuzhiyun if (IS_ERR(keyring_ref))
482*4882a593Smuzhiyun goto error;
483*4882a593Smuzhiyun if (test_bit(KEY_FLAG_ROOT_CAN_CLEAR,
484*4882a593Smuzhiyun &key_ref_to_ptr(keyring_ref)->flags))
485*4882a593Smuzhiyun goto clear;
486*4882a593Smuzhiyun goto error_put;
487*4882a593Smuzhiyun }
488*4882a593Smuzhiyun
489*4882a593Smuzhiyun goto error;
490*4882a593Smuzhiyun }
491*4882a593Smuzhiyun
492*4882a593Smuzhiyun clear:
493*4882a593Smuzhiyun keyring = key_ref_to_ptr(keyring_ref);
494*4882a593Smuzhiyun if (test_bit(KEY_FLAG_KEEP, &keyring->flags))
495*4882a593Smuzhiyun ret = -EPERM;
496*4882a593Smuzhiyun else
497*4882a593Smuzhiyun ret = keyring_clear(keyring);
498*4882a593Smuzhiyun error_put:
499*4882a593Smuzhiyun key_ref_put(keyring_ref);
500*4882a593Smuzhiyun error:
501*4882a593Smuzhiyun return ret;
502*4882a593Smuzhiyun }
503*4882a593Smuzhiyun
504*4882a593Smuzhiyun /*
505*4882a593Smuzhiyun * Create a link from a keyring to a key if there's no matching key in the
506*4882a593Smuzhiyun * keyring, otherwise replace the link to the matching key with a link to the
507*4882a593Smuzhiyun * new key.
508*4882a593Smuzhiyun *
509*4882a593Smuzhiyun * The key must grant the caller Link permission and the the keyring must grant
510*4882a593Smuzhiyun * the caller Write permission. Furthermore, if an additional link is created,
511*4882a593Smuzhiyun * the keyring's quota will be extended.
512*4882a593Smuzhiyun *
513*4882a593Smuzhiyun * If successful, 0 will be returned.
514*4882a593Smuzhiyun */
keyctl_keyring_link(key_serial_t id,key_serial_t ringid)515*4882a593Smuzhiyun long keyctl_keyring_link(key_serial_t id, key_serial_t ringid)
516*4882a593Smuzhiyun {
517*4882a593Smuzhiyun key_ref_t keyring_ref, key_ref;
518*4882a593Smuzhiyun long ret;
519*4882a593Smuzhiyun
520*4882a593Smuzhiyun keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
521*4882a593Smuzhiyun if (IS_ERR(keyring_ref)) {
522*4882a593Smuzhiyun ret = PTR_ERR(keyring_ref);
523*4882a593Smuzhiyun goto error;
524*4882a593Smuzhiyun }
525*4882a593Smuzhiyun
526*4882a593Smuzhiyun key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE, KEY_NEED_LINK);
527*4882a593Smuzhiyun if (IS_ERR(key_ref)) {
528*4882a593Smuzhiyun ret = PTR_ERR(key_ref);
529*4882a593Smuzhiyun goto error2;
530*4882a593Smuzhiyun }
531*4882a593Smuzhiyun
532*4882a593Smuzhiyun ret = key_link(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref));
533*4882a593Smuzhiyun
534*4882a593Smuzhiyun key_ref_put(key_ref);
535*4882a593Smuzhiyun error2:
536*4882a593Smuzhiyun key_ref_put(keyring_ref);
537*4882a593Smuzhiyun error:
538*4882a593Smuzhiyun return ret;
539*4882a593Smuzhiyun }
540*4882a593Smuzhiyun
541*4882a593Smuzhiyun /*
542*4882a593Smuzhiyun * Unlink a key from a keyring.
543*4882a593Smuzhiyun *
544*4882a593Smuzhiyun * The keyring must grant the caller Write permission for this to work; the key
545*4882a593Smuzhiyun * itself need not grant the caller anything. If the last link to a key is
546*4882a593Smuzhiyun * removed then that key will be scheduled for destruction.
547*4882a593Smuzhiyun *
548*4882a593Smuzhiyun * Keys or keyrings with KEY_FLAG_KEEP set should not be unlinked.
549*4882a593Smuzhiyun *
550*4882a593Smuzhiyun * If successful, 0 will be returned.
551*4882a593Smuzhiyun */
keyctl_keyring_unlink(key_serial_t id,key_serial_t ringid)552*4882a593Smuzhiyun long keyctl_keyring_unlink(key_serial_t id, key_serial_t ringid)
553*4882a593Smuzhiyun {
554*4882a593Smuzhiyun key_ref_t keyring_ref, key_ref;
555*4882a593Smuzhiyun struct key *keyring, *key;
556*4882a593Smuzhiyun long ret;
557*4882a593Smuzhiyun
558*4882a593Smuzhiyun keyring_ref = lookup_user_key(ringid, 0, KEY_NEED_WRITE);
559*4882a593Smuzhiyun if (IS_ERR(keyring_ref)) {
560*4882a593Smuzhiyun ret = PTR_ERR(keyring_ref);
561*4882a593Smuzhiyun goto error;
562*4882a593Smuzhiyun }
563*4882a593Smuzhiyun
564*4882a593Smuzhiyun key_ref = lookup_user_key(id, KEY_LOOKUP_PARTIAL, KEY_NEED_UNLINK);
565*4882a593Smuzhiyun if (IS_ERR(key_ref)) {
566*4882a593Smuzhiyun ret = PTR_ERR(key_ref);
567*4882a593Smuzhiyun goto error2;
568*4882a593Smuzhiyun }
569*4882a593Smuzhiyun
570*4882a593Smuzhiyun keyring = key_ref_to_ptr(keyring_ref);
571*4882a593Smuzhiyun key = key_ref_to_ptr(key_ref);
572*4882a593Smuzhiyun if (test_bit(KEY_FLAG_KEEP, &keyring->flags) &&
573*4882a593Smuzhiyun test_bit(KEY_FLAG_KEEP, &key->flags))
574*4882a593Smuzhiyun ret = -EPERM;
575*4882a593Smuzhiyun else
576*4882a593Smuzhiyun ret = key_unlink(keyring, key);
577*4882a593Smuzhiyun
578*4882a593Smuzhiyun key_ref_put(key_ref);
579*4882a593Smuzhiyun error2:
580*4882a593Smuzhiyun key_ref_put(keyring_ref);
581*4882a593Smuzhiyun error:
582*4882a593Smuzhiyun return ret;
583*4882a593Smuzhiyun }
584*4882a593Smuzhiyun
585*4882a593Smuzhiyun /*
586*4882a593Smuzhiyun * Move a link to a key from one keyring to another, displacing any matching
587*4882a593Smuzhiyun * key from the destination keyring.
588*4882a593Smuzhiyun *
589*4882a593Smuzhiyun * The key must grant the caller Link permission and both keyrings must grant
590*4882a593Smuzhiyun * the caller Write permission. There must also be a link in the from keyring
591*4882a593Smuzhiyun * to the key. If both keyrings are the same, nothing is done.
592*4882a593Smuzhiyun *
593*4882a593Smuzhiyun * If successful, 0 will be returned.
594*4882a593Smuzhiyun */
keyctl_keyring_move(key_serial_t id,key_serial_t from_ringid,key_serial_t to_ringid,unsigned int flags)595*4882a593Smuzhiyun long keyctl_keyring_move(key_serial_t id, key_serial_t from_ringid,
596*4882a593Smuzhiyun key_serial_t to_ringid, unsigned int flags)
597*4882a593Smuzhiyun {
598*4882a593Smuzhiyun key_ref_t key_ref, from_ref, to_ref;
599*4882a593Smuzhiyun long ret;
600*4882a593Smuzhiyun
601*4882a593Smuzhiyun if (flags & ~KEYCTL_MOVE_EXCL)
602*4882a593Smuzhiyun return -EINVAL;
603*4882a593Smuzhiyun
604*4882a593Smuzhiyun key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE, KEY_NEED_LINK);
605*4882a593Smuzhiyun if (IS_ERR(key_ref))
606*4882a593Smuzhiyun return PTR_ERR(key_ref);
607*4882a593Smuzhiyun
608*4882a593Smuzhiyun from_ref = lookup_user_key(from_ringid, 0, KEY_NEED_WRITE);
609*4882a593Smuzhiyun if (IS_ERR(from_ref)) {
610*4882a593Smuzhiyun ret = PTR_ERR(from_ref);
611*4882a593Smuzhiyun goto error2;
612*4882a593Smuzhiyun }
613*4882a593Smuzhiyun
614*4882a593Smuzhiyun to_ref = lookup_user_key(to_ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
615*4882a593Smuzhiyun if (IS_ERR(to_ref)) {
616*4882a593Smuzhiyun ret = PTR_ERR(to_ref);
617*4882a593Smuzhiyun goto error3;
618*4882a593Smuzhiyun }
619*4882a593Smuzhiyun
620*4882a593Smuzhiyun ret = key_move(key_ref_to_ptr(key_ref), key_ref_to_ptr(from_ref),
621*4882a593Smuzhiyun key_ref_to_ptr(to_ref), flags);
622*4882a593Smuzhiyun
623*4882a593Smuzhiyun key_ref_put(to_ref);
624*4882a593Smuzhiyun error3:
625*4882a593Smuzhiyun key_ref_put(from_ref);
626*4882a593Smuzhiyun error2:
627*4882a593Smuzhiyun key_ref_put(key_ref);
628*4882a593Smuzhiyun return ret;
629*4882a593Smuzhiyun }
630*4882a593Smuzhiyun
631*4882a593Smuzhiyun /*
632*4882a593Smuzhiyun * Return a description of a key to userspace.
633*4882a593Smuzhiyun *
634*4882a593Smuzhiyun * The key must grant the caller View permission for this to work.
635*4882a593Smuzhiyun *
636*4882a593Smuzhiyun * If there's a buffer, we place up to buflen bytes of data into it formatted
637*4882a593Smuzhiyun * in the following way:
638*4882a593Smuzhiyun *
639*4882a593Smuzhiyun * type;uid;gid;perm;description<NUL>
640*4882a593Smuzhiyun *
641*4882a593Smuzhiyun * If successful, we return the amount of description available, irrespective
642*4882a593Smuzhiyun * of how much we may have copied into the buffer.
643*4882a593Smuzhiyun */
keyctl_describe_key(key_serial_t keyid,char __user * buffer,size_t buflen)644*4882a593Smuzhiyun long keyctl_describe_key(key_serial_t keyid,
645*4882a593Smuzhiyun char __user *buffer,
646*4882a593Smuzhiyun size_t buflen)
647*4882a593Smuzhiyun {
648*4882a593Smuzhiyun struct key *key, *instkey;
649*4882a593Smuzhiyun key_ref_t key_ref;
650*4882a593Smuzhiyun char *infobuf;
651*4882a593Smuzhiyun long ret;
652*4882a593Smuzhiyun int desclen, infolen;
653*4882a593Smuzhiyun
654*4882a593Smuzhiyun key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_NEED_VIEW);
655*4882a593Smuzhiyun if (IS_ERR(key_ref)) {
656*4882a593Smuzhiyun /* viewing a key under construction is permitted if we have the
657*4882a593Smuzhiyun * authorisation token handy */
658*4882a593Smuzhiyun if (PTR_ERR(key_ref) == -EACCES) {
659*4882a593Smuzhiyun instkey = key_get_instantiation_authkey(keyid);
660*4882a593Smuzhiyun if (!IS_ERR(instkey)) {
661*4882a593Smuzhiyun key_put(instkey);
662*4882a593Smuzhiyun key_ref = lookup_user_key(keyid,
663*4882a593Smuzhiyun KEY_LOOKUP_PARTIAL,
664*4882a593Smuzhiyun KEY_AUTHTOKEN_OVERRIDE);
665*4882a593Smuzhiyun if (!IS_ERR(key_ref))
666*4882a593Smuzhiyun goto okay;
667*4882a593Smuzhiyun }
668*4882a593Smuzhiyun }
669*4882a593Smuzhiyun
670*4882a593Smuzhiyun ret = PTR_ERR(key_ref);
671*4882a593Smuzhiyun goto error;
672*4882a593Smuzhiyun }
673*4882a593Smuzhiyun
674*4882a593Smuzhiyun okay:
675*4882a593Smuzhiyun key = key_ref_to_ptr(key_ref);
676*4882a593Smuzhiyun desclen = strlen(key->description);
677*4882a593Smuzhiyun
678*4882a593Smuzhiyun /* calculate how much information we're going to return */
679*4882a593Smuzhiyun ret = -ENOMEM;
680*4882a593Smuzhiyun infobuf = kasprintf(GFP_KERNEL,
681*4882a593Smuzhiyun "%s;%d;%d;%08x;",
682*4882a593Smuzhiyun key->type->name,
683*4882a593Smuzhiyun from_kuid_munged(current_user_ns(), key->uid),
684*4882a593Smuzhiyun from_kgid_munged(current_user_ns(), key->gid),
685*4882a593Smuzhiyun key->perm);
686*4882a593Smuzhiyun if (!infobuf)
687*4882a593Smuzhiyun goto error2;
688*4882a593Smuzhiyun infolen = strlen(infobuf);
689*4882a593Smuzhiyun ret = infolen + desclen + 1;
690*4882a593Smuzhiyun
691*4882a593Smuzhiyun /* consider returning the data */
692*4882a593Smuzhiyun if (buffer && buflen >= ret) {
693*4882a593Smuzhiyun if (copy_to_user(buffer, infobuf, infolen) != 0 ||
694*4882a593Smuzhiyun copy_to_user(buffer + infolen, key->description,
695*4882a593Smuzhiyun desclen + 1) != 0)
696*4882a593Smuzhiyun ret = -EFAULT;
697*4882a593Smuzhiyun }
698*4882a593Smuzhiyun
699*4882a593Smuzhiyun kfree(infobuf);
700*4882a593Smuzhiyun error2:
701*4882a593Smuzhiyun key_ref_put(key_ref);
702*4882a593Smuzhiyun error:
703*4882a593Smuzhiyun return ret;
704*4882a593Smuzhiyun }
705*4882a593Smuzhiyun
706*4882a593Smuzhiyun /*
707*4882a593Smuzhiyun * Search the specified keyring and any keyrings it links to for a matching
708*4882a593Smuzhiyun * key. Only keyrings that grant the caller Search permission will be searched
709*4882a593Smuzhiyun * (this includes the starting keyring). Only keys with Search permission can
710*4882a593Smuzhiyun * be found.
711*4882a593Smuzhiyun *
712*4882a593Smuzhiyun * If successful, the found key will be linked to the destination keyring if
713*4882a593Smuzhiyun * supplied and the key has Link permission, and the found key ID will be
714*4882a593Smuzhiyun * returned.
715*4882a593Smuzhiyun */
keyctl_keyring_search(key_serial_t ringid,const char __user * _type,const char __user * _description,key_serial_t destringid)716*4882a593Smuzhiyun long keyctl_keyring_search(key_serial_t ringid,
717*4882a593Smuzhiyun const char __user *_type,
718*4882a593Smuzhiyun const char __user *_description,
719*4882a593Smuzhiyun key_serial_t destringid)
720*4882a593Smuzhiyun {
721*4882a593Smuzhiyun struct key_type *ktype;
722*4882a593Smuzhiyun key_ref_t keyring_ref, key_ref, dest_ref;
723*4882a593Smuzhiyun char type[32], *description;
724*4882a593Smuzhiyun long ret;
725*4882a593Smuzhiyun
726*4882a593Smuzhiyun /* pull the type and description into kernel space */
727*4882a593Smuzhiyun ret = key_get_type_from_user(type, _type, sizeof(type));
728*4882a593Smuzhiyun if (ret < 0)
729*4882a593Smuzhiyun goto error;
730*4882a593Smuzhiyun
731*4882a593Smuzhiyun description = strndup_user(_description, KEY_MAX_DESC_SIZE);
732*4882a593Smuzhiyun if (IS_ERR(description)) {
733*4882a593Smuzhiyun ret = PTR_ERR(description);
734*4882a593Smuzhiyun goto error;
735*4882a593Smuzhiyun }
736*4882a593Smuzhiyun
737*4882a593Smuzhiyun /* get the keyring at which to begin the search */
738*4882a593Smuzhiyun keyring_ref = lookup_user_key(ringid, 0, KEY_NEED_SEARCH);
739*4882a593Smuzhiyun if (IS_ERR(keyring_ref)) {
740*4882a593Smuzhiyun ret = PTR_ERR(keyring_ref);
741*4882a593Smuzhiyun goto error2;
742*4882a593Smuzhiyun }
743*4882a593Smuzhiyun
744*4882a593Smuzhiyun /* get the destination keyring if specified */
745*4882a593Smuzhiyun dest_ref = NULL;
746*4882a593Smuzhiyun if (destringid) {
747*4882a593Smuzhiyun dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE,
748*4882a593Smuzhiyun KEY_NEED_WRITE);
749*4882a593Smuzhiyun if (IS_ERR(dest_ref)) {
750*4882a593Smuzhiyun ret = PTR_ERR(dest_ref);
751*4882a593Smuzhiyun goto error3;
752*4882a593Smuzhiyun }
753*4882a593Smuzhiyun }
754*4882a593Smuzhiyun
755*4882a593Smuzhiyun /* find the key type */
756*4882a593Smuzhiyun ktype = key_type_lookup(type);
757*4882a593Smuzhiyun if (IS_ERR(ktype)) {
758*4882a593Smuzhiyun ret = PTR_ERR(ktype);
759*4882a593Smuzhiyun goto error4;
760*4882a593Smuzhiyun }
761*4882a593Smuzhiyun
762*4882a593Smuzhiyun /* do the search */
763*4882a593Smuzhiyun key_ref = keyring_search(keyring_ref, ktype, description, true);
764*4882a593Smuzhiyun if (IS_ERR(key_ref)) {
765*4882a593Smuzhiyun ret = PTR_ERR(key_ref);
766*4882a593Smuzhiyun
767*4882a593Smuzhiyun /* treat lack or presence of a negative key the same */
768*4882a593Smuzhiyun if (ret == -EAGAIN)
769*4882a593Smuzhiyun ret = -ENOKEY;
770*4882a593Smuzhiyun goto error5;
771*4882a593Smuzhiyun }
772*4882a593Smuzhiyun
773*4882a593Smuzhiyun /* link the resulting key to the destination keyring if we can */
774*4882a593Smuzhiyun if (dest_ref) {
775*4882a593Smuzhiyun ret = key_permission(key_ref, KEY_NEED_LINK);
776*4882a593Smuzhiyun if (ret < 0)
777*4882a593Smuzhiyun goto error6;
778*4882a593Smuzhiyun
779*4882a593Smuzhiyun ret = key_link(key_ref_to_ptr(dest_ref), key_ref_to_ptr(key_ref));
780*4882a593Smuzhiyun if (ret < 0)
781*4882a593Smuzhiyun goto error6;
782*4882a593Smuzhiyun }
783*4882a593Smuzhiyun
784*4882a593Smuzhiyun ret = key_ref_to_ptr(key_ref)->serial;
785*4882a593Smuzhiyun
786*4882a593Smuzhiyun error6:
787*4882a593Smuzhiyun key_ref_put(key_ref);
788*4882a593Smuzhiyun error5:
789*4882a593Smuzhiyun key_type_put(ktype);
790*4882a593Smuzhiyun error4:
791*4882a593Smuzhiyun key_ref_put(dest_ref);
792*4882a593Smuzhiyun error3:
793*4882a593Smuzhiyun key_ref_put(keyring_ref);
794*4882a593Smuzhiyun error2:
795*4882a593Smuzhiyun kfree(description);
796*4882a593Smuzhiyun error:
797*4882a593Smuzhiyun return ret;
798*4882a593Smuzhiyun }
799*4882a593Smuzhiyun
800*4882a593Smuzhiyun /*
801*4882a593Smuzhiyun * Call the read method
802*4882a593Smuzhiyun */
__keyctl_read_key(struct key * key,char * buffer,size_t buflen)803*4882a593Smuzhiyun static long __keyctl_read_key(struct key *key, char *buffer, size_t buflen)
804*4882a593Smuzhiyun {
805*4882a593Smuzhiyun long ret;
806*4882a593Smuzhiyun
807*4882a593Smuzhiyun down_read(&key->sem);
808*4882a593Smuzhiyun ret = key_validate(key);
809*4882a593Smuzhiyun if (ret == 0)
810*4882a593Smuzhiyun ret = key->type->read(key, buffer, buflen);
811*4882a593Smuzhiyun up_read(&key->sem);
812*4882a593Smuzhiyun return ret;
813*4882a593Smuzhiyun }
814*4882a593Smuzhiyun
815*4882a593Smuzhiyun /*
816*4882a593Smuzhiyun * Read a key's payload.
817*4882a593Smuzhiyun *
818*4882a593Smuzhiyun * The key must either grant the caller Read permission, or it must grant the
819*4882a593Smuzhiyun * caller Search permission when searched for from the process keyrings.
820*4882a593Smuzhiyun *
821*4882a593Smuzhiyun * If successful, we place up to buflen bytes of data into the buffer, if one
822*4882a593Smuzhiyun * is provided, and return the amount of data that is available in the key,
823*4882a593Smuzhiyun * irrespective of how much we copied into the buffer.
824*4882a593Smuzhiyun */
keyctl_read_key(key_serial_t keyid,char __user * buffer,size_t buflen)825*4882a593Smuzhiyun long keyctl_read_key(key_serial_t keyid, char __user *buffer, size_t buflen)
826*4882a593Smuzhiyun {
827*4882a593Smuzhiyun struct key *key;
828*4882a593Smuzhiyun key_ref_t key_ref;
829*4882a593Smuzhiyun long ret;
830*4882a593Smuzhiyun char *key_data = NULL;
831*4882a593Smuzhiyun size_t key_data_len;
832*4882a593Smuzhiyun
833*4882a593Smuzhiyun /* find the key first */
834*4882a593Smuzhiyun key_ref = lookup_user_key(keyid, 0, KEY_DEFER_PERM_CHECK);
835*4882a593Smuzhiyun if (IS_ERR(key_ref)) {
836*4882a593Smuzhiyun ret = -ENOKEY;
837*4882a593Smuzhiyun goto out;
838*4882a593Smuzhiyun }
839*4882a593Smuzhiyun
840*4882a593Smuzhiyun key = key_ref_to_ptr(key_ref);
841*4882a593Smuzhiyun
842*4882a593Smuzhiyun ret = key_read_state(key);
843*4882a593Smuzhiyun if (ret < 0)
844*4882a593Smuzhiyun goto key_put_out; /* Negatively instantiated */
845*4882a593Smuzhiyun
846*4882a593Smuzhiyun /* see if we can read it directly */
847*4882a593Smuzhiyun ret = key_permission(key_ref, KEY_NEED_READ);
848*4882a593Smuzhiyun if (ret == 0)
849*4882a593Smuzhiyun goto can_read_key;
850*4882a593Smuzhiyun if (ret != -EACCES)
851*4882a593Smuzhiyun goto key_put_out;
852*4882a593Smuzhiyun
853*4882a593Smuzhiyun /* we can't; see if it's searchable from this process's keyrings
854*4882a593Smuzhiyun * - we automatically take account of the fact that it may be
855*4882a593Smuzhiyun * dangling off an instantiation key
856*4882a593Smuzhiyun */
857*4882a593Smuzhiyun if (!is_key_possessed(key_ref)) {
858*4882a593Smuzhiyun ret = -EACCES;
859*4882a593Smuzhiyun goto key_put_out;
860*4882a593Smuzhiyun }
861*4882a593Smuzhiyun
862*4882a593Smuzhiyun /* the key is probably readable - now try to read it */
863*4882a593Smuzhiyun can_read_key:
864*4882a593Smuzhiyun if (!key->type->read) {
865*4882a593Smuzhiyun ret = -EOPNOTSUPP;
866*4882a593Smuzhiyun goto key_put_out;
867*4882a593Smuzhiyun }
868*4882a593Smuzhiyun
869*4882a593Smuzhiyun if (!buffer || !buflen) {
870*4882a593Smuzhiyun /* Get the key length from the read method */
871*4882a593Smuzhiyun ret = __keyctl_read_key(key, NULL, 0);
872*4882a593Smuzhiyun goto key_put_out;
873*4882a593Smuzhiyun }
874*4882a593Smuzhiyun
875*4882a593Smuzhiyun /*
876*4882a593Smuzhiyun * Read the data with the semaphore held (since we might sleep)
877*4882a593Smuzhiyun * to protect against the key being updated or revoked.
878*4882a593Smuzhiyun *
879*4882a593Smuzhiyun * Allocating a temporary buffer to hold the keys before
880*4882a593Smuzhiyun * transferring them to user buffer to avoid potential
881*4882a593Smuzhiyun * deadlock involving page fault and mmap_lock.
882*4882a593Smuzhiyun *
883*4882a593Smuzhiyun * key_data_len = (buflen <= PAGE_SIZE)
884*4882a593Smuzhiyun * ? buflen : actual length of key data
885*4882a593Smuzhiyun *
886*4882a593Smuzhiyun * This prevents allocating arbitrary large buffer which can
887*4882a593Smuzhiyun * be much larger than the actual key length. In the latter case,
888*4882a593Smuzhiyun * at least 2 passes of this loop is required.
889*4882a593Smuzhiyun */
890*4882a593Smuzhiyun key_data_len = (buflen <= PAGE_SIZE) ? buflen : 0;
891*4882a593Smuzhiyun for (;;) {
892*4882a593Smuzhiyun if (key_data_len) {
893*4882a593Smuzhiyun key_data = kvmalloc(key_data_len, GFP_KERNEL);
894*4882a593Smuzhiyun if (!key_data) {
895*4882a593Smuzhiyun ret = -ENOMEM;
896*4882a593Smuzhiyun goto key_put_out;
897*4882a593Smuzhiyun }
898*4882a593Smuzhiyun }
899*4882a593Smuzhiyun
900*4882a593Smuzhiyun ret = __keyctl_read_key(key, key_data, key_data_len);
901*4882a593Smuzhiyun
902*4882a593Smuzhiyun /*
903*4882a593Smuzhiyun * Read methods will just return the required length without
904*4882a593Smuzhiyun * any copying if the provided length isn't large enough.
905*4882a593Smuzhiyun */
906*4882a593Smuzhiyun if (ret <= 0 || ret > buflen)
907*4882a593Smuzhiyun break;
908*4882a593Smuzhiyun
909*4882a593Smuzhiyun /*
910*4882a593Smuzhiyun * The key may change (unlikely) in between 2 consecutive
911*4882a593Smuzhiyun * __keyctl_read_key() calls. In this case, we reallocate
912*4882a593Smuzhiyun * a larger buffer and redo the key read when
913*4882a593Smuzhiyun * key_data_len < ret <= buflen.
914*4882a593Smuzhiyun */
915*4882a593Smuzhiyun if (ret > key_data_len) {
916*4882a593Smuzhiyun if (unlikely(key_data))
917*4882a593Smuzhiyun kvfree_sensitive(key_data, key_data_len);
918*4882a593Smuzhiyun key_data_len = ret;
919*4882a593Smuzhiyun continue; /* Allocate buffer */
920*4882a593Smuzhiyun }
921*4882a593Smuzhiyun
922*4882a593Smuzhiyun if (copy_to_user(buffer, key_data, ret))
923*4882a593Smuzhiyun ret = -EFAULT;
924*4882a593Smuzhiyun break;
925*4882a593Smuzhiyun }
926*4882a593Smuzhiyun kvfree_sensitive(key_data, key_data_len);
927*4882a593Smuzhiyun
928*4882a593Smuzhiyun key_put_out:
929*4882a593Smuzhiyun key_put(key);
930*4882a593Smuzhiyun out:
931*4882a593Smuzhiyun return ret;
932*4882a593Smuzhiyun }
933*4882a593Smuzhiyun
934*4882a593Smuzhiyun /*
935*4882a593Smuzhiyun * Change the ownership of a key
936*4882a593Smuzhiyun *
937*4882a593Smuzhiyun * The key must grant the caller Setattr permission for this to work, though
938*4882a593Smuzhiyun * the key need not be fully instantiated yet. For the UID to be changed, or
939*4882a593Smuzhiyun * for the GID to be changed to a group the caller is not a member of, the
940*4882a593Smuzhiyun * caller must have sysadmin capability. If either uid or gid is -1 then that
941*4882a593Smuzhiyun * attribute is not changed.
942*4882a593Smuzhiyun *
943*4882a593Smuzhiyun * If the UID is to be changed, the new user must have sufficient quota to
944*4882a593Smuzhiyun * accept the key. The quota deduction will be removed from the old user to
945*4882a593Smuzhiyun * the new user should the attribute be changed.
946*4882a593Smuzhiyun *
947*4882a593Smuzhiyun * If successful, 0 will be returned.
948*4882a593Smuzhiyun */
keyctl_chown_key(key_serial_t id,uid_t user,gid_t group)949*4882a593Smuzhiyun long keyctl_chown_key(key_serial_t id, uid_t user, gid_t group)
950*4882a593Smuzhiyun {
951*4882a593Smuzhiyun struct key_user *newowner, *zapowner = NULL;
952*4882a593Smuzhiyun struct key *key;
953*4882a593Smuzhiyun key_ref_t key_ref;
954*4882a593Smuzhiyun long ret;
955*4882a593Smuzhiyun kuid_t uid;
956*4882a593Smuzhiyun kgid_t gid;
957*4882a593Smuzhiyun
958*4882a593Smuzhiyun uid = make_kuid(current_user_ns(), user);
959*4882a593Smuzhiyun gid = make_kgid(current_user_ns(), group);
960*4882a593Smuzhiyun ret = -EINVAL;
961*4882a593Smuzhiyun if ((user != (uid_t) -1) && !uid_valid(uid))
962*4882a593Smuzhiyun goto error;
963*4882a593Smuzhiyun if ((group != (gid_t) -1) && !gid_valid(gid))
964*4882a593Smuzhiyun goto error;
965*4882a593Smuzhiyun
966*4882a593Smuzhiyun ret = 0;
967*4882a593Smuzhiyun if (user == (uid_t) -1 && group == (gid_t) -1)
968*4882a593Smuzhiyun goto error;
969*4882a593Smuzhiyun
970*4882a593Smuzhiyun key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
971*4882a593Smuzhiyun KEY_NEED_SETATTR);
972*4882a593Smuzhiyun if (IS_ERR(key_ref)) {
973*4882a593Smuzhiyun ret = PTR_ERR(key_ref);
974*4882a593Smuzhiyun goto error;
975*4882a593Smuzhiyun }
976*4882a593Smuzhiyun
977*4882a593Smuzhiyun key = key_ref_to_ptr(key_ref);
978*4882a593Smuzhiyun
979*4882a593Smuzhiyun /* make the changes with the locks held to prevent chown/chown races */
980*4882a593Smuzhiyun ret = -EACCES;
981*4882a593Smuzhiyun down_write(&key->sem);
982*4882a593Smuzhiyun
983*4882a593Smuzhiyun if (!capable(CAP_SYS_ADMIN)) {
984*4882a593Smuzhiyun /* only the sysadmin can chown a key to some other UID */
985*4882a593Smuzhiyun if (user != (uid_t) -1 && !uid_eq(key->uid, uid))
986*4882a593Smuzhiyun goto error_put;
987*4882a593Smuzhiyun
988*4882a593Smuzhiyun /* only the sysadmin can set the key's GID to a group other
989*4882a593Smuzhiyun * than one of those that the current process subscribes to */
990*4882a593Smuzhiyun if (group != (gid_t) -1 && !gid_eq(gid, key->gid) && !in_group_p(gid))
991*4882a593Smuzhiyun goto error_put;
992*4882a593Smuzhiyun }
993*4882a593Smuzhiyun
994*4882a593Smuzhiyun /* change the UID */
995*4882a593Smuzhiyun if (user != (uid_t) -1 && !uid_eq(uid, key->uid)) {
996*4882a593Smuzhiyun ret = -ENOMEM;
997*4882a593Smuzhiyun newowner = key_user_lookup(uid);
998*4882a593Smuzhiyun if (!newowner)
999*4882a593Smuzhiyun goto error_put;
1000*4882a593Smuzhiyun
1001*4882a593Smuzhiyun /* transfer the quota burden to the new user */
1002*4882a593Smuzhiyun if (test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) {
1003*4882a593Smuzhiyun unsigned maxkeys = uid_eq(uid, GLOBAL_ROOT_UID) ?
1004*4882a593Smuzhiyun key_quota_root_maxkeys : key_quota_maxkeys;
1005*4882a593Smuzhiyun unsigned maxbytes = uid_eq(uid, GLOBAL_ROOT_UID) ?
1006*4882a593Smuzhiyun key_quota_root_maxbytes : key_quota_maxbytes;
1007*4882a593Smuzhiyun
1008*4882a593Smuzhiyun spin_lock(&newowner->lock);
1009*4882a593Smuzhiyun if (newowner->qnkeys + 1 > maxkeys ||
1010*4882a593Smuzhiyun newowner->qnbytes + key->quotalen > maxbytes ||
1011*4882a593Smuzhiyun newowner->qnbytes + key->quotalen <
1012*4882a593Smuzhiyun newowner->qnbytes)
1013*4882a593Smuzhiyun goto quota_overrun;
1014*4882a593Smuzhiyun
1015*4882a593Smuzhiyun newowner->qnkeys++;
1016*4882a593Smuzhiyun newowner->qnbytes += key->quotalen;
1017*4882a593Smuzhiyun spin_unlock(&newowner->lock);
1018*4882a593Smuzhiyun
1019*4882a593Smuzhiyun spin_lock(&key->user->lock);
1020*4882a593Smuzhiyun key->user->qnkeys--;
1021*4882a593Smuzhiyun key->user->qnbytes -= key->quotalen;
1022*4882a593Smuzhiyun spin_unlock(&key->user->lock);
1023*4882a593Smuzhiyun }
1024*4882a593Smuzhiyun
1025*4882a593Smuzhiyun atomic_dec(&key->user->nkeys);
1026*4882a593Smuzhiyun atomic_inc(&newowner->nkeys);
1027*4882a593Smuzhiyun
1028*4882a593Smuzhiyun if (key->state != KEY_IS_UNINSTANTIATED) {
1029*4882a593Smuzhiyun atomic_dec(&key->user->nikeys);
1030*4882a593Smuzhiyun atomic_inc(&newowner->nikeys);
1031*4882a593Smuzhiyun }
1032*4882a593Smuzhiyun
1033*4882a593Smuzhiyun zapowner = key->user;
1034*4882a593Smuzhiyun key->user = newowner;
1035*4882a593Smuzhiyun key->uid = uid;
1036*4882a593Smuzhiyun }
1037*4882a593Smuzhiyun
1038*4882a593Smuzhiyun /* change the GID */
1039*4882a593Smuzhiyun if (group != (gid_t) -1)
1040*4882a593Smuzhiyun key->gid = gid;
1041*4882a593Smuzhiyun
1042*4882a593Smuzhiyun notify_key(key, NOTIFY_KEY_SETATTR, 0);
1043*4882a593Smuzhiyun ret = 0;
1044*4882a593Smuzhiyun
1045*4882a593Smuzhiyun error_put:
1046*4882a593Smuzhiyun up_write(&key->sem);
1047*4882a593Smuzhiyun key_put(key);
1048*4882a593Smuzhiyun if (zapowner)
1049*4882a593Smuzhiyun key_user_put(zapowner);
1050*4882a593Smuzhiyun error:
1051*4882a593Smuzhiyun return ret;
1052*4882a593Smuzhiyun
1053*4882a593Smuzhiyun quota_overrun:
1054*4882a593Smuzhiyun spin_unlock(&newowner->lock);
1055*4882a593Smuzhiyun zapowner = newowner;
1056*4882a593Smuzhiyun ret = -EDQUOT;
1057*4882a593Smuzhiyun goto error_put;
1058*4882a593Smuzhiyun }
1059*4882a593Smuzhiyun
1060*4882a593Smuzhiyun /*
1061*4882a593Smuzhiyun * Change the permission mask on a key.
1062*4882a593Smuzhiyun *
1063*4882a593Smuzhiyun * The key must grant the caller Setattr permission for this to work, though
1064*4882a593Smuzhiyun * the key need not be fully instantiated yet. If the caller does not have
1065*4882a593Smuzhiyun * sysadmin capability, it may only change the permission on keys that it owns.
1066*4882a593Smuzhiyun */
keyctl_setperm_key(key_serial_t id,key_perm_t perm)1067*4882a593Smuzhiyun long keyctl_setperm_key(key_serial_t id, key_perm_t perm)
1068*4882a593Smuzhiyun {
1069*4882a593Smuzhiyun struct key *key;
1070*4882a593Smuzhiyun key_ref_t key_ref;
1071*4882a593Smuzhiyun long ret;
1072*4882a593Smuzhiyun
1073*4882a593Smuzhiyun ret = -EINVAL;
1074*4882a593Smuzhiyun if (perm & ~(KEY_POS_ALL | KEY_USR_ALL | KEY_GRP_ALL | KEY_OTH_ALL))
1075*4882a593Smuzhiyun goto error;
1076*4882a593Smuzhiyun
1077*4882a593Smuzhiyun key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
1078*4882a593Smuzhiyun KEY_NEED_SETATTR);
1079*4882a593Smuzhiyun if (IS_ERR(key_ref)) {
1080*4882a593Smuzhiyun ret = PTR_ERR(key_ref);
1081*4882a593Smuzhiyun goto error;
1082*4882a593Smuzhiyun }
1083*4882a593Smuzhiyun
1084*4882a593Smuzhiyun key = key_ref_to_ptr(key_ref);
1085*4882a593Smuzhiyun
1086*4882a593Smuzhiyun /* make the changes with the locks held to prevent chown/chmod races */
1087*4882a593Smuzhiyun ret = -EACCES;
1088*4882a593Smuzhiyun down_write(&key->sem);
1089*4882a593Smuzhiyun
1090*4882a593Smuzhiyun /* if we're not the sysadmin, we can only change a key that we own */
1091*4882a593Smuzhiyun if (capable(CAP_SYS_ADMIN) || uid_eq(key->uid, current_fsuid())) {
1092*4882a593Smuzhiyun key->perm = perm;
1093*4882a593Smuzhiyun notify_key(key, NOTIFY_KEY_SETATTR, 0);
1094*4882a593Smuzhiyun ret = 0;
1095*4882a593Smuzhiyun }
1096*4882a593Smuzhiyun
1097*4882a593Smuzhiyun up_write(&key->sem);
1098*4882a593Smuzhiyun key_put(key);
1099*4882a593Smuzhiyun error:
1100*4882a593Smuzhiyun return ret;
1101*4882a593Smuzhiyun }
1102*4882a593Smuzhiyun
1103*4882a593Smuzhiyun /*
1104*4882a593Smuzhiyun * Get the destination keyring for instantiation and check that the caller has
1105*4882a593Smuzhiyun * Write permission on it.
1106*4882a593Smuzhiyun */
get_instantiation_keyring(key_serial_t ringid,struct request_key_auth * rka,struct key ** _dest_keyring)1107*4882a593Smuzhiyun static long get_instantiation_keyring(key_serial_t ringid,
1108*4882a593Smuzhiyun struct request_key_auth *rka,
1109*4882a593Smuzhiyun struct key **_dest_keyring)
1110*4882a593Smuzhiyun {
1111*4882a593Smuzhiyun key_ref_t dkref;
1112*4882a593Smuzhiyun
1113*4882a593Smuzhiyun *_dest_keyring = NULL;
1114*4882a593Smuzhiyun
1115*4882a593Smuzhiyun /* just return a NULL pointer if we weren't asked to make a link */
1116*4882a593Smuzhiyun if (ringid == 0)
1117*4882a593Smuzhiyun return 0;
1118*4882a593Smuzhiyun
1119*4882a593Smuzhiyun /* if a specific keyring is nominated by ID, then use that */
1120*4882a593Smuzhiyun if (ringid > 0) {
1121*4882a593Smuzhiyun dkref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
1122*4882a593Smuzhiyun if (IS_ERR(dkref))
1123*4882a593Smuzhiyun return PTR_ERR(dkref);
1124*4882a593Smuzhiyun *_dest_keyring = key_ref_to_ptr(dkref);
1125*4882a593Smuzhiyun return 0;
1126*4882a593Smuzhiyun }
1127*4882a593Smuzhiyun
1128*4882a593Smuzhiyun if (ringid == KEY_SPEC_REQKEY_AUTH_KEY)
1129*4882a593Smuzhiyun return -EINVAL;
1130*4882a593Smuzhiyun
1131*4882a593Smuzhiyun /* otherwise specify the destination keyring recorded in the
1132*4882a593Smuzhiyun * authorisation key (any KEY_SPEC_*_KEYRING) */
1133*4882a593Smuzhiyun if (ringid >= KEY_SPEC_REQUESTOR_KEYRING) {
1134*4882a593Smuzhiyun *_dest_keyring = key_get(rka->dest_keyring);
1135*4882a593Smuzhiyun return 0;
1136*4882a593Smuzhiyun }
1137*4882a593Smuzhiyun
1138*4882a593Smuzhiyun return -ENOKEY;
1139*4882a593Smuzhiyun }
1140*4882a593Smuzhiyun
1141*4882a593Smuzhiyun /*
1142*4882a593Smuzhiyun * Change the request_key authorisation key on the current process.
1143*4882a593Smuzhiyun */
keyctl_change_reqkey_auth(struct key * key)1144*4882a593Smuzhiyun static int keyctl_change_reqkey_auth(struct key *key)
1145*4882a593Smuzhiyun {
1146*4882a593Smuzhiyun struct cred *new;
1147*4882a593Smuzhiyun
1148*4882a593Smuzhiyun new = prepare_creds();
1149*4882a593Smuzhiyun if (!new)
1150*4882a593Smuzhiyun return -ENOMEM;
1151*4882a593Smuzhiyun
1152*4882a593Smuzhiyun key_put(new->request_key_auth);
1153*4882a593Smuzhiyun new->request_key_auth = key_get(key);
1154*4882a593Smuzhiyun
1155*4882a593Smuzhiyun return commit_creds(new);
1156*4882a593Smuzhiyun }
1157*4882a593Smuzhiyun
1158*4882a593Smuzhiyun /*
1159*4882a593Smuzhiyun * Instantiate a key with the specified payload and link the key into the
1160*4882a593Smuzhiyun * destination keyring if one is given.
1161*4882a593Smuzhiyun *
1162*4882a593Smuzhiyun * The caller must have the appropriate instantiation permit set for this to
1163*4882a593Smuzhiyun * work (see keyctl_assume_authority). No other permissions are required.
1164*4882a593Smuzhiyun *
1165*4882a593Smuzhiyun * If successful, 0 will be returned.
1166*4882a593Smuzhiyun */
keyctl_instantiate_key_common(key_serial_t id,struct iov_iter * from,key_serial_t ringid)1167*4882a593Smuzhiyun static long keyctl_instantiate_key_common(key_serial_t id,
1168*4882a593Smuzhiyun struct iov_iter *from,
1169*4882a593Smuzhiyun key_serial_t ringid)
1170*4882a593Smuzhiyun {
1171*4882a593Smuzhiyun const struct cred *cred = current_cred();
1172*4882a593Smuzhiyun struct request_key_auth *rka;
1173*4882a593Smuzhiyun struct key *instkey, *dest_keyring;
1174*4882a593Smuzhiyun size_t plen = from ? iov_iter_count(from) : 0;
1175*4882a593Smuzhiyun void *payload;
1176*4882a593Smuzhiyun long ret;
1177*4882a593Smuzhiyun
1178*4882a593Smuzhiyun kenter("%d,,%zu,%d", id, plen, ringid);
1179*4882a593Smuzhiyun
1180*4882a593Smuzhiyun if (!plen)
1181*4882a593Smuzhiyun from = NULL;
1182*4882a593Smuzhiyun
1183*4882a593Smuzhiyun ret = -EINVAL;
1184*4882a593Smuzhiyun if (plen > 1024 * 1024 - 1)
1185*4882a593Smuzhiyun goto error;
1186*4882a593Smuzhiyun
1187*4882a593Smuzhiyun /* the appropriate instantiation authorisation key must have been
1188*4882a593Smuzhiyun * assumed before calling this */
1189*4882a593Smuzhiyun ret = -EPERM;
1190*4882a593Smuzhiyun instkey = cred->request_key_auth;
1191*4882a593Smuzhiyun if (!instkey)
1192*4882a593Smuzhiyun goto error;
1193*4882a593Smuzhiyun
1194*4882a593Smuzhiyun rka = instkey->payload.data[0];
1195*4882a593Smuzhiyun if (rka->target_key->serial != id)
1196*4882a593Smuzhiyun goto error;
1197*4882a593Smuzhiyun
1198*4882a593Smuzhiyun /* pull the payload in if one was supplied */
1199*4882a593Smuzhiyun payload = NULL;
1200*4882a593Smuzhiyun
1201*4882a593Smuzhiyun if (from) {
1202*4882a593Smuzhiyun ret = -ENOMEM;
1203*4882a593Smuzhiyun payload = kvmalloc(plen, GFP_KERNEL);
1204*4882a593Smuzhiyun if (!payload)
1205*4882a593Smuzhiyun goto error;
1206*4882a593Smuzhiyun
1207*4882a593Smuzhiyun ret = -EFAULT;
1208*4882a593Smuzhiyun if (!copy_from_iter_full(payload, plen, from))
1209*4882a593Smuzhiyun goto error2;
1210*4882a593Smuzhiyun }
1211*4882a593Smuzhiyun
1212*4882a593Smuzhiyun /* find the destination keyring amongst those belonging to the
1213*4882a593Smuzhiyun * requesting task */
1214*4882a593Smuzhiyun ret = get_instantiation_keyring(ringid, rka, &dest_keyring);
1215*4882a593Smuzhiyun if (ret < 0)
1216*4882a593Smuzhiyun goto error2;
1217*4882a593Smuzhiyun
1218*4882a593Smuzhiyun /* instantiate the key and link it into a keyring */
1219*4882a593Smuzhiyun ret = key_instantiate_and_link(rka->target_key, payload, plen,
1220*4882a593Smuzhiyun dest_keyring, instkey);
1221*4882a593Smuzhiyun
1222*4882a593Smuzhiyun key_put(dest_keyring);
1223*4882a593Smuzhiyun
1224*4882a593Smuzhiyun /* discard the assumed authority if it's just been disabled by
1225*4882a593Smuzhiyun * instantiation of the key */
1226*4882a593Smuzhiyun if (ret == 0)
1227*4882a593Smuzhiyun keyctl_change_reqkey_auth(NULL);
1228*4882a593Smuzhiyun
1229*4882a593Smuzhiyun error2:
1230*4882a593Smuzhiyun kvfree_sensitive(payload, plen);
1231*4882a593Smuzhiyun error:
1232*4882a593Smuzhiyun return ret;
1233*4882a593Smuzhiyun }
1234*4882a593Smuzhiyun
1235*4882a593Smuzhiyun /*
1236*4882a593Smuzhiyun * Instantiate a key with the specified payload and link the key into the
1237*4882a593Smuzhiyun * destination keyring if one is given.
1238*4882a593Smuzhiyun *
1239*4882a593Smuzhiyun * The caller must have the appropriate instantiation permit set for this to
1240*4882a593Smuzhiyun * work (see keyctl_assume_authority). No other permissions are required.
1241*4882a593Smuzhiyun *
1242*4882a593Smuzhiyun * If successful, 0 will be returned.
1243*4882a593Smuzhiyun */
keyctl_instantiate_key(key_serial_t id,const void __user * _payload,size_t plen,key_serial_t ringid)1244*4882a593Smuzhiyun long keyctl_instantiate_key(key_serial_t id,
1245*4882a593Smuzhiyun const void __user *_payload,
1246*4882a593Smuzhiyun size_t plen,
1247*4882a593Smuzhiyun key_serial_t ringid)
1248*4882a593Smuzhiyun {
1249*4882a593Smuzhiyun if (_payload && plen) {
1250*4882a593Smuzhiyun struct iovec iov;
1251*4882a593Smuzhiyun struct iov_iter from;
1252*4882a593Smuzhiyun int ret;
1253*4882a593Smuzhiyun
1254*4882a593Smuzhiyun ret = import_single_range(WRITE, (void __user *)_payload, plen,
1255*4882a593Smuzhiyun &iov, &from);
1256*4882a593Smuzhiyun if (unlikely(ret))
1257*4882a593Smuzhiyun return ret;
1258*4882a593Smuzhiyun
1259*4882a593Smuzhiyun return keyctl_instantiate_key_common(id, &from, ringid);
1260*4882a593Smuzhiyun }
1261*4882a593Smuzhiyun
1262*4882a593Smuzhiyun return keyctl_instantiate_key_common(id, NULL, ringid);
1263*4882a593Smuzhiyun }
1264*4882a593Smuzhiyun
1265*4882a593Smuzhiyun /*
1266*4882a593Smuzhiyun * Instantiate a key with the specified multipart payload and link the key into
1267*4882a593Smuzhiyun * the destination keyring if one is given.
1268*4882a593Smuzhiyun *
1269*4882a593Smuzhiyun * The caller must have the appropriate instantiation permit set for this to
1270*4882a593Smuzhiyun * work (see keyctl_assume_authority). No other permissions are required.
1271*4882a593Smuzhiyun *
1272*4882a593Smuzhiyun * If successful, 0 will be returned.
1273*4882a593Smuzhiyun */
keyctl_instantiate_key_iov(key_serial_t id,const struct iovec __user * _payload_iov,unsigned ioc,key_serial_t ringid)1274*4882a593Smuzhiyun long keyctl_instantiate_key_iov(key_serial_t id,
1275*4882a593Smuzhiyun const struct iovec __user *_payload_iov,
1276*4882a593Smuzhiyun unsigned ioc,
1277*4882a593Smuzhiyun key_serial_t ringid)
1278*4882a593Smuzhiyun {
1279*4882a593Smuzhiyun struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
1280*4882a593Smuzhiyun struct iov_iter from;
1281*4882a593Smuzhiyun long ret;
1282*4882a593Smuzhiyun
1283*4882a593Smuzhiyun if (!_payload_iov)
1284*4882a593Smuzhiyun ioc = 0;
1285*4882a593Smuzhiyun
1286*4882a593Smuzhiyun ret = import_iovec(WRITE, _payload_iov, ioc,
1287*4882a593Smuzhiyun ARRAY_SIZE(iovstack), &iov, &from);
1288*4882a593Smuzhiyun if (ret < 0)
1289*4882a593Smuzhiyun return ret;
1290*4882a593Smuzhiyun ret = keyctl_instantiate_key_common(id, &from, ringid);
1291*4882a593Smuzhiyun kfree(iov);
1292*4882a593Smuzhiyun return ret;
1293*4882a593Smuzhiyun }
1294*4882a593Smuzhiyun
1295*4882a593Smuzhiyun /*
1296*4882a593Smuzhiyun * Negatively instantiate the key with the given timeout (in seconds) and link
1297*4882a593Smuzhiyun * the key into the destination keyring if one is given.
1298*4882a593Smuzhiyun *
1299*4882a593Smuzhiyun * The caller must have the appropriate instantiation permit set for this to
1300*4882a593Smuzhiyun * work (see keyctl_assume_authority). No other permissions are required.
1301*4882a593Smuzhiyun *
1302*4882a593Smuzhiyun * The key and any links to the key will be automatically garbage collected
1303*4882a593Smuzhiyun * after the timeout expires.
1304*4882a593Smuzhiyun *
1305*4882a593Smuzhiyun * Negative keys are used to rate limit repeated request_key() calls by causing
1306*4882a593Smuzhiyun * them to return -ENOKEY until the negative key expires.
1307*4882a593Smuzhiyun *
1308*4882a593Smuzhiyun * If successful, 0 will be returned.
1309*4882a593Smuzhiyun */
keyctl_negate_key(key_serial_t id,unsigned timeout,key_serial_t ringid)1310*4882a593Smuzhiyun long keyctl_negate_key(key_serial_t id, unsigned timeout, key_serial_t ringid)
1311*4882a593Smuzhiyun {
1312*4882a593Smuzhiyun return keyctl_reject_key(id, timeout, ENOKEY, ringid);
1313*4882a593Smuzhiyun }
1314*4882a593Smuzhiyun
1315*4882a593Smuzhiyun /*
1316*4882a593Smuzhiyun * Negatively instantiate the key with the given timeout (in seconds) and error
1317*4882a593Smuzhiyun * code and link the key into the destination keyring if one is given.
1318*4882a593Smuzhiyun *
1319*4882a593Smuzhiyun * The caller must have the appropriate instantiation permit set for this to
1320*4882a593Smuzhiyun * work (see keyctl_assume_authority). No other permissions are required.
1321*4882a593Smuzhiyun *
1322*4882a593Smuzhiyun * The key and any links to the key will be automatically garbage collected
1323*4882a593Smuzhiyun * after the timeout expires.
1324*4882a593Smuzhiyun *
1325*4882a593Smuzhiyun * Negative keys are used to rate limit repeated request_key() calls by causing
1326*4882a593Smuzhiyun * them to return the specified error code until the negative key expires.
1327*4882a593Smuzhiyun *
1328*4882a593Smuzhiyun * If successful, 0 will be returned.
1329*4882a593Smuzhiyun */
keyctl_reject_key(key_serial_t id,unsigned timeout,unsigned error,key_serial_t ringid)1330*4882a593Smuzhiyun long keyctl_reject_key(key_serial_t id, unsigned timeout, unsigned error,
1331*4882a593Smuzhiyun key_serial_t ringid)
1332*4882a593Smuzhiyun {
1333*4882a593Smuzhiyun const struct cred *cred = current_cred();
1334*4882a593Smuzhiyun struct request_key_auth *rka;
1335*4882a593Smuzhiyun struct key *instkey, *dest_keyring;
1336*4882a593Smuzhiyun long ret;
1337*4882a593Smuzhiyun
1338*4882a593Smuzhiyun kenter("%d,%u,%u,%d", id, timeout, error, ringid);
1339*4882a593Smuzhiyun
1340*4882a593Smuzhiyun /* must be a valid error code and mustn't be a kernel special */
1341*4882a593Smuzhiyun if (error <= 0 ||
1342*4882a593Smuzhiyun error >= MAX_ERRNO ||
1343*4882a593Smuzhiyun error == ERESTARTSYS ||
1344*4882a593Smuzhiyun error == ERESTARTNOINTR ||
1345*4882a593Smuzhiyun error == ERESTARTNOHAND ||
1346*4882a593Smuzhiyun error == ERESTART_RESTARTBLOCK)
1347*4882a593Smuzhiyun return -EINVAL;
1348*4882a593Smuzhiyun
1349*4882a593Smuzhiyun /* the appropriate instantiation authorisation key must have been
1350*4882a593Smuzhiyun * assumed before calling this */
1351*4882a593Smuzhiyun ret = -EPERM;
1352*4882a593Smuzhiyun instkey = cred->request_key_auth;
1353*4882a593Smuzhiyun if (!instkey)
1354*4882a593Smuzhiyun goto error;
1355*4882a593Smuzhiyun
1356*4882a593Smuzhiyun rka = instkey->payload.data[0];
1357*4882a593Smuzhiyun if (rka->target_key->serial != id)
1358*4882a593Smuzhiyun goto error;
1359*4882a593Smuzhiyun
1360*4882a593Smuzhiyun /* find the destination keyring if present (which must also be
1361*4882a593Smuzhiyun * writable) */
1362*4882a593Smuzhiyun ret = get_instantiation_keyring(ringid, rka, &dest_keyring);
1363*4882a593Smuzhiyun if (ret < 0)
1364*4882a593Smuzhiyun goto error;
1365*4882a593Smuzhiyun
1366*4882a593Smuzhiyun /* instantiate the key and link it into a keyring */
1367*4882a593Smuzhiyun ret = key_reject_and_link(rka->target_key, timeout, error,
1368*4882a593Smuzhiyun dest_keyring, instkey);
1369*4882a593Smuzhiyun
1370*4882a593Smuzhiyun key_put(dest_keyring);
1371*4882a593Smuzhiyun
1372*4882a593Smuzhiyun /* discard the assumed authority if it's just been disabled by
1373*4882a593Smuzhiyun * instantiation of the key */
1374*4882a593Smuzhiyun if (ret == 0)
1375*4882a593Smuzhiyun keyctl_change_reqkey_auth(NULL);
1376*4882a593Smuzhiyun
1377*4882a593Smuzhiyun error:
1378*4882a593Smuzhiyun return ret;
1379*4882a593Smuzhiyun }
1380*4882a593Smuzhiyun
1381*4882a593Smuzhiyun /*
1382*4882a593Smuzhiyun * Read or set the default keyring in which request_key() will cache keys and
1383*4882a593Smuzhiyun * return the old setting.
1384*4882a593Smuzhiyun *
1385*4882a593Smuzhiyun * If a thread or process keyring is specified then it will be created if it
1386*4882a593Smuzhiyun * doesn't yet exist. The old setting will be returned if successful.
1387*4882a593Smuzhiyun */
keyctl_set_reqkey_keyring(int reqkey_defl)1388*4882a593Smuzhiyun long keyctl_set_reqkey_keyring(int reqkey_defl)
1389*4882a593Smuzhiyun {
1390*4882a593Smuzhiyun struct cred *new;
1391*4882a593Smuzhiyun int ret, old_setting;
1392*4882a593Smuzhiyun
1393*4882a593Smuzhiyun old_setting = current_cred_xxx(jit_keyring);
1394*4882a593Smuzhiyun
1395*4882a593Smuzhiyun if (reqkey_defl == KEY_REQKEY_DEFL_NO_CHANGE)
1396*4882a593Smuzhiyun return old_setting;
1397*4882a593Smuzhiyun
1398*4882a593Smuzhiyun new = prepare_creds();
1399*4882a593Smuzhiyun if (!new)
1400*4882a593Smuzhiyun return -ENOMEM;
1401*4882a593Smuzhiyun
1402*4882a593Smuzhiyun switch (reqkey_defl) {
1403*4882a593Smuzhiyun case KEY_REQKEY_DEFL_THREAD_KEYRING:
1404*4882a593Smuzhiyun ret = install_thread_keyring_to_cred(new);
1405*4882a593Smuzhiyun if (ret < 0)
1406*4882a593Smuzhiyun goto error;
1407*4882a593Smuzhiyun goto set;
1408*4882a593Smuzhiyun
1409*4882a593Smuzhiyun case KEY_REQKEY_DEFL_PROCESS_KEYRING:
1410*4882a593Smuzhiyun ret = install_process_keyring_to_cred(new);
1411*4882a593Smuzhiyun if (ret < 0)
1412*4882a593Smuzhiyun goto error;
1413*4882a593Smuzhiyun goto set;
1414*4882a593Smuzhiyun
1415*4882a593Smuzhiyun case KEY_REQKEY_DEFL_DEFAULT:
1416*4882a593Smuzhiyun case KEY_REQKEY_DEFL_SESSION_KEYRING:
1417*4882a593Smuzhiyun case KEY_REQKEY_DEFL_USER_KEYRING:
1418*4882a593Smuzhiyun case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
1419*4882a593Smuzhiyun case KEY_REQKEY_DEFL_REQUESTOR_KEYRING:
1420*4882a593Smuzhiyun goto set;
1421*4882a593Smuzhiyun
1422*4882a593Smuzhiyun case KEY_REQKEY_DEFL_NO_CHANGE:
1423*4882a593Smuzhiyun case KEY_REQKEY_DEFL_GROUP_KEYRING:
1424*4882a593Smuzhiyun default:
1425*4882a593Smuzhiyun ret = -EINVAL;
1426*4882a593Smuzhiyun goto error;
1427*4882a593Smuzhiyun }
1428*4882a593Smuzhiyun
1429*4882a593Smuzhiyun set:
1430*4882a593Smuzhiyun new->jit_keyring = reqkey_defl;
1431*4882a593Smuzhiyun commit_creds(new);
1432*4882a593Smuzhiyun return old_setting;
1433*4882a593Smuzhiyun error:
1434*4882a593Smuzhiyun abort_creds(new);
1435*4882a593Smuzhiyun return ret;
1436*4882a593Smuzhiyun }
1437*4882a593Smuzhiyun
1438*4882a593Smuzhiyun /*
1439*4882a593Smuzhiyun * Set or clear the timeout on a key.
1440*4882a593Smuzhiyun *
1441*4882a593Smuzhiyun * Either the key must grant the caller Setattr permission or else the caller
1442*4882a593Smuzhiyun * must hold an instantiation authorisation token for the key.
1443*4882a593Smuzhiyun *
1444*4882a593Smuzhiyun * The timeout is either 0 to clear the timeout, or a number of seconds from
1445*4882a593Smuzhiyun * the current time. The key and any links to the key will be automatically
1446*4882a593Smuzhiyun * garbage collected after the timeout expires.
1447*4882a593Smuzhiyun *
1448*4882a593Smuzhiyun * Keys with KEY_FLAG_KEEP set should not be timed out.
1449*4882a593Smuzhiyun *
1450*4882a593Smuzhiyun * If successful, 0 is returned.
1451*4882a593Smuzhiyun */
keyctl_set_timeout(key_serial_t id,unsigned timeout)1452*4882a593Smuzhiyun long keyctl_set_timeout(key_serial_t id, unsigned timeout)
1453*4882a593Smuzhiyun {
1454*4882a593Smuzhiyun struct key *key, *instkey;
1455*4882a593Smuzhiyun key_ref_t key_ref;
1456*4882a593Smuzhiyun long ret;
1457*4882a593Smuzhiyun
1458*4882a593Smuzhiyun key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
1459*4882a593Smuzhiyun KEY_NEED_SETATTR);
1460*4882a593Smuzhiyun if (IS_ERR(key_ref)) {
1461*4882a593Smuzhiyun /* setting the timeout on a key under construction is permitted
1462*4882a593Smuzhiyun * if we have the authorisation token handy */
1463*4882a593Smuzhiyun if (PTR_ERR(key_ref) == -EACCES) {
1464*4882a593Smuzhiyun instkey = key_get_instantiation_authkey(id);
1465*4882a593Smuzhiyun if (!IS_ERR(instkey)) {
1466*4882a593Smuzhiyun key_put(instkey);
1467*4882a593Smuzhiyun key_ref = lookup_user_key(id,
1468*4882a593Smuzhiyun KEY_LOOKUP_PARTIAL,
1469*4882a593Smuzhiyun KEY_AUTHTOKEN_OVERRIDE);
1470*4882a593Smuzhiyun if (!IS_ERR(key_ref))
1471*4882a593Smuzhiyun goto okay;
1472*4882a593Smuzhiyun }
1473*4882a593Smuzhiyun }
1474*4882a593Smuzhiyun
1475*4882a593Smuzhiyun ret = PTR_ERR(key_ref);
1476*4882a593Smuzhiyun goto error;
1477*4882a593Smuzhiyun }
1478*4882a593Smuzhiyun
1479*4882a593Smuzhiyun okay:
1480*4882a593Smuzhiyun key = key_ref_to_ptr(key_ref);
1481*4882a593Smuzhiyun ret = 0;
1482*4882a593Smuzhiyun if (test_bit(KEY_FLAG_KEEP, &key->flags)) {
1483*4882a593Smuzhiyun ret = -EPERM;
1484*4882a593Smuzhiyun } else {
1485*4882a593Smuzhiyun key_set_timeout(key, timeout);
1486*4882a593Smuzhiyun notify_key(key, NOTIFY_KEY_SETATTR, 0);
1487*4882a593Smuzhiyun }
1488*4882a593Smuzhiyun key_put(key);
1489*4882a593Smuzhiyun
1490*4882a593Smuzhiyun error:
1491*4882a593Smuzhiyun return ret;
1492*4882a593Smuzhiyun }
1493*4882a593Smuzhiyun
1494*4882a593Smuzhiyun /*
1495*4882a593Smuzhiyun * Assume (or clear) the authority to instantiate the specified key.
1496*4882a593Smuzhiyun *
1497*4882a593Smuzhiyun * This sets the authoritative token currently in force for key instantiation.
1498*4882a593Smuzhiyun * This must be done for a key to be instantiated. It has the effect of making
1499*4882a593Smuzhiyun * available all the keys from the caller of the request_key() that created a
1500*4882a593Smuzhiyun * key to request_key() calls made by the caller of this function.
1501*4882a593Smuzhiyun *
1502*4882a593Smuzhiyun * The caller must have the instantiation key in their process keyrings with a
1503*4882a593Smuzhiyun * Search permission grant available to the caller.
1504*4882a593Smuzhiyun *
1505*4882a593Smuzhiyun * If the ID given is 0, then the setting will be cleared and 0 returned.
1506*4882a593Smuzhiyun *
1507*4882a593Smuzhiyun * If the ID given has a matching an authorisation key, then that key will be
1508*4882a593Smuzhiyun * set and its ID will be returned. The authorisation key can be read to get
1509*4882a593Smuzhiyun * the callout information passed to request_key().
1510*4882a593Smuzhiyun */
keyctl_assume_authority(key_serial_t id)1511*4882a593Smuzhiyun long keyctl_assume_authority(key_serial_t id)
1512*4882a593Smuzhiyun {
1513*4882a593Smuzhiyun struct key *authkey;
1514*4882a593Smuzhiyun long ret;
1515*4882a593Smuzhiyun
1516*4882a593Smuzhiyun /* special key IDs aren't permitted */
1517*4882a593Smuzhiyun ret = -EINVAL;
1518*4882a593Smuzhiyun if (id < 0)
1519*4882a593Smuzhiyun goto error;
1520*4882a593Smuzhiyun
1521*4882a593Smuzhiyun /* we divest ourselves of authority if given an ID of 0 */
1522*4882a593Smuzhiyun if (id == 0) {
1523*4882a593Smuzhiyun ret = keyctl_change_reqkey_auth(NULL);
1524*4882a593Smuzhiyun goto error;
1525*4882a593Smuzhiyun }
1526*4882a593Smuzhiyun
1527*4882a593Smuzhiyun /* attempt to assume the authority temporarily granted to us whilst we
1528*4882a593Smuzhiyun * instantiate the specified key
1529*4882a593Smuzhiyun * - the authorisation key must be in the current task's keyrings
1530*4882a593Smuzhiyun * somewhere
1531*4882a593Smuzhiyun */
1532*4882a593Smuzhiyun authkey = key_get_instantiation_authkey(id);
1533*4882a593Smuzhiyun if (IS_ERR(authkey)) {
1534*4882a593Smuzhiyun ret = PTR_ERR(authkey);
1535*4882a593Smuzhiyun goto error;
1536*4882a593Smuzhiyun }
1537*4882a593Smuzhiyun
1538*4882a593Smuzhiyun ret = keyctl_change_reqkey_auth(authkey);
1539*4882a593Smuzhiyun if (ret == 0)
1540*4882a593Smuzhiyun ret = authkey->serial;
1541*4882a593Smuzhiyun key_put(authkey);
1542*4882a593Smuzhiyun error:
1543*4882a593Smuzhiyun return ret;
1544*4882a593Smuzhiyun }
1545*4882a593Smuzhiyun
1546*4882a593Smuzhiyun /*
1547*4882a593Smuzhiyun * Get a key's the LSM security label.
1548*4882a593Smuzhiyun *
1549*4882a593Smuzhiyun * The key must grant the caller View permission for this to work.
1550*4882a593Smuzhiyun *
1551*4882a593Smuzhiyun * If there's a buffer, then up to buflen bytes of data will be placed into it.
1552*4882a593Smuzhiyun *
1553*4882a593Smuzhiyun * If successful, the amount of information available will be returned,
1554*4882a593Smuzhiyun * irrespective of how much was copied (including the terminal NUL).
1555*4882a593Smuzhiyun */
keyctl_get_security(key_serial_t keyid,char __user * buffer,size_t buflen)1556*4882a593Smuzhiyun long keyctl_get_security(key_serial_t keyid,
1557*4882a593Smuzhiyun char __user *buffer,
1558*4882a593Smuzhiyun size_t buflen)
1559*4882a593Smuzhiyun {
1560*4882a593Smuzhiyun struct key *key, *instkey;
1561*4882a593Smuzhiyun key_ref_t key_ref;
1562*4882a593Smuzhiyun char *context;
1563*4882a593Smuzhiyun long ret;
1564*4882a593Smuzhiyun
1565*4882a593Smuzhiyun key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_NEED_VIEW);
1566*4882a593Smuzhiyun if (IS_ERR(key_ref)) {
1567*4882a593Smuzhiyun if (PTR_ERR(key_ref) != -EACCES)
1568*4882a593Smuzhiyun return PTR_ERR(key_ref);
1569*4882a593Smuzhiyun
1570*4882a593Smuzhiyun /* viewing a key under construction is also permitted if we
1571*4882a593Smuzhiyun * have the authorisation token handy */
1572*4882a593Smuzhiyun instkey = key_get_instantiation_authkey(keyid);
1573*4882a593Smuzhiyun if (IS_ERR(instkey))
1574*4882a593Smuzhiyun return PTR_ERR(instkey);
1575*4882a593Smuzhiyun key_put(instkey);
1576*4882a593Smuzhiyun
1577*4882a593Smuzhiyun key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL,
1578*4882a593Smuzhiyun KEY_AUTHTOKEN_OVERRIDE);
1579*4882a593Smuzhiyun if (IS_ERR(key_ref))
1580*4882a593Smuzhiyun return PTR_ERR(key_ref);
1581*4882a593Smuzhiyun }
1582*4882a593Smuzhiyun
1583*4882a593Smuzhiyun key = key_ref_to_ptr(key_ref);
1584*4882a593Smuzhiyun ret = security_key_getsecurity(key, &context);
1585*4882a593Smuzhiyun if (ret == 0) {
1586*4882a593Smuzhiyun /* if no information was returned, give userspace an empty
1587*4882a593Smuzhiyun * string */
1588*4882a593Smuzhiyun ret = 1;
1589*4882a593Smuzhiyun if (buffer && buflen > 0 &&
1590*4882a593Smuzhiyun copy_to_user(buffer, "", 1) != 0)
1591*4882a593Smuzhiyun ret = -EFAULT;
1592*4882a593Smuzhiyun } else if (ret > 0) {
1593*4882a593Smuzhiyun /* return as much data as there's room for */
1594*4882a593Smuzhiyun if (buffer && buflen > 0) {
1595*4882a593Smuzhiyun if (buflen > ret)
1596*4882a593Smuzhiyun buflen = ret;
1597*4882a593Smuzhiyun
1598*4882a593Smuzhiyun if (copy_to_user(buffer, context, buflen) != 0)
1599*4882a593Smuzhiyun ret = -EFAULT;
1600*4882a593Smuzhiyun }
1601*4882a593Smuzhiyun
1602*4882a593Smuzhiyun kfree(context);
1603*4882a593Smuzhiyun }
1604*4882a593Smuzhiyun
1605*4882a593Smuzhiyun key_ref_put(key_ref);
1606*4882a593Smuzhiyun return ret;
1607*4882a593Smuzhiyun }
1608*4882a593Smuzhiyun
1609*4882a593Smuzhiyun /*
1610*4882a593Smuzhiyun * Attempt to install the calling process's session keyring on the process's
1611*4882a593Smuzhiyun * parent process.
1612*4882a593Smuzhiyun *
1613*4882a593Smuzhiyun * The keyring must exist and must grant the caller LINK permission, and the
1614*4882a593Smuzhiyun * parent process must be single-threaded and must have the same effective
1615*4882a593Smuzhiyun * ownership as this process and mustn't be SUID/SGID.
1616*4882a593Smuzhiyun *
1617*4882a593Smuzhiyun * The keyring will be emplaced on the parent when it next resumes userspace.
1618*4882a593Smuzhiyun *
1619*4882a593Smuzhiyun * If successful, 0 will be returned.
1620*4882a593Smuzhiyun */
keyctl_session_to_parent(void)1621*4882a593Smuzhiyun long keyctl_session_to_parent(void)
1622*4882a593Smuzhiyun {
1623*4882a593Smuzhiyun struct task_struct *me, *parent;
1624*4882a593Smuzhiyun const struct cred *mycred, *pcred;
1625*4882a593Smuzhiyun struct callback_head *newwork, *oldwork;
1626*4882a593Smuzhiyun key_ref_t keyring_r;
1627*4882a593Smuzhiyun struct cred *cred;
1628*4882a593Smuzhiyun int ret;
1629*4882a593Smuzhiyun
1630*4882a593Smuzhiyun keyring_r = lookup_user_key(KEY_SPEC_SESSION_KEYRING, 0, KEY_NEED_LINK);
1631*4882a593Smuzhiyun if (IS_ERR(keyring_r))
1632*4882a593Smuzhiyun return PTR_ERR(keyring_r);
1633*4882a593Smuzhiyun
1634*4882a593Smuzhiyun ret = -ENOMEM;
1635*4882a593Smuzhiyun
1636*4882a593Smuzhiyun /* our parent is going to need a new cred struct, a new tgcred struct
1637*4882a593Smuzhiyun * and new security data, so we allocate them here to prevent ENOMEM in
1638*4882a593Smuzhiyun * our parent */
1639*4882a593Smuzhiyun cred = cred_alloc_blank();
1640*4882a593Smuzhiyun if (!cred)
1641*4882a593Smuzhiyun goto error_keyring;
1642*4882a593Smuzhiyun newwork = &cred->rcu;
1643*4882a593Smuzhiyun
1644*4882a593Smuzhiyun cred->session_keyring = key_ref_to_ptr(keyring_r);
1645*4882a593Smuzhiyun keyring_r = NULL;
1646*4882a593Smuzhiyun init_task_work(newwork, key_change_session_keyring);
1647*4882a593Smuzhiyun
1648*4882a593Smuzhiyun me = current;
1649*4882a593Smuzhiyun rcu_read_lock();
1650*4882a593Smuzhiyun write_lock_irq(&tasklist_lock);
1651*4882a593Smuzhiyun
1652*4882a593Smuzhiyun ret = -EPERM;
1653*4882a593Smuzhiyun oldwork = NULL;
1654*4882a593Smuzhiyun parent = rcu_dereference_protected(me->real_parent,
1655*4882a593Smuzhiyun lockdep_is_held(&tasklist_lock));
1656*4882a593Smuzhiyun
1657*4882a593Smuzhiyun /* the parent mustn't be init and mustn't be a kernel thread */
1658*4882a593Smuzhiyun if (parent->pid <= 1 || !parent->mm)
1659*4882a593Smuzhiyun goto unlock;
1660*4882a593Smuzhiyun
1661*4882a593Smuzhiyun /* the parent must be single threaded */
1662*4882a593Smuzhiyun if (!thread_group_empty(parent))
1663*4882a593Smuzhiyun goto unlock;
1664*4882a593Smuzhiyun
1665*4882a593Smuzhiyun /* the parent and the child must have different session keyrings or
1666*4882a593Smuzhiyun * there's no point */
1667*4882a593Smuzhiyun mycred = current_cred();
1668*4882a593Smuzhiyun pcred = __task_cred(parent);
1669*4882a593Smuzhiyun if (mycred == pcred ||
1670*4882a593Smuzhiyun mycred->session_keyring == pcred->session_keyring) {
1671*4882a593Smuzhiyun ret = 0;
1672*4882a593Smuzhiyun goto unlock;
1673*4882a593Smuzhiyun }
1674*4882a593Smuzhiyun
1675*4882a593Smuzhiyun /* the parent must have the same effective ownership and mustn't be
1676*4882a593Smuzhiyun * SUID/SGID */
1677*4882a593Smuzhiyun if (!uid_eq(pcred->uid, mycred->euid) ||
1678*4882a593Smuzhiyun !uid_eq(pcred->euid, mycred->euid) ||
1679*4882a593Smuzhiyun !uid_eq(pcred->suid, mycred->euid) ||
1680*4882a593Smuzhiyun !gid_eq(pcred->gid, mycred->egid) ||
1681*4882a593Smuzhiyun !gid_eq(pcred->egid, mycred->egid) ||
1682*4882a593Smuzhiyun !gid_eq(pcred->sgid, mycred->egid))
1683*4882a593Smuzhiyun goto unlock;
1684*4882a593Smuzhiyun
1685*4882a593Smuzhiyun /* the keyrings must have the same UID */
1686*4882a593Smuzhiyun if ((pcred->session_keyring &&
1687*4882a593Smuzhiyun !uid_eq(pcred->session_keyring->uid, mycred->euid)) ||
1688*4882a593Smuzhiyun !uid_eq(mycred->session_keyring->uid, mycred->euid))
1689*4882a593Smuzhiyun goto unlock;
1690*4882a593Smuzhiyun
1691*4882a593Smuzhiyun /* cancel an already pending keyring replacement */
1692*4882a593Smuzhiyun oldwork = task_work_cancel(parent, key_change_session_keyring);
1693*4882a593Smuzhiyun
1694*4882a593Smuzhiyun /* the replacement session keyring is applied just prior to userspace
1695*4882a593Smuzhiyun * restarting */
1696*4882a593Smuzhiyun ret = task_work_add(parent, newwork, TWA_RESUME);
1697*4882a593Smuzhiyun if (!ret)
1698*4882a593Smuzhiyun newwork = NULL;
1699*4882a593Smuzhiyun unlock:
1700*4882a593Smuzhiyun write_unlock_irq(&tasklist_lock);
1701*4882a593Smuzhiyun rcu_read_unlock();
1702*4882a593Smuzhiyun if (oldwork)
1703*4882a593Smuzhiyun put_cred(container_of(oldwork, struct cred, rcu));
1704*4882a593Smuzhiyun if (newwork)
1705*4882a593Smuzhiyun put_cred(cred);
1706*4882a593Smuzhiyun return ret;
1707*4882a593Smuzhiyun
1708*4882a593Smuzhiyun error_keyring:
1709*4882a593Smuzhiyun key_ref_put(keyring_r);
1710*4882a593Smuzhiyun return ret;
1711*4882a593Smuzhiyun }
1712*4882a593Smuzhiyun
1713*4882a593Smuzhiyun /*
1714*4882a593Smuzhiyun * Apply a restriction to a given keyring.
1715*4882a593Smuzhiyun *
1716*4882a593Smuzhiyun * The caller must have Setattr permission to change keyring restrictions.
1717*4882a593Smuzhiyun *
1718*4882a593Smuzhiyun * The requested type name may be a NULL pointer to reject all attempts
1719*4882a593Smuzhiyun * to link to the keyring. In this case, _restriction must also be NULL.
1720*4882a593Smuzhiyun * Otherwise, both _type and _restriction must be non-NULL.
1721*4882a593Smuzhiyun *
1722*4882a593Smuzhiyun * Returns 0 if successful.
1723*4882a593Smuzhiyun */
keyctl_restrict_keyring(key_serial_t id,const char __user * _type,const char __user * _restriction)1724*4882a593Smuzhiyun long keyctl_restrict_keyring(key_serial_t id, const char __user *_type,
1725*4882a593Smuzhiyun const char __user *_restriction)
1726*4882a593Smuzhiyun {
1727*4882a593Smuzhiyun key_ref_t key_ref;
1728*4882a593Smuzhiyun char type[32];
1729*4882a593Smuzhiyun char *restriction = NULL;
1730*4882a593Smuzhiyun long ret;
1731*4882a593Smuzhiyun
1732*4882a593Smuzhiyun key_ref = lookup_user_key(id, 0, KEY_NEED_SETATTR);
1733*4882a593Smuzhiyun if (IS_ERR(key_ref))
1734*4882a593Smuzhiyun return PTR_ERR(key_ref);
1735*4882a593Smuzhiyun
1736*4882a593Smuzhiyun ret = -EINVAL;
1737*4882a593Smuzhiyun if (_type) {
1738*4882a593Smuzhiyun if (!_restriction)
1739*4882a593Smuzhiyun goto error;
1740*4882a593Smuzhiyun
1741*4882a593Smuzhiyun ret = key_get_type_from_user(type, _type, sizeof(type));
1742*4882a593Smuzhiyun if (ret < 0)
1743*4882a593Smuzhiyun goto error;
1744*4882a593Smuzhiyun
1745*4882a593Smuzhiyun restriction = strndup_user(_restriction, PAGE_SIZE);
1746*4882a593Smuzhiyun if (IS_ERR(restriction)) {
1747*4882a593Smuzhiyun ret = PTR_ERR(restriction);
1748*4882a593Smuzhiyun goto error;
1749*4882a593Smuzhiyun }
1750*4882a593Smuzhiyun } else {
1751*4882a593Smuzhiyun if (_restriction)
1752*4882a593Smuzhiyun goto error;
1753*4882a593Smuzhiyun }
1754*4882a593Smuzhiyun
1755*4882a593Smuzhiyun ret = keyring_restrict(key_ref, _type ? type : NULL, restriction);
1756*4882a593Smuzhiyun kfree(restriction);
1757*4882a593Smuzhiyun error:
1758*4882a593Smuzhiyun key_ref_put(key_ref);
1759*4882a593Smuzhiyun return ret;
1760*4882a593Smuzhiyun }
1761*4882a593Smuzhiyun
1762*4882a593Smuzhiyun #ifdef CONFIG_KEY_NOTIFICATIONS
1763*4882a593Smuzhiyun /*
1764*4882a593Smuzhiyun * Watch for changes to a key.
1765*4882a593Smuzhiyun *
1766*4882a593Smuzhiyun * The caller must have View permission to watch a key or keyring.
1767*4882a593Smuzhiyun */
keyctl_watch_key(key_serial_t id,int watch_queue_fd,int watch_id)1768*4882a593Smuzhiyun long keyctl_watch_key(key_serial_t id, int watch_queue_fd, int watch_id)
1769*4882a593Smuzhiyun {
1770*4882a593Smuzhiyun struct watch_queue *wqueue;
1771*4882a593Smuzhiyun struct watch_list *wlist = NULL;
1772*4882a593Smuzhiyun struct watch *watch = NULL;
1773*4882a593Smuzhiyun struct key *key;
1774*4882a593Smuzhiyun key_ref_t key_ref;
1775*4882a593Smuzhiyun long ret;
1776*4882a593Smuzhiyun
1777*4882a593Smuzhiyun if (watch_id < -1 || watch_id > 0xff)
1778*4882a593Smuzhiyun return -EINVAL;
1779*4882a593Smuzhiyun
1780*4882a593Smuzhiyun key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE, KEY_NEED_VIEW);
1781*4882a593Smuzhiyun if (IS_ERR(key_ref))
1782*4882a593Smuzhiyun return PTR_ERR(key_ref);
1783*4882a593Smuzhiyun key = key_ref_to_ptr(key_ref);
1784*4882a593Smuzhiyun
1785*4882a593Smuzhiyun wqueue = get_watch_queue(watch_queue_fd);
1786*4882a593Smuzhiyun if (IS_ERR(wqueue)) {
1787*4882a593Smuzhiyun ret = PTR_ERR(wqueue);
1788*4882a593Smuzhiyun goto err_key;
1789*4882a593Smuzhiyun }
1790*4882a593Smuzhiyun
1791*4882a593Smuzhiyun if (watch_id >= 0) {
1792*4882a593Smuzhiyun ret = -ENOMEM;
1793*4882a593Smuzhiyun if (!key->watchers) {
1794*4882a593Smuzhiyun wlist = kzalloc(sizeof(*wlist), GFP_KERNEL);
1795*4882a593Smuzhiyun if (!wlist)
1796*4882a593Smuzhiyun goto err_wqueue;
1797*4882a593Smuzhiyun init_watch_list(wlist, NULL);
1798*4882a593Smuzhiyun }
1799*4882a593Smuzhiyun
1800*4882a593Smuzhiyun watch = kzalloc(sizeof(*watch), GFP_KERNEL);
1801*4882a593Smuzhiyun if (!watch)
1802*4882a593Smuzhiyun goto err_wlist;
1803*4882a593Smuzhiyun
1804*4882a593Smuzhiyun init_watch(watch, wqueue);
1805*4882a593Smuzhiyun watch->id = key->serial;
1806*4882a593Smuzhiyun watch->info_id = (u32)watch_id << WATCH_INFO_ID__SHIFT;
1807*4882a593Smuzhiyun
1808*4882a593Smuzhiyun ret = security_watch_key(key);
1809*4882a593Smuzhiyun if (ret < 0)
1810*4882a593Smuzhiyun goto err_watch;
1811*4882a593Smuzhiyun
1812*4882a593Smuzhiyun down_write(&key->sem);
1813*4882a593Smuzhiyun if (!key->watchers) {
1814*4882a593Smuzhiyun key->watchers = wlist;
1815*4882a593Smuzhiyun wlist = NULL;
1816*4882a593Smuzhiyun }
1817*4882a593Smuzhiyun
1818*4882a593Smuzhiyun ret = add_watch_to_object(watch, key->watchers);
1819*4882a593Smuzhiyun up_write(&key->sem);
1820*4882a593Smuzhiyun
1821*4882a593Smuzhiyun if (ret == 0)
1822*4882a593Smuzhiyun watch = NULL;
1823*4882a593Smuzhiyun } else {
1824*4882a593Smuzhiyun ret = -EBADSLT;
1825*4882a593Smuzhiyun if (key->watchers) {
1826*4882a593Smuzhiyun down_write(&key->sem);
1827*4882a593Smuzhiyun ret = remove_watch_from_object(key->watchers,
1828*4882a593Smuzhiyun wqueue, key_serial(key),
1829*4882a593Smuzhiyun false);
1830*4882a593Smuzhiyun up_write(&key->sem);
1831*4882a593Smuzhiyun }
1832*4882a593Smuzhiyun }
1833*4882a593Smuzhiyun
1834*4882a593Smuzhiyun err_watch:
1835*4882a593Smuzhiyun kfree(watch);
1836*4882a593Smuzhiyun err_wlist:
1837*4882a593Smuzhiyun kfree(wlist);
1838*4882a593Smuzhiyun err_wqueue:
1839*4882a593Smuzhiyun put_watch_queue(wqueue);
1840*4882a593Smuzhiyun err_key:
1841*4882a593Smuzhiyun key_put(key);
1842*4882a593Smuzhiyun return ret;
1843*4882a593Smuzhiyun }
1844*4882a593Smuzhiyun #endif /* CONFIG_KEY_NOTIFICATIONS */
1845*4882a593Smuzhiyun
1846*4882a593Smuzhiyun /*
1847*4882a593Smuzhiyun * Get keyrings subsystem capabilities.
1848*4882a593Smuzhiyun */
keyctl_capabilities(unsigned char __user * _buffer,size_t buflen)1849*4882a593Smuzhiyun long keyctl_capabilities(unsigned char __user *_buffer, size_t buflen)
1850*4882a593Smuzhiyun {
1851*4882a593Smuzhiyun size_t size = buflen;
1852*4882a593Smuzhiyun
1853*4882a593Smuzhiyun if (size > 0) {
1854*4882a593Smuzhiyun if (size > sizeof(keyrings_capabilities))
1855*4882a593Smuzhiyun size = sizeof(keyrings_capabilities);
1856*4882a593Smuzhiyun if (copy_to_user(_buffer, keyrings_capabilities, size) != 0)
1857*4882a593Smuzhiyun return -EFAULT;
1858*4882a593Smuzhiyun if (size < buflen &&
1859*4882a593Smuzhiyun clear_user(_buffer + size, buflen - size) != 0)
1860*4882a593Smuzhiyun return -EFAULT;
1861*4882a593Smuzhiyun }
1862*4882a593Smuzhiyun
1863*4882a593Smuzhiyun return sizeof(keyrings_capabilities);
1864*4882a593Smuzhiyun }
1865*4882a593Smuzhiyun
1866*4882a593Smuzhiyun /*
1867*4882a593Smuzhiyun * The key control system call
1868*4882a593Smuzhiyun */
SYSCALL_DEFINE5(keyctl,int,option,unsigned long,arg2,unsigned long,arg3,unsigned long,arg4,unsigned long,arg5)1869*4882a593Smuzhiyun SYSCALL_DEFINE5(keyctl, int, option, unsigned long, arg2, unsigned long, arg3,
1870*4882a593Smuzhiyun unsigned long, arg4, unsigned long, arg5)
1871*4882a593Smuzhiyun {
1872*4882a593Smuzhiyun switch (option) {
1873*4882a593Smuzhiyun case KEYCTL_GET_KEYRING_ID:
1874*4882a593Smuzhiyun return keyctl_get_keyring_ID((key_serial_t) arg2,
1875*4882a593Smuzhiyun (int) arg3);
1876*4882a593Smuzhiyun
1877*4882a593Smuzhiyun case KEYCTL_JOIN_SESSION_KEYRING:
1878*4882a593Smuzhiyun return keyctl_join_session_keyring((const char __user *) arg2);
1879*4882a593Smuzhiyun
1880*4882a593Smuzhiyun case KEYCTL_UPDATE:
1881*4882a593Smuzhiyun return keyctl_update_key((key_serial_t) arg2,
1882*4882a593Smuzhiyun (const void __user *) arg3,
1883*4882a593Smuzhiyun (size_t) arg4);
1884*4882a593Smuzhiyun
1885*4882a593Smuzhiyun case KEYCTL_REVOKE:
1886*4882a593Smuzhiyun return keyctl_revoke_key((key_serial_t) arg2);
1887*4882a593Smuzhiyun
1888*4882a593Smuzhiyun case KEYCTL_DESCRIBE:
1889*4882a593Smuzhiyun return keyctl_describe_key((key_serial_t) arg2,
1890*4882a593Smuzhiyun (char __user *) arg3,
1891*4882a593Smuzhiyun (unsigned) arg4);
1892*4882a593Smuzhiyun
1893*4882a593Smuzhiyun case KEYCTL_CLEAR:
1894*4882a593Smuzhiyun return keyctl_keyring_clear((key_serial_t) arg2);
1895*4882a593Smuzhiyun
1896*4882a593Smuzhiyun case KEYCTL_LINK:
1897*4882a593Smuzhiyun return keyctl_keyring_link((key_serial_t) arg2,
1898*4882a593Smuzhiyun (key_serial_t) arg3);
1899*4882a593Smuzhiyun
1900*4882a593Smuzhiyun case KEYCTL_UNLINK:
1901*4882a593Smuzhiyun return keyctl_keyring_unlink((key_serial_t) arg2,
1902*4882a593Smuzhiyun (key_serial_t) arg3);
1903*4882a593Smuzhiyun
1904*4882a593Smuzhiyun case KEYCTL_SEARCH:
1905*4882a593Smuzhiyun return keyctl_keyring_search((key_serial_t) arg2,
1906*4882a593Smuzhiyun (const char __user *) arg3,
1907*4882a593Smuzhiyun (const char __user *) arg4,
1908*4882a593Smuzhiyun (key_serial_t) arg5);
1909*4882a593Smuzhiyun
1910*4882a593Smuzhiyun case KEYCTL_READ:
1911*4882a593Smuzhiyun return keyctl_read_key((key_serial_t) arg2,
1912*4882a593Smuzhiyun (char __user *) arg3,
1913*4882a593Smuzhiyun (size_t) arg4);
1914*4882a593Smuzhiyun
1915*4882a593Smuzhiyun case KEYCTL_CHOWN:
1916*4882a593Smuzhiyun return keyctl_chown_key((key_serial_t) arg2,
1917*4882a593Smuzhiyun (uid_t) arg3,
1918*4882a593Smuzhiyun (gid_t) arg4);
1919*4882a593Smuzhiyun
1920*4882a593Smuzhiyun case KEYCTL_SETPERM:
1921*4882a593Smuzhiyun return keyctl_setperm_key((key_serial_t) arg2,
1922*4882a593Smuzhiyun (key_perm_t) arg3);
1923*4882a593Smuzhiyun
1924*4882a593Smuzhiyun case KEYCTL_INSTANTIATE:
1925*4882a593Smuzhiyun return keyctl_instantiate_key((key_serial_t) arg2,
1926*4882a593Smuzhiyun (const void __user *) arg3,
1927*4882a593Smuzhiyun (size_t) arg4,
1928*4882a593Smuzhiyun (key_serial_t) arg5);
1929*4882a593Smuzhiyun
1930*4882a593Smuzhiyun case KEYCTL_NEGATE:
1931*4882a593Smuzhiyun return keyctl_negate_key((key_serial_t) arg2,
1932*4882a593Smuzhiyun (unsigned) arg3,
1933*4882a593Smuzhiyun (key_serial_t) arg4);
1934*4882a593Smuzhiyun
1935*4882a593Smuzhiyun case KEYCTL_SET_REQKEY_KEYRING:
1936*4882a593Smuzhiyun return keyctl_set_reqkey_keyring(arg2);
1937*4882a593Smuzhiyun
1938*4882a593Smuzhiyun case KEYCTL_SET_TIMEOUT:
1939*4882a593Smuzhiyun return keyctl_set_timeout((key_serial_t) arg2,
1940*4882a593Smuzhiyun (unsigned) arg3);
1941*4882a593Smuzhiyun
1942*4882a593Smuzhiyun case KEYCTL_ASSUME_AUTHORITY:
1943*4882a593Smuzhiyun return keyctl_assume_authority((key_serial_t) arg2);
1944*4882a593Smuzhiyun
1945*4882a593Smuzhiyun case KEYCTL_GET_SECURITY:
1946*4882a593Smuzhiyun return keyctl_get_security((key_serial_t) arg2,
1947*4882a593Smuzhiyun (char __user *) arg3,
1948*4882a593Smuzhiyun (size_t) arg4);
1949*4882a593Smuzhiyun
1950*4882a593Smuzhiyun case KEYCTL_SESSION_TO_PARENT:
1951*4882a593Smuzhiyun return keyctl_session_to_parent();
1952*4882a593Smuzhiyun
1953*4882a593Smuzhiyun case KEYCTL_REJECT:
1954*4882a593Smuzhiyun return keyctl_reject_key((key_serial_t) arg2,
1955*4882a593Smuzhiyun (unsigned) arg3,
1956*4882a593Smuzhiyun (unsigned) arg4,
1957*4882a593Smuzhiyun (key_serial_t) arg5);
1958*4882a593Smuzhiyun
1959*4882a593Smuzhiyun case KEYCTL_INSTANTIATE_IOV:
1960*4882a593Smuzhiyun return keyctl_instantiate_key_iov(
1961*4882a593Smuzhiyun (key_serial_t) arg2,
1962*4882a593Smuzhiyun (const struct iovec __user *) arg3,
1963*4882a593Smuzhiyun (unsigned) arg4,
1964*4882a593Smuzhiyun (key_serial_t) arg5);
1965*4882a593Smuzhiyun
1966*4882a593Smuzhiyun case KEYCTL_INVALIDATE:
1967*4882a593Smuzhiyun return keyctl_invalidate_key((key_serial_t) arg2);
1968*4882a593Smuzhiyun
1969*4882a593Smuzhiyun case KEYCTL_GET_PERSISTENT:
1970*4882a593Smuzhiyun return keyctl_get_persistent((uid_t)arg2, (key_serial_t)arg3);
1971*4882a593Smuzhiyun
1972*4882a593Smuzhiyun case KEYCTL_DH_COMPUTE:
1973*4882a593Smuzhiyun return keyctl_dh_compute((struct keyctl_dh_params __user *) arg2,
1974*4882a593Smuzhiyun (char __user *) arg3, (size_t) arg4,
1975*4882a593Smuzhiyun (struct keyctl_kdf_params __user *) arg5);
1976*4882a593Smuzhiyun
1977*4882a593Smuzhiyun case KEYCTL_RESTRICT_KEYRING:
1978*4882a593Smuzhiyun return keyctl_restrict_keyring((key_serial_t) arg2,
1979*4882a593Smuzhiyun (const char __user *) arg3,
1980*4882a593Smuzhiyun (const char __user *) arg4);
1981*4882a593Smuzhiyun
1982*4882a593Smuzhiyun case KEYCTL_PKEY_QUERY:
1983*4882a593Smuzhiyun if (arg3 != 0)
1984*4882a593Smuzhiyun return -EINVAL;
1985*4882a593Smuzhiyun return keyctl_pkey_query((key_serial_t)arg2,
1986*4882a593Smuzhiyun (const char __user *)arg4,
1987*4882a593Smuzhiyun (struct keyctl_pkey_query __user *)arg5);
1988*4882a593Smuzhiyun
1989*4882a593Smuzhiyun case KEYCTL_PKEY_ENCRYPT:
1990*4882a593Smuzhiyun case KEYCTL_PKEY_DECRYPT:
1991*4882a593Smuzhiyun case KEYCTL_PKEY_SIGN:
1992*4882a593Smuzhiyun return keyctl_pkey_e_d_s(
1993*4882a593Smuzhiyun option,
1994*4882a593Smuzhiyun (const struct keyctl_pkey_params __user *)arg2,
1995*4882a593Smuzhiyun (const char __user *)arg3,
1996*4882a593Smuzhiyun (const void __user *)arg4,
1997*4882a593Smuzhiyun (void __user *)arg5);
1998*4882a593Smuzhiyun
1999*4882a593Smuzhiyun case KEYCTL_PKEY_VERIFY:
2000*4882a593Smuzhiyun return keyctl_pkey_verify(
2001*4882a593Smuzhiyun (const struct keyctl_pkey_params __user *)arg2,
2002*4882a593Smuzhiyun (const char __user *)arg3,
2003*4882a593Smuzhiyun (const void __user *)arg4,
2004*4882a593Smuzhiyun (const void __user *)arg5);
2005*4882a593Smuzhiyun
2006*4882a593Smuzhiyun case KEYCTL_MOVE:
2007*4882a593Smuzhiyun return keyctl_keyring_move((key_serial_t)arg2,
2008*4882a593Smuzhiyun (key_serial_t)arg3,
2009*4882a593Smuzhiyun (key_serial_t)arg4,
2010*4882a593Smuzhiyun (unsigned int)arg5);
2011*4882a593Smuzhiyun
2012*4882a593Smuzhiyun case KEYCTL_CAPABILITIES:
2013*4882a593Smuzhiyun return keyctl_capabilities((unsigned char __user *)arg2, (size_t)arg3);
2014*4882a593Smuzhiyun
2015*4882a593Smuzhiyun case KEYCTL_WATCH_KEY:
2016*4882a593Smuzhiyun return keyctl_watch_key((key_serial_t)arg2, (int)arg3, (int)arg4);
2017*4882a593Smuzhiyun
2018*4882a593Smuzhiyun default:
2019*4882a593Smuzhiyun return -EOPNOTSUPP;
2020*4882a593Smuzhiyun }
2021*4882a593Smuzhiyun }
2022