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 .sem
= __RWSEM_INITIALIZER(root_user_keyring
.sem
),
43 .flags
= 1 << KEY_FLAG_INSTANTIATED
,
44 .description
= "_uid.0",
46 .magic
= KEY_DEBUG_MAGIC
,
50 /* the root user's default session keyring */
51 struct key root_session_keyring
= {
52 .usage
= ATOMIC_INIT(1),
54 .type
= &key_type_keyring
,
55 .user
= &root_key_user
,
56 .sem
= __RWSEM_INITIALIZER(root_session_keyring
.sem
),
58 .flags
= 1 << KEY_FLAG_INSTANTIATED
,
59 .description
= "_uid_ses.0",
61 .magic
= KEY_DEBUG_MAGIC
,
65 /*****************************************************************************/
67 * allocate the keyrings to be associated with a UID
69 int alloc_uid_keyring(struct user_struct
*user
)
71 struct key
*uid_keyring
, *session_keyring
;
75 /* concoct a default session keyring */
76 sprintf(buf
, "_uid_ses.%u", user
->uid
);
78 session_keyring
= keyring_alloc(buf
, user
->uid
, (gid_t
) -1, 0, NULL
);
79 if (IS_ERR(session_keyring
)) {
80 ret
= PTR_ERR(session_keyring
);
84 /* and a UID specific keyring, pointed to by the default session
86 sprintf(buf
, "_uid.%u", user
->uid
);
88 uid_keyring
= keyring_alloc(buf
, user
->uid
, (gid_t
) -1, 0,
90 if (IS_ERR(uid_keyring
)) {
91 key_put(session_keyring
);
92 ret
= PTR_ERR(uid_keyring
);
96 /* install the keyrings */
97 user
->uid_keyring
= uid_keyring
;
98 user
->session_keyring
= session_keyring
;
104 } /* end alloc_uid_keyring() */
106 /*****************************************************************************/
108 * deal with the UID changing
110 void switch_uid_keyring(struct user_struct
*new_user
)
112 #if 0 /* do nothing for now */
115 /* switch to the new user's session keyring if we were running under
116 * root's default session keyring */
117 if (new_user
->uid
!= 0 &&
118 current
->session_keyring
== &root_session_keyring
120 atomic_inc(&new_user
->session_keyring
->usage
);
123 old
= current
->session_keyring
;
124 current
->session_keyring
= new_user
->session_keyring
;
125 task_unlock(current
);
131 } /* end switch_uid_keyring() */
133 /*****************************************************************************/
135 * install a fresh thread keyring, discarding the old one
137 int install_thread_keyring(struct task_struct
*tsk
)
139 struct key
*keyring
, *old
;
143 sprintf(buf
, "_tid.%u", tsk
->pid
);
145 keyring
= keyring_alloc(buf
, tsk
->uid
, tsk
->gid
, 1, NULL
);
146 if (IS_ERR(keyring
)) {
147 ret
= PTR_ERR(keyring
);
152 old
= tsk
->thread_keyring
;
153 tsk
->thread_keyring
= keyring
;
162 } /* end install_thread_keyring() */
164 /*****************************************************************************/
166 * make sure a process keyring is installed
168 static int install_process_keyring(struct task_struct
*tsk
)
175 if (!tsk
->signal
->process_keyring
) {
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
);
184 /* attach or swap keyrings */
185 spin_lock_irqsave(&tsk
->sighand
->siglock
, flags
);
186 if (!tsk
->signal
->process_keyring
) {
187 tsk
->signal
->process_keyring
= keyring
;
190 spin_unlock_irqrestore(&tsk
->sighand
->siglock
, flags
);
199 } /* end install_process_keyring() */
201 /*****************************************************************************/
203 * install a session keyring, discarding the old one
204 * - if a keyring is not supplied, an empty one is invented
206 static int install_session_keyring(struct task_struct
*tsk
,
214 /* create an empty session keyring */
216 sprintf(buf
, "_ses.%u", tsk
->tgid
);
218 keyring
= keyring_alloc(buf
, tsk
->uid
, tsk
->gid
, 1, NULL
);
219 if (IS_ERR(keyring
)) {
220 ret
= PTR_ERR(keyring
);
225 atomic_inc(&keyring
->usage
);
228 /* install the keyring */
229 spin_lock_irqsave(&tsk
->sighand
->siglock
, flags
);
230 old
= tsk
->signal
->session_keyring
;
231 tsk
->signal
->session_keyring
= keyring
;
232 spin_unlock_irqrestore(&tsk
->sighand
->siglock
, flags
);
240 } /* end install_session_keyring() */
242 /*****************************************************************************/
244 * copy the keys in a thread group for fork without CLONE_THREAD
246 int copy_thread_group_keys(struct task_struct
*tsk
)
250 key_check(current
->thread_group
->session_keyring
);
251 key_check(current
->thread_group
->process_keyring
);
253 /* no process keyring yet */
254 tsk
->signal
->process_keyring
= NULL
;
256 /* same session keyring */
257 spin_lock_irqsave(¤t
->sighand
->siglock
, flags
);
258 tsk
->signal
->session_keyring
=
259 key_get(current
->signal
->session_keyring
);
260 spin_unlock_irqrestore(¤t
->sighand
->siglock
, flags
);
264 } /* end copy_thread_group_keys() */
266 /*****************************************************************************/
268 * copy the keys for fork
270 int copy_keys(unsigned long clone_flags
, struct task_struct
*tsk
)
272 key_check(tsk
->thread_keyring
);
274 /* no thread keyring yet */
275 tsk
->thread_keyring
= NULL
;
278 } /* end copy_keys() */
280 /*****************************************************************************/
282 * dispose of thread group keys upon thread group destruction
284 void exit_thread_group_keys(struct signal_struct
*tg
)
286 key_put(tg
->session_keyring
);
287 key_put(tg
->process_keyring
);
289 } /* end exit_thread_group_keys() */
291 /*****************************************************************************/
293 * dispose of keys upon thread exit
295 void exit_keys(struct task_struct
*tsk
)
297 key_put(tsk
->thread_keyring
);
299 } /* end exit_keys() */
301 /*****************************************************************************/
305 int exec_keys(struct task_struct
*tsk
)
310 /* newly exec'd tasks don't get a thread keyring */
312 old
= tsk
->thread_keyring
;
313 tsk
->thread_keyring
= NULL
;
318 /* discard the process keyring from a newly exec'd task */
319 spin_lock_irqsave(&tsk
->sighand
->siglock
, flags
);
320 old
= tsk
->signal
->process_keyring
;
321 tsk
->signal
->process_keyring
= NULL
;
322 spin_unlock_irqrestore(&tsk
->sighand
->siglock
, flags
);
328 } /* end exec_keys() */
330 /*****************************************************************************/
332 * deal with SUID programs
333 * - we might want to make this invent a new session keyring
335 int suid_keys(struct task_struct
*tsk
)
339 } /* end suid_keys() */
341 /*****************************************************************************/
343 * the filesystem user ID changed
345 void key_fsuid_changed(struct task_struct
*tsk
)
347 /* update the ownership of the thread keyring */
348 if (tsk
->thread_keyring
) {
349 down_write(&tsk
->thread_keyring
->sem
);
350 tsk
->thread_keyring
->uid
= tsk
->fsuid
;
351 up_write(&tsk
->thread_keyring
->sem
);
354 } /* end key_fsuid_changed() */
356 /*****************************************************************************/
358 * the filesystem group ID changed
360 void key_fsgid_changed(struct task_struct
*tsk
)
362 /* update the ownership of the thread keyring */
363 if (tsk
->thread_keyring
) {
364 down_write(&tsk
->thread_keyring
->sem
);
365 tsk
->thread_keyring
->gid
= tsk
->fsgid
;
366 up_write(&tsk
->thread_keyring
->sem
);
369 } /* end key_fsgid_changed() */
371 /*****************************************************************************/
373 * search the process keyrings for the first matching key
374 * - we use the supplied match function to see if the description (or other
375 * feature of interest) matches
376 * - we return -EAGAIN if we didn't find any matching key
377 * - we return -ENOKEY if we found only negative matching keys
379 struct key
*search_process_keyrings_aux(struct key_type
*type
,
380 const void *description
,
381 key_match_func_t match
)
383 struct task_struct
*tsk
= current
;
385 struct key
*key
, *ret
, *err
, *tmp
;
387 /* we want to return -EAGAIN or -ENOKEY if any of the keyrings were
388 * searchable, but we failed to find a key or we found a negative key;
389 * otherwise we want to return a sample error (probably -EACCES) if
390 * none of the keyrings were searchable
392 * in terms of priority: success > -ENOKEY > -EAGAIN > other error
396 err
= ERR_PTR(-EAGAIN
);
398 /* search the thread keyring first */
399 if (tsk
->thread_keyring
) {
400 key
= keyring_search_aux(tsk
->thread_keyring
, type
,
405 switch (PTR_ERR(key
)) {
406 case -EAGAIN
: /* no key */
409 case -ENOKEY
: /* negative key */
418 /* search the process keyring second */
419 if (tsk
->signal
->process_keyring
) {
420 key
= keyring_search_aux(tsk
->signal
->process_keyring
,
421 type
, description
, match
);
425 switch (PTR_ERR(key
)) {
426 case -EAGAIN
: /* no key */
429 case -ENOKEY
: /* negative key */
438 /* search the session keyring last */
439 spin_lock_irqsave(&tsk
->sighand
->siglock
, flags
);
441 tmp
= tsk
->signal
->session_keyring
;
443 tmp
= tsk
->user
->session_keyring
;
444 atomic_inc(&tmp
->usage
);
446 spin_unlock_irqrestore(&tsk
->sighand
->siglock
, flags
);
448 key
= keyring_search_aux(tmp
, type
, description
, match
);
453 switch (PTR_ERR(key
)) {
454 case -EAGAIN
: /* no key */
457 case -ENOKEY
: /* negative key */
465 /* no key - decide on the error we're going to go for */
466 key
= ret
? ret
: err
;
471 } /* end search_process_keyrings_aux() */
473 /*****************************************************************************/
475 * search the process keyrings for the first matching key
476 * - we return -EAGAIN if we didn't find any matching key
477 * - we return -ENOKEY if we found only negative matching keys
479 struct key
*search_process_keyrings(struct key_type
*type
,
480 const char *description
)
482 return search_process_keyrings_aux(type
, description
, type
->match
);
484 } /* end search_process_keyrings() */
486 /*****************************************************************************/
488 * lookup a key given a key ID from userspace with a given permissions mask
489 * - don't create special keyrings unless so requested
490 * - partially constructed keys aren't found unless requested
492 struct key
*lookup_user_key(key_serial_t id
, int create
, int partial
,
495 struct task_struct
*tsk
= current
;
500 key
= ERR_PTR(-ENOKEY
);
503 case KEY_SPEC_THREAD_KEYRING
:
504 if (!tsk
->thread_keyring
) {
508 ret
= install_thread_keyring(tsk
);
515 key
= tsk
->thread_keyring
;
516 atomic_inc(&key
->usage
);
519 case KEY_SPEC_PROCESS_KEYRING
:
520 if (!tsk
->signal
->process_keyring
) {
524 ret
= install_process_keyring(tsk
);
531 key
= tsk
->signal
->process_keyring
;
532 atomic_inc(&key
->usage
);
535 case KEY_SPEC_SESSION_KEYRING
:
536 if (!tsk
->signal
->session_keyring
) {
537 /* always install a session keyring upon access if one
538 * doesn't exist yet */
539 ret
= install_session_keyring(
540 tsk
, tsk
->user
->session_keyring
);
545 spin_lock_irqsave(&tsk
->sighand
->siglock
, flags
);
546 key
= tsk
->signal
->session_keyring
;
547 atomic_inc(&key
->usage
);
548 spin_unlock_irqrestore(&tsk
->sighand
->siglock
, flags
);
551 case KEY_SPEC_USER_KEYRING
:
552 key
= tsk
->user
->uid_keyring
;
553 atomic_inc(&key
->usage
);
556 case KEY_SPEC_USER_SESSION_KEYRING
:
557 key
= tsk
->user
->session_keyring
;
558 atomic_inc(&key
->usage
);
561 case KEY_SPEC_GROUP_KEYRING
:
562 /* group keyrings are not yet supported */
563 key
= ERR_PTR(-EINVAL
);
567 key
= ERR_PTR(-EINVAL
);
571 key
= key_lookup(id
);
577 /* check the status and permissions */
579 ret
= key_validate(key
);
585 if (!partial
&& !test_bit(KEY_FLAG_INSTANTIATED
, &key
->flags
))
589 if (!key_permission(key
, perm
))
600 } /* end lookup_user_key() */
602 /*****************************************************************************/
604 * join the named keyring as the session keyring if possible, or attempt to
605 * create a new one of that name if not
606 * - if the name is NULL, an empty anonymous keyring is installed instead
607 * - named session keyring joining is done with a semaphore held
609 long join_session_keyring(const char *name
)
611 struct task_struct
*tsk
= current
;
616 /* if no name is provided, install an anonymous keyring */
618 ret
= install_session_keyring(tsk
, NULL
);
622 spin_lock_irqsave(&tsk
->sighand
->siglock
, flags
);
623 ret
= tsk
->signal
->session_keyring
->serial
;
624 spin_unlock_irqrestore(&tsk
->sighand
->siglock
, flags
);
628 /* allow the user to join or create a named keyring */
629 down(&key_session_sem
);
631 /* look for an existing keyring of this name */
632 keyring
= find_keyring_by_name(name
, 0);
633 if (PTR_ERR(keyring
) == -ENOKEY
) {
634 /* not found - try and create a new one */
635 keyring
= keyring_alloc(name
, tsk
->uid
, tsk
->gid
, 0, NULL
);
636 if (IS_ERR(keyring
)) {
637 ret
= PTR_ERR(keyring
);
641 else if (IS_ERR(keyring
)) {
642 ret
= PTR_ERR(keyring
);
646 /* we've got a keyring - now to install it */
647 ret
= install_session_keyring(tsk
, keyring
);
651 ret
= keyring
->serial
;
655 up(&key_session_sem
);
659 } /* end join_session_keyring() */