keyring.c 24 KB

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