keyring.c 30 KB

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