CRED: Separate per-task-group keyrings from signal_struct
[safe/jmp/linux-2.6] / security / keys / request_key.c
1 /* Request a key from userspace
2  *
3  * Copyright (C) 2004-2007 Red Hat, Inc. All Rights Reserved.
4  * Written by David Howells (dhowells@redhat.com)
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version
9  * 2 of the License, or (at your option) any later version.
10  *
11  * See Documentation/keys-request-key.txt
12  */
13
14 #include <linux/module.h>
15 #include <linux/sched.h>
16 #include <linux/kmod.h>
17 #include <linux/err.h>
18 #include <linux/keyctl.h>
19 #include <linux/slab.h>
20 #include "internal.h"
21
22 #define key_negative_timeout    60      /* default timeout on a negative key's existence */
23
24 /*
25  * wait_on_bit() sleep function for uninterruptible waiting
26  */
27 static int key_wait_bit(void *flags)
28 {
29         schedule();
30         return 0;
31 }
32
33 /*
34  * wait_on_bit() sleep function for interruptible waiting
35  */
36 static int key_wait_bit_intr(void *flags)
37 {
38         schedule();
39         return signal_pending(current) ? -ERESTARTSYS : 0;
40 }
41
42 /*
43  * call to complete the construction of a key
44  */
45 void complete_request_key(struct key_construction *cons, int error)
46 {
47         kenter("{%d,%d},%d", cons->key->serial, cons->authkey->serial, error);
48
49         if (error < 0)
50                 key_negate_and_link(cons->key, key_negative_timeout, NULL,
51                                     cons->authkey);
52         else
53                 key_revoke(cons->authkey);
54
55         key_put(cons->key);
56         key_put(cons->authkey);
57         kfree(cons);
58 }
59 EXPORT_SYMBOL(complete_request_key);
60
61 /*
62  * request userspace finish the construction of a key
63  * - execute "/sbin/request-key <op> <key> <uid> <gid> <keyring> <keyring> <keyring>"
64  */
65 static int call_sbin_request_key(struct key_construction *cons,
66                                  const char *op,
67                                  void *aux)
68 {
69         const struct cred *cred = current_cred();
70         key_serial_t prkey, sskey;
71         struct key *key = cons->key, *authkey = cons->authkey, *keyring;
72         char *argv[9], *envp[3], uid_str[12], gid_str[12];
73         char key_str[12], keyring_str[3][12];
74         char desc[20];
75         int ret, i;
76
77         kenter("{%d},{%d},%s", key->serial, authkey->serial, op);
78
79         ret = install_user_keyrings();
80         if (ret < 0)
81                 goto error_alloc;
82
83         /* allocate a new session keyring */
84         sprintf(desc, "_req.%u", key->serial);
85
86         keyring = keyring_alloc(desc, current_fsuid(), current_fsgid(), current,
87                                 KEY_ALLOC_QUOTA_OVERRUN, NULL);
88         if (IS_ERR(keyring)) {
89                 ret = PTR_ERR(keyring);
90                 goto error_alloc;
91         }
92
93         /* attach the auth key to the session keyring */
94         ret = __key_link(keyring, authkey);
95         if (ret < 0)
96                 goto error_link;
97
98         /* record the UID and GID */
99         sprintf(uid_str, "%d", cred->fsuid);
100         sprintf(gid_str, "%d", cred->fsgid);
101
102         /* we say which key is under construction */
103         sprintf(key_str, "%d", key->serial);
104
105         /* we specify the process's default keyrings */
106         sprintf(keyring_str[0], "%d",
107                 cred->thread_keyring ?
108                 cred->thread_keyring->serial : 0);
109
110         prkey = 0;
111         if (cred->tgcred->process_keyring)
112                 prkey = cred->tgcred->process_keyring->serial;
113
114         if (cred->tgcred->session_keyring)
115                 sskey = rcu_dereference(cred->tgcred->session_keyring)->serial;
116         else
117                 sskey = cred->user->session_keyring->serial;
118
119         sprintf(keyring_str[2], "%d", sskey);
120
121         /* set up a minimal environment */
122         i = 0;
123         envp[i++] = "HOME=/";
124         envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
125         envp[i] = NULL;
126
127         /* set up the argument list */
128         i = 0;
129         argv[i++] = "/sbin/request-key";
130         argv[i++] = (char *) op;
131         argv[i++] = key_str;
132         argv[i++] = uid_str;
133         argv[i++] = gid_str;
134         argv[i++] = keyring_str[0];
135         argv[i++] = keyring_str[1];
136         argv[i++] = keyring_str[2];
137         argv[i] = NULL;
138
139         /* do it */
140         ret = call_usermodehelper_keys(argv[0], argv, envp, keyring,
141                                        UMH_WAIT_PROC);
142         kdebug("usermode -> 0x%x", ret);
143         if (ret >= 0) {
144                 /* ret is the exit/wait code */
145                 if (test_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags) ||
146                     key_validate(key) < 0)
147                         ret = -ENOKEY;
148                 else
149                         /* ignore any errors from userspace if the key was
150                          * instantiated */
151                         ret = 0;
152         }
153
154 error_link:
155         key_put(keyring);
156
157 error_alloc:
158         kleave(" = %d", ret);
159         complete_request_key(cons, ret);
160         return ret;
161 }
162
163 /*
164  * call out to userspace for key construction
165  * - we ignore program failure and go on key status instead
166  */
167 static int construct_key(struct key *key, const void *callout_info,
168                          size_t callout_len, void *aux,
169                          struct key *dest_keyring)
170 {
171         struct key_construction *cons;
172         request_key_actor_t actor;
173         struct key *authkey;
174         int ret;
175
176         kenter("%d,%p,%zu,%p", key->serial, callout_info, callout_len, aux);
177
178         cons = kmalloc(sizeof(*cons), GFP_KERNEL);
179         if (!cons)
180                 return -ENOMEM;
181
182         /* allocate an authorisation key */
183         authkey = request_key_auth_new(key, callout_info, callout_len,
184                                        dest_keyring);
185         if (IS_ERR(authkey)) {
186                 kfree(cons);
187                 ret = PTR_ERR(authkey);
188                 authkey = NULL;
189         } else {
190                 cons->authkey = key_get(authkey);
191                 cons->key = key_get(key);
192
193                 /* make the call */
194                 actor = call_sbin_request_key;
195                 if (key->type->request_key)
196                         actor = key->type->request_key;
197
198                 ret = actor(cons, "create", aux);
199
200                 /* check that the actor called complete_request_key() prior to
201                  * returning an error */
202                 WARN_ON(ret < 0 &&
203                         !test_bit(KEY_FLAG_REVOKED, &authkey->flags));
204                 key_put(authkey);
205         }
206
207         kleave(" = %d", ret);
208         return ret;
209 }
210
211 /*
212  * get the appropriate destination keyring for the request
213  * - we return whatever keyring we select with an extra reference upon it which
214  *   the caller must release
215  */
216 static void construct_get_dest_keyring(struct key **_dest_keyring)
217 {
218         struct request_key_auth *rka;
219         const struct cred *cred = current_cred();
220         struct key *dest_keyring = *_dest_keyring, *authkey;
221
222         kenter("%p", dest_keyring);
223
224         /* find the appropriate keyring */
225         if (dest_keyring) {
226                 /* the caller supplied one */
227                 key_get(dest_keyring);
228         } else {
229                 /* use a default keyring; falling through the cases until we
230                  * find one that we actually have */
231                 switch (cred->jit_keyring) {
232                 case KEY_REQKEY_DEFL_DEFAULT:
233                 case KEY_REQKEY_DEFL_REQUESTOR_KEYRING:
234                         if (cred->request_key_auth) {
235                                 authkey = cred->request_key_auth;
236                                 down_read(&authkey->sem);
237                                 rka = authkey->payload.data;
238                                 if (!test_bit(KEY_FLAG_REVOKED,
239                                               &authkey->flags))
240                                         dest_keyring =
241                                                 key_get(rka->dest_keyring);
242                                 up_read(&authkey->sem);
243                                 if (dest_keyring)
244                                         break;
245                         }
246
247                 case KEY_REQKEY_DEFL_THREAD_KEYRING:
248                         dest_keyring = key_get(cred->thread_keyring);
249                         if (dest_keyring)
250                                 break;
251
252                 case KEY_REQKEY_DEFL_PROCESS_KEYRING:
253                         dest_keyring = key_get(cred->tgcred->process_keyring);
254                         if (dest_keyring)
255                                 break;
256
257                 case KEY_REQKEY_DEFL_SESSION_KEYRING:
258                         rcu_read_lock();
259                         dest_keyring = key_get(
260                                 rcu_dereference(cred->tgcred->session_keyring));
261                         rcu_read_unlock();
262
263                         if (dest_keyring)
264                                 break;
265
266                 case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
267                         dest_keyring =
268                                 key_get(cred->user->session_keyring);
269                         break;
270
271                 case KEY_REQKEY_DEFL_USER_KEYRING:
272                         dest_keyring = key_get(cred->user->uid_keyring);
273                         break;
274
275                 case KEY_REQKEY_DEFL_GROUP_KEYRING:
276                 default:
277                         BUG();
278                 }
279         }
280
281         *_dest_keyring = dest_keyring;
282         kleave(" [dk %d]", key_serial(dest_keyring));
283         return;
284 }
285
286 /*
287  * allocate a new key in under-construction state and attempt to link it in to
288  * the requested place
289  * - may return a key that's already under construction instead
290  */
291 static int construct_alloc_key(struct key_type *type,
292                                const char *description,
293                                struct key *dest_keyring,
294                                unsigned long flags,
295                                struct key_user *user,
296                                struct key **_key)
297 {
298         struct key *key;
299         key_ref_t key_ref;
300
301         kenter("%s,%s,,,", type->name, description);
302
303         mutex_lock(&user->cons_lock);
304
305         key = key_alloc(type, description,
306                         current_fsuid(), current_fsgid(), current, KEY_POS_ALL,
307                         flags);
308         if (IS_ERR(key))
309                 goto alloc_failed;
310
311         set_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags);
312
313         down_write(&dest_keyring->sem);
314
315         /* attach the key to the destination keyring under lock, but we do need
316          * to do another check just in case someone beat us to it whilst we
317          * waited for locks */
318         mutex_lock(&key_construction_mutex);
319
320         key_ref = search_process_keyrings(type, description, type->match,
321                                           current);
322         if (!IS_ERR(key_ref))
323                 goto key_already_present;
324
325         __key_link(dest_keyring, key);
326
327         mutex_unlock(&key_construction_mutex);
328         up_write(&dest_keyring->sem);
329         mutex_unlock(&user->cons_lock);
330         *_key = key;
331         kleave(" = 0 [%d]", key_serial(key));
332         return 0;
333
334 key_already_present:
335         mutex_unlock(&key_construction_mutex);
336         if (dest_keyring)
337                 up_write(&dest_keyring->sem);
338         mutex_unlock(&user->cons_lock);
339         key_put(key);
340         *_key = key = key_ref_to_ptr(key_ref);
341         kleave(" = -EINPROGRESS [%d]", key_serial(key));
342         return -EINPROGRESS;
343
344 alloc_failed:
345         mutex_unlock(&user->cons_lock);
346         *_key = NULL;
347         kleave(" = %ld", PTR_ERR(key));
348         return PTR_ERR(key);
349 }
350
351 /*
352  * commence key construction
353  */
354 static struct key *construct_key_and_link(struct key_type *type,
355                                           const char *description,
356                                           const char *callout_info,
357                                           size_t callout_len,
358                                           void *aux,
359                                           struct key *dest_keyring,
360                                           unsigned long flags)
361 {
362         struct key_user *user;
363         struct key *key;
364         int ret;
365
366         user = key_user_lookup(current_fsuid());
367         if (!user)
368                 return ERR_PTR(-ENOMEM);
369
370         construct_get_dest_keyring(&dest_keyring);
371
372         ret = construct_alloc_key(type, description, dest_keyring, flags, user,
373                                   &key);
374         key_user_put(user);
375
376         if (ret == 0) {
377                 ret = construct_key(key, callout_info, callout_len, aux,
378                                     dest_keyring);
379                 if (ret < 0)
380                         goto construction_failed;
381         }
382
383         key_put(dest_keyring);
384         return key;
385
386 construction_failed:
387         key_negate_and_link(key, key_negative_timeout, NULL, NULL);
388         key_put(key);
389         key_put(dest_keyring);
390         return ERR_PTR(ret);
391 }
392
393 /*
394  * request a key
395  * - search the process's keyrings
396  * - check the list of keys being created or updated
397  * - call out to userspace for a key if supplementary info was provided
398  * - cache the key in an appropriate keyring
399  */
400 struct key *request_key_and_link(struct key_type *type,
401                                  const char *description,
402                                  const void *callout_info,
403                                  size_t callout_len,
404                                  void *aux,
405                                  struct key *dest_keyring,
406                                  unsigned long flags)
407 {
408         struct key *key;
409         key_ref_t key_ref;
410
411         kenter("%s,%s,%p,%zu,%p,%p,%lx",
412                type->name, description, callout_info, callout_len, aux,
413                dest_keyring, flags);
414
415         /* search all the process keyrings for a key */
416         key_ref = search_process_keyrings(type, description, type->match,
417                                           current);
418
419         if (!IS_ERR(key_ref)) {
420                 key = key_ref_to_ptr(key_ref);
421         } else if (PTR_ERR(key_ref) != -EAGAIN) {
422                 key = ERR_CAST(key_ref);
423         } else  {
424                 /* the search failed, but the keyrings were searchable, so we
425                  * should consult userspace if we can */
426                 key = ERR_PTR(-ENOKEY);
427                 if (!callout_info)
428                         goto error;
429
430                 key = construct_key_and_link(type, description, callout_info,
431                                              callout_len, aux, dest_keyring,
432                                              flags);
433         }
434
435 error:
436         kleave(" = %p", key);
437         return key;
438 }
439
440 /*
441  * wait for construction of a key to complete
442  */
443 int wait_for_key_construction(struct key *key, bool intr)
444 {
445         int ret;
446
447         ret = wait_on_bit(&key->flags, KEY_FLAG_USER_CONSTRUCT,
448                           intr ? key_wait_bit_intr : key_wait_bit,
449                           intr ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE);
450         if (ret < 0)
451                 return ret;
452         return key_validate(key);
453 }
454 EXPORT_SYMBOL(wait_for_key_construction);
455
456 /*
457  * request a key
458  * - search the process's keyrings
459  * - check the list of keys being created or updated
460  * - call out to userspace for a key if supplementary info was provided
461  * - waits uninterruptible for creation to complete
462  */
463 struct key *request_key(struct key_type *type,
464                         const char *description,
465                         const char *callout_info)
466 {
467         struct key *key;
468         size_t callout_len = 0;
469         int ret;
470
471         if (callout_info)
472                 callout_len = strlen(callout_info);
473         key = request_key_and_link(type, description, callout_info, callout_len,
474                                    NULL, NULL, KEY_ALLOC_IN_QUOTA);
475         if (!IS_ERR(key)) {
476                 ret = wait_for_key_construction(key, false);
477                 if (ret < 0) {
478                         key_put(key);
479                         return ERR_PTR(ret);
480                 }
481         }
482         return key;
483 }
484 EXPORT_SYMBOL(request_key);
485
486 /*
487  * request a key with auxiliary data for the upcaller
488  * - search the process's keyrings
489  * - check the list of keys being created or updated
490  * - call out to userspace for a key if supplementary info was provided
491  * - waits uninterruptible for creation to complete
492  */
493 struct key *request_key_with_auxdata(struct key_type *type,
494                                      const char *description,
495                                      const void *callout_info,
496                                      size_t callout_len,
497                                      void *aux)
498 {
499         struct key *key;
500         int ret;
501
502         key = request_key_and_link(type, description, callout_info, callout_len,
503                                    aux, NULL, KEY_ALLOC_IN_QUOTA);
504         if (!IS_ERR(key)) {
505                 ret = wait_for_key_construction(key, false);
506                 if (ret < 0) {
507                         key_put(key);
508                         return ERR_PTR(ret);
509                 }
510         }
511         return key;
512 }
513 EXPORT_SYMBOL(request_key_with_auxdata);
514
515 /*
516  * request a key (allow async construction)
517  * - search the process's keyrings
518  * - check the list of keys being created or updated
519  * - call out to userspace for a key if supplementary info was provided
520  */
521 struct key *request_key_async(struct key_type *type,
522                               const char *description,
523                               const void *callout_info,
524                               size_t callout_len)
525 {
526         return request_key_and_link(type, description, callout_info,
527                                     callout_len, NULL, NULL,
528                                     KEY_ALLOC_IN_QUOTA);
529 }
530 EXPORT_SYMBOL(request_key_async);
531
532 /*
533  * request a key with auxiliary data for the upcaller (allow async construction)
534  * - search the process's keyrings
535  * - check the list of keys being created or updated
536  * - call out to userspace for a key if supplementary info was provided
537  */
538 struct key *request_key_async_with_auxdata(struct key_type *type,
539                                            const char *description,
540                                            const void *callout_info,
541                                            size_t callout_len,
542                                            void *aux)
543 {
544         return request_key_and_link(type, description, callout_info,
545                                     callout_len, aux, NULL, KEY_ALLOC_IN_QUOTA);
546 }
547 EXPORT_SYMBOL(request_key_async_with_auxdata);