Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /* Task credentials management - see Documentation/security/credentials.rst
0003  *
0004  * Copyright (C) 2008 Red Hat, Inc. All Rights Reserved.
0005  * Written by David Howells (dhowells@redhat.com)
0006  */
0007 #include <linux/export.h>
0008 #include <linux/cred.h>
0009 #include <linux/slab.h>
0010 #include <linux/sched.h>
0011 #include <linux/sched/coredump.h>
0012 #include <linux/key.h>
0013 #include <linux/keyctl.h>
0014 #include <linux/init_task.h>
0015 #include <linux/security.h>
0016 #include <linux/binfmts.h>
0017 #include <linux/cn_proc.h>
0018 #include <linux/uidgid.h>
0019 
0020 #if 0
0021 #define kdebug(FMT, ...)                        \
0022     printk("[%-5.5s%5u] " FMT "\n",                 \
0023            current->comm, current->pid, ##__VA_ARGS__)
0024 #else
0025 #define kdebug(FMT, ...)                        \
0026 do {                                    \
0027     if (0)                              \
0028         no_printk("[%-5.5s%5u] " FMT "\n",          \
0029               current->comm, current->pid, ##__VA_ARGS__);  \
0030 } while (0)
0031 #endif
0032 
0033 static struct kmem_cache *cred_jar;
0034 
0035 /* init to 2 - one for init_task, one to ensure it is never freed */
0036 static struct group_info init_groups = { .usage = ATOMIC_INIT(2) };
0037 
0038 /*
0039  * The initial credentials for the initial task
0040  */
0041 struct cred init_cred = {
0042     .usage          = ATOMIC_INIT(4),
0043 #ifdef CONFIG_DEBUG_CREDENTIALS
0044     .subscribers        = ATOMIC_INIT(2),
0045     .magic          = CRED_MAGIC,
0046 #endif
0047     .uid            = GLOBAL_ROOT_UID,
0048     .gid            = GLOBAL_ROOT_GID,
0049     .suid           = GLOBAL_ROOT_UID,
0050     .sgid           = GLOBAL_ROOT_GID,
0051     .euid           = GLOBAL_ROOT_UID,
0052     .egid           = GLOBAL_ROOT_GID,
0053     .fsuid          = GLOBAL_ROOT_UID,
0054     .fsgid          = GLOBAL_ROOT_GID,
0055     .securebits     = SECUREBITS_DEFAULT,
0056     .cap_inheritable    = CAP_EMPTY_SET,
0057     .cap_permitted      = CAP_FULL_SET,
0058     .cap_effective      = CAP_FULL_SET,
0059     .cap_bset       = CAP_FULL_SET,
0060     .user           = INIT_USER,
0061     .user_ns        = &init_user_ns,
0062     .group_info     = &init_groups,
0063     .ucounts        = &init_ucounts,
0064 };
0065 
0066 static inline void set_cred_subscribers(struct cred *cred, int n)
0067 {
0068 #ifdef CONFIG_DEBUG_CREDENTIALS
0069     atomic_set(&cred->subscribers, n);
0070 #endif
0071 }
0072 
0073 static inline int read_cred_subscribers(const struct cred *cred)
0074 {
0075 #ifdef CONFIG_DEBUG_CREDENTIALS
0076     return atomic_read(&cred->subscribers);
0077 #else
0078     return 0;
0079 #endif
0080 }
0081 
0082 static inline void alter_cred_subscribers(const struct cred *_cred, int n)
0083 {
0084 #ifdef CONFIG_DEBUG_CREDENTIALS
0085     struct cred *cred = (struct cred *) _cred;
0086 
0087     atomic_add(n, &cred->subscribers);
0088 #endif
0089 }
0090 
0091 /*
0092  * The RCU callback to actually dispose of a set of credentials
0093  */
0094 static void put_cred_rcu(struct rcu_head *rcu)
0095 {
0096     struct cred *cred = container_of(rcu, struct cred, rcu);
0097 
0098     kdebug("put_cred_rcu(%p)", cred);
0099 
0100 #ifdef CONFIG_DEBUG_CREDENTIALS
0101     if (cred->magic != CRED_MAGIC_DEAD ||
0102         atomic_read(&cred->usage) != 0 ||
0103         read_cred_subscribers(cred) != 0)
0104         panic("CRED: put_cred_rcu() sees %p with"
0105               " mag %x, put %p, usage %d, subscr %d\n",
0106               cred, cred->magic, cred->put_addr,
0107               atomic_read(&cred->usage),
0108               read_cred_subscribers(cred));
0109 #else
0110     if (atomic_read(&cred->usage) != 0)
0111         panic("CRED: put_cred_rcu() sees %p with usage %d\n",
0112               cred, atomic_read(&cred->usage));
0113 #endif
0114 
0115     security_cred_free(cred);
0116     key_put(cred->session_keyring);
0117     key_put(cred->process_keyring);
0118     key_put(cred->thread_keyring);
0119     key_put(cred->request_key_auth);
0120     if (cred->group_info)
0121         put_group_info(cred->group_info);
0122     free_uid(cred->user);
0123     if (cred->ucounts)
0124         put_ucounts(cred->ucounts);
0125     put_user_ns(cred->user_ns);
0126     kmem_cache_free(cred_jar, cred);
0127 }
0128 
0129 /**
0130  * __put_cred - Destroy a set of credentials
0131  * @cred: The record to release
0132  *
0133  * Destroy a set of credentials on which no references remain.
0134  */
0135 void __put_cred(struct cred *cred)
0136 {
0137     kdebug("__put_cred(%p{%d,%d})", cred,
0138            atomic_read(&cred->usage),
0139            read_cred_subscribers(cred));
0140 
0141     BUG_ON(atomic_read(&cred->usage) != 0);
0142 #ifdef CONFIG_DEBUG_CREDENTIALS
0143     BUG_ON(read_cred_subscribers(cred) != 0);
0144     cred->magic = CRED_MAGIC_DEAD;
0145     cred->put_addr = __builtin_return_address(0);
0146 #endif
0147     BUG_ON(cred == current->cred);
0148     BUG_ON(cred == current->real_cred);
0149 
0150     if (cred->non_rcu)
0151         put_cred_rcu(&cred->rcu);
0152     else
0153         call_rcu(&cred->rcu, put_cred_rcu);
0154 }
0155 EXPORT_SYMBOL(__put_cred);
0156 
0157 /*
0158  * Clean up a task's credentials when it exits
0159  */
0160 void exit_creds(struct task_struct *tsk)
0161 {
0162     struct cred *cred;
0163 
0164     kdebug("exit_creds(%u,%p,%p,{%d,%d})", tsk->pid, tsk->real_cred, tsk->cred,
0165            atomic_read(&tsk->cred->usage),
0166            read_cred_subscribers(tsk->cred));
0167 
0168     cred = (struct cred *) tsk->real_cred;
0169     tsk->real_cred = NULL;
0170     validate_creds(cred);
0171     alter_cred_subscribers(cred, -1);
0172     put_cred(cred);
0173 
0174     cred = (struct cred *) tsk->cred;
0175     tsk->cred = NULL;
0176     validate_creds(cred);
0177     alter_cred_subscribers(cred, -1);
0178     put_cred(cred);
0179 
0180 #ifdef CONFIG_KEYS_REQUEST_CACHE
0181     key_put(tsk->cached_requested_key);
0182     tsk->cached_requested_key = NULL;
0183 #endif
0184 }
0185 
0186 /**
0187  * get_task_cred - Get another task's objective credentials
0188  * @task: The task to query
0189  *
0190  * Get the objective credentials of a task, pinning them so that they can't go
0191  * away.  Accessing a task's credentials directly is not permitted.
0192  *
0193  * The caller must also make sure task doesn't get deleted, either by holding a
0194  * ref on task or by holding tasklist_lock to prevent it from being unlinked.
0195  */
0196 const struct cred *get_task_cred(struct task_struct *task)
0197 {
0198     const struct cred *cred;
0199 
0200     rcu_read_lock();
0201 
0202     do {
0203         cred = __task_cred((task));
0204         BUG_ON(!cred);
0205     } while (!get_cred_rcu(cred));
0206 
0207     rcu_read_unlock();
0208     return cred;
0209 }
0210 EXPORT_SYMBOL(get_task_cred);
0211 
0212 /*
0213  * Allocate blank credentials, such that the credentials can be filled in at a
0214  * later date without risk of ENOMEM.
0215  */
0216 struct cred *cred_alloc_blank(void)
0217 {
0218     struct cred *new;
0219 
0220     new = kmem_cache_zalloc(cred_jar, GFP_KERNEL);
0221     if (!new)
0222         return NULL;
0223 
0224     atomic_set(&new->usage, 1);
0225 #ifdef CONFIG_DEBUG_CREDENTIALS
0226     new->magic = CRED_MAGIC;
0227 #endif
0228     if (security_cred_alloc_blank(new, GFP_KERNEL_ACCOUNT) < 0)
0229         goto error;
0230 
0231     return new;
0232 
0233 error:
0234     abort_creds(new);
0235     return NULL;
0236 }
0237 
0238 /**
0239  * prepare_creds - Prepare a new set of credentials for modification
0240  *
0241  * Prepare a new set of task credentials for modification.  A task's creds
0242  * shouldn't generally be modified directly, therefore this function is used to
0243  * prepare a new copy, which the caller then modifies and then commits by
0244  * calling commit_creds().
0245  *
0246  * Preparation involves making a copy of the objective creds for modification.
0247  *
0248  * Returns a pointer to the new creds-to-be if successful, NULL otherwise.
0249  *
0250  * Call commit_creds() or abort_creds() to clean up.
0251  */
0252 struct cred *prepare_creds(void)
0253 {
0254     struct task_struct *task = current;
0255     const struct cred *old;
0256     struct cred *new;
0257 
0258     validate_process_creds();
0259 
0260     new = kmem_cache_alloc(cred_jar, GFP_KERNEL);
0261     if (!new)
0262         return NULL;
0263 
0264     kdebug("prepare_creds() alloc %p", new);
0265 
0266     old = task->cred;
0267     memcpy(new, old, sizeof(struct cred));
0268 
0269     new->non_rcu = 0;
0270     atomic_set(&new->usage, 1);
0271     set_cred_subscribers(new, 0);
0272     get_group_info(new->group_info);
0273     get_uid(new->user);
0274     get_user_ns(new->user_ns);
0275 
0276 #ifdef CONFIG_KEYS
0277     key_get(new->session_keyring);
0278     key_get(new->process_keyring);
0279     key_get(new->thread_keyring);
0280     key_get(new->request_key_auth);
0281 #endif
0282 
0283 #ifdef CONFIG_SECURITY
0284     new->security = NULL;
0285 #endif
0286 
0287     new->ucounts = get_ucounts(new->ucounts);
0288     if (!new->ucounts)
0289         goto error;
0290 
0291     if (security_prepare_creds(new, old, GFP_KERNEL_ACCOUNT) < 0)
0292         goto error;
0293 
0294     validate_creds(new);
0295     return new;
0296 
0297 error:
0298     abort_creds(new);
0299     return NULL;
0300 }
0301 EXPORT_SYMBOL(prepare_creds);
0302 
0303 /*
0304  * Prepare credentials for current to perform an execve()
0305  * - The caller must hold ->cred_guard_mutex
0306  */
0307 struct cred *prepare_exec_creds(void)
0308 {
0309     struct cred *new;
0310 
0311     new = prepare_creds();
0312     if (!new)
0313         return new;
0314 
0315 #ifdef CONFIG_KEYS
0316     /* newly exec'd tasks don't get a thread keyring */
0317     key_put(new->thread_keyring);
0318     new->thread_keyring = NULL;
0319 
0320     /* inherit the session keyring; new process keyring */
0321     key_put(new->process_keyring);
0322     new->process_keyring = NULL;
0323 #endif
0324 
0325     new->suid = new->fsuid = new->euid;
0326     new->sgid = new->fsgid = new->egid;
0327 
0328     return new;
0329 }
0330 
0331 /*
0332  * Copy credentials for the new process created by fork()
0333  *
0334  * We share if we can, but under some circumstances we have to generate a new
0335  * set.
0336  *
0337  * The new process gets the current process's subjective credentials as its
0338  * objective and subjective credentials
0339  */
0340 int copy_creds(struct task_struct *p, unsigned long clone_flags)
0341 {
0342     struct cred *new;
0343     int ret;
0344 
0345 #ifdef CONFIG_KEYS_REQUEST_CACHE
0346     p->cached_requested_key = NULL;
0347 #endif
0348 
0349     if (
0350 #ifdef CONFIG_KEYS
0351         !p->cred->thread_keyring &&
0352 #endif
0353         clone_flags & CLONE_THREAD
0354         ) {
0355         p->real_cred = get_cred(p->cred);
0356         get_cred(p->cred);
0357         alter_cred_subscribers(p->cred, 2);
0358         kdebug("share_creds(%p{%d,%d})",
0359                p->cred, atomic_read(&p->cred->usage),
0360                read_cred_subscribers(p->cred));
0361         inc_rlimit_ucounts(task_ucounts(p), UCOUNT_RLIMIT_NPROC, 1);
0362         return 0;
0363     }
0364 
0365     new = prepare_creds();
0366     if (!new)
0367         return -ENOMEM;
0368 
0369     if (clone_flags & CLONE_NEWUSER) {
0370         ret = create_user_ns(new);
0371         if (ret < 0)
0372             goto error_put;
0373         ret = set_cred_ucounts(new);
0374         if (ret < 0)
0375             goto error_put;
0376     }
0377 
0378 #ifdef CONFIG_KEYS
0379     /* new threads get their own thread keyrings if their parent already
0380      * had one */
0381     if (new->thread_keyring) {
0382         key_put(new->thread_keyring);
0383         new->thread_keyring = NULL;
0384         if (clone_flags & CLONE_THREAD)
0385             install_thread_keyring_to_cred(new);
0386     }
0387 
0388     /* The process keyring is only shared between the threads in a process;
0389      * anything outside of those threads doesn't inherit.
0390      */
0391     if (!(clone_flags & CLONE_THREAD)) {
0392         key_put(new->process_keyring);
0393         new->process_keyring = NULL;
0394     }
0395 #endif
0396 
0397     p->cred = p->real_cred = get_cred(new);
0398     inc_rlimit_ucounts(task_ucounts(p), UCOUNT_RLIMIT_NPROC, 1);
0399     alter_cred_subscribers(new, 2);
0400     validate_creds(new);
0401     return 0;
0402 
0403 error_put:
0404     put_cred(new);
0405     return ret;
0406 }
0407 
0408 static bool cred_cap_issubset(const struct cred *set, const struct cred *subset)
0409 {
0410     const struct user_namespace *set_ns = set->user_ns;
0411     const struct user_namespace *subset_ns = subset->user_ns;
0412 
0413     /* If the two credentials are in the same user namespace see if
0414      * the capabilities of subset are a subset of set.
0415      */
0416     if (set_ns == subset_ns)
0417         return cap_issubset(subset->cap_permitted, set->cap_permitted);
0418 
0419     /* The credentials are in a different user namespaces
0420      * therefore one is a subset of the other only if a set is an
0421      * ancestor of subset and set->euid is owner of subset or one
0422      * of subsets ancestors.
0423      */
0424     for (;subset_ns != &init_user_ns; subset_ns = subset_ns->parent) {
0425         if ((set_ns == subset_ns->parent)  &&
0426             uid_eq(subset_ns->owner, set->euid))
0427             return true;
0428     }
0429 
0430     return false;
0431 }
0432 
0433 /**
0434  * commit_creds - Install new credentials upon the current task
0435  * @new: The credentials to be assigned
0436  *
0437  * Install a new set of credentials to the current task, using RCU to replace
0438  * the old set.  Both the objective and the subjective credentials pointers are
0439  * updated.  This function may not be called if the subjective credentials are
0440  * in an overridden state.
0441  *
0442  * This function eats the caller's reference to the new credentials.
0443  *
0444  * Always returns 0 thus allowing this function to be tail-called at the end
0445  * of, say, sys_setgid().
0446  */
0447 int commit_creds(struct cred *new)
0448 {
0449     struct task_struct *task = current;
0450     const struct cred *old = task->real_cred;
0451 
0452     kdebug("commit_creds(%p{%d,%d})", new,
0453            atomic_read(&new->usage),
0454            read_cred_subscribers(new));
0455 
0456     BUG_ON(task->cred != old);
0457 #ifdef CONFIG_DEBUG_CREDENTIALS
0458     BUG_ON(read_cred_subscribers(old) < 2);
0459     validate_creds(old);
0460     validate_creds(new);
0461 #endif
0462     BUG_ON(atomic_read(&new->usage) < 1);
0463 
0464     get_cred(new); /* we will require a ref for the subj creds too */
0465 
0466     /* dumpability changes */
0467     if (!uid_eq(old->euid, new->euid) ||
0468         !gid_eq(old->egid, new->egid) ||
0469         !uid_eq(old->fsuid, new->fsuid) ||
0470         !gid_eq(old->fsgid, new->fsgid) ||
0471         !cred_cap_issubset(old, new)) {
0472         if (task->mm)
0473             set_dumpable(task->mm, suid_dumpable);
0474         task->pdeath_signal = 0;
0475         /*
0476          * If a task drops privileges and becomes nondumpable,
0477          * the dumpability change must become visible before
0478          * the credential change; otherwise, a __ptrace_may_access()
0479          * racing with this change may be able to attach to a task it
0480          * shouldn't be able to attach to (as if the task had dropped
0481          * privileges without becoming nondumpable).
0482          * Pairs with a read barrier in __ptrace_may_access().
0483          */
0484         smp_wmb();
0485     }
0486 
0487     /* alter the thread keyring */
0488     if (!uid_eq(new->fsuid, old->fsuid))
0489         key_fsuid_changed(new);
0490     if (!gid_eq(new->fsgid, old->fsgid))
0491         key_fsgid_changed(new);
0492 
0493     /* do it
0494      * RLIMIT_NPROC limits on user->processes have already been checked
0495      * in set_user().
0496      */
0497     alter_cred_subscribers(new, 2);
0498     if (new->user != old->user || new->user_ns != old->user_ns)
0499         inc_rlimit_ucounts(new->ucounts, UCOUNT_RLIMIT_NPROC, 1);
0500     rcu_assign_pointer(task->real_cred, new);
0501     rcu_assign_pointer(task->cred, new);
0502     if (new->user != old->user || new->user_ns != old->user_ns)
0503         dec_rlimit_ucounts(old->ucounts, UCOUNT_RLIMIT_NPROC, 1);
0504     alter_cred_subscribers(old, -2);
0505 
0506     /* send notifications */
0507     if (!uid_eq(new->uid,   old->uid)  ||
0508         !uid_eq(new->euid,  old->euid) ||
0509         !uid_eq(new->suid,  old->suid) ||
0510         !uid_eq(new->fsuid, old->fsuid))
0511         proc_id_connector(task, PROC_EVENT_UID);
0512 
0513     if (!gid_eq(new->gid,   old->gid)  ||
0514         !gid_eq(new->egid,  old->egid) ||
0515         !gid_eq(new->sgid,  old->sgid) ||
0516         !gid_eq(new->fsgid, old->fsgid))
0517         proc_id_connector(task, PROC_EVENT_GID);
0518 
0519     /* release the old obj and subj refs both */
0520     put_cred(old);
0521     put_cred(old);
0522     return 0;
0523 }
0524 EXPORT_SYMBOL(commit_creds);
0525 
0526 /**
0527  * abort_creds - Discard a set of credentials and unlock the current task
0528  * @new: The credentials that were going to be applied
0529  *
0530  * Discard a set of credentials that were under construction and unlock the
0531  * current task.
0532  */
0533 void abort_creds(struct cred *new)
0534 {
0535     kdebug("abort_creds(%p{%d,%d})", new,
0536            atomic_read(&new->usage),
0537            read_cred_subscribers(new));
0538 
0539 #ifdef CONFIG_DEBUG_CREDENTIALS
0540     BUG_ON(read_cred_subscribers(new) != 0);
0541 #endif
0542     BUG_ON(atomic_read(&new->usage) < 1);
0543     put_cred(new);
0544 }
0545 EXPORT_SYMBOL(abort_creds);
0546 
0547 /**
0548  * override_creds - Override the current process's subjective credentials
0549  * @new: The credentials to be assigned
0550  *
0551  * Install a set of temporary override subjective credentials on the current
0552  * process, returning the old set for later reversion.
0553  */
0554 const struct cred *override_creds(const struct cred *new)
0555 {
0556     const struct cred *old = current->cred;
0557 
0558     kdebug("override_creds(%p{%d,%d})", new,
0559            atomic_read(&new->usage),
0560            read_cred_subscribers(new));
0561 
0562     validate_creds(old);
0563     validate_creds(new);
0564 
0565     /*
0566      * NOTE! This uses 'get_new_cred()' rather than 'get_cred()'.
0567      *
0568      * That means that we do not clear the 'non_rcu' flag, since
0569      * we are only installing the cred into the thread-synchronous
0570      * '->cred' pointer, not the '->real_cred' pointer that is
0571      * visible to other threads under RCU.
0572      *
0573      * Also note that we did validate_creds() manually, not depending
0574      * on the validation in 'get_cred()'.
0575      */
0576     get_new_cred((struct cred *)new);
0577     alter_cred_subscribers(new, 1);
0578     rcu_assign_pointer(current->cred, new);
0579     alter_cred_subscribers(old, -1);
0580 
0581     kdebug("override_creds() = %p{%d,%d}", old,
0582            atomic_read(&old->usage),
0583            read_cred_subscribers(old));
0584     return old;
0585 }
0586 EXPORT_SYMBOL(override_creds);
0587 
0588 /**
0589  * revert_creds - Revert a temporary subjective credentials override
0590  * @old: The credentials to be restored
0591  *
0592  * Revert a temporary set of override subjective credentials to an old set,
0593  * discarding the override set.
0594  */
0595 void revert_creds(const struct cred *old)
0596 {
0597     const struct cred *override = current->cred;
0598 
0599     kdebug("revert_creds(%p{%d,%d})", old,
0600            atomic_read(&old->usage),
0601            read_cred_subscribers(old));
0602 
0603     validate_creds(old);
0604     validate_creds(override);
0605     alter_cred_subscribers(old, 1);
0606     rcu_assign_pointer(current->cred, old);
0607     alter_cred_subscribers(override, -1);
0608     put_cred(override);
0609 }
0610 EXPORT_SYMBOL(revert_creds);
0611 
0612 /**
0613  * cred_fscmp - Compare two credentials with respect to filesystem access.
0614  * @a: The first credential
0615  * @b: The second credential
0616  *
0617  * cred_cmp() will return zero if both credentials have the same
0618  * fsuid, fsgid, and supplementary groups.  That is, if they will both
0619  * provide the same access to files based on mode/uid/gid.
0620  * If the credentials are different, then either -1 or 1 will
0621  * be returned depending on whether @a comes before or after @b
0622  * respectively in an arbitrary, but stable, ordering of credentials.
0623  *
0624  * Return: -1, 0, or 1 depending on comparison
0625  */
0626 int cred_fscmp(const struct cred *a, const struct cred *b)
0627 {
0628     struct group_info *ga, *gb;
0629     int g;
0630 
0631     if (a == b)
0632         return 0;
0633     if (uid_lt(a->fsuid, b->fsuid))
0634         return -1;
0635     if (uid_gt(a->fsuid, b->fsuid))
0636         return 1;
0637 
0638     if (gid_lt(a->fsgid, b->fsgid))
0639         return -1;
0640     if (gid_gt(a->fsgid, b->fsgid))
0641         return 1;
0642 
0643     ga = a->group_info;
0644     gb = b->group_info;
0645     if (ga == gb)
0646         return 0;
0647     if (ga == NULL)
0648         return -1;
0649     if (gb == NULL)
0650         return 1;
0651     if (ga->ngroups < gb->ngroups)
0652         return -1;
0653     if (ga->ngroups > gb->ngroups)
0654         return 1;
0655 
0656     for (g = 0; g < ga->ngroups; g++) {
0657         if (gid_lt(ga->gid[g], gb->gid[g]))
0658             return -1;
0659         if (gid_gt(ga->gid[g], gb->gid[g]))
0660             return 1;
0661     }
0662     return 0;
0663 }
0664 EXPORT_SYMBOL(cred_fscmp);
0665 
0666 int set_cred_ucounts(struct cred *new)
0667 {
0668     struct ucounts *new_ucounts, *old_ucounts = new->ucounts;
0669 
0670     /*
0671      * This optimization is needed because alloc_ucounts() uses locks
0672      * for table lookups.
0673      */
0674     if (old_ucounts->ns == new->user_ns && uid_eq(old_ucounts->uid, new->uid))
0675         return 0;
0676 
0677     if (!(new_ucounts = alloc_ucounts(new->user_ns, new->uid)))
0678         return -EAGAIN;
0679 
0680     new->ucounts = new_ucounts;
0681     put_ucounts(old_ucounts);
0682 
0683     return 0;
0684 }
0685 
0686 /*
0687  * initialise the credentials stuff
0688  */
0689 void __init cred_init(void)
0690 {
0691     /* allocate a slab in which we can store credentials */
0692     cred_jar = kmem_cache_create("cred_jar", sizeof(struct cred), 0,
0693             SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_ACCOUNT, NULL);
0694 }
0695 
0696 /**
0697  * prepare_kernel_cred - Prepare a set of credentials for a kernel service
0698  * @daemon: A userspace daemon to be used as a reference
0699  *
0700  * Prepare a set of credentials for a kernel service.  This can then be used to
0701  * override a task's own credentials so that work can be done on behalf of that
0702  * task that requires a different subjective context.
0703  *
0704  * @daemon is used to provide a base for the security record, but can be NULL.
0705  * If @daemon is supplied, then the security data will be derived from that;
0706  * otherwise they'll be set to 0 and no groups, full capabilities and no keys.
0707  *
0708  * The caller may change these controls afterwards if desired.
0709  *
0710  * Returns the new credentials or NULL if out of memory.
0711  */
0712 struct cred *prepare_kernel_cred(struct task_struct *daemon)
0713 {
0714     const struct cred *old;
0715     struct cred *new;
0716 
0717     new = kmem_cache_alloc(cred_jar, GFP_KERNEL);
0718     if (!new)
0719         return NULL;
0720 
0721     kdebug("prepare_kernel_cred() alloc %p", new);
0722 
0723     if (daemon)
0724         old = get_task_cred(daemon);
0725     else
0726         old = get_cred(&init_cred);
0727 
0728     validate_creds(old);
0729 
0730     *new = *old;
0731     new->non_rcu = 0;
0732     atomic_set(&new->usage, 1);
0733     set_cred_subscribers(new, 0);
0734     get_uid(new->user);
0735     get_user_ns(new->user_ns);
0736     get_group_info(new->group_info);
0737 
0738 #ifdef CONFIG_KEYS
0739     new->session_keyring = NULL;
0740     new->process_keyring = NULL;
0741     new->thread_keyring = NULL;
0742     new->request_key_auth = NULL;
0743     new->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING;
0744 #endif
0745 
0746 #ifdef CONFIG_SECURITY
0747     new->security = NULL;
0748 #endif
0749     new->ucounts = get_ucounts(new->ucounts);
0750     if (!new->ucounts)
0751         goto error;
0752 
0753     if (security_prepare_creds(new, old, GFP_KERNEL_ACCOUNT) < 0)
0754         goto error;
0755 
0756     put_cred(old);
0757     validate_creds(new);
0758     return new;
0759 
0760 error:
0761     put_cred(new);
0762     put_cred(old);
0763     return NULL;
0764 }
0765 EXPORT_SYMBOL(prepare_kernel_cred);
0766 
0767 /**
0768  * set_security_override - Set the security ID in a set of credentials
0769  * @new: The credentials to alter
0770  * @secid: The LSM security ID to set
0771  *
0772  * Set the LSM security ID in a set of credentials so that the subjective
0773  * security is overridden when an alternative set of credentials is used.
0774  */
0775 int set_security_override(struct cred *new, u32 secid)
0776 {
0777     return security_kernel_act_as(new, secid);
0778 }
0779 EXPORT_SYMBOL(set_security_override);
0780 
0781 /**
0782  * set_security_override_from_ctx - Set the security ID in a set of credentials
0783  * @new: The credentials to alter
0784  * @secctx: The LSM security context to generate the security ID from.
0785  *
0786  * Set the LSM security ID in a set of credentials so that the subjective
0787  * security is overridden when an alternative set of credentials is used.  The
0788  * security ID is specified in string form as a security context to be
0789  * interpreted by the LSM.
0790  */
0791 int set_security_override_from_ctx(struct cred *new, const char *secctx)
0792 {
0793     u32 secid;
0794     int ret;
0795 
0796     ret = security_secctx_to_secid(secctx, strlen(secctx), &secid);
0797     if (ret < 0)
0798         return ret;
0799 
0800     return set_security_override(new, secid);
0801 }
0802 EXPORT_SYMBOL(set_security_override_from_ctx);
0803 
0804 /**
0805  * set_create_files_as - Set the LSM file create context in a set of credentials
0806  * @new: The credentials to alter
0807  * @inode: The inode to take the context from
0808  *
0809  * Change the LSM file creation context in a set of credentials to be the same
0810  * as the object context of the specified inode, so that the new inodes have
0811  * the same MAC context as that inode.
0812  */
0813 int set_create_files_as(struct cred *new, struct inode *inode)
0814 {
0815     if (!uid_valid(inode->i_uid) || !gid_valid(inode->i_gid))
0816         return -EINVAL;
0817     new->fsuid = inode->i_uid;
0818     new->fsgid = inode->i_gid;
0819     return security_kernel_create_files_as(new, inode);
0820 }
0821 EXPORT_SYMBOL(set_create_files_as);
0822 
0823 #ifdef CONFIG_DEBUG_CREDENTIALS
0824 
0825 bool creds_are_invalid(const struct cred *cred)
0826 {
0827     if (cred->magic != CRED_MAGIC)
0828         return true;
0829     return false;
0830 }
0831 EXPORT_SYMBOL(creds_are_invalid);
0832 
0833 /*
0834  * dump invalid credentials
0835  */
0836 static void dump_invalid_creds(const struct cred *cred, const char *label,
0837                    const struct task_struct *tsk)
0838 {
0839     printk(KERN_ERR "CRED: %s credentials: %p %s%s%s\n",
0840            label, cred,
0841            cred == &init_cred ? "[init]" : "",
0842            cred == tsk->real_cred ? "[real]" : "",
0843            cred == tsk->cred ? "[eff]" : "");
0844     printk(KERN_ERR "CRED: ->magic=%x, put_addr=%p\n",
0845            cred->magic, cred->put_addr);
0846     printk(KERN_ERR "CRED: ->usage=%d, subscr=%d\n",
0847            atomic_read(&cred->usage),
0848            read_cred_subscribers(cred));
0849     printk(KERN_ERR "CRED: ->*uid = { %d,%d,%d,%d }\n",
0850         from_kuid_munged(&init_user_ns, cred->uid),
0851         from_kuid_munged(&init_user_ns, cred->euid),
0852         from_kuid_munged(&init_user_ns, cred->suid),
0853         from_kuid_munged(&init_user_ns, cred->fsuid));
0854     printk(KERN_ERR "CRED: ->*gid = { %d,%d,%d,%d }\n",
0855         from_kgid_munged(&init_user_ns, cred->gid),
0856         from_kgid_munged(&init_user_ns, cred->egid),
0857         from_kgid_munged(&init_user_ns, cred->sgid),
0858         from_kgid_munged(&init_user_ns, cred->fsgid));
0859 #ifdef CONFIG_SECURITY
0860     printk(KERN_ERR "CRED: ->security is %p\n", cred->security);
0861     if ((unsigned long) cred->security >= PAGE_SIZE &&
0862         (((unsigned long) cred->security & 0xffffff00) !=
0863          (POISON_FREE << 24 | POISON_FREE << 16 | POISON_FREE << 8)))
0864         printk(KERN_ERR "CRED: ->security {%x, %x}\n",
0865                ((u32*)cred->security)[0],
0866                ((u32*)cred->security)[1]);
0867 #endif
0868 }
0869 
0870 /*
0871  * report use of invalid credentials
0872  */
0873 void __noreturn __invalid_creds(const struct cred *cred, const char *file, unsigned line)
0874 {
0875     printk(KERN_ERR "CRED: Invalid credentials\n");
0876     printk(KERN_ERR "CRED: At %s:%u\n", file, line);
0877     dump_invalid_creds(cred, "Specified", current);
0878     BUG();
0879 }
0880 EXPORT_SYMBOL(__invalid_creds);
0881 
0882 /*
0883  * check the credentials on a process
0884  */
0885 void __validate_process_creds(struct task_struct *tsk,
0886                   const char *file, unsigned line)
0887 {
0888     if (tsk->cred == tsk->real_cred) {
0889         if (unlikely(read_cred_subscribers(tsk->cred) < 2 ||
0890                  creds_are_invalid(tsk->cred)))
0891             goto invalid_creds;
0892     } else {
0893         if (unlikely(read_cred_subscribers(tsk->real_cred) < 1 ||
0894                  read_cred_subscribers(tsk->cred) < 1 ||
0895                  creds_are_invalid(tsk->real_cred) ||
0896                  creds_are_invalid(tsk->cred)))
0897             goto invalid_creds;
0898     }
0899     return;
0900 
0901 invalid_creds:
0902     printk(KERN_ERR "CRED: Invalid process credentials\n");
0903     printk(KERN_ERR "CRED: At %s:%u\n", file, line);
0904 
0905     dump_invalid_creds(tsk->real_cred, "Real", tsk);
0906     if (tsk->cred != tsk->real_cred)
0907         dump_invalid_creds(tsk->cred, "Effective", tsk);
0908     else
0909         printk(KERN_ERR "CRED: Effective creds == Real creds\n");
0910     BUG();
0911 }
0912 EXPORT_SYMBOL(__validate_process_creds);
0913 
0914 /*
0915  * check creds for do_exit()
0916  */
0917 void validate_creds_for_do_exit(struct task_struct *tsk)
0918 {
0919     kdebug("validate_creds_for_do_exit(%p,%p{%d,%d})",
0920            tsk->real_cred, tsk->cred,
0921            atomic_read(&tsk->cred->usage),
0922            read_cred_subscribers(tsk->cred));
0923 
0924     __validate_process_creds(tsk, __FILE__, __LINE__);
0925 }
0926 
0927 #endif /* CONFIG_DEBUG_CREDENTIALS */