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 * install a fresh process keyring, discarding the old one
170 static int install_process_keyring(struct task_struct *tsk)
172 struct key *keyring, *old;
176 sprintf(buf, "_pid.%u", tsk->tgid);
178 keyring = keyring_alloc(buf, tsk->uid, tsk->gid, 1, NULL);
179 if (IS_ERR(keyring)) {
180 ret = PTR_ERR(keyring);
185 old = tsk->process_keyring;
186 tsk->process_keyring = keyring;
195 } /* end install_process_keyring() */
197 /*****************************************************************************/
199 * install a session keyring, discarding the old one
200 * - if a keyring is not supplied, an empty one is invented
202 static int install_session_keyring(struct task_struct *tsk,
209 /* create an empty session keyring */
211 sprintf(buf, "_ses.%u", tsk->tgid);
213 keyring = keyring_alloc(buf, tsk->uid, tsk->gid, 1, NULL);
214 if (IS_ERR(keyring)) {
215 ret = PTR_ERR(keyring);
220 atomic_inc(&keyring->usage);
223 /* install the keyring */
225 old = tsk->session_keyring;
226 tsk->session_keyring = keyring;
235 } /* end install_session_keyring() */
237 /*****************************************************************************/
239 * copy the keys for fork
241 int copy_keys(unsigned long clone_flags, struct task_struct *tsk)
245 key_check(tsk->session_keyring);
246 key_check(tsk->process_keyring);
247 key_check(tsk->thread_keyring);
249 if (tsk->session_keyring)
250 atomic_inc(&tsk->session_keyring->usage);
252 if (tsk->process_keyring) {
253 if (clone_flags & CLONE_THREAD) {
254 atomic_inc(&tsk->process_keyring->usage);
257 tsk->process_keyring = NULL;
258 ret = install_process_keyring(tsk);
262 tsk->thread_keyring = NULL;
265 } /* end copy_keys() */
267 /*****************************************************************************/
269 * dispose of keys upon exit
271 void exit_keys(struct task_struct *tsk)
273 key_put(tsk->session_keyring);
274 key_put(tsk->process_keyring);
275 key_put(tsk->thread_keyring);
277 } /* end exit_keys() */
279 /*****************************************************************************/
283 int exec_keys(struct task_struct *tsk)
287 /* newly exec'd tasks don't get a thread keyring */
289 old = tsk->thread_keyring;
290 tsk->thread_keyring = NULL;
295 /* newly exec'd tasks get a fresh process keyring */
296 return install_process_keyring(tsk);
298 } /* end exec_keys() */
300 /*****************************************************************************/
302 * deal with SUID programs
303 * - we might want to make this invent a new session keyring
305 int suid_keys(struct task_struct *tsk)
309 } /* end suid_keys() */
311 /*****************************************************************************/
313 * the filesystem user ID changed
315 void key_fsuid_changed(struct task_struct *tsk)
317 /* update the ownership of the process keyring */
318 if (tsk->process_keyring) {
319 down_write(&tsk->process_keyring->sem);
320 write_lock(&tsk->process_keyring->lock);
321 tsk->process_keyring->uid = tsk->fsuid;
322 write_unlock(&tsk->process_keyring->lock);
323 up_write(&tsk->process_keyring->sem);
326 /* update the ownership of the thread keyring */
327 if (tsk->thread_keyring) {
328 down_write(&tsk->thread_keyring->sem);
329 write_lock(&tsk->thread_keyring->lock);
330 tsk->thread_keyring->uid = tsk->fsuid;
331 write_unlock(&tsk->thread_keyring->lock);
332 up_write(&tsk->thread_keyring->sem);
335 } /* end key_fsuid_changed() */
337 /*****************************************************************************/
339 * the filesystem group ID changed
341 void key_fsgid_changed(struct task_struct *tsk)
343 /* update the ownership of the process keyring */
344 if (tsk->process_keyring) {
345 down_write(&tsk->process_keyring->sem);
346 write_lock(&tsk->process_keyring->lock);
347 tsk->process_keyring->gid = tsk->fsgid;
348 write_unlock(&tsk->process_keyring->lock);
349 up_write(&tsk->process_keyring->sem);
352 /* update the ownership of the thread keyring */
353 if (tsk->thread_keyring) {
354 down_write(&tsk->thread_keyring->sem);
355 write_lock(&tsk->thread_keyring->lock);
356 tsk->thread_keyring->gid = tsk->fsgid;
357 write_unlock(&tsk->thread_keyring->lock);
358 up_write(&tsk->thread_keyring->sem);
361 } /* end key_fsgid_changed() */
363 /*****************************************************************************/
365 * search the process keyrings for the first matching key
366 * - we use the supplied match function to see if the description (or other
367 * feature of interest) matches
368 * - we return -EAGAIN if we didn't find any matching key
369 * - we return -ENOKEY if we found only negative matching keys
371 struct key *search_process_keyrings_aux(struct key_type *type,
372 const void *description,
373 key_match_func_t match)
375 struct task_struct *tsk = current;
376 struct key *key, *ret, *err, *session;
378 /* we want to return -EAGAIN or -ENOKEY if any of the keyrings were
379 * searchable, but we failed to find a key or we found a negative key;
380 * otherwise we want to return a sample error (probably -EACCES) if
381 * none of the keyrings were searchable
383 * in terms of priority: success > -ENOKEY > -EAGAIN > other error
387 err = ERR_PTR(-EAGAIN);
389 /* search the thread keyring first */
390 if (tsk->thread_keyring) {
391 key = keyring_search_aux(tsk->thread_keyring, type,
396 switch (PTR_ERR(key)) {
397 case -EAGAIN: /* no key */
400 case -ENOKEY: /* negative key */
409 /* search the process keyring second */
410 if (tsk->process_keyring) {
411 key = keyring_search_aux(tsk->process_keyring, type,
416 switch (PTR_ERR(key)) {
417 case -EAGAIN: /* no key */
420 case -ENOKEY: /* negative key */
429 /* search the session keyring last */
430 session = tsk->session_keyring;
432 session = tsk->user->session_keyring;
434 key = keyring_search_aux(session, type,
439 switch (PTR_ERR(key)) {
440 case -EAGAIN: /* no key */
443 case -ENOKEY: /* negative key */
451 /* no key - decide on the error we're going to go for */
452 key = ret ? ret : err;
457 } /* end search_process_keyrings_aux() */
459 /*****************************************************************************/
461 * search the process keyrings for the first matching key
462 * - we return -EAGAIN if we didn't find any matching key
463 * - we return -ENOKEY if we found only negative matching keys
465 struct key *search_process_keyrings(struct key_type *type,
466 const char *description)
468 return search_process_keyrings_aux(type, description, type->match);
470 } /* end search_process_keyrings() */
472 /*****************************************************************************/
474 * lookup a key given a key ID from userspace with a given permissions mask
475 * - don't create special keyrings unless so requested
476 * - partially constructed keys aren't found unless requested
478 struct key *lookup_user_key(key_serial_t id, int create, int partial,
481 struct task_struct *tsk = current;
485 key = ERR_PTR(-ENOKEY);
488 case KEY_SPEC_THREAD_KEYRING:
489 if (!tsk->thread_keyring) {
493 ret = install_thread_keyring(tsk);
500 key = tsk->thread_keyring;
501 atomic_inc(&key->usage);
504 case KEY_SPEC_PROCESS_KEYRING:
505 if (!tsk->process_keyring) {
509 ret = install_process_keyring(tsk);
516 key = tsk->process_keyring;
517 atomic_inc(&key->usage);
520 case KEY_SPEC_SESSION_KEYRING:
521 if (!tsk->session_keyring) {
522 /* always install a session keyring upon access if one
523 * doesn't exist yet */
524 ret = install_session_keyring(
525 tsk, tsk->user->session_keyring);
530 key = tsk->session_keyring;
531 atomic_inc(&key->usage);
534 case KEY_SPEC_USER_KEYRING:
535 key = tsk->user->uid_keyring;
536 atomic_inc(&key->usage);
539 case KEY_SPEC_USER_SESSION_KEYRING:
540 key = tsk->user->session_keyring;
541 atomic_inc(&key->usage);
544 case KEY_SPEC_GROUP_KEYRING:
545 /* group keyrings are not yet supported */
546 key = ERR_PTR(-EINVAL);
550 key = ERR_PTR(-EINVAL);
554 key = key_lookup(id);
560 /* check the status and permissions */
562 ret = key_validate(key);
568 if (!partial && !(key->flags & KEY_FLAG_INSTANTIATED))
572 if (!key_permission(key, perm))
583 } /* end lookup_user_key() */
585 /*****************************************************************************/
587 * join the named keyring as the session keyring if possible, or attempt to
588 * create a new one of that name if not
589 * - if the name is NULL, an empty anonymous keyring is installed instead
590 * - named session keyring joining is done with a semaphore held
592 long join_session_keyring(const char *name)
594 struct task_struct *tsk = current;
598 /* if no name is provided, install an anonymous keyring */
600 ret = install_session_keyring(tsk, NULL);
604 ret = tsk->session_keyring->serial;
608 /* allow the user to join or create a named keyring */
609 down(&key_session_sem);
611 /* look for an existing keyring of this name */
612 keyring = find_keyring_by_name(name, 0);
613 if (PTR_ERR(keyring) == -ENOKEY) {
614 /* not found - try and create a new one */
615 keyring = keyring_alloc(name, tsk->uid, tsk->gid, 0, NULL);
616 if (IS_ERR(keyring)) {
617 ret = PTR_ERR(keyring);
621 else if (IS_ERR(keyring)) {
622 ret = PTR_ERR(keyring);
626 /* we've got a keyring - now to install it */
627 ret = install_session_keyring(tsk, keyring);
633 ret = tsk->session_keyring->serial;
636 up(&key_session_sem);
640 } /* end join_session_keyring() */