key.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118
  1. /* Basic authentication token and access key management
  2. *
  3. * Copyright (C) 2004-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/poison.h>
  14. #include <linux/sched.h>
  15. #include <linux/slab.h>
  16. #include <linux/security.h>
  17. #include <linux/workqueue.h>
  18. #include <linux/random.h>
  19. #include <linux/err.h>
  20. #include <linux/user_namespace.h>
  21. #include "internal.h"
  22. static struct kmem_cache *key_jar;
  23. struct rb_root key_serial_tree; /* tree of keys indexed by serial */
  24. DEFINE_SPINLOCK(key_serial_lock);
  25. struct rb_root key_user_tree; /* tree of quota records indexed by UID */
  26. DEFINE_SPINLOCK(key_user_lock);
  27. unsigned int key_quota_root_maxkeys = 200; /* root's key count quota */
  28. unsigned int key_quota_root_maxbytes = 20000; /* root's key space quota */
  29. unsigned int key_quota_maxkeys = 200; /* general key count quota */
  30. unsigned int key_quota_maxbytes = 20000; /* general key space quota */
  31. static LIST_HEAD(key_types_list);
  32. static DECLARE_RWSEM(key_types_sem);
  33. static void key_cleanup(struct work_struct *work);
  34. static DECLARE_WORK(key_cleanup_task, key_cleanup);
  35. /* We serialise key instantiation and link */
  36. DEFINE_MUTEX(key_construction_mutex);
  37. /* Any key who's type gets unegistered will be re-typed to this */
  38. static struct key_type key_type_dead = {
  39. .name = "dead",
  40. };
  41. #ifdef KEY_DEBUGGING
  42. void __key_check(const struct key *key)
  43. {
  44. printk("__key_check: key %p {%08x} should be {%08x}\n",
  45. key, key->magic, KEY_DEBUG_MAGIC);
  46. BUG();
  47. }
  48. #endif
  49. /*
  50. * Get the key quota record for a user, allocating a new record if one doesn't
  51. * already exist.
  52. */
  53. struct key_user *key_user_lookup(uid_t uid, struct user_namespace *user_ns)
  54. {
  55. struct key_user *candidate = NULL, *user;
  56. struct rb_node *parent = NULL;
  57. struct rb_node **p;
  58. try_again:
  59. p = &key_user_tree.rb_node;
  60. spin_lock(&key_user_lock);
  61. /* search the tree for a user record with a matching UID */
  62. while (*p) {
  63. parent = *p;
  64. user = rb_entry(parent, struct key_user, node);
  65. if (uid < user->uid)
  66. p = &(*p)->rb_left;
  67. else if (uid > user->uid)
  68. p = &(*p)->rb_right;
  69. else if (user_ns < user->user_ns)
  70. p = &(*p)->rb_left;
  71. else if (user_ns > user->user_ns)
  72. p = &(*p)->rb_right;
  73. else
  74. goto found;
  75. }
  76. /* if we get here, we failed to find a match in the tree */
  77. if (!candidate) {
  78. /* allocate a candidate user record if we don't already have
  79. * one */
  80. spin_unlock(&key_user_lock);
  81. user = NULL;
  82. candidate = kmalloc(sizeof(struct key_user), GFP_KERNEL);
  83. if (unlikely(!candidate))
  84. goto out;
  85. /* the allocation may have scheduled, so we need to repeat the
  86. * search lest someone else added the record whilst we were
  87. * asleep */
  88. goto try_again;
  89. }
  90. /* if we get here, then the user record still hadn't appeared on the
  91. * second pass - so we use the candidate record */
  92. atomic_set(&candidate->usage, 1);
  93. atomic_set(&candidate->nkeys, 0);
  94. atomic_set(&candidate->nikeys, 0);
  95. candidate->uid = uid;
  96. candidate->user_ns = get_user_ns(user_ns);
  97. candidate->qnkeys = 0;
  98. candidate->qnbytes = 0;
  99. spin_lock_init(&candidate->lock);
  100. mutex_init(&candidate->cons_lock);
  101. rb_link_node(&candidate->node, parent, p);
  102. rb_insert_color(&candidate->node, &key_user_tree);
  103. spin_unlock(&key_user_lock);
  104. user = candidate;
  105. goto out;
  106. /* okay - we found a user record for this UID */
  107. found:
  108. atomic_inc(&user->usage);
  109. spin_unlock(&key_user_lock);
  110. kfree(candidate);
  111. out:
  112. return user;
  113. }
  114. /*
  115. * Dispose of a user structure
  116. */
  117. void key_user_put(struct key_user *user)
  118. {
  119. if (atomic_dec_and_lock(&user->usage, &key_user_lock)) {
  120. rb_erase(&user->node, &key_user_tree);
  121. spin_unlock(&key_user_lock);
  122. put_user_ns(user->user_ns);
  123. kfree(user);
  124. }
  125. }
  126. /*
  127. * Allocate a serial number for a key. These are assigned randomly to avoid
  128. * security issues through covert channel problems.
  129. */
  130. static inline void key_alloc_serial(struct key *key)
  131. {
  132. struct rb_node *parent, **p;
  133. struct key *xkey;
  134. /* propose a random serial number and look for a hole for it in the
  135. * serial number tree */
  136. do {
  137. get_random_bytes(&key->serial, sizeof(key->serial));
  138. key->serial >>= 1; /* negative numbers are not permitted */
  139. } while (key->serial < 3);
  140. spin_lock(&key_serial_lock);
  141. attempt_insertion:
  142. parent = NULL;
  143. p = &key_serial_tree.rb_node;
  144. while (*p) {
  145. parent = *p;
  146. xkey = rb_entry(parent, struct key, serial_node);
  147. if (key->serial < xkey->serial)
  148. p = &(*p)->rb_left;
  149. else if (key->serial > xkey->serial)
  150. p = &(*p)->rb_right;
  151. else
  152. goto serial_exists;
  153. }
  154. /* we've found a suitable hole - arrange for this key to occupy it */
  155. rb_link_node(&key->serial_node, parent, p);
  156. rb_insert_color(&key->serial_node, &key_serial_tree);
  157. spin_unlock(&key_serial_lock);
  158. return;
  159. /* we found a key with the proposed serial number - walk the tree from
  160. * that point looking for the next unused serial number */
  161. serial_exists:
  162. for (;;) {
  163. key->serial++;
  164. if (key->serial < 3) {
  165. key->serial = 3;
  166. goto attempt_insertion;
  167. }
  168. parent = rb_next(parent);
  169. if (!parent)
  170. goto attempt_insertion;
  171. xkey = rb_entry(parent, struct key, serial_node);
  172. if (key->serial < xkey->serial)
  173. goto attempt_insertion;
  174. }
  175. }
  176. /**
  177. * key_alloc - Allocate a key of the specified type.
  178. * @type: The type of key to allocate.
  179. * @desc: The key description to allow the key to be searched out.
  180. * @uid: The owner of the new key.
  181. * @gid: The group ID for the new key's group permissions.
  182. * @cred: The credentials specifying UID namespace.
  183. * @perm: The permissions mask of the new key.
  184. * @flags: Flags specifying quota properties.
  185. *
  186. * Allocate a key of the specified type with the attributes given. The key is
  187. * returned in an uninstantiated state and the caller needs to instantiate the
  188. * key before returning.
  189. *
  190. * The user's key count quota is updated to reflect the creation of the key and
  191. * the user's key data quota has the default for the key type reserved. The
  192. * instantiation function should amend this as necessary. If insufficient
  193. * quota is available, -EDQUOT will be returned.
  194. *
  195. * The LSM security modules can prevent a key being created, in which case
  196. * -EACCES will be returned.
  197. *
  198. * Returns a pointer to the new key if successful and an error code otherwise.
  199. *
  200. * Note that the caller needs to ensure the key type isn't uninstantiated.
  201. * Internally this can be done by locking key_types_sem. Externally, this can
  202. * be done by either never unregistering the key type, or making sure
  203. * key_alloc() calls don't race with module unloading.
  204. */
  205. struct key *key_alloc(struct key_type *type, const char *desc,
  206. uid_t uid, gid_t gid, const struct cred *cred,
  207. key_perm_t perm, unsigned long flags)
  208. {
  209. struct key_user *user = NULL;
  210. struct key *key;
  211. size_t desclen, quotalen;
  212. int ret;
  213. key = ERR_PTR(-EINVAL);
  214. if (!desc || !*desc)
  215. goto error;
  216. if (type->vet_description) {
  217. ret = type->vet_description(desc);
  218. if (ret < 0) {
  219. key = ERR_PTR(ret);
  220. goto error;
  221. }
  222. }
  223. desclen = strlen(desc) + 1;
  224. quotalen = desclen + type->def_datalen;
  225. /* get hold of the key tracking for this user */
  226. user = key_user_lookup(uid, cred->user->user_ns);
  227. if (!user)
  228. goto no_memory_1;
  229. /* check that the user's quota permits allocation of another key and
  230. * its description */
  231. if (!(flags & KEY_ALLOC_NOT_IN_QUOTA)) {
  232. unsigned maxkeys = (uid == 0) ?
  233. key_quota_root_maxkeys : key_quota_maxkeys;
  234. unsigned maxbytes = (uid == 0) ?
  235. key_quota_root_maxbytes : key_quota_maxbytes;
  236. spin_lock(&user->lock);
  237. if (!(flags & KEY_ALLOC_QUOTA_OVERRUN)) {
  238. if (user->qnkeys + 1 >= maxkeys ||
  239. user->qnbytes + quotalen >= maxbytes ||
  240. user->qnbytes + quotalen < user->qnbytes)
  241. goto no_quota;
  242. }
  243. user->qnkeys++;
  244. user->qnbytes += quotalen;
  245. spin_unlock(&user->lock);
  246. }
  247. /* allocate and initialise the key and its description */
  248. key = kmem_cache_alloc(key_jar, GFP_KERNEL);
  249. if (!key)
  250. goto no_memory_2;
  251. if (desc) {
  252. key->description = kmemdup(desc, desclen, GFP_KERNEL);
  253. if (!key->description)
  254. goto no_memory_3;
  255. }
  256. atomic_set(&key->usage, 1);
  257. init_rwsem(&key->sem);
  258. key->type = type;
  259. key->user = user;
  260. key->quotalen = quotalen;
  261. key->datalen = type->def_datalen;
  262. key->uid = uid;
  263. key->gid = gid;
  264. key->perm = perm;
  265. key->flags = 0;
  266. key->expiry = 0;
  267. key->payload.data = NULL;
  268. key->security = NULL;
  269. if (!(flags & KEY_ALLOC_NOT_IN_QUOTA))
  270. key->flags |= 1 << KEY_FLAG_IN_QUOTA;
  271. memset(&key->type_data, 0, sizeof(key->type_data));
  272. #ifdef KEY_DEBUGGING
  273. key->magic = KEY_DEBUG_MAGIC;
  274. #endif
  275. /* let the security module know about the key */
  276. ret = security_key_alloc(key, cred, flags);
  277. if (ret < 0)
  278. goto security_error;
  279. /* publish the key by giving it a serial number */
  280. atomic_inc(&user->nkeys);
  281. key_alloc_serial(key);
  282. error:
  283. return key;
  284. security_error:
  285. kfree(key->description);
  286. kmem_cache_free(key_jar, key);
  287. if (!(flags & KEY_ALLOC_NOT_IN_QUOTA)) {
  288. spin_lock(&user->lock);
  289. user->qnkeys--;
  290. user->qnbytes -= quotalen;
  291. spin_unlock(&user->lock);
  292. }
  293. key_user_put(user);
  294. key = ERR_PTR(ret);
  295. goto error;
  296. no_memory_3:
  297. kmem_cache_free(key_jar, key);
  298. no_memory_2:
  299. if (!(flags & KEY_ALLOC_NOT_IN_QUOTA)) {
  300. spin_lock(&user->lock);
  301. user->qnkeys--;
  302. user->qnbytes -= quotalen;
  303. spin_unlock(&user->lock);
  304. }
  305. key_user_put(user);
  306. no_memory_1:
  307. key = ERR_PTR(-ENOMEM);
  308. goto error;
  309. no_quota:
  310. spin_unlock(&user->lock);
  311. key_user_put(user);
  312. key = ERR_PTR(-EDQUOT);
  313. goto error;
  314. }
  315. EXPORT_SYMBOL(key_alloc);
  316. /**
  317. * key_payload_reserve - Adjust data quota reservation for the key's payload
  318. * @key: The key to make the reservation for.
  319. * @datalen: The amount of data payload the caller now wants.
  320. *
  321. * Adjust the amount of the owning user's key data quota that a key reserves.
  322. * If the amount is increased, then -EDQUOT may be returned if there isn't
  323. * enough free quota available.
  324. *
  325. * If successful, 0 is returned.
  326. */
  327. int key_payload_reserve(struct key *key, size_t datalen)
  328. {
  329. int delta = (int)datalen - key->datalen;
  330. int ret = 0;
  331. key_check(key);
  332. /* contemplate the quota adjustment */
  333. if (delta != 0 && test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) {
  334. unsigned maxbytes = (key->user->uid == 0) ?
  335. key_quota_root_maxbytes : key_quota_maxbytes;
  336. spin_lock(&key->user->lock);
  337. if (delta > 0 &&
  338. (key->user->qnbytes + delta >= maxbytes ||
  339. key->user->qnbytes + delta < key->user->qnbytes)) {
  340. ret = -EDQUOT;
  341. }
  342. else {
  343. key->user->qnbytes += delta;
  344. key->quotalen += delta;
  345. }
  346. spin_unlock(&key->user->lock);
  347. }
  348. /* change the recorded data length if that didn't generate an error */
  349. if (ret == 0)
  350. key->datalen = datalen;
  351. return ret;
  352. }
  353. EXPORT_SYMBOL(key_payload_reserve);
  354. /*
  355. * Instantiate a key and link it into the target keyring atomically. Must be
  356. * called with the target keyring's semaphore writelocked. The target key's
  357. * semaphore need not be locked as instantiation is serialised by
  358. * key_construction_mutex.
  359. */
  360. static int __key_instantiate_and_link(struct key *key,
  361. const void *data,
  362. size_t datalen,
  363. struct key *keyring,
  364. struct key *authkey,
  365. unsigned long *_prealloc)
  366. {
  367. int ret, awaken;
  368. key_check(key);
  369. key_check(keyring);
  370. awaken = 0;
  371. ret = -EBUSY;
  372. mutex_lock(&key_construction_mutex);
  373. /* can't instantiate twice */
  374. if (!test_bit(KEY_FLAG_INSTANTIATED, &key->flags)) {
  375. /* instantiate the key */
  376. ret = key->type->instantiate(key, data, datalen);
  377. if (ret == 0) {
  378. /* mark the key as being instantiated */
  379. atomic_inc(&key->user->nikeys);
  380. set_bit(KEY_FLAG_INSTANTIATED, &key->flags);
  381. if (test_and_clear_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags))
  382. awaken = 1;
  383. /* and link it into the destination keyring */
  384. if (keyring)
  385. __key_link(keyring, key, _prealloc);
  386. /* disable the authorisation key */
  387. if (authkey)
  388. key_revoke(authkey);
  389. }
  390. }
  391. mutex_unlock(&key_construction_mutex);
  392. /* wake up anyone waiting for a key to be constructed */
  393. if (awaken)
  394. wake_up_bit(&key->flags, KEY_FLAG_USER_CONSTRUCT);
  395. return ret;
  396. }
  397. /**
  398. * key_instantiate_and_link - Instantiate a key and link it into the keyring.
  399. * @key: The key to instantiate.
  400. * @data: The data to use to instantiate the keyring.
  401. * @datalen: The length of @data.
  402. * @keyring: Keyring to create a link in on success (or NULL).
  403. * @authkey: The authorisation token permitting instantiation.
  404. *
  405. * Instantiate a key that's in the uninstantiated state using the provided data
  406. * and, if successful, link it in to the destination keyring if one is
  407. * supplied.
  408. *
  409. * If successful, 0 is returned, the authorisation token is revoked and anyone
  410. * waiting for the key is woken up. If the key was already instantiated,
  411. * -EBUSY will be returned.
  412. */
  413. int key_instantiate_and_link(struct key *key,
  414. const void *data,
  415. size_t datalen,
  416. struct key *keyring,
  417. struct key *authkey)
  418. {
  419. unsigned long prealloc;
  420. int ret;
  421. if (keyring) {
  422. ret = __key_link_begin(keyring, key->type, key->description,
  423. &prealloc);
  424. if (ret < 0)
  425. return ret;
  426. }
  427. ret = __key_instantiate_and_link(key, data, datalen, keyring, authkey,
  428. &prealloc);
  429. if (keyring)
  430. __key_link_end(keyring, key->type, prealloc);
  431. return ret;
  432. }
  433. EXPORT_SYMBOL(key_instantiate_and_link);
  434. /**
  435. * key_reject_and_link - Negatively instantiate a key and link it into the keyring.
  436. * @key: The key to instantiate.
  437. * @timeout: The timeout on the negative key.
  438. * @error: The error to return when the key is hit.
  439. * @keyring: Keyring to create a link in on success (or NULL).
  440. * @authkey: The authorisation token permitting instantiation.
  441. *
  442. * Negatively instantiate a key that's in the uninstantiated state and, if
  443. * successful, set its timeout and stored error and link it in to the
  444. * destination keyring if one is supplied. The key and any links to the key
  445. * will be automatically garbage collected after the timeout expires.
  446. *
  447. * Negative keys are used to rate limit repeated request_key() calls by causing
  448. * them to return the stored error code (typically ENOKEY) until the negative
  449. * key expires.
  450. *
  451. * If successful, 0 is returned, the authorisation token is revoked and anyone
  452. * waiting for the key is woken up. If the key was already instantiated,
  453. * -EBUSY will be returned.
  454. */
  455. int key_reject_and_link(struct key *key,
  456. unsigned timeout,
  457. unsigned error,
  458. struct key *keyring,
  459. struct key *authkey)
  460. {
  461. unsigned long prealloc;
  462. struct timespec now;
  463. int ret, awaken, link_ret = 0;
  464. key_check(key);
  465. key_check(keyring);
  466. awaken = 0;
  467. ret = -EBUSY;
  468. if (keyring)
  469. link_ret = __key_link_begin(keyring, key->type,
  470. key->description, &prealloc);
  471. mutex_lock(&key_construction_mutex);
  472. /* can't instantiate twice */
  473. if (!test_bit(KEY_FLAG_INSTANTIATED, &key->flags)) {
  474. /* mark the key as being negatively instantiated */
  475. atomic_inc(&key->user->nikeys);
  476. set_bit(KEY_FLAG_NEGATIVE, &key->flags);
  477. set_bit(KEY_FLAG_INSTANTIATED, &key->flags);
  478. key->type_data.reject_error = -error;
  479. now = current_kernel_time();
  480. key->expiry = now.tv_sec + timeout;
  481. key_schedule_gc(key->expiry + key_gc_delay);
  482. if (test_and_clear_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags))
  483. awaken = 1;
  484. ret = 0;
  485. /* and link it into the destination keyring */
  486. if (keyring && link_ret == 0)
  487. __key_link(keyring, key, &prealloc);
  488. /* disable the authorisation key */
  489. if (authkey)
  490. key_revoke(authkey);
  491. }
  492. mutex_unlock(&key_construction_mutex);
  493. if (keyring)
  494. __key_link_end(keyring, key->type, prealloc);
  495. /* wake up anyone waiting for a key to be constructed */
  496. if (awaken)
  497. wake_up_bit(&key->flags, KEY_FLAG_USER_CONSTRUCT);
  498. return ret == 0 ? link_ret : ret;
  499. }
  500. EXPORT_SYMBOL(key_reject_and_link);
  501. /*
  502. * Garbage collect keys in process context so that we don't have to disable
  503. * interrupts all over the place.
  504. *
  505. * key_put() schedules this rather than trying to do the cleanup itself, which
  506. * means key_put() doesn't have to sleep.
  507. */
  508. static void key_cleanup(struct work_struct *work)
  509. {
  510. struct rb_node *_n;
  511. struct key *key;
  512. go_again:
  513. /* look for a dead key in the tree */
  514. spin_lock(&key_serial_lock);
  515. for (_n = rb_first(&key_serial_tree); _n; _n = rb_next(_n)) {
  516. key = rb_entry(_n, struct key, serial_node);
  517. if (atomic_read(&key->usage) == 0)
  518. goto found_dead_key;
  519. }
  520. spin_unlock(&key_serial_lock);
  521. return;
  522. found_dead_key:
  523. /* we found a dead key - once we've removed it from the tree, we can
  524. * drop the lock */
  525. rb_erase(&key->serial_node, &key_serial_tree);
  526. spin_unlock(&key_serial_lock);
  527. key_check(key);
  528. security_key_free(key);
  529. /* deal with the user's key tracking and quota */
  530. if (test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) {
  531. spin_lock(&key->user->lock);
  532. key->user->qnkeys--;
  533. key->user->qnbytes -= key->quotalen;
  534. spin_unlock(&key->user->lock);
  535. }
  536. atomic_dec(&key->user->nkeys);
  537. if (test_bit(KEY_FLAG_INSTANTIATED, &key->flags))
  538. atomic_dec(&key->user->nikeys);
  539. key_user_put(key->user);
  540. /* now throw away the key memory */
  541. if (key->type->destroy)
  542. key->type->destroy(key);
  543. kfree(key->description);
  544. #ifdef KEY_DEBUGGING
  545. key->magic = KEY_DEBUG_MAGIC_X;
  546. #endif
  547. kmem_cache_free(key_jar, key);
  548. /* there may, of course, be more than one key to destroy */
  549. goto go_again;
  550. }
  551. /**
  552. * key_put - Discard a reference to a key.
  553. * @key: The key to discard a reference from.
  554. *
  555. * Discard a reference to a key, and when all the references are gone, we
  556. * schedule the cleanup task to come and pull it out of the tree in process
  557. * context at some later time.
  558. */
  559. void key_put(struct key *key)
  560. {
  561. if (key) {
  562. key_check(key);
  563. if (atomic_dec_and_test(&key->usage))
  564. schedule_work(&key_cleanup_task);
  565. }
  566. }
  567. EXPORT_SYMBOL(key_put);
  568. /*
  569. * Find a key by its serial number.
  570. */
  571. struct key *key_lookup(key_serial_t id)
  572. {
  573. struct rb_node *n;
  574. struct key *key;
  575. spin_lock(&key_serial_lock);
  576. /* search the tree for the specified key */
  577. n = key_serial_tree.rb_node;
  578. while (n) {
  579. key = rb_entry(n, struct key, serial_node);
  580. if (id < key->serial)
  581. n = n->rb_left;
  582. else if (id > key->serial)
  583. n = n->rb_right;
  584. else
  585. goto found;
  586. }
  587. not_found:
  588. key = ERR_PTR(-ENOKEY);
  589. goto error;
  590. found:
  591. /* pretend it doesn't exist if it is awaiting deletion */
  592. if (atomic_read(&key->usage) == 0)
  593. goto not_found;
  594. /* this races with key_put(), but that doesn't matter since key_put()
  595. * doesn't actually change the key
  596. */
  597. atomic_inc(&key->usage);
  598. error:
  599. spin_unlock(&key_serial_lock);
  600. return key;
  601. }
  602. /*
  603. * Find and lock the specified key type against removal.
  604. *
  605. * We return with the sem read-locked if successful. If the type wasn't
  606. * available -ENOKEY is returned instead.
  607. */
  608. struct key_type *key_type_lookup(const char *type)
  609. {
  610. struct key_type *ktype;
  611. down_read(&key_types_sem);
  612. /* look up the key type to see if it's one of the registered kernel
  613. * types */
  614. list_for_each_entry(ktype, &key_types_list, link) {
  615. if (strcmp(ktype->name, type) == 0)
  616. goto found_kernel_type;
  617. }
  618. up_read(&key_types_sem);
  619. ktype = ERR_PTR(-ENOKEY);
  620. found_kernel_type:
  621. return ktype;
  622. }
  623. /*
  624. * Unlock a key type locked by key_type_lookup().
  625. */
  626. void key_type_put(struct key_type *ktype)
  627. {
  628. up_read(&key_types_sem);
  629. }
  630. /*
  631. * Attempt to update an existing key.
  632. *
  633. * The key is given to us with an incremented refcount that we need to discard
  634. * if we get an error.
  635. */
  636. static inline key_ref_t __key_update(key_ref_t key_ref,
  637. const void *payload, size_t plen)
  638. {
  639. struct key *key = key_ref_to_ptr(key_ref);
  640. int ret;
  641. /* need write permission on the key to update it */
  642. ret = key_permission(key_ref, KEY_WRITE);
  643. if (ret < 0)
  644. goto error;
  645. ret = -EEXIST;
  646. if (!key->type->update)
  647. goto error;
  648. down_write(&key->sem);
  649. ret = key->type->update(key, payload, plen);
  650. if (ret == 0)
  651. /* updating a negative key instantiates it */
  652. clear_bit(KEY_FLAG_NEGATIVE, &key->flags);
  653. up_write(&key->sem);
  654. if (ret < 0)
  655. goto error;
  656. out:
  657. return key_ref;
  658. error:
  659. key_put(key);
  660. key_ref = ERR_PTR(ret);
  661. goto out;
  662. }
  663. /**
  664. * key_create_or_update - Update or create and instantiate a key.
  665. * @keyring_ref: A pointer to the destination keyring with possession flag.
  666. * @type: The type of key.
  667. * @description: The searchable description for the key.
  668. * @payload: The data to use to instantiate or update the key.
  669. * @plen: The length of @payload.
  670. * @perm: The permissions mask for a new key.
  671. * @flags: The quota flags for a new key.
  672. *
  673. * Search the destination keyring for a key of the same description and if one
  674. * is found, update it, otherwise create and instantiate a new one and create a
  675. * link to it from that keyring.
  676. *
  677. * If perm is KEY_PERM_UNDEF then an appropriate key permissions mask will be
  678. * concocted.
  679. *
  680. * Returns a pointer to the new key if successful, -ENODEV if the key type
  681. * wasn't available, -ENOTDIR if the keyring wasn't a keyring, -EACCES if the
  682. * caller isn't permitted to modify the keyring or the LSM did not permit
  683. * creation of the key.
  684. *
  685. * On success, the possession flag from the keyring ref will be tacked on to
  686. * the key ref before it is returned.
  687. */
  688. key_ref_t key_create_or_update(key_ref_t keyring_ref,
  689. const char *type,
  690. const char *description,
  691. const void *payload,
  692. size_t plen,
  693. key_perm_t perm,
  694. unsigned long flags)
  695. {
  696. unsigned long prealloc;
  697. const struct cred *cred = current_cred();
  698. struct key_type *ktype;
  699. struct key *keyring, *key = NULL;
  700. key_ref_t key_ref;
  701. int ret;
  702. /* look up the key type to see if it's one of the registered kernel
  703. * types */
  704. ktype = key_type_lookup(type);
  705. if (IS_ERR(ktype)) {
  706. key_ref = ERR_PTR(-ENODEV);
  707. goto error;
  708. }
  709. key_ref = ERR_PTR(-EINVAL);
  710. if (!ktype->match || !ktype->instantiate)
  711. goto error_2;
  712. keyring = key_ref_to_ptr(keyring_ref);
  713. key_check(keyring);
  714. key_ref = ERR_PTR(-ENOTDIR);
  715. if (keyring->type != &key_type_keyring)
  716. goto error_2;
  717. ret = __key_link_begin(keyring, ktype, description, &prealloc);
  718. if (ret < 0)
  719. goto error_2;
  720. /* if we're going to allocate a new key, we're going to have
  721. * to modify the keyring */
  722. ret = key_permission(keyring_ref, KEY_WRITE);
  723. if (ret < 0) {
  724. key_ref = ERR_PTR(ret);
  725. goto error_3;
  726. }
  727. /* if it's possible to update this type of key, search for an existing
  728. * key of the same type and description in the destination keyring and
  729. * update that instead if possible
  730. */
  731. if (ktype->update) {
  732. key_ref = __keyring_search_one(keyring_ref, ktype, description,
  733. 0);
  734. if (!IS_ERR(key_ref))
  735. goto found_matching_key;
  736. }
  737. /* if the client doesn't provide, decide on the permissions we want */
  738. if (perm == KEY_PERM_UNDEF) {
  739. perm = KEY_POS_VIEW | KEY_POS_SEARCH | KEY_POS_LINK | KEY_POS_SETATTR;
  740. perm |= KEY_USR_VIEW | KEY_USR_SEARCH | KEY_USR_LINK | KEY_USR_SETATTR;
  741. if (ktype->read)
  742. perm |= KEY_POS_READ | KEY_USR_READ;
  743. if (ktype == &key_type_keyring || ktype->update)
  744. perm |= KEY_USR_WRITE;
  745. }
  746. /* allocate a new key */
  747. key = key_alloc(ktype, description, cred->fsuid, cred->fsgid, cred,
  748. perm, flags);
  749. if (IS_ERR(key)) {
  750. key_ref = ERR_CAST(key);
  751. goto error_3;
  752. }
  753. /* instantiate it and link it into the target keyring */
  754. ret = __key_instantiate_and_link(key, payload, plen, keyring, NULL,
  755. &prealloc);
  756. if (ret < 0) {
  757. key_put(key);
  758. key_ref = ERR_PTR(ret);
  759. goto error_3;
  760. }
  761. key_ref = make_key_ref(key, is_key_possessed(keyring_ref));
  762. error_3:
  763. __key_link_end(keyring, ktype, prealloc);
  764. error_2:
  765. key_type_put(ktype);
  766. error:
  767. return key_ref;
  768. found_matching_key:
  769. /* we found a matching key, so we're going to try to update it
  770. * - we can drop the locks first as we have the key pinned
  771. */
  772. __key_link_end(keyring, ktype, prealloc);
  773. key_type_put(ktype);
  774. key_ref = __key_update(key_ref, payload, plen);
  775. goto error;
  776. }
  777. EXPORT_SYMBOL(key_create_or_update);
  778. /**
  779. * key_update - Update a key's contents.
  780. * @key_ref: The pointer (plus possession flag) to the key.
  781. * @payload: The data to be used to update the key.
  782. * @plen: The length of @payload.
  783. *
  784. * Attempt to update the contents of a key with the given payload data. The
  785. * caller must be granted Write permission on the key. Negative keys can be
  786. * instantiated by this method.
  787. *
  788. * Returns 0 on success, -EACCES if not permitted and -EOPNOTSUPP if the key
  789. * type does not support updating. The key type may return other errors.
  790. */
  791. int key_update(key_ref_t key_ref, const void *payload, size_t plen)
  792. {
  793. struct key *key = key_ref_to_ptr(key_ref);
  794. int ret;
  795. key_check(key);
  796. /* the key must be writable */
  797. ret = key_permission(key_ref, KEY_WRITE);
  798. if (ret < 0)
  799. goto error;
  800. /* attempt to update it if supported */
  801. ret = -EOPNOTSUPP;
  802. if (key->type->update) {
  803. down_write(&key->sem);
  804. ret = key->type->update(key, payload, plen);
  805. if (ret == 0)
  806. /* updating a negative key instantiates it */
  807. clear_bit(KEY_FLAG_NEGATIVE, &key->flags);
  808. up_write(&key->sem);
  809. }
  810. error:
  811. return ret;
  812. }
  813. EXPORT_SYMBOL(key_update);
  814. /**
  815. * key_revoke - Revoke a key.
  816. * @key: The key to be revoked.
  817. *
  818. * Mark a key as being revoked and ask the type to free up its resources. The
  819. * revocation timeout is set and the key and all its links will be
  820. * automatically garbage collected after key_gc_delay amount of time if they
  821. * are not manually dealt with first.
  822. */
  823. void key_revoke(struct key *key)
  824. {
  825. struct timespec now;
  826. time_t time;
  827. key_check(key);
  828. /* make sure no one's trying to change or use the key when we mark it
  829. * - we tell lockdep that we might nest because we might be revoking an
  830. * authorisation key whilst holding the sem on a key we've just
  831. * instantiated
  832. */
  833. down_write_nested(&key->sem, 1);
  834. if (!test_and_set_bit(KEY_FLAG_REVOKED, &key->flags) &&
  835. key->type->revoke)
  836. key->type->revoke(key);
  837. /* set the death time to no more than the expiry time */
  838. now = current_kernel_time();
  839. time = now.tv_sec;
  840. if (key->revoked_at == 0 || key->revoked_at > time) {
  841. key->revoked_at = time;
  842. key_schedule_gc(key->revoked_at + key_gc_delay);
  843. }
  844. up_write(&key->sem);
  845. }
  846. EXPORT_SYMBOL(key_revoke);
  847. /**
  848. * register_key_type - Register a type of key.
  849. * @ktype: The new key type.
  850. *
  851. * Register a new key type.
  852. *
  853. * Returns 0 on success or -EEXIST if a type of this name already exists.
  854. */
  855. int register_key_type(struct key_type *ktype)
  856. {
  857. struct key_type *p;
  858. int ret;
  859. ret = -EEXIST;
  860. down_write(&key_types_sem);
  861. /* disallow key types with the same name */
  862. list_for_each_entry(p, &key_types_list, link) {
  863. if (strcmp(p->name, ktype->name) == 0)
  864. goto out;
  865. }
  866. /* store the type */
  867. list_add(&ktype->link, &key_types_list);
  868. ret = 0;
  869. out:
  870. up_write(&key_types_sem);
  871. return ret;
  872. }
  873. EXPORT_SYMBOL(register_key_type);
  874. /**
  875. * unregister_key_type - Unregister a type of key.
  876. * @ktype: The key type.
  877. *
  878. * Unregister a key type and mark all the extant keys of this type as dead.
  879. * Those keys of this type are then destroyed to get rid of their payloads and
  880. * they and their links will be garbage collected as soon as possible.
  881. */
  882. void unregister_key_type(struct key_type *ktype)
  883. {
  884. struct rb_node *_n;
  885. struct key *key;
  886. down_write(&key_types_sem);
  887. /* withdraw the key type */
  888. list_del_init(&ktype->link);
  889. /* mark all the keys of this type dead */
  890. spin_lock(&key_serial_lock);
  891. for (_n = rb_first(&key_serial_tree); _n; _n = rb_next(_n)) {
  892. key = rb_entry(_n, struct key, serial_node);
  893. if (key->type == ktype) {
  894. key->type = &key_type_dead;
  895. set_bit(KEY_FLAG_DEAD, &key->flags);
  896. }
  897. }
  898. spin_unlock(&key_serial_lock);
  899. /* make sure everyone revalidates their keys */
  900. synchronize_rcu();
  901. /* we should now be able to destroy the payloads of all the keys of
  902. * this type with impunity */
  903. spin_lock(&key_serial_lock);
  904. for (_n = rb_first(&key_serial_tree); _n; _n = rb_next(_n)) {
  905. key = rb_entry(_n, struct key, serial_node);
  906. if (key->type == ktype) {
  907. if (ktype->destroy)
  908. ktype->destroy(key);
  909. memset(&key->payload, KEY_DESTROY, sizeof(key->payload));
  910. }
  911. }
  912. spin_unlock(&key_serial_lock);
  913. up_write(&key_types_sem);
  914. key_schedule_gc(0);
  915. }
  916. EXPORT_SYMBOL(unregister_key_type);
  917. /*
  918. * Initialise the key management state.
  919. */
  920. void __init key_init(void)
  921. {
  922. /* allocate a slab in which we can store keys */
  923. key_jar = kmem_cache_create("key_jar", sizeof(struct key),
  924. 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
  925. /* add the special key types */
  926. list_add_tail(&key_type_keyring.link, &key_types_list);
  927. list_add_tail(&key_type_dead.link, &key_types_list);
  928. list_add_tail(&key_type_user.link, &key_types_list);
  929. /* record the root user tracking */
  930. rb_link_node(&root_key_user.node,
  931. NULL,
  932. &key_user_tree.rb_node);
  933. rb_insert_color(&root_key_user.node,
  934. &key_user_tree);
  935. }