key.c 27 KB

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