Commit | Line | Data |
---|---|---|
973c9f4f | 1 | /* Manage a process's keyrings |
1da177e4 | 2 | * |
69664cf1 | 3 | * Copyright (C) 2004-2005, 2008 Red Hat, Inc. All Rights Reserved. |
1da177e4 LT |
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 License | |
8 | * as published by the Free Software Foundation; either version | |
9 | * 2 of the License, or (at your option) any later version. | |
10 | */ | |
11 | ||
12 | #include <linux/module.h> | |
13 | #include <linux/init.h> | |
14 | #include <linux/sched.h> | |
8703e8a4 | 15 | #include <linux/sched/user.h> |
1da177e4 LT |
16 | #include <linux/keyctl.h> |
17 | #include <linux/fs.h> | |
18 | #include <linux/err.h> | |
bb003079 | 19 | #include <linux/mutex.h> |
ee18d64c | 20 | #include <linux/security.h> |
1d1e9756 | 21 | #include <linux/user_namespace.h> |
7c0f6ba6 | 22 | #include <linux/uaccess.h> |
1da177e4 LT |
23 | #include "internal.h" |
24 | ||
973c9f4f | 25 | /* Session keyring create vs join semaphore */ |
bb003079 | 26 | static DEFINE_MUTEX(key_session_mutex); |
1da177e4 | 27 | |
973c9f4f | 28 | /* User keyring creation semaphore */ |
69664cf1 DH |
29 | static DEFINE_MUTEX(key_user_keyring_mutex); |
30 | ||
973c9f4f | 31 | /* The root user's tracking struct */ |
1da177e4 | 32 | struct key_user root_key_user = { |
ddb99e11 | 33 | .usage = REFCOUNT_INIT(3), |
76181c13 | 34 | .cons_lock = __MUTEX_INITIALIZER(root_key_user.cons_lock), |
6cfd76a2 | 35 | .lock = __SPIN_LOCK_UNLOCKED(root_key_user.lock), |
1da177e4 LT |
36 | .nkeys = ATOMIC_INIT(2), |
37 | .nikeys = ATOMIC_INIT(2), | |
9a56c2db | 38 | .uid = GLOBAL_ROOT_UID, |
1da177e4 LT |
39 | }; |
40 | ||
1da177e4 | 41 | /* |
973c9f4f | 42 | * Install the user and user session keyrings for the current process's UID. |
1da177e4 | 43 | */ |
8bbf4976 | 44 | int install_user_keyrings(void) |
1da177e4 | 45 | { |
d84f4f99 DH |
46 | struct user_struct *user; |
47 | const struct cred *cred; | |
1da177e4 | 48 | struct key *uid_keyring, *session_keyring; |
96b5c8fe | 49 | key_perm_t user_keyring_perm; |
1da177e4 LT |
50 | char buf[20]; |
51 | int ret; | |
9a56c2db | 52 | uid_t uid; |
1da177e4 | 53 | |
96b5c8fe | 54 | user_keyring_perm = (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_ALL; |
d84f4f99 DH |
55 | cred = current_cred(); |
56 | user = cred->user; | |
9a56c2db | 57 | uid = from_kuid(cred->user_ns, user->uid); |
d84f4f99 | 58 | |
9a56c2db | 59 | kenter("%p{%u}", user, uid); |
1da177e4 | 60 | |
0da9dfdd | 61 | if (user->uid_keyring && user->session_keyring) { |
69664cf1 DH |
62 | kleave(" = 0 [exist]"); |
63 | return 0; | |
1da177e4 LT |
64 | } |
65 | ||
69664cf1 DH |
66 | mutex_lock(&key_user_keyring_mutex); |
67 | ret = 0; | |
1da177e4 | 68 | |
69664cf1 DH |
69 | if (!user->uid_keyring) { |
70 | /* get the UID-specific keyring | |
71 | * - there may be one in existence already as it may have been | |
72 | * pinned by a session, but the user_struct pointing to it | |
73 | * may have been destroyed by setuid */ | |
9a56c2db | 74 | sprintf(buf, "_uid.%u", uid); |
69664cf1 DH |
75 | |
76 | uid_keyring = find_keyring_by_name(buf, true); | |
77 | if (IS_ERR(uid_keyring)) { | |
9a56c2db | 78 | uid_keyring = keyring_alloc(buf, user->uid, INVALID_GID, |
96b5c8fe | 79 | cred, user_keyring_perm, |
237bbd29 EB |
80 | KEY_ALLOC_UID_KEYRING | |
81 | KEY_ALLOC_IN_QUOTA, | |
5ac7eace | 82 | NULL, NULL); |
69664cf1 DH |
83 | if (IS_ERR(uid_keyring)) { |
84 | ret = PTR_ERR(uid_keyring); | |
85 | goto error; | |
86 | } | |
87 | } | |
88 | ||
89 | /* get a default session keyring (which might also exist | |
90 | * already) */ | |
9a56c2db | 91 | sprintf(buf, "_uid_ses.%u", uid); |
69664cf1 DH |
92 | |
93 | session_keyring = find_keyring_by_name(buf, true); | |
94 | if (IS_ERR(session_keyring)) { | |
95 | session_keyring = | |
9a56c2db | 96 | keyring_alloc(buf, user->uid, INVALID_GID, |
96b5c8fe | 97 | cred, user_keyring_perm, |
237bbd29 EB |
98 | KEY_ALLOC_UID_KEYRING | |
99 | KEY_ALLOC_IN_QUOTA, | |
5ac7eace | 100 | NULL, NULL); |
69664cf1 DH |
101 | if (IS_ERR(session_keyring)) { |
102 | ret = PTR_ERR(session_keyring); | |
103 | goto error_release; | |
104 | } | |
105 | ||
106 | /* we install a link from the user session keyring to | |
107 | * the user keyring */ | |
108 | ret = key_link(session_keyring, uid_keyring); | |
109 | if (ret < 0) | |
110 | goto error_release_both; | |
111 | } | |
112 | ||
113 | /* install the keyrings */ | |
114 | user->uid_keyring = uid_keyring; | |
115 | user->session_keyring = session_keyring; | |
1da177e4 LT |
116 | } |
117 | ||
69664cf1 DH |
118 | mutex_unlock(&key_user_keyring_mutex); |
119 | kleave(" = 0"); | |
120 | return 0; | |
1da177e4 | 121 | |
69664cf1 DH |
122 | error_release_both: |
123 | key_put(session_keyring); | |
124 | error_release: | |
125 | key_put(uid_keyring); | |
664cceb0 | 126 | error: |
69664cf1 DH |
127 | mutex_unlock(&key_user_keyring_mutex); |
128 | kleave(" = %d", ret); | |
1da177e4 | 129 | return ret; |
69664cf1 | 130 | } |
1da177e4 | 131 | |
1da177e4 | 132 | /* |
c9f838d1 EB |
133 | * Install a thread keyring to the given credentials struct if it didn't have |
134 | * one already. This is allowed to overrun the quota. | |
135 | * | |
136 | * Return: 0 if a thread keyring is now present; -errno on failure. | |
1da177e4 | 137 | */ |
d84f4f99 | 138 | int install_thread_keyring_to_cred(struct cred *new) |
1da177e4 | 139 | { |
d84f4f99 | 140 | struct key *keyring; |
1da177e4 | 141 | |
c9f838d1 EB |
142 | if (new->thread_keyring) |
143 | return 0; | |
144 | ||
d84f4f99 | 145 | keyring = keyring_alloc("_tid", new->uid, new->gid, new, |
96b5c8fe | 146 | KEY_POS_ALL | KEY_USR_VIEW, |
5ac7eace DH |
147 | KEY_ALLOC_QUOTA_OVERRUN, |
148 | NULL, NULL); | |
d84f4f99 DH |
149 | if (IS_ERR(keyring)) |
150 | return PTR_ERR(keyring); | |
1da177e4 | 151 | |
d84f4f99 DH |
152 | new->thread_keyring = keyring; |
153 | return 0; | |
154 | } | |
1da177e4 | 155 | |
1da177e4 | 156 | /* |
c9f838d1 EB |
157 | * Install a thread keyring to the current task if it didn't have one already. |
158 | * | |
159 | * Return: 0 if a thread keyring is now present; -errno on failure. | |
1da177e4 | 160 | */ |
d84f4f99 | 161 | static int install_thread_keyring(void) |
1da177e4 | 162 | { |
d84f4f99 | 163 | struct cred *new; |
1da177e4 LT |
164 | int ret; |
165 | ||
d84f4f99 DH |
166 | new = prepare_creds(); |
167 | if (!new) | |
168 | return -ENOMEM; | |
1da177e4 | 169 | |
d84f4f99 DH |
170 | ret = install_thread_keyring_to_cred(new); |
171 | if (ret < 0) { | |
172 | abort_creds(new); | |
173 | return ret; | |
1da177e4 LT |
174 | } |
175 | ||
d84f4f99 DH |
176 | return commit_creds(new); |
177 | } | |
1da177e4 | 178 | |
d84f4f99 | 179 | /* |
c9f838d1 EB |
180 | * Install a process keyring to the given credentials struct if it didn't have |
181 | * one already. This is allowed to overrun the quota. | |
973c9f4f | 182 | * |
c9f838d1 | 183 | * Return: 0 if a process keyring is now present; -errno on failure. |
d84f4f99 DH |
184 | */ |
185 | int install_process_keyring_to_cred(struct cred *new) | |
186 | { | |
187 | struct key *keyring; | |
1da177e4 | 188 | |
3a50597d | 189 | if (new->process_keyring) |
c9f838d1 | 190 | return 0; |
d84f4f99 | 191 | |
96b5c8fe DH |
192 | keyring = keyring_alloc("_pid", new->uid, new->gid, new, |
193 | KEY_POS_ALL | KEY_USR_VIEW, | |
5ac7eace DH |
194 | KEY_ALLOC_QUOTA_OVERRUN, |
195 | NULL, NULL); | |
d84f4f99 DH |
196 | if (IS_ERR(keyring)) |
197 | return PTR_ERR(keyring); | |
198 | ||
3a50597d DH |
199 | new->process_keyring = keyring; |
200 | return 0; | |
d84f4f99 | 201 | } |
1da177e4 | 202 | |
1da177e4 | 203 | /* |
c9f838d1 | 204 | * Install a process keyring to the current task if it didn't have one already. |
973c9f4f | 205 | * |
c9f838d1 | 206 | * Return: 0 if a process keyring is now present; -errno on failure. |
1da177e4 | 207 | */ |
d84f4f99 | 208 | static int install_process_keyring(void) |
1da177e4 | 209 | { |
d84f4f99 | 210 | struct cred *new; |
1da177e4 LT |
211 | int ret; |
212 | ||
d84f4f99 DH |
213 | new = prepare_creds(); |
214 | if (!new) | |
215 | return -ENOMEM; | |
1da177e4 | 216 | |
d84f4f99 DH |
217 | ret = install_process_keyring_to_cred(new); |
218 | if (ret < 0) { | |
219 | abort_creds(new); | |
c9f838d1 | 220 | return ret; |
1da177e4 LT |
221 | } |
222 | ||
d84f4f99 DH |
223 | return commit_creds(new); |
224 | } | |
1da177e4 | 225 | |
1da177e4 | 226 | /* |
c9f838d1 EB |
227 | * Install the given keyring as the session keyring of the given credentials |
228 | * struct, replacing the existing one if any. If the given keyring is NULL, | |
229 | * then install a new anonymous session keyring. | |
230 | * | |
231 | * Return: 0 on success; -errno on failure. | |
1da177e4 | 232 | */ |
685bfd2c | 233 | int install_session_keyring_to_cred(struct cred *cred, struct key *keyring) |
1da177e4 | 234 | { |
7e047ef5 | 235 | unsigned long flags; |
1da177e4 | 236 | struct key *old; |
1a26feb9 DH |
237 | |
238 | might_sleep(); | |
1da177e4 LT |
239 | |
240 | /* create an empty session keyring */ | |
241 | if (!keyring) { | |
7e047ef5 | 242 | flags = KEY_ALLOC_QUOTA_OVERRUN; |
3a50597d | 243 | if (cred->session_keyring) |
7e047ef5 DH |
244 | flags = KEY_ALLOC_IN_QUOTA; |
245 | ||
96b5c8fe DH |
246 | keyring = keyring_alloc("_ses", cred->uid, cred->gid, cred, |
247 | KEY_POS_ALL | KEY_USR_VIEW | KEY_USR_READ, | |
5ac7eace | 248 | flags, NULL, NULL); |
1a26feb9 DH |
249 | if (IS_ERR(keyring)) |
250 | return PTR_ERR(keyring); | |
d84f4f99 | 251 | } else { |
ccc3e6d9 | 252 | __key_get(keyring); |
1da177e4 LT |
253 | } |
254 | ||
255 | /* install the keyring */ | |
3a50597d DH |
256 | old = cred->session_keyring; |
257 | rcu_assign_pointer(cred->session_keyring, keyring); | |
258 | ||
259 | if (old) | |
1a26feb9 | 260 | key_put(old); |
1da177e4 | 261 | |
1a26feb9 | 262 | return 0; |
d84f4f99 | 263 | } |
1da177e4 | 264 | |
1da177e4 | 265 | /* |
c9f838d1 EB |
266 | * Install the given keyring as the session keyring of the current task, |
267 | * replacing the existing one if any. If the given keyring is NULL, then | |
268 | * install a new anonymous session keyring. | |
269 | * | |
270 | * Return: 0 on success; -errno on failure. | |
1da177e4 | 271 | */ |
d84f4f99 | 272 | static int install_session_keyring(struct key *keyring) |
1da177e4 | 273 | { |
d84f4f99 DH |
274 | struct cred *new; |
275 | int ret; | |
1da177e4 | 276 | |
d84f4f99 DH |
277 | new = prepare_creds(); |
278 | if (!new) | |
279 | return -ENOMEM; | |
1da177e4 | 280 | |
99599537 | 281 | ret = install_session_keyring_to_cred(new, keyring); |
d84f4f99 DH |
282 | if (ret < 0) { |
283 | abort_creds(new); | |
284 | return ret; | |
285 | } | |
1da177e4 | 286 | |
d84f4f99 DH |
287 | return commit_creds(new); |
288 | } | |
1da177e4 | 289 | |
1da177e4 | 290 | /* |
973c9f4f | 291 | * Handle the fsuid changing. |
1da177e4 LT |
292 | */ |
293 | void key_fsuid_changed(struct task_struct *tsk) | |
294 | { | |
295 | /* update the ownership of the thread keyring */ | |
b6dff3ec DH |
296 | BUG_ON(!tsk->cred); |
297 | if (tsk->cred->thread_keyring) { | |
298 | down_write(&tsk->cred->thread_keyring->sem); | |
299 | tsk->cred->thread_keyring->uid = tsk->cred->fsuid; | |
300 | up_write(&tsk->cred->thread_keyring->sem); | |
1da177e4 | 301 | } |
a8b17ed0 | 302 | } |
1da177e4 | 303 | |
1da177e4 | 304 | /* |
973c9f4f | 305 | * Handle the fsgid changing. |
1da177e4 LT |
306 | */ |
307 | void key_fsgid_changed(struct task_struct *tsk) | |
308 | { | |
309 | /* update the ownership of the thread keyring */ | |
b6dff3ec DH |
310 | BUG_ON(!tsk->cred); |
311 | if (tsk->cred->thread_keyring) { | |
312 | down_write(&tsk->cred->thread_keyring->sem); | |
313 | tsk->cred->thread_keyring->gid = tsk->cred->fsgid; | |
314 | up_write(&tsk->cred->thread_keyring->sem); | |
1da177e4 | 315 | } |
a8b17ed0 | 316 | } |
1da177e4 | 317 | |
1da177e4 | 318 | /* |
973c9f4f DH |
319 | * Search the process keyrings attached to the supplied cred for the first |
320 | * matching key. | |
321 | * | |
322 | * The search criteria are the type and the match function. The description is | |
323 | * given to the match function as a parameter, but doesn't otherwise influence | |
324 | * the search. Typically the match function will compare the description | |
325 | * parameter to the key's description. | |
326 | * | |
327 | * This can only search keyrings that grant Search permission to the supplied | |
328 | * credentials. Keyrings linked to searched keyrings will also be searched if | |
329 | * they grant Search permission too. Keys can only be found if they grant | |
330 | * Search permission to the credentials. | |
331 | * | |
332 | * Returns a pointer to the key with the key usage count incremented if | |
333 | * successful, -EAGAIN if we didn't find any matching key or -ENOKEY if we only | |
334 | * matched negative keys. | |
335 | * | |
336 | * In the case of a successful return, the possession attribute is set on the | |
337 | * returned key reference. | |
1da177e4 | 338 | */ |
4bdf0bc3 | 339 | key_ref_t search_my_process_keyrings(struct keyring_search_context *ctx) |
1da177e4 | 340 | { |
b5f545c8 | 341 | key_ref_t key_ref, ret, err; |
1da177e4 LT |
342 | |
343 | /* we want to return -EAGAIN or -ENOKEY if any of the keyrings were | |
344 | * searchable, but we failed to find a key or we found a negative key; | |
345 | * otherwise we want to return a sample error (probably -EACCES) if | |
346 | * none of the keyrings were searchable | |
347 | * | |
348 | * in terms of priority: success > -ENOKEY > -EAGAIN > other error | |
349 | */ | |
664cceb0 | 350 | key_ref = NULL; |
1da177e4 LT |
351 | ret = NULL; |
352 | err = ERR_PTR(-EAGAIN); | |
353 | ||
354 | /* search the thread keyring first */ | |
4bdf0bc3 | 355 | if (ctx->cred->thread_keyring) { |
664cceb0 | 356 | key_ref = keyring_search_aux( |
4bdf0bc3 | 357 | make_key_ref(ctx->cred->thread_keyring, 1), ctx); |
664cceb0 | 358 | if (!IS_ERR(key_ref)) |
1da177e4 LT |
359 | goto found; |
360 | ||
664cceb0 | 361 | switch (PTR_ERR(key_ref)) { |
1da177e4 | 362 | case -EAGAIN: /* no key */ |
1da177e4 | 363 | case -ENOKEY: /* negative key */ |
664cceb0 | 364 | ret = key_ref; |
1da177e4 LT |
365 | break; |
366 | default: | |
664cceb0 | 367 | err = key_ref; |
1da177e4 LT |
368 | break; |
369 | } | |
370 | } | |
371 | ||
372 | /* search the process keyring second */ | |
4bdf0bc3 | 373 | if (ctx->cred->process_keyring) { |
664cceb0 | 374 | key_ref = keyring_search_aux( |
4bdf0bc3 | 375 | make_key_ref(ctx->cred->process_keyring, 1), ctx); |
664cceb0 | 376 | if (!IS_ERR(key_ref)) |
1da177e4 LT |
377 | goto found; |
378 | ||
664cceb0 | 379 | switch (PTR_ERR(key_ref)) { |
1da177e4 | 380 | case -EAGAIN: /* no key */ |
fe9453a1 DH |
381 | if (ret) |
382 | break; | |
1da177e4 | 383 | case -ENOKEY: /* negative key */ |
664cceb0 | 384 | ret = key_ref; |
1da177e4 LT |
385 | break; |
386 | default: | |
664cceb0 | 387 | err = key_ref; |
1da177e4 LT |
388 | break; |
389 | } | |
390 | } | |
391 | ||
3e30148c | 392 | /* search the session keyring */ |
4bdf0bc3 | 393 | if (ctx->cred->session_keyring) { |
8589b4e0 | 394 | rcu_read_lock(); |
664cceb0 | 395 | key_ref = keyring_search_aux( |
4bdf0bc3 DH |
396 | make_key_ref(rcu_dereference(ctx->cred->session_keyring), 1), |
397 | ctx); | |
8589b4e0 | 398 | rcu_read_unlock(); |
3e30148c | 399 | |
664cceb0 | 400 | if (!IS_ERR(key_ref)) |
3e30148c DH |
401 | goto found; |
402 | ||
664cceb0 | 403 | switch (PTR_ERR(key_ref)) { |
3e30148c DH |
404 | case -EAGAIN: /* no key */ |
405 | if (ret) | |
406 | break; | |
407 | case -ENOKEY: /* negative key */ | |
664cceb0 | 408 | ret = key_ref; |
3e30148c DH |
409 | break; |
410 | default: | |
664cceb0 | 411 | err = key_ref; |
3e30148c DH |
412 | break; |
413 | } | |
b5f545c8 DH |
414 | } |
415 | /* or search the user-session keyring */ | |
4bdf0bc3 | 416 | else if (ctx->cred->user->session_keyring) { |
b5f545c8 | 417 | key_ref = keyring_search_aux( |
4bdf0bc3 DH |
418 | make_key_ref(ctx->cred->user->session_keyring, 1), |
419 | ctx); | |
664cceb0 | 420 | if (!IS_ERR(key_ref)) |
3e30148c DH |
421 | goto found; |
422 | ||
664cceb0 | 423 | switch (PTR_ERR(key_ref)) { |
3e30148c DH |
424 | case -EAGAIN: /* no key */ |
425 | if (ret) | |
426 | break; | |
427 | case -ENOKEY: /* negative key */ | |
664cceb0 | 428 | ret = key_ref; |
3e30148c DH |
429 | break; |
430 | default: | |
664cceb0 | 431 | err = key_ref; |
3e30148c DH |
432 | break; |
433 | } | |
8589b4e0 | 434 | } |
b5f545c8 | 435 | |
927942aa DH |
436 | /* no key - decide on the error we're going to go for */ |
437 | key_ref = ret ? ret : err; | |
438 | ||
439 | found: | |
440 | return key_ref; | |
441 | } | |
442 | ||
927942aa | 443 | /* |
973c9f4f DH |
444 | * Search the process keyrings attached to the supplied cred for the first |
445 | * matching key in the manner of search_my_process_keyrings(), but also search | |
446 | * the keys attached to the assumed authorisation key using its credentials if | |
447 | * one is available. | |
448 | * | |
449 | * Return same as search_my_process_keyrings(). | |
927942aa | 450 | */ |
4bdf0bc3 | 451 | key_ref_t search_process_keyrings(struct keyring_search_context *ctx) |
927942aa DH |
452 | { |
453 | struct request_key_auth *rka; | |
454 | key_ref_t key_ref, ret = ERR_PTR(-EACCES), err; | |
455 | ||
456 | might_sleep(); | |
457 | ||
4bdf0bc3 | 458 | key_ref = search_my_process_keyrings(ctx); |
927942aa DH |
459 | if (!IS_ERR(key_ref)) |
460 | goto found; | |
461 | err = key_ref; | |
462 | ||
b5f545c8 DH |
463 | /* if this process has an instantiation authorisation key, then we also |
464 | * search the keyrings of the process mentioned there | |
465 | * - we don't permit access to request_key auth keys via this method | |
466 | */ | |
4bdf0bc3 DH |
467 | if (ctx->cred->request_key_auth && |
468 | ctx->cred == current_cred() && | |
469 | ctx->index_key.type != &key_type_request_key_auth | |
b5f545c8 | 470 | ) { |
4bdf0bc3 DH |
471 | const struct cred *cred = ctx->cred; |
472 | ||
04c567d9 | 473 | /* defend against the auth key being revoked */ |
c69e8d9c | 474 | down_read(&cred->request_key_auth->sem); |
b5f545c8 | 475 | |
4bdf0bc3 | 476 | if (key_validate(ctx->cred->request_key_auth) == 0) { |
146aa8b1 | 477 | rka = ctx->cred->request_key_auth->payload.data[0]; |
b5f545c8 | 478 | |
4bdf0bc3 DH |
479 | ctx->cred = rka->cred; |
480 | key_ref = search_process_keyrings(ctx); | |
481 | ctx->cred = cred; | |
1da177e4 | 482 | |
c69e8d9c | 483 | up_read(&cred->request_key_auth->sem); |
04c567d9 DH |
484 | |
485 | if (!IS_ERR(key_ref)) | |
486 | goto found; | |
487 | ||
927942aa | 488 | ret = key_ref; |
04c567d9 | 489 | } else { |
c69e8d9c | 490 | up_read(&cred->request_key_auth->sem); |
3e30148c | 491 | } |
1da177e4 LT |
492 | } |
493 | ||
494 | /* no key - decide on the error we're going to go for */ | |
927942aa DH |
495 | if (err == ERR_PTR(-ENOKEY) || ret == ERR_PTR(-ENOKEY)) |
496 | key_ref = ERR_PTR(-ENOKEY); | |
497 | else if (err == ERR_PTR(-EACCES)) | |
498 | key_ref = ret; | |
499 | else | |
500 | key_ref = err; | |
1da177e4 | 501 | |
3e30148c | 502 | found: |
664cceb0 | 503 | return key_ref; |
a8b17ed0 | 504 | } |
1da177e4 | 505 | |
664cceb0 | 506 | /* |
973c9f4f | 507 | * See if the key we're looking at is the target key. |
664cceb0 | 508 | */ |
0c903ab6 DH |
509 | bool lookup_user_key_possessed(const struct key *key, |
510 | const struct key_match_data *match_data) | |
664cceb0 | 511 | { |
46291959 | 512 | return key == match_data->raw_data; |
a8b17ed0 | 513 | } |
664cceb0 | 514 | |
1da177e4 | 515 | /* |
973c9f4f DH |
516 | * Look up a key ID given us by userspace with a given permissions mask to get |
517 | * the key it refers to. | |
518 | * | |
519 | * Flags can be passed to request that special keyrings be created if referred | |
520 | * to directly, to permit partially constructed keys to be found and to skip | |
521 | * validity and permission checks on the found key. | |
522 | * | |
523 | * Returns a pointer to the key with an incremented usage count if successful; | |
524 | * -EINVAL if the key ID is invalid; -ENOKEY if the key ID does not correspond | |
525 | * to a key or the best found key was a negative key; -EKEYREVOKED or | |
526 | * -EKEYEXPIRED if the best found key was revoked or expired; -EACCES if the | |
527 | * found key doesn't grant the requested permit or the LSM denied access to it; | |
528 | * or -ENOMEM if a special keyring couldn't be created. | |
529 | * | |
530 | * In the case of a successful return, the possession attribute is set on the | |
531 | * returned key reference. | |
1da177e4 | 532 | */ |
5593122e | 533 | key_ref_t lookup_user_key(key_serial_t id, unsigned long lflags, |
8bbf4976 | 534 | key_perm_t perm) |
1da177e4 | 535 | { |
4bdf0bc3 | 536 | struct keyring_search_context ctx = { |
46291959 DH |
537 | .match_data.cmp = lookup_user_key_possessed, |
538 | .match_data.lookup_type = KEYRING_SEARCH_LOOKUP_DIRECT, | |
539 | .flags = KEYRING_SEARCH_NO_STATE_CHECK, | |
4bdf0bc3 | 540 | }; |
8bbf4976 | 541 | struct request_key_auth *rka; |
1da177e4 | 542 | struct key *key; |
b6dff3ec | 543 | key_ref_t key_ref, skey_ref; |
1da177e4 LT |
544 | int ret; |
545 | ||
bb952bb9 | 546 | try_again: |
4bdf0bc3 | 547 | ctx.cred = get_current_cred(); |
664cceb0 | 548 | key_ref = ERR_PTR(-ENOKEY); |
1da177e4 LT |
549 | |
550 | switch (id) { | |
551 | case KEY_SPEC_THREAD_KEYRING: | |
4bdf0bc3 | 552 | if (!ctx.cred->thread_keyring) { |
5593122e | 553 | if (!(lflags & KEY_LOOKUP_CREATE)) |
1da177e4 LT |
554 | goto error; |
555 | ||
8bbf4976 | 556 | ret = install_thread_keyring(); |
1da177e4 | 557 | if (ret < 0) { |
4d09ec0f | 558 | key_ref = ERR_PTR(ret); |
1da177e4 LT |
559 | goto error; |
560 | } | |
bb952bb9 | 561 | goto reget_creds; |
1da177e4 LT |
562 | } |
563 | ||
4bdf0bc3 | 564 | key = ctx.cred->thread_keyring; |
ccc3e6d9 | 565 | __key_get(key); |
664cceb0 | 566 | key_ref = make_key_ref(key, 1); |
1da177e4 LT |
567 | break; |
568 | ||
569 | case KEY_SPEC_PROCESS_KEYRING: | |
4bdf0bc3 | 570 | if (!ctx.cred->process_keyring) { |
5593122e | 571 | if (!(lflags & KEY_LOOKUP_CREATE)) |
1da177e4 LT |
572 | goto error; |
573 | ||
8bbf4976 | 574 | ret = install_process_keyring(); |
1da177e4 | 575 | if (ret < 0) { |
4d09ec0f | 576 | key_ref = ERR_PTR(ret); |
1da177e4 LT |
577 | goto error; |
578 | } | |
bb952bb9 | 579 | goto reget_creds; |
1da177e4 LT |
580 | } |
581 | ||
4bdf0bc3 | 582 | key = ctx.cred->process_keyring; |
ccc3e6d9 | 583 | __key_get(key); |
664cceb0 | 584 | key_ref = make_key_ref(key, 1); |
1da177e4 LT |
585 | break; |
586 | ||
587 | case KEY_SPEC_SESSION_KEYRING: | |
4bdf0bc3 | 588 | if (!ctx.cred->session_keyring) { |
1da177e4 LT |
589 | /* always install a session keyring upon access if one |
590 | * doesn't exist yet */ | |
8bbf4976 | 591 | ret = install_user_keyrings(); |
69664cf1 DH |
592 | if (ret < 0) |
593 | goto error; | |
3ecf1b4f DH |
594 | if (lflags & KEY_LOOKUP_CREATE) |
595 | ret = join_session_keyring(NULL); | |
596 | else | |
597 | ret = install_session_keyring( | |
4bdf0bc3 | 598 | ctx.cred->user->session_keyring); |
d84f4f99 | 599 | |
1da177e4 LT |
600 | if (ret < 0) |
601 | goto error; | |
bb952bb9 | 602 | goto reget_creds; |
4bdf0bc3 DH |
603 | } else if (ctx.cred->session_keyring == |
604 | ctx.cred->user->session_keyring && | |
3ecf1b4f DH |
605 | lflags & KEY_LOOKUP_CREATE) { |
606 | ret = join_session_keyring(NULL); | |
607 | if (ret < 0) | |
608 | goto error; | |
609 | goto reget_creds; | |
1da177e4 LT |
610 | } |
611 | ||
3e30148c | 612 | rcu_read_lock(); |
4bdf0bc3 | 613 | key = rcu_dereference(ctx.cred->session_keyring); |
ccc3e6d9 | 614 | __key_get(key); |
3e30148c | 615 | rcu_read_unlock(); |
664cceb0 | 616 | key_ref = make_key_ref(key, 1); |
1da177e4 LT |
617 | break; |
618 | ||
619 | case KEY_SPEC_USER_KEYRING: | |
4bdf0bc3 | 620 | if (!ctx.cred->user->uid_keyring) { |
8bbf4976 | 621 | ret = install_user_keyrings(); |
69664cf1 DH |
622 | if (ret < 0) |
623 | goto error; | |
624 | } | |
625 | ||
4bdf0bc3 | 626 | key = ctx.cred->user->uid_keyring; |
ccc3e6d9 | 627 | __key_get(key); |
664cceb0 | 628 | key_ref = make_key_ref(key, 1); |
1da177e4 LT |
629 | break; |
630 | ||
631 | case KEY_SPEC_USER_SESSION_KEYRING: | |
4bdf0bc3 | 632 | if (!ctx.cred->user->session_keyring) { |
8bbf4976 | 633 | ret = install_user_keyrings(); |
69664cf1 DH |
634 | if (ret < 0) |
635 | goto error; | |
636 | } | |
637 | ||
4bdf0bc3 | 638 | key = ctx.cred->user->session_keyring; |
ccc3e6d9 | 639 | __key_get(key); |
664cceb0 | 640 | key_ref = make_key_ref(key, 1); |
1da177e4 LT |
641 | break; |
642 | ||
643 | case KEY_SPEC_GROUP_KEYRING: | |
644 | /* group keyrings are not yet supported */ | |
4d09ec0f | 645 | key_ref = ERR_PTR(-EINVAL); |
1da177e4 LT |
646 | goto error; |
647 | ||
b5f545c8 | 648 | case KEY_SPEC_REQKEY_AUTH_KEY: |
4bdf0bc3 | 649 | key = ctx.cred->request_key_auth; |
b5f545c8 DH |
650 | if (!key) |
651 | goto error; | |
652 | ||
ccc3e6d9 | 653 | __key_get(key); |
b5f545c8 DH |
654 | key_ref = make_key_ref(key, 1); |
655 | break; | |
656 | ||
8bbf4976 | 657 | case KEY_SPEC_REQUESTOR_KEYRING: |
4bdf0bc3 | 658 | if (!ctx.cred->request_key_auth) |
8bbf4976 DH |
659 | goto error; |
660 | ||
4bdf0bc3 | 661 | down_read(&ctx.cred->request_key_auth->sem); |
f67dabbd | 662 | if (test_bit(KEY_FLAG_REVOKED, |
4bdf0bc3 | 663 | &ctx.cred->request_key_auth->flags)) { |
8bbf4976 DH |
664 | key_ref = ERR_PTR(-EKEYREVOKED); |
665 | key = NULL; | |
666 | } else { | |
146aa8b1 | 667 | rka = ctx.cred->request_key_auth->payload.data[0]; |
8bbf4976 | 668 | key = rka->dest_keyring; |
ccc3e6d9 | 669 | __key_get(key); |
8bbf4976 | 670 | } |
4bdf0bc3 | 671 | up_read(&ctx.cred->request_key_auth->sem); |
8bbf4976 DH |
672 | if (!key) |
673 | goto error; | |
674 | key_ref = make_key_ref(key, 1); | |
675 | break; | |
676 | ||
1da177e4 | 677 | default: |
664cceb0 | 678 | key_ref = ERR_PTR(-EINVAL); |
1da177e4 LT |
679 | if (id < 1) |
680 | goto error; | |
681 | ||
682 | key = key_lookup(id); | |
664cceb0 | 683 | if (IS_ERR(key)) { |
e231c2ee | 684 | key_ref = ERR_CAST(key); |
1da177e4 | 685 | goto error; |
664cceb0 DH |
686 | } |
687 | ||
688 | key_ref = make_key_ref(key, 0); | |
689 | ||
690 | /* check to see if we possess the key */ | |
4bdf0bc3 DH |
691 | ctx.index_key.type = key->type; |
692 | ctx.index_key.description = key->description; | |
693 | ctx.index_key.desc_len = strlen(key->description); | |
46291959 | 694 | ctx.match_data.raw_data = key; |
4bdf0bc3 DH |
695 | kdebug("check possessed"); |
696 | skey_ref = search_process_keyrings(&ctx); | |
697 | kdebug("possessed=%p", skey_ref); | |
664cceb0 DH |
698 | |
699 | if (!IS_ERR(skey_ref)) { | |
700 | key_put(key); | |
701 | key_ref = skey_ref; | |
702 | } | |
703 | ||
1da177e4 LT |
704 | break; |
705 | } | |
706 | ||
5593122e DH |
707 | /* unlink does not use the nominated key in any way, so can skip all |
708 | * the permission checks as it is only concerned with the keyring */ | |
709 | if (lflags & KEY_LOOKUP_FOR_UNLINK) { | |
710 | ret = 0; | |
711 | goto error; | |
712 | } | |
713 | ||
714 | if (!(lflags & KEY_LOOKUP_PARTIAL)) { | |
76181c13 DH |
715 | ret = wait_for_key_construction(key, true); |
716 | switch (ret) { | |
717 | case -ERESTARTSYS: | |
718 | goto invalid_key; | |
719 | default: | |
720 | if (perm) | |
721 | goto invalid_key; | |
722 | case 0: | |
723 | break; | |
724 | } | |
725 | } else if (perm) { | |
1da177e4 LT |
726 | ret = key_validate(key); |
727 | if (ret < 0) | |
728 | goto invalid_key; | |
729 | } | |
730 | ||
731 | ret = -EIO; | |
5593122e | 732 | if (!(lflags & KEY_LOOKUP_PARTIAL) && |
363b02da | 733 | key_read_state(key) == KEY_IS_UNINSTANTIATED) |
1da177e4 LT |
734 | goto invalid_key; |
735 | ||
3e30148c | 736 | /* check the permissions */ |
4bdf0bc3 | 737 | ret = key_task_permission(key_ref, ctx.cred, perm); |
29db9190 | 738 | if (ret < 0) |
1da177e4 LT |
739 | goto invalid_key; |
740 | ||
074d5898 | 741 | key->last_used_at = ktime_get_real_seconds(); |
31d5a79d | 742 | |
664cceb0 | 743 | error: |
4bdf0bc3 | 744 | put_cred(ctx.cred); |
664cceb0 | 745 | return key_ref; |
1da177e4 | 746 | |
664cceb0 DH |
747 | invalid_key: |
748 | key_ref_put(key_ref); | |
749 | key_ref = ERR_PTR(ret); | |
1da177e4 LT |
750 | goto error; |
751 | ||
bb952bb9 DH |
752 | /* if we attempted to install a keyring, then it may have caused new |
753 | * creds to be installed */ | |
754 | reget_creds: | |
4bdf0bc3 | 755 | put_cred(ctx.cred); |
bb952bb9 | 756 | goto try_again; |
a8b17ed0 | 757 | } |
bb952bb9 | 758 | |
1da177e4 | 759 | /* |
973c9f4f DH |
760 | * Join the named keyring as the session keyring if possible else attempt to |
761 | * create a new one of that name and join that. | |
762 | * | |
763 | * If the name is NULL, an empty anonymous keyring will be installed as the | |
764 | * session keyring. | |
765 | * | |
766 | * Named session keyrings are joined with a semaphore held to prevent the | |
767 | * keyrings from going away whilst the attempt is made to going them and also | |
768 | * to prevent a race in creating compatible session keyrings. | |
1da177e4 LT |
769 | */ |
770 | long join_session_keyring(const char *name) | |
771 | { | |
d84f4f99 DH |
772 | const struct cred *old; |
773 | struct cred *new; | |
1da177e4 | 774 | struct key *keyring; |
d84f4f99 DH |
775 | long ret, serial; |
776 | ||
d84f4f99 DH |
777 | new = prepare_creds(); |
778 | if (!new) | |
779 | return -ENOMEM; | |
780 | old = current_cred(); | |
1da177e4 LT |
781 | |
782 | /* if no name is provided, install an anonymous keyring */ | |
783 | if (!name) { | |
d84f4f99 | 784 | ret = install_session_keyring_to_cred(new, NULL); |
1da177e4 LT |
785 | if (ret < 0) |
786 | goto error; | |
787 | ||
3a50597d | 788 | serial = new->session_keyring->serial; |
d84f4f99 DH |
789 | ret = commit_creds(new); |
790 | if (ret == 0) | |
791 | ret = serial; | |
792 | goto okay; | |
1da177e4 LT |
793 | } |
794 | ||
795 | /* allow the user to join or create a named keyring */ | |
bb003079 | 796 | mutex_lock(&key_session_mutex); |
1da177e4 LT |
797 | |
798 | /* look for an existing keyring of this name */ | |
69664cf1 | 799 | keyring = find_keyring_by_name(name, false); |
1da177e4 LT |
800 | if (PTR_ERR(keyring) == -ENOKEY) { |
801 | /* not found - try and create a new one */ | |
96b5c8fe DH |
802 | keyring = keyring_alloc( |
803 | name, old->uid, old->gid, old, | |
804 | KEY_POS_ALL | KEY_USR_VIEW | KEY_USR_READ | KEY_USR_LINK, | |
5ac7eace | 805 | KEY_ALLOC_IN_QUOTA, NULL, NULL); |
1da177e4 LT |
806 | if (IS_ERR(keyring)) { |
807 | ret = PTR_ERR(keyring); | |
bcf945d3 | 808 | goto error2; |
1da177e4 | 809 | } |
d84f4f99 | 810 | } else if (IS_ERR(keyring)) { |
1da177e4 LT |
811 | ret = PTR_ERR(keyring); |
812 | goto error2; | |
3a50597d DH |
813 | } else if (keyring == new->session_keyring) { |
814 | ret = 0; | |
d636bd9f | 815 | goto error3; |
1da177e4 LT |
816 | } |
817 | ||
818 | /* we've got a keyring - now to install it */ | |
d84f4f99 | 819 | ret = install_session_keyring_to_cred(new, keyring); |
1da177e4 | 820 | if (ret < 0) |
d636bd9f | 821 | goto error3; |
1da177e4 | 822 | |
d84f4f99 DH |
823 | commit_creds(new); |
824 | mutex_unlock(&key_session_mutex); | |
825 | ||
1da177e4 LT |
826 | ret = keyring->serial; |
827 | key_put(keyring); | |
d84f4f99 DH |
828 | okay: |
829 | return ret; | |
1da177e4 | 830 | |
d636bd9f EB |
831 | error3: |
832 | key_put(keyring); | |
664cceb0 | 833 | error2: |
bb003079 | 834 | mutex_unlock(&key_session_mutex); |
664cceb0 | 835 | error: |
d84f4f99 | 836 | abort_creds(new); |
1da177e4 | 837 | return ret; |
d84f4f99 | 838 | } |
ee18d64c DH |
839 | |
840 | /* | |
973c9f4f DH |
841 | * Replace a process's session keyring on behalf of one of its children when |
842 | * the target process is about to resume userspace execution. | |
ee18d64c | 843 | */ |
67d12145 | 844 | void key_change_session_keyring(struct callback_head *twork) |
ee18d64c | 845 | { |
413cd3d9 | 846 | const struct cred *old = current_cred(); |
67d12145 | 847 | struct cred *new = container_of(twork, struct cred, rcu); |
ee18d64c | 848 | |
413cd3d9 ON |
849 | if (unlikely(current->flags & PF_EXITING)) { |
850 | put_cred(new); | |
ee18d64c | 851 | return; |
413cd3d9 | 852 | } |
ee18d64c | 853 | |
ee18d64c DH |
854 | new-> uid = old-> uid; |
855 | new-> euid = old-> euid; | |
856 | new-> suid = old-> suid; | |
857 | new->fsuid = old->fsuid; | |
858 | new-> gid = old-> gid; | |
859 | new-> egid = old-> egid; | |
860 | new-> sgid = old-> sgid; | |
861 | new->fsgid = old->fsgid; | |
862 | new->user = get_uid(old->user); | |
ba0e3427 | 863 | new->user_ns = get_user_ns(old->user_ns); |
ee18d64c DH |
864 | new->group_info = get_group_info(old->group_info); |
865 | ||
866 | new->securebits = old->securebits; | |
867 | new->cap_inheritable = old->cap_inheritable; | |
868 | new->cap_permitted = old->cap_permitted; | |
869 | new->cap_effective = old->cap_effective; | |
58319057 | 870 | new->cap_ambient = old->cap_ambient; |
ee18d64c DH |
871 | new->cap_bset = old->cap_bset; |
872 | ||
873 | new->jit_keyring = old->jit_keyring; | |
874 | new->thread_keyring = key_get(old->thread_keyring); | |
3a50597d | 875 | new->process_keyring = key_get(old->process_keyring); |
ee18d64c DH |
876 | |
877 | security_transfer_creds(new, old); | |
878 | ||
879 | commit_creds(new); | |
880 | } | |
c124bde2 MZ |
881 | |
882 | /* | |
883 | * Make sure that root's user and user-session keyrings exist. | |
884 | */ | |
885 | static int __init init_root_keyring(void) | |
886 | { | |
887 | return install_user_keyrings(); | |
888 | } | |
889 | ||
890 | late_initcall(init_root_keyring); |