12 #include <linux/module.h>
14 #include <linux/sched.h>
21 #include <asm/uaccess.h>
47 struct key *uid_keyring, *session_keyring;
56 kenter(
"%p{%u}", user, uid);
58 if (user->uid_keyring) {
66 if (!user->uid_keyring) {
74 if (IS_ERR(uid_keyring)) {
76 cred, KEY_ALLOC_IN_QUOTA,
78 if (IS_ERR(uid_keyring)) {
79 ret = PTR_ERR(uid_keyring);
86 sprintf(buf,
"_uid_ses.%u", uid);
89 if (IS_ERR(session_keyring)) {
92 cred, KEY_ALLOC_IN_QUOTA,
NULL);
93 if (IS_ERR(session_keyring)) {
94 ret = PTR_ERR(session_keyring);
100 ret =
key_link(session_keyring, uid_keyring);
102 goto error_release_both;
106 user->uid_keyring = uid_keyring;
107 user->session_keyring = session_keyring;
133 KEY_ALLOC_QUOTA_OVERRUN,
NULL);
135 return PTR_ERR(keyring);
137 new->thread_keyring = keyring;
144 static int install_thread_keyring(
void)
153 BUG_ON(new->thread_keyring);
175 if (new->tgcred->process_keyring)
179 new, KEY_ALLOC_QUOTA_OVERRUN,
NULL);
181 return PTR_ERR(keyring);
183 spin_lock_irq(&new->tgcred->lock);
184 if (!new->tgcred->process_keyring) {
185 new->tgcred->process_keyring = keyring;
191 spin_unlock_irq(&new->tgcred->lock);
203 static int install_process_keyring(
void)
215 return ret != -
EEXIST ? ret : 0;
233 flags = KEY_ALLOC_QUOTA_OVERRUN;
234 if (cred->tgcred->session_keyring)
235 flags = KEY_ALLOC_IN_QUOTA;
240 return PTR_ERR(keyring);
246 spin_lock_irq(&cred->tgcred->lock);
247 old = cred->tgcred->session_keyring;
249 spin_unlock_irq(&cred->tgcred->lock);
265 static int install_session_keyring(
struct key *keyring)
290 if (tsk->
cred->thread_keyring) {
292 tsk->
cred->thread_keyring->uid = tsk->
cred->fsuid;
304 if (tsk->
cred->thread_keyring) {
306 tsk->
cred->thread_keyring->gid = tsk->
cred->fsgid;
338 key_ref_t key_ref,
ret,
err;
352 if (cred->thread_keyring) {
354 make_key_ref(cred->thread_keyring, 1),
355 cred, type, description, match, no_state_check);
356 if (!IS_ERR(key_ref))
359 switch (PTR_ERR(key_ref)) {
373 if (cred->tgcred->process_keyring) {
375 make_key_ref(cred->tgcred->process_keyring, 1),
376 cred, type, description, match, no_state_check);
377 if (!IS_ERR(key_ref))
380 switch (PTR_ERR(key_ref)) {
394 if (cred->tgcred->session_keyring) {
398 cred->tgcred->session_keyring),
400 cred, type, description, match, no_state_check);
403 if (!IS_ERR(key_ref))
406 switch (PTR_ERR(key_ref)) {
419 else if (cred->
user->session_keyring) {
421 make_key_ref(cred->
user->session_keyring, 1),
422 cred, type, description, match, no_state_check);
423 if (!IS_ERR(key_ref))
426 switch (PTR_ERR(key_ref)) {
440 key_ref = ret ? ret :
err;
460 key_ref_t key_ref, ret = ERR_PTR(-
EACCES),
err;
466 if (!IS_ERR(key_ref))
474 if (cred->request_key_auth &&
482 rka = cred->request_key_auth->payload.data;
487 up_read(&cred->request_key_auth->sem);
489 if (!IS_ERR(key_ref))
494 up_read(&cred->request_key_auth->sem);
500 key_ref = ERR_PTR(-
ENOKEY);
542 key_ref_t key_ref, skey_ref;
547 key_ref = ERR_PTR(-
ENOKEY);
551 if (!cred->thread_keyring) {
555 ret = install_thread_keyring();
557 key_ref = ERR_PTR(ret);
563 key = cred->thread_keyring;
565 key_ref = make_key_ref(key, 1);
569 if (!cred->tgcred->process_keyring) {
573 ret = install_process_keyring();
575 key_ref = ERR_PTR(ret);
581 key = cred->tgcred->process_keyring;
583 key_ref = make_key_ref(key, 1);
587 if (!cred->tgcred->session_keyring) {
596 ret = install_session_keyring(
597 cred->
user->session_keyring);
602 }
else if (cred->tgcred->session_keyring ==
603 cred->
user->session_keyring &&
615 key_ref = make_key_ref(key, 1);
619 if (!cred->
user->uid_keyring) {
625 key = cred->
user->uid_keyring;
627 key_ref = make_key_ref(key, 1);
631 if (!cred->
user->session_keyring) {
637 key = cred->
user->session_keyring;
639 key_ref = make_key_ref(key, 1);
644 key_ref = ERR_PTR(-
EINVAL);
648 key = cred->request_key_auth;
653 key_ref = make_key_ref(key, 1);
657 if (!cred->request_key_auth)
662 &cred->request_key_auth->flags)) {
666 rka = cred->request_key_auth->payload.data;
670 up_read(&cred->request_key_auth->sem);
673 key_ref = make_key_ref(key, 1);
677 key_ref = ERR_PTR(-
EINVAL);
683 key_ref = ERR_CAST(key);
687 key_ref = make_key_ref(key, 0);
694 if (!IS_ERR(skey_ref)) {
727 if (!(lflags & KEY_LOOKUP_PARTIAL) &&
728 !
test_bit(KEY_FLAG_INSTANTIATED, &key->flags))
743 key_ref_put(key_ref);
744 key_ref = ERR_PTR(ret);
767 const struct cred *old;
789 serial =
new->tgcred->session_keyring->serial;
801 if (PTR_ERR(keyring) == -
ENOKEY) {
804 KEY_ALLOC_IN_QUOTA,
NULL);
805 if (IS_ERR(keyring)) {
806 ret = PTR_ERR(keyring);
809 }
else if (IS_ERR(keyring)) {
810 ret = PTR_ERR(keyring);
822 ret = keyring->serial;
851 new->fsuid = old->
fsuid;
855 new->fsgid = old->
fsgid;
856 new->user = get_uid(old->
user);
857 new->user_ns = get_user_ns(new->user_ns);
858 new->group_info = get_group_info(old->
group_info);
866 new->jit_keyring = old->jit_keyring;
867 new->thread_keyring = key_get(old->thread_keyring);
868 new->tgcred->tgid = old->tgcred->tgid;
869 new->tgcred->process_keyring = key_get(old->tgcred->process_keyring);