keyring.c 24 KB

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