process_keys.c 19 KB

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