process_keys.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  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->cred->tgcred->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->cred->tgcred->lock);
  171. if (!tsk->cred->tgcred->process_keyring) {
  172. tsk->cred->tgcred->process_keyring = keyring;
  173. keyring = NULL;
  174. }
  175. spin_unlock_irq(&tsk->cred->tgcred->lock);
  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->cred->tgcred->session_keyring)
  199. flags = KEY_ALLOC_IN_QUOTA;
  200. keyring = keyring_alloc(buf, tsk->cred->uid, tsk->cred->gid,
  201. tsk, 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->cred->tgcred->lock);
  210. old = tsk->cred->tgcred->session_keyring;
  211. rcu_assign_pointer(tsk->cred->tgcred->session_keyring, keyring);
  212. spin_unlock_irq(&tsk->cred->tgcred->lock);
  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 for fork
  224. */
  225. int copy_keys(unsigned long clone_flags, struct task_struct *tsk)
  226. {
  227. key_check(tsk->cred->thread_keyring);
  228. key_check(tsk->cred->request_key_auth);
  229. /* no thread keyring yet */
  230. tsk->cred->thread_keyring = NULL;
  231. /* copy the request_key() authorisation for this thread */
  232. key_get(tsk->cred->request_key_auth);
  233. return 0;
  234. } /* end copy_keys() */
  235. /*****************************************************************************/
  236. /*
  237. * dispose of per-thread keys upon thread exit
  238. */
  239. void exit_keys(struct task_struct *tsk)
  240. {
  241. key_put(tsk->cred->thread_keyring);
  242. key_put(tsk->cred->request_key_auth);
  243. } /* end exit_keys() */
  244. /*****************************************************************************/
  245. /*
  246. * deal with execve()
  247. */
  248. int exec_keys(struct task_struct *tsk)
  249. {
  250. struct key *old;
  251. /* newly exec'd tasks don't get a thread keyring */
  252. task_lock(tsk);
  253. old = tsk->cred->thread_keyring;
  254. tsk->cred->thread_keyring = NULL;
  255. task_unlock(tsk);
  256. key_put(old);
  257. /* discard the process keyring from a newly exec'd task */
  258. spin_lock_irq(&tsk->cred->tgcred->lock);
  259. old = tsk->cred->tgcred->process_keyring;
  260. tsk->cred->tgcred->process_keyring = NULL;
  261. spin_unlock_irq(&tsk->cred->tgcred->lock);
  262. key_put(old);
  263. return 0;
  264. } /* end exec_keys() */
  265. /*****************************************************************************/
  266. /*
  267. * deal with SUID programs
  268. * - we might want to make this invent a new session keyring
  269. */
  270. int suid_keys(struct task_struct *tsk)
  271. {
  272. return 0;
  273. } /* end suid_keys() */
  274. /*****************************************************************************/
  275. /*
  276. * the filesystem user ID changed
  277. */
  278. void key_fsuid_changed(struct task_struct *tsk)
  279. {
  280. /* update the ownership of the thread keyring */
  281. BUG_ON(!tsk->cred);
  282. if (tsk->cred->thread_keyring) {
  283. down_write(&tsk->cred->thread_keyring->sem);
  284. tsk->cred->thread_keyring->uid = tsk->cred->fsuid;
  285. up_write(&tsk->cred->thread_keyring->sem);
  286. }
  287. } /* end key_fsuid_changed() */
  288. /*****************************************************************************/
  289. /*
  290. * the filesystem group ID changed
  291. */
  292. void key_fsgid_changed(struct task_struct *tsk)
  293. {
  294. /* update the ownership of the thread keyring */
  295. BUG_ON(!tsk->cred);
  296. if (tsk->cred->thread_keyring) {
  297. down_write(&tsk->cred->thread_keyring->sem);
  298. tsk->cred->thread_keyring->gid = tsk->cred->fsgid;
  299. up_write(&tsk->cred->thread_keyring->sem);
  300. }
  301. } /* end key_fsgid_changed() */
  302. /*****************************************************************************/
  303. /*
  304. * search the process keyrings for the first matching key
  305. * - we use the supplied match function to see if the description (or other
  306. * feature of interest) matches
  307. * - we return -EAGAIN if we didn't find any matching key
  308. * - we return -ENOKEY if we found only negative matching keys
  309. */
  310. key_ref_t search_process_keyrings(struct key_type *type,
  311. const void *description,
  312. key_match_func_t match,
  313. struct task_struct *context)
  314. {
  315. struct request_key_auth *rka;
  316. struct cred *cred;
  317. key_ref_t key_ref, ret, err;
  318. might_sleep();
  319. cred = get_task_cred(context);
  320. /* we want to return -EAGAIN or -ENOKEY if any of the keyrings were
  321. * searchable, but we failed to find a key or we found a negative key;
  322. * otherwise we want to return a sample error (probably -EACCES) if
  323. * none of the keyrings were searchable
  324. *
  325. * in terms of priority: success > -ENOKEY > -EAGAIN > other error
  326. */
  327. key_ref = NULL;
  328. ret = NULL;
  329. err = ERR_PTR(-EAGAIN);
  330. /* search the thread keyring first */
  331. if (cred->thread_keyring) {
  332. key_ref = keyring_search_aux(
  333. make_key_ref(cred->thread_keyring, 1),
  334. context, type, description, match);
  335. if (!IS_ERR(key_ref))
  336. goto found;
  337. switch (PTR_ERR(key_ref)) {
  338. case -EAGAIN: /* no key */
  339. if (ret)
  340. break;
  341. case -ENOKEY: /* negative key */
  342. ret = key_ref;
  343. break;
  344. default:
  345. err = key_ref;
  346. break;
  347. }
  348. }
  349. /* search the process keyring second */
  350. if (cred->tgcred->process_keyring) {
  351. key_ref = keyring_search_aux(
  352. make_key_ref(cred->tgcred->process_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 session keyring */
  369. if (cred->tgcred->session_keyring) {
  370. rcu_read_lock();
  371. key_ref = keyring_search_aux(
  372. make_key_ref(rcu_dereference(
  373. cred->tgcred->session_keyring),
  374. 1),
  375. context, type, description, match);
  376. rcu_read_unlock();
  377. if (!IS_ERR(key_ref))
  378. goto found;
  379. switch (PTR_ERR(key_ref)) {
  380. case -EAGAIN: /* no key */
  381. if (ret)
  382. break;
  383. case -ENOKEY: /* negative key */
  384. ret = key_ref;
  385. break;
  386. default:
  387. err = key_ref;
  388. break;
  389. }
  390. }
  391. /* or search the user-session keyring */
  392. else if (cred->user->session_keyring) {
  393. key_ref = keyring_search_aux(
  394. make_key_ref(cred->user->session_keyring, 1),
  395. context, type, description, match);
  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. /* if this process has an instantiation authorisation key, then we also
  411. * search the keyrings of the process mentioned there
  412. * - we don't permit access to request_key auth keys via this method
  413. */
  414. if (cred->request_key_auth &&
  415. context == current &&
  416. type != &key_type_request_key_auth
  417. ) {
  418. /* defend against the auth key being revoked */
  419. down_read(&cred->request_key_auth->sem);
  420. if (key_validate(cred->request_key_auth) == 0) {
  421. rka = cred->request_key_auth->payload.data;
  422. key_ref = search_process_keyrings(type, description,
  423. match, rka->context);
  424. up_read(&cred->request_key_auth->sem);
  425. if (!IS_ERR(key_ref))
  426. goto found;
  427. switch (PTR_ERR(key_ref)) {
  428. case -EAGAIN: /* no key */
  429. if (ret)
  430. break;
  431. case -ENOKEY: /* negative key */
  432. ret = key_ref;
  433. break;
  434. default:
  435. err = key_ref;
  436. break;
  437. }
  438. } else {
  439. up_read(&cred->request_key_auth->sem);
  440. }
  441. }
  442. /* no key - decide on the error we're going to go for */
  443. key_ref = ret ? ret : err;
  444. found:
  445. put_cred(cred);
  446. return key_ref;
  447. } /* end search_process_keyrings() */
  448. /*****************************************************************************/
  449. /*
  450. * see if the key we're looking at is the target key
  451. */
  452. static int lookup_user_key_possessed(const struct key *key, const void *target)
  453. {
  454. return key == target;
  455. } /* end lookup_user_key_possessed() */
  456. /*****************************************************************************/
  457. /*
  458. * lookup a key given a key ID from userspace with a given permissions mask
  459. * - don't create special keyrings unless so requested
  460. * - partially constructed keys aren't found unless requested
  461. */
  462. key_ref_t lookup_user_key(key_serial_t id, int create, int partial,
  463. key_perm_t perm)
  464. {
  465. struct request_key_auth *rka;
  466. struct task_struct *t = current;
  467. struct cred *cred;
  468. struct key *key;
  469. key_ref_t key_ref, skey_ref;
  470. int ret;
  471. try_again:
  472. cred = get_current_cred();
  473. key_ref = ERR_PTR(-ENOKEY);
  474. switch (id) {
  475. case KEY_SPEC_THREAD_KEYRING:
  476. if (!cred->thread_keyring) {
  477. if (!create)
  478. goto error;
  479. ret = install_thread_keyring();
  480. if (ret < 0) {
  481. key = ERR_PTR(ret);
  482. goto error;
  483. }
  484. goto reget_creds;
  485. }
  486. key = cred->thread_keyring;
  487. atomic_inc(&key->usage);
  488. key_ref = make_key_ref(key, 1);
  489. break;
  490. case KEY_SPEC_PROCESS_KEYRING:
  491. if (!cred->tgcred->process_keyring) {
  492. if (!create)
  493. goto error;
  494. ret = install_process_keyring();
  495. if (ret < 0) {
  496. key = ERR_PTR(ret);
  497. goto error;
  498. }
  499. goto reget_creds;
  500. }
  501. key = cred->tgcred->process_keyring;
  502. atomic_inc(&key->usage);
  503. key_ref = make_key_ref(key, 1);
  504. break;
  505. case KEY_SPEC_SESSION_KEYRING:
  506. if (!cred->tgcred->session_keyring) {
  507. /* always install a session keyring upon access if one
  508. * doesn't exist yet */
  509. ret = install_user_keyrings();
  510. if (ret < 0)
  511. goto error;
  512. ret = install_session_keyring(
  513. cred->user->session_keyring);
  514. if (ret < 0)
  515. goto error;
  516. goto reget_creds;
  517. }
  518. rcu_read_lock();
  519. key = rcu_dereference(cred->tgcred->session_keyring);
  520. atomic_inc(&key->usage);
  521. rcu_read_unlock();
  522. key_ref = make_key_ref(key, 1);
  523. break;
  524. case KEY_SPEC_USER_KEYRING:
  525. if (!cred->user->uid_keyring) {
  526. ret = install_user_keyrings();
  527. if (ret < 0)
  528. goto error;
  529. }
  530. key = cred->user->uid_keyring;
  531. atomic_inc(&key->usage);
  532. key_ref = make_key_ref(key, 1);
  533. break;
  534. case KEY_SPEC_USER_SESSION_KEYRING:
  535. if (!cred->user->session_keyring) {
  536. ret = install_user_keyrings();
  537. if (ret < 0)
  538. goto error;
  539. }
  540. key = cred->user->session_keyring;
  541. atomic_inc(&key->usage);
  542. key_ref = make_key_ref(key, 1);
  543. break;
  544. case KEY_SPEC_GROUP_KEYRING:
  545. /* group keyrings are not yet supported */
  546. key = ERR_PTR(-EINVAL);
  547. goto error;
  548. case KEY_SPEC_REQKEY_AUTH_KEY:
  549. key = cred->request_key_auth;
  550. if (!key)
  551. goto error;
  552. atomic_inc(&key->usage);
  553. key_ref = make_key_ref(key, 1);
  554. break;
  555. case KEY_SPEC_REQUESTOR_KEYRING:
  556. if (!cred->request_key_auth)
  557. goto error;
  558. down_read(&cred->request_key_auth->sem);
  559. if (cred->request_key_auth->flags & KEY_FLAG_REVOKED) {
  560. key_ref = ERR_PTR(-EKEYREVOKED);
  561. key = NULL;
  562. } else {
  563. rka = cred->request_key_auth->payload.data;
  564. key = rka->dest_keyring;
  565. atomic_inc(&key->usage);
  566. }
  567. up_read(&cred->request_key_auth->sem);
  568. if (!key)
  569. goto error;
  570. key_ref = make_key_ref(key, 1);
  571. break;
  572. default:
  573. key_ref = ERR_PTR(-EINVAL);
  574. if (id < 1)
  575. goto error;
  576. key = key_lookup(id);
  577. if (IS_ERR(key)) {
  578. key_ref = ERR_CAST(key);
  579. goto error;
  580. }
  581. key_ref = make_key_ref(key, 0);
  582. /* check to see if we possess the key */
  583. skey_ref = search_process_keyrings(key->type, key,
  584. lookup_user_key_possessed,
  585. current);
  586. if (!IS_ERR(skey_ref)) {
  587. key_put(key);
  588. key_ref = skey_ref;
  589. }
  590. break;
  591. }
  592. if (!partial) {
  593. ret = wait_for_key_construction(key, true);
  594. switch (ret) {
  595. case -ERESTARTSYS:
  596. goto invalid_key;
  597. default:
  598. if (perm)
  599. goto invalid_key;
  600. case 0:
  601. break;
  602. }
  603. } else if (perm) {
  604. ret = key_validate(key);
  605. if (ret < 0)
  606. goto invalid_key;
  607. }
  608. ret = -EIO;
  609. if (!partial && !test_bit(KEY_FLAG_INSTANTIATED, &key->flags))
  610. goto invalid_key;
  611. /* check the permissions */
  612. ret = key_task_permission(key_ref, t, perm);
  613. if (ret < 0)
  614. goto invalid_key;
  615. error:
  616. put_cred(cred);
  617. return key_ref;
  618. invalid_key:
  619. key_ref_put(key_ref);
  620. key_ref = ERR_PTR(ret);
  621. goto error;
  622. /* if we attempted to install a keyring, then it may have caused new
  623. * creds to be installed */
  624. reget_creds:
  625. put_cred(cred);
  626. goto try_again;
  627. } /* end lookup_user_key() */
  628. /*****************************************************************************/
  629. /*
  630. * join the named keyring as the session keyring if possible, or attempt to
  631. * create a new one of that name if not
  632. * - if the name is NULL, an empty anonymous keyring is installed instead
  633. * - named session keyring joining is done with a semaphore held
  634. */
  635. long join_session_keyring(const char *name)
  636. {
  637. struct task_struct *tsk = current;
  638. struct cred *cred = current->cred;
  639. struct key *keyring;
  640. long ret;
  641. /* if no name is provided, install an anonymous keyring */
  642. if (!name) {
  643. ret = install_session_keyring(NULL);
  644. if (ret < 0)
  645. goto error;
  646. rcu_read_lock();
  647. ret = rcu_dereference(cred->tgcred->session_keyring)->serial;
  648. rcu_read_unlock();
  649. goto error;
  650. }
  651. /* allow the user to join or create a named keyring */
  652. mutex_lock(&key_session_mutex);
  653. /* look for an existing keyring of this name */
  654. keyring = find_keyring_by_name(name, false);
  655. if (PTR_ERR(keyring) == -ENOKEY) {
  656. /* not found - try and create a new one */
  657. keyring = keyring_alloc(name, cred->uid, cred->gid, tsk,
  658. KEY_ALLOC_IN_QUOTA, NULL);
  659. if (IS_ERR(keyring)) {
  660. ret = PTR_ERR(keyring);
  661. goto error2;
  662. }
  663. }
  664. else if (IS_ERR(keyring)) {
  665. ret = PTR_ERR(keyring);
  666. goto error2;
  667. }
  668. /* we've got a keyring - now to install it */
  669. ret = install_session_keyring(keyring);
  670. if (ret < 0)
  671. goto error2;
  672. ret = keyring->serial;
  673. key_put(keyring);
  674. error2:
  675. mutex_unlock(&key_session_mutex);
  676. error:
  677. return ret;
  678. } /* end join_session_keyring() */