1 /* process_keys.c: management of a process's keyrings
2 *
3 * Copyright (C) 2004 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 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>
15 #include <linux/slab.h>
16 #include <linux/keyctl.h>
17 #include <linux/fs.h>
18 #include <linux/err.h>
19 #include <asm/uaccess.h>
20 #include "internal.h"
21
22 /* session keyring create vs join semaphore */
23 static DECLARE_MUTEX(key_session_sem);
24
25 /* the root user's tracking struct */
26 struct key_user root_key_user = {
27 .usage = ATOMIC_INIT(3),
28 .consq = LIST_HEAD_INIT(root_key_user.consq),
29 .lock = SPIN_LOCK_UNLOCKED,
30 .nkeys = ATOMIC_INIT(2),
31 .nikeys = ATOMIC_INIT(2),
32 .uid = 0,
33 };
34
35 /* the root user's UID keyring */
36 struct key root_user_keyring = {
37 .usage = ATOMIC_INIT(1),
38 .serial = 2,
39 .type = &key_type_keyring,
40 .user = &root_key_user,
41 .lock = RW_LOCK_UNLOCKED,
42 .sem = __RWSEM_INITIALIZER(root_user_keyring.sem),
43 .perm = KEY_USR_ALL,
44 .flags = KEY_FLAG_INSTANTIATED,
45 .description = "_uid.0",
46 #ifdef KEY_DEBUGGING
47 .magic = KEY_DEBUG_MAGIC,
48 #endif
49 };
50
51 /* the root user's default session keyring */
52 struct key root_session_keyring = {
53 .usage = ATOMIC_INIT(1),
54 .serial = 1,
55 .type = &key_type_keyring,
56 .user = &root_key_user,
57 .lock = RW_LOCK_UNLOCKED,
58 .sem = __RWSEM_INITIALIZER(root_session_keyring.sem),
59 .perm = KEY_USR_ALL,
60 .flags = KEY_FLAG_INSTANTIATED,
61 .description = "_uid_ses.0",
62 #ifdef KEY_DEBUGGING
63 .magic = KEY_DEBUG_MAGIC,
64 #endif
65 };
66
67 /*****************************************************************************/
68 /*
69 * allocate the keyrings to be associated with a UID
70 */
71 int alloc_uid_keyring(struct user_struct *user)
72 {
73 struct key *uid_keyring, *session_keyring;
74 char buf[20];
75 int ret;
76
77 /* concoct a default session keyring */
78 sprintf(buf, "_uid_ses.%u", user->uid);
79
80 session_keyring = keyring_alloc(buf, user->uid, (gid_t) -1, 0, NULL);
81 if (IS_ERR(session_keyring)) {
82 ret = PTR_ERR(session_keyring);
83 goto error;
84 }
85
86 /* and a UID specific keyring, pointed to by the default session
87 * keyring */
88 sprintf(buf, "_uid.%u", user->uid);
89
90 uid_keyring = keyring_alloc(buf, user->uid, (gid_t) -1, 0,
91 session_keyring);
92 if (IS_ERR(uid_keyring)) {
93 key_put(session_keyring);
94 ret = PTR_ERR(uid_keyring);
95 goto error;
96 }
97
98 /* install the keyrings */
99 user->uid_keyring = uid_keyring;
100 user->session_keyring = session_keyring;
101 ret = 0;
102
103 error:
104 return ret;
105
106 } /* end alloc_uid_keyring() */
107
108 /*****************************************************************************/
109 /*
110 * deal with the UID changing
111 */
112 void switch_uid_keyring(struct user_struct *new_user)
113 {
114 #if 0 /* do nothing for now */
115 struct key *old;
116
117 /* switch to the new user's session keyring if we were running under
118 * root's default session keyring */
119 if (new_user->uid != 0 &&
120 current->session_keyring == &root_session_keyring
121 ) {
122 atomic_inc(&new_user->session_keyring->usage);
123
124 task_lock(current);
125 old = current->session_keyring;
126 current->session_keyring = new_user->session_keyring;
127 task_unlock(current);
128
129 key_put(old);
130 }
131 #endif
132
133 } /* end switch_uid_keyring() */
134
135 /*****************************************************************************/
136 /*
137 * install a fresh thread keyring, discarding the old one
138 */
139 int install_thread_keyring(struct task_struct *tsk)
140 {
141 struct key *keyring, *old;
142 char buf[20];
143 int ret;
144
145 sprintf(buf, "_tid.%u", tsk->pid);
146
147 keyring = keyring_alloc(buf, tsk->uid, tsk->gid, 1, NULL);
148 if (IS_ERR(keyring)) {
149 ret = PTR_ERR(keyring);
150 goto error;
151 }
152
153 task_lock(tsk);
154 old = tsk->thread_keyring;
155 tsk->thread_keyring = keyring;
156 task_unlock(tsk);
157
158 ret = 0;
159
160 key_put(old);
161 error:
162 return ret;
163
164 } /* end install_thread_keyring() */
165
166 /*****************************************************************************/
167 /*
168 * install a fresh process keyring, discarding the old one
169 */
170 static int install_process_keyring(struct task_struct *tsk)
171 {
172 struct key *keyring, *old;
173 char buf[20];
174 int ret;
175
176 sprintf(buf, "_pid.%u", tsk->tgid);
177
178 keyring = keyring_alloc(buf, tsk->uid, tsk->gid, 1, NULL);
179 if (IS_ERR(keyring)) {
180 ret = PTR_ERR(keyring);
181 goto error;
182 }
183
184 task_lock(tsk);
185 old = tsk->process_keyring;
186 tsk->process_keyring = keyring;
187 task_unlock(tsk);
188
189 ret = 0;
190
191 key_put(old);
192 error:
193 return ret;
194
195 } /* end install_process_keyring() */
196
197 /*****************************************************************************/
198 /*
199 * install a session keyring, discarding the old one
200 * - if a keyring is not supplied, an empty one is invented
201 */
202 static int install_session_keyring(struct task_struct *tsk,
203 struct key *keyring)
204 {
205 struct key *old;
206 char buf[20];
207 int ret;
208
209 /* create an empty session keyring */
210 if (!keyring) {
211 sprintf(buf, "_ses.%u", tsk->tgid);
212
213 keyring = keyring_alloc(buf, tsk->uid, tsk->gid, 1, NULL);
214 if (IS_ERR(keyring)) {
215 ret = PTR_ERR(keyring);
216 goto error;
217 }
218 }
219 else {
220 atomic_inc(&keyring->usage);
221 }
222
223 /* install the keyring */
224 task_lock(tsk);
225 old = tsk->session_keyring;
226 tsk->session_keyring = keyring;
227 task_unlock(tsk);
228
229 ret = 0;
230
231 key_put(old);
232 error:
233 return ret;
234
235 } /* end install_session_keyring() */
236
237 /*****************************************************************************/
238 /*
239 * copy the keys for fork
240 */
241 int copy_keys(unsigned long clone_flags, struct task_struct *tsk)
242 {
243 int ret = 0;
244
245 key_check(tsk->session_keyring);
246 key_check(tsk->process_keyring);
247 key_check(tsk->thread_keyring);
248
249 if (tsk->session_keyring)
250 atomic_inc(&tsk->session_keyring->usage);
251
252 if (tsk->process_keyring) {
253 if (clone_flags & CLONE_THREAD) {
254 atomic_inc(&tsk->process_keyring->usage);
255 }
256 else {
257 tsk->process_keyring = NULL;
258 ret = install_process_keyring(tsk);
259 }
260 }
261
262 tsk->thread_keyring = NULL;
263 return ret;
264
265 } /* end copy_keys() */
266
267 /*****************************************************************************/
268 /*
269 * dispose of keys upon exit
270 */
271 void exit_keys(struct task_struct *tsk)
272 {
273 key_put(tsk->session_keyring);
274 key_put(tsk->process_keyring);
275 key_put(tsk->thread_keyring);
276
277 } /* end exit_keys() */
278
279 /*****************************************************************************/
280 /*
281 * deal with execve()
282 */
283 int exec_keys(struct task_struct *tsk)
284 {
285 struct key *old;
286
287 /* newly exec'd tasks don't get a thread keyring */
288 task_lock(tsk);
289 old = tsk->thread_keyring;
290 tsk->thread_keyring = NULL;
291 task_unlock(tsk);
292
293 key_put(old);
294
295 /* newly exec'd tasks get a fresh process keyring */
296 return install_process_keyring(tsk);
297
298 } /* end exec_keys() */
299
300 /*****************************************************************************/
301 /*
302 * deal with SUID programs
303 * - we might want to make this invent a new session keyring
304 */
305 int suid_keys(struct task_struct *tsk)
306 {
307 return 0;
308
309 } /* end suid_keys() */
310
311 /*****************************************************************************/
312 /*
313 * the filesystem user ID changed
314 */
315 void key_fsuid_changed(struct task_struct *tsk)
316 {
317 /* update the ownership of the process keyring */
318 if (tsk->process_keyring) {
319 down_write(&tsk->process_keyring->sem);
320 write_lock(&tsk->process_keyring->lock);
321 tsk->process_keyring->uid = tsk->fsuid;
322 write_unlock(&tsk->process_keyring->lock);
323 up_write(&tsk->process_keyring->sem);
324 }
325
326 /* update the ownership of the thread keyring */
327 if (tsk->thread_keyring) {
328 down_write(&tsk->thread_keyring->sem);
329 write_lock(&tsk->thread_keyring->lock);
330 tsk->thread_keyring->uid = tsk->fsuid;
331 write_unlock(&tsk->thread_keyring->lock);
332 up_write(&tsk->thread_keyring->sem);
333 }
334
335 } /* end key_fsuid_changed() */
336
337 /*****************************************************************************/
338 /*
339 * the filesystem group ID changed
340 */
341 void key_fsgid_changed(struct task_struct *tsk)
342 {
343 /* update the ownership of the process keyring */
344 if (tsk->process_keyring) {
345 down_write(&tsk->process_keyring->sem);
346 write_lock(&tsk->process_keyring->lock);
347 tsk->process_keyring->gid = tsk->fsgid;
348 write_unlock(&tsk->process_keyring->lock);
349 up_write(&tsk->process_keyring->sem);
350 }
351
352 /* update the ownership of the thread keyring */
353 if (tsk->thread_keyring) {
354 down_write(&tsk->thread_keyring->sem);
355 write_lock(&tsk->thread_keyring->lock);
356 tsk->thread_keyring->gid = tsk->fsgid;
357 write_unlock(&tsk->thread_keyring->lock);
358 up_write(&tsk->thread_keyring->sem);
359 }
360
361 } /* end key_fsgid_changed() */
362
363 /*****************************************************************************/
364 /*
365 * search the process keyrings for the first matching key
366 * - we use the supplied match function to see if the description (or other
367 * feature of interest) matches
368 * - we return -EAGAIN if we didn't find any matching key
369 * - we return -ENOKEY if we found only negative matching keys
370 */
371 struct key *search_process_keyrings_aux(struct key_type *type,
372 const void *description,
373 key_match_func_t match)
374 {
375 struct task_struct *tsk = current;
376 struct key *key, *ret, *err, *session;
377
378 /* we want to return -EAGAIN or -ENOKEY if any of the keyrings were
379 * searchable, but we failed to find a key or we found a negative key;
380 * otherwise we want to return a sample error (probably -EACCES) if
381 * none of the keyrings were searchable
382 *
383 * in terms of priority: success > -ENOKEY > -EAGAIN > other error
384 */
385 key = NULL;
386 ret = NULL;
387 err = ERR_PTR(-EAGAIN);
388
389 /* search the thread keyring first */
390 if (tsk->thread_keyring) {
391 key = keyring_search_aux(tsk->thread_keyring, type,
392 description, match);
393 if (!IS_ERR(key))
394 goto found;
395
396 switch (PTR_ERR(key)) {
397 case -EAGAIN: /* no key */
398 if (ret)
399 break;
400 case -ENOKEY: /* negative key */
401 ret = key;
402 break;
403 default:
404 err = key;
405 break;
406 }
407 }
408
409 /* search the process keyring second */
410 if (tsk->process_keyring) {
411 key = keyring_search_aux(tsk->process_keyring, type,
412 description, match);
413 if (!IS_ERR(key))
414 goto found;
415
416 switch (PTR_ERR(key)) {
417 case -EAGAIN: /* no key */
418 if (ret)
419 break;
420 case -ENOKEY: /* negative key */
421 ret = key;
422 break;
423 default:
424 err = key;
425 break;
426 }
427 }
428
429 /* search the session keyring last */
430 session = tsk->session_keyring;
431 if (!session)
432 session = tsk->user->session_keyring;
433
434 key = keyring_search_aux(session, type,
435 description, match);
436 if (!IS_ERR(key))
437 goto found;
438
439 switch (PTR_ERR(key)) {
440 case -EAGAIN: /* no key */
441 if (ret)
442 break;
443 case -ENOKEY: /* negative key */
444 ret = key;
445 break;
446 default:
447 err = key;
448 break;
449 }
450
451 /* no key - decide on the error we're going to go for */
452 key = ret ? ret : err;
453
454 found:
455 return key;
456
457 } /* end search_process_keyrings_aux() */
458
459 /*****************************************************************************/
460 /*
461 * search the process keyrings for the first matching key
462 * - we return -EAGAIN if we didn't find any matching key
463 * - we return -ENOKEY if we found only negative matching keys
464 */
465 struct key *search_process_keyrings(struct key_type *type,
466 const char *description)
467 {
468 return search_process_keyrings_aux(type, description, type->match);
469
470 } /* end search_process_keyrings() */
471
472 /*****************************************************************************/
473 /*
474 * lookup a key given a key ID from userspace with a given permissions mask
475 * - don't create special keyrings unless so requested
476 * - partially constructed keys aren't found unless requested
477 */
478 struct key *lookup_user_key(key_serial_t id, int create, int partial,
479 key_perm_t perm)
480 {
481 struct task_struct *tsk = current;
482 struct key *key;
483 int ret;
484
485 key = ERR_PTR(-ENOKEY);
486
487 switch (id) {
488 case KEY_SPEC_THREAD_KEYRING:
489 if (!tsk->thread_keyring) {
490 if (!create)
491 goto error;
492
493 ret = install_thread_keyring(tsk);
494 if (ret < 0) {
495 key = ERR_PTR(ret);
496 goto error;
497 }
498 }
499
500 key = tsk->thread_keyring;
501 atomic_inc(&key->usage);
502 break;
503
504 case KEY_SPEC_PROCESS_KEYRING:
505 if (!tsk->process_keyring) {
506 if (!create)
507 goto error;
508
509 ret = install_process_keyring(tsk);
510 if (ret < 0) {
511 key = ERR_PTR(ret);
512 goto error;
513 }
514 }
515
516 key = tsk->process_keyring;
517 atomic_inc(&key->usage);
518 break;
519
520 case KEY_SPEC_SESSION_KEYRING:
521 if (!tsk->session_keyring) {
522 /* always install a session keyring upon access if one
523 * doesn't exist yet */
524 ret = install_session_keyring(
525 tsk, tsk->user->session_keyring);
526 if (ret < 0)
527 goto error;
528 }
529
530 key = tsk->session_keyring;
531 atomic_inc(&key->usage);
532 break;
533
534 case KEY_SPEC_USER_KEYRING:
535 key = tsk->user->uid_keyring;
536 atomic_inc(&key->usage);
537 break;
538
539 case KEY_SPEC_USER_SESSION_KEYRING:
540 key = tsk->user->session_keyring;
541 atomic_inc(&key->usage);
542 break;
543
544 case KEY_SPEC_GROUP_KEYRING:
545 /* group keyrings are not yet supported */
546 key = ERR_PTR(-EINVAL);
547 goto error;
548
549 default:
550 key = ERR_PTR(-EINVAL);
551 if (id < 1)
552 goto error;
553
554 key = key_lookup(id);
555 if (IS_ERR(key))
556 goto error;
557 break;
558 }
559
560 /* check the status and permissions */
561 if (perm) {
562 ret = key_validate(key);
563 if (ret < 0)
564 goto invalid_key;
565 }
566
567 ret = -EIO;
568 if (!partial && !(key->flags & KEY_FLAG_INSTANTIATED))
569 goto invalid_key;
570
571 ret = -EACCES;
572 if (!key_permission(key, perm))
573 goto invalid_key;
574
575 error:
576 return key;
577
578 invalid_key:
579 key_put(key);
580 key = ERR_PTR(ret);
581 goto error;
582
583 } /* end lookup_user_key() */
584
585 /*****************************************************************************/
586 /*
587 * join the named keyring as the session keyring if possible, or attempt to
588 * create a new one of that name if not
589 * - if the name is NULL, an empty anonymous keyring is installed instead
590 * - named session keyring joining is done with a semaphore held
591 */
592 long join_session_keyring(const char *name)
593 {
594 struct task_struct *tsk = current;
595 struct key *keyring;
596 long ret;
597
598 /* if no name is provided, install an anonymous keyring */
599 if (!name) {
600 ret = install_session_keyring(tsk, NULL);
601 if (ret < 0)
602 goto error;
603
604 ret = tsk->session_keyring->serial;
605 goto error;
606 }
607
608 /* allow the user to join or create a named keyring */
609 down(&key_session_sem);
610
611 /* look for an existing keyring of this name */
612 keyring = find_keyring_by_name(name, 0);
613 if (PTR_ERR(keyring) == -ENOKEY) {
614 /* not found - try and create a new one */
615 keyring = keyring_alloc(name, tsk->uid, tsk->gid, 0, NULL);
616 if (IS_ERR(keyring)) {
617 ret = PTR_ERR(keyring);
618 goto error;
619 }
620 }
621 else if (IS_ERR(keyring)) {
622 ret = PTR_ERR(keyring);
623 goto error2;
624 }
625
626 /* we've got a keyring - now to install it */
627 ret = install_session_keyring(tsk, keyring);
628 if (ret < 0)
629 goto error2;
630
631 key_put(keyring);
632
633 ret = tsk->session_keyring->serial;
634
635 error2:
636 up(&key_session_sem);
637 error:
638 return ret;
639
640 } /* end join_session_keyring() */
641
|
This page was automatically generated by the
LXR engine.
|