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 = rcu_dereference(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. keyring = ERR_PTR(-EINVAL);
  436. if (!name)
  437. goto error;
  438. bucket = keyring_hash(name);
  439. read_lock(&keyring_name_lock);
  440. if (keyring_name_hash[bucket].next) {
  441. /* search this hash bucket for a keyring with a matching name
  442. * that's readable and that hasn't been revoked */
  443. list_for_each_entry(keyring,
  444. &keyring_name_hash[bucket],
  445. type_data.link
  446. ) {
  447. if (keyring->user->user_ns != current_user_ns())
  448. continue;
  449. if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
  450. continue;
  451. if (strcmp(keyring->description, name) != 0)
  452. continue;
  453. if (!skip_perm_check &&
  454. key_permission(make_key_ref(keyring, 0),
  455. KEY_SEARCH) < 0)
  456. continue;
  457. /* we've got a match */
  458. atomic_inc(&keyring->usage);
  459. read_unlock(&keyring_name_lock);
  460. goto error;
  461. }
  462. }
  463. read_unlock(&keyring_name_lock);
  464. keyring = ERR_PTR(-ENOKEY);
  465. error:
  466. return keyring;
  467. } /* end find_keyring_by_name() */
  468. /*****************************************************************************/
  469. /*
  470. * see if a cycle will will be created by inserting acyclic tree B in acyclic
  471. * tree A at the topmost level (ie: as a direct child of A)
  472. * - since we are adding B to A at the top level, checking for cycles should
  473. * just be a matter of seeing if node A is somewhere in tree B
  474. */
  475. static int keyring_detect_cycle(struct key *A, struct key *B)
  476. {
  477. struct {
  478. struct keyring_list *keylist;
  479. int kix;
  480. } stack[KEYRING_SEARCH_MAX_DEPTH];
  481. struct keyring_list *keylist;
  482. struct key *subtree, *key;
  483. int sp, kix, ret;
  484. rcu_read_lock();
  485. ret = -EDEADLK;
  486. if (A == B)
  487. goto cycle_detected;
  488. subtree = B;
  489. sp = 0;
  490. /* start processing a new keyring */
  491. descend:
  492. if (test_bit(KEY_FLAG_REVOKED, &subtree->flags))
  493. goto not_this_keyring;
  494. keylist = rcu_dereference(subtree->payload.subscriptions);
  495. if (!keylist)
  496. goto not_this_keyring;
  497. kix = 0;
  498. ascend:
  499. /* iterate through the remaining keys in this keyring */
  500. for (; kix < keylist->nkeys; kix++) {
  501. key = keylist->keys[kix];
  502. if (key == A)
  503. goto cycle_detected;
  504. /* recursively check nested keyrings */
  505. if (key->type == &key_type_keyring) {
  506. if (sp >= KEYRING_SEARCH_MAX_DEPTH)
  507. goto too_deep;
  508. /* stack the current position */
  509. stack[sp].keylist = keylist;
  510. stack[sp].kix = kix;
  511. sp++;
  512. /* begin again with the new keyring */
  513. subtree = key;
  514. goto descend;
  515. }
  516. }
  517. /* the keyring we're looking at was disqualified or didn't contain a
  518. * matching key */
  519. not_this_keyring:
  520. if (sp > 0) {
  521. /* resume the checking of a keyring higher up in the tree */
  522. sp--;
  523. keylist = stack[sp].keylist;
  524. kix = stack[sp].kix + 1;
  525. goto ascend;
  526. }
  527. ret = 0; /* no cycles detected */
  528. error:
  529. rcu_read_unlock();
  530. return ret;
  531. too_deep:
  532. ret = -ELOOP;
  533. goto error;
  534. cycle_detected:
  535. ret = -EDEADLK;
  536. goto error;
  537. } /* end keyring_detect_cycle() */
  538. /*****************************************************************************/
  539. /*
  540. * dispose of a keyring list after the RCU grace period
  541. */
  542. static void keyring_link_rcu_disposal(struct rcu_head *rcu)
  543. {
  544. struct keyring_list *klist =
  545. container_of(rcu, struct keyring_list, rcu);
  546. kfree(klist);
  547. } /* end keyring_link_rcu_disposal() */
  548. /*****************************************************************************/
  549. /*
  550. * dispose of a keyring list after the RCU grace period, freeing the unlinked
  551. * key
  552. */
  553. static void keyring_unlink_rcu_disposal(struct rcu_head *rcu)
  554. {
  555. struct keyring_list *klist =
  556. container_of(rcu, struct keyring_list, rcu);
  557. key_put(klist->keys[klist->delkey]);
  558. kfree(klist);
  559. } /* end keyring_unlink_rcu_disposal() */
  560. /*****************************************************************************/
  561. /*
  562. * link a key into to a keyring
  563. * - must be called with the keyring's semaphore write-locked
  564. * - discard already extant link to matching key if there is one
  565. */
  566. int __key_link(struct key *keyring, struct key *key)
  567. {
  568. struct keyring_list *klist, *nklist;
  569. unsigned max;
  570. size_t size;
  571. int loop, ret;
  572. ret = -EKEYREVOKED;
  573. if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
  574. goto error;
  575. ret = -ENOTDIR;
  576. if (keyring->type != &key_type_keyring)
  577. goto error;
  578. /* serialise link/link calls to prevent parallel calls causing a
  579. * cycle when applied to two keyring in opposite orders */
  580. down_write(&keyring_serialise_link_sem);
  581. /* check that we aren't going to create a cycle adding one keyring to
  582. * another */
  583. if (key->type == &key_type_keyring) {
  584. ret = keyring_detect_cycle(keyring, key);
  585. if (ret < 0)
  586. goto error2;
  587. }
  588. /* see if there's a matching key we can displace */
  589. klist = keyring->payload.subscriptions;
  590. if (klist && klist->nkeys > 0) {
  591. struct key_type *type = key->type;
  592. for (loop = klist->nkeys - 1; loop >= 0; loop--) {
  593. if (klist->keys[loop]->type == type &&
  594. strcmp(klist->keys[loop]->description,
  595. key->description) == 0
  596. ) {
  597. /* found a match - replace with new key */
  598. size = sizeof(struct key *) * klist->maxkeys;
  599. size += sizeof(*klist);
  600. BUG_ON(size > PAGE_SIZE);
  601. ret = -ENOMEM;
  602. nklist = kmemdup(klist, size, GFP_KERNEL);
  603. if (!nklist)
  604. goto error2;
  605. /* replace matched key */
  606. atomic_inc(&key->usage);
  607. nklist->keys[loop] = key;
  608. rcu_assign_pointer(
  609. keyring->payload.subscriptions,
  610. nklist);
  611. /* dispose of the old keyring list and the
  612. * displaced key */
  613. klist->delkey = loop;
  614. call_rcu(&klist->rcu,
  615. keyring_unlink_rcu_disposal);
  616. goto done;
  617. }
  618. }
  619. }
  620. /* check that we aren't going to overrun the user's quota */
  621. ret = key_payload_reserve(keyring,
  622. keyring->datalen + KEYQUOTA_LINK_BYTES);
  623. if (ret < 0)
  624. goto error2;
  625. klist = keyring->payload.subscriptions;
  626. if (klist && klist->nkeys < klist->maxkeys) {
  627. /* there's sufficient slack space to add directly */
  628. atomic_inc(&key->usage);
  629. klist->keys[klist->nkeys] = key;
  630. smp_wmb();
  631. klist->nkeys++;
  632. smp_wmb();
  633. }
  634. else {
  635. /* grow the key list */
  636. max = 4;
  637. if (klist)
  638. max += klist->maxkeys;
  639. ret = -ENFILE;
  640. if (max > 65535)
  641. goto error3;
  642. size = sizeof(*klist) + sizeof(struct key *) * max;
  643. if (size > PAGE_SIZE)
  644. goto error3;
  645. ret = -ENOMEM;
  646. nklist = kmalloc(size, GFP_KERNEL);
  647. if (!nklist)
  648. goto error3;
  649. nklist->maxkeys = max;
  650. nklist->nkeys = 0;
  651. if (klist) {
  652. nklist->nkeys = klist->nkeys;
  653. memcpy(nklist->keys,
  654. klist->keys,
  655. sizeof(struct key *) * klist->nkeys);
  656. }
  657. /* add the key into the new space */
  658. atomic_inc(&key->usage);
  659. nklist->keys[nklist->nkeys++] = key;
  660. rcu_assign_pointer(keyring->payload.subscriptions, nklist);
  661. /* dispose of the old keyring list */
  662. if (klist)
  663. call_rcu(&klist->rcu, keyring_link_rcu_disposal);
  664. }
  665. done:
  666. ret = 0;
  667. error2:
  668. up_write(&keyring_serialise_link_sem);
  669. error:
  670. return ret;
  671. error3:
  672. /* undo the quota changes */
  673. key_payload_reserve(keyring,
  674. keyring->datalen - KEYQUOTA_LINK_BYTES);
  675. goto error2;
  676. } /* end __key_link() */
  677. /*****************************************************************************/
  678. /*
  679. * link a key to a keyring
  680. */
  681. int key_link(struct key *keyring, struct key *key)
  682. {
  683. int ret;
  684. key_check(keyring);
  685. key_check(key);
  686. down_write(&keyring->sem);
  687. ret = __key_link(keyring, key);
  688. up_write(&keyring->sem);
  689. return ret;
  690. } /* end key_link() */
  691. EXPORT_SYMBOL(key_link);
  692. /*****************************************************************************/
  693. /*
  694. * unlink the first link to a key from a keyring
  695. */
  696. int key_unlink(struct key *keyring, struct key *key)
  697. {
  698. struct keyring_list *klist, *nklist;
  699. int loop, ret;
  700. key_check(keyring);
  701. key_check(key);
  702. ret = -ENOTDIR;
  703. if (keyring->type != &key_type_keyring)
  704. goto error;
  705. down_write(&keyring->sem);
  706. klist = keyring->payload.subscriptions;
  707. if (klist) {
  708. /* search the keyring for the key */
  709. for (loop = 0; loop < klist->nkeys; loop++)
  710. if (klist->keys[loop] == key)
  711. goto key_is_present;
  712. }
  713. up_write(&keyring->sem);
  714. ret = -ENOENT;
  715. goto error;
  716. key_is_present:
  717. /* we need to copy the key list for RCU purposes */
  718. nklist = kmalloc(sizeof(*klist) +
  719. sizeof(struct key *) * klist->maxkeys,
  720. GFP_KERNEL);
  721. if (!nklist)
  722. goto nomem;
  723. nklist->maxkeys = klist->maxkeys;
  724. nklist->nkeys = klist->nkeys - 1;
  725. if (loop > 0)
  726. memcpy(&nklist->keys[0],
  727. &klist->keys[0],
  728. loop * sizeof(struct key *));
  729. if (loop < nklist->nkeys)
  730. memcpy(&nklist->keys[loop],
  731. &klist->keys[loop + 1],
  732. (nklist->nkeys - loop) * sizeof(struct key *));
  733. /* adjust the user's quota */
  734. key_payload_reserve(keyring,
  735. keyring->datalen - KEYQUOTA_LINK_BYTES);
  736. rcu_assign_pointer(keyring->payload.subscriptions, nklist);
  737. up_write(&keyring->sem);
  738. /* schedule for later cleanup */
  739. klist->delkey = loop;
  740. call_rcu(&klist->rcu, keyring_unlink_rcu_disposal);
  741. ret = 0;
  742. error:
  743. return ret;
  744. nomem:
  745. ret = -ENOMEM;
  746. up_write(&keyring->sem);
  747. goto error;
  748. } /* end key_unlink() */
  749. EXPORT_SYMBOL(key_unlink);
  750. /*****************************************************************************/
  751. /*
  752. * dispose of a keyring list after the RCU grace period, releasing the keys it
  753. * links to
  754. */
  755. static void keyring_clear_rcu_disposal(struct rcu_head *rcu)
  756. {
  757. struct keyring_list *klist;
  758. int loop;
  759. klist = container_of(rcu, struct keyring_list, rcu);
  760. for (loop = klist->nkeys - 1; loop >= 0; loop--)
  761. key_put(klist->keys[loop]);
  762. kfree(klist);
  763. } /* end keyring_clear_rcu_disposal() */
  764. /*****************************************************************************/
  765. /*
  766. * clear the specified process keyring
  767. * - implements keyctl(KEYCTL_CLEAR)
  768. */
  769. int keyring_clear(struct key *keyring)
  770. {
  771. struct keyring_list *klist;
  772. int ret;
  773. ret = -ENOTDIR;
  774. if (keyring->type == &key_type_keyring) {
  775. /* detach the pointer block with the locks held */
  776. down_write(&keyring->sem);
  777. klist = keyring->payload.subscriptions;
  778. if (klist) {
  779. /* adjust the quota */
  780. key_payload_reserve(keyring,
  781. sizeof(struct keyring_list));
  782. rcu_assign_pointer(keyring->payload.subscriptions,
  783. NULL);
  784. }
  785. up_write(&keyring->sem);
  786. /* free the keys after the locks have been dropped */
  787. if (klist)
  788. call_rcu(&klist->rcu, keyring_clear_rcu_disposal);
  789. ret = 0;
  790. }
  791. return ret;
  792. } /* end keyring_clear() */
  793. EXPORT_SYMBOL(keyring_clear);
  794. /*****************************************************************************/
  795. /*
  796. * dispose of the links from a revoked keyring
  797. * - called with the key sem write-locked
  798. */
  799. static void keyring_revoke(struct key *keyring)
  800. {
  801. struct keyring_list *klist = keyring->payload.subscriptions;
  802. /* adjust the quota */
  803. key_payload_reserve(keyring, 0);
  804. if (klist) {
  805. rcu_assign_pointer(keyring->payload.subscriptions, NULL);
  806. call_rcu(&klist->rcu, keyring_clear_rcu_disposal);
  807. }
  808. } /* end keyring_revoke() */
  809. /*
  810. * Determine whether a key is dead
  811. */
  812. static bool key_is_dead(struct key *key, time_t limit)
  813. {
  814. return test_bit(KEY_FLAG_DEAD, &key->flags) ||
  815. (key->expiry > 0 && key->expiry <= limit);
  816. }
  817. /*
  818. * Collect garbage from the contents of a keyring
  819. */
  820. void keyring_gc(struct key *keyring, time_t limit)
  821. {
  822. struct keyring_list *klist, *new;
  823. struct key *key;
  824. int loop, keep, max;
  825. kenter("{%x,%s}", key_serial(keyring), keyring->description);
  826. down_write(&keyring->sem);
  827. klist = keyring->payload.subscriptions;
  828. if (!klist)
  829. goto no_klist;
  830. /* work out how many subscriptions we're keeping */
  831. keep = 0;
  832. for (loop = klist->nkeys - 1; loop >= 0; loop--)
  833. if (!key_is_dead(klist->keys[loop], limit))
  834. keep++;
  835. if (keep == klist->nkeys)
  836. goto just_return;
  837. /* allocate a new keyring payload */
  838. max = roundup(keep, 4);
  839. new = kmalloc(sizeof(struct keyring_list) + max * sizeof(struct key *),
  840. GFP_KERNEL);
  841. if (!new)
  842. goto nomem;
  843. new->maxkeys = max;
  844. new->nkeys = 0;
  845. new->delkey = 0;
  846. /* install the live keys
  847. * - must take care as expired keys may be updated back to life
  848. */
  849. keep = 0;
  850. for (loop = klist->nkeys - 1; loop >= 0; loop--) {
  851. key = klist->keys[loop];
  852. if (!key_is_dead(key, limit)) {
  853. if (keep >= max)
  854. goto discard_new;
  855. new->keys[keep++] = key_get(key);
  856. }
  857. }
  858. new->nkeys = keep;
  859. /* adjust the quota */
  860. key_payload_reserve(keyring,
  861. sizeof(struct keyring_list) +
  862. KEYQUOTA_LINK_BYTES * keep);
  863. if (keep == 0) {
  864. rcu_assign_pointer(keyring->payload.subscriptions, NULL);
  865. kfree(new);
  866. } else {
  867. rcu_assign_pointer(keyring->payload.subscriptions, new);
  868. }
  869. up_write(&keyring->sem);
  870. call_rcu(&klist->rcu, keyring_clear_rcu_disposal);
  871. kleave(" [yes]");
  872. return;
  873. discard_new:
  874. new->nkeys = keep;
  875. keyring_clear_rcu_disposal(&new->rcu);
  876. up_write(&keyring->sem);
  877. kleave(" [discard]");
  878. return;
  879. just_return:
  880. up_write(&keyring->sem);
  881. kleave(" [no dead]");
  882. return;
  883. no_klist:
  884. up_write(&keyring->sem);
  885. kleave(" [no_klist]");
  886. return;
  887. nomem:
  888. up_write(&keyring->sem);
  889. kleave(" [oom]");
  890. }