keyring.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  1. /* keyring.c: keyring handling
  2. *
  3. * Copyright (C) 2004 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. sklist = source->payload.subscriptions;
  112. if (sklist && sklist->nkeys > 0) {
  113. max = sklist->nkeys;
  114. BUG_ON(max > limit);
  115. max = (max + 3) & ~3;
  116. if (max > limit)
  117. max = limit;
  118. ret = -ENOMEM;
  119. size = sizeof(*klist) + sizeof(struct key) * max;
  120. klist = kmalloc(size, GFP_KERNEL);
  121. if (!klist)
  122. goto error;
  123. klist->maxkeys = max;
  124. klist->nkeys = sklist->nkeys;
  125. memcpy(klist->keys,
  126. sklist->keys,
  127. sklist->nkeys * sizeof(struct key));
  128. for (loop = klist->nkeys - 1; loop >= 0; loop--)
  129. atomic_inc(&klist->keys[loop]->usage);
  130. keyring->payload.subscriptions = klist;
  131. ret = 0;
  132. }
  133. error:
  134. return ret;
  135. } /* end keyring_duplicate() */
  136. /*****************************************************************************/
  137. /*
  138. * match keyrings on their name
  139. */
  140. static int keyring_match(const struct key *keyring, const void *description)
  141. {
  142. return keyring->description &&
  143. strcmp(keyring->description, description) == 0;
  144. } /* end keyring_match() */
  145. /*****************************************************************************/
  146. /*
  147. * dispose of the data dangling from the corpse of a keyring
  148. */
  149. static void keyring_destroy(struct key *keyring)
  150. {
  151. struct keyring_list *klist;
  152. int loop;
  153. if (keyring->description) {
  154. write_lock(&keyring_name_lock);
  155. list_del(&keyring->type_data.link);
  156. write_unlock(&keyring_name_lock);
  157. }
  158. klist = keyring->payload.subscriptions;
  159. if (klist) {
  160. for (loop = klist->nkeys - 1; loop >= 0; loop--)
  161. key_put(klist->keys[loop]);
  162. kfree(klist);
  163. }
  164. } /* end keyring_destroy() */
  165. /*****************************************************************************/
  166. /*
  167. * describe the keyring
  168. */
  169. static void keyring_describe(const struct key *keyring, struct seq_file *m)
  170. {
  171. struct keyring_list *klist;
  172. if (keyring->description) {
  173. seq_puts(m, keyring->description);
  174. }
  175. else {
  176. seq_puts(m, "[anon]");
  177. }
  178. klist = keyring->payload.subscriptions;
  179. if (klist)
  180. seq_printf(m, ": %u/%u", klist->nkeys, klist->maxkeys);
  181. else
  182. seq_puts(m, ": empty");
  183. } /* end keyring_describe() */
  184. /*****************************************************************************/
  185. /*
  186. * read a list of key IDs from the keyring's contents
  187. */
  188. static long keyring_read(const struct key *keyring,
  189. char __user *buffer, size_t buflen)
  190. {
  191. struct keyring_list *klist;
  192. struct key *key;
  193. size_t qty, tmp;
  194. int loop, ret;
  195. ret = 0;
  196. klist = keyring->payload.subscriptions;
  197. if (klist) {
  198. /* calculate how much data we could return */
  199. qty = klist->nkeys * sizeof(key_serial_t);
  200. if (buffer && buflen > 0) {
  201. if (buflen > qty)
  202. buflen = qty;
  203. /* copy the IDs of the subscribed keys into the
  204. * buffer */
  205. ret = -EFAULT;
  206. for (loop = 0; loop < klist->nkeys; loop++) {
  207. key = klist->keys[loop];
  208. tmp = sizeof(key_serial_t);
  209. if (tmp > buflen)
  210. tmp = buflen;
  211. if (copy_to_user(buffer,
  212. &key->serial,
  213. tmp) != 0)
  214. goto error;
  215. buflen -= tmp;
  216. if (buflen == 0)
  217. break;
  218. buffer += tmp;
  219. }
  220. }
  221. ret = qty;
  222. }
  223. error:
  224. return ret;
  225. } /* end keyring_read() */
  226. /*****************************************************************************/
  227. /*
  228. * allocate a keyring and link into the destination keyring
  229. */
  230. struct key *keyring_alloc(const char *description, uid_t uid, gid_t gid,
  231. int not_in_quota, struct key *dest)
  232. {
  233. struct key *keyring;
  234. int ret;
  235. keyring = key_alloc(&key_type_keyring, description,
  236. uid, gid, KEY_USR_ALL, not_in_quota);
  237. if (!IS_ERR(keyring)) {
  238. ret = key_instantiate_and_link(keyring, NULL, 0, dest);
  239. if (ret < 0) {
  240. key_put(keyring);
  241. keyring = ERR_PTR(ret);
  242. }
  243. }
  244. return keyring;
  245. } /* end keyring_alloc() */
  246. /*****************************************************************************/
  247. /*
  248. * search the supplied keyring tree for a key that matches the criterion
  249. * - perform a breadth-then-depth search up to the prescribed limit
  250. * - we only find keys on which we have search permission
  251. * - we use the supplied match function to see if the description (or other
  252. * feature of interest) matches
  253. * - we readlock the keyrings as we search down the tree
  254. * - we return -EAGAIN if we didn't find any matching key
  255. * - we return -ENOKEY if we only found negative matching keys
  256. */
  257. struct key *keyring_search_aux(struct key *keyring,
  258. struct key_type *type,
  259. const void *description,
  260. key_match_func_t match)
  261. {
  262. struct {
  263. struct key *keyring;
  264. int kix;
  265. } stack[KEYRING_SEARCH_MAX_DEPTH];
  266. struct keyring_list *keylist;
  267. struct timespec now;
  268. struct key *key;
  269. long err;
  270. int sp, psp, kix;
  271. key_check(keyring);
  272. /* top keyring must have search permission to begin the search */
  273. key = ERR_PTR(-EACCES);
  274. if (!key_permission(keyring, KEY_SEARCH))
  275. goto error;
  276. key = ERR_PTR(-ENOTDIR);
  277. if (keyring->type != &key_type_keyring)
  278. goto error;
  279. now = current_kernel_time();
  280. err = -EAGAIN;
  281. sp = 0;
  282. /* start processing a new keyring */
  283. descend:
  284. read_lock(&keyring->lock);
  285. if (keyring->flags & KEY_FLAG_REVOKED)
  286. goto not_this_keyring;
  287. keylist = keyring->payload.subscriptions;
  288. if (!keylist)
  289. goto not_this_keyring;
  290. /* iterate through the keys in this keyring first */
  291. for (kix = 0; kix < keylist->nkeys; kix++) {
  292. key = keylist->keys[kix];
  293. /* ignore keys not of this type */
  294. if (key->type != type)
  295. continue;
  296. /* skip revoked keys and expired keys */
  297. if (key->flags & KEY_FLAG_REVOKED)
  298. continue;
  299. if (key->expiry && now.tv_sec >= key->expiry)
  300. continue;
  301. /* keys that don't match */
  302. if (!match(key, description))
  303. continue;
  304. /* key must have search permissions */
  305. if (!key_permission(key, KEY_SEARCH))
  306. continue;
  307. /* we set a different error code if we find a negative key */
  308. if (key->flags & KEY_FLAG_NEGATIVE) {
  309. err = -ENOKEY;
  310. continue;
  311. }
  312. goto found;
  313. }
  314. /* search through the keyrings nested in this one */
  315. kix = 0;
  316. ascend:
  317. while (kix < keylist->nkeys) {
  318. key = keylist->keys[kix];
  319. if (key->type != &key_type_keyring)
  320. goto next;
  321. /* recursively search nested keyrings
  322. * - only search keyrings for which we have search permission
  323. */
  324. if (sp >= KEYRING_SEARCH_MAX_DEPTH)
  325. goto next;
  326. if (!key_permission(key, KEY_SEARCH))
  327. goto next;
  328. /* evade loops in the keyring tree */
  329. for (psp = 0; psp < sp; psp++)
  330. if (stack[psp].keyring == keyring)
  331. goto next;
  332. /* stack the current position */
  333. stack[sp].keyring = keyring;
  334. stack[sp].kix = kix;
  335. sp++;
  336. /* begin again with the new keyring */
  337. keyring = key;
  338. goto descend;
  339. next:
  340. kix++;
  341. }
  342. /* the keyring we're looking at was disqualified or didn't contain a
  343. * matching key */
  344. not_this_keyring:
  345. read_unlock(&keyring->lock);
  346. if (sp > 0) {
  347. /* resume the processing of a keyring higher up in the tree */
  348. sp--;
  349. keyring = stack[sp].keyring;
  350. keylist = keyring->payload.subscriptions;
  351. kix = stack[sp].kix + 1;
  352. goto ascend;
  353. }
  354. key = ERR_PTR(err);
  355. goto error;
  356. /* we found a viable match */
  357. found:
  358. atomic_inc(&key->usage);
  359. read_unlock(&keyring->lock);
  360. /* unwind the keyring stack */
  361. while (sp > 0) {
  362. sp--;
  363. read_unlock(&stack[sp].keyring->lock);
  364. }
  365. key_check(key);
  366. error:
  367. return key;
  368. } /* end keyring_search_aux() */
  369. /*****************************************************************************/
  370. /*
  371. * search the supplied keyring tree for a key that matches the criterion
  372. * - perform a breadth-then-depth search up to the prescribed limit
  373. * - we only find keys on which we have search permission
  374. * - we readlock the keyrings as we search down the tree
  375. * - we return -EAGAIN if we didn't find any matching key
  376. * - we return -ENOKEY if we only found negative matching keys
  377. */
  378. struct key *keyring_search(struct key *keyring,
  379. struct key_type *type,
  380. const char *description)
  381. {
  382. return keyring_search_aux(keyring, type, description, type->match);
  383. } /* end keyring_search() */
  384. EXPORT_SYMBOL(keyring_search);
  385. /*****************************************************************************/
  386. /*
  387. * search the given keyring only (no recursion)
  388. * - keyring must be locked by caller
  389. */
  390. struct key *__keyring_search_one(struct key *keyring,
  391. const struct key_type *ktype,
  392. const char *description,
  393. key_perm_t perm)
  394. {
  395. struct keyring_list *klist;
  396. struct key *key;
  397. int loop;
  398. klist = keyring->payload.subscriptions;
  399. if (klist) {
  400. for (loop = 0; loop < klist->nkeys; loop++) {
  401. key = klist->keys[loop];
  402. if (key->type == ktype &&
  403. key->type->match(key, description) &&
  404. key_permission(key, perm) &&
  405. !(key->flags & KEY_FLAG_REVOKED)
  406. )
  407. goto found;
  408. }
  409. }
  410. key = ERR_PTR(-ENOKEY);
  411. goto error;
  412. found:
  413. atomic_inc(&key->usage);
  414. error:
  415. return key;
  416. } /* end __keyring_search_one() */
  417. /*****************************************************************************/
  418. /*
  419. * find a keyring with the specified name
  420. * - all named keyrings are searched
  421. * - only find keyrings with search permission for the process
  422. * - only find keyrings with a serial number greater than the one specified
  423. */
  424. struct key *find_keyring_by_name(const char *name, key_serial_t bound)
  425. {
  426. struct key *keyring;
  427. int bucket;
  428. keyring = ERR_PTR(-EINVAL);
  429. if (!name)
  430. goto error;
  431. bucket = keyring_hash(name);
  432. read_lock(&keyring_name_lock);
  433. if (keyring_name_hash[bucket].next) {
  434. /* search this hash bucket for a keyring with a matching name
  435. * that's readable and that hasn't been revoked */
  436. list_for_each_entry(keyring,
  437. &keyring_name_hash[bucket],
  438. type_data.link
  439. ) {
  440. if (keyring->flags & KEY_FLAG_REVOKED)
  441. continue;
  442. if (strcmp(keyring->description, name) != 0)
  443. continue;
  444. if (!key_permission(keyring, KEY_SEARCH))
  445. continue;
  446. /* found a potential candidate, but we still need to
  447. * check the serial number */
  448. if (keyring->serial <= bound)
  449. continue;
  450. /* we've got a match */
  451. atomic_inc(&keyring->usage);
  452. read_unlock(&keyring_name_lock);
  453. goto error;
  454. }
  455. }
  456. read_unlock(&keyring_name_lock);
  457. keyring = ERR_PTR(-ENOKEY);
  458. error:
  459. return keyring;
  460. } /* end find_keyring_by_name() */
  461. /*****************************************************************************/
  462. /*
  463. * see if a cycle will will be created by inserting acyclic tree B in acyclic
  464. * tree A at the topmost level (ie: as a direct child of A)
  465. * - since we are adding B to A at the top level, checking for cycles should
  466. * just be a matter of seeing if node A is somewhere in tree B
  467. */
  468. static int keyring_detect_cycle(struct key *A, struct key *B)
  469. {
  470. struct {
  471. struct key *subtree;
  472. int kix;
  473. } stack[KEYRING_SEARCH_MAX_DEPTH];
  474. struct keyring_list *keylist;
  475. struct key *subtree, *key;
  476. int sp, kix, ret;
  477. ret = -EDEADLK;
  478. if (A == B)
  479. goto error;
  480. subtree = B;
  481. sp = 0;
  482. /* start processing a new keyring */
  483. descend:
  484. read_lock(&subtree->lock);
  485. if (subtree->flags & KEY_FLAG_REVOKED)
  486. goto not_this_keyring;
  487. keylist = subtree->payload.subscriptions;
  488. if (!keylist)
  489. goto not_this_keyring;
  490. kix = 0;
  491. ascend:
  492. /* iterate through the remaining keys in this keyring */
  493. for (; kix < keylist->nkeys; kix++) {
  494. key = keylist->keys[kix];
  495. if (key == A)
  496. goto cycle_detected;
  497. /* recursively check nested keyrings */
  498. if (key->type == &key_type_keyring) {
  499. if (sp >= KEYRING_SEARCH_MAX_DEPTH)
  500. goto too_deep;
  501. /* stack the current position */
  502. stack[sp].subtree = subtree;
  503. stack[sp].kix = kix;
  504. sp++;
  505. /* begin again with the new keyring */
  506. subtree = key;
  507. goto descend;
  508. }
  509. }
  510. /* the keyring we're looking at was disqualified or didn't contain a
  511. * matching key */
  512. not_this_keyring:
  513. read_unlock(&subtree->lock);
  514. if (sp > 0) {
  515. /* resume the checking of a keyring higher up in the tree */
  516. sp--;
  517. subtree = stack[sp].subtree;
  518. keylist = subtree->payload.subscriptions;
  519. kix = stack[sp].kix + 1;
  520. goto ascend;
  521. }
  522. ret = 0; /* no cycles detected */
  523. error:
  524. return ret;
  525. too_deep:
  526. ret = -ELOOP;
  527. goto error_unwind;
  528. cycle_detected:
  529. ret = -EDEADLK;
  530. error_unwind:
  531. read_unlock(&subtree->lock);
  532. /* unwind the keyring stack */
  533. while (sp > 0) {
  534. sp--;
  535. read_unlock(&stack[sp].subtree->lock);
  536. }
  537. goto error;
  538. } /* end keyring_detect_cycle() */
  539. /*****************************************************************************/
  540. /*
  541. * link a key into to a keyring
  542. * - must be called with the keyring's semaphore held
  543. */
  544. int __key_link(struct key *keyring, struct key *key)
  545. {
  546. struct keyring_list *klist, *nklist;
  547. unsigned max;
  548. size_t size;
  549. int ret;
  550. ret = -EKEYREVOKED;
  551. if (keyring->flags & KEY_FLAG_REVOKED)
  552. goto error;
  553. ret = -ENOTDIR;
  554. if (keyring->type != &key_type_keyring)
  555. goto error;
  556. /* serialise link/link calls to prevent parallel calls causing a
  557. * cycle when applied to two keyring in opposite orders */
  558. down_write(&keyring_serialise_link_sem);
  559. /* check that we aren't going to create a cycle adding one keyring to
  560. * another */
  561. if (key->type == &key_type_keyring) {
  562. ret = keyring_detect_cycle(keyring, key);
  563. if (ret < 0)
  564. goto error2;
  565. }
  566. /* check that we aren't going to overrun the user's quota */
  567. ret = key_payload_reserve(keyring,
  568. keyring->datalen + KEYQUOTA_LINK_BYTES);
  569. if (ret < 0)
  570. goto error2;
  571. klist = keyring->payload.subscriptions;
  572. if (klist && klist->nkeys < klist->maxkeys) {
  573. /* there's sufficient slack space to add directly */
  574. atomic_inc(&key->usage);
  575. write_lock(&keyring->lock);
  576. klist->keys[klist->nkeys++] = key;
  577. write_unlock(&keyring->lock);
  578. ret = 0;
  579. }
  580. else {
  581. /* grow the key list */
  582. max = 4;
  583. if (klist)
  584. max += klist->maxkeys;
  585. ret = -ENFILE;
  586. size = sizeof(*klist) + sizeof(*key) * max;
  587. if (size > PAGE_SIZE)
  588. goto error3;
  589. ret = -ENOMEM;
  590. nklist = kmalloc(size, GFP_KERNEL);
  591. if (!nklist)
  592. goto error3;
  593. nklist->maxkeys = max;
  594. nklist->nkeys = 0;
  595. if (klist) {
  596. nklist->nkeys = klist->nkeys;
  597. memcpy(nklist->keys,
  598. klist->keys,
  599. sizeof(struct key *) * klist->nkeys);
  600. }
  601. /* add the key into the new space */
  602. atomic_inc(&key->usage);
  603. write_lock(&keyring->lock);
  604. keyring->payload.subscriptions = nklist;
  605. nklist->keys[nklist->nkeys++] = key;
  606. write_unlock(&keyring->lock);
  607. /* dispose of the old keyring list */
  608. kfree(klist);
  609. ret = 0;
  610. }
  611. error2:
  612. up_write(&keyring_serialise_link_sem);
  613. error:
  614. return ret;
  615. error3:
  616. /* undo the quota changes */
  617. key_payload_reserve(keyring,
  618. keyring->datalen - KEYQUOTA_LINK_BYTES);
  619. goto error2;
  620. } /* end __key_link() */
  621. /*****************************************************************************/
  622. /*
  623. * link a key to a keyring
  624. */
  625. int key_link(struct key *keyring, struct key *key)
  626. {
  627. int ret;
  628. key_check(keyring);
  629. key_check(key);
  630. down_write(&keyring->sem);
  631. ret = __key_link(keyring, key);
  632. up_write(&keyring->sem);
  633. return ret;
  634. } /* end key_link() */
  635. EXPORT_SYMBOL(key_link);
  636. /*****************************************************************************/
  637. /*
  638. * unlink the first link to a key from a keyring
  639. */
  640. int key_unlink(struct key *keyring, struct key *key)
  641. {
  642. struct keyring_list *klist;
  643. int loop, ret;
  644. key_check(keyring);
  645. key_check(key);
  646. ret = -ENOTDIR;
  647. if (keyring->type != &key_type_keyring)
  648. goto error;
  649. down_write(&keyring->sem);
  650. klist = keyring->payload.subscriptions;
  651. if (klist) {
  652. /* search the keyring for the key */
  653. for (loop = 0; loop < klist->nkeys; loop++)
  654. if (klist->keys[loop] == key)
  655. goto key_is_present;
  656. }
  657. up_write(&keyring->sem);
  658. ret = -ENOENT;
  659. goto error;
  660. key_is_present:
  661. /* adjust the user's quota */
  662. key_payload_reserve(keyring,
  663. keyring->datalen - KEYQUOTA_LINK_BYTES);
  664. /* shuffle down the key pointers
  665. * - it might be worth shrinking the allocated memory, but that runs
  666. * the risk of ENOMEM as we would have to copy
  667. */
  668. write_lock(&keyring->lock);
  669. klist->nkeys--;
  670. if (loop < klist->nkeys)
  671. memcpy(&klist->keys[loop],
  672. &klist->keys[loop + 1],
  673. (klist->nkeys - loop) * sizeof(struct key *));
  674. write_unlock(&keyring->lock);
  675. up_write(&keyring->sem);
  676. key_put(key);
  677. ret = 0;
  678. error:
  679. return ret;
  680. } /* end key_unlink() */
  681. EXPORT_SYMBOL(key_unlink);
  682. /*****************************************************************************/
  683. /*
  684. * clear the specified process keyring
  685. * - implements keyctl(KEYCTL_CLEAR)
  686. */
  687. int keyring_clear(struct key *keyring)
  688. {
  689. struct keyring_list *klist;
  690. int loop, ret;
  691. ret = -ENOTDIR;
  692. if (keyring->type == &key_type_keyring) {
  693. /* detach the pointer block with the locks held */
  694. down_write(&keyring->sem);
  695. klist = keyring->payload.subscriptions;
  696. if (klist) {
  697. /* adjust the quota */
  698. key_payload_reserve(keyring,
  699. sizeof(struct keyring_list));
  700. write_lock(&keyring->lock);
  701. keyring->payload.subscriptions = NULL;
  702. write_unlock(&keyring->lock);
  703. }
  704. up_write(&keyring->sem);
  705. /* free the keys after the locks have been dropped */
  706. if (klist) {
  707. for (loop = klist->nkeys - 1; loop >= 0; loop--)
  708. key_put(klist->keys[loop]);
  709. kfree(klist);
  710. }
  711. ret = 0;
  712. }
  713. return ret;
  714. } /* end keyring_clear() */
  715. EXPORT_SYMBOL(keyring_clear);