keyring.c 26 KB

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