process_keys.c 18 KB

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