Merge tag 'fixes-for-v4.1-rc2' of https://github.com/rjarzmik/linux into fixes
[linux-2.6-block.git] / kernel / cred.c
CommitLineData
d410fa4e 1/* Task credentials management - see Documentation/security/credentials.txt
f1752eec
DH
2 *
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 */
9984de1a 11#include <linux/export.h>
f1752eec 12#include <linux/cred.h>
5a0e3ad6 13#include <linux/slab.h>
f1752eec
DH
14#include <linux/sched.h>
15#include <linux/key.h>
16#include <linux/keyctl.h>
17#include <linux/init_task.h>
18#include <linux/security.h>
40401530 19#include <linux/binfmts.h>
d84f4f99 20#include <linux/cn_proc.h>
d84f4f99 21
e0e81739
DH
22#if 0
23#define kdebug(FMT, ...) \
24 printk("[%-5.5s%5u] "FMT"\n", current->comm, current->pid ,##__VA_ARGS__)
25#else
e0e81739
DH
26#define kdebug(FMT, ...) \
27 no_printk("[%-5.5s%5u] "FMT"\n", current->comm, current->pid ,##__VA_ARGS__)
28#endif
29
d84f4f99 30static struct kmem_cache *cred_jar;
f1752eec 31
2813893f
IM
32/* init to 2 - one for init_task, one to ensure it is never freed */
33struct group_info init_groups = { .usage = ATOMIC_INIT(2) };
34
f1752eec
DH
35/*
36 * The initial credentials for the initial task
37 */
38struct cred init_cred = {
3b11a1de 39 .usage = ATOMIC_INIT(4),
e0e81739
DH
40#ifdef CONFIG_DEBUG_CREDENTIALS
41 .subscribers = ATOMIC_INIT(2),
42 .magic = CRED_MAGIC,
43#endif
078de5f7
EB
44 .uid = GLOBAL_ROOT_UID,
45 .gid = GLOBAL_ROOT_GID,
46 .suid = GLOBAL_ROOT_UID,
47 .sgid = GLOBAL_ROOT_GID,
48 .euid = GLOBAL_ROOT_UID,
49 .egid = GLOBAL_ROOT_GID,
50 .fsuid = GLOBAL_ROOT_UID,
51 .fsgid = GLOBAL_ROOT_GID,
f1752eec 52 .securebits = SECUREBITS_DEFAULT,
a3232d2f 53 .cap_inheritable = CAP_EMPTY_SET,
f1752eec 54 .cap_permitted = CAP_FULL_SET,
a3232d2f
EP
55 .cap_effective = CAP_FULL_SET,
56 .cap_bset = CAP_FULL_SET,
f1752eec 57 .user = INIT_USER,
47a150ed 58 .user_ns = &init_user_ns,
f1752eec
DH
59 .group_info = &init_groups,
60};
61
e0e81739
DH
62static inline void set_cred_subscribers(struct cred *cred, int n)
63{
64#ifdef CONFIG_DEBUG_CREDENTIALS
65 atomic_set(&cred->subscribers, n);
66#endif
67}
68
69static inline int read_cred_subscribers(const struct cred *cred)
70{
71#ifdef CONFIG_DEBUG_CREDENTIALS
72 return atomic_read(&cred->subscribers);
73#else
74 return 0;
75#endif
76}
77
78static inline void alter_cred_subscribers(const struct cred *_cred, int n)
79{
80#ifdef CONFIG_DEBUG_CREDENTIALS
81 struct cred *cred = (struct cred *) _cred;
82
83 atomic_add(n, &cred->subscribers);
84#endif
85}
86
f1752eec
DH
87/*
88 * The RCU callback to actually dispose of a set of credentials
89 */
90static void put_cred_rcu(struct rcu_head *rcu)
91{
92 struct cred *cred = container_of(rcu, struct cred, rcu);
93
e0e81739
DH
94 kdebug("put_cred_rcu(%p)", cred);
95
96#ifdef CONFIG_DEBUG_CREDENTIALS
97 if (cred->magic != CRED_MAGIC_DEAD ||
98 atomic_read(&cred->usage) != 0 ||
99 read_cred_subscribers(cred) != 0)
100 panic("CRED: put_cred_rcu() sees %p with"
101 " mag %x, put %p, usage %d, subscr %d\n",
102 cred, cred->magic, cred->put_addr,
103 atomic_read(&cred->usage),
104 read_cred_subscribers(cred));
105#else
d84f4f99
DH
106 if (atomic_read(&cred->usage) != 0)
107 panic("CRED: put_cred_rcu() sees %p with usage %d\n",
108 cred, atomic_read(&cred->usage));
e0e81739 109#endif
f1752eec 110
d84f4f99 111 security_cred_free(cred);
3a50597d
DH
112 key_put(cred->session_keyring);
113 key_put(cred->process_keyring);
f1752eec
DH
114 key_put(cred->thread_keyring);
115 key_put(cred->request_key_auth);
4a5d6ba1
DH
116 if (cred->group_info)
117 put_group_info(cred->group_info);
f1752eec 118 free_uid(cred->user);
0093ccb6 119 put_user_ns(cred->user_ns);
d84f4f99 120 kmem_cache_free(cred_jar, cred);
f1752eec
DH
121}
122
123/**
124 * __put_cred - Destroy a set of credentials
d84f4f99 125 * @cred: The record to release
f1752eec
DH
126 *
127 * Destroy a set of credentials on which no references remain.
128 */
129void __put_cred(struct cred *cred)
130{
e0e81739
DH
131 kdebug("__put_cred(%p{%d,%d})", cred,
132 atomic_read(&cred->usage),
133 read_cred_subscribers(cred));
134
d84f4f99 135 BUG_ON(atomic_read(&cred->usage) != 0);
e0e81739
DH
136#ifdef CONFIG_DEBUG_CREDENTIALS
137 BUG_ON(read_cred_subscribers(cred) != 0);
138 cred->magic = CRED_MAGIC_DEAD;
139 cred->put_addr = __builtin_return_address(0);
140#endif
141 BUG_ON(cred == current->cred);
142 BUG_ON(cred == current->real_cred);
d84f4f99 143
f1752eec
DH
144 call_rcu(&cred->rcu, put_cred_rcu);
145}
146EXPORT_SYMBOL(__put_cred);
147
e0e81739
DH
148/*
149 * Clean up a task's credentials when it exits
150 */
151void exit_creds(struct task_struct *tsk)
152{
153 struct cred *cred;
154
155 kdebug("exit_creds(%u,%p,%p,{%d,%d})", tsk->pid, tsk->real_cred, tsk->cred,
156 atomic_read(&tsk->cred->usage),
157 read_cred_subscribers(tsk->cred));
158
159 cred = (struct cred *) tsk->real_cred;
160 tsk->real_cred = NULL;
161 validate_creds(cred);
162 alter_cred_subscribers(cred, -1);
163 put_cred(cred);
164
165 cred = (struct cred *) tsk->cred;
166 tsk->cred = NULL;
167 validate_creds(cred);
168 alter_cred_subscribers(cred, -1);
169 put_cred(cred);
ee18d64c
DH
170}
171
de09a977
DH
172/**
173 * get_task_cred - Get another task's objective credentials
174 * @task: The task to query
175 *
176 * Get the objective credentials of a task, pinning them so that they can't go
177 * away. Accessing a task's credentials directly is not permitted.
178 *
179 * The caller must also make sure task doesn't get deleted, either by holding a
180 * ref on task or by holding tasklist_lock to prevent it from being unlinked.
181 */
182const struct cred *get_task_cred(struct task_struct *task)
183{
184 const struct cred *cred;
185
186 rcu_read_lock();
187
188 do {
189 cred = __task_cred((task));
190 BUG_ON(!cred);
191 } while (!atomic_inc_not_zero(&((struct cred *)cred)->usage));
192
193 rcu_read_unlock();
194 return cred;
195}
196
ee18d64c
DH
197/*
198 * Allocate blank credentials, such that the credentials can be filled in at a
199 * later date without risk of ENOMEM.
200 */
201struct cred *cred_alloc_blank(void)
202{
203 struct cred *new;
204
205 new = kmem_cache_zalloc(cred_jar, GFP_KERNEL);
206 if (!new)
207 return NULL;
208
ee18d64c 209 atomic_set(&new->usage, 1);
2edeaa34
TH
210#ifdef CONFIG_DEBUG_CREDENTIALS
211 new->magic = CRED_MAGIC;
212#endif
ee18d64c
DH
213
214 if (security_cred_alloc_blank(new, GFP_KERNEL) < 0)
215 goto error;
216
ee18d64c
DH
217 return new;
218
219error:
220 abort_creds(new);
221 return NULL;
e0e81739
DH
222}
223
d84f4f99
DH
224/**
225 * prepare_creds - Prepare a new set of credentials for modification
226 *
227 * Prepare a new set of task credentials for modification. A task's creds
228 * shouldn't generally be modified directly, therefore this function is used to
229 * prepare a new copy, which the caller then modifies and then commits by
230 * calling commit_creds().
231 *
3b11a1de
DH
232 * Preparation involves making a copy of the objective creds for modification.
233 *
d84f4f99
DH
234 * Returns a pointer to the new creds-to-be if successful, NULL otherwise.
235 *
236 * Call commit_creds() or abort_creds() to clean up.
237 */
238struct cred *prepare_creds(void)
239{
240 struct task_struct *task = current;
241 const struct cred *old;
242 struct cred *new;
243
e0e81739 244 validate_process_creds();
d84f4f99
DH
245
246 new = kmem_cache_alloc(cred_jar, GFP_KERNEL);
247 if (!new)
248 return NULL;
249
e0e81739
DH
250 kdebug("prepare_creds() alloc %p", new);
251
d84f4f99
DH
252 old = task->cred;
253 memcpy(new, old, sizeof(struct cred));
254
255 atomic_set(&new->usage, 1);
e0e81739 256 set_cred_subscribers(new, 0);
d84f4f99
DH
257 get_group_info(new->group_info);
258 get_uid(new->user);
0093ccb6 259 get_user_ns(new->user_ns);
d84f4f99
DH
260
261#ifdef CONFIG_KEYS
3a50597d
DH
262 key_get(new->session_keyring);
263 key_get(new->process_keyring);
d84f4f99
DH
264 key_get(new->thread_keyring);
265 key_get(new->request_key_auth);
d84f4f99
DH
266#endif
267
268#ifdef CONFIG_SECURITY
269 new->security = NULL;
270#endif
271
272 if (security_prepare_creds(new, old, GFP_KERNEL) < 0)
273 goto error;
e0e81739 274 validate_creds(new);
d84f4f99
DH
275 return new;
276
277error:
278 abort_creds(new);
279 return NULL;
280}
281EXPORT_SYMBOL(prepare_creds);
282
a6f76f23
DH
283/*
284 * Prepare credentials for current to perform an execve()
9b1bf12d 285 * - The caller must hold ->cred_guard_mutex
a6f76f23
DH
286 */
287struct cred *prepare_exec_creds(void)
288{
a6f76f23
DH
289 struct cred *new;
290
a6f76f23 291 new = prepare_creds();
3a50597d 292 if (!new)
a6f76f23 293 return new;
a6f76f23
DH
294
295#ifdef CONFIG_KEYS
296 /* newly exec'd tasks don't get a thread keyring */
297 key_put(new->thread_keyring);
298 new->thread_keyring = NULL;
299
a6f76f23 300 /* inherit the session keyring; new process keyring */
3a50597d
DH
301 key_put(new->process_keyring);
302 new->process_keyring = NULL;
a6f76f23
DH
303#endif
304
305 return new;
306}
307
f1752eec
DH
308/*
309 * Copy credentials for the new process created by fork()
d84f4f99
DH
310 *
311 * We share if we can, but under some circumstances we have to generate a new
312 * set.
3b11a1de
DH
313 *
314 * The new process gets the current process's subjective credentials as its
315 * objective and subjective credentials
f1752eec
DH
316 */
317int copy_creds(struct task_struct *p, unsigned long clone_flags)
318{
d84f4f99 319 struct cred *new;
18b6e041 320 int ret;
d84f4f99 321
d84f4f99
DH
322 if (
323#ifdef CONFIG_KEYS
324 !p->cred->thread_keyring &&
325#endif
326 clone_flags & CLONE_THREAD
327 ) {
3b11a1de 328 p->real_cred = get_cred(p->cred);
d84f4f99 329 get_cred(p->cred);
e0e81739
DH
330 alter_cred_subscribers(p->cred, 2);
331 kdebug("share_creds(%p{%d,%d})",
332 p->cred, atomic_read(&p->cred->usage),
333 read_cred_subscribers(p->cred));
d84f4f99
DH
334 atomic_inc(&p->cred->user->processes);
335 return 0;
336 }
337
338 new = prepare_creds();
339 if (!new)
f1752eec
DH
340 return -ENOMEM;
341
18b6e041
SH
342 if (clone_flags & CLONE_NEWUSER) {
343 ret = create_user_ns(new);
344 if (ret < 0)
345 goto error_put;
346 }
347
bb952bb9 348#ifdef CONFIG_KEYS
d84f4f99
DH
349 /* new threads get their own thread keyrings if their parent already
350 * had one */
351 if (new->thread_keyring) {
352 key_put(new->thread_keyring);
353 new->thread_keyring = NULL;
354 if (clone_flags & CLONE_THREAD)
355 install_thread_keyring_to_cred(new);
356 }
357
3a50597d
DH
358 /* The process keyring is only shared between the threads in a process;
359 * anything outside of those threads doesn't inherit.
360 */
d84f4f99 361 if (!(clone_flags & CLONE_THREAD)) {
3a50597d
DH
362 key_put(new->process_keyring);
363 new->process_keyring = NULL;
bb952bb9
DH
364 }
365#endif
366
d84f4f99 367 atomic_inc(&new->user->processes);
3b11a1de 368 p->cred = p->real_cred = get_cred(new);
e0e81739
DH
369 alter_cred_subscribers(new, 2);
370 validate_creds(new);
d84f4f99 371 return 0;
18b6e041
SH
372
373error_put:
374 put_cred(new);
375 return ret;
d84f4f99 376}
f1752eec 377
aa6d054e
EB
378static bool cred_cap_issubset(const struct cred *set, const struct cred *subset)
379{
380 const struct user_namespace *set_ns = set->user_ns;
381 const struct user_namespace *subset_ns = subset->user_ns;
382
383 /* If the two credentials are in the same user namespace see if
384 * the capabilities of subset are a subset of set.
385 */
386 if (set_ns == subset_ns)
387 return cap_issubset(subset->cap_permitted, set->cap_permitted);
388
389 /* The credentials are in a different user namespaces
390 * therefore one is a subset of the other only if a set is an
391 * ancestor of subset and set->euid is owner of subset or one
392 * of subsets ancestors.
393 */
394 for (;subset_ns != &init_user_ns; subset_ns = subset_ns->parent) {
395 if ((set_ns == subset_ns->parent) &&
396 uid_eq(subset_ns->owner, set->euid))
397 return true;
398 }
399
400 return false;
401}
402
d84f4f99
DH
403/**
404 * commit_creds - Install new credentials upon the current task
405 * @new: The credentials to be assigned
406 *
407 * Install a new set of credentials to the current task, using RCU to replace
3b11a1de
DH
408 * the old set. Both the objective and the subjective credentials pointers are
409 * updated. This function may not be called if the subjective credentials are
410 * in an overridden state.
d84f4f99
DH
411 *
412 * This function eats the caller's reference to the new credentials.
413 *
414 * Always returns 0 thus allowing this function to be tail-called at the end
415 * of, say, sys_setgid().
416 */
417int commit_creds(struct cred *new)
418{
419 struct task_struct *task = current;
e0e81739 420 const struct cred *old = task->real_cred;
d84f4f99 421
e0e81739
DH
422 kdebug("commit_creds(%p{%d,%d})", new,
423 atomic_read(&new->usage),
424 read_cred_subscribers(new));
425
426 BUG_ON(task->cred != old);
427#ifdef CONFIG_DEBUG_CREDENTIALS
428 BUG_ON(read_cred_subscribers(old) < 2);
429 validate_creds(old);
430 validate_creds(new);
431#endif
d84f4f99 432 BUG_ON(atomic_read(&new->usage) < 1);
d84f4f99 433
3b11a1de
DH
434 get_cred(new); /* we will require a ref for the subj creds too */
435
d84f4f99 436 /* dumpability changes */
078de5f7
EB
437 if (!uid_eq(old->euid, new->euid) ||
438 !gid_eq(old->egid, new->egid) ||
439 !uid_eq(old->fsuid, new->fsuid) ||
440 !gid_eq(old->fsgid, new->fsgid) ||
aa6d054e 441 !cred_cap_issubset(old, new)) {
b9456371
DH
442 if (task->mm)
443 set_dumpable(task->mm, suid_dumpable);
d84f4f99
DH
444 task->pdeath_signal = 0;
445 smp_wmb();
f1752eec
DH
446 }
447
d84f4f99 448 /* alter the thread keyring */
078de5f7 449 if (!uid_eq(new->fsuid, old->fsuid))
d84f4f99 450 key_fsuid_changed(task);
078de5f7 451 if (!gid_eq(new->fsgid, old->fsgid))
d84f4f99
DH
452 key_fsgid_changed(task);
453
454 /* do it
72fa5997
VK
455 * RLIMIT_NPROC limits on user->processes have already been checked
456 * in set_user().
d84f4f99 457 */
e0e81739 458 alter_cred_subscribers(new, 2);
d84f4f99
DH
459 if (new->user != old->user)
460 atomic_inc(&new->user->processes);
3b11a1de 461 rcu_assign_pointer(task->real_cred, new);
d84f4f99
DH
462 rcu_assign_pointer(task->cred, new);
463 if (new->user != old->user)
464 atomic_dec(&old->user->processes);
e0e81739 465 alter_cred_subscribers(old, -2);
d84f4f99 466
d84f4f99 467 /* send notifications */
078de5f7
EB
468 if (!uid_eq(new->uid, old->uid) ||
469 !uid_eq(new->euid, old->euid) ||
470 !uid_eq(new->suid, old->suid) ||
471 !uid_eq(new->fsuid, old->fsuid))
d84f4f99 472 proc_id_connector(task, PROC_EVENT_UID);
f1752eec 473
078de5f7
EB
474 if (!gid_eq(new->gid, old->gid) ||
475 !gid_eq(new->egid, old->egid) ||
476 !gid_eq(new->sgid, old->sgid) ||
477 !gid_eq(new->fsgid, old->fsgid))
d84f4f99 478 proc_id_connector(task, PROC_EVENT_GID);
f1752eec 479
3b11a1de
DH
480 /* release the old obj and subj refs both */
481 put_cred(old);
d84f4f99 482 put_cred(old);
f1752eec
DH
483 return 0;
484}
d84f4f99
DH
485EXPORT_SYMBOL(commit_creds);
486
487/**
488 * abort_creds - Discard a set of credentials and unlock the current task
489 * @new: The credentials that were going to be applied
490 *
491 * Discard a set of credentials that were under construction and unlock the
492 * current task.
493 */
494void abort_creds(struct cred *new)
495{
e0e81739
DH
496 kdebug("abort_creds(%p{%d,%d})", new,
497 atomic_read(&new->usage),
498 read_cred_subscribers(new));
499
500#ifdef CONFIG_DEBUG_CREDENTIALS
501 BUG_ON(read_cred_subscribers(new) != 0);
502#endif
d84f4f99
DH
503 BUG_ON(atomic_read(&new->usage) < 1);
504 put_cred(new);
505}
506EXPORT_SYMBOL(abort_creds);
507
508/**
3b11a1de 509 * override_creds - Override the current process's subjective credentials
d84f4f99
DH
510 * @new: The credentials to be assigned
511 *
3b11a1de
DH
512 * Install a set of temporary override subjective credentials on the current
513 * process, returning the old set for later reversion.
d84f4f99
DH
514 */
515const struct cred *override_creds(const struct cred *new)
516{
517 const struct cred *old = current->cred;
518
e0e81739
DH
519 kdebug("override_creds(%p{%d,%d})", new,
520 atomic_read(&new->usage),
521 read_cred_subscribers(new));
522
523 validate_creds(old);
524 validate_creds(new);
525 get_cred(new);
526 alter_cred_subscribers(new, 1);
527 rcu_assign_pointer(current->cred, new);
528 alter_cred_subscribers(old, -1);
529
530 kdebug("override_creds() = %p{%d,%d}", old,
531 atomic_read(&old->usage),
532 read_cred_subscribers(old));
d84f4f99
DH
533 return old;
534}
535EXPORT_SYMBOL(override_creds);
536
537/**
3b11a1de 538 * revert_creds - Revert a temporary subjective credentials override
d84f4f99
DH
539 * @old: The credentials to be restored
540 *
3b11a1de
DH
541 * Revert a temporary set of override subjective credentials to an old set,
542 * discarding the override set.
d84f4f99
DH
543 */
544void revert_creds(const struct cred *old)
545{
546 const struct cred *override = current->cred;
547
e0e81739
DH
548 kdebug("revert_creds(%p{%d,%d})", old,
549 atomic_read(&old->usage),
550 read_cred_subscribers(old));
551
552 validate_creds(old);
553 validate_creds(override);
554 alter_cred_subscribers(old, 1);
d84f4f99 555 rcu_assign_pointer(current->cred, old);
e0e81739 556 alter_cred_subscribers(override, -1);
d84f4f99
DH
557 put_cred(override);
558}
559EXPORT_SYMBOL(revert_creds);
560
561/*
562 * initialise the credentials stuff
563 */
564void __init cred_init(void)
565{
566 /* allocate a slab in which we can store credentials */
567 cred_jar = kmem_cache_create("cred_jar", sizeof(struct cred),
568 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
569}
3a3b7ce9
DH
570
571/**
572 * prepare_kernel_cred - Prepare a set of credentials for a kernel service
573 * @daemon: A userspace daemon to be used as a reference
574 *
575 * Prepare a set of credentials for a kernel service. This can then be used to
576 * override a task's own credentials so that work can be done on behalf of that
577 * task that requires a different subjective context.
578 *
579 * @daemon is used to provide a base for the security record, but can be NULL.
580 * If @daemon is supplied, then the security data will be derived from that;
581 * otherwise they'll be set to 0 and no groups, full capabilities and no keys.
582 *
583 * The caller may change these controls afterwards if desired.
584 *
585 * Returns the new credentials or NULL if out of memory.
586 *
587 * Does not take, and does not return holding current->cred_replace_mutex.
588 */
589struct cred *prepare_kernel_cred(struct task_struct *daemon)
590{
591 const struct cred *old;
592 struct cred *new;
593
594 new = kmem_cache_alloc(cred_jar, GFP_KERNEL);
595 if (!new)
596 return NULL;
597
e0e81739
DH
598 kdebug("prepare_kernel_cred() alloc %p", new);
599
3a3b7ce9
DH
600 if (daemon)
601 old = get_task_cred(daemon);
602 else
603 old = get_cred(&init_cred);
604
e0e81739
DH
605 validate_creds(old);
606
43529c97 607 *new = *old;
fb2b2a1d
TH
608 atomic_set(&new->usage, 1);
609 set_cred_subscribers(new, 0);
3a3b7ce9 610 get_uid(new->user);
0093ccb6 611 get_user_ns(new->user_ns);
3a3b7ce9
DH
612 get_group_info(new->group_info);
613
614#ifdef CONFIG_KEYS
3a50597d
DH
615 new->session_keyring = NULL;
616 new->process_keyring = NULL;
3a3b7ce9 617 new->thread_keyring = NULL;
3a50597d 618 new->request_key_auth = NULL;
3a3b7ce9
DH
619 new->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING;
620#endif
621
622#ifdef CONFIG_SECURITY
623 new->security = NULL;
624#endif
625 if (security_prepare_creds(new, old, GFP_KERNEL) < 0)
626 goto error;
627
3a3b7ce9 628 put_cred(old);
e0e81739 629 validate_creds(new);
3a3b7ce9
DH
630 return new;
631
632error:
633 put_cred(new);
0de33681 634 put_cred(old);
3a3b7ce9
DH
635 return NULL;
636}
637EXPORT_SYMBOL(prepare_kernel_cred);
638
639/**
640 * set_security_override - Set the security ID in a set of credentials
641 * @new: The credentials to alter
642 * @secid: The LSM security ID to set
643 *
644 * Set the LSM security ID in a set of credentials so that the subjective
645 * security is overridden when an alternative set of credentials is used.
646 */
647int set_security_override(struct cred *new, u32 secid)
648{
649 return security_kernel_act_as(new, secid);
650}
651EXPORT_SYMBOL(set_security_override);
652
653/**
654 * set_security_override_from_ctx - Set the security ID in a set of credentials
655 * @new: The credentials to alter
656 * @secctx: The LSM security context to generate the security ID from.
657 *
658 * Set the LSM security ID in a set of credentials so that the subjective
659 * security is overridden when an alternative set of credentials is used. The
660 * security ID is specified in string form as a security context to be
661 * interpreted by the LSM.
662 */
663int set_security_override_from_ctx(struct cred *new, const char *secctx)
664{
665 u32 secid;
666 int ret;
667
668 ret = security_secctx_to_secid(secctx, strlen(secctx), &secid);
669 if (ret < 0)
670 return ret;
671
672 return set_security_override(new, secid);
673}
674EXPORT_SYMBOL(set_security_override_from_ctx);
675
676/**
677 * set_create_files_as - Set the LSM file create context in a set of credentials
678 * @new: The credentials to alter
679 * @inode: The inode to take the context from
680 *
681 * Change the LSM file creation context in a set of credentials to be the same
682 * as the object context of the specified inode, so that the new inodes have
683 * the same MAC context as that inode.
684 */
685int set_create_files_as(struct cred *new, struct inode *inode)
686{
687 new->fsuid = inode->i_uid;
688 new->fsgid = inode->i_gid;
689 return security_kernel_create_files_as(new, inode);
690}
691EXPORT_SYMBOL(set_create_files_as);
e0e81739
DH
692
693#ifdef CONFIG_DEBUG_CREDENTIALS
694
74908a00
AM
695bool creds_are_invalid(const struct cred *cred)
696{
697 if (cred->magic != CRED_MAGIC)
698 return true;
74908a00 699#ifdef CONFIG_SECURITY_SELINUX
2edeaa34
TH
700 /*
701 * cred->security == NULL if security_cred_alloc_blank() or
702 * security_prepare_creds() returned an error.
703 */
704 if (selinux_is_enabled() && cred->security) {
74908a00
AM
705 if ((unsigned long) cred->security < PAGE_SIZE)
706 return true;
707 if ((*(u32 *)cred->security & 0xffffff00) ==
708 (POISON_FREE << 24 | POISON_FREE << 16 | POISON_FREE << 8))
709 return true;
710 }
711#endif
712 return false;
713}
764db03f 714EXPORT_SYMBOL(creds_are_invalid);
74908a00 715
e0e81739
DH
716/*
717 * dump invalid credentials
718 */
719static void dump_invalid_creds(const struct cred *cred, const char *label,
720 const struct task_struct *tsk)
721{
722 printk(KERN_ERR "CRED: %s credentials: %p %s%s%s\n",
723 label, cred,
724 cred == &init_cred ? "[init]" : "",
725 cred == tsk->real_cred ? "[real]" : "",
726 cred == tsk->cred ? "[eff]" : "");
727 printk(KERN_ERR "CRED: ->magic=%x, put_addr=%p\n",
728 cred->magic, cred->put_addr);
729 printk(KERN_ERR "CRED: ->usage=%d, subscr=%d\n",
730 atomic_read(&cred->usage),
731 read_cred_subscribers(cred));
732 printk(KERN_ERR "CRED: ->*uid = { %d,%d,%d,%d }\n",
c9235f48
EB
733 from_kuid_munged(&init_user_ns, cred->uid),
734 from_kuid_munged(&init_user_ns, cred->euid),
735 from_kuid_munged(&init_user_ns, cred->suid),
736 from_kuid_munged(&init_user_ns, cred->fsuid));
e0e81739 737 printk(KERN_ERR "CRED: ->*gid = { %d,%d,%d,%d }\n",
c9235f48
EB
738 from_kgid_munged(&init_user_ns, cred->gid),
739 from_kgid_munged(&init_user_ns, cred->egid),
740 from_kgid_munged(&init_user_ns, cred->sgid),
741 from_kgid_munged(&init_user_ns, cred->fsgid));
e0e81739
DH
742#ifdef CONFIG_SECURITY
743 printk(KERN_ERR "CRED: ->security is %p\n", cred->security);
744 if ((unsigned long) cred->security >= PAGE_SIZE &&
745 (((unsigned long) cred->security & 0xffffff00) !=
746 (POISON_FREE << 24 | POISON_FREE << 16 | POISON_FREE << 8)))
747 printk(KERN_ERR "CRED: ->security {%x, %x}\n",
748 ((u32*)cred->security)[0],
749 ((u32*)cred->security)[1]);
750#endif
751}
752
753/*
754 * report use of invalid credentials
755 */
756void __invalid_creds(const struct cred *cred, const char *file, unsigned line)
757{
758 printk(KERN_ERR "CRED: Invalid credentials\n");
759 printk(KERN_ERR "CRED: At %s:%u\n", file, line);
760 dump_invalid_creds(cred, "Specified", current);
761 BUG();
762}
763EXPORT_SYMBOL(__invalid_creds);
764
765/*
766 * check the credentials on a process
767 */
768void __validate_process_creds(struct task_struct *tsk,
769 const char *file, unsigned line)
770{
771 if (tsk->cred == tsk->real_cred) {
772 if (unlikely(read_cred_subscribers(tsk->cred) < 2 ||
773 creds_are_invalid(tsk->cred)))
774 goto invalid_creds;
775 } else {
776 if (unlikely(read_cred_subscribers(tsk->real_cred) < 1 ||
777 read_cred_subscribers(tsk->cred) < 1 ||
778 creds_are_invalid(tsk->real_cred) ||
779 creds_are_invalid(tsk->cred)))
780 goto invalid_creds;
781 }
782 return;
783
784invalid_creds:
785 printk(KERN_ERR "CRED: Invalid process credentials\n");
786 printk(KERN_ERR "CRED: At %s:%u\n", file, line);
787
788 dump_invalid_creds(tsk->real_cred, "Real", tsk);
789 if (tsk->cred != tsk->real_cred)
790 dump_invalid_creds(tsk->cred, "Effective", tsk);
791 else
792 printk(KERN_ERR "CRED: Effective creds == Real creds\n");
793 BUG();
794}
795EXPORT_SYMBOL(__validate_process_creds);
796
797/*
798 * check creds for do_exit()
799 */
800void validate_creds_for_do_exit(struct task_struct *tsk)
801{
802 kdebug("validate_creds_for_do_exit(%p,%p{%d,%d})",
803 tsk->real_cred, tsk->cred,
804 atomic_read(&tsk->cred->usage),
805 read_cred_subscribers(tsk->cred));
806
807 __validate_process_creds(tsk, __FILE__, __LINE__);
808}
809
810#endif /* CONFIG_DEBUG_CREDENTIALS */