key.c 28 KB

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