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