key.c 28 KB

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