keyring.c 26 KB

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