key.c 27 KB

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