1 /* process_keys.c: management of a process's keyrings
3 * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
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.
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/sched.h>
15 #include <linux/slab.h>
16 #include <linux/keyctl.h>
18 #include <linux/err.h>
19 #include <asm/uaccess.h>
22 /* session keyring create vs join semaphore */
23 static DECLARE_MUTEX(key_session_sem);
25 /* the root user's tracking struct */
26 struct key_user root_key_user = {
27 .usage = ATOMIC_INIT(3),
28 .consq = LIST_HEAD_INIT(root_key_user.consq),
29 .lock = SPIN_LOCK_UNLOCKED,
30 .nkeys = ATOMIC_INIT(2),
31 .nikeys = ATOMIC_INIT(2),
35 /* the root user's UID keyring */
36 struct key root_user_keyring = {
37 .usage = ATOMIC_INIT(1),
39 .type = &key_type_keyring,
40 .user = &root_key_user,
41 .lock = RW_LOCK_UNLOCKED,
42 .sem = __RWSEM_INITIALIZER(root_user_keyring.sem),
44 .flags = KEY_FLAG_INSTANTIATED,
45 .description = "_uid.0",
47 .magic = KEY_DEBUG_MAGIC,
51 /* the root user's default session keyring */
52 struct key root_session_keyring = {
53 .usage = ATOMIC_INIT(1),
55 .type = &key_type_keyring,
56 .user = &root_key_user,
57 .lock = RW_LOCK_UNLOCKED,
58 .sem = __RWSEM_INITIALIZER(root_session_keyring.sem),
60 .flags = KEY_FLAG_INSTANTIATED,
61 .description = "_uid_ses.0",
63 .magic = KEY_DEBUG_MAGIC,
67 /*****************************************************************************/
69 * allocate the keyrings to be associated with a UID
71 int alloc_uid_keyring(struct user_struct *user)
73 struct key *uid_keyring, *session_keyring;
77 /* concoct a default session keyring */
78 sprintf(buf, "_uid_ses.%u", user->uid);
80 session_keyring = keyring_alloc(buf, user->uid, (gid_t) -1, 0, NULL);
81 if (IS_ERR(session_keyring)) {
82 ret = PTR_ERR(session_keyring);
86 /* and a UID specific keyring, pointed to by the default session
88 sprintf(buf, "_uid.%u", user->uid);
90 uid_keyring = keyring_alloc(buf, user->uid, (gid_t) -1, 0,
92 if (IS_ERR(uid_keyring)) {
93 key_put(session_keyring);
94 ret = PTR_ERR(uid_keyring);
98 /* install the keyrings */
99 user->uid_keyring = uid_keyring;
100 user->session_keyring = session_keyring;
106 } /* end alloc_uid_keyring() */
108 /*****************************************************************************/
110 * deal with the UID changing
112 void switch_uid_keyring(struct user_struct *new_user)
114 #if 0 /* do nothing for now */
117 /* switch to the new user's session keyring if we were running under
118 * root's default session keyring */
119 if (new_user->uid != 0 &&
120 current->session_keyring == &root_session_keyring
122 atomic_inc(&new_user->session_keyring->usage);
125 old = current->session_keyring;
126 current->session_keyring = new_user->session_keyring;
127 task_unlock(current);
133 } /* end switch_uid_keyring() */
135 /*****************************************************************************/
137 * install a fresh thread keyring, discarding the old one
139 int install_thread_keyring(struct task_struct *tsk)
141 struct key *keyring, *old;
145 sprintf(buf, "_tid.%u", tsk->pid);
147 keyring = keyring_alloc(buf, tsk->uid, tsk->gid, 1, NULL);
148 if (IS_ERR(keyring)) {
149 ret = PTR_ERR(keyring);
154 old = tsk->thread_keyring;
155 tsk->thread_keyring = keyring;
164 } /* end install_thread_keyring() */
166 /*****************************************************************************/
168 * make sure a process keyring is installed
170 static int install_process_keyring(struct task_struct *tsk)
177 if (!tsk->signal->process_keyring) {
178 sprintf(buf, "_pid.%u", tsk->tgid);
180 keyring = keyring_alloc(buf, tsk->uid, tsk->gid, 1, NULL);
181 if (IS_ERR(keyring)) {
182 ret = PTR_ERR(keyring);
186 /* attach or swap keyrings */
187 spin_lock_irqsave(&tsk->sighand->siglock, flags);
188 if (!tsk->signal->process_keyring) {
189 tsk->signal->process_keyring = keyring;
192 spin_unlock_irqrestore(&tsk->sighand->siglock, flags);
201 } /* end install_process_keyring() */
203 /*****************************************************************************/
205 * install a session keyring, discarding the old one
206 * - if a keyring is not supplied, an empty one is invented
208 static int install_session_keyring(struct task_struct *tsk,
216 /* create an empty session keyring */
218 sprintf(buf, "_ses.%u", tsk->tgid);
220 keyring = keyring_alloc(buf, tsk->uid, tsk->gid, 1, NULL);
221 if (IS_ERR(keyring)) {
222 ret = PTR_ERR(keyring);
227 atomic_inc(&keyring->usage);
230 /* install the keyring */
231 spin_lock_irqsave(&tsk->sighand->siglock, flags);
232 old = tsk->signal->session_keyring;
233 tsk->signal->session_keyring = keyring;
234 spin_unlock_irqrestore(&tsk->sighand->siglock, flags);
242 } /* end install_session_keyring() */
244 /*****************************************************************************/
246 * copy the keys in a thread group for fork without CLONE_THREAD
248 int copy_thread_group_keys(struct task_struct *tsk)
252 key_check(current->thread_group->session_keyring);
253 key_check(current->thread_group->process_keyring);
255 /* no process keyring yet */
256 tsk->signal->process_keyring = NULL;
258 /* same session keyring */
259 spin_lock_irqsave(¤t->sighand->siglock, flags);
260 tsk->signal->session_keyring =
261 key_get(current->signal->session_keyring);
262 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
266 } /* end copy_thread_group_keys() */
268 /*****************************************************************************/
270 * copy the keys for fork
272 int copy_keys(unsigned long clone_flags, struct task_struct *tsk)
274 key_check(tsk->thread_keyring);
276 /* no thread keyring yet */
277 tsk->thread_keyring = NULL;
280 } /* end copy_keys() */
282 /*****************************************************************************/
284 * dispose of thread group keys upon thread group destruction
286 void exit_thread_group_keys(struct signal_struct *tg)
288 key_put(tg->session_keyring);
289 key_put(tg->process_keyring);
291 } /* end exit_thread_group_keys() */
293 /*****************************************************************************/
295 * dispose of keys upon thread exit
297 void exit_keys(struct task_struct *tsk)
299 key_put(tsk->thread_keyring);
301 } /* end exit_keys() */
303 /*****************************************************************************/
307 int exec_keys(struct task_struct *tsk)
312 /* newly exec'd tasks don't get a thread keyring */
314 old = tsk->thread_keyring;
315 tsk->thread_keyring = NULL;
320 /* discard the process keyring from a newly exec'd task */
321 spin_lock_irqsave(&tsk->sighand->siglock, flags);
322 old = tsk->signal->process_keyring;
323 tsk->signal->process_keyring = NULL;
324 spin_unlock_irqrestore(&tsk->sighand->siglock, flags);
330 } /* end exec_keys() */
332 /*****************************************************************************/
334 * deal with SUID programs
335 * - we might want to make this invent a new session keyring
337 int suid_keys(struct task_struct *tsk)
341 } /* end suid_keys() */
343 /*****************************************************************************/
345 * the filesystem user ID changed
347 void key_fsuid_changed(struct task_struct *tsk)
349 /* update the ownership of the thread keyring */
350 if (tsk->thread_keyring) {
351 down_write(&tsk->thread_keyring->sem);
352 write_lock(&tsk->thread_keyring->lock);
353 tsk->thread_keyring->uid = tsk->fsuid;
354 write_unlock(&tsk->thread_keyring->lock);
355 up_write(&tsk->thread_keyring->sem);
358 } /* end key_fsuid_changed() */
360 /*****************************************************************************/
362 * the filesystem group ID changed
364 void key_fsgid_changed(struct task_struct *tsk)
366 /* update the ownership of the thread keyring */
367 if (tsk->thread_keyring) {
368 down_write(&tsk->thread_keyring->sem);
369 write_lock(&tsk->thread_keyring->lock);
370 tsk->thread_keyring->gid = tsk->fsgid;
371 write_unlock(&tsk->thread_keyring->lock);
372 up_write(&tsk->thread_keyring->sem);
375 } /* end key_fsgid_changed() */
377 /*****************************************************************************/
379 * search the process keyrings for the first matching key
380 * - we use the supplied match function to see if the description (or other
381 * feature of interest) matches
382 * - we return -EAGAIN if we didn't find any matching key
383 * - we return -ENOKEY if we found only negative matching keys
385 struct key *search_process_keyrings_aux(struct key_type *type,
386 const void *description,
387 key_match_func_t match)
389 struct task_struct *tsk = current;
391 struct key *key, *ret, *err, *tmp;
393 /* we want to return -EAGAIN or -ENOKEY if any of the keyrings were
394 * searchable, but we failed to find a key or we found a negative key;
395 * otherwise we want to return a sample error (probably -EACCES) if
396 * none of the keyrings were searchable
398 * in terms of priority: success > -ENOKEY > -EAGAIN > other error
402 err = ERR_PTR(-EAGAIN);
404 /* search the thread keyring first */
405 if (tsk->thread_keyring) {
406 key = keyring_search_aux(tsk->thread_keyring, type,
411 switch (PTR_ERR(key)) {
412 case -EAGAIN: /* no key */
415 case -ENOKEY: /* negative key */
424 /* search the process keyring second */
425 if (tsk->signal->process_keyring) {
426 key = keyring_search_aux(tsk->signal->process_keyring,
427 type, description, match);
431 switch (PTR_ERR(key)) {
432 case -EAGAIN: /* no key */
435 case -ENOKEY: /* negative key */
444 /* search the session keyring last */
445 spin_lock_irqsave(&tsk->sighand->siglock, flags);
447 tmp = tsk->signal->session_keyring;
449 tmp = tsk->user->session_keyring;
450 atomic_inc(&tmp->usage);
452 spin_unlock_irqrestore(&tsk->sighand->siglock, flags);
454 key = keyring_search_aux(tmp, type, description, match);
459 switch (PTR_ERR(key)) {
460 case -EAGAIN: /* no key */
463 case -ENOKEY: /* negative key */
471 /* no key - decide on the error we're going to go for */
472 key = ret ? ret : err;
477 } /* end search_process_keyrings_aux() */
479 /*****************************************************************************/
481 * search the process keyrings for the first matching key
482 * - we return -EAGAIN if we didn't find any matching key
483 * - we return -ENOKEY if we found only negative matching keys
485 struct key *search_process_keyrings(struct key_type *type,
486 const char *description)
488 return search_process_keyrings_aux(type, description, type->match);
490 } /* end search_process_keyrings() */
492 /*****************************************************************************/
494 * lookup a key given a key ID from userspace with a given permissions mask
495 * - don't create special keyrings unless so requested
496 * - partially constructed keys aren't found unless requested
498 struct key *lookup_user_key(key_serial_t id, int create, int partial,
501 struct task_struct *tsk = current;
506 key = ERR_PTR(-ENOKEY);
509 case KEY_SPEC_THREAD_KEYRING:
510 if (!tsk->thread_keyring) {
514 ret = install_thread_keyring(tsk);
521 key = tsk->thread_keyring;
522 atomic_inc(&key->usage);
525 case KEY_SPEC_PROCESS_KEYRING:
526 if (!tsk->signal->process_keyring) {
530 ret = install_process_keyring(tsk);
537 key = tsk->signal->process_keyring;
538 atomic_inc(&key->usage);
541 case KEY_SPEC_SESSION_KEYRING:
542 if (!tsk->signal->session_keyring) {
543 /* always install a session keyring upon access if one
544 * doesn't exist yet */
545 ret = install_session_keyring(
546 tsk, tsk->user->session_keyring);
551 spin_lock_irqsave(&tsk->sighand->siglock, flags);
552 key = tsk->signal->session_keyring;
553 atomic_inc(&key->usage);
554 spin_unlock_irqrestore(&tsk->sighand->siglock, flags);
557 case KEY_SPEC_USER_KEYRING:
558 key = tsk->user->uid_keyring;
559 atomic_inc(&key->usage);
562 case KEY_SPEC_USER_SESSION_KEYRING:
563 key = tsk->user->session_keyring;
564 atomic_inc(&key->usage);
567 case KEY_SPEC_GROUP_KEYRING:
568 /* group keyrings are not yet supported */
569 key = ERR_PTR(-EINVAL);
573 key = ERR_PTR(-EINVAL);
577 key = key_lookup(id);
583 /* check the status and permissions */
585 ret = key_validate(key);
591 if (!partial && !(key->flags & KEY_FLAG_INSTANTIATED))
595 if (!key_permission(key, perm))
606 } /* end lookup_user_key() */
608 /*****************************************************************************/
610 * join the named keyring as the session keyring if possible, or attempt to
611 * create a new one of that name if not
612 * - if the name is NULL, an empty anonymous keyring is installed instead
613 * - named session keyring joining is done with a semaphore held
615 long join_session_keyring(const char *name)
617 struct task_struct *tsk = current;
622 /* if no name is provided, install an anonymous keyring */
624 ret = install_session_keyring(tsk, NULL);
628 spin_lock_irqsave(&tsk->sighand->siglock, flags);
629 ret = tsk->signal->session_keyring->serial;
630 spin_unlock_irqrestore(&tsk->sighand->siglock, flags);
634 /* allow the user to join or create a named keyring */
635 down(&key_session_sem);
637 /* look for an existing keyring of this name */
638 keyring = find_keyring_by_name(name, 0);
639 if (PTR_ERR(keyring) == -ENOKEY) {
640 /* not found - try and create a new one */
641 keyring = keyring_alloc(name, tsk->uid, tsk->gid, 0, NULL);
642 if (IS_ERR(keyring)) {
643 ret = PTR_ERR(keyring);
647 else if (IS_ERR(keyring)) {
648 ret = PTR_ERR(keyring);
652 /* we've got a keyring - now to install it */
653 ret = install_session_keyring(tsk, keyring);
657 ret = keyring->serial;
661 up(&key_session_sem);
665 } /* end join_session_keyring() */