11 #include <linux/export.h>
13 #include <linux/slab.h>
14 #include <linux/sched.h>
19 #include <linux/binfmts.h>
20 #include <linux/cn_proc.h>
23 #define kdebug(FMT, ...) \
24 printk("[%-5.5s%5u] "FMT"\n", current->comm, current->pid ,##__VA_ARGS__)
26 #define kdebug(FMT, ...) \
27 no_printk("[%-5.5s%5u] "FMT"\n", current->comm, current->pid ,##__VA_ARGS__)
36 static struct thread_group_cred init_tgcred = {
48 #ifdef CONFIG_DEBUG_CREDENTIALS
61 .cap_inheritable = CAP_EMPTY_SET,
62 .cap_permitted = CAP_FULL_SET,
63 .cap_effective = CAP_FULL_SET,
64 .cap_bset = CAP_FULL_SET,
69 .tgcred = &init_tgcred,
73 static inline void set_cred_subscribers(
struct cred *
cred,
int n)
75 #ifdef CONFIG_DEBUG_CREDENTIALS
80 static inline int read_cred_subscribers(
const struct cred *
cred)
82 #ifdef CONFIG_DEBUG_CREDENTIALS
89 static inline void alter_cred_subscribers(
const struct cred *_cred,
int n)
91 #ifdef CONFIG_DEBUG_CREDENTIALS
92 struct cred *
cred = (
struct cred *) _cred;
102 static void release_tgcred_rcu(
struct rcu_head *
rcu)
104 struct thread_group_cred *tgcred =
109 key_put(tgcred->session_keyring);
110 key_put(tgcred->process_keyring);
118 static void release_tgcred(
struct cred *
cred)
121 struct thread_group_cred *tgcred = cred->tgcred;
124 call_rcu(&tgcred->rcu, release_tgcred_rcu);
131 static void put_cred_rcu(
struct rcu_head *rcu)
133 struct cred *cred =
container_of(rcu,
struct cred, rcu);
135 kdebug(
"put_cred_rcu(%p)", cred);
137 #ifdef CONFIG_DEBUG_CREDENTIALS
138 if (cred->magic != CRED_MAGIC_DEAD ||
140 read_cred_subscribers(cred) != 0)
141 panic(
"CRED: put_cred_rcu() sees %p with"
142 " mag %x, put %p, usage %d, subscr %d\n",
143 cred, cred->magic, cred->put_addr,
145 read_cred_subscribers(cred));
148 panic(
"CRED: put_cred_rcu() sees %p with usage %d\n",
154 key_put(cred->request_key_auth);
155 release_tgcred(cred);
171 kdebug(
"__put_cred(%p{%d,%d})", cred,
173 read_cred_subscribers(cred));
176 #ifdef CONFIG_DEBUG_CREDENTIALS
177 BUG_ON(read_cred_subscribers(cred) != 0);
178 cred->magic = CRED_MAGIC_DEAD;
179 cred->put_addr = __builtin_return_address(0);
197 read_cred_subscribers(tsk->
cred));
201 validate_creds(cred);
202 alter_cred_subscribers(cred, -1);
205 cred = (
struct cred *) tsk->
cred;
207 validate_creds(cred);
208 alter_cred_subscribers(cred, -1);
224 const struct cred *cred;
245 new = kmem_cache_zalloc(cred_jar,
GFP_KERNEL);
250 new->tgcred = kzalloc(
sizeof(*new->tgcred),
GFP_KERNEL);
259 #ifdef CONFIG_DEBUG_CREDENTIALS
260 new->magic = CRED_MAGIC;
290 const struct cred *old;
293 validate_process_creds();
299 kdebug(
"prepare_creds() alloc %p",
new);
302 memcpy(
new, old,
sizeof(
struct cred));
305 set_cred_subscribers(
new, 0);
306 get_group_info(new->group_info);
308 get_user_ns(new->user_ns);
311 key_get(new->thread_keyring);
312 key_get(new->request_key_auth);
316 #ifdef CONFIG_SECURITY
317 new->security =
NULL;
337 struct thread_group_cred *tgcred =
NULL;
355 new->thread_keyring =
NULL;
359 memcpy(tgcred, new->tgcred,
sizeof(
struct thread_group_cred));
365 key_get(tgcred->session_keyring);
366 tgcred->process_keyring =
NULL;
369 new->tgcred = tgcred;
387 struct thread_group_cred *tgcred;
394 !p->
cred->thread_keyring &&
400 alter_cred_subscribers(p->
cred, 2);
401 kdebug(
"share_creds(%p{%d,%d})",
403 read_cred_subscribers(p->
cred));
421 if (new->thread_keyring) {
423 new->thread_keyring =
NULL;
424 if (clone_flags & CLONE_THREAD)
431 if (!(clone_flags & CLONE_THREAD)) {
439 tgcred->process_keyring =
NULL;
440 tgcred->session_keyring = key_get(new->tgcred->session_keyring);
443 new->tgcred = tgcred;
449 alter_cred_subscribers(
new, 2);
475 const struct cred *old = task->
real_cred;
477 kdebug(
"commit_creds(%p{%d,%d})",
new,
479 read_cred_subscribers(
new));
482 #ifdef CONFIG_DEBUG_CREDENTIALS
483 BUG_ON(read_cred_subscribers(old) < 2);
492 if (!uid_eq(old->
euid, new->euid) ||
493 !gid_eq(old->
egid, new->egid) ||
494 !uid_eq(old->
fsuid, new->fsuid) ||
495 !gid_eq(old->
fsgid, new->fsgid) ||
504 if (!uid_eq(new->fsuid, old->
fsuid))
506 if (!gid_eq(new->fsgid, old->
fsgid))
513 alter_cred_subscribers(
new, 2);
514 if (new->user != old->
user)
518 if (new->user != old->
user)
520 alter_cred_subscribers(old, -2);
523 if (!uid_eq(new->uid, old->
uid) ||
524 !uid_eq(new->euid, old->
euid) ||
525 !uid_eq(new->suid, old->
suid) ||
526 !uid_eq(new->fsuid, old->
fsuid))
529 if (!gid_eq(new->gid, old->
gid) ||
530 !gid_eq(new->egid, old->
egid) ||
531 !gid_eq(new->sgid, old->
sgid) ||
532 !gid_eq(new->fsgid, old->
fsgid))
551 kdebug(
"abort_creds(%p{%d,%d})",
new,
553 read_cred_subscribers(
new));
555 #ifdef CONFIG_DEBUG_CREDENTIALS
556 BUG_ON(read_cred_subscribers(
new) != 0);
572 const struct cred *old =
current->cred;
574 kdebug(
"override_creds(%p{%d,%d})",
new,
576 read_cred_subscribers(
new));
581 alter_cred_subscribers(
new, 1);
583 alter_cred_subscribers(old, -1);
585 kdebug(
"override_creds() = %p{%d,%d}", old,
587 read_cred_subscribers(old));
601 const struct cred *
override =
current->cred;
603 kdebug(
"revert_creds(%p{%d,%d})", old,
605 read_cred_subscribers(old));
608 validate_creds(
override);
609 alter_cred_subscribers(old, 1);
611 alter_cred_subscribers(
override, -1);
647 struct thread_group_cred *tgcred;
649 const struct cred *old;
664 kdebug(
"prepare_kernel_cred() alloc %p",
new);
669 old = get_cred(&init_cred);
675 set_cred_subscribers(
new, 0);
677 get_user_ns(new->user_ns);
678 get_group_info(new->group_info);
683 tgcred->process_keyring =
NULL;
684 tgcred->session_keyring =
NULL;
685 new->tgcred = tgcred;
686 new->request_key_auth =
NULL;
687 new->thread_keyring =
NULL;
691 #ifdef CONFIG_SECURITY
692 new->security =
NULL;
756 new->fsuid = inode->
i_uid;
757 new->fsgid = inode->
i_gid;
762 #ifdef CONFIG_DEBUG_CREDENTIALS
764 bool creds_are_invalid(
const struct cred *cred)
766 if (cred->magic != CRED_MAGIC)
768 #ifdef CONFIG_SECURITY_SELINUX
774 if ((
unsigned long) cred->security <
PAGE_SIZE)
776 if ((*(
u32 *)cred->security & 0xffffff00) ==
788 static void dump_invalid_creds(
const struct cred *cred,
const char *
label,
793 cred == &init_cred ?
"[init]" :
"",
795 cred == tsk->
cred ?
"[eff]" :
"");
797 cred->magic, cred->put_addr);
800 read_cred_subscribers(cred));
811 #ifdef CONFIG_SECURITY
813 if ((
unsigned long) cred->security >=
PAGE_SIZE &&
814 (((
unsigned long) cred->security & 0xffffff00) !=
817 ((
u32*)cred->security)[0],
818 ((
u32*)cred->security)[1]);
825 void __invalid_creds(
const struct cred *cred,
const char *
file,
unsigned line)
829 dump_invalid_creds(cred,
"Specified",
current);
837 void __validate_process_creds(
struct task_struct *tsk,
842 creds_are_invalid(tsk->
cred)))
846 read_cred_subscribers(tsk->
cred) < 1 ||
848 creds_are_invalid(tsk->
cred)))
857 dump_invalid_creds(tsk->
real_cred,
"Real", tsk);
859 dump_invalid_creds(tsk->
cred,
"Effective", tsk);
869 void validate_creds_for_do_exit(
struct task_struct *tsk)
871 kdebug(
"validate_creds_for_do_exit(%p,%p{%d,%d})",
874 read_cred_subscribers(tsk->
cred));
876 __validate_process_creds(tsk, __FILE__, __LINE__);