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