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