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

Archive Download this file



interactive