blob: 13697ca2bb382cb0950d4902a4e4cbaa9e6b1c53 [file] [log] [blame]
David Howells98870ab2008-11-14 10:39:26 +11001/* Task credentials management - see Documentation/credentials.txt
David Howellsf1752ee2008-11-14 10:39:17 +11002 *
3 * Copyright (C) 2008 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 *
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.
10 */
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>
David Howellsd84f4f92008-11-14 10:39:23 +110018#include <linux/cn_proc.h>
19#include "cred-internals.h"
20
21static struct kmem_cache *cred_jar;
David Howellsf1752ee2008-11-14 10:39:17 +110022
23/*
David Howellsbb952bb2008-11-14 10:39:20 +110024 * The common credentials for the initial task's thread group
25 */
26#ifdef CONFIG_KEYS
27static struct thread_group_cred init_tgcred = {
28 .usage = ATOMIC_INIT(2),
29 .tgid = 0,
30 .lock = SPIN_LOCK_UNLOCKED,
31};
32#endif
33
34/*
David Howellsf1752ee2008-11-14 10:39:17 +110035 * The initial credentials for the initial task
36 */
37struct cred init_cred = {
David Howells3b11a1d2008-11-14 10:39:26 +110038 .usage = ATOMIC_INIT(4),
David Howellsf1752ee2008-11-14 10:39:17 +110039 .securebits = SECUREBITS_DEFAULT,
40 .cap_inheritable = CAP_INIT_INH_SET,
41 .cap_permitted = CAP_FULL_SET,
42 .cap_effective = CAP_INIT_EFF_SET,
43 .cap_bset = CAP_INIT_BSET,
44 .user = INIT_USER,
45 .group_info = &init_groups,
David Howellsbb952bb2008-11-14 10:39:20 +110046#ifdef CONFIG_KEYS
47 .tgcred = &init_tgcred,
48#endif
David Howellsf1752ee2008-11-14 10:39:17 +110049};
50
51/*
David Howellsbb952bb2008-11-14 10:39:20 +110052 * Dispose of the shared task group credentials
53 */
54#ifdef CONFIG_KEYS
55static void release_tgcred_rcu(struct rcu_head *rcu)
56{
57 struct thread_group_cred *tgcred =
58 container_of(rcu, struct thread_group_cred, rcu);
59
60 BUG_ON(atomic_read(&tgcred->usage) != 0);
61
62 key_put(tgcred->session_keyring);
63 key_put(tgcred->process_keyring);
64 kfree(tgcred);
65}
66#endif
67
68/*
69 * Release a set of thread group credentials.
70 */
David Howellsa6f76f22008-11-14 10:39:24 +110071static void release_tgcred(struct cred *cred)
David Howellsbb952bb2008-11-14 10:39:20 +110072{
73#ifdef CONFIG_KEYS
74 struct thread_group_cred *tgcred = cred->tgcred;
75
76 if (atomic_dec_and_test(&tgcred->usage))
77 call_rcu(&tgcred->rcu, release_tgcred_rcu);
78#endif
79}
80
81/*
David Howellsf1752ee2008-11-14 10:39:17 +110082 * The RCU callback to actually dispose of a set of credentials
83 */
84static void put_cred_rcu(struct rcu_head *rcu)
85{
86 struct cred *cred = container_of(rcu, struct cred, rcu);
87
David Howellsd84f4f92008-11-14 10:39:23 +110088 if (atomic_read(&cred->usage) != 0)
89 panic("CRED: put_cred_rcu() sees %p with usage %d\n",
90 cred, atomic_read(&cred->usage));
David Howellsf1752ee2008-11-14 10:39:17 +110091
David Howellsd84f4f92008-11-14 10:39:23 +110092 security_cred_free(cred);
David Howellsf1752ee2008-11-14 10:39:17 +110093 key_put(cred->thread_keyring);
94 key_put(cred->request_key_auth);
David Howellsbb952bb2008-11-14 10:39:20 +110095 release_tgcred(cred);
David Howellsf1752ee2008-11-14 10:39:17 +110096 put_group_info(cred->group_info);
97 free_uid(cred->user);
David Howellsd84f4f92008-11-14 10:39:23 +110098 kmem_cache_free(cred_jar, cred);
David Howellsf1752ee2008-11-14 10:39:17 +110099}
100
101/**
102 * __put_cred - Destroy a set of credentials
David Howellsd84f4f92008-11-14 10:39:23 +1100103 * @cred: The record to release
David Howellsf1752ee2008-11-14 10:39:17 +1100104 *
105 * Destroy a set of credentials on which no references remain.
106 */
107void __put_cred(struct cred *cred)
108{
David Howellsd84f4f92008-11-14 10:39:23 +1100109 BUG_ON(atomic_read(&cred->usage) != 0);
110
David Howellsf1752ee2008-11-14 10:39:17 +1100111 call_rcu(&cred->rcu, put_cred_rcu);
112}
113EXPORT_SYMBOL(__put_cred);
114
David Howellsd84f4f92008-11-14 10:39:23 +1100115/**
116 * prepare_creds - Prepare a new set of credentials for modification
117 *
118 * Prepare a new set of task credentials for modification. A task's creds
119 * shouldn't generally be modified directly, therefore this function is used to
120 * prepare a new copy, which the caller then modifies and then commits by
121 * calling commit_creds().
122 *
David Howells3b11a1d2008-11-14 10:39:26 +1100123 * Preparation involves making a copy of the objective creds for modification.
124 *
David Howellsd84f4f92008-11-14 10:39:23 +1100125 * Returns a pointer to the new creds-to-be if successful, NULL otherwise.
126 *
127 * Call commit_creds() or abort_creds() to clean up.
David Howellsf1752ee2008-11-14 10:39:17 +1100128 */
David Howellsd84f4f92008-11-14 10:39:23 +1100129struct cred *prepare_creds(void)
David Howellsf1752ee2008-11-14 10:39:17 +1100130{
David Howellsd84f4f92008-11-14 10:39:23 +1100131 struct task_struct *task = current;
132 const struct cred *old;
133 struct cred *new;
David Howellsf1752ee2008-11-14 10:39:17 +1100134
David Howells3b11a1d2008-11-14 10:39:26 +1100135 BUG_ON(atomic_read(&task->real_cred->usage) < 1);
David Howellsd84f4f92008-11-14 10:39:23 +1100136
137 new = kmem_cache_alloc(cred_jar, GFP_KERNEL);
138 if (!new)
139 return NULL;
140
141 old = task->cred;
142 memcpy(new, old, sizeof(struct cred));
143
144 atomic_set(&new->usage, 1);
145 get_group_info(new->group_info);
146 get_uid(new->user);
David Howellsf1752ee2008-11-14 10:39:17 +1100147
David Howellsbb952bb2008-11-14 10:39:20 +1100148#ifdef CONFIG_KEYS
David Howellsd84f4f92008-11-14 10:39:23 +1100149 key_get(new->thread_keyring);
150 key_get(new->request_key_auth);
151 atomic_inc(&new->tgcred->usage);
David Howellsbb952bb2008-11-14 10:39:20 +1100152#endif
153
David Howellsf1752ee2008-11-14 10:39:17 +1100154#ifdef CONFIG_SECURITY
David Howellsd84f4f92008-11-14 10:39:23 +1100155 new->security = NULL;
David Howellsf1752ee2008-11-14 10:39:17 +1100156#endif
157
David Howellsd84f4f92008-11-14 10:39:23 +1100158 if (security_prepare_creds(new, old, GFP_KERNEL) < 0)
159 goto error;
160 return new;
161
162error:
163 abort_creds(new);
164 return NULL;
165}
166EXPORT_SYMBOL(prepare_creds);
167
168/*
David Howellsa6f76f22008-11-14 10:39:24 +1100169 * Prepare credentials for current to perform an execve()
170 * - The caller must hold current->cred_exec_mutex
171 */
172struct cred *prepare_exec_creds(void)
173{
174 struct thread_group_cred *tgcred = NULL;
175 struct cred *new;
176
177#ifdef CONFIG_KEYS
178 tgcred = kmalloc(sizeof(*tgcred), GFP_KERNEL);
179 if (!tgcred)
180 return NULL;
181#endif
182
183 new = prepare_creds();
184 if (!new) {
185 kfree(tgcred);
186 return new;
187 }
188
189#ifdef CONFIG_KEYS
190 /* newly exec'd tasks don't get a thread keyring */
191 key_put(new->thread_keyring);
192 new->thread_keyring = NULL;
193
194 /* create a new per-thread-group creds for all this set of threads to
195 * share */
196 memcpy(tgcred, new->tgcred, sizeof(struct thread_group_cred));
197
198 atomic_set(&tgcred->usage, 1);
199 spin_lock_init(&tgcred->lock);
200
201 /* inherit the session keyring; new process keyring */
202 key_get(tgcred->session_keyring);
203 tgcred->process_keyring = NULL;
204
205 release_tgcred(new);
206 new->tgcred = tgcred;
207#endif
208
209 return new;
210}
211
212/*
David Howellsd84f4f92008-11-14 10:39:23 +1100213 * prepare new credentials for the usermode helper dispatcher
214 */
215struct cred *prepare_usermodehelper_creds(void)
216{
217#ifdef CONFIG_KEYS
218 struct thread_group_cred *tgcred = NULL;
219#endif
220 struct cred *new;
221
222#ifdef CONFIG_KEYS
223 tgcred = kzalloc(sizeof(*new->tgcred), GFP_ATOMIC);
224 if (!tgcred)
225 return NULL;
226#endif
227
228 new = kmem_cache_alloc(cred_jar, GFP_ATOMIC);
229 if (!new)
230 return NULL;
231
232 memcpy(new, &init_cred, sizeof(struct cred));
233
234 atomic_set(&new->usage, 1);
235 get_group_info(new->group_info);
236 get_uid(new->user);
237
238#ifdef CONFIG_KEYS
239 new->thread_keyring = NULL;
240 new->request_key_auth = NULL;
241 new->jit_keyring = KEY_REQKEY_DEFL_DEFAULT;
242
243 atomic_set(&tgcred->usage, 1);
244 spin_lock_init(&tgcred->lock);
245 new->tgcred = tgcred;
246#endif
247
248#ifdef CONFIG_SECURITY
249 new->security = NULL;
250#endif
251 if (security_prepare_creds(new, &init_cred, GFP_ATOMIC) < 0)
252 goto error;
253
254 BUG_ON(atomic_read(&new->usage) != 1);
255 return new;
256
257error:
258 put_cred(new);
259 return NULL;
260}
261
262/*
263 * Copy credentials for the new process created by fork()
264 *
265 * We share if we can, but under some circumstances we have to generate a new
266 * set.
David Howells3b11a1d2008-11-14 10:39:26 +1100267 *
268 * The new process gets the current process's subjective credentials as its
269 * objective and subjective credentials
David Howellsd84f4f92008-11-14 10:39:23 +1100270 */
271int copy_creds(struct task_struct *p, unsigned long clone_flags)
272{
273#ifdef CONFIG_KEYS
274 struct thread_group_cred *tgcred;
275#endif
276 struct cred *new;
277
278 mutex_init(&p->cred_exec_mutex);
279
280 if (
281#ifdef CONFIG_KEYS
282 !p->cred->thread_keyring &&
283#endif
284 clone_flags & CLONE_THREAD
285 ) {
David Howells3b11a1d2008-11-14 10:39:26 +1100286 p->real_cred = get_cred(p->cred);
David Howellsd84f4f92008-11-14 10:39:23 +1100287 get_cred(p->cred);
288 atomic_inc(&p->cred->user->processes);
289 return 0;
David Howellsf1752ee2008-11-14 10:39:17 +1100290 }
291
David Howellsd84f4f92008-11-14 10:39:23 +1100292 new = prepare_creds();
293 if (!new)
294 return -ENOMEM;
David Howellsf1752ee2008-11-14 10:39:17 +1100295
David Howellsd84f4f92008-11-14 10:39:23 +1100296#ifdef CONFIG_KEYS
297 /* new threads get their own thread keyrings if their parent already
298 * had one */
299 if (new->thread_keyring) {
300 key_put(new->thread_keyring);
301 new->thread_keyring = NULL;
302 if (clone_flags & CLONE_THREAD)
303 install_thread_keyring_to_cred(new);
304 }
David Howellsf1752ee2008-11-14 10:39:17 +1100305
David Howellsd84f4f92008-11-14 10:39:23 +1100306 /* we share the process and session keyrings between all the threads in
307 * a process - this is slightly icky as we violate COW credentials a
308 * bit */
309 if (!(clone_flags & CLONE_THREAD)) {
310 tgcred = kmalloc(sizeof(*tgcred), GFP_KERNEL);
311 if (!tgcred) {
312 put_cred(new);
313 return -ENOMEM;
314 }
315 atomic_set(&tgcred->usage, 1);
316 spin_lock_init(&tgcred->lock);
317 tgcred->process_keyring = NULL;
318 tgcred->session_keyring = key_get(new->tgcred->session_keyring);
319
320 release_tgcred(new);
321 new->tgcred = tgcred;
322 }
323#endif
324
325 atomic_inc(&new->user->processes);
David Howells3b11a1d2008-11-14 10:39:26 +1100326 p->cred = p->real_cred = get_cred(new);
David Howellsf1752ee2008-11-14 10:39:17 +1100327 return 0;
328}
David Howellsd84f4f92008-11-14 10:39:23 +1100329
330/**
331 * commit_creds - Install new credentials upon the current task
332 * @new: The credentials to be assigned
333 *
334 * Install a new set of credentials to the current task, using RCU to replace
David Howells3b11a1d2008-11-14 10:39:26 +1100335 * the old set. Both the objective and the subjective credentials pointers are
336 * updated. This function may not be called if the subjective credentials are
337 * in an overridden state.
David Howellsd84f4f92008-11-14 10:39:23 +1100338 *
339 * This function eats the caller's reference to the new credentials.
340 *
341 * Always returns 0 thus allowing this function to be tail-called at the end
342 * of, say, sys_setgid().
343 */
344int commit_creds(struct cred *new)
345{
346 struct task_struct *task = current;
347 const struct cred *old;
348
David Howells3b11a1d2008-11-14 10:39:26 +1100349 BUG_ON(task->cred != task->real_cred);
350 BUG_ON(atomic_read(&task->real_cred->usage) < 2);
David Howellsd84f4f92008-11-14 10:39:23 +1100351 BUG_ON(atomic_read(&new->usage) < 1);
David Howellsd84f4f92008-11-14 10:39:23 +1100352
David Howells3b11a1d2008-11-14 10:39:26 +1100353 old = task->real_cred;
David Howellsd84f4f92008-11-14 10:39:23 +1100354 security_commit_creds(new, old);
355
David Howells3b11a1d2008-11-14 10:39:26 +1100356 get_cred(new); /* we will require a ref for the subj creds too */
357
David Howellsd84f4f92008-11-14 10:39:23 +1100358 /* dumpability changes */
359 if (old->euid != new->euid ||
360 old->egid != new->egid ||
361 old->fsuid != new->fsuid ||
362 old->fsgid != new->fsgid ||
363 !cap_issubset(new->cap_permitted, old->cap_permitted)) {
364 set_dumpable(task->mm, suid_dumpable);
365 task->pdeath_signal = 0;
366 smp_wmb();
367 }
368
369 /* alter the thread keyring */
370 if (new->fsuid != old->fsuid)
371 key_fsuid_changed(task);
372 if (new->fsgid != old->fsgid)
373 key_fsgid_changed(task);
374
375 /* do it
376 * - What if a process setreuid()'s and this brings the
377 * new uid over his NPROC rlimit? We can check this now
378 * cheaply with the new uid cache, so if it matters
379 * we should be checking for it. -DaveM
380 */
381 if (new->user != old->user)
382 atomic_inc(&new->user->processes);
David Howells3b11a1d2008-11-14 10:39:26 +1100383 rcu_assign_pointer(task->real_cred, new);
David Howellsd84f4f92008-11-14 10:39:23 +1100384 rcu_assign_pointer(task->cred, new);
385 if (new->user != old->user)
386 atomic_dec(&old->user->processes);
387
388 sched_switch_user(task);
389
390 /* send notifications */
391 if (new->uid != old->uid ||
392 new->euid != old->euid ||
393 new->suid != old->suid ||
394 new->fsuid != old->fsuid)
395 proc_id_connector(task, PROC_EVENT_UID);
396
397 if (new->gid != old->gid ||
398 new->egid != old->egid ||
399 new->sgid != old->sgid ||
400 new->fsgid != old->fsgid)
401 proc_id_connector(task, PROC_EVENT_GID);
402
David Howells3b11a1d2008-11-14 10:39:26 +1100403 /* release the old obj and subj refs both */
404 put_cred(old);
David Howellsd84f4f92008-11-14 10:39:23 +1100405 put_cred(old);
406 return 0;
407}
408EXPORT_SYMBOL(commit_creds);
409
410/**
411 * abort_creds - Discard a set of credentials and unlock the current task
412 * @new: The credentials that were going to be applied
413 *
414 * Discard a set of credentials that were under construction and unlock the
415 * current task.
416 */
417void abort_creds(struct cred *new)
418{
419 BUG_ON(atomic_read(&new->usage) < 1);
420 put_cred(new);
421}
422EXPORT_SYMBOL(abort_creds);
423
424/**
David Howells3b11a1d2008-11-14 10:39:26 +1100425 * override_creds - Override the current process's subjective credentials
David Howellsd84f4f92008-11-14 10:39:23 +1100426 * @new: The credentials to be assigned
427 *
David Howells3b11a1d2008-11-14 10:39:26 +1100428 * Install a set of temporary override subjective credentials on the current
429 * process, returning the old set for later reversion.
David Howellsd84f4f92008-11-14 10:39:23 +1100430 */
431const struct cred *override_creds(const struct cred *new)
432{
433 const struct cred *old = current->cred;
434
435 rcu_assign_pointer(current->cred, get_cred(new));
436 return old;
437}
438EXPORT_SYMBOL(override_creds);
439
440/**
David Howells3b11a1d2008-11-14 10:39:26 +1100441 * revert_creds - Revert a temporary subjective credentials override
David Howellsd84f4f92008-11-14 10:39:23 +1100442 * @old: The credentials to be restored
443 *
David Howells3b11a1d2008-11-14 10:39:26 +1100444 * Revert a temporary set of override subjective credentials to an old set,
445 * discarding the override set.
David Howellsd84f4f92008-11-14 10:39:23 +1100446 */
447void revert_creds(const struct cred *old)
448{
449 const struct cred *override = current->cred;
450
451 rcu_assign_pointer(current->cred, old);
452 put_cred(override);
453}
454EXPORT_SYMBOL(revert_creds);
455
456/*
457 * initialise the credentials stuff
458 */
459void __init cred_init(void)
460{
461 /* allocate a slab in which we can store credentials */
462 cred_jar = kmem_cache_create("cred_jar", sizeof(struct cred),
463 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
464}
David Howells3a3b7ce2008-11-14 10:39:28 +1100465
466/**
467 * prepare_kernel_cred - Prepare a set of credentials for a kernel service
468 * @daemon: A userspace daemon to be used as a reference
469 *
470 * Prepare a set of credentials for a kernel service. This can then be used to
471 * override a task's own credentials so that work can be done on behalf of that
472 * task that requires a different subjective context.
473 *
474 * @daemon is used to provide a base for the security record, but can be NULL.
475 * If @daemon is supplied, then the security data will be derived from that;
476 * otherwise they'll be set to 0 and no groups, full capabilities and no keys.
477 *
478 * The caller may change these controls afterwards if desired.
479 *
480 * Returns the new credentials or NULL if out of memory.
481 *
482 * Does not take, and does not return holding current->cred_replace_mutex.
483 */
484struct cred *prepare_kernel_cred(struct task_struct *daemon)
485{
486 const struct cred *old;
487 struct cred *new;
488
489 new = kmem_cache_alloc(cred_jar, GFP_KERNEL);
490 if (!new)
491 return NULL;
492
493 if (daemon)
494 old = get_task_cred(daemon);
495 else
496 old = get_cred(&init_cred);
497
498 get_uid(new->user);
499 get_group_info(new->group_info);
500
501#ifdef CONFIG_KEYS
502 atomic_inc(&init_tgcred.usage);
503 new->tgcred = &init_tgcred;
504 new->request_key_auth = NULL;
505 new->thread_keyring = NULL;
506 new->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING;
507#endif
508
509#ifdef CONFIG_SECURITY
510 new->security = NULL;
511#endif
512 if (security_prepare_creds(new, old, GFP_KERNEL) < 0)
513 goto error;
514
515 atomic_set(&new->usage, 1);
516 put_cred(old);
517 return new;
518
519error:
520 put_cred(new);
521 return NULL;
522}
523EXPORT_SYMBOL(prepare_kernel_cred);
524
525/**
526 * set_security_override - Set the security ID in a set of credentials
527 * @new: The credentials to alter
528 * @secid: The LSM security ID to set
529 *
530 * Set the LSM security ID in a set of credentials so that the subjective
531 * security is overridden when an alternative set of credentials is used.
532 */
533int set_security_override(struct cred *new, u32 secid)
534{
535 return security_kernel_act_as(new, secid);
536}
537EXPORT_SYMBOL(set_security_override);
538
539/**
540 * set_security_override_from_ctx - Set the security ID in a set of credentials
541 * @new: The credentials to alter
542 * @secctx: The LSM security context to generate the security ID from.
543 *
544 * Set the LSM security ID in a set of credentials so that the subjective
545 * security is overridden when an alternative set of credentials is used. The
546 * security ID is specified in string form as a security context to be
547 * interpreted by the LSM.
548 */
549int set_security_override_from_ctx(struct cred *new, const char *secctx)
550{
551 u32 secid;
552 int ret;
553
554 ret = security_secctx_to_secid(secctx, strlen(secctx), &secid);
555 if (ret < 0)
556 return ret;
557
558 return set_security_override(new, secid);
559}
560EXPORT_SYMBOL(set_security_override_from_ctx);
561
562/**
563 * set_create_files_as - Set the LSM file create context in a set of credentials
564 * @new: The credentials to alter
565 * @inode: The inode to take the context from
566 *
567 * Change the LSM file creation context in a set of credentials to be the same
568 * as the object context of the specified inode, so that the new inodes have
569 * the same MAC context as that inode.
570 */
571int set_create_files_as(struct cred *new, struct inode *inode)
572{
573 new->fsuid = inode->i_uid;
574 new->fsgid = inode->i_gid;
575 return security_kernel_create_files_as(new, inode);
576}
577EXPORT_SYMBOL(set_create_files_as);