keyring.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106
  1. /* Keyring handling
  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/security.h>
  16. #include <linux/seq_file.h>
  17. #include <linux/err.h>
  18. #include <keys/keyring-type.h>
  19. #include <asm/uaccess.h>
  20. #include "internal.h"
  21. #define rcu_dereference_locked_keyring(keyring) \
  22. (rcu_dereference_protected( \
  23. (keyring)->payload.subscriptions, \
  24. rwsem_is_locked((struct rw_semaphore *)&(keyring)->sem)))
  25. /*
  26. * when plumbing the depths of the key tree, this sets a hard limit set on how
  27. * deep we're willing to go
  28. */
  29. #define KEYRING_SEARCH_MAX_DEPTH 6
  30. /*
  31. * we keep all named keyrings in a hash to speed looking them up
  32. */
  33. #define KEYRING_NAME_HASH_SIZE (1 << 5)
  34. static struct list_head keyring_name_hash[KEYRING_NAME_HASH_SIZE];
  35. static DEFINE_RWLOCK(keyring_name_lock);
  36. static inline unsigned keyring_hash(const char *desc)
  37. {
  38. unsigned bucket = 0;
  39. for (; *desc; desc++)
  40. bucket += (unsigned char) *desc;
  41. return bucket & (KEYRING_NAME_HASH_SIZE - 1);
  42. }
  43. /*
  44. * the keyring type definition
  45. */
  46. static int keyring_instantiate(struct key *keyring,
  47. const void *data, size_t datalen);
  48. static int keyring_match(const struct key *keyring, const void *criterion);
  49. static void keyring_revoke(struct key *keyring);
  50. static void keyring_destroy(struct key *keyring);
  51. static void keyring_describe(const struct key *keyring, struct seq_file *m);
  52. static long keyring_read(const struct key *keyring,
  53. char __user *buffer, size_t buflen);
  54. struct key_type key_type_keyring = {
  55. .name = "keyring",
  56. .def_datalen = sizeof(struct keyring_list),
  57. .instantiate = keyring_instantiate,
  58. .match = keyring_match,
  59. .revoke = keyring_revoke,
  60. .destroy = keyring_destroy,
  61. .describe = keyring_describe,
  62. .read = keyring_read,
  63. };
  64. EXPORT_SYMBOL(key_type_keyring);
  65. /*
  66. * semaphore to serialise link/link calls to prevent two link calls in parallel
  67. * introducing a cycle
  68. */
  69. static DECLARE_RWSEM(keyring_serialise_link_sem);
  70. /*****************************************************************************/
  71. /*
  72. * publish the name of a keyring so that it can be found by name (if it has
  73. * one)
  74. */
  75. static void keyring_publish_name(struct key *keyring)
  76. {
  77. int bucket;
  78. if (keyring->description) {
  79. bucket = keyring_hash(keyring->description);
  80. write_lock(&keyring_name_lock);
  81. if (!keyring_name_hash[bucket].next)
  82. INIT_LIST_HEAD(&keyring_name_hash[bucket]);
  83. list_add_tail(&keyring->type_data.link,
  84. &keyring_name_hash[bucket]);
  85. write_unlock(&keyring_name_lock);
  86. }
  87. } /* end keyring_publish_name() */
  88. /*****************************************************************************/
  89. /*
  90. * initialise a keyring
  91. * - we object if we were given any data
  92. */
  93. static int keyring_instantiate(struct key *keyring,
  94. const void *data, size_t datalen)
  95. {
  96. int ret;
  97. ret = -EINVAL;
  98. if (datalen == 0) {
  99. /* make the keyring available by name if it has one */
  100. keyring_publish_name(keyring);
  101. ret = 0;
  102. }
  103. return ret;
  104. } /* end keyring_instantiate() */
  105. /*****************************************************************************/
  106. /*
  107. * match keyrings on their name
  108. */
  109. static int keyring_match(const struct key *keyring, const void *description)
  110. {
  111. return keyring->description &&
  112. strcmp(keyring->description, description) == 0;
  113. } /* end keyring_match() */
  114. /*****************************************************************************/
  115. /*
  116. * dispose of the data dangling from the corpse of a keyring
  117. */
  118. static void keyring_destroy(struct key *keyring)
  119. {
  120. struct keyring_list *klist;
  121. int loop;
  122. if (keyring->description) {
  123. write_lock(&keyring_name_lock);
  124. if (keyring->type_data.link.next != NULL &&
  125. !list_empty(&keyring->type_data.link))
  126. list_del(&keyring->type_data.link);
  127. write_unlock(&keyring_name_lock);
  128. }
  129. klist = rcu_dereference_check(keyring->payload.subscriptions,
  130. rcu_read_lock_held() ||
  131. atomic_read(&keyring->usage) == 0);
  132. if (klist) {
  133. for (loop = klist->nkeys - 1; loop >= 0; loop--)
  134. key_put(klist->keys[loop]);
  135. kfree(klist);
  136. }
  137. } /* end keyring_destroy() */
  138. /*****************************************************************************/
  139. /*
  140. * describe the keyring
  141. */
  142. static void keyring_describe(const struct key *keyring, struct seq_file *m)
  143. {
  144. struct keyring_list *klist;
  145. if (keyring->description) {
  146. seq_puts(m, keyring->description);
  147. }
  148. else {
  149. seq_puts(m, "[anon]");
  150. }
  151. rcu_read_lock();
  152. klist = rcu_dereference(keyring->payload.subscriptions);
  153. if (klist)
  154. seq_printf(m, ": %u/%u", klist->nkeys, klist->maxkeys);
  155. else
  156. seq_puts(m, ": empty");
  157. rcu_read_unlock();
  158. } /* end keyring_describe() */
  159. /*****************************************************************************/
  160. /*
  161. * read a list of key IDs from the keyring's contents
  162. * - the keyring's semaphore is read-locked
  163. */
  164. static long keyring_read(const struct key *keyring,
  165. char __user *buffer, size_t buflen)
  166. {
  167. struct keyring_list *klist;
  168. struct key *key;
  169. size_t qty, tmp;
  170. int loop, ret;
  171. ret = 0;
  172. klist = rcu_dereference_locked_keyring(keyring);
  173. if (klist) {
  174. /* calculate how much data we could return */
  175. qty = klist->nkeys * sizeof(key_serial_t);
  176. if (buffer && buflen > 0) {
  177. if (buflen > qty)
  178. buflen = qty;
  179. /* copy the IDs of the subscribed keys into the
  180. * buffer */
  181. ret = -EFAULT;
  182. for (loop = 0; loop < klist->nkeys; loop++) {
  183. key = klist->keys[loop];
  184. tmp = sizeof(key_serial_t);
  185. if (tmp > buflen)
  186. tmp = buflen;
  187. if (copy_to_user(buffer,
  188. &key->serial,
  189. tmp) != 0)
  190. goto error;
  191. buflen -= tmp;
  192. if (buflen == 0)
  193. break;
  194. buffer += tmp;
  195. }
  196. }
  197. ret = qty;
  198. }
  199. error:
  200. return ret;
  201. } /* end keyring_read() */
  202. /*****************************************************************************/
  203. /*
  204. * allocate a keyring and link into the destination keyring
  205. */
  206. struct key *keyring_alloc(const char *description, uid_t uid, gid_t gid,
  207. const struct cred *cred, unsigned long flags,
  208. struct key *dest)
  209. {
  210. struct key *keyring;
  211. int ret;
  212. keyring = key_alloc(&key_type_keyring, description,
  213. uid, gid, cred,
  214. (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_ALL,
  215. flags);
  216. if (!IS_ERR(keyring)) {
  217. ret = key_instantiate_and_link(keyring, NULL, 0, dest, NULL);
  218. if (ret < 0) {
  219. key_put(keyring);
  220. keyring = ERR_PTR(ret);
  221. }
  222. }
  223. return keyring;
  224. } /* end keyring_alloc() */
  225. /*****************************************************************************/
  226. /*
  227. * search the supplied keyring tree for a key that matches the criterion
  228. * - perform a breadth-then-depth search up to the prescribed limit
  229. * - we only find keys on which we have search permission
  230. * - we use the supplied match function to see if the description (or other
  231. * feature of interest) matches
  232. * - we rely on RCU to prevent the keyring lists from disappearing on us
  233. * - we return -EAGAIN if we didn't find any matching key
  234. * - we return -ENOKEY if we only found negative matching keys
  235. * - we propagate the possession attribute from the keyring ref to the key ref
  236. */
  237. key_ref_t keyring_search_aux(key_ref_t keyring_ref,
  238. const struct cred *cred,
  239. struct key_type *type,
  240. const void *description,
  241. key_match_func_t match)
  242. {
  243. struct {
  244. struct keyring_list *keylist;
  245. int kix;
  246. } stack[KEYRING_SEARCH_MAX_DEPTH];
  247. struct keyring_list *keylist;
  248. struct timespec now;
  249. unsigned long possessed, kflags;
  250. struct key *keyring, *key;
  251. key_ref_t key_ref;
  252. long err;
  253. int sp, kix;
  254. keyring = key_ref_to_ptr(keyring_ref);
  255. possessed = is_key_possessed(keyring_ref);
  256. key_check(keyring);
  257. /* top keyring must have search permission to begin the search */
  258. err = key_task_permission(keyring_ref, cred, KEY_SEARCH);
  259. if (err < 0) {
  260. key_ref = ERR_PTR(err);
  261. goto error;
  262. }
  263. key_ref = ERR_PTR(-ENOTDIR);
  264. if (keyring->type != &key_type_keyring)
  265. goto error;
  266. rcu_read_lock();
  267. now = current_kernel_time();
  268. err = -EAGAIN;
  269. sp = 0;
  270. /* firstly we should check to see if this top-level keyring is what we
  271. * are looking for */
  272. key_ref = ERR_PTR(-EAGAIN);
  273. kflags = keyring->flags;
  274. if (keyring->type == type && match(keyring, description)) {
  275. key = keyring;
  276. /* check it isn't negative and hasn't expired or been
  277. * revoked */
  278. if (kflags & (1 << KEY_FLAG_REVOKED))
  279. goto error_2;
  280. if (key->expiry && now.tv_sec >= key->expiry)
  281. goto error_2;
  282. key_ref = ERR_PTR(-ENOKEY);
  283. if (kflags & (1 << KEY_FLAG_NEGATIVE))
  284. goto error_2;
  285. goto found;
  286. }
  287. /* otherwise, the top keyring must not be revoked, expired, or
  288. * negatively instantiated if we are to search it */
  289. key_ref = ERR_PTR(-EAGAIN);
  290. if (kflags & ((1 << KEY_FLAG_REVOKED) | (1 << KEY_FLAG_NEGATIVE)) ||
  291. (keyring->expiry && now.tv_sec >= keyring->expiry))
  292. goto error_2;
  293. /* start processing a new keyring */
  294. descend:
  295. if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
  296. goto not_this_keyring;
  297. keylist = rcu_dereference(keyring->payload.subscriptions);
  298. if (!keylist)
  299. goto not_this_keyring;
  300. /* iterate through the keys in this keyring first */
  301. for (kix = 0; kix < keylist->nkeys; kix++) {
  302. key = keylist->keys[kix];
  303. kflags = key->flags;
  304. /* ignore keys not of this type */
  305. if (key->type != type)
  306. continue;
  307. /* skip revoked keys and expired keys */
  308. if (kflags & (1 << KEY_FLAG_REVOKED))
  309. continue;
  310. if (key->expiry && now.tv_sec >= key->expiry)
  311. continue;
  312. /* keys that don't match */
  313. if (!match(key, description))
  314. continue;
  315. /* key must have search permissions */
  316. if (key_task_permission(make_key_ref(key, possessed),
  317. cred, KEY_SEARCH) < 0)
  318. continue;
  319. /* we set a different error code if we pass a negative key */
  320. if (kflags & (1 << KEY_FLAG_NEGATIVE)) {
  321. err = -ENOKEY;
  322. continue;
  323. }
  324. goto found;
  325. }
  326. /* search through the keyrings nested in this one */
  327. kix = 0;
  328. ascend:
  329. for (; kix < keylist->nkeys; kix++) {
  330. key = keylist->keys[kix];
  331. if (key->type != &key_type_keyring)
  332. continue;
  333. /* recursively search nested keyrings
  334. * - only search keyrings for which we have search permission
  335. */
  336. if (sp >= KEYRING_SEARCH_MAX_DEPTH)
  337. continue;
  338. if (key_task_permission(make_key_ref(key, possessed),
  339. cred, KEY_SEARCH) < 0)
  340. continue;
  341. /* stack the current position */
  342. stack[sp].keylist = keylist;
  343. stack[sp].kix = kix;
  344. sp++;
  345. /* begin again with the new keyring */
  346. keyring = key;
  347. goto descend;
  348. }
  349. /* the keyring we're looking at was disqualified or didn't contain a
  350. * matching key */
  351. not_this_keyring:
  352. if (sp > 0) {
  353. /* resume the processing of a keyring higher up in the tree */
  354. sp--;
  355. keylist = stack[sp].keylist;
  356. kix = stack[sp].kix + 1;
  357. goto ascend;
  358. }
  359. key_ref = ERR_PTR(err);
  360. goto error_2;
  361. /* we found a viable match */
  362. found:
  363. atomic_inc(&key->usage);
  364. key_check(key);
  365. key_ref = make_key_ref(key, possessed);
  366. error_2:
  367. rcu_read_unlock();
  368. error:
  369. return key_ref;
  370. } /* end keyring_search_aux() */
  371. /*****************************************************************************/
  372. /*
  373. * search the supplied keyring tree for a key that matches the criterion
  374. * - perform a breadth-then-depth search up to the prescribed limit
  375. * - we only find keys on which we have search permission
  376. * - we readlock the keyrings as we search down the tree
  377. * - we return -EAGAIN if we didn't find any matching key
  378. * - we return -ENOKEY if we only found negative matching keys
  379. */
  380. key_ref_t keyring_search(key_ref_t keyring,
  381. struct key_type *type,
  382. const char *description)
  383. {
  384. if (!type->match)
  385. return ERR_PTR(-ENOKEY);
  386. return keyring_search_aux(keyring, current->cred,
  387. type, description, type->match);
  388. } /* end keyring_search() */
  389. EXPORT_SYMBOL(keyring_search);
  390. /*****************************************************************************/
  391. /*
  392. * search the given keyring only (no recursion)
  393. * - keyring must be locked by caller
  394. * - caller must guarantee that the keyring is a keyring
  395. */
  396. key_ref_t __keyring_search_one(key_ref_t keyring_ref,
  397. const struct key_type *ktype,
  398. const char *description,
  399. key_perm_t perm)
  400. {
  401. struct keyring_list *klist;
  402. unsigned long possessed;
  403. struct key *keyring, *key;
  404. int loop;
  405. keyring = key_ref_to_ptr(keyring_ref);
  406. possessed = is_key_possessed(keyring_ref);
  407. rcu_read_lock();
  408. klist = rcu_dereference(keyring->payload.subscriptions);
  409. if (klist) {
  410. for (loop = 0; loop < klist->nkeys; loop++) {
  411. key = klist->keys[loop];
  412. if (key->type == ktype &&
  413. (!key->type->match ||
  414. key->type->match(key, description)) &&
  415. key_permission(make_key_ref(key, possessed),
  416. perm) == 0 &&
  417. !test_bit(KEY_FLAG_REVOKED, &key->flags)
  418. )
  419. goto found;
  420. }
  421. }
  422. rcu_read_unlock();
  423. return ERR_PTR(-ENOKEY);
  424. found:
  425. atomic_inc(&key->usage);
  426. rcu_read_unlock();
  427. return make_key_ref(key, possessed);
  428. } /* end __keyring_search_one() */
  429. /*****************************************************************************/
  430. /*
  431. * find a keyring with the specified name
  432. * - all named keyrings are searched
  433. * - normally only finds keyrings with search permission for the current process
  434. */
  435. struct key *find_keyring_by_name(const char *name, bool skip_perm_check)
  436. {
  437. struct key *keyring;
  438. int bucket;
  439. if (!name)
  440. return ERR_PTR(-EINVAL);
  441. bucket = keyring_hash(name);
  442. read_lock(&keyring_name_lock);
  443. if (keyring_name_hash[bucket].next) {
  444. /* search this hash bucket for a keyring with a matching name
  445. * that's readable and that hasn't been revoked */
  446. list_for_each_entry(keyring,
  447. &keyring_name_hash[bucket],
  448. type_data.link
  449. ) {
  450. if (keyring->user->user_ns != current_user_ns())
  451. continue;
  452. if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
  453. continue;
  454. if (strcmp(keyring->description, name) != 0)
  455. continue;
  456. if (!skip_perm_check &&
  457. key_permission(make_key_ref(keyring, 0),
  458. KEY_SEARCH) < 0)
  459. continue;
  460. /* we've got a match but we might end up racing with
  461. * key_cleanup() if the keyring is currently 'dead'
  462. * (ie. it has a zero usage count) */
  463. if (!atomic_inc_not_zero(&keyring->usage))
  464. continue;
  465. goto out;
  466. }
  467. }
  468. keyring = ERR_PTR(-ENOKEY);
  469. out:
  470. read_unlock(&keyring_name_lock);
  471. return keyring;
  472. } /* end find_keyring_by_name() */
  473. /*****************************************************************************/
  474. /*
  475. * see if a cycle will will be created by inserting acyclic tree B in acyclic
  476. * tree A at the topmost level (ie: as a direct child of A)
  477. * - since we are adding B to A at the top level, checking for cycles should
  478. * just be a matter of seeing if node A is somewhere in tree B
  479. */
  480. static int keyring_detect_cycle(struct key *A, struct key *B)
  481. {
  482. struct {
  483. struct keyring_list *keylist;
  484. int kix;
  485. } stack[KEYRING_SEARCH_MAX_DEPTH];
  486. struct keyring_list *keylist;
  487. struct key *subtree, *key;
  488. int sp, kix, ret;
  489. rcu_read_lock();
  490. ret = -EDEADLK;
  491. if (A == B)
  492. goto cycle_detected;
  493. subtree = B;
  494. sp = 0;
  495. /* start processing a new keyring */
  496. descend:
  497. if (test_bit(KEY_FLAG_REVOKED, &subtree->flags))
  498. goto not_this_keyring;
  499. keylist = rcu_dereference(subtree->payload.subscriptions);
  500. if (!keylist)
  501. goto not_this_keyring;
  502. kix = 0;
  503. ascend:
  504. /* iterate through the remaining keys in this keyring */
  505. for (; kix < keylist->nkeys; kix++) {
  506. key = keylist->keys[kix];
  507. if (key == A)
  508. goto cycle_detected;
  509. /* recursively check nested keyrings */
  510. if (key->type == &key_type_keyring) {
  511. if (sp >= KEYRING_SEARCH_MAX_DEPTH)
  512. goto too_deep;
  513. /* stack the current position */
  514. stack[sp].keylist = keylist;
  515. stack[sp].kix = kix;
  516. sp++;
  517. /* begin again with the new keyring */
  518. subtree = key;
  519. goto descend;
  520. }
  521. }
  522. /* the keyring we're looking at was disqualified or didn't contain a
  523. * matching key */
  524. not_this_keyring:
  525. if (sp > 0) {
  526. /* resume the checking of a keyring higher up in the tree */
  527. sp--;
  528. keylist = stack[sp].keylist;
  529. kix = stack[sp].kix + 1;
  530. goto ascend;
  531. }
  532. ret = 0; /* no cycles detected */
  533. error:
  534. rcu_read_unlock();
  535. return ret;
  536. too_deep:
  537. ret = -ELOOP;
  538. goto error;
  539. cycle_detected:
  540. ret = -EDEADLK;
  541. goto error;
  542. } /* end keyring_detect_cycle() */
  543. /*****************************************************************************/
  544. /*
  545. * dispose of a keyring list after the RCU grace period
  546. */
  547. static void keyring_link_rcu_disposal(struct rcu_head *rcu)
  548. {
  549. struct keyring_list *klist =
  550. container_of(rcu, struct keyring_list, rcu);
  551. kfree(klist);
  552. } /* end keyring_link_rcu_disposal() */
  553. /*****************************************************************************/
  554. /*
  555. * dispose of a keyring list after the RCU grace period, freeing the unlinked
  556. * key
  557. */
  558. static void keyring_unlink_rcu_disposal(struct rcu_head *rcu)
  559. {
  560. struct keyring_list *klist =
  561. container_of(rcu, struct keyring_list, rcu);
  562. key_put(klist->keys[klist->delkey]);
  563. kfree(klist);
  564. } /* end keyring_unlink_rcu_disposal() */
  565. /*****************************************************************************/
  566. /*
  567. * link a key into to a keyring
  568. * - must be called with the keyring's semaphore write-locked
  569. * - discard already extant link to matching key if there is one
  570. */
  571. int __key_link(struct key *keyring, struct key *key)
  572. {
  573. struct keyring_list *klist, *nklist;
  574. unsigned max;
  575. size_t size;
  576. int loop, ret;
  577. ret = -EKEYREVOKED;
  578. if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
  579. goto error;
  580. ret = -ENOTDIR;
  581. if (keyring->type != &key_type_keyring)
  582. goto error;
  583. /* serialise link/link calls to prevent parallel calls causing a
  584. * cycle when applied to two keyring in opposite orders */
  585. down_write(&keyring_serialise_link_sem);
  586. /* check that we aren't going to create a cycle adding one keyring to
  587. * another */
  588. if (key->type == &key_type_keyring) {
  589. ret = keyring_detect_cycle(keyring, key);
  590. if (ret < 0)
  591. goto error2;
  592. }
  593. /* see if there's a matching key we can displace */
  594. klist = rcu_dereference_locked_keyring(keyring);
  595. if (klist && klist->nkeys > 0) {
  596. struct key_type *type = key->type;
  597. for (loop = klist->nkeys - 1; loop >= 0; loop--) {
  598. if (klist->keys[loop]->type == type &&
  599. strcmp(klist->keys[loop]->description,
  600. key->description) == 0
  601. ) {
  602. /* found a match - replace with new key */
  603. size = sizeof(struct key *) * klist->maxkeys;
  604. size += sizeof(*klist);
  605. BUG_ON(size > PAGE_SIZE);
  606. ret = -ENOMEM;
  607. nklist = kmemdup(klist, size, GFP_KERNEL);
  608. if (!nklist)
  609. goto error2;
  610. /* replace matched key */
  611. atomic_inc(&key->usage);
  612. nklist->keys[loop] = key;
  613. rcu_assign_pointer(
  614. keyring->payload.subscriptions,
  615. nklist);
  616. /* dispose of the old keyring list and the
  617. * displaced key */
  618. klist->delkey = loop;
  619. call_rcu(&klist->rcu,
  620. keyring_unlink_rcu_disposal);
  621. goto done;
  622. }
  623. }
  624. }
  625. /* check that we aren't going to overrun the user's quota */
  626. ret = key_payload_reserve(keyring,
  627. keyring->datalen + KEYQUOTA_LINK_BYTES);
  628. if (ret < 0)
  629. goto error2;
  630. if (klist && klist->nkeys < klist->maxkeys) {
  631. /* there's sufficient slack space to add directly */
  632. atomic_inc(&key->usage);
  633. klist->keys[klist->nkeys] = key;
  634. smp_wmb();
  635. klist->nkeys++;
  636. smp_wmb();
  637. }
  638. else {
  639. /* grow the key list */
  640. max = 4;
  641. if (klist)
  642. max += klist->maxkeys;
  643. ret = -ENFILE;
  644. if (max > 65535)
  645. goto error3;
  646. size = sizeof(*klist) + sizeof(struct key *) * max;
  647. if (size > PAGE_SIZE)
  648. goto error3;
  649. ret = -ENOMEM;
  650. nklist = kmalloc(size, GFP_KERNEL);
  651. if (!nklist)
  652. goto error3;
  653. nklist->maxkeys = max;
  654. nklist->nkeys = 0;
  655. if (klist) {
  656. nklist->nkeys = klist->nkeys;
  657. memcpy(nklist->keys,
  658. klist->keys,
  659. sizeof(struct key *) * klist->nkeys);
  660. }
  661. /* add the key into the new space */
  662. atomic_inc(&key->usage);
  663. nklist->keys[nklist->nkeys++] = key;
  664. rcu_assign_pointer(keyring->payload.subscriptions, nklist);
  665. /* dispose of the old keyring list */
  666. if (klist)
  667. call_rcu(&klist->rcu, keyring_link_rcu_disposal);
  668. }
  669. done:
  670. ret = 0;
  671. error2:
  672. up_write(&keyring_serialise_link_sem);
  673. error:
  674. return ret;
  675. error3:
  676. /* undo the quota changes */
  677. key_payload_reserve(keyring,
  678. keyring->datalen - KEYQUOTA_LINK_BYTES);
  679. goto error2;
  680. } /* end __key_link() */
  681. /*****************************************************************************/
  682. /*
  683. * link a key to a keyring
  684. */
  685. int key_link(struct key *keyring, struct key *key)
  686. {
  687. int ret;
  688. key_check(keyring);
  689. key_check(key);
  690. down_write(&keyring->sem);
  691. ret = __key_link(keyring, key);
  692. up_write(&keyring->sem);
  693. return ret;
  694. } /* end key_link() */
  695. EXPORT_SYMBOL(key_link);
  696. /*****************************************************************************/
  697. /*
  698. * unlink the first link to a key from a keyring
  699. */
  700. int key_unlink(struct key *keyring, struct key *key)
  701. {
  702. struct keyring_list *klist, *nklist;
  703. int loop, ret;
  704. key_check(keyring);
  705. key_check(key);
  706. ret = -ENOTDIR;
  707. if (keyring->type != &key_type_keyring)
  708. goto error;
  709. down_write(&keyring->sem);
  710. klist = rcu_dereference_locked_keyring(keyring);
  711. if (klist) {
  712. /* search the keyring for the key */
  713. for (loop = 0; loop < klist->nkeys; loop++)
  714. if (klist->keys[loop] == key)
  715. goto key_is_present;
  716. }
  717. up_write(&keyring->sem);
  718. ret = -ENOENT;
  719. goto error;
  720. key_is_present:
  721. /* we need to copy the key list for RCU purposes */
  722. nklist = kmalloc(sizeof(*klist) +
  723. sizeof(struct key *) * klist->maxkeys,
  724. GFP_KERNEL);
  725. if (!nklist)
  726. goto nomem;
  727. nklist->maxkeys = klist->maxkeys;
  728. nklist->nkeys = klist->nkeys - 1;
  729. if (loop > 0)
  730. memcpy(&nklist->keys[0],
  731. &klist->keys[0],
  732. loop * sizeof(struct key *));
  733. if (loop < nklist->nkeys)
  734. memcpy(&nklist->keys[loop],
  735. &klist->keys[loop + 1],
  736. (nklist->nkeys - loop) * sizeof(struct key *));
  737. /* adjust the user's quota */
  738. key_payload_reserve(keyring,
  739. keyring->datalen - KEYQUOTA_LINK_BYTES);
  740. rcu_assign_pointer(keyring->payload.subscriptions, nklist);
  741. up_write(&keyring->sem);
  742. /* schedule for later cleanup */
  743. klist->delkey = loop;
  744. call_rcu(&klist->rcu, keyring_unlink_rcu_disposal);
  745. ret = 0;
  746. error:
  747. return ret;
  748. nomem:
  749. ret = -ENOMEM;
  750. up_write(&keyring->sem);
  751. goto error;
  752. } /* end key_unlink() */
  753. EXPORT_SYMBOL(key_unlink);
  754. /*****************************************************************************/
  755. /*
  756. * dispose of a keyring list after the RCU grace period, releasing the keys it
  757. * links to
  758. */
  759. static void keyring_clear_rcu_disposal(struct rcu_head *rcu)
  760. {
  761. struct keyring_list *klist;
  762. int loop;
  763. klist = container_of(rcu, struct keyring_list, rcu);
  764. for (loop = klist->nkeys - 1; loop >= 0; loop--)
  765. key_put(klist->keys[loop]);
  766. kfree(klist);
  767. } /* end keyring_clear_rcu_disposal() */
  768. /*****************************************************************************/
  769. /*
  770. * clear the specified process keyring
  771. * - implements keyctl(KEYCTL_CLEAR)
  772. */
  773. int keyring_clear(struct key *keyring)
  774. {
  775. struct keyring_list *klist;
  776. int ret;
  777. ret = -ENOTDIR;
  778. if (keyring->type == &key_type_keyring) {
  779. /* detach the pointer block with the locks held */
  780. down_write(&keyring->sem);
  781. klist = rcu_dereference_locked_keyring(keyring);
  782. if (klist) {
  783. /* adjust the quota */
  784. key_payload_reserve(keyring,
  785. sizeof(struct keyring_list));
  786. rcu_assign_pointer(keyring->payload.subscriptions,
  787. NULL);
  788. }
  789. up_write(&keyring->sem);
  790. /* free the keys after the locks have been dropped */
  791. if (klist)
  792. call_rcu(&klist->rcu, keyring_clear_rcu_disposal);
  793. ret = 0;
  794. }
  795. return ret;
  796. } /* end keyring_clear() */
  797. EXPORT_SYMBOL(keyring_clear);
  798. /*****************************************************************************/
  799. /*
  800. * dispose of the links from a revoked keyring
  801. * - called with the key sem write-locked
  802. */
  803. static void keyring_revoke(struct key *keyring)
  804. {
  805. struct keyring_list *klist;
  806. klist = rcu_dereference_locked_keyring(keyring);
  807. /* adjust the quota */
  808. key_payload_reserve(keyring, 0);
  809. if (klist) {
  810. rcu_assign_pointer(keyring->payload.subscriptions, NULL);
  811. call_rcu(&klist->rcu, keyring_clear_rcu_disposal);
  812. }
  813. } /* end keyring_revoke() */
  814. /*
  815. * Determine whether a key is dead
  816. */
  817. static bool key_is_dead(struct key *key, time_t limit)
  818. {
  819. return test_bit(KEY_FLAG_DEAD, &key->flags) ||
  820. (key->expiry > 0 && key->expiry <= limit);
  821. }
  822. /*
  823. * Collect garbage from the contents of a keyring
  824. */
  825. void keyring_gc(struct key *keyring, time_t limit)
  826. {
  827. struct keyring_list *klist, *new;
  828. struct key *key;
  829. int loop, keep, max;
  830. kenter("{%x,%s}", key_serial(keyring), keyring->description);
  831. down_write(&keyring->sem);
  832. klist = rcu_dereference_locked_keyring(keyring);
  833. if (!klist)
  834. goto no_klist;
  835. /* work out how many subscriptions we're keeping */
  836. keep = 0;
  837. for (loop = klist->nkeys - 1; loop >= 0; loop--)
  838. if (!key_is_dead(klist->keys[loop], limit))
  839. keep++;
  840. if (keep == klist->nkeys)
  841. goto just_return;
  842. /* allocate a new keyring payload */
  843. max = roundup(keep, 4);
  844. new = kmalloc(sizeof(struct keyring_list) + max * sizeof(struct key *),
  845. GFP_KERNEL);
  846. if (!new)
  847. goto nomem;
  848. new->maxkeys = max;
  849. new->nkeys = 0;
  850. new->delkey = 0;
  851. /* install the live keys
  852. * - must take care as expired keys may be updated back to life
  853. */
  854. keep = 0;
  855. for (loop = klist->nkeys - 1; loop >= 0; loop--) {
  856. key = klist->keys[loop];
  857. if (!key_is_dead(key, limit)) {
  858. if (keep >= max)
  859. goto discard_new;
  860. new->keys[keep++] = key_get(key);
  861. }
  862. }
  863. new->nkeys = keep;
  864. /* adjust the quota */
  865. key_payload_reserve(keyring,
  866. sizeof(struct keyring_list) +
  867. KEYQUOTA_LINK_BYTES * keep);
  868. if (keep == 0) {
  869. rcu_assign_pointer(keyring->payload.subscriptions, NULL);
  870. kfree(new);
  871. } else {
  872. rcu_assign_pointer(keyring->payload.subscriptions, new);
  873. }
  874. up_write(&keyring->sem);
  875. call_rcu(&klist->rcu, keyring_clear_rcu_disposal);
  876. kleave(" [yes]");
  877. return;
  878. discard_new:
  879. new->nkeys = keep;
  880. keyring_clear_rcu_disposal(&new->rcu);
  881. up_write(&keyring->sem);
  882. kleave(" [discard]");
  883. return;
  884. just_return:
  885. up_write(&keyring->sem);
  886. kleave(" [no dead]");
  887. return;
  888. no_klist:
  889. up_write(&keyring->sem);
  890. kleave(" [no_klist]");
  891. return;
  892. nomem:
  893. up_write(&keyring->sem);
  894. kleave(" [oom]");
  895. }