Randy Dunlap | d410fa4 | 2011-05-19 15:59:38 -0700 | [diff] [blame] | 1 | /* Task credentials management - see Documentation/security/credentials.txt |
David Howells | f1752ee | 2008-11-14 10:39:17 +1100 | [diff] [blame] | 2 | * |
| 3 | * Copyright (C) 2008 Red Hat, Inc. All Rights Reserved. |
| 4 | * Written by David Howells (dhowells@redhat.com) |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public Licence |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the Licence, or (at your option) any later version. |
| 10 | */ |
Paul Gortmaker | 9984de1 | 2011-05-23 14:51:41 -0400 | [diff] [blame] | 11 | #include <linux/export.h> |
David Howells | f1752ee | 2008-11-14 10:39:17 +1100 | [diff] [blame] | 12 | #include <linux/cred.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 13 | #include <linux/slab.h> |
David Howells | f1752ee | 2008-11-14 10:39:17 +1100 | [diff] [blame] | 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> |
Al Viro | 4040153 | 2012-02-13 03:58:52 +0000 | [diff] [blame] | 19 | #include <linux/binfmts.h> |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 20 | #include <linux/cn_proc.h> |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 21 | |
David Howells | e0e8173 | 2009-09-02 09:13:40 +0100 | [diff] [blame] | 22 | #if 0 |
| 23 | #define kdebug(FMT, ...) \ |
| 24 | printk("[%-5.5s%5u] "FMT"\n", current->comm, current->pid ,##__VA_ARGS__) |
| 25 | #else |
David Howells | e0e8173 | 2009-09-02 09:13:40 +0100 | [diff] [blame] | 26 | #define kdebug(FMT, ...) \ |
| 27 | no_printk("[%-5.5s%5u] "FMT"\n", current->comm, current->pid ,##__VA_ARGS__) |
| 28 | #endif |
| 29 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 30 | static struct kmem_cache *cred_jar; |
David Howells | f1752ee | 2008-11-14 10:39:17 +1100 | [diff] [blame] | 31 | |
Iulia Manda | 2813893 | 2015-04-15 16:16:41 -0700 | [diff] [blame] | 32 | /* init to 2 - one for init_task, one to ensure it is never freed */ |
| 33 | struct group_info init_groups = { .usage = ATOMIC_INIT(2) }; |
| 34 | |
David Howells | f1752ee | 2008-11-14 10:39:17 +1100 | [diff] [blame] | 35 | /* |
| 36 | * The initial credentials for the initial task |
| 37 | */ |
| 38 | struct cred init_cred = { |
David Howells | 3b11a1d | 2008-11-14 10:39:26 +1100 | [diff] [blame] | 39 | .usage = ATOMIC_INIT(4), |
David Howells | e0e8173 | 2009-09-02 09:13:40 +0100 | [diff] [blame] | 40 | #ifdef CONFIG_DEBUG_CREDENTIALS |
| 41 | .subscribers = ATOMIC_INIT(2), |
| 42 | .magic = CRED_MAGIC, |
| 43 | #endif |
Eric W. Biederman | 078de5f | 2012-02-08 07:00:08 -0800 | [diff] [blame] | 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, |
David Howells | f1752ee | 2008-11-14 10:39:17 +1100 | [diff] [blame] | 52 | .securebits = SECUREBITS_DEFAULT, |
Eric Paris | a3232d2 | 2011-04-01 17:08:45 -0400 | [diff] [blame] | 53 | .cap_inheritable = CAP_EMPTY_SET, |
David Howells | f1752ee | 2008-11-14 10:39:17 +1100 | [diff] [blame] | 54 | .cap_permitted = CAP_FULL_SET, |
Eric Paris | a3232d2 | 2011-04-01 17:08:45 -0400 | [diff] [blame] | 55 | .cap_effective = CAP_FULL_SET, |
| 56 | .cap_bset = CAP_FULL_SET, |
David Howells | f1752ee | 2008-11-14 10:39:17 +1100 | [diff] [blame] | 57 | .user = INIT_USER, |
Serge E. Hallyn | 47a150e | 2011-05-13 04:27:54 +0100 | [diff] [blame] | 58 | .user_ns = &init_user_ns, |
David Howells | f1752ee | 2008-11-14 10:39:17 +1100 | [diff] [blame] | 59 | .group_info = &init_groups, |
| 60 | }; |
| 61 | |
David Howells | e0e8173 | 2009-09-02 09:13:40 +0100 | [diff] [blame] | 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 | |
David Howells | f1752ee | 2008-11-14 10:39:17 +1100 | [diff] [blame] | 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 | |
David Howells | e0e8173 | 2009-09-02 09:13:40 +0100 | [diff] [blame] | 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 |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 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)); |
David Howells | e0e8173 | 2009-09-02 09:13:40 +0100 | [diff] [blame] | 109 | #endif |
David Howells | f1752ee | 2008-11-14 10:39:17 +1100 | [diff] [blame] | 110 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 111 | security_cred_free(cred); |
David Howells | 3a50597 | 2012-10-02 19:24:29 +0100 | [diff] [blame] | 112 | key_put(cred->session_keyring); |
| 113 | key_put(cred->process_keyring); |
David Howells | f1752ee | 2008-11-14 10:39:17 +1100 | [diff] [blame] | 114 | key_put(cred->thread_keyring); |
| 115 | key_put(cred->request_key_auth); |
David Howells | 4a5d6ba | 2009-09-14 12:45:39 +0100 | [diff] [blame] | 116 | if (cred->group_info) |
| 117 | put_group_info(cred->group_info); |
David Howells | f1752ee | 2008-11-14 10:39:17 +1100 | [diff] [blame] | 118 | free_uid(cred->user); |
Eric W. Biederman | 0093ccb | 2011-11-16 21:52:53 -0800 | [diff] [blame] | 119 | put_user_ns(cred->user_ns); |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 120 | kmem_cache_free(cred_jar, cred); |
David Howells | f1752ee | 2008-11-14 10:39:17 +1100 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | /** |
| 124 | * __put_cred - Destroy a set of credentials |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 125 | * @cred: The record to release |
David Howells | f1752ee | 2008-11-14 10:39:17 +1100 | [diff] [blame] | 126 | * |
| 127 | * Destroy a set of credentials on which no references remain. |
| 128 | */ |
| 129 | void __put_cred(struct cred *cred) |
| 130 | { |
David Howells | e0e8173 | 2009-09-02 09:13:40 +0100 | [diff] [blame] | 131 | kdebug("__put_cred(%p{%d,%d})", cred, |
| 132 | atomic_read(&cred->usage), |
| 133 | read_cred_subscribers(cred)); |
| 134 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 135 | BUG_ON(atomic_read(&cred->usage) != 0); |
David Howells | e0e8173 | 2009-09-02 09:13:40 +0100 | [diff] [blame] | 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); |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 143 | |
David Howells | f1752ee | 2008-11-14 10:39:17 +1100 | [diff] [blame] | 144 | call_rcu(&cred->rcu, put_cred_rcu); |
| 145 | } |
| 146 | EXPORT_SYMBOL(__put_cred); |
| 147 | |
David Howells | e0e8173 | 2009-09-02 09:13:40 +0100 | [diff] [blame] | 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); |
David Howells | ee18d64 | 2009-09-02 09:14:21 +0100 | [diff] [blame] | 170 | } |
| 171 | |
David Howells | de09a97 | 2010-07-29 12:45:49 +0100 | [diff] [blame] | 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 | |
David Howells | ee18d64 | 2009-09-02 09:14:21 +0100 | [diff] [blame] | 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 | |
David Howells | ee18d64 | 2009-09-02 09:14:21 +0100 | [diff] [blame] | 209 | atomic_set(&new->usage, 1); |
Tetsuo Handa | 2edeaa3 | 2011-02-07 13:36:10 +0000 | [diff] [blame] | 210 | #ifdef CONFIG_DEBUG_CREDENTIALS |
| 211 | new->magic = CRED_MAGIC; |
| 212 | #endif |
David Howells | ee18d64 | 2009-09-02 09:14:21 +0100 | [diff] [blame] | 213 | |
| 214 | if (security_cred_alloc_blank(new, GFP_KERNEL) < 0) |
| 215 | goto error; |
| 216 | |
David Howells | ee18d64 | 2009-09-02 09:14:21 +0100 | [diff] [blame] | 217 | return new; |
| 218 | |
| 219 | error: |
| 220 | abort_creds(new); |
| 221 | return NULL; |
David Howells | e0e8173 | 2009-09-02 09:13:40 +0100 | [diff] [blame] | 222 | } |
| 223 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 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 | * |
David Howells | 3b11a1d | 2008-11-14 10:39:26 +1100 | [diff] [blame] | 232 | * Preparation involves making a copy of the objective creds for modification. |
| 233 | * |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 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. |
David Howells | f1752ee | 2008-11-14 10:39:17 +1100 | [diff] [blame] | 237 | */ |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 238 | struct cred *prepare_creds(void) |
David Howells | f1752ee | 2008-11-14 10:39:17 +1100 | [diff] [blame] | 239 | { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 240 | struct task_struct *task = current; |
| 241 | const struct cred *old; |
| 242 | struct cred *new; |
David Howells | f1752ee | 2008-11-14 10:39:17 +1100 | [diff] [blame] | 243 | |
David Howells | e0e8173 | 2009-09-02 09:13:40 +0100 | [diff] [blame] | 244 | validate_process_creds(); |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 245 | |
| 246 | new = kmem_cache_alloc(cred_jar, GFP_KERNEL); |
| 247 | if (!new) |
| 248 | return NULL; |
| 249 | |
David Howells | e0e8173 | 2009-09-02 09:13:40 +0100 | [diff] [blame] | 250 | kdebug("prepare_creds() alloc %p", new); |
| 251 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 252 | old = task->cred; |
| 253 | memcpy(new, old, sizeof(struct cred)); |
| 254 | |
| 255 | atomic_set(&new->usage, 1); |
David Howells | e0e8173 | 2009-09-02 09:13:40 +0100 | [diff] [blame] | 256 | set_cred_subscribers(new, 0); |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 257 | get_group_info(new->group_info); |
| 258 | get_uid(new->user); |
Eric W. Biederman | 0093ccb | 2011-11-16 21:52:53 -0800 | [diff] [blame] | 259 | get_user_ns(new->user_ns); |
David Howells | f1752ee | 2008-11-14 10:39:17 +1100 | [diff] [blame] | 260 | |
David Howells | bb952bb | 2008-11-14 10:39:20 +1100 | [diff] [blame] | 261 | #ifdef CONFIG_KEYS |
David Howells | 3a50597 | 2012-10-02 19:24:29 +0100 | [diff] [blame] | 262 | key_get(new->session_keyring); |
| 263 | key_get(new->process_keyring); |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 264 | key_get(new->thread_keyring); |
| 265 | key_get(new->request_key_auth); |
David Howells | bb952bb | 2008-11-14 10:39:20 +1100 | [diff] [blame] | 266 | #endif |
| 267 | |
David Howells | f1752ee | 2008-11-14 10:39:17 +1100 | [diff] [blame] | 268 | #ifdef CONFIG_SECURITY |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 269 | new->security = NULL; |
David Howells | f1752ee | 2008-11-14 10:39:17 +1100 | [diff] [blame] | 270 | #endif |
| 271 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 272 | if (security_prepare_creds(new, old, GFP_KERNEL) < 0) |
| 273 | goto error; |
David Howells | e0e8173 | 2009-09-02 09:13:40 +0100 | [diff] [blame] | 274 | validate_creds(new); |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 275 | return new; |
| 276 | |
| 277 | error: |
| 278 | abort_creds(new); |
| 279 | return NULL; |
| 280 | } |
| 281 | EXPORT_SYMBOL(prepare_creds); |
| 282 | |
| 283 | /* |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 284 | * Prepare credentials for current to perform an execve() |
KOSAKI Motohiro | 9b1bf12 | 2010-10-27 15:34:08 -0700 | [diff] [blame] | 285 | * - The caller must hold ->cred_guard_mutex |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 286 | */ |
| 287 | struct cred *prepare_exec_creds(void) |
| 288 | { |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 289 | struct cred *new; |
| 290 | |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 291 | new = prepare_creds(); |
David Howells | 3a50597 | 2012-10-02 19:24:29 +0100 | [diff] [blame] | 292 | if (!new) |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 293 | return new; |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 294 | |
| 295 | #ifdef CONFIG_KEYS |
| 296 | /* newly exec'd tasks don't get a thread keyring */ |
| 297 | key_put(new->thread_keyring); |
| 298 | new->thread_keyring = NULL; |
| 299 | |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 300 | /* inherit the session keyring; new process keyring */ |
David Howells | 3a50597 | 2012-10-02 19:24:29 +0100 | [diff] [blame] | 301 | key_put(new->process_keyring); |
| 302 | new->process_keyring = NULL; |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 303 | #endif |
| 304 | |
| 305 | return new; |
| 306 | } |
| 307 | |
| 308 | /* |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 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. |
David Howells | 3b11a1d | 2008-11-14 10:39:26 +1100 | [diff] [blame] | 313 | * |
| 314 | * The new process gets the current process's subjective credentials as its |
| 315 | * objective and subjective credentials |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 316 | */ |
| 317 | int copy_creds(struct task_struct *p, unsigned long clone_flags) |
| 318 | { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 319 | struct cred *new; |
Serge Hallyn | 18b6e04 | 2008-10-15 16:38:45 -0500 | [diff] [blame] | 320 | int ret; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 321 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 322 | if ( |
| 323 | #ifdef CONFIG_KEYS |
| 324 | !p->cred->thread_keyring && |
| 325 | #endif |
| 326 | clone_flags & CLONE_THREAD |
| 327 | ) { |
David Howells | 3b11a1d | 2008-11-14 10:39:26 +1100 | [diff] [blame] | 328 | p->real_cred = get_cred(p->cred); |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 329 | get_cred(p->cred); |
David Howells | e0e8173 | 2009-09-02 09:13:40 +0100 | [diff] [blame] | 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)); |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 334 | atomic_inc(&p->cred->user->processes); |
| 335 | return 0; |
David Howells | f1752ee | 2008-11-14 10:39:17 +1100 | [diff] [blame] | 336 | } |
| 337 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 338 | new = prepare_creds(); |
| 339 | if (!new) |
| 340 | return -ENOMEM; |
David Howells | f1752ee | 2008-11-14 10:39:17 +1100 | [diff] [blame] | 341 | |
Serge Hallyn | 18b6e04 | 2008-10-15 16:38:45 -0500 | [diff] [blame] | 342 | if (clone_flags & CLONE_NEWUSER) { |
| 343 | ret = create_user_ns(new); |
| 344 | if (ret < 0) |
| 345 | goto error_put; |
| 346 | } |
| 347 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 348 | #ifdef CONFIG_KEYS |
| 349 | /* new threads get their own thread keyrings if their parent already |
| 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 | } |
David Howells | f1752ee | 2008-11-14 10:39:17 +1100 | [diff] [blame] | 357 | |
David Howells | 3a50597 | 2012-10-02 19:24:29 +0100 | [diff] [blame] | 358 | /* The process keyring is only shared between the threads in a process; |
| 359 | * anything outside of those threads doesn't inherit. |
| 360 | */ |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 361 | if (!(clone_flags & CLONE_THREAD)) { |
David Howells | 3a50597 | 2012-10-02 19:24:29 +0100 | [diff] [blame] | 362 | key_put(new->process_keyring); |
| 363 | new->process_keyring = NULL; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 364 | } |
| 365 | #endif |
| 366 | |
| 367 | atomic_inc(&new->user->processes); |
David Howells | 3b11a1d | 2008-11-14 10:39:26 +1100 | [diff] [blame] | 368 | p->cred = p->real_cred = get_cred(new); |
David Howells | e0e8173 | 2009-09-02 09:13:40 +0100 | [diff] [blame] | 369 | alter_cred_subscribers(new, 2); |
| 370 | validate_creds(new); |
David Howells | f1752ee | 2008-11-14 10:39:17 +1100 | [diff] [blame] | 371 | return 0; |
Serge Hallyn | 18b6e04 | 2008-10-15 16:38:45 -0500 | [diff] [blame] | 372 | |
| 373 | error_put: |
| 374 | put_cred(new); |
| 375 | return ret; |
David Howells | f1752ee | 2008-11-14 10:39:17 +1100 | [diff] [blame] | 376 | } |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 377 | |
Eric W. Biederman | aa6d054 | 2012-12-14 08:50:54 -0800 | [diff] [blame] | 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 | /* If the two credentials are in the same user namespace see if |
| 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 | /* The credentials are in a different user namespaces |
| 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 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 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 |
David Howells | 3b11a1d | 2008-11-14 10:39:26 +1100 | [diff] [blame] | 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. |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 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; |
David Howells | e0e8173 | 2009-09-02 09:13:40 +0100 | [diff] [blame] | 420 | const struct cred *old = task->real_cred; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 421 | |
David Howells | e0e8173 | 2009-09-02 09:13:40 +0100 | [diff] [blame] | 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 |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 432 | BUG_ON(atomic_read(&new->usage) < 1); |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 433 | |
David Howells | 3b11a1d | 2008-11-14 10:39:26 +1100 | [diff] [blame] | 434 | get_cred(new); /* we will require a ref for the subj creds too */ |
| 435 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 436 | /* dumpability changes */ |
Eric W. Biederman | 078de5f | 2012-02-08 07:00:08 -0800 | [diff] [blame] | 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) || |
Eric W. Biederman | aa6d054 | 2012-12-14 08:50:54 -0800 | [diff] [blame] | 441 | !cred_cap_issubset(old, new)) { |
David Howells | b945637 | 2009-01-08 11:18:31 +0000 | [diff] [blame] | 442 | if (task->mm) |
| 443 | set_dumpable(task->mm, suid_dumpable); |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 444 | task->pdeath_signal = 0; |
| 445 | smp_wmb(); |
| 446 | } |
| 447 | |
| 448 | /* alter the thread keyring */ |
Eric W. Biederman | 078de5f | 2012-02-08 07:00:08 -0800 | [diff] [blame] | 449 | if (!uid_eq(new->fsuid, old->fsuid)) |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 450 | key_fsuid_changed(task); |
Eric W. Biederman | 078de5f | 2012-02-08 07:00:08 -0800 | [diff] [blame] | 451 | if (!gid_eq(new->fsgid, old->fsgid)) |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 452 | key_fsgid_changed(task); |
| 453 | |
| 454 | /* do it |
Vasiliy Kulikov | 72fa599 | 2011-08-08 19:02:04 +0400 | [diff] [blame] | 455 | * RLIMIT_NPROC limits on user->processes have already been checked |
| 456 | * in set_user(). |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 457 | */ |
David Howells | e0e8173 | 2009-09-02 09:13:40 +0100 | [diff] [blame] | 458 | alter_cred_subscribers(new, 2); |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 459 | if (new->user != old->user) |
| 460 | atomic_inc(&new->user->processes); |
David Howells | 3b11a1d | 2008-11-14 10:39:26 +1100 | [diff] [blame] | 461 | rcu_assign_pointer(task->real_cred, new); |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 462 | rcu_assign_pointer(task->cred, new); |
| 463 | if (new->user != old->user) |
| 464 | atomic_dec(&old->user->processes); |
David Howells | e0e8173 | 2009-09-02 09:13:40 +0100 | [diff] [blame] | 465 | alter_cred_subscribers(old, -2); |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 466 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 467 | /* send notifications */ |
Eric W. Biederman | 078de5f | 2012-02-08 07:00:08 -0800 | [diff] [blame] | 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)) |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 472 | proc_id_connector(task, PROC_EVENT_UID); |
| 473 | |
Eric W. Biederman | 078de5f | 2012-02-08 07:00:08 -0800 | [diff] [blame] | 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)) |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 478 | proc_id_connector(task, PROC_EVENT_GID); |
| 479 | |
David Howells | 3b11a1d | 2008-11-14 10:39:26 +1100 | [diff] [blame] | 480 | /* release the old obj and subj refs both */ |
| 481 | put_cred(old); |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 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 | { |
David Howells | e0e8173 | 2009-09-02 09:13:40 +0100 | [diff] [blame] | 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 |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 503 | BUG_ON(atomic_read(&new->usage) < 1); |
| 504 | put_cred(new); |
| 505 | } |
| 506 | EXPORT_SYMBOL(abort_creds); |
| 507 | |
| 508 | /** |
David Howells | 3b11a1d | 2008-11-14 10:39:26 +1100 | [diff] [blame] | 509 | * override_creds - Override the current process's subjective credentials |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 510 | * @new: The credentials to be assigned |
| 511 | * |
David Howells | 3b11a1d | 2008-11-14 10:39:26 +1100 | [diff] [blame] | 512 | * Install a set of temporary override subjective credentials on the current |
| 513 | * process, returning the old set for later reversion. |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 514 | */ |
| 515 | const struct cred *override_creds(const struct cred *new) |
| 516 | { |
| 517 | const struct cred *old = current->cred; |
| 518 | |
David Howells | e0e8173 | 2009-09-02 09:13:40 +0100 | [diff] [blame] | 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)); |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 533 | return old; |
| 534 | } |
| 535 | EXPORT_SYMBOL(override_creds); |
| 536 | |
| 537 | /** |
David Howells | 3b11a1d | 2008-11-14 10:39:26 +1100 | [diff] [blame] | 538 | * revert_creds - Revert a temporary subjective credentials override |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 539 | * @old: The credentials to be restored |
| 540 | * |
David Howells | 3b11a1d | 2008-11-14 10:39:26 +1100 | [diff] [blame] | 541 | * Revert a temporary set of override subjective credentials to an old set, |
| 542 | * discarding the override set. |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 543 | */ |
| 544 | void revert_creds(const struct cred *old) |
| 545 | { |
| 546 | const struct cred *override = current->cred; |
| 547 | |
David Howells | e0e8173 | 2009-09-02 09:13:40 +0100 | [diff] [blame] | 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); |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 555 | rcu_assign_pointer(current->cred, old); |
David Howells | e0e8173 | 2009-09-02 09:13:40 +0100 | [diff] [blame] | 556 | alter_cred_subscribers(override, -1); |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 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 | /* allocate a slab in which we can store credentials */ |
| 567 | cred_jar = kmem_cache_create("cred_jar", sizeof(struct cred), |
| 568 | 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL); |
| 569 | } |
David Howells | 3a3b7ce | 2008-11-14 10:39:28 +1100 | [diff] [blame] | 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 | |
David Howells | e0e8173 | 2009-09-02 09:13:40 +0100 | [diff] [blame] | 598 | kdebug("prepare_kernel_cred() alloc %p", new); |
| 599 | |
David Howells | 3a3b7ce | 2008-11-14 10:39:28 +1100 | [diff] [blame] | 600 | if (daemon) |
| 601 | old = get_task_cred(daemon); |
| 602 | else |
| 603 | old = get_cred(&init_cred); |
| 604 | |
David Howells | e0e8173 | 2009-09-02 09:13:40 +0100 | [diff] [blame] | 605 | validate_creds(old); |
| 606 | |
David Howells | 43529c9 | 2009-01-09 16:13:46 +0000 | [diff] [blame] | 607 | *new = *old; |
Tetsuo Handa | fb2b2a1 | 2011-02-07 13:36:16 +0000 | [diff] [blame] | 608 | atomic_set(&new->usage, 1); |
| 609 | set_cred_subscribers(new, 0); |
David Howells | 3a3b7ce | 2008-11-14 10:39:28 +1100 | [diff] [blame] | 610 | get_uid(new->user); |
Eric W. Biederman | 0093ccb | 2011-11-16 21:52:53 -0800 | [diff] [blame] | 611 | get_user_ns(new->user_ns); |
David Howells | 3a3b7ce | 2008-11-14 10:39:28 +1100 | [diff] [blame] | 612 | get_group_info(new->group_info); |
| 613 | |
| 614 | #ifdef CONFIG_KEYS |
David Howells | 3a50597 | 2012-10-02 19:24:29 +0100 | [diff] [blame] | 615 | new->session_keyring = NULL; |
| 616 | new->process_keyring = NULL; |
David Howells | 3a3b7ce | 2008-11-14 10:39:28 +1100 | [diff] [blame] | 617 | new->thread_keyring = NULL; |
David Howells | 3a50597 | 2012-10-02 19:24:29 +0100 | [diff] [blame] | 618 | new->request_key_auth = NULL; |
David Howells | 3a3b7ce | 2008-11-14 10:39:28 +1100 | [diff] [blame] | 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 | |
David Howells | 3a3b7ce | 2008-11-14 10:39:28 +1100 | [diff] [blame] | 628 | put_cred(old); |
David Howells | e0e8173 | 2009-09-02 09:13:40 +0100 | [diff] [blame] | 629 | validate_creds(new); |
David Howells | 3a3b7ce | 2008-11-14 10:39:28 +1100 | [diff] [blame] | 630 | return new; |
| 631 | |
| 632 | error: |
| 633 | put_cred(new); |
David Howells | 0de3368 | 2009-01-09 16:13:41 +0000 | [diff] [blame] | 634 | put_cred(old); |
David Howells | 3a3b7ce | 2008-11-14 10:39:28 +1100 | [diff] [blame] | 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); |
David Howells | e0e8173 | 2009-09-02 09:13:40 +0100 | [diff] [blame] | 692 | |
| 693 | #ifdef CONFIG_DEBUG_CREDENTIALS |
| 694 | |
Andrew Morton | 74908a0 | 2009-09-17 17:47:12 -0700 | [diff] [blame] | 695 | bool creds_are_invalid(const struct cred *cred) |
| 696 | { |
| 697 | if (cred->magic != CRED_MAGIC) |
| 698 | return true; |
Andrew Morton | 74908a0 | 2009-09-17 17:47:12 -0700 | [diff] [blame] | 699 | #ifdef CONFIG_SECURITY_SELINUX |
Tetsuo Handa | 2edeaa3 | 2011-02-07 13:36:10 +0000 | [diff] [blame] | 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) { |
Andrew Morton | 74908a0 | 2009-09-17 17:47:12 -0700 | [diff] [blame] | 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 | } |
Randy Dunlap | 764db03 | 2009-09-18 11:06:47 -0700 | [diff] [blame] | 714 | EXPORT_SYMBOL(creds_are_invalid); |
Andrew Morton | 74908a0 | 2009-09-17 17:47:12 -0700 | [diff] [blame] | 715 | |
David Howells | e0e8173 | 2009-09-02 09:13:40 +0100 | [diff] [blame] | 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", |
Eric W. Biederman | c9235f4 | 2012-04-23 17:06:34 -0700 | [diff] [blame] | 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)); |
David Howells | e0e8173 | 2009-09-02 09:13:40 +0100 | [diff] [blame] | 737 | printk(KERN_ERR "CRED: ->*gid = { %d,%d,%d,%d }\n", |
Eric W. Biederman | c9235f4 | 2012-04-23 17:06:34 -0700 | [diff] [blame] | 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)); |
David Howells | e0e8173 | 2009-09-02 09:13:40 +0100 | [diff] [blame] | 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 /* CONFIG_DEBUG_CREDENTIALS */ |