keyring.c 27 KB

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