1 /* Task credentials management
3 * Copyright (C) 2008 Red Hat, Inc. All Rights Reserved.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public Licence
8 * as published by the Free Software Foundation; either version
9 * 2 of the Licence, or (at your option) any later version.
11 #include <linux/module.h>
12 #include <linux/cred.h>
13 #include <linux/sched.h>
14 #include <linux/key.h>
15 #include <linux/keyctl.h>
16 #include <linux/init_task.h>
17 #include <linux/security.h>
20 * The common credentials for the initial task's thread group
23 static struct thread_group_cred init_tgcred = {
24 .usage = ATOMIC_INIT(2),
26 .lock = SPIN_LOCK_UNLOCKED,
31 * The initial credentials for the initial task
33 struct cred init_cred = {
34 .usage = ATOMIC_INIT(3),
35 .securebits = SECUREBITS_DEFAULT,
36 .cap_inheritable = CAP_INIT_INH_SET,
37 .cap_permitted = CAP_FULL_SET,
38 .cap_effective = CAP_INIT_EFF_SET,
39 .cap_bset = CAP_INIT_BSET,
41 .group_info = &init_groups,
43 .tgcred = &init_tgcred,
48 * Dispose of the shared task group credentials
51 static void release_tgcred_rcu(struct rcu_head *rcu)
53 struct thread_group_cred *tgcred =
54 container_of(rcu, struct thread_group_cred, rcu);
56 BUG_ON(atomic_read(&tgcred->usage) != 0);
58 key_put(tgcred->session_keyring);
59 key_put(tgcred->process_keyring);
65 * Release a set of thread group credentials.
67 static void release_tgcred(struct cred *cred)
70 struct thread_group_cred *tgcred = cred->tgcred;
72 if (atomic_dec_and_test(&tgcred->usage))
73 call_rcu(&tgcred->rcu, release_tgcred_rcu);
78 * The RCU callback to actually dispose of a set of credentials
80 static void put_cred_rcu(struct rcu_head *rcu)
82 struct cred *cred = container_of(rcu, struct cred, rcu);
84 BUG_ON(atomic_read(&cred->usage) != 0);
86 key_put(cred->thread_keyring);
87 key_put(cred->request_key_auth);
89 put_group_info(cred->group_info);
91 security_cred_free(cred);
96 * __put_cred - Destroy a set of credentials
97 * @sec: The record to release
99 * Destroy a set of credentials on which no references remain.
101 void __put_cred(struct cred *cred)
103 call_rcu(&cred->rcu, put_cred_rcu);
105 EXPORT_SYMBOL(__put_cred);
108 * Copy credentials for the new process created by fork()
110 int copy_creds(struct task_struct *p, unsigned long clone_flags)
115 pcred = kmemdup(p->cred, sizeof(*p->cred), GFP_KERNEL);
120 if (clone_flags & CLONE_THREAD) {
121 atomic_inc(&pcred->tgcred->usage);
123 pcred->tgcred = kmalloc(sizeof(struct cred), GFP_KERNEL);
124 if (!pcred->tgcred) {
128 atomic_set(&pcred->tgcred->usage, 1);
129 spin_lock_init(&pcred->tgcred->lock);
130 pcred->tgcred->process_keyring = NULL;
131 pcred->tgcred->session_keyring =
132 key_get(p->cred->tgcred->session_keyring);
136 #ifdef CONFIG_SECURITY
137 pcred->security = NULL;
140 ret = security_cred_alloc(pcred);
142 release_tgcred(pcred);
147 atomic_set(&pcred->usage, 1);
148 get_group_info(pcred->group_info);
149 get_uid(pcred->user);
150 key_get(pcred->thread_keyring);
151 key_get(pcred->request_key_auth);
153 atomic_inc(&pcred->user->processes);
155 /* RCU assignment is unneeded here as no-one can have accessed this
156 * pointer yet, barring us */