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