key.c 26 KB

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