KEYS: Disperse linux/key_ui.h
[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         struct task_struct *tsk = current;
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         /* allocate a new session keyring */
80         sprintf(desc, "_req.%u", key->serial);
81
82         keyring = keyring_alloc(desc, current_fsuid(), current_fsgid(), current,
83                                 KEY_ALLOC_QUOTA_OVERRUN, NULL);
84         if (IS_ERR(keyring)) {
85                 ret = PTR_ERR(keyring);
86                 goto error_alloc;
87         }
88
89         /* attach the auth key to the session keyring */
90         ret = __key_link(keyring, authkey);
91         if (ret < 0)
92                 goto error_link;
93
94         /* record the UID and GID */
95         sprintf(uid_str, "%d", current_fsuid());
96         sprintf(gid_str, "%d", current_fsgid());
97
98         /* we say which key is under construction */
99         sprintf(key_str, "%d", key->serial);
100
101         /* we specify the process's default keyrings */
102         sprintf(keyring_str[0], "%d",
103                 tsk->thread_keyring ? tsk->thread_keyring->serial : 0);
104
105         prkey = 0;
106         if (tsk->signal->process_keyring)
107                 prkey = tsk->signal->process_keyring->serial;
108
109         sprintf(keyring_str[1], "%d", prkey);
110
111         if (tsk->signal->session_keyring) {
112                 rcu_read_lock();
113                 sskey = rcu_dereference(tsk->signal->session_keyring)->serial;
114                 rcu_read_unlock();
115         } else {
116                 sskey = tsk->user->session_keyring->serial;
117         }
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 {
170         struct key_construction *cons;
171         request_key_actor_t actor;
172         struct key *authkey;
173         int ret;
174
175         kenter("%d,%p,%zu,%p", key->serial, callout_info, callout_len, aux);
176
177         cons = kmalloc(sizeof(*cons), GFP_KERNEL);
178         if (!cons)
179                 return -ENOMEM;
180
181         /* allocate an authorisation key */
182         authkey = request_key_auth_new(key, callout_info, callout_len);
183         if (IS_ERR(authkey)) {
184                 kfree(cons);
185                 ret = PTR_ERR(authkey);
186                 authkey = NULL;
187         } else {
188                 cons->authkey = key_get(authkey);
189                 cons->key = key_get(key);
190
191                 /* make the call */
192                 actor = call_sbin_request_key;
193                 if (key->type->request_key)
194                         actor = key->type->request_key;
195
196                 ret = actor(cons, "create", aux);
197
198                 /* check that the actor called complete_request_key() prior to
199                  * returning an error */
200                 WARN_ON(ret < 0 &&
201                         !test_bit(KEY_FLAG_REVOKED, &authkey->flags));
202                 key_put(authkey);
203         }
204
205         kleave(" = %d", ret);
206         return ret;
207 }
208
209 /*
210  * link a key to the appropriate destination keyring
211  * - the caller must hold a write lock on the destination keyring
212  */
213 static void construct_key_make_link(struct key *key, struct key *dest_keyring)
214 {
215         struct task_struct *tsk = current;
216         struct key *drop = NULL;
217
218         kenter("{%d},%p", key->serial, dest_keyring);
219
220         /* find the appropriate keyring */
221         if (!dest_keyring) {
222                 switch (tsk->jit_keyring) {
223                 case KEY_REQKEY_DEFL_DEFAULT:
224                 case KEY_REQKEY_DEFL_THREAD_KEYRING:
225                         dest_keyring = tsk->thread_keyring;
226                         if (dest_keyring)
227                                 break;
228
229                 case KEY_REQKEY_DEFL_PROCESS_KEYRING:
230                         dest_keyring = tsk->signal->process_keyring;
231                         if (dest_keyring)
232                                 break;
233
234                 case KEY_REQKEY_DEFL_SESSION_KEYRING:
235                         rcu_read_lock();
236                         dest_keyring = key_get(
237                                 rcu_dereference(tsk->signal->session_keyring));
238                         rcu_read_unlock();
239                         drop = dest_keyring;
240
241                         if (dest_keyring)
242                                 break;
243
244                 case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
245                         dest_keyring = tsk->user->session_keyring;
246                         break;
247
248                 case KEY_REQKEY_DEFL_USER_KEYRING:
249                         dest_keyring = tsk->user->uid_keyring;
250                         break;
251
252                 case KEY_REQKEY_DEFL_GROUP_KEYRING:
253                 default:
254                         BUG();
255                 }
256         }
257
258         /* and attach the key to it */
259         __key_link(dest_keyring, key);
260         key_put(drop);
261         kleave("");
262 }
263
264 /*
265  * allocate a new key in under-construction state and attempt to link it in to
266  * the requested place
267  * - may return a key that's already under construction instead
268  */
269 static int construct_alloc_key(struct key_type *type,
270                                const char *description,
271                                struct key *dest_keyring,
272                                unsigned long flags,
273                                struct key_user *user,
274                                struct key **_key)
275 {
276         struct key *key;
277         key_ref_t key_ref;
278
279         kenter("%s,%s,,,", type->name, description);
280
281         mutex_lock(&user->cons_lock);
282
283         key = key_alloc(type, description,
284                         current_fsuid(), current_fsgid(), current, KEY_POS_ALL,
285                         flags);
286         if (IS_ERR(key))
287                 goto alloc_failed;
288
289         set_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags);
290
291         if (dest_keyring)
292                 down_write(&dest_keyring->sem);
293
294         /* attach the key to the destination keyring under lock, but we do need
295          * to do another check just in case someone beat us to it whilst we
296          * waited for locks */
297         mutex_lock(&key_construction_mutex);
298
299         key_ref = search_process_keyrings(type, description, type->match,
300                                           current);
301         if (!IS_ERR(key_ref))
302                 goto key_already_present;
303
304         if (dest_keyring)
305                 construct_key_make_link(key, dest_keyring);
306
307         mutex_unlock(&key_construction_mutex);
308         if (dest_keyring)
309                 up_write(&dest_keyring->sem);
310         mutex_unlock(&user->cons_lock);
311         *_key = key;
312         kleave(" = 0 [%d]", key_serial(key));
313         return 0;
314
315 key_already_present:
316         mutex_unlock(&key_construction_mutex);
317         if (dest_keyring)
318                 up_write(&dest_keyring->sem);
319         mutex_unlock(&user->cons_lock);
320         key_put(key);
321         *_key = key = key_ref_to_ptr(key_ref);
322         kleave(" = -EINPROGRESS [%d]", key_serial(key));
323         return -EINPROGRESS;
324
325 alloc_failed:
326         mutex_unlock(&user->cons_lock);
327         *_key = NULL;
328         kleave(" = %ld", PTR_ERR(key));
329         return PTR_ERR(key);
330 }
331
332 /*
333  * commence key construction
334  */
335 static struct key *construct_key_and_link(struct key_type *type,
336                                           const char *description,
337                                           const char *callout_info,
338                                           size_t callout_len,
339                                           void *aux,
340                                           struct key *dest_keyring,
341                                           unsigned long flags)
342 {
343         struct key_user *user;
344         struct key *key;
345         int ret;
346
347         user = key_user_lookup(current_fsuid());
348         if (!user)
349                 return ERR_PTR(-ENOMEM);
350
351         ret = construct_alloc_key(type, description, dest_keyring, flags, user,
352                                   &key);
353         key_user_put(user);
354
355         if (ret == 0) {
356                 ret = construct_key(key, callout_info, callout_len, aux);
357                 if (ret < 0)
358                         goto construction_failed;
359         }
360
361         return key;
362
363 construction_failed:
364         key_negate_and_link(key, key_negative_timeout, NULL, NULL);
365         key_put(key);
366         return ERR_PTR(ret);
367 }
368
369 /*
370  * request a key
371  * - search the process's keyrings
372  * - check the list of keys being created or updated
373  * - call out to userspace for a key if supplementary info was provided
374  * - cache the key in an appropriate keyring
375  */
376 struct key *request_key_and_link(struct key_type *type,
377                                  const char *description,
378                                  const void *callout_info,
379                                  size_t callout_len,
380                                  void *aux,
381                                  struct key *dest_keyring,
382                                  unsigned long flags)
383 {
384         struct key *key;
385         key_ref_t key_ref;
386
387         kenter("%s,%s,%p,%zu,%p,%p,%lx",
388                type->name, description, callout_info, callout_len, aux,
389                dest_keyring, flags);
390
391         /* search all the process keyrings for a key */
392         key_ref = search_process_keyrings(type, description, type->match,
393                                           current);
394
395         if (!IS_ERR(key_ref)) {
396                 key = key_ref_to_ptr(key_ref);
397         } else if (PTR_ERR(key_ref) != -EAGAIN) {
398                 key = ERR_CAST(key_ref);
399         } else  {
400                 /* the search failed, but the keyrings were searchable, so we
401                  * should consult userspace if we can */
402                 key = ERR_PTR(-ENOKEY);
403                 if (!callout_info)
404                         goto error;
405
406                 key = construct_key_and_link(type, description, callout_info,
407                                              callout_len, aux, dest_keyring,
408                                              flags);
409         }
410
411 error:
412         kleave(" = %p", key);
413         return key;
414 }
415
416 /*
417  * wait for construction of a key to complete
418  */
419 int wait_for_key_construction(struct key *key, bool intr)
420 {
421         int ret;
422
423         ret = wait_on_bit(&key->flags, KEY_FLAG_USER_CONSTRUCT,
424                           intr ? key_wait_bit_intr : key_wait_bit,
425                           intr ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE);
426         if (ret < 0)
427                 return ret;
428         return key_validate(key);
429 }
430 EXPORT_SYMBOL(wait_for_key_construction);
431
432 /*
433  * request a key
434  * - search the process's keyrings
435  * - check the list of keys being created or updated
436  * - call out to userspace for a key if supplementary info was provided
437  * - waits uninterruptible for creation to complete
438  */
439 struct key *request_key(struct key_type *type,
440                         const char *description,
441                         const char *callout_info)
442 {
443         struct key *key;
444         size_t callout_len = 0;
445         int ret;
446
447         if (callout_info)
448                 callout_len = strlen(callout_info);
449         key = request_key_and_link(type, description, callout_info, callout_len,
450                                    NULL, NULL, KEY_ALLOC_IN_QUOTA);
451         if (!IS_ERR(key)) {
452                 ret = wait_for_key_construction(key, false);
453                 if (ret < 0) {
454                         key_put(key);
455                         return ERR_PTR(ret);
456                 }
457         }
458         return key;
459 }
460 EXPORT_SYMBOL(request_key);
461
462 /*
463  * request a key with auxiliary data for the upcaller
464  * - search the process's keyrings
465  * - check the list of keys being created or updated
466  * - call out to userspace for a key if supplementary info was provided
467  * - waits uninterruptible for creation to complete
468  */
469 struct key *request_key_with_auxdata(struct key_type *type,
470                                      const char *description,
471                                      const void *callout_info,
472                                      size_t callout_len,
473                                      void *aux)
474 {
475         struct key *key;
476         int ret;
477
478         key = request_key_and_link(type, description, callout_info, callout_len,
479                                    aux, NULL, KEY_ALLOC_IN_QUOTA);
480         if (!IS_ERR(key)) {
481                 ret = wait_for_key_construction(key, false);
482                 if (ret < 0) {
483                         key_put(key);
484                         return ERR_PTR(ret);
485                 }
486         }
487         return key;
488 }
489 EXPORT_SYMBOL(request_key_with_auxdata);
490
491 /*
492  * request a key (allow async construction)
493  * - search the process's keyrings
494  * - check the list of keys being created or updated
495  * - call out to userspace for a key if supplementary info was provided
496  */
497 struct key *request_key_async(struct key_type *type,
498                               const char *description,
499                               const void *callout_info,
500                               size_t callout_len)
501 {
502         return request_key_and_link(type, description, callout_info,
503                                     callout_len, NULL, NULL,
504                                     KEY_ALLOC_IN_QUOTA);
505 }
506 EXPORT_SYMBOL(request_key_async);
507
508 /*
509  * request a key with auxiliary data for the upcaller (allow async construction)
510  * - search the process's keyrings
511  * - check the list of keys being created or updated
512  * - call out to userspace for a key if supplementary info was provided
513  */
514 struct key *request_key_async_with_auxdata(struct key_type *type,
515                                            const char *description,
516                                            const void *callout_info,
517                                            size_t callout_len,
518                                            void *aux)
519 {
520         return request_key_and_link(type, description, callout_info,
521                                     callout_len, aux, NULL, KEY_ALLOC_IN_QUOTA);
522 }
523 EXPORT_SYMBOL(request_key_async_with_auxdata);